blob: e020fed0b019b3f257de80c7952e8cf89764e848 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivityb7d491e2013-01-04 16:18:49 +020027#include <linux/stringify.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028
Avi Kivity3eeb3282010-01-21 15:31:48 +020029#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020030#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020031
Avi Kivity6aa8b732006-12-10 02:21:36 -080032/*
Avi Kivitya99455492011-09-13 10:45:41 +030033 * Operand types
34 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030035#define OpNone 0ull
36#define OpImplicit 1ull /* No generic decode */
37#define OpReg 2ull /* Register */
38#define OpMem 3ull /* Memory */
39#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
40#define OpDI 5ull /* ES:DI/EDI/RDI */
41#define OpMem64 6ull /* Memory, 64-bit */
42#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
43#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030044#define OpCL 9ull /* CL register (for shifts) */
45#define OpImmByte 10ull /* 8-bit sign extended immediate */
46#define OpOne 11ull /* Implied 1 */
Nadav Amit5e2c6882012-12-06 21:55:10 -020047#define OpImm 12ull /* Sign extended up to 32-bit immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030048#define OpMem16 13ull /* Memory operand (16-bit). */
49#define OpMem32 14ull /* Memory operand (32-bit). */
50#define OpImmU 15ull /* Immediate operand, zero extended */
51#define OpSI 16ull /* SI/ESI/RSI */
52#define OpImmFAddr 17ull /* Immediate far address */
53#define OpMemFAddr 18ull /* Far address in memory */
54#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030055#define OpES 20ull /* ES */
56#define OpCS 21ull /* CS */
57#define OpSS 22ull /* SS */
58#define OpDS 23ull /* DS */
59#define OpFS 24ull /* FS */
60#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020061#define OpMem8 26ull /* 8-bit zero extended memory operand */
Nadav Amit5e2c6882012-12-06 21:55:10 -020062#define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */
Paolo Bonzini7fa57952013-05-09 11:32:50 +020063#define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */
Avi Kivity820207c2013-02-09 11:31:45 +020064#define OpAccLo 29ull /* Low part of extended acc (AX/AX/EAX/RAX) */
65#define OpAccHi 30ull /* High part of extended acc (-/DX/EDX/RDX) */
Avi Kivitya99455492011-09-13 10:45:41 +030066
Avi Kivity0fe59122011-09-13 10:45:47 +030067#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030068#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya99455492011-09-13 10:45:41 +030069
70/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080071 * Opcode effective-address decode tables.
72 * Note that we only emulate instructions that have at least one memory
73 * operand (excluding implicit stack references). We assume that stack
74 * references and instruction fetches will never occur in special memory
75 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
76 * not be handled.
77 */
78
79/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030080#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080081/* Destination operand type. */
Avi Kivitya99455492011-09-13 10:45:41 +030082#define DstShift 1
83#define ImplicitOps (OpImplicit << DstShift)
84#define DstReg (OpReg << DstShift)
85#define DstMem (OpMem << DstShift)
86#define DstAcc (OpAcc << DstShift)
87#define DstDI (OpDI << DstShift)
88#define DstMem64 (OpMem64 << DstShift)
89#define DstImmUByte (OpImmUByte << DstShift)
90#define DstDX (OpDX << DstShift)
Avi Kivity820207c2013-02-09 11:31:45 +020091#define DstAccLo (OpAccLo << DstShift)
Avi Kivitya99455492011-09-13 10:45:41 +030092#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080093/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030094#define SrcShift 6
95#define SrcNone (OpNone << SrcShift)
96#define SrcReg (OpReg << SrcShift)
97#define SrcMem (OpMem << SrcShift)
98#define SrcMem16 (OpMem16 << SrcShift)
99#define SrcMem32 (OpMem32 << SrcShift)
100#define SrcImm (OpImm << SrcShift)
101#define SrcImmByte (OpImmByte << SrcShift)
102#define SrcOne (OpOne << SrcShift)
103#define SrcImmUByte (OpImmUByte << SrcShift)
104#define SrcImmU (OpImmU << SrcShift)
105#define SrcSI (OpSI << SrcShift)
Paolo Bonzini7fa57952013-05-09 11:32:50 +0200106#define SrcXLat (OpXLat << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300107#define SrcImmFAddr (OpImmFAddr << SrcShift)
108#define SrcMemFAddr (OpMemFAddr << SrcShift)
109#define SrcAcc (OpAcc << SrcShift)
110#define SrcImmU16 (OpImmU16 << SrcShift)
Nadav Amit5e2c6882012-12-06 21:55:10 -0200111#define SrcImm64 (OpImm64 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300112#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200113#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity820207c2013-02-09 11:31:45 +0200114#define SrcAccHi (OpAccHi << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300115#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300116#define BitOp (1<<11)
117#define MemAbs (1<<12) /* Memory operand is absolute displacement */
118#define String (1<<13) /* String instruction (rep capable) */
119#define Stack (1<<14) /* Stack instruction (push/pop) */
120#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
121#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
122#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
123#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
124#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
Gleb Natapov045a2822012-12-20 16:57:43 +0200125#define Escape (5<<15) /* Escape to coprocessor instruction */
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300126#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300127/* Generic ModRM decode. */
128#define ModRM (1<<19)
129/* Destination is only written; never read. */
130#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300131/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200132#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Borislav Petkovb51e9742013-09-22 16:44:52 +0200133#define EmulateOnUD (1<<22) /* Emulate if unsupported by the host */
Avi Kivity5a506b12010-08-01 15:10:29 +0300134#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300135#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300136#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200137#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200138#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300139#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800140#define PageTable (1 << 29) /* instruction used to write page table */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300141#define NotImpl (1 << 30) /* instruction is not implemented */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100142/* Source 2 operand type */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300143#define Src2Shift (31)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300144#define Src2None (OpNone << Src2Shift)
Avi Kivityab2c5ce2013-02-09 11:31:46 +0200145#define Src2Mem (OpMem << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300146#define Src2CL (OpCL << Src2Shift)
147#define Src2ImmByte (OpImmByte << Src2Shift)
148#define Src2One (OpOne << Src2Shift)
149#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300150#define Src2ES (OpES << Src2Shift)
151#define Src2CS (OpCS << Src2Shift)
152#define Src2SS (OpSS << Src2Shift)
153#define Src2DS (OpDS << Src2Shift)
154#define Src2FS (OpFS << Src2Shift)
155#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300156#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300157#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Avi Kivity1c11b372012-04-09 18:39:59 +0300158#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
159#define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */
160#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
Avi Kivitye28bbd42013-01-04 16:18:48 +0200161#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
Avi Kivityb6744dc2013-01-04 16:18:50 +0200162#define NoWrite ((u64)1 << 45) /* No writeback */
Avi Kivityfb32b1e2013-02-09 11:31:44 +0200163#define SrcWrite ((u64)1 << 46) /* Write back src operand */
Nadav Amit9b88ae92014-05-25 23:05:21 +0300164#define NoMod ((u64)1 << 47) /* Mod field is ignored */
Paolo Bonzinid40a6892014-03-27 11:58:02 +0100165#define Intercept ((u64)1 << 48) /* Has valid intercept field */
166#define CheckPerm ((u64)1 << 49) /* Has valid check_perm field */
Nadav Amit10e38fc2014-06-18 17:19:34 +0300167#define NoBigReal ((u64)1 << 50) /* No big real mode */
Nadav Amit68efa762014-06-18 17:19:35 +0300168#define PrivUD ((u64)1 << 51) /* #UD instead of #GP on CPL > 0 */
Nadav Amit58b70752014-10-24 11:35:09 +0300169#define NearBranch ((u64)1 << 52) /* Near branches */
Nadav Amited9aad22014-11-02 11:55:00 +0200170#define No16 ((u64)1 << 53) /* No 16 bit operand */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800171
Avi Kivity820207c2013-02-09 11:31:45 +0200172#define DstXacc (DstAccLo | SrcAccHi | SrcWrite)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800173
Avi Kivityd0e53322010-07-29 15:11:54 +0300174#define X2(x...) x, x
175#define X3(x...) X2(x), x
176#define X4(x...) X2(x), X2(x)
177#define X5(x...) X4(x), x
178#define X6(x...) X4(x), X2(x)
179#define X7(x...) X4(x), X3(x)
180#define X8(x...) X4(x), X4(x)
181#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300182
Avi Kivitye28bbd42013-01-04 16:18:48 +0200183#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
184#define FASTOP_SIZE 8
185
186/*
187 * fastop functions have a special calling convention:
188 *
Avi Kivity017da7b2013-02-09 11:31:47 +0200189 * dst: rax (in/out)
190 * src: rdx (in/out)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200191 * src2: rcx (in)
192 * flags: rflags (in/out)
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200193 * ex: rsi (in:fastop pointer, out:zero if exception)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200194 *
195 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
196 * different operand sizes can be reached by calculation, rather than a jump
197 * table (which would be bigger than the code).
198 *
199 * fastop functions are declared as taking a never-defined fastop parameter,
200 * so they can't be called from C directly.
201 */
202
203struct fastop;
204
Avi Kivityd65b1de2010-07-29 15:11:35 +0300205struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300206 u64 flags : 56;
207 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300208 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300209 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200210 const struct opcode *group;
211 const struct group_dual *gdual;
212 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200213 const struct escape *esc;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200214 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300215 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200216 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300217};
218
219struct group_dual {
220 struct opcode mod012[8];
221 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300222};
223
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200224struct gprefix {
225 struct opcode pfx_no;
226 struct opcode pfx_66;
227 struct opcode pfx_f2;
228 struct opcode pfx_f3;
229};
230
Gleb Natapov045a2822012-12-20 16:57:43 +0200231struct escape {
232 struct opcode op[8];
233 struct opcode high[64];
234};
235
Avi Kivity6aa8b732006-12-10 02:21:36 -0800236/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200237#define EFLG_ID (1<<21)
238#define EFLG_VIP (1<<20)
239#define EFLG_VIF (1<<19)
240#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200241#define EFLG_VM (1<<17)
242#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200243#define EFLG_IOPL (3<<12)
244#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800245#define EFLG_OF (1<<11)
246#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200247#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200248#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800249#define EFLG_SF (1<<7)
250#define EFLG_ZF (1<<6)
251#define EFLG_AF (1<<4)
252#define EFLG_PF (1<<2)
253#define EFLG_CF (1<<0)
254
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300255#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
256#define EFLG_RESERVED_ONE_MASK 2
257
Avi Kivitydd856ef2012-08-27 23:46:17 +0300258static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
259{
260 if (!(ctxt->regs_valid & (1 << nr))) {
261 ctxt->regs_valid |= 1 << nr;
262 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
263 }
264 return ctxt->_regs[nr];
265}
266
267static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
268{
269 ctxt->regs_valid |= 1 << nr;
270 ctxt->regs_dirty |= 1 << nr;
271 return &ctxt->_regs[nr];
272}
273
274static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
275{
276 reg_read(ctxt, nr);
277 return reg_write(ctxt, nr);
278}
279
280static void writeback_registers(struct x86_emulate_ctxt *ctxt)
281{
282 unsigned reg;
283
284 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
285 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
286}
287
288static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
289{
290 ctxt->regs_dirty = 0;
291 ctxt->regs_valid = 0;
292}
293
Avi Kivity6aa8b732006-12-10 02:21:36 -0800294/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800295 * These EFLAGS bits are restored from saved value during emulation, and
296 * any changes are written back to the saved value after emulation.
297 */
298#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
299
Avi Kivitydda96d82008-11-26 15:14:10 +0200300#ifdef CONFIG_X86_64
301#define ON64(x) x
302#else
303#define ON64(x)
304#endif
305
Avi Kivity4d758342013-01-19 19:51:55 +0200306static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *));
307
Avi Kivityb7d491e2013-01-04 16:18:49 +0200308#define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t"
309#define FOP_RET "ret \n\t"
310
311#define FOP_START(op) \
312 extern void em_##op(struct fastop *fake); \
313 asm(".pushsection .text, \"ax\" \n\t" \
314 ".global em_" #op " \n\t" \
315 FOP_ALIGN \
316 "em_" #op ": \n\t"
317
318#define FOP_END \
319 ".popsection")
320
Avi Kivity0bdea062013-01-19 19:51:50 +0200321#define FOPNOP() FOP_ALIGN FOP_RET
322
Avi Kivityb7d491e2013-01-04 16:18:49 +0200323#define FOP1E(op, dst) \
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200324 FOP_ALIGN "10: " #op " %" #dst " \n\t" FOP_RET
325
326#define FOP1EEX(op, dst) \
327 FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200328
329#define FASTOP1(op) \
330 FOP_START(op) \
331 FOP1E(op##b, al) \
332 FOP1E(op##w, ax) \
333 FOP1E(op##l, eax) \
334 ON64(FOP1E(op##q, rax)) \
335 FOP_END
336
Avi Kivityb9fa4092013-02-09 11:31:48 +0200337/* 1-operand, using src2 (for MUL/DIV r/m) */
338#define FASTOP1SRC2(op, name) \
339 FOP_START(name) \
340 FOP1E(op, cl) \
341 FOP1E(op, cx) \
342 FOP1E(op, ecx) \
343 ON64(FOP1E(op, rcx)) \
344 FOP_END
345
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200346/* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */
347#define FASTOP1SRC2EX(op, name) \
348 FOP_START(name) \
349 FOP1EEX(op, cl) \
350 FOP1EEX(op, cx) \
351 FOP1EEX(op, ecx) \
352 ON64(FOP1EEX(op, rcx)) \
353 FOP_END
354
Avi Kivityf7857f32013-01-04 16:18:53 +0200355#define FOP2E(op, dst, src) \
356 FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET
357
358#define FASTOP2(op) \
359 FOP_START(op) \
Avi Kivity017da7b2013-02-09 11:31:47 +0200360 FOP2E(op##b, al, dl) \
361 FOP2E(op##w, ax, dx) \
362 FOP2E(op##l, eax, edx) \
363 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivityf7857f32013-01-04 16:18:53 +0200364 FOP_END
365
Avi Kivity11c363b2013-01-19 19:51:54 +0200366/* 2 operand, word only */
367#define FASTOP2W(op) \
368 FOP_START(op) \
369 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200370 FOP2E(op##w, ax, dx) \
371 FOP2E(op##l, eax, edx) \
372 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivity11c363b2013-01-19 19:51:54 +0200373 FOP_END
374
Avi Kivity007a3b52013-01-19 19:51:51 +0200375/* 2 operand, src is CL */
376#define FASTOP2CL(op) \
377 FOP_START(op) \
378 FOP2E(op##b, al, cl) \
379 FOP2E(op##w, ax, cl) \
380 FOP2E(op##l, eax, cl) \
381 ON64(FOP2E(op##q, rax, cl)) \
382 FOP_END
383
Nadav Amit5aca3722014-11-02 11:54:50 +0200384/* 2 operand, src and dest are reversed */
385#define FASTOP2R(op, name) \
386 FOP_START(name) \
387 FOP2E(op##b, dl, al) \
388 FOP2E(op##w, dx, ax) \
389 FOP2E(op##l, edx, eax) \
390 ON64(FOP2E(op##q, rdx, rax)) \
391 FOP_END
392
Avi Kivity0bdea062013-01-19 19:51:50 +0200393#define FOP3E(op, dst, src, src2) \
394 FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
395
396/* 3-operand, word-only, src2=cl */
397#define FASTOP3WCL(op) \
398 FOP_START(op) \
399 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200400 FOP3E(op##w, ax, dx, cl) \
401 FOP3E(op##l, eax, edx, cl) \
402 ON64(FOP3E(op##q, rax, rdx, cl)) \
Avi Kivity0bdea062013-01-19 19:51:50 +0200403 FOP_END
404
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200405/* Special case for SETcc - 1 instruction per cc */
406#define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t"
407
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200408asm(".global kvm_fastop_exception \n"
409 "kvm_fastop_exception: xor %esi, %esi; ret");
410
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200411FOP_START(setcc)
412FOP_SETCC(seto)
413FOP_SETCC(setno)
414FOP_SETCC(setc)
415FOP_SETCC(setnc)
416FOP_SETCC(setz)
417FOP_SETCC(setnz)
418FOP_SETCC(setbe)
419FOP_SETCC(setnbe)
420FOP_SETCC(sets)
421FOP_SETCC(setns)
422FOP_SETCC(setp)
423FOP_SETCC(setnp)
424FOP_SETCC(setl)
425FOP_SETCC(setnl)
426FOP_SETCC(setle)
427FOP_SETCC(setnle)
428FOP_END;
429
Paolo Bonzini326f5782013-05-09 11:32:51 +0200430FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET
431FOP_END;
432
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200433static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
434 enum x86_intercept intercept,
435 enum x86_intercept_stage stage)
436{
437 struct x86_instruction_info info = {
438 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300439 .rep_prefix = ctxt->rep_prefix,
440 .modrm_mod = ctxt->modrm_mod,
441 .modrm_reg = ctxt->modrm_reg,
442 .modrm_rm = ctxt->modrm_rm,
443 .src_val = ctxt->src.val64,
Jan Kiszka6cbc5f52014-06-30 12:52:55 +0200444 .dst_val = ctxt->dst.val64,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300445 .src_bytes = ctxt->src.bytes,
446 .dst_bytes = ctxt->dst.bytes,
447 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200448 .next_rip = ctxt->eip,
449 };
450
Avi Kivity29535382011-04-20 13:37:53 +0300451 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200452}
453
Avi Kivityf47cfa32012-06-07 17:49:24 +0300454static void assign_masked(ulong *dest, ulong src, ulong mask)
455{
456 *dest = (*dest & ~mask) | (src & mask);
457}
458
Avi Kivity9dac77f2011-06-01 15:34:25 +0300459static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800460{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300461 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800462}
463
Avi Kivityf47cfa32012-06-07 17:49:24 +0300464static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
465{
466 u16 sel;
467 struct desc_struct ss;
468
469 if (ctxt->mode == X86EMUL_MODE_PROT64)
470 return ~0UL;
471 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
472 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
473}
474
Avi Kivity612e89f2012-06-12 20:03:23 +0300475static int stack_size(struct x86_emulate_ctxt *ctxt)
476{
477 return (__fls(stack_mask(ctxt)) + 1) >> 3;
478}
479
Avi Kivity6aa8b732006-12-10 02:21:36 -0800480/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800481static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300482address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800483{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300484 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800485 return reg;
486 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300487 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800488}
489
490static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300491register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800492{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300493 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800494}
495
Avi Kivity5ad105e2012-08-19 14:34:31 +0300496static void masked_increment(ulong *reg, ulong mask, int inc)
497{
498 assign_masked(reg, *reg + inc, mask);
499}
500
Harvey Harrison7a9572752008-02-19 07:40:41 -0800501static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300502register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800503{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300504 ulong mask;
505
Avi Kivity9dac77f2011-06-01 15:34:25 +0300506 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300507 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800508 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300509 mask = ad_mask(ctxt);
510 masked_increment(reg, mask, inc);
511}
512
513static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
514{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300515 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800516}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800517
Avi Kivity56697682011-04-03 14:08:51 +0300518static u32 desc_limit_scaled(struct desc_struct *desc)
519{
520 u32 limit = get_desc_limit(desc);
521
522 return desc->g ? (limit << 12) | 0xfff : limit;
523}
524
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900525static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300526{
527 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
528 return 0;
529
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900530 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300531}
532
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200533static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
534 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300535{
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +0200536 WARN_ON(vec > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +0200537 ctxt->exception.vector = vec;
538 ctxt->exception.error_code = error;
539 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200540 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300541}
542
Joerg Roedel3b88e412011-04-04 12:39:29 +0200543static int emulate_db(struct x86_emulate_ctxt *ctxt)
544{
545 return emulate_exception(ctxt, DB_VECTOR, 0, false);
546}
547
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200548static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300549{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200550 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300551}
552
Avi Kivity618ff152011-04-03 12:32:09 +0300553static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
554{
555 return emulate_exception(ctxt, SS_VECTOR, err, true);
556}
557
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200558static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300559{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200560 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300561}
562
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200563static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300564{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200565 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300566}
567
Avi Kivity34d1f492010-08-26 11:59:01 +0300568static int emulate_de(struct x86_emulate_ctxt *ctxt)
569{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200570 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300571}
572
Avi Kivity1253791d2011-03-29 11:41:27 +0200573static int emulate_nm(struct x86_emulate_ctxt *ctxt)
574{
575 return emulate_exception(ctxt, NM_VECTOR, 0, false);
576}
577
Nadav Amit234f3ce2014-09-18 22:39:38 +0300578static inline int assign_eip_far(struct x86_emulate_ctxt *ctxt, ulong dst,
579 int cs_l)
Nadav Amit05c83ec2014-09-18 22:39:37 +0300580{
581 switch (ctxt->op_bytes) {
582 case 2:
583 ctxt->_eip = (u16)dst;
584 break;
585 case 4:
586 ctxt->_eip = (u32)dst;
587 break;
Nadav Amitcd9b8e2c2014-10-28 00:03:43 +0200588#ifdef CONFIG_X86_64
Nadav Amit05c83ec2014-09-18 22:39:37 +0300589 case 8:
Nadav Amit234f3ce2014-09-18 22:39:38 +0300590 if ((cs_l && is_noncanonical_address(dst)) ||
Nadav Amitcd9b8e2c2014-10-28 00:03:43 +0200591 (!cs_l && (dst >> 32) != 0))
Nadav Amit234f3ce2014-09-18 22:39:38 +0300592 return emulate_gp(ctxt, 0);
Nadav Amit05c83ec2014-09-18 22:39:37 +0300593 ctxt->_eip = dst;
594 break;
Nadav Amitcd9b8e2c2014-10-28 00:03:43 +0200595#endif
Nadav Amit05c83ec2014-09-18 22:39:37 +0300596 default:
597 WARN(1, "unsupported eip assignment size\n");
598 }
Nadav Amit234f3ce2014-09-18 22:39:38 +0300599 return X86EMUL_CONTINUE;
Nadav Amit05c83ec2014-09-18 22:39:37 +0300600}
601
Nadav Amit234f3ce2014-09-18 22:39:38 +0300602static inline int assign_eip_near(struct x86_emulate_ctxt *ctxt, ulong dst)
Nadav Amit05c83ec2014-09-18 22:39:37 +0300603{
Nadav Amit234f3ce2014-09-18 22:39:38 +0300604 return assign_eip_far(ctxt, dst, ctxt->mode == X86EMUL_MODE_PROT64);
605}
606
607static inline int jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
608{
609 return assign_eip_near(ctxt, ctxt->_eip + rel);
Nadav Amit05c83ec2014-09-18 22:39:37 +0300610}
611
Avi Kivity1aa36612011-04-27 13:20:30 +0300612static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
613{
614 u16 selector;
615 struct desc_struct desc;
616
617 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
618 return selector;
619}
620
621static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
622 unsigned seg)
623{
624 u16 dummy;
625 u32 base3;
626 struct desc_struct desc;
627
628 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
629 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
630}
631
Avi Kivity1c11b372012-04-09 18:39:59 +0300632/*
633 * x86 defines three classes of vector instructions: explicitly
634 * aligned, explicitly unaligned, and the rest, which change behaviour
635 * depending on whether they're AVX encoded or not.
636 *
637 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
638 * subject to the same check.
639 */
640static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
641{
642 if (likely(size < 16))
643 return false;
644
645 if (ctxt->d & Aligned)
646 return true;
647 else if (ctxt->d & Unaligned)
648 return false;
649 else if (ctxt->d & Avx)
650 return false;
651 else
652 return true;
653}
654
Paolo Bonzinid09155d2014-10-27 14:54:44 +0100655static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
656 struct segmented_address addr,
657 unsigned *max_size, unsigned size,
658 bool write, bool fetch,
659 ulong *linear)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300660{
Avi Kivity618ff152011-04-03 12:32:09 +0300661 struct desc_struct desc;
662 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300663 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300664 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300665 u16 sel;
Gleb Natapov3a78a4f2012-12-20 16:57:42 +0200666 unsigned cpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300667
Nadav Amit518547b2014-09-30 20:49:19 +0300668 la = seg_base(ctxt, addr.seg) +
669 (fetch || ctxt->ad_bytes == 8 ? addr.ea : (u32)addr.ea);
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100670 *max_size = 0;
Avi Kivity618ff152011-04-03 12:32:09 +0300671 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300672 case X86EMUL_MODE_PROT64:
Nadav Amit4be4de72014-09-18 22:39:40 +0300673 if (is_noncanonical_address(la))
Avi Kivity618ff152011-04-03 12:32:09 +0300674 return emulate_gp(ctxt, 0);
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100675
676 *max_size = min_t(u64, ~0u, (1ull << 48) - la);
677 if (size > *max_size)
678 goto bad;
Avi Kivity618ff152011-04-03 12:32:09 +0300679 break;
680 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300681 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
682 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300683 if (!usable)
684 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200685 /* code segment in protected mode or read-only data segment */
686 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
687 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300688 goto bad;
689 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400690 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300691 goto bad;
692 lim = desc_limit_scaled(&desc);
Nadav Amit10e38fc2014-06-18 17:19:34 +0300693 if ((ctxt->mode == X86EMUL_MODE_REAL) && !fetch &&
694 (ctxt->d & NoBigReal)) {
695 /* la is between zero and 0xffff */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100696 if (la > 0xffff)
Nadav Amit10e38fc2014-06-18 17:19:34 +0300697 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100698 *max_size = 0x10000 - la;
Nadav Amit10e38fc2014-06-18 17:19:34 +0300699 } else if ((desc.type & 8) || !(desc.type & 4)) {
Avi Kivity618ff152011-04-03 12:32:09 +0300700 /* expand-up segment */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100701 if (addr.ea > lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300702 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100703 *max_size = min_t(u64, ~0u, (u64)lim + 1 - addr.ea);
Avi Kivity618ff152011-04-03 12:32:09 +0300704 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800705 /* expand-down segment */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100706 if (addr.ea <= lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300707 goto bad;
708 lim = desc.d ? 0xffffffff : 0xffff;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100709 if (addr.ea > lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300710 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100711 *max_size = min_t(u64, ~0u, (u64)lim + 1 - addr.ea);
Avi Kivity618ff152011-04-03 12:32:09 +0300712 }
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100713 if (size > *max_size)
714 goto bad;
Avi Kivity717746e2011-04-20 13:37:53 +0300715 cpl = ctxt->ops->cpl(ctxt);
Nadav Amitc49c7592014-10-03 01:10:03 +0300716 if (!fetch) {
717 /* data segment or readable code segment */
Avi Kivity618ff152011-04-03 12:32:09 +0300718 if (cpl > desc.dpl)
719 goto bad;
720 } else if ((desc.type & 8) && !(desc.type & 4)) {
721 /* nonconforming code segment */
722 if (cpl != desc.dpl)
723 goto bad;
724 } else if ((desc.type & 8) && (desc.type & 4)) {
725 /* conforming code segment */
726 if (cpl < desc.dpl)
727 goto bad;
728 }
729 break;
730 }
Nadav Amit518547b2014-09-30 20:49:19 +0300731 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300732 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300733 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
734 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300735 *linear = la;
736 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300737bad:
738 if (addr.seg == VCPU_SREG_SS)
Paolo Bonzini36061892014-10-27 14:40:49 +0100739 return emulate_ss(ctxt, 0);
Avi Kivity618ff152011-04-03 12:32:09 +0300740 else
Paolo Bonzini36061892014-10-27 14:40:49 +0100741 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300742}
743
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400744static int linearize(struct x86_emulate_ctxt *ctxt,
745 struct segmented_address addr,
746 unsigned size, bool write,
747 ulong *linear)
748{
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100749 unsigned max_size;
750 return __linearize(ctxt, addr, &max_size, size, write, false, linear);
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400751}
752
753
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200754static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
755 struct segmented_address addr,
756 void *data,
757 unsigned size)
758{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200759 int rc;
760 ulong linear;
761
Avi Kivity83b87952011-04-03 11:31:19 +0300762 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200763 if (rc != X86EMUL_CONTINUE)
764 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300765 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200766}
767
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900768/*
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200769 * Prefetch the remaining bytes of the instruction without crossing page
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900770 * boundary if they are not in fetch_cache yet.
771 */
Paolo Bonzini9506d572014-05-06 13:05:25 +0200772static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
Avi Kivity62266862007-11-20 13:15:52 +0200773{
Avi Kivity62266862007-11-20 13:15:52 +0200774 int rc;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100775 unsigned size, max_size;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200776 unsigned long linear;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200777 int cur_size = ctxt->fetch.end - ctxt->fetch.data;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200778 struct segmented_address addr = { .seg = VCPU_SREG_CS,
Paolo Bonzini17052f12014-05-06 16:33:01 +0200779 .ea = ctxt->eip + cur_size };
780
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100781 /*
782 * We do not know exactly how many bytes will be needed, and
783 * __linearize is expensive, so fetch as much as possible. We
784 * just have to avoid going beyond the 15 byte limit, the end
785 * of the segment, or the end of the page.
786 *
787 * __linearize is called with size 0 so that it does not do any
788 * boundary check itself. Instead, we use max_size to check
789 * against op_size.
790 */
791 rc = __linearize(ctxt, addr, &max_size, 0, false, true, &linear);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200792 if (unlikely(rc != X86EMUL_CONTINUE))
793 return rc;
794
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100795 size = min_t(unsigned, 15UL ^ cur_size, max_size);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200796 size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear));
Paolo Bonzini5cfc7e02014-05-06 13:05:25 +0200797
798 /*
799 * One instruction can only straddle two pages,
800 * and one has been loaded at the beginning of
801 * x86_decode_insn. So, if not enough bytes
802 * still, we must have hit the 15-byte boundary.
803 */
804 if (unlikely(size < op_size))
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100805 return emulate_gp(ctxt, 0);
806
Paolo Bonzini17052f12014-05-06 16:33:01 +0200807 rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end,
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200808 size, &ctxt->exception);
809 if (unlikely(rc != X86EMUL_CONTINUE))
810 return rc;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200811 ctxt->fetch.end += size;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900812 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200813}
814
Paolo Bonzini9506d572014-05-06 13:05:25 +0200815static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt,
816 unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200817{
Nadav Amit08da44a2014-10-03 01:10:04 +0300818 unsigned done_size = ctxt->fetch.end - ctxt->fetch.ptr;
819
820 if (unlikely(done_size < size))
821 return __do_insn_fetch_bytes(ctxt, size - done_size);
Paolo Bonzini9506d572014-05-06 13:05:25 +0200822 else
823 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200824}
825
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900826/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900827#define insn_fetch(_type, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200828({ _type _x; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200829 \
830 rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900831 if (rc != X86EMUL_CONTINUE) \
832 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200833 ctxt->_eip += sizeof(_type); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200834 _x = *(_type __aligned(1) *) ctxt->fetch.ptr; \
835 ctxt->fetch.ptr += sizeof(_type); \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200836 _x; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900837})
838
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900839#define insn_fetch_arr(_arr, _size, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200840({ \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200841 rc = do_insn_fetch_bytes(_ctxt, _size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900842 if (rc != X86EMUL_CONTINUE) \
843 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200844 ctxt->_eip += (_size); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200845 memcpy(_arr, ctxt->fetch.ptr, _size); \
846 ctxt->fetch.ptr += (_size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900847})
848
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000849/*
850 * Given the 'reg' portion of a ModRM byte, and a register block, return a
851 * pointer into the block that addresses the relevant register.
852 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
853 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300854static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200855 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800856{
857 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200858 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800859
Avi Kivity6aa8b732006-12-10 02:21:36 -0800860 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300861 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
862 else
863 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800864 return p;
865}
866
867static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200868 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800869 u16 *size, unsigned long *address, int op_bytes)
870{
871 int rc;
872
873 if (op_bytes == 2)
874 op_bytes = 3;
875 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200876 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900877 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800878 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200879 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200880 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881 return rc;
882}
883
Avi Kivity34b77652013-01-19 19:51:56 +0200884FASTOP2(add);
885FASTOP2(or);
886FASTOP2(adc);
887FASTOP2(sbb);
888FASTOP2(and);
889FASTOP2(sub);
890FASTOP2(xor);
891FASTOP2(cmp);
892FASTOP2(test);
893
Avi Kivityb9fa4092013-02-09 11:31:48 +0200894FASTOP1SRC2(mul, mul_ex);
895FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200896FASTOP1SRC2EX(div, div_ex);
897FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +0200898
Avi Kivity34b77652013-01-19 19:51:56 +0200899FASTOP3WCL(shld);
900FASTOP3WCL(shrd);
901
902FASTOP2W(imul);
903
904FASTOP1(not);
905FASTOP1(neg);
906FASTOP1(inc);
907FASTOP1(dec);
908
909FASTOP2CL(rol);
910FASTOP2CL(ror);
911FASTOP2CL(rcl);
912FASTOP2CL(rcr);
913FASTOP2CL(shl);
914FASTOP2CL(shr);
915FASTOP2CL(sar);
916
917FASTOP2W(bsf);
918FASTOP2W(bsr);
919FASTOP2W(bt);
920FASTOP2W(bts);
921FASTOP2W(btr);
922FASTOP2W(btc);
923
Avi Kivitye47a5f52013-02-09 11:31:51 +0200924FASTOP2(xadd);
925
Nadav Amit5aca3722014-11-02 11:54:50 +0200926FASTOP2R(cmp, cmp_r);
927
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200928static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300929{
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200930 u8 rc;
931 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300932
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200933 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +0200934 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200935 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
936 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300937}
938
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300939static void fetch_register_operand(struct operand *op)
940{
941 switch (op->bytes) {
942 case 1:
943 op->val = *(u8 *)op->addr.reg;
944 break;
945 case 2:
946 op->val = *(u16 *)op->addr.reg;
947 break;
948 case 4:
949 op->val = *(u32 *)op->addr.reg;
950 break;
951 case 8:
952 op->val = *(u64 *)op->addr.reg;
953 break;
954 }
955}
956
Avi Kivity1253791d2011-03-29 11:41:27 +0200957static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
958{
959 ctxt->ops->get_fpu(ctxt);
960 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200961 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
962 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
963 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
964 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
965 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
966 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
967 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
968 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200969#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200970 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
971 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
972 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
973 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
974 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
975 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
976 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
977 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200978#endif
979 default: BUG();
980 }
981 ctxt->ops->put_fpu(ctxt);
982}
983
984static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
985 int reg)
986{
987 ctxt->ops->get_fpu(ctxt);
988 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200989 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
990 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
991 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
992 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
993 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
994 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
995 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
996 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200997#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200998 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
999 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
1000 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
1001 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
1002 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
1003 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
1004 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
1005 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001006#endif
1007 default: BUG();
1008 }
1009 ctxt->ops->put_fpu(ctxt);
1010}
1011
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001012static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1013{
1014 ctxt->ops->get_fpu(ctxt);
1015 switch (reg) {
1016 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1017 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1018 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1019 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1020 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1021 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1022 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1023 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1024 default: BUG();
1025 }
1026 ctxt->ops->put_fpu(ctxt);
1027}
1028
1029static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1030{
1031 ctxt->ops->get_fpu(ctxt);
1032 switch (reg) {
1033 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1034 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1035 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1036 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1037 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1038 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1039 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1040 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1041 default: BUG();
1042 }
1043 ctxt->ops->put_fpu(ctxt);
1044}
1045
Gleb Natapov045a2822012-12-20 16:57:43 +02001046static int em_fninit(struct x86_emulate_ctxt *ctxt)
1047{
1048 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1049 return emulate_nm(ctxt);
1050
1051 ctxt->ops->get_fpu(ctxt);
1052 asm volatile("fninit");
1053 ctxt->ops->put_fpu(ctxt);
1054 return X86EMUL_CONTINUE;
1055}
1056
1057static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1058{
1059 u16 fcw;
1060
1061 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1062 return emulate_nm(ctxt);
1063
1064 ctxt->ops->get_fpu(ctxt);
1065 asm volatile("fnstcw %0": "+m"(fcw));
1066 ctxt->ops->put_fpu(ctxt);
1067
1068 /* force 2 byte destination */
1069 ctxt->dst.bytes = 2;
1070 ctxt->dst.val = fcw;
1071
1072 return X86EMUL_CONTINUE;
1073}
1074
1075static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1076{
1077 u16 fsw;
1078
1079 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1080 return emulate_nm(ctxt);
1081
1082 ctxt->ops->get_fpu(ctxt);
1083 asm volatile("fnstsw %0": "+m"(fsw));
1084 ctxt->ops->put_fpu(ctxt);
1085
1086 /* force 2 byte destination */
1087 ctxt->dst.bytes = 2;
1088 ctxt->dst.val = fsw;
1089
1090 return X86EMUL_CONTINUE;
1091}
1092
Avi Kivity1253791d2011-03-29 11:41:27 +02001093static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001094 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001095{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001096 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001097
Avi Kivity9dac77f2011-06-01 15:34:25 +03001098 if (!(ctxt->d & ModRM))
1099 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity1253791d2011-03-29 11:41:27 +02001100
Avi Kivity9dac77f2011-06-01 15:34:25 +03001101 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001102 op->type = OP_XMM;
1103 op->bytes = 16;
1104 op->addr.xmm = reg;
1105 read_sse_reg(ctxt, &op->vec_val, reg);
1106 return;
1107 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001108 if (ctxt->d & Mmx) {
1109 reg &= 7;
1110 op->type = OP_MM;
1111 op->bytes = 8;
1112 op->addr.mm = reg;
1113 return;
1114 }
Avi Kivity1253791d2011-03-29 11:41:27 +02001115
Avi Kivity3c118e22007-10-31 10:27:04 +02001116 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001117 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1118 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1119
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001120 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001121 op->orig_val = op->val;
1122}
1123
Avi Kivitya6e34072012-06-10 17:15:39 +03001124static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1125{
1126 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1127 ctxt->modrm_seg = VCPU_SREG_SS;
1128}
1129
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001130static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001131 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001132{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001133 u8 sib;
Bandan Das02357bd2014-04-16 12:46:11 -04001134 int index_reg, base_reg, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001135 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001136 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001137
Bandan Das02357bd2014-04-16 12:46:11 -04001138 ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */
1139 index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */
1140 base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001141
Bandan Das02357bd2014-04-16 12:46:11 -04001142 ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001143 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
Bandan Das02357bd2014-04-16 12:46:11 -04001144 ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001145 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001146
Nadav Amit9b88ae92014-05-25 23:05:21 +03001147 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001148 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001149 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001150 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001151 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001152 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001153 op->type = OP_XMM;
1154 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001155 op->addr.xmm = ctxt->modrm_rm;
1156 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001157 return rc;
1158 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001159 if (ctxt->d & Mmx) {
1160 op->type = OP_MM;
1161 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001162 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001163 return rc;
1164 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001165 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001166 return rc;
1167 }
1168
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001169 op->type = OP_MEM;
1170
Avi Kivity9dac77f2011-06-01 15:34:25 +03001171 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001172 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1173 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1174 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1175 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001176
1177 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001178 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001179 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001180 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001181 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001182 break;
1183 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001184 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001185 break;
1186 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001187 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001188 break;
1189 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001190 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001191 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001192 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001193 break;
1194 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001195 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001196 break;
1197 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001198 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001199 break;
1200 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001201 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001202 break;
1203 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001204 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001205 break;
1206 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001207 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001208 break;
1209 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001210 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001211 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001212 break;
1213 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001214 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001215 break;
1216 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001217 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1218 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1219 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001220 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001221 } else {
1222 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001223 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001224 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001225 index_reg |= (sib >> 3) & 7;
1226 base_reg |= sib & 7;
1227 scale = sib >> 6;
1228
Avi Kivity9dac77f2011-06-01 15:34:25 +03001229 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001230 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001231 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001232 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001233 adjust_modrm_seg(ctxt, base_reg);
1234 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001235 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001236 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001237 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Nadav Amit5b38ab82014-11-02 11:54:41 +02001238 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity84411d82008-06-15 21:53:26 -07001239 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001240 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001241 } else {
1242 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001243 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001244 adjust_modrm_seg(ctxt, base_reg);
1245 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001246 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001247 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001248 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001249 break;
1250 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001251 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001252 break;
1253 }
1254 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001255 op->addr.mem.ea = modrm_ea;
Bandan Das41061cd2014-04-16 12:46:14 -04001256 if (ctxt->ad_bytes != 8)
1257 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
1258
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001259done:
1260 return rc;
1261}
1262
1263static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001264 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001265{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001266 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001267
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001268 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001269 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001270 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001271 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001272 break;
1273 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001274 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001275 break;
1276 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001277 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001278 break;
1279 }
1280done:
1281 return rc;
1282}
1283
Avi Kivity9dac77f2011-06-01 15:34:25 +03001284static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001285{
Sheng Yang7129eec2010-09-28 16:33:32 +08001286 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001287
Avi Kivity9dac77f2011-06-01 15:34:25 +03001288 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001289 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001290
Avi Kivity9dac77f2011-06-01 15:34:25 +03001291 if (ctxt->src.bytes == 2)
1292 sv = (s16)ctxt->src.val & (s16)mask;
1293 else if (ctxt->src.bytes == 4)
1294 sv = (s32)ctxt->src.val & (s32)mask;
Nadav Amit7dec5602014-06-15 16:12:57 +03001295 else
1296 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001297
Avi Kivity9dac77f2011-06-01 15:34:25 +03001298 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001299 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001300
1301 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001302 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001303}
1304
Gleb Natapov9de41572010-04-28 19:15:22 +03001305static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001306 unsigned long addr, void *dest, unsigned size)
1307{
1308 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001309 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001310
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001311 if (mc->pos < mc->end)
1312 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001313
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001314 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001315
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001316 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1317 &ctxt->exception);
1318 if (rc != X86EMUL_CONTINUE)
1319 return rc;
1320
1321 mc->end += size;
1322
1323read_cached:
1324 memcpy(dest, mc->data + mc->pos, size);
1325 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001326 return X86EMUL_CONTINUE;
1327}
1328
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001329static int segmented_read(struct x86_emulate_ctxt *ctxt,
1330 struct segmented_address addr,
1331 void *data,
1332 unsigned size)
1333{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001334 int rc;
1335 ulong linear;
1336
Avi Kivity83b87952011-04-03 11:31:19 +03001337 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001338 if (rc != X86EMUL_CONTINUE)
1339 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001340 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001341}
1342
1343static int segmented_write(struct x86_emulate_ctxt *ctxt,
1344 struct segmented_address addr,
1345 const void *data,
1346 unsigned size)
1347{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001348 int rc;
1349 ulong linear;
1350
Avi Kivity83b87952011-04-03 11:31:19 +03001351 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001352 if (rc != X86EMUL_CONTINUE)
1353 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001354 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1355 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001356}
1357
1358static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1359 struct segmented_address addr,
1360 const void *orig_data, const void *data,
1361 unsigned size)
1362{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001363 int rc;
1364 ulong linear;
1365
Avi Kivity83b87952011-04-03 11:31:19 +03001366 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001367 if (rc != X86EMUL_CONTINUE)
1368 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001369 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1370 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001371}
1372
Gleb Natapov7b262e92010-03-18 15:20:27 +02001373static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001374 unsigned int size, unsigned short port,
1375 void *dest)
1376{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001377 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001378
1379 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001380 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001381 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001382 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001383 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001384 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1385 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Mark Rustadb55a8142014-07-25 06:27:05 -07001386 n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001387 if (n == 0)
1388 n = 1;
1389 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001390 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001391 return 0;
1392 rc->end = n * size;
1393 }
1394
Nadav Amite6e39f02014-04-18 03:35:10 +03001395 if (ctxt->rep_prefix && (ctxt->d & String) &&
1396 !(ctxt->eflags & EFLG_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001397 ctxt->dst.data = rc->data + rc->pos;
1398 ctxt->dst.type = OP_MEM_STR;
1399 ctxt->dst.count = (rc->end - rc->pos) / size;
1400 rc->pos = rc->end;
1401 } else {
1402 memcpy(dest, rc->data + rc->pos, size);
1403 rc->pos += size;
1404 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001405 return 1;
1406}
1407
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001408static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1409 u16 index, struct desc_struct *desc)
1410{
1411 struct desc_ptr dt;
1412 ulong addr;
1413
1414 ctxt->ops->get_idt(ctxt, &dt);
1415
1416 if (dt.size < index * 8 + 7)
1417 return emulate_gp(ctxt, index << 3 | 0x2);
1418
1419 addr = dt.address + index * 8;
1420 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1421 &ctxt->exception);
1422}
1423
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001424static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001425 u16 selector, struct desc_ptr *dt)
1426{
Mathias Krause0225fb52012-08-30 01:30:16 +02001427 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001428 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001429
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001430 if (selector & 1 << 2) {
1431 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001432 u16 sel;
1433
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001434 memset (dt, 0, sizeof *dt);
Nadav Amit2eedcac2014-06-02 18:34:05 +03001435 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1436 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001437 return;
1438
1439 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001440 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001441 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001442 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001443}
1444
1445/* allowed just for 8 bytes segments */
1446static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001447 u16 selector, struct desc_struct *desc,
1448 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001449{
1450 struct desc_ptr dt;
1451 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001452 ulong addr;
1453
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001454 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001455
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001456 if (dt.size < index * 8 + 7)
1457 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001458
Avi Kivitye9194642012-06-13 16:29:39 +03001459 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001460 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1461 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001462}
1463
1464/* allowed just for 8 bytes segments */
1465static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001466 u16 selector, struct desc_struct *desc)
1467{
1468 struct desc_ptr dt;
1469 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001470 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001471
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001472 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001473
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001474 if (dt.size < index * 8 + 7)
1475 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001476
1477 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001478 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1479 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001480}
1481
Gleb Natapov5601d052011-03-07 14:55:06 +02001482/* Does not support long mode */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001483static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Nadav Amitd1442d82014-09-18 22:39:39 +03001484 u16 selector, int seg, u8 cpl,
1485 bool in_task_switch,
1486 struct desc_struct *desc)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001487{
Avi Kivity869be992012-06-13 16:30:53 +03001488 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001489 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001490 unsigned err_vec = GP_VECTOR;
1491 u32 err_code = 0;
1492 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001493 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001494 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001495 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001496 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001497
1498 memset(&seg_desc, 0, sizeof seg_desc);
1499
Kevin Wolff8da94e2013-04-11 14:06:03 +02001500 if (ctxt->mode == X86EMUL_MODE_REAL) {
1501 /* set real mode segment descriptor (keep limit etc. for
1502 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001503 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001504 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001505 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001506 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1507 /* VM86 needs a clean new segment descriptor */
1508 set_desc_base(&seg_desc, selector << 4);
1509 set_desc_limit(&seg_desc, 0xffff);
1510 seg_desc.type = 3;
1511 seg_desc.p = 1;
1512 seg_desc.s = 1;
1513 seg_desc.dpl = 3;
1514 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001515 }
1516
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001517 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001518
1519 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1520 if ((seg == VCPU_SREG_CS
1521 || (seg == VCPU_SREG_SS
1522 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1523 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001524 && null_selector)
1525 goto exception;
1526
1527 /* TR should be in GDT only */
1528 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1529 goto exception;
1530
1531 if (null_selector) /* for NULL selector skip all following checks */
1532 goto load;
1533
Avi Kivitye9194642012-06-13 16:29:39 +03001534 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001535 if (ret != X86EMUL_CONTINUE)
1536 return ret;
1537
1538 err_code = selector & 0xfffc;
Paolo Bonzini15fc0752014-08-18 13:17:00 +02001539 err_vec = in_task_switch ? TS_VECTOR : GP_VECTOR;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001540
Guo Chaofc058682012-06-28 15:19:51 +08001541 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001542 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1543 goto exception;
1544
1545 if (!seg_desc.p) {
1546 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1547 goto exception;
1548 }
1549
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001550 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001551
1552 switch (seg) {
1553 case VCPU_SREG_SS:
1554 /*
1555 * segment is not a writable data segment or segment
1556 * selector's RPL != CPL or segment selector's RPL != CPL
1557 */
1558 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1559 goto exception;
1560 break;
1561 case VCPU_SREG_CS:
1562 if (!(seg_desc.type & 8))
1563 goto exception;
1564
1565 if (seg_desc.type & 4) {
1566 /* conforming */
1567 if (dpl > cpl)
1568 goto exception;
1569 } else {
1570 /* nonconforming */
1571 if (rpl > cpl || dpl != cpl)
1572 goto exception;
1573 }
Nadav Amit040c8dc2014-09-18 22:39:43 +03001574 /* in long-mode d/b must be clear if l is set */
1575 if (seg_desc.d && seg_desc.l) {
1576 u64 efer = 0;
1577
1578 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1579 if (efer & EFER_LMA)
1580 goto exception;
1581 }
1582
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001583 /* CS(RPL) <- CPL */
1584 selector = (selector & 0xfffc) | cpl;
1585 break;
1586 case VCPU_SREG_TR:
1587 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1588 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001589 old_desc = seg_desc;
1590 seg_desc.type |= 2; /* busy */
1591 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1592 sizeof(seg_desc), &ctxt->exception);
1593 if (ret != X86EMUL_CONTINUE)
1594 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001595 break;
1596 case VCPU_SREG_LDTR:
1597 if (seg_desc.s || seg_desc.type != 2)
1598 goto exception;
1599 break;
1600 default: /* DS, ES, FS, or GS */
1601 /*
1602 * segment is not a data or readable code segment or
1603 * ((segment is a data or nonconforming code segment)
1604 * and (both RPL and CPL > DPL))
1605 */
1606 if ((seg_desc.type & 0xa) == 0x8 ||
1607 (((seg_desc.type & 0xc) != 0xc) &&
1608 (rpl > dpl && cpl > dpl)))
1609 goto exception;
1610 break;
1611 }
1612
1613 if (seg_desc.s) {
1614 /* mark segment as accessed */
1615 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001616 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001617 if (ret != X86EMUL_CONTINUE)
1618 return ret;
Nadav Amite37a75a2014-06-02 18:34:04 +03001619 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
1620 ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3,
1621 sizeof(base3), &ctxt->exception);
1622 if (ret != X86EMUL_CONTINUE)
1623 return ret;
Nadav Amit9a9abf62014-11-02 11:54:56 +02001624 if (is_noncanonical_address(get_desc_base(&seg_desc) |
1625 ((u64)base3 << 32)))
1626 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001627 }
1628load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001629 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Nadav Amitd1442d82014-09-18 22:39:39 +03001630 if (desc)
1631 *desc = seg_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001632 return X86EMUL_CONTINUE;
1633exception:
Paolo Bonzini592f0852014-08-20 10:05:08 +02001634 return emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001635}
1636
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001637static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1638 u16 selector, int seg)
1639{
1640 u8 cpl = ctxt->ops->cpl(ctxt);
Nadav Amitd1442d82014-09-18 22:39:39 +03001641 return __load_segment_descriptor(ctxt, selector, seg, cpl, false, NULL);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001642}
1643
Wei Yongjun31be40b2010-08-17 09:17:30 +08001644static void write_register_operand(struct operand *op)
1645{
1646 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1647 switch (op->bytes) {
1648 case 1:
1649 *(u8 *)op->addr.reg = (u8)op->val;
1650 break;
1651 case 2:
1652 *(u16 *)op->addr.reg = (u16)op->val;
1653 break;
1654 case 4:
1655 *op->addr.reg = (u32)op->val;
1656 break; /* 64b: zero-extend */
1657 case 8:
1658 *op->addr.reg = op->val;
1659 break;
1660 }
1661}
1662
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001663static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001664{
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001665 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001666 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001667 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001668 break;
1669 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001670 if (ctxt->lock_prefix)
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001671 return segmented_cmpxchg(ctxt,
1672 op->addr.mem,
1673 &op->orig_val,
1674 &op->val,
1675 op->bytes);
1676 else
1677 return segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001678 op->addr.mem,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001679 &op->val,
1680 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001681 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001682 case OP_MEM_STR:
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001683 return segmented_write(ctxt,
1684 op->addr.mem,
1685 op->data,
1686 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001687 break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001688 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001689 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001690 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001691 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001692 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001693 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001694 case OP_NONE:
1695 /* no writeback */
1696 break;
1697 default:
1698 break;
1699 }
1700 return X86EMUL_CONTINUE;
1701}
1702
Avi Kivity51ddff52012-06-12 20:19:40 +03001703static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001704{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001705 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001706
Avi Kivity5ad105e2012-08-19 14:34:31 +03001707 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001708 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001709 addr.seg = VCPU_SREG_SS;
1710
Avi Kivity51ddff52012-06-12 20:19:40 +03001711 return segmented_write(ctxt, addr, data, bytes);
1712}
1713
1714static int em_push(struct x86_emulate_ctxt *ctxt)
1715{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001716 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001717 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001718 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001719}
1720
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001721static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001722 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001723{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001724 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001725 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001726
Avi Kivitydd856ef2012-08-27 23:46:17 +03001727 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001728 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001729 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001730 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001731 return rc;
1732
Avi Kivity5ad105e2012-08-19 14:34:31 +03001733 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001734 return rc;
1735}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001736
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001737static int em_pop(struct x86_emulate_ctxt *ctxt)
1738{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001739 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001740}
1741
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001742static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001743 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001744{
1745 int rc;
1746 unsigned long val, change_mask;
1747 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001748 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001749
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001750 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001751 if (rc != X86EMUL_CONTINUE)
1752 return rc;
1753
1754 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
Nadav Amit163b1352014-07-21 14:37:28 +03001755 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_AC | EFLG_ID;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001756
1757 switch(ctxt->mode) {
1758 case X86EMUL_MODE_PROT64:
1759 case X86EMUL_MODE_PROT32:
1760 case X86EMUL_MODE_PROT16:
1761 if (cpl == 0)
1762 change_mask |= EFLG_IOPL;
1763 if (cpl <= iopl)
1764 change_mask |= EFLG_IF;
1765 break;
1766 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001767 if (iopl < 3)
1768 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001769 change_mask |= EFLG_IF;
1770 break;
1771 default: /* real mode */
1772 change_mask |= (EFLG_IOPL | EFLG_IF);
1773 break;
1774 }
1775
1776 *(unsigned long *)dest =
1777 (ctxt->eflags & ~change_mask) | (val & change_mask);
1778
1779 return rc;
1780}
1781
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001782static int em_popf(struct x86_emulate_ctxt *ctxt)
1783{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001784 ctxt->dst.type = OP_REG;
1785 ctxt->dst.addr.reg = &ctxt->eflags;
1786 ctxt->dst.bytes = ctxt->op_bytes;
1787 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001788}
1789
Avi Kivity612e89f2012-06-12 20:03:23 +03001790static int em_enter(struct x86_emulate_ctxt *ctxt)
1791{
1792 int rc;
1793 unsigned frame_size = ctxt->src.val;
1794 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001795 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001796
1797 if (nesting_level)
1798 return X86EMUL_UNHANDLEABLE;
1799
Avi Kivitydd856ef2012-08-27 23:46:17 +03001800 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1801 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001802 if (rc != X86EMUL_CONTINUE)
1803 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001804 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001805 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001806 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1807 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001808 stack_mask(ctxt));
1809 return X86EMUL_CONTINUE;
1810}
1811
Avi Kivityf47cfa32012-06-07 17:49:24 +03001812static int em_leave(struct x86_emulate_ctxt *ctxt)
1813{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001814 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001815 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001816 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001817}
1818
Avi Kivity1cd196e2011-09-13 10:45:51 +03001819static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001820{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001821 int seg = ctxt->src2.val;
1822
Avi Kivity9dac77f2011-06-01 15:34:25 +03001823 ctxt->src.val = get_segment_selector(ctxt, seg);
Nadav Amit0fcc2072014-11-02 11:54:51 +02001824 if (ctxt->op_bytes == 4) {
1825 rsp_increment(ctxt, -2);
1826 ctxt->op_bytes = 2;
1827 }
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001828
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001829 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001830}
1831
Avi Kivity1cd196e2011-09-13 10:45:51 +03001832static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001833{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001834 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001835 unsigned long selector;
1836 int rc;
1837
Avi Kivity9dac77f2011-06-01 15:34:25 +03001838 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001839 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001840 return rc;
1841
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001842 if (ctxt->modrm_reg == VCPU_SREG_SS)
1843 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
1844
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001845 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001846 return rc;
1847}
1848
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001849static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001850{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001851 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001852 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001853 int reg = VCPU_REGS_RAX;
1854
1855 while (reg <= VCPU_REGS_RDI) {
1856 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001857 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001858
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001859 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001860 if (rc != X86EMUL_CONTINUE)
1861 return rc;
1862
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001863 ++reg;
1864 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001865
Wei Yongjunc37eda12010-06-15 09:03:33 +08001866 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001867}
1868
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001869static int em_pushf(struct x86_emulate_ctxt *ctxt)
1870{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001871 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001872 return em_push(ctxt);
1873}
1874
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001875static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001876{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001877 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001878 int reg = VCPU_REGS_RDI;
1879
1880 while (reg >= VCPU_REGS_RAX) {
1881 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001882 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001883 --reg;
1884 }
1885
Avi Kivitydd856ef2012-08-27 23:46:17 +03001886 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001887 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001888 break;
1889 --reg;
1890 }
1891 return rc;
1892}
1893
Avi Kivitydd856ef2012-08-27 23:46:17 +03001894static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001895{
Mathias Krause0225fb52012-08-30 01:30:16 +02001896 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001897 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001898 struct desc_ptr dt;
1899 gva_t cs_addr;
1900 gva_t eip_addr;
1901 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001902
1903 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001904 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001905 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001906 if (rc != X86EMUL_CONTINUE)
1907 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001908
1909 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1910
Avi Kivity9dac77f2011-06-01 15:34:25 +03001911 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001912 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001913 if (rc != X86EMUL_CONTINUE)
1914 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001915
Avi Kivity9dac77f2011-06-01 15:34:25 +03001916 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001917 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001918 if (rc != X86EMUL_CONTINUE)
1919 return rc;
1920
Avi Kivity4bff1e862011-04-20 13:37:53 +03001921 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001922
1923 eip_addr = dt.address + (irq << 2);
1924 cs_addr = dt.address + (irq << 2) + 2;
1925
Avi Kivity0f65dd72011-04-20 13:37:53 +03001926 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001927 if (rc != X86EMUL_CONTINUE)
1928 return rc;
1929
Avi Kivity0f65dd72011-04-20 13:37:53 +03001930 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001931 if (rc != X86EMUL_CONTINUE)
1932 return rc;
1933
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001934 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001935 if (rc != X86EMUL_CONTINUE)
1936 return rc;
1937
Avi Kivity9dac77f2011-06-01 15:34:25 +03001938 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001939
1940 return rc;
1941}
1942
Avi Kivitydd856ef2012-08-27 23:46:17 +03001943int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1944{
1945 int rc;
1946
1947 invalidate_registers(ctxt);
1948 rc = __emulate_int_real(ctxt, irq);
1949 if (rc == X86EMUL_CONTINUE)
1950 writeback_registers(ctxt);
1951 return rc;
1952}
1953
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001954static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001955{
1956 switch(ctxt->mode) {
1957 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001958 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001959 case X86EMUL_MODE_VM86:
1960 case X86EMUL_MODE_PROT16:
1961 case X86EMUL_MODE_PROT32:
1962 case X86EMUL_MODE_PROT64:
1963 default:
1964 /* Protected mode interrupts unimplemented yet */
1965 return X86EMUL_UNHANDLEABLE;
1966 }
1967}
1968
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001969static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001970{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001971 int rc = X86EMUL_CONTINUE;
1972 unsigned long temp_eip = 0;
1973 unsigned long temp_eflags = 0;
1974 unsigned long cs = 0;
1975 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1976 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1977 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1978 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1979
1980 /* TODO: Add stack limit check */
1981
Avi Kivity9dac77f2011-06-01 15:34:25 +03001982 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001983
1984 if (rc != X86EMUL_CONTINUE)
1985 return rc;
1986
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001987 if (temp_eip & ~0xffff)
1988 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001989
Avi Kivity9dac77f2011-06-01 15:34:25 +03001990 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001991
1992 if (rc != X86EMUL_CONTINUE)
1993 return rc;
1994
Avi Kivity9dac77f2011-06-01 15:34:25 +03001995 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001996
1997 if (rc != X86EMUL_CONTINUE)
1998 return rc;
1999
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002000 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002001
2002 if (rc != X86EMUL_CONTINUE)
2003 return rc;
2004
Avi Kivity9dac77f2011-06-01 15:34:25 +03002005 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002006
2007
Avi Kivity9dac77f2011-06-01 15:34:25 +03002008 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002009 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03002010 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002011 ctxt->eflags &= ~0xffff;
2012 ctxt->eflags |= temp_eflags;
2013 }
2014
2015 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
2016 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
2017
2018 return rc;
2019}
2020
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002021static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002022{
2023 switch(ctxt->mode) {
2024 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002025 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002026 case X86EMUL_MODE_VM86:
2027 case X86EMUL_MODE_PROT16:
2028 case X86EMUL_MODE_PROT32:
2029 case X86EMUL_MODE_PROT64:
2030 default:
2031 /* iret from protected mode unimplemented yet */
2032 return X86EMUL_UNHANDLEABLE;
2033 }
2034}
2035
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002036static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2037{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002038 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002039 unsigned short sel, old_sel;
2040 struct desc_struct old_desc, new_desc;
2041 const struct x86_emulate_ops *ops = ctxt->ops;
2042 u8 cpl = ctxt->ops->cpl(ctxt);
2043
2044 /* Assignment of RIP may only fail in 64-bit mode */
2045 if (ctxt->mode == X86EMUL_MODE_PROT64)
2046 ops->get_segment(ctxt, &old_sel, &old_desc, NULL,
2047 VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002048
Avi Kivity9dac77f2011-06-01 15:34:25 +03002049 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002050
Nadav Amitd1442d82014-09-18 22:39:39 +03002051 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl, false,
2052 &new_desc);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002053 if (rc != X86EMUL_CONTINUE)
2054 return rc;
2055
Nadav Amitd1442d82014-09-18 22:39:39 +03002056 rc = assign_eip_far(ctxt, ctxt->src.val, new_desc.l);
2057 if (rc != X86EMUL_CONTINUE) {
Nadav Amitcd9b8e2c2014-10-28 00:03:43 +02002058 WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64);
Nadav Amitd1442d82014-09-18 22:39:39 +03002059 /* assigning eip failed; restore the old cs */
2060 ops->set_segment(ctxt, old_sel, &old_desc, 0, VCPU_SREG_CS);
2061 return rc;
2062 }
2063 return rc;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002064}
2065
Nadav Amitf7784042014-09-18 22:39:41 +03002066static int em_jmp_abs(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002067{
Nadav Amitf7784042014-09-18 22:39:41 +03002068 return assign_eip_near(ctxt, ctxt->src.val);
2069}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002070
Nadav Amitf7784042014-09-18 22:39:41 +03002071static int em_call_near_abs(struct x86_emulate_ctxt *ctxt)
2072{
2073 int rc;
2074 long int old_eip;
2075
2076 old_eip = ctxt->_eip;
2077 rc = assign_eip_near(ctxt, ctxt->src.val);
2078 if (rc != X86EMUL_CONTINUE)
2079 return rc;
2080 ctxt->src.val = old_eip;
2081 rc = em_push(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002082 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002083}
2084
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002085static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002086{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002087 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002088
Nadav Amitaaa05f22014-06-02 18:34:10 +03002089 if (ctxt->dst.bytes == 16)
2090 return X86EMUL_UNHANDLEABLE;
2091
Avi Kivitydd856ef2012-08-27 23:46:17 +03002092 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2093 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2094 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2095 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002096 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002097 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002098 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2099 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002100
Laurent Vivier05f086f2007-09-24 11:10:55 +02002101 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002102 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002103 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002104}
2105
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002106static int em_ret(struct x86_emulate_ctxt *ctxt)
2107{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002108 int rc;
2109 unsigned long eip;
2110
2111 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
2112 if (rc != X86EMUL_CONTINUE)
2113 return rc;
2114
2115 return assign_eip_near(ctxt, eip);
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002116}
2117
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002118static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002119{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002120 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002121 unsigned long eip, cs;
2122 u16 old_cs;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002123 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amitd1442d82014-09-18 22:39:39 +03002124 struct desc_struct old_desc, new_desc;
2125 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002126
Nadav Amitd1442d82014-09-18 22:39:39 +03002127 if (ctxt->mode == X86EMUL_MODE_PROT64)
2128 ops->get_segment(ctxt, &old_cs, &old_desc, NULL,
2129 VCPU_SREG_CS);
2130
2131 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002132 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002133 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002134 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002135 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002136 return rc;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002137 /* Outer-privilege level return is not implemented */
2138 if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2139 return X86EMUL_UNHANDLEABLE;
Nadav Amitd1442d82014-09-18 22:39:39 +03002140 rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, 0, false,
2141 &new_desc);
2142 if (rc != X86EMUL_CONTINUE)
2143 return rc;
2144 rc = assign_eip_far(ctxt, eip, new_desc.l);
2145 if (rc != X86EMUL_CONTINUE) {
Nadav Amitcd9b8e2c2014-10-28 00:03:43 +02002146 WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64);
Nadav Amitd1442d82014-09-18 22:39:39 +03002147 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
2148 }
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002149 return rc;
2150}
2151
Bruce Rogers32611072013-09-09 09:40:20 -06002152static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2153{
2154 int rc;
2155
2156 rc = em_ret_far(ctxt);
2157 if (rc != X86EMUL_CONTINUE)
2158 return rc;
2159 rsp_increment(ctxt, ctxt->src.val);
2160 return X86EMUL_CONTINUE;
2161}
2162
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002163static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2164{
2165 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002166 ctxt->dst.orig_val = ctxt->dst.val;
2167 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002168 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002169 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002170 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002171
2172 if (ctxt->eflags & EFLG_ZF) {
2173 /* Success: write back to memory. */
2174 ctxt->dst.val = ctxt->src.orig_val;
2175 } else {
2176 /* Failure: write the value we saw to EAX. */
2177 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002178 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Nadav Amit37c564f2014-06-02 18:34:07 +03002179 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002180 }
2181 return X86EMUL_CONTINUE;
2182}
2183
Avi Kivityd4b43252011-09-13 10:45:50 +03002184static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002185{
Avi Kivityd4b43252011-09-13 10:45:50 +03002186 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002187 unsigned short sel;
2188 int rc;
2189
Avi Kivity9dac77f2011-06-01 15:34:25 +03002190 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002191
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002192 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002193 if (rc != X86EMUL_CONTINUE)
2194 return rc;
2195
Avi Kivity9dac77f2011-06-01 15:34:25 +03002196 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002197 return rc;
2198}
2199
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002200static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002201setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002202 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002203{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002204 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002205 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002206 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002207 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002208 cs->type = 0x0b; /* Read, Execute, Accessed */
2209 cs->s = 1;
2210 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002211 cs->p = 1;
2212 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002213 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002214
Gleb Natapov79168fd2010-04-28 19:15:30 +03002215 set_desc_base(ss, 0); /* flat segment */
2216 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002217 ss->g = 1; /* 4kb granularity */
2218 ss->s = 1;
2219 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002220 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002221 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002222 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002223 ss->l = 0;
2224 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002225}
2226
Avi Kivity1a18a692012-02-01 12:23:21 +02002227static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2228{
2229 u32 eax, ebx, ecx, edx;
2230
2231 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002232 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2233 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002234 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2235 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2236}
2237
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002238static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2239{
Mathias Krause0225fb52012-08-30 01:30:16 +02002240 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002241 u32 eax, ebx, ecx, edx;
2242
2243 /*
2244 * syscall should always be enabled in longmode - so only become
2245 * vendor specific (cpuid) if other modes are active...
2246 */
2247 if (ctxt->mode == X86EMUL_MODE_PROT64)
2248 return true;
2249
2250 eax = 0x00000000;
2251 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002252 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2253 /*
2254 * Intel ("GenuineIntel")
2255 * remark: Intel CPUs only support "syscall" in 64bit
2256 * longmode. Also an 64bit guest with a
2257 * 32bit compat-app running will #UD !! While this
2258 * behaviour can be fixed (by emulating) into AMD
2259 * response - CPUs of AMD can't behave like Intel.
2260 */
2261 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2262 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2263 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2264 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002265
Avi Kivity0017f932012-06-07 14:10:16 +03002266 /* AMD ("AuthenticAMD") */
2267 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2268 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2269 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2270 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002271
Avi Kivity0017f932012-06-07 14:10:16 +03002272 /* AMD ("AMDisbetter!") */
2273 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2274 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2275 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2276 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002277
2278 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2279 return false;
2280}
2281
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002282static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002283{
Mathias Krause0225fb52012-08-30 01:30:16 +02002284 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002285 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002286 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002287 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002288 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002289
2290 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002291 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002292 ctxt->mode == X86EMUL_MODE_VM86)
2293 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002294
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002295 if (!(em_syscall_is_enabled(ctxt)))
2296 return emulate_ud(ctxt);
2297
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002298 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002299 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002300
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002301 if (!(efer & EFER_SCE))
2302 return emulate_ud(ctxt);
2303
Avi Kivity717746e2011-04-20 13:37:53 +03002304 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002305 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002306 cs_sel = (u16)(msr_data & 0xfffc);
2307 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002308
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002309 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002310 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002311 cs.l = 1;
2312 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002313 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2314 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002315
Avi Kivitydd856ef2012-08-27 23:46:17 +03002316 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002317 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002318#ifdef CONFIG_X86_64
Nadav Amit6c6cb692014-07-21 14:37:30 +03002319 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002320
Avi Kivity717746e2011-04-20 13:37:53 +03002321 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002322 ctxt->mode == X86EMUL_MODE_PROT64 ?
2323 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002324 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002325
Avi Kivity717746e2011-04-20 13:37:53 +03002326 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Nadav Amit6c6cb692014-07-21 14:37:30 +03002327 ctxt->eflags &= ~msr_data;
Nadav Amit807c1422014-11-02 11:54:49 +02002328 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002329#endif
2330 } else {
2331 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002332 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002333 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002334
Nadav Amit6c6cb692014-07-21 14:37:30 +03002335 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002336 }
2337
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002338 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002339}
2340
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002341static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +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 Przywara8c604352009-06-18 12:56:01 +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 Przywara8c604352009-06-18 12:56:01 +02002348
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002349 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002350 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002351 if (ctxt->mode == X86EMUL_MODE_REAL)
2352 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002353
Avi Kivity1a18a692012-02-01 12:23:21 +02002354 /*
2355 * Not recognized on AMD in compat mode (but is recognized in legacy
2356 * mode).
2357 */
2358 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2359 && !vendor_intel(ctxt))
2360 return emulate_ud(ctxt);
2361
Nadav Amitb2c9d432014-11-02 11:55:01 +02002362 /* sysenter/sysexit have not been tested in 64bit mode. */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002363 if (ctxt->mode == X86EMUL_MODE_PROT64)
Nadav Amitb2c9d432014-11-02 11:55:01 +02002364 return X86EMUL_UNHANDLEABLE;
Andre Przywara8c604352009-06-18 12:56:01 +02002365
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002366 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002367
Avi Kivity717746e2011-04-20 13:37:53 +03002368 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002369 switch (ctxt->mode) {
2370 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002371 if ((msr_data & 0xfffc) == 0x0)
2372 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002373 break;
2374 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002375 if (msr_data == 0x0)
2376 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002377 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002378 default:
2379 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002380 }
2381
Nadav Amit6c6cb692014-07-21 14:37:30 +03002382 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002383 cs_sel = (u16)msr_data;
2384 cs_sel &= ~SELECTOR_RPL_MASK;
2385 ss_sel = cs_sel + 8;
2386 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002387 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002388 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002389 cs.l = 1;
2390 }
2391
Avi Kivity1aa36612011-04-27 13:20:30 +03002392 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2393 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002394
Avi Kivity717746e2011-04-20 13:37:53 +03002395 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002396 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002397
Avi Kivity717746e2011-04-20 13:37:53 +03002398 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002399 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002400
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002401 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002402}
2403
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002404static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002405{
Mathias Krause0225fb52012-08-30 01:30:16 +02002406 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002407 struct desc_struct cs, ss;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002408 u64 msr_data, rcx, rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002409 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002410 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002411
Gleb Natapova0044752010-02-10 14:21:31 +02002412 /* inject #GP if in real mode or Virtual 8086 mode */
2413 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002414 ctxt->mode == X86EMUL_MODE_VM86)
2415 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002416
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002417 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002418
Avi Kivity9dac77f2011-06-01 15:34:25 +03002419 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002420 usermode = X86EMUL_MODE_PROT64;
2421 else
2422 usermode = X86EMUL_MODE_PROT32;
2423
Nadav Amit234f3ce2014-09-18 22:39:38 +03002424 rcx = reg_read(ctxt, VCPU_REGS_RCX);
2425 rdx = reg_read(ctxt, VCPU_REGS_RDX);
2426
Andre Przywara4668f052009-06-18 12:56:02 +02002427 cs.dpl = 3;
2428 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002429 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002430 switch (usermode) {
2431 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002432 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002433 if ((msr_data & 0xfffc) == 0x0)
2434 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002435 ss_sel = (u16)(msr_data + 24);
Nadav Amitbf0b6822014-09-18 22:39:45 +03002436 rcx = (u32)rcx;
2437 rdx = (u32)rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002438 break;
2439 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002440 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002441 if (msr_data == 0x0)
2442 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002443 ss_sel = cs_sel + 8;
2444 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002445 cs.l = 1;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002446 if (is_noncanonical_address(rcx) ||
2447 is_noncanonical_address(rdx))
2448 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002449 break;
2450 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002451 cs_sel |= SELECTOR_RPL_MASK;
2452 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002453
Avi Kivity1aa36612011-04-27 13:20:30 +03002454 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2455 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002456
Nadav Amit234f3ce2014-09-18 22:39:38 +03002457 ctxt->_eip = rdx;
2458 *reg_write(ctxt, VCPU_REGS_RSP) = rcx;
Andre Przywara4668f052009-06-18 12:56:02 +02002459
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002460 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002461}
2462
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002463static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002464{
2465 int iopl;
2466 if (ctxt->mode == X86EMUL_MODE_REAL)
2467 return false;
2468 if (ctxt->mode == X86EMUL_MODE_VM86)
2469 return true;
2470 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002471 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002472}
2473
2474static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002475 u16 port, u16 len)
2476{
Mathias Krause0225fb52012-08-30 01:30:16 +02002477 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002478 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002479 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002480 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002481 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002482 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002483 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002484
Avi Kivity1aa36612011-04-27 13:20:30 +03002485 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002486 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002487 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002488 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002489 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002490 base = get_desc_base(&tr_seg);
2491#ifdef CONFIG_X86_64
2492 base |= ((u64)base3) << 32;
2493#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002494 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002495 if (r != X86EMUL_CONTINUE)
2496 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002497 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002498 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002499 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002500 if (r != X86EMUL_CONTINUE)
2501 return false;
2502 if ((perm >> bit_idx) & mask)
2503 return false;
2504 return true;
2505}
2506
2507static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002508 u16 port, u16 len)
2509{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002510 if (ctxt->perm_ok)
2511 return true;
2512
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002513 if (emulator_bad_iopl(ctxt))
2514 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002515 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002516
2517 ctxt->perm_ok = true;
2518
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002519 return true;
2520}
2521
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002522static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002523 struct tss_segment_16 *tss)
2524{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002525 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002526 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002527 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2528 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2529 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2530 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2531 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2532 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2533 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2534 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002535
Avi Kivity1aa36612011-04-27 13:20:30 +03002536 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2537 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2538 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2539 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2540 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002541}
2542
2543static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002544 struct tss_segment_16 *tss)
2545{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002546 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002547 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002548
Avi Kivity9dac77f2011-06-01 15:34:25 +03002549 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002550 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002551 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2552 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2553 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2554 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2555 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2556 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2557 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2558 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002559
2560 /*
2561 * SDM says that segment selectors are loaded before segment
2562 * descriptors
2563 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002564 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2565 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2566 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2567 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2568 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002569
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002570 cpl = tss->cs & 3;
2571
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002572 /*
Guo Chaofc058682012-06-28 15:19:51 +08002573 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002574 * it is handled in a context of new task
2575 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002576 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl,
2577 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002578 if (ret != X86EMUL_CONTINUE)
2579 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002580 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
2581 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002582 if (ret != X86EMUL_CONTINUE)
2583 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002584 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
2585 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002586 if (ret != X86EMUL_CONTINUE)
2587 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002588 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
2589 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002590 if (ret != X86EMUL_CONTINUE)
2591 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002592 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
2593 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002594 if (ret != X86EMUL_CONTINUE)
2595 return ret;
2596
2597 return X86EMUL_CONTINUE;
2598}
2599
2600static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002601 u16 tss_selector, u16 old_tss_sel,
2602 ulong old_tss_base, struct desc_struct *new_desc)
2603{
Mathias Krause0225fb52012-08-30 01:30:16 +02002604 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002605 struct tss_segment_16 tss_seg;
2606 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002607 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002608
Avi Kivity0f65dd72011-04-20 13:37:53 +03002609 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002610 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002611 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002612 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002613 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002614
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002615 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002616
Avi Kivity0f65dd72011-04-20 13:37:53 +03002617 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002618 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002619 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002620 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002621 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002622
Avi Kivity0f65dd72011-04-20 13:37:53 +03002623 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002624 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002625 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002626 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002627 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002628
2629 if (old_tss_sel != 0xffff) {
2630 tss_seg.prev_task_link = old_tss_sel;
2631
Avi Kivity0f65dd72011-04-20 13:37:53 +03002632 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002633 &tss_seg.prev_task_link,
2634 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002635 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002636 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002637 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002638 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002639 }
2640
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002641 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002642}
2643
2644static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002645 struct tss_segment_32 *tss)
2646{
Nadav Amit5c7411e2014-04-07 18:37:47 +03002647 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002648 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002649 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002650 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2651 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2652 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2653 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2654 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2655 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2656 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2657 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002658
Avi Kivity1aa36612011-04-27 13:20:30 +03002659 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2660 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2661 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2662 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2663 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2664 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002665}
2666
2667static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002668 struct tss_segment_32 *tss)
2669{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002670 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002671 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002672
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002673 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002674 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002675 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002676 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002677
2678 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002679 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2680 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2681 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2682 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2683 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2684 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2685 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2686 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002687
2688 /*
2689 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002690 * descriptors. This is important because CPL checks will
2691 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002692 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002693 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2694 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2695 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2696 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2697 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2698 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2699 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002700
2701 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002702 * If we're switching between Protected Mode and VM86, we need to make
2703 * sure to update the mode before loading the segment descriptors so
2704 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01002705 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002706 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002707 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002708 cpl = 3;
2709 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002710 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002711 cpl = tss->cs & 3;
2712 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01002713
2714 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002715 * Now load segment descriptors. If fault happenes at this stage
2716 * it is handled in a context of new task
2717 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002718 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR,
2719 cpl, true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002720 if (ret != X86EMUL_CONTINUE)
2721 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002722 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
2723 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002724 if (ret != X86EMUL_CONTINUE)
2725 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002726 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
2727 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002728 if (ret != X86EMUL_CONTINUE)
2729 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002730 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
2731 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002732 if (ret != X86EMUL_CONTINUE)
2733 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002734 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
2735 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002736 if (ret != X86EMUL_CONTINUE)
2737 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002738 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl,
2739 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002740 if (ret != X86EMUL_CONTINUE)
2741 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002742 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl,
2743 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002744 if (ret != X86EMUL_CONTINUE)
2745 return ret;
2746
2747 return X86EMUL_CONTINUE;
2748}
2749
2750static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002751 u16 tss_selector, u16 old_tss_sel,
2752 ulong old_tss_base, struct desc_struct *new_desc)
2753{
Mathias Krause0225fb52012-08-30 01:30:16 +02002754 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002755 struct tss_segment_32 tss_seg;
2756 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002757 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03002758 u32 eip_offset = offsetof(struct tss_segment_32, eip);
2759 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002760
Avi Kivity0f65dd72011-04-20 13:37:53 +03002761 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002762 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002763 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002764 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002765 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002766
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002767 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002768
Nadav Amit5c7411e2014-04-07 18:37:47 +03002769 /* Only GP registers and segment selectors are saved */
2770 ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
2771 ldt_sel_offset - eip_offset, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002772 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002773 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002774 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002775
Avi Kivity0f65dd72011-04-20 13:37:53 +03002776 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002777 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002778 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002779 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002780 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002781
2782 if (old_tss_sel != 0xffff) {
2783 tss_seg.prev_task_link = old_tss_sel;
2784
Avi Kivity0f65dd72011-04-20 13:37:53 +03002785 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002786 &tss_seg.prev_task_link,
2787 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002788 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002789 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002790 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002791 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002792 }
2793
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002794 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002795}
2796
2797static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002798 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002799 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002800{
Mathias Krause0225fb52012-08-30 01:30:16 +02002801 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002802 struct desc_struct curr_tss_desc, next_tss_desc;
2803 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002804 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002805 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002806 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002807 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002808 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002809
2810 /* FIXME: old_tss_base == ~0 ? */
2811
Avi Kivitye9194642012-06-13 16:29:39 +03002812 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002813 if (ret != X86EMUL_CONTINUE)
2814 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002815 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002816 if (ret != X86EMUL_CONTINUE)
2817 return ret;
2818
2819 /* FIXME: check that next_tss_desc is tss */
2820
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002821 /*
2822 * Check privileges. The three cases are task switch caused by...
2823 *
2824 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2825 * 2. Exception/IRQ/iret: No check is performed
Nadav Amit2c2ca2d2014-11-02 11:54:57 +02002826 * 3. jmp/call to TSS/task-gate: No check is performed since the
2827 * hardware checks it before exiting.
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002828 */
2829 if (reason == TASK_SWITCH_GATE) {
2830 if (idt_index != -1) {
2831 /* Software interrupts */
2832 struct desc_struct task_gate_desc;
2833 int dpl;
2834
2835 ret = read_interrupt_descriptor(ctxt, idt_index,
2836 &task_gate_desc);
2837 if (ret != X86EMUL_CONTINUE)
2838 return ret;
2839
2840 dpl = task_gate_desc.dpl;
2841 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2842 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2843 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002844 }
2845
Gleb Natapovceffb452010-03-18 15:20:19 +02002846 desc_limit = desc_limit_scaled(&next_tss_desc);
2847 if (!next_tss_desc.p ||
2848 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2849 desc_limit < 0x2b)) {
Paolo Bonzini592f0852014-08-20 10:05:08 +02002850 return emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002851 }
2852
2853 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2854 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002855 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002856 }
2857
2858 if (reason == TASK_SWITCH_IRET)
2859 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2860
2861 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002862 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002863 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2864 old_tss_sel = 0xffff;
2865
2866 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002867 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002868 old_tss_base, &next_tss_desc);
2869 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002870 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002871 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002872 if (ret != X86EMUL_CONTINUE)
2873 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002874
2875 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2876 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2877
2878 if (reason != TASK_SWITCH_IRET) {
2879 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002880 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002881 }
2882
Avi Kivity717746e2011-04-20 13:37:53 +03002883 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002884 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002885
Jan Kiszkae269fb22010-04-14 15:51:09 +02002886 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002887 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2888 ctxt->lock_prefix = 0;
2889 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002890 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002891 }
2892
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002893 return ret;
2894}
2895
2896int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002897 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002898 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002899{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002900 int rc;
2901
Avi Kivitydd856ef2012-08-27 23:46:17 +03002902 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002903 ctxt->_eip = ctxt->eip;
2904 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002905
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002906 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002907 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002908
Avi Kivitydd856ef2012-08-27 23:46:17 +03002909 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002910 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002911 writeback_registers(ctxt);
2912 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002913
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002914 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002915}
2916
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002917static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2918 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002919{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002920 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002921
Avi Kivitydd856ef2012-08-27 23:46:17 +03002922 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2923 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002924}
2925
Avi Kivity7af04fc2010-08-18 14:16:35 +03002926static int em_das(struct x86_emulate_ctxt *ctxt)
2927{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002928 u8 al, old_al;
2929 bool af, cf, old_cf;
2930
2931 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002932 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002933
2934 old_al = al;
2935 old_cf = cf;
2936 cf = false;
2937 af = ctxt->eflags & X86_EFLAGS_AF;
2938 if ((al & 0x0f) > 9 || af) {
2939 al -= 6;
2940 cf = old_cf | (al >= 250);
2941 af = true;
2942 } else {
2943 af = false;
2944 }
2945 if (old_al > 0x99 || old_cf) {
2946 al -= 0x60;
2947 cf = true;
2948 }
2949
Avi Kivity9dac77f2011-06-01 15:34:25 +03002950 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002951 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002952 ctxt->src.type = OP_IMM;
2953 ctxt->src.val = 0;
2954 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02002955 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03002956 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2957 if (cf)
2958 ctxt->eflags |= X86_EFLAGS_CF;
2959 if (af)
2960 ctxt->eflags |= X86_EFLAGS_AF;
2961 return X86EMUL_CONTINUE;
2962}
2963
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02002964static int em_aam(struct x86_emulate_ctxt *ctxt)
2965{
2966 u8 al, ah;
2967
2968 if (ctxt->src.val == 0)
2969 return emulate_de(ctxt);
2970
2971 al = ctxt->dst.val & 0xff;
2972 ah = al / ctxt->src.val;
2973 al %= ctxt->src.val;
2974
2975 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
2976
2977 /* Set PF, ZF, SF */
2978 ctxt->src.type = OP_IMM;
2979 ctxt->src.val = 0;
2980 ctxt->src.bytes = 1;
2981 fastop(ctxt, em_or);
2982
2983 return X86EMUL_CONTINUE;
2984}
2985
Gleb Natapov7f662272012-12-10 11:42:30 +02002986static int em_aad(struct x86_emulate_ctxt *ctxt)
2987{
2988 u8 al = ctxt->dst.val & 0xff;
2989 u8 ah = (ctxt->dst.val >> 8) & 0xff;
2990
2991 al = (al + (ah * ctxt->src.val)) & 0xff;
2992
2993 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
2994
Gleb Natapovf583c292013-02-13 17:50:39 +02002995 /* Set PF, ZF, SF */
2996 ctxt->src.type = OP_IMM;
2997 ctxt->src.val = 0;
2998 ctxt->src.bytes = 1;
2999 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02003000
3001 return X86EMUL_CONTINUE;
3002}
3003
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003004static int em_call(struct x86_emulate_ctxt *ctxt)
3005{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003006 int rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003007 long rel = ctxt->src.val;
3008
3009 ctxt->src.val = (unsigned long)ctxt->_eip;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003010 rc = jmp_rel(ctxt, rel);
3011 if (rc != X86EMUL_CONTINUE)
3012 return rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003013 return em_push(ctxt);
3014}
3015
Avi Kivity0ef753b2010-08-18 14:51:45 +03003016static int em_call_far(struct x86_emulate_ctxt *ctxt)
3017{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003018 u16 sel, old_cs;
3019 ulong old_eip;
3020 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03003021 struct desc_struct old_desc, new_desc;
3022 const struct x86_emulate_ops *ops = ctxt->ops;
3023 int cpl = ctxt->ops->cpl(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003024
Avi Kivity9dac77f2011-06-01 15:34:25 +03003025 old_eip = ctxt->_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003026 ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003027
Avi Kivity9dac77f2011-06-01 15:34:25 +03003028 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Nadav Amitd1442d82014-09-18 22:39:39 +03003029 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl, false,
3030 &new_desc);
3031 if (rc != X86EMUL_CONTINUE)
Avi Kivity0ef753b2010-08-18 14:51:45 +03003032 return X86EMUL_CONTINUE;
3033
Nadav Amitd1442d82014-09-18 22:39:39 +03003034 rc = assign_eip_far(ctxt, ctxt->src.val, new_desc.l);
3035 if (rc != X86EMUL_CONTINUE)
3036 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003037
Avi Kivity9dac77f2011-06-01 15:34:25 +03003038 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003039 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003040 if (rc != X86EMUL_CONTINUE)
Nadav Amitd1442d82014-09-18 22:39:39 +03003041 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003042
Avi Kivity9dac77f2011-06-01 15:34:25 +03003043 ctxt->src.val = old_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003044 rc = em_push(ctxt);
3045 /* If we failed, we tainted the memory, but the very least we should
3046 restore cs */
3047 if (rc != X86EMUL_CONTINUE)
3048 goto fail;
3049 return rc;
3050fail:
3051 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
3052 return rc;
3053
Avi Kivity0ef753b2010-08-18 14:51:45 +03003054}
3055
Avi Kivity40ece7c2010-08-18 15:12:09 +03003056static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3057{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003058 int rc;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003059 unsigned long eip;
Avi Kivity40ece7c2010-08-18 15:12:09 +03003060
Nadav Amit234f3ce2014-09-18 22:39:38 +03003061 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
3062 if (rc != X86EMUL_CONTINUE)
3063 return rc;
3064 rc = assign_eip_near(ctxt, eip);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003065 if (rc != X86EMUL_CONTINUE)
3066 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003067 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003068 return X86EMUL_CONTINUE;
3069}
3070
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003071static int em_xchg(struct x86_emulate_ctxt *ctxt)
3072{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003073 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003074 ctxt->src.val = ctxt->dst.val;
3075 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003076
3077 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003078 ctxt->dst.val = ctxt->src.orig_val;
3079 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003080 return X86EMUL_CONTINUE;
3081}
3082
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003083static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3084{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003085 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003086 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003087}
3088
Avi Kivity61429142010-08-19 15:13:00 +03003089static int em_cwd(struct x86_emulate_ctxt *ctxt)
3090{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003091 ctxt->dst.type = OP_REG;
3092 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003093 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003094 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003095
3096 return X86EMUL_CONTINUE;
3097}
3098
Avi Kivity48bb5d32010-08-18 18:54:34 +03003099static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3100{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003101 u64 tsc = 0;
3102
Avi Kivity717746e2011-04-20 13:37:53 +03003103 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003104 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3105 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003106 return X86EMUL_CONTINUE;
3107}
3108
Avi Kivity222d21a2011-11-10 14:57:30 +02003109static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3110{
3111 u64 pmc;
3112
Avi Kivitydd856ef2012-08-27 23:46:17 +03003113 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003114 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003115 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3116 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003117 return X86EMUL_CONTINUE;
3118}
3119
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003120static int em_mov(struct x86_emulate_ctxt *ctxt)
3121{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01003122 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003123 return X86EMUL_CONTINUE;
3124}
3125
Borislav Petkov84cffe42013-10-29 12:54:56 +01003126#define FFL(x) bit(X86_FEATURE_##x)
3127
3128static int em_movbe(struct x86_emulate_ctxt *ctxt)
3129{
3130 u32 ebx, ecx, edx, eax = 1;
3131 u16 tmp;
3132
3133 /*
3134 * Check MOVBE is set in the guest-visible CPUID leaf.
3135 */
3136 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
3137 if (!(ecx & FFL(MOVBE)))
3138 return emulate_ud(ctxt);
3139
3140 switch (ctxt->op_bytes) {
3141 case 2:
3142 /*
3143 * From MOVBE definition: "...When the operand size is 16 bits,
3144 * the upper word of the destination register remains unchanged
3145 * ..."
3146 *
3147 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3148 * rules so we have to do the operation almost per hand.
3149 */
3150 tmp = (u16)ctxt->src.val;
3151 ctxt->dst.val &= ~0xffffUL;
3152 ctxt->dst.val |= (unsigned long)swab16(tmp);
3153 break;
3154 case 4:
3155 ctxt->dst.val = swab32((u32)ctxt->src.val);
3156 break;
3157 case 8:
3158 ctxt->dst.val = swab64(ctxt->src.val);
3159 break;
3160 default:
Paolo Bonzini592f0852014-08-20 10:05:08 +02003161 BUG();
Borislav Petkov84cffe42013-10-29 12:54:56 +01003162 }
3163 return X86EMUL_CONTINUE;
3164}
3165
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003166static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3167{
3168 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3169 return emulate_gp(ctxt, 0);
3170
3171 /* Disable writeback. */
3172 ctxt->dst.type = OP_NONE;
3173 return X86EMUL_CONTINUE;
3174}
3175
3176static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3177{
3178 unsigned long val;
3179
3180 if (ctxt->mode == X86EMUL_MODE_PROT64)
3181 val = ctxt->src.val & ~0ULL;
3182 else
3183 val = ctxt->src.val & ~0U;
3184
3185 /* #UD condition is already handled. */
3186 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3187 return emulate_gp(ctxt, 0);
3188
3189 /* Disable writeback. */
3190 ctxt->dst.type = OP_NONE;
3191 return X86EMUL_CONTINUE;
3192}
3193
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003194static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3195{
3196 u64 msr_data;
3197
Avi Kivitydd856ef2012-08-27 23:46:17 +03003198 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3199 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3200 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003201 return emulate_gp(ctxt, 0);
3202
3203 return X86EMUL_CONTINUE;
3204}
3205
3206static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3207{
3208 u64 msr_data;
3209
Avi Kivitydd856ef2012-08-27 23:46:17 +03003210 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003211 return emulate_gp(ctxt, 0);
3212
Avi Kivitydd856ef2012-08-27 23:46:17 +03003213 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3214 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003215 return X86EMUL_CONTINUE;
3216}
3217
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003218static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3219{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003220 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003221 return emulate_ud(ctxt);
3222
Avi Kivity9dac77f2011-06-01 15:34:25 +03003223 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Nadav Amitb5bbf102014-11-02 11:54:46 +02003224 if (ctxt->dst.bytes == 4 && ctxt->dst.type == OP_MEM)
3225 ctxt->dst.bytes = 2;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003226 return X86EMUL_CONTINUE;
3227}
3228
3229static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3230{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003231 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003232
Avi Kivity9dac77f2011-06-01 15:34:25 +03003233 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003234 return emulate_ud(ctxt);
3235
Avi Kivity9dac77f2011-06-01 15:34:25 +03003236 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003237 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3238
3239 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003240 ctxt->dst.type = OP_NONE;
3241 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003242}
3243
Avi Kivitya14e5792012-06-13 12:28:33 +03003244static int em_lldt(struct x86_emulate_ctxt *ctxt)
3245{
3246 u16 sel = ctxt->src.val;
3247
3248 /* Disable writeback. */
3249 ctxt->dst.type = OP_NONE;
3250 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3251}
3252
Avi Kivity80890002012-06-13 16:33:29 +03003253static int em_ltr(struct x86_emulate_ctxt *ctxt)
3254{
3255 u16 sel = ctxt->src.val;
3256
3257 /* Disable writeback. */
3258 ctxt->dst.type = OP_NONE;
3259 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3260}
3261
Avi Kivity38503912011-03-31 18:48:09 +02003262static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3263{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003264 int rc;
3265 ulong linear;
3266
Avi Kivity9dac77f2011-06-01 15:34:25 +03003267 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003268 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003269 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003270 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003271 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003272 return X86EMUL_CONTINUE;
3273}
3274
Avi Kivity2d04a052011-04-20 15:32:49 +03003275static int em_clts(struct x86_emulate_ctxt *ctxt)
3276{
3277 ulong cr0;
3278
3279 cr0 = ctxt->ops->get_cr(ctxt, 0);
3280 cr0 &= ~X86_CR0_TS;
3281 ctxt->ops->set_cr(ctxt, 0, cr0);
3282 return X86EMUL_CONTINUE;
3283}
3284
Avi Kivity26d05cc2011-04-21 12:07:59 +03003285static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3286{
Nadav Amit0f54a322014-08-29 11:26:55 +03003287 int rc = ctxt->ops->fix_hypercall(ctxt);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003288
Avi Kivity26d05cc2011-04-21 12:07:59 +03003289 if (rc != X86EMUL_CONTINUE)
3290 return rc;
3291
3292 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003293 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003294 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003295 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003296 return X86EMUL_CONTINUE;
3297}
3298
Avi Kivity96051572012-06-10 17:21:18 +03003299static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3300 void (*get)(struct x86_emulate_ctxt *ctxt,
3301 struct desc_ptr *ptr))
3302{
3303 struct desc_ptr desc_ptr;
3304
3305 if (ctxt->mode == X86EMUL_MODE_PROT64)
3306 ctxt->op_bytes = 8;
3307 get(ctxt, &desc_ptr);
3308 if (ctxt->op_bytes == 2) {
3309 ctxt->op_bytes = 4;
3310 desc_ptr.address &= 0x00ffffff;
3311 }
3312 /* Disable writeback. */
3313 ctxt->dst.type = OP_NONE;
3314 return segmented_write(ctxt, ctxt->dst.addr.mem,
3315 &desc_ptr, 2 + ctxt->op_bytes);
3316}
3317
3318static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3319{
3320 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3321}
3322
3323static int em_sidt(struct x86_emulate_ctxt *ctxt)
3324{
3325 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3326}
3327
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003328static int em_lgdt_lidt(struct x86_emulate_ctxt *ctxt, bool lgdt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003329{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003330 struct desc_ptr desc_ptr;
3331 int rc;
3332
Avi Kivity510425f2012-06-07 17:04:36 +03003333 if (ctxt->mode == X86EMUL_MODE_PROT64)
3334 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003335 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003336 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003337 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003338 if (rc != X86EMUL_CONTINUE)
3339 return rc;
Nadav Amit9a9abf62014-11-02 11:54:56 +02003340 if (ctxt->mode == X86EMUL_MODE_PROT64 &&
3341 is_noncanonical_address(desc_ptr.address))
3342 return emulate_gp(ctxt, 0);
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003343 if (lgdt)
3344 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3345 else
3346 ctxt->ops->set_idt(ctxt, &desc_ptr);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003347 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003348 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003349 return X86EMUL_CONTINUE;
3350}
3351
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003352static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3353{
3354 return em_lgdt_lidt(ctxt, true);
3355}
3356
Avi Kivity5ef39c72011-04-21 12:21:50 +03003357static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003358{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003359 int rc;
3360
Avi Kivity5ef39c72011-04-21 12:21:50 +03003361 rc = ctxt->ops->fix_hypercall(ctxt);
3362
Avi Kivity26d05cc2011-04-21 12:07:59 +03003363 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003364 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003365 return rc;
3366}
3367
3368static int em_lidt(struct x86_emulate_ctxt *ctxt)
3369{
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003370 return em_lgdt_lidt(ctxt, false);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003371}
3372
3373static int em_smsw(struct x86_emulate_ctxt *ctxt)
3374{
Nadav Amit32e94d02014-06-02 18:34:11 +03003375 if (ctxt->dst.type == OP_MEM)
3376 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003377 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003378 return X86EMUL_CONTINUE;
3379}
3380
3381static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3382{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003383 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003384 | (ctxt->src.val & 0x0f));
3385 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003386 return X86EMUL_CONTINUE;
3387}
3388
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003389static int em_loop(struct x86_emulate_ctxt *ctxt)
3390{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003391 int rc = X86EMUL_CONTINUE;
3392
Avi Kivitydd856ef2012-08-27 23:46:17 +03003393 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3394 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003395 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
Nadav Amit234f3ce2014-09-18 22:39:38 +03003396 rc = jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003397
Nadav Amit234f3ce2014-09-18 22:39:38 +03003398 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003399}
3400
3401static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3402{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003403 int rc = X86EMUL_CONTINUE;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003404
Nadav Amit234f3ce2014-09-18 22:39:38 +03003405 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
3406 rc = jmp_rel(ctxt, ctxt->src.val);
3407
3408 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003409}
3410
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003411static int em_in(struct x86_emulate_ctxt *ctxt)
3412{
3413 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3414 &ctxt->dst.val))
3415 return X86EMUL_IO_NEEDED;
3416
3417 return X86EMUL_CONTINUE;
3418}
3419
3420static int em_out(struct x86_emulate_ctxt *ctxt)
3421{
3422 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3423 &ctxt->src.val, 1);
3424 /* Disable writeback. */
3425 ctxt->dst.type = OP_NONE;
3426 return X86EMUL_CONTINUE;
3427}
3428
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003429static int em_cli(struct x86_emulate_ctxt *ctxt)
3430{
3431 if (emulator_bad_iopl(ctxt))
3432 return emulate_gp(ctxt, 0);
3433
3434 ctxt->eflags &= ~X86_EFLAGS_IF;
3435 return X86EMUL_CONTINUE;
3436}
3437
3438static int em_sti(struct x86_emulate_ctxt *ctxt)
3439{
3440 if (emulator_bad_iopl(ctxt))
3441 return emulate_gp(ctxt, 0);
3442
3443 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3444 ctxt->eflags |= X86_EFLAGS_IF;
3445 return X86EMUL_CONTINUE;
3446}
3447
Avi Kivity6d6eede2012-06-07 14:11:36 +03003448static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3449{
3450 u32 eax, ebx, ecx, edx;
3451
Avi Kivitydd856ef2012-08-27 23:46:17 +03003452 eax = reg_read(ctxt, VCPU_REGS_RAX);
3453 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003454 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003455 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3456 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3457 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3458 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003459 return X86EMUL_CONTINUE;
3460}
3461
Paolo Bonzini98f73632013-10-31 11:19:42 +01003462static int em_sahf(struct x86_emulate_ctxt *ctxt)
3463{
3464 u32 flags;
3465
3466 flags = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF;
3467 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3468
3469 ctxt->eflags &= ~0xffUL;
3470 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3471 return X86EMUL_CONTINUE;
3472}
3473
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003474static int em_lahf(struct x86_emulate_ctxt *ctxt)
3475{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003476 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3477 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003478 return X86EMUL_CONTINUE;
3479}
3480
Avi Kivity92998362012-06-13 12:25:06 +03003481static int em_bswap(struct x86_emulate_ctxt *ctxt)
3482{
3483 switch (ctxt->op_bytes) {
3484#ifdef CONFIG_X86_64
3485 case 8:
3486 asm("bswap %0" : "+r"(ctxt->dst.val));
3487 break;
3488#endif
3489 default:
3490 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3491 break;
3492 }
3493 return X86EMUL_CONTINUE;
3494}
3495
Nadav Amit13e457e2014-10-13 13:04:13 +03003496static int em_clflush(struct x86_emulate_ctxt *ctxt)
3497{
3498 /* emulating clflush regardless of cpuid */
3499 return X86EMUL_CONTINUE;
3500}
3501
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003502static bool valid_cr(int nr)
3503{
3504 switch (nr) {
3505 case 0:
3506 case 2 ... 4:
3507 case 8:
3508 return true;
3509 default:
3510 return false;
3511 }
3512}
3513
3514static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3515{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003516 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003517 return emulate_ud(ctxt);
3518
3519 return X86EMUL_CONTINUE;
3520}
3521
3522static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3523{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003524 u64 new_val = ctxt->src.val64;
3525 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003526 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003527
3528 static u64 cr_reserved_bits[] = {
3529 0xffffffff00000000ULL,
3530 0, 0, 0, /* CR3 checked later */
3531 CR4_RESERVED_BITS,
3532 0, 0, 0,
3533 CR8_RESERVED_BITS,
3534 };
3535
3536 if (!valid_cr(cr))
3537 return emulate_ud(ctxt);
3538
3539 if (new_val & cr_reserved_bits[cr])
3540 return emulate_gp(ctxt, 0);
3541
3542 switch (cr) {
3543 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003544 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003545 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3546 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3547 return emulate_gp(ctxt, 0);
3548
Avi Kivity717746e2011-04-20 13:37:53 +03003549 cr4 = ctxt->ops->get_cr(ctxt, 4);
3550 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003551
3552 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3553 !(cr4 & X86_CR4_PAE))
3554 return emulate_gp(ctxt, 0);
3555
3556 break;
3557 }
3558 case 3: {
3559 u64 rsvd = 0;
3560
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003561 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3562 if (efer & EFER_LMA)
Nadav Amit9d88fca2014-11-02 11:54:52 +02003563 rsvd = CR3_L_MODE_RESERVED_BITS & ~CR3_PCID_INVD;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003564
3565 if (new_val & rsvd)
3566 return emulate_gp(ctxt, 0);
3567
3568 break;
3569 }
3570 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003571 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003572
3573 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3574 return emulate_gp(ctxt, 0);
3575
3576 break;
3577 }
3578 }
3579
3580 return X86EMUL_CONTINUE;
3581}
3582
Joerg Roedel3b88e412011-04-04 12:39:29 +02003583static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3584{
3585 unsigned long dr7;
3586
Avi Kivity717746e2011-04-20 13:37:53 +03003587 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003588
3589 /* Check if DR7.Global_Enable is set */
3590 return dr7 & (1 << 13);
3591}
3592
3593static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3594{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003595 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003596 u64 cr4;
3597
3598 if (dr > 7)
3599 return emulate_ud(ctxt);
3600
Avi Kivity717746e2011-04-20 13:37:53 +03003601 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003602 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3603 return emulate_ud(ctxt);
3604
Nadav Amit6d2a0522014-11-02 11:54:43 +02003605 if (check_dr7_gd(ctxt)) {
3606 ulong dr6;
3607
3608 ctxt->ops->get_dr(ctxt, 6, &dr6);
3609 dr6 &= ~15;
3610 dr6 |= DR6_BD | DR6_RTM;
3611 ctxt->ops->set_dr(ctxt, 6, dr6);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003612 return emulate_db(ctxt);
Nadav Amit6d2a0522014-11-02 11:54:43 +02003613 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02003614
3615 return X86EMUL_CONTINUE;
3616}
3617
3618static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3619{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003620 u64 new_val = ctxt->src.val64;
3621 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003622
3623 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3624 return emulate_gp(ctxt, 0);
3625
3626 return check_dr_read(ctxt);
3627}
3628
Joerg Roedel01de8b02011-04-04 12:39:31 +02003629static int check_svme(struct x86_emulate_ctxt *ctxt)
3630{
3631 u64 efer;
3632
Avi Kivity717746e2011-04-20 13:37:53 +03003633 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003634
3635 if (!(efer & EFER_SVME))
3636 return emulate_ud(ctxt);
3637
3638 return X86EMUL_CONTINUE;
3639}
3640
3641static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3642{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003643 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003644
3645 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003646 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003647 return emulate_gp(ctxt, 0);
3648
3649 return check_svme(ctxt);
3650}
3651
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003652static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3653{
Avi Kivity717746e2011-04-20 13:37:53 +03003654 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003655
Avi Kivity717746e2011-04-20 13:37:53 +03003656 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003657 return emulate_ud(ctxt);
3658
3659 return X86EMUL_CONTINUE;
3660}
3661
Joerg Roedel80612522011-04-04 12:39:33 +02003662static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3663{
Avi Kivity717746e2011-04-20 13:37:53 +03003664 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003665 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003666
Avi Kivity717746e2011-04-20 13:37:53 +03003667 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03003668 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02003669 return emulate_gp(ctxt, 0);
3670
3671 return X86EMUL_CONTINUE;
3672}
3673
Joerg Roedelf6511932011-04-04 12:39:35 +02003674static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3675{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003676 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3677 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003678 return emulate_gp(ctxt, 0);
3679
3680 return X86EMUL_CONTINUE;
3681}
3682
3683static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3684{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003685 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3686 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003687 return emulate_gp(ctxt, 0);
3688
3689 return X86EMUL_CONTINUE;
3690}
3691
Avi Kivity73fba5f2010-07-29 15:11:53 +03003692#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003693#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
3694#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
3695 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003696#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003697#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003698#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3699#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003700#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003701#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003702#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003703#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003704 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003705#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003706 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
3707 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003708#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003709
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003710#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003711#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003712#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003713#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003714#define I2bvIP(_f, _e, _i, _p) \
3715 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003716
Avi Kivityfb864fb2013-01-04 16:18:54 +02003717#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3718 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3719 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003720
Nadav Amit0f54a322014-08-29 11:26:55 +03003721static const struct opcode group7_rm0[] = {
3722 N,
3723 I(SrcNone | Priv | EmulateOnUD, em_vmcall),
3724 N, N, N, N, N, N,
3725};
3726
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003727static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003728 DI(SrcNone | Priv, monitor),
3729 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003730 N, N, N, N, N, N,
3731};
3732
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003733static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003734 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003735 II(SrcNone | Prot | EmulateOnUD, em_vmmcall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003736 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3737 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3738 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3739 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3740 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3741 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003742};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003743
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003744static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003745 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003746 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003747 N, N, N, N, N, N,
3748};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003749
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003750static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003751 F(Lock, em_add),
3752 F(Lock | PageTable, em_or),
3753 F(Lock, em_adc),
3754 F(Lock, em_sbb),
3755 F(Lock | PageTable, em_and),
3756 F(Lock, em_sub),
3757 F(Lock, em_xor),
3758 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003759};
3760
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003761static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003762 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003763};
3764
Avi Kivity007a3b52013-01-19 19:51:51 +02003765static const struct opcode group2[] = {
3766 F(DstMem | ModRM, em_rol),
3767 F(DstMem | ModRM, em_ror),
3768 F(DstMem | ModRM, em_rcl),
3769 F(DstMem | ModRM, em_rcr),
3770 F(DstMem | ModRM, em_shl),
3771 F(DstMem | ModRM, em_shr),
3772 F(DstMem | ModRM, em_shl),
3773 F(DstMem | ModRM, em_sar),
3774};
3775
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003776static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003777 F(DstMem | SrcImm | NoWrite, em_test),
3778 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003779 F(DstMem | SrcNone | Lock, em_not),
3780 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02003781 F(DstXacc | Src2Mem, em_mul_ex),
3782 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02003783 F(DstXacc | Src2Mem, em_div_ex),
3784 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003785};
3786
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003787static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003788 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3789 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003790 N, N, N, N, N, N,
3791};
3792
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003793static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003794 F(DstMem | SrcNone | Lock, em_inc),
3795 F(DstMem | SrcNone | Lock, em_dec),
Nadav Amit58b70752014-10-24 11:35:09 +03003796 I(SrcMem | NearBranch, em_call_near_abs),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003797 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
Nadav Amit58b70752014-10-24 11:35:09 +03003798 I(SrcMem | NearBranch, em_jmp_abs),
Nadav Amitf7784042014-09-18 22:39:41 +03003799 I(SrcMemFAddr | ImplicitOps, em_jmp_far),
3800 I(SrcMem | Stack, em_push), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003801};
3802
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003803static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003804 DI(Prot, sldt),
3805 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003806 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003807 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003808 N, N, N, N,
3809};
3810
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003811static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03003812 II(Mov | DstMem, em_sgdt, sgdt),
3813 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003814 II(SrcMem | Priv, em_lgdt, lgdt),
3815 II(SrcMem | Priv, em_lidt, lidt),
3816 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3817 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3818 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003819}, {
Nadav Amit0f54a322014-08-29 11:26:55 +03003820 EXT(0, group7_rm0),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003821 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003822 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003823 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3824 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3825 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003826} };
3827
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003828static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003829 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003830 F(DstMem | SrcImmByte | NoWrite, em_bt),
3831 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3832 F(DstMem | SrcImmByte | Lock, em_btr),
3833 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003834};
3835
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003836static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003837 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003838}, {
3839 N, N, N, N, N, N, N, N,
3840} };
3841
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003842static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003843 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003844 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003845};
3846
Nadav Amit13e457e2014-10-13 13:04:13 +03003847static const struct gprefix pfx_0f_ae_7 = {
Nadav Amit3f6f1482014-10-13 13:04:14 +03003848 I(SrcMem | ByteOp, em_clflush), N, N, N,
Nadav Amit13e457e2014-10-13 13:04:13 +03003849};
3850
3851static const struct group_dual group15 = { {
3852 N, N, N, N, N, N, N, GP(0, &pfx_0f_ae_7),
3853}, {
3854 N, N, N, N, N, N, N, N,
3855} };
3856
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003857static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003858 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003859};
3860
Paolo Bonzinid5b77062014-07-14 12:54:48 +02003861static const struct gprefix pfx_0f_2b = {
3862 I(0, em_mov), I(0, em_mov), N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003863};
3864
Igor Mammedov27ce8252014-03-15 21:01:59 +01003865static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01003866 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003867};
3868
Alex Williamson0a370272014-07-11 11:56:31 -06003869static const struct gprefix pfx_0f_e7 = {
3870 N, I(Sse, em_mov), N, N,
3871};
3872
Gleb Natapov045a2822012-12-20 16:57:43 +02003873static const struct escape escape_d9 = { {
3874 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3875}, {
3876 /* 0xC0 - 0xC7 */
3877 N, N, N, N, N, N, N, N,
3878 /* 0xC8 - 0xCF */
3879 N, N, N, N, N, N, N, N,
3880 /* 0xD0 - 0xC7 */
3881 N, N, N, N, N, N, N, N,
3882 /* 0xD8 - 0xDF */
3883 N, N, N, N, N, N, N, N,
3884 /* 0xE0 - 0xE7 */
3885 N, N, N, N, N, N, N, N,
3886 /* 0xE8 - 0xEF */
3887 N, N, N, N, N, N, N, N,
3888 /* 0xF0 - 0xF7 */
3889 N, N, N, N, N, N, N, N,
3890 /* 0xF8 - 0xFF */
3891 N, N, N, N, N, N, N, N,
3892} };
3893
3894static const struct escape escape_db = { {
3895 N, N, N, N, N, N, N, N,
3896}, {
3897 /* 0xC0 - 0xC7 */
3898 N, N, N, N, N, N, N, N,
3899 /* 0xC8 - 0xCF */
3900 N, N, N, N, N, N, N, N,
3901 /* 0xD0 - 0xC7 */
3902 N, N, N, N, N, N, N, N,
3903 /* 0xD8 - 0xDF */
3904 N, N, N, N, N, N, N, N,
3905 /* 0xE0 - 0xE7 */
3906 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3907 /* 0xE8 - 0xEF */
3908 N, N, N, N, N, N, N, N,
3909 /* 0xF0 - 0xF7 */
3910 N, N, N, N, N, N, N, N,
3911 /* 0xF8 - 0xFF */
3912 N, N, N, N, N, N, N, N,
3913} };
3914
3915static const struct escape escape_dd = { {
3916 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3917}, {
3918 /* 0xC0 - 0xC7 */
3919 N, N, N, N, N, N, N, N,
3920 /* 0xC8 - 0xCF */
3921 N, N, N, N, N, N, N, N,
3922 /* 0xD0 - 0xC7 */
3923 N, N, N, N, N, N, N, N,
3924 /* 0xD8 - 0xDF */
3925 N, N, N, N, N, N, N, N,
3926 /* 0xE0 - 0xE7 */
3927 N, N, N, N, N, N, N, N,
3928 /* 0xE8 - 0xEF */
3929 N, N, N, N, N, N, N, N,
3930 /* 0xF0 - 0xF7 */
3931 N, N, N, N, N, N, N, N,
3932 /* 0xF8 - 0xFF */
3933 N, N, N, N, N, N, N, N,
3934} };
3935
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003936static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003937 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003938 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003939 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3940 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003941 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003942 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003943 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3944 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003945 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003946 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003947 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3948 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003949 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003950 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003951 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3952 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003953 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003954 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003955 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003956 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003957 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003958 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003959 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003960 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003961 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02003962 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003963 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003964 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003965 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003966 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003967 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003968 I(ImplicitOps | Stack | No64, em_pusha),
3969 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003970 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3971 N, N, N, N,
3972 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003973 I(SrcImm | Mov | Stack, em_push),
3974 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003975 I(SrcImmByte | Mov | Stack, em_push),
3976 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003977 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003978 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003979 /* 0x70 - 0x7F */
Nadav Amit58b70752014-10-24 11:35:09 +03003980 X16(D(SrcImmByte | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003981 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003982 G(ByteOp | DstMem | SrcImm, group1),
3983 G(DstMem | SrcImm, group1),
3984 G(ByteOp | DstMem | SrcImm | No64, group1),
3985 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003986 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003987 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003988 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003989 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003990 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003991 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003992 D(ModRM | SrcMem | NoAccess | DstReg),
3993 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3994 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003995 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003996 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003997 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003998 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003999 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09004000 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01004001 II(ImplicitOps | Stack, em_popf, popf),
4002 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004003 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004004 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004005 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004006 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004007 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004008 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004009 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004010 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
4011 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004012 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004013 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004014 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004015 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02004016 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004017 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004018 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Nadav Amit58b70752014-10-24 11:35:09 +03004019 I(ImplicitOps | NearBranch | SrcImmU16, em_ret_near_imm),
4020 I(ImplicitOps | NearBranch, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03004021 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
4022 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004023 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004024 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03004025 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Bruce Rogers32611072013-09-09 09:40:20 -06004026 I(ImplicitOps | Stack | SrcImmU16, em_ret_far_imm),
4027 I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02004028 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004029 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004030 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004031 G(Src2One | ByteOp, group2), G(Src2One, group2),
4032 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02004033 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02004034 I(DstAcc | SrcImmUByte | No64, em_aad),
4035 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004036 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004037 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02004038 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004039 /* 0xE0 - 0xE7 */
Nadav Amit58b70752014-10-24 11:35:09 +03004040 X3(I(SrcImmByte | NearBranch, em_loop)),
4041 I(SrcImmByte | NearBranch, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004042 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
4043 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004044 /* 0xE8 - 0xEF */
Nadav Amit58b70752014-10-24 11:35:09 +03004045 I(SrcImm | NearBranch, em_call), D(SrcImm | ImplicitOps | NearBranch),
4046 I(SrcImmFAddr | No64, em_jmp_far),
4047 D(SrcImmByte | ImplicitOps | NearBranch),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004048 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
4049 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004050 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004051 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004052 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4053 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004054 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09004055 D(ImplicitOps), D(ImplicitOps),
4056 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004057 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4058};
4059
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004060static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004061 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004062 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02004063 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004064 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004065 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004066 N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004067 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004068 N, N, N, N, N, N, N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004069 D(ImplicitOps | ModRM | SrcMem | NoAccess),
4070 N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004071 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03004072 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
4073 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
4074 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
4075 check_cr_write),
4076 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
4077 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004078 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004079 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
4080 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004081 N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b),
Avi Kivity3e114eb2012-04-09 18:40:01 +03004082 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004083 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004084 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004085 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004086 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004087 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02004088 I(ImplicitOps | EmulateOnUD, em_sysenter),
4089 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004090 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004091 N, N, N, N, N, N, N, N,
4092 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03004093 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004094 /* 0x50 - 0x5F */
4095 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4096 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004097 N, N, N, N,
4098 N, N, N, N,
4099 N, N, N, N,
4100 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004101 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004102 N, N, N, N,
4103 N, N, N, N,
4104 N, N, N, N,
4105 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004106 /* 0x80 - 0x8F */
Nadav Amit58b70752014-10-24 11:35:09 +03004107 X16(D(SrcImm | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004108 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004109 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004110 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004111 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004112 II(ImplicitOps, em_cpuid, cpuid),
4113 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004114 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4115 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004116 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004117 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004118 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004119 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004120 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4121 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Nadav Amit13e457e2014-10-13 13:04:13 +03004122 GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004123 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09004124 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004125 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004126 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004127 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4128 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004129 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004130 /* 0xB8 - 0xBF */
4131 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004132 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004133 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
4134 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004135 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004136 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02004137 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Nadav Amited9aad22014-11-02 11:55:00 +02004138 N, I(DstMem | SrcReg | ModRM | No16 | Mov, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004139 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004140 /* 0xC8 - 0xCF */
4141 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004142 /* 0xD0 - 0xDF */
4143 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4144 /* 0xE0 - 0xEF */
Alex Williamson0a370272014-07-11 11:56:31 -06004145 N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
4146 N, N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004147 /* 0xF0 - 0xFF */
4148 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4149};
4150
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004151static const struct gprefix three_byte_0f_38_f0 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01004152 I(DstReg | SrcMem | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004153};
4154
4155static const struct gprefix three_byte_0f_38_f1 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01004156 I(DstMem | SrcReg | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004157};
4158
4159/*
4160 * Insns below are selected by the prefix which indexed by the third opcode
4161 * byte.
4162 */
4163static const struct opcode opcode_map_0f_38[256] = {
4164 /* 0x00 - 0x7f */
4165 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004166 /* 0x80 - 0xef */
4167 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4168 /* 0xf0 - 0xf1 */
4169 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f0),
4170 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f1),
4171 /* 0xf2 - 0xff */
4172 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004173};
4174
Avi Kivity73fba5f2010-07-29 15:11:53 +03004175#undef D
4176#undef N
4177#undef G
4178#undef GD
4179#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004180#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004181#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004182
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004183#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004184#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004185#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004186#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004187#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004188
Avi Kivity9dac77f2011-06-01 15:34:25 +03004189static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004190{
4191 unsigned size;
4192
Avi Kivity9dac77f2011-06-01 15:34:25 +03004193 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004194 if (size == 8)
4195 size = 4;
4196 return size;
4197}
4198
4199static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4200 unsigned size, bool sign_extension)
4201{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004202 int rc = X86EMUL_CONTINUE;
4203
4204 op->type = OP_IMM;
4205 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004206 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004207 /* NB. Immediates are sign-extended as necessary. */
4208 switch (op->bytes) {
4209 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004210 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004211 break;
4212 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004213 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004214 break;
4215 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004216 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004217 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004218 case 8:
4219 op->val = insn_fetch(s64, ctxt);
4220 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004221 }
4222 if (!sign_extension) {
4223 switch (op->bytes) {
4224 case 1:
4225 op->val &= 0xff;
4226 break;
4227 case 2:
4228 op->val &= 0xffff;
4229 break;
4230 case 4:
4231 op->val &= 0xffffffff;
4232 break;
4233 }
4234 }
4235done:
4236 return rc;
4237}
4238
Avi Kivitya99455492011-09-13 10:45:41 +03004239static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4240 unsigned d)
4241{
4242 int rc = X86EMUL_CONTINUE;
4243
4244 switch (d) {
4245 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004246 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03004247 break;
4248 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004249 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03004250 break;
4251 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004252 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004253 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03004254 *op = ctxt->memop;
4255 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004256 if (ctxt->d & BitOp)
Avi Kivitya99455492011-09-13 10:45:41 +03004257 fetch_bit_operand(ctxt);
4258 op->orig_val = op->val;
4259 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004260 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004261 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004262 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03004263 case OpAcc:
4264 op->type = OP_REG;
4265 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004266 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03004267 fetch_register_operand(op);
4268 op->orig_val = op->val;
4269 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004270 case OpAccLo:
4271 op->type = OP_REG;
4272 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4273 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4274 fetch_register_operand(op);
4275 op->orig_val = op->val;
4276 break;
4277 case OpAccHi:
4278 if (ctxt->d & ByteOp) {
4279 op->type = OP_NONE;
4280 break;
4281 }
4282 op->type = OP_REG;
4283 op->bytes = ctxt->op_bytes;
4284 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4285 fetch_register_operand(op);
4286 op->orig_val = op->val;
4287 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004288 case OpDI:
4289 op->type = OP_MEM;
4290 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4291 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004292 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya99455492011-09-13 10:45:41 +03004293 op->addr.mem.seg = VCPU_SREG_ES;
4294 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004295 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03004296 break;
4297 case OpDX:
4298 op->type = OP_REG;
4299 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004300 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03004301 fetch_register_operand(op);
4302 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004303 case OpCL:
4304 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004305 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004306 break;
4307 case OpImmByte:
4308 rc = decode_imm(ctxt, op, 1, true);
4309 break;
4310 case OpOne:
4311 op->bytes = 1;
4312 op->val = 1;
4313 break;
4314 case OpImm:
4315 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4316 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004317 case OpImm64:
4318 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4319 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004320 case OpMem8:
4321 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004322 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004323 ctxt->memop.addr.reg = decode_register(ctxt,
4324 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004325 fetch_register_operand(&ctxt->memop);
4326 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004327 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004328 case OpMem16:
4329 ctxt->memop.bytes = 2;
4330 goto mem_common;
4331 case OpMem32:
4332 ctxt->memop.bytes = 4;
4333 goto mem_common;
4334 case OpImmU16:
4335 rc = decode_imm(ctxt, op, 2, false);
4336 break;
4337 case OpImmU:
4338 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4339 break;
4340 case OpSI:
4341 op->type = OP_MEM;
4342 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4343 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004344 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Bandan Das573e80f2014-04-16 12:46:13 -04004345 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03004346 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004347 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004348 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004349 case OpXLat:
4350 op->type = OP_MEM;
4351 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4352 op->addr.mem.ea =
4353 register_address(ctxt,
4354 reg_read(ctxt, VCPU_REGS_RBX) +
4355 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04004356 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004357 op->val = 0;
4358 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004359 case OpImmFAddr:
4360 op->type = OP_IMM;
4361 op->addr.mem.ea = ctxt->_eip;
4362 op->bytes = ctxt->op_bytes + 2;
4363 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4364 break;
4365 case OpMemFAddr:
4366 ctxt->memop.bytes = ctxt->op_bytes + 2;
4367 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004368 case OpES:
4369 op->val = VCPU_SREG_ES;
4370 break;
4371 case OpCS:
4372 op->val = VCPU_SREG_CS;
4373 break;
4374 case OpSS:
4375 op->val = VCPU_SREG_SS;
4376 break;
4377 case OpDS:
4378 op->val = VCPU_SREG_DS;
4379 break;
4380 case OpFS:
4381 op->val = VCPU_SREG_FS;
4382 break;
4383 case OpGS:
4384 op->val = VCPU_SREG_GS;
4385 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004386 case OpImplicit:
4387 /* Special instructions do their own operand decoding. */
4388 default:
4389 op->type = OP_NONE; /* Disable writeback. */
4390 break;
4391 }
4392
4393done:
4394 return rc;
4395}
4396
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004397int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004398{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004399 int rc = X86EMUL_CONTINUE;
4400 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004401 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004402 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04004403 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03004404 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004405
Avi Kivityf09ed832011-09-13 10:45:40 +03004406 ctxt->memop.type = OP_NONE;
4407 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004408 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02004409 ctxt->fetch.ptr = ctxt->fetch.data;
4410 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004411 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01004412 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004413 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004414 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02004415 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004416 if (rc != X86EMUL_CONTINUE)
4417 return rc;
4418 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004419
4420 switch (mode) {
4421 case X86EMUL_MODE_REAL:
4422 case X86EMUL_MODE_VM86:
4423 case X86EMUL_MODE_PROT16:
4424 def_op_bytes = def_ad_bytes = 2;
4425 break;
4426 case X86EMUL_MODE_PROT32:
4427 def_op_bytes = def_ad_bytes = 4;
4428 break;
4429#ifdef CONFIG_X86_64
4430 case X86EMUL_MODE_PROT64:
4431 def_op_bytes = 4;
4432 def_ad_bytes = 8;
4433 break;
4434#endif
4435 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004436 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004437 }
4438
Avi Kivity9dac77f2011-06-01 15:34:25 +03004439 ctxt->op_bytes = def_op_bytes;
4440 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004441
4442 /* Legacy prefixes. */
4443 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004444 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004445 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004446 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004447 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004448 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004449 break;
4450 case 0x67: /* address-size override */
4451 if (mode == X86EMUL_MODE_PROT64)
4452 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004453 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004454 else
4455 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004456 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004457 break;
4458 case 0x26: /* ES override */
4459 case 0x2e: /* CS override */
4460 case 0x36: /* SS override */
4461 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004462 has_seg_override = true;
4463 ctxt->seg_override = (ctxt->b >> 3) & 3;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004464 break;
4465 case 0x64: /* FS override */
4466 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004467 has_seg_override = true;
4468 ctxt->seg_override = ctxt->b & 7;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004469 break;
4470 case 0x40 ... 0x4f: /* REX */
4471 if (mode != X86EMUL_MODE_PROT64)
4472 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004473 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004474 continue;
4475 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004476 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004477 break;
4478 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004479 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004480 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004481 break;
4482 default:
4483 goto done_prefixes;
4484 }
4485
4486 /* Any legacy prefix after a REX prefix nullifies its effect. */
4487
Avi Kivity9dac77f2011-06-01 15:34:25 +03004488 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004489 }
4490
4491done_prefixes:
4492
4493 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004494 if (ctxt->rex_prefix & 8)
4495 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004496
4497 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004498 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004499 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004500 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004501 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004502 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004503 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004504
4505 /* 0F_38 opcode map */
4506 if (ctxt->b == 0x38) {
4507 ctxt->opcode_len = 3;
4508 ctxt->b = insn_fetch(u8, ctxt);
4509 opcode = opcode_map_0f_38[ctxt->b];
4510 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004511 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004512 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004513
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004514 if (ctxt->d & ModRM)
4515 ctxt->modrm = insn_fetch(u8, ctxt);
4516
Nadav Amit7fe864d2014-06-02 18:34:03 +03004517 /* vex-prefix instructions are not implemented */
4518 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
Nadav Amitd14cb5d2014-11-02 11:54:58 +02004519 (mode == X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) == 0xc0)) {
Nadav Amit7fe864d2014-06-02 18:34:03 +03004520 ctxt->d = NotImpl;
4521 }
4522
Avi Kivity9dac77f2011-06-01 15:34:25 +03004523 while (ctxt->d & GroupMask) {
4524 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004525 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004526 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004527 opcode = opcode.u.group[goffset];
4528 break;
4529 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004530 goffset = (ctxt->modrm >> 3) & 7;
4531 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004532 opcode = opcode.u.gdual->mod3[goffset];
4533 else
4534 opcode = opcode.u.gdual->mod012[goffset];
4535 break;
4536 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004537 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004538 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004539 break;
4540 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004541 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004542 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004543 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004544 switch (simd_prefix) {
4545 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4546 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4547 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4548 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4549 }
4550 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004551 case Escape:
4552 if (ctxt->modrm > 0xbf)
4553 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4554 else
4555 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4556 break;
Avi Kivity46561642011-04-24 14:09:59 +03004557 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004558 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004559 }
Avi Kivity46561642011-04-24 14:09:59 +03004560
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004561 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004562 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004563 }
4564
Paolo Bonzinie24186e2014-03-27 12:00:57 +01004565 /* Unrecognised? */
4566 if (ctxt->d == 0)
4567 return EMULATION_FAILED;
4568
Avi Kivity9dac77f2011-06-01 15:34:25 +03004569 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004570
Nadav Amit3a6095a2014-08-13 16:50:13 +03004571 if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD)))
4572 return EMULATION_FAILED;
4573
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004574 if (unlikely(ctxt->d &
Nadav Amited9aad22014-11-02 11:55:00 +02004575 (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch|
4576 No16))) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004577 /*
4578 * These are copied unconditionally here, and checked unconditionally
4579 * in x86_emulate_insn.
4580 */
4581 ctxt->check_perm = opcode.check_perm;
4582 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004583
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004584 if (ctxt->d & NotImpl)
4585 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004586
Nadav Amit58b70752014-10-24 11:35:09 +03004587 if (mode == X86EMUL_MODE_PROT64) {
4588 if (ctxt->op_bytes == 4 && (ctxt->d & Stack))
4589 ctxt->op_bytes = 8;
4590 else if (ctxt->d & NearBranch)
4591 ctxt->op_bytes = 8;
4592 }
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004593
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004594 if (ctxt->d & Op3264) {
4595 if (mode == X86EMUL_MODE_PROT64)
4596 ctxt->op_bytes = 8;
4597 else
4598 ctxt->op_bytes = 4;
4599 }
4600
Nadav Amited9aad22014-11-02 11:55:00 +02004601 if ((ctxt->d & No16) && ctxt->op_bytes == 2)
4602 ctxt->op_bytes = 4;
4603
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004604 if (ctxt->d & Sse)
4605 ctxt->op_bytes = 16;
4606 else if (ctxt->d & Mmx)
4607 ctxt->op_bytes = 8;
4608 }
Avi Kivity1253791d2011-03-29 11:41:27 +02004609
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004610 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004611 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004612 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04004613 if (!has_seg_override) {
4614 has_seg_override = true;
4615 ctxt->seg_override = ctxt->modrm_seg;
4616 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004617 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004618 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004619 if (rc != X86EMUL_CONTINUE)
4620 goto done;
4621
Bandan Das573e80f2014-04-16 12:46:13 -04004622 if (!has_seg_override)
4623 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004624
Bandan Das573e80f2014-04-16 12:46:13 -04004625 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004626
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004627 /*
4628 * Decode and fetch the source operand: register, memory
4629 * or immediate.
4630 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004631 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004632 if (rc != X86EMUL_CONTINUE)
4633 goto done;
4634
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004635 /*
4636 * Decode and fetch the second source operand: register, memory
4637 * or immediate.
4638 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004639 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004640 if (rc != X86EMUL_CONTINUE)
4641 goto done;
4642
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004643 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03004644 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004645
Bandan Das41061cd2014-04-16 12:46:14 -04004646 if (ctxt->rip_relative)
Avi Kivityf09ed832011-09-13 10:45:40 +03004647 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004648
Paolo Bonzinia430c912014-10-23 14:54:14 +02004649done:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004650 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004651}
4652
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004653bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4654{
4655 return ctxt->d & PageTable;
4656}
4657
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004658static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4659{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004660 /* The second termination condition only applies for REPE
4661 * and REPNE. Test if the repeat string operation prefix is
4662 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4663 * corresponding termination condition according to:
4664 * - if REPE/REPZ and ZF = 0 then done
4665 * - if REPNE/REPNZ and ZF = 1 then done
4666 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004667 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4668 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4669 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004670 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004671 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004672 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4673 return true;
4674
4675 return false;
4676}
4677
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004678static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4679{
4680 bool fault = false;
4681
4682 ctxt->ops->get_fpu(ctxt);
4683 asm volatile("1: fwait \n\t"
4684 "2: \n\t"
4685 ".pushsection .fixup,\"ax\" \n\t"
4686 "3: \n\t"
4687 "movb $1, %[fault] \n\t"
4688 "jmp 2b \n\t"
4689 ".popsection \n\t"
4690 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004691 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004692 ctxt->ops->put_fpu(ctxt);
4693
4694 if (unlikely(fault))
4695 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4696
4697 return X86EMUL_CONTINUE;
4698}
4699
4700static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4701 struct operand *op)
4702{
4703 if (op->type == OP_MM)
4704 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4705}
4706
Avi Kivitye28bbd42013-01-04 16:18:48 +02004707static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4708{
4709 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivityb9fa4092013-02-09 11:31:48 +02004710 if (!(ctxt->d & ByteOp))
4711 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Avi Kivitye28bbd42013-01-04 16:18:48 +02004712 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004713 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
4714 [fastop]"+S"(fop)
4715 : "c"(ctxt->src2.val));
Avi Kivitye28bbd42013-01-04 16:18:48 +02004716 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004717 if (!fop) /* exception is returned in fop variable */
4718 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02004719 return X86EMUL_CONTINUE;
4720}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004721
Bandan Das14985072014-04-16 12:46:09 -04004722void init_decode_cache(struct x86_emulate_ctxt *ctxt)
4723{
Bandan Das573e80f2014-04-16 12:46:13 -04004724 memset(&ctxt->rip_relative, 0,
4725 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04004726
Bandan Das14985072014-04-16 12:46:09 -04004727 ctxt->io_read.pos = 0;
4728 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04004729 ctxt->mem_read.end = 0;
4730}
4731
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004732int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004733{
Mathias Krause0225fb52012-08-30 01:30:16 +02004734 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004735 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004736 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004737
Avi Kivity9dac77f2011-06-01 15:34:25 +03004738 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004739
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004740 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004741 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004742 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004743 goto done;
4744 }
4745
Avi Kivity9dac77f2011-06-01 15:34:25 +03004746 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004747 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004748 goto done;
4749 }
4750
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004751 if (unlikely(ctxt->d &
4752 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
4753 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4754 (ctxt->d & Undefined)) {
4755 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004756 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004757 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004758
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004759 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4760 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
4761 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004762 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004763 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004764
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004765 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
4766 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004767 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004768 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004769
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004770 if (ctxt->d & Mmx) {
4771 rc = flush_pending_x87_faults(ctxt);
4772 if (rc != X86EMUL_CONTINUE)
4773 goto done;
4774 /*
4775 * Now that we know the fpu is exception safe, we can fetch
4776 * operands from it.
4777 */
4778 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4779 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4780 if (!(ctxt->d & Mov))
4781 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4782 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004783
Bandan Das685bbf42014-04-16 12:46:10 -04004784 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004785 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4786 X86_ICPT_PRE_EXCEPT);
4787 if (rc != X86EMUL_CONTINUE)
4788 goto done;
4789 }
4790
4791 /* Privileged instruction can be executed only in CPL=0 */
4792 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Nadav Amit68efa762014-06-18 17:19:35 +03004793 if (ctxt->d & PrivUD)
4794 rc = emulate_ud(ctxt);
4795 else
4796 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004797 goto done;
4798 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004799
4800 /* Instruction can only be executed in protected mode */
4801 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
4802 rc = emulate_ud(ctxt);
4803 goto done;
4804 }
4805
4806 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04004807 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004808 rc = ctxt->check_perm(ctxt);
4809 if (rc != X86EMUL_CONTINUE)
4810 goto done;
4811 }
4812
Bandan Das685bbf42014-04-16 12:46:10 -04004813 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004814 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4815 X86_ICPT_POST_EXCEPT);
4816 if (rc != X86EMUL_CONTINUE)
4817 goto done;
4818 }
4819
4820 if (ctxt->rep_prefix && (ctxt->d & String)) {
4821 /* All REP prefixes have the same first termination condition */
4822 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
4823 ctxt->eip = ctxt->_eip;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004824 ctxt->eflags &= ~EFLG_RF;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004825 goto done;
4826 }
4827 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004828 }
4829
Avi Kivity9dac77f2011-06-01 15:34:25 +03004830 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4831 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4832 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004833 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004834 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004835 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004836 }
4837
Avi Kivity9dac77f2011-06-01 15:34:25 +03004838 if (ctxt->src2.type == OP_MEM) {
4839 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4840 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004841 if (rc != X86EMUL_CONTINUE)
4842 goto done;
4843 }
4844
Avi Kivity9dac77f2011-06-01 15:34:25 +03004845 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004846 goto special_insn;
4847
4848
Avi Kivity9dac77f2011-06-01 15:34:25 +03004849 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004850 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004851 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4852 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004853 if (rc != X86EMUL_CONTINUE)
4854 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004855 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004856 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004857
Avi Kivity018a98d2007-11-27 19:30:56 +02004858special_insn:
4859
Bandan Das685bbf42014-04-16 12:46:10 -04004860 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004861 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004862 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004863 if (rc != X86EMUL_CONTINUE)
4864 goto done;
4865 }
4866
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004867 if (ctxt->rep_prefix && (ctxt->d & String))
4868 ctxt->eflags |= EFLG_RF;
4869 else
4870 ctxt->eflags &= ~EFLG_RF;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004871
Avi Kivity9dac77f2011-06-01 15:34:25 +03004872 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004873 if (ctxt->d & Fastop) {
4874 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4875 rc = fastop(ctxt, fop);
4876 if (rc != X86EMUL_CONTINUE)
4877 goto done;
4878 goto writeback;
4879 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004880 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004881 if (rc != X86EMUL_CONTINUE)
4882 goto done;
4883 goto writeback;
4884 }
4885
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004886 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004887 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004888 else if (ctxt->opcode_len == 3)
4889 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004890
Avi Kivity9dac77f2011-06-01 15:34:25 +03004891 switch (ctxt->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004892 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004893 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004894 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004895 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004896 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004897 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004898 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03004899 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004900 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004901 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004902 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004903 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004904 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004905 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03004906 ctxt->dst.type = OP_NONE;
4907 else
4908 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004909 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004910 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004911 switch (ctxt->op_bytes) {
4912 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4913 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4914 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004915 }
4916 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004917 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004918 rc = emulate_int(ctxt, 3);
4919 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004920 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004921 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004922 break;
4923 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004924 if (ctxt->eflags & EFLG_OF)
4925 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004926 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004927 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004928 case 0xeb: /* jmp rel short */
Nadav Amit234f3ce2014-09-18 22:39:38 +03004929 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004930 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004931 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004932 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004933 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004934 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004935 case 0xf5: /* cmc */
4936 /* complement carry flag from eflags reg */
4937 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004938 break;
4939 case 0xf8: /* clc */
4940 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004941 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004942 case 0xf9: /* stc */
4943 ctxt->eflags |= EFLG_CF;
4944 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004945 case 0xfc: /* cld */
4946 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004947 break;
4948 case 0xfd: /* std */
4949 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004950 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004951 default:
4952 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004953 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004954
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004955 if (rc != X86EMUL_CONTINUE)
4956 goto done;
4957
Avi Kivity018a98d2007-11-27 19:30:56 +02004958writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02004959 if (ctxt->d & SrcWrite) {
4960 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
4961 rc = writeback(ctxt, &ctxt->src);
4962 if (rc != X86EMUL_CONTINUE)
4963 goto done;
4964 }
Nadav Amitee212292014-06-15 16:12:58 +03004965 if (!(ctxt->d & NoWrite)) {
4966 rc = writeback(ctxt, &ctxt->dst);
4967 if (rc != X86EMUL_CONTINUE)
4968 goto done;
4969 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004970
Gleb Natapov5cd21912010-03-18 15:20:26 +02004971 /*
4972 * restore dst type in case the decoding will be reused
4973 * (happens for string instruction )
4974 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004975 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004976
Avi Kivity9dac77f2011-06-01 15:34:25 +03004977 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004978 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004979
Avi Kivity9dac77f2011-06-01 15:34:25 +03004980 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004981 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004982
Avi Kivity9dac77f2011-06-01 15:34:25 +03004983 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004984 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004985 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004986 if ((ctxt->d & SrcMask) == SrcSI)
4987 count = ctxt->src.count;
4988 else
4989 count = ctxt->dst.count;
4990 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4991 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004992
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004993 if (!string_insn_completed(ctxt)) {
4994 /*
4995 * Re-enter guest when pio read ahead buffer is empty
4996 * or, if it is not used, after each 1024 iteration.
4997 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004998 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004999 (r->end == 0 || r->end != r->pos)) {
5000 /*
5001 * Reset read cache. Usually happens before
5002 * decode, but since instruction is restarted
5003 * we have to do it here.
5004 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005005 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005006 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005007 return EMULATION_RESTART;
5008 }
5009 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03005010 }
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03005011 ctxt->eflags &= ~EFLG_RF;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005012 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005013
Avi Kivity9dac77f2011-06-01 15:34:25 +03005014 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02005015
5016done:
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005017 if (rc == X86EMUL_PROPAGATE_FAULT) {
5018 WARN_ON(ctxt->exception.vector > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +02005019 ctxt->have_exception = true;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005020 }
Joerg Roedel775fde82011-04-04 12:39:24 +02005021 if (rc == X86EMUL_INTERCEPTED)
5022 return EMULATION_INTERCEPTED;
5023
Avi Kivitydd856ef2012-08-27 23:46:17 +03005024 if (rc == X86EMUL_CONTINUE)
5025 writeback_registers(ctxt);
5026
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005027 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005028
5029twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005030 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02005031 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02005032 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005033 break;
5034 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02005035 case 0x0d: /* GrpP (prefetch) */
5036 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02005037 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02005038 break;
5039 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005040 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02005041 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005042 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005043 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005044 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005045 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03005046 if (test_cc(ctxt->b, ctxt->eflags))
5047 ctxt->dst.val = ctxt->src.val;
5048 else if (ctxt->mode != X86EMUL_MODE_PROT64 ||
5049 ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005050 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08005051 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03005052 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03005053 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005054 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005055 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08005056 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005057 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08005058 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005059 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005060 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005061 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03005062 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005063 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005064 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005065 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005066 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03005067 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005068 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005069 default:
5070 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005071 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005072
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005073threebyte_insn:
5074
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005075 if (rc != X86EMUL_CONTINUE)
5076 goto done;
5077
Avi Kivity6aa8b732006-12-10 02:21:36 -08005078 goto writeback;
5079
5080cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02005081 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005082}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005083
5084void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
5085{
5086 invalidate_registers(ctxt);
5087}
5088
5089void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
5090{
5091 writeback_registers(ctxt);
5092}