blob: 069d79926e2b3cdd4eea77ba6f6b686441cd941e [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 Kivity4d758342013-01-19 19:51:55 +0200444static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *));
445
Avi Kivityb7d491e2013-01-04 16:18:49 +0200446#define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t"
447#define FOP_RET "ret \n\t"
448
449#define FOP_START(op) \
450 extern void em_##op(struct fastop *fake); \
451 asm(".pushsection .text, \"ax\" \n\t" \
452 ".global em_" #op " \n\t" \
453 FOP_ALIGN \
454 "em_" #op ": \n\t"
455
456#define FOP_END \
457 ".popsection")
458
Avi Kivity0bdea062013-01-19 19:51:50 +0200459#define FOPNOP() FOP_ALIGN FOP_RET
460
Avi Kivityb7d491e2013-01-04 16:18:49 +0200461#define FOP1E(op, dst) \
462 FOP_ALIGN #op " %" #dst " \n\t" FOP_RET
463
464#define FASTOP1(op) \
465 FOP_START(op) \
466 FOP1E(op##b, al) \
467 FOP1E(op##w, ax) \
468 FOP1E(op##l, eax) \
469 ON64(FOP1E(op##q, rax)) \
470 FOP_END
471
Avi Kivityf7857f32013-01-04 16:18:53 +0200472#define FOP2E(op, dst, src) \
473 FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET
474
475#define FASTOP2(op) \
476 FOP_START(op) \
477 FOP2E(op##b, al, bl) \
478 FOP2E(op##w, ax, bx) \
479 FOP2E(op##l, eax, ebx) \
480 ON64(FOP2E(op##q, rax, rbx)) \
481 FOP_END
482
Avi Kivity11c363b2013-01-19 19:51:54 +0200483/* 2 operand, word only */
484#define FASTOP2W(op) \
485 FOP_START(op) \
486 FOPNOP() \
487 FOP2E(op##w, ax, bx) \
488 FOP2E(op##l, eax, ebx) \
489 ON64(FOP2E(op##q, rax, rbx)) \
490 FOP_END
491
Avi Kivity007a3b52013-01-19 19:51:51 +0200492/* 2 operand, src is CL */
493#define FASTOP2CL(op) \
494 FOP_START(op) \
495 FOP2E(op##b, al, cl) \
496 FOP2E(op##w, ax, cl) \
497 FOP2E(op##l, eax, cl) \
498 ON64(FOP2E(op##q, rax, cl)) \
499 FOP_END
500
Avi Kivity0bdea062013-01-19 19:51:50 +0200501#define FOP3E(op, dst, src, src2) \
502 FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
503
504/* 3-operand, word-only, src2=cl */
505#define FASTOP3WCL(op) \
506 FOP_START(op) \
507 FOPNOP() \
508 FOP3E(op##w, ax, bx, cl) \
509 FOP3E(op##l, eax, ebx, cl) \
510 ON64(FOP3E(op##q, rax, rbx, cl)) \
511 FOP_END
512
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200513/* Special case for SETcc - 1 instruction per cc */
514#define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t"
515
516FOP_START(setcc)
517FOP_SETCC(seto)
518FOP_SETCC(setno)
519FOP_SETCC(setc)
520FOP_SETCC(setnc)
521FOP_SETCC(setz)
522FOP_SETCC(setnz)
523FOP_SETCC(setbe)
524FOP_SETCC(setnbe)
525FOP_SETCC(sets)
526FOP_SETCC(setns)
527FOP_SETCC(setp)
528FOP_SETCC(setnp)
529FOP_SETCC(setl)
530FOP_SETCC(setnl)
531FOP_SETCC(setle)
532FOP_SETCC(setnle)
533FOP_END;
534
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300535#define __emulate_1op_rax_rdx(ctxt, _op, _suffix, _ex) \
Avi Kivityf6b35972010-08-26 11:59:00 +0300536 do { \
537 unsigned long _tmp; \
Avi Kivitydd856ef2012-08-27 23:46:17 +0300538 ulong *rax = reg_rmw((ctxt), VCPU_REGS_RAX); \
539 ulong *rdx = reg_rmw((ctxt), VCPU_REGS_RDX); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300540 \
541 __asm__ __volatile__ ( \
542 _PRE_EFLAGS("0", "5", "1") \
543 "1: \n\t" \
544 _op _suffix " %6; " \
545 "2: \n\t" \
546 _POST_EFLAGS("0", "5", "1") \
547 ".pushsection .fixup,\"ax\" \n\t" \
548 "3: movb $1, %4 \n\t" \
549 "jmp 2b \n\t" \
550 ".popsection \n\t" \
551 _ASM_EXTABLE(1b, 3b) \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300552 : "=m" ((ctxt)->eflags), "=&r" (_tmp), \
553 "+a" (*rax), "+d" (*rdx), "+qm"(_ex) \
H. Peter Anvincb7cb282012-11-21 14:41:21 -0800554 : "i" (EFLAGS_MASK), "m" ((ctxt)->src.val)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300555 } while (0)
556
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300557/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300558#define emulate_1op_rax_rdx(ctxt, _op, _ex) \
Avi Kivity72952612011-04-20 13:12:27 +0300559 do { \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300560 switch((ctxt)->src.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300561 case 1: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300562 __emulate_1op_rax_rdx(ctxt, _op, "b", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300563 break; \
564 case 2: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300565 __emulate_1op_rax_rdx(ctxt, _op, "w", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300566 break; \
567 case 4: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300568 __emulate_1op_rax_rdx(ctxt, _op, "l", _ex); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300569 break; \
570 case 8: ON64( \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300571 __emulate_1op_rax_rdx(ctxt, _op, "q", _ex)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300572 break; \
573 } \
574 } while (0)
575
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200576static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
577 enum x86_intercept intercept,
578 enum x86_intercept_stage stage)
579{
580 struct x86_instruction_info info = {
581 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300582 .rep_prefix = ctxt->rep_prefix,
583 .modrm_mod = ctxt->modrm_mod,
584 .modrm_reg = ctxt->modrm_reg,
585 .modrm_rm = ctxt->modrm_rm,
586 .src_val = ctxt->src.val64,
587 .src_bytes = ctxt->src.bytes,
588 .dst_bytes = ctxt->dst.bytes,
589 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200590 .next_rip = ctxt->eip,
591 };
592
Avi Kivity29535382011-04-20 13:37:53 +0300593 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200594}
595
Avi Kivityf47cfa32012-06-07 17:49:24 +0300596static void assign_masked(ulong *dest, ulong src, ulong mask)
597{
598 *dest = (*dest & ~mask) | (src & mask);
599}
600
Avi Kivity9dac77f2011-06-01 15:34:25 +0300601static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800602{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300603 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800604}
605
Avi Kivityf47cfa32012-06-07 17:49:24 +0300606static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
607{
608 u16 sel;
609 struct desc_struct ss;
610
611 if (ctxt->mode == X86EMUL_MODE_PROT64)
612 return ~0UL;
613 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
614 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
615}
616
Avi Kivity612e89f2012-06-12 20:03:23 +0300617static int stack_size(struct x86_emulate_ctxt *ctxt)
618{
619 return (__fls(stack_mask(ctxt)) + 1) >> 3;
620}
621
Avi Kivity6aa8b732006-12-10 02:21:36 -0800622/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800623static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300624address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800625{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300626 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800627 return reg;
628 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300629 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800630}
631
632static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300633register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800634{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300635 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800636}
637
Avi Kivity5ad105e2012-08-19 14:34:31 +0300638static void masked_increment(ulong *reg, ulong mask, int inc)
639{
640 assign_masked(reg, *reg + inc, mask);
641}
642
Harvey Harrison7a9572752008-02-19 07:40:41 -0800643static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300644register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800645{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300646 ulong mask;
647
Avi Kivity9dac77f2011-06-01 15:34:25 +0300648 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300649 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800650 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300651 mask = ad_mask(ctxt);
652 masked_increment(reg, mask, inc);
653}
654
655static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
656{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300657 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800658}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800659
Avi Kivity9dac77f2011-06-01 15:34:25 +0300660static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800661{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300662 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800663}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300664
Avi Kivity56697682011-04-03 14:08:51 +0300665static u32 desc_limit_scaled(struct desc_struct *desc)
666{
667 u32 limit = get_desc_limit(desc);
668
669 return desc->g ? (limit << 12) | 0xfff : limit;
670}
671
Avi Kivity9dac77f2011-06-01 15:34:25 +0300672static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300673{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300674 ctxt->has_seg_override = true;
675 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300676}
677
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900678static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300679{
680 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
681 return 0;
682
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900683 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300684}
685
Avi Kivity9dac77f2011-06-01 15:34:25 +0300686static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300687{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300688 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300689 return 0;
690
Avi Kivity9dac77f2011-06-01 15:34:25 +0300691 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300692}
693
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200694static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
695 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300696{
Avi Kivityda9cb572010-11-22 17:53:21 +0200697 ctxt->exception.vector = vec;
698 ctxt->exception.error_code = error;
699 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200700 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300701}
702
Joerg Roedel3b88e412011-04-04 12:39:29 +0200703static int emulate_db(struct x86_emulate_ctxt *ctxt)
704{
705 return emulate_exception(ctxt, DB_VECTOR, 0, false);
706}
707
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200708static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300709{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200710 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300711}
712
Avi Kivity618ff152011-04-03 12:32:09 +0300713static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
714{
715 return emulate_exception(ctxt, SS_VECTOR, err, true);
716}
717
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200718static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300719{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200720 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300721}
722
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200723static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300724{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200725 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300726}
727
Avi Kivity34d1f492010-08-26 11:59:01 +0300728static int emulate_de(struct x86_emulate_ctxt *ctxt)
729{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200730 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300731}
732
Avi Kivity12537912011-03-29 11:41:27 +0200733static int emulate_nm(struct x86_emulate_ctxt *ctxt)
734{
735 return emulate_exception(ctxt, NM_VECTOR, 0, false);
736}
737
Avi Kivity1aa36612011-04-27 13:20:30 +0300738static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
739{
740 u16 selector;
741 struct desc_struct desc;
742
743 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
744 return selector;
745}
746
747static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
748 unsigned seg)
749{
750 u16 dummy;
751 u32 base3;
752 struct desc_struct desc;
753
754 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
755 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
756}
757
Avi Kivity1c11b372012-04-09 18:39:59 +0300758/*
759 * x86 defines three classes of vector instructions: explicitly
760 * aligned, explicitly unaligned, and the rest, which change behaviour
761 * depending on whether they're AVX encoded or not.
762 *
763 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
764 * subject to the same check.
765 */
766static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
767{
768 if (likely(size < 16))
769 return false;
770
771 if (ctxt->d & Aligned)
772 return true;
773 else if (ctxt->d & Unaligned)
774 return false;
775 else if (ctxt->d & Avx)
776 return false;
777 else
778 return true;
779}
780
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400781static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300782 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400783 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300784 ulong *linear)
785{
Avi Kivity618ff152011-04-03 12:32:09 +0300786 struct desc_struct desc;
787 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300788 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300789 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300790 u16 sel;
Gleb Natapov3a78a4f2012-12-20 16:57:42 +0200791 unsigned cpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300792
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900793 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300794 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300795 case X86EMUL_MODE_PROT64:
796 if (((signed long)la << 16) >> 16 != la)
797 return emulate_gp(ctxt, 0);
798 break;
799 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300800 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
801 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300802 if (!usable)
803 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200804 /* code segment in protected mode or read-only data segment */
805 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
806 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300807 goto bad;
808 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400809 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300810 goto bad;
811 lim = desc_limit_scaled(&desc);
812 if ((desc.type & 8) || !(desc.type & 4)) {
813 /* expand-up segment */
814 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
815 goto bad;
816 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800817 /* expand-down segment */
Avi Kivity618ff152011-04-03 12:32:09 +0300818 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
819 goto bad;
820 lim = desc.d ? 0xffffffff : 0xffff;
821 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
822 goto bad;
823 }
Avi Kivity717746e2011-04-20 13:37:53 +0300824 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity618ff152011-04-03 12:32:09 +0300825 if (!(desc.type & 8)) {
826 /* data segment */
827 if (cpl > desc.dpl)
828 goto bad;
829 } else if ((desc.type & 8) && !(desc.type & 4)) {
830 /* nonconforming code segment */
831 if (cpl != desc.dpl)
832 goto bad;
833 } else if ((desc.type & 8) && (desc.type & 4)) {
834 /* conforming code segment */
835 if (cpl < desc.dpl)
836 goto bad;
837 }
838 break;
839 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300840 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300841 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300842 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
843 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300844 *linear = la;
845 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300846bad:
847 if (addr.seg == VCPU_SREG_SS)
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300848 return emulate_ss(ctxt, sel);
Avi Kivity618ff152011-04-03 12:32:09 +0300849 else
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300850 return emulate_gp(ctxt, sel);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300851}
852
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400853static int linearize(struct x86_emulate_ctxt *ctxt,
854 struct segmented_address addr,
855 unsigned size, bool write,
856 ulong *linear)
857{
858 return __linearize(ctxt, addr, size, write, false, linear);
859}
860
861
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200862static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
863 struct segmented_address addr,
864 void *data,
865 unsigned size)
866{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200867 int rc;
868 ulong linear;
869
Avi Kivity83b87952011-04-03 11:31:19 +0300870 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200871 if (rc != X86EMUL_CONTINUE)
872 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300873 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200874}
875
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900876/*
877 * Fetch the next byte of the instruction being emulated which is pointed to
878 * by ctxt->_eip, then increment ctxt->_eip.
879 *
880 * Also prefetch the remaining bytes of the instruction without crossing page
881 * boundary if they are not in fetch_cache yet.
882 */
883static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200884{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300885 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200886 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300887 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200888
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900889 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400890 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900891 struct segmented_address addr = { .seg = VCPU_SREG_CS,
892 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300893 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900894 size = min(15UL - cur_size,
895 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400896 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900897 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400898 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900899 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
900 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900901 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200902 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300903 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200904 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900905 *dest = fc->data[ctxt->_eip - fc->start];
906 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900907 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200908}
909
910static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900911 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200912{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900913 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200914
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200915 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900916 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200917 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200918 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900919 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900920 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200921 return rc;
922 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900923 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200924}
925
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900926/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900927#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900928({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900929 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900930 if (rc != X86EMUL_CONTINUE) \
931 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900932 (_type)_x; \
933})
934
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900935#define insn_fetch_arr(_arr, _size, _ctxt) \
936({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900937 if (rc != X86EMUL_CONTINUE) \
938 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900939})
940
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000941/*
942 * Given the 'reg' portion of a ModRM byte, and a register block, return a
943 * pointer into the block that addresses the relevant register.
944 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
945 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300946static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000947 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800948{
949 void *p;
950
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300952 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
953 else
954 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955 return p;
956}
957
958static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200959 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960 u16 *size, unsigned long *address, int op_bytes)
961{
962 int rc;
963
964 if (op_bytes == 2)
965 op_bytes = 3;
966 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200967 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900968 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200970 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200971 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972 return rc;
973}
974
Avi Kivity34b77652013-01-19 19:51:56 +0200975FASTOP2(add);
976FASTOP2(or);
977FASTOP2(adc);
978FASTOP2(sbb);
979FASTOP2(and);
980FASTOP2(sub);
981FASTOP2(xor);
982FASTOP2(cmp);
983FASTOP2(test);
984
985FASTOP3WCL(shld);
986FASTOP3WCL(shrd);
987
988FASTOP2W(imul);
989
990FASTOP1(not);
991FASTOP1(neg);
992FASTOP1(inc);
993FASTOP1(dec);
994
995FASTOP2CL(rol);
996FASTOP2CL(ror);
997FASTOP2CL(rcl);
998FASTOP2CL(rcr);
999FASTOP2CL(shl);
1000FASTOP2CL(shr);
1001FASTOP2CL(sar);
1002
1003FASTOP2W(bsf);
1004FASTOP2W(bsr);
1005FASTOP2W(bt);
1006FASTOP2W(bts);
1007FASTOP2W(btr);
1008FASTOP2W(btc);
1009
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001010static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001011{
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001012 u8 rc;
1013 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001014
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001015 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +02001016 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001017 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
1018 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001019}
1020
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001021static void fetch_register_operand(struct operand *op)
1022{
1023 switch (op->bytes) {
1024 case 1:
1025 op->val = *(u8 *)op->addr.reg;
1026 break;
1027 case 2:
1028 op->val = *(u16 *)op->addr.reg;
1029 break;
1030 case 4:
1031 op->val = *(u32 *)op->addr.reg;
1032 break;
1033 case 8:
1034 op->val = *(u64 *)op->addr.reg;
1035 break;
1036 }
1037}
1038
Avi Kivity12537912011-03-29 11:41:27 +02001039static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
1040{
1041 ctxt->ops->get_fpu(ctxt);
1042 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001043 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
1044 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
1045 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
1046 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
1047 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
1048 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
1049 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
1050 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001051#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001052 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
1053 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
1054 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
1055 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
1056 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
1057 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
1058 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
1059 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001060#endif
1061 default: BUG();
1062 }
1063 ctxt->ops->put_fpu(ctxt);
1064}
1065
1066static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
1067 int reg)
1068{
1069 ctxt->ops->get_fpu(ctxt);
1070 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001071 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
1072 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
1073 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
1074 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
1075 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
1076 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
1077 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
1078 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001079#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001080 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
1081 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
1082 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
1083 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
1084 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
1085 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
1086 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
1087 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001088#endif
1089 default: BUG();
1090 }
1091 ctxt->ops->put_fpu(ctxt);
1092}
1093
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001094static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1095{
1096 ctxt->ops->get_fpu(ctxt);
1097 switch (reg) {
1098 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1099 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1100 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1101 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1102 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1103 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1104 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1105 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1106 default: BUG();
1107 }
1108 ctxt->ops->put_fpu(ctxt);
1109}
1110
1111static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1112{
1113 ctxt->ops->get_fpu(ctxt);
1114 switch (reg) {
1115 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1116 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1117 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1118 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1119 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1120 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1121 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1122 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1123 default: BUG();
1124 }
1125 ctxt->ops->put_fpu(ctxt);
1126}
1127
Gleb Natapov045a2822012-12-20 16:57:43 +02001128static int em_fninit(struct x86_emulate_ctxt *ctxt)
1129{
1130 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1131 return emulate_nm(ctxt);
1132
1133 ctxt->ops->get_fpu(ctxt);
1134 asm volatile("fninit");
1135 ctxt->ops->put_fpu(ctxt);
1136 return X86EMUL_CONTINUE;
1137}
1138
1139static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1140{
1141 u16 fcw;
1142
1143 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1144 return emulate_nm(ctxt);
1145
1146 ctxt->ops->get_fpu(ctxt);
1147 asm volatile("fnstcw %0": "+m"(fcw));
1148 ctxt->ops->put_fpu(ctxt);
1149
1150 /* force 2 byte destination */
1151 ctxt->dst.bytes = 2;
1152 ctxt->dst.val = fcw;
1153
1154 return X86EMUL_CONTINUE;
1155}
1156
1157static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1158{
1159 u16 fsw;
1160
1161 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1162 return emulate_nm(ctxt);
1163
1164 ctxt->ops->get_fpu(ctxt);
1165 asm volatile("fnstsw %0": "+m"(fsw));
1166 ctxt->ops->put_fpu(ctxt);
1167
1168 /* force 2 byte destination */
1169 ctxt->dst.bytes = 2;
1170 ctxt->dst.val = fsw;
1171
1172 return X86EMUL_CONTINUE;
1173}
1174
Avi Kivity12537912011-03-29 11:41:27 +02001175static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001176 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001177{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001178 unsigned reg = ctxt->modrm_reg;
1179 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +02001180
Avi Kivity9dac77f2011-06-01 15:34:25 +03001181 if (!(ctxt->d & ModRM))
1182 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +02001183
Avi Kivity9dac77f2011-06-01 15:34:25 +03001184 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001185 op->type = OP_XMM;
1186 op->bytes = 16;
1187 op->addr.xmm = reg;
1188 read_sse_reg(ctxt, &op->vec_val, reg);
1189 return;
1190 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001191 if (ctxt->d & Mmx) {
1192 reg &= 7;
1193 op->type = OP_MM;
1194 op->bytes = 8;
1195 op->addr.mm = reg;
1196 return;
1197 }
Avi Kivity12537912011-03-29 11:41:27 +02001198
Avi Kivity3c118e22007-10-31 10:27:04 +02001199 op->type = OP_REG;
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001200 if (ctxt->d & ByteOp) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001201 op->addr.reg = decode_register(ctxt, reg, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +02001202 op->bytes = 1;
1203 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001204 op->addr.reg = decode_register(ctxt, reg, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001205 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +02001206 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001207 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001208 op->orig_val = op->val;
1209}
1210
Avi Kivitya6e34072012-06-10 17:15:39 +03001211static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1212{
1213 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1214 ctxt->modrm_seg = VCPU_SREG_SS;
1215}
1216
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001217static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001218 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001219{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001220 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001221 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001222 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001223 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001224
Avi Kivity9dac77f2011-06-01 15:34:25 +03001225 if (ctxt->rex_prefix) {
1226 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
1227 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
1228 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001229 }
1230
Avi Kivity9dac77f2011-06-01 15:34:25 +03001231 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
1232 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
1233 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
1234 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001235
Avi Kivity9dac77f2011-06-01 15:34:25 +03001236 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001237 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001238 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001239 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm, ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001240 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001241 op->type = OP_XMM;
1242 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001243 op->addr.xmm = ctxt->modrm_rm;
1244 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001245 return rc;
1246 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001247 if (ctxt->d & Mmx) {
1248 op->type = OP_MM;
1249 op->bytes = 8;
1250 op->addr.xmm = ctxt->modrm_rm & 7;
1251 return rc;
1252 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001253 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001254 return rc;
1255 }
1256
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001257 op->type = OP_MEM;
1258
Avi Kivity9dac77f2011-06-01 15:34:25 +03001259 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001260 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1261 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1262 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1263 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001264
1265 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001266 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001267 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001268 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001269 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001270 break;
1271 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001272 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001273 break;
1274 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001275 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001276 break;
1277 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001278 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001279 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001280 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001281 break;
1282 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001283 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001284 break;
1285 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001286 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001287 break;
1288 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001289 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001290 break;
1291 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001292 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001293 break;
1294 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001295 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001296 break;
1297 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001298 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001299 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001300 break;
1301 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001302 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001303 break;
1304 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001305 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1306 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1307 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001308 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001309 } else {
1310 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001311 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001312 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001313 index_reg |= (sib >> 3) & 7;
1314 base_reg |= sib & 7;
1315 scale = sib >> 6;
1316
Avi Kivity9dac77f2011-06-01 15:34:25 +03001317 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001318 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001319 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001320 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001321 adjust_modrm_seg(ctxt, base_reg);
1322 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001323 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001324 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001325 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001326 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001327 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001328 } else {
1329 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001330 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001331 adjust_modrm_seg(ctxt, base_reg);
1332 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001333 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001334 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001335 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001336 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001337 break;
1338 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001339 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001340 break;
1341 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001342 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001343 break;
1344 }
1345 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001346 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001347done:
1348 return rc;
1349}
1350
1351static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001352 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001353{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001354 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001355
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001356 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001357 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001358 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001359 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001360 break;
1361 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001362 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001363 break;
1364 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001365 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001366 break;
1367 }
1368done:
1369 return rc;
1370}
1371
Avi Kivity9dac77f2011-06-01 15:34:25 +03001372static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001373{
Sheng Yang7129eec2010-09-28 16:33:32 +08001374 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001375
Avi Kivity9dac77f2011-06-01 15:34:25 +03001376 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1377 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001378
Avi Kivity9dac77f2011-06-01 15:34:25 +03001379 if (ctxt->src.bytes == 2)
1380 sv = (s16)ctxt->src.val & (s16)mask;
1381 else if (ctxt->src.bytes == 4)
1382 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001383
Avi Kivity9dac77f2011-06-01 15:34:25 +03001384 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001385 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001386
1387 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001388 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001389}
1390
Gleb Natapov9de41572010-04-28 19:15:22 +03001391static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001392 unsigned long addr, void *dest, unsigned size)
1393{
1394 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001395 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001396
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001397 if (mc->pos < mc->end)
1398 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001399
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001400 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001401
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001402 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1403 &ctxt->exception);
1404 if (rc != X86EMUL_CONTINUE)
1405 return rc;
1406
1407 mc->end += size;
1408
1409read_cached:
1410 memcpy(dest, mc->data + mc->pos, size);
1411 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001412 return X86EMUL_CONTINUE;
1413}
1414
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001415static int segmented_read(struct x86_emulate_ctxt *ctxt,
1416 struct segmented_address addr,
1417 void *data,
1418 unsigned size)
1419{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001420 int rc;
1421 ulong linear;
1422
Avi Kivity83b87952011-04-03 11:31:19 +03001423 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001424 if (rc != X86EMUL_CONTINUE)
1425 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001426 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001427}
1428
1429static int segmented_write(struct x86_emulate_ctxt *ctxt,
1430 struct segmented_address addr,
1431 const void *data,
1432 unsigned size)
1433{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001434 int rc;
1435 ulong linear;
1436
Avi Kivity83b87952011-04-03 11:31:19 +03001437 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001438 if (rc != X86EMUL_CONTINUE)
1439 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001440 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1441 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001442}
1443
1444static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1445 struct segmented_address addr,
1446 const void *orig_data, const void *data,
1447 unsigned size)
1448{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001449 int rc;
1450 ulong linear;
1451
Avi Kivity83b87952011-04-03 11:31:19 +03001452 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001453 if (rc != X86EMUL_CONTINUE)
1454 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001455 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1456 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001457}
1458
Gleb Natapov7b262e92010-03-18 15:20:27 +02001459static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001460 unsigned int size, unsigned short port,
1461 void *dest)
1462{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001463 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001464
1465 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001466 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001467 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001468 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001469 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001470 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1471 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Gleb Natapov7b262e92010-03-18 15:20:27 +02001472 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1473 count);
1474 if (n == 0)
1475 n = 1;
1476 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001477 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001478 return 0;
1479 rc->end = n * size;
1480 }
1481
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001482 if (ctxt->rep_prefix && !(ctxt->eflags & EFLG_DF)) {
1483 ctxt->dst.data = rc->data + rc->pos;
1484 ctxt->dst.type = OP_MEM_STR;
1485 ctxt->dst.count = (rc->end - rc->pos) / size;
1486 rc->pos = rc->end;
1487 } else {
1488 memcpy(dest, rc->data + rc->pos, size);
1489 rc->pos += size;
1490 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001491 return 1;
1492}
1493
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001494static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1495 u16 index, struct desc_struct *desc)
1496{
1497 struct desc_ptr dt;
1498 ulong addr;
1499
1500 ctxt->ops->get_idt(ctxt, &dt);
1501
1502 if (dt.size < index * 8 + 7)
1503 return emulate_gp(ctxt, index << 3 | 0x2);
1504
1505 addr = dt.address + index * 8;
1506 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1507 &ctxt->exception);
1508}
1509
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001510static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001511 u16 selector, struct desc_ptr *dt)
1512{
Mathias Krause0225fb52012-08-30 01:30:16 +02001513 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001514
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001515 if (selector & 1 << 2) {
1516 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001517 u16 sel;
1518
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001519 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001520 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001521 return;
1522
1523 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1524 dt->address = get_desc_base(&desc);
1525 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001526 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001527}
1528
1529/* allowed just for 8 bytes segments */
1530static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001531 u16 selector, struct desc_struct *desc,
1532 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001533{
1534 struct desc_ptr dt;
1535 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001536 ulong addr;
1537
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001538 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001539
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001540 if (dt.size < index * 8 + 7)
1541 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001542
Avi Kivitye9194642012-06-13 16:29:39 +03001543 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001544 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1545 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001546}
1547
1548/* allowed just for 8 bytes segments */
1549static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001550 u16 selector, struct desc_struct *desc)
1551{
1552 struct desc_ptr dt;
1553 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001554 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001555
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001556 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001557
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001558 if (dt.size < index * 8 + 7)
1559 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001560
1561 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001562 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1563 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001564}
1565
Gleb Natapov5601d052011-03-07 14:55:06 +02001566/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001567static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001568 u16 selector, int seg)
1569{
Avi Kivity869be992012-06-13 16:30:53 +03001570 struct desc_struct seg_desc, old_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001571 u8 dpl, rpl, cpl;
1572 unsigned err_vec = GP_VECTOR;
1573 u32 err_code = 0;
1574 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001575 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001576 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001577 u16 dummy;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001578
1579 memset(&seg_desc, 0, sizeof seg_desc);
1580
Kevin Wolff8da94e2013-04-11 14:06:03 +02001581 if (ctxt->mode == X86EMUL_MODE_REAL) {
1582 /* set real mode segment descriptor (keep limit etc. for
1583 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001584 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001585 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001586 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001587 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1588 /* VM86 needs a clean new segment descriptor */
1589 set_desc_base(&seg_desc, selector << 4);
1590 set_desc_limit(&seg_desc, 0xffff);
1591 seg_desc.type = 3;
1592 seg_desc.p = 1;
1593 seg_desc.s = 1;
1594 seg_desc.dpl = 3;
1595 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001596 }
1597
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001598 rpl = selector & 3;
1599 cpl = ctxt->ops->cpl(ctxt);
1600
1601 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1602 if ((seg == VCPU_SREG_CS
1603 || (seg == VCPU_SREG_SS
1604 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1605 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001606 && null_selector)
1607 goto exception;
1608
1609 /* TR should be in GDT only */
1610 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1611 goto exception;
1612
1613 if (null_selector) /* for NULL selector skip all following checks */
1614 goto load;
1615
Avi Kivitye9194642012-06-13 16:29:39 +03001616 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001617 if (ret != X86EMUL_CONTINUE)
1618 return ret;
1619
1620 err_code = selector & 0xfffc;
1621 err_vec = GP_VECTOR;
1622
Guo Chaofc058682012-06-28 15:19:51 +08001623 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001624 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1625 goto exception;
1626
1627 if (!seg_desc.p) {
1628 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1629 goto exception;
1630 }
1631
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001632 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001633
1634 switch (seg) {
1635 case VCPU_SREG_SS:
1636 /*
1637 * segment is not a writable data segment or segment
1638 * selector's RPL != CPL or segment selector's RPL != CPL
1639 */
1640 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1641 goto exception;
1642 break;
1643 case VCPU_SREG_CS:
1644 if (!(seg_desc.type & 8))
1645 goto exception;
1646
1647 if (seg_desc.type & 4) {
1648 /* conforming */
1649 if (dpl > cpl)
1650 goto exception;
1651 } else {
1652 /* nonconforming */
1653 if (rpl > cpl || dpl != cpl)
1654 goto exception;
1655 }
1656 /* CS(RPL) <- CPL */
1657 selector = (selector & 0xfffc) | cpl;
1658 break;
1659 case VCPU_SREG_TR:
1660 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1661 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001662 old_desc = seg_desc;
1663 seg_desc.type |= 2; /* busy */
1664 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1665 sizeof(seg_desc), &ctxt->exception);
1666 if (ret != X86EMUL_CONTINUE)
1667 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001668 break;
1669 case VCPU_SREG_LDTR:
1670 if (seg_desc.s || seg_desc.type != 2)
1671 goto exception;
1672 break;
1673 default: /* DS, ES, FS, or GS */
1674 /*
1675 * segment is not a data or readable code segment or
1676 * ((segment is a data or nonconforming code segment)
1677 * and (both RPL and CPL > DPL))
1678 */
1679 if ((seg_desc.type & 0xa) == 0x8 ||
1680 (((seg_desc.type & 0xc) != 0xc) &&
1681 (rpl > dpl && cpl > dpl)))
1682 goto exception;
1683 break;
1684 }
1685
1686 if (seg_desc.s) {
1687 /* mark segment as accessed */
1688 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001689 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001690 if (ret != X86EMUL_CONTINUE)
1691 return ret;
1692 }
1693load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001694 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001695 return X86EMUL_CONTINUE;
1696exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001697 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001698 return X86EMUL_PROPAGATE_FAULT;
1699}
1700
Wei Yongjun31be40b2010-08-17 09:17:30 +08001701static void write_register_operand(struct operand *op)
1702{
1703 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1704 switch (op->bytes) {
1705 case 1:
1706 *(u8 *)op->addr.reg = (u8)op->val;
1707 break;
1708 case 2:
1709 *(u16 *)op->addr.reg = (u16)op->val;
1710 break;
1711 case 4:
1712 *op->addr.reg = (u32)op->val;
1713 break; /* 64b: zero-extend */
1714 case 8:
1715 *op->addr.reg = op->val;
1716 break;
1717 }
1718}
1719
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001720static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001721{
1722 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001723
Avi Kivityb6744dc2013-01-04 16:18:50 +02001724 if (ctxt->d & NoWrite)
1725 return X86EMUL_CONTINUE;
1726
Avi Kivity9dac77f2011-06-01 15:34:25 +03001727 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001728 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001729 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001730 break;
1731 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001732 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001733 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001734 ctxt->dst.addr.mem,
1735 &ctxt->dst.orig_val,
1736 &ctxt->dst.val,
1737 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001738 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001739 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001740 ctxt->dst.addr.mem,
1741 &ctxt->dst.val,
1742 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001743 if (rc != X86EMUL_CONTINUE)
1744 return rc;
1745 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001746 case OP_MEM_STR:
1747 rc = segmented_write(ctxt,
1748 ctxt->dst.addr.mem,
1749 ctxt->dst.data,
1750 ctxt->dst.bytes * ctxt->dst.count);
1751 if (rc != X86EMUL_CONTINUE)
1752 return rc;
1753 break;
Avi Kivity12537912011-03-29 11:41:27 +02001754 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001755 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001756 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001757 case OP_MM:
1758 write_mmx_reg(ctxt, &ctxt->dst.mm_val, ctxt->dst.addr.mm);
1759 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001760 case OP_NONE:
1761 /* no writeback */
1762 break;
1763 default:
1764 break;
1765 }
1766 return X86EMUL_CONTINUE;
1767}
1768
Avi Kivity51ddff52012-06-12 20:19:40 +03001769static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001770{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001771 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001772
Avi Kivity5ad105e2012-08-19 14:34:31 +03001773 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001774 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001775 addr.seg = VCPU_SREG_SS;
1776
Avi Kivity51ddff52012-06-12 20:19:40 +03001777 return segmented_write(ctxt, addr, data, bytes);
1778}
1779
1780static int em_push(struct x86_emulate_ctxt *ctxt)
1781{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001782 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001783 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001784 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001785}
1786
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001787static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001788 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001789{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001790 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001791 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001792
Avi Kivitydd856ef2012-08-27 23:46:17 +03001793 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001794 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001795 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001796 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001797 return rc;
1798
Avi Kivity5ad105e2012-08-19 14:34:31 +03001799 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001800 return rc;
1801}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001802
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001803static int em_pop(struct x86_emulate_ctxt *ctxt)
1804{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001805 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001806}
1807
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001808static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001809 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001810{
1811 int rc;
1812 unsigned long val, change_mask;
1813 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001814 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001815
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001816 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001817 if (rc != X86EMUL_CONTINUE)
1818 return rc;
1819
1820 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1821 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1822
1823 switch(ctxt->mode) {
1824 case X86EMUL_MODE_PROT64:
1825 case X86EMUL_MODE_PROT32:
1826 case X86EMUL_MODE_PROT16:
1827 if (cpl == 0)
1828 change_mask |= EFLG_IOPL;
1829 if (cpl <= iopl)
1830 change_mask |= EFLG_IF;
1831 break;
1832 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001833 if (iopl < 3)
1834 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001835 change_mask |= EFLG_IF;
1836 break;
1837 default: /* real mode */
1838 change_mask |= (EFLG_IOPL | EFLG_IF);
1839 break;
1840 }
1841
1842 *(unsigned long *)dest =
1843 (ctxt->eflags & ~change_mask) | (val & change_mask);
1844
1845 return rc;
1846}
1847
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001848static int em_popf(struct x86_emulate_ctxt *ctxt)
1849{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001850 ctxt->dst.type = OP_REG;
1851 ctxt->dst.addr.reg = &ctxt->eflags;
1852 ctxt->dst.bytes = ctxt->op_bytes;
1853 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001854}
1855
Avi Kivity612e89f2012-06-12 20:03:23 +03001856static int em_enter(struct x86_emulate_ctxt *ctxt)
1857{
1858 int rc;
1859 unsigned frame_size = ctxt->src.val;
1860 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001861 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001862
1863 if (nesting_level)
1864 return X86EMUL_UNHANDLEABLE;
1865
Avi Kivitydd856ef2012-08-27 23:46:17 +03001866 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1867 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001868 if (rc != X86EMUL_CONTINUE)
1869 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001870 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001871 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001872 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1873 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001874 stack_mask(ctxt));
1875 return X86EMUL_CONTINUE;
1876}
1877
Avi Kivityf47cfa32012-06-07 17:49:24 +03001878static int em_leave(struct x86_emulate_ctxt *ctxt)
1879{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001880 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001881 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001882 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001883}
1884
Avi Kivity1cd196e2011-09-13 10:45:51 +03001885static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001886{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001887 int seg = ctxt->src2.val;
1888
Avi Kivity9dac77f2011-06-01 15:34:25 +03001889 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001890
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001891 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001892}
1893
Avi Kivity1cd196e2011-09-13 10:45:51 +03001894static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001895{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001896 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001897 unsigned long selector;
1898 int rc;
1899
Avi Kivity9dac77f2011-06-01 15:34:25 +03001900 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001901 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001902 return rc;
1903
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001904 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001905 return rc;
1906}
1907
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001908static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001909{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001910 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001911 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001912 int reg = VCPU_REGS_RAX;
1913
1914 while (reg <= VCPU_REGS_RDI) {
1915 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001916 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001917
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001918 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001919 if (rc != X86EMUL_CONTINUE)
1920 return rc;
1921
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001922 ++reg;
1923 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001924
Wei Yongjunc37eda12010-06-15 09:03:33 +08001925 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001926}
1927
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001928static int em_pushf(struct x86_emulate_ctxt *ctxt)
1929{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001930 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001931 return em_push(ctxt);
1932}
1933
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001934static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001935{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001936 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001937 int reg = VCPU_REGS_RDI;
1938
1939 while (reg >= VCPU_REGS_RAX) {
1940 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001941 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001942 --reg;
1943 }
1944
Avi Kivitydd856ef2012-08-27 23:46:17 +03001945 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001946 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001947 break;
1948 --reg;
1949 }
1950 return rc;
1951}
1952
Avi Kivitydd856ef2012-08-27 23:46:17 +03001953static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001954{
Mathias Krause0225fb52012-08-30 01:30:16 +02001955 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001956 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001957 struct desc_ptr dt;
1958 gva_t cs_addr;
1959 gva_t eip_addr;
1960 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001961
1962 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001963 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001964 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001965 if (rc != X86EMUL_CONTINUE)
1966 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001967
1968 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1969
Avi Kivity9dac77f2011-06-01 15:34:25 +03001970 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001971 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001972 if (rc != X86EMUL_CONTINUE)
1973 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001974
Avi Kivity9dac77f2011-06-01 15:34:25 +03001975 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001976 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001977 if (rc != X86EMUL_CONTINUE)
1978 return rc;
1979
Avi Kivity4bff1e862011-04-20 13:37:53 +03001980 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001981
1982 eip_addr = dt.address + (irq << 2);
1983 cs_addr = dt.address + (irq << 2) + 2;
1984
Avi Kivity0f65dd72011-04-20 13:37:53 +03001985 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001986 if (rc != X86EMUL_CONTINUE)
1987 return rc;
1988
Avi Kivity0f65dd72011-04-20 13:37:53 +03001989 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001990 if (rc != X86EMUL_CONTINUE)
1991 return rc;
1992
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001993 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001994 if (rc != X86EMUL_CONTINUE)
1995 return rc;
1996
Avi Kivity9dac77f2011-06-01 15:34:25 +03001997 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001998
1999 return rc;
2000}
2001
Avi Kivitydd856ef2012-08-27 23:46:17 +03002002int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
2003{
2004 int rc;
2005
2006 invalidate_registers(ctxt);
2007 rc = __emulate_int_real(ctxt, irq);
2008 if (rc == X86EMUL_CONTINUE)
2009 writeback_registers(ctxt);
2010 return rc;
2011}
2012
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002013static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002014{
2015 switch(ctxt->mode) {
2016 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03002017 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002018 case X86EMUL_MODE_VM86:
2019 case X86EMUL_MODE_PROT16:
2020 case X86EMUL_MODE_PROT32:
2021 case X86EMUL_MODE_PROT64:
2022 default:
2023 /* Protected mode interrupts unimplemented yet */
2024 return X86EMUL_UNHANDLEABLE;
2025 }
2026}
2027
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002028static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002029{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002030 int rc = X86EMUL_CONTINUE;
2031 unsigned long temp_eip = 0;
2032 unsigned long temp_eflags = 0;
2033 unsigned long cs = 0;
2034 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
2035 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
2036 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
2037 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
2038
2039 /* TODO: Add stack limit check */
2040
Avi Kivity9dac77f2011-06-01 15:34:25 +03002041 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002042
2043 if (rc != X86EMUL_CONTINUE)
2044 return rc;
2045
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002046 if (temp_eip & ~0xffff)
2047 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002048
Avi Kivity9dac77f2011-06-01 15:34:25 +03002049 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002050
2051 if (rc != X86EMUL_CONTINUE)
2052 return rc;
2053
Avi Kivity9dac77f2011-06-01 15:34:25 +03002054 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002055
2056 if (rc != X86EMUL_CONTINUE)
2057 return rc;
2058
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002059 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002060
2061 if (rc != X86EMUL_CONTINUE)
2062 return rc;
2063
Avi Kivity9dac77f2011-06-01 15:34:25 +03002064 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002065
2066
Avi Kivity9dac77f2011-06-01 15:34:25 +03002067 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002068 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03002069 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002070 ctxt->eflags &= ~0xffff;
2071 ctxt->eflags |= temp_eflags;
2072 }
2073
2074 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
2075 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
2076
2077 return rc;
2078}
2079
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002080static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002081{
2082 switch(ctxt->mode) {
2083 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002084 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002085 case X86EMUL_MODE_VM86:
2086 case X86EMUL_MODE_PROT16:
2087 case X86EMUL_MODE_PROT32:
2088 case X86EMUL_MODE_PROT64:
2089 default:
2090 /* iret from protected mode unimplemented yet */
2091 return X86EMUL_UNHANDLEABLE;
2092 }
2093}
2094
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002095static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2096{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002097 int rc;
2098 unsigned short sel;
2099
Avi Kivity9dac77f2011-06-01 15:34:25 +03002100 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002101
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002102 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002103 if (rc != X86EMUL_CONTINUE)
2104 return rc;
2105
Avi Kivity9dac77f2011-06-01 15:34:25 +03002106 ctxt->_eip = 0;
2107 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002108 return X86EMUL_CONTINUE;
2109}
2110
Avi Kivity3329ece2011-09-13 10:45:39 +03002111static int em_mul_ex(struct x86_emulate_ctxt *ctxt)
2112{
2113 u8 ex = 0;
2114
2115 emulate_1op_rax_rdx(ctxt, "mul", ex);
2116 return X86EMUL_CONTINUE;
2117}
2118
2119static int em_imul_ex(struct x86_emulate_ctxt *ctxt)
2120{
2121 u8 ex = 0;
2122
2123 emulate_1op_rax_rdx(ctxt, "imul", ex);
2124 return X86EMUL_CONTINUE;
2125}
2126
2127static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002128{
Avi Kivity34d1f492010-08-26 11:59:01 +03002129 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002130
Avi Kivity3329ece2011-09-13 10:45:39 +03002131 emulate_1op_rax_rdx(ctxt, "div", de);
2132 if (de)
2133 return emulate_de(ctxt);
2134 return X86EMUL_CONTINUE;
2135}
2136
2137static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
2138{
2139 u8 de = 0;
2140
2141 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03002142 if (de)
2143 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03002144 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002145}
2146
Takuya Yoshikawa51187682011-05-02 02:29:17 +09002147static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002148{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002149 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002150
Avi Kivity9dac77f2011-06-01 15:34:25 +03002151 switch (ctxt->modrm_reg) {
Mohammed Gamald19292e2008-09-08 21:47:19 +03002152 case 2: /* call near abs */ {
2153 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002154 old_eip = ctxt->_eip;
2155 ctxt->_eip = ctxt->src.val;
2156 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002157 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03002158 break;
2159 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002160 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002161 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002162 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002163 case 5: /* jmp far */
2164 rc = em_jmp_far(ctxt);
2165 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002166 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002167 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002168 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002169 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002170 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002171}
2172
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002173static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002174{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002175 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002176
Avi Kivitydd856ef2012-08-27 23:46:17 +03002177 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2178 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2179 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2180 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002181 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002182 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002183 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2184 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002185
Laurent Vivier05f086f2007-09-24 11:10:55 +02002186 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002187 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002188 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002189}
2190
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002191static int em_ret(struct x86_emulate_ctxt *ctxt)
2192{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002193 ctxt->dst.type = OP_REG;
2194 ctxt->dst.addr.reg = &ctxt->_eip;
2195 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002196 return em_pop(ctxt);
2197}
2198
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002199static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002200{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002201 int rc;
2202 unsigned long cs;
2203
Avi Kivity9dac77f2011-06-01 15:34:25 +03002204 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002205 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002206 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002207 if (ctxt->op_bytes == 4)
2208 ctxt->_eip = (u32)ctxt->_eip;
2209 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002210 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002211 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002212 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002213 return rc;
2214}
2215
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002216static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2217{
2218 /* Save real source value, then compare EAX against destination. */
2219 ctxt->src.orig_val = ctxt->src.val;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002220 ctxt->src.val = reg_read(ctxt, VCPU_REGS_RAX);
Avi Kivity158de572013-01-19 19:51:57 +02002221 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002222
2223 if (ctxt->eflags & EFLG_ZF) {
2224 /* Success: write back to memory. */
2225 ctxt->dst.val = ctxt->src.orig_val;
2226 } else {
2227 /* Failure: write the value we saw to EAX. */
2228 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002229 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002230 }
2231 return X86EMUL_CONTINUE;
2232}
2233
Avi Kivityd4b43252011-09-13 10:45:50 +03002234static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002235{
Avi Kivityd4b43252011-09-13 10:45:50 +03002236 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002237 unsigned short sel;
2238 int rc;
2239
Avi Kivity9dac77f2011-06-01 15:34:25 +03002240 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002241
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002242 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002243 if (rc != X86EMUL_CONTINUE)
2244 return rc;
2245
Avi Kivity9dac77f2011-06-01 15:34:25 +03002246 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002247 return rc;
2248}
2249
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002250static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002251setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002252 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002253{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002254 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002255 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002256 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002257 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002258 cs->type = 0x0b; /* Read, Execute, Accessed */
2259 cs->s = 1;
2260 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002261 cs->p = 1;
2262 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002263 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002264
Gleb Natapov79168fd2010-04-28 19:15:30 +03002265 set_desc_base(ss, 0); /* flat segment */
2266 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002267 ss->g = 1; /* 4kb granularity */
2268 ss->s = 1;
2269 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002270 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002271 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002272 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002273 ss->l = 0;
2274 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002275}
2276
Avi Kivity1a18a692012-02-01 12:23:21 +02002277static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2278{
2279 u32 eax, ebx, ecx, edx;
2280
2281 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002282 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2283 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002284 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2285 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2286}
2287
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002288static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2289{
Mathias Krause0225fb52012-08-30 01:30:16 +02002290 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002291 u32 eax, ebx, ecx, edx;
2292
2293 /*
2294 * syscall should always be enabled in longmode - so only become
2295 * vendor specific (cpuid) if other modes are active...
2296 */
2297 if (ctxt->mode == X86EMUL_MODE_PROT64)
2298 return true;
2299
2300 eax = 0x00000000;
2301 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002302 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2303 /*
2304 * Intel ("GenuineIntel")
2305 * remark: Intel CPUs only support "syscall" in 64bit
2306 * longmode. Also an 64bit guest with a
2307 * 32bit compat-app running will #UD !! While this
2308 * behaviour can be fixed (by emulating) into AMD
2309 * response - CPUs of AMD can't behave like Intel.
2310 */
2311 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2312 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2313 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2314 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002315
Avi Kivity0017f932012-06-07 14:10:16 +03002316 /* AMD ("AuthenticAMD") */
2317 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2318 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2319 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2320 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002321
Avi Kivity0017f932012-06-07 14:10:16 +03002322 /* AMD ("AMDisbetter!") */
2323 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2324 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2325 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2326 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002327
2328 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2329 return false;
2330}
2331
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002332static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002333{
Mathias Krause0225fb52012-08-30 01:30:16 +02002334 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002335 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002336 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002337 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002338 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002339
2340 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002341 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002342 ctxt->mode == X86EMUL_MODE_VM86)
2343 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002344
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002345 if (!(em_syscall_is_enabled(ctxt)))
2346 return emulate_ud(ctxt);
2347
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002348 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002349 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002350
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002351 if (!(efer & EFER_SCE))
2352 return emulate_ud(ctxt);
2353
Avi Kivity717746e2011-04-20 13:37:53 +03002354 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002355 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002356 cs_sel = (u16)(msr_data & 0xfffc);
2357 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002358
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002359 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002360 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002361 cs.l = 1;
2362 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002363 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2364 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002365
Avi Kivitydd856ef2012-08-27 23:46:17 +03002366 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002367 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002368#ifdef CONFIG_X86_64
Avi Kivitydd856ef2012-08-27 23:46:17 +03002369 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002370
Avi Kivity717746e2011-04-20 13:37:53 +03002371 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002372 ctxt->mode == X86EMUL_MODE_PROT64 ?
2373 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002374 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002375
Avi Kivity717746e2011-04-20 13:37:53 +03002376 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002377 ctxt->eflags &= ~(msr_data | EFLG_RF);
2378#endif
2379 } else {
2380 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002381 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002382 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002383
2384 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2385 }
2386
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002387 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002388}
2389
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002390static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002391{
Mathias Krause0225fb52012-08-30 01:30:16 +02002392 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002393 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002394 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002395 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002396 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002397
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002398 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002399 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002400 if (ctxt->mode == X86EMUL_MODE_REAL)
2401 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002402
Avi Kivity1a18a692012-02-01 12:23:21 +02002403 /*
2404 * Not recognized on AMD in compat mode (but is recognized in legacy
2405 * mode).
2406 */
2407 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2408 && !vendor_intel(ctxt))
2409 return emulate_ud(ctxt);
2410
Andre Przywara8c604352009-06-18 12:56:01 +02002411 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2412 * Therefore, we inject an #UD.
2413 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002414 if (ctxt->mode == X86EMUL_MODE_PROT64)
2415 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002416
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002417 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002418
Avi Kivity717746e2011-04-20 13:37:53 +03002419 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002420 switch (ctxt->mode) {
2421 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002422 if ((msr_data & 0xfffc) == 0x0)
2423 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002424 break;
2425 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002426 if (msr_data == 0x0)
2427 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002428 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002429 default:
2430 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002431 }
2432
2433 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002434 cs_sel = (u16)msr_data;
2435 cs_sel &= ~SELECTOR_RPL_MASK;
2436 ss_sel = cs_sel + 8;
2437 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002438 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002439 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002440 cs.l = 1;
2441 }
2442
Avi Kivity1aa36612011-04-27 13:20:30 +03002443 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2444 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002445
Avi Kivity717746e2011-04-20 13:37:53 +03002446 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002447 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002448
Avi Kivity717746e2011-04-20 13:37:53 +03002449 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002450 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002451
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002452 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002453}
2454
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002455static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002456{
Mathias Krause0225fb52012-08-30 01:30:16 +02002457 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002458 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002459 u64 msr_data;
2460 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002461 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002462
Gleb Natapova0044752010-02-10 14:21:31 +02002463 /* inject #GP if in real mode or Virtual 8086 mode */
2464 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002465 ctxt->mode == X86EMUL_MODE_VM86)
2466 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002467
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002468 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002469
Avi Kivity9dac77f2011-06-01 15:34:25 +03002470 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002471 usermode = X86EMUL_MODE_PROT64;
2472 else
2473 usermode = X86EMUL_MODE_PROT32;
2474
2475 cs.dpl = 3;
2476 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002477 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002478 switch (usermode) {
2479 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002480 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002481 if ((msr_data & 0xfffc) == 0x0)
2482 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002483 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002484 break;
2485 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002486 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002487 if (msr_data == 0x0)
2488 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002489 ss_sel = cs_sel + 8;
2490 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002491 cs.l = 1;
2492 break;
2493 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002494 cs_sel |= SELECTOR_RPL_MASK;
2495 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002496
Avi Kivity1aa36612011-04-27 13:20:30 +03002497 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2498 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002499
Avi Kivitydd856ef2012-08-27 23:46:17 +03002500 ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX);
2501 *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX);
Andre Przywara4668f052009-06-18 12:56:02 +02002502
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002503 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002504}
2505
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002506static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002507{
2508 int iopl;
2509 if (ctxt->mode == X86EMUL_MODE_REAL)
2510 return false;
2511 if (ctxt->mode == X86EMUL_MODE_VM86)
2512 return true;
2513 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002514 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002515}
2516
2517static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002518 u16 port, u16 len)
2519{
Mathias Krause0225fb52012-08-30 01:30:16 +02002520 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002521 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002522 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002523 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002524 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002525 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002526 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002527
Avi Kivity1aa36612011-04-27 13:20:30 +03002528 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002529 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002530 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002531 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002532 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002533 base = get_desc_base(&tr_seg);
2534#ifdef CONFIG_X86_64
2535 base |= ((u64)base3) << 32;
2536#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002537 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002538 if (r != X86EMUL_CONTINUE)
2539 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002540 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002541 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002542 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002543 if (r != X86EMUL_CONTINUE)
2544 return false;
2545 if ((perm >> bit_idx) & mask)
2546 return false;
2547 return true;
2548}
2549
2550static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002551 u16 port, u16 len)
2552{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002553 if (ctxt->perm_ok)
2554 return true;
2555
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002556 if (emulator_bad_iopl(ctxt))
2557 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002558 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002559
2560 ctxt->perm_ok = true;
2561
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002562 return true;
2563}
2564
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002565static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002566 struct tss_segment_16 *tss)
2567{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002568 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002569 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002570 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2571 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2572 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2573 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2574 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2575 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2576 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2577 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002578
Avi Kivity1aa36612011-04-27 13:20:30 +03002579 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2580 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2581 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2582 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2583 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002584}
2585
2586static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002587 struct tss_segment_16 *tss)
2588{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002589 int ret;
2590
Avi Kivity9dac77f2011-06-01 15:34:25 +03002591 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002592 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002593 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2594 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2595 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2596 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2597 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2598 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2599 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2600 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002601
2602 /*
2603 * SDM says that segment selectors are loaded before segment
2604 * descriptors
2605 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002606 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2607 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2608 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2609 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2610 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002611
2612 /*
Guo Chaofc058682012-06-28 15:19:51 +08002613 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002614 * it is handled in a context of new task
2615 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002616 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002617 if (ret != X86EMUL_CONTINUE)
2618 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002619 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002620 if (ret != X86EMUL_CONTINUE)
2621 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002622 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002623 if (ret != X86EMUL_CONTINUE)
2624 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002625 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002626 if (ret != X86EMUL_CONTINUE)
2627 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002628 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002629 if (ret != X86EMUL_CONTINUE)
2630 return ret;
2631
2632 return X86EMUL_CONTINUE;
2633}
2634
2635static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002636 u16 tss_selector, u16 old_tss_sel,
2637 ulong old_tss_base, struct desc_struct *new_desc)
2638{
Mathias Krause0225fb52012-08-30 01:30:16 +02002639 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002640 struct tss_segment_16 tss_seg;
2641 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002642 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002643
Avi Kivity0f65dd72011-04-20 13:37:53 +03002644 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002645 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002646 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002647 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002648 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002649
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002650 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002651
Avi Kivity0f65dd72011-04-20 13:37:53 +03002652 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002653 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002654 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002655 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002656 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002657
Avi Kivity0f65dd72011-04-20 13:37:53 +03002658 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002659 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002660 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002661 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002662 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002663
2664 if (old_tss_sel != 0xffff) {
2665 tss_seg.prev_task_link = old_tss_sel;
2666
Avi Kivity0f65dd72011-04-20 13:37:53 +03002667 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002668 &tss_seg.prev_task_link,
2669 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002670 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002671 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002672 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002673 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002674 }
2675
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002676 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002677}
2678
2679static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002680 struct tss_segment_32 *tss)
2681{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002682 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002683 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002684 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002685 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2686 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2687 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2688 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2689 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2690 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2691 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2692 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002693
Avi Kivity1aa36612011-04-27 13:20:30 +03002694 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2695 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2696 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2697 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2698 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2699 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2700 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002701}
2702
2703static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002704 struct tss_segment_32 *tss)
2705{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002706 int ret;
2707
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002708 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002709 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002710 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002711 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002712
2713 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002714 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2715 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2716 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2717 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2718 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2719 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2720 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2721 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002722
2723 /*
2724 * SDM says that segment selectors are loaded before segment
2725 * descriptors
2726 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002727 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2728 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2729 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2730 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2731 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2732 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2733 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002734
2735 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002736 * If we're switching between Protected Mode and VM86, we need to make
2737 * sure to update the mode before loading the segment descriptors so
2738 * that the selectors are interpreted correctly.
2739 *
2740 * Need to get rflags to the vcpu struct immediately because it
2741 * influences the CPL which is checked at least when loading the segment
2742 * descriptors and when pushing an error code to the new kernel stack.
2743 *
2744 * TODO Introduce a separate ctxt->ops->set_cpl callback
2745 */
2746 if (ctxt->eflags & X86_EFLAGS_VM)
2747 ctxt->mode = X86EMUL_MODE_VM86;
2748 else
2749 ctxt->mode = X86EMUL_MODE_PROT32;
2750
2751 ctxt->ops->set_rflags(ctxt, ctxt->eflags);
2752
2753 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002754 * Now load segment descriptors. If fault happenes at this stage
2755 * it is handled in a context of new task
2756 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002757 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002758 if (ret != X86EMUL_CONTINUE)
2759 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002760 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002761 if (ret != X86EMUL_CONTINUE)
2762 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002763 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002764 if (ret != X86EMUL_CONTINUE)
2765 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002766 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002767 if (ret != X86EMUL_CONTINUE)
2768 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002769 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002770 if (ret != X86EMUL_CONTINUE)
2771 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002772 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002773 if (ret != X86EMUL_CONTINUE)
2774 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002775 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002776 if (ret != X86EMUL_CONTINUE)
2777 return ret;
2778
2779 return X86EMUL_CONTINUE;
2780}
2781
2782static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002783 u16 tss_selector, u16 old_tss_sel,
2784 ulong old_tss_base, struct desc_struct *new_desc)
2785{
Mathias Krause0225fb52012-08-30 01:30:16 +02002786 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002787 struct tss_segment_32 tss_seg;
2788 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002789 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002790
Avi Kivity0f65dd72011-04-20 13:37:53 +03002791 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002792 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002793 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002794 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002795 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002796
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002797 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002798
Avi Kivity0f65dd72011-04-20 13:37:53 +03002799 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002800 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002801 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002802 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002803 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002804
Avi Kivity0f65dd72011-04-20 13:37:53 +03002805 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002806 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002807 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002808 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002809 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002810
2811 if (old_tss_sel != 0xffff) {
2812 tss_seg.prev_task_link = old_tss_sel;
2813
Avi Kivity0f65dd72011-04-20 13:37:53 +03002814 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002815 &tss_seg.prev_task_link,
2816 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002817 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002818 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002819 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002820 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002821 }
2822
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002823 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002824}
2825
2826static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002827 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002828 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002829{
Mathias Krause0225fb52012-08-30 01:30:16 +02002830 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002831 struct desc_struct curr_tss_desc, next_tss_desc;
2832 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002833 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002834 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002835 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002836 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002837 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002838
2839 /* FIXME: old_tss_base == ~0 ? */
2840
Avi Kivitye9194642012-06-13 16:29:39 +03002841 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002842 if (ret != X86EMUL_CONTINUE)
2843 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002844 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002845 if (ret != X86EMUL_CONTINUE)
2846 return ret;
2847
2848 /* FIXME: check that next_tss_desc is tss */
2849
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002850 /*
2851 * Check privileges. The three cases are task switch caused by...
2852 *
2853 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2854 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002855 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002856 */
2857 if (reason == TASK_SWITCH_GATE) {
2858 if (idt_index != -1) {
2859 /* Software interrupts */
2860 struct desc_struct task_gate_desc;
2861 int dpl;
2862
2863 ret = read_interrupt_descriptor(ctxt, idt_index,
2864 &task_gate_desc);
2865 if (ret != X86EMUL_CONTINUE)
2866 return ret;
2867
2868 dpl = task_gate_desc.dpl;
2869 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2870 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2871 }
2872 } else if (reason != TASK_SWITCH_IRET) {
2873 int dpl = next_tss_desc.dpl;
2874 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2875 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002876 }
2877
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002878
Gleb Natapovceffb452010-03-18 15:20:19 +02002879 desc_limit = desc_limit_scaled(&next_tss_desc);
2880 if (!next_tss_desc.p ||
2881 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2882 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002883 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002884 return X86EMUL_PROPAGATE_FAULT;
2885 }
2886
2887 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2888 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002889 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002890 }
2891
2892 if (reason == TASK_SWITCH_IRET)
2893 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2894
2895 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002896 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002897 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2898 old_tss_sel = 0xffff;
2899
2900 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002901 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002902 old_tss_base, &next_tss_desc);
2903 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002904 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002905 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002906 if (ret != X86EMUL_CONTINUE)
2907 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002908
2909 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2910 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2911
2912 if (reason != TASK_SWITCH_IRET) {
2913 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002914 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002915 }
2916
Avi Kivity717746e2011-04-20 13:37:53 +03002917 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002918 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002919
Jan Kiszkae269fb22010-04-14 15:51:09 +02002920 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002921 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2922 ctxt->lock_prefix = 0;
2923 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002924 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002925 }
2926
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002927 return ret;
2928}
2929
2930int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002931 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002932 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002933{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002934 int rc;
2935
Avi Kivitydd856ef2012-08-27 23:46:17 +03002936 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002937 ctxt->_eip = ctxt->eip;
2938 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002939
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002940 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002941 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002942
Avi Kivitydd856ef2012-08-27 23:46:17 +03002943 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002944 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002945 writeback_registers(ctxt);
2946 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002947
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002948 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002949}
2950
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002951static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2952 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002953{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002954 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002955
Avi Kivitydd856ef2012-08-27 23:46:17 +03002956 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2957 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002958}
2959
Avi Kivity7af04fc2010-08-18 14:16:35 +03002960static int em_das(struct x86_emulate_ctxt *ctxt)
2961{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002962 u8 al, old_al;
2963 bool af, cf, old_cf;
2964
2965 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002966 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002967
2968 old_al = al;
2969 old_cf = cf;
2970 cf = false;
2971 af = ctxt->eflags & X86_EFLAGS_AF;
2972 if ((al & 0x0f) > 9 || af) {
2973 al -= 6;
2974 cf = old_cf | (al >= 250);
2975 af = true;
2976 } else {
2977 af = false;
2978 }
2979 if (old_al > 0x99 || old_cf) {
2980 al -= 0x60;
2981 cf = true;
2982 }
2983
Avi Kivity9dac77f2011-06-01 15:34:25 +03002984 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002985 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002986 ctxt->src.type = OP_IMM;
2987 ctxt->src.val = 0;
2988 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02002989 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03002990 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2991 if (cf)
2992 ctxt->eflags |= X86_EFLAGS_CF;
2993 if (af)
2994 ctxt->eflags |= X86_EFLAGS_AF;
2995 return X86EMUL_CONTINUE;
2996}
2997
Gleb Natapov7f662272012-12-10 11:42:30 +02002998static int em_aad(struct x86_emulate_ctxt *ctxt)
2999{
3000 u8 al = ctxt->dst.val & 0xff;
3001 u8 ah = (ctxt->dst.val >> 8) & 0xff;
3002
3003 al = (al + (ah * ctxt->src.val)) & 0xff;
3004
3005 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
3006
Gleb Natapovf583c292013-02-13 17:50:39 +02003007 /* Set PF, ZF, SF */
3008 ctxt->src.type = OP_IMM;
3009 ctxt->src.val = 0;
3010 ctxt->src.bytes = 1;
3011 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02003012
3013 return X86EMUL_CONTINUE;
3014}
3015
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003016static int em_call(struct x86_emulate_ctxt *ctxt)
3017{
3018 long rel = ctxt->src.val;
3019
3020 ctxt->src.val = (unsigned long)ctxt->_eip;
3021 jmp_rel(ctxt, rel);
3022 return em_push(ctxt);
3023}
3024
Avi Kivity0ef753b2010-08-18 14:51:45 +03003025static int em_call_far(struct x86_emulate_ctxt *ctxt)
3026{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003027 u16 sel, old_cs;
3028 ulong old_eip;
3029 int rc;
3030
Avi Kivity1aa36612011-04-27 13:20:30 +03003031 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003032 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003033
Avi Kivity9dac77f2011-06-01 15:34:25 +03003034 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003035 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03003036 return X86EMUL_CONTINUE;
3037
Avi Kivity9dac77f2011-06-01 15:34:25 +03003038 ctxt->_eip = 0;
3039 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003040
Avi Kivity9dac77f2011-06-01 15:34:25 +03003041 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003042 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003043 if (rc != X86EMUL_CONTINUE)
3044 return rc;
3045
Avi Kivity9dac77f2011-06-01 15:34:25 +03003046 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003047 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003048}
3049
Avi Kivity40ece7c2010-08-18 15:12:09 +03003050static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3051{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003052 int rc;
3053
Avi Kivity9dac77f2011-06-01 15:34:25 +03003054 ctxt->dst.type = OP_REG;
3055 ctxt->dst.addr.reg = &ctxt->_eip;
3056 ctxt->dst.bytes = ctxt->op_bytes;
3057 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003058 if (rc != X86EMUL_CONTINUE)
3059 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003060 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003061 return X86EMUL_CONTINUE;
3062}
3063
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003064static int em_xchg(struct x86_emulate_ctxt *ctxt)
3065{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003066 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003067 ctxt->src.val = ctxt->dst.val;
3068 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003069
3070 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003071 ctxt->dst.val = ctxt->src.orig_val;
3072 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003073 return X86EMUL_CONTINUE;
3074}
3075
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003076static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3077{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003078 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003079 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003080}
3081
Avi Kivity61429142010-08-19 15:13:00 +03003082static int em_cwd(struct x86_emulate_ctxt *ctxt)
3083{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003084 ctxt->dst.type = OP_REG;
3085 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003086 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003087 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003088
3089 return X86EMUL_CONTINUE;
3090}
3091
Avi Kivity48bb5d32010-08-18 18:54:34 +03003092static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3093{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003094 u64 tsc = 0;
3095
Avi Kivity717746e2011-04-20 13:37:53 +03003096 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003097 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3098 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003099 return X86EMUL_CONTINUE;
3100}
3101
Avi Kivity222d21a2011-11-10 14:57:30 +02003102static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3103{
3104 u64 pmc;
3105
Avi Kivitydd856ef2012-08-27 23:46:17 +03003106 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003107 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003108 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3109 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003110 return X86EMUL_CONTINUE;
3111}
3112
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003113static int em_mov(struct x86_emulate_ctxt *ctxt)
3114{
Stefan Hajnoczi49597d82012-04-09 18:40:00 +03003115 memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003116 return X86EMUL_CONTINUE;
3117}
3118
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003119static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3120{
3121 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3122 return emulate_gp(ctxt, 0);
3123
3124 /* Disable writeback. */
3125 ctxt->dst.type = OP_NONE;
3126 return X86EMUL_CONTINUE;
3127}
3128
3129static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3130{
3131 unsigned long val;
3132
3133 if (ctxt->mode == X86EMUL_MODE_PROT64)
3134 val = ctxt->src.val & ~0ULL;
3135 else
3136 val = ctxt->src.val & ~0U;
3137
3138 /* #UD condition is already handled. */
3139 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3140 return emulate_gp(ctxt, 0);
3141
3142 /* Disable writeback. */
3143 ctxt->dst.type = OP_NONE;
3144 return X86EMUL_CONTINUE;
3145}
3146
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003147static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3148{
3149 u64 msr_data;
3150
Avi Kivitydd856ef2012-08-27 23:46:17 +03003151 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3152 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3153 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003154 return emulate_gp(ctxt, 0);
3155
3156 return X86EMUL_CONTINUE;
3157}
3158
3159static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3160{
3161 u64 msr_data;
3162
Avi Kivitydd856ef2012-08-27 23:46:17 +03003163 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003164 return emulate_gp(ctxt, 0);
3165
Avi Kivitydd856ef2012-08-27 23:46:17 +03003166 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3167 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003168 return X86EMUL_CONTINUE;
3169}
3170
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003171static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3172{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003173 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003174 return emulate_ud(ctxt);
3175
Avi Kivity9dac77f2011-06-01 15:34:25 +03003176 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003177 return X86EMUL_CONTINUE;
3178}
3179
3180static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3181{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003182 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003183
Avi Kivity9dac77f2011-06-01 15:34:25 +03003184 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003185 return emulate_ud(ctxt);
3186
Avi Kivity9dac77f2011-06-01 15:34:25 +03003187 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003188 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3189
3190 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003191 ctxt->dst.type = OP_NONE;
3192 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003193}
3194
Avi Kivitya14e5792012-06-13 12:28:33 +03003195static int em_lldt(struct x86_emulate_ctxt *ctxt)
3196{
3197 u16 sel = ctxt->src.val;
3198
3199 /* Disable writeback. */
3200 ctxt->dst.type = OP_NONE;
3201 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3202}
3203
Avi Kivity80890002012-06-13 16:33:29 +03003204static int em_ltr(struct x86_emulate_ctxt *ctxt)
3205{
3206 u16 sel = ctxt->src.val;
3207
3208 /* Disable writeback. */
3209 ctxt->dst.type = OP_NONE;
3210 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3211}
3212
Avi Kivity38503912011-03-31 18:48:09 +02003213static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3214{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003215 int rc;
3216 ulong linear;
3217
Avi Kivity9dac77f2011-06-01 15:34:25 +03003218 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003219 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003220 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003221 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003222 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003223 return X86EMUL_CONTINUE;
3224}
3225
Avi Kivity2d04a052011-04-20 15:32:49 +03003226static int em_clts(struct x86_emulate_ctxt *ctxt)
3227{
3228 ulong cr0;
3229
3230 cr0 = ctxt->ops->get_cr(ctxt, 0);
3231 cr0 &= ~X86_CR0_TS;
3232 ctxt->ops->set_cr(ctxt, 0, cr0);
3233 return X86EMUL_CONTINUE;
3234}
3235
Avi Kivity26d05cc2011-04-21 12:07:59 +03003236static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3237{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003238 int rc;
3239
Avi Kivity9dac77f2011-06-01 15:34:25 +03003240 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003241 return X86EMUL_UNHANDLEABLE;
3242
3243 rc = ctxt->ops->fix_hypercall(ctxt);
3244 if (rc != X86EMUL_CONTINUE)
3245 return rc;
3246
3247 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003248 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003249 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003250 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003251 return X86EMUL_CONTINUE;
3252}
3253
Avi Kivity96051572012-06-10 17:21:18 +03003254static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3255 void (*get)(struct x86_emulate_ctxt *ctxt,
3256 struct desc_ptr *ptr))
3257{
3258 struct desc_ptr desc_ptr;
3259
3260 if (ctxt->mode == X86EMUL_MODE_PROT64)
3261 ctxt->op_bytes = 8;
3262 get(ctxt, &desc_ptr);
3263 if (ctxt->op_bytes == 2) {
3264 ctxt->op_bytes = 4;
3265 desc_ptr.address &= 0x00ffffff;
3266 }
3267 /* Disable writeback. */
3268 ctxt->dst.type = OP_NONE;
3269 return segmented_write(ctxt, ctxt->dst.addr.mem,
3270 &desc_ptr, 2 + ctxt->op_bytes);
3271}
3272
3273static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3274{
3275 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3276}
3277
3278static int em_sidt(struct x86_emulate_ctxt *ctxt)
3279{
3280 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3281}
3282
Avi Kivity26d05cc2011-04-21 12:07:59 +03003283static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3284{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003285 struct desc_ptr desc_ptr;
3286 int rc;
3287
Avi Kivity510425f2012-06-07 17:04:36 +03003288 if (ctxt->mode == X86EMUL_MODE_PROT64)
3289 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003290 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003291 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003292 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003293 if (rc != X86EMUL_CONTINUE)
3294 return rc;
3295 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3296 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003297 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003298 return X86EMUL_CONTINUE;
3299}
3300
Avi Kivity5ef39c72011-04-21 12:21:50 +03003301static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003302{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003303 int rc;
3304
Avi Kivity5ef39c72011-04-21 12:21:50 +03003305 rc = ctxt->ops->fix_hypercall(ctxt);
3306
Avi Kivity26d05cc2011-04-21 12:07:59 +03003307 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003308 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003309 return rc;
3310}
3311
3312static int em_lidt(struct x86_emulate_ctxt *ctxt)
3313{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003314 struct desc_ptr desc_ptr;
3315 int rc;
3316
Avi Kivity510425f2012-06-07 17:04:36 +03003317 if (ctxt->mode == X86EMUL_MODE_PROT64)
3318 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003319 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003320 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003321 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003322 if (rc != X86EMUL_CONTINUE)
3323 return rc;
3324 ctxt->ops->set_idt(ctxt, &desc_ptr);
3325 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003326 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003327 return X86EMUL_CONTINUE;
3328}
3329
3330static int em_smsw(struct x86_emulate_ctxt *ctxt)
3331{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003332 ctxt->dst.bytes = 2;
3333 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003334 return X86EMUL_CONTINUE;
3335}
3336
3337static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3338{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003339 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003340 | (ctxt->src.val & 0x0f));
3341 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003342 return X86EMUL_CONTINUE;
3343}
3344
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003345static int em_loop(struct x86_emulate_ctxt *ctxt)
3346{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003347 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3348 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003349 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3350 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003351
3352 return X86EMUL_CONTINUE;
3353}
3354
3355static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3356{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003357 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003358 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003359
3360 return X86EMUL_CONTINUE;
3361}
3362
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003363static int em_in(struct x86_emulate_ctxt *ctxt)
3364{
3365 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3366 &ctxt->dst.val))
3367 return X86EMUL_IO_NEEDED;
3368
3369 return X86EMUL_CONTINUE;
3370}
3371
3372static int em_out(struct x86_emulate_ctxt *ctxt)
3373{
3374 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3375 &ctxt->src.val, 1);
3376 /* Disable writeback. */
3377 ctxt->dst.type = OP_NONE;
3378 return X86EMUL_CONTINUE;
3379}
3380
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003381static int em_cli(struct x86_emulate_ctxt *ctxt)
3382{
3383 if (emulator_bad_iopl(ctxt))
3384 return emulate_gp(ctxt, 0);
3385
3386 ctxt->eflags &= ~X86_EFLAGS_IF;
3387 return X86EMUL_CONTINUE;
3388}
3389
3390static int em_sti(struct x86_emulate_ctxt *ctxt)
3391{
3392 if (emulator_bad_iopl(ctxt))
3393 return emulate_gp(ctxt, 0);
3394
3395 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3396 ctxt->eflags |= X86_EFLAGS_IF;
3397 return X86EMUL_CONTINUE;
3398}
3399
Avi Kivity6d6eede2012-06-07 14:11:36 +03003400static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3401{
3402 u32 eax, ebx, ecx, edx;
3403
Avi Kivitydd856ef2012-08-27 23:46:17 +03003404 eax = reg_read(ctxt, VCPU_REGS_RAX);
3405 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003406 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003407 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3408 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3409 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3410 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003411 return X86EMUL_CONTINUE;
3412}
3413
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003414static int em_lahf(struct x86_emulate_ctxt *ctxt)
3415{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003416 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3417 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003418 return X86EMUL_CONTINUE;
3419}
3420
Avi Kivity92998362012-06-13 12:25:06 +03003421static int em_bswap(struct x86_emulate_ctxt *ctxt)
3422{
3423 switch (ctxt->op_bytes) {
3424#ifdef CONFIG_X86_64
3425 case 8:
3426 asm("bswap %0" : "+r"(ctxt->dst.val));
3427 break;
3428#endif
3429 default:
3430 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3431 break;
3432 }
3433 return X86EMUL_CONTINUE;
3434}
3435
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003436static bool valid_cr(int nr)
3437{
3438 switch (nr) {
3439 case 0:
3440 case 2 ... 4:
3441 case 8:
3442 return true;
3443 default:
3444 return false;
3445 }
3446}
3447
3448static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3449{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003450 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003451 return emulate_ud(ctxt);
3452
3453 return X86EMUL_CONTINUE;
3454}
3455
3456static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3457{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003458 u64 new_val = ctxt->src.val64;
3459 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003460 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003461
3462 static u64 cr_reserved_bits[] = {
3463 0xffffffff00000000ULL,
3464 0, 0, 0, /* CR3 checked later */
3465 CR4_RESERVED_BITS,
3466 0, 0, 0,
3467 CR8_RESERVED_BITS,
3468 };
3469
3470 if (!valid_cr(cr))
3471 return emulate_ud(ctxt);
3472
3473 if (new_val & cr_reserved_bits[cr])
3474 return emulate_gp(ctxt, 0);
3475
3476 switch (cr) {
3477 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003478 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003479 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3480 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3481 return emulate_gp(ctxt, 0);
3482
Avi Kivity717746e2011-04-20 13:37:53 +03003483 cr4 = ctxt->ops->get_cr(ctxt, 4);
3484 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003485
3486 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3487 !(cr4 & X86_CR4_PAE))
3488 return emulate_gp(ctxt, 0);
3489
3490 break;
3491 }
3492 case 3: {
3493 u64 rsvd = 0;
3494
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003495 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3496 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003497 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003498 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003499 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003500 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003501 rsvd = CR3_NONPAE_RESERVED_BITS;
3502
3503 if (new_val & rsvd)
3504 return emulate_gp(ctxt, 0);
3505
3506 break;
3507 }
3508 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003509 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003510
3511 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3512 return emulate_gp(ctxt, 0);
3513
3514 break;
3515 }
3516 }
3517
3518 return X86EMUL_CONTINUE;
3519}
3520
Joerg Roedel3b88e412011-04-04 12:39:29 +02003521static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3522{
3523 unsigned long dr7;
3524
Avi Kivity717746e2011-04-20 13:37:53 +03003525 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003526
3527 /* Check if DR7.Global_Enable is set */
3528 return dr7 & (1 << 13);
3529}
3530
3531static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3532{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003533 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003534 u64 cr4;
3535
3536 if (dr > 7)
3537 return emulate_ud(ctxt);
3538
Avi Kivity717746e2011-04-20 13:37:53 +03003539 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003540 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3541 return emulate_ud(ctxt);
3542
3543 if (check_dr7_gd(ctxt))
3544 return emulate_db(ctxt);
3545
3546 return X86EMUL_CONTINUE;
3547}
3548
3549static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3550{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003551 u64 new_val = ctxt->src.val64;
3552 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003553
3554 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3555 return emulate_gp(ctxt, 0);
3556
3557 return check_dr_read(ctxt);
3558}
3559
Joerg Roedel01de8b02011-04-04 12:39:31 +02003560static int check_svme(struct x86_emulate_ctxt *ctxt)
3561{
3562 u64 efer;
3563
Avi Kivity717746e2011-04-20 13:37:53 +03003564 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003565
3566 if (!(efer & EFER_SVME))
3567 return emulate_ud(ctxt);
3568
3569 return X86EMUL_CONTINUE;
3570}
3571
3572static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3573{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003574 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003575
3576 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003577 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003578 return emulate_gp(ctxt, 0);
3579
3580 return check_svme(ctxt);
3581}
3582
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003583static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3584{
Avi Kivity717746e2011-04-20 13:37:53 +03003585 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003586
Avi Kivity717746e2011-04-20 13:37:53 +03003587 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003588 return emulate_ud(ctxt);
3589
3590 return X86EMUL_CONTINUE;
3591}
3592
Joerg Roedel80612522011-04-04 12:39:33 +02003593static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3594{
Avi Kivity717746e2011-04-20 13:37:53 +03003595 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003596 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003597
Avi Kivity717746e2011-04-20 13:37:53 +03003598 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02003599 (rcx > 3))
3600 return emulate_gp(ctxt, 0);
3601
3602 return X86EMUL_CONTINUE;
3603}
3604
Joerg Roedelf6511932011-04-04 12:39:35 +02003605static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3606{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003607 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3608 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003609 return emulate_gp(ctxt, 0);
3610
3611 return X86EMUL_CONTINUE;
3612}
3613
3614static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3615{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003616 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3617 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003618 return emulate_gp(ctxt, 0);
3619
3620 return X86EMUL_CONTINUE;
3621}
3622
Avi Kivity73fba5f2010-07-29 15:11:53 +03003623#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003624#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003625#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3626 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003627#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003628#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003629#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3630#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003631#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003632#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003633#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003634#define II(_f, _e, _i) \
3635 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003636#define IIP(_f, _e, _i, _p) \
3637 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3638 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003639#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003640
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003641#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003642#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003643#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003644#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003645#define I2bvIP(_f, _e, _i, _p) \
3646 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003647
Avi Kivityfb864fb2013-01-04 16:18:54 +02003648#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3649 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3650 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003651
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003652static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003653 DI(SrcNone | Priv, monitor),
3654 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003655 N, N, N, N, N, N,
3656};
3657
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003658static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003659 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
3660 II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall),
3661 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3662 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3663 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3664 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3665 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3666 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003667};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003668
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003669static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003670 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003671 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003672 N, N, N, N, N, N,
3673};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003674
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003675static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003676 F(Lock, em_add),
3677 F(Lock | PageTable, em_or),
3678 F(Lock, em_adc),
3679 F(Lock, em_sbb),
3680 F(Lock | PageTable, em_and),
3681 F(Lock, em_sub),
3682 F(Lock, em_xor),
3683 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003684};
3685
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003686static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003687 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003688};
3689
Avi Kivity007a3b52013-01-19 19:51:51 +02003690static const struct opcode group2[] = {
3691 F(DstMem | ModRM, em_rol),
3692 F(DstMem | ModRM, em_ror),
3693 F(DstMem | ModRM, em_rcl),
3694 F(DstMem | ModRM, em_rcr),
3695 F(DstMem | ModRM, em_shl),
3696 F(DstMem | ModRM, em_shr),
3697 F(DstMem | ModRM, em_shl),
3698 F(DstMem | ModRM, em_sar),
3699};
3700
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003701static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003702 F(DstMem | SrcImm | NoWrite, em_test),
3703 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003704 F(DstMem | SrcNone | Lock, em_not),
3705 F(DstMem | SrcNone | Lock, em_neg),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003706 I(SrcMem, em_mul_ex),
3707 I(SrcMem, em_imul_ex),
3708 I(SrcMem, em_div_ex),
3709 I(SrcMem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003710};
3711
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003712static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003713 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3714 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003715 N, N, N, N, N, N,
3716};
3717
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003718static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003719 F(DstMem | SrcNone | Lock, em_inc),
3720 F(DstMem | SrcNone | Lock, em_dec),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003721 I(SrcMem | Stack, em_grp45),
3722 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3723 I(SrcMem | Stack, em_grp45),
3724 I(SrcMemFAddr | ImplicitOps, em_grp45),
3725 I(SrcMem | Stack, em_grp45), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003726};
3727
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003728static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003729 DI(Prot, sldt),
3730 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003731 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003732 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003733 N, N, N, N,
3734};
3735
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003736static const struct group_dual group7 = { {
Avi Kivity96051572012-06-10 17:21:18 +03003737 II(Mov | DstMem | Priv, em_sgdt, sgdt),
3738 II(Mov | DstMem | Priv, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003739 II(SrcMem | Priv, em_lgdt, lgdt),
3740 II(SrcMem | Priv, em_lidt, lidt),
3741 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3742 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3743 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003744}, {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003745 I(SrcNone | Priv | VendorSpecific, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003746 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003747 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003748 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3749 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3750 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003751} };
3752
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003753static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003754 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003755 F(DstMem | SrcImmByte | NoWrite, em_bt),
3756 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3757 F(DstMem | SrcImmByte | Lock, em_btr),
3758 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003759};
3760
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003761static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003762 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003763}, {
3764 N, N, N, N, N, N, N, N,
3765} };
3766
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003767static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003768 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003769 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003770};
3771
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003772static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003773 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003774};
3775
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003776static const struct gprefix pfx_vmovntpx = {
Avi Kivity3e114eb2012-04-09 18:40:01 +03003777 I(0, em_mov), N, N, N,
3778};
3779
Gleb Natapov045a2822012-12-20 16:57:43 +02003780static const struct escape escape_d9 = { {
3781 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3782}, {
3783 /* 0xC0 - 0xC7 */
3784 N, N, N, N, N, N, N, N,
3785 /* 0xC8 - 0xCF */
3786 N, N, N, N, N, N, N, N,
3787 /* 0xD0 - 0xC7 */
3788 N, N, N, N, N, N, N, N,
3789 /* 0xD8 - 0xDF */
3790 N, N, N, N, N, N, N, N,
3791 /* 0xE0 - 0xE7 */
3792 N, N, N, N, N, N, N, N,
3793 /* 0xE8 - 0xEF */
3794 N, N, N, N, N, N, N, N,
3795 /* 0xF0 - 0xF7 */
3796 N, N, N, N, N, N, N, N,
3797 /* 0xF8 - 0xFF */
3798 N, N, N, N, N, N, N, N,
3799} };
3800
3801static const struct escape escape_db = { {
3802 N, N, N, N, N, N, N, N,
3803}, {
3804 /* 0xC0 - 0xC7 */
3805 N, N, N, N, N, N, N, N,
3806 /* 0xC8 - 0xCF */
3807 N, N, N, N, N, N, N, N,
3808 /* 0xD0 - 0xC7 */
3809 N, N, N, N, N, N, N, N,
3810 /* 0xD8 - 0xDF */
3811 N, N, N, N, N, N, N, N,
3812 /* 0xE0 - 0xE7 */
3813 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3814 /* 0xE8 - 0xEF */
3815 N, N, N, N, N, N, N, N,
3816 /* 0xF0 - 0xF7 */
3817 N, N, N, N, N, N, N, N,
3818 /* 0xF8 - 0xFF */
3819 N, N, N, N, N, N, N, N,
3820} };
3821
3822static const struct escape escape_dd = { {
3823 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3824}, {
3825 /* 0xC0 - 0xC7 */
3826 N, N, N, N, N, N, N, N,
3827 /* 0xC8 - 0xCF */
3828 N, N, N, N, N, N, N, N,
3829 /* 0xD0 - 0xC7 */
3830 N, N, N, N, N, N, N, N,
3831 /* 0xD8 - 0xDF */
3832 N, N, N, N, N, N, N, N,
3833 /* 0xE0 - 0xE7 */
3834 N, N, N, N, N, N, N, N,
3835 /* 0xE8 - 0xEF */
3836 N, N, N, N, N, N, N, N,
3837 /* 0xF0 - 0xF7 */
3838 N, N, N, N, N, N, N, N,
3839 /* 0xF8 - 0xFF */
3840 N, N, N, N, N, N, N, N,
3841} };
3842
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003843static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003844 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003845 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003846 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3847 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003848 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003849 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003850 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3851 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003852 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003853 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003854 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3855 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003856 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003857 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003858 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3859 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003860 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003861 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003862 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003863 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003864 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003865 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003866 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003867 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003868 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02003869 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003870 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003871 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003872 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003873 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003874 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003875 I(ImplicitOps | Stack | No64, em_pusha),
3876 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003877 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3878 N, N, N, N,
3879 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003880 I(SrcImm | Mov | Stack, em_push),
3881 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003882 I(SrcImmByte | Mov | Stack, em_push),
3883 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003884 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003885 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003886 /* 0x70 - 0x7F */
3887 X16(D(SrcImmByte)),
3888 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003889 G(ByteOp | DstMem | SrcImm, group1),
3890 G(DstMem | SrcImm, group1),
3891 G(ByteOp | DstMem | SrcImm | No64, group1),
3892 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003893 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003894 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003895 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003896 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003897 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003898 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003899 D(ModRM | SrcMem | NoAccess | DstReg),
3900 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3901 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003902 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003903 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003904 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003905 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003906 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003907 II(ImplicitOps | Stack, em_pushf, pushf),
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003908 II(ImplicitOps | Stack, em_popf, popf), N, I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003909 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003910 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003911 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003912 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003913 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003914 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003915 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003916 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3917 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003918 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003919 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003920 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003921 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02003922 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003923 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003924 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003925 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003926 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003927 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3928 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003929 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003930 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003931 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
3932 N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003933 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003934 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003935 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003936 G(Src2One | ByteOp, group2), G(Src2One, group2),
3937 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Gleb Natapov7f662272012-12-10 11:42:30 +02003938 N, I(DstAcc | SrcImmByte | No64, em_aad), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003939 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02003940 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003941 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003942 X3(I(SrcImmByte, em_loop)),
3943 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003944 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3945 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003946 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003947 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003948 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003949 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3950 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003951 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003952 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003953 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3954 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003955 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003956 D(ImplicitOps), D(ImplicitOps),
3957 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003958 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3959};
3960
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003961static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003962 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003963 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003964 N, I(ImplicitOps | VendorSpecific, em_syscall),
3965 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003966 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003967 N, D(ImplicitOps | ModRM), N, N,
3968 /* 0x10 - 0x1F */
3969 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3970 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003971 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003972 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003973 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
3974 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003975 N, N, N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003976 N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
3977 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003978 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003979 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02003980 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003981 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02003982 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003983 I(ImplicitOps | VendorSpecific, em_sysenter),
3984 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003985 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003986 N, N, N, N, N, N, N, N,
3987 /* 0x40 - 0x4F */
3988 X16(D(DstReg | SrcMem | ModRM | Mov)),
3989 /* 0x50 - 0x5F */
3990 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3991 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003992 N, N, N, N,
3993 N, N, N, N,
3994 N, N, N, N,
3995 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003996 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003997 N, N, N, N,
3998 N, N, N, N,
3999 N, N, N, N,
4000 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004001 /* 0x80 - 0x8F */
4002 X16(D(SrcImm)),
4003 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004004 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004005 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004006 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004007 II(ImplicitOps, em_cpuid, cpuid),
4008 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004009 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4010 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004011 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004012 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004013 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004014 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004015 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4016 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Avi Kivity4d758342013-01-19 19:51:55 +02004017 D(ModRM), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004018 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09004019 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004020 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004021 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004022 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4023 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004024 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004025 /* 0xB8 - 0xBF */
4026 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004027 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004028 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
4029 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004030 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004031 /* 0xC0 - 0xC7 */
Avi Kivity739ae402010-08-26 11:56:13 +03004032 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08004033 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004034 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004035 /* 0xC8 - 0xCF */
4036 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004037 /* 0xD0 - 0xDF */
4038 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4039 /* 0xE0 - 0xEF */
4040 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4041 /* 0xF0 - 0xFF */
4042 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4043};
4044
4045#undef D
4046#undef N
4047#undef G
4048#undef GD
4049#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004050#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004051#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004052
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004053#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004054#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004055#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004056#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004057#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004058
Avi Kivity9dac77f2011-06-01 15:34:25 +03004059static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004060{
4061 unsigned size;
4062
Avi Kivity9dac77f2011-06-01 15:34:25 +03004063 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004064 if (size == 8)
4065 size = 4;
4066 return size;
4067}
4068
4069static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4070 unsigned size, bool sign_extension)
4071{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004072 int rc = X86EMUL_CONTINUE;
4073
4074 op->type = OP_IMM;
4075 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004076 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004077 /* NB. Immediates are sign-extended as necessary. */
4078 switch (op->bytes) {
4079 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004080 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004081 break;
4082 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004083 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004084 break;
4085 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004086 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004087 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004088 case 8:
4089 op->val = insn_fetch(s64, ctxt);
4090 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004091 }
4092 if (!sign_extension) {
4093 switch (op->bytes) {
4094 case 1:
4095 op->val &= 0xff;
4096 break;
4097 case 2:
4098 op->val &= 0xffff;
4099 break;
4100 case 4:
4101 op->val &= 0xffffffff;
4102 break;
4103 }
4104 }
4105done:
4106 return rc;
4107}
4108
Avi Kivitya9945542011-09-13 10:45:41 +03004109static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4110 unsigned d)
4111{
4112 int rc = X86EMUL_CONTINUE;
4113
4114 switch (d) {
4115 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004116 decode_register_operand(ctxt, op);
Avi Kivitya9945542011-09-13 10:45:41 +03004117 break;
4118 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004119 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03004120 break;
4121 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004122 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004123 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03004124 *op = ctxt->memop;
4125 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03004126 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya9945542011-09-13 10:45:41 +03004127 fetch_bit_operand(ctxt);
4128 op->orig_val = op->val;
4129 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004130 case OpMem64:
4131 ctxt->memop.bytes = 8;
4132 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03004133 case OpAcc:
4134 op->type = OP_REG;
4135 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004136 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya9945542011-09-13 10:45:41 +03004137 fetch_register_operand(op);
4138 op->orig_val = op->val;
4139 break;
4140 case OpDI:
4141 op->type = OP_MEM;
4142 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4143 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004144 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya9945542011-09-13 10:45:41 +03004145 op->addr.mem.seg = VCPU_SREG_ES;
4146 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004147 op->count = 1;
Avi Kivitya9945542011-09-13 10:45:41 +03004148 break;
4149 case OpDX:
4150 op->type = OP_REG;
4151 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004152 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya9945542011-09-13 10:45:41 +03004153 fetch_register_operand(op);
4154 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004155 case OpCL:
4156 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004157 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004158 break;
4159 case OpImmByte:
4160 rc = decode_imm(ctxt, op, 1, true);
4161 break;
4162 case OpOne:
4163 op->bytes = 1;
4164 op->val = 1;
4165 break;
4166 case OpImm:
4167 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4168 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004169 case OpImm64:
4170 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4171 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004172 case OpMem8:
4173 ctxt->memop.bytes = 1;
4174 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004175 case OpMem16:
4176 ctxt->memop.bytes = 2;
4177 goto mem_common;
4178 case OpMem32:
4179 ctxt->memop.bytes = 4;
4180 goto mem_common;
4181 case OpImmU16:
4182 rc = decode_imm(ctxt, op, 2, false);
4183 break;
4184 case OpImmU:
4185 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4186 break;
4187 case OpSI:
4188 op->type = OP_MEM;
4189 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4190 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004191 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Avi Kivity0fe59122011-09-13 10:45:47 +03004192 op->addr.mem.seg = seg_override(ctxt);
4193 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004194 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004195 break;
4196 case OpImmFAddr:
4197 op->type = OP_IMM;
4198 op->addr.mem.ea = ctxt->_eip;
4199 op->bytes = ctxt->op_bytes + 2;
4200 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4201 break;
4202 case OpMemFAddr:
4203 ctxt->memop.bytes = ctxt->op_bytes + 2;
4204 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004205 case OpES:
4206 op->val = VCPU_SREG_ES;
4207 break;
4208 case OpCS:
4209 op->val = VCPU_SREG_CS;
4210 break;
4211 case OpSS:
4212 op->val = VCPU_SREG_SS;
4213 break;
4214 case OpDS:
4215 op->val = VCPU_SREG_DS;
4216 break;
4217 case OpFS:
4218 op->val = VCPU_SREG_FS;
4219 break;
4220 case OpGS:
4221 op->val = VCPU_SREG_GS;
4222 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004223 case OpImplicit:
4224 /* Special instructions do their own operand decoding. */
4225 default:
4226 op->type = OP_NONE; /* Disable writeback. */
4227 break;
4228 }
4229
4230done:
4231 return rc;
4232}
4233
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004234int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004235{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004236 int rc = X86EMUL_CONTINUE;
4237 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004238 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004239 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03004240 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004241
Avi Kivityf09ed832011-09-13 10:45:40 +03004242 ctxt->memop.type = OP_NONE;
4243 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004244 ctxt->_eip = ctxt->eip;
4245 ctxt->fetch.start = ctxt->_eip;
4246 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01004247 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004248 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004249
4250 switch (mode) {
4251 case X86EMUL_MODE_REAL:
4252 case X86EMUL_MODE_VM86:
4253 case X86EMUL_MODE_PROT16:
4254 def_op_bytes = def_ad_bytes = 2;
4255 break;
4256 case X86EMUL_MODE_PROT32:
4257 def_op_bytes = def_ad_bytes = 4;
4258 break;
4259#ifdef CONFIG_X86_64
4260 case X86EMUL_MODE_PROT64:
4261 def_op_bytes = 4;
4262 def_ad_bytes = 8;
4263 break;
4264#endif
4265 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004266 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004267 }
4268
Avi Kivity9dac77f2011-06-01 15:34:25 +03004269 ctxt->op_bytes = def_op_bytes;
4270 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004271
4272 /* Legacy prefixes. */
4273 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004274 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004275 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004276 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004277 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004278 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004279 break;
4280 case 0x67: /* address-size override */
4281 if (mode == X86EMUL_MODE_PROT64)
4282 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004283 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004284 else
4285 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004286 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004287 break;
4288 case 0x26: /* ES override */
4289 case 0x2e: /* CS override */
4290 case 0x36: /* SS override */
4291 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004292 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004293 break;
4294 case 0x64: /* FS override */
4295 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004296 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004297 break;
4298 case 0x40 ... 0x4f: /* REX */
4299 if (mode != X86EMUL_MODE_PROT64)
4300 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004301 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004302 continue;
4303 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004304 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004305 break;
4306 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004307 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004308 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004309 break;
4310 default:
4311 goto done_prefixes;
4312 }
4313
4314 /* Any legacy prefix after a REX prefix nullifies its effect. */
4315
Avi Kivity9dac77f2011-06-01 15:34:25 +03004316 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004317 }
4318
4319done_prefixes:
4320
4321 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004322 if (ctxt->rex_prefix & 8)
4323 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004324
4325 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004326 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004327 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004328 if (ctxt->b == 0x0f) {
4329 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004330 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004331 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004332 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004333 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004334
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004335 if (ctxt->d & ModRM)
4336 ctxt->modrm = insn_fetch(u8, ctxt);
4337
Avi Kivity9dac77f2011-06-01 15:34:25 +03004338 while (ctxt->d & GroupMask) {
4339 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004340 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004341 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004342 opcode = opcode.u.group[goffset];
4343 break;
4344 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004345 goffset = (ctxt->modrm >> 3) & 7;
4346 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004347 opcode = opcode.u.gdual->mod3[goffset];
4348 else
4349 opcode = opcode.u.gdual->mod012[goffset];
4350 break;
4351 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004352 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004353 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004354 break;
4355 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004356 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004357 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004358 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004359 switch (simd_prefix) {
4360 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4361 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4362 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4363 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4364 }
4365 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004366 case Escape:
4367 if (ctxt->modrm > 0xbf)
4368 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4369 else
4370 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4371 break;
Avi Kivity46561642011-04-24 14:09:59 +03004372 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004373 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004374 }
Avi Kivity46561642011-04-24 14:09:59 +03004375
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004376 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004377 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004378 }
4379
Avi Kivity9dac77f2011-06-01 15:34:25 +03004380 ctxt->execute = opcode.u.execute;
4381 ctxt->check_perm = opcode.check_perm;
4382 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004383
4384 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004385 if (ctxt->d == 0 || (ctxt->d & Undefined))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004386 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004387
Avi Kivity9dac77f2011-06-01 15:34:25 +03004388 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004389 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004390
Avi Kivity9dac77f2011-06-01 15:34:25 +03004391 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
4392 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004393
Avi Kivity9dac77f2011-06-01 15:34:25 +03004394 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004395 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004396 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004397 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03004398 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004399 }
4400
Avi Kivity9dac77f2011-06-01 15:34:25 +03004401 if (ctxt->d & Sse)
4402 ctxt->op_bytes = 16;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004403 else if (ctxt->d & Mmx)
4404 ctxt->op_bytes = 8;
Avi Kivity12537912011-03-29 11:41:27 +02004405
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004406 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004407 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004408 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004409 if (!ctxt->has_seg_override)
4410 set_seg_override(ctxt, ctxt->modrm_seg);
4411 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004412 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004413 if (rc != X86EMUL_CONTINUE)
4414 goto done;
4415
Avi Kivity9dac77f2011-06-01 15:34:25 +03004416 if (!ctxt->has_seg_override)
4417 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004418
Avi Kivityf09ed832011-09-13 10:45:40 +03004419 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004420
Avi Kivityf09ed832011-09-13 10:45:40 +03004421 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
4422 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004423
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004424 /*
4425 * Decode and fetch the source operand: register, memory
4426 * or immediate.
4427 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004428 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004429 if (rc != X86EMUL_CONTINUE)
4430 goto done;
4431
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004432 /*
4433 * Decode and fetch the second source operand: register, memory
4434 * or immediate.
4435 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004436 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004437 if (rc != X86EMUL_CONTINUE)
4438 goto done;
4439
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004440 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03004441 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004442
4443done:
Avi Kivityf09ed832011-09-13 10:45:40 +03004444 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
4445 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004446
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004447 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004448}
4449
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004450bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4451{
4452 return ctxt->d & PageTable;
4453}
4454
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004455static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4456{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004457 /* The second termination condition only applies for REPE
4458 * and REPNE. Test if the repeat string operation prefix is
4459 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4460 * corresponding termination condition according to:
4461 * - if REPE/REPZ and ZF = 0 then done
4462 * - if REPNE/REPNZ and ZF = 1 then done
4463 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004464 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4465 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4466 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004467 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004468 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004469 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4470 return true;
4471
4472 return false;
4473}
4474
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004475static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4476{
4477 bool fault = false;
4478
4479 ctxt->ops->get_fpu(ctxt);
4480 asm volatile("1: fwait \n\t"
4481 "2: \n\t"
4482 ".pushsection .fixup,\"ax\" \n\t"
4483 "3: \n\t"
4484 "movb $1, %[fault] \n\t"
4485 "jmp 2b \n\t"
4486 ".popsection \n\t"
4487 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004488 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004489 ctxt->ops->put_fpu(ctxt);
4490
4491 if (unlikely(fault))
4492 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4493
4494 return X86EMUL_CONTINUE;
4495}
4496
4497static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4498 struct operand *op)
4499{
4500 if (op->type == OP_MM)
4501 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4502}
4503
Avi Kivitye28bbd42013-01-04 16:18:48 +02004504static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4505{
4506 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
4507 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
4508 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
4509 : "+a"(ctxt->dst.val), "+b"(ctxt->src.val), [flags]"+D"(flags)
4510 : "c"(ctxt->src2.val), [fastop]"S"(fop));
4511 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
4512 return X86EMUL_CONTINUE;
4513}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004514
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004515int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004516{
Mathias Krause0225fb52012-08-30 01:30:16 +02004517 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004518 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004519 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004520
Avi Kivity9dac77f2011-06-01 15:34:25 +03004521 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004522
Avi Kivity9dac77f2011-06-01 15:34:25 +03004523 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004524 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02004525 goto done;
4526 }
4527
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004528 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004529 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004530 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004531 goto done;
4532 }
4533
Avi Kivity9dac77f2011-06-01 15:34:25 +03004534 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004535 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004536 goto done;
4537 }
4538
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004539 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4540 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02004541 rc = emulate_ud(ctxt);
4542 goto done;
4543 }
4544
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004545 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02004546 rc = emulate_nm(ctxt);
4547 goto done;
4548 }
4549
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004550 if (ctxt->d & Mmx) {
4551 rc = flush_pending_x87_faults(ctxt);
4552 if (rc != X86EMUL_CONTINUE)
4553 goto done;
4554 /*
4555 * Now that we know the fpu is exception safe, we can fetch
4556 * operands from it.
4557 */
4558 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4559 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4560 if (!(ctxt->d & Mov))
4561 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4562 }
4563
Avi Kivity9dac77f2011-06-01 15:34:25 +03004564 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4565 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004566 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004567 if (rc != X86EMUL_CONTINUE)
4568 goto done;
4569 }
4570
Gleb Natapove92805a2010-02-10 14:21:35 +02004571 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004572 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004573 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02004574 goto done;
4575 }
4576
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004577 /* Instruction can only be executed in protected mode */
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03004578 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004579 rc = emulate_ud(ctxt);
4580 goto done;
4581 }
4582
Joerg Roedeld09beab2011-04-04 12:39:25 +02004583 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004584 if (ctxt->check_perm) {
4585 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004586 if (rc != X86EMUL_CONTINUE)
4587 goto done;
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_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004593 if (rc != X86EMUL_CONTINUE)
4594 goto done;
4595 }
4596
Avi Kivity9dac77f2011-06-01 15:34:25 +03004597 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004598 /* All REP prefixes have the same first termination condition */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004599 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004600 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004601 goto done;
4602 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004603 }
4604
Avi Kivity9dac77f2011-06-01 15:34:25 +03004605 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4606 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4607 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004608 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004609 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004610 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004611 }
4612
Avi Kivity9dac77f2011-06-01 15:34:25 +03004613 if (ctxt->src2.type == OP_MEM) {
4614 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4615 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004616 if (rc != X86EMUL_CONTINUE)
4617 goto done;
4618 }
4619
Avi Kivity9dac77f2011-06-01 15:34:25 +03004620 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004621 goto special_insn;
4622
4623
Avi Kivity9dac77f2011-06-01 15:34:25 +03004624 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004625 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004626 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4627 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004628 if (rc != X86EMUL_CONTINUE)
4629 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004630 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004631 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004632
Avi Kivity018a98d2007-11-27 19:30:56 +02004633special_insn:
4634
Avi Kivity9dac77f2011-06-01 15:34:25 +03004635 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4636 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004637 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004638 if (rc != X86EMUL_CONTINUE)
4639 goto done;
4640 }
4641
Avi Kivity9dac77f2011-06-01 15:34:25 +03004642 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004643 if (ctxt->d & Fastop) {
4644 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4645 rc = fastop(ctxt, fop);
4646 if (rc != X86EMUL_CONTINUE)
4647 goto done;
4648 goto writeback;
4649 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004650 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004651 if (rc != X86EMUL_CONTINUE)
4652 goto done;
4653 goto writeback;
4654 }
4655
Avi Kivity9dac77f2011-06-01 15:34:25 +03004656 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004657 goto twobyte_insn;
4658
Avi Kivity9dac77f2011-06-01 15:34:25 +03004659 switch (ctxt->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004660 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004661 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004662 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004663 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004664 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004665 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004666 if (test_cc(ctxt->b, ctxt->eflags))
4667 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004668 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004669 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004670 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004671 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004672 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004673 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Mohammed Gamal34698d82010-08-04 14:41:04 +03004674 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004675 rc = em_xchg(ctxt);
4676 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004677 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004678 switch (ctxt->op_bytes) {
4679 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4680 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4681 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004682 }
4683 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004684 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004685 rc = emulate_int(ctxt, 3);
4686 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004687 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004688 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004689 break;
4690 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004691 if (ctxt->eflags & EFLG_OF)
4692 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004693 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004694 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004695 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004696 jmp_rel(ctxt, ctxt->src.val);
4697 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004698 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004699 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004700 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004701 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004702 case 0xf5: /* cmc */
4703 /* complement carry flag from eflags reg */
4704 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004705 break;
4706 case 0xf8: /* clc */
4707 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004708 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004709 case 0xf9: /* stc */
4710 ctxt->eflags |= EFLG_CF;
4711 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004712 case 0xfc: /* cld */
4713 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004714 break;
4715 case 0xfd: /* std */
4716 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004717 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004718 default:
4719 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004720 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004721
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004722 if (rc != X86EMUL_CONTINUE)
4723 goto done;
4724
Avi Kivity018a98d2007-11-27 19:30:56 +02004725writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09004726 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004727 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02004728 goto done;
4729
Gleb Natapov5cd21912010-03-18 15:20:26 +02004730 /*
4731 * restore dst type in case the decoding will be reused
4732 * (happens for string instruction )
4733 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004734 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004735
Avi Kivity9dac77f2011-06-01 15:34:25 +03004736 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004737 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004738
Avi Kivity9dac77f2011-06-01 15:34:25 +03004739 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004740 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004741
Avi Kivity9dac77f2011-06-01 15:34:25 +03004742 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004743 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004744 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004745 if ((ctxt->d & SrcMask) == SrcSI)
4746 count = ctxt->src.count;
4747 else
4748 count = ctxt->dst.count;
4749 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4750 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004751
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004752 if (!string_insn_completed(ctxt)) {
4753 /*
4754 * Re-enter guest when pio read ahead buffer is empty
4755 * or, if it is not used, after each 1024 iteration.
4756 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004757 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004758 (r->end == 0 || r->end != r->pos)) {
4759 /*
4760 * Reset read cache. Usually happens before
4761 * decode, but since instruction is restarted
4762 * we have to do it here.
4763 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004764 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004765 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004766 return EMULATION_RESTART;
4767 }
4768 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004769 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004770 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004771
Avi Kivity9dac77f2011-06-01 15:34:25 +03004772 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004773
4774done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004775 if (rc == X86EMUL_PROPAGATE_FAULT)
4776 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004777 if (rc == X86EMUL_INTERCEPTED)
4778 return EMULATION_INTERCEPTED;
4779
Avi Kivitydd856ef2012-08-27 23:46:17 +03004780 if (rc == X86EMUL_CONTINUE)
4781 writeback_registers(ctxt);
4782
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004783 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004784
4785twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004786 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004787 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004788 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004789 break;
4790 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004791 case 0x0d: /* GrpP (prefetch) */
4792 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004793 break;
4794 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004795 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004796 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004797 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004798 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004799 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004800 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004801 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4802 if (!test_cc(ctxt->b, ctxt->eflags))
4803 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004804 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004805 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004806 if (test_cc(ctxt->b, ctxt->eflags))
4807 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004808 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004809 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004810 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004811 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004812 case 0xae: /* clflush */
4813 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004814 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004815 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004816 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004817 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004818 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004819 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004820 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004821 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004822 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004823 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004824 case 0xc0 ... 0xc1: /* xadd */
Avi Kivity158de572013-01-19 19:51:57 +02004825 fastop(ctxt, em_add);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004826 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004827 ctxt->src.val = ctxt->dst.orig_val;
4828 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004829 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004830 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004831 ctxt->dst.bytes = ctxt->op_bytes;
4832 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4833 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004834 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004835 default:
4836 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004837 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004838
4839 if (rc != X86EMUL_CONTINUE)
4840 goto done;
4841
Avi Kivity6aa8b732006-12-10 02:21:36 -08004842 goto writeback;
4843
4844cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004845 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004846}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004847
4848void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
4849{
4850 invalidate_registers(ctxt);
4851}
4852
4853void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
4854{
4855 writeback_registers(ctxt);
4856}