blob: a06a550c2db8764f50eecfe346fe6783eabd341e [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 Kivitya9945542011-09-13 10:45:41 +030064
Avi Kivity0fe59122011-09-13 10:45:47 +030065#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030066#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya9945542011-09-13 10:45:41 +030067
68/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080069 * Opcode effective-address decode tables.
70 * Note that we only emulate instructions that have at least one memory
71 * operand (excluding implicit stack references). We assume that stack
72 * references and instruction fetches will never occur in special memory
73 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
74 * not be handled.
75 */
76
77/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030078#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080079/* Destination operand type. */
Avi Kivitya9945542011-09-13 10:45:41 +030080#define DstShift 1
81#define ImplicitOps (OpImplicit << DstShift)
82#define DstReg (OpReg << DstShift)
83#define DstMem (OpMem << DstShift)
84#define DstAcc (OpAcc << DstShift)
85#define DstDI (OpDI << DstShift)
86#define DstMem64 (OpMem64 << DstShift)
87#define DstImmUByte (OpImmUByte << DstShift)
88#define DstDX (OpDX << DstShift)
89#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080090/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030091#define SrcShift 6
92#define SrcNone (OpNone << SrcShift)
93#define SrcReg (OpReg << SrcShift)
94#define SrcMem (OpMem << SrcShift)
95#define SrcMem16 (OpMem16 << SrcShift)
96#define SrcMem32 (OpMem32 << SrcShift)
97#define SrcImm (OpImm << SrcShift)
98#define SrcImmByte (OpImmByte << SrcShift)
99#define SrcOne (OpOne << SrcShift)
100#define SrcImmUByte (OpImmUByte << SrcShift)
101#define SrcImmU (OpImmU << SrcShift)
102#define SrcSI (OpSI << SrcShift)
Paolo Bonzini7fa57952013-05-09 11:32:50 +0200103#define SrcXLat (OpXLat << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300104#define SrcImmFAddr (OpImmFAddr << SrcShift)
105#define SrcMemFAddr (OpMemFAddr << SrcShift)
106#define SrcAcc (OpAcc << SrcShift)
107#define SrcImmU16 (OpImmU16 << SrcShift)
Nadav Amit5e2c6882012-12-06 21:55:10 -0200108#define SrcImm64 (OpImm64 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300109#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200110#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300111#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300112#define BitOp (1<<11)
113#define MemAbs (1<<12) /* Memory operand is absolute displacement */
114#define String (1<<13) /* String instruction (rep capable) */
115#define Stack (1<<14) /* Stack instruction (push/pop) */
116#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
117#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
118#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
119#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
120#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
Gleb Natapov045a2822012-12-20 16:57:43 +0200121#define Escape (5<<15) /* Escape to coprocessor instruction */
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300122#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300123/* Generic ModRM decode. */
124#define ModRM (1<<19)
125/* Destination is only written; never read. */
126#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300127/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200128#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Avi Kivityd8671622011-02-01 16:32:03 +0200129#define VendorSpecific (1<<22) /* Vendor specific instruction */
Avi Kivity5a506b12010-08-01 15:10:29 +0300130#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300131#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300132#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200133#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200134#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300135#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800136#define PageTable (1 << 29) /* instruction used to write page table */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300137#define NotImpl (1 << 30) /* instruction is not implemented */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100138/* Source 2 operand type */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300139#define Src2Shift (31)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300140#define Src2None (OpNone << Src2Shift)
141#define Src2CL (OpCL << Src2Shift)
142#define Src2ImmByte (OpImmByte << Src2Shift)
143#define Src2One (OpOne << Src2Shift)
144#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300145#define Src2ES (OpES << Src2Shift)
146#define Src2CS (OpCS << Src2Shift)
147#define Src2SS (OpSS << Src2Shift)
148#define Src2DS (OpDS << Src2Shift)
149#define Src2FS (OpFS << Src2Shift)
150#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300151#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300152#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Avi Kivity1c11b372012-04-09 18:39:59 +0300153#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
154#define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */
155#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
Avi Kivitye28bbd42013-01-04 16:18:48 +0200156#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
Avi Kivityb6744dc2013-01-04 16:18:50 +0200157#define NoWrite ((u64)1 << 45) /* No writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800158
Avi Kivityd0e53322010-07-29 15:11:54 +0300159#define X2(x...) x, x
160#define X3(x...) X2(x), x
161#define X4(x...) X2(x), X2(x)
162#define X5(x...) X4(x), x
163#define X6(x...) X4(x), X2(x)
164#define X7(x...) X4(x), X3(x)
165#define X8(x...) X4(x), X4(x)
166#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300167
Avi Kivitye28bbd42013-01-04 16:18:48 +0200168#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
169#define FASTOP_SIZE 8
170
171/*
172 * fastop functions have a special calling convention:
173 *
174 * dst: [rdx]:rax (in/out)
175 * src: rbx (in/out)
176 * src2: rcx (in)
177 * flags: rflags (in/out)
178 *
179 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
180 * different operand sizes can be reached by calculation, rather than a jump
181 * table (which would be bigger than the code).
182 *
183 * fastop functions are declared as taking a never-defined fastop parameter,
184 * so they can't be called from C directly.
185 */
186
187struct fastop;
188
Avi Kivityd65b1de2010-07-29 15:11:35 +0300189struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300190 u64 flags : 56;
191 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300192 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300193 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200194 const struct opcode *group;
195 const struct group_dual *gdual;
196 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200197 const struct escape *esc;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200198 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300199 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200200 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300201};
202
203struct group_dual {
204 struct opcode mod012[8];
205 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300206};
207
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200208struct gprefix {
209 struct opcode pfx_no;
210 struct opcode pfx_66;
211 struct opcode pfx_f2;
212 struct opcode pfx_f3;
213};
214
Gleb Natapov045a2822012-12-20 16:57:43 +0200215struct escape {
216 struct opcode op[8];
217 struct opcode high[64];
218};
219
Avi Kivity6aa8b732006-12-10 02:21:36 -0800220/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200221#define EFLG_ID (1<<21)
222#define EFLG_VIP (1<<20)
223#define EFLG_VIF (1<<19)
224#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200225#define EFLG_VM (1<<17)
226#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200227#define EFLG_IOPL (3<<12)
228#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800229#define EFLG_OF (1<<11)
230#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200231#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200232#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800233#define EFLG_SF (1<<7)
234#define EFLG_ZF (1<<6)
235#define EFLG_AF (1<<4)
236#define EFLG_PF (1<<2)
237#define EFLG_CF (1<<0)
238
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300239#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
240#define EFLG_RESERVED_ONE_MASK 2
241
Avi Kivitydd856ef2012-08-27 23:46:17 +0300242static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
243{
244 if (!(ctxt->regs_valid & (1 << nr))) {
245 ctxt->regs_valid |= 1 << nr;
246 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
247 }
248 return ctxt->_regs[nr];
249}
250
251static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
252{
253 ctxt->regs_valid |= 1 << nr;
254 ctxt->regs_dirty |= 1 << nr;
255 return &ctxt->_regs[nr];
256}
257
258static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
259{
260 reg_read(ctxt, nr);
261 return reg_write(ctxt, nr);
262}
263
264static void writeback_registers(struct x86_emulate_ctxt *ctxt)
265{
266 unsigned reg;
267
268 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
269 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
270}
271
272static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
273{
274 ctxt->regs_dirty = 0;
275 ctxt->regs_valid = 0;
276}
277
Avi Kivity6aa8b732006-12-10 02:21:36 -0800278/*
279 * Instruction emulation:
280 * Most instructions are emulated directly via a fragment of inline assembly
281 * code. This allows us to save/restore EFLAGS and thus very easily pick up
282 * any modified flags.
283 */
284
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800285#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800286#define _LO32 "k" /* force 32-bit operand */
287#define _STK "%%rsp" /* stack pointer */
288#elif defined(__i386__)
289#define _LO32 "" /* force 32-bit operand */
290#define _STK "%%esp" /* stack pointer */
291#endif
292
293/*
294 * These EFLAGS bits are restored from saved value during emulation, and
295 * any changes are written back to the saved value after emulation.
296 */
297#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
298
299/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200300#define _PRE_EFLAGS(_sav, _msk, _tmp) \
301 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
302 "movl %"_sav",%"_LO32 _tmp"; " \
303 "push %"_tmp"; " \
304 "push %"_tmp"; " \
305 "movl %"_msk",%"_LO32 _tmp"; " \
306 "andl %"_LO32 _tmp",("_STK"); " \
307 "pushf; " \
308 "notl %"_LO32 _tmp"; " \
309 "andl %"_LO32 _tmp",("_STK"); " \
310 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
311 "pop %"_tmp"; " \
312 "orl %"_LO32 _tmp",("_STK"); " \
313 "popf; " \
314 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800315
316/* After executing instruction: write-back necessary bits in EFLAGS. */
317#define _POST_EFLAGS(_sav, _msk, _tmp) \
318 /* _sav |= EFLAGS & _msk; */ \
319 "pushf; " \
320 "pop %"_tmp"; " \
321 "andl %"_msk",%"_LO32 _tmp"; " \
322 "orl %"_LO32 _tmp",%"_sav"; "
323
Avi Kivitydda96d82008-11-26 15:14:10 +0200324#ifdef CONFIG_X86_64
325#define ON64(x) x
326#else
327#define ON64(x)
328#endif
329
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300330#define ____emulate_2op(ctxt, _op, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200331 do { \
332 __asm__ __volatile__ ( \
333 _PRE_EFLAGS("0", "4", "2") \
334 _op _suffix " %"_x"3,%1; " \
335 _POST_EFLAGS("0", "4", "2") \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300336 : "=m" ((ctxt)->eflags), \
337 "+q" (*(_dsttype*)&(ctxt)->dst.val), \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200338 "=&r" (_tmp) \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300339 : _y ((ctxt)->src.val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200340 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200341
342
Avi Kivity6aa8b732006-12-10 02:21:36 -0800343/* Raw emulation: instruction has two explicit operands. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300344#define __emulate_2op_nobyte(ctxt,_op,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200345 do { \
346 unsigned long _tmp; \
347 \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300348 switch ((ctxt)->dst.bytes) { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200349 case 2: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300350 ____emulate_2op(ctxt,_op,_wx,_wy,"w",u16); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200351 break; \
352 case 4: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300353 ____emulate_2op(ctxt,_op,_lx,_ly,"l",u32); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200354 break; \
355 case 8: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300356 ON64(____emulate_2op(ctxt,_op,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200357 break; \
358 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800359 } while (0)
360
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300361#define __emulate_2op(ctxt,_op,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800362 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200363 unsigned long _tmp; \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300364 switch ((ctxt)->dst.bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800365 case 1: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300366 ____emulate_2op(ctxt,_op,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800367 break; \
368 default: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300369 __emulate_2op_nobyte(ctxt, _op, \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800370 _wx, _wy, _lx, _ly, _qx, _qy); \
371 break; \
372 } \
373 } while (0)
374
375/* Source operand is byte-sized and may be restricted to just %cl. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300376#define emulate_2op_SrcB(ctxt, _op) \
377 __emulate_2op(ctxt, _op, "b", "c", "b", "c", "b", "c", "b", "c")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800378
379/* Source operand is byte, word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300380#define emulate_2op_SrcV(ctxt, _op) \
381 __emulate_2op(ctxt, _op, "b", "q", "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800382
383/* Source operand is word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300384#define emulate_2op_SrcV_nobyte(ctxt, _op) \
385 __emulate_2op_nobyte(ctxt, _op, "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800386
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100387/* Instruction has three operands and one operand is stored in ECX register */
Avi Kivity29053a62011-09-07 16:41:37 +0300388#define __emulate_2op_cl(ctxt, _op, _suffix, _type) \
Avi Kivity72952612011-04-20 13:12:27 +0300389 do { \
390 unsigned long _tmp; \
Avi Kivity761441b2011-09-07 16:41:36 +0300391 _type _clv = (ctxt)->src2.val; \
392 _type _srcv = (ctxt)->src.val; \
393 _type _dstv = (ctxt)->dst.val; \
Avi Kivity72952612011-04-20 13:12:27 +0300394 \
395 __asm__ __volatile__ ( \
396 _PRE_EFLAGS("0", "5", "2") \
397 _op _suffix " %4,%1 \n" \
398 _POST_EFLAGS("0", "5", "2") \
Avi Kivity761441b2011-09-07 16:41:36 +0300399 : "=m" ((ctxt)->eflags), "+r" (_dstv), "=&r" (_tmp) \
Avi Kivity72952612011-04-20 13:12:27 +0300400 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
401 ); \
402 \
Avi Kivity761441b2011-09-07 16:41:36 +0300403 (ctxt)->src2.val = (unsigned long) _clv; \
404 (ctxt)->src2.val = (unsigned long) _srcv; \
405 (ctxt)->dst.val = (unsigned long) _dstv; \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100406 } while (0)
407
Avi Kivity761441b2011-09-07 16:41:36 +0300408#define emulate_2op_cl(ctxt, _op) \
Avi Kivity72952612011-04-20 13:12:27 +0300409 do { \
Avi Kivity761441b2011-09-07 16:41:36 +0300410 switch ((ctxt)->dst.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300411 case 2: \
Avi Kivity29053a62011-09-07 16:41:37 +0300412 __emulate_2op_cl(ctxt, _op, "w", u16); \
Avi Kivity72952612011-04-20 13:12:27 +0300413 break; \
414 case 4: \
Avi Kivity29053a62011-09-07 16:41:37 +0300415 __emulate_2op_cl(ctxt, _op, "l", u32); \
Avi Kivity72952612011-04-20 13:12:27 +0300416 break; \
417 case 8: \
Avi Kivity29053a62011-09-07 16:41:37 +0300418 ON64(__emulate_2op_cl(ctxt, _op, "q", ulong)); \
Avi Kivity72952612011-04-20 13:12:27 +0300419 break; \
420 } \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100421 } while (0)
422
Avi Kivityd1eef452011-09-07 16:41:38 +0300423#define __emulate_1op(ctxt, _op, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424 do { \
425 unsigned long _tmp; \
426 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200427 __asm__ __volatile__ ( \
428 _PRE_EFLAGS("0", "3", "2") \
429 _op _suffix " %1; " \
430 _POST_EFLAGS("0", "3", "2") \
Avi Kivityd1eef452011-09-07 16:41:38 +0300431 : "=m" ((ctxt)->eflags), "+m" ((ctxt)->dst.val), \
Avi Kivitydda96d82008-11-26 15:14:10 +0200432 "=&r" (_tmp) \
433 : "i" (EFLAGS_MASK)); \
434 } while (0)
435
436/* Instruction has only one explicit operand (no source operand). */
Avi Kivityd1eef452011-09-07 16:41:38 +0300437#define emulate_1op(ctxt, _op) \
Avi Kivitydda96d82008-11-26 15:14:10 +0200438 do { \
Avi Kivityd1eef452011-09-07 16:41:38 +0300439 switch ((ctxt)->dst.bytes) { \
440 case 1: __emulate_1op(ctxt, _op, "b"); break; \
441 case 2: __emulate_1op(ctxt, _op, "w"); break; \
442 case 4: __emulate_1op(ctxt, _op, "l"); break; \
443 case 8: ON64(__emulate_1op(ctxt, _op, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800444 } \
445 } while (0)
446
Avi Kivity4d758342013-01-19 19:51:55 +0200447static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *));
448
Avi Kivityb7d491e2013-01-04 16:18:49 +0200449#define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t"
450#define FOP_RET "ret \n\t"
451
452#define FOP_START(op) \
453 extern void em_##op(struct fastop *fake); \
454 asm(".pushsection .text, \"ax\" \n\t" \
455 ".global em_" #op " \n\t" \
456 FOP_ALIGN \
457 "em_" #op ": \n\t"
458
459#define FOP_END \
460 ".popsection")
461
Avi Kivity0bdea062013-01-19 19:51:50 +0200462#define FOPNOP() FOP_ALIGN FOP_RET
463
Avi Kivityb7d491e2013-01-04 16:18:49 +0200464#define FOP1E(op, dst) \
465 FOP_ALIGN #op " %" #dst " \n\t" FOP_RET
466
467#define FASTOP1(op) \
468 FOP_START(op) \
469 FOP1E(op##b, al) \
470 FOP1E(op##w, ax) \
471 FOP1E(op##l, eax) \
472 ON64(FOP1E(op##q, rax)) \
473 FOP_END
474
Avi Kivityf7857f32013-01-04 16:18:53 +0200475#define FOP2E(op, dst, src) \
476 FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET
477
478#define FASTOP2(op) \
479 FOP_START(op) \
480 FOP2E(op##b, al, bl) \
481 FOP2E(op##w, ax, bx) \
482 FOP2E(op##l, eax, ebx) \
483 ON64(FOP2E(op##q, rax, rbx)) \
484 FOP_END
485
Avi Kivity11c363b2013-01-19 19:51:54 +0200486/* 2 operand, word only */
487#define FASTOP2W(op) \
488 FOP_START(op) \
489 FOPNOP() \
490 FOP2E(op##w, ax, bx) \
491 FOP2E(op##l, eax, ebx) \
492 ON64(FOP2E(op##q, rax, rbx)) \
493 FOP_END
494
Avi Kivity007a3b52013-01-19 19:51:51 +0200495/* 2 operand, src is CL */
496#define FASTOP2CL(op) \
497 FOP_START(op) \
498 FOP2E(op##b, al, cl) \
499 FOP2E(op##w, ax, cl) \
500 FOP2E(op##l, eax, cl) \
501 ON64(FOP2E(op##q, rax, cl)) \
502 FOP_END
503
Avi Kivity0bdea062013-01-19 19:51:50 +0200504#define FOP3E(op, dst, src, src2) \
505 FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
506
507/* 3-operand, word-only, src2=cl */
508#define FASTOP3WCL(op) \
509 FOP_START(op) \
510 FOPNOP() \
511 FOP3E(op##w, ax, bx, cl) \
512 FOP3E(op##l, eax, ebx, cl) \
513 ON64(FOP3E(op##q, rax, rbx, cl)) \
514 FOP_END
515
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200516/* Special case for SETcc - 1 instruction per cc */
517#define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t"
518
519FOP_START(setcc)
520FOP_SETCC(seto)
521FOP_SETCC(setno)
522FOP_SETCC(setc)
523FOP_SETCC(setnc)
524FOP_SETCC(setz)
525FOP_SETCC(setnz)
526FOP_SETCC(setbe)
527FOP_SETCC(setnbe)
528FOP_SETCC(sets)
529FOP_SETCC(setns)
530FOP_SETCC(setp)
531FOP_SETCC(setnp)
532FOP_SETCC(setl)
533FOP_SETCC(setnl)
534FOP_SETCC(setle)
535FOP_SETCC(setnle)
536FOP_END;
537
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300538#define __emulate_1op_rax_rdx(ctxt, _op, _suffix, _ex) \
Avi Kivityf6b35972010-08-26 11:59:00 +0300539 do { \
540 unsigned long _tmp; \
Avi Kivitydd856ef2012-08-27 23:46:17 +0300541 ulong *rax = reg_rmw((ctxt), VCPU_REGS_RAX); \
542 ulong *rdx = reg_rmw((ctxt), VCPU_REGS_RDX); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300543 \
544 __asm__ __volatile__ ( \
545 _PRE_EFLAGS("0", "5", "1") \
546 "1: \n\t" \
547 _op _suffix " %6; " \
548 "2: \n\t" \
549 _POST_EFLAGS("0", "5", "1") \
550 ".pushsection .fixup,\"ax\" \n\t" \
551 "3: movb $1, %4 \n\t" \
552 "jmp 2b \n\t" \
553 ".popsection \n\t" \
554 _ASM_EXTABLE(1b, 3b) \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300555 : "=m" ((ctxt)->eflags), "=&r" (_tmp), \
556 "+a" (*rax), "+d" (*rdx), "+qm"(_ex) \
H. Peter Anvincb7cb282012-11-21 14:41:21 -0800557 : "i" (EFLAGS_MASK), "m" ((ctxt)->src.val)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300558 } while (0)
559
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300560/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300561#define emulate_1op_rax_rdx(ctxt, _op, _ex) \
Avi Kivity72952612011-04-20 13:12:27 +0300562 do { \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300563 switch((ctxt)->src.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300564 case 1: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300565 __emulate_1op_rax_rdx(ctxt, _op, "b", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300566 break; \
567 case 2: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300568 __emulate_1op_rax_rdx(ctxt, _op, "w", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300569 break; \
570 case 4: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300571 __emulate_1op_rax_rdx(ctxt, _op, "l", _ex); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300572 break; \
573 case 8: ON64( \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300574 __emulate_1op_rax_rdx(ctxt, _op, "q", _ex)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300575 break; \
576 } \
577 } while (0)
578
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200579static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
580 enum x86_intercept intercept,
581 enum x86_intercept_stage stage)
582{
583 struct x86_instruction_info info = {
584 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300585 .rep_prefix = ctxt->rep_prefix,
586 .modrm_mod = ctxt->modrm_mod,
587 .modrm_reg = ctxt->modrm_reg,
588 .modrm_rm = ctxt->modrm_rm,
589 .src_val = ctxt->src.val64,
590 .src_bytes = ctxt->src.bytes,
591 .dst_bytes = ctxt->dst.bytes,
592 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200593 .next_rip = ctxt->eip,
594 };
595
Avi Kivity29535382011-04-20 13:37:53 +0300596 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200597}
598
Avi Kivityf47cfa32012-06-07 17:49:24 +0300599static void assign_masked(ulong *dest, ulong src, ulong mask)
600{
601 *dest = (*dest & ~mask) | (src & mask);
602}
603
Avi Kivity9dac77f2011-06-01 15:34:25 +0300604static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800605{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300606 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800607}
608
Avi Kivityf47cfa32012-06-07 17:49:24 +0300609static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
610{
611 u16 sel;
612 struct desc_struct ss;
613
614 if (ctxt->mode == X86EMUL_MODE_PROT64)
615 return ~0UL;
616 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
617 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
618}
619
Avi Kivity612e89f2012-06-12 20:03:23 +0300620static int stack_size(struct x86_emulate_ctxt *ctxt)
621{
622 return (__fls(stack_mask(ctxt)) + 1) >> 3;
623}
624
Avi Kivity6aa8b732006-12-10 02:21:36 -0800625/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800626static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300627address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800628{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300629 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800630 return reg;
631 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300632 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800633}
634
635static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300636register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800637{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300638 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800639}
640
Avi Kivity5ad105e2012-08-19 14:34:31 +0300641static void masked_increment(ulong *reg, ulong mask, int inc)
642{
643 assign_masked(reg, *reg + inc, mask);
644}
645
Harvey Harrison7a9572752008-02-19 07:40:41 -0800646static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300647register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800648{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300649 ulong mask;
650
Avi Kivity9dac77f2011-06-01 15:34:25 +0300651 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300652 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800653 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300654 mask = ad_mask(ctxt);
655 masked_increment(reg, mask, inc);
656}
657
658static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
659{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300660 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800661}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800662
Avi Kivity9dac77f2011-06-01 15:34:25 +0300663static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800664{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300665 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800666}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300667
Avi Kivity56697682011-04-03 14:08:51 +0300668static u32 desc_limit_scaled(struct desc_struct *desc)
669{
670 u32 limit = get_desc_limit(desc);
671
672 return desc->g ? (limit << 12) | 0xfff : limit;
673}
674
Avi Kivity9dac77f2011-06-01 15:34:25 +0300675static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300676{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300677 ctxt->has_seg_override = true;
678 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300679}
680
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900681static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300682{
683 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
684 return 0;
685
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900686 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300687}
688
Avi Kivity9dac77f2011-06-01 15:34:25 +0300689static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300690{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300691 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300692 return 0;
693
Avi Kivity9dac77f2011-06-01 15:34:25 +0300694 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300695}
696
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200697static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
698 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300699{
Avi Kivityda9cb572010-11-22 17:53:21 +0200700 ctxt->exception.vector = vec;
701 ctxt->exception.error_code = error;
702 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200703 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300704}
705
Joerg Roedel3b88e412011-04-04 12:39:29 +0200706static int emulate_db(struct x86_emulate_ctxt *ctxt)
707{
708 return emulate_exception(ctxt, DB_VECTOR, 0, false);
709}
710
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200711static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300712{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200713 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300714}
715
Avi Kivity618ff152011-04-03 12:32:09 +0300716static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
717{
718 return emulate_exception(ctxt, SS_VECTOR, err, true);
719}
720
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200721static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300722{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200723 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300724}
725
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200726static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300727{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200728 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300729}
730
Avi Kivity34d1f492010-08-26 11:59:01 +0300731static int emulate_de(struct x86_emulate_ctxt *ctxt)
732{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200733 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300734}
735
Avi Kivity12537912011-03-29 11:41:27 +0200736static int emulate_nm(struct x86_emulate_ctxt *ctxt)
737{
738 return emulate_exception(ctxt, NM_VECTOR, 0, false);
739}
740
Avi Kivity1aa36612011-04-27 13:20:30 +0300741static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
742{
743 u16 selector;
744 struct desc_struct desc;
745
746 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
747 return selector;
748}
749
750static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
751 unsigned seg)
752{
753 u16 dummy;
754 u32 base3;
755 struct desc_struct desc;
756
757 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
758 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
759}
760
Avi Kivity1c11b372012-04-09 18:39:59 +0300761/*
762 * x86 defines three classes of vector instructions: explicitly
763 * aligned, explicitly unaligned, and the rest, which change behaviour
764 * depending on whether they're AVX encoded or not.
765 *
766 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
767 * subject to the same check.
768 */
769static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
770{
771 if (likely(size < 16))
772 return false;
773
774 if (ctxt->d & Aligned)
775 return true;
776 else if (ctxt->d & Unaligned)
777 return false;
778 else if (ctxt->d & Avx)
779 return false;
780 else
781 return true;
782}
783
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400784static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300785 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400786 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300787 ulong *linear)
788{
Avi Kivity618ff152011-04-03 12:32:09 +0300789 struct desc_struct desc;
790 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300791 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300792 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300793 u16 sel;
Gleb Natapov3a78a4f2012-12-20 16:57:42 +0200794 unsigned cpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300795
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900796 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300797 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300798 case X86EMUL_MODE_PROT64:
799 if (((signed long)la << 16) >> 16 != la)
800 return emulate_gp(ctxt, 0);
801 break;
802 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300803 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
804 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300805 if (!usable)
806 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200807 /* code segment in protected mode or read-only data segment */
808 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
809 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300810 goto bad;
811 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400812 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300813 goto bad;
814 lim = desc_limit_scaled(&desc);
815 if ((desc.type & 8) || !(desc.type & 4)) {
816 /* expand-up segment */
817 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
818 goto bad;
819 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800820 /* expand-down segment */
Avi Kivity618ff152011-04-03 12:32:09 +0300821 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
822 goto bad;
823 lim = desc.d ? 0xffffffff : 0xffff;
824 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
825 goto bad;
826 }
Avi Kivity717746e2011-04-20 13:37:53 +0300827 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity618ff152011-04-03 12:32:09 +0300828 if (!(desc.type & 8)) {
829 /* data segment */
830 if (cpl > desc.dpl)
831 goto bad;
832 } else if ((desc.type & 8) && !(desc.type & 4)) {
833 /* nonconforming code segment */
834 if (cpl != desc.dpl)
835 goto bad;
836 } else if ((desc.type & 8) && (desc.type & 4)) {
837 /* conforming code segment */
838 if (cpl < desc.dpl)
839 goto bad;
840 }
841 break;
842 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300843 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300844 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300845 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
846 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300847 *linear = la;
848 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300849bad:
850 if (addr.seg == VCPU_SREG_SS)
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300851 return emulate_ss(ctxt, sel);
Avi Kivity618ff152011-04-03 12:32:09 +0300852 else
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300853 return emulate_gp(ctxt, sel);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300854}
855
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400856static int linearize(struct x86_emulate_ctxt *ctxt,
857 struct segmented_address addr,
858 unsigned size, bool write,
859 ulong *linear)
860{
861 return __linearize(ctxt, addr, size, write, false, linear);
862}
863
864
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200865static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
866 struct segmented_address addr,
867 void *data,
868 unsigned size)
869{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200870 int rc;
871 ulong linear;
872
Avi Kivity83b87952011-04-03 11:31:19 +0300873 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200874 if (rc != X86EMUL_CONTINUE)
875 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300876 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200877}
878
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900879/*
880 * Fetch the next byte of the instruction being emulated which is pointed to
881 * by ctxt->_eip, then increment ctxt->_eip.
882 *
883 * Also prefetch the remaining bytes of the instruction without crossing page
884 * boundary if they are not in fetch_cache yet.
885 */
886static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200887{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300888 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200889 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300890 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200891
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900892 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400893 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900894 struct segmented_address addr = { .seg = VCPU_SREG_CS,
895 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300896 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900897 size = min(15UL - cur_size,
898 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400899 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900900 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400901 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900902 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
903 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900904 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200905 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300906 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200907 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900908 *dest = fc->data[ctxt->_eip - fc->start];
909 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900910 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200911}
912
913static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900914 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200915{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900916 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200917
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200918 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900919 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200920 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200921 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900922 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900923 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200924 return rc;
925 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900926 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200927}
928
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900929/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900930#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900931({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900932 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900933 if (rc != X86EMUL_CONTINUE) \
934 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900935 (_type)_x; \
936})
937
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900938#define insn_fetch_arr(_arr, _size, _ctxt) \
939({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900940 if (rc != X86EMUL_CONTINUE) \
941 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900942})
943
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000944/*
945 * Given the 'reg' portion of a ModRM byte, and a register block, return a
946 * pointer into the block that addresses the relevant register.
947 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
948 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300949static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000950 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951{
952 void *p;
953
Avi Kivity6aa8b732006-12-10 02:21:36 -0800954 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300955 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
956 else
957 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958 return p;
959}
960
961static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200962 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963 u16 *size, unsigned long *address, int op_bytes)
964{
965 int rc;
966
967 if (op_bytes == 2)
968 op_bytes = 3;
969 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200970 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900971 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200973 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200974 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800975 return rc;
976}
977
Avi Kivity34b77652013-01-19 19:51:56 +0200978FASTOP2(add);
979FASTOP2(or);
980FASTOP2(adc);
981FASTOP2(sbb);
982FASTOP2(and);
983FASTOP2(sub);
984FASTOP2(xor);
985FASTOP2(cmp);
986FASTOP2(test);
987
988FASTOP3WCL(shld);
989FASTOP3WCL(shrd);
990
991FASTOP2W(imul);
992
993FASTOP1(not);
994FASTOP1(neg);
995FASTOP1(inc);
996FASTOP1(dec);
997
998FASTOP2CL(rol);
999FASTOP2CL(ror);
1000FASTOP2CL(rcl);
1001FASTOP2CL(rcr);
1002FASTOP2CL(shl);
1003FASTOP2CL(shr);
1004FASTOP2CL(sar);
1005
1006FASTOP2W(bsf);
1007FASTOP2W(bsr);
1008FASTOP2W(bt);
1009FASTOP2W(bts);
1010FASTOP2W(btr);
1011FASTOP2W(btc);
1012
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001013static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001014{
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001015 u8 rc;
1016 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001017
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001018 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +02001019 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001020 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
1021 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001022}
1023
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001024static void fetch_register_operand(struct operand *op)
1025{
1026 switch (op->bytes) {
1027 case 1:
1028 op->val = *(u8 *)op->addr.reg;
1029 break;
1030 case 2:
1031 op->val = *(u16 *)op->addr.reg;
1032 break;
1033 case 4:
1034 op->val = *(u32 *)op->addr.reg;
1035 break;
1036 case 8:
1037 op->val = *(u64 *)op->addr.reg;
1038 break;
1039 }
1040}
1041
Avi Kivity12537912011-03-29 11:41:27 +02001042static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
1043{
1044 ctxt->ops->get_fpu(ctxt);
1045 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001046 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
1047 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
1048 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
1049 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
1050 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
1051 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
1052 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
1053 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001054#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001055 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
1056 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
1057 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
1058 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
1059 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
1060 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
1061 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
1062 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001063#endif
1064 default: BUG();
1065 }
1066 ctxt->ops->put_fpu(ctxt);
1067}
1068
1069static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
1070 int reg)
1071{
1072 ctxt->ops->get_fpu(ctxt);
1073 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001074 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
1075 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
1076 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
1077 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
1078 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
1079 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
1080 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
1081 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001082#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001083 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
1084 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
1085 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
1086 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
1087 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
1088 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
1089 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
1090 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001091#endif
1092 default: BUG();
1093 }
1094 ctxt->ops->put_fpu(ctxt);
1095}
1096
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001097static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1098{
1099 ctxt->ops->get_fpu(ctxt);
1100 switch (reg) {
1101 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1102 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1103 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1104 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1105 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1106 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1107 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1108 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1109 default: BUG();
1110 }
1111 ctxt->ops->put_fpu(ctxt);
1112}
1113
1114static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1115{
1116 ctxt->ops->get_fpu(ctxt);
1117 switch (reg) {
1118 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1119 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1120 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1121 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1122 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1123 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1124 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1125 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1126 default: BUG();
1127 }
1128 ctxt->ops->put_fpu(ctxt);
1129}
1130
Gleb Natapov045a2822012-12-20 16:57:43 +02001131static int em_fninit(struct x86_emulate_ctxt *ctxt)
1132{
1133 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1134 return emulate_nm(ctxt);
1135
1136 ctxt->ops->get_fpu(ctxt);
1137 asm volatile("fninit");
1138 ctxt->ops->put_fpu(ctxt);
1139 return X86EMUL_CONTINUE;
1140}
1141
1142static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1143{
1144 u16 fcw;
1145
1146 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1147 return emulate_nm(ctxt);
1148
1149 ctxt->ops->get_fpu(ctxt);
1150 asm volatile("fnstcw %0": "+m"(fcw));
1151 ctxt->ops->put_fpu(ctxt);
1152
1153 /* force 2 byte destination */
1154 ctxt->dst.bytes = 2;
1155 ctxt->dst.val = fcw;
1156
1157 return X86EMUL_CONTINUE;
1158}
1159
1160static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1161{
1162 u16 fsw;
1163
1164 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1165 return emulate_nm(ctxt);
1166
1167 ctxt->ops->get_fpu(ctxt);
1168 asm volatile("fnstsw %0": "+m"(fsw));
1169 ctxt->ops->put_fpu(ctxt);
1170
1171 /* force 2 byte destination */
1172 ctxt->dst.bytes = 2;
1173 ctxt->dst.val = fsw;
1174
1175 return X86EMUL_CONTINUE;
1176}
1177
Avi Kivity12537912011-03-29 11:41:27 +02001178static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001179 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001180{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001181 unsigned reg = ctxt->modrm_reg;
1182 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +02001183
Avi Kivity9dac77f2011-06-01 15:34:25 +03001184 if (!(ctxt->d & ModRM))
1185 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +02001186
Avi Kivity9dac77f2011-06-01 15:34:25 +03001187 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001188 op->type = OP_XMM;
1189 op->bytes = 16;
1190 op->addr.xmm = reg;
1191 read_sse_reg(ctxt, &op->vec_val, reg);
1192 return;
1193 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001194 if (ctxt->d & Mmx) {
1195 reg &= 7;
1196 op->type = OP_MM;
1197 op->bytes = 8;
1198 op->addr.mm = reg;
1199 return;
1200 }
Avi Kivity12537912011-03-29 11:41:27 +02001201
Avi Kivity3c118e22007-10-31 10:27:04 +02001202 op->type = OP_REG;
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001203 if (ctxt->d & ByteOp) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001204 op->addr.reg = decode_register(ctxt, reg, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +02001205 op->bytes = 1;
1206 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001207 op->addr.reg = decode_register(ctxt, reg, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001208 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +02001209 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001210 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001211 op->orig_val = op->val;
1212}
1213
Avi Kivitya6e34072012-06-10 17:15:39 +03001214static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1215{
1216 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1217 ctxt->modrm_seg = VCPU_SREG_SS;
1218}
1219
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001220static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001221 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001222{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001223 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001224 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001225 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001226 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001227
Avi Kivity9dac77f2011-06-01 15:34:25 +03001228 if (ctxt->rex_prefix) {
1229 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
1230 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
1231 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001232 }
1233
Avi Kivity9dac77f2011-06-01 15:34:25 +03001234 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
1235 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
1236 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
1237 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001238
Avi Kivity9dac77f2011-06-01 15:34:25 +03001239 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001240 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001241 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001242 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm, ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001243 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001244 op->type = OP_XMM;
1245 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001246 op->addr.xmm = ctxt->modrm_rm;
1247 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001248 return rc;
1249 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001250 if (ctxt->d & Mmx) {
1251 op->type = OP_MM;
1252 op->bytes = 8;
1253 op->addr.xmm = ctxt->modrm_rm & 7;
1254 return rc;
1255 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001256 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001257 return rc;
1258 }
1259
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001260 op->type = OP_MEM;
1261
Avi Kivity9dac77f2011-06-01 15:34:25 +03001262 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001263 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1264 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1265 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1266 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001267
1268 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001269 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001270 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001271 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001272 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001273 break;
1274 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001275 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001276 break;
1277 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001278 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001279 break;
1280 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001281 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001282 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001283 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001284 break;
1285 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001286 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001287 break;
1288 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001289 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001290 break;
1291 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001292 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001293 break;
1294 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001295 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001296 break;
1297 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001298 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001299 break;
1300 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001301 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001302 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001303 break;
1304 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001305 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001306 break;
1307 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001308 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1309 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1310 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001311 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001312 } else {
1313 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001314 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001315 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001316 index_reg |= (sib >> 3) & 7;
1317 base_reg |= sib & 7;
1318 scale = sib >> 6;
1319
Avi Kivity9dac77f2011-06-01 15:34:25 +03001320 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001321 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001322 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001323 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001324 adjust_modrm_seg(ctxt, base_reg);
1325 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001326 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001327 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001328 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001329 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001330 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001331 } else {
1332 base_reg = ctxt->modrm_rm;
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 Kivity9dac77f2011-06-01 15:34:25 +03001336 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001337 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001338 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001339 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001340 break;
1341 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001342 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001343 break;
1344 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001345 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001346 break;
1347 }
1348 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001349 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001350done:
1351 return rc;
1352}
1353
1354static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001355 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001356{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001357 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001358
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001359 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001360 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001361 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001362 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001363 break;
1364 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001365 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001366 break;
1367 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001368 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001369 break;
1370 }
1371done:
1372 return rc;
1373}
1374
Avi Kivity9dac77f2011-06-01 15:34:25 +03001375static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001376{
Sheng Yang7129eec2010-09-28 16:33:32 +08001377 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001378
Avi Kivity9dac77f2011-06-01 15:34:25 +03001379 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1380 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001381
Avi Kivity9dac77f2011-06-01 15:34:25 +03001382 if (ctxt->src.bytes == 2)
1383 sv = (s16)ctxt->src.val & (s16)mask;
1384 else if (ctxt->src.bytes == 4)
1385 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001386
Avi Kivity9dac77f2011-06-01 15:34:25 +03001387 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001388 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001389
1390 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001391 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001392}
1393
Gleb Natapov9de41572010-04-28 19:15:22 +03001394static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001395 unsigned long addr, void *dest, unsigned size)
1396{
1397 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001398 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001399
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001400 if (mc->pos < mc->end)
1401 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001402
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001403 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001404
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001405 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1406 &ctxt->exception);
1407 if (rc != X86EMUL_CONTINUE)
1408 return rc;
1409
1410 mc->end += size;
1411
1412read_cached:
1413 memcpy(dest, mc->data + mc->pos, size);
1414 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001415 return X86EMUL_CONTINUE;
1416}
1417
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001418static int segmented_read(struct x86_emulate_ctxt *ctxt,
1419 struct segmented_address addr,
1420 void *data,
1421 unsigned size)
1422{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001423 int rc;
1424 ulong linear;
1425
Avi Kivity83b87952011-04-03 11:31:19 +03001426 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001427 if (rc != X86EMUL_CONTINUE)
1428 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001429 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001430}
1431
1432static int segmented_write(struct x86_emulate_ctxt *ctxt,
1433 struct segmented_address addr,
1434 const void *data,
1435 unsigned size)
1436{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001437 int rc;
1438 ulong linear;
1439
Avi Kivity83b87952011-04-03 11:31:19 +03001440 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001441 if (rc != X86EMUL_CONTINUE)
1442 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001443 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1444 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001445}
1446
1447static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1448 struct segmented_address addr,
1449 const void *orig_data, const void *data,
1450 unsigned size)
1451{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001452 int rc;
1453 ulong linear;
1454
Avi Kivity83b87952011-04-03 11:31:19 +03001455 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001456 if (rc != X86EMUL_CONTINUE)
1457 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001458 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1459 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001460}
1461
Gleb Natapov7b262e92010-03-18 15:20:27 +02001462static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001463 unsigned int size, unsigned short port,
1464 void *dest)
1465{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001466 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001467
1468 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001469 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001470 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001471 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001472 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001473 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1474 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Gleb Natapov7b262e92010-03-18 15:20:27 +02001475 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1476 count);
1477 if (n == 0)
1478 n = 1;
1479 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001480 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001481 return 0;
1482 rc->end = n * size;
1483 }
1484
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001485 if (ctxt->rep_prefix && !(ctxt->eflags & EFLG_DF)) {
1486 ctxt->dst.data = rc->data + rc->pos;
1487 ctxt->dst.type = OP_MEM_STR;
1488 ctxt->dst.count = (rc->end - rc->pos) / size;
1489 rc->pos = rc->end;
1490 } else {
1491 memcpy(dest, rc->data + rc->pos, size);
1492 rc->pos += size;
1493 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001494 return 1;
1495}
1496
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001497static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1498 u16 index, struct desc_struct *desc)
1499{
1500 struct desc_ptr dt;
1501 ulong addr;
1502
1503 ctxt->ops->get_idt(ctxt, &dt);
1504
1505 if (dt.size < index * 8 + 7)
1506 return emulate_gp(ctxt, index << 3 | 0x2);
1507
1508 addr = dt.address + index * 8;
1509 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1510 &ctxt->exception);
1511}
1512
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001513static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001514 u16 selector, struct desc_ptr *dt)
1515{
Mathias Krause0225fb52012-08-30 01:30:16 +02001516 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001517
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001518 if (selector & 1 << 2) {
1519 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001520 u16 sel;
1521
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001522 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001523 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001524 return;
1525
1526 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1527 dt->address = get_desc_base(&desc);
1528 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001529 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001530}
1531
1532/* allowed just for 8 bytes segments */
1533static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001534 u16 selector, struct desc_struct *desc,
1535 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001536{
1537 struct desc_ptr dt;
1538 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001539 ulong addr;
1540
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001541 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001542
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001543 if (dt.size < index * 8 + 7)
1544 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001545
Avi Kivitye9194642012-06-13 16:29:39 +03001546 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001547 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1548 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001549}
1550
1551/* allowed just for 8 bytes segments */
1552static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001553 u16 selector, struct desc_struct *desc)
1554{
1555 struct desc_ptr dt;
1556 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001557 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001558
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001559 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001560
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001561 if (dt.size < index * 8 + 7)
1562 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001563
1564 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001565 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1566 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001567}
1568
Gleb Natapov5601d052011-03-07 14:55:06 +02001569/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001570static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001571 u16 selector, int seg)
1572{
Avi Kivity869be992012-06-13 16:30:53 +03001573 struct desc_struct seg_desc, old_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001574 u8 dpl, rpl, cpl;
1575 unsigned err_vec = GP_VECTOR;
1576 u32 err_code = 0;
1577 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001578 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001579 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001580 u16 dummy;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001581
1582 memset(&seg_desc, 0, sizeof seg_desc);
1583
Kevin Wolff8da94e2013-04-11 14:06:03 +02001584 if (ctxt->mode == X86EMUL_MODE_REAL) {
1585 /* set real mode segment descriptor (keep limit etc. for
1586 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001587 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001588 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001589 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001590 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1591 /* VM86 needs a clean new segment descriptor */
1592 set_desc_base(&seg_desc, selector << 4);
1593 set_desc_limit(&seg_desc, 0xffff);
1594 seg_desc.type = 3;
1595 seg_desc.p = 1;
1596 seg_desc.s = 1;
1597 seg_desc.dpl = 3;
1598 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001599 }
1600
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001601 rpl = selector & 3;
1602 cpl = ctxt->ops->cpl(ctxt);
1603
1604 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1605 if ((seg == VCPU_SREG_CS
1606 || (seg == VCPU_SREG_SS
1607 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1608 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001609 && null_selector)
1610 goto exception;
1611
1612 /* TR should be in GDT only */
1613 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1614 goto exception;
1615
1616 if (null_selector) /* for NULL selector skip all following checks */
1617 goto load;
1618
Avi Kivitye9194642012-06-13 16:29:39 +03001619 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001620 if (ret != X86EMUL_CONTINUE)
1621 return ret;
1622
1623 err_code = selector & 0xfffc;
1624 err_vec = GP_VECTOR;
1625
Guo Chaofc058682012-06-28 15:19:51 +08001626 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001627 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1628 goto exception;
1629
1630 if (!seg_desc.p) {
1631 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1632 goto exception;
1633 }
1634
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001635 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001636
1637 switch (seg) {
1638 case VCPU_SREG_SS:
1639 /*
1640 * segment is not a writable data segment or segment
1641 * selector's RPL != CPL or segment selector's RPL != CPL
1642 */
1643 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1644 goto exception;
1645 break;
1646 case VCPU_SREG_CS:
1647 if (!(seg_desc.type & 8))
1648 goto exception;
1649
1650 if (seg_desc.type & 4) {
1651 /* conforming */
1652 if (dpl > cpl)
1653 goto exception;
1654 } else {
1655 /* nonconforming */
1656 if (rpl > cpl || dpl != cpl)
1657 goto exception;
1658 }
1659 /* CS(RPL) <- CPL */
1660 selector = (selector & 0xfffc) | cpl;
1661 break;
1662 case VCPU_SREG_TR:
1663 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1664 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001665 old_desc = seg_desc;
1666 seg_desc.type |= 2; /* busy */
1667 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1668 sizeof(seg_desc), &ctxt->exception);
1669 if (ret != X86EMUL_CONTINUE)
1670 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001671 break;
1672 case VCPU_SREG_LDTR:
1673 if (seg_desc.s || seg_desc.type != 2)
1674 goto exception;
1675 break;
1676 default: /* DS, ES, FS, or GS */
1677 /*
1678 * segment is not a data or readable code segment or
1679 * ((segment is a data or nonconforming code segment)
1680 * and (both RPL and CPL > DPL))
1681 */
1682 if ((seg_desc.type & 0xa) == 0x8 ||
1683 (((seg_desc.type & 0xc) != 0xc) &&
1684 (rpl > dpl && cpl > dpl)))
1685 goto exception;
1686 break;
1687 }
1688
1689 if (seg_desc.s) {
1690 /* mark segment as accessed */
1691 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001692 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001693 if (ret != X86EMUL_CONTINUE)
1694 return ret;
1695 }
1696load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001697 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001698 return X86EMUL_CONTINUE;
1699exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001700 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001701 return X86EMUL_PROPAGATE_FAULT;
1702}
1703
Wei Yongjun31be40b2010-08-17 09:17:30 +08001704static void write_register_operand(struct operand *op)
1705{
1706 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1707 switch (op->bytes) {
1708 case 1:
1709 *(u8 *)op->addr.reg = (u8)op->val;
1710 break;
1711 case 2:
1712 *(u16 *)op->addr.reg = (u16)op->val;
1713 break;
1714 case 4:
1715 *op->addr.reg = (u32)op->val;
1716 break; /* 64b: zero-extend */
1717 case 8:
1718 *op->addr.reg = op->val;
1719 break;
1720 }
1721}
1722
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001723static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001724{
1725 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001726
Avi Kivityb6744dc2013-01-04 16:18:50 +02001727 if (ctxt->d & NoWrite)
1728 return X86EMUL_CONTINUE;
1729
Avi Kivity9dac77f2011-06-01 15:34:25 +03001730 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001731 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001732 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001733 break;
1734 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001735 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001736 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001737 ctxt->dst.addr.mem,
1738 &ctxt->dst.orig_val,
1739 &ctxt->dst.val,
1740 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001741 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001742 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001743 ctxt->dst.addr.mem,
1744 &ctxt->dst.val,
1745 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001746 if (rc != X86EMUL_CONTINUE)
1747 return rc;
1748 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001749 case OP_MEM_STR:
1750 rc = segmented_write(ctxt,
1751 ctxt->dst.addr.mem,
1752 ctxt->dst.data,
1753 ctxt->dst.bytes * ctxt->dst.count);
1754 if (rc != X86EMUL_CONTINUE)
1755 return rc;
1756 break;
Avi Kivity12537912011-03-29 11:41:27 +02001757 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001758 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001759 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001760 case OP_MM:
1761 write_mmx_reg(ctxt, &ctxt->dst.mm_val, ctxt->dst.addr.mm);
1762 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001763 case OP_NONE:
1764 /* no writeback */
1765 break;
1766 default:
1767 break;
1768 }
1769 return X86EMUL_CONTINUE;
1770}
1771
Avi Kivity51ddff52012-06-12 20:19:40 +03001772static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001773{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001774 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001775
Avi Kivity5ad105e2012-08-19 14:34:31 +03001776 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001777 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001778 addr.seg = VCPU_SREG_SS;
1779
Avi Kivity51ddff52012-06-12 20:19:40 +03001780 return segmented_write(ctxt, addr, data, bytes);
1781}
1782
1783static int em_push(struct x86_emulate_ctxt *ctxt)
1784{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001785 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001786 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001787 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001788}
1789
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001790static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001791 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001792{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001793 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001794 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001795
Avi Kivitydd856ef2012-08-27 23:46:17 +03001796 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001797 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001798 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001799 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001800 return rc;
1801
Avi Kivity5ad105e2012-08-19 14:34:31 +03001802 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001803 return rc;
1804}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001805
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001806static int em_pop(struct x86_emulate_ctxt *ctxt)
1807{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001808 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001809}
1810
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001811static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001812 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001813{
1814 int rc;
1815 unsigned long val, change_mask;
1816 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001817 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001818
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001819 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001820 if (rc != X86EMUL_CONTINUE)
1821 return rc;
1822
1823 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1824 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1825
1826 switch(ctxt->mode) {
1827 case X86EMUL_MODE_PROT64:
1828 case X86EMUL_MODE_PROT32:
1829 case X86EMUL_MODE_PROT16:
1830 if (cpl == 0)
1831 change_mask |= EFLG_IOPL;
1832 if (cpl <= iopl)
1833 change_mask |= EFLG_IF;
1834 break;
1835 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001836 if (iopl < 3)
1837 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001838 change_mask |= EFLG_IF;
1839 break;
1840 default: /* real mode */
1841 change_mask |= (EFLG_IOPL | EFLG_IF);
1842 break;
1843 }
1844
1845 *(unsigned long *)dest =
1846 (ctxt->eflags & ~change_mask) | (val & change_mask);
1847
1848 return rc;
1849}
1850
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001851static int em_popf(struct x86_emulate_ctxt *ctxt)
1852{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001853 ctxt->dst.type = OP_REG;
1854 ctxt->dst.addr.reg = &ctxt->eflags;
1855 ctxt->dst.bytes = ctxt->op_bytes;
1856 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001857}
1858
Avi Kivity612e89f2012-06-12 20:03:23 +03001859static int em_enter(struct x86_emulate_ctxt *ctxt)
1860{
1861 int rc;
1862 unsigned frame_size = ctxt->src.val;
1863 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001864 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001865
1866 if (nesting_level)
1867 return X86EMUL_UNHANDLEABLE;
1868
Avi Kivitydd856ef2012-08-27 23:46:17 +03001869 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1870 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001871 if (rc != X86EMUL_CONTINUE)
1872 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001873 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001874 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001875 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1876 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001877 stack_mask(ctxt));
1878 return X86EMUL_CONTINUE;
1879}
1880
Avi Kivityf47cfa32012-06-07 17:49:24 +03001881static int em_leave(struct x86_emulate_ctxt *ctxt)
1882{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001883 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001884 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001885 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001886}
1887
Avi Kivity1cd196e2011-09-13 10:45:51 +03001888static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001889{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001890 int seg = ctxt->src2.val;
1891
Avi Kivity9dac77f2011-06-01 15:34:25 +03001892 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001893
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001894 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001895}
1896
Avi Kivity1cd196e2011-09-13 10:45:51 +03001897static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001898{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001899 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001900 unsigned long selector;
1901 int rc;
1902
Avi Kivity9dac77f2011-06-01 15:34:25 +03001903 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001904 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001905 return rc;
1906
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001907 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001908 return rc;
1909}
1910
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001911static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001912{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001913 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001914 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001915 int reg = VCPU_REGS_RAX;
1916
1917 while (reg <= VCPU_REGS_RDI) {
1918 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001919 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001920
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001921 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001922 if (rc != X86EMUL_CONTINUE)
1923 return rc;
1924
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001925 ++reg;
1926 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001927
Wei Yongjunc37eda12010-06-15 09:03:33 +08001928 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001929}
1930
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001931static int em_pushf(struct x86_emulate_ctxt *ctxt)
1932{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001933 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001934 return em_push(ctxt);
1935}
1936
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001937static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001938{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001939 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001940 int reg = VCPU_REGS_RDI;
1941
1942 while (reg >= VCPU_REGS_RAX) {
1943 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001944 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001945 --reg;
1946 }
1947
Avi Kivitydd856ef2012-08-27 23:46:17 +03001948 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001949 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001950 break;
1951 --reg;
1952 }
1953 return rc;
1954}
1955
Avi Kivitydd856ef2012-08-27 23:46:17 +03001956static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001957{
Mathias Krause0225fb52012-08-30 01:30:16 +02001958 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001959 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001960 struct desc_ptr dt;
1961 gva_t cs_addr;
1962 gva_t eip_addr;
1963 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001964
1965 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001966 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001967 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001968 if (rc != X86EMUL_CONTINUE)
1969 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001970
1971 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1972
Avi Kivity9dac77f2011-06-01 15:34:25 +03001973 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
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
Avi Kivity9dac77f2011-06-01 15:34:25 +03001978 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001979 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001980 if (rc != X86EMUL_CONTINUE)
1981 return rc;
1982
Avi Kivity4bff1e862011-04-20 13:37:53 +03001983 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001984
1985 eip_addr = dt.address + (irq << 2);
1986 cs_addr = dt.address + (irq << 2) + 2;
1987
Avi Kivity0f65dd72011-04-20 13:37:53 +03001988 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001989 if (rc != X86EMUL_CONTINUE)
1990 return rc;
1991
Avi Kivity0f65dd72011-04-20 13:37:53 +03001992 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001993 if (rc != X86EMUL_CONTINUE)
1994 return rc;
1995
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001996 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001997 if (rc != X86EMUL_CONTINUE)
1998 return rc;
1999
Avi Kivity9dac77f2011-06-01 15:34:25 +03002000 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002001
2002 return rc;
2003}
2004
Avi Kivitydd856ef2012-08-27 23:46:17 +03002005int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
2006{
2007 int rc;
2008
2009 invalidate_registers(ctxt);
2010 rc = __emulate_int_real(ctxt, irq);
2011 if (rc == X86EMUL_CONTINUE)
2012 writeback_registers(ctxt);
2013 return rc;
2014}
2015
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002016static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002017{
2018 switch(ctxt->mode) {
2019 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03002020 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002021 case X86EMUL_MODE_VM86:
2022 case X86EMUL_MODE_PROT16:
2023 case X86EMUL_MODE_PROT32:
2024 case X86EMUL_MODE_PROT64:
2025 default:
2026 /* Protected mode interrupts unimplemented yet */
2027 return X86EMUL_UNHANDLEABLE;
2028 }
2029}
2030
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002031static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002032{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002033 int rc = X86EMUL_CONTINUE;
2034 unsigned long temp_eip = 0;
2035 unsigned long temp_eflags = 0;
2036 unsigned long cs = 0;
2037 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
2038 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
2039 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
2040 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
2041
2042 /* TODO: Add stack limit check */
2043
Avi Kivity9dac77f2011-06-01 15:34:25 +03002044 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002045
2046 if (rc != X86EMUL_CONTINUE)
2047 return rc;
2048
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002049 if (temp_eip & ~0xffff)
2050 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002051
Avi Kivity9dac77f2011-06-01 15:34:25 +03002052 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002053
2054 if (rc != X86EMUL_CONTINUE)
2055 return rc;
2056
Avi Kivity9dac77f2011-06-01 15:34:25 +03002057 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002058
2059 if (rc != X86EMUL_CONTINUE)
2060 return rc;
2061
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002062 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002063
2064 if (rc != X86EMUL_CONTINUE)
2065 return rc;
2066
Avi Kivity9dac77f2011-06-01 15:34:25 +03002067 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002068
2069
Avi Kivity9dac77f2011-06-01 15:34:25 +03002070 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002071 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03002072 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002073 ctxt->eflags &= ~0xffff;
2074 ctxt->eflags |= temp_eflags;
2075 }
2076
2077 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
2078 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
2079
2080 return rc;
2081}
2082
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002083static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002084{
2085 switch(ctxt->mode) {
2086 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002087 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002088 case X86EMUL_MODE_VM86:
2089 case X86EMUL_MODE_PROT16:
2090 case X86EMUL_MODE_PROT32:
2091 case X86EMUL_MODE_PROT64:
2092 default:
2093 /* iret from protected mode unimplemented yet */
2094 return X86EMUL_UNHANDLEABLE;
2095 }
2096}
2097
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002098static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2099{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002100 int rc;
2101 unsigned short sel;
2102
Avi Kivity9dac77f2011-06-01 15:34:25 +03002103 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002104
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002105 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002106 if (rc != X86EMUL_CONTINUE)
2107 return rc;
2108
Avi Kivity9dac77f2011-06-01 15:34:25 +03002109 ctxt->_eip = 0;
2110 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002111 return X86EMUL_CONTINUE;
2112}
2113
Avi Kivity3329ece2011-09-13 10:45:39 +03002114static int em_mul_ex(struct x86_emulate_ctxt *ctxt)
2115{
2116 u8 ex = 0;
2117
2118 emulate_1op_rax_rdx(ctxt, "mul", ex);
2119 return X86EMUL_CONTINUE;
2120}
2121
2122static int em_imul_ex(struct x86_emulate_ctxt *ctxt)
2123{
2124 u8 ex = 0;
2125
2126 emulate_1op_rax_rdx(ctxt, "imul", ex);
2127 return X86EMUL_CONTINUE;
2128}
2129
2130static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002131{
Avi Kivity34d1f492010-08-26 11:59:01 +03002132 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002133
Avi Kivity3329ece2011-09-13 10:45:39 +03002134 emulate_1op_rax_rdx(ctxt, "div", de);
2135 if (de)
2136 return emulate_de(ctxt);
2137 return X86EMUL_CONTINUE;
2138}
2139
2140static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
2141{
2142 u8 de = 0;
2143
2144 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03002145 if (de)
2146 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03002147 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002148}
2149
Takuya Yoshikawa51187682011-05-02 02:29:17 +09002150static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002151{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002152 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002153
Avi Kivity9dac77f2011-06-01 15:34:25 +03002154 switch (ctxt->modrm_reg) {
Mohammed Gamald19292e2008-09-08 21:47:19 +03002155 case 2: /* call near abs */ {
2156 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002157 old_eip = ctxt->_eip;
2158 ctxt->_eip = ctxt->src.val;
2159 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002160 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03002161 break;
2162 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002163 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002164 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002165 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002166 case 5: /* jmp far */
2167 rc = em_jmp_far(ctxt);
2168 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002169 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002170 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002171 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002172 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002173 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002174}
2175
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002176static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002177{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002178 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002179
Avi Kivitydd856ef2012-08-27 23:46:17 +03002180 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2181 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2182 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2183 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002184 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002185 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002186 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2187 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002188
Laurent Vivier05f086f2007-09-24 11:10:55 +02002189 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002190 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002191 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002192}
2193
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002194static int em_ret(struct x86_emulate_ctxt *ctxt)
2195{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002196 ctxt->dst.type = OP_REG;
2197 ctxt->dst.addr.reg = &ctxt->_eip;
2198 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002199 return em_pop(ctxt);
2200}
2201
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002202static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002203{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002204 int rc;
2205 unsigned long cs;
2206
Avi Kivity9dac77f2011-06-01 15:34:25 +03002207 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002208 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002209 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002210 if (ctxt->op_bytes == 4)
2211 ctxt->_eip = (u32)ctxt->_eip;
2212 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002213 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002214 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002215 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002216 return rc;
2217}
2218
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002219static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2220{
2221 /* Save real source value, then compare EAX against destination. */
2222 ctxt->src.orig_val = ctxt->src.val;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002223 ctxt->src.val = reg_read(ctxt, VCPU_REGS_RAX);
Avi Kivity158de572013-01-19 19:51:57 +02002224 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002225
2226 if (ctxt->eflags & EFLG_ZF) {
2227 /* Success: write back to memory. */
2228 ctxt->dst.val = ctxt->src.orig_val;
2229 } else {
2230 /* Failure: write the value we saw to EAX. */
2231 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002232 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002233 }
2234 return X86EMUL_CONTINUE;
2235}
2236
Avi Kivityd4b43252011-09-13 10:45:50 +03002237static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002238{
Avi Kivityd4b43252011-09-13 10:45:50 +03002239 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002240 unsigned short sel;
2241 int rc;
2242
Avi Kivity9dac77f2011-06-01 15:34:25 +03002243 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002244
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002245 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002246 if (rc != X86EMUL_CONTINUE)
2247 return rc;
2248
Avi Kivity9dac77f2011-06-01 15:34:25 +03002249 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002250 return rc;
2251}
2252
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002253static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002254setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002255 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002256{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002257 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002258 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002259 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002260 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002261 cs->type = 0x0b; /* Read, Execute, Accessed */
2262 cs->s = 1;
2263 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002264 cs->p = 1;
2265 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002266 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002267
Gleb Natapov79168fd2010-04-28 19:15:30 +03002268 set_desc_base(ss, 0); /* flat segment */
2269 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002270 ss->g = 1; /* 4kb granularity */
2271 ss->s = 1;
2272 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002273 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002274 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002275 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002276 ss->l = 0;
2277 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002278}
2279
Avi Kivity1a18a692012-02-01 12:23:21 +02002280static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2281{
2282 u32 eax, ebx, ecx, edx;
2283
2284 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002285 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2286 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002287 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2288 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2289}
2290
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002291static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2292{
Mathias Krause0225fb52012-08-30 01:30:16 +02002293 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002294 u32 eax, ebx, ecx, edx;
2295
2296 /*
2297 * syscall should always be enabled in longmode - so only become
2298 * vendor specific (cpuid) if other modes are active...
2299 */
2300 if (ctxt->mode == X86EMUL_MODE_PROT64)
2301 return true;
2302
2303 eax = 0x00000000;
2304 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002305 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2306 /*
2307 * Intel ("GenuineIntel")
2308 * remark: Intel CPUs only support "syscall" in 64bit
2309 * longmode. Also an 64bit guest with a
2310 * 32bit compat-app running will #UD !! While this
2311 * behaviour can be fixed (by emulating) into AMD
2312 * response - CPUs of AMD can't behave like Intel.
2313 */
2314 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2315 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2316 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2317 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002318
Avi Kivity0017f932012-06-07 14:10:16 +03002319 /* AMD ("AuthenticAMD") */
2320 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2321 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2322 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2323 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002324
Avi Kivity0017f932012-06-07 14:10:16 +03002325 /* AMD ("AMDisbetter!") */
2326 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2327 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2328 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2329 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002330
2331 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2332 return false;
2333}
2334
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002335static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002336{
Mathias Krause0225fb52012-08-30 01:30:16 +02002337 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002338 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002339 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002340 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002341 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002342
2343 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002344 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002345 ctxt->mode == X86EMUL_MODE_VM86)
2346 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002347
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002348 if (!(em_syscall_is_enabled(ctxt)))
2349 return emulate_ud(ctxt);
2350
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002351 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002352 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002353
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002354 if (!(efer & EFER_SCE))
2355 return emulate_ud(ctxt);
2356
Avi Kivity717746e2011-04-20 13:37:53 +03002357 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002358 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002359 cs_sel = (u16)(msr_data & 0xfffc);
2360 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002361
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002362 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002363 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002364 cs.l = 1;
2365 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002366 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2367 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002368
Avi Kivitydd856ef2012-08-27 23:46:17 +03002369 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002370 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002371#ifdef CONFIG_X86_64
Avi Kivitydd856ef2012-08-27 23:46:17 +03002372 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002373
Avi Kivity717746e2011-04-20 13:37:53 +03002374 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002375 ctxt->mode == X86EMUL_MODE_PROT64 ?
2376 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002377 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002378
Avi Kivity717746e2011-04-20 13:37:53 +03002379 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002380 ctxt->eflags &= ~(msr_data | EFLG_RF);
2381#endif
2382 } else {
2383 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002384 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002385 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002386
2387 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2388 }
2389
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002390 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002391}
2392
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002393static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002394{
Mathias Krause0225fb52012-08-30 01:30:16 +02002395 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002396 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002397 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002398 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002399 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002400
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002401 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002402 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002403 if (ctxt->mode == X86EMUL_MODE_REAL)
2404 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002405
Avi Kivity1a18a692012-02-01 12:23:21 +02002406 /*
2407 * Not recognized on AMD in compat mode (but is recognized in legacy
2408 * mode).
2409 */
2410 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2411 && !vendor_intel(ctxt))
2412 return emulate_ud(ctxt);
2413
Andre Przywara8c604352009-06-18 12:56:01 +02002414 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2415 * Therefore, we inject an #UD.
2416 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002417 if (ctxt->mode == X86EMUL_MODE_PROT64)
2418 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002419
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002420 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002421
Avi Kivity717746e2011-04-20 13:37:53 +03002422 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002423 switch (ctxt->mode) {
2424 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002425 if ((msr_data & 0xfffc) == 0x0)
2426 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002427 break;
2428 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002429 if (msr_data == 0x0)
2430 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002431 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002432 default:
2433 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002434 }
2435
2436 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002437 cs_sel = (u16)msr_data;
2438 cs_sel &= ~SELECTOR_RPL_MASK;
2439 ss_sel = cs_sel + 8;
2440 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002441 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002442 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002443 cs.l = 1;
2444 }
2445
Avi Kivity1aa36612011-04-27 13:20:30 +03002446 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2447 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002448
Avi Kivity717746e2011-04-20 13:37:53 +03002449 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002450 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002451
Avi Kivity717746e2011-04-20 13:37:53 +03002452 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002453 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002454
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002455 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002456}
2457
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002458static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002459{
Mathias Krause0225fb52012-08-30 01:30:16 +02002460 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002461 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002462 u64 msr_data;
2463 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002464 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002465
Gleb Natapova0044752010-02-10 14:21:31 +02002466 /* inject #GP if in real mode or Virtual 8086 mode */
2467 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002468 ctxt->mode == X86EMUL_MODE_VM86)
2469 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002470
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002471 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002472
Avi Kivity9dac77f2011-06-01 15:34:25 +03002473 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002474 usermode = X86EMUL_MODE_PROT64;
2475 else
2476 usermode = X86EMUL_MODE_PROT32;
2477
2478 cs.dpl = 3;
2479 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002480 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002481 switch (usermode) {
2482 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002483 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002484 if ((msr_data & 0xfffc) == 0x0)
2485 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002486 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002487 break;
2488 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002489 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002490 if (msr_data == 0x0)
2491 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002492 ss_sel = cs_sel + 8;
2493 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002494 cs.l = 1;
2495 break;
2496 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002497 cs_sel |= SELECTOR_RPL_MASK;
2498 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002499
Avi Kivity1aa36612011-04-27 13:20:30 +03002500 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2501 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002502
Avi Kivitydd856ef2012-08-27 23:46:17 +03002503 ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX);
2504 *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX);
Andre Przywara4668f052009-06-18 12:56:02 +02002505
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002506 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002507}
2508
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002509static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002510{
2511 int iopl;
2512 if (ctxt->mode == X86EMUL_MODE_REAL)
2513 return false;
2514 if (ctxt->mode == X86EMUL_MODE_VM86)
2515 return true;
2516 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002517 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002518}
2519
2520static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002521 u16 port, u16 len)
2522{
Mathias Krause0225fb52012-08-30 01:30:16 +02002523 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002524 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002525 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002526 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002527 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002528 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002529 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002530
Avi Kivity1aa36612011-04-27 13:20:30 +03002531 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002532 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002533 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002534 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002535 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002536 base = get_desc_base(&tr_seg);
2537#ifdef CONFIG_X86_64
2538 base |= ((u64)base3) << 32;
2539#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002540 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002541 if (r != X86EMUL_CONTINUE)
2542 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002543 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002544 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002545 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002546 if (r != X86EMUL_CONTINUE)
2547 return false;
2548 if ((perm >> bit_idx) & mask)
2549 return false;
2550 return true;
2551}
2552
2553static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002554 u16 port, u16 len)
2555{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002556 if (ctxt->perm_ok)
2557 return true;
2558
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002559 if (emulator_bad_iopl(ctxt))
2560 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002561 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002562
2563 ctxt->perm_ok = true;
2564
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002565 return true;
2566}
2567
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002568static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002569 struct tss_segment_16 *tss)
2570{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002571 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002572 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002573 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2574 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2575 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2576 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2577 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2578 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2579 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2580 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002581
Avi Kivity1aa36612011-04-27 13:20:30 +03002582 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2583 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2584 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2585 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2586 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002587}
2588
2589static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002590 struct tss_segment_16 *tss)
2591{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002592 int ret;
2593
Avi Kivity9dac77f2011-06-01 15:34:25 +03002594 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002595 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002596 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2597 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2598 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2599 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2600 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2601 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2602 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2603 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002604
2605 /*
2606 * SDM says that segment selectors are loaded before segment
2607 * descriptors
2608 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002609 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2610 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2611 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2612 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2613 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002614
2615 /*
Guo Chaofc058682012-06-28 15:19:51 +08002616 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002617 * it is handled in a context of new task
2618 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002619 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002620 if (ret != X86EMUL_CONTINUE)
2621 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002622 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002623 if (ret != X86EMUL_CONTINUE)
2624 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002625 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002626 if (ret != X86EMUL_CONTINUE)
2627 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002628 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002629 if (ret != X86EMUL_CONTINUE)
2630 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002631 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002632 if (ret != X86EMUL_CONTINUE)
2633 return ret;
2634
2635 return X86EMUL_CONTINUE;
2636}
2637
2638static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002639 u16 tss_selector, u16 old_tss_sel,
2640 ulong old_tss_base, struct desc_struct *new_desc)
2641{
Mathias Krause0225fb52012-08-30 01:30:16 +02002642 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002643 struct tss_segment_16 tss_seg;
2644 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002645 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002646
Avi Kivity0f65dd72011-04-20 13:37:53 +03002647 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002648 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002649 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002650 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002651 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002652
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002653 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002654
Avi Kivity0f65dd72011-04-20 13:37:53 +03002655 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002656 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002657 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002658 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002659 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002660
Avi Kivity0f65dd72011-04-20 13:37:53 +03002661 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002662 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002663 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002664 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002665 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002666
2667 if (old_tss_sel != 0xffff) {
2668 tss_seg.prev_task_link = old_tss_sel;
2669
Avi Kivity0f65dd72011-04-20 13:37:53 +03002670 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002671 &tss_seg.prev_task_link,
2672 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002673 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002674 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002675 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002676 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002677 }
2678
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002679 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002680}
2681
2682static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002683 struct tss_segment_32 *tss)
2684{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002685 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002686 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002687 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002688 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2689 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2690 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2691 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2692 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2693 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2694 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2695 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002696
Avi Kivity1aa36612011-04-27 13:20:30 +03002697 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2698 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2699 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2700 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2701 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2702 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2703 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002704}
2705
2706static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002707 struct tss_segment_32 *tss)
2708{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002709 int ret;
2710
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002711 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002712 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002713 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002714 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002715
2716 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002717 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2718 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2719 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2720 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2721 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2722 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2723 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2724 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002725
2726 /*
2727 * SDM says that segment selectors are loaded before segment
2728 * descriptors
2729 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002730 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2731 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2732 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2733 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2734 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2735 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2736 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002737
2738 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002739 * If we're switching between Protected Mode and VM86, we need to make
2740 * sure to update the mode before loading the segment descriptors so
2741 * that the selectors are interpreted correctly.
2742 *
2743 * Need to get rflags to the vcpu struct immediately because it
2744 * influences the CPL which is checked at least when loading the segment
2745 * descriptors and when pushing an error code to the new kernel stack.
2746 *
2747 * TODO Introduce a separate ctxt->ops->set_cpl callback
2748 */
2749 if (ctxt->eflags & X86_EFLAGS_VM)
2750 ctxt->mode = X86EMUL_MODE_VM86;
2751 else
2752 ctxt->mode = X86EMUL_MODE_PROT32;
2753
2754 ctxt->ops->set_rflags(ctxt, ctxt->eflags);
2755
2756 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002757 * Now load segment descriptors. If fault happenes at this stage
2758 * it is handled in a context of new task
2759 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002760 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002761 if (ret != X86EMUL_CONTINUE)
2762 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002763 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002764 if (ret != X86EMUL_CONTINUE)
2765 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002766 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002767 if (ret != X86EMUL_CONTINUE)
2768 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002769 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002770 if (ret != X86EMUL_CONTINUE)
2771 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002772 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002773 if (ret != X86EMUL_CONTINUE)
2774 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002775 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002776 if (ret != X86EMUL_CONTINUE)
2777 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002778 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002779 if (ret != X86EMUL_CONTINUE)
2780 return ret;
2781
2782 return X86EMUL_CONTINUE;
2783}
2784
2785static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002786 u16 tss_selector, u16 old_tss_sel,
2787 ulong old_tss_base, struct desc_struct *new_desc)
2788{
Mathias Krause0225fb52012-08-30 01:30:16 +02002789 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002790 struct tss_segment_32 tss_seg;
2791 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002792 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002793
Avi Kivity0f65dd72011-04-20 13:37:53 +03002794 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002795 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002796 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002797 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002798 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002799
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002800 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002801
Avi Kivity0f65dd72011-04-20 13:37:53 +03002802 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002803 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002804 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002805 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002806 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002807
Avi Kivity0f65dd72011-04-20 13:37:53 +03002808 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002809 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002810 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002811 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002812 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002813
2814 if (old_tss_sel != 0xffff) {
2815 tss_seg.prev_task_link = old_tss_sel;
2816
Avi Kivity0f65dd72011-04-20 13:37:53 +03002817 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002818 &tss_seg.prev_task_link,
2819 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002820 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002821 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002822 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002823 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002824 }
2825
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002826 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002827}
2828
2829static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002830 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002831 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002832{
Mathias Krause0225fb52012-08-30 01:30:16 +02002833 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002834 struct desc_struct curr_tss_desc, next_tss_desc;
2835 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002836 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002837 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002838 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002839 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002840 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002841
2842 /* FIXME: old_tss_base == ~0 ? */
2843
Avi Kivitye9194642012-06-13 16:29:39 +03002844 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002845 if (ret != X86EMUL_CONTINUE)
2846 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002847 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002848 if (ret != X86EMUL_CONTINUE)
2849 return ret;
2850
2851 /* FIXME: check that next_tss_desc is tss */
2852
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002853 /*
2854 * Check privileges. The three cases are task switch caused by...
2855 *
2856 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2857 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002858 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002859 */
2860 if (reason == TASK_SWITCH_GATE) {
2861 if (idt_index != -1) {
2862 /* Software interrupts */
2863 struct desc_struct task_gate_desc;
2864 int dpl;
2865
2866 ret = read_interrupt_descriptor(ctxt, idt_index,
2867 &task_gate_desc);
2868 if (ret != X86EMUL_CONTINUE)
2869 return ret;
2870
2871 dpl = task_gate_desc.dpl;
2872 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2873 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2874 }
2875 } else if (reason != TASK_SWITCH_IRET) {
2876 int dpl = next_tss_desc.dpl;
2877 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2878 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002879 }
2880
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002881
Gleb Natapovceffb452010-03-18 15:20:19 +02002882 desc_limit = desc_limit_scaled(&next_tss_desc);
2883 if (!next_tss_desc.p ||
2884 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2885 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002886 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002887 return X86EMUL_PROPAGATE_FAULT;
2888 }
2889
2890 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2891 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002892 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002893 }
2894
2895 if (reason == TASK_SWITCH_IRET)
2896 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2897
2898 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002899 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002900 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2901 old_tss_sel = 0xffff;
2902
2903 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002904 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002905 old_tss_base, &next_tss_desc);
2906 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002907 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002908 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002909 if (ret != X86EMUL_CONTINUE)
2910 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002911
2912 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2913 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2914
2915 if (reason != TASK_SWITCH_IRET) {
2916 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002917 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002918 }
2919
Avi Kivity717746e2011-04-20 13:37:53 +03002920 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002921 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002922
Jan Kiszkae269fb22010-04-14 15:51:09 +02002923 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002924 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2925 ctxt->lock_prefix = 0;
2926 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002927 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002928 }
2929
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002930 return ret;
2931}
2932
2933int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002934 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002935 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002936{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002937 int rc;
2938
Avi Kivitydd856ef2012-08-27 23:46:17 +03002939 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002940 ctxt->_eip = ctxt->eip;
2941 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002942
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002943 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002944 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002945
Avi Kivitydd856ef2012-08-27 23:46:17 +03002946 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002947 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002948 writeback_registers(ctxt);
2949 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002950
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002951 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002952}
2953
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002954static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2955 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002956{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002957 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002958
Avi Kivitydd856ef2012-08-27 23:46:17 +03002959 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2960 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002961}
2962
Avi Kivity7af04fc2010-08-18 14:16:35 +03002963static int em_das(struct x86_emulate_ctxt *ctxt)
2964{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002965 u8 al, old_al;
2966 bool af, cf, old_cf;
2967
2968 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002969 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002970
2971 old_al = al;
2972 old_cf = cf;
2973 cf = false;
2974 af = ctxt->eflags & X86_EFLAGS_AF;
2975 if ((al & 0x0f) > 9 || af) {
2976 al -= 6;
2977 cf = old_cf | (al >= 250);
2978 af = true;
2979 } else {
2980 af = false;
2981 }
2982 if (old_al > 0x99 || old_cf) {
2983 al -= 0x60;
2984 cf = true;
2985 }
2986
Avi Kivity9dac77f2011-06-01 15:34:25 +03002987 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002988 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002989 ctxt->src.type = OP_IMM;
2990 ctxt->src.val = 0;
2991 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02002992 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03002993 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2994 if (cf)
2995 ctxt->eflags |= X86_EFLAGS_CF;
2996 if (af)
2997 ctxt->eflags |= X86_EFLAGS_AF;
2998 return X86EMUL_CONTINUE;
2999}
3000
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003001static int em_aam(struct x86_emulate_ctxt *ctxt)
3002{
3003 u8 al, ah;
3004
3005 if (ctxt->src.val == 0)
3006 return emulate_de(ctxt);
3007
3008 al = ctxt->dst.val & 0xff;
3009 ah = al / ctxt->src.val;
3010 al %= ctxt->src.val;
3011
3012 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
3013
3014 /* Set PF, ZF, SF */
3015 ctxt->src.type = OP_IMM;
3016 ctxt->src.val = 0;
3017 ctxt->src.bytes = 1;
3018 fastop(ctxt, em_or);
3019
3020 return X86EMUL_CONTINUE;
3021}
3022
Gleb Natapov7f662272012-12-10 11:42:30 +02003023static int em_aad(struct x86_emulate_ctxt *ctxt)
3024{
3025 u8 al = ctxt->dst.val & 0xff;
3026 u8 ah = (ctxt->dst.val >> 8) & 0xff;
3027
3028 al = (al + (ah * ctxt->src.val)) & 0xff;
3029
3030 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
3031
Gleb Natapovf583c292013-02-13 17:50:39 +02003032 /* Set PF, ZF, SF */
3033 ctxt->src.type = OP_IMM;
3034 ctxt->src.val = 0;
3035 ctxt->src.bytes = 1;
3036 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02003037
3038 return X86EMUL_CONTINUE;
3039}
3040
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003041static int em_call(struct x86_emulate_ctxt *ctxt)
3042{
3043 long rel = ctxt->src.val;
3044
3045 ctxt->src.val = (unsigned long)ctxt->_eip;
3046 jmp_rel(ctxt, rel);
3047 return em_push(ctxt);
3048}
3049
Avi Kivity0ef753b2010-08-18 14:51:45 +03003050static int em_call_far(struct x86_emulate_ctxt *ctxt)
3051{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003052 u16 sel, old_cs;
3053 ulong old_eip;
3054 int rc;
3055
Avi Kivity1aa36612011-04-27 13:20:30 +03003056 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003057 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003058
Avi Kivity9dac77f2011-06-01 15:34:25 +03003059 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003060 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03003061 return X86EMUL_CONTINUE;
3062
Avi Kivity9dac77f2011-06-01 15:34:25 +03003063 ctxt->_eip = 0;
3064 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003065
Avi Kivity9dac77f2011-06-01 15:34:25 +03003066 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003067 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003068 if (rc != X86EMUL_CONTINUE)
3069 return rc;
3070
Avi Kivity9dac77f2011-06-01 15:34:25 +03003071 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003072 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003073}
3074
Avi Kivity40ece7c2010-08-18 15:12:09 +03003075static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3076{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003077 int rc;
3078
Avi Kivity9dac77f2011-06-01 15:34:25 +03003079 ctxt->dst.type = OP_REG;
3080 ctxt->dst.addr.reg = &ctxt->_eip;
3081 ctxt->dst.bytes = ctxt->op_bytes;
3082 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003083 if (rc != X86EMUL_CONTINUE)
3084 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003085 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003086 return X86EMUL_CONTINUE;
3087}
3088
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003089static int em_xchg(struct x86_emulate_ctxt *ctxt)
3090{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003091 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003092 ctxt->src.val = ctxt->dst.val;
3093 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003094
3095 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003096 ctxt->dst.val = ctxt->src.orig_val;
3097 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003098 return X86EMUL_CONTINUE;
3099}
3100
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003101static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3102{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003103 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003104 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003105}
3106
Avi Kivity61429142010-08-19 15:13:00 +03003107static int em_cwd(struct x86_emulate_ctxt *ctxt)
3108{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003109 ctxt->dst.type = OP_REG;
3110 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003111 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003112 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003113
3114 return X86EMUL_CONTINUE;
3115}
3116
Avi Kivity48bb5d32010-08-18 18:54:34 +03003117static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3118{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003119 u64 tsc = 0;
3120
Avi Kivity717746e2011-04-20 13:37:53 +03003121 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003122 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3123 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003124 return X86EMUL_CONTINUE;
3125}
3126
Avi Kivity222d21a2011-11-10 14:57:30 +02003127static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3128{
3129 u64 pmc;
3130
Avi Kivitydd856ef2012-08-27 23:46:17 +03003131 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003132 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003133 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3134 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003135 return X86EMUL_CONTINUE;
3136}
3137
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003138static int em_mov(struct x86_emulate_ctxt *ctxt)
3139{
Stefan Hajnoczi49597d82012-04-09 18:40:00 +03003140 memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003141 return X86EMUL_CONTINUE;
3142}
3143
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003144static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3145{
3146 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3147 return emulate_gp(ctxt, 0);
3148
3149 /* Disable writeback. */
3150 ctxt->dst.type = OP_NONE;
3151 return X86EMUL_CONTINUE;
3152}
3153
3154static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3155{
3156 unsigned long val;
3157
3158 if (ctxt->mode == X86EMUL_MODE_PROT64)
3159 val = ctxt->src.val & ~0ULL;
3160 else
3161 val = ctxt->src.val & ~0U;
3162
3163 /* #UD condition is already handled. */
3164 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3165 return emulate_gp(ctxt, 0);
3166
3167 /* Disable writeback. */
3168 ctxt->dst.type = OP_NONE;
3169 return X86EMUL_CONTINUE;
3170}
3171
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003172static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3173{
3174 u64 msr_data;
3175
Avi Kivitydd856ef2012-08-27 23:46:17 +03003176 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3177 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3178 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003179 return emulate_gp(ctxt, 0);
3180
3181 return X86EMUL_CONTINUE;
3182}
3183
3184static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3185{
3186 u64 msr_data;
3187
Avi Kivitydd856ef2012-08-27 23:46:17 +03003188 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003189 return emulate_gp(ctxt, 0);
3190
Avi Kivitydd856ef2012-08-27 23:46:17 +03003191 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3192 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003193 return X86EMUL_CONTINUE;
3194}
3195
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003196static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3197{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003198 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003199 return emulate_ud(ctxt);
3200
Avi Kivity9dac77f2011-06-01 15:34:25 +03003201 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003202 return X86EMUL_CONTINUE;
3203}
3204
3205static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3206{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003207 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003208
Avi Kivity9dac77f2011-06-01 15:34:25 +03003209 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003210 return emulate_ud(ctxt);
3211
Avi Kivity9dac77f2011-06-01 15:34:25 +03003212 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003213 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3214
3215 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003216 ctxt->dst.type = OP_NONE;
3217 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003218}
3219
Avi Kivitya14e5792012-06-13 12:28:33 +03003220static int em_lldt(struct x86_emulate_ctxt *ctxt)
3221{
3222 u16 sel = ctxt->src.val;
3223
3224 /* Disable writeback. */
3225 ctxt->dst.type = OP_NONE;
3226 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3227}
3228
Avi Kivity80890002012-06-13 16:33:29 +03003229static int em_ltr(struct x86_emulate_ctxt *ctxt)
3230{
3231 u16 sel = ctxt->src.val;
3232
3233 /* Disable writeback. */
3234 ctxt->dst.type = OP_NONE;
3235 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3236}
3237
Avi Kivity38503912011-03-31 18:48:09 +02003238static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3239{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003240 int rc;
3241 ulong linear;
3242
Avi Kivity9dac77f2011-06-01 15:34:25 +03003243 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003244 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003245 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003246 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003247 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003248 return X86EMUL_CONTINUE;
3249}
3250
Avi Kivity2d04a052011-04-20 15:32:49 +03003251static int em_clts(struct x86_emulate_ctxt *ctxt)
3252{
3253 ulong cr0;
3254
3255 cr0 = ctxt->ops->get_cr(ctxt, 0);
3256 cr0 &= ~X86_CR0_TS;
3257 ctxt->ops->set_cr(ctxt, 0, cr0);
3258 return X86EMUL_CONTINUE;
3259}
3260
Avi Kivity26d05cc2011-04-21 12:07:59 +03003261static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3262{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003263 int rc;
3264
Avi Kivity9dac77f2011-06-01 15:34:25 +03003265 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003266 return X86EMUL_UNHANDLEABLE;
3267
3268 rc = ctxt->ops->fix_hypercall(ctxt);
3269 if (rc != X86EMUL_CONTINUE)
3270 return rc;
3271
3272 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003273 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003274 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003275 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003276 return X86EMUL_CONTINUE;
3277}
3278
Avi Kivity96051572012-06-10 17:21:18 +03003279static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3280 void (*get)(struct x86_emulate_ctxt *ctxt,
3281 struct desc_ptr *ptr))
3282{
3283 struct desc_ptr desc_ptr;
3284
3285 if (ctxt->mode == X86EMUL_MODE_PROT64)
3286 ctxt->op_bytes = 8;
3287 get(ctxt, &desc_ptr);
3288 if (ctxt->op_bytes == 2) {
3289 ctxt->op_bytes = 4;
3290 desc_ptr.address &= 0x00ffffff;
3291 }
3292 /* Disable writeback. */
3293 ctxt->dst.type = OP_NONE;
3294 return segmented_write(ctxt, ctxt->dst.addr.mem,
3295 &desc_ptr, 2 + ctxt->op_bytes);
3296}
3297
3298static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3299{
3300 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3301}
3302
3303static int em_sidt(struct x86_emulate_ctxt *ctxt)
3304{
3305 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3306}
3307
Avi Kivity26d05cc2011-04-21 12:07:59 +03003308static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3309{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003310 struct desc_ptr desc_ptr;
3311 int rc;
3312
Avi Kivity510425f2012-06-07 17:04:36 +03003313 if (ctxt->mode == X86EMUL_MODE_PROT64)
3314 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003315 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003316 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003317 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003318 if (rc != X86EMUL_CONTINUE)
3319 return rc;
3320 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3321 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003322 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003323 return X86EMUL_CONTINUE;
3324}
3325
Avi Kivity5ef39c72011-04-21 12:21:50 +03003326static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003327{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003328 int rc;
3329
Avi Kivity5ef39c72011-04-21 12:21:50 +03003330 rc = ctxt->ops->fix_hypercall(ctxt);
3331
Avi Kivity26d05cc2011-04-21 12:07:59 +03003332 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003333 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003334 return rc;
3335}
3336
3337static int em_lidt(struct x86_emulate_ctxt *ctxt)
3338{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003339 struct desc_ptr desc_ptr;
3340 int rc;
3341
Avi Kivity510425f2012-06-07 17:04:36 +03003342 if (ctxt->mode == X86EMUL_MODE_PROT64)
3343 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003344 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003345 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003346 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003347 if (rc != X86EMUL_CONTINUE)
3348 return rc;
3349 ctxt->ops->set_idt(ctxt, &desc_ptr);
3350 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003351 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003352 return X86EMUL_CONTINUE;
3353}
3354
3355static int em_smsw(struct x86_emulate_ctxt *ctxt)
3356{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003357 ctxt->dst.bytes = 2;
3358 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003359 return X86EMUL_CONTINUE;
3360}
3361
3362static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3363{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003364 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003365 | (ctxt->src.val & 0x0f));
3366 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003367 return X86EMUL_CONTINUE;
3368}
3369
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003370static int em_loop(struct x86_emulate_ctxt *ctxt)
3371{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003372 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3373 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003374 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3375 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003376
3377 return X86EMUL_CONTINUE;
3378}
3379
3380static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3381{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003382 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003383 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003384
3385 return X86EMUL_CONTINUE;
3386}
3387
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003388static int em_in(struct x86_emulate_ctxt *ctxt)
3389{
3390 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3391 &ctxt->dst.val))
3392 return X86EMUL_IO_NEEDED;
3393
3394 return X86EMUL_CONTINUE;
3395}
3396
3397static int em_out(struct x86_emulate_ctxt *ctxt)
3398{
3399 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3400 &ctxt->src.val, 1);
3401 /* Disable writeback. */
3402 ctxt->dst.type = OP_NONE;
3403 return X86EMUL_CONTINUE;
3404}
3405
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003406static int em_cli(struct x86_emulate_ctxt *ctxt)
3407{
3408 if (emulator_bad_iopl(ctxt))
3409 return emulate_gp(ctxt, 0);
3410
3411 ctxt->eflags &= ~X86_EFLAGS_IF;
3412 return X86EMUL_CONTINUE;
3413}
3414
3415static int em_sti(struct x86_emulate_ctxt *ctxt)
3416{
3417 if (emulator_bad_iopl(ctxt))
3418 return emulate_gp(ctxt, 0);
3419
3420 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3421 ctxt->eflags |= X86_EFLAGS_IF;
3422 return X86EMUL_CONTINUE;
3423}
3424
Avi Kivity6d6eede2012-06-07 14:11:36 +03003425static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3426{
3427 u32 eax, ebx, ecx, edx;
3428
Avi Kivitydd856ef2012-08-27 23:46:17 +03003429 eax = reg_read(ctxt, VCPU_REGS_RAX);
3430 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003431 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003432 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3433 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3434 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3435 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003436 return X86EMUL_CONTINUE;
3437}
3438
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003439static int em_lahf(struct x86_emulate_ctxt *ctxt)
3440{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003441 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3442 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003443 return X86EMUL_CONTINUE;
3444}
3445
Avi Kivity92998362012-06-13 12:25:06 +03003446static int em_bswap(struct x86_emulate_ctxt *ctxt)
3447{
3448 switch (ctxt->op_bytes) {
3449#ifdef CONFIG_X86_64
3450 case 8:
3451 asm("bswap %0" : "+r"(ctxt->dst.val));
3452 break;
3453#endif
3454 default:
3455 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3456 break;
3457 }
3458 return X86EMUL_CONTINUE;
3459}
3460
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003461static bool valid_cr(int nr)
3462{
3463 switch (nr) {
3464 case 0:
3465 case 2 ... 4:
3466 case 8:
3467 return true;
3468 default:
3469 return false;
3470 }
3471}
3472
3473static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3474{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003475 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003476 return emulate_ud(ctxt);
3477
3478 return X86EMUL_CONTINUE;
3479}
3480
3481static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3482{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003483 u64 new_val = ctxt->src.val64;
3484 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003485 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003486
3487 static u64 cr_reserved_bits[] = {
3488 0xffffffff00000000ULL,
3489 0, 0, 0, /* CR3 checked later */
3490 CR4_RESERVED_BITS,
3491 0, 0, 0,
3492 CR8_RESERVED_BITS,
3493 };
3494
3495 if (!valid_cr(cr))
3496 return emulate_ud(ctxt);
3497
3498 if (new_val & cr_reserved_bits[cr])
3499 return emulate_gp(ctxt, 0);
3500
3501 switch (cr) {
3502 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003503 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003504 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3505 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3506 return emulate_gp(ctxt, 0);
3507
Avi Kivity717746e2011-04-20 13:37:53 +03003508 cr4 = ctxt->ops->get_cr(ctxt, 4);
3509 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003510
3511 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3512 !(cr4 & X86_CR4_PAE))
3513 return emulate_gp(ctxt, 0);
3514
3515 break;
3516 }
3517 case 3: {
3518 u64 rsvd = 0;
3519
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003520 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3521 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003522 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003523 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003524 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003525 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003526 rsvd = CR3_NONPAE_RESERVED_BITS;
3527
3528 if (new_val & rsvd)
3529 return emulate_gp(ctxt, 0);
3530
3531 break;
3532 }
3533 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003534 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003535
3536 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3537 return emulate_gp(ctxt, 0);
3538
3539 break;
3540 }
3541 }
3542
3543 return X86EMUL_CONTINUE;
3544}
3545
Joerg Roedel3b88e412011-04-04 12:39:29 +02003546static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3547{
3548 unsigned long dr7;
3549
Avi Kivity717746e2011-04-20 13:37:53 +03003550 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003551
3552 /* Check if DR7.Global_Enable is set */
3553 return dr7 & (1 << 13);
3554}
3555
3556static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3557{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003558 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003559 u64 cr4;
3560
3561 if (dr > 7)
3562 return emulate_ud(ctxt);
3563
Avi Kivity717746e2011-04-20 13:37:53 +03003564 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003565 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3566 return emulate_ud(ctxt);
3567
3568 if (check_dr7_gd(ctxt))
3569 return emulate_db(ctxt);
3570
3571 return X86EMUL_CONTINUE;
3572}
3573
3574static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3575{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003576 u64 new_val = ctxt->src.val64;
3577 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003578
3579 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3580 return emulate_gp(ctxt, 0);
3581
3582 return check_dr_read(ctxt);
3583}
3584
Joerg Roedel01de8b02011-04-04 12:39:31 +02003585static int check_svme(struct x86_emulate_ctxt *ctxt)
3586{
3587 u64 efer;
3588
Avi Kivity717746e2011-04-20 13:37:53 +03003589 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003590
3591 if (!(efer & EFER_SVME))
3592 return emulate_ud(ctxt);
3593
3594 return X86EMUL_CONTINUE;
3595}
3596
3597static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3598{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003599 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003600
3601 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003602 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003603 return emulate_gp(ctxt, 0);
3604
3605 return check_svme(ctxt);
3606}
3607
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003608static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3609{
Avi Kivity717746e2011-04-20 13:37:53 +03003610 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003611
Avi Kivity717746e2011-04-20 13:37:53 +03003612 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003613 return emulate_ud(ctxt);
3614
3615 return X86EMUL_CONTINUE;
3616}
3617
Joerg Roedel80612522011-04-04 12:39:33 +02003618static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3619{
Avi Kivity717746e2011-04-20 13:37:53 +03003620 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003621 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003622
Avi Kivity717746e2011-04-20 13:37:53 +03003623 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02003624 (rcx > 3))
3625 return emulate_gp(ctxt, 0);
3626
3627 return X86EMUL_CONTINUE;
3628}
3629
Joerg Roedelf6511932011-04-04 12:39:35 +02003630static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3631{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003632 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3633 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003634 return emulate_gp(ctxt, 0);
3635
3636 return X86EMUL_CONTINUE;
3637}
3638
3639static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3640{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003641 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3642 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003643 return emulate_gp(ctxt, 0);
3644
3645 return X86EMUL_CONTINUE;
3646}
3647
Avi Kivity73fba5f2010-07-29 15:11:53 +03003648#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003649#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003650#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3651 .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003652#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003653#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003654#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3655#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003656#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003657#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003658#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003659#define II(_f, _e, _i) \
3660 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003661#define IIP(_f, _e, _i, _p) \
3662 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3663 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003664#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003665
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003666#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003667#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003668#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003669#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003670#define I2bvIP(_f, _e, _i, _p) \
3671 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003672
Avi Kivityfb864fb2013-01-04 16:18:54 +02003673#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3674 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3675 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003676
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003677static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003678 DI(SrcNone | Priv, monitor),
3679 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003680 N, N, N, N, N, N,
3681};
3682
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003683static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003684 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
3685 II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall),
3686 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3687 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3688 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3689 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3690 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3691 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003692};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003693
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003694static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003695 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003696 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003697 N, N, N, N, N, N,
3698};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003699
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003700static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003701 F(Lock, em_add),
3702 F(Lock | PageTable, em_or),
3703 F(Lock, em_adc),
3704 F(Lock, em_sbb),
3705 F(Lock | PageTable, em_and),
3706 F(Lock, em_sub),
3707 F(Lock, em_xor),
3708 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003709};
3710
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003711static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003712 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003713};
3714
Avi Kivity007a3b52013-01-19 19:51:51 +02003715static const struct opcode group2[] = {
3716 F(DstMem | ModRM, em_rol),
3717 F(DstMem | ModRM, em_ror),
3718 F(DstMem | ModRM, em_rcl),
3719 F(DstMem | ModRM, em_rcr),
3720 F(DstMem | ModRM, em_shl),
3721 F(DstMem | ModRM, em_shr),
3722 F(DstMem | ModRM, em_shl),
3723 F(DstMem | ModRM, em_sar),
3724};
3725
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003726static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003727 F(DstMem | SrcImm | NoWrite, em_test),
3728 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003729 F(DstMem | SrcNone | Lock, em_not),
3730 F(DstMem | SrcNone | Lock, em_neg),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003731 I(SrcMem, em_mul_ex),
3732 I(SrcMem, em_imul_ex),
3733 I(SrcMem, em_div_ex),
3734 I(SrcMem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003735};
3736
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003737static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003738 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3739 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003740 N, N, N, N, N, N,
3741};
3742
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003743static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003744 F(DstMem | SrcNone | Lock, em_inc),
3745 F(DstMem | SrcNone | Lock, em_dec),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003746 I(SrcMem | Stack, em_grp45),
3747 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3748 I(SrcMem | Stack, em_grp45),
3749 I(SrcMemFAddr | ImplicitOps, em_grp45),
Gleb Natapov188424b2013-04-11 12:32:14 +03003750 I(SrcMem | Stack, em_grp45), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003751};
3752
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003753static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003754 DI(Prot, sldt),
3755 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003756 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003757 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003758 N, N, N, N,
3759};
3760
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003761static const struct group_dual group7 = { {
Avi Kivity96051572012-06-10 17:21:18 +03003762 II(Mov | DstMem | Priv, em_sgdt, sgdt),
3763 II(Mov | DstMem | Priv, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003764 II(SrcMem | Priv, em_lgdt, lgdt),
3765 II(SrcMem | Priv, em_lidt, lidt),
3766 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3767 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3768 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003769}, {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003770 I(SrcNone | Priv | VendorSpecific, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003771 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003772 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003773 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3774 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3775 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003776} };
3777
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003778static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003779 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003780 F(DstMem | SrcImmByte | NoWrite, em_bt),
3781 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3782 F(DstMem | SrcImmByte | Lock, em_btr),
3783 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003784};
3785
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003786static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003787 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003788}, {
3789 N, N, N, N, N, N, N, N,
3790} };
3791
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003792static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003793 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003794 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003795};
3796
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003797static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003798 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003799};
3800
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003801static const struct gprefix pfx_vmovntpx = {
Avi Kivity3e114eb2012-04-09 18:40:01 +03003802 I(0, em_mov), N, N, N,
3803};
3804
Gleb Natapov045a2822012-12-20 16:57:43 +02003805static const struct escape escape_d9 = { {
3806 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3807}, {
3808 /* 0xC0 - 0xC7 */
3809 N, N, N, N, N, N, N, N,
3810 /* 0xC8 - 0xCF */
3811 N, N, N, N, N, N, N, N,
3812 /* 0xD0 - 0xC7 */
3813 N, N, N, N, N, N, N, N,
3814 /* 0xD8 - 0xDF */
3815 N, N, N, N, N, N, N, N,
3816 /* 0xE0 - 0xE7 */
3817 N, N, N, N, N, N, N, N,
3818 /* 0xE8 - 0xEF */
3819 N, N, N, N, N, N, N, N,
3820 /* 0xF0 - 0xF7 */
3821 N, N, N, N, N, N, N, N,
3822 /* 0xF8 - 0xFF */
3823 N, N, N, N, N, N, N, N,
3824} };
3825
3826static const struct escape escape_db = { {
3827 N, N, N, N, N, N, N, N,
3828}, {
3829 /* 0xC0 - 0xC7 */
3830 N, N, N, N, N, N, N, N,
3831 /* 0xC8 - 0xCF */
3832 N, N, N, N, N, N, N, N,
3833 /* 0xD0 - 0xC7 */
3834 N, N, N, N, N, N, N, N,
3835 /* 0xD8 - 0xDF */
3836 N, N, N, N, N, N, N, N,
3837 /* 0xE0 - 0xE7 */
3838 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3839 /* 0xE8 - 0xEF */
3840 N, N, N, N, N, N, N, N,
3841 /* 0xF0 - 0xF7 */
3842 N, N, N, N, N, N, N, N,
3843 /* 0xF8 - 0xFF */
3844 N, N, N, N, N, N, N, N,
3845} };
3846
3847static const struct escape escape_dd = { {
3848 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3849}, {
3850 /* 0xC0 - 0xC7 */
3851 N, N, N, N, N, N, N, N,
3852 /* 0xC8 - 0xCF */
3853 N, N, N, N, N, N, N, N,
3854 /* 0xD0 - 0xC7 */
3855 N, N, N, N, N, N, N, N,
3856 /* 0xD8 - 0xDF */
3857 N, N, N, N, N, N, N, N,
3858 /* 0xE0 - 0xE7 */
3859 N, N, N, N, N, N, N, N,
3860 /* 0xE8 - 0xEF */
3861 N, N, N, N, N, N, N, N,
3862 /* 0xF0 - 0xF7 */
3863 N, N, N, N, N, N, N, N,
3864 /* 0xF8 - 0xFF */
3865 N, N, N, N, N, N, N, N,
3866} };
3867
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003868static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003869 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003870 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003871 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3872 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003873 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003874 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003875 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3876 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003877 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003878 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003879 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3880 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003881 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003882 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003883 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3884 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003885 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003886 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003887 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003888 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003889 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003890 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003891 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003892 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003893 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02003894 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003895 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003896 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003897 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003898 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003899 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003900 I(ImplicitOps | Stack | No64, em_pusha),
3901 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003902 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3903 N, N, N, N,
3904 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003905 I(SrcImm | Mov | Stack, em_push),
3906 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003907 I(SrcImmByte | Mov | Stack, em_push),
3908 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003909 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003910 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003911 /* 0x70 - 0x7F */
3912 X16(D(SrcImmByte)),
3913 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003914 G(ByteOp | DstMem | SrcImm, group1),
3915 G(DstMem | SrcImm, group1),
3916 G(ByteOp | DstMem | SrcImm | No64, group1),
3917 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003918 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003919 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003920 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003921 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003922 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003923 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003924 D(ModRM | SrcMem | NoAccess | DstReg),
3925 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3926 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003927 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003928 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003929 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003930 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003931 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003932 II(ImplicitOps | Stack, em_pushf, pushf),
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003933 II(ImplicitOps | Stack, em_popf, popf), N, I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003934 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003935 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003936 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003937 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003938 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003939 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003940 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003941 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3942 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003943 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003944 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003945 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003946 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02003947 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003948 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003949 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003950 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003951 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003952 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3953 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003954 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003955 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003956 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
3957 N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003958 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003959 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003960 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003961 G(Src2One | ByteOp, group2), G(Src2One, group2),
3962 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003963 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02003964 I(DstAcc | SrcImmUByte | No64, em_aad), N,
3965 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003966 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02003967 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003968 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003969 X3(I(SrcImmByte, em_loop)),
3970 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003971 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3972 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003973 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003974 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003975 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003976 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3977 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003978 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003979 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003980 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3981 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003982 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003983 D(ImplicitOps), D(ImplicitOps),
3984 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003985 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3986};
3987
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003988static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003989 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003990 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003991 N, I(ImplicitOps | VendorSpecific, em_syscall),
3992 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003993 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003994 N, D(ImplicitOps | ModRM), N, N,
3995 /* 0x10 - 0x1F */
3996 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3997 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003998 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003999 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09004000 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
4001 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004002 N, N, N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03004003 N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
4004 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004005 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004006 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004007 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004008 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004009 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004010 I(ImplicitOps | VendorSpecific, em_sysenter),
4011 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004012 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004013 N, N, N, N, N, N, N, N,
4014 /* 0x40 - 0x4F */
4015 X16(D(DstReg | SrcMem | ModRM | Mov)),
4016 /* 0x50 - 0x5F */
4017 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4018 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004019 N, N, N, N,
4020 N, N, N, N,
4021 N, N, N, N,
4022 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004023 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004024 N, N, N, N,
4025 N, N, N, N,
4026 N, N, N, N,
4027 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004028 /* 0x80 - 0x8F */
4029 X16(D(SrcImm)),
4030 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004031 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004032 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004033 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004034 II(ImplicitOps, em_cpuid, cpuid),
4035 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004036 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4037 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004038 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004039 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004040 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004041 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004042 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4043 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Avi Kivity4d758342013-01-19 19:51:55 +02004044 D(ModRM), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004045 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09004046 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004047 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004048 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004049 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4050 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004051 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004052 /* 0xB8 - 0xBF */
4053 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004054 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004055 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
4056 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004057 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004058 /* 0xC0 - 0xC7 */
Avi Kivity739ae402010-08-26 11:56:13 +03004059 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08004060 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004061 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004062 /* 0xC8 - 0xCF */
4063 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004064 /* 0xD0 - 0xDF */
4065 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4066 /* 0xE0 - 0xEF */
4067 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4068 /* 0xF0 - 0xFF */
4069 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4070};
4071
4072#undef D
4073#undef N
4074#undef G
4075#undef GD
4076#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004077#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004078#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004079
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004080#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004081#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004082#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004083#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004084#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004085
Avi Kivity9dac77f2011-06-01 15:34:25 +03004086static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004087{
4088 unsigned size;
4089
Avi Kivity9dac77f2011-06-01 15:34:25 +03004090 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004091 if (size == 8)
4092 size = 4;
4093 return size;
4094}
4095
4096static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4097 unsigned size, bool sign_extension)
4098{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004099 int rc = X86EMUL_CONTINUE;
4100
4101 op->type = OP_IMM;
4102 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004103 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004104 /* NB. Immediates are sign-extended as necessary. */
4105 switch (op->bytes) {
4106 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004107 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004108 break;
4109 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004110 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004111 break;
4112 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004113 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004114 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004115 case 8:
4116 op->val = insn_fetch(s64, ctxt);
4117 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004118 }
4119 if (!sign_extension) {
4120 switch (op->bytes) {
4121 case 1:
4122 op->val &= 0xff;
4123 break;
4124 case 2:
4125 op->val &= 0xffff;
4126 break;
4127 case 4:
4128 op->val &= 0xffffffff;
4129 break;
4130 }
4131 }
4132done:
4133 return rc;
4134}
4135
Avi Kivitya9945542011-09-13 10:45:41 +03004136static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4137 unsigned d)
4138{
4139 int rc = X86EMUL_CONTINUE;
4140
4141 switch (d) {
4142 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004143 decode_register_operand(ctxt, op);
Avi Kivitya9945542011-09-13 10:45:41 +03004144 break;
4145 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004146 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03004147 break;
4148 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004149 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004150 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03004151 *op = ctxt->memop;
4152 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03004153 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya9945542011-09-13 10:45:41 +03004154 fetch_bit_operand(ctxt);
4155 op->orig_val = op->val;
4156 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004157 case OpMem64:
4158 ctxt->memop.bytes = 8;
4159 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03004160 case OpAcc:
4161 op->type = OP_REG;
4162 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004163 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya9945542011-09-13 10:45:41 +03004164 fetch_register_operand(op);
4165 op->orig_val = op->val;
4166 break;
4167 case OpDI:
4168 op->type = OP_MEM;
4169 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4170 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004171 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya9945542011-09-13 10:45:41 +03004172 op->addr.mem.seg = VCPU_SREG_ES;
4173 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004174 op->count = 1;
Avi Kivitya9945542011-09-13 10:45:41 +03004175 break;
4176 case OpDX:
4177 op->type = OP_REG;
4178 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004179 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya9945542011-09-13 10:45:41 +03004180 fetch_register_operand(op);
4181 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004182 case OpCL:
4183 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004184 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004185 break;
4186 case OpImmByte:
4187 rc = decode_imm(ctxt, op, 1, true);
4188 break;
4189 case OpOne:
4190 op->bytes = 1;
4191 op->val = 1;
4192 break;
4193 case OpImm:
4194 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4195 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004196 case OpImm64:
4197 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4198 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004199 case OpMem8:
4200 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004201 if (ctxt->memop.type == OP_REG) {
4202 ctxt->memop.addr.reg = decode_register(ctxt, ctxt->modrm_rm, 1);
4203 fetch_register_operand(&ctxt->memop);
4204 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004205 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004206 case OpMem16:
4207 ctxt->memop.bytes = 2;
4208 goto mem_common;
4209 case OpMem32:
4210 ctxt->memop.bytes = 4;
4211 goto mem_common;
4212 case OpImmU16:
4213 rc = decode_imm(ctxt, op, 2, false);
4214 break;
4215 case OpImmU:
4216 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4217 break;
4218 case OpSI:
4219 op->type = OP_MEM;
4220 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4221 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004222 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Avi Kivity0fe59122011-09-13 10:45:47 +03004223 op->addr.mem.seg = seg_override(ctxt);
4224 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004225 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004226 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004227 case OpXLat:
4228 op->type = OP_MEM;
4229 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4230 op->addr.mem.ea =
4231 register_address(ctxt,
4232 reg_read(ctxt, VCPU_REGS_RBX) +
4233 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
4234 op->addr.mem.seg = seg_override(ctxt);
4235 op->val = 0;
4236 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004237 case OpImmFAddr:
4238 op->type = OP_IMM;
4239 op->addr.mem.ea = ctxt->_eip;
4240 op->bytes = ctxt->op_bytes + 2;
4241 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4242 break;
4243 case OpMemFAddr:
4244 ctxt->memop.bytes = ctxt->op_bytes + 2;
4245 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004246 case OpES:
4247 op->val = VCPU_SREG_ES;
4248 break;
4249 case OpCS:
4250 op->val = VCPU_SREG_CS;
4251 break;
4252 case OpSS:
4253 op->val = VCPU_SREG_SS;
4254 break;
4255 case OpDS:
4256 op->val = VCPU_SREG_DS;
4257 break;
4258 case OpFS:
4259 op->val = VCPU_SREG_FS;
4260 break;
4261 case OpGS:
4262 op->val = VCPU_SREG_GS;
4263 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004264 case OpImplicit:
4265 /* Special instructions do their own operand decoding. */
4266 default:
4267 op->type = OP_NONE; /* Disable writeback. */
4268 break;
4269 }
4270
4271done:
4272 return rc;
4273}
4274
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004275int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004276{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004277 int rc = X86EMUL_CONTINUE;
4278 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004279 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004280 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03004281 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004282
Avi Kivityf09ed832011-09-13 10:45:40 +03004283 ctxt->memop.type = OP_NONE;
4284 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004285 ctxt->_eip = ctxt->eip;
4286 ctxt->fetch.start = ctxt->_eip;
4287 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01004288 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004289 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004290
4291 switch (mode) {
4292 case X86EMUL_MODE_REAL:
4293 case X86EMUL_MODE_VM86:
4294 case X86EMUL_MODE_PROT16:
4295 def_op_bytes = def_ad_bytes = 2;
4296 break;
4297 case X86EMUL_MODE_PROT32:
4298 def_op_bytes = def_ad_bytes = 4;
4299 break;
4300#ifdef CONFIG_X86_64
4301 case X86EMUL_MODE_PROT64:
4302 def_op_bytes = 4;
4303 def_ad_bytes = 8;
4304 break;
4305#endif
4306 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004307 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004308 }
4309
Avi Kivity9dac77f2011-06-01 15:34:25 +03004310 ctxt->op_bytes = def_op_bytes;
4311 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004312
4313 /* Legacy prefixes. */
4314 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004315 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004316 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004317 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004318 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004319 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004320 break;
4321 case 0x67: /* address-size override */
4322 if (mode == X86EMUL_MODE_PROT64)
4323 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004324 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004325 else
4326 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004327 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004328 break;
4329 case 0x26: /* ES override */
4330 case 0x2e: /* CS override */
4331 case 0x36: /* SS override */
4332 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004333 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004334 break;
4335 case 0x64: /* FS override */
4336 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004337 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004338 break;
4339 case 0x40 ... 0x4f: /* REX */
4340 if (mode != X86EMUL_MODE_PROT64)
4341 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004342 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004343 continue;
4344 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004345 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004346 break;
4347 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004348 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004349 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004350 break;
4351 default:
4352 goto done_prefixes;
4353 }
4354
4355 /* Any legacy prefix after a REX prefix nullifies its effect. */
4356
Avi Kivity9dac77f2011-06-01 15:34:25 +03004357 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004358 }
4359
4360done_prefixes:
4361
4362 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004363 if (ctxt->rex_prefix & 8)
4364 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004365
4366 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004367 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004368 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004369 if (ctxt->b == 0x0f) {
4370 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004371 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004372 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004373 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004374 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004375
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004376 if (ctxt->d & ModRM)
4377 ctxt->modrm = insn_fetch(u8, ctxt);
4378
Avi Kivity9dac77f2011-06-01 15:34:25 +03004379 while (ctxt->d & GroupMask) {
4380 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004381 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004382 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004383 opcode = opcode.u.group[goffset];
4384 break;
4385 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004386 goffset = (ctxt->modrm >> 3) & 7;
4387 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004388 opcode = opcode.u.gdual->mod3[goffset];
4389 else
4390 opcode = opcode.u.gdual->mod012[goffset];
4391 break;
4392 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004393 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004394 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004395 break;
4396 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004397 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004398 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004399 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004400 switch (simd_prefix) {
4401 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4402 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4403 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4404 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4405 }
4406 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004407 case Escape:
4408 if (ctxt->modrm > 0xbf)
4409 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4410 else
4411 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4412 break;
Avi Kivity46561642011-04-24 14:09:59 +03004413 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004414 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004415 }
Avi Kivity46561642011-04-24 14:09:59 +03004416
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004417 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004418 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004419 }
4420
Avi Kivity9dac77f2011-06-01 15:34:25 +03004421 ctxt->execute = opcode.u.execute;
4422 ctxt->check_perm = opcode.check_perm;
4423 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004424
4425 /* Unrecognised? */
Gleb Natapov1146a782013-04-11 12:30:01 +03004426 if (ctxt->d == 0 || (ctxt->d & NotImpl))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004427 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004428
Avi Kivity9dac77f2011-06-01 15:34:25 +03004429 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004430 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004431
Avi Kivity9dac77f2011-06-01 15:34:25 +03004432 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
4433 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004434
Avi Kivity9dac77f2011-06-01 15:34:25 +03004435 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004436 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004437 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004438 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03004439 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004440 }
4441
Avi Kivity9dac77f2011-06-01 15:34:25 +03004442 if (ctxt->d & Sse)
4443 ctxt->op_bytes = 16;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004444 else if (ctxt->d & Mmx)
4445 ctxt->op_bytes = 8;
Avi Kivity12537912011-03-29 11:41:27 +02004446
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004447 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004448 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004449 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004450 if (!ctxt->has_seg_override)
4451 set_seg_override(ctxt, ctxt->modrm_seg);
4452 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004453 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004454 if (rc != X86EMUL_CONTINUE)
4455 goto done;
4456
Avi Kivity9dac77f2011-06-01 15:34:25 +03004457 if (!ctxt->has_seg_override)
4458 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004459
Avi Kivityf09ed832011-09-13 10:45:40 +03004460 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004461
Avi Kivityf09ed832011-09-13 10:45:40 +03004462 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
4463 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004464
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004465 /*
4466 * Decode and fetch the source operand: register, memory
4467 * or immediate.
4468 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004469 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004470 if (rc != X86EMUL_CONTINUE)
4471 goto done;
4472
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004473 /*
4474 * Decode and fetch the second source operand: register, memory
4475 * or immediate.
4476 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004477 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004478 if (rc != X86EMUL_CONTINUE)
4479 goto done;
4480
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004481 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03004482 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004483
4484done:
Avi Kivityf09ed832011-09-13 10:45:40 +03004485 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
4486 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004487
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004488 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004489}
4490
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004491bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4492{
4493 return ctxt->d & PageTable;
4494}
4495
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004496static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4497{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004498 /* The second termination condition only applies for REPE
4499 * and REPNE. Test if the repeat string operation prefix is
4500 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4501 * corresponding termination condition according to:
4502 * - if REPE/REPZ and ZF = 0 then done
4503 * - if REPNE/REPNZ and ZF = 1 then done
4504 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004505 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4506 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4507 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004508 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004509 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004510 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4511 return true;
4512
4513 return false;
4514}
4515
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004516static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4517{
4518 bool fault = false;
4519
4520 ctxt->ops->get_fpu(ctxt);
4521 asm volatile("1: fwait \n\t"
4522 "2: \n\t"
4523 ".pushsection .fixup,\"ax\" \n\t"
4524 "3: \n\t"
4525 "movb $1, %[fault] \n\t"
4526 "jmp 2b \n\t"
4527 ".popsection \n\t"
4528 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004529 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004530 ctxt->ops->put_fpu(ctxt);
4531
4532 if (unlikely(fault))
4533 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4534
4535 return X86EMUL_CONTINUE;
4536}
4537
4538static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4539 struct operand *op)
4540{
4541 if (op->type == OP_MM)
4542 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4543}
4544
Avi Kivitye28bbd42013-01-04 16:18:48 +02004545static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4546{
4547 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
4548 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
4549 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
4550 : "+a"(ctxt->dst.val), "+b"(ctxt->src.val), [flags]"+D"(flags)
4551 : "c"(ctxt->src2.val), [fastop]"S"(fop));
4552 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
4553 return X86EMUL_CONTINUE;
4554}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004555
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004556int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004557{
Mathias Krause0225fb52012-08-30 01:30:16 +02004558 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004559 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004560 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004561
Avi Kivity9dac77f2011-06-01 15:34:25 +03004562 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004563
Gleb Natapov1146a782013-04-11 12:30:01 +03004564 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4565 (ctxt->d & Undefined)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004566 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02004567 goto done;
4568 }
4569
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004570 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004571 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004572 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004573 goto done;
4574 }
4575
Avi Kivity9dac77f2011-06-01 15:34:25 +03004576 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004577 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004578 goto done;
4579 }
4580
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004581 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4582 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02004583 rc = emulate_ud(ctxt);
4584 goto done;
4585 }
4586
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004587 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02004588 rc = emulate_nm(ctxt);
4589 goto done;
4590 }
4591
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004592 if (ctxt->d & Mmx) {
4593 rc = flush_pending_x87_faults(ctxt);
4594 if (rc != X86EMUL_CONTINUE)
4595 goto done;
4596 /*
4597 * Now that we know the fpu is exception safe, we can fetch
4598 * operands from it.
4599 */
4600 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4601 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4602 if (!(ctxt->d & Mov))
4603 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4604 }
4605
Avi Kivity9dac77f2011-06-01 15:34:25 +03004606 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4607 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004608 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004609 if (rc != X86EMUL_CONTINUE)
4610 goto done;
4611 }
4612
Gleb Natapove92805a2010-02-10 14:21:35 +02004613 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004614 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004615 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02004616 goto done;
4617 }
4618
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004619 /* Instruction can only be executed in protected mode */
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03004620 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004621 rc = emulate_ud(ctxt);
4622 goto done;
4623 }
4624
Joerg Roedeld09beab2011-04-04 12:39:25 +02004625 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004626 if (ctxt->check_perm) {
4627 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004628 if (rc != X86EMUL_CONTINUE)
4629 goto done;
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_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004635 if (rc != X86EMUL_CONTINUE)
4636 goto done;
4637 }
4638
Avi Kivity9dac77f2011-06-01 15:34:25 +03004639 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004640 /* All REP prefixes have the same first termination condition */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004641 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004642 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004643 goto done;
4644 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004645 }
4646
Avi Kivity9dac77f2011-06-01 15:34:25 +03004647 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4648 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4649 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004650 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004651 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004652 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004653 }
4654
Avi Kivity9dac77f2011-06-01 15:34:25 +03004655 if (ctxt->src2.type == OP_MEM) {
4656 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4657 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004658 if (rc != X86EMUL_CONTINUE)
4659 goto done;
4660 }
4661
Avi Kivity9dac77f2011-06-01 15:34:25 +03004662 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004663 goto special_insn;
4664
4665
Avi Kivity9dac77f2011-06-01 15:34:25 +03004666 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004667 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004668 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4669 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004670 if (rc != X86EMUL_CONTINUE)
4671 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004672 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004673 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004674
Avi Kivity018a98d2007-11-27 19:30:56 +02004675special_insn:
4676
Avi Kivity9dac77f2011-06-01 15:34:25 +03004677 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4678 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004679 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004680 if (rc != X86EMUL_CONTINUE)
4681 goto done;
4682 }
4683
Avi Kivity9dac77f2011-06-01 15:34:25 +03004684 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004685 if (ctxt->d & Fastop) {
4686 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4687 rc = fastop(ctxt, fop);
4688 if (rc != X86EMUL_CONTINUE)
4689 goto done;
4690 goto writeback;
4691 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004692 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004693 if (rc != X86EMUL_CONTINUE)
4694 goto done;
4695 goto writeback;
4696 }
4697
Avi Kivity9dac77f2011-06-01 15:34:25 +03004698 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004699 goto twobyte_insn;
4700
Avi Kivity9dac77f2011-06-01 15:34:25 +03004701 switch (ctxt->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004702 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004703 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004704 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004705 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004706 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004707 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004708 if (test_cc(ctxt->b, ctxt->eflags))
4709 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004710 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004711 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004712 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004713 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004714 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004715 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Mohammed Gamal34698d82010-08-04 14:41:04 +03004716 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004717 rc = em_xchg(ctxt);
4718 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004719 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004720 switch (ctxt->op_bytes) {
4721 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4722 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4723 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004724 }
4725 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004726 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004727 rc = emulate_int(ctxt, 3);
4728 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004729 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004730 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004731 break;
4732 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004733 if (ctxt->eflags & EFLG_OF)
4734 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004735 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004736 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004737 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004738 jmp_rel(ctxt, ctxt->src.val);
4739 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004740 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004741 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004742 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004743 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004744 case 0xf5: /* cmc */
4745 /* complement carry flag from eflags reg */
4746 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004747 break;
4748 case 0xf8: /* clc */
4749 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004750 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004751 case 0xf9: /* stc */
4752 ctxt->eflags |= EFLG_CF;
4753 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004754 case 0xfc: /* cld */
4755 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004756 break;
4757 case 0xfd: /* std */
4758 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004759 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004760 default:
4761 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004762 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004763
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004764 if (rc != X86EMUL_CONTINUE)
4765 goto done;
4766
Avi Kivity018a98d2007-11-27 19:30:56 +02004767writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09004768 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004769 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02004770 goto done;
4771
Gleb Natapov5cd21912010-03-18 15:20:26 +02004772 /*
4773 * restore dst type in case the decoding will be reused
4774 * (happens for string instruction )
4775 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004776 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004777
Avi Kivity9dac77f2011-06-01 15:34:25 +03004778 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004779 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004780
Avi Kivity9dac77f2011-06-01 15:34:25 +03004781 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004782 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004783
Avi Kivity9dac77f2011-06-01 15:34:25 +03004784 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004785 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004786 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004787 if ((ctxt->d & SrcMask) == SrcSI)
4788 count = ctxt->src.count;
4789 else
4790 count = ctxt->dst.count;
4791 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4792 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004793
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004794 if (!string_insn_completed(ctxt)) {
4795 /*
4796 * Re-enter guest when pio read ahead buffer is empty
4797 * or, if it is not used, after each 1024 iteration.
4798 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004799 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004800 (r->end == 0 || r->end != r->pos)) {
4801 /*
4802 * Reset read cache. Usually happens before
4803 * decode, but since instruction is restarted
4804 * we have to do it here.
4805 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004806 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004807 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004808 return EMULATION_RESTART;
4809 }
4810 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004811 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004812 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004813
Avi Kivity9dac77f2011-06-01 15:34:25 +03004814 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004815
4816done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004817 if (rc == X86EMUL_PROPAGATE_FAULT)
4818 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004819 if (rc == X86EMUL_INTERCEPTED)
4820 return EMULATION_INTERCEPTED;
4821
Avi Kivitydd856ef2012-08-27 23:46:17 +03004822 if (rc == X86EMUL_CONTINUE)
4823 writeback_registers(ctxt);
4824
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004825 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004826
4827twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004828 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004829 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004830 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004831 break;
4832 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004833 case 0x0d: /* GrpP (prefetch) */
4834 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004835 break;
4836 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004837 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004838 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004839 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004840 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004841 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004842 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004843 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4844 if (!test_cc(ctxt->b, ctxt->eflags))
4845 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004846 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004847 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004848 if (test_cc(ctxt->b, ctxt->eflags))
4849 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004850 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004851 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004852 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004853 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004854 case 0xae: /* clflush */
4855 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004856 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004857 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004858 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004859 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004860 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004861 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004862 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004863 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004864 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004865 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004866 case 0xc0 ... 0xc1: /* xadd */
Avi Kivity158de572013-01-19 19:51:57 +02004867 fastop(ctxt, em_add);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004868 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004869 ctxt->src.val = ctxt->dst.orig_val;
4870 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004871 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004872 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004873 ctxt->dst.bytes = ctxt->op_bytes;
4874 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4875 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004876 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004877 default:
4878 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004879 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004880
4881 if (rc != X86EMUL_CONTINUE)
4882 goto done;
4883
Avi Kivity6aa8b732006-12-10 02:21:36 -08004884 goto writeback;
4885
4886cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004887 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004888}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004889
4890void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
4891{
4892 invalidate_registers(ctxt);
4893}
4894
4895void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
4896{
4897 writeback_registers(ctxt);
4898}