blob: 3a3542a882893294593d8993be30d285322d371b [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivityb7d491e2013-01-04 16:18:49 +020027#include <linux/stringify.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028
Avi Kivity3eeb3282010-01-21 15:31:48 +020029#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020030#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020031
Avi Kivity6aa8b732006-12-10 02:21:36 -080032/*
Avi Kivitya99455492011-09-13 10:45:41 +030033 * Operand types
34 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030035#define OpNone 0ull
36#define OpImplicit 1ull /* No generic decode */
37#define OpReg 2ull /* Register */
38#define OpMem 3ull /* Memory */
39#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
40#define OpDI 5ull /* ES:DI/EDI/RDI */
41#define OpMem64 6ull /* Memory, 64-bit */
42#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
43#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030044#define OpCL 9ull /* CL register (for shifts) */
45#define OpImmByte 10ull /* 8-bit sign extended immediate */
46#define OpOne 11ull /* Implied 1 */
Nadav Amit5e2c6882012-12-06 21:55:10 -020047#define OpImm 12ull /* Sign extended up to 32-bit immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030048#define OpMem16 13ull /* Memory operand (16-bit). */
49#define OpMem32 14ull /* Memory operand (32-bit). */
50#define OpImmU 15ull /* Immediate operand, zero extended */
51#define OpSI 16ull /* SI/ESI/RSI */
52#define OpImmFAddr 17ull /* Immediate far address */
53#define OpMemFAddr 18ull /* Far address in memory */
54#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030055#define OpES 20ull /* ES */
56#define OpCS 21ull /* CS */
57#define OpSS 22ull /* SS */
58#define OpDS 23ull /* DS */
59#define OpFS 24ull /* FS */
60#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020061#define OpMem8 26ull /* 8-bit zero extended memory operand */
Nadav Amit5e2c6882012-12-06 21:55:10 -020062#define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */
Paolo Bonzini7fa57952013-05-09 11:32:50 +020063#define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */
Avi Kivity820207c2013-02-09 11:31:45 +020064#define OpAccLo 29ull /* Low part of extended acc (AX/AX/EAX/RAX) */
65#define OpAccHi 30ull /* High part of extended acc (-/DX/EDX/RDX) */
Avi Kivitya99455492011-09-13 10:45:41 +030066
Avi Kivity0fe59122011-09-13 10:45:47 +030067#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030068#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya99455492011-09-13 10:45:41 +030069
70/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080071 * Opcode effective-address decode tables.
72 * Note that we only emulate instructions that have at least one memory
73 * operand (excluding implicit stack references). We assume that stack
74 * references and instruction fetches will never occur in special memory
75 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
76 * not be handled.
77 */
78
79/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030080#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080081/* Destination operand type. */
Avi Kivitya99455492011-09-13 10:45:41 +030082#define DstShift 1
83#define ImplicitOps (OpImplicit << DstShift)
84#define DstReg (OpReg << DstShift)
85#define DstMem (OpMem << DstShift)
86#define DstAcc (OpAcc << DstShift)
87#define DstDI (OpDI << DstShift)
88#define DstMem64 (OpMem64 << DstShift)
89#define DstImmUByte (OpImmUByte << DstShift)
90#define DstDX (OpDX << DstShift)
Avi Kivity820207c2013-02-09 11:31:45 +020091#define DstAccLo (OpAccLo << DstShift)
Avi Kivitya99455492011-09-13 10:45:41 +030092#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080093/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030094#define SrcShift 6
95#define SrcNone (OpNone << SrcShift)
96#define SrcReg (OpReg << SrcShift)
97#define SrcMem (OpMem << SrcShift)
98#define SrcMem16 (OpMem16 << SrcShift)
99#define SrcMem32 (OpMem32 << SrcShift)
100#define SrcImm (OpImm << SrcShift)
101#define SrcImmByte (OpImmByte << SrcShift)
102#define SrcOne (OpOne << SrcShift)
103#define SrcImmUByte (OpImmUByte << SrcShift)
104#define SrcImmU (OpImmU << SrcShift)
105#define SrcSI (OpSI << SrcShift)
Paolo Bonzini7fa57952013-05-09 11:32:50 +0200106#define SrcXLat (OpXLat << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300107#define SrcImmFAddr (OpImmFAddr << SrcShift)
108#define SrcMemFAddr (OpMemFAddr << SrcShift)
109#define SrcAcc (OpAcc << SrcShift)
110#define SrcImmU16 (OpImmU16 << SrcShift)
Nadav Amit5e2c6882012-12-06 21:55:10 -0200111#define SrcImm64 (OpImm64 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300112#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200113#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity820207c2013-02-09 11:31:45 +0200114#define SrcAccHi (OpAccHi << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300115#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300116#define BitOp (1<<11)
117#define MemAbs (1<<12) /* Memory operand is absolute displacement */
118#define String (1<<13) /* String instruction (rep capable) */
119#define Stack (1<<14) /* Stack instruction (push/pop) */
120#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
121#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
122#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
123#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
124#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
Gleb Natapov045a2822012-12-20 16:57:43 +0200125#define Escape (5<<15) /* Escape to coprocessor instruction */
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300126#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300127/* Generic ModRM decode. */
128#define ModRM (1<<19)
129/* Destination is only written; never read. */
130#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300131/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200132#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Avi Kivityd8671622011-02-01 16:32:03 +0200133#define VendorSpecific (1<<22) /* Vendor specific instruction */
Avi Kivity5a506b12010-08-01 15:10:29 +0300134#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300135#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300136#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200137#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200138#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300139#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800140#define PageTable (1 << 29) /* instruction used to write page table */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300141#define NotImpl (1 << 30) /* instruction is not implemented */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100142/* Source 2 operand type */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300143#define Src2Shift (31)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300144#define Src2None (OpNone << Src2Shift)
Avi Kivityab2c5ce2013-02-09 11:31:46 +0200145#define Src2Mem (OpMem << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300146#define Src2CL (OpCL << Src2Shift)
147#define Src2ImmByte (OpImmByte << Src2Shift)
148#define Src2One (OpOne << Src2Shift)
149#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300150#define Src2ES (OpES << Src2Shift)
151#define Src2CS (OpCS << Src2Shift)
152#define Src2SS (OpSS << Src2Shift)
153#define Src2DS (OpDS << Src2Shift)
154#define Src2FS (OpFS << Src2Shift)
155#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300156#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300157#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Avi Kivity1c11b372012-04-09 18:39:59 +0300158#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
159#define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */
160#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
Avi Kivitye28bbd42013-01-04 16:18:48 +0200161#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
Avi Kivityb6744dc2013-01-04 16:18:50 +0200162#define NoWrite ((u64)1 << 45) /* No writeback */
Avi Kivityfb32b1e2013-02-09 11:31:44 +0200163#define SrcWrite ((u64)1 << 46) /* Write back src operand */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800164
Avi Kivity820207c2013-02-09 11:31:45 +0200165#define DstXacc (DstAccLo | SrcAccHi | SrcWrite)
166
Avi Kivityd0e53322010-07-29 15:11:54 +0300167#define X2(x...) x, x
168#define X3(x...) X2(x), x
169#define X4(x...) X2(x), X2(x)
170#define X5(x...) X4(x), x
171#define X6(x...) X4(x), X2(x)
172#define X7(x...) X4(x), X3(x)
173#define X8(x...) X4(x), X4(x)
174#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300175
Avi Kivitye28bbd42013-01-04 16:18:48 +0200176#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
177#define FASTOP_SIZE 8
178
179/*
180 * fastop functions have a special calling convention:
181 *
Avi Kivity017da7b2013-02-09 11:31:47 +0200182 * dst: rax (in/out)
183 * src: rdx (in/out)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200184 * src2: rcx (in)
185 * flags: rflags (in/out)
186 *
187 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
188 * different operand sizes can be reached by calculation, rather than a jump
189 * table (which would be bigger than the code).
190 *
191 * fastop functions are declared as taking a never-defined fastop parameter,
192 * so they can't be called from C directly.
193 */
194
195struct fastop;
196
Avi Kivityd65b1de2010-07-29 15:11:35 +0300197struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300198 u64 flags : 56;
199 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300200 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300201 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200202 const struct opcode *group;
203 const struct group_dual *gdual;
204 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200205 const struct escape *esc;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200206 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300207 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200208 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300209};
210
211struct group_dual {
212 struct opcode mod012[8];
213 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300214};
215
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200216struct gprefix {
217 struct opcode pfx_no;
218 struct opcode pfx_66;
219 struct opcode pfx_f2;
220 struct opcode pfx_f3;
221};
222
Gleb Natapov045a2822012-12-20 16:57:43 +0200223struct escape {
224 struct opcode op[8];
225 struct opcode high[64];
226};
227
Avi Kivity6aa8b732006-12-10 02:21:36 -0800228/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200229#define EFLG_ID (1<<21)
230#define EFLG_VIP (1<<20)
231#define EFLG_VIF (1<<19)
232#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200233#define EFLG_VM (1<<17)
234#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200235#define EFLG_IOPL (3<<12)
236#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800237#define EFLG_OF (1<<11)
238#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200239#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200240#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800241#define EFLG_SF (1<<7)
242#define EFLG_ZF (1<<6)
243#define EFLG_AF (1<<4)
244#define EFLG_PF (1<<2)
245#define EFLG_CF (1<<0)
246
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300247#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
248#define EFLG_RESERVED_ONE_MASK 2
249
Avi Kivitydd856ef2012-08-27 23:46:17 +0300250static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
251{
252 if (!(ctxt->regs_valid & (1 << nr))) {
253 ctxt->regs_valid |= 1 << nr;
254 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
255 }
256 return ctxt->_regs[nr];
257}
258
259static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
260{
261 ctxt->regs_valid |= 1 << nr;
262 ctxt->regs_dirty |= 1 << nr;
263 return &ctxt->_regs[nr];
264}
265
266static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
267{
268 reg_read(ctxt, nr);
269 return reg_write(ctxt, nr);
270}
271
272static void writeback_registers(struct x86_emulate_ctxt *ctxt)
273{
274 unsigned reg;
275
276 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
277 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
278}
279
280static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
281{
282 ctxt->regs_dirty = 0;
283 ctxt->regs_valid = 0;
284}
285
Avi Kivity6aa8b732006-12-10 02:21:36 -0800286/*
287 * Instruction emulation:
288 * Most instructions are emulated directly via a fragment of inline assembly
289 * code. This allows us to save/restore EFLAGS and thus very easily pick up
290 * any modified flags.
291 */
292
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800293#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800294#define _LO32 "k" /* force 32-bit operand */
295#define _STK "%%rsp" /* stack pointer */
296#elif defined(__i386__)
297#define _LO32 "" /* force 32-bit operand */
298#define _STK "%%esp" /* stack pointer */
299#endif
300
301/*
302 * These EFLAGS bits are restored from saved value during emulation, and
303 * any changes are written back to the saved value after emulation.
304 */
305#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
306
307/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200308#define _PRE_EFLAGS(_sav, _msk, _tmp) \
309 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
310 "movl %"_sav",%"_LO32 _tmp"; " \
311 "push %"_tmp"; " \
312 "push %"_tmp"; " \
313 "movl %"_msk",%"_LO32 _tmp"; " \
314 "andl %"_LO32 _tmp",("_STK"); " \
315 "pushf; " \
316 "notl %"_LO32 _tmp"; " \
317 "andl %"_LO32 _tmp",("_STK"); " \
318 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
319 "pop %"_tmp"; " \
320 "orl %"_LO32 _tmp",("_STK"); " \
321 "popf; " \
322 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800323
324/* After executing instruction: write-back necessary bits in EFLAGS. */
325#define _POST_EFLAGS(_sav, _msk, _tmp) \
326 /* _sav |= EFLAGS & _msk; */ \
327 "pushf; " \
328 "pop %"_tmp"; " \
329 "andl %"_msk",%"_LO32 _tmp"; " \
330 "orl %"_LO32 _tmp",%"_sav"; "
331
Avi Kivitydda96d82008-11-26 15:14:10 +0200332#ifdef CONFIG_X86_64
333#define ON64(x) x
334#else
335#define ON64(x)
336#endif
337
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300338#define ____emulate_2op(ctxt, _op, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200339 do { \
340 __asm__ __volatile__ ( \
341 _PRE_EFLAGS("0", "4", "2") \
342 _op _suffix " %"_x"3,%1; " \
343 _POST_EFLAGS("0", "4", "2") \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300344 : "=m" ((ctxt)->eflags), \
345 "+q" (*(_dsttype*)&(ctxt)->dst.val), \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200346 "=&r" (_tmp) \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300347 : _y ((ctxt)->src.val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200348 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200349
350
Avi Kivity6aa8b732006-12-10 02:21:36 -0800351/* Raw emulation: instruction has two explicit operands. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300352#define __emulate_2op_nobyte(ctxt,_op,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200353 do { \
354 unsigned long _tmp; \
355 \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300356 switch ((ctxt)->dst.bytes) { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200357 case 2: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300358 ____emulate_2op(ctxt,_op,_wx,_wy,"w",u16); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200359 break; \
360 case 4: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300361 ____emulate_2op(ctxt,_op,_lx,_ly,"l",u32); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200362 break; \
363 case 8: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300364 ON64(____emulate_2op(ctxt,_op,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200365 break; \
366 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800367 } while (0)
368
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300369#define __emulate_2op(ctxt,_op,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800370 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200371 unsigned long _tmp; \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300372 switch ((ctxt)->dst.bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800373 case 1: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300374 ____emulate_2op(ctxt,_op,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800375 break; \
376 default: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300377 __emulate_2op_nobyte(ctxt, _op, \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800378 _wx, _wy, _lx, _ly, _qx, _qy); \
379 break; \
380 } \
381 } while (0)
382
383/* Source operand is byte-sized and may be restricted to just %cl. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300384#define emulate_2op_SrcB(ctxt, _op) \
385 __emulate_2op(ctxt, _op, "b", "c", "b", "c", "b", "c", "b", "c")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800386
387/* Source operand is byte, word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300388#define emulate_2op_SrcV(ctxt, _op) \
389 __emulate_2op(ctxt, _op, "b", "q", "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800390
391/* Source operand is word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300392#define emulate_2op_SrcV_nobyte(ctxt, _op) \
393 __emulate_2op_nobyte(ctxt, _op, "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800394
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100395/* Instruction has three operands and one operand is stored in ECX register */
Avi Kivity29053a62011-09-07 16:41:37 +0300396#define __emulate_2op_cl(ctxt, _op, _suffix, _type) \
Avi Kivity72952612011-04-20 13:12:27 +0300397 do { \
398 unsigned long _tmp; \
Avi Kivity761441b2011-09-07 16:41:36 +0300399 _type _clv = (ctxt)->src2.val; \
400 _type _srcv = (ctxt)->src.val; \
401 _type _dstv = (ctxt)->dst.val; \
Avi Kivity72952612011-04-20 13:12:27 +0300402 \
403 __asm__ __volatile__ ( \
404 _PRE_EFLAGS("0", "5", "2") \
405 _op _suffix " %4,%1 \n" \
406 _POST_EFLAGS("0", "5", "2") \
Avi Kivity761441b2011-09-07 16:41:36 +0300407 : "=m" ((ctxt)->eflags), "+r" (_dstv), "=&r" (_tmp) \
Avi Kivity72952612011-04-20 13:12:27 +0300408 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
409 ); \
410 \
Avi Kivity761441b2011-09-07 16:41:36 +0300411 (ctxt)->src2.val = (unsigned long) _clv; \
412 (ctxt)->src2.val = (unsigned long) _srcv; \
413 (ctxt)->dst.val = (unsigned long) _dstv; \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100414 } while (0)
415
Avi Kivity761441b2011-09-07 16:41:36 +0300416#define emulate_2op_cl(ctxt, _op) \
Avi Kivity72952612011-04-20 13:12:27 +0300417 do { \
Avi Kivity761441b2011-09-07 16:41:36 +0300418 switch ((ctxt)->dst.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300419 case 2: \
Avi Kivity29053a62011-09-07 16:41:37 +0300420 __emulate_2op_cl(ctxt, _op, "w", u16); \
Avi Kivity72952612011-04-20 13:12:27 +0300421 break; \
422 case 4: \
Avi Kivity29053a62011-09-07 16:41:37 +0300423 __emulate_2op_cl(ctxt, _op, "l", u32); \
Avi Kivity72952612011-04-20 13:12:27 +0300424 break; \
425 case 8: \
Avi Kivity29053a62011-09-07 16:41:37 +0300426 ON64(__emulate_2op_cl(ctxt, _op, "q", ulong)); \
Avi Kivity72952612011-04-20 13:12:27 +0300427 break; \
428 } \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100429 } while (0)
430
Avi Kivityd1eef452011-09-07 16:41:38 +0300431#define __emulate_1op(ctxt, _op, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800432 do { \
433 unsigned long _tmp; \
434 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200435 __asm__ __volatile__ ( \
436 _PRE_EFLAGS("0", "3", "2") \
437 _op _suffix " %1; " \
438 _POST_EFLAGS("0", "3", "2") \
Avi Kivityd1eef452011-09-07 16:41:38 +0300439 : "=m" ((ctxt)->eflags), "+m" ((ctxt)->dst.val), \
Avi Kivitydda96d82008-11-26 15:14:10 +0200440 "=&r" (_tmp) \
441 : "i" (EFLAGS_MASK)); \
442 } while (0)
443
444/* Instruction has only one explicit operand (no source operand). */
Avi Kivityd1eef452011-09-07 16:41:38 +0300445#define emulate_1op(ctxt, _op) \
Avi Kivitydda96d82008-11-26 15:14:10 +0200446 do { \
Avi Kivityd1eef452011-09-07 16:41:38 +0300447 switch ((ctxt)->dst.bytes) { \
448 case 1: __emulate_1op(ctxt, _op, "b"); break; \
449 case 2: __emulate_1op(ctxt, _op, "w"); break; \
450 case 4: __emulate_1op(ctxt, _op, "l"); break; \
451 case 8: ON64(__emulate_1op(ctxt, _op, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800452 } \
453 } while (0)
454
Avi Kivity4d758342013-01-19 19:51:55 +0200455static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *));
456
Avi Kivityb7d491e2013-01-04 16:18:49 +0200457#define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t"
458#define FOP_RET "ret \n\t"
459
460#define FOP_START(op) \
461 extern void em_##op(struct fastop *fake); \
462 asm(".pushsection .text, \"ax\" \n\t" \
463 ".global em_" #op " \n\t" \
464 FOP_ALIGN \
465 "em_" #op ": \n\t"
466
467#define FOP_END \
468 ".popsection")
469
Avi Kivity0bdea062013-01-19 19:51:50 +0200470#define FOPNOP() FOP_ALIGN FOP_RET
471
Avi Kivityb7d491e2013-01-04 16:18:49 +0200472#define FOP1E(op, dst) \
473 FOP_ALIGN #op " %" #dst " \n\t" FOP_RET
474
475#define FASTOP1(op) \
476 FOP_START(op) \
477 FOP1E(op##b, al) \
478 FOP1E(op##w, ax) \
479 FOP1E(op##l, eax) \
480 ON64(FOP1E(op##q, rax)) \
481 FOP_END
482
Avi Kivityb9fa4092013-02-09 11:31:48 +0200483/* 1-operand, using src2 (for MUL/DIV r/m) */
484#define FASTOP1SRC2(op, name) \
485 FOP_START(name) \
486 FOP1E(op, cl) \
487 FOP1E(op, cx) \
488 FOP1E(op, ecx) \
489 ON64(FOP1E(op, rcx)) \
490 FOP_END
491
Avi Kivityf7857f32013-01-04 16:18:53 +0200492#define FOP2E(op, dst, src) \
493 FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET
494
495#define FASTOP2(op) \
496 FOP_START(op) \
Avi Kivity017da7b2013-02-09 11:31:47 +0200497 FOP2E(op##b, al, dl) \
498 FOP2E(op##w, ax, dx) \
499 FOP2E(op##l, eax, edx) \
500 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivityf7857f32013-01-04 16:18:53 +0200501 FOP_END
502
Avi Kivity11c363b2013-01-19 19:51:54 +0200503/* 2 operand, word only */
504#define FASTOP2W(op) \
505 FOP_START(op) \
506 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200507 FOP2E(op##w, ax, dx) \
508 FOP2E(op##l, eax, edx) \
509 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivity11c363b2013-01-19 19:51:54 +0200510 FOP_END
511
Avi Kivity007a3b52013-01-19 19:51:51 +0200512/* 2 operand, src is CL */
513#define FASTOP2CL(op) \
514 FOP_START(op) \
515 FOP2E(op##b, al, cl) \
516 FOP2E(op##w, ax, cl) \
517 FOP2E(op##l, eax, cl) \
518 ON64(FOP2E(op##q, rax, cl)) \
519 FOP_END
520
Avi Kivity0bdea062013-01-19 19:51:50 +0200521#define FOP3E(op, dst, src, src2) \
522 FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
523
524/* 3-operand, word-only, src2=cl */
525#define FASTOP3WCL(op) \
526 FOP_START(op) \
527 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200528 FOP3E(op##w, ax, dx, cl) \
529 FOP3E(op##l, eax, edx, cl) \
530 ON64(FOP3E(op##q, rax, rdx, cl)) \
Avi Kivity0bdea062013-01-19 19:51:50 +0200531 FOP_END
532
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200533/* Special case for SETcc - 1 instruction per cc */
534#define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t"
535
536FOP_START(setcc)
537FOP_SETCC(seto)
538FOP_SETCC(setno)
539FOP_SETCC(setc)
540FOP_SETCC(setnc)
541FOP_SETCC(setz)
542FOP_SETCC(setnz)
543FOP_SETCC(setbe)
544FOP_SETCC(setnbe)
545FOP_SETCC(sets)
546FOP_SETCC(setns)
547FOP_SETCC(setp)
548FOP_SETCC(setnp)
549FOP_SETCC(setl)
550FOP_SETCC(setnl)
551FOP_SETCC(setle)
552FOP_SETCC(setnle)
553FOP_END;
554
Paolo Bonzini326f5782013-05-09 11:32:51 +0200555FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET
556FOP_END;
557
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300558#define __emulate_1op_rax_rdx(ctxt, _op, _suffix, _ex) \
Avi Kivityf6b35972010-08-26 11:59:00 +0300559 do { \
560 unsigned long _tmp; \
Avi Kivityab2c5ce2013-02-09 11:31:46 +0200561 ulong *rax = &ctxt->dst.val; \
562 ulong *rdx = &ctxt->src.val; \
Avi Kivityf6b35972010-08-26 11:59:00 +0300563 \
564 __asm__ __volatile__ ( \
565 _PRE_EFLAGS("0", "5", "1") \
566 "1: \n\t" \
567 _op _suffix " %6; " \
568 "2: \n\t" \
569 _POST_EFLAGS("0", "5", "1") \
570 ".pushsection .fixup,\"ax\" \n\t" \
571 "3: movb $1, %4 \n\t" \
572 "jmp 2b \n\t" \
573 ".popsection \n\t" \
574 _ASM_EXTABLE(1b, 3b) \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300575 : "=m" ((ctxt)->eflags), "=&r" (_tmp), \
576 "+a" (*rax), "+d" (*rdx), "+qm"(_ex) \
Avi Kivityab2c5ce2013-02-09 11:31:46 +0200577 : "i" (EFLAGS_MASK), "m" ((ctxt)->src2.val)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300578 } while (0)
579
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300580/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300581#define emulate_1op_rax_rdx(ctxt, _op, _ex) \
Avi Kivity72952612011-04-20 13:12:27 +0300582 do { \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300583 switch((ctxt)->src.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300584 case 1: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300585 __emulate_1op_rax_rdx(ctxt, _op, "b", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300586 break; \
587 case 2: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300588 __emulate_1op_rax_rdx(ctxt, _op, "w", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300589 break; \
590 case 4: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300591 __emulate_1op_rax_rdx(ctxt, _op, "l", _ex); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300592 break; \
593 case 8: ON64( \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300594 __emulate_1op_rax_rdx(ctxt, _op, "q", _ex)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300595 break; \
596 } \
597 } while (0)
598
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200599static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
600 enum x86_intercept intercept,
601 enum x86_intercept_stage stage)
602{
603 struct x86_instruction_info info = {
604 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300605 .rep_prefix = ctxt->rep_prefix,
606 .modrm_mod = ctxt->modrm_mod,
607 .modrm_reg = ctxt->modrm_reg,
608 .modrm_rm = ctxt->modrm_rm,
609 .src_val = ctxt->src.val64,
610 .src_bytes = ctxt->src.bytes,
611 .dst_bytes = ctxt->dst.bytes,
612 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200613 .next_rip = ctxt->eip,
614 };
615
Avi Kivity29535382011-04-20 13:37:53 +0300616 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200617}
618
Avi Kivityf47cfa32012-06-07 17:49:24 +0300619static void assign_masked(ulong *dest, ulong src, ulong mask)
620{
621 *dest = (*dest & ~mask) | (src & mask);
622}
623
Avi Kivity9dac77f2011-06-01 15:34:25 +0300624static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800625{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300626 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800627}
628
Avi Kivityf47cfa32012-06-07 17:49:24 +0300629static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
630{
631 u16 sel;
632 struct desc_struct ss;
633
634 if (ctxt->mode == X86EMUL_MODE_PROT64)
635 return ~0UL;
636 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
637 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
638}
639
Avi Kivity612e89f2012-06-12 20:03:23 +0300640static int stack_size(struct x86_emulate_ctxt *ctxt)
641{
642 return (__fls(stack_mask(ctxt)) + 1) >> 3;
643}
644
Avi Kivity6aa8b732006-12-10 02:21:36 -0800645/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800646static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300647address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800648{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300649 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800650 return reg;
651 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300652 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800653}
654
655static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300656register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800657{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300658 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800659}
660
Avi Kivity5ad105e2012-08-19 14:34:31 +0300661static void masked_increment(ulong *reg, ulong mask, int inc)
662{
663 assign_masked(reg, *reg + inc, mask);
664}
665
Harvey Harrison7a9572752008-02-19 07:40:41 -0800666static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300667register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800668{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300669 ulong mask;
670
Avi Kivity9dac77f2011-06-01 15:34:25 +0300671 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300672 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800673 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300674 mask = ad_mask(ctxt);
675 masked_increment(reg, mask, inc);
676}
677
678static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
679{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300680 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800681}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800682
Avi Kivity9dac77f2011-06-01 15:34:25 +0300683static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800684{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300685 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800686}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300687
Avi Kivity56697682011-04-03 14:08:51 +0300688static u32 desc_limit_scaled(struct desc_struct *desc)
689{
690 u32 limit = get_desc_limit(desc);
691
692 return desc->g ? (limit << 12) | 0xfff : limit;
693}
694
Avi Kivity9dac77f2011-06-01 15:34:25 +0300695static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300696{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300697 ctxt->has_seg_override = true;
698 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300699}
700
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900701static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300702{
703 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
704 return 0;
705
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900706 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300707}
708
Avi Kivity9dac77f2011-06-01 15:34:25 +0300709static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300710{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300711 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300712 return 0;
713
Avi Kivity9dac77f2011-06-01 15:34:25 +0300714 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300715}
716
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200717static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
718 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300719{
Avi Kivityda9cb572010-11-22 17:53:21 +0200720 ctxt->exception.vector = vec;
721 ctxt->exception.error_code = error;
722 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200723 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300724}
725
Joerg Roedel3b88e412011-04-04 12:39:29 +0200726static int emulate_db(struct x86_emulate_ctxt *ctxt)
727{
728 return emulate_exception(ctxt, DB_VECTOR, 0, false);
729}
730
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200731static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300732{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200733 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300734}
735
Avi Kivity618ff152011-04-03 12:32:09 +0300736static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
737{
738 return emulate_exception(ctxt, SS_VECTOR, err, true);
739}
740
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200741static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300742{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200743 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300744}
745
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200746static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300747{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200748 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300749}
750
Avi Kivity34d1f492010-08-26 11:59:01 +0300751static int emulate_de(struct x86_emulate_ctxt *ctxt)
752{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200753 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300754}
755
Avi Kivity1253791d2011-03-29 11:41:27 +0200756static int emulate_nm(struct x86_emulate_ctxt *ctxt)
757{
758 return emulate_exception(ctxt, NM_VECTOR, 0, false);
759}
760
Avi Kivity1aa36612011-04-27 13:20:30 +0300761static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
762{
763 u16 selector;
764 struct desc_struct desc;
765
766 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
767 return selector;
768}
769
770static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
771 unsigned seg)
772{
773 u16 dummy;
774 u32 base3;
775 struct desc_struct desc;
776
777 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
778 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
779}
780
Avi Kivity1c11b372012-04-09 18:39:59 +0300781/*
782 * x86 defines three classes of vector instructions: explicitly
783 * aligned, explicitly unaligned, and the rest, which change behaviour
784 * depending on whether they're AVX encoded or not.
785 *
786 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
787 * subject to the same check.
788 */
789static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
790{
791 if (likely(size < 16))
792 return false;
793
794 if (ctxt->d & Aligned)
795 return true;
796 else if (ctxt->d & Unaligned)
797 return false;
798 else if (ctxt->d & Avx)
799 return false;
800 else
801 return true;
802}
803
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400804static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300805 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400806 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300807 ulong *linear)
808{
Avi Kivity618ff152011-04-03 12:32:09 +0300809 struct desc_struct desc;
810 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300811 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300812 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300813 u16 sel;
Gleb Natapov3a78a4f2012-12-20 16:57:42 +0200814 unsigned cpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300815
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900816 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300817 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300818 case X86EMUL_MODE_PROT64:
819 if (((signed long)la << 16) >> 16 != la)
820 return emulate_gp(ctxt, 0);
821 break;
822 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300823 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
824 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300825 if (!usable)
826 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200827 /* code segment in protected mode or read-only data segment */
828 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
829 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300830 goto bad;
831 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400832 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300833 goto bad;
834 lim = desc_limit_scaled(&desc);
835 if ((desc.type & 8) || !(desc.type & 4)) {
836 /* expand-up segment */
837 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
838 goto bad;
839 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800840 /* expand-down segment */
Avi Kivity618ff152011-04-03 12:32:09 +0300841 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
842 goto bad;
843 lim = desc.d ? 0xffffffff : 0xffff;
844 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
845 goto bad;
846 }
Avi Kivity717746e2011-04-20 13:37:53 +0300847 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity618ff152011-04-03 12:32:09 +0300848 if (!(desc.type & 8)) {
849 /* data segment */
850 if (cpl > desc.dpl)
851 goto bad;
852 } else if ((desc.type & 8) && !(desc.type & 4)) {
853 /* nonconforming code segment */
854 if (cpl != desc.dpl)
855 goto bad;
856 } else if ((desc.type & 8) && (desc.type & 4)) {
857 /* conforming code segment */
858 if (cpl < desc.dpl)
859 goto bad;
860 }
861 break;
862 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300863 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300864 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300865 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
866 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300867 *linear = la;
868 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300869bad:
870 if (addr.seg == VCPU_SREG_SS)
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300871 return emulate_ss(ctxt, sel);
Avi Kivity618ff152011-04-03 12:32:09 +0300872 else
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300873 return emulate_gp(ctxt, sel);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300874}
875
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400876static int linearize(struct x86_emulate_ctxt *ctxt,
877 struct segmented_address addr,
878 unsigned size, bool write,
879 ulong *linear)
880{
881 return __linearize(ctxt, addr, size, write, false, linear);
882}
883
884
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200885static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
886 struct segmented_address addr,
887 void *data,
888 unsigned size)
889{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200890 int rc;
891 ulong linear;
892
Avi Kivity83b87952011-04-03 11:31:19 +0300893 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200894 if (rc != X86EMUL_CONTINUE)
895 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300896 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200897}
898
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900899/*
900 * Fetch the next byte of the instruction being emulated which is pointed to
901 * by ctxt->_eip, then increment ctxt->_eip.
902 *
903 * Also prefetch the remaining bytes of the instruction without crossing page
904 * boundary if they are not in fetch_cache yet.
905 */
906static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200907{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300908 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200909 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300910 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200911
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900912 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400913 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900914 struct segmented_address addr = { .seg = VCPU_SREG_CS,
915 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300916 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900917 size = min(15UL - cur_size,
918 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400919 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900920 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400921 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900922 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
923 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900924 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200925 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300926 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200927 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900928 *dest = fc->data[ctxt->_eip - fc->start];
929 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900930 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200931}
932
933static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900934 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200935{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900936 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200937
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200938 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900939 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200940 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200941 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900942 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900943 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200944 return rc;
945 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900946 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200947}
948
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900949/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900950#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900951({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900952 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900953 if (rc != X86EMUL_CONTINUE) \
954 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900955 (_type)_x; \
956})
957
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900958#define insn_fetch_arr(_arr, _size, _ctxt) \
959({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900960 if (rc != X86EMUL_CONTINUE) \
961 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900962})
963
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000964/*
965 * Given the 'reg' portion of a ModRM byte, and a register block, return a
966 * pointer into the block that addresses the relevant register.
967 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
968 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300969static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000970 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971{
972 void *p;
973
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300975 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
976 else
977 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800978 return p;
979}
980
981static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200982 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800983 u16 *size, unsigned long *address, int op_bytes)
984{
985 int rc;
986
987 if (op_bytes == 2)
988 op_bytes = 3;
989 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200990 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900991 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200993 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200994 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995 return rc;
996}
997
Avi Kivity34b77652013-01-19 19:51:56 +0200998FASTOP2(add);
999FASTOP2(or);
1000FASTOP2(adc);
1001FASTOP2(sbb);
1002FASTOP2(and);
1003FASTOP2(sub);
1004FASTOP2(xor);
1005FASTOP2(cmp);
1006FASTOP2(test);
1007
Avi Kivityb9fa4092013-02-09 11:31:48 +02001008FASTOP1SRC2(mul, mul_ex);
1009FASTOP1SRC2(imul, imul_ex);
1010
Avi Kivity34b77652013-01-19 19:51:56 +02001011FASTOP3WCL(shld);
1012FASTOP3WCL(shrd);
1013
1014FASTOP2W(imul);
1015
1016FASTOP1(not);
1017FASTOP1(neg);
1018FASTOP1(inc);
1019FASTOP1(dec);
1020
1021FASTOP2CL(rol);
1022FASTOP2CL(ror);
1023FASTOP2CL(rcl);
1024FASTOP2CL(rcr);
1025FASTOP2CL(shl);
1026FASTOP2CL(shr);
1027FASTOP2CL(sar);
1028
1029FASTOP2W(bsf);
1030FASTOP2W(bsr);
1031FASTOP2W(bt);
1032FASTOP2W(bts);
1033FASTOP2W(btr);
1034FASTOP2W(btc);
1035
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001036static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001037{
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001038 u8 rc;
1039 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001040
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001041 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +02001042 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001043 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
1044 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001045}
1046
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001047static void fetch_register_operand(struct operand *op)
1048{
1049 switch (op->bytes) {
1050 case 1:
1051 op->val = *(u8 *)op->addr.reg;
1052 break;
1053 case 2:
1054 op->val = *(u16 *)op->addr.reg;
1055 break;
1056 case 4:
1057 op->val = *(u32 *)op->addr.reg;
1058 break;
1059 case 8:
1060 op->val = *(u64 *)op->addr.reg;
1061 break;
1062 }
1063}
1064
Avi Kivity1253791d2011-03-29 11:41:27 +02001065static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
1066{
1067 ctxt->ops->get_fpu(ctxt);
1068 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001069 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
1070 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
1071 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
1072 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
1073 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
1074 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
1075 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
1076 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001077#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001078 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
1079 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
1080 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
1081 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
1082 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
1083 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
1084 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
1085 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001086#endif
1087 default: BUG();
1088 }
1089 ctxt->ops->put_fpu(ctxt);
1090}
1091
1092static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
1093 int reg)
1094{
1095 ctxt->ops->get_fpu(ctxt);
1096 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001097 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
1098 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
1099 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
1100 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
1101 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
1102 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
1103 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
1104 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001105#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001106 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
1107 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
1108 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
1109 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
1110 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
1111 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
1112 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
1113 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001114#endif
1115 default: BUG();
1116 }
1117 ctxt->ops->put_fpu(ctxt);
1118}
1119
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001120static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1121{
1122 ctxt->ops->get_fpu(ctxt);
1123 switch (reg) {
1124 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1125 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1126 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1127 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1128 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1129 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1130 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1131 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1132 default: BUG();
1133 }
1134 ctxt->ops->put_fpu(ctxt);
1135}
1136
1137static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1138{
1139 ctxt->ops->get_fpu(ctxt);
1140 switch (reg) {
1141 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1142 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1143 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1144 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1145 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1146 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1147 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1148 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1149 default: BUG();
1150 }
1151 ctxt->ops->put_fpu(ctxt);
1152}
1153
Gleb Natapov045a2822012-12-20 16:57:43 +02001154static int em_fninit(struct x86_emulate_ctxt *ctxt)
1155{
1156 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1157 return emulate_nm(ctxt);
1158
1159 ctxt->ops->get_fpu(ctxt);
1160 asm volatile("fninit");
1161 ctxt->ops->put_fpu(ctxt);
1162 return X86EMUL_CONTINUE;
1163}
1164
1165static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1166{
1167 u16 fcw;
1168
1169 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1170 return emulate_nm(ctxt);
1171
1172 ctxt->ops->get_fpu(ctxt);
1173 asm volatile("fnstcw %0": "+m"(fcw));
1174 ctxt->ops->put_fpu(ctxt);
1175
1176 /* force 2 byte destination */
1177 ctxt->dst.bytes = 2;
1178 ctxt->dst.val = fcw;
1179
1180 return X86EMUL_CONTINUE;
1181}
1182
1183static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1184{
1185 u16 fsw;
1186
1187 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1188 return emulate_nm(ctxt);
1189
1190 ctxt->ops->get_fpu(ctxt);
1191 asm volatile("fnstsw %0": "+m"(fsw));
1192 ctxt->ops->put_fpu(ctxt);
1193
1194 /* force 2 byte destination */
1195 ctxt->dst.bytes = 2;
1196 ctxt->dst.val = fsw;
1197
1198 return X86EMUL_CONTINUE;
1199}
1200
Avi Kivity1253791d2011-03-29 11:41:27 +02001201static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001202 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001203{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001204 unsigned reg = ctxt->modrm_reg;
1205 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +02001206
Avi Kivity9dac77f2011-06-01 15:34:25 +03001207 if (!(ctxt->d & ModRM))
1208 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity1253791d2011-03-29 11:41:27 +02001209
Avi Kivity9dac77f2011-06-01 15:34:25 +03001210 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001211 op->type = OP_XMM;
1212 op->bytes = 16;
1213 op->addr.xmm = reg;
1214 read_sse_reg(ctxt, &op->vec_val, reg);
1215 return;
1216 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001217 if (ctxt->d & Mmx) {
1218 reg &= 7;
1219 op->type = OP_MM;
1220 op->bytes = 8;
1221 op->addr.mm = reg;
1222 return;
1223 }
Avi Kivity1253791d2011-03-29 11:41:27 +02001224
Avi Kivity3c118e22007-10-31 10:27:04 +02001225 op->type = OP_REG;
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001226 if (ctxt->d & ByteOp) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001227 op->addr.reg = decode_register(ctxt, reg, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +02001228 op->bytes = 1;
1229 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001230 op->addr.reg = decode_register(ctxt, reg, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001231 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +02001232 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001233 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001234 op->orig_val = op->val;
1235}
1236
Avi Kivitya6e34072012-06-10 17:15:39 +03001237static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1238{
1239 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1240 ctxt->modrm_seg = VCPU_SREG_SS;
1241}
1242
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001243static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001244 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001245{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001246 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001247 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001248 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001249 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001250
Avi Kivity9dac77f2011-06-01 15:34:25 +03001251 if (ctxt->rex_prefix) {
1252 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
1253 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
1254 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001255 }
1256
Avi Kivity9dac77f2011-06-01 15:34:25 +03001257 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
1258 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
1259 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
1260 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001261
Avi Kivity9dac77f2011-06-01 15:34:25 +03001262 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001263 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001264 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001265 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm, ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001266 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001267 op->type = OP_XMM;
1268 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001269 op->addr.xmm = ctxt->modrm_rm;
1270 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001271 return rc;
1272 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001273 if (ctxt->d & Mmx) {
1274 op->type = OP_MM;
1275 op->bytes = 8;
1276 op->addr.xmm = ctxt->modrm_rm & 7;
1277 return rc;
1278 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001279 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001280 return rc;
1281 }
1282
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001283 op->type = OP_MEM;
1284
Avi Kivity9dac77f2011-06-01 15:34:25 +03001285 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001286 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1287 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1288 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1289 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001290
1291 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001292 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001293 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001294 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001295 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001296 break;
1297 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001298 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001299 break;
1300 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001301 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001302 break;
1303 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001304 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001305 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001306 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001307 break;
1308 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001309 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001310 break;
1311 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001312 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001313 break;
1314 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001315 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001316 break;
1317 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001318 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001319 break;
1320 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001321 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001322 break;
1323 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001324 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001325 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001326 break;
1327 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001328 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001329 break;
1330 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001331 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1332 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1333 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001334 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001335 } else {
1336 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001337 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001338 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001339 index_reg |= (sib >> 3) & 7;
1340 base_reg |= sib & 7;
1341 scale = sib >> 6;
1342
Avi Kivity9dac77f2011-06-01 15:34:25 +03001343 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001344 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001345 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001346 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001347 adjust_modrm_seg(ctxt, base_reg);
1348 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001349 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001350 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001351 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001352 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001353 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001354 } else {
1355 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001356 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001357 adjust_modrm_seg(ctxt, base_reg);
1358 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001359 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001360 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001361 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001362 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001363 break;
1364 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001365 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001366 break;
1367 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001368 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001369 break;
1370 }
1371 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001372 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001373done:
1374 return rc;
1375}
1376
1377static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001378 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001379{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001380 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001381
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001382 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001383 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001384 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001385 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001386 break;
1387 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001388 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001389 break;
1390 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001391 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001392 break;
1393 }
1394done:
1395 return rc;
1396}
1397
Avi Kivity9dac77f2011-06-01 15:34:25 +03001398static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001399{
Sheng Yang7129eec2010-09-28 16:33:32 +08001400 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001401
Avi Kivity9dac77f2011-06-01 15:34:25 +03001402 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1403 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001404
Avi Kivity9dac77f2011-06-01 15:34:25 +03001405 if (ctxt->src.bytes == 2)
1406 sv = (s16)ctxt->src.val & (s16)mask;
1407 else if (ctxt->src.bytes == 4)
1408 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001409
Avi Kivity9dac77f2011-06-01 15:34:25 +03001410 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001411 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001412
1413 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001414 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001415}
1416
Gleb Natapov9de41572010-04-28 19:15:22 +03001417static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001418 unsigned long addr, void *dest, unsigned size)
1419{
1420 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001421 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001422
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001423 if (mc->pos < mc->end)
1424 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001425
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001426 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001427
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001428 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1429 &ctxt->exception);
1430 if (rc != X86EMUL_CONTINUE)
1431 return rc;
1432
1433 mc->end += size;
1434
1435read_cached:
1436 memcpy(dest, mc->data + mc->pos, size);
1437 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001438 return X86EMUL_CONTINUE;
1439}
1440
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001441static int segmented_read(struct x86_emulate_ctxt *ctxt,
1442 struct segmented_address addr,
1443 void *data,
1444 unsigned size)
1445{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001446 int rc;
1447 ulong linear;
1448
Avi Kivity83b87952011-04-03 11:31:19 +03001449 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001450 if (rc != X86EMUL_CONTINUE)
1451 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001452 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001453}
1454
1455static int segmented_write(struct x86_emulate_ctxt *ctxt,
1456 struct segmented_address addr,
1457 const void *data,
1458 unsigned size)
1459{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001460 int rc;
1461 ulong linear;
1462
Avi Kivity83b87952011-04-03 11:31:19 +03001463 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001464 if (rc != X86EMUL_CONTINUE)
1465 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001466 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1467 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001468}
1469
1470static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1471 struct segmented_address addr,
1472 const void *orig_data, const void *data,
1473 unsigned size)
1474{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001475 int rc;
1476 ulong linear;
1477
Avi Kivity83b87952011-04-03 11:31:19 +03001478 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001479 if (rc != X86EMUL_CONTINUE)
1480 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001481 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1482 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001483}
1484
Gleb Natapov7b262e92010-03-18 15:20:27 +02001485static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001486 unsigned int size, unsigned short port,
1487 void *dest)
1488{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001489 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001490
1491 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001492 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001493 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001494 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001495 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001496 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1497 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Gleb Natapov7b262e92010-03-18 15:20:27 +02001498 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1499 count);
1500 if (n == 0)
1501 n = 1;
1502 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001503 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001504 return 0;
1505 rc->end = n * size;
1506 }
1507
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001508 if (ctxt->rep_prefix && !(ctxt->eflags & EFLG_DF)) {
1509 ctxt->dst.data = rc->data + rc->pos;
1510 ctxt->dst.type = OP_MEM_STR;
1511 ctxt->dst.count = (rc->end - rc->pos) / size;
1512 rc->pos = rc->end;
1513 } else {
1514 memcpy(dest, rc->data + rc->pos, size);
1515 rc->pos += size;
1516 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001517 return 1;
1518}
1519
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001520static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1521 u16 index, struct desc_struct *desc)
1522{
1523 struct desc_ptr dt;
1524 ulong addr;
1525
1526 ctxt->ops->get_idt(ctxt, &dt);
1527
1528 if (dt.size < index * 8 + 7)
1529 return emulate_gp(ctxt, index << 3 | 0x2);
1530
1531 addr = dt.address + index * 8;
1532 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1533 &ctxt->exception);
1534}
1535
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001536static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001537 u16 selector, struct desc_ptr *dt)
1538{
Mathias Krause0225fb52012-08-30 01:30:16 +02001539 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001540
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001541 if (selector & 1 << 2) {
1542 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001543 u16 sel;
1544
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001545 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001546 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001547 return;
1548
1549 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1550 dt->address = get_desc_base(&desc);
1551 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001552 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001553}
1554
1555/* allowed just for 8 bytes segments */
1556static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001557 u16 selector, struct desc_struct *desc,
1558 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001559{
1560 struct desc_ptr dt;
1561 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001562 ulong addr;
1563
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001564 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001565
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001566 if (dt.size < index * 8 + 7)
1567 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001568
Avi Kivitye9194642012-06-13 16:29:39 +03001569 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001570 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1571 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001572}
1573
1574/* allowed just for 8 bytes segments */
1575static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001576 u16 selector, struct desc_struct *desc)
1577{
1578 struct desc_ptr dt;
1579 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001580 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001581
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001582 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001583
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001584 if (dt.size < index * 8 + 7)
1585 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001586
1587 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001588 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1589 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001590}
1591
Gleb Natapov5601d052011-03-07 14:55:06 +02001592/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001593static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001594 u16 selector, int seg)
1595{
Avi Kivity869be992012-06-13 16:30:53 +03001596 struct desc_struct seg_desc, old_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001597 u8 dpl, rpl, cpl;
1598 unsigned err_vec = GP_VECTOR;
1599 u32 err_code = 0;
1600 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001601 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001602 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001603 u16 dummy;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001604
1605 memset(&seg_desc, 0, sizeof seg_desc);
1606
Kevin Wolff8da94e2013-04-11 14:06:03 +02001607 if (ctxt->mode == X86EMUL_MODE_REAL) {
1608 /* set real mode segment descriptor (keep limit etc. for
1609 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001610 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001611 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001612 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001613 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1614 /* VM86 needs a clean new segment descriptor */
1615 set_desc_base(&seg_desc, selector << 4);
1616 set_desc_limit(&seg_desc, 0xffff);
1617 seg_desc.type = 3;
1618 seg_desc.p = 1;
1619 seg_desc.s = 1;
1620 seg_desc.dpl = 3;
1621 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001622 }
1623
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001624 rpl = selector & 3;
1625 cpl = ctxt->ops->cpl(ctxt);
1626
1627 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1628 if ((seg == VCPU_SREG_CS
1629 || (seg == VCPU_SREG_SS
1630 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1631 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001632 && null_selector)
1633 goto exception;
1634
1635 /* TR should be in GDT only */
1636 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1637 goto exception;
1638
1639 if (null_selector) /* for NULL selector skip all following checks */
1640 goto load;
1641
Avi Kivitye9194642012-06-13 16:29:39 +03001642 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001643 if (ret != X86EMUL_CONTINUE)
1644 return ret;
1645
1646 err_code = selector & 0xfffc;
1647 err_vec = GP_VECTOR;
1648
Guo Chaofc058682012-06-28 15:19:51 +08001649 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001650 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1651 goto exception;
1652
1653 if (!seg_desc.p) {
1654 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1655 goto exception;
1656 }
1657
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001658 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001659
1660 switch (seg) {
1661 case VCPU_SREG_SS:
1662 /*
1663 * segment is not a writable data segment or segment
1664 * selector's RPL != CPL or segment selector's RPL != CPL
1665 */
1666 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1667 goto exception;
1668 break;
1669 case VCPU_SREG_CS:
1670 if (!(seg_desc.type & 8))
1671 goto exception;
1672
1673 if (seg_desc.type & 4) {
1674 /* conforming */
1675 if (dpl > cpl)
1676 goto exception;
1677 } else {
1678 /* nonconforming */
1679 if (rpl > cpl || dpl != cpl)
1680 goto exception;
1681 }
1682 /* CS(RPL) <- CPL */
1683 selector = (selector & 0xfffc) | cpl;
1684 break;
1685 case VCPU_SREG_TR:
1686 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1687 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001688 old_desc = seg_desc;
1689 seg_desc.type |= 2; /* busy */
1690 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1691 sizeof(seg_desc), &ctxt->exception);
1692 if (ret != X86EMUL_CONTINUE)
1693 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001694 break;
1695 case VCPU_SREG_LDTR:
1696 if (seg_desc.s || seg_desc.type != 2)
1697 goto exception;
1698 break;
1699 default: /* DS, ES, FS, or GS */
1700 /*
1701 * segment is not a data or readable code segment or
1702 * ((segment is a data or nonconforming code segment)
1703 * and (both RPL and CPL > DPL))
1704 */
1705 if ((seg_desc.type & 0xa) == 0x8 ||
1706 (((seg_desc.type & 0xc) != 0xc) &&
1707 (rpl > dpl && cpl > dpl)))
1708 goto exception;
1709 break;
1710 }
1711
1712 if (seg_desc.s) {
1713 /* mark segment as accessed */
1714 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001715 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001716 if (ret != X86EMUL_CONTINUE)
1717 return ret;
1718 }
1719load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001720 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001721 return X86EMUL_CONTINUE;
1722exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001723 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001724 return X86EMUL_PROPAGATE_FAULT;
1725}
1726
Wei Yongjun31be40b2010-08-17 09:17:30 +08001727static void write_register_operand(struct operand *op)
1728{
1729 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1730 switch (op->bytes) {
1731 case 1:
1732 *(u8 *)op->addr.reg = (u8)op->val;
1733 break;
1734 case 2:
1735 *(u16 *)op->addr.reg = (u16)op->val;
1736 break;
1737 case 4:
1738 *op->addr.reg = (u32)op->val;
1739 break; /* 64b: zero-extend */
1740 case 8:
1741 *op->addr.reg = op->val;
1742 break;
1743 }
1744}
1745
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001746static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001747{
1748 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001749
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001750 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001751 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001752 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001753 break;
1754 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001755 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001756 rc = segmented_cmpxchg(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001757 op->addr.mem,
1758 &op->orig_val,
1759 &op->val,
1760 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001761 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001762 rc = segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001763 op->addr.mem,
1764 &op->val,
1765 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001766 if (rc != X86EMUL_CONTINUE)
1767 return rc;
1768 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001769 case OP_MEM_STR:
1770 rc = segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001771 op->addr.mem,
1772 op->data,
1773 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001774 if (rc != X86EMUL_CONTINUE)
1775 return rc;
1776 break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001777 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001778 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001779 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001780 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001781 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001782 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001783 case OP_NONE:
1784 /* no writeback */
1785 break;
1786 default:
1787 break;
1788 }
1789 return X86EMUL_CONTINUE;
1790}
1791
Avi Kivity51ddff52012-06-12 20:19:40 +03001792static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001793{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001794 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001795
Avi Kivity5ad105e2012-08-19 14:34:31 +03001796 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001797 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001798 addr.seg = VCPU_SREG_SS;
1799
Avi Kivity51ddff52012-06-12 20:19:40 +03001800 return segmented_write(ctxt, addr, data, bytes);
1801}
1802
1803static int em_push(struct x86_emulate_ctxt *ctxt)
1804{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001805 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001806 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001807 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001808}
1809
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001810static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001811 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001812{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001813 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001814 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001815
Avi Kivitydd856ef2012-08-27 23:46:17 +03001816 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001817 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001818 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001819 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001820 return rc;
1821
Avi Kivity5ad105e2012-08-19 14:34:31 +03001822 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001823 return rc;
1824}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001825
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001826static int em_pop(struct x86_emulate_ctxt *ctxt)
1827{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001828 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001829}
1830
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001831static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001832 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001833{
1834 int rc;
1835 unsigned long val, change_mask;
1836 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001837 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001838
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001839 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001840 if (rc != X86EMUL_CONTINUE)
1841 return rc;
1842
1843 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1844 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1845
1846 switch(ctxt->mode) {
1847 case X86EMUL_MODE_PROT64:
1848 case X86EMUL_MODE_PROT32:
1849 case X86EMUL_MODE_PROT16:
1850 if (cpl == 0)
1851 change_mask |= EFLG_IOPL;
1852 if (cpl <= iopl)
1853 change_mask |= EFLG_IF;
1854 break;
1855 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001856 if (iopl < 3)
1857 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001858 change_mask |= EFLG_IF;
1859 break;
1860 default: /* real mode */
1861 change_mask |= (EFLG_IOPL | EFLG_IF);
1862 break;
1863 }
1864
1865 *(unsigned long *)dest =
1866 (ctxt->eflags & ~change_mask) | (val & change_mask);
1867
1868 return rc;
1869}
1870
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001871static int em_popf(struct x86_emulate_ctxt *ctxt)
1872{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001873 ctxt->dst.type = OP_REG;
1874 ctxt->dst.addr.reg = &ctxt->eflags;
1875 ctxt->dst.bytes = ctxt->op_bytes;
1876 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001877}
1878
Avi Kivity612e89f2012-06-12 20:03:23 +03001879static int em_enter(struct x86_emulate_ctxt *ctxt)
1880{
1881 int rc;
1882 unsigned frame_size = ctxt->src.val;
1883 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001884 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001885
1886 if (nesting_level)
1887 return X86EMUL_UNHANDLEABLE;
1888
Avi Kivitydd856ef2012-08-27 23:46:17 +03001889 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1890 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001891 if (rc != X86EMUL_CONTINUE)
1892 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001893 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001894 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001895 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1896 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001897 stack_mask(ctxt));
1898 return X86EMUL_CONTINUE;
1899}
1900
Avi Kivityf47cfa32012-06-07 17:49:24 +03001901static int em_leave(struct x86_emulate_ctxt *ctxt)
1902{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001903 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001904 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001905 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001906}
1907
Avi Kivity1cd196e2011-09-13 10:45:51 +03001908static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001909{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001910 int seg = ctxt->src2.val;
1911
Avi Kivity9dac77f2011-06-01 15:34:25 +03001912 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001913
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001914 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001915}
1916
Avi Kivity1cd196e2011-09-13 10:45:51 +03001917static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001918{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001919 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001920 unsigned long selector;
1921 int rc;
1922
Avi Kivity9dac77f2011-06-01 15:34:25 +03001923 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001924 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001925 return rc;
1926
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001927 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001928 return rc;
1929}
1930
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001931static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001932{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001933 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001934 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001935 int reg = VCPU_REGS_RAX;
1936
1937 while (reg <= VCPU_REGS_RDI) {
1938 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001939 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001940
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001941 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001942 if (rc != X86EMUL_CONTINUE)
1943 return rc;
1944
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001945 ++reg;
1946 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001947
Wei Yongjunc37eda12010-06-15 09:03:33 +08001948 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001949}
1950
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001951static int em_pushf(struct x86_emulate_ctxt *ctxt)
1952{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001953 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001954 return em_push(ctxt);
1955}
1956
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001957static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001958{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001959 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001960 int reg = VCPU_REGS_RDI;
1961
1962 while (reg >= VCPU_REGS_RAX) {
1963 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001964 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001965 --reg;
1966 }
1967
Avi Kivitydd856ef2012-08-27 23:46:17 +03001968 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001969 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001970 break;
1971 --reg;
1972 }
1973 return rc;
1974}
1975
Avi Kivitydd856ef2012-08-27 23:46:17 +03001976static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001977{
Mathias Krause0225fb52012-08-30 01:30:16 +02001978 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001979 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001980 struct desc_ptr dt;
1981 gva_t cs_addr;
1982 gva_t eip_addr;
1983 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001984
1985 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001986 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001987 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001988 if (rc != X86EMUL_CONTINUE)
1989 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001990
1991 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1992
Avi Kivity9dac77f2011-06-01 15:34:25 +03001993 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001994 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001995 if (rc != X86EMUL_CONTINUE)
1996 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001997
Avi Kivity9dac77f2011-06-01 15:34:25 +03001998 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001999 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03002000 if (rc != X86EMUL_CONTINUE)
2001 return rc;
2002
Avi Kivity4bff1e862011-04-20 13:37:53 +03002003 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002004
2005 eip_addr = dt.address + (irq << 2);
2006 cs_addr = dt.address + (irq << 2) + 2;
2007
Avi Kivity0f65dd72011-04-20 13:37:53 +03002008 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002009 if (rc != X86EMUL_CONTINUE)
2010 return rc;
2011
Avi Kivity0f65dd72011-04-20 13:37:53 +03002012 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002013 if (rc != X86EMUL_CONTINUE)
2014 return rc;
2015
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002016 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002017 if (rc != X86EMUL_CONTINUE)
2018 return rc;
2019
Avi Kivity9dac77f2011-06-01 15:34:25 +03002020 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002021
2022 return rc;
2023}
2024
Avi Kivitydd856ef2012-08-27 23:46:17 +03002025int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
2026{
2027 int rc;
2028
2029 invalidate_registers(ctxt);
2030 rc = __emulate_int_real(ctxt, irq);
2031 if (rc == X86EMUL_CONTINUE)
2032 writeback_registers(ctxt);
2033 return rc;
2034}
2035
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002036static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002037{
2038 switch(ctxt->mode) {
2039 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03002040 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002041 case X86EMUL_MODE_VM86:
2042 case X86EMUL_MODE_PROT16:
2043 case X86EMUL_MODE_PROT32:
2044 case X86EMUL_MODE_PROT64:
2045 default:
2046 /* Protected mode interrupts unimplemented yet */
2047 return X86EMUL_UNHANDLEABLE;
2048 }
2049}
2050
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002051static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002052{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002053 int rc = X86EMUL_CONTINUE;
2054 unsigned long temp_eip = 0;
2055 unsigned long temp_eflags = 0;
2056 unsigned long cs = 0;
2057 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
2058 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
2059 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
2060 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
2061
2062 /* TODO: Add stack limit check */
2063
Avi Kivity9dac77f2011-06-01 15:34:25 +03002064 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002065
2066 if (rc != X86EMUL_CONTINUE)
2067 return rc;
2068
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002069 if (temp_eip & ~0xffff)
2070 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002071
Avi Kivity9dac77f2011-06-01 15:34:25 +03002072 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002073
2074 if (rc != X86EMUL_CONTINUE)
2075 return rc;
2076
Avi Kivity9dac77f2011-06-01 15:34:25 +03002077 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002078
2079 if (rc != X86EMUL_CONTINUE)
2080 return rc;
2081
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002082 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002083
2084 if (rc != X86EMUL_CONTINUE)
2085 return rc;
2086
Avi Kivity9dac77f2011-06-01 15:34:25 +03002087 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002088
2089
Avi Kivity9dac77f2011-06-01 15:34:25 +03002090 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002091 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03002092 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002093 ctxt->eflags &= ~0xffff;
2094 ctxt->eflags |= temp_eflags;
2095 }
2096
2097 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
2098 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
2099
2100 return rc;
2101}
2102
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002103static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002104{
2105 switch(ctxt->mode) {
2106 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002107 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002108 case X86EMUL_MODE_VM86:
2109 case X86EMUL_MODE_PROT16:
2110 case X86EMUL_MODE_PROT32:
2111 case X86EMUL_MODE_PROT64:
2112 default:
2113 /* iret from protected mode unimplemented yet */
2114 return X86EMUL_UNHANDLEABLE;
2115 }
2116}
2117
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002118static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2119{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002120 int rc;
2121 unsigned short sel;
2122
Avi Kivity9dac77f2011-06-01 15:34:25 +03002123 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002124
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002125 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002126 if (rc != X86EMUL_CONTINUE)
2127 return rc;
2128
Avi Kivity9dac77f2011-06-01 15:34:25 +03002129 ctxt->_eip = 0;
2130 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002131 return X86EMUL_CONTINUE;
2132}
2133
Avi Kivity3329ece2011-09-13 10:45:39 +03002134static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002135{
Avi Kivity34d1f492010-08-26 11:59:01 +03002136 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002137
Avi Kivity3329ece2011-09-13 10:45:39 +03002138 emulate_1op_rax_rdx(ctxt, "div", de);
2139 if (de)
2140 return emulate_de(ctxt);
2141 return X86EMUL_CONTINUE;
2142}
2143
2144static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
2145{
2146 u8 de = 0;
2147
2148 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03002149 if (de)
2150 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03002151 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002152}
2153
Takuya Yoshikawa51187682011-05-02 02:29:17 +09002154static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002155{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002156 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002157
Avi Kivity9dac77f2011-06-01 15:34:25 +03002158 switch (ctxt->modrm_reg) {
Mohammed Gamald19292e2008-09-08 21:47:19 +03002159 case 2: /* call near abs */ {
2160 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002161 old_eip = ctxt->_eip;
2162 ctxt->_eip = ctxt->src.val;
2163 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002164 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03002165 break;
2166 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002167 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002168 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002169 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002170 case 5: /* jmp far */
2171 rc = em_jmp_far(ctxt);
2172 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002173 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002174 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002175 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002176 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002177 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002178}
2179
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002180static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002181{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002182 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002183
Avi Kivitydd856ef2012-08-27 23:46:17 +03002184 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2185 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2186 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2187 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002188 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002189 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002190 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2191 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002192
Laurent Vivier05f086f2007-09-24 11:10:55 +02002193 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002194 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002195 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002196}
2197
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002198static int em_ret(struct x86_emulate_ctxt *ctxt)
2199{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002200 ctxt->dst.type = OP_REG;
2201 ctxt->dst.addr.reg = &ctxt->_eip;
2202 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002203 return em_pop(ctxt);
2204}
2205
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002206static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002207{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002208 int rc;
2209 unsigned long cs;
2210
Avi Kivity9dac77f2011-06-01 15:34:25 +03002211 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002212 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002213 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002214 if (ctxt->op_bytes == 4)
2215 ctxt->_eip = (u32)ctxt->_eip;
2216 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002217 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002218 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002219 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002220 return rc;
2221}
2222
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002223static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2224{
2225 /* Save real source value, then compare EAX against destination. */
2226 ctxt->src.orig_val = ctxt->src.val;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002227 ctxt->src.val = reg_read(ctxt, VCPU_REGS_RAX);
Avi Kivity158de572013-01-19 19:51:57 +02002228 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002229
2230 if (ctxt->eflags & EFLG_ZF) {
2231 /* Success: write back to memory. */
2232 ctxt->dst.val = ctxt->src.orig_val;
2233 } else {
2234 /* Failure: write the value we saw to EAX. */
2235 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002236 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002237 }
2238 return X86EMUL_CONTINUE;
2239}
2240
Avi Kivityd4b43252011-09-13 10:45:50 +03002241static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002242{
Avi Kivityd4b43252011-09-13 10:45:50 +03002243 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002244 unsigned short sel;
2245 int rc;
2246
Avi Kivity9dac77f2011-06-01 15:34:25 +03002247 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002248
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002249 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002250 if (rc != X86EMUL_CONTINUE)
2251 return rc;
2252
Avi Kivity9dac77f2011-06-01 15:34:25 +03002253 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002254 return rc;
2255}
2256
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002257static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002258setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002259 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002260{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002261 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002262 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002263 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002264 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002265 cs->type = 0x0b; /* Read, Execute, Accessed */
2266 cs->s = 1;
2267 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002268 cs->p = 1;
2269 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002270 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002271
Gleb Natapov79168fd2010-04-28 19:15:30 +03002272 set_desc_base(ss, 0); /* flat segment */
2273 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002274 ss->g = 1; /* 4kb granularity */
2275 ss->s = 1;
2276 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002277 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002278 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002279 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002280 ss->l = 0;
2281 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002282}
2283
Avi Kivity1a18a692012-02-01 12:23:21 +02002284static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2285{
2286 u32 eax, ebx, ecx, edx;
2287
2288 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002289 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2290 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002291 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2292 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2293}
2294
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002295static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2296{
Mathias Krause0225fb52012-08-30 01:30:16 +02002297 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002298 u32 eax, ebx, ecx, edx;
2299
2300 /*
2301 * syscall should always be enabled in longmode - so only become
2302 * vendor specific (cpuid) if other modes are active...
2303 */
2304 if (ctxt->mode == X86EMUL_MODE_PROT64)
2305 return true;
2306
2307 eax = 0x00000000;
2308 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002309 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2310 /*
2311 * Intel ("GenuineIntel")
2312 * remark: Intel CPUs only support "syscall" in 64bit
2313 * longmode. Also an 64bit guest with a
2314 * 32bit compat-app running will #UD !! While this
2315 * behaviour can be fixed (by emulating) into AMD
2316 * response - CPUs of AMD can't behave like Intel.
2317 */
2318 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2319 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2320 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2321 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002322
Avi Kivity0017f932012-06-07 14:10:16 +03002323 /* AMD ("AuthenticAMD") */
2324 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2325 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2326 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2327 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002328
Avi Kivity0017f932012-06-07 14:10:16 +03002329 /* AMD ("AMDisbetter!") */
2330 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2331 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2332 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2333 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002334
2335 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2336 return false;
2337}
2338
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002339static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002340{
Mathias Krause0225fb52012-08-30 01:30:16 +02002341 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002342 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002343 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002344 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002345 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002346
2347 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002348 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002349 ctxt->mode == X86EMUL_MODE_VM86)
2350 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002351
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002352 if (!(em_syscall_is_enabled(ctxt)))
2353 return emulate_ud(ctxt);
2354
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002355 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002356 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002357
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002358 if (!(efer & EFER_SCE))
2359 return emulate_ud(ctxt);
2360
Avi Kivity717746e2011-04-20 13:37:53 +03002361 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002362 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002363 cs_sel = (u16)(msr_data & 0xfffc);
2364 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002365
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002366 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002367 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002368 cs.l = 1;
2369 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002370 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2371 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002372
Avi Kivitydd856ef2012-08-27 23:46:17 +03002373 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002374 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002375#ifdef CONFIG_X86_64
Avi Kivitydd856ef2012-08-27 23:46:17 +03002376 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002377
Avi Kivity717746e2011-04-20 13:37:53 +03002378 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002379 ctxt->mode == X86EMUL_MODE_PROT64 ?
2380 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002381 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002382
Avi Kivity717746e2011-04-20 13:37:53 +03002383 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002384 ctxt->eflags &= ~(msr_data | EFLG_RF);
2385#endif
2386 } else {
2387 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002388 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002389 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002390
2391 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2392 }
2393
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002394 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002395}
2396
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002397static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002398{
Mathias Krause0225fb52012-08-30 01:30:16 +02002399 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002400 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002401 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002402 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002403 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002404
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002405 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002406 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002407 if (ctxt->mode == X86EMUL_MODE_REAL)
2408 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002409
Avi Kivity1a18a692012-02-01 12:23:21 +02002410 /*
2411 * Not recognized on AMD in compat mode (but is recognized in legacy
2412 * mode).
2413 */
2414 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2415 && !vendor_intel(ctxt))
2416 return emulate_ud(ctxt);
2417
Andre Przywara8c604352009-06-18 12:56:01 +02002418 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2419 * Therefore, we inject an #UD.
2420 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002421 if (ctxt->mode == X86EMUL_MODE_PROT64)
2422 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002423
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002424 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002425
Avi Kivity717746e2011-04-20 13:37:53 +03002426 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002427 switch (ctxt->mode) {
2428 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002429 if ((msr_data & 0xfffc) == 0x0)
2430 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002431 break;
2432 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002433 if (msr_data == 0x0)
2434 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002435 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002436 default:
2437 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002438 }
2439
2440 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002441 cs_sel = (u16)msr_data;
2442 cs_sel &= ~SELECTOR_RPL_MASK;
2443 ss_sel = cs_sel + 8;
2444 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002445 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002446 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002447 cs.l = 1;
2448 }
2449
Avi Kivity1aa36612011-04-27 13:20:30 +03002450 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2451 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002452
Avi Kivity717746e2011-04-20 13:37:53 +03002453 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002454 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002455
Avi Kivity717746e2011-04-20 13:37:53 +03002456 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002457 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002458
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002459 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002460}
2461
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002462static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002463{
Mathias Krause0225fb52012-08-30 01:30:16 +02002464 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002465 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002466 u64 msr_data;
2467 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002468 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002469
Gleb Natapova0044752010-02-10 14:21:31 +02002470 /* inject #GP if in real mode or Virtual 8086 mode */
2471 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002472 ctxt->mode == X86EMUL_MODE_VM86)
2473 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002474
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002475 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002476
Avi Kivity9dac77f2011-06-01 15:34:25 +03002477 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002478 usermode = X86EMUL_MODE_PROT64;
2479 else
2480 usermode = X86EMUL_MODE_PROT32;
2481
2482 cs.dpl = 3;
2483 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002484 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002485 switch (usermode) {
2486 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002487 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002488 if ((msr_data & 0xfffc) == 0x0)
2489 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002490 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002491 break;
2492 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002493 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002494 if (msr_data == 0x0)
2495 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002496 ss_sel = cs_sel + 8;
2497 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002498 cs.l = 1;
2499 break;
2500 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002501 cs_sel |= SELECTOR_RPL_MASK;
2502 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002503
Avi Kivity1aa36612011-04-27 13:20:30 +03002504 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2505 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002506
Avi Kivitydd856ef2012-08-27 23:46:17 +03002507 ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX);
2508 *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX);
Andre Przywara4668f052009-06-18 12:56:02 +02002509
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002510 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002511}
2512
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002513static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002514{
2515 int iopl;
2516 if (ctxt->mode == X86EMUL_MODE_REAL)
2517 return false;
2518 if (ctxt->mode == X86EMUL_MODE_VM86)
2519 return true;
2520 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002521 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002522}
2523
2524static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002525 u16 port, u16 len)
2526{
Mathias Krause0225fb52012-08-30 01:30:16 +02002527 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002528 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002529 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002530 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002531 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002532 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002533 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002534
Avi Kivity1aa36612011-04-27 13:20:30 +03002535 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002536 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002537 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002538 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002539 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002540 base = get_desc_base(&tr_seg);
2541#ifdef CONFIG_X86_64
2542 base |= ((u64)base3) << 32;
2543#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002544 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002545 if (r != X86EMUL_CONTINUE)
2546 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002547 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002548 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002549 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002550 if (r != X86EMUL_CONTINUE)
2551 return false;
2552 if ((perm >> bit_idx) & mask)
2553 return false;
2554 return true;
2555}
2556
2557static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002558 u16 port, u16 len)
2559{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002560 if (ctxt->perm_ok)
2561 return true;
2562
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002563 if (emulator_bad_iopl(ctxt))
2564 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002565 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002566
2567 ctxt->perm_ok = true;
2568
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002569 return true;
2570}
2571
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002572static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002573 struct tss_segment_16 *tss)
2574{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002575 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002576 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002577 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2578 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2579 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2580 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2581 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2582 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2583 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2584 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002585
Avi Kivity1aa36612011-04-27 13:20:30 +03002586 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2587 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2588 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2589 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2590 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002591}
2592
2593static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002594 struct tss_segment_16 *tss)
2595{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002596 int ret;
2597
Avi Kivity9dac77f2011-06-01 15:34:25 +03002598 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002599 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002600 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2601 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2602 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2603 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2604 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2605 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2606 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2607 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002608
2609 /*
2610 * SDM says that segment selectors are loaded before segment
2611 * descriptors
2612 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002613 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2614 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2615 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2616 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2617 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002618
2619 /*
Guo Chaofc058682012-06-28 15:19:51 +08002620 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002621 * it is handled in a context of new task
2622 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002623 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002624 if (ret != X86EMUL_CONTINUE)
2625 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002626 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002627 if (ret != X86EMUL_CONTINUE)
2628 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002629 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002630 if (ret != X86EMUL_CONTINUE)
2631 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002632 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002633 if (ret != X86EMUL_CONTINUE)
2634 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002635 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002636 if (ret != X86EMUL_CONTINUE)
2637 return ret;
2638
2639 return X86EMUL_CONTINUE;
2640}
2641
2642static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002643 u16 tss_selector, u16 old_tss_sel,
2644 ulong old_tss_base, struct desc_struct *new_desc)
2645{
Mathias Krause0225fb52012-08-30 01:30:16 +02002646 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002647 struct tss_segment_16 tss_seg;
2648 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002649 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002650
Avi Kivity0f65dd72011-04-20 13:37:53 +03002651 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002652 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002653 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002654 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002655 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002656
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002657 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002658
Avi Kivity0f65dd72011-04-20 13:37:53 +03002659 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002660 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002661 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002662 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002663 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002664
Avi Kivity0f65dd72011-04-20 13:37:53 +03002665 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002666 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002667 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002668 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002669 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002670
2671 if (old_tss_sel != 0xffff) {
2672 tss_seg.prev_task_link = old_tss_sel;
2673
Avi Kivity0f65dd72011-04-20 13:37:53 +03002674 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002675 &tss_seg.prev_task_link,
2676 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002677 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002678 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002679 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002680 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002681 }
2682
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002683 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002684}
2685
2686static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002687 struct tss_segment_32 *tss)
2688{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002689 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002690 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002691 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002692 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2693 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2694 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2695 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2696 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2697 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2698 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2699 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002700
Avi Kivity1aa36612011-04-27 13:20:30 +03002701 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2702 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2703 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2704 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2705 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2706 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2707 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002708}
2709
2710static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002711 struct tss_segment_32 *tss)
2712{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002713 int ret;
2714
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002715 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002716 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002717 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002718 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002719
2720 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002721 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2722 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2723 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2724 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2725 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2726 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2727 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2728 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002729
2730 /*
2731 * SDM says that segment selectors are loaded before segment
2732 * descriptors
2733 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002734 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2735 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2736 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2737 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2738 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2739 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2740 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002741
2742 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002743 * If we're switching between Protected Mode and VM86, we need to make
2744 * sure to update the mode before loading the segment descriptors so
2745 * that the selectors are interpreted correctly.
2746 *
2747 * Need to get rflags to the vcpu struct immediately because it
2748 * influences the CPL which is checked at least when loading the segment
2749 * descriptors and when pushing an error code to the new kernel stack.
2750 *
2751 * TODO Introduce a separate ctxt->ops->set_cpl callback
2752 */
2753 if (ctxt->eflags & X86_EFLAGS_VM)
2754 ctxt->mode = X86EMUL_MODE_VM86;
2755 else
2756 ctxt->mode = X86EMUL_MODE_PROT32;
2757
2758 ctxt->ops->set_rflags(ctxt, ctxt->eflags);
2759
2760 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002761 * Now load segment descriptors. If fault happenes at this stage
2762 * it is handled in a context of new task
2763 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002764 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002765 if (ret != X86EMUL_CONTINUE)
2766 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002767 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002768 if (ret != X86EMUL_CONTINUE)
2769 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002770 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002771 if (ret != X86EMUL_CONTINUE)
2772 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002773 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002774 if (ret != X86EMUL_CONTINUE)
2775 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002776 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002777 if (ret != X86EMUL_CONTINUE)
2778 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002779 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002780 if (ret != X86EMUL_CONTINUE)
2781 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002782 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002783 if (ret != X86EMUL_CONTINUE)
2784 return ret;
2785
2786 return X86EMUL_CONTINUE;
2787}
2788
2789static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002790 u16 tss_selector, u16 old_tss_sel,
2791 ulong old_tss_base, struct desc_struct *new_desc)
2792{
Mathias Krause0225fb52012-08-30 01:30:16 +02002793 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002794 struct tss_segment_32 tss_seg;
2795 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002796 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002797
Avi Kivity0f65dd72011-04-20 13:37:53 +03002798 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002799 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002800 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002801 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002802 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002803
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002804 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002805
Avi Kivity0f65dd72011-04-20 13:37:53 +03002806 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002807 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002808 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002809 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002810 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002811
Avi Kivity0f65dd72011-04-20 13:37:53 +03002812 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002813 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002814 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002815 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002816 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002817
2818 if (old_tss_sel != 0xffff) {
2819 tss_seg.prev_task_link = old_tss_sel;
2820
Avi Kivity0f65dd72011-04-20 13:37:53 +03002821 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002822 &tss_seg.prev_task_link,
2823 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002824 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002825 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002826 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002827 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002828 }
2829
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002830 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002831}
2832
2833static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002834 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002835 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002836{
Mathias Krause0225fb52012-08-30 01:30:16 +02002837 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002838 struct desc_struct curr_tss_desc, next_tss_desc;
2839 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002840 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002841 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002842 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002843 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002844 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002845
2846 /* FIXME: old_tss_base == ~0 ? */
2847
Avi Kivitye9194642012-06-13 16:29:39 +03002848 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002849 if (ret != X86EMUL_CONTINUE)
2850 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002851 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002852 if (ret != X86EMUL_CONTINUE)
2853 return ret;
2854
2855 /* FIXME: check that next_tss_desc is tss */
2856
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002857 /*
2858 * Check privileges. The three cases are task switch caused by...
2859 *
2860 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2861 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002862 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002863 */
2864 if (reason == TASK_SWITCH_GATE) {
2865 if (idt_index != -1) {
2866 /* Software interrupts */
2867 struct desc_struct task_gate_desc;
2868 int dpl;
2869
2870 ret = read_interrupt_descriptor(ctxt, idt_index,
2871 &task_gate_desc);
2872 if (ret != X86EMUL_CONTINUE)
2873 return ret;
2874
2875 dpl = task_gate_desc.dpl;
2876 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2877 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2878 }
2879 } else if (reason != TASK_SWITCH_IRET) {
2880 int dpl = next_tss_desc.dpl;
2881 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2882 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002883 }
2884
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002885
Gleb Natapovceffb452010-03-18 15:20:19 +02002886 desc_limit = desc_limit_scaled(&next_tss_desc);
2887 if (!next_tss_desc.p ||
2888 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2889 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002890 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002891 return X86EMUL_PROPAGATE_FAULT;
2892 }
2893
2894 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2895 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002896 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002897 }
2898
2899 if (reason == TASK_SWITCH_IRET)
2900 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2901
2902 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002903 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002904 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2905 old_tss_sel = 0xffff;
2906
2907 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002908 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002909 old_tss_base, &next_tss_desc);
2910 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002911 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002912 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002913 if (ret != X86EMUL_CONTINUE)
2914 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002915
2916 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2917 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2918
2919 if (reason != TASK_SWITCH_IRET) {
2920 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002921 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002922 }
2923
Avi Kivity717746e2011-04-20 13:37:53 +03002924 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002925 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002926
Jan Kiszkae269fb22010-04-14 15:51:09 +02002927 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002928 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2929 ctxt->lock_prefix = 0;
2930 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002931 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002932 }
2933
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002934 return ret;
2935}
2936
2937int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002938 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002939 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002940{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002941 int rc;
2942
Avi Kivitydd856ef2012-08-27 23:46:17 +03002943 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002944 ctxt->_eip = ctxt->eip;
2945 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002946
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002947 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002948 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002949
Avi Kivitydd856ef2012-08-27 23:46:17 +03002950 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002951 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002952 writeback_registers(ctxt);
2953 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002954
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002955 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002956}
2957
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002958static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2959 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002960{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002961 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002962
Avi Kivitydd856ef2012-08-27 23:46:17 +03002963 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2964 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002965}
2966
Avi Kivity7af04fc2010-08-18 14:16:35 +03002967static int em_das(struct x86_emulate_ctxt *ctxt)
2968{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002969 u8 al, old_al;
2970 bool af, cf, old_cf;
2971
2972 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002973 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002974
2975 old_al = al;
2976 old_cf = cf;
2977 cf = false;
2978 af = ctxt->eflags & X86_EFLAGS_AF;
2979 if ((al & 0x0f) > 9 || af) {
2980 al -= 6;
2981 cf = old_cf | (al >= 250);
2982 af = true;
2983 } else {
2984 af = false;
2985 }
2986 if (old_al > 0x99 || old_cf) {
2987 al -= 0x60;
2988 cf = true;
2989 }
2990
Avi Kivity9dac77f2011-06-01 15:34:25 +03002991 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002992 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002993 ctxt->src.type = OP_IMM;
2994 ctxt->src.val = 0;
2995 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02002996 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03002997 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2998 if (cf)
2999 ctxt->eflags |= X86_EFLAGS_CF;
3000 if (af)
3001 ctxt->eflags |= X86_EFLAGS_AF;
3002 return X86EMUL_CONTINUE;
3003}
3004
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003005static int em_aam(struct x86_emulate_ctxt *ctxt)
3006{
3007 u8 al, ah;
3008
3009 if (ctxt->src.val == 0)
3010 return emulate_de(ctxt);
3011
3012 al = ctxt->dst.val & 0xff;
3013 ah = al / ctxt->src.val;
3014 al %= ctxt->src.val;
3015
3016 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
3017
3018 /* Set PF, ZF, SF */
3019 ctxt->src.type = OP_IMM;
3020 ctxt->src.val = 0;
3021 ctxt->src.bytes = 1;
3022 fastop(ctxt, em_or);
3023
3024 return X86EMUL_CONTINUE;
3025}
3026
Gleb Natapov7f662272012-12-10 11:42:30 +02003027static int em_aad(struct x86_emulate_ctxt *ctxt)
3028{
3029 u8 al = ctxt->dst.val & 0xff;
3030 u8 ah = (ctxt->dst.val >> 8) & 0xff;
3031
3032 al = (al + (ah * ctxt->src.val)) & 0xff;
3033
3034 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
3035
Gleb Natapovf583c292013-02-13 17:50:39 +02003036 /* Set PF, ZF, SF */
3037 ctxt->src.type = OP_IMM;
3038 ctxt->src.val = 0;
3039 ctxt->src.bytes = 1;
3040 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02003041
3042 return X86EMUL_CONTINUE;
3043}
3044
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003045static int em_call(struct x86_emulate_ctxt *ctxt)
3046{
3047 long rel = ctxt->src.val;
3048
3049 ctxt->src.val = (unsigned long)ctxt->_eip;
3050 jmp_rel(ctxt, rel);
3051 return em_push(ctxt);
3052}
3053
Avi Kivity0ef753b2010-08-18 14:51:45 +03003054static int em_call_far(struct x86_emulate_ctxt *ctxt)
3055{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003056 u16 sel, old_cs;
3057 ulong old_eip;
3058 int rc;
3059
Avi Kivity1aa36612011-04-27 13:20:30 +03003060 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003061 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003062
Avi Kivity9dac77f2011-06-01 15:34:25 +03003063 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003064 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03003065 return X86EMUL_CONTINUE;
3066
Avi Kivity9dac77f2011-06-01 15:34:25 +03003067 ctxt->_eip = 0;
3068 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003069
Avi Kivity9dac77f2011-06-01 15:34:25 +03003070 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003071 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003072 if (rc != X86EMUL_CONTINUE)
3073 return rc;
3074
Avi Kivity9dac77f2011-06-01 15:34:25 +03003075 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003076 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003077}
3078
Avi Kivity40ece7c2010-08-18 15:12:09 +03003079static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3080{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003081 int rc;
3082
Avi Kivity9dac77f2011-06-01 15:34:25 +03003083 ctxt->dst.type = OP_REG;
3084 ctxt->dst.addr.reg = &ctxt->_eip;
3085 ctxt->dst.bytes = ctxt->op_bytes;
3086 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003087 if (rc != X86EMUL_CONTINUE)
3088 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003089 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003090 return X86EMUL_CONTINUE;
3091}
3092
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003093static int em_xchg(struct x86_emulate_ctxt *ctxt)
3094{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003095 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003096 ctxt->src.val = ctxt->dst.val;
3097 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003098
3099 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003100 ctxt->dst.val = ctxt->src.orig_val;
3101 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003102 return X86EMUL_CONTINUE;
3103}
3104
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003105static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3106{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003107 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003108 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003109}
3110
Avi Kivity61429142010-08-19 15:13:00 +03003111static int em_cwd(struct x86_emulate_ctxt *ctxt)
3112{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003113 ctxt->dst.type = OP_REG;
3114 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003115 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003116 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003117
3118 return X86EMUL_CONTINUE;
3119}
3120
Avi Kivity48bb5d32010-08-18 18:54:34 +03003121static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3122{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003123 u64 tsc = 0;
3124
Avi Kivity717746e2011-04-20 13:37:53 +03003125 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003126 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3127 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003128 return X86EMUL_CONTINUE;
3129}
3130
Avi Kivity222d21a2011-11-10 14:57:30 +02003131static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3132{
3133 u64 pmc;
3134
Avi Kivitydd856ef2012-08-27 23:46:17 +03003135 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003136 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003137 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3138 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003139 return X86EMUL_CONTINUE;
3140}
3141
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003142static int em_mov(struct x86_emulate_ctxt *ctxt)
3143{
Stefan Hajnoczi49597d82012-04-09 18:40:00 +03003144 memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003145 return X86EMUL_CONTINUE;
3146}
3147
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003148static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3149{
3150 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3151 return emulate_gp(ctxt, 0);
3152
3153 /* Disable writeback. */
3154 ctxt->dst.type = OP_NONE;
3155 return X86EMUL_CONTINUE;
3156}
3157
3158static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3159{
3160 unsigned long val;
3161
3162 if (ctxt->mode == X86EMUL_MODE_PROT64)
3163 val = ctxt->src.val & ~0ULL;
3164 else
3165 val = ctxt->src.val & ~0U;
3166
3167 /* #UD condition is already handled. */
3168 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3169 return emulate_gp(ctxt, 0);
3170
3171 /* Disable writeback. */
3172 ctxt->dst.type = OP_NONE;
3173 return X86EMUL_CONTINUE;
3174}
3175
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003176static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3177{
3178 u64 msr_data;
3179
Avi Kivitydd856ef2012-08-27 23:46:17 +03003180 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3181 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3182 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003183 return emulate_gp(ctxt, 0);
3184
3185 return X86EMUL_CONTINUE;
3186}
3187
3188static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3189{
3190 u64 msr_data;
3191
Avi Kivitydd856ef2012-08-27 23:46:17 +03003192 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003193 return emulate_gp(ctxt, 0);
3194
Avi Kivitydd856ef2012-08-27 23:46:17 +03003195 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3196 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003197 return X86EMUL_CONTINUE;
3198}
3199
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003200static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3201{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003202 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003203 return emulate_ud(ctxt);
3204
Avi Kivity9dac77f2011-06-01 15:34:25 +03003205 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003206 return X86EMUL_CONTINUE;
3207}
3208
3209static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3210{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003211 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003212
Avi Kivity9dac77f2011-06-01 15:34:25 +03003213 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003214 return emulate_ud(ctxt);
3215
Avi Kivity9dac77f2011-06-01 15:34:25 +03003216 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003217 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3218
3219 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003220 ctxt->dst.type = OP_NONE;
3221 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003222}
3223
Avi Kivitya14e5792012-06-13 12:28:33 +03003224static int em_lldt(struct x86_emulate_ctxt *ctxt)
3225{
3226 u16 sel = ctxt->src.val;
3227
3228 /* Disable writeback. */
3229 ctxt->dst.type = OP_NONE;
3230 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3231}
3232
Avi Kivity80890002012-06-13 16:33:29 +03003233static int em_ltr(struct x86_emulate_ctxt *ctxt)
3234{
3235 u16 sel = ctxt->src.val;
3236
3237 /* Disable writeback. */
3238 ctxt->dst.type = OP_NONE;
3239 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3240}
3241
Avi Kivity38503912011-03-31 18:48:09 +02003242static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3243{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003244 int rc;
3245 ulong linear;
3246
Avi Kivity9dac77f2011-06-01 15:34:25 +03003247 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003248 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003249 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003250 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003251 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003252 return X86EMUL_CONTINUE;
3253}
3254
Avi Kivity2d04a052011-04-20 15:32:49 +03003255static int em_clts(struct x86_emulate_ctxt *ctxt)
3256{
3257 ulong cr0;
3258
3259 cr0 = ctxt->ops->get_cr(ctxt, 0);
3260 cr0 &= ~X86_CR0_TS;
3261 ctxt->ops->set_cr(ctxt, 0, cr0);
3262 return X86EMUL_CONTINUE;
3263}
3264
Avi Kivity26d05cc2011-04-21 12:07:59 +03003265static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3266{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003267 int rc;
3268
Avi Kivity9dac77f2011-06-01 15:34:25 +03003269 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003270 return X86EMUL_UNHANDLEABLE;
3271
3272 rc = ctxt->ops->fix_hypercall(ctxt);
3273 if (rc != X86EMUL_CONTINUE)
3274 return rc;
3275
3276 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003277 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003278 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003279 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003280 return X86EMUL_CONTINUE;
3281}
3282
Avi Kivity96051572012-06-10 17:21:18 +03003283static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3284 void (*get)(struct x86_emulate_ctxt *ctxt,
3285 struct desc_ptr *ptr))
3286{
3287 struct desc_ptr desc_ptr;
3288
3289 if (ctxt->mode == X86EMUL_MODE_PROT64)
3290 ctxt->op_bytes = 8;
3291 get(ctxt, &desc_ptr);
3292 if (ctxt->op_bytes == 2) {
3293 ctxt->op_bytes = 4;
3294 desc_ptr.address &= 0x00ffffff;
3295 }
3296 /* Disable writeback. */
3297 ctxt->dst.type = OP_NONE;
3298 return segmented_write(ctxt, ctxt->dst.addr.mem,
3299 &desc_ptr, 2 + ctxt->op_bytes);
3300}
3301
3302static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3303{
3304 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3305}
3306
3307static int em_sidt(struct x86_emulate_ctxt *ctxt)
3308{
3309 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3310}
3311
Avi Kivity26d05cc2011-04-21 12:07:59 +03003312static int em_lgdt(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,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003320 &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_gdt(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
Avi Kivity5ef39c72011-04-21 12:21:50 +03003330static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003331{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003332 int rc;
3333
Avi Kivity5ef39c72011-04-21 12:21:50 +03003334 rc = ctxt->ops->fix_hypercall(ctxt);
3335
Avi Kivity26d05cc2011-04-21 12:07:59 +03003336 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003337 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003338 return rc;
3339}
3340
3341static int em_lidt(struct x86_emulate_ctxt *ctxt)
3342{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003343 struct desc_ptr desc_ptr;
3344 int rc;
3345
Avi Kivity510425f2012-06-07 17:04:36 +03003346 if (ctxt->mode == X86EMUL_MODE_PROT64)
3347 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003348 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003349 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003350 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003351 if (rc != X86EMUL_CONTINUE)
3352 return rc;
3353 ctxt->ops->set_idt(ctxt, &desc_ptr);
3354 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003355 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003356 return X86EMUL_CONTINUE;
3357}
3358
3359static int em_smsw(struct x86_emulate_ctxt *ctxt)
3360{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003361 ctxt->dst.bytes = 2;
3362 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003363 return X86EMUL_CONTINUE;
3364}
3365
3366static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3367{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003368 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003369 | (ctxt->src.val & 0x0f));
3370 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003371 return X86EMUL_CONTINUE;
3372}
3373
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003374static int em_loop(struct x86_emulate_ctxt *ctxt)
3375{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003376 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3377 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003378 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3379 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003380
3381 return X86EMUL_CONTINUE;
3382}
3383
3384static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3385{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003386 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003387 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003388
3389 return X86EMUL_CONTINUE;
3390}
3391
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003392static int em_in(struct x86_emulate_ctxt *ctxt)
3393{
3394 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3395 &ctxt->dst.val))
3396 return X86EMUL_IO_NEEDED;
3397
3398 return X86EMUL_CONTINUE;
3399}
3400
3401static int em_out(struct x86_emulate_ctxt *ctxt)
3402{
3403 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3404 &ctxt->src.val, 1);
3405 /* Disable writeback. */
3406 ctxt->dst.type = OP_NONE;
3407 return X86EMUL_CONTINUE;
3408}
3409
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003410static int em_cli(struct x86_emulate_ctxt *ctxt)
3411{
3412 if (emulator_bad_iopl(ctxt))
3413 return emulate_gp(ctxt, 0);
3414
3415 ctxt->eflags &= ~X86_EFLAGS_IF;
3416 return X86EMUL_CONTINUE;
3417}
3418
3419static int em_sti(struct x86_emulate_ctxt *ctxt)
3420{
3421 if (emulator_bad_iopl(ctxt))
3422 return emulate_gp(ctxt, 0);
3423
3424 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3425 ctxt->eflags |= X86_EFLAGS_IF;
3426 return X86EMUL_CONTINUE;
3427}
3428
Avi Kivity6d6eede2012-06-07 14:11:36 +03003429static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3430{
3431 u32 eax, ebx, ecx, edx;
3432
Avi Kivitydd856ef2012-08-27 23:46:17 +03003433 eax = reg_read(ctxt, VCPU_REGS_RAX);
3434 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003435 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003436 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3437 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3438 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3439 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003440 return X86EMUL_CONTINUE;
3441}
3442
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003443static int em_lahf(struct x86_emulate_ctxt *ctxt)
3444{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003445 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3446 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003447 return X86EMUL_CONTINUE;
3448}
3449
Avi Kivity92998362012-06-13 12:25:06 +03003450static int em_bswap(struct x86_emulate_ctxt *ctxt)
3451{
3452 switch (ctxt->op_bytes) {
3453#ifdef CONFIG_X86_64
3454 case 8:
3455 asm("bswap %0" : "+r"(ctxt->dst.val));
3456 break;
3457#endif
3458 default:
3459 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3460 break;
3461 }
3462 return X86EMUL_CONTINUE;
3463}
3464
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003465static bool valid_cr(int nr)
3466{
3467 switch (nr) {
3468 case 0:
3469 case 2 ... 4:
3470 case 8:
3471 return true;
3472 default:
3473 return false;
3474 }
3475}
3476
3477static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3478{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003479 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003480 return emulate_ud(ctxt);
3481
3482 return X86EMUL_CONTINUE;
3483}
3484
3485static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3486{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003487 u64 new_val = ctxt->src.val64;
3488 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003489 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003490
3491 static u64 cr_reserved_bits[] = {
3492 0xffffffff00000000ULL,
3493 0, 0, 0, /* CR3 checked later */
3494 CR4_RESERVED_BITS,
3495 0, 0, 0,
3496 CR8_RESERVED_BITS,
3497 };
3498
3499 if (!valid_cr(cr))
3500 return emulate_ud(ctxt);
3501
3502 if (new_val & cr_reserved_bits[cr])
3503 return emulate_gp(ctxt, 0);
3504
3505 switch (cr) {
3506 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003507 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003508 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3509 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3510 return emulate_gp(ctxt, 0);
3511
Avi Kivity717746e2011-04-20 13:37:53 +03003512 cr4 = ctxt->ops->get_cr(ctxt, 4);
3513 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003514
3515 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3516 !(cr4 & X86_CR4_PAE))
3517 return emulate_gp(ctxt, 0);
3518
3519 break;
3520 }
3521 case 3: {
3522 u64 rsvd = 0;
3523
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003524 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3525 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003526 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003527 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003528 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003529 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003530 rsvd = CR3_NONPAE_RESERVED_BITS;
3531
3532 if (new_val & rsvd)
3533 return emulate_gp(ctxt, 0);
3534
3535 break;
3536 }
3537 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003538 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003539
3540 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3541 return emulate_gp(ctxt, 0);
3542
3543 break;
3544 }
3545 }
3546
3547 return X86EMUL_CONTINUE;
3548}
3549
Joerg Roedel3b88e412011-04-04 12:39:29 +02003550static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3551{
3552 unsigned long dr7;
3553
Avi Kivity717746e2011-04-20 13:37:53 +03003554 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003555
3556 /* Check if DR7.Global_Enable is set */
3557 return dr7 & (1 << 13);
3558}
3559
3560static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3561{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003562 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003563 u64 cr4;
3564
3565 if (dr > 7)
3566 return emulate_ud(ctxt);
3567
Avi Kivity717746e2011-04-20 13:37:53 +03003568 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003569 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3570 return emulate_ud(ctxt);
3571
3572 if (check_dr7_gd(ctxt))
3573 return emulate_db(ctxt);
3574
3575 return X86EMUL_CONTINUE;
3576}
3577
3578static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3579{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003580 u64 new_val = ctxt->src.val64;
3581 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003582
3583 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3584 return emulate_gp(ctxt, 0);
3585
3586 return check_dr_read(ctxt);
3587}
3588
Joerg Roedel01de8b02011-04-04 12:39:31 +02003589static int check_svme(struct x86_emulate_ctxt *ctxt)
3590{
3591 u64 efer;
3592
Avi Kivity717746e2011-04-20 13:37:53 +03003593 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003594
3595 if (!(efer & EFER_SVME))
3596 return emulate_ud(ctxt);
3597
3598 return X86EMUL_CONTINUE;
3599}
3600
3601static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3602{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003603 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003604
3605 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003606 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003607 return emulate_gp(ctxt, 0);
3608
3609 return check_svme(ctxt);
3610}
3611
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003612static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3613{
Avi Kivity717746e2011-04-20 13:37:53 +03003614 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003615
Avi Kivity717746e2011-04-20 13:37:53 +03003616 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003617 return emulate_ud(ctxt);
3618
3619 return X86EMUL_CONTINUE;
3620}
3621
Joerg Roedel80612522011-04-04 12:39:33 +02003622static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3623{
Avi Kivity717746e2011-04-20 13:37:53 +03003624 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003625 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003626
Avi Kivity717746e2011-04-20 13:37:53 +03003627 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02003628 (rcx > 3))
3629 return emulate_gp(ctxt, 0);
3630
3631 return X86EMUL_CONTINUE;
3632}
3633
Joerg Roedelf6511932011-04-04 12:39:35 +02003634static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3635{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003636 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3637 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003638 return emulate_gp(ctxt, 0);
3639
3640 return X86EMUL_CONTINUE;
3641}
3642
3643static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3644{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003645 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3646 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003647 return emulate_gp(ctxt, 0);
3648
3649 return X86EMUL_CONTINUE;
3650}
3651
Avi Kivity73fba5f2010-07-29 15:11:53 +03003652#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003653#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003654#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3655 .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003656#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003657#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003658#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3659#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003660#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003661#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003662#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003663#define II(_f, _e, _i) \
3664 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003665#define IIP(_f, _e, _i, _p) \
3666 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3667 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003668#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003669
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003670#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003671#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003672#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003673#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003674#define I2bvIP(_f, _e, _i, _p) \
3675 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003676
Avi Kivityfb864fb2013-01-04 16:18:54 +02003677#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3678 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3679 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003680
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003681static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003682 DI(SrcNone | Priv, monitor),
3683 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003684 N, N, N, N, N, N,
3685};
3686
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003687static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003688 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
3689 II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall),
3690 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3691 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3692 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3693 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3694 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3695 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003696};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003697
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003698static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003699 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003700 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003701 N, N, N, N, N, N,
3702};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003703
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003704static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003705 F(Lock, em_add),
3706 F(Lock | PageTable, em_or),
3707 F(Lock, em_adc),
3708 F(Lock, em_sbb),
3709 F(Lock | PageTable, em_and),
3710 F(Lock, em_sub),
3711 F(Lock, em_xor),
3712 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003713};
3714
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003715static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003716 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003717};
3718
Avi Kivity007a3b52013-01-19 19:51:51 +02003719static const struct opcode group2[] = {
3720 F(DstMem | ModRM, em_rol),
3721 F(DstMem | ModRM, em_ror),
3722 F(DstMem | ModRM, em_rcl),
3723 F(DstMem | ModRM, em_rcr),
3724 F(DstMem | ModRM, em_shl),
3725 F(DstMem | ModRM, em_shr),
3726 F(DstMem | ModRM, em_shl),
3727 F(DstMem | ModRM, em_sar),
3728};
3729
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003730static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003731 F(DstMem | SrcImm | NoWrite, em_test),
3732 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003733 F(DstMem | SrcNone | Lock, em_not),
3734 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02003735 F(DstXacc | Src2Mem, em_mul_ex),
3736 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityab2c5ce2013-02-09 11:31:46 +02003737 I(DstXacc | Src2Mem, em_div_ex),
3738 I(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003739};
3740
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003741static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003742 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3743 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003744 N, N, N, N, N, N,
3745};
3746
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003747static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003748 F(DstMem | SrcNone | Lock, em_inc),
3749 F(DstMem | SrcNone | Lock, em_dec),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003750 I(SrcMem | Stack, em_grp45),
3751 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3752 I(SrcMem | Stack, em_grp45),
3753 I(SrcMemFAddr | ImplicitOps, em_grp45),
Gleb Natapov188424b2013-04-11 12:32:14 +03003754 I(SrcMem | Stack, em_grp45), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003755};
3756
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003757static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003758 DI(Prot, sldt),
3759 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003760 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003761 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003762 N, N, N, N,
3763};
3764
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003765static const struct group_dual group7 = { {
Avi Kivity96051572012-06-10 17:21:18 +03003766 II(Mov | DstMem | Priv, em_sgdt, sgdt),
3767 II(Mov | DstMem | Priv, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003768 II(SrcMem | Priv, em_lgdt, lgdt),
3769 II(SrcMem | Priv, em_lidt, lidt),
3770 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3771 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3772 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003773}, {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003774 I(SrcNone | Priv | VendorSpecific, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003775 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003776 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003777 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3778 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3779 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003780} };
3781
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003782static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003783 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003784 F(DstMem | SrcImmByte | NoWrite, em_bt),
3785 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3786 F(DstMem | SrcImmByte | Lock, em_btr),
3787 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003788};
3789
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003790static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003791 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003792}, {
3793 N, N, N, N, N, N, N, N,
3794} };
3795
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003796static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003797 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003798 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003799};
3800
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003801static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003802 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003803};
3804
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003805static const struct gprefix pfx_vmovntpx = {
Avi Kivity3e114eb2012-04-09 18:40:01 +03003806 I(0, em_mov), N, N, N,
3807};
3808
Gleb Natapov045a2822012-12-20 16:57:43 +02003809static const struct escape escape_d9 = { {
3810 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3811}, {
3812 /* 0xC0 - 0xC7 */
3813 N, N, N, N, N, N, N, N,
3814 /* 0xC8 - 0xCF */
3815 N, N, N, N, N, N, N, N,
3816 /* 0xD0 - 0xC7 */
3817 N, N, N, N, N, N, N, N,
3818 /* 0xD8 - 0xDF */
3819 N, N, N, N, N, N, N, N,
3820 /* 0xE0 - 0xE7 */
3821 N, N, N, N, N, N, N, N,
3822 /* 0xE8 - 0xEF */
3823 N, N, N, N, N, N, N, N,
3824 /* 0xF0 - 0xF7 */
3825 N, N, N, N, N, N, N, N,
3826 /* 0xF8 - 0xFF */
3827 N, N, N, N, N, N, N, N,
3828} };
3829
3830static const struct escape escape_db = { {
3831 N, N, N, N, N, N, N, N,
3832}, {
3833 /* 0xC0 - 0xC7 */
3834 N, N, N, N, N, N, N, N,
3835 /* 0xC8 - 0xCF */
3836 N, N, N, N, N, N, N, N,
3837 /* 0xD0 - 0xC7 */
3838 N, N, N, N, N, N, N, N,
3839 /* 0xD8 - 0xDF */
3840 N, N, N, N, N, N, N, N,
3841 /* 0xE0 - 0xE7 */
3842 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3843 /* 0xE8 - 0xEF */
3844 N, N, N, N, N, N, N, N,
3845 /* 0xF0 - 0xF7 */
3846 N, N, N, N, N, N, N, N,
3847 /* 0xF8 - 0xFF */
3848 N, N, N, N, N, N, N, N,
3849} };
3850
3851static const struct escape escape_dd = { {
3852 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3853}, {
3854 /* 0xC0 - 0xC7 */
3855 N, N, N, N, N, N, N, N,
3856 /* 0xC8 - 0xCF */
3857 N, N, N, N, N, N, N, N,
3858 /* 0xD0 - 0xC7 */
3859 N, N, N, N, N, N, N, N,
3860 /* 0xD8 - 0xDF */
3861 N, N, N, N, N, N, N, N,
3862 /* 0xE0 - 0xE7 */
3863 N, N, N, N, N, N, N, N,
3864 /* 0xE8 - 0xEF */
3865 N, N, N, N, N, N, N, N,
3866 /* 0xF0 - 0xF7 */
3867 N, N, N, N, N, N, N, N,
3868 /* 0xF8 - 0xFF */
3869 N, N, N, N, N, N, N, N,
3870} };
3871
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003872static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003873 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003874 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003875 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3876 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003877 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003878 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003879 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3880 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003881 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003882 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003883 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3884 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003885 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003886 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003887 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3888 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003889 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003890 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003891 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003892 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003893 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003894 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003895 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003896 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003897 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02003898 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003899 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003900 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003901 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003902 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003903 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003904 I(ImplicitOps | Stack | No64, em_pusha),
3905 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003906 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3907 N, N, N, N,
3908 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003909 I(SrcImm | Mov | Stack, em_push),
3910 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003911 I(SrcImmByte | Mov | Stack, em_push),
3912 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003913 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003914 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003915 /* 0x70 - 0x7F */
3916 X16(D(SrcImmByte)),
3917 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003918 G(ByteOp | DstMem | SrcImm, group1),
3919 G(DstMem | SrcImm, group1),
3920 G(ByteOp | DstMem | SrcImm | No64, group1),
3921 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003922 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003923 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003924 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003925 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003926 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003927 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003928 D(ModRM | SrcMem | NoAccess | DstReg),
3929 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3930 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003931 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003932 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003933 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003934 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003935 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003936 II(ImplicitOps | Stack, em_pushf, pushf),
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003937 II(ImplicitOps | Stack, em_popf, popf), N, I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003938 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003939 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003940 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003941 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003942 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003943 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003944 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003945 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3946 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003947 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003948 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003949 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003950 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02003951 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003952 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003953 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003954 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003955 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003956 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3957 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003958 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003959 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003960 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
3961 N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003962 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003963 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003964 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003965 G(Src2One | ByteOp, group2), G(Src2One, group2),
3966 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003967 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02003968 I(DstAcc | SrcImmUByte | No64, em_aad),
3969 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02003970 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003971 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02003972 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003973 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003974 X3(I(SrcImmByte, em_loop)),
3975 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003976 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3977 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003978 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003979 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003980 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003981 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3982 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003983 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003984 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003985 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3986 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003987 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003988 D(ImplicitOps), D(ImplicitOps),
3989 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003990 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3991};
3992
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003993static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003994 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003995 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003996 N, I(ImplicitOps | VendorSpecific, em_syscall),
3997 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003998 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003999 N, D(ImplicitOps | ModRM), N, N,
4000 /* 0x10 - 0x1F */
4001 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
4002 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004003 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02004004 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09004005 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
4006 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004007 N, N, N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03004008 N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
4009 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004010 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004011 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004012 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004013 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004014 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004015 I(ImplicitOps | VendorSpecific, em_sysenter),
4016 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004017 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004018 N, N, N, N, N, N, N, N,
4019 /* 0x40 - 0x4F */
4020 X16(D(DstReg | SrcMem | ModRM | Mov)),
4021 /* 0x50 - 0x5F */
4022 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4023 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004024 N, N, N, N,
4025 N, N, N, N,
4026 N, N, N, N,
4027 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004028 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004029 N, N, N, N,
4030 N, N, N, N,
4031 N, N, N, N,
4032 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004033 /* 0x80 - 0x8F */
4034 X16(D(SrcImm)),
4035 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004036 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004037 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004038 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004039 II(ImplicitOps, em_cpuid, cpuid),
4040 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004041 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4042 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004043 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004044 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004045 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004046 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004047 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4048 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Avi Kivity4d758342013-01-19 19:51:55 +02004049 D(ModRM), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004050 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09004051 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004052 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004053 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004054 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4055 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004056 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004057 /* 0xB8 - 0xBF */
4058 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004059 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004060 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
4061 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004062 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004063 /* 0xC0 - 0xC7 */
Avi Kivity739ae402010-08-26 11:56:13 +03004064 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a52010-08-17 09:19:34 +08004065 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004066 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004067 /* 0xC8 - 0xCF */
4068 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004069 /* 0xD0 - 0xDF */
4070 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4071 /* 0xE0 - 0xEF */
4072 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4073 /* 0xF0 - 0xFF */
4074 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4075};
4076
4077#undef D
4078#undef N
4079#undef G
4080#undef GD
4081#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004082#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004083#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004084
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004085#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004086#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004087#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004088#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004089#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004090
Avi Kivity9dac77f2011-06-01 15:34:25 +03004091static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004092{
4093 unsigned size;
4094
Avi Kivity9dac77f2011-06-01 15:34:25 +03004095 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004096 if (size == 8)
4097 size = 4;
4098 return size;
4099}
4100
4101static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4102 unsigned size, bool sign_extension)
4103{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004104 int rc = X86EMUL_CONTINUE;
4105
4106 op->type = OP_IMM;
4107 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004108 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004109 /* NB. Immediates are sign-extended as necessary. */
4110 switch (op->bytes) {
4111 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004112 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004113 break;
4114 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004115 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004116 break;
4117 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004118 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004119 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004120 case 8:
4121 op->val = insn_fetch(s64, ctxt);
4122 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004123 }
4124 if (!sign_extension) {
4125 switch (op->bytes) {
4126 case 1:
4127 op->val &= 0xff;
4128 break;
4129 case 2:
4130 op->val &= 0xffff;
4131 break;
4132 case 4:
4133 op->val &= 0xffffffff;
4134 break;
4135 }
4136 }
4137done:
4138 return rc;
4139}
4140
Avi Kivitya99455492011-09-13 10:45:41 +03004141static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4142 unsigned d)
4143{
4144 int rc = X86EMUL_CONTINUE;
4145
4146 switch (d) {
4147 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004148 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03004149 break;
4150 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004151 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03004152 break;
4153 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004154 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004155 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03004156 *op = ctxt->memop;
4157 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03004158 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya99455492011-09-13 10:45:41 +03004159 fetch_bit_operand(ctxt);
4160 op->orig_val = op->val;
4161 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004162 case OpMem64:
4163 ctxt->memop.bytes = 8;
4164 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03004165 case OpAcc:
4166 op->type = OP_REG;
4167 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004168 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03004169 fetch_register_operand(op);
4170 op->orig_val = op->val;
4171 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004172 case OpAccLo:
4173 op->type = OP_REG;
4174 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4175 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4176 fetch_register_operand(op);
4177 op->orig_val = op->val;
4178 break;
4179 case OpAccHi:
4180 if (ctxt->d & ByteOp) {
4181 op->type = OP_NONE;
4182 break;
4183 }
4184 op->type = OP_REG;
4185 op->bytes = ctxt->op_bytes;
4186 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4187 fetch_register_operand(op);
4188 op->orig_val = op->val;
4189 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004190 case OpDI:
4191 op->type = OP_MEM;
4192 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4193 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004194 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya99455492011-09-13 10:45:41 +03004195 op->addr.mem.seg = VCPU_SREG_ES;
4196 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004197 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03004198 break;
4199 case OpDX:
4200 op->type = OP_REG;
4201 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004202 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03004203 fetch_register_operand(op);
4204 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004205 case OpCL:
4206 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004207 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004208 break;
4209 case OpImmByte:
4210 rc = decode_imm(ctxt, op, 1, true);
4211 break;
4212 case OpOne:
4213 op->bytes = 1;
4214 op->val = 1;
4215 break;
4216 case OpImm:
4217 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4218 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004219 case OpImm64:
4220 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4221 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004222 case OpMem8:
4223 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004224 if (ctxt->memop.type == OP_REG) {
4225 ctxt->memop.addr.reg = decode_register(ctxt, ctxt->modrm_rm, 1);
4226 fetch_register_operand(&ctxt->memop);
4227 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004228 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004229 case OpMem16:
4230 ctxt->memop.bytes = 2;
4231 goto mem_common;
4232 case OpMem32:
4233 ctxt->memop.bytes = 4;
4234 goto mem_common;
4235 case OpImmU16:
4236 rc = decode_imm(ctxt, op, 2, false);
4237 break;
4238 case OpImmU:
4239 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4240 break;
4241 case OpSI:
4242 op->type = OP_MEM;
4243 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4244 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004245 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Avi Kivity0fe59122011-09-13 10:45:47 +03004246 op->addr.mem.seg = seg_override(ctxt);
4247 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004248 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004249 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004250 case OpXLat:
4251 op->type = OP_MEM;
4252 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4253 op->addr.mem.ea =
4254 register_address(ctxt,
4255 reg_read(ctxt, VCPU_REGS_RBX) +
4256 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
4257 op->addr.mem.seg = seg_override(ctxt);
4258 op->val = 0;
4259 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004260 case OpImmFAddr:
4261 op->type = OP_IMM;
4262 op->addr.mem.ea = ctxt->_eip;
4263 op->bytes = ctxt->op_bytes + 2;
4264 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4265 break;
4266 case OpMemFAddr:
4267 ctxt->memop.bytes = ctxt->op_bytes + 2;
4268 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004269 case OpES:
4270 op->val = VCPU_SREG_ES;
4271 break;
4272 case OpCS:
4273 op->val = VCPU_SREG_CS;
4274 break;
4275 case OpSS:
4276 op->val = VCPU_SREG_SS;
4277 break;
4278 case OpDS:
4279 op->val = VCPU_SREG_DS;
4280 break;
4281 case OpFS:
4282 op->val = VCPU_SREG_FS;
4283 break;
4284 case OpGS:
4285 op->val = VCPU_SREG_GS;
4286 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004287 case OpImplicit:
4288 /* Special instructions do their own operand decoding. */
4289 default:
4290 op->type = OP_NONE; /* Disable writeback. */
4291 break;
4292 }
4293
4294done:
4295 return rc;
4296}
4297
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004298int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004299{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004300 int rc = X86EMUL_CONTINUE;
4301 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004302 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004303 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03004304 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004305
Avi Kivityf09ed832011-09-13 10:45:40 +03004306 ctxt->memop.type = OP_NONE;
4307 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004308 ctxt->_eip = ctxt->eip;
4309 ctxt->fetch.start = ctxt->_eip;
4310 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01004311 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004312 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004313
4314 switch (mode) {
4315 case X86EMUL_MODE_REAL:
4316 case X86EMUL_MODE_VM86:
4317 case X86EMUL_MODE_PROT16:
4318 def_op_bytes = def_ad_bytes = 2;
4319 break;
4320 case X86EMUL_MODE_PROT32:
4321 def_op_bytes = def_ad_bytes = 4;
4322 break;
4323#ifdef CONFIG_X86_64
4324 case X86EMUL_MODE_PROT64:
4325 def_op_bytes = 4;
4326 def_ad_bytes = 8;
4327 break;
4328#endif
4329 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004330 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004331 }
4332
Avi Kivity9dac77f2011-06-01 15:34:25 +03004333 ctxt->op_bytes = def_op_bytes;
4334 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004335
4336 /* Legacy prefixes. */
4337 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004338 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004339 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004340 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004341 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004342 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004343 break;
4344 case 0x67: /* address-size override */
4345 if (mode == X86EMUL_MODE_PROT64)
4346 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004347 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004348 else
4349 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004350 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004351 break;
4352 case 0x26: /* ES override */
4353 case 0x2e: /* CS override */
4354 case 0x36: /* SS override */
4355 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004356 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004357 break;
4358 case 0x64: /* FS override */
4359 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004360 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004361 break;
4362 case 0x40 ... 0x4f: /* REX */
4363 if (mode != X86EMUL_MODE_PROT64)
4364 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004365 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004366 continue;
4367 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004368 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004369 break;
4370 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004371 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004372 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004373 break;
4374 default:
4375 goto done_prefixes;
4376 }
4377
4378 /* Any legacy prefix after a REX prefix nullifies its effect. */
4379
Avi Kivity9dac77f2011-06-01 15:34:25 +03004380 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004381 }
4382
4383done_prefixes:
4384
4385 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004386 if (ctxt->rex_prefix & 8)
4387 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004388
4389 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004390 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004391 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004392 if (ctxt->b == 0x0f) {
4393 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004394 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004395 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004396 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004397 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004398
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004399 if (ctxt->d & ModRM)
4400 ctxt->modrm = insn_fetch(u8, ctxt);
4401
Avi Kivity9dac77f2011-06-01 15:34:25 +03004402 while (ctxt->d & GroupMask) {
4403 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004404 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004405 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004406 opcode = opcode.u.group[goffset];
4407 break;
4408 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004409 goffset = (ctxt->modrm >> 3) & 7;
4410 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004411 opcode = opcode.u.gdual->mod3[goffset];
4412 else
4413 opcode = opcode.u.gdual->mod012[goffset];
4414 break;
4415 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004416 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004417 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004418 break;
4419 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004420 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004421 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004422 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004423 switch (simd_prefix) {
4424 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4425 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4426 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4427 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4428 }
4429 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004430 case Escape:
4431 if (ctxt->modrm > 0xbf)
4432 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4433 else
4434 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4435 break;
Avi Kivity46561642011-04-24 14:09:59 +03004436 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004437 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004438 }
Avi Kivity46561642011-04-24 14:09:59 +03004439
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004440 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004441 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004442 }
4443
Avi Kivity9dac77f2011-06-01 15:34:25 +03004444 ctxt->execute = opcode.u.execute;
4445 ctxt->check_perm = opcode.check_perm;
4446 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004447
4448 /* Unrecognised? */
Gleb Natapov1146a782013-04-11 12:30:01 +03004449 if (ctxt->d == 0 || (ctxt->d & NotImpl))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004450 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004451
Avi Kivity9dac77f2011-06-01 15:34:25 +03004452 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004453 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004454
Avi Kivity9dac77f2011-06-01 15:34:25 +03004455 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
4456 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004457
Avi Kivity9dac77f2011-06-01 15:34:25 +03004458 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004459 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004460 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004461 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03004462 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004463 }
4464
Avi Kivity9dac77f2011-06-01 15:34:25 +03004465 if (ctxt->d & Sse)
4466 ctxt->op_bytes = 16;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004467 else if (ctxt->d & Mmx)
4468 ctxt->op_bytes = 8;
Avi Kivity1253791d2011-03-29 11:41:27 +02004469
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004470 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004471 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004472 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004473 if (!ctxt->has_seg_override)
4474 set_seg_override(ctxt, ctxt->modrm_seg);
4475 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004476 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004477 if (rc != X86EMUL_CONTINUE)
4478 goto done;
4479
Avi Kivity9dac77f2011-06-01 15:34:25 +03004480 if (!ctxt->has_seg_override)
4481 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004482
Avi Kivityf09ed832011-09-13 10:45:40 +03004483 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004484
Avi Kivityf09ed832011-09-13 10:45:40 +03004485 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
4486 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004487
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004488 /*
4489 * Decode and fetch the source operand: register, memory
4490 * or immediate.
4491 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004492 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004493 if (rc != X86EMUL_CONTINUE)
4494 goto done;
4495
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004496 /*
4497 * Decode and fetch the second source operand: register, memory
4498 * or immediate.
4499 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004500 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004501 if (rc != X86EMUL_CONTINUE)
4502 goto done;
4503
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004504 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03004505 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004506
4507done:
Avi Kivityf09ed832011-09-13 10:45:40 +03004508 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
4509 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004510
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004511 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004512}
4513
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004514bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4515{
4516 return ctxt->d & PageTable;
4517}
4518
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004519static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4520{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004521 /* The second termination condition only applies for REPE
4522 * and REPNE. Test if the repeat string operation prefix is
4523 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4524 * corresponding termination condition according to:
4525 * - if REPE/REPZ and ZF = 0 then done
4526 * - if REPNE/REPNZ and ZF = 1 then done
4527 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004528 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4529 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4530 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004531 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004532 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004533 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4534 return true;
4535
4536 return false;
4537}
4538
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004539static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4540{
4541 bool fault = false;
4542
4543 ctxt->ops->get_fpu(ctxt);
4544 asm volatile("1: fwait \n\t"
4545 "2: \n\t"
4546 ".pushsection .fixup,\"ax\" \n\t"
4547 "3: \n\t"
4548 "movb $1, %[fault] \n\t"
4549 "jmp 2b \n\t"
4550 ".popsection \n\t"
4551 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004552 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004553 ctxt->ops->put_fpu(ctxt);
4554
4555 if (unlikely(fault))
4556 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4557
4558 return X86EMUL_CONTINUE;
4559}
4560
4561static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4562 struct operand *op)
4563{
4564 if (op->type == OP_MM)
4565 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4566}
4567
Avi Kivitye28bbd42013-01-04 16:18:48 +02004568static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4569{
4570 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivityb9fa4092013-02-09 11:31:48 +02004571 if (!(ctxt->d & ByteOp))
4572 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Avi Kivitye28bbd42013-01-04 16:18:48 +02004573 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
Avi Kivity017da7b2013-02-09 11:31:47 +02004574 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags)
Avi Kivitye28bbd42013-01-04 16:18:48 +02004575 : "c"(ctxt->src2.val), [fastop]"S"(fop));
4576 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
4577 return X86EMUL_CONTINUE;
4578}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004579
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004580int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004581{
Mathias Krause0225fb52012-08-30 01:30:16 +02004582 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004583 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004584 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004585
Avi Kivity9dac77f2011-06-01 15:34:25 +03004586 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004587
Gleb Natapov1146a782013-04-11 12:30:01 +03004588 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4589 (ctxt->d & Undefined)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004590 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02004591 goto done;
4592 }
4593
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004594 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004595 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004596 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004597 goto done;
4598 }
4599
Avi Kivity9dac77f2011-06-01 15:34:25 +03004600 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004601 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004602 goto done;
4603 }
4604
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004605 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4606 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity1253791d2011-03-29 11:41:27 +02004607 rc = emulate_ud(ctxt);
4608 goto done;
4609 }
4610
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004611 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity1253791d2011-03-29 11:41:27 +02004612 rc = emulate_nm(ctxt);
4613 goto done;
4614 }
4615
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004616 if (ctxt->d & Mmx) {
4617 rc = flush_pending_x87_faults(ctxt);
4618 if (rc != X86EMUL_CONTINUE)
4619 goto done;
4620 /*
4621 * Now that we know the fpu is exception safe, we can fetch
4622 * operands from it.
4623 */
4624 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4625 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4626 if (!(ctxt->d & Mov))
4627 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4628 }
4629
Avi Kivity9dac77f2011-06-01 15:34:25 +03004630 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4631 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004632 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004633 if (rc != X86EMUL_CONTINUE)
4634 goto done;
4635 }
4636
Gleb Natapove92805a2010-02-10 14:21:35 +02004637 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004638 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004639 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02004640 goto done;
4641 }
4642
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004643 /* Instruction can only be executed in protected mode */
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03004644 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004645 rc = emulate_ud(ctxt);
4646 goto done;
4647 }
4648
Joerg Roedeld09beab2011-04-04 12:39:25 +02004649 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004650 if (ctxt->check_perm) {
4651 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004652 if (rc != X86EMUL_CONTINUE)
4653 goto done;
4654 }
4655
Avi Kivity9dac77f2011-06-01 15:34:25 +03004656 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4657 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004658 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004659 if (rc != X86EMUL_CONTINUE)
4660 goto done;
4661 }
4662
Avi Kivity9dac77f2011-06-01 15:34:25 +03004663 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004664 /* All REP prefixes have the same first termination condition */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004665 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004666 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004667 goto done;
4668 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004669 }
4670
Avi Kivity9dac77f2011-06-01 15:34:25 +03004671 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4672 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4673 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004674 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004675 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004676 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004677 }
4678
Avi Kivity9dac77f2011-06-01 15:34:25 +03004679 if (ctxt->src2.type == OP_MEM) {
4680 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4681 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004682 if (rc != X86EMUL_CONTINUE)
4683 goto done;
4684 }
4685
Avi Kivity9dac77f2011-06-01 15:34:25 +03004686 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004687 goto special_insn;
4688
4689
Avi Kivity9dac77f2011-06-01 15:34:25 +03004690 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004691 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004692 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4693 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004694 if (rc != X86EMUL_CONTINUE)
4695 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004696 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004697 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004698
Avi Kivity018a98d2007-11-27 19:30:56 +02004699special_insn:
4700
Avi Kivity9dac77f2011-06-01 15:34:25 +03004701 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4702 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004703 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004704 if (rc != X86EMUL_CONTINUE)
4705 goto done;
4706 }
4707
Avi Kivity9dac77f2011-06-01 15:34:25 +03004708 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004709 if (ctxt->d & Fastop) {
4710 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4711 rc = fastop(ctxt, fop);
4712 if (rc != X86EMUL_CONTINUE)
4713 goto done;
4714 goto writeback;
4715 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004716 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004717 if (rc != X86EMUL_CONTINUE)
4718 goto done;
4719 goto writeback;
4720 }
4721
Avi Kivity9dac77f2011-06-01 15:34:25 +03004722 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004723 goto twobyte_insn;
4724
Avi Kivity9dac77f2011-06-01 15:34:25 +03004725 switch (ctxt->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004726 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004727 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004728 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004729 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004730 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004731 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004732 if (test_cc(ctxt->b, ctxt->eflags))
4733 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004734 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004735 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004736 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004737 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004738 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004739 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Mohammed Gamal34698d82010-08-04 14:41:04 +03004740 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004741 rc = em_xchg(ctxt);
4742 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004743 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004744 switch (ctxt->op_bytes) {
4745 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4746 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4747 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004748 }
4749 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004750 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004751 rc = emulate_int(ctxt, 3);
4752 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004753 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004754 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004755 break;
4756 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004757 if (ctxt->eflags & EFLG_OF)
4758 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004759 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004760 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004761 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004762 jmp_rel(ctxt, ctxt->src.val);
4763 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004764 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004765 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004766 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004767 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004768 case 0xf5: /* cmc */
4769 /* complement carry flag from eflags reg */
4770 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004771 break;
4772 case 0xf8: /* clc */
4773 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004774 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004775 case 0xf9: /* stc */
4776 ctxt->eflags |= EFLG_CF;
4777 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004778 case 0xfc: /* cld */
4779 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004780 break;
4781 case 0xfd: /* std */
4782 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004783 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004784 default:
4785 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004786 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004787
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004788 if (rc != X86EMUL_CONTINUE)
4789 goto done;
4790
Avi Kivity018a98d2007-11-27 19:30:56 +02004791writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02004792 if (!(ctxt->d & NoWrite)) {
4793 rc = writeback(ctxt, &ctxt->dst);
4794 if (rc != X86EMUL_CONTINUE)
4795 goto done;
4796 }
4797 if (ctxt->d & SrcWrite) {
4798 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
4799 rc = writeback(ctxt, &ctxt->src);
4800 if (rc != X86EMUL_CONTINUE)
4801 goto done;
4802 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004803
Gleb Natapov5cd21912010-03-18 15:20:26 +02004804 /*
4805 * restore dst type in case the decoding will be reused
4806 * (happens for string instruction )
4807 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004808 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004809
Avi Kivity9dac77f2011-06-01 15:34:25 +03004810 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004811 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004812
Avi Kivity9dac77f2011-06-01 15:34:25 +03004813 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004814 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004815
Avi Kivity9dac77f2011-06-01 15:34:25 +03004816 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004817 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004818 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004819 if ((ctxt->d & SrcMask) == SrcSI)
4820 count = ctxt->src.count;
4821 else
4822 count = ctxt->dst.count;
4823 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4824 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004825
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004826 if (!string_insn_completed(ctxt)) {
4827 /*
4828 * Re-enter guest when pio read ahead buffer is empty
4829 * or, if it is not used, after each 1024 iteration.
4830 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004831 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004832 (r->end == 0 || r->end != r->pos)) {
4833 /*
4834 * Reset read cache. Usually happens before
4835 * decode, but since instruction is restarted
4836 * we have to do it here.
4837 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004838 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004839 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004840 return EMULATION_RESTART;
4841 }
4842 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004843 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004844 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004845
Avi Kivity9dac77f2011-06-01 15:34:25 +03004846 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004847
4848done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004849 if (rc == X86EMUL_PROPAGATE_FAULT)
4850 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004851 if (rc == X86EMUL_INTERCEPTED)
4852 return EMULATION_INTERCEPTED;
4853
Avi Kivitydd856ef2012-08-27 23:46:17 +03004854 if (rc == X86EMUL_CONTINUE)
4855 writeback_registers(ctxt);
4856
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004857 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004858
4859twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004860 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004861 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004862 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004863 break;
4864 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004865 case 0x0d: /* GrpP (prefetch) */
4866 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004867 break;
4868 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004869 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004870 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004871 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004872 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004873 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004874 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004875 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4876 if (!test_cc(ctxt->b, ctxt->eflags))
4877 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004878 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004879 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004880 if (test_cc(ctxt->b, ctxt->eflags))
4881 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004882 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004883 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004884 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004885 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004886 case 0xae: /* clflush */
4887 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004888 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004889 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004890 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004891 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004892 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004893 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004894 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004895 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004896 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004897 break;
Wei Yongjun92f738a52010-08-17 09:19:34 +08004898 case 0xc0 ... 0xc1: /* xadd */
Avi Kivity158de572013-01-19 19:51:57 +02004899 fastop(ctxt, em_add);
Wei Yongjun92f738a52010-08-17 09:19:34 +08004900 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004901 ctxt->src.val = ctxt->dst.orig_val;
4902 write_register_operand(&ctxt->src);
Wei Yongjun92f738a52010-08-17 09:19:34 +08004903 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004904 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004905 ctxt->dst.bytes = ctxt->op_bytes;
4906 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4907 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004908 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004909 default:
4910 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004911 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004912
4913 if (rc != X86EMUL_CONTINUE)
4914 goto done;
4915
Avi Kivity6aa8b732006-12-10 02:21:36 -08004916 goto writeback;
4917
4918cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004919 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004920}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004921
4922void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
4923{
4924 invalidate_registers(ctxt);
4925}
4926
4927void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
4928{
4929 writeback_registers(ctxt);
4930}