blob: 5953dcea752d08e950d62293abbdec94ae95f62b [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivityb7d491e2013-01-04 16:18:49 +020027#include <linux/stringify.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028
Avi Kivity3eeb3282010-01-21 15:31:48 +020029#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020030#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020031
Avi Kivity6aa8b732006-12-10 02:21:36 -080032/*
Avi Kivitya99455492011-09-13 10:45:41 +030033 * Operand types
34 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030035#define OpNone 0ull
36#define OpImplicit 1ull /* No generic decode */
37#define OpReg 2ull /* Register */
38#define OpMem 3ull /* Memory */
39#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
40#define OpDI 5ull /* ES:DI/EDI/RDI */
41#define OpMem64 6ull /* Memory, 64-bit */
42#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
43#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030044#define OpCL 9ull /* CL register (for shifts) */
45#define OpImmByte 10ull /* 8-bit sign extended immediate */
46#define OpOne 11ull /* Implied 1 */
Nadav Amit5e2c6882012-12-06 21:55:10 -020047#define OpImm 12ull /* Sign extended up to 32-bit immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030048#define OpMem16 13ull /* Memory operand (16-bit). */
49#define OpMem32 14ull /* Memory operand (32-bit). */
50#define OpImmU 15ull /* Immediate operand, zero extended */
51#define OpSI 16ull /* SI/ESI/RSI */
52#define OpImmFAddr 17ull /* Immediate far address */
53#define OpMemFAddr 18ull /* Far address in memory */
54#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030055#define OpES 20ull /* ES */
56#define OpCS 21ull /* CS */
57#define OpSS 22ull /* SS */
58#define OpDS 23ull /* DS */
59#define OpFS 24ull /* FS */
60#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020061#define OpMem8 26ull /* 8-bit zero extended memory operand */
Nadav Amit5e2c6882012-12-06 21:55:10 -020062#define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */
Paolo Bonzini7fa57952013-05-09 11:32:50 +020063#define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */
Avi Kivitya99455492011-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 Kivitya99455492011-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 Kivitya99455492011-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
Paolo Bonzini326f5782013-05-09 11:32:51 +0200538FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET
539FOP_END;
540
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300541#define __emulate_1op_rax_rdx(ctxt, _op, _suffix, _ex) \
Avi Kivityf6b35972010-08-26 11:59:00 +0300542 do { \
543 unsigned long _tmp; \
Avi Kivitydd856ef2012-08-27 23:46:17 +0300544 ulong *rax = reg_rmw((ctxt), VCPU_REGS_RAX); \
545 ulong *rdx = reg_rmw((ctxt), VCPU_REGS_RDX); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300546 \
547 __asm__ __volatile__ ( \
548 _PRE_EFLAGS("0", "5", "1") \
549 "1: \n\t" \
550 _op _suffix " %6; " \
551 "2: \n\t" \
552 _POST_EFLAGS("0", "5", "1") \
553 ".pushsection .fixup,\"ax\" \n\t" \
554 "3: movb $1, %4 \n\t" \
555 "jmp 2b \n\t" \
556 ".popsection \n\t" \
557 _ASM_EXTABLE(1b, 3b) \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300558 : "=m" ((ctxt)->eflags), "=&r" (_tmp), \
559 "+a" (*rax), "+d" (*rdx), "+qm"(_ex) \
H. Peter Anvincb7cb282012-11-21 14:41:21 -0800560 : "i" (EFLAGS_MASK), "m" ((ctxt)->src.val)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300561 } while (0)
562
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300563/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300564#define emulate_1op_rax_rdx(ctxt, _op, _ex) \
Avi Kivity72952612011-04-20 13:12:27 +0300565 do { \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300566 switch((ctxt)->src.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300567 case 1: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300568 __emulate_1op_rax_rdx(ctxt, _op, "b", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300569 break; \
570 case 2: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300571 __emulate_1op_rax_rdx(ctxt, _op, "w", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300572 break; \
573 case 4: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300574 __emulate_1op_rax_rdx(ctxt, _op, "l", _ex); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300575 break; \
576 case 8: ON64( \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300577 __emulate_1op_rax_rdx(ctxt, _op, "q", _ex)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300578 break; \
579 } \
580 } while (0)
581
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200582static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
583 enum x86_intercept intercept,
584 enum x86_intercept_stage stage)
585{
586 struct x86_instruction_info info = {
587 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300588 .rep_prefix = ctxt->rep_prefix,
589 .modrm_mod = ctxt->modrm_mod,
590 .modrm_reg = ctxt->modrm_reg,
591 .modrm_rm = ctxt->modrm_rm,
592 .src_val = ctxt->src.val64,
593 .src_bytes = ctxt->src.bytes,
594 .dst_bytes = ctxt->dst.bytes,
595 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200596 .next_rip = ctxt->eip,
597 };
598
Avi Kivity29535382011-04-20 13:37:53 +0300599 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200600}
601
Avi Kivityf47cfa32012-06-07 17:49:24 +0300602static void assign_masked(ulong *dest, ulong src, ulong mask)
603{
604 *dest = (*dest & ~mask) | (src & mask);
605}
606
Avi Kivity9dac77f2011-06-01 15:34:25 +0300607static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800608{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300609 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800610}
611
Avi Kivityf47cfa32012-06-07 17:49:24 +0300612static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
613{
614 u16 sel;
615 struct desc_struct ss;
616
617 if (ctxt->mode == X86EMUL_MODE_PROT64)
618 return ~0UL;
619 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
620 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
621}
622
Avi Kivity612e89f2012-06-12 20:03:23 +0300623static int stack_size(struct x86_emulate_ctxt *ctxt)
624{
625 return (__fls(stack_mask(ctxt)) + 1) >> 3;
626}
627
Avi Kivity6aa8b732006-12-10 02:21:36 -0800628/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800629static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300630address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800631{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300632 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800633 return reg;
634 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300635 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800636}
637
638static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300639register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800640{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300641 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800642}
643
Avi Kivity5ad105e2012-08-19 14:34:31 +0300644static void masked_increment(ulong *reg, ulong mask, int inc)
645{
646 assign_masked(reg, *reg + inc, mask);
647}
648
Harvey Harrison7a9572752008-02-19 07:40:41 -0800649static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300650register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800651{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300652 ulong mask;
653
Avi Kivity9dac77f2011-06-01 15:34:25 +0300654 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300655 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800656 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300657 mask = ad_mask(ctxt);
658 masked_increment(reg, mask, inc);
659}
660
661static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
662{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300663 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800664}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800665
Avi Kivity9dac77f2011-06-01 15:34:25 +0300666static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800667{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300668 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800669}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300670
Avi Kivity56697682011-04-03 14:08:51 +0300671static u32 desc_limit_scaled(struct desc_struct *desc)
672{
673 u32 limit = get_desc_limit(desc);
674
675 return desc->g ? (limit << 12) | 0xfff : limit;
676}
677
Avi Kivity9dac77f2011-06-01 15:34:25 +0300678static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300679{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300680 ctxt->has_seg_override = true;
681 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300682}
683
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900684static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300685{
686 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
687 return 0;
688
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900689 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300690}
691
Avi Kivity9dac77f2011-06-01 15:34:25 +0300692static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300693{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300694 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300695 return 0;
696
Avi Kivity9dac77f2011-06-01 15:34:25 +0300697 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300698}
699
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200700static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
701 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300702{
Avi Kivityda9cb572010-11-22 17:53:21 +0200703 ctxt->exception.vector = vec;
704 ctxt->exception.error_code = error;
705 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200706 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300707}
708
Joerg Roedel3b88e412011-04-04 12:39:29 +0200709static int emulate_db(struct x86_emulate_ctxt *ctxt)
710{
711 return emulate_exception(ctxt, DB_VECTOR, 0, false);
712}
713
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200714static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300715{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200716 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300717}
718
Avi Kivity618ff152011-04-03 12:32:09 +0300719static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
720{
721 return emulate_exception(ctxt, SS_VECTOR, err, true);
722}
723
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200724static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300725{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200726 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300727}
728
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200729static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300730{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200731 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300732}
733
Avi Kivity34d1f492010-08-26 11:59:01 +0300734static int emulate_de(struct x86_emulate_ctxt *ctxt)
735{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200736 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300737}
738
Avi Kivity1253791d2011-03-29 11:41:27 +0200739static int emulate_nm(struct x86_emulate_ctxt *ctxt)
740{
741 return emulate_exception(ctxt, NM_VECTOR, 0, false);
742}
743
Avi Kivity1aa36612011-04-27 13:20:30 +0300744static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
745{
746 u16 selector;
747 struct desc_struct desc;
748
749 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
750 return selector;
751}
752
753static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
754 unsigned seg)
755{
756 u16 dummy;
757 u32 base3;
758 struct desc_struct desc;
759
760 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
761 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
762}
763
Avi Kivity1c11b372012-04-09 18:39:59 +0300764/*
765 * x86 defines three classes of vector instructions: explicitly
766 * aligned, explicitly unaligned, and the rest, which change behaviour
767 * depending on whether they're AVX encoded or not.
768 *
769 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
770 * subject to the same check.
771 */
772static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
773{
774 if (likely(size < 16))
775 return false;
776
777 if (ctxt->d & Aligned)
778 return true;
779 else if (ctxt->d & Unaligned)
780 return false;
781 else if (ctxt->d & Avx)
782 return false;
783 else
784 return true;
785}
786
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400787static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300788 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400789 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300790 ulong *linear)
791{
Avi Kivity618ff152011-04-03 12:32:09 +0300792 struct desc_struct desc;
793 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300794 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300795 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300796 u16 sel;
Gleb Natapov3a78a4f2012-12-20 16:57:42 +0200797 unsigned cpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300798
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900799 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300800 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300801 case X86EMUL_MODE_PROT64:
802 if (((signed long)la << 16) >> 16 != la)
803 return emulate_gp(ctxt, 0);
804 break;
805 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300806 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
807 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300808 if (!usable)
809 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200810 /* code segment in protected mode or read-only data segment */
811 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
812 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300813 goto bad;
814 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400815 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300816 goto bad;
817 lim = desc_limit_scaled(&desc);
818 if ((desc.type & 8) || !(desc.type & 4)) {
819 /* expand-up segment */
820 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
821 goto bad;
822 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800823 /* expand-down segment */
Avi Kivity618ff152011-04-03 12:32:09 +0300824 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
825 goto bad;
826 lim = desc.d ? 0xffffffff : 0xffff;
827 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
828 goto bad;
829 }
Avi Kivity717746e2011-04-20 13:37:53 +0300830 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity618ff152011-04-03 12:32:09 +0300831 if (!(desc.type & 8)) {
832 /* data segment */
833 if (cpl > desc.dpl)
834 goto bad;
835 } else if ((desc.type & 8) && !(desc.type & 4)) {
836 /* nonconforming code segment */
837 if (cpl != desc.dpl)
838 goto bad;
839 } else if ((desc.type & 8) && (desc.type & 4)) {
840 /* conforming code segment */
841 if (cpl < desc.dpl)
842 goto bad;
843 }
844 break;
845 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300846 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300847 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300848 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
849 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300850 *linear = la;
851 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300852bad:
853 if (addr.seg == VCPU_SREG_SS)
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300854 return emulate_ss(ctxt, sel);
Avi Kivity618ff152011-04-03 12:32:09 +0300855 else
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300856 return emulate_gp(ctxt, sel);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300857}
858
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400859static int linearize(struct x86_emulate_ctxt *ctxt,
860 struct segmented_address addr,
861 unsigned size, bool write,
862 ulong *linear)
863{
864 return __linearize(ctxt, addr, size, write, false, linear);
865}
866
867
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200868static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
869 struct segmented_address addr,
870 void *data,
871 unsigned size)
872{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200873 int rc;
874 ulong linear;
875
Avi Kivity83b87952011-04-03 11:31:19 +0300876 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200877 if (rc != X86EMUL_CONTINUE)
878 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300879 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200880}
881
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900882/*
883 * Fetch the next byte of the instruction being emulated which is pointed to
884 * by ctxt->_eip, then increment ctxt->_eip.
885 *
886 * Also prefetch the remaining bytes of the instruction without crossing page
887 * boundary if they are not in fetch_cache yet.
888 */
889static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200890{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300891 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200892 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300893 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200894
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900895 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400896 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900897 struct segmented_address addr = { .seg = VCPU_SREG_CS,
898 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300899 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900900 size = min(15UL - cur_size,
901 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400902 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900903 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400904 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900905 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
906 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900907 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200908 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300909 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200910 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900911 *dest = fc->data[ctxt->_eip - fc->start];
912 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900913 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200914}
915
916static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900917 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200918{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900919 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200920
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200921 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900922 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200923 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200924 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900925 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900926 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200927 return rc;
928 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900929 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200930}
931
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900932/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900933#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900934({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900935 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900936 if (rc != X86EMUL_CONTINUE) \
937 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900938 (_type)_x; \
939})
940
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900941#define insn_fetch_arr(_arr, _size, _ctxt) \
942({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900943 if (rc != X86EMUL_CONTINUE) \
944 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900945})
946
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000947/*
948 * Given the 'reg' portion of a ModRM byte, and a register block, return a
949 * pointer into the block that addresses the relevant register.
950 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
951 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300952static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000953 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800954{
955 void *p;
956
Avi Kivity6aa8b732006-12-10 02:21:36 -0800957 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300958 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
959 else
960 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800961 return p;
962}
963
964static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200965 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966 u16 *size, unsigned long *address, int op_bytes)
967{
968 int rc;
969
970 if (op_bytes == 2)
971 op_bytes = 3;
972 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200973 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900974 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800975 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200976 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200977 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800978 return rc;
979}
980
Avi Kivity34b77652013-01-19 19:51:56 +0200981FASTOP2(add);
982FASTOP2(or);
983FASTOP2(adc);
984FASTOP2(sbb);
985FASTOP2(and);
986FASTOP2(sub);
987FASTOP2(xor);
988FASTOP2(cmp);
989FASTOP2(test);
990
991FASTOP3WCL(shld);
992FASTOP3WCL(shrd);
993
994FASTOP2W(imul);
995
996FASTOP1(not);
997FASTOP1(neg);
998FASTOP1(inc);
999FASTOP1(dec);
1000
1001FASTOP2CL(rol);
1002FASTOP2CL(ror);
1003FASTOP2CL(rcl);
1004FASTOP2CL(rcr);
1005FASTOP2CL(shl);
1006FASTOP2CL(shr);
1007FASTOP2CL(sar);
1008
1009FASTOP2W(bsf);
1010FASTOP2W(bsr);
1011FASTOP2W(bt);
1012FASTOP2W(bts);
1013FASTOP2W(btr);
1014FASTOP2W(btc);
1015
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001016static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001017{
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001018 u8 rc;
1019 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001020
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001021 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +02001022 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001023 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
1024 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001025}
1026
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001027static void fetch_register_operand(struct operand *op)
1028{
1029 switch (op->bytes) {
1030 case 1:
1031 op->val = *(u8 *)op->addr.reg;
1032 break;
1033 case 2:
1034 op->val = *(u16 *)op->addr.reg;
1035 break;
1036 case 4:
1037 op->val = *(u32 *)op->addr.reg;
1038 break;
1039 case 8:
1040 op->val = *(u64 *)op->addr.reg;
1041 break;
1042 }
1043}
1044
Avi Kivity1253791d2011-03-29 11:41:27 +02001045static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
1046{
1047 ctxt->ops->get_fpu(ctxt);
1048 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001049 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
1050 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
1051 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
1052 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
1053 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
1054 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
1055 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
1056 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001057#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001058 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
1059 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
1060 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
1061 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
1062 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
1063 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
1064 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
1065 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001066#endif
1067 default: BUG();
1068 }
1069 ctxt->ops->put_fpu(ctxt);
1070}
1071
1072static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
1073 int reg)
1074{
1075 ctxt->ops->get_fpu(ctxt);
1076 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001077 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
1078 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
1079 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
1080 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
1081 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
1082 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
1083 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
1084 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001085#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001086 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
1087 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
1088 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
1089 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
1090 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
1091 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
1092 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
1093 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001094#endif
1095 default: BUG();
1096 }
1097 ctxt->ops->put_fpu(ctxt);
1098}
1099
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001100static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1101{
1102 ctxt->ops->get_fpu(ctxt);
1103 switch (reg) {
1104 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1105 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1106 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1107 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1108 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1109 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1110 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1111 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1112 default: BUG();
1113 }
1114 ctxt->ops->put_fpu(ctxt);
1115}
1116
1117static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1118{
1119 ctxt->ops->get_fpu(ctxt);
1120 switch (reg) {
1121 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1122 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1123 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1124 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1125 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1126 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1127 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1128 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1129 default: BUG();
1130 }
1131 ctxt->ops->put_fpu(ctxt);
1132}
1133
Gleb Natapov045a2822012-12-20 16:57:43 +02001134static int em_fninit(struct x86_emulate_ctxt *ctxt)
1135{
1136 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1137 return emulate_nm(ctxt);
1138
1139 ctxt->ops->get_fpu(ctxt);
1140 asm volatile("fninit");
1141 ctxt->ops->put_fpu(ctxt);
1142 return X86EMUL_CONTINUE;
1143}
1144
1145static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1146{
1147 u16 fcw;
1148
1149 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1150 return emulate_nm(ctxt);
1151
1152 ctxt->ops->get_fpu(ctxt);
1153 asm volatile("fnstcw %0": "+m"(fcw));
1154 ctxt->ops->put_fpu(ctxt);
1155
1156 /* force 2 byte destination */
1157 ctxt->dst.bytes = 2;
1158 ctxt->dst.val = fcw;
1159
1160 return X86EMUL_CONTINUE;
1161}
1162
1163static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1164{
1165 u16 fsw;
1166
1167 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1168 return emulate_nm(ctxt);
1169
1170 ctxt->ops->get_fpu(ctxt);
1171 asm volatile("fnstsw %0": "+m"(fsw));
1172 ctxt->ops->put_fpu(ctxt);
1173
1174 /* force 2 byte destination */
1175 ctxt->dst.bytes = 2;
1176 ctxt->dst.val = fsw;
1177
1178 return X86EMUL_CONTINUE;
1179}
1180
Avi Kivity1253791d2011-03-29 11:41:27 +02001181static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001182 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001183{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001184 unsigned reg = ctxt->modrm_reg;
1185 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +02001186
Avi Kivity9dac77f2011-06-01 15:34:25 +03001187 if (!(ctxt->d & ModRM))
1188 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity1253791d2011-03-29 11:41:27 +02001189
Avi Kivity9dac77f2011-06-01 15:34:25 +03001190 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001191 op->type = OP_XMM;
1192 op->bytes = 16;
1193 op->addr.xmm = reg;
1194 read_sse_reg(ctxt, &op->vec_val, reg);
1195 return;
1196 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001197 if (ctxt->d & Mmx) {
1198 reg &= 7;
1199 op->type = OP_MM;
1200 op->bytes = 8;
1201 op->addr.mm = reg;
1202 return;
1203 }
Avi Kivity1253791d2011-03-29 11:41:27 +02001204
Avi Kivity3c118e22007-10-31 10:27:04 +02001205 op->type = OP_REG;
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001206 if (ctxt->d & ByteOp) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001207 op->addr.reg = decode_register(ctxt, reg, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +02001208 op->bytes = 1;
1209 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001210 op->addr.reg = decode_register(ctxt, reg, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001211 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +02001212 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001213 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001214 op->orig_val = op->val;
1215}
1216
Avi Kivitya6e34072012-06-10 17:15:39 +03001217static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1218{
1219 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1220 ctxt->modrm_seg = VCPU_SREG_SS;
1221}
1222
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001223static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001224 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001225{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001226 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001227 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001228 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001229 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001230
Avi Kivity9dac77f2011-06-01 15:34:25 +03001231 if (ctxt->rex_prefix) {
1232 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
1233 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
1234 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001235 }
1236
Avi Kivity9dac77f2011-06-01 15:34:25 +03001237 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
1238 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
1239 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
1240 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001241
Avi Kivity9dac77f2011-06-01 15:34:25 +03001242 if (ctxt->modrm_mod == 3) {
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001243 int highbyte_regs = ctxt->rex_prefix == 0;
1244
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001245 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001246 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001247 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
1248 highbyte_regs && (ctxt->d & ByteOp));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001249 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001250 op->type = OP_XMM;
1251 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001252 op->addr.xmm = ctxt->modrm_rm;
1253 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001254 return rc;
1255 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001256 if (ctxt->d & Mmx) {
1257 op->type = OP_MM;
1258 op->bytes = 8;
1259 op->addr.xmm = ctxt->modrm_rm & 7;
1260 return rc;
1261 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001262 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001263 return rc;
1264 }
1265
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001266 op->type = OP_MEM;
1267
Avi Kivity9dac77f2011-06-01 15:34:25 +03001268 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001269 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1270 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1271 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1272 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001273
1274 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001275 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001276 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001277 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001278 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001279 break;
1280 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001281 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001282 break;
1283 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001284 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001285 break;
1286 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001287 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001288 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001289 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001290 break;
1291 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001292 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001293 break;
1294 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001295 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001296 break;
1297 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001298 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001299 break;
1300 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001301 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001302 break;
1303 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001304 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001305 break;
1306 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001307 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001308 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001309 break;
1310 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001311 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001312 break;
1313 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001314 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1315 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1316 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001317 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001318 } else {
1319 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001320 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001321 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001322 index_reg |= (sib >> 3) & 7;
1323 base_reg |= sib & 7;
1324 scale = sib >> 6;
1325
Avi Kivity9dac77f2011-06-01 15:34:25 +03001326 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001327 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001328 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001329 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001330 adjust_modrm_seg(ctxt, base_reg);
1331 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001332 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001333 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001334 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001335 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001336 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001337 } else {
1338 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001339 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001340 adjust_modrm_seg(ctxt, base_reg);
1341 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001342 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001343 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001344 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001345 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001346 break;
1347 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001348 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001349 break;
1350 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001351 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001352 break;
1353 }
1354 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001355 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001356done:
1357 return rc;
1358}
1359
1360static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001361 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001362{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001363 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001364
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001365 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001366 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001367 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001368 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001369 break;
1370 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001371 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001372 break;
1373 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001374 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001375 break;
1376 }
1377done:
1378 return rc;
1379}
1380
Avi Kivity9dac77f2011-06-01 15:34:25 +03001381static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001382{
Sheng Yang7129eec2010-09-28 16:33:32 +08001383 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001384
Avi Kivity9dac77f2011-06-01 15:34:25 +03001385 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1386 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001387
Avi Kivity9dac77f2011-06-01 15:34:25 +03001388 if (ctxt->src.bytes == 2)
1389 sv = (s16)ctxt->src.val & (s16)mask;
1390 else if (ctxt->src.bytes == 4)
1391 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001392
Avi Kivity9dac77f2011-06-01 15:34:25 +03001393 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001394 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001395
1396 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001397 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001398}
1399
Gleb Natapov9de41572010-04-28 19:15:22 +03001400static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001401 unsigned long addr, void *dest, unsigned size)
1402{
1403 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001404 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001405
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001406 if (mc->pos < mc->end)
1407 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001408
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001409 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001410
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001411 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1412 &ctxt->exception);
1413 if (rc != X86EMUL_CONTINUE)
1414 return rc;
1415
1416 mc->end += size;
1417
1418read_cached:
1419 memcpy(dest, mc->data + mc->pos, size);
1420 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001421 return X86EMUL_CONTINUE;
1422}
1423
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001424static int segmented_read(struct x86_emulate_ctxt *ctxt,
1425 struct segmented_address addr,
1426 void *data,
1427 unsigned size)
1428{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001429 int rc;
1430 ulong linear;
1431
Avi Kivity83b87952011-04-03 11:31:19 +03001432 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001433 if (rc != X86EMUL_CONTINUE)
1434 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001435 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001436}
1437
1438static int segmented_write(struct x86_emulate_ctxt *ctxt,
1439 struct segmented_address addr,
1440 const void *data,
1441 unsigned size)
1442{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001443 int rc;
1444 ulong linear;
1445
Avi Kivity83b87952011-04-03 11:31:19 +03001446 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001447 if (rc != X86EMUL_CONTINUE)
1448 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001449 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1450 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001451}
1452
1453static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1454 struct segmented_address addr,
1455 const void *orig_data, const void *data,
1456 unsigned size)
1457{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001458 int rc;
1459 ulong linear;
1460
Avi Kivity83b87952011-04-03 11:31:19 +03001461 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001462 if (rc != X86EMUL_CONTINUE)
1463 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001464 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1465 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001466}
1467
Gleb Natapov7b262e92010-03-18 15:20:27 +02001468static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001469 unsigned int size, unsigned short port,
1470 void *dest)
1471{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001472 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001473
1474 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001475 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001476 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001477 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001478 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001479 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1480 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Gleb Natapov7b262e92010-03-18 15:20:27 +02001481 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1482 count);
1483 if (n == 0)
1484 n = 1;
1485 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001486 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001487 return 0;
1488 rc->end = n * size;
1489 }
1490
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001491 if (ctxt->rep_prefix && !(ctxt->eflags & EFLG_DF)) {
1492 ctxt->dst.data = rc->data + rc->pos;
1493 ctxt->dst.type = OP_MEM_STR;
1494 ctxt->dst.count = (rc->end - rc->pos) / size;
1495 rc->pos = rc->end;
1496 } else {
1497 memcpy(dest, rc->data + rc->pos, size);
1498 rc->pos += size;
1499 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001500 return 1;
1501}
1502
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001503static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1504 u16 index, struct desc_struct *desc)
1505{
1506 struct desc_ptr dt;
1507 ulong addr;
1508
1509 ctxt->ops->get_idt(ctxt, &dt);
1510
1511 if (dt.size < index * 8 + 7)
1512 return emulate_gp(ctxt, index << 3 | 0x2);
1513
1514 addr = dt.address + index * 8;
1515 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1516 &ctxt->exception);
1517}
1518
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001519static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001520 u16 selector, struct desc_ptr *dt)
1521{
Mathias Krause0225fb52012-08-30 01:30:16 +02001522 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001523
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001524 if (selector & 1 << 2) {
1525 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001526 u16 sel;
1527
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001528 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001529 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001530 return;
1531
1532 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1533 dt->address = get_desc_base(&desc);
1534 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001535 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001536}
1537
1538/* allowed just for 8 bytes segments */
1539static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001540 u16 selector, struct desc_struct *desc,
1541 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001542{
1543 struct desc_ptr dt;
1544 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001545 ulong addr;
1546
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001547 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001548
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001549 if (dt.size < index * 8 + 7)
1550 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001551
Avi Kivitye9194642012-06-13 16:29:39 +03001552 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001553 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1554 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001555}
1556
1557/* allowed just for 8 bytes segments */
1558static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001559 u16 selector, struct desc_struct *desc)
1560{
1561 struct desc_ptr dt;
1562 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001563 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001564
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001565 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001566
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001567 if (dt.size < index * 8 + 7)
1568 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001569
1570 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001571 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1572 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001573}
1574
Gleb Natapov5601d052011-03-07 14:55:06 +02001575/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001576static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001577 u16 selector, int seg)
1578{
Avi Kivity869be992012-06-13 16:30:53 +03001579 struct desc_struct seg_desc, old_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001580 u8 dpl, rpl, cpl;
1581 unsigned err_vec = GP_VECTOR;
1582 u32 err_code = 0;
1583 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001584 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001585 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001586 u16 dummy;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001587
1588 memset(&seg_desc, 0, sizeof seg_desc);
1589
Kevin Wolff8da94e2013-04-11 14:06:03 +02001590 if (ctxt->mode == X86EMUL_MODE_REAL) {
1591 /* set real mode segment descriptor (keep limit etc. for
1592 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001593 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001594 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001595 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001596 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1597 /* VM86 needs a clean new segment descriptor */
1598 set_desc_base(&seg_desc, selector << 4);
1599 set_desc_limit(&seg_desc, 0xffff);
1600 seg_desc.type = 3;
1601 seg_desc.p = 1;
1602 seg_desc.s = 1;
1603 seg_desc.dpl = 3;
1604 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001605 }
1606
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001607 rpl = selector & 3;
1608 cpl = ctxt->ops->cpl(ctxt);
1609
1610 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1611 if ((seg == VCPU_SREG_CS
1612 || (seg == VCPU_SREG_SS
1613 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1614 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001615 && null_selector)
1616 goto exception;
1617
1618 /* TR should be in GDT only */
1619 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1620 goto exception;
1621
1622 if (null_selector) /* for NULL selector skip all following checks */
1623 goto load;
1624
Avi Kivitye9194642012-06-13 16:29:39 +03001625 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001626 if (ret != X86EMUL_CONTINUE)
1627 return ret;
1628
1629 err_code = selector & 0xfffc;
1630 err_vec = GP_VECTOR;
1631
Guo Chaofc058682012-06-28 15:19:51 +08001632 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001633 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1634 goto exception;
1635
1636 if (!seg_desc.p) {
1637 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1638 goto exception;
1639 }
1640
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001641 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001642
1643 switch (seg) {
1644 case VCPU_SREG_SS:
1645 /*
1646 * segment is not a writable data segment or segment
1647 * selector's RPL != CPL or segment selector's RPL != CPL
1648 */
1649 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1650 goto exception;
1651 break;
1652 case VCPU_SREG_CS:
1653 if (!(seg_desc.type & 8))
1654 goto exception;
1655
1656 if (seg_desc.type & 4) {
1657 /* conforming */
1658 if (dpl > cpl)
1659 goto exception;
1660 } else {
1661 /* nonconforming */
1662 if (rpl > cpl || dpl != cpl)
1663 goto exception;
1664 }
1665 /* CS(RPL) <- CPL */
1666 selector = (selector & 0xfffc) | cpl;
1667 break;
1668 case VCPU_SREG_TR:
1669 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1670 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001671 old_desc = seg_desc;
1672 seg_desc.type |= 2; /* busy */
1673 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1674 sizeof(seg_desc), &ctxt->exception);
1675 if (ret != X86EMUL_CONTINUE)
1676 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001677 break;
1678 case VCPU_SREG_LDTR:
1679 if (seg_desc.s || seg_desc.type != 2)
1680 goto exception;
1681 break;
1682 default: /* DS, ES, FS, or GS */
1683 /*
1684 * segment is not a data or readable code segment or
1685 * ((segment is a data or nonconforming code segment)
1686 * and (both RPL and CPL > DPL))
1687 */
1688 if ((seg_desc.type & 0xa) == 0x8 ||
1689 (((seg_desc.type & 0xc) != 0xc) &&
1690 (rpl > dpl && cpl > dpl)))
1691 goto exception;
1692 break;
1693 }
1694
1695 if (seg_desc.s) {
1696 /* mark segment as accessed */
1697 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001698 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001699 if (ret != X86EMUL_CONTINUE)
1700 return ret;
1701 }
1702load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001703 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001704 return X86EMUL_CONTINUE;
1705exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001706 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001707 return X86EMUL_PROPAGATE_FAULT;
1708}
1709
Wei Yongjun31be40b2010-08-17 09:17:30 +08001710static void write_register_operand(struct operand *op)
1711{
1712 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1713 switch (op->bytes) {
1714 case 1:
1715 *(u8 *)op->addr.reg = (u8)op->val;
1716 break;
1717 case 2:
1718 *(u16 *)op->addr.reg = (u16)op->val;
1719 break;
1720 case 4:
1721 *op->addr.reg = (u32)op->val;
1722 break; /* 64b: zero-extend */
1723 case 8:
1724 *op->addr.reg = op->val;
1725 break;
1726 }
1727}
1728
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001729static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001730{
1731 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001732
Avi Kivityb6744dc2013-01-04 16:18:50 +02001733 if (ctxt->d & NoWrite)
1734 return X86EMUL_CONTINUE;
1735
Avi Kivity9dac77f2011-06-01 15:34:25 +03001736 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001737 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001738 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001739 break;
1740 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001741 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001742 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001743 ctxt->dst.addr.mem,
1744 &ctxt->dst.orig_val,
1745 &ctxt->dst.val,
1746 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001747 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001748 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001749 ctxt->dst.addr.mem,
1750 &ctxt->dst.val,
1751 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001752 if (rc != X86EMUL_CONTINUE)
1753 return rc;
1754 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001755 case OP_MEM_STR:
1756 rc = segmented_write(ctxt,
1757 ctxt->dst.addr.mem,
1758 ctxt->dst.data,
1759 ctxt->dst.bytes * ctxt->dst.count);
1760 if (rc != X86EMUL_CONTINUE)
1761 return rc;
1762 break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001763 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001764 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001765 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001766 case OP_MM:
1767 write_mmx_reg(ctxt, &ctxt->dst.mm_val, ctxt->dst.addr.mm);
1768 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001769 case OP_NONE:
1770 /* no writeback */
1771 break;
1772 default:
1773 break;
1774 }
1775 return X86EMUL_CONTINUE;
1776}
1777
Avi Kivity51ddff52012-06-12 20:19:40 +03001778static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001779{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001780 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001781
Avi Kivity5ad105e2012-08-19 14:34:31 +03001782 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001783 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001784 addr.seg = VCPU_SREG_SS;
1785
Avi Kivity51ddff52012-06-12 20:19:40 +03001786 return segmented_write(ctxt, addr, data, bytes);
1787}
1788
1789static int em_push(struct x86_emulate_ctxt *ctxt)
1790{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001791 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001792 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001793 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001794}
1795
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001796static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001797 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001798{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001799 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001800 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001801
Avi Kivitydd856ef2012-08-27 23:46:17 +03001802 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001803 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001804 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001805 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001806 return rc;
1807
Avi Kivity5ad105e2012-08-19 14:34:31 +03001808 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001809 return rc;
1810}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001811
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001812static int em_pop(struct x86_emulate_ctxt *ctxt)
1813{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001814 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001815}
1816
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001817static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001818 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001819{
1820 int rc;
1821 unsigned long val, change_mask;
1822 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001823 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001824
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001825 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001826 if (rc != X86EMUL_CONTINUE)
1827 return rc;
1828
1829 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1830 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1831
1832 switch(ctxt->mode) {
1833 case X86EMUL_MODE_PROT64:
1834 case X86EMUL_MODE_PROT32:
1835 case X86EMUL_MODE_PROT16:
1836 if (cpl == 0)
1837 change_mask |= EFLG_IOPL;
1838 if (cpl <= iopl)
1839 change_mask |= EFLG_IF;
1840 break;
1841 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001842 if (iopl < 3)
1843 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001844 change_mask |= EFLG_IF;
1845 break;
1846 default: /* real mode */
1847 change_mask |= (EFLG_IOPL | EFLG_IF);
1848 break;
1849 }
1850
1851 *(unsigned long *)dest =
1852 (ctxt->eflags & ~change_mask) | (val & change_mask);
1853
1854 return rc;
1855}
1856
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001857static int em_popf(struct x86_emulate_ctxt *ctxt)
1858{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001859 ctxt->dst.type = OP_REG;
1860 ctxt->dst.addr.reg = &ctxt->eflags;
1861 ctxt->dst.bytes = ctxt->op_bytes;
1862 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001863}
1864
Avi Kivity612e89f2012-06-12 20:03:23 +03001865static int em_enter(struct x86_emulate_ctxt *ctxt)
1866{
1867 int rc;
1868 unsigned frame_size = ctxt->src.val;
1869 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001870 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001871
1872 if (nesting_level)
1873 return X86EMUL_UNHANDLEABLE;
1874
Avi Kivitydd856ef2012-08-27 23:46:17 +03001875 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1876 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001877 if (rc != X86EMUL_CONTINUE)
1878 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001879 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001880 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001881 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1882 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001883 stack_mask(ctxt));
1884 return X86EMUL_CONTINUE;
1885}
1886
Avi Kivityf47cfa32012-06-07 17:49:24 +03001887static int em_leave(struct x86_emulate_ctxt *ctxt)
1888{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001889 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001890 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001891 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001892}
1893
Avi Kivity1cd196e2011-09-13 10:45:51 +03001894static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001895{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001896 int seg = ctxt->src2.val;
1897
Avi Kivity9dac77f2011-06-01 15:34:25 +03001898 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001899
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001900 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001901}
1902
Avi Kivity1cd196e2011-09-13 10:45:51 +03001903static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001904{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001905 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001906 unsigned long selector;
1907 int rc;
1908
Avi Kivity9dac77f2011-06-01 15:34:25 +03001909 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001910 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001911 return rc;
1912
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001913 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001914 return rc;
1915}
1916
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001917static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001918{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001919 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001920 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001921 int reg = VCPU_REGS_RAX;
1922
1923 while (reg <= VCPU_REGS_RDI) {
1924 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001925 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001926
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001927 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001928 if (rc != X86EMUL_CONTINUE)
1929 return rc;
1930
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001931 ++reg;
1932 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001933
Wei Yongjunc37eda12010-06-15 09:03:33 +08001934 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001935}
1936
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001937static int em_pushf(struct x86_emulate_ctxt *ctxt)
1938{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001939 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001940 return em_push(ctxt);
1941}
1942
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001943static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001944{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001945 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001946 int reg = VCPU_REGS_RDI;
1947
1948 while (reg >= VCPU_REGS_RAX) {
1949 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001950 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001951 --reg;
1952 }
1953
Avi Kivitydd856ef2012-08-27 23:46:17 +03001954 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001955 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001956 break;
1957 --reg;
1958 }
1959 return rc;
1960}
1961
Avi Kivitydd856ef2012-08-27 23:46:17 +03001962static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001963{
Mathias Krause0225fb52012-08-30 01:30:16 +02001964 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001965 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001966 struct desc_ptr dt;
1967 gva_t cs_addr;
1968 gva_t eip_addr;
1969 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001970
1971 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001972 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001973 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001974 if (rc != X86EMUL_CONTINUE)
1975 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001976
1977 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1978
Avi Kivity9dac77f2011-06-01 15:34:25 +03001979 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001980 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001981 if (rc != X86EMUL_CONTINUE)
1982 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001983
Avi Kivity9dac77f2011-06-01 15:34:25 +03001984 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001985 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001986 if (rc != X86EMUL_CONTINUE)
1987 return rc;
1988
Avi Kivity4bff1e862011-04-20 13:37:53 +03001989 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001990
1991 eip_addr = dt.address + (irq << 2);
1992 cs_addr = dt.address + (irq << 2) + 2;
1993
Avi Kivity0f65dd72011-04-20 13:37:53 +03001994 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001995 if (rc != X86EMUL_CONTINUE)
1996 return rc;
1997
Avi Kivity0f65dd72011-04-20 13:37:53 +03001998 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001999 if (rc != X86EMUL_CONTINUE)
2000 return rc;
2001
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002002 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002003 if (rc != X86EMUL_CONTINUE)
2004 return rc;
2005
Avi Kivity9dac77f2011-06-01 15:34:25 +03002006 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002007
2008 return rc;
2009}
2010
Avi Kivitydd856ef2012-08-27 23:46:17 +03002011int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
2012{
2013 int rc;
2014
2015 invalidate_registers(ctxt);
2016 rc = __emulate_int_real(ctxt, irq);
2017 if (rc == X86EMUL_CONTINUE)
2018 writeback_registers(ctxt);
2019 return rc;
2020}
2021
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002022static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002023{
2024 switch(ctxt->mode) {
2025 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03002026 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002027 case X86EMUL_MODE_VM86:
2028 case X86EMUL_MODE_PROT16:
2029 case X86EMUL_MODE_PROT32:
2030 case X86EMUL_MODE_PROT64:
2031 default:
2032 /* Protected mode interrupts unimplemented yet */
2033 return X86EMUL_UNHANDLEABLE;
2034 }
2035}
2036
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002037static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002038{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002039 int rc = X86EMUL_CONTINUE;
2040 unsigned long temp_eip = 0;
2041 unsigned long temp_eflags = 0;
2042 unsigned long cs = 0;
2043 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
2044 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
2045 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
2046 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
2047
2048 /* TODO: Add stack limit check */
2049
Avi Kivity9dac77f2011-06-01 15:34:25 +03002050 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002051
2052 if (rc != X86EMUL_CONTINUE)
2053 return rc;
2054
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002055 if (temp_eip & ~0xffff)
2056 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002057
Avi Kivity9dac77f2011-06-01 15:34:25 +03002058 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002059
2060 if (rc != X86EMUL_CONTINUE)
2061 return rc;
2062
Avi Kivity9dac77f2011-06-01 15:34:25 +03002063 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002064
2065 if (rc != X86EMUL_CONTINUE)
2066 return rc;
2067
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002068 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002069
2070 if (rc != X86EMUL_CONTINUE)
2071 return rc;
2072
Avi Kivity9dac77f2011-06-01 15:34:25 +03002073 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002074
2075
Avi Kivity9dac77f2011-06-01 15:34:25 +03002076 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002077 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03002078 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002079 ctxt->eflags &= ~0xffff;
2080 ctxt->eflags |= temp_eflags;
2081 }
2082
2083 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
2084 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
2085
2086 return rc;
2087}
2088
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002089static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002090{
2091 switch(ctxt->mode) {
2092 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002093 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002094 case X86EMUL_MODE_VM86:
2095 case X86EMUL_MODE_PROT16:
2096 case X86EMUL_MODE_PROT32:
2097 case X86EMUL_MODE_PROT64:
2098 default:
2099 /* iret from protected mode unimplemented yet */
2100 return X86EMUL_UNHANDLEABLE;
2101 }
2102}
2103
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002104static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2105{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002106 int rc;
2107 unsigned short sel;
2108
Avi Kivity9dac77f2011-06-01 15:34:25 +03002109 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002110
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002111 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002112 if (rc != X86EMUL_CONTINUE)
2113 return rc;
2114
Avi Kivity9dac77f2011-06-01 15:34:25 +03002115 ctxt->_eip = 0;
2116 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002117 return X86EMUL_CONTINUE;
2118}
2119
Avi Kivity3329ece2011-09-13 10:45:39 +03002120static int em_mul_ex(struct x86_emulate_ctxt *ctxt)
2121{
2122 u8 ex = 0;
2123
2124 emulate_1op_rax_rdx(ctxt, "mul", ex);
2125 return X86EMUL_CONTINUE;
2126}
2127
2128static int em_imul_ex(struct x86_emulate_ctxt *ctxt)
2129{
2130 u8 ex = 0;
2131
2132 emulate_1op_rax_rdx(ctxt, "imul", ex);
2133 return X86EMUL_CONTINUE;
2134}
2135
2136static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002137{
Avi Kivity34d1f492010-08-26 11:59:01 +03002138 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002139
Avi Kivity3329ece2011-09-13 10:45:39 +03002140 emulate_1op_rax_rdx(ctxt, "div", de);
2141 if (de)
2142 return emulate_de(ctxt);
2143 return X86EMUL_CONTINUE;
2144}
2145
2146static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
2147{
2148 u8 de = 0;
2149
2150 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03002151 if (de)
2152 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03002153 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002154}
2155
Takuya Yoshikawa51187682011-05-02 02:29:17 +09002156static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002157{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002158 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002159
Avi Kivity9dac77f2011-06-01 15:34:25 +03002160 switch (ctxt->modrm_reg) {
Mohammed Gamald19292e2008-09-08 21:47:19 +03002161 case 2: /* call near abs */ {
2162 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002163 old_eip = ctxt->_eip;
2164 ctxt->_eip = ctxt->src.val;
2165 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002166 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03002167 break;
2168 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002169 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002170 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002171 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002172 case 5: /* jmp far */
2173 rc = em_jmp_far(ctxt);
2174 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002175 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002176 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002177 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002178 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002179 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002180}
2181
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002182static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002183{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002184 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002185
Avi Kivitydd856ef2012-08-27 23:46:17 +03002186 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2187 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2188 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2189 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002190 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002191 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002192 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2193 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002194
Laurent Vivier05f086f2007-09-24 11:10:55 +02002195 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002196 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002197 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002198}
2199
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002200static int em_ret(struct x86_emulate_ctxt *ctxt)
2201{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002202 ctxt->dst.type = OP_REG;
2203 ctxt->dst.addr.reg = &ctxt->_eip;
2204 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002205 return em_pop(ctxt);
2206}
2207
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002208static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002209{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002210 int rc;
2211 unsigned long cs;
2212
Avi Kivity9dac77f2011-06-01 15:34:25 +03002213 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002214 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002215 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002216 if (ctxt->op_bytes == 4)
2217 ctxt->_eip = (u32)ctxt->_eip;
2218 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002219 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002220 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002221 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002222 return rc;
2223}
2224
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002225static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2226{
2227 /* Save real source value, then compare EAX against destination. */
2228 ctxt->src.orig_val = ctxt->src.val;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002229 ctxt->src.val = reg_read(ctxt, VCPU_REGS_RAX);
Avi Kivity158de572013-01-19 19:51:57 +02002230 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002231
2232 if (ctxt->eflags & EFLG_ZF) {
2233 /* Success: write back to memory. */
2234 ctxt->dst.val = ctxt->src.orig_val;
2235 } else {
2236 /* Failure: write the value we saw to EAX. */
2237 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002238 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002239 }
2240 return X86EMUL_CONTINUE;
2241}
2242
Avi Kivityd4b43252011-09-13 10:45:50 +03002243static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002244{
Avi Kivityd4b43252011-09-13 10:45:50 +03002245 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002246 unsigned short sel;
2247 int rc;
2248
Avi Kivity9dac77f2011-06-01 15:34:25 +03002249 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002250
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002251 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002252 if (rc != X86EMUL_CONTINUE)
2253 return rc;
2254
Avi Kivity9dac77f2011-06-01 15:34:25 +03002255 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002256 return rc;
2257}
2258
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002259static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002260setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002261 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002262{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002263 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002264 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002265 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002266 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002267 cs->type = 0x0b; /* Read, Execute, Accessed */
2268 cs->s = 1;
2269 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002270 cs->p = 1;
2271 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002272 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002273
Gleb Natapov79168fd2010-04-28 19:15:30 +03002274 set_desc_base(ss, 0); /* flat segment */
2275 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002276 ss->g = 1; /* 4kb granularity */
2277 ss->s = 1;
2278 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002279 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002280 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002281 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002282 ss->l = 0;
2283 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002284}
2285
Avi Kivity1a18a692012-02-01 12:23:21 +02002286static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2287{
2288 u32 eax, ebx, ecx, edx;
2289
2290 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002291 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2292 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002293 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2294 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2295}
2296
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002297static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2298{
Mathias Krause0225fb52012-08-30 01:30:16 +02002299 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002300 u32 eax, ebx, ecx, edx;
2301
2302 /*
2303 * syscall should always be enabled in longmode - so only become
2304 * vendor specific (cpuid) if other modes are active...
2305 */
2306 if (ctxt->mode == X86EMUL_MODE_PROT64)
2307 return true;
2308
2309 eax = 0x00000000;
2310 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002311 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2312 /*
2313 * Intel ("GenuineIntel")
2314 * remark: Intel CPUs only support "syscall" in 64bit
2315 * longmode. Also an 64bit guest with a
2316 * 32bit compat-app running will #UD !! While this
2317 * behaviour can be fixed (by emulating) into AMD
2318 * response - CPUs of AMD can't behave like Intel.
2319 */
2320 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2321 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2322 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2323 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002324
Avi Kivity0017f932012-06-07 14:10:16 +03002325 /* AMD ("AuthenticAMD") */
2326 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2327 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2328 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2329 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002330
Avi Kivity0017f932012-06-07 14:10:16 +03002331 /* AMD ("AMDisbetter!") */
2332 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2333 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2334 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2335 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002336
2337 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2338 return false;
2339}
2340
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002341static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002342{
Mathias Krause0225fb52012-08-30 01:30:16 +02002343 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002344 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002345 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002346 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002347 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002348
2349 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002350 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002351 ctxt->mode == X86EMUL_MODE_VM86)
2352 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002353
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002354 if (!(em_syscall_is_enabled(ctxt)))
2355 return emulate_ud(ctxt);
2356
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002357 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002358 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002359
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002360 if (!(efer & EFER_SCE))
2361 return emulate_ud(ctxt);
2362
Avi Kivity717746e2011-04-20 13:37:53 +03002363 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002364 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002365 cs_sel = (u16)(msr_data & 0xfffc);
2366 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002367
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002368 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002369 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002370 cs.l = 1;
2371 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002372 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2373 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002374
Avi Kivitydd856ef2012-08-27 23:46:17 +03002375 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002376 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002377#ifdef CONFIG_X86_64
Avi Kivitydd856ef2012-08-27 23:46:17 +03002378 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002379
Avi Kivity717746e2011-04-20 13:37:53 +03002380 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002381 ctxt->mode == X86EMUL_MODE_PROT64 ?
2382 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002383 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002384
Avi Kivity717746e2011-04-20 13:37:53 +03002385 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002386 ctxt->eflags &= ~(msr_data | EFLG_RF);
2387#endif
2388 } else {
2389 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002390 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002391 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002392
2393 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2394 }
2395
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002396 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002397}
2398
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002399static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002400{
Mathias Krause0225fb52012-08-30 01:30:16 +02002401 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002402 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002403 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002404 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002405 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002406
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002407 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002408 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002409 if (ctxt->mode == X86EMUL_MODE_REAL)
2410 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002411
Avi Kivity1a18a692012-02-01 12:23:21 +02002412 /*
2413 * Not recognized on AMD in compat mode (but is recognized in legacy
2414 * mode).
2415 */
2416 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2417 && !vendor_intel(ctxt))
2418 return emulate_ud(ctxt);
2419
Andre Przywara8c604352009-06-18 12:56:01 +02002420 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2421 * Therefore, we inject an #UD.
2422 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002423 if (ctxt->mode == X86EMUL_MODE_PROT64)
2424 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002425
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002426 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002427
Avi Kivity717746e2011-04-20 13:37:53 +03002428 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002429 switch (ctxt->mode) {
2430 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002431 if ((msr_data & 0xfffc) == 0x0)
2432 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002433 break;
2434 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002435 if (msr_data == 0x0)
2436 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002437 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002438 default:
2439 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002440 }
2441
2442 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002443 cs_sel = (u16)msr_data;
2444 cs_sel &= ~SELECTOR_RPL_MASK;
2445 ss_sel = cs_sel + 8;
2446 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002447 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002448 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002449 cs.l = 1;
2450 }
2451
Avi Kivity1aa36612011-04-27 13:20:30 +03002452 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2453 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002454
Avi Kivity717746e2011-04-20 13:37:53 +03002455 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002456 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002457
Avi Kivity717746e2011-04-20 13:37:53 +03002458 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002459 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002460
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002461 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002462}
2463
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002464static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002465{
Mathias Krause0225fb52012-08-30 01:30:16 +02002466 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002467 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002468 u64 msr_data;
2469 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002470 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002471
Gleb Natapova0044752010-02-10 14:21:31 +02002472 /* inject #GP if in real mode or Virtual 8086 mode */
2473 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002474 ctxt->mode == X86EMUL_MODE_VM86)
2475 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002476
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002477 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002478
Avi Kivity9dac77f2011-06-01 15:34:25 +03002479 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002480 usermode = X86EMUL_MODE_PROT64;
2481 else
2482 usermode = X86EMUL_MODE_PROT32;
2483
2484 cs.dpl = 3;
2485 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002486 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002487 switch (usermode) {
2488 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002489 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002490 if ((msr_data & 0xfffc) == 0x0)
2491 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002492 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002493 break;
2494 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002495 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002496 if (msr_data == 0x0)
2497 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002498 ss_sel = cs_sel + 8;
2499 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002500 cs.l = 1;
2501 break;
2502 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002503 cs_sel |= SELECTOR_RPL_MASK;
2504 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002505
Avi Kivity1aa36612011-04-27 13:20:30 +03002506 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2507 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002508
Avi Kivitydd856ef2012-08-27 23:46:17 +03002509 ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX);
2510 *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX);
Andre Przywara4668f052009-06-18 12:56:02 +02002511
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002512 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002513}
2514
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002515static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002516{
2517 int iopl;
2518 if (ctxt->mode == X86EMUL_MODE_REAL)
2519 return false;
2520 if (ctxt->mode == X86EMUL_MODE_VM86)
2521 return true;
2522 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002523 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002524}
2525
2526static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002527 u16 port, u16 len)
2528{
Mathias Krause0225fb52012-08-30 01:30:16 +02002529 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002530 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002531 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002532 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002533 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002534 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002535 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002536
Avi Kivity1aa36612011-04-27 13:20:30 +03002537 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002538 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002539 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002540 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002541 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002542 base = get_desc_base(&tr_seg);
2543#ifdef CONFIG_X86_64
2544 base |= ((u64)base3) << 32;
2545#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002546 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002547 if (r != X86EMUL_CONTINUE)
2548 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002549 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002550 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002551 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002552 if (r != X86EMUL_CONTINUE)
2553 return false;
2554 if ((perm >> bit_idx) & mask)
2555 return false;
2556 return true;
2557}
2558
2559static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002560 u16 port, u16 len)
2561{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002562 if (ctxt->perm_ok)
2563 return true;
2564
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002565 if (emulator_bad_iopl(ctxt))
2566 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002567 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002568
2569 ctxt->perm_ok = true;
2570
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002571 return true;
2572}
2573
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002574static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002575 struct tss_segment_16 *tss)
2576{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002577 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002578 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002579 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2580 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2581 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2582 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2583 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2584 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2585 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2586 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002587
Avi Kivity1aa36612011-04-27 13:20:30 +03002588 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2589 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2590 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2591 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2592 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002593}
2594
2595static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002596 struct tss_segment_16 *tss)
2597{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002598 int ret;
2599
Avi Kivity9dac77f2011-06-01 15:34:25 +03002600 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002601 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002602 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2603 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2604 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2605 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2606 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2607 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2608 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2609 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002610
2611 /*
2612 * SDM says that segment selectors are loaded before segment
2613 * descriptors
2614 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002615 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2616 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2617 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2618 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2619 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002620
2621 /*
Guo Chaofc058682012-06-28 15:19:51 +08002622 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002623 * it is handled in a context of new task
2624 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002625 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
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->es, VCPU_SREG_ES);
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->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002632 if (ret != X86EMUL_CONTINUE)
2633 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002634 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002635 if (ret != X86EMUL_CONTINUE)
2636 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002637 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002638 if (ret != X86EMUL_CONTINUE)
2639 return ret;
2640
2641 return X86EMUL_CONTINUE;
2642}
2643
2644static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002645 u16 tss_selector, u16 old_tss_sel,
2646 ulong old_tss_base, struct desc_struct *new_desc)
2647{
Mathias Krause0225fb52012-08-30 01:30:16 +02002648 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002649 struct tss_segment_16 tss_seg;
2650 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002651 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002652
Avi Kivity0f65dd72011-04-20 13:37:53 +03002653 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002654 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002655 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002656 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002657 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002658
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002659 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002660
Avi Kivity0f65dd72011-04-20 13:37:53 +03002661 ret = ops->write_std(ctxt, old_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
Avi Kivity0f65dd72011-04-20 13:37:53 +03002667 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002668 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002669 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002670 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002671 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002672
2673 if (old_tss_sel != 0xffff) {
2674 tss_seg.prev_task_link = old_tss_sel;
2675
Avi Kivity0f65dd72011-04-20 13:37:53 +03002676 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002677 &tss_seg.prev_task_link,
2678 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002679 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002680 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002681 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002682 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002683 }
2684
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002685 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002686}
2687
2688static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002689 struct tss_segment_32 *tss)
2690{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002691 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002692 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002693 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002694 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2695 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2696 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2697 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2698 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2699 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2700 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2701 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002702
Avi Kivity1aa36612011-04-27 13:20:30 +03002703 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2704 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2705 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2706 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2707 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2708 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2709 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002710}
2711
2712static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002713 struct tss_segment_32 *tss)
2714{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002715 int ret;
2716
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002717 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002718 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002719 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002720 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002721
2722 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002723 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2724 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2725 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2726 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2727 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2728 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2729 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2730 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002731
2732 /*
2733 * SDM says that segment selectors are loaded before segment
2734 * descriptors
2735 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002736 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2737 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2738 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2739 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2740 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2741 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2742 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002743
2744 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002745 * If we're switching between Protected Mode and VM86, we need to make
2746 * sure to update the mode before loading the segment descriptors so
2747 * that the selectors are interpreted correctly.
2748 *
2749 * Need to get rflags to the vcpu struct immediately because it
2750 * influences the CPL which is checked at least when loading the segment
2751 * descriptors and when pushing an error code to the new kernel stack.
2752 *
2753 * TODO Introduce a separate ctxt->ops->set_cpl callback
2754 */
2755 if (ctxt->eflags & X86_EFLAGS_VM)
2756 ctxt->mode = X86EMUL_MODE_VM86;
2757 else
2758 ctxt->mode = X86EMUL_MODE_PROT32;
2759
2760 ctxt->ops->set_rflags(ctxt, ctxt->eflags);
2761
2762 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002763 * Now load segment descriptors. If fault happenes at this stage
2764 * it is handled in a context of new task
2765 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002766 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
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->es, VCPU_SREG_ES);
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->cs, VCPU_SREG_CS);
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->ss, VCPU_SREG_SS);
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->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002779 if (ret != X86EMUL_CONTINUE)
2780 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002781 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002782 if (ret != X86EMUL_CONTINUE)
2783 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002784 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002785 if (ret != X86EMUL_CONTINUE)
2786 return ret;
2787
2788 return X86EMUL_CONTINUE;
2789}
2790
2791static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002792 u16 tss_selector, u16 old_tss_sel,
2793 ulong old_tss_base, struct desc_struct *new_desc)
2794{
Mathias Krause0225fb52012-08-30 01:30:16 +02002795 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002796 struct tss_segment_32 tss_seg;
2797 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002798 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002799
Avi Kivity0f65dd72011-04-20 13:37:53 +03002800 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002801 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002802 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002803 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002804 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002805
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002806 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002807
Avi Kivity0f65dd72011-04-20 13:37:53 +03002808 ret = ops->write_std(ctxt, old_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
Avi Kivity0f65dd72011-04-20 13:37:53 +03002814 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002815 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002816 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002817 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002818 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002819
2820 if (old_tss_sel != 0xffff) {
2821 tss_seg.prev_task_link = old_tss_sel;
2822
Avi Kivity0f65dd72011-04-20 13:37:53 +03002823 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002824 &tss_seg.prev_task_link,
2825 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002826 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002827 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002828 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002829 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002830 }
2831
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002832 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002833}
2834
2835static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002836 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002837 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002838{
Mathias Krause0225fb52012-08-30 01:30:16 +02002839 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002840 struct desc_struct curr_tss_desc, next_tss_desc;
2841 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002842 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002843 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002844 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002845 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002846 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002847
2848 /* FIXME: old_tss_base == ~0 ? */
2849
Avi Kivitye9194642012-06-13 16:29:39 +03002850 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002851 if (ret != X86EMUL_CONTINUE)
2852 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002853 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002854 if (ret != X86EMUL_CONTINUE)
2855 return ret;
2856
2857 /* FIXME: check that next_tss_desc is tss */
2858
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002859 /*
2860 * Check privileges. The three cases are task switch caused by...
2861 *
2862 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2863 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002864 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002865 */
2866 if (reason == TASK_SWITCH_GATE) {
2867 if (idt_index != -1) {
2868 /* Software interrupts */
2869 struct desc_struct task_gate_desc;
2870 int dpl;
2871
2872 ret = read_interrupt_descriptor(ctxt, idt_index,
2873 &task_gate_desc);
2874 if (ret != X86EMUL_CONTINUE)
2875 return ret;
2876
2877 dpl = task_gate_desc.dpl;
2878 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2879 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2880 }
2881 } else if (reason != TASK_SWITCH_IRET) {
2882 int dpl = next_tss_desc.dpl;
2883 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2884 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002885 }
2886
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002887
Gleb Natapovceffb452010-03-18 15:20:19 +02002888 desc_limit = desc_limit_scaled(&next_tss_desc);
2889 if (!next_tss_desc.p ||
2890 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2891 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002892 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002893 return X86EMUL_PROPAGATE_FAULT;
2894 }
2895
2896 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2897 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002898 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002899 }
2900
2901 if (reason == TASK_SWITCH_IRET)
2902 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2903
2904 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002905 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002906 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2907 old_tss_sel = 0xffff;
2908
2909 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002910 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002911 old_tss_base, &next_tss_desc);
2912 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002913 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002914 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002915 if (ret != X86EMUL_CONTINUE)
2916 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002917
2918 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2919 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2920
2921 if (reason != TASK_SWITCH_IRET) {
2922 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002923 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002924 }
2925
Avi Kivity717746e2011-04-20 13:37:53 +03002926 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002927 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002928
Jan Kiszkae269fb22010-04-14 15:51:09 +02002929 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002930 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2931 ctxt->lock_prefix = 0;
2932 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002933 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002934 }
2935
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002936 return ret;
2937}
2938
2939int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002940 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002941 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002942{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002943 int rc;
2944
Avi Kivitydd856ef2012-08-27 23:46:17 +03002945 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002946 ctxt->_eip = ctxt->eip;
2947 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002948
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002949 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002950 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002951
Avi Kivitydd856ef2012-08-27 23:46:17 +03002952 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002953 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002954 writeback_registers(ctxt);
2955 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002956
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002957 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002958}
2959
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002960static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2961 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002962{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002963 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002964
Avi Kivitydd856ef2012-08-27 23:46:17 +03002965 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2966 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002967}
2968
Avi Kivity7af04fc2010-08-18 14:16:35 +03002969static int em_das(struct x86_emulate_ctxt *ctxt)
2970{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002971 u8 al, old_al;
2972 bool af, cf, old_cf;
2973
2974 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002975 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002976
2977 old_al = al;
2978 old_cf = cf;
2979 cf = false;
2980 af = ctxt->eflags & X86_EFLAGS_AF;
2981 if ((al & 0x0f) > 9 || af) {
2982 al -= 6;
2983 cf = old_cf | (al >= 250);
2984 af = true;
2985 } else {
2986 af = false;
2987 }
2988 if (old_al > 0x99 || old_cf) {
2989 al -= 0x60;
2990 cf = true;
2991 }
2992
Avi Kivity9dac77f2011-06-01 15:34:25 +03002993 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002994 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002995 ctxt->src.type = OP_IMM;
2996 ctxt->src.val = 0;
2997 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02002998 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03002999 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
3000 if (cf)
3001 ctxt->eflags |= X86_EFLAGS_CF;
3002 if (af)
3003 ctxt->eflags |= X86_EFLAGS_AF;
3004 return X86EMUL_CONTINUE;
3005}
3006
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003007static int em_aam(struct x86_emulate_ctxt *ctxt)
3008{
3009 u8 al, ah;
3010
3011 if (ctxt->src.val == 0)
3012 return emulate_de(ctxt);
3013
3014 al = ctxt->dst.val & 0xff;
3015 ah = al / ctxt->src.val;
3016 al %= ctxt->src.val;
3017
3018 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
3019
3020 /* Set PF, ZF, SF */
3021 ctxt->src.type = OP_IMM;
3022 ctxt->src.val = 0;
3023 ctxt->src.bytes = 1;
3024 fastop(ctxt, em_or);
3025
3026 return X86EMUL_CONTINUE;
3027}
3028
Gleb Natapov7f662272012-12-10 11:42:30 +02003029static int em_aad(struct x86_emulate_ctxt *ctxt)
3030{
3031 u8 al = ctxt->dst.val & 0xff;
3032 u8 ah = (ctxt->dst.val >> 8) & 0xff;
3033
3034 al = (al + (ah * ctxt->src.val)) & 0xff;
3035
3036 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
3037
Gleb Natapovf583c292013-02-13 17:50:39 +02003038 /* Set PF, ZF, SF */
3039 ctxt->src.type = OP_IMM;
3040 ctxt->src.val = 0;
3041 ctxt->src.bytes = 1;
3042 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02003043
3044 return X86EMUL_CONTINUE;
3045}
3046
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003047static int em_call(struct x86_emulate_ctxt *ctxt)
3048{
3049 long rel = ctxt->src.val;
3050
3051 ctxt->src.val = (unsigned long)ctxt->_eip;
3052 jmp_rel(ctxt, rel);
3053 return em_push(ctxt);
3054}
3055
Avi Kivity0ef753b2010-08-18 14:51:45 +03003056static int em_call_far(struct x86_emulate_ctxt *ctxt)
3057{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003058 u16 sel, old_cs;
3059 ulong old_eip;
3060 int rc;
3061
Avi Kivity1aa36612011-04-27 13:20:30 +03003062 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003063 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003064
Avi Kivity9dac77f2011-06-01 15:34:25 +03003065 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003066 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03003067 return X86EMUL_CONTINUE;
3068
Avi Kivity9dac77f2011-06-01 15:34:25 +03003069 ctxt->_eip = 0;
3070 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003071
Avi Kivity9dac77f2011-06-01 15:34:25 +03003072 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003073 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003074 if (rc != X86EMUL_CONTINUE)
3075 return rc;
3076
Avi Kivity9dac77f2011-06-01 15:34:25 +03003077 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003078 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003079}
3080
Avi Kivity40ece7c2010-08-18 15:12:09 +03003081static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3082{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003083 int rc;
3084
Avi Kivity9dac77f2011-06-01 15:34:25 +03003085 ctxt->dst.type = OP_REG;
3086 ctxt->dst.addr.reg = &ctxt->_eip;
3087 ctxt->dst.bytes = ctxt->op_bytes;
3088 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003089 if (rc != X86EMUL_CONTINUE)
3090 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003091 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003092 return X86EMUL_CONTINUE;
3093}
3094
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003095static int em_xchg(struct x86_emulate_ctxt *ctxt)
3096{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003097 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003098 ctxt->src.val = ctxt->dst.val;
3099 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003100
3101 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003102 ctxt->dst.val = ctxt->src.orig_val;
3103 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003104 return X86EMUL_CONTINUE;
3105}
3106
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003107static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3108{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003109 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003110 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003111}
3112
Avi Kivity61429142010-08-19 15:13:00 +03003113static int em_cwd(struct x86_emulate_ctxt *ctxt)
3114{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003115 ctxt->dst.type = OP_REG;
3116 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003117 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003118 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003119
3120 return X86EMUL_CONTINUE;
3121}
3122
Avi Kivity48bb5d32010-08-18 18:54:34 +03003123static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3124{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003125 u64 tsc = 0;
3126
Avi Kivity717746e2011-04-20 13:37:53 +03003127 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003128 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3129 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003130 return X86EMUL_CONTINUE;
3131}
3132
Avi Kivity222d21a2011-11-10 14:57:30 +02003133static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3134{
3135 u64 pmc;
3136
Avi Kivitydd856ef2012-08-27 23:46:17 +03003137 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003138 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003139 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3140 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003141 return X86EMUL_CONTINUE;
3142}
3143
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003144static int em_mov(struct x86_emulate_ctxt *ctxt)
3145{
Stefan Hajnoczi49597d82012-04-09 18:40:00 +03003146 memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003147 return X86EMUL_CONTINUE;
3148}
3149
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003150static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3151{
3152 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3153 return emulate_gp(ctxt, 0);
3154
3155 /* Disable writeback. */
3156 ctxt->dst.type = OP_NONE;
3157 return X86EMUL_CONTINUE;
3158}
3159
3160static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3161{
3162 unsigned long val;
3163
3164 if (ctxt->mode == X86EMUL_MODE_PROT64)
3165 val = ctxt->src.val & ~0ULL;
3166 else
3167 val = ctxt->src.val & ~0U;
3168
3169 /* #UD condition is already handled. */
3170 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3171 return emulate_gp(ctxt, 0);
3172
3173 /* Disable writeback. */
3174 ctxt->dst.type = OP_NONE;
3175 return X86EMUL_CONTINUE;
3176}
3177
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003178static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3179{
3180 u64 msr_data;
3181
Avi Kivitydd856ef2012-08-27 23:46:17 +03003182 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3183 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3184 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003185 return emulate_gp(ctxt, 0);
3186
3187 return X86EMUL_CONTINUE;
3188}
3189
3190static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3191{
3192 u64 msr_data;
3193
Avi Kivitydd856ef2012-08-27 23:46:17 +03003194 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003195 return emulate_gp(ctxt, 0);
3196
Avi Kivitydd856ef2012-08-27 23:46:17 +03003197 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3198 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003199 return X86EMUL_CONTINUE;
3200}
3201
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003202static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3203{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003204 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003205 return emulate_ud(ctxt);
3206
Avi Kivity9dac77f2011-06-01 15:34:25 +03003207 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003208 return X86EMUL_CONTINUE;
3209}
3210
3211static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3212{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003213 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003214
Avi Kivity9dac77f2011-06-01 15:34:25 +03003215 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003216 return emulate_ud(ctxt);
3217
Avi Kivity9dac77f2011-06-01 15:34:25 +03003218 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003219 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3220
3221 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003222 ctxt->dst.type = OP_NONE;
3223 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003224}
3225
Avi Kivitya14e5792012-06-13 12:28:33 +03003226static int em_lldt(struct x86_emulate_ctxt *ctxt)
3227{
3228 u16 sel = ctxt->src.val;
3229
3230 /* Disable writeback. */
3231 ctxt->dst.type = OP_NONE;
3232 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3233}
3234
Avi Kivity80890002012-06-13 16:33:29 +03003235static int em_ltr(struct x86_emulate_ctxt *ctxt)
3236{
3237 u16 sel = ctxt->src.val;
3238
3239 /* Disable writeback. */
3240 ctxt->dst.type = OP_NONE;
3241 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3242}
3243
Avi Kivity38503912011-03-31 18:48:09 +02003244static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3245{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003246 int rc;
3247 ulong linear;
3248
Avi Kivity9dac77f2011-06-01 15:34:25 +03003249 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003250 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003251 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003252 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003253 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003254 return X86EMUL_CONTINUE;
3255}
3256
Avi Kivity2d04a052011-04-20 15:32:49 +03003257static int em_clts(struct x86_emulate_ctxt *ctxt)
3258{
3259 ulong cr0;
3260
3261 cr0 = ctxt->ops->get_cr(ctxt, 0);
3262 cr0 &= ~X86_CR0_TS;
3263 ctxt->ops->set_cr(ctxt, 0, cr0);
3264 return X86EMUL_CONTINUE;
3265}
3266
Avi Kivity26d05cc2011-04-21 12:07:59 +03003267static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3268{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003269 int rc;
3270
Avi Kivity9dac77f2011-06-01 15:34:25 +03003271 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003272 return X86EMUL_UNHANDLEABLE;
3273
3274 rc = ctxt->ops->fix_hypercall(ctxt);
3275 if (rc != X86EMUL_CONTINUE)
3276 return rc;
3277
3278 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003279 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003280 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003281 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003282 return X86EMUL_CONTINUE;
3283}
3284
Avi Kivity96051572012-06-10 17:21:18 +03003285static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3286 void (*get)(struct x86_emulate_ctxt *ctxt,
3287 struct desc_ptr *ptr))
3288{
3289 struct desc_ptr desc_ptr;
3290
3291 if (ctxt->mode == X86EMUL_MODE_PROT64)
3292 ctxt->op_bytes = 8;
3293 get(ctxt, &desc_ptr);
3294 if (ctxt->op_bytes == 2) {
3295 ctxt->op_bytes = 4;
3296 desc_ptr.address &= 0x00ffffff;
3297 }
3298 /* Disable writeback. */
3299 ctxt->dst.type = OP_NONE;
3300 return segmented_write(ctxt, ctxt->dst.addr.mem,
3301 &desc_ptr, 2 + ctxt->op_bytes);
3302}
3303
3304static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3305{
3306 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3307}
3308
3309static int em_sidt(struct x86_emulate_ctxt *ctxt)
3310{
3311 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3312}
3313
Avi Kivity26d05cc2011-04-21 12:07:59 +03003314static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3315{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003316 struct desc_ptr desc_ptr;
3317 int rc;
3318
Avi Kivity510425f2012-06-07 17:04:36 +03003319 if (ctxt->mode == X86EMUL_MODE_PROT64)
3320 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003321 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003322 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003323 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003324 if (rc != X86EMUL_CONTINUE)
3325 return rc;
3326 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3327 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003328 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003329 return X86EMUL_CONTINUE;
3330}
3331
Avi Kivity5ef39c72011-04-21 12:21:50 +03003332static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003333{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003334 int rc;
3335
Avi Kivity5ef39c72011-04-21 12:21:50 +03003336 rc = ctxt->ops->fix_hypercall(ctxt);
3337
Avi Kivity26d05cc2011-04-21 12:07:59 +03003338 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003339 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003340 return rc;
3341}
3342
3343static int em_lidt(struct x86_emulate_ctxt *ctxt)
3344{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003345 struct desc_ptr desc_ptr;
3346 int rc;
3347
Avi Kivity510425f2012-06-07 17:04:36 +03003348 if (ctxt->mode == X86EMUL_MODE_PROT64)
3349 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003350 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003351 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003352 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003353 if (rc != X86EMUL_CONTINUE)
3354 return rc;
3355 ctxt->ops->set_idt(ctxt, &desc_ptr);
3356 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003357 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003358 return X86EMUL_CONTINUE;
3359}
3360
3361static int em_smsw(struct x86_emulate_ctxt *ctxt)
3362{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003363 ctxt->dst.bytes = 2;
3364 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003365 return X86EMUL_CONTINUE;
3366}
3367
3368static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3369{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003370 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003371 | (ctxt->src.val & 0x0f));
3372 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003373 return X86EMUL_CONTINUE;
3374}
3375
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003376static int em_loop(struct x86_emulate_ctxt *ctxt)
3377{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003378 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3379 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003380 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3381 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003382
3383 return X86EMUL_CONTINUE;
3384}
3385
3386static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3387{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003388 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003389 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003390
3391 return X86EMUL_CONTINUE;
3392}
3393
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003394static int em_in(struct x86_emulate_ctxt *ctxt)
3395{
3396 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3397 &ctxt->dst.val))
3398 return X86EMUL_IO_NEEDED;
3399
3400 return X86EMUL_CONTINUE;
3401}
3402
3403static int em_out(struct x86_emulate_ctxt *ctxt)
3404{
3405 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3406 &ctxt->src.val, 1);
3407 /* Disable writeback. */
3408 ctxt->dst.type = OP_NONE;
3409 return X86EMUL_CONTINUE;
3410}
3411
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003412static int em_cli(struct x86_emulate_ctxt *ctxt)
3413{
3414 if (emulator_bad_iopl(ctxt))
3415 return emulate_gp(ctxt, 0);
3416
3417 ctxt->eflags &= ~X86_EFLAGS_IF;
3418 return X86EMUL_CONTINUE;
3419}
3420
3421static int em_sti(struct x86_emulate_ctxt *ctxt)
3422{
3423 if (emulator_bad_iopl(ctxt))
3424 return emulate_gp(ctxt, 0);
3425
3426 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3427 ctxt->eflags |= X86_EFLAGS_IF;
3428 return X86EMUL_CONTINUE;
3429}
3430
Avi Kivity6d6eede2012-06-07 14:11:36 +03003431static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3432{
3433 u32 eax, ebx, ecx, edx;
3434
Avi Kivitydd856ef2012-08-27 23:46:17 +03003435 eax = reg_read(ctxt, VCPU_REGS_RAX);
3436 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003437 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003438 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3439 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3440 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3441 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003442 return X86EMUL_CONTINUE;
3443}
3444
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003445static int em_lahf(struct x86_emulate_ctxt *ctxt)
3446{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003447 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3448 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003449 return X86EMUL_CONTINUE;
3450}
3451
Avi Kivity92998362012-06-13 12:25:06 +03003452static int em_bswap(struct x86_emulate_ctxt *ctxt)
3453{
3454 switch (ctxt->op_bytes) {
3455#ifdef CONFIG_X86_64
3456 case 8:
3457 asm("bswap %0" : "+r"(ctxt->dst.val));
3458 break;
3459#endif
3460 default:
3461 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3462 break;
3463 }
3464 return X86EMUL_CONTINUE;
3465}
3466
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003467static bool valid_cr(int nr)
3468{
3469 switch (nr) {
3470 case 0:
3471 case 2 ... 4:
3472 case 8:
3473 return true;
3474 default:
3475 return false;
3476 }
3477}
3478
3479static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3480{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003481 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003482 return emulate_ud(ctxt);
3483
3484 return X86EMUL_CONTINUE;
3485}
3486
3487static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3488{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003489 u64 new_val = ctxt->src.val64;
3490 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003491 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003492
3493 static u64 cr_reserved_bits[] = {
3494 0xffffffff00000000ULL,
3495 0, 0, 0, /* CR3 checked later */
3496 CR4_RESERVED_BITS,
3497 0, 0, 0,
3498 CR8_RESERVED_BITS,
3499 };
3500
3501 if (!valid_cr(cr))
3502 return emulate_ud(ctxt);
3503
3504 if (new_val & cr_reserved_bits[cr])
3505 return emulate_gp(ctxt, 0);
3506
3507 switch (cr) {
3508 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003509 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003510 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3511 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3512 return emulate_gp(ctxt, 0);
3513
Avi Kivity717746e2011-04-20 13:37:53 +03003514 cr4 = ctxt->ops->get_cr(ctxt, 4);
3515 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003516
3517 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3518 !(cr4 & X86_CR4_PAE))
3519 return emulate_gp(ctxt, 0);
3520
3521 break;
3522 }
3523 case 3: {
3524 u64 rsvd = 0;
3525
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003526 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3527 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003528 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003529 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003530 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003531 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003532 rsvd = CR3_NONPAE_RESERVED_BITS;
3533
3534 if (new_val & rsvd)
3535 return emulate_gp(ctxt, 0);
3536
3537 break;
3538 }
3539 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003540 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003541
3542 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3543 return emulate_gp(ctxt, 0);
3544
3545 break;
3546 }
3547 }
3548
3549 return X86EMUL_CONTINUE;
3550}
3551
Joerg Roedel3b88e412011-04-04 12:39:29 +02003552static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3553{
3554 unsigned long dr7;
3555
Avi Kivity717746e2011-04-20 13:37:53 +03003556 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003557
3558 /* Check if DR7.Global_Enable is set */
3559 return dr7 & (1 << 13);
3560}
3561
3562static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3563{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003564 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003565 u64 cr4;
3566
3567 if (dr > 7)
3568 return emulate_ud(ctxt);
3569
Avi Kivity717746e2011-04-20 13:37:53 +03003570 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003571 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3572 return emulate_ud(ctxt);
3573
3574 if (check_dr7_gd(ctxt))
3575 return emulate_db(ctxt);
3576
3577 return X86EMUL_CONTINUE;
3578}
3579
3580static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3581{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003582 u64 new_val = ctxt->src.val64;
3583 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003584
3585 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3586 return emulate_gp(ctxt, 0);
3587
3588 return check_dr_read(ctxt);
3589}
3590
Joerg Roedel01de8b02011-04-04 12:39:31 +02003591static int check_svme(struct x86_emulate_ctxt *ctxt)
3592{
3593 u64 efer;
3594
Avi Kivity717746e2011-04-20 13:37:53 +03003595 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003596
3597 if (!(efer & EFER_SVME))
3598 return emulate_ud(ctxt);
3599
3600 return X86EMUL_CONTINUE;
3601}
3602
3603static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3604{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003605 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003606
3607 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003608 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003609 return emulate_gp(ctxt, 0);
3610
3611 return check_svme(ctxt);
3612}
3613
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003614static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3615{
Avi Kivity717746e2011-04-20 13:37:53 +03003616 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003617
Avi Kivity717746e2011-04-20 13:37:53 +03003618 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003619 return emulate_ud(ctxt);
3620
3621 return X86EMUL_CONTINUE;
3622}
3623
Joerg Roedel80612522011-04-04 12:39:33 +02003624static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3625{
Avi Kivity717746e2011-04-20 13:37:53 +03003626 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003627 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003628
Avi Kivity717746e2011-04-20 13:37:53 +03003629 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02003630 (rcx > 3))
3631 return emulate_gp(ctxt, 0);
3632
3633 return X86EMUL_CONTINUE;
3634}
3635
Joerg Roedelf6511932011-04-04 12:39:35 +02003636static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3637{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003638 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3639 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003640 return emulate_gp(ctxt, 0);
3641
3642 return X86EMUL_CONTINUE;
3643}
3644
3645static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3646{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003647 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3648 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003649 return emulate_gp(ctxt, 0);
3650
3651 return X86EMUL_CONTINUE;
3652}
3653
Avi Kivity73fba5f2010-07-29 15:11:53 +03003654#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003655#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003656#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3657 .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003658#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003659#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003660#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3661#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003662#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003663#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003664#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003665#define II(_f, _e, _i) \
3666 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003667#define IIP(_f, _e, _i, _p) \
3668 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3669 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003670#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003671
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003672#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003673#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003674#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003675#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003676#define I2bvIP(_f, _e, _i, _p) \
3677 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003678
Avi Kivityfb864fb2013-01-04 16:18:54 +02003679#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3680 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3681 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003682
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003683static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003684 DI(SrcNone | Priv, monitor),
3685 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003686 N, N, N, N, N, N,
3687};
3688
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003689static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003690 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
3691 II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall),
3692 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3693 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3694 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3695 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3696 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3697 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003698};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003699
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003700static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003701 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003702 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003703 N, N, N, N, N, N,
3704};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003705
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003706static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003707 F(Lock, em_add),
3708 F(Lock | PageTable, em_or),
3709 F(Lock, em_adc),
3710 F(Lock, em_sbb),
3711 F(Lock | PageTable, em_and),
3712 F(Lock, em_sub),
3713 F(Lock, em_xor),
3714 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003715};
3716
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003717static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003718 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003719};
3720
Avi Kivity007a3b52013-01-19 19:51:51 +02003721static const struct opcode group2[] = {
3722 F(DstMem | ModRM, em_rol),
3723 F(DstMem | ModRM, em_ror),
3724 F(DstMem | ModRM, em_rcl),
3725 F(DstMem | ModRM, em_rcr),
3726 F(DstMem | ModRM, em_shl),
3727 F(DstMem | ModRM, em_shr),
3728 F(DstMem | ModRM, em_shl),
3729 F(DstMem | ModRM, em_sar),
3730};
3731
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003732static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003733 F(DstMem | SrcImm | NoWrite, em_test),
3734 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003735 F(DstMem | SrcNone | Lock, em_not),
3736 F(DstMem | SrcNone | Lock, em_neg),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003737 I(SrcMem, em_mul_ex),
3738 I(SrcMem, em_imul_ex),
3739 I(SrcMem, em_div_ex),
3740 I(SrcMem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003741};
3742
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003743static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003744 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3745 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003746 N, N, N, N, N, N,
3747};
3748
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003749static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003750 F(DstMem | SrcNone | Lock, em_inc),
3751 F(DstMem | SrcNone | Lock, em_dec),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003752 I(SrcMem | Stack, em_grp45),
3753 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3754 I(SrcMem | Stack, em_grp45),
3755 I(SrcMemFAddr | ImplicitOps, em_grp45),
Gleb Natapov188424b2013-04-11 12:32:14 +03003756 I(SrcMem | Stack, em_grp45), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003757};
3758
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003759static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003760 DI(Prot, sldt),
3761 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003762 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003763 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003764 N, N, N, N,
3765};
3766
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003767static const struct group_dual group7 = { {
Avi Kivity96051572012-06-10 17:21:18 +03003768 II(Mov | DstMem | Priv, em_sgdt, sgdt),
3769 II(Mov | DstMem | Priv, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003770 II(SrcMem | Priv, em_lgdt, lgdt),
3771 II(SrcMem | Priv, em_lidt, lidt),
3772 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3773 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3774 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003775}, {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003776 I(SrcNone | Priv | VendorSpecific, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003777 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003778 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003779 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3780 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3781 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003782} };
3783
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003784static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003785 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003786 F(DstMem | SrcImmByte | NoWrite, em_bt),
3787 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3788 F(DstMem | SrcImmByte | Lock, em_btr),
3789 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003790};
3791
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003792static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003793 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003794}, {
3795 N, N, N, N, N, N, N, N,
3796} };
3797
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003798static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003799 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003800 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003801};
3802
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003803static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003804 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003805};
3806
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003807static const struct gprefix pfx_vmovntpx = {
Avi Kivity3e114eb2012-04-09 18:40:01 +03003808 I(0, em_mov), N, N, N,
3809};
3810
Gleb Natapov045a2822012-12-20 16:57:43 +02003811static const struct escape escape_d9 = { {
3812 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3813}, {
3814 /* 0xC0 - 0xC7 */
3815 N, N, N, N, N, N, N, N,
3816 /* 0xC8 - 0xCF */
3817 N, N, N, N, N, N, N, N,
3818 /* 0xD0 - 0xC7 */
3819 N, N, N, N, N, N, N, N,
3820 /* 0xD8 - 0xDF */
3821 N, N, N, N, N, N, N, N,
3822 /* 0xE0 - 0xE7 */
3823 N, N, N, N, N, N, N, N,
3824 /* 0xE8 - 0xEF */
3825 N, N, N, N, N, N, N, N,
3826 /* 0xF0 - 0xF7 */
3827 N, N, N, N, N, N, N, N,
3828 /* 0xF8 - 0xFF */
3829 N, N, N, N, N, N, N, N,
3830} };
3831
3832static const struct escape escape_db = { {
3833 N, N, N, N, N, N, N, N,
3834}, {
3835 /* 0xC0 - 0xC7 */
3836 N, N, N, N, N, N, N, N,
3837 /* 0xC8 - 0xCF */
3838 N, N, N, N, N, N, N, N,
3839 /* 0xD0 - 0xC7 */
3840 N, N, N, N, N, N, N, N,
3841 /* 0xD8 - 0xDF */
3842 N, N, N, N, N, N, N, N,
3843 /* 0xE0 - 0xE7 */
3844 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3845 /* 0xE8 - 0xEF */
3846 N, N, N, N, N, N, N, N,
3847 /* 0xF0 - 0xF7 */
3848 N, N, N, N, N, N, N, N,
3849 /* 0xF8 - 0xFF */
3850 N, N, N, N, N, N, N, N,
3851} };
3852
3853static const struct escape escape_dd = { {
3854 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3855}, {
3856 /* 0xC0 - 0xC7 */
3857 N, N, N, N, N, N, N, N,
3858 /* 0xC8 - 0xCF */
3859 N, N, N, N, N, N, N, N,
3860 /* 0xD0 - 0xC7 */
3861 N, N, N, N, N, N, N, N,
3862 /* 0xD8 - 0xDF */
3863 N, N, N, N, N, N, N, N,
3864 /* 0xE0 - 0xE7 */
3865 N, N, N, N, N, N, N, N,
3866 /* 0xE8 - 0xEF */
3867 N, N, N, N, N, N, N, N,
3868 /* 0xF0 - 0xF7 */
3869 N, N, N, N, N, N, N, N,
3870 /* 0xF8 - 0xFF */
3871 N, N, N, N, N, N, N, N,
3872} };
3873
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003874static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003875 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003876 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003877 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3878 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003879 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003880 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003881 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3882 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003883 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003884 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003885 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3886 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003887 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003888 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003889 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3890 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003891 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003892 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003893 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003894 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003895 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003896 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003897 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003898 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003899 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02003900 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003901 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003902 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003903 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003904 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003905 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003906 I(ImplicitOps | Stack | No64, em_pusha),
3907 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003908 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3909 N, N, N, N,
3910 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003911 I(SrcImm | Mov | Stack, em_push),
3912 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003913 I(SrcImmByte | Mov | Stack, em_push),
3914 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003915 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003916 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003917 /* 0x70 - 0x7F */
3918 X16(D(SrcImmByte)),
3919 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003920 G(ByteOp | DstMem | SrcImm, group1),
3921 G(DstMem | SrcImm, group1),
3922 G(ByteOp | DstMem | SrcImm | No64, group1),
3923 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003924 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003925 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003926 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003927 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003928 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003929 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003930 D(ModRM | SrcMem | NoAccess | DstReg),
3931 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3932 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003933 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003934 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003935 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003936 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003937 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003938 II(ImplicitOps | Stack, em_pushf, pushf),
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003939 II(ImplicitOps | Stack, em_popf, popf), N, I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003940 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003941 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003942 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003943 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003944 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003945 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003946 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003947 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3948 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003949 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003950 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003951 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003952 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02003953 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003954 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003955 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003956 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003957 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003958 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3959 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003960 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003961 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003962 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
3963 N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003964 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003965 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003966 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003967 G(Src2One | ByteOp, group2), G(Src2One, group2),
3968 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003969 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02003970 I(DstAcc | SrcImmUByte | No64, em_aad),
3971 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02003972 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003973 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02003974 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003975 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003976 X3(I(SrcImmByte, em_loop)),
3977 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003978 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3979 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003980 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003981 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003982 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003983 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3984 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003985 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003986 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003987 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3988 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003989 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003990 D(ImplicitOps), D(ImplicitOps),
3991 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003992 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3993};
3994
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003995static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003996 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003997 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003998 N, I(ImplicitOps | VendorSpecific, em_syscall),
3999 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004000 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004001 N, D(ImplicitOps | ModRM), N, N,
4002 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004003 N, N, N, N, N, N, N, N,
4004 D(ImplicitOps | ModRM), N, N, N, N, N, N, D(ImplicitOps | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004005 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004006 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02004007 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09004008 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
4009 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004010 N, N, N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03004011 N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
4012 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004013 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004014 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004015 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004016 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004017 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004018 I(ImplicitOps | VendorSpecific, em_sysenter),
4019 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004020 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004021 N, N, N, N, N, N, N, N,
4022 /* 0x40 - 0x4F */
4023 X16(D(DstReg | SrcMem | ModRM | Mov)),
4024 /* 0x50 - 0x5F */
4025 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4026 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004027 N, N, N, N,
4028 N, N, N, N,
4029 N, N, N, N,
4030 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004031 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004032 N, N, N, N,
4033 N, N, N, N,
4034 N, N, N, N,
4035 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004036 /* 0x80 - 0x8F */
4037 X16(D(SrcImm)),
4038 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004039 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004040 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004041 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004042 II(ImplicitOps, em_cpuid, cpuid),
4043 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004044 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4045 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004046 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004047 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004048 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004049 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004050 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4051 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Avi Kivity4d758342013-01-19 19:51:55 +02004052 D(ModRM), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004053 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09004054 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004055 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004056 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004057 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4058 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004059 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004060 /* 0xB8 - 0xBF */
4061 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004062 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004063 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
4064 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004065 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004066 /* 0xC0 - 0xC7 */
Avi Kivity739ae402010-08-26 11:56:13 +03004067 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a52010-08-17 09:19:34 +08004068 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004069 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004070 /* 0xC8 - 0xCF */
4071 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004072 /* 0xD0 - 0xDF */
4073 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4074 /* 0xE0 - 0xEF */
4075 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4076 /* 0xF0 - 0xFF */
4077 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4078};
4079
4080#undef D
4081#undef N
4082#undef G
4083#undef GD
4084#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004085#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004086#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004087
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004088#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004089#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004090#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004091#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004092#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004093
Avi Kivity9dac77f2011-06-01 15:34:25 +03004094static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004095{
4096 unsigned size;
4097
Avi Kivity9dac77f2011-06-01 15:34:25 +03004098 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004099 if (size == 8)
4100 size = 4;
4101 return size;
4102}
4103
4104static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4105 unsigned size, bool sign_extension)
4106{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004107 int rc = X86EMUL_CONTINUE;
4108
4109 op->type = OP_IMM;
4110 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004111 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004112 /* NB. Immediates are sign-extended as necessary. */
4113 switch (op->bytes) {
4114 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004115 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004116 break;
4117 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004118 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004119 break;
4120 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004121 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004122 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004123 case 8:
4124 op->val = insn_fetch(s64, ctxt);
4125 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004126 }
4127 if (!sign_extension) {
4128 switch (op->bytes) {
4129 case 1:
4130 op->val &= 0xff;
4131 break;
4132 case 2:
4133 op->val &= 0xffff;
4134 break;
4135 case 4:
4136 op->val &= 0xffffffff;
4137 break;
4138 }
4139 }
4140done:
4141 return rc;
4142}
4143
Avi Kivitya99455492011-09-13 10:45:41 +03004144static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4145 unsigned d)
4146{
4147 int rc = X86EMUL_CONTINUE;
4148
4149 switch (d) {
4150 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004151 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03004152 break;
4153 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004154 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03004155 break;
4156 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004157 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004158 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03004159 *op = ctxt->memop;
4160 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03004161 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya99455492011-09-13 10:45:41 +03004162 fetch_bit_operand(ctxt);
4163 op->orig_val = op->val;
4164 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004165 case OpMem64:
4166 ctxt->memop.bytes = 8;
4167 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03004168 case OpAcc:
4169 op->type = OP_REG;
4170 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004171 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03004172 fetch_register_operand(op);
4173 op->orig_val = op->val;
4174 break;
4175 case OpDI:
4176 op->type = OP_MEM;
4177 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4178 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004179 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya99455492011-09-13 10:45:41 +03004180 op->addr.mem.seg = VCPU_SREG_ES;
4181 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004182 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03004183 break;
4184 case OpDX:
4185 op->type = OP_REG;
4186 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004187 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03004188 fetch_register_operand(op);
4189 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004190 case OpCL:
4191 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004192 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004193 break;
4194 case OpImmByte:
4195 rc = decode_imm(ctxt, op, 1, true);
4196 break;
4197 case OpOne:
4198 op->bytes = 1;
4199 op->val = 1;
4200 break;
4201 case OpImm:
4202 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4203 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004204 case OpImm64:
4205 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4206 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004207 case OpMem8:
4208 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004209 if (ctxt->memop.type == OP_REG) {
4210 ctxt->memop.addr.reg = decode_register(ctxt, ctxt->modrm_rm, 1);
4211 fetch_register_operand(&ctxt->memop);
4212 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004213 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004214 case OpMem16:
4215 ctxt->memop.bytes = 2;
4216 goto mem_common;
4217 case OpMem32:
4218 ctxt->memop.bytes = 4;
4219 goto mem_common;
4220 case OpImmU16:
4221 rc = decode_imm(ctxt, op, 2, false);
4222 break;
4223 case OpImmU:
4224 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4225 break;
4226 case OpSI:
4227 op->type = OP_MEM;
4228 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4229 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004230 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Avi Kivity0fe59122011-09-13 10:45:47 +03004231 op->addr.mem.seg = seg_override(ctxt);
4232 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004233 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004234 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004235 case OpXLat:
4236 op->type = OP_MEM;
4237 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4238 op->addr.mem.ea =
4239 register_address(ctxt,
4240 reg_read(ctxt, VCPU_REGS_RBX) +
4241 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
4242 op->addr.mem.seg = seg_override(ctxt);
4243 op->val = 0;
4244 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004245 case OpImmFAddr:
4246 op->type = OP_IMM;
4247 op->addr.mem.ea = ctxt->_eip;
4248 op->bytes = ctxt->op_bytes + 2;
4249 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4250 break;
4251 case OpMemFAddr:
4252 ctxt->memop.bytes = ctxt->op_bytes + 2;
4253 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004254 case OpES:
4255 op->val = VCPU_SREG_ES;
4256 break;
4257 case OpCS:
4258 op->val = VCPU_SREG_CS;
4259 break;
4260 case OpSS:
4261 op->val = VCPU_SREG_SS;
4262 break;
4263 case OpDS:
4264 op->val = VCPU_SREG_DS;
4265 break;
4266 case OpFS:
4267 op->val = VCPU_SREG_FS;
4268 break;
4269 case OpGS:
4270 op->val = VCPU_SREG_GS;
4271 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004272 case OpImplicit:
4273 /* Special instructions do their own operand decoding. */
4274 default:
4275 op->type = OP_NONE; /* Disable writeback. */
4276 break;
4277 }
4278
4279done:
4280 return rc;
4281}
4282
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004283int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004284{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004285 int rc = X86EMUL_CONTINUE;
4286 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004287 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004288 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03004289 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004290
Avi Kivityf09ed832011-09-13 10:45:40 +03004291 ctxt->memop.type = OP_NONE;
4292 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004293 ctxt->_eip = ctxt->eip;
4294 ctxt->fetch.start = ctxt->_eip;
4295 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01004296 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004297 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004298
4299 switch (mode) {
4300 case X86EMUL_MODE_REAL:
4301 case X86EMUL_MODE_VM86:
4302 case X86EMUL_MODE_PROT16:
4303 def_op_bytes = def_ad_bytes = 2;
4304 break;
4305 case X86EMUL_MODE_PROT32:
4306 def_op_bytes = def_ad_bytes = 4;
4307 break;
4308#ifdef CONFIG_X86_64
4309 case X86EMUL_MODE_PROT64:
4310 def_op_bytes = 4;
4311 def_ad_bytes = 8;
4312 break;
4313#endif
4314 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004315 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004316 }
4317
Avi Kivity9dac77f2011-06-01 15:34:25 +03004318 ctxt->op_bytes = def_op_bytes;
4319 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004320
4321 /* Legacy prefixes. */
4322 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004323 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004324 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004325 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004326 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004327 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004328 break;
4329 case 0x67: /* address-size override */
4330 if (mode == X86EMUL_MODE_PROT64)
4331 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004332 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004333 else
4334 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004335 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004336 break;
4337 case 0x26: /* ES override */
4338 case 0x2e: /* CS override */
4339 case 0x36: /* SS override */
4340 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004341 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004342 break;
4343 case 0x64: /* FS override */
4344 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004345 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004346 break;
4347 case 0x40 ... 0x4f: /* REX */
4348 if (mode != X86EMUL_MODE_PROT64)
4349 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004350 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004351 continue;
4352 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004353 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004354 break;
4355 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004356 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004357 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004358 break;
4359 default:
4360 goto done_prefixes;
4361 }
4362
4363 /* Any legacy prefix after a REX prefix nullifies its effect. */
4364
Avi Kivity9dac77f2011-06-01 15:34:25 +03004365 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004366 }
4367
4368done_prefixes:
4369
4370 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004371 if (ctxt->rex_prefix & 8)
4372 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004373
4374 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004375 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004376 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004377 if (ctxt->b == 0x0f) {
4378 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004379 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004380 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004381 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004382 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004383
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004384 if (ctxt->d & ModRM)
4385 ctxt->modrm = insn_fetch(u8, ctxt);
4386
Avi Kivity9dac77f2011-06-01 15:34:25 +03004387 while (ctxt->d & GroupMask) {
4388 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004389 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004390 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004391 opcode = opcode.u.group[goffset];
4392 break;
4393 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004394 goffset = (ctxt->modrm >> 3) & 7;
4395 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004396 opcode = opcode.u.gdual->mod3[goffset];
4397 else
4398 opcode = opcode.u.gdual->mod012[goffset];
4399 break;
4400 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004401 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004402 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004403 break;
4404 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004405 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004406 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004407 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004408 switch (simd_prefix) {
4409 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4410 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4411 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4412 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4413 }
4414 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004415 case Escape:
4416 if (ctxt->modrm > 0xbf)
4417 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4418 else
4419 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4420 break;
Avi Kivity46561642011-04-24 14:09:59 +03004421 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004422 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004423 }
Avi Kivity46561642011-04-24 14:09:59 +03004424
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004425 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004426 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004427 }
4428
Avi Kivity9dac77f2011-06-01 15:34:25 +03004429 ctxt->execute = opcode.u.execute;
4430 ctxt->check_perm = opcode.check_perm;
4431 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004432
4433 /* Unrecognised? */
Gleb Natapov1146a782013-04-11 12:30:01 +03004434 if (ctxt->d == 0 || (ctxt->d & NotImpl))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004435 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004436
Avi Kivity9dac77f2011-06-01 15:34:25 +03004437 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004438 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004439
Avi Kivity9dac77f2011-06-01 15:34:25 +03004440 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
4441 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004442
Avi Kivity9dac77f2011-06-01 15:34:25 +03004443 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004444 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004445 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004446 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03004447 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004448 }
4449
Avi Kivity9dac77f2011-06-01 15:34:25 +03004450 if (ctxt->d & Sse)
4451 ctxt->op_bytes = 16;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004452 else if (ctxt->d & Mmx)
4453 ctxt->op_bytes = 8;
Avi Kivity1253791d2011-03-29 11:41:27 +02004454
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004455 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004456 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004457 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004458 if (!ctxt->has_seg_override)
4459 set_seg_override(ctxt, ctxt->modrm_seg);
4460 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004461 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004462 if (rc != X86EMUL_CONTINUE)
4463 goto done;
4464
Avi Kivity9dac77f2011-06-01 15:34:25 +03004465 if (!ctxt->has_seg_override)
4466 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004467
Avi Kivityf09ed832011-09-13 10:45:40 +03004468 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004469
Avi Kivityf09ed832011-09-13 10:45:40 +03004470 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
4471 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004472
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004473 /*
4474 * Decode and fetch the source operand: register, memory
4475 * or immediate.
4476 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004477 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & 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 /*
4482 * Decode and fetch the second source operand: register, memory
4483 * or immediate.
4484 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004485 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004486 if (rc != X86EMUL_CONTINUE)
4487 goto done;
4488
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004489 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03004490 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004491
4492done:
Avi Kivityf09ed832011-09-13 10:45:40 +03004493 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
4494 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004495
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004496 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004497}
4498
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004499bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4500{
4501 return ctxt->d & PageTable;
4502}
4503
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004504static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4505{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004506 /* The second termination condition only applies for REPE
4507 * and REPNE. Test if the repeat string operation prefix is
4508 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4509 * corresponding termination condition according to:
4510 * - if REPE/REPZ and ZF = 0 then done
4511 * - if REPNE/REPNZ and ZF = 1 then done
4512 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004513 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4514 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4515 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004516 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004517 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004518 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4519 return true;
4520
4521 return false;
4522}
4523
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004524static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4525{
4526 bool fault = false;
4527
4528 ctxt->ops->get_fpu(ctxt);
4529 asm volatile("1: fwait \n\t"
4530 "2: \n\t"
4531 ".pushsection .fixup,\"ax\" \n\t"
4532 "3: \n\t"
4533 "movb $1, %[fault] \n\t"
4534 "jmp 2b \n\t"
4535 ".popsection \n\t"
4536 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004537 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004538 ctxt->ops->put_fpu(ctxt);
4539
4540 if (unlikely(fault))
4541 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4542
4543 return X86EMUL_CONTINUE;
4544}
4545
4546static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4547 struct operand *op)
4548{
4549 if (op->type == OP_MM)
4550 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4551}
4552
Avi Kivitye28bbd42013-01-04 16:18:48 +02004553static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4554{
4555 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
4556 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
4557 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
4558 : "+a"(ctxt->dst.val), "+b"(ctxt->src.val), [flags]"+D"(flags)
4559 : "c"(ctxt->src2.val), [fastop]"S"(fop));
4560 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
4561 return X86EMUL_CONTINUE;
4562}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004563
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004564int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004565{
Mathias Krause0225fb52012-08-30 01:30:16 +02004566 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004567 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004568 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004569
Avi Kivity9dac77f2011-06-01 15:34:25 +03004570 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004571
Gleb Natapov1146a782013-04-11 12:30:01 +03004572 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4573 (ctxt->d & Undefined)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004574 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02004575 goto done;
4576 }
4577
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004578 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004579 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004580 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004581 goto done;
4582 }
4583
Avi Kivity9dac77f2011-06-01 15:34:25 +03004584 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004585 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004586 goto done;
4587 }
4588
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004589 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4590 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity1253791d2011-03-29 11:41:27 +02004591 rc = emulate_ud(ctxt);
4592 goto done;
4593 }
4594
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004595 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity1253791d2011-03-29 11:41:27 +02004596 rc = emulate_nm(ctxt);
4597 goto done;
4598 }
4599
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004600 if (ctxt->d & Mmx) {
4601 rc = flush_pending_x87_faults(ctxt);
4602 if (rc != X86EMUL_CONTINUE)
4603 goto done;
4604 /*
4605 * Now that we know the fpu is exception safe, we can fetch
4606 * operands from it.
4607 */
4608 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4609 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4610 if (!(ctxt->d & Mov))
4611 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4612 }
4613
Avi Kivity9dac77f2011-06-01 15:34:25 +03004614 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4615 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004616 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004617 if (rc != X86EMUL_CONTINUE)
4618 goto done;
4619 }
4620
Gleb Natapove92805a2010-02-10 14:21:35 +02004621 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004622 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004623 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02004624 goto done;
4625 }
4626
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004627 /* Instruction can only be executed in protected mode */
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03004628 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004629 rc = emulate_ud(ctxt);
4630 goto done;
4631 }
4632
Joerg Roedeld09beab2011-04-04 12:39:25 +02004633 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004634 if (ctxt->check_perm) {
4635 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004636 if (rc != X86EMUL_CONTINUE)
4637 goto done;
4638 }
4639
Avi Kivity9dac77f2011-06-01 15:34:25 +03004640 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4641 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004642 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004643 if (rc != X86EMUL_CONTINUE)
4644 goto done;
4645 }
4646
Avi Kivity9dac77f2011-06-01 15:34:25 +03004647 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004648 /* All REP prefixes have the same first termination condition */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004649 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004650 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004651 goto done;
4652 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004653 }
4654
Avi Kivity9dac77f2011-06-01 15:34:25 +03004655 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4656 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4657 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004658 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004659 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004660 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004661 }
4662
Avi Kivity9dac77f2011-06-01 15:34:25 +03004663 if (ctxt->src2.type == OP_MEM) {
4664 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4665 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004666 if (rc != X86EMUL_CONTINUE)
4667 goto done;
4668 }
4669
Avi Kivity9dac77f2011-06-01 15:34:25 +03004670 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004671 goto special_insn;
4672
4673
Avi Kivity9dac77f2011-06-01 15:34:25 +03004674 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004675 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004676 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4677 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004678 if (rc != X86EMUL_CONTINUE)
4679 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004680 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004681 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004682
Avi Kivity018a98d2007-11-27 19:30:56 +02004683special_insn:
4684
Avi Kivity9dac77f2011-06-01 15:34:25 +03004685 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4686 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004687 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004688 if (rc != X86EMUL_CONTINUE)
4689 goto done;
4690 }
4691
Avi Kivity9dac77f2011-06-01 15:34:25 +03004692 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004693 if (ctxt->d & Fastop) {
4694 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4695 rc = fastop(ctxt, fop);
4696 if (rc != X86EMUL_CONTINUE)
4697 goto done;
4698 goto writeback;
4699 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004700 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004701 if (rc != X86EMUL_CONTINUE)
4702 goto done;
4703 goto writeback;
4704 }
4705
Avi Kivity9dac77f2011-06-01 15:34:25 +03004706 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004707 goto twobyte_insn;
4708
Avi Kivity9dac77f2011-06-01 15:34:25 +03004709 switch (ctxt->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004710 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004711 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004712 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004713 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004714 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004715 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004716 if (test_cc(ctxt->b, ctxt->eflags))
4717 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004718 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004719 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004720 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004721 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004722 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004723 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Mohammed Gamal34698d82010-08-04 14:41:04 +03004724 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004725 rc = em_xchg(ctxt);
4726 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004727 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004728 switch (ctxt->op_bytes) {
4729 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4730 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4731 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004732 }
4733 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004734 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004735 rc = emulate_int(ctxt, 3);
4736 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004737 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004738 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004739 break;
4740 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004741 if (ctxt->eflags & EFLG_OF)
4742 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004743 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004744 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004745 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004746 jmp_rel(ctxt, ctxt->src.val);
4747 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004748 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004749 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004750 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004751 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004752 case 0xf5: /* cmc */
4753 /* complement carry flag from eflags reg */
4754 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004755 break;
4756 case 0xf8: /* clc */
4757 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004758 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004759 case 0xf9: /* stc */
4760 ctxt->eflags |= EFLG_CF;
4761 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004762 case 0xfc: /* cld */
4763 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004764 break;
4765 case 0xfd: /* std */
4766 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004767 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004768 default:
4769 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004770 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004771
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004772 if (rc != X86EMUL_CONTINUE)
4773 goto done;
4774
Avi Kivity018a98d2007-11-27 19:30:56 +02004775writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09004776 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004777 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02004778 goto done;
4779
Gleb Natapov5cd21912010-03-18 15:20:26 +02004780 /*
4781 * restore dst type in case the decoding will be reused
4782 * (happens for string instruction )
4783 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004784 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004785
Avi Kivity9dac77f2011-06-01 15:34:25 +03004786 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004787 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004788
Avi Kivity9dac77f2011-06-01 15:34:25 +03004789 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004790 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004791
Avi Kivity9dac77f2011-06-01 15:34:25 +03004792 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004793 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004794 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004795 if ((ctxt->d & SrcMask) == SrcSI)
4796 count = ctxt->src.count;
4797 else
4798 count = ctxt->dst.count;
4799 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4800 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004801
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004802 if (!string_insn_completed(ctxt)) {
4803 /*
4804 * Re-enter guest when pio read ahead buffer is empty
4805 * or, if it is not used, after each 1024 iteration.
4806 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004807 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004808 (r->end == 0 || r->end != r->pos)) {
4809 /*
4810 * Reset read cache. Usually happens before
4811 * decode, but since instruction is restarted
4812 * we have to do it here.
4813 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004814 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004815 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004816 return EMULATION_RESTART;
4817 }
4818 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004819 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004820 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004821
Avi Kivity9dac77f2011-06-01 15:34:25 +03004822 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004823
4824done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004825 if (rc == X86EMUL_PROPAGATE_FAULT)
4826 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004827 if (rc == X86EMUL_INTERCEPTED)
4828 return EMULATION_INTERCEPTED;
4829
Avi Kivitydd856ef2012-08-27 23:46:17 +03004830 if (rc == X86EMUL_CONTINUE)
4831 writeback_registers(ctxt);
4832
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004833 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004834
4835twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004836 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004837 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004838 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004839 break;
4840 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004841 case 0x0d: /* GrpP (prefetch) */
4842 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004843 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02004844 break;
4845 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004846 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004847 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004848 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004849 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004850 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004851 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004852 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4853 if (!test_cc(ctxt->b, ctxt->eflags))
4854 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004855 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004856 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004857 if (test_cc(ctxt->b, ctxt->eflags))
4858 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004859 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004860 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004861 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004862 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004863 case 0xae: /* clflush */
4864 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004865 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004866 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004867 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004868 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004869 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004870 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004871 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004872 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004873 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004874 break;
Wei Yongjun92f738a52010-08-17 09:19:34 +08004875 case 0xc0 ... 0xc1: /* xadd */
Avi Kivity158de572013-01-19 19:51:57 +02004876 fastop(ctxt, em_add);
Wei Yongjun92f738a52010-08-17 09:19:34 +08004877 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004878 ctxt->src.val = ctxt->dst.orig_val;
4879 write_register_operand(&ctxt->src);
Wei Yongjun92f738a52010-08-17 09:19:34 +08004880 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004881 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004882 ctxt->dst.bytes = ctxt->op_bytes;
4883 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4884 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004885 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004886 default:
4887 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004888 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004889
4890 if (rc != X86EMUL_CONTINUE)
4891 goto done;
4892
Avi Kivity6aa8b732006-12-10 02:21:36 -08004893 goto writeback;
4894
4895cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004896 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004897}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004898
4899void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
4900{
4901 invalidate_registers(ctxt);
4902}
4903
4904void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
4905{
4906 writeback_registers(ctxt);
4907}