blob: 36cb786122fe02b2b6dfe6e5c5183b08bbf4bb5e [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivityb7d491e2013-01-04 16:18:49 +020027#include <linux/stringify.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028
Avi Kivity3eeb3282010-01-21 15:31:48 +020029#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020030#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020031
Avi Kivity6aa8b732006-12-10 02:21:36 -080032/*
Avi Kivitya9945542011-09-13 10:45:41 +030033 * Operand types
34 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030035#define OpNone 0ull
36#define OpImplicit 1ull /* No generic decode */
37#define OpReg 2ull /* Register */
38#define OpMem 3ull /* Memory */
39#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
40#define OpDI 5ull /* ES:DI/EDI/RDI */
41#define OpMem64 6ull /* Memory, 64-bit */
42#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
43#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030044#define OpCL 9ull /* CL register (for shifts) */
45#define OpImmByte 10ull /* 8-bit sign extended immediate */
46#define OpOne 11ull /* Implied 1 */
Nadav Amit5e2c6882012-12-06 21:55:10 -020047#define OpImm 12ull /* Sign extended up to 32-bit immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030048#define OpMem16 13ull /* Memory operand (16-bit). */
49#define OpMem32 14ull /* Memory operand (32-bit). */
50#define OpImmU 15ull /* Immediate operand, zero extended */
51#define OpSI 16ull /* SI/ESI/RSI */
52#define OpImmFAddr 17ull /* Immediate far address */
53#define OpMemFAddr 18ull /* Far address in memory */
54#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030055#define OpES 20ull /* ES */
56#define OpCS 21ull /* CS */
57#define OpSS 22ull /* SS */
58#define OpDS 23ull /* DS */
59#define OpFS 24ull /* FS */
60#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020061#define OpMem8 26ull /* 8-bit zero extended memory operand */
Nadav Amit5e2c6882012-12-06 21:55:10 -020062#define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */
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 Kivitya9945542011-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 Kivitya9945542011-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 Kivitya9945542011-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 Kivitya9945542011-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)
145#define Src2CL (OpCL << Src2Shift)
146#define Src2ImmByte (OpImmByte << Src2Shift)
147#define Src2One (OpOne << Src2Shift)
148#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300149#define Src2ES (OpES << Src2Shift)
150#define Src2CS (OpCS << Src2Shift)
151#define Src2SS (OpSS << Src2Shift)
152#define Src2DS (OpDS << Src2Shift)
153#define Src2FS (OpFS << Src2Shift)
154#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300155#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300156#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Avi Kivity1c11b372012-04-09 18:39:59 +0300157#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
158#define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */
159#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
Avi Kivitye28bbd42013-01-04 16:18:48 +0200160#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
Avi Kivityb6744dc2013-01-04 16:18:50 +0200161#define NoWrite ((u64)1 << 45) /* No writeback */
Avi Kivityfb32b1e2013-02-09 11:31:44 +0200162#define SrcWrite ((u64)1 << 46) /* Write back src operand */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800163
Avi Kivity820207c2013-02-09 11:31:45 +0200164#define DstXacc (DstAccLo | SrcAccHi | SrcWrite)
165
Avi Kivityd0e53322010-07-29 15:11:54 +0300166#define X2(x...) x, x
167#define X3(x...) X2(x), x
168#define X4(x...) X2(x), X2(x)
169#define X5(x...) X4(x), x
170#define X6(x...) X4(x), X2(x)
171#define X7(x...) X4(x), X3(x)
172#define X8(x...) X4(x), X4(x)
173#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300174
Avi Kivitye28bbd42013-01-04 16:18:48 +0200175#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
176#define FASTOP_SIZE 8
177
178/*
179 * fastop functions have a special calling convention:
180 *
181 * dst: [rdx]:rax (in/out)
182 * src: rbx (in/out)
183 * src2: rcx (in)
184 * flags: rflags (in/out)
185 *
186 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
187 * different operand sizes can be reached by calculation, rather than a jump
188 * table (which would be bigger than the code).
189 *
190 * fastop functions are declared as taking a never-defined fastop parameter,
191 * so they can't be called from C directly.
192 */
193
194struct fastop;
195
Avi Kivityd65b1de2010-07-29 15:11:35 +0300196struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300197 u64 flags : 56;
198 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300199 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300200 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200201 const struct opcode *group;
202 const struct group_dual *gdual;
203 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200204 const struct escape *esc;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200205 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300206 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200207 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300208};
209
210struct group_dual {
211 struct opcode mod012[8];
212 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300213};
214
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200215struct gprefix {
216 struct opcode pfx_no;
217 struct opcode pfx_66;
218 struct opcode pfx_f2;
219 struct opcode pfx_f3;
220};
221
Gleb Natapov045a2822012-12-20 16:57:43 +0200222struct escape {
223 struct opcode op[8];
224 struct opcode high[64];
225};
226
Avi Kivity6aa8b732006-12-10 02:21:36 -0800227/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200228#define EFLG_ID (1<<21)
229#define EFLG_VIP (1<<20)
230#define EFLG_VIF (1<<19)
231#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200232#define EFLG_VM (1<<17)
233#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200234#define EFLG_IOPL (3<<12)
235#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800236#define EFLG_OF (1<<11)
237#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200238#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200239#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800240#define EFLG_SF (1<<7)
241#define EFLG_ZF (1<<6)
242#define EFLG_AF (1<<4)
243#define EFLG_PF (1<<2)
244#define EFLG_CF (1<<0)
245
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300246#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
247#define EFLG_RESERVED_ONE_MASK 2
248
Avi Kivitydd856ef2012-08-27 23:46:17 +0300249static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
250{
251 if (!(ctxt->regs_valid & (1 << nr))) {
252 ctxt->regs_valid |= 1 << nr;
253 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
254 }
255 return ctxt->_regs[nr];
256}
257
258static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
259{
260 ctxt->regs_valid |= 1 << nr;
261 ctxt->regs_dirty |= 1 << nr;
262 return &ctxt->_regs[nr];
263}
264
265static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
266{
267 reg_read(ctxt, nr);
268 return reg_write(ctxt, nr);
269}
270
271static void writeback_registers(struct x86_emulate_ctxt *ctxt)
272{
273 unsigned reg;
274
275 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
276 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
277}
278
279static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
280{
281 ctxt->regs_dirty = 0;
282 ctxt->regs_valid = 0;
283}
284
Avi Kivity6aa8b732006-12-10 02:21:36 -0800285/*
286 * Instruction emulation:
287 * Most instructions are emulated directly via a fragment of inline assembly
288 * code. This allows us to save/restore EFLAGS and thus very easily pick up
289 * any modified flags.
290 */
291
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800292#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800293#define _LO32 "k" /* force 32-bit operand */
294#define _STK "%%rsp" /* stack pointer */
295#elif defined(__i386__)
296#define _LO32 "" /* force 32-bit operand */
297#define _STK "%%esp" /* stack pointer */
298#endif
299
300/*
301 * These EFLAGS bits are restored from saved value during emulation, and
302 * any changes are written back to the saved value after emulation.
303 */
304#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
305
306/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200307#define _PRE_EFLAGS(_sav, _msk, _tmp) \
308 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
309 "movl %"_sav",%"_LO32 _tmp"; " \
310 "push %"_tmp"; " \
311 "push %"_tmp"; " \
312 "movl %"_msk",%"_LO32 _tmp"; " \
313 "andl %"_LO32 _tmp",("_STK"); " \
314 "pushf; " \
315 "notl %"_LO32 _tmp"; " \
316 "andl %"_LO32 _tmp",("_STK"); " \
317 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
318 "pop %"_tmp"; " \
319 "orl %"_LO32 _tmp",("_STK"); " \
320 "popf; " \
321 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800322
323/* After executing instruction: write-back necessary bits in EFLAGS. */
324#define _POST_EFLAGS(_sav, _msk, _tmp) \
325 /* _sav |= EFLAGS & _msk; */ \
326 "pushf; " \
327 "pop %"_tmp"; " \
328 "andl %"_msk",%"_LO32 _tmp"; " \
329 "orl %"_LO32 _tmp",%"_sav"; "
330
Avi Kivitydda96d82008-11-26 15:14:10 +0200331#ifdef CONFIG_X86_64
332#define ON64(x) x
333#else
334#define ON64(x)
335#endif
336
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300337#define ____emulate_2op(ctxt, _op, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200338 do { \
339 __asm__ __volatile__ ( \
340 _PRE_EFLAGS("0", "4", "2") \
341 _op _suffix " %"_x"3,%1; " \
342 _POST_EFLAGS("0", "4", "2") \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300343 : "=m" ((ctxt)->eflags), \
344 "+q" (*(_dsttype*)&(ctxt)->dst.val), \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200345 "=&r" (_tmp) \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300346 : _y ((ctxt)->src.val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200347 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200348
349
Avi Kivity6aa8b732006-12-10 02:21:36 -0800350/* Raw emulation: instruction has two explicit operands. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300351#define __emulate_2op_nobyte(ctxt,_op,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200352 do { \
353 unsigned long _tmp; \
354 \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300355 switch ((ctxt)->dst.bytes) { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200356 case 2: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300357 ____emulate_2op(ctxt,_op,_wx,_wy,"w",u16); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200358 break; \
359 case 4: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300360 ____emulate_2op(ctxt,_op,_lx,_ly,"l",u32); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200361 break; \
362 case 8: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300363 ON64(____emulate_2op(ctxt,_op,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200364 break; \
365 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800366 } while (0)
367
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300368#define __emulate_2op(ctxt,_op,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800369 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200370 unsigned long _tmp; \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300371 switch ((ctxt)->dst.bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800372 case 1: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300373 ____emulate_2op(ctxt,_op,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800374 break; \
375 default: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300376 __emulate_2op_nobyte(ctxt, _op, \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800377 _wx, _wy, _lx, _ly, _qx, _qy); \
378 break; \
379 } \
380 } while (0)
381
382/* Source operand is byte-sized and may be restricted to just %cl. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300383#define emulate_2op_SrcB(ctxt, _op) \
384 __emulate_2op(ctxt, _op, "b", "c", "b", "c", "b", "c", "b", "c")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800385
386/* Source operand is byte, word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300387#define emulate_2op_SrcV(ctxt, _op) \
388 __emulate_2op(ctxt, _op, "b", "q", "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800389
390/* Source operand is word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300391#define emulate_2op_SrcV_nobyte(ctxt, _op) \
392 __emulate_2op_nobyte(ctxt, _op, "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800393
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100394/* Instruction has three operands and one operand is stored in ECX register */
Avi Kivity29053a62011-09-07 16:41:37 +0300395#define __emulate_2op_cl(ctxt, _op, _suffix, _type) \
Avi Kivity72952612011-04-20 13:12:27 +0300396 do { \
397 unsigned long _tmp; \
Avi Kivity761441b2011-09-07 16:41:36 +0300398 _type _clv = (ctxt)->src2.val; \
399 _type _srcv = (ctxt)->src.val; \
400 _type _dstv = (ctxt)->dst.val; \
Avi Kivity72952612011-04-20 13:12:27 +0300401 \
402 __asm__ __volatile__ ( \
403 _PRE_EFLAGS("0", "5", "2") \
404 _op _suffix " %4,%1 \n" \
405 _POST_EFLAGS("0", "5", "2") \
Avi Kivity761441b2011-09-07 16:41:36 +0300406 : "=m" ((ctxt)->eflags), "+r" (_dstv), "=&r" (_tmp) \
Avi Kivity72952612011-04-20 13:12:27 +0300407 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
408 ); \
409 \
Avi Kivity761441b2011-09-07 16:41:36 +0300410 (ctxt)->src2.val = (unsigned long) _clv; \
411 (ctxt)->src2.val = (unsigned long) _srcv; \
412 (ctxt)->dst.val = (unsigned long) _dstv; \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100413 } while (0)
414
Avi Kivity761441b2011-09-07 16:41:36 +0300415#define emulate_2op_cl(ctxt, _op) \
Avi Kivity72952612011-04-20 13:12:27 +0300416 do { \
Avi Kivity761441b2011-09-07 16:41:36 +0300417 switch ((ctxt)->dst.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300418 case 2: \
Avi Kivity29053a62011-09-07 16:41:37 +0300419 __emulate_2op_cl(ctxt, _op, "w", u16); \
Avi Kivity72952612011-04-20 13:12:27 +0300420 break; \
421 case 4: \
Avi Kivity29053a62011-09-07 16:41:37 +0300422 __emulate_2op_cl(ctxt, _op, "l", u32); \
Avi Kivity72952612011-04-20 13:12:27 +0300423 break; \
424 case 8: \
Avi Kivity29053a62011-09-07 16:41:37 +0300425 ON64(__emulate_2op_cl(ctxt, _op, "q", ulong)); \
Avi Kivity72952612011-04-20 13:12:27 +0300426 break; \
427 } \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100428 } while (0)
429
Avi Kivityd1eef452011-09-07 16:41:38 +0300430#define __emulate_1op(ctxt, _op, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800431 do { \
432 unsigned long _tmp; \
433 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200434 __asm__ __volatile__ ( \
435 _PRE_EFLAGS("0", "3", "2") \
436 _op _suffix " %1; " \
437 _POST_EFLAGS("0", "3", "2") \
Avi Kivityd1eef452011-09-07 16:41:38 +0300438 : "=m" ((ctxt)->eflags), "+m" ((ctxt)->dst.val), \
Avi Kivitydda96d82008-11-26 15:14:10 +0200439 "=&r" (_tmp) \
440 : "i" (EFLAGS_MASK)); \
441 } while (0)
442
443/* Instruction has only one explicit operand (no source operand). */
Avi Kivityd1eef452011-09-07 16:41:38 +0300444#define emulate_1op(ctxt, _op) \
Avi Kivitydda96d82008-11-26 15:14:10 +0200445 do { \
Avi Kivityd1eef452011-09-07 16:41:38 +0300446 switch ((ctxt)->dst.bytes) { \
447 case 1: __emulate_1op(ctxt, _op, "b"); break; \
448 case 2: __emulate_1op(ctxt, _op, "w"); break; \
449 case 4: __emulate_1op(ctxt, _op, "l"); break; \
450 case 8: ON64(__emulate_1op(ctxt, _op, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800451 } \
452 } while (0)
453
Avi Kivity4d758342013-01-19 19:51:55 +0200454static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *));
455
Avi Kivityb7d491e2013-01-04 16:18:49 +0200456#define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t"
457#define FOP_RET "ret \n\t"
458
459#define FOP_START(op) \
460 extern void em_##op(struct fastop *fake); \
461 asm(".pushsection .text, \"ax\" \n\t" \
462 ".global em_" #op " \n\t" \
463 FOP_ALIGN \
464 "em_" #op ": \n\t"
465
466#define FOP_END \
467 ".popsection")
468
Avi Kivity0bdea062013-01-19 19:51:50 +0200469#define FOPNOP() FOP_ALIGN FOP_RET
470
Avi Kivityb7d491e2013-01-04 16:18:49 +0200471#define FOP1E(op, dst) \
472 FOP_ALIGN #op " %" #dst " \n\t" FOP_RET
473
474#define FASTOP1(op) \
475 FOP_START(op) \
476 FOP1E(op##b, al) \
477 FOP1E(op##w, ax) \
478 FOP1E(op##l, eax) \
479 ON64(FOP1E(op##q, rax)) \
480 FOP_END
481
Avi Kivityf7857f32013-01-04 16:18:53 +0200482#define FOP2E(op, dst, src) \
483 FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET
484
485#define FASTOP2(op) \
486 FOP_START(op) \
487 FOP2E(op##b, al, bl) \
488 FOP2E(op##w, ax, bx) \
489 FOP2E(op##l, eax, ebx) \
490 ON64(FOP2E(op##q, rax, rbx)) \
491 FOP_END
492
Avi Kivity11c363b2013-01-19 19:51:54 +0200493/* 2 operand, word only */
494#define FASTOP2W(op) \
495 FOP_START(op) \
496 FOPNOP() \
497 FOP2E(op##w, ax, bx) \
498 FOP2E(op##l, eax, ebx) \
499 ON64(FOP2E(op##q, rax, rbx)) \
500 FOP_END
501
Avi Kivity007a3b52013-01-19 19:51:51 +0200502/* 2 operand, src is CL */
503#define FASTOP2CL(op) \
504 FOP_START(op) \
505 FOP2E(op##b, al, cl) \
506 FOP2E(op##w, ax, cl) \
507 FOP2E(op##l, eax, cl) \
508 ON64(FOP2E(op##q, rax, cl)) \
509 FOP_END
510
Avi Kivity0bdea062013-01-19 19:51:50 +0200511#define FOP3E(op, dst, src, src2) \
512 FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
513
514/* 3-operand, word-only, src2=cl */
515#define FASTOP3WCL(op) \
516 FOP_START(op) \
517 FOPNOP() \
518 FOP3E(op##w, ax, bx, cl) \
519 FOP3E(op##l, eax, ebx, cl) \
520 ON64(FOP3E(op##q, rax, rbx, cl)) \
521 FOP_END
522
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200523/* Special case for SETcc - 1 instruction per cc */
524#define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t"
525
526FOP_START(setcc)
527FOP_SETCC(seto)
528FOP_SETCC(setno)
529FOP_SETCC(setc)
530FOP_SETCC(setnc)
531FOP_SETCC(setz)
532FOP_SETCC(setnz)
533FOP_SETCC(setbe)
534FOP_SETCC(setnbe)
535FOP_SETCC(sets)
536FOP_SETCC(setns)
537FOP_SETCC(setp)
538FOP_SETCC(setnp)
539FOP_SETCC(setl)
540FOP_SETCC(setnl)
541FOP_SETCC(setle)
542FOP_SETCC(setnle)
543FOP_END;
544
Paolo Bonzini326f5782013-05-09 11:32:51 +0200545FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET
546FOP_END;
547
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300548#define __emulate_1op_rax_rdx(ctxt, _op, _suffix, _ex) \
Avi Kivityf6b35972010-08-26 11:59:00 +0300549 do { \
550 unsigned long _tmp; \
Avi Kivitydd856ef2012-08-27 23:46:17 +0300551 ulong *rax = reg_rmw((ctxt), VCPU_REGS_RAX); \
552 ulong *rdx = reg_rmw((ctxt), VCPU_REGS_RDX); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300553 \
554 __asm__ __volatile__ ( \
555 _PRE_EFLAGS("0", "5", "1") \
556 "1: \n\t" \
557 _op _suffix " %6; " \
558 "2: \n\t" \
559 _POST_EFLAGS("0", "5", "1") \
560 ".pushsection .fixup,\"ax\" \n\t" \
561 "3: movb $1, %4 \n\t" \
562 "jmp 2b \n\t" \
563 ".popsection \n\t" \
564 _ASM_EXTABLE(1b, 3b) \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300565 : "=m" ((ctxt)->eflags), "=&r" (_tmp), \
566 "+a" (*rax), "+d" (*rdx), "+qm"(_ex) \
H. Peter Anvincb7cb282012-11-21 14:41:21 -0800567 : "i" (EFLAGS_MASK), "m" ((ctxt)->src.val)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300568 } while (0)
569
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300570/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300571#define emulate_1op_rax_rdx(ctxt, _op, _ex) \
Avi Kivity72952612011-04-20 13:12:27 +0300572 do { \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300573 switch((ctxt)->src.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300574 case 1: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300575 __emulate_1op_rax_rdx(ctxt, _op, "b", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300576 break; \
577 case 2: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300578 __emulate_1op_rax_rdx(ctxt, _op, "w", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300579 break; \
580 case 4: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300581 __emulate_1op_rax_rdx(ctxt, _op, "l", _ex); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300582 break; \
583 case 8: ON64( \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300584 __emulate_1op_rax_rdx(ctxt, _op, "q", _ex)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300585 break; \
586 } \
587 } while (0)
588
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200589static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
590 enum x86_intercept intercept,
591 enum x86_intercept_stage stage)
592{
593 struct x86_instruction_info info = {
594 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300595 .rep_prefix = ctxt->rep_prefix,
596 .modrm_mod = ctxt->modrm_mod,
597 .modrm_reg = ctxt->modrm_reg,
598 .modrm_rm = ctxt->modrm_rm,
599 .src_val = ctxt->src.val64,
600 .src_bytes = ctxt->src.bytes,
601 .dst_bytes = ctxt->dst.bytes,
602 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200603 .next_rip = ctxt->eip,
604 };
605
Avi Kivity29535382011-04-20 13:37:53 +0300606 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200607}
608
Avi Kivityf47cfa32012-06-07 17:49:24 +0300609static void assign_masked(ulong *dest, ulong src, ulong mask)
610{
611 *dest = (*dest & ~mask) | (src & mask);
612}
613
Avi Kivity9dac77f2011-06-01 15:34:25 +0300614static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800615{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300616 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800617}
618
Avi Kivityf47cfa32012-06-07 17:49:24 +0300619static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
620{
621 u16 sel;
622 struct desc_struct ss;
623
624 if (ctxt->mode == X86EMUL_MODE_PROT64)
625 return ~0UL;
626 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
627 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
628}
629
Avi Kivity612e89f2012-06-12 20:03:23 +0300630static int stack_size(struct x86_emulate_ctxt *ctxt)
631{
632 return (__fls(stack_mask(ctxt)) + 1) >> 3;
633}
634
Avi Kivity6aa8b732006-12-10 02:21:36 -0800635/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800636static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300637address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800638{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300639 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800640 return reg;
641 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300642 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800643}
644
645static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300646register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800647{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300648 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800649}
650
Avi Kivity5ad105e2012-08-19 14:34:31 +0300651static void masked_increment(ulong *reg, ulong mask, int inc)
652{
653 assign_masked(reg, *reg + inc, mask);
654}
655
Harvey Harrison7a9572752008-02-19 07:40:41 -0800656static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300657register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800658{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300659 ulong mask;
660
Avi Kivity9dac77f2011-06-01 15:34:25 +0300661 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300662 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800663 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300664 mask = ad_mask(ctxt);
665 masked_increment(reg, mask, inc);
666}
667
668static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
669{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300670 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800671}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800672
Avi Kivity9dac77f2011-06-01 15:34:25 +0300673static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800674{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300675 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800676}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300677
Avi Kivity56697682011-04-03 14:08:51 +0300678static u32 desc_limit_scaled(struct desc_struct *desc)
679{
680 u32 limit = get_desc_limit(desc);
681
682 return desc->g ? (limit << 12) | 0xfff : limit;
683}
684
Avi Kivity9dac77f2011-06-01 15:34:25 +0300685static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300686{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300687 ctxt->has_seg_override = true;
688 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300689}
690
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900691static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300692{
693 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
694 return 0;
695
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900696 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300697}
698
Avi Kivity9dac77f2011-06-01 15:34:25 +0300699static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300700{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300701 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300702 return 0;
703
Avi Kivity9dac77f2011-06-01 15:34:25 +0300704 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300705}
706
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200707static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
708 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300709{
Avi Kivityda9cb572010-11-22 17:53:21 +0200710 ctxt->exception.vector = vec;
711 ctxt->exception.error_code = error;
712 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200713 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300714}
715
Joerg Roedel3b88e412011-04-04 12:39:29 +0200716static int emulate_db(struct x86_emulate_ctxt *ctxt)
717{
718 return emulate_exception(ctxt, DB_VECTOR, 0, false);
719}
720
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200721static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300722{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200723 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300724}
725
Avi Kivity618ff152011-04-03 12:32:09 +0300726static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
727{
728 return emulate_exception(ctxt, SS_VECTOR, err, true);
729}
730
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200731static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300732{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200733 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300734}
735
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200736static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300737{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200738 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300739}
740
Avi Kivity34d1f492010-08-26 11:59:01 +0300741static int emulate_de(struct x86_emulate_ctxt *ctxt)
742{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200743 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300744}
745
Avi Kivity12537912011-03-29 11:41:27 +0200746static int emulate_nm(struct x86_emulate_ctxt *ctxt)
747{
748 return emulate_exception(ctxt, NM_VECTOR, 0, false);
749}
750
Avi Kivity1aa36612011-04-27 13:20:30 +0300751static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
752{
753 u16 selector;
754 struct desc_struct desc;
755
756 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
757 return selector;
758}
759
760static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
761 unsigned seg)
762{
763 u16 dummy;
764 u32 base3;
765 struct desc_struct desc;
766
767 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
768 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
769}
770
Avi Kivity1c11b372012-04-09 18:39:59 +0300771/*
772 * x86 defines three classes of vector instructions: explicitly
773 * aligned, explicitly unaligned, and the rest, which change behaviour
774 * depending on whether they're AVX encoded or not.
775 *
776 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
777 * subject to the same check.
778 */
779static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
780{
781 if (likely(size < 16))
782 return false;
783
784 if (ctxt->d & Aligned)
785 return true;
786 else if (ctxt->d & Unaligned)
787 return false;
788 else if (ctxt->d & Avx)
789 return false;
790 else
791 return true;
792}
793
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400794static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300795 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400796 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300797 ulong *linear)
798{
Avi Kivity618ff152011-04-03 12:32:09 +0300799 struct desc_struct desc;
800 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300801 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300802 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300803 u16 sel;
Gleb Natapov3a78a4f2012-12-20 16:57:42 +0200804 unsigned cpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300805
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900806 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300807 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300808 case X86EMUL_MODE_PROT64:
809 if (((signed long)la << 16) >> 16 != la)
810 return emulate_gp(ctxt, 0);
811 break;
812 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300813 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
814 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300815 if (!usable)
816 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200817 /* code segment in protected mode or read-only data segment */
818 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
819 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300820 goto bad;
821 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400822 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300823 goto bad;
824 lim = desc_limit_scaled(&desc);
825 if ((desc.type & 8) || !(desc.type & 4)) {
826 /* expand-up segment */
827 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
828 goto bad;
829 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800830 /* expand-down segment */
Avi Kivity618ff152011-04-03 12:32:09 +0300831 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
832 goto bad;
833 lim = desc.d ? 0xffffffff : 0xffff;
834 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
835 goto bad;
836 }
Avi Kivity717746e2011-04-20 13:37:53 +0300837 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity618ff152011-04-03 12:32:09 +0300838 if (!(desc.type & 8)) {
839 /* data segment */
840 if (cpl > desc.dpl)
841 goto bad;
842 } else if ((desc.type & 8) && !(desc.type & 4)) {
843 /* nonconforming code segment */
844 if (cpl != desc.dpl)
845 goto bad;
846 } else if ((desc.type & 8) && (desc.type & 4)) {
847 /* conforming code segment */
848 if (cpl < desc.dpl)
849 goto bad;
850 }
851 break;
852 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300853 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300854 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300855 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
856 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300857 *linear = la;
858 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300859bad:
860 if (addr.seg == VCPU_SREG_SS)
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300861 return emulate_ss(ctxt, sel);
Avi Kivity618ff152011-04-03 12:32:09 +0300862 else
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300863 return emulate_gp(ctxt, sel);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300864}
865
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400866static int linearize(struct x86_emulate_ctxt *ctxt,
867 struct segmented_address addr,
868 unsigned size, bool write,
869 ulong *linear)
870{
871 return __linearize(ctxt, addr, size, write, false, linear);
872}
873
874
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200875static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
876 struct segmented_address addr,
877 void *data,
878 unsigned size)
879{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200880 int rc;
881 ulong linear;
882
Avi Kivity83b87952011-04-03 11:31:19 +0300883 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200884 if (rc != X86EMUL_CONTINUE)
885 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300886 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200887}
888
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900889/*
890 * Fetch the next byte of the instruction being emulated which is pointed to
891 * by ctxt->_eip, then increment ctxt->_eip.
892 *
893 * Also prefetch the remaining bytes of the instruction without crossing page
894 * boundary if they are not in fetch_cache yet.
895 */
896static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200897{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300898 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200899 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300900 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200901
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900902 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400903 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900904 struct segmented_address addr = { .seg = VCPU_SREG_CS,
905 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300906 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900907 size = min(15UL - cur_size,
908 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400909 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900910 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400911 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900912 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
913 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900914 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200915 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300916 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200917 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900918 *dest = fc->data[ctxt->_eip - fc->start];
919 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900920 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200921}
922
923static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900924 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200925{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900926 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200927
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200928 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900929 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200930 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200931 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900932 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900933 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200934 return rc;
935 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900936 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200937}
938
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900939/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900940#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900941({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900942 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900943 if (rc != X86EMUL_CONTINUE) \
944 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900945 (_type)_x; \
946})
947
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900948#define insn_fetch_arr(_arr, _size, _ctxt) \
949({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900950 if (rc != X86EMUL_CONTINUE) \
951 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900952})
953
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000954/*
955 * Given the 'reg' portion of a ModRM byte, and a register block, return a
956 * pointer into the block that addresses the relevant register.
957 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
958 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300959static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000960 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800961{
962 void *p;
963
Avi Kivity6aa8b732006-12-10 02:21:36 -0800964 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300965 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
966 else
967 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968 return p;
969}
970
971static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200972 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800973 u16 *size, unsigned long *address, int op_bytes)
974{
975 int rc;
976
977 if (op_bytes == 2)
978 op_bytes = 3;
979 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200980 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900981 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200983 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200984 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985 return rc;
986}
987
Avi Kivity34b77652013-01-19 19:51:56 +0200988FASTOP2(add);
989FASTOP2(or);
990FASTOP2(adc);
991FASTOP2(sbb);
992FASTOP2(and);
993FASTOP2(sub);
994FASTOP2(xor);
995FASTOP2(cmp);
996FASTOP2(test);
997
998FASTOP3WCL(shld);
999FASTOP3WCL(shrd);
1000
1001FASTOP2W(imul);
1002
1003FASTOP1(not);
1004FASTOP1(neg);
1005FASTOP1(inc);
1006FASTOP1(dec);
1007
1008FASTOP2CL(rol);
1009FASTOP2CL(ror);
1010FASTOP2CL(rcl);
1011FASTOP2CL(rcr);
1012FASTOP2CL(shl);
1013FASTOP2CL(shr);
1014FASTOP2CL(sar);
1015
1016FASTOP2W(bsf);
1017FASTOP2W(bsr);
1018FASTOP2W(bt);
1019FASTOP2W(bts);
1020FASTOP2W(btr);
1021FASTOP2W(btc);
1022
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001023static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001024{
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001025 u8 rc;
1026 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001027
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001028 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +02001029 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001030 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
1031 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001032}
1033
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001034static void fetch_register_operand(struct operand *op)
1035{
1036 switch (op->bytes) {
1037 case 1:
1038 op->val = *(u8 *)op->addr.reg;
1039 break;
1040 case 2:
1041 op->val = *(u16 *)op->addr.reg;
1042 break;
1043 case 4:
1044 op->val = *(u32 *)op->addr.reg;
1045 break;
1046 case 8:
1047 op->val = *(u64 *)op->addr.reg;
1048 break;
1049 }
1050}
1051
Avi Kivity12537912011-03-29 11:41:27 +02001052static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
1053{
1054 ctxt->ops->get_fpu(ctxt);
1055 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001056 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
1057 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
1058 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
1059 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
1060 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
1061 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
1062 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
1063 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001064#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001065 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
1066 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
1067 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
1068 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
1069 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
1070 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
1071 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
1072 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001073#endif
1074 default: BUG();
1075 }
1076 ctxt->ops->put_fpu(ctxt);
1077}
1078
1079static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
1080 int reg)
1081{
1082 ctxt->ops->get_fpu(ctxt);
1083 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001084 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
1085 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
1086 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
1087 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
1088 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
1089 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
1090 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
1091 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001092#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001093 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
1094 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
1095 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
1096 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
1097 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
1098 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
1099 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
1100 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001101#endif
1102 default: BUG();
1103 }
1104 ctxt->ops->put_fpu(ctxt);
1105}
1106
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001107static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1108{
1109 ctxt->ops->get_fpu(ctxt);
1110 switch (reg) {
1111 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1112 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1113 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1114 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1115 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1116 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1117 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1118 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1119 default: BUG();
1120 }
1121 ctxt->ops->put_fpu(ctxt);
1122}
1123
1124static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1125{
1126 ctxt->ops->get_fpu(ctxt);
1127 switch (reg) {
1128 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1129 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1130 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1131 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1132 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1133 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1134 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1135 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1136 default: BUG();
1137 }
1138 ctxt->ops->put_fpu(ctxt);
1139}
1140
Gleb Natapov045a2822012-12-20 16:57:43 +02001141static int em_fninit(struct x86_emulate_ctxt *ctxt)
1142{
1143 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1144 return emulate_nm(ctxt);
1145
1146 ctxt->ops->get_fpu(ctxt);
1147 asm volatile("fninit");
1148 ctxt->ops->put_fpu(ctxt);
1149 return X86EMUL_CONTINUE;
1150}
1151
1152static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1153{
1154 u16 fcw;
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("fnstcw %0": "+m"(fcw));
1161 ctxt->ops->put_fpu(ctxt);
1162
1163 /* force 2 byte destination */
1164 ctxt->dst.bytes = 2;
1165 ctxt->dst.val = fcw;
1166
1167 return X86EMUL_CONTINUE;
1168}
1169
1170static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1171{
1172 u16 fsw;
1173
1174 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1175 return emulate_nm(ctxt);
1176
1177 ctxt->ops->get_fpu(ctxt);
1178 asm volatile("fnstsw %0": "+m"(fsw));
1179 ctxt->ops->put_fpu(ctxt);
1180
1181 /* force 2 byte destination */
1182 ctxt->dst.bytes = 2;
1183 ctxt->dst.val = fsw;
1184
1185 return X86EMUL_CONTINUE;
1186}
1187
Avi Kivity12537912011-03-29 11:41:27 +02001188static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001189 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001190{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001191 unsigned reg = ctxt->modrm_reg;
1192 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +02001193
Avi Kivity9dac77f2011-06-01 15:34:25 +03001194 if (!(ctxt->d & ModRM))
1195 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +02001196
Avi Kivity9dac77f2011-06-01 15:34:25 +03001197 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001198 op->type = OP_XMM;
1199 op->bytes = 16;
1200 op->addr.xmm = reg;
1201 read_sse_reg(ctxt, &op->vec_val, reg);
1202 return;
1203 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001204 if (ctxt->d & Mmx) {
1205 reg &= 7;
1206 op->type = OP_MM;
1207 op->bytes = 8;
1208 op->addr.mm = reg;
1209 return;
1210 }
Avi Kivity12537912011-03-29 11:41:27 +02001211
Avi Kivity3c118e22007-10-31 10:27:04 +02001212 op->type = OP_REG;
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001213 if (ctxt->d & ByteOp) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001214 op->addr.reg = decode_register(ctxt, reg, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +02001215 op->bytes = 1;
1216 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001217 op->addr.reg = decode_register(ctxt, reg, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001218 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +02001219 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001220 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001221 op->orig_val = op->val;
1222}
1223
Avi Kivitya6e34072012-06-10 17:15:39 +03001224static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1225{
1226 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1227 ctxt->modrm_seg = VCPU_SREG_SS;
1228}
1229
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001230static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001231 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001232{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001233 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001234 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001235 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001236 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001237
Avi Kivity9dac77f2011-06-01 15:34:25 +03001238 if (ctxt->rex_prefix) {
1239 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
1240 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
1241 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001242 }
1243
Avi Kivity9dac77f2011-06-01 15:34:25 +03001244 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
1245 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
1246 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
1247 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001248
Avi Kivity9dac77f2011-06-01 15:34:25 +03001249 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001250 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001251 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001252 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm, ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001253 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001254 op->type = OP_XMM;
1255 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001256 op->addr.xmm = ctxt->modrm_rm;
1257 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001258 return rc;
1259 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001260 if (ctxt->d & Mmx) {
1261 op->type = OP_MM;
1262 op->bytes = 8;
1263 op->addr.xmm = ctxt->modrm_rm & 7;
1264 return rc;
1265 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001266 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001267 return rc;
1268 }
1269
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001270 op->type = OP_MEM;
1271
Avi Kivity9dac77f2011-06-01 15:34:25 +03001272 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001273 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1274 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1275 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1276 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001277
1278 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001279 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001280 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001281 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001282 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001283 break;
1284 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001285 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001286 break;
1287 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001288 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001289 break;
1290 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001291 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001292 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001293 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001294 break;
1295 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001296 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001297 break;
1298 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001299 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001300 break;
1301 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001302 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001303 break;
1304 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001305 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001306 break;
1307 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001308 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001309 break;
1310 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001311 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001312 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001313 break;
1314 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001315 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001316 break;
1317 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001318 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1319 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1320 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001321 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001322 } else {
1323 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001324 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001325 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001326 index_reg |= (sib >> 3) & 7;
1327 base_reg |= sib & 7;
1328 scale = sib >> 6;
1329
Avi Kivity9dac77f2011-06-01 15:34:25 +03001330 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001331 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001332 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001333 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001334 adjust_modrm_seg(ctxt, base_reg);
1335 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001336 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001337 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001338 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001339 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001340 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001341 } else {
1342 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001343 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001344 adjust_modrm_seg(ctxt, base_reg);
1345 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001346 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001347 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001348 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001349 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001350 break;
1351 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001352 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001353 break;
1354 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001355 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001356 break;
1357 }
1358 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001359 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001360done:
1361 return rc;
1362}
1363
1364static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001365 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001366{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001367 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001368
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001369 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001370 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001371 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001372 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001373 break;
1374 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001375 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001376 break;
1377 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001378 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001379 break;
1380 }
1381done:
1382 return rc;
1383}
1384
Avi Kivity9dac77f2011-06-01 15:34:25 +03001385static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001386{
Sheng Yang7129eec2010-09-28 16:33:32 +08001387 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001388
Avi Kivity9dac77f2011-06-01 15:34:25 +03001389 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1390 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001391
Avi Kivity9dac77f2011-06-01 15:34:25 +03001392 if (ctxt->src.bytes == 2)
1393 sv = (s16)ctxt->src.val & (s16)mask;
1394 else if (ctxt->src.bytes == 4)
1395 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001396
Avi Kivity9dac77f2011-06-01 15:34:25 +03001397 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001398 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001399
1400 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001401 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001402}
1403
Gleb Natapov9de41572010-04-28 19:15:22 +03001404static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001405 unsigned long addr, void *dest, unsigned size)
1406{
1407 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001408 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001409
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001410 if (mc->pos < mc->end)
1411 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001412
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001413 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001414
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001415 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1416 &ctxt->exception);
1417 if (rc != X86EMUL_CONTINUE)
1418 return rc;
1419
1420 mc->end += size;
1421
1422read_cached:
1423 memcpy(dest, mc->data + mc->pos, size);
1424 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001425 return X86EMUL_CONTINUE;
1426}
1427
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001428static int segmented_read(struct x86_emulate_ctxt *ctxt,
1429 struct segmented_address addr,
1430 void *data,
1431 unsigned size)
1432{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001433 int rc;
1434 ulong linear;
1435
Avi Kivity83b87952011-04-03 11:31:19 +03001436 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001437 if (rc != X86EMUL_CONTINUE)
1438 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001439 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001440}
1441
1442static int segmented_write(struct x86_emulate_ctxt *ctxt,
1443 struct segmented_address addr,
1444 const void *data,
1445 unsigned size)
1446{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001447 int rc;
1448 ulong linear;
1449
Avi Kivity83b87952011-04-03 11:31:19 +03001450 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001451 if (rc != X86EMUL_CONTINUE)
1452 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001453 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1454 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001455}
1456
1457static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1458 struct segmented_address addr,
1459 const void *orig_data, const void *data,
1460 unsigned size)
1461{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001462 int rc;
1463 ulong linear;
1464
Avi Kivity83b87952011-04-03 11:31:19 +03001465 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001466 if (rc != X86EMUL_CONTINUE)
1467 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001468 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1469 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001470}
1471
Gleb Natapov7b262e92010-03-18 15:20:27 +02001472static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001473 unsigned int size, unsigned short port,
1474 void *dest)
1475{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001476 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001477
1478 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001479 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001480 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001481 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001482 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001483 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1484 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Gleb Natapov7b262e92010-03-18 15:20:27 +02001485 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1486 count);
1487 if (n == 0)
1488 n = 1;
1489 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001490 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001491 return 0;
1492 rc->end = n * size;
1493 }
1494
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001495 if (ctxt->rep_prefix && !(ctxt->eflags & EFLG_DF)) {
1496 ctxt->dst.data = rc->data + rc->pos;
1497 ctxt->dst.type = OP_MEM_STR;
1498 ctxt->dst.count = (rc->end - rc->pos) / size;
1499 rc->pos = rc->end;
1500 } else {
1501 memcpy(dest, rc->data + rc->pos, size);
1502 rc->pos += size;
1503 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001504 return 1;
1505}
1506
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001507static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1508 u16 index, struct desc_struct *desc)
1509{
1510 struct desc_ptr dt;
1511 ulong addr;
1512
1513 ctxt->ops->get_idt(ctxt, &dt);
1514
1515 if (dt.size < index * 8 + 7)
1516 return emulate_gp(ctxt, index << 3 | 0x2);
1517
1518 addr = dt.address + index * 8;
1519 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1520 &ctxt->exception);
1521}
1522
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001523static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001524 u16 selector, struct desc_ptr *dt)
1525{
Mathias Krause0225fb52012-08-30 01:30:16 +02001526 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001527
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001528 if (selector & 1 << 2) {
1529 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001530 u16 sel;
1531
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001532 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001533 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001534 return;
1535
1536 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1537 dt->address = get_desc_base(&desc);
1538 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001539 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001540}
1541
1542/* allowed just for 8 bytes segments */
1543static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001544 u16 selector, struct desc_struct *desc,
1545 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001546{
1547 struct desc_ptr dt;
1548 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001549 ulong addr;
1550
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001551 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001552
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001553 if (dt.size < index * 8 + 7)
1554 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001555
Avi Kivitye9194642012-06-13 16:29:39 +03001556 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001557 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1558 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001559}
1560
1561/* allowed just for 8 bytes segments */
1562static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001563 u16 selector, struct desc_struct *desc)
1564{
1565 struct desc_ptr dt;
1566 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001567 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001568
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001569 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001570
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001571 if (dt.size < index * 8 + 7)
1572 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001573
1574 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001575 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1576 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001577}
1578
Gleb Natapov5601d052011-03-07 14:55:06 +02001579/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001580static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001581 u16 selector, int seg)
1582{
Avi Kivity869be992012-06-13 16:30:53 +03001583 struct desc_struct seg_desc, old_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001584 u8 dpl, rpl, cpl;
1585 unsigned err_vec = GP_VECTOR;
1586 u32 err_code = 0;
1587 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001588 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001589 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001590 u16 dummy;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001591
1592 memset(&seg_desc, 0, sizeof seg_desc);
1593
Kevin Wolff8da94e2013-04-11 14:06:03 +02001594 if (ctxt->mode == X86EMUL_MODE_REAL) {
1595 /* set real mode segment descriptor (keep limit etc. for
1596 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001597 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001598 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001599 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001600 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1601 /* VM86 needs a clean new segment descriptor */
1602 set_desc_base(&seg_desc, selector << 4);
1603 set_desc_limit(&seg_desc, 0xffff);
1604 seg_desc.type = 3;
1605 seg_desc.p = 1;
1606 seg_desc.s = 1;
1607 seg_desc.dpl = 3;
1608 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001609 }
1610
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001611 rpl = selector & 3;
1612 cpl = ctxt->ops->cpl(ctxt);
1613
1614 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1615 if ((seg == VCPU_SREG_CS
1616 || (seg == VCPU_SREG_SS
1617 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1618 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001619 && null_selector)
1620 goto exception;
1621
1622 /* TR should be in GDT only */
1623 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1624 goto exception;
1625
1626 if (null_selector) /* for NULL selector skip all following checks */
1627 goto load;
1628
Avi Kivitye9194642012-06-13 16:29:39 +03001629 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001630 if (ret != X86EMUL_CONTINUE)
1631 return ret;
1632
1633 err_code = selector & 0xfffc;
1634 err_vec = GP_VECTOR;
1635
Guo Chaofc058682012-06-28 15:19:51 +08001636 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001637 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1638 goto exception;
1639
1640 if (!seg_desc.p) {
1641 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1642 goto exception;
1643 }
1644
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001645 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001646
1647 switch (seg) {
1648 case VCPU_SREG_SS:
1649 /*
1650 * segment is not a writable data segment or segment
1651 * selector's RPL != CPL or segment selector's RPL != CPL
1652 */
1653 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1654 goto exception;
1655 break;
1656 case VCPU_SREG_CS:
1657 if (!(seg_desc.type & 8))
1658 goto exception;
1659
1660 if (seg_desc.type & 4) {
1661 /* conforming */
1662 if (dpl > cpl)
1663 goto exception;
1664 } else {
1665 /* nonconforming */
1666 if (rpl > cpl || dpl != cpl)
1667 goto exception;
1668 }
1669 /* CS(RPL) <- CPL */
1670 selector = (selector & 0xfffc) | cpl;
1671 break;
1672 case VCPU_SREG_TR:
1673 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1674 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001675 old_desc = seg_desc;
1676 seg_desc.type |= 2; /* busy */
1677 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1678 sizeof(seg_desc), &ctxt->exception);
1679 if (ret != X86EMUL_CONTINUE)
1680 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001681 break;
1682 case VCPU_SREG_LDTR:
1683 if (seg_desc.s || seg_desc.type != 2)
1684 goto exception;
1685 break;
1686 default: /* DS, ES, FS, or GS */
1687 /*
1688 * segment is not a data or readable code segment or
1689 * ((segment is a data or nonconforming code segment)
1690 * and (both RPL and CPL > DPL))
1691 */
1692 if ((seg_desc.type & 0xa) == 0x8 ||
1693 (((seg_desc.type & 0xc) != 0xc) &&
1694 (rpl > dpl && cpl > dpl)))
1695 goto exception;
1696 break;
1697 }
1698
1699 if (seg_desc.s) {
1700 /* mark segment as accessed */
1701 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001702 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001703 if (ret != X86EMUL_CONTINUE)
1704 return ret;
1705 }
1706load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001707 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001708 return X86EMUL_CONTINUE;
1709exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001710 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001711 return X86EMUL_PROPAGATE_FAULT;
1712}
1713
Wei Yongjun31be40b2010-08-17 09:17:30 +08001714static void write_register_operand(struct operand *op)
1715{
1716 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1717 switch (op->bytes) {
1718 case 1:
1719 *(u8 *)op->addr.reg = (u8)op->val;
1720 break;
1721 case 2:
1722 *(u16 *)op->addr.reg = (u16)op->val;
1723 break;
1724 case 4:
1725 *op->addr.reg = (u32)op->val;
1726 break; /* 64b: zero-extend */
1727 case 8:
1728 *op->addr.reg = op->val;
1729 break;
1730 }
1731}
1732
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001733static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001734{
1735 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001736
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001737 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001738 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001739 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001740 break;
1741 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001742 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001743 rc = segmented_cmpxchg(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001744 op->addr.mem,
1745 &op->orig_val,
1746 &op->val,
1747 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001748 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001749 rc = segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001750 op->addr.mem,
1751 &op->val,
1752 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001753 if (rc != X86EMUL_CONTINUE)
1754 return rc;
1755 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001756 case OP_MEM_STR:
1757 rc = segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001758 op->addr.mem,
1759 op->data,
1760 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001761 if (rc != X86EMUL_CONTINUE)
1762 return rc;
1763 break;
Avi Kivity12537912011-03-29 11:41:27 +02001764 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001765 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001766 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001767 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001768 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001769 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001770 case OP_NONE:
1771 /* no writeback */
1772 break;
1773 default:
1774 break;
1775 }
1776 return X86EMUL_CONTINUE;
1777}
1778
Avi Kivity51ddff52012-06-12 20:19:40 +03001779static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001780{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001781 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001782
Avi Kivity5ad105e2012-08-19 14:34:31 +03001783 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001784 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001785 addr.seg = VCPU_SREG_SS;
1786
Avi Kivity51ddff52012-06-12 20:19:40 +03001787 return segmented_write(ctxt, addr, data, bytes);
1788}
1789
1790static int em_push(struct x86_emulate_ctxt *ctxt)
1791{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001792 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001793 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001794 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001795}
1796
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001797static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001798 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001799{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001800 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001801 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001802
Avi Kivitydd856ef2012-08-27 23:46:17 +03001803 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001804 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001805 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001806 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001807 return rc;
1808
Avi Kivity5ad105e2012-08-19 14:34:31 +03001809 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001810 return rc;
1811}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001812
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001813static int em_pop(struct x86_emulate_ctxt *ctxt)
1814{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001815 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001816}
1817
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001818static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001819 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001820{
1821 int rc;
1822 unsigned long val, change_mask;
1823 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001824 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001825
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001826 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001827 if (rc != X86EMUL_CONTINUE)
1828 return rc;
1829
1830 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1831 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1832
1833 switch(ctxt->mode) {
1834 case X86EMUL_MODE_PROT64:
1835 case X86EMUL_MODE_PROT32:
1836 case X86EMUL_MODE_PROT16:
1837 if (cpl == 0)
1838 change_mask |= EFLG_IOPL;
1839 if (cpl <= iopl)
1840 change_mask |= EFLG_IF;
1841 break;
1842 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001843 if (iopl < 3)
1844 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001845 change_mask |= EFLG_IF;
1846 break;
1847 default: /* real mode */
1848 change_mask |= (EFLG_IOPL | EFLG_IF);
1849 break;
1850 }
1851
1852 *(unsigned long *)dest =
1853 (ctxt->eflags & ~change_mask) | (val & change_mask);
1854
1855 return rc;
1856}
1857
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001858static int em_popf(struct x86_emulate_ctxt *ctxt)
1859{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001860 ctxt->dst.type = OP_REG;
1861 ctxt->dst.addr.reg = &ctxt->eflags;
1862 ctxt->dst.bytes = ctxt->op_bytes;
1863 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001864}
1865
Avi Kivity612e89f2012-06-12 20:03:23 +03001866static int em_enter(struct x86_emulate_ctxt *ctxt)
1867{
1868 int rc;
1869 unsigned frame_size = ctxt->src.val;
1870 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001871 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001872
1873 if (nesting_level)
1874 return X86EMUL_UNHANDLEABLE;
1875
Avi Kivitydd856ef2012-08-27 23:46:17 +03001876 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1877 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001878 if (rc != X86EMUL_CONTINUE)
1879 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001880 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001881 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001882 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1883 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001884 stack_mask(ctxt));
1885 return X86EMUL_CONTINUE;
1886}
1887
Avi Kivityf47cfa32012-06-07 17:49:24 +03001888static int em_leave(struct x86_emulate_ctxt *ctxt)
1889{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001890 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001891 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001892 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001893}
1894
Avi Kivity1cd196e2011-09-13 10:45:51 +03001895static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001896{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001897 int seg = ctxt->src2.val;
1898
Avi Kivity9dac77f2011-06-01 15:34:25 +03001899 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001900
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001901 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001902}
1903
Avi Kivity1cd196e2011-09-13 10:45:51 +03001904static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001905{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001906 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001907 unsigned long selector;
1908 int rc;
1909
Avi Kivity9dac77f2011-06-01 15:34:25 +03001910 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001911 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001912 return rc;
1913
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001914 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001915 return rc;
1916}
1917
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001918static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001919{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001920 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001921 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001922 int reg = VCPU_REGS_RAX;
1923
1924 while (reg <= VCPU_REGS_RDI) {
1925 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001926 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001927
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001928 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001929 if (rc != X86EMUL_CONTINUE)
1930 return rc;
1931
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001932 ++reg;
1933 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001934
Wei Yongjunc37eda12010-06-15 09:03:33 +08001935 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001936}
1937
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001938static int em_pushf(struct x86_emulate_ctxt *ctxt)
1939{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001940 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001941 return em_push(ctxt);
1942}
1943
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001944static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001945{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001946 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001947 int reg = VCPU_REGS_RDI;
1948
1949 while (reg >= VCPU_REGS_RAX) {
1950 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001951 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001952 --reg;
1953 }
1954
Avi Kivitydd856ef2012-08-27 23:46:17 +03001955 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001956 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001957 break;
1958 --reg;
1959 }
1960 return rc;
1961}
1962
Avi Kivitydd856ef2012-08-27 23:46:17 +03001963static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001964{
Mathias Krause0225fb52012-08-30 01:30:16 +02001965 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001966 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001967 struct desc_ptr dt;
1968 gva_t cs_addr;
1969 gva_t eip_addr;
1970 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001971
1972 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001973 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001974 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001975 if (rc != X86EMUL_CONTINUE)
1976 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001977
1978 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1979
Avi Kivity9dac77f2011-06-01 15:34:25 +03001980 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001981 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001982 if (rc != X86EMUL_CONTINUE)
1983 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001984
Avi Kivity9dac77f2011-06-01 15:34:25 +03001985 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001986 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001987 if (rc != X86EMUL_CONTINUE)
1988 return rc;
1989
Avi Kivity4bff1e862011-04-20 13:37:53 +03001990 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001991
1992 eip_addr = dt.address + (irq << 2);
1993 cs_addr = dt.address + (irq << 2) + 2;
1994
Avi Kivity0f65dd72011-04-20 13:37:53 +03001995 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001996 if (rc != X86EMUL_CONTINUE)
1997 return rc;
1998
Avi Kivity0f65dd72011-04-20 13:37:53 +03001999 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002000 if (rc != X86EMUL_CONTINUE)
2001 return rc;
2002
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002003 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002004 if (rc != X86EMUL_CONTINUE)
2005 return rc;
2006
Avi Kivity9dac77f2011-06-01 15:34:25 +03002007 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002008
2009 return rc;
2010}
2011
Avi Kivitydd856ef2012-08-27 23:46:17 +03002012int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
2013{
2014 int rc;
2015
2016 invalidate_registers(ctxt);
2017 rc = __emulate_int_real(ctxt, irq);
2018 if (rc == X86EMUL_CONTINUE)
2019 writeback_registers(ctxt);
2020 return rc;
2021}
2022
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002023static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002024{
2025 switch(ctxt->mode) {
2026 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03002027 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002028 case X86EMUL_MODE_VM86:
2029 case X86EMUL_MODE_PROT16:
2030 case X86EMUL_MODE_PROT32:
2031 case X86EMUL_MODE_PROT64:
2032 default:
2033 /* Protected mode interrupts unimplemented yet */
2034 return X86EMUL_UNHANDLEABLE;
2035 }
2036}
2037
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002038static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002039{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002040 int rc = X86EMUL_CONTINUE;
2041 unsigned long temp_eip = 0;
2042 unsigned long temp_eflags = 0;
2043 unsigned long cs = 0;
2044 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
2045 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
2046 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
2047 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
2048
2049 /* TODO: Add stack limit check */
2050
Avi Kivity9dac77f2011-06-01 15:34:25 +03002051 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002052
2053 if (rc != X86EMUL_CONTINUE)
2054 return rc;
2055
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002056 if (temp_eip & ~0xffff)
2057 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002058
Avi Kivity9dac77f2011-06-01 15:34:25 +03002059 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002060
2061 if (rc != X86EMUL_CONTINUE)
2062 return rc;
2063
Avi Kivity9dac77f2011-06-01 15:34:25 +03002064 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002065
2066 if (rc != X86EMUL_CONTINUE)
2067 return rc;
2068
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002069 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002070
2071 if (rc != X86EMUL_CONTINUE)
2072 return rc;
2073
Avi Kivity9dac77f2011-06-01 15:34:25 +03002074 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002075
2076
Avi Kivity9dac77f2011-06-01 15:34:25 +03002077 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002078 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03002079 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002080 ctxt->eflags &= ~0xffff;
2081 ctxt->eflags |= temp_eflags;
2082 }
2083
2084 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
2085 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
2086
2087 return rc;
2088}
2089
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002090static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002091{
2092 switch(ctxt->mode) {
2093 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002094 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002095 case X86EMUL_MODE_VM86:
2096 case X86EMUL_MODE_PROT16:
2097 case X86EMUL_MODE_PROT32:
2098 case X86EMUL_MODE_PROT64:
2099 default:
2100 /* iret from protected mode unimplemented yet */
2101 return X86EMUL_UNHANDLEABLE;
2102 }
2103}
2104
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002105static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2106{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002107 int rc;
2108 unsigned short sel;
2109
Avi Kivity9dac77f2011-06-01 15:34:25 +03002110 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002111
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002112 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002113 if (rc != X86EMUL_CONTINUE)
2114 return rc;
2115
Avi Kivity9dac77f2011-06-01 15:34:25 +03002116 ctxt->_eip = 0;
2117 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002118 return X86EMUL_CONTINUE;
2119}
2120
Avi Kivity3329ece2011-09-13 10:45:39 +03002121static int em_mul_ex(struct x86_emulate_ctxt *ctxt)
2122{
2123 u8 ex = 0;
2124
2125 emulate_1op_rax_rdx(ctxt, "mul", ex);
2126 return X86EMUL_CONTINUE;
2127}
2128
2129static int em_imul_ex(struct x86_emulate_ctxt *ctxt)
2130{
2131 u8 ex = 0;
2132
2133 emulate_1op_rax_rdx(ctxt, "imul", ex);
2134 return X86EMUL_CONTINUE;
2135}
2136
2137static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002138{
Avi Kivity34d1f492010-08-26 11:59:01 +03002139 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002140
Avi Kivity3329ece2011-09-13 10:45:39 +03002141 emulate_1op_rax_rdx(ctxt, "div", de);
2142 if (de)
2143 return emulate_de(ctxt);
2144 return X86EMUL_CONTINUE;
2145}
2146
2147static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
2148{
2149 u8 de = 0;
2150
2151 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03002152 if (de)
2153 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03002154 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002155}
2156
Takuya Yoshikawa51187682011-05-02 02:29:17 +09002157static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002158{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002159 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002160
Avi Kivity9dac77f2011-06-01 15:34:25 +03002161 switch (ctxt->modrm_reg) {
Mohammed Gamald19292e2008-09-08 21:47:19 +03002162 case 2: /* call near abs */ {
2163 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002164 old_eip = ctxt->_eip;
2165 ctxt->_eip = ctxt->src.val;
2166 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002167 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03002168 break;
2169 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002170 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002171 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002172 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002173 case 5: /* jmp far */
2174 rc = em_jmp_far(ctxt);
2175 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002176 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002177 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002178 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002179 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002180 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002181}
2182
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002183static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002184{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002185 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002186
Avi Kivitydd856ef2012-08-27 23:46:17 +03002187 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2188 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2189 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2190 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002191 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002192 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002193 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2194 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002195
Laurent Vivier05f086f2007-09-24 11:10:55 +02002196 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002197 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002198 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002199}
2200
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002201static int em_ret(struct x86_emulate_ctxt *ctxt)
2202{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002203 ctxt->dst.type = OP_REG;
2204 ctxt->dst.addr.reg = &ctxt->_eip;
2205 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002206 return em_pop(ctxt);
2207}
2208
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002209static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002210{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002211 int rc;
2212 unsigned long cs;
2213
Avi Kivity9dac77f2011-06-01 15:34:25 +03002214 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002215 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002216 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002217 if (ctxt->op_bytes == 4)
2218 ctxt->_eip = (u32)ctxt->_eip;
2219 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002220 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002221 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002222 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002223 return rc;
2224}
2225
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002226static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2227{
2228 /* Save real source value, then compare EAX against destination. */
2229 ctxt->src.orig_val = ctxt->src.val;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002230 ctxt->src.val = reg_read(ctxt, VCPU_REGS_RAX);
Avi Kivity158de572013-01-19 19:51:57 +02002231 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002232
2233 if (ctxt->eflags & EFLG_ZF) {
2234 /* Success: write back to memory. */
2235 ctxt->dst.val = ctxt->src.orig_val;
2236 } else {
2237 /* Failure: write the value we saw to EAX. */
2238 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002239 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002240 }
2241 return X86EMUL_CONTINUE;
2242}
2243
Avi Kivityd4b43252011-09-13 10:45:50 +03002244static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002245{
Avi Kivityd4b43252011-09-13 10:45:50 +03002246 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002247 unsigned short sel;
2248 int rc;
2249
Avi Kivity9dac77f2011-06-01 15:34:25 +03002250 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002251
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002252 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002253 if (rc != X86EMUL_CONTINUE)
2254 return rc;
2255
Avi Kivity9dac77f2011-06-01 15:34:25 +03002256 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002257 return rc;
2258}
2259
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002260static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002261setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002262 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002263{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002264 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002265 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002266 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002267 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002268 cs->type = 0x0b; /* Read, Execute, Accessed */
2269 cs->s = 1;
2270 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002271 cs->p = 1;
2272 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002273 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002274
Gleb Natapov79168fd2010-04-28 19:15:30 +03002275 set_desc_base(ss, 0); /* flat segment */
2276 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002277 ss->g = 1; /* 4kb granularity */
2278 ss->s = 1;
2279 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002280 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002281 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002282 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002283 ss->l = 0;
2284 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002285}
2286
Avi Kivity1a18a692012-02-01 12:23:21 +02002287static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2288{
2289 u32 eax, ebx, ecx, edx;
2290
2291 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002292 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2293 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002294 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2295 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2296}
2297
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002298static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2299{
Mathias Krause0225fb52012-08-30 01:30:16 +02002300 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002301 u32 eax, ebx, ecx, edx;
2302
2303 /*
2304 * syscall should always be enabled in longmode - so only become
2305 * vendor specific (cpuid) if other modes are active...
2306 */
2307 if (ctxt->mode == X86EMUL_MODE_PROT64)
2308 return true;
2309
2310 eax = 0x00000000;
2311 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002312 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2313 /*
2314 * Intel ("GenuineIntel")
2315 * remark: Intel CPUs only support "syscall" in 64bit
2316 * longmode. Also an 64bit guest with a
2317 * 32bit compat-app running will #UD !! While this
2318 * behaviour can be fixed (by emulating) into AMD
2319 * response - CPUs of AMD can't behave like Intel.
2320 */
2321 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2322 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2323 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2324 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002325
Avi Kivity0017f932012-06-07 14:10:16 +03002326 /* AMD ("AuthenticAMD") */
2327 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2328 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2329 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2330 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002331
Avi Kivity0017f932012-06-07 14:10:16 +03002332 /* AMD ("AMDisbetter!") */
2333 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2334 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2335 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2336 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002337
2338 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2339 return false;
2340}
2341
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002342static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002343{
Mathias Krause0225fb52012-08-30 01:30:16 +02002344 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002345 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002346 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002347 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002348 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002349
2350 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002351 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002352 ctxt->mode == X86EMUL_MODE_VM86)
2353 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002354
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002355 if (!(em_syscall_is_enabled(ctxt)))
2356 return emulate_ud(ctxt);
2357
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002358 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002359 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002360
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002361 if (!(efer & EFER_SCE))
2362 return emulate_ud(ctxt);
2363
Avi Kivity717746e2011-04-20 13:37:53 +03002364 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002365 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002366 cs_sel = (u16)(msr_data & 0xfffc);
2367 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002368
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002369 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002370 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002371 cs.l = 1;
2372 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002373 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2374 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002375
Avi Kivitydd856ef2012-08-27 23:46:17 +03002376 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002377 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002378#ifdef CONFIG_X86_64
Avi Kivitydd856ef2012-08-27 23:46:17 +03002379 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002380
Avi Kivity717746e2011-04-20 13:37:53 +03002381 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002382 ctxt->mode == X86EMUL_MODE_PROT64 ?
2383 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002384 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002385
Avi Kivity717746e2011-04-20 13:37:53 +03002386 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002387 ctxt->eflags &= ~(msr_data | EFLG_RF);
2388#endif
2389 } else {
2390 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002391 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002392 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002393
2394 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2395 }
2396
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002397 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002398}
2399
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002400static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002401{
Mathias Krause0225fb52012-08-30 01:30:16 +02002402 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002403 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002404 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002405 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002406 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002407
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002408 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002409 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002410 if (ctxt->mode == X86EMUL_MODE_REAL)
2411 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002412
Avi Kivity1a18a692012-02-01 12:23:21 +02002413 /*
2414 * Not recognized on AMD in compat mode (but is recognized in legacy
2415 * mode).
2416 */
2417 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2418 && !vendor_intel(ctxt))
2419 return emulate_ud(ctxt);
2420
Andre Przywara8c604352009-06-18 12:56:01 +02002421 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2422 * Therefore, we inject an #UD.
2423 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002424 if (ctxt->mode == X86EMUL_MODE_PROT64)
2425 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002426
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002427 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002428
Avi Kivity717746e2011-04-20 13:37:53 +03002429 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002430 switch (ctxt->mode) {
2431 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002432 if ((msr_data & 0xfffc) == 0x0)
2433 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002434 break;
2435 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002436 if (msr_data == 0x0)
2437 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002438 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002439 default:
2440 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002441 }
2442
2443 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002444 cs_sel = (u16)msr_data;
2445 cs_sel &= ~SELECTOR_RPL_MASK;
2446 ss_sel = cs_sel + 8;
2447 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002448 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002449 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002450 cs.l = 1;
2451 }
2452
Avi Kivity1aa36612011-04-27 13:20:30 +03002453 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2454 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002455
Avi Kivity717746e2011-04-20 13:37:53 +03002456 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002457 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002458
Avi Kivity717746e2011-04-20 13:37:53 +03002459 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002460 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002461
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002462 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002463}
2464
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002465static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002466{
Mathias Krause0225fb52012-08-30 01:30:16 +02002467 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002468 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002469 u64 msr_data;
2470 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002471 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002472
Gleb Natapova0044752010-02-10 14:21:31 +02002473 /* inject #GP if in real mode or Virtual 8086 mode */
2474 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002475 ctxt->mode == X86EMUL_MODE_VM86)
2476 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002477
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002478 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002479
Avi Kivity9dac77f2011-06-01 15:34:25 +03002480 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002481 usermode = X86EMUL_MODE_PROT64;
2482 else
2483 usermode = X86EMUL_MODE_PROT32;
2484
2485 cs.dpl = 3;
2486 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002487 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002488 switch (usermode) {
2489 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002490 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002491 if ((msr_data & 0xfffc) == 0x0)
2492 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002493 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002494 break;
2495 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002496 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002497 if (msr_data == 0x0)
2498 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002499 ss_sel = cs_sel + 8;
2500 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002501 cs.l = 1;
2502 break;
2503 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002504 cs_sel |= SELECTOR_RPL_MASK;
2505 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002506
Avi Kivity1aa36612011-04-27 13:20:30 +03002507 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2508 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002509
Avi Kivitydd856ef2012-08-27 23:46:17 +03002510 ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX);
2511 *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX);
Andre Przywara4668f052009-06-18 12:56:02 +02002512
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002513 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002514}
2515
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002516static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002517{
2518 int iopl;
2519 if (ctxt->mode == X86EMUL_MODE_REAL)
2520 return false;
2521 if (ctxt->mode == X86EMUL_MODE_VM86)
2522 return true;
2523 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002524 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002525}
2526
2527static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002528 u16 port, u16 len)
2529{
Mathias Krause0225fb52012-08-30 01:30:16 +02002530 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002531 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002532 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002533 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002534 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002535 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002536 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002537
Avi Kivity1aa36612011-04-27 13:20:30 +03002538 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002539 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002540 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002541 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002542 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002543 base = get_desc_base(&tr_seg);
2544#ifdef CONFIG_X86_64
2545 base |= ((u64)base3) << 32;
2546#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002547 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002548 if (r != X86EMUL_CONTINUE)
2549 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002550 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002551 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002552 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002553 if (r != X86EMUL_CONTINUE)
2554 return false;
2555 if ((perm >> bit_idx) & mask)
2556 return false;
2557 return true;
2558}
2559
2560static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002561 u16 port, u16 len)
2562{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002563 if (ctxt->perm_ok)
2564 return true;
2565
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002566 if (emulator_bad_iopl(ctxt))
2567 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002568 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002569
2570 ctxt->perm_ok = true;
2571
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002572 return true;
2573}
2574
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002575static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002576 struct tss_segment_16 *tss)
2577{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002578 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002579 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002580 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2581 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2582 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2583 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2584 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2585 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2586 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2587 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002588
Avi Kivity1aa36612011-04-27 13:20:30 +03002589 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2590 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2591 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2592 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2593 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002594}
2595
2596static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002597 struct tss_segment_16 *tss)
2598{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002599 int ret;
2600
Avi Kivity9dac77f2011-06-01 15:34:25 +03002601 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002602 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002603 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2604 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2605 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2606 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2607 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2608 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2609 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2610 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002611
2612 /*
2613 * SDM says that segment selectors are loaded before segment
2614 * descriptors
2615 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002616 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2617 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2618 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2619 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2620 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002621
2622 /*
Guo Chaofc058682012-06-28 15:19:51 +08002623 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002624 * it is handled in a context of new task
2625 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002626 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
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->es, VCPU_SREG_ES);
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->cs, VCPU_SREG_CS);
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->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002636 if (ret != X86EMUL_CONTINUE)
2637 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002638 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002639 if (ret != X86EMUL_CONTINUE)
2640 return ret;
2641
2642 return X86EMUL_CONTINUE;
2643}
2644
2645static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002646 u16 tss_selector, u16 old_tss_sel,
2647 ulong old_tss_base, struct desc_struct *new_desc)
2648{
Mathias Krause0225fb52012-08-30 01:30:16 +02002649 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002650 struct tss_segment_16 tss_seg;
2651 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002652 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002653
Avi Kivity0f65dd72011-04-20 13:37:53 +03002654 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002655 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002656 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002657 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002658 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002659
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002660 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002661
Avi Kivity0f65dd72011-04-20 13:37:53 +03002662 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002663 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002664 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002665 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002666 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002667
Avi Kivity0f65dd72011-04-20 13:37:53 +03002668 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002669 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002670 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002671 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002672 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002673
2674 if (old_tss_sel != 0xffff) {
2675 tss_seg.prev_task_link = old_tss_sel;
2676
Avi Kivity0f65dd72011-04-20 13:37:53 +03002677 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002678 &tss_seg.prev_task_link,
2679 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002680 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002681 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002682 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002683 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002684 }
2685
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002686 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002687}
2688
2689static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002690 struct tss_segment_32 *tss)
2691{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002692 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002693 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002694 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002695 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2696 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2697 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2698 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2699 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2700 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2701 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2702 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002703
Avi Kivity1aa36612011-04-27 13:20:30 +03002704 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2705 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2706 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2707 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2708 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2709 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2710 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002711}
2712
2713static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002714 struct tss_segment_32 *tss)
2715{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002716 int ret;
2717
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002718 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002719 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002720 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002721 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002722
2723 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002724 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2725 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2726 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2727 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2728 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2729 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2730 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2731 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002732
2733 /*
2734 * SDM says that segment selectors are loaded before segment
2735 * descriptors
2736 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002737 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2738 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2739 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2740 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2741 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2742 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2743 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002744
2745 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002746 * If we're switching between Protected Mode and VM86, we need to make
2747 * sure to update the mode before loading the segment descriptors so
2748 * that the selectors are interpreted correctly.
2749 *
2750 * Need to get rflags to the vcpu struct immediately because it
2751 * influences the CPL which is checked at least when loading the segment
2752 * descriptors and when pushing an error code to the new kernel stack.
2753 *
2754 * TODO Introduce a separate ctxt->ops->set_cpl callback
2755 */
2756 if (ctxt->eflags & X86_EFLAGS_VM)
2757 ctxt->mode = X86EMUL_MODE_VM86;
2758 else
2759 ctxt->mode = X86EMUL_MODE_PROT32;
2760
2761 ctxt->ops->set_rflags(ctxt, ctxt->eflags);
2762
2763 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002764 * Now load segment descriptors. If fault happenes at this stage
2765 * it is handled in a context of new task
2766 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002767 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
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->es, VCPU_SREG_ES);
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->cs, VCPU_SREG_CS);
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->ss, VCPU_SREG_SS);
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->ds, VCPU_SREG_DS);
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->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002783 if (ret != X86EMUL_CONTINUE)
2784 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002785 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002786 if (ret != X86EMUL_CONTINUE)
2787 return ret;
2788
2789 return X86EMUL_CONTINUE;
2790}
2791
2792static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002793 u16 tss_selector, u16 old_tss_sel,
2794 ulong old_tss_base, struct desc_struct *new_desc)
2795{
Mathias Krause0225fb52012-08-30 01:30:16 +02002796 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002797 struct tss_segment_32 tss_seg;
2798 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002799 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002800
Avi Kivity0f65dd72011-04-20 13:37:53 +03002801 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002802 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002803 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002804 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002805 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002806
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002807 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002808
Avi Kivity0f65dd72011-04-20 13:37:53 +03002809 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002810 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002811 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002812 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002813 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002814
Avi Kivity0f65dd72011-04-20 13:37:53 +03002815 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002816 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002817 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002818 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002819 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002820
2821 if (old_tss_sel != 0xffff) {
2822 tss_seg.prev_task_link = old_tss_sel;
2823
Avi Kivity0f65dd72011-04-20 13:37:53 +03002824 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002825 &tss_seg.prev_task_link,
2826 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002827 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002828 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002829 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002830 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002831 }
2832
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002833 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002834}
2835
2836static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002837 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002838 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002839{
Mathias Krause0225fb52012-08-30 01:30:16 +02002840 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002841 struct desc_struct curr_tss_desc, next_tss_desc;
2842 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002843 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002844 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002845 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002846 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002847 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002848
2849 /* FIXME: old_tss_base == ~0 ? */
2850
Avi Kivitye9194642012-06-13 16:29:39 +03002851 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002852 if (ret != X86EMUL_CONTINUE)
2853 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002854 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002855 if (ret != X86EMUL_CONTINUE)
2856 return ret;
2857
2858 /* FIXME: check that next_tss_desc is tss */
2859
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002860 /*
2861 * Check privileges. The three cases are task switch caused by...
2862 *
2863 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2864 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002865 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002866 */
2867 if (reason == TASK_SWITCH_GATE) {
2868 if (idt_index != -1) {
2869 /* Software interrupts */
2870 struct desc_struct task_gate_desc;
2871 int dpl;
2872
2873 ret = read_interrupt_descriptor(ctxt, idt_index,
2874 &task_gate_desc);
2875 if (ret != X86EMUL_CONTINUE)
2876 return ret;
2877
2878 dpl = task_gate_desc.dpl;
2879 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2880 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2881 }
2882 } else if (reason != TASK_SWITCH_IRET) {
2883 int dpl = next_tss_desc.dpl;
2884 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2885 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002886 }
2887
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002888
Gleb Natapovceffb452010-03-18 15:20:19 +02002889 desc_limit = desc_limit_scaled(&next_tss_desc);
2890 if (!next_tss_desc.p ||
2891 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2892 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002893 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002894 return X86EMUL_PROPAGATE_FAULT;
2895 }
2896
2897 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2898 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002899 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002900 }
2901
2902 if (reason == TASK_SWITCH_IRET)
2903 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2904
2905 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002906 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002907 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2908 old_tss_sel = 0xffff;
2909
2910 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002911 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002912 old_tss_base, &next_tss_desc);
2913 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002914 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002915 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002916 if (ret != X86EMUL_CONTINUE)
2917 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002918
2919 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2920 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2921
2922 if (reason != TASK_SWITCH_IRET) {
2923 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002924 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002925 }
2926
Avi Kivity717746e2011-04-20 13:37:53 +03002927 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002928 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002929
Jan Kiszkae269fb22010-04-14 15:51:09 +02002930 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002931 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2932 ctxt->lock_prefix = 0;
2933 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002934 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002935 }
2936
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002937 return ret;
2938}
2939
2940int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002941 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002942 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002943{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002944 int rc;
2945
Avi Kivitydd856ef2012-08-27 23:46:17 +03002946 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002947 ctxt->_eip = ctxt->eip;
2948 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002949
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002950 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002951 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002952
Avi Kivitydd856ef2012-08-27 23:46:17 +03002953 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002954 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002955 writeback_registers(ctxt);
2956 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002957
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002958 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002959}
2960
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002961static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2962 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002963{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002964 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002965
Avi Kivitydd856ef2012-08-27 23:46:17 +03002966 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2967 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002968}
2969
Avi Kivity7af04fc2010-08-18 14:16:35 +03002970static int em_das(struct x86_emulate_ctxt *ctxt)
2971{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002972 u8 al, old_al;
2973 bool af, cf, old_cf;
2974
2975 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002976 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002977
2978 old_al = al;
2979 old_cf = cf;
2980 cf = false;
2981 af = ctxt->eflags & X86_EFLAGS_AF;
2982 if ((al & 0x0f) > 9 || af) {
2983 al -= 6;
2984 cf = old_cf | (al >= 250);
2985 af = true;
2986 } else {
2987 af = false;
2988 }
2989 if (old_al > 0x99 || old_cf) {
2990 al -= 0x60;
2991 cf = true;
2992 }
2993
Avi Kivity9dac77f2011-06-01 15:34:25 +03002994 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002995 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002996 ctxt->src.type = OP_IMM;
2997 ctxt->src.val = 0;
2998 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02002999 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03003000 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
3001 if (cf)
3002 ctxt->eflags |= X86_EFLAGS_CF;
3003 if (af)
3004 ctxt->eflags |= X86_EFLAGS_AF;
3005 return X86EMUL_CONTINUE;
3006}
3007
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003008static int em_aam(struct x86_emulate_ctxt *ctxt)
3009{
3010 u8 al, ah;
3011
3012 if (ctxt->src.val == 0)
3013 return emulate_de(ctxt);
3014
3015 al = ctxt->dst.val & 0xff;
3016 ah = al / ctxt->src.val;
3017 al %= ctxt->src.val;
3018
3019 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
3020
3021 /* Set PF, ZF, SF */
3022 ctxt->src.type = OP_IMM;
3023 ctxt->src.val = 0;
3024 ctxt->src.bytes = 1;
3025 fastop(ctxt, em_or);
3026
3027 return X86EMUL_CONTINUE;
3028}
3029
Gleb Natapov7f662272012-12-10 11:42:30 +02003030static int em_aad(struct x86_emulate_ctxt *ctxt)
3031{
3032 u8 al = ctxt->dst.val & 0xff;
3033 u8 ah = (ctxt->dst.val >> 8) & 0xff;
3034
3035 al = (al + (ah * ctxt->src.val)) & 0xff;
3036
3037 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
3038
Gleb Natapovf583c292013-02-13 17:50:39 +02003039 /* Set PF, ZF, SF */
3040 ctxt->src.type = OP_IMM;
3041 ctxt->src.val = 0;
3042 ctxt->src.bytes = 1;
3043 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02003044
3045 return X86EMUL_CONTINUE;
3046}
3047
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003048static int em_call(struct x86_emulate_ctxt *ctxt)
3049{
3050 long rel = ctxt->src.val;
3051
3052 ctxt->src.val = (unsigned long)ctxt->_eip;
3053 jmp_rel(ctxt, rel);
3054 return em_push(ctxt);
3055}
3056
Avi Kivity0ef753b2010-08-18 14:51:45 +03003057static int em_call_far(struct x86_emulate_ctxt *ctxt)
3058{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003059 u16 sel, old_cs;
3060 ulong old_eip;
3061 int rc;
3062
Avi Kivity1aa36612011-04-27 13:20:30 +03003063 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003064 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003065
Avi Kivity9dac77f2011-06-01 15:34:25 +03003066 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003067 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03003068 return X86EMUL_CONTINUE;
3069
Avi Kivity9dac77f2011-06-01 15:34:25 +03003070 ctxt->_eip = 0;
3071 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003072
Avi Kivity9dac77f2011-06-01 15:34:25 +03003073 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003074 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003075 if (rc != X86EMUL_CONTINUE)
3076 return rc;
3077
Avi Kivity9dac77f2011-06-01 15:34:25 +03003078 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003079 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003080}
3081
Avi Kivity40ece7c2010-08-18 15:12:09 +03003082static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3083{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003084 int rc;
3085
Avi Kivity9dac77f2011-06-01 15:34:25 +03003086 ctxt->dst.type = OP_REG;
3087 ctxt->dst.addr.reg = &ctxt->_eip;
3088 ctxt->dst.bytes = ctxt->op_bytes;
3089 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003090 if (rc != X86EMUL_CONTINUE)
3091 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003092 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003093 return X86EMUL_CONTINUE;
3094}
3095
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003096static int em_xchg(struct x86_emulate_ctxt *ctxt)
3097{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003098 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003099 ctxt->src.val = ctxt->dst.val;
3100 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003101
3102 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003103 ctxt->dst.val = ctxt->src.orig_val;
3104 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003105 return X86EMUL_CONTINUE;
3106}
3107
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003108static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3109{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003110 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003111 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003112}
3113
Avi Kivity61429142010-08-19 15:13:00 +03003114static int em_cwd(struct x86_emulate_ctxt *ctxt)
3115{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003116 ctxt->dst.type = OP_REG;
3117 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003118 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003119 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003120
3121 return X86EMUL_CONTINUE;
3122}
3123
Avi Kivity48bb5d32010-08-18 18:54:34 +03003124static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3125{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003126 u64 tsc = 0;
3127
Avi Kivity717746e2011-04-20 13:37:53 +03003128 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003129 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3130 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003131 return X86EMUL_CONTINUE;
3132}
3133
Avi Kivity222d21a2011-11-10 14:57:30 +02003134static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3135{
3136 u64 pmc;
3137
Avi Kivitydd856ef2012-08-27 23:46:17 +03003138 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003139 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003140 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3141 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003142 return X86EMUL_CONTINUE;
3143}
3144
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003145static int em_mov(struct x86_emulate_ctxt *ctxt)
3146{
Stefan Hajnoczi49597d82012-04-09 18:40:00 +03003147 memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003148 return X86EMUL_CONTINUE;
3149}
3150
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003151static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3152{
3153 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3154 return emulate_gp(ctxt, 0);
3155
3156 /* Disable writeback. */
3157 ctxt->dst.type = OP_NONE;
3158 return X86EMUL_CONTINUE;
3159}
3160
3161static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3162{
3163 unsigned long val;
3164
3165 if (ctxt->mode == X86EMUL_MODE_PROT64)
3166 val = ctxt->src.val & ~0ULL;
3167 else
3168 val = ctxt->src.val & ~0U;
3169
3170 /* #UD condition is already handled. */
3171 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3172 return emulate_gp(ctxt, 0);
3173
3174 /* Disable writeback. */
3175 ctxt->dst.type = OP_NONE;
3176 return X86EMUL_CONTINUE;
3177}
3178
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003179static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3180{
3181 u64 msr_data;
3182
Avi Kivitydd856ef2012-08-27 23:46:17 +03003183 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3184 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3185 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003186 return emulate_gp(ctxt, 0);
3187
3188 return X86EMUL_CONTINUE;
3189}
3190
3191static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3192{
3193 u64 msr_data;
3194
Avi Kivitydd856ef2012-08-27 23:46:17 +03003195 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003196 return emulate_gp(ctxt, 0);
3197
Avi Kivitydd856ef2012-08-27 23:46:17 +03003198 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3199 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003200 return X86EMUL_CONTINUE;
3201}
3202
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003203static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3204{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003205 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003206 return emulate_ud(ctxt);
3207
Avi Kivity9dac77f2011-06-01 15:34:25 +03003208 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003209 return X86EMUL_CONTINUE;
3210}
3211
3212static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3213{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003214 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003215
Avi Kivity9dac77f2011-06-01 15:34:25 +03003216 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003217 return emulate_ud(ctxt);
3218
Avi Kivity9dac77f2011-06-01 15:34:25 +03003219 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003220 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3221
3222 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003223 ctxt->dst.type = OP_NONE;
3224 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003225}
3226
Avi Kivitya14e5792012-06-13 12:28:33 +03003227static int em_lldt(struct x86_emulate_ctxt *ctxt)
3228{
3229 u16 sel = ctxt->src.val;
3230
3231 /* Disable writeback. */
3232 ctxt->dst.type = OP_NONE;
3233 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3234}
3235
Avi Kivity80890002012-06-13 16:33:29 +03003236static int em_ltr(struct x86_emulate_ctxt *ctxt)
3237{
3238 u16 sel = ctxt->src.val;
3239
3240 /* Disable writeback. */
3241 ctxt->dst.type = OP_NONE;
3242 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3243}
3244
Avi Kivity38503912011-03-31 18:48:09 +02003245static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3246{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003247 int rc;
3248 ulong linear;
3249
Avi Kivity9dac77f2011-06-01 15:34:25 +03003250 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003251 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003252 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003253 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003254 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003255 return X86EMUL_CONTINUE;
3256}
3257
Avi Kivity2d04a052011-04-20 15:32:49 +03003258static int em_clts(struct x86_emulate_ctxt *ctxt)
3259{
3260 ulong cr0;
3261
3262 cr0 = ctxt->ops->get_cr(ctxt, 0);
3263 cr0 &= ~X86_CR0_TS;
3264 ctxt->ops->set_cr(ctxt, 0, cr0);
3265 return X86EMUL_CONTINUE;
3266}
3267
Avi Kivity26d05cc2011-04-21 12:07:59 +03003268static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3269{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003270 int rc;
3271
Avi Kivity9dac77f2011-06-01 15:34:25 +03003272 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003273 return X86EMUL_UNHANDLEABLE;
3274
3275 rc = ctxt->ops->fix_hypercall(ctxt);
3276 if (rc != X86EMUL_CONTINUE)
3277 return rc;
3278
3279 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003280 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003281 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003282 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003283 return X86EMUL_CONTINUE;
3284}
3285
Avi Kivity96051572012-06-10 17:21:18 +03003286static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3287 void (*get)(struct x86_emulate_ctxt *ctxt,
3288 struct desc_ptr *ptr))
3289{
3290 struct desc_ptr desc_ptr;
3291
3292 if (ctxt->mode == X86EMUL_MODE_PROT64)
3293 ctxt->op_bytes = 8;
3294 get(ctxt, &desc_ptr);
3295 if (ctxt->op_bytes == 2) {
3296 ctxt->op_bytes = 4;
3297 desc_ptr.address &= 0x00ffffff;
3298 }
3299 /* Disable writeback. */
3300 ctxt->dst.type = OP_NONE;
3301 return segmented_write(ctxt, ctxt->dst.addr.mem,
3302 &desc_ptr, 2 + ctxt->op_bytes);
3303}
3304
3305static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3306{
3307 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3308}
3309
3310static int em_sidt(struct x86_emulate_ctxt *ctxt)
3311{
3312 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3313}
3314
Avi Kivity26d05cc2011-04-21 12:07:59 +03003315static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3316{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003317 struct desc_ptr desc_ptr;
3318 int rc;
3319
Avi Kivity510425f2012-06-07 17:04:36 +03003320 if (ctxt->mode == X86EMUL_MODE_PROT64)
3321 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003322 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003323 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003324 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003325 if (rc != X86EMUL_CONTINUE)
3326 return rc;
3327 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3328 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003329 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003330 return X86EMUL_CONTINUE;
3331}
3332
Avi Kivity5ef39c72011-04-21 12:21:50 +03003333static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003334{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003335 int rc;
3336
Avi Kivity5ef39c72011-04-21 12:21:50 +03003337 rc = ctxt->ops->fix_hypercall(ctxt);
3338
Avi Kivity26d05cc2011-04-21 12:07:59 +03003339 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003340 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003341 return rc;
3342}
3343
3344static int em_lidt(struct x86_emulate_ctxt *ctxt)
3345{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003346 struct desc_ptr desc_ptr;
3347 int rc;
3348
Avi Kivity510425f2012-06-07 17:04:36 +03003349 if (ctxt->mode == X86EMUL_MODE_PROT64)
3350 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003351 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003352 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003353 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003354 if (rc != X86EMUL_CONTINUE)
3355 return rc;
3356 ctxt->ops->set_idt(ctxt, &desc_ptr);
3357 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003358 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003359 return X86EMUL_CONTINUE;
3360}
3361
3362static int em_smsw(struct x86_emulate_ctxt *ctxt)
3363{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003364 ctxt->dst.bytes = 2;
3365 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003366 return X86EMUL_CONTINUE;
3367}
3368
3369static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3370{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003371 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003372 | (ctxt->src.val & 0x0f));
3373 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003374 return X86EMUL_CONTINUE;
3375}
3376
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003377static int em_loop(struct x86_emulate_ctxt *ctxt)
3378{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003379 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3380 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003381 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3382 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003383
3384 return X86EMUL_CONTINUE;
3385}
3386
3387static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3388{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003389 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003390 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003391
3392 return X86EMUL_CONTINUE;
3393}
3394
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003395static int em_in(struct x86_emulate_ctxt *ctxt)
3396{
3397 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3398 &ctxt->dst.val))
3399 return X86EMUL_IO_NEEDED;
3400
3401 return X86EMUL_CONTINUE;
3402}
3403
3404static int em_out(struct x86_emulate_ctxt *ctxt)
3405{
3406 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3407 &ctxt->src.val, 1);
3408 /* Disable writeback. */
3409 ctxt->dst.type = OP_NONE;
3410 return X86EMUL_CONTINUE;
3411}
3412
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003413static int em_cli(struct x86_emulate_ctxt *ctxt)
3414{
3415 if (emulator_bad_iopl(ctxt))
3416 return emulate_gp(ctxt, 0);
3417
3418 ctxt->eflags &= ~X86_EFLAGS_IF;
3419 return X86EMUL_CONTINUE;
3420}
3421
3422static int em_sti(struct x86_emulate_ctxt *ctxt)
3423{
3424 if (emulator_bad_iopl(ctxt))
3425 return emulate_gp(ctxt, 0);
3426
3427 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3428 ctxt->eflags |= X86_EFLAGS_IF;
3429 return X86EMUL_CONTINUE;
3430}
3431
Avi Kivity6d6eede2012-06-07 14:11:36 +03003432static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3433{
3434 u32 eax, ebx, ecx, edx;
3435
Avi Kivitydd856ef2012-08-27 23:46:17 +03003436 eax = reg_read(ctxt, VCPU_REGS_RAX);
3437 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003438 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003439 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3440 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3441 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3442 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003443 return X86EMUL_CONTINUE;
3444}
3445
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003446static int em_lahf(struct x86_emulate_ctxt *ctxt)
3447{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003448 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3449 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003450 return X86EMUL_CONTINUE;
3451}
3452
Avi Kivity92998362012-06-13 12:25:06 +03003453static int em_bswap(struct x86_emulate_ctxt *ctxt)
3454{
3455 switch (ctxt->op_bytes) {
3456#ifdef CONFIG_X86_64
3457 case 8:
3458 asm("bswap %0" : "+r"(ctxt->dst.val));
3459 break;
3460#endif
3461 default:
3462 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3463 break;
3464 }
3465 return X86EMUL_CONTINUE;
3466}
3467
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003468static bool valid_cr(int nr)
3469{
3470 switch (nr) {
3471 case 0:
3472 case 2 ... 4:
3473 case 8:
3474 return true;
3475 default:
3476 return false;
3477 }
3478}
3479
3480static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3481{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003482 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003483 return emulate_ud(ctxt);
3484
3485 return X86EMUL_CONTINUE;
3486}
3487
3488static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3489{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003490 u64 new_val = ctxt->src.val64;
3491 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003492 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003493
3494 static u64 cr_reserved_bits[] = {
3495 0xffffffff00000000ULL,
3496 0, 0, 0, /* CR3 checked later */
3497 CR4_RESERVED_BITS,
3498 0, 0, 0,
3499 CR8_RESERVED_BITS,
3500 };
3501
3502 if (!valid_cr(cr))
3503 return emulate_ud(ctxt);
3504
3505 if (new_val & cr_reserved_bits[cr])
3506 return emulate_gp(ctxt, 0);
3507
3508 switch (cr) {
3509 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003510 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003511 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3512 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3513 return emulate_gp(ctxt, 0);
3514
Avi Kivity717746e2011-04-20 13:37:53 +03003515 cr4 = ctxt->ops->get_cr(ctxt, 4);
3516 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003517
3518 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3519 !(cr4 & X86_CR4_PAE))
3520 return emulate_gp(ctxt, 0);
3521
3522 break;
3523 }
3524 case 3: {
3525 u64 rsvd = 0;
3526
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003527 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3528 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003529 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003530 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003531 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003532 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003533 rsvd = CR3_NONPAE_RESERVED_BITS;
3534
3535 if (new_val & rsvd)
3536 return emulate_gp(ctxt, 0);
3537
3538 break;
3539 }
3540 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003541 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003542
3543 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3544 return emulate_gp(ctxt, 0);
3545
3546 break;
3547 }
3548 }
3549
3550 return X86EMUL_CONTINUE;
3551}
3552
Joerg Roedel3b88e412011-04-04 12:39:29 +02003553static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3554{
3555 unsigned long dr7;
3556
Avi Kivity717746e2011-04-20 13:37:53 +03003557 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003558
3559 /* Check if DR7.Global_Enable is set */
3560 return dr7 & (1 << 13);
3561}
3562
3563static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3564{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003565 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003566 u64 cr4;
3567
3568 if (dr > 7)
3569 return emulate_ud(ctxt);
3570
Avi Kivity717746e2011-04-20 13:37:53 +03003571 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003572 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3573 return emulate_ud(ctxt);
3574
3575 if (check_dr7_gd(ctxt))
3576 return emulate_db(ctxt);
3577
3578 return X86EMUL_CONTINUE;
3579}
3580
3581static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3582{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003583 u64 new_val = ctxt->src.val64;
3584 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003585
3586 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3587 return emulate_gp(ctxt, 0);
3588
3589 return check_dr_read(ctxt);
3590}
3591
Joerg Roedel01de8b02011-04-04 12:39:31 +02003592static int check_svme(struct x86_emulate_ctxt *ctxt)
3593{
3594 u64 efer;
3595
Avi Kivity717746e2011-04-20 13:37:53 +03003596 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003597
3598 if (!(efer & EFER_SVME))
3599 return emulate_ud(ctxt);
3600
3601 return X86EMUL_CONTINUE;
3602}
3603
3604static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3605{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003606 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003607
3608 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003609 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003610 return emulate_gp(ctxt, 0);
3611
3612 return check_svme(ctxt);
3613}
3614
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003615static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3616{
Avi Kivity717746e2011-04-20 13:37:53 +03003617 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003618
Avi Kivity717746e2011-04-20 13:37:53 +03003619 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003620 return emulate_ud(ctxt);
3621
3622 return X86EMUL_CONTINUE;
3623}
3624
Joerg Roedel80612522011-04-04 12:39:33 +02003625static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3626{
Avi Kivity717746e2011-04-20 13:37:53 +03003627 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003628 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003629
Avi Kivity717746e2011-04-20 13:37:53 +03003630 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02003631 (rcx > 3))
3632 return emulate_gp(ctxt, 0);
3633
3634 return X86EMUL_CONTINUE;
3635}
3636
Joerg Roedelf6511932011-04-04 12:39:35 +02003637static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3638{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003639 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3640 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003641 return emulate_gp(ctxt, 0);
3642
3643 return X86EMUL_CONTINUE;
3644}
3645
3646static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3647{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003648 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3649 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003650 return emulate_gp(ctxt, 0);
3651
3652 return X86EMUL_CONTINUE;
3653}
3654
Avi Kivity73fba5f2010-07-29 15:11:53 +03003655#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003656#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003657#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3658 .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003659#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003660#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003661#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3662#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003663#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003664#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003665#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003666#define II(_f, _e, _i) \
3667 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003668#define IIP(_f, _e, _i, _p) \
3669 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3670 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003671#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003672
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003673#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003674#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003675#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003676#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003677#define I2bvIP(_f, _e, _i, _p) \
3678 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003679
Avi Kivityfb864fb2013-01-04 16:18:54 +02003680#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3681 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3682 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003683
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003684static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003685 DI(SrcNone | Priv, monitor),
3686 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003687 N, N, N, N, N, N,
3688};
3689
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003690static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003691 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
3692 II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall),
3693 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3694 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3695 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3696 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3697 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3698 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003699};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003700
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003701static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003702 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003703 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003704 N, N, N, N, N, N,
3705};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003706
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003707static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003708 F(Lock, em_add),
3709 F(Lock | PageTable, em_or),
3710 F(Lock, em_adc),
3711 F(Lock, em_sbb),
3712 F(Lock | PageTable, em_and),
3713 F(Lock, em_sub),
3714 F(Lock, em_xor),
3715 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003716};
3717
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003718static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003719 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003720};
3721
Avi Kivity007a3b52013-01-19 19:51:51 +02003722static const struct opcode group2[] = {
3723 F(DstMem | ModRM, em_rol),
3724 F(DstMem | ModRM, em_ror),
3725 F(DstMem | ModRM, em_rcl),
3726 F(DstMem | ModRM, em_rcr),
3727 F(DstMem | ModRM, em_shl),
3728 F(DstMem | ModRM, em_shr),
3729 F(DstMem | ModRM, em_shl),
3730 F(DstMem | ModRM, em_sar),
3731};
3732
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003733static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003734 F(DstMem | SrcImm | NoWrite, em_test),
3735 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003736 F(DstMem | SrcNone | Lock, em_not),
3737 F(DstMem | SrcNone | Lock, em_neg),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003738 I(SrcMem, em_mul_ex),
3739 I(SrcMem, em_imul_ex),
3740 I(SrcMem, em_div_ex),
3741 I(SrcMem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003742};
3743
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003744static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003745 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3746 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003747 N, N, N, N, N, N,
3748};
3749
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003750static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003751 F(DstMem | SrcNone | Lock, em_inc),
3752 F(DstMem | SrcNone | Lock, em_dec),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003753 I(SrcMem | Stack, em_grp45),
3754 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3755 I(SrcMem | Stack, em_grp45),
3756 I(SrcMemFAddr | ImplicitOps, em_grp45),
Gleb Natapov188424b2013-04-11 12:32:14 +03003757 I(SrcMem | Stack, em_grp45), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003758};
3759
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003760static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003761 DI(Prot, sldt),
3762 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003763 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003764 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003765 N, N, N, N,
3766};
3767
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003768static const struct group_dual group7 = { {
Avi Kivity96051572012-06-10 17:21:18 +03003769 II(Mov | DstMem | Priv, em_sgdt, sgdt),
3770 II(Mov | DstMem | Priv, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003771 II(SrcMem | Priv, em_lgdt, lgdt),
3772 II(SrcMem | Priv, em_lidt, lidt),
3773 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3774 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3775 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003776}, {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003777 I(SrcNone | Priv | VendorSpecific, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003778 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003779 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003780 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3781 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3782 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003783} };
3784
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003785static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003786 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003787 F(DstMem | SrcImmByte | NoWrite, em_bt),
3788 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3789 F(DstMem | SrcImmByte | Lock, em_btr),
3790 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003791};
3792
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003793static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003794 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003795}, {
3796 N, N, N, N, N, N, N, N,
3797} };
3798
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003799static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003800 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003801 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003802};
3803
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003804static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003805 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003806};
3807
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003808static const struct gprefix pfx_vmovntpx = {
Avi Kivity3e114eb2012-04-09 18:40:01 +03003809 I(0, em_mov), N, N, N,
3810};
3811
Gleb Natapov045a2822012-12-20 16:57:43 +02003812static const struct escape escape_d9 = { {
3813 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3814}, {
3815 /* 0xC0 - 0xC7 */
3816 N, N, N, N, N, N, N, N,
3817 /* 0xC8 - 0xCF */
3818 N, N, N, N, N, N, N, N,
3819 /* 0xD0 - 0xC7 */
3820 N, N, N, N, N, N, N, N,
3821 /* 0xD8 - 0xDF */
3822 N, N, N, N, N, N, N, N,
3823 /* 0xE0 - 0xE7 */
3824 N, N, N, N, N, N, N, N,
3825 /* 0xE8 - 0xEF */
3826 N, N, N, N, N, N, N, N,
3827 /* 0xF0 - 0xF7 */
3828 N, N, N, N, N, N, N, N,
3829 /* 0xF8 - 0xFF */
3830 N, N, N, N, N, N, N, N,
3831} };
3832
3833static const struct escape escape_db = { {
3834 N, N, N, N, N, N, N, N,
3835}, {
3836 /* 0xC0 - 0xC7 */
3837 N, N, N, N, N, N, N, N,
3838 /* 0xC8 - 0xCF */
3839 N, N, N, N, N, N, N, N,
3840 /* 0xD0 - 0xC7 */
3841 N, N, N, N, N, N, N, N,
3842 /* 0xD8 - 0xDF */
3843 N, N, N, N, N, N, N, N,
3844 /* 0xE0 - 0xE7 */
3845 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3846 /* 0xE8 - 0xEF */
3847 N, N, N, N, N, N, N, N,
3848 /* 0xF0 - 0xF7 */
3849 N, N, N, N, N, N, N, N,
3850 /* 0xF8 - 0xFF */
3851 N, N, N, N, N, N, N, N,
3852} };
3853
3854static const struct escape escape_dd = { {
3855 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3856}, {
3857 /* 0xC0 - 0xC7 */
3858 N, N, N, N, N, N, N, N,
3859 /* 0xC8 - 0xCF */
3860 N, N, N, N, N, N, N, N,
3861 /* 0xD0 - 0xC7 */
3862 N, N, N, N, N, N, N, N,
3863 /* 0xD8 - 0xDF */
3864 N, N, N, N, N, N, N, N,
3865 /* 0xE0 - 0xE7 */
3866 N, N, N, N, N, N, N, N,
3867 /* 0xE8 - 0xEF */
3868 N, N, N, N, N, N, N, N,
3869 /* 0xF0 - 0xF7 */
3870 N, N, N, N, N, N, N, N,
3871 /* 0xF8 - 0xFF */
3872 N, N, N, N, N, N, N, N,
3873} };
3874
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003875static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003876 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003877 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003878 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3879 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003880 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003881 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003882 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3883 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003884 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003885 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003886 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3887 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003888 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003889 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003890 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3891 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003892 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003893 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003894 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003895 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003896 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003897 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003898 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003899 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003900 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02003901 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003902 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003903 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003904 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003905 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003906 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003907 I(ImplicitOps | Stack | No64, em_pusha),
3908 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003909 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3910 N, N, N, N,
3911 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003912 I(SrcImm | Mov | Stack, em_push),
3913 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003914 I(SrcImmByte | Mov | Stack, em_push),
3915 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003916 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003917 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003918 /* 0x70 - 0x7F */
3919 X16(D(SrcImmByte)),
3920 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003921 G(ByteOp | DstMem | SrcImm, group1),
3922 G(DstMem | SrcImm, group1),
3923 G(ByteOp | DstMem | SrcImm | No64, group1),
3924 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003925 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003926 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003927 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003928 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003929 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003930 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003931 D(ModRM | SrcMem | NoAccess | DstReg),
3932 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3933 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003934 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003935 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003936 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003937 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003938 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003939 II(ImplicitOps | Stack, em_pushf, pushf),
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003940 II(ImplicitOps | Stack, em_popf, popf), N, I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003941 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003942 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003943 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003944 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003945 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003946 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003947 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003948 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3949 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003950 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003951 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003952 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003953 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02003954 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003955 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003956 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003957 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003958 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003959 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3960 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003961 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003962 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003963 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
3964 N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003965 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003966 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003967 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003968 G(Src2One | ByteOp, group2), G(Src2One, group2),
3969 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003970 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02003971 I(DstAcc | SrcImmUByte | No64, em_aad),
3972 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02003973 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003974 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02003975 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003976 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003977 X3(I(SrcImmByte, em_loop)),
3978 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003979 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3980 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003981 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003982 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003983 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003984 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3985 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003986 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003987 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003988 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3989 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003990 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003991 D(ImplicitOps), D(ImplicitOps),
3992 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003993 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3994};
3995
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003996static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003997 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003998 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003999 N, I(ImplicitOps | VendorSpecific, em_syscall),
4000 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004001 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004002 N, D(ImplicitOps | ModRM), N, N,
4003 /* 0x10 - 0x1F */
4004 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
4005 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004006 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02004007 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09004008 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
4009 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004010 N, N, N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03004011 N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
4012 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004013 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004014 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004015 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004016 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004017 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004018 I(ImplicitOps | VendorSpecific, em_sysenter),
4019 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004020 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004021 N, N, N, N, N, N, N, N,
4022 /* 0x40 - 0x4F */
4023 X16(D(DstReg | SrcMem | ModRM | Mov)),
4024 /* 0x50 - 0x5F */
4025 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4026 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004027 N, N, N, N,
4028 N, N, N, N,
4029 N, N, N, N,
4030 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004031 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004032 N, N, N, N,
4033 N, N, N, N,
4034 N, N, N, N,
4035 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004036 /* 0x80 - 0x8F */
4037 X16(D(SrcImm)),
4038 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004039 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004040 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004041 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004042 II(ImplicitOps, em_cpuid, cpuid),
4043 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004044 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4045 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004046 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004047 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004048 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004049 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004050 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4051 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Avi Kivity4d758342013-01-19 19:51:55 +02004052 D(ModRM), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004053 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09004054 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004055 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004056 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004057 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4058 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004059 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004060 /* 0xB8 - 0xBF */
4061 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004062 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004063 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
4064 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004065 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004066 /* 0xC0 - 0xC7 */
Avi Kivity739ae402010-08-26 11:56:13 +03004067 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08004068 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004069 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004070 /* 0xC8 - 0xCF */
4071 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004072 /* 0xD0 - 0xDF */
4073 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4074 /* 0xE0 - 0xEF */
4075 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4076 /* 0xF0 - 0xFF */
4077 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4078};
4079
4080#undef D
4081#undef N
4082#undef G
4083#undef GD
4084#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004085#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004086#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004087
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004088#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004089#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004090#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004091#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004092#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004093
Avi Kivity9dac77f2011-06-01 15:34:25 +03004094static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004095{
4096 unsigned size;
4097
Avi Kivity9dac77f2011-06-01 15:34:25 +03004098 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004099 if (size == 8)
4100 size = 4;
4101 return size;
4102}
4103
4104static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4105 unsigned size, bool sign_extension)
4106{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004107 int rc = X86EMUL_CONTINUE;
4108
4109 op->type = OP_IMM;
4110 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004111 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004112 /* NB. Immediates are sign-extended as necessary. */
4113 switch (op->bytes) {
4114 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004115 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004116 break;
4117 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004118 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004119 break;
4120 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004121 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004122 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004123 case 8:
4124 op->val = insn_fetch(s64, ctxt);
4125 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004126 }
4127 if (!sign_extension) {
4128 switch (op->bytes) {
4129 case 1:
4130 op->val &= 0xff;
4131 break;
4132 case 2:
4133 op->val &= 0xffff;
4134 break;
4135 case 4:
4136 op->val &= 0xffffffff;
4137 break;
4138 }
4139 }
4140done:
4141 return rc;
4142}
4143
Avi Kivitya9945542011-09-13 10:45:41 +03004144static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4145 unsigned d)
4146{
4147 int rc = X86EMUL_CONTINUE;
4148
4149 switch (d) {
4150 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004151 decode_register_operand(ctxt, op);
Avi Kivitya9945542011-09-13 10:45:41 +03004152 break;
4153 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004154 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03004155 break;
4156 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004157 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004158 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03004159 *op = ctxt->memop;
4160 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03004161 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya9945542011-09-13 10:45:41 +03004162 fetch_bit_operand(ctxt);
4163 op->orig_val = op->val;
4164 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004165 case OpMem64:
4166 ctxt->memop.bytes = 8;
4167 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03004168 case OpAcc:
4169 op->type = OP_REG;
4170 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004171 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya9945542011-09-13 10:45:41 +03004172 fetch_register_operand(op);
4173 op->orig_val = op->val;
4174 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004175 case OpAccLo:
4176 op->type = OP_REG;
4177 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4178 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4179 fetch_register_operand(op);
4180 op->orig_val = op->val;
4181 break;
4182 case OpAccHi:
4183 if (ctxt->d & ByteOp) {
4184 op->type = OP_NONE;
4185 break;
4186 }
4187 op->type = OP_REG;
4188 op->bytes = ctxt->op_bytes;
4189 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4190 fetch_register_operand(op);
4191 op->orig_val = op->val;
4192 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004193 case OpDI:
4194 op->type = OP_MEM;
4195 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4196 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004197 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya9945542011-09-13 10:45:41 +03004198 op->addr.mem.seg = VCPU_SREG_ES;
4199 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004200 op->count = 1;
Avi Kivitya9945542011-09-13 10:45:41 +03004201 break;
4202 case OpDX:
4203 op->type = OP_REG;
4204 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004205 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya9945542011-09-13 10:45:41 +03004206 fetch_register_operand(op);
4207 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004208 case OpCL:
4209 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004210 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004211 break;
4212 case OpImmByte:
4213 rc = decode_imm(ctxt, op, 1, true);
4214 break;
4215 case OpOne:
4216 op->bytes = 1;
4217 op->val = 1;
4218 break;
4219 case OpImm:
4220 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4221 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004222 case OpImm64:
4223 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4224 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004225 case OpMem8:
4226 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004227 if (ctxt->memop.type == OP_REG) {
4228 ctxt->memop.addr.reg = decode_register(ctxt, ctxt->modrm_rm, 1);
4229 fetch_register_operand(&ctxt->memop);
4230 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004231 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004232 case OpMem16:
4233 ctxt->memop.bytes = 2;
4234 goto mem_common;
4235 case OpMem32:
4236 ctxt->memop.bytes = 4;
4237 goto mem_common;
4238 case OpImmU16:
4239 rc = decode_imm(ctxt, op, 2, false);
4240 break;
4241 case OpImmU:
4242 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4243 break;
4244 case OpSI:
4245 op->type = OP_MEM;
4246 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4247 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004248 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Avi Kivity0fe59122011-09-13 10:45:47 +03004249 op->addr.mem.seg = seg_override(ctxt);
4250 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004251 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004252 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004253 case OpXLat:
4254 op->type = OP_MEM;
4255 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4256 op->addr.mem.ea =
4257 register_address(ctxt,
4258 reg_read(ctxt, VCPU_REGS_RBX) +
4259 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
4260 op->addr.mem.seg = seg_override(ctxt);
4261 op->val = 0;
4262 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004263 case OpImmFAddr:
4264 op->type = OP_IMM;
4265 op->addr.mem.ea = ctxt->_eip;
4266 op->bytes = ctxt->op_bytes + 2;
4267 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4268 break;
4269 case OpMemFAddr:
4270 ctxt->memop.bytes = ctxt->op_bytes + 2;
4271 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004272 case OpES:
4273 op->val = VCPU_SREG_ES;
4274 break;
4275 case OpCS:
4276 op->val = VCPU_SREG_CS;
4277 break;
4278 case OpSS:
4279 op->val = VCPU_SREG_SS;
4280 break;
4281 case OpDS:
4282 op->val = VCPU_SREG_DS;
4283 break;
4284 case OpFS:
4285 op->val = VCPU_SREG_FS;
4286 break;
4287 case OpGS:
4288 op->val = VCPU_SREG_GS;
4289 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004290 case OpImplicit:
4291 /* Special instructions do their own operand decoding. */
4292 default:
4293 op->type = OP_NONE; /* Disable writeback. */
4294 break;
4295 }
4296
4297done:
4298 return rc;
4299}
4300
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004301int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004302{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004303 int rc = X86EMUL_CONTINUE;
4304 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004305 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004306 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03004307 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004308
Avi Kivityf09ed832011-09-13 10:45:40 +03004309 ctxt->memop.type = OP_NONE;
4310 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004311 ctxt->_eip = ctxt->eip;
4312 ctxt->fetch.start = ctxt->_eip;
4313 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01004314 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004315 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004316
4317 switch (mode) {
4318 case X86EMUL_MODE_REAL:
4319 case X86EMUL_MODE_VM86:
4320 case X86EMUL_MODE_PROT16:
4321 def_op_bytes = def_ad_bytes = 2;
4322 break;
4323 case X86EMUL_MODE_PROT32:
4324 def_op_bytes = def_ad_bytes = 4;
4325 break;
4326#ifdef CONFIG_X86_64
4327 case X86EMUL_MODE_PROT64:
4328 def_op_bytes = 4;
4329 def_ad_bytes = 8;
4330 break;
4331#endif
4332 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004333 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004334 }
4335
Avi Kivity9dac77f2011-06-01 15:34:25 +03004336 ctxt->op_bytes = def_op_bytes;
4337 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004338
4339 /* Legacy prefixes. */
4340 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004341 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004342 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004343 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004344 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004345 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004346 break;
4347 case 0x67: /* address-size override */
4348 if (mode == X86EMUL_MODE_PROT64)
4349 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004350 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004351 else
4352 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004353 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004354 break;
4355 case 0x26: /* ES override */
4356 case 0x2e: /* CS override */
4357 case 0x36: /* SS override */
4358 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004359 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004360 break;
4361 case 0x64: /* FS override */
4362 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004363 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004364 break;
4365 case 0x40 ... 0x4f: /* REX */
4366 if (mode != X86EMUL_MODE_PROT64)
4367 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004368 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004369 continue;
4370 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004371 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004372 break;
4373 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004374 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004375 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004376 break;
4377 default:
4378 goto done_prefixes;
4379 }
4380
4381 /* Any legacy prefix after a REX prefix nullifies its effect. */
4382
Avi Kivity9dac77f2011-06-01 15:34:25 +03004383 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004384 }
4385
4386done_prefixes:
4387
4388 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004389 if (ctxt->rex_prefix & 8)
4390 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004391
4392 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004393 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004394 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004395 if (ctxt->b == 0x0f) {
4396 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004397 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004398 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004399 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004400 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004401
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004402 if (ctxt->d & ModRM)
4403 ctxt->modrm = insn_fetch(u8, ctxt);
4404
Avi Kivity9dac77f2011-06-01 15:34:25 +03004405 while (ctxt->d & GroupMask) {
4406 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004407 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004408 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004409 opcode = opcode.u.group[goffset];
4410 break;
4411 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004412 goffset = (ctxt->modrm >> 3) & 7;
4413 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004414 opcode = opcode.u.gdual->mod3[goffset];
4415 else
4416 opcode = opcode.u.gdual->mod012[goffset];
4417 break;
4418 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004419 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004420 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004421 break;
4422 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004423 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004424 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004425 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004426 switch (simd_prefix) {
4427 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4428 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4429 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4430 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4431 }
4432 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004433 case Escape:
4434 if (ctxt->modrm > 0xbf)
4435 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4436 else
4437 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4438 break;
Avi Kivity46561642011-04-24 14:09:59 +03004439 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004440 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004441 }
Avi Kivity46561642011-04-24 14:09:59 +03004442
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004443 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004444 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004445 }
4446
Avi Kivity9dac77f2011-06-01 15:34:25 +03004447 ctxt->execute = opcode.u.execute;
4448 ctxt->check_perm = opcode.check_perm;
4449 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004450
4451 /* Unrecognised? */
Gleb Natapov1146a782013-04-11 12:30:01 +03004452 if (ctxt->d == 0 || (ctxt->d & NotImpl))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004453 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004454
Avi Kivity9dac77f2011-06-01 15:34:25 +03004455 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004456 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004457
Avi Kivity9dac77f2011-06-01 15:34:25 +03004458 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
4459 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004460
Avi Kivity9dac77f2011-06-01 15:34:25 +03004461 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004462 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004463 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004464 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03004465 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004466 }
4467
Avi Kivity9dac77f2011-06-01 15:34:25 +03004468 if (ctxt->d & Sse)
4469 ctxt->op_bytes = 16;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004470 else if (ctxt->d & Mmx)
4471 ctxt->op_bytes = 8;
Avi Kivity12537912011-03-29 11:41:27 +02004472
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004473 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004474 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004475 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004476 if (!ctxt->has_seg_override)
4477 set_seg_override(ctxt, ctxt->modrm_seg);
4478 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004479 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004480 if (rc != X86EMUL_CONTINUE)
4481 goto done;
4482
Avi Kivity9dac77f2011-06-01 15:34:25 +03004483 if (!ctxt->has_seg_override)
4484 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004485
Avi Kivityf09ed832011-09-13 10:45:40 +03004486 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004487
Avi Kivityf09ed832011-09-13 10:45:40 +03004488 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
4489 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004490
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004491 /*
4492 * Decode and fetch the source operand: register, memory
4493 * or immediate.
4494 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004495 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004496 if (rc != X86EMUL_CONTINUE)
4497 goto done;
4498
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004499 /*
4500 * Decode and fetch the second source operand: register, memory
4501 * or immediate.
4502 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004503 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004504 if (rc != X86EMUL_CONTINUE)
4505 goto done;
4506
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004507 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03004508 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004509
4510done:
Avi Kivityf09ed832011-09-13 10:45:40 +03004511 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
4512 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004513
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004514 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004515}
4516
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004517bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4518{
4519 return ctxt->d & PageTable;
4520}
4521
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004522static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4523{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004524 /* The second termination condition only applies for REPE
4525 * and REPNE. Test if the repeat string operation prefix is
4526 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4527 * corresponding termination condition according to:
4528 * - if REPE/REPZ and ZF = 0 then done
4529 * - if REPNE/REPNZ and ZF = 1 then done
4530 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004531 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4532 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4533 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004534 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004535 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004536 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4537 return true;
4538
4539 return false;
4540}
4541
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004542static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4543{
4544 bool fault = false;
4545
4546 ctxt->ops->get_fpu(ctxt);
4547 asm volatile("1: fwait \n\t"
4548 "2: \n\t"
4549 ".pushsection .fixup,\"ax\" \n\t"
4550 "3: \n\t"
4551 "movb $1, %[fault] \n\t"
4552 "jmp 2b \n\t"
4553 ".popsection \n\t"
4554 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004555 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004556 ctxt->ops->put_fpu(ctxt);
4557
4558 if (unlikely(fault))
4559 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4560
4561 return X86EMUL_CONTINUE;
4562}
4563
4564static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4565 struct operand *op)
4566{
4567 if (op->type == OP_MM)
4568 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4569}
4570
Avi Kivitye28bbd42013-01-04 16:18:48 +02004571static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4572{
4573 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
4574 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
4575 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
4576 : "+a"(ctxt->dst.val), "+b"(ctxt->src.val), [flags]"+D"(flags)
4577 : "c"(ctxt->src2.val), [fastop]"S"(fop));
4578 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
4579 return X86EMUL_CONTINUE;
4580}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004581
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004582int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004583{
Mathias Krause0225fb52012-08-30 01:30:16 +02004584 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004585 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004586 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004587
Avi Kivity9dac77f2011-06-01 15:34:25 +03004588 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004589
Gleb Natapov1146a782013-04-11 12:30:01 +03004590 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4591 (ctxt->d & Undefined)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004592 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02004593 goto done;
4594 }
4595
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004596 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004597 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004598 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004599 goto done;
4600 }
4601
Avi Kivity9dac77f2011-06-01 15:34:25 +03004602 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004603 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004604 goto done;
4605 }
4606
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004607 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4608 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02004609 rc = emulate_ud(ctxt);
4610 goto done;
4611 }
4612
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004613 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02004614 rc = emulate_nm(ctxt);
4615 goto done;
4616 }
4617
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004618 if (ctxt->d & Mmx) {
4619 rc = flush_pending_x87_faults(ctxt);
4620 if (rc != X86EMUL_CONTINUE)
4621 goto done;
4622 /*
4623 * Now that we know the fpu is exception safe, we can fetch
4624 * operands from it.
4625 */
4626 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4627 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4628 if (!(ctxt->d & Mov))
4629 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4630 }
4631
Avi Kivity9dac77f2011-06-01 15:34:25 +03004632 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4633 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004634 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004635 if (rc != X86EMUL_CONTINUE)
4636 goto done;
4637 }
4638
Gleb Natapove92805a2010-02-10 14:21:35 +02004639 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004640 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004641 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02004642 goto done;
4643 }
4644
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004645 /* Instruction can only be executed in protected mode */
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03004646 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004647 rc = emulate_ud(ctxt);
4648 goto done;
4649 }
4650
Joerg Roedeld09beab2011-04-04 12:39:25 +02004651 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004652 if (ctxt->check_perm) {
4653 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004654 if (rc != X86EMUL_CONTINUE)
4655 goto done;
4656 }
4657
Avi Kivity9dac77f2011-06-01 15:34:25 +03004658 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4659 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004660 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004661 if (rc != X86EMUL_CONTINUE)
4662 goto done;
4663 }
4664
Avi Kivity9dac77f2011-06-01 15:34:25 +03004665 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004666 /* All REP prefixes have the same first termination condition */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004667 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004668 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004669 goto done;
4670 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004671 }
4672
Avi Kivity9dac77f2011-06-01 15:34:25 +03004673 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4674 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4675 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004676 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004677 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004678 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004679 }
4680
Avi Kivity9dac77f2011-06-01 15:34:25 +03004681 if (ctxt->src2.type == OP_MEM) {
4682 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4683 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004684 if (rc != X86EMUL_CONTINUE)
4685 goto done;
4686 }
4687
Avi Kivity9dac77f2011-06-01 15:34:25 +03004688 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004689 goto special_insn;
4690
4691
Avi Kivity9dac77f2011-06-01 15:34:25 +03004692 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004693 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004694 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4695 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004696 if (rc != X86EMUL_CONTINUE)
4697 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004698 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004699 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004700
Avi Kivity018a98d2007-11-27 19:30:56 +02004701special_insn:
4702
Avi Kivity9dac77f2011-06-01 15:34:25 +03004703 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4704 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004705 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004706 if (rc != X86EMUL_CONTINUE)
4707 goto done;
4708 }
4709
Avi Kivity9dac77f2011-06-01 15:34:25 +03004710 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004711 if (ctxt->d & Fastop) {
4712 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4713 rc = fastop(ctxt, fop);
4714 if (rc != X86EMUL_CONTINUE)
4715 goto done;
4716 goto writeback;
4717 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004718 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004719 if (rc != X86EMUL_CONTINUE)
4720 goto done;
4721 goto writeback;
4722 }
4723
Avi Kivity9dac77f2011-06-01 15:34:25 +03004724 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004725 goto twobyte_insn;
4726
Avi Kivity9dac77f2011-06-01 15:34:25 +03004727 switch (ctxt->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004728 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004729 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004730 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004731 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004732 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004733 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004734 if (test_cc(ctxt->b, ctxt->eflags))
4735 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004736 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004737 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004738 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004739 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004740 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004741 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Mohammed Gamal34698d82010-08-04 14:41:04 +03004742 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004743 rc = em_xchg(ctxt);
4744 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004745 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004746 switch (ctxt->op_bytes) {
4747 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4748 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4749 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004750 }
4751 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004752 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004753 rc = emulate_int(ctxt, 3);
4754 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004755 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004756 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004757 break;
4758 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004759 if (ctxt->eflags & EFLG_OF)
4760 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004761 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004762 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004763 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004764 jmp_rel(ctxt, ctxt->src.val);
4765 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004766 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004767 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004768 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004769 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004770 case 0xf5: /* cmc */
4771 /* complement carry flag from eflags reg */
4772 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004773 break;
4774 case 0xf8: /* clc */
4775 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004776 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004777 case 0xf9: /* stc */
4778 ctxt->eflags |= EFLG_CF;
4779 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004780 case 0xfc: /* cld */
4781 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004782 break;
4783 case 0xfd: /* std */
4784 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004785 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004786 default:
4787 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004788 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004789
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004790 if (rc != X86EMUL_CONTINUE)
4791 goto done;
4792
Avi Kivity018a98d2007-11-27 19:30:56 +02004793writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02004794 if (!(ctxt->d & NoWrite)) {
4795 rc = writeback(ctxt, &ctxt->dst);
4796 if (rc != X86EMUL_CONTINUE)
4797 goto done;
4798 }
4799 if (ctxt->d & SrcWrite) {
4800 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
4801 rc = writeback(ctxt, &ctxt->src);
4802 if (rc != X86EMUL_CONTINUE)
4803 goto done;
4804 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004805
Gleb Natapov5cd21912010-03-18 15:20:26 +02004806 /*
4807 * restore dst type in case the decoding will be reused
4808 * (happens for string instruction )
4809 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004810 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004811
Avi Kivity9dac77f2011-06-01 15:34:25 +03004812 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004813 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004814
Avi Kivity9dac77f2011-06-01 15:34:25 +03004815 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004816 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004817
Avi Kivity9dac77f2011-06-01 15:34:25 +03004818 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004819 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004820 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004821 if ((ctxt->d & SrcMask) == SrcSI)
4822 count = ctxt->src.count;
4823 else
4824 count = ctxt->dst.count;
4825 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4826 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004827
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004828 if (!string_insn_completed(ctxt)) {
4829 /*
4830 * Re-enter guest when pio read ahead buffer is empty
4831 * or, if it is not used, after each 1024 iteration.
4832 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004833 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004834 (r->end == 0 || r->end != r->pos)) {
4835 /*
4836 * Reset read cache. Usually happens before
4837 * decode, but since instruction is restarted
4838 * we have to do it here.
4839 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004840 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004841 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004842 return EMULATION_RESTART;
4843 }
4844 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004845 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004846 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004847
Avi Kivity9dac77f2011-06-01 15:34:25 +03004848 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004849
4850done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004851 if (rc == X86EMUL_PROPAGATE_FAULT)
4852 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004853 if (rc == X86EMUL_INTERCEPTED)
4854 return EMULATION_INTERCEPTED;
4855
Avi Kivitydd856ef2012-08-27 23:46:17 +03004856 if (rc == X86EMUL_CONTINUE)
4857 writeback_registers(ctxt);
4858
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004859 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004860
4861twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004862 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004863 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004864 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004865 break;
4866 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004867 case 0x0d: /* GrpP (prefetch) */
4868 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004869 break;
4870 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004871 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004872 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004873 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004874 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004875 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004876 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004877 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4878 if (!test_cc(ctxt->b, ctxt->eflags))
4879 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004880 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004881 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004882 if (test_cc(ctxt->b, ctxt->eflags))
4883 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004884 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004885 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004886 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004887 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004888 case 0xae: /* clflush */
4889 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004890 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004891 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004892 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004893 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004894 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004895 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004896 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004897 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004898 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004899 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004900 case 0xc0 ... 0xc1: /* xadd */
Avi Kivity158de572013-01-19 19:51:57 +02004901 fastop(ctxt, em_add);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004902 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004903 ctxt->src.val = ctxt->dst.orig_val;
4904 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004905 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004906 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004907 ctxt->dst.bytes = ctxt->op_bytes;
4908 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4909 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004910 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004911 default:
4912 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004913 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004914
4915 if (rc != X86EMUL_CONTINUE)
4916 goto done;
4917
Avi Kivity6aa8b732006-12-10 02:21:36 -08004918 goto writeback;
4919
4920cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004921 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004922}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004923
4924void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
4925{
4926 invalidate_registers(ctxt);
4927}
4928
4929void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
4930{
4931 writeback_registers(ctxt);
4932}