blob: b304728aabe3b20143bfa91af74726f2d732e911 [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)
Nadav Amit16bebef2014-12-25 02:52:18 +020089#define DstMem16 (OpMem16 << DstShift)
Avi Kivitya99455492011-09-13 10:45:41 +030090#define DstImmUByte (OpImmUByte << DstShift)
91#define DstDX (OpDX << DstShift)
Avi Kivity820207c2013-02-09 11:31:45 +020092#define DstAccLo (OpAccLo << DstShift)
Avi Kivitya99455492011-09-13 10:45:41 +030093#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080094/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030095#define SrcShift 6
96#define SrcNone (OpNone << SrcShift)
97#define SrcReg (OpReg << SrcShift)
98#define SrcMem (OpMem << SrcShift)
99#define SrcMem16 (OpMem16 << SrcShift)
100#define SrcMem32 (OpMem32 << SrcShift)
101#define SrcImm (OpImm << SrcShift)
102#define SrcImmByte (OpImmByte << SrcShift)
103#define SrcOne (OpOne << SrcShift)
104#define SrcImmUByte (OpImmUByte << SrcShift)
105#define SrcImmU (OpImmU << SrcShift)
106#define SrcSI (OpSI << SrcShift)
Paolo Bonzini7fa57952013-05-09 11:32:50 +0200107#define SrcXLat (OpXLat << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300108#define SrcImmFAddr (OpImmFAddr << SrcShift)
109#define SrcMemFAddr (OpMemFAddr << SrcShift)
110#define SrcAcc (OpAcc << SrcShift)
111#define SrcImmU16 (OpImmU16 << SrcShift)
Nadav Amit5e2c6882012-12-06 21:55:10 -0200112#define SrcImm64 (OpImm64 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300113#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200114#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity820207c2013-02-09 11:31:45 +0200115#define SrcAccHi (OpAccHi << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300116#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300117#define BitOp (1<<11)
118#define MemAbs (1<<12) /* Memory operand is absolute displacement */
119#define String (1<<13) /* String instruction (rep capable) */
120#define Stack (1<<14) /* Stack instruction (push/pop) */
121#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
122#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
123#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
124#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
125#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
Gleb Natapov045a2822012-12-20 16:57:43 +0200126#define Escape (5<<15) /* Escape to coprocessor instruction */
Nadav Amit39f062f2014-11-26 15:47:18 +0200127#define InstrDual (6<<15) /* Alternate instruction decoding of mod == 3 */
Nadav Amit2276b512015-01-26 09:32:24 +0200128#define ModeDual (7<<15) /* Different instruction for 32/64 bit */
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300129#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300130/* Generic ModRM decode. */
131#define ModRM (1<<19)
132/* Destination is only written; never read. */
133#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300134/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200135#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Borislav Petkovb51e9742013-09-22 16:44:52 +0200136#define EmulateOnUD (1<<22) /* Emulate if unsupported by the host */
Avi Kivity5a506b12010-08-01 15:10:29 +0300137#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300138#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300139#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200140#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200141#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300142#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800143#define PageTable (1 << 29) /* instruction used to write page table */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300144#define NotImpl (1 << 30) /* instruction is not implemented */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100145/* Source 2 operand type */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300146#define Src2Shift (31)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300147#define Src2None (OpNone << Src2Shift)
Avi Kivityab2c5ce2013-02-09 11:31:46 +0200148#define Src2Mem (OpMem << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300149#define Src2CL (OpCL << Src2Shift)
150#define Src2ImmByte (OpImmByte << Src2Shift)
151#define Src2One (OpOne << Src2Shift)
152#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300153#define Src2ES (OpES << Src2Shift)
154#define Src2CS (OpCS << Src2Shift)
155#define Src2SS (OpSS << Src2Shift)
156#define Src2DS (OpDS << Src2Shift)
157#define Src2FS (OpFS << Src2Shift)
158#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300159#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300160#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Avi Kivity1c11b372012-04-09 18:39:59 +0300161#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
162#define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */
163#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
Avi Kivitye28bbd42013-01-04 16:18:48 +0200164#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
Avi Kivityb6744dc2013-01-04 16:18:50 +0200165#define NoWrite ((u64)1 << 45) /* No writeback */
Avi Kivityfb32b1e2013-02-09 11:31:44 +0200166#define SrcWrite ((u64)1 << 46) /* Write back src operand */
Nadav Amit9b88ae92014-05-25 23:05:21 +0300167#define NoMod ((u64)1 << 47) /* Mod field is ignored */
Paolo Bonzinid40a6892014-03-27 11:58:02 +0100168#define Intercept ((u64)1 << 48) /* Has valid intercept field */
169#define CheckPerm ((u64)1 << 49) /* Has valid check_perm field */
Nadav Amit68efa762014-06-18 17:19:35 +0300170#define PrivUD ((u64)1 << 51) /* #UD instead of #GP on CPL > 0 */
Nadav Amit58b70752014-10-24 11:35:09 +0300171#define NearBranch ((u64)1 << 52) /* Near branches */
Nadav Amited9aad22014-11-02 11:55:00 +0200172#define No16 ((u64)1 << 53) /* No 16 bit operand */
Nadav Amitab708092014-12-25 02:52:21 +0200173#define IncSP ((u64)1 << 54) /* SP is incremented before ModRM calc */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800174
Avi Kivity820207c2013-02-09 11:31:45 +0200175#define DstXacc (DstAccLo | SrcAccHi | SrcWrite)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800176
Avi Kivityd0e53322010-07-29 15:11:54 +0300177#define X2(x...) x, x
178#define X3(x...) X2(x), x
179#define X4(x...) X2(x), X2(x)
180#define X5(x...) X4(x), x
181#define X6(x...) X4(x), X2(x)
182#define X7(x...) X4(x), X3(x)
183#define X8(x...) X4(x), X4(x)
184#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300185
Avi Kivitye28bbd42013-01-04 16:18:48 +0200186#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
187#define FASTOP_SIZE 8
188
189/*
190 * fastop functions have a special calling convention:
191 *
Avi Kivity017da7b2013-02-09 11:31:47 +0200192 * dst: rax (in/out)
193 * src: rdx (in/out)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200194 * src2: rcx (in)
195 * flags: rflags (in/out)
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200196 * ex: rsi (in:fastop pointer, out:zero if exception)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200197 *
198 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
199 * different operand sizes can be reached by calculation, rather than a jump
200 * table (which would be bigger than the code).
201 *
202 * fastop functions are declared as taking a never-defined fastop parameter,
203 * so they can't be called from C directly.
204 */
205
206struct fastop;
207
Avi Kivityd65b1de2010-07-29 15:11:35 +0300208struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300209 u64 flags : 56;
210 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300211 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300212 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200213 const struct opcode *group;
214 const struct group_dual *gdual;
215 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200216 const struct escape *esc;
Nadav Amit39f062f2014-11-26 15:47:18 +0200217 const struct instr_dual *idual;
Nadav Amit2276b512015-01-26 09:32:24 +0200218 const struct mode_dual *mdual;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200219 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300220 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200221 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300222};
223
224struct group_dual {
225 struct opcode mod012[8];
226 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300227};
228
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200229struct gprefix {
230 struct opcode pfx_no;
231 struct opcode pfx_66;
232 struct opcode pfx_f2;
233 struct opcode pfx_f3;
234};
235
Gleb Natapov045a2822012-12-20 16:57:43 +0200236struct escape {
237 struct opcode op[8];
238 struct opcode high[64];
239};
240
Nadav Amit39f062f2014-11-26 15:47:18 +0200241struct instr_dual {
242 struct opcode mod012;
243 struct opcode mod3;
244};
245
Nadav Amit2276b512015-01-26 09:32:24 +0200246struct mode_dual {
247 struct opcode mode32;
248 struct opcode mode64;
249};
250
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300251#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300252
Nadav Amit3dc4bc42014-12-25 02:52:19 +0200253enum x86_transfer_type {
254 X86_TRANSFER_NONE,
255 X86_TRANSFER_CALL_JMP,
256 X86_TRANSFER_RET,
257 X86_TRANSFER_TASK_SWITCH,
258};
259
Avi Kivitydd856ef2012-08-27 23:46:17 +0300260static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
261{
262 if (!(ctxt->regs_valid & (1 << nr))) {
263 ctxt->regs_valid |= 1 << nr;
264 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
265 }
266 return ctxt->_regs[nr];
267}
268
269static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
270{
271 ctxt->regs_valid |= 1 << nr;
272 ctxt->regs_dirty |= 1 << nr;
273 return &ctxt->_regs[nr];
274}
275
276static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
277{
278 reg_read(ctxt, nr);
279 return reg_write(ctxt, nr);
280}
281
282static void writeback_registers(struct x86_emulate_ctxt *ctxt)
283{
284 unsigned reg;
285
286 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
287 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
288}
289
290static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
291{
292 ctxt->regs_dirty = 0;
293 ctxt->regs_valid = 0;
294}
295
Avi Kivity6aa8b732006-12-10 02:21:36 -0800296/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800297 * These EFLAGS bits are restored from saved value during emulation, and
298 * any changes are written back to the saved value after emulation.
299 */
Nadav Amit0efb0442015-03-29 16:33:03 +0300300#define EFLAGS_MASK (X86_EFLAGS_OF|X86_EFLAGS_SF|X86_EFLAGS_ZF|X86_EFLAGS_AF|\
301 X86_EFLAGS_PF|X86_EFLAGS_CF)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800302
Avi Kivitydda96d82008-11-26 15:14:10 +0200303#ifdef CONFIG_X86_64
304#define ON64(x) x
305#else
306#define ON64(x)
307#endif
308
Avi Kivity4d758342013-01-19 19:51:55 +0200309static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *));
310
Avi Kivityb7d491e2013-01-04 16:18:49 +0200311#define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t"
312#define FOP_RET "ret \n\t"
313
314#define FOP_START(op) \
315 extern void em_##op(struct fastop *fake); \
316 asm(".pushsection .text, \"ax\" \n\t" \
317 ".global em_" #op " \n\t" \
318 FOP_ALIGN \
319 "em_" #op ": \n\t"
320
321#define FOP_END \
322 ".popsection")
323
Avi Kivity0bdea062013-01-19 19:51:50 +0200324#define FOPNOP() FOP_ALIGN FOP_RET
325
Avi Kivityb7d491e2013-01-04 16:18:49 +0200326#define FOP1E(op, dst) \
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200327 FOP_ALIGN "10: " #op " %" #dst " \n\t" FOP_RET
328
329#define FOP1EEX(op, dst) \
330 FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200331
332#define FASTOP1(op) \
333 FOP_START(op) \
334 FOP1E(op##b, al) \
335 FOP1E(op##w, ax) \
336 FOP1E(op##l, eax) \
337 ON64(FOP1E(op##q, rax)) \
338 FOP_END
339
Avi Kivityb9fa4092013-02-09 11:31:48 +0200340/* 1-operand, using src2 (for MUL/DIV r/m) */
341#define FASTOP1SRC2(op, name) \
342 FOP_START(name) \
343 FOP1E(op, cl) \
344 FOP1E(op, cx) \
345 FOP1E(op, ecx) \
346 ON64(FOP1E(op, rcx)) \
347 FOP_END
348
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200349/* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */
350#define FASTOP1SRC2EX(op, name) \
351 FOP_START(name) \
352 FOP1EEX(op, cl) \
353 FOP1EEX(op, cx) \
354 FOP1EEX(op, ecx) \
355 ON64(FOP1EEX(op, rcx)) \
356 FOP_END
357
Avi Kivityf7857f32013-01-04 16:18:53 +0200358#define FOP2E(op, dst, src) \
359 FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET
360
361#define FASTOP2(op) \
362 FOP_START(op) \
Avi Kivity017da7b2013-02-09 11:31:47 +0200363 FOP2E(op##b, al, dl) \
364 FOP2E(op##w, ax, dx) \
365 FOP2E(op##l, eax, edx) \
366 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivityf7857f32013-01-04 16:18:53 +0200367 FOP_END
368
Avi Kivity11c363b2013-01-19 19:51:54 +0200369/* 2 operand, word only */
370#define FASTOP2W(op) \
371 FOP_START(op) \
372 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200373 FOP2E(op##w, ax, dx) \
374 FOP2E(op##l, eax, edx) \
375 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivity11c363b2013-01-19 19:51:54 +0200376 FOP_END
377
Avi Kivity007a3b52013-01-19 19:51:51 +0200378/* 2 operand, src is CL */
379#define FASTOP2CL(op) \
380 FOP_START(op) \
381 FOP2E(op##b, al, cl) \
382 FOP2E(op##w, ax, cl) \
383 FOP2E(op##l, eax, cl) \
384 ON64(FOP2E(op##q, rax, cl)) \
385 FOP_END
386
Nadav Amit5aca3722014-11-02 11:54:50 +0200387/* 2 operand, src and dest are reversed */
388#define FASTOP2R(op, name) \
389 FOP_START(name) \
390 FOP2E(op##b, dl, al) \
391 FOP2E(op##w, dx, ax) \
392 FOP2E(op##l, edx, eax) \
393 ON64(FOP2E(op##q, rdx, rax)) \
394 FOP_END
395
Avi Kivity0bdea062013-01-19 19:51:50 +0200396#define FOP3E(op, dst, src, src2) \
397 FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
398
399/* 3-operand, word-only, src2=cl */
400#define FASTOP3WCL(op) \
401 FOP_START(op) \
402 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200403 FOP3E(op##w, ax, dx, cl) \
404 FOP3E(op##l, eax, edx, cl) \
405 ON64(FOP3E(op##q, rax, rdx, cl)) \
Avi Kivity0bdea062013-01-19 19:51:50 +0200406 FOP_END
407
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200408/* Special case for SETcc - 1 instruction per cc */
409#define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t"
410
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200411asm(".global kvm_fastop_exception \n"
412 "kvm_fastop_exception: xor %esi, %esi; ret");
413
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200414FOP_START(setcc)
415FOP_SETCC(seto)
416FOP_SETCC(setno)
417FOP_SETCC(setc)
418FOP_SETCC(setnc)
419FOP_SETCC(setz)
420FOP_SETCC(setnz)
421FOP_SETCC(setbe)
422FOP_SETCC(setnbe)
423FOP_SETCC(sets)
424FOP_SETCC(setns)
425FOP_SETCC(setp)
426FOP_SETCC(setnp)
427FOP_SETCC(setl)
428FOP_SETCC(setnl)
429FOP_SETCC(setle)
430FOP_SETCC(setnle)
431FOP_END;
432
Paolo Bonzini326f5782013-05-09 11:32:51 +0200433FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET
434FOP_END;
435
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200436static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
437 enum x86_intercept intercept,
438 enum x86_intercept_stage stage)
439{
440 struct x86_instruction_info info = {
441 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300442 .rep_prefix = ctxt->rep_prefix,
443 .modrm_mod = ctxt->modrm_mod,
444 .modrm_reg = ctxt->modrm_reg,
445 .modrm_rm = ctxt->modrm_rm,
446 .src_val = ctxt->src.val64,
Jan Kiszka6cbc5f52014-06-30 12:52:55 +0200447 .dst_val = ctxt->dst.val64,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300448 .src_bytes = ctxt->src.bytes,
449 .dst_bytes = ctxt->dst.bytes,
450 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200451 .next_rip = ctxt->eip,
452 };
453
Avi Kivity29535382011-04-20 13:37:53 +0300454 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200455}
456
Avi Kivityf47cfa32012-06-07 17:49:24 +0300457static void assign_masked(ulong *dest, ulong src, ulong mask)
458{
459 *dest = (*dest & ~mask) | (src & mask);
460}
461
Nadav Amit6fd8e122015-03-30 15:39:20 +0300462static void assign_register(unsigned long *reg, u64 val, int bytes)
463{
464 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
465 switch (bytes) {
466 case 1:
467 *(u8 *)reg = (u8)val;
468 break;
469 case 2:
470 *(u16 *)reg = (u16)val;
471 break;
472 case 4:
473 *reg = (u32)val;
474 break; /* 64b: zero-extend */
475 case 8:
476 *reg = val;
477 break;
478 }
479}
480
Avi Kivity9dac77f2011-06-01 15:34:25 +0300481static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800482{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300483 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800484}
485
Avi Kivityf47cfa32012-06-07 17:49:24 +0300486static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
487{
488 u16 sel;
489 struct desc_struct ss;
490
491 if (ctxt->mode == X86EMUL_MODE_PROT64)
492 return ~0UL;
493 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
494 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
495}
496
Avi Kivity612e89f2012-06-12 20:03:23 +0300497static int stack_size(struct x86_emulate_ctxt *ctxt)
498{
499 return (__fls(stack_mask(ctxt)) + 1) >> 3;
500}
501
Avi Kivity6aa8b732006-12-10 02:21:36 -0800502/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800503static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300504address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800505{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300506 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800507 return reg;
508 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300509 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800510}
511
512static inline unsigned long
Paolo Bonzini01485a22014-11-19 18:25:08 +0100513register_address(struct x86_emulate_ctxt *ctxt, int reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800514{
Paolo Bonzini01485a22014-11-19 18:25:08 +0100515 return address_mask(ctxt, reg_read(ctxt, reg));
Harvey Harrisone4706772008-02-19 07:40:38 -0800516}
517
Avi Kivity5ad105e2012-08-19 14:34:31 +0300518static void masked_increment(ulong *reg, ulong mask, int inc)
519{
520 assign_masked(reg, *reg + inc, mask);
521}
522
Harvey Harrison7a9572752008-02-19 07:40:41 -0800523static inline void
Paolo Bonzini01485a22014-11-19 18:25:08 +0100524register_address_increment(struct x86_emulate_ctxt *ctxt, int reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800525{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300526 ulong mask;
527
Avi Kivity9dac77f2011-06-01 15:34:25 +0300528 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300529 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800530 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300531 mask = ad_mask(ctxt);
Paolo Bonzini01485a22014-11-19 18:25:08 +0100532 masked_increment(reg_rmw(ctxt, reg), mask, inc);
Avi Kivity5ad105e2012-08-19 14:34:31 +0300533}
534
535static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
536{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300537 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800538}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800539
Avi Kivity56697682011-04-03 14:08:51 +0300540static u32 desc_limit_scaled(struct desc_struct *desc)
541{
542 u32 limit = get_desc_limit(desc);
543
544 return desc->g ? (limit << 12) | 0xfff : limit;
545}
546
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900547static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300548{
549 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
550 return 0;
551
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900552 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300553}
554
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200555static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
556 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300557{
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +0200558 WARN_ON(vec > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +0200559 ctxt->exception.vector = vec;
560 ctxt->exception.error_code = error;
561 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200562 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300563}
564
Joerg Roedel3b88e412011-04-04 12:39:29 +0200565static int emulate_db(struct x86_emulate_ctxt *ctxt)
566{
567 return emulate_exception(ctxt, DB_VECTOR, 0, false);
568}
569
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200570static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300571{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200572 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300573}
574
Avi Kivity618ff152011-04-03 12:32:09 +0300575static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
576{
577 return emulate_exception(ctxt, SS_VECTOR, err, true);
578}
579
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200580static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300581{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200582 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300583}
584
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200585static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300586{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200587 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300588}
589
Avi Kivity34d1f492010-08-26 11:59:01 +0300590static int emulate_de(struct x86_emulate_ctxt *ctxt)
591{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200592 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300593}
594
Avi Kivity1253791d2011-03-29 11:41:27 +0200595static int emulate_nm(struct x86_emulate_ctxt *ctxt)
596{
597 return emulate_exception(ctxt, NM_VECTOR, 0, false);
598}
599
Avi Kivity1aa36612011-04-27 13:20:30 +0300600static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
601{
602 u16 selector;
603 struct desc_struct desc;
604
605 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
606 return selector;
607}
608
609static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
610 unsigned seg)
611{
612 u16 dummy;
613 u32 base3;
614 struct desc_struct desc;
615
616 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
617 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
618}
619
Avi Kivity1c11b372012-04-09 18:39:59 +0300620/*
621 * x86 defines three classes of vector instructions: explicitly
622 * aligned, explicitly unaligned, and the rest, which change behaviour
623 * depending on whether they're AVX encoded or not.
624 *
625 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
626 * subject to the same check.
627 */
628static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
629{
630 if (likely(size < 16))
631 return false;
632
633 if (ctxt->d & Aligned)
634 return true;
635 else if (ctxt->d & Unaligned)
636 return false;
637 else if (ctxt->d & Avx)
638 return false;
639 else
640 return true;
641}
642
Paolo Bonzinid09155d2014-10-27 14:54:44 +0100643static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
644 struct segmented_address addr,
645 unsigned *max_size, unsigned size,
646 bool write, bool fetch,
Nadav Amitd50eaa12014-11-19 17:43:11 +0200647 enum x86emul_mode mode, ulong *linear)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300648{
Avi Kivity618ff152011-04-03 12:32:09 +0300649 struct desc_struct desc;
650 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300651 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300652 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300653 u16 sel;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300654
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900655 la = seg_base(ctxt, addr.seg) + addr.ea;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100656 *max_size = 0;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200657 switch (mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300658 case X86EMUL_MODE_PROT64:
Nadav Amit4be4de72014-09-18 22:39:40 +0300659 if (is_noncanonical_address(la))
Nadav Amitabc7d8a2014-11-19 17:43:12 +0200660 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100661
662 *max_size = min_t(u64, ~0u, (1ull << 48) - la);
663 if (size > *max_size)
664 goto bad;
Avi Kivity618ff152011-04-03 12:32:09 +0300665 break;
666 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300667 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
668 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300669 if (!usable)
670 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200671 /* code segment in protected mode or read-only data segment */
672 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
673 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300674 goto bad;
675 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400676 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300677 goto bad;
678 lim = desc_limit_scaled(&desc);
Paolo Bonzini997b0412014-11-19 18:33:38 +0100679 if (!(desc.type & 8) && (desc.type & 4)) {
Guo Chaofc058682012-06-28 15:19:51 +0800680 /* expand-down segment */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100681 if (addr.ea <= lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300682 goto bad;
683 lim = desc.d ? 0xffffffff : 0xffff;
Avi Kivity618ff152011-04-03 12:32:09 +0300684 }
Paolo Bonzini997b0412014-11-19 18:33:38 +0100685 if (addr.ea > lim)
686 goto bad;
Nadav Amitbac155312015-01-26 09:32:26 +0200687 if (lim == 0xffffffff)
688 *max_size = ~0u;
689 else {
690 *max_size = (u64)lim + 1 - addr.ea;
691 if (size > *max_size)
692 goto bad;
693 }
Nadav Amit31ff6482014-11-19 17:43:13 +0200694 la &= (u32)-1;
Avi Kivity618ff152011-04-03 12:32:09 +0300695 break;
696 }
Avi Kivity1c11b372012-04-09 18:39:59 +0300697 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
698 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300699 *linear = la;
700 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300701bad:
702 if (addr.seg == VCPU_SREG_SS)
Paolo Bonzini36061892014-10-27 14:40:49 +0100703 return emulate_ss(ctxt, 0);
Avi Kivity618ff152011-04-03 12:32:09 +0300704 else
Paolo Bonzini36061892014-10-27 14:40:49 +0100705 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300706}
707
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400708static int linearize(struct x86_emulate_ctxt *ctxt,
709 struct segmented_address addr,
710 unsigned size, bool write,
711 ulong *linear)
712{
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100713 unsigned max_size;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200714 return __linearize(ctxt, addr, &max_size, size, write, false,
715 ctxt->mode, linear);
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400716}
717
Nadav Amitd50eaa12014-11-19 17:43:11 +0200718static inline int assign_eip(struct x86_emulate_ctxt *ctxt, ulong dst,
719 enum x86emul_mode mode)
720{
721 ulong linear;
722 int rc;
723 unsigned max_size;
724 struct segmented_address addr = { .seg = VCPU_SREG_CS,
725 .ea = dst };
726
727 if (ctxt->op_bytes != sizeof(unsigned long))
728 addr.ea = dst & ((1UL << (ctxt->op_bytes << 3)) - 1);
729 rc = __linearize(ctxt, addr, &max_size, 1, false, true, mode, &linear);
730 if (rc == X86EMUL_CONTINUE)
731 ctxt->_eip = addr.ea;
732 return rc;
733}
734
735static inline int assign_eip_near(struct x86_emulate_ctxt *ctxt, ulong dst)
736{
737 return assign_eip(ctxt, dst, ctxt->mode);
738}
739
740static int assign_eip_far(struct x86_emulate_ctxt *ctxt, ulong dst,
741 const struct desc_struct *cs_desc)
742{
743 enum x86emul_mode mode = ctxt->mode;
Nadav Amit82268082015-01-26 09:32:27 +0200744 int rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200745
746#ifdef CONFIG_X86_64
Nadav Amit82268082015-01-26 09:32:27 +0200747 if (ctxt->mode >= X86EMUL_MODE_PROT16) {
748 if (cs_desc->l) {
749 u64 efer = 0;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200750
Nadav Amit82268082015-01-26 09:32:27 +0200751 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
752 if (efer & EFER_LMA)
753 mode = X86EMUL_MODE_PROT64;
754 } else
755 mode = X86EMUL_MODE_PROT32; /* temporary value */
Nadav Amitd50eaa12014-11-19 17:43:11 +0200756 }
757#endif
758 if (mode == X86EMUL_MODE_PROT16 || mode == X86EMUL_MODE_PROT32)
759 mode = cs_desc->d ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
Nadav Amit82268082015-01-26 09:32:27 +0200760 rc = assign_eip(ctxt, dst, mode);
761 if (rc == X86EMUL_CONTINUE)
762 ctxt->mode = mode;
763 return rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200764}
765
766static inline int jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
767{
768 return assign_eip_near(ctxt, ctxt->_eip + rel);
769}
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400770
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200771static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
772 struct segmented_address addr,
773 void *data,
774 unsigned size)
775{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200776 int rc;
777 ulong linear;
778
Avi Kivity83b87952011-04-03 11:31:19 +0300779 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200780 if (rc != X86EMUL_CONTINUE)
781 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300782 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200783}
784
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900785/*
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200786 * Prefetch the remaining bytes of the instruction without crossing page
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900787 * boundary if they are not in fetch_cache yet.
788 */
Paolo Bonzini9506d572014-05-06 13:05:25 +0200789static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
Avi Kivity62266862007-11-20 13:15:52 +0200790{
Avi Kivity62266862007-11-20 13:15:52 +0200791 int rc;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100792 unsigned size, max_size;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200793 unsigned long linear;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200794 int cur_size = ctxt->fetch.end - ctxt->fetch.data;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200795 struct segmented_address addr = { .seg = VCPU_SREG_CS,
Paolo Bonzini17052f12014-05-06 16:33:01 +0200796 .ea = ctxt->eip + cur_size };
797
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100798 /*
799 * We do not know exactly how many bytes will be needed, and
800 * __linearize is expensive, so fetch as much as possible. We
801 * just have to avoid going beyond the 15 byte limit, the end
802 * of the segment, or the end of the page.
803 *
804 * __linearize is called with size 0 so that it does not do any
805 * boundary check itself. Instead, we use max_size to check
806 * against op_size.
807 */
Nadav Amitd50eaa12014-11-19 17:43:11 +0200808 rc = __linearize(ctxt, addr, &max_size, 0, false, true, ctxt->mode,
809 &linear);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200810 if (unlikely(rc != X86EMUL_CONTINUE))
811 return rc;
812
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100813 size = min_t(unsigned, 15UL ^ cur_size, max_size);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200814 size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear));
Paolo Bonzini5cfc7e02014-05-06 13:05:25 +0200815
816 /*
817 * One instruction can only straddle two pages,
818 * and one has been loaded at the beginning of
819 * x86_decode_insn. So, if not enough bytes
820 * still, we must have hit the 15-byte boundary.
821 */
822 if (unlikely(size < op_size))
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100823 return emulate_gp(ctxt, 0);
824
Paolo Bonzini17052f12014-05-06 16:33:01 +0200825 rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end,
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200826 size, &ctxt->exception);
827 if (unlikely(rc != X86EMUL_CONTINUE))
828 return rc;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200829 ctxt->fetch.end += size;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900830 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200831}
832
Paolo Bonzini9506d572014-05-06 13:05:25 +0200833static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt,
834 unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200835{
Nadav Amit08da44a2014-10-03 01:10:04 +0300836 unsigned done_size = ctxt->fetch.end - ctxt->fetch.ptr;
837
838 if (unlikely(done_size < size))
839 return __do_insn_fetch_bytes(ctxt, size - done_size);
Paolo Bonzini9506d572014-05-06 13:05:25 +0200840 else
841 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200842}
843
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900844/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900845#define insn_fetch(_type, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200846({ _type _x; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200847 \
848 rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900849 if (rc != X86EMUL_CONTINUE) \
850 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200851 ctxt->_eip += sizeof(_type); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200852 _x = *(_type __aligned(1) *) ctxt->fetch.ptr; \
853 ctxt->fetch.ptr += sizeof(_type); \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200854 _x; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900855})
856
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900857#define insn_fetch_arr(_arr, _size, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200858({ \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200859 rc = do_insn_fetch_bytes(_ctxt, _size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900860 if (rc != X86EMUL_CONTINUE) \
861 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200862 ctxt->_eip += (_size); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200863 memcpy(_arr, ctxt->fetch.ptr, _size); \
864 ctxt->fetch.ptr += (_size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900865})
866
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000867/*
868 * Given the 'reg' portion of a ModRM byte, and a register block, return a
869 * pointer into the block that addresses the relevant register.
870 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
871 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300872static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200873 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800874{
875 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200876 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800877
Avi Kivity6aa8b732006-12-10 02:21:36 -0800878 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300879 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
880 else
881 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800882 return p;
883}
884
885static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200886 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800887 u16 *size, unsigned long *address, int op_bytes)
888{
889 int rc;
890
891 if (op_bytes == 2)
892 op_bytes = 3;
893 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200894 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900895 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800896 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200897 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200898 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899 return rc;
900}
901
Avi Kivity34b77652013-01-19 19:51:56 +0200902FASTOP2(add);
903FASTOP2(or);
904FASTOP2(adc);
905FASTOP2(sbb);
906FASTOP2(and);
907FASTOP2(sub);
908FASTOP2(xor);
909FASTOP2(cmp);
910FASTOP2(test);
911
Avi Kivityb9fa4092013-02-09 11:31:48 +0200912FASTOP1SRC2(mul, mul_ex);
913FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200914FASTOP1SRC2EX(div, div_ex);
915FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +0200916
Avi Kivity34b77652013-01-19 19:51:56 +0200917FASTOP3WCL(shld);
918FASTOP3WCL(shrd);
919
920FASTOP2W(imul);
921
922FASTOP1(not);
923FASTOP1(neg);
924FASTOP1(inc);
925FASTOP1(dec);
926
927FASTOP2CL(rol);
928FASTOP2CL(ror);
929FASTOP2CL(rcl);
930FASTOP2CL(rcr);
931FASTOP2CL(shl);
932FASTOP2CL(shr);
933FASTOP2CL(sar);
934
935FASTOP2W(bsf);
936FASTOP2W(bsr);
937FASTOP2W(bt);
938FASTOP2W(bts);
939FASTOP2W(btr);
940FASTOP2W(btc);
941
Avi Kivitye47a5f52013-02-09 11:31:51 +0200942FASTOP2(xadd);
943
Nadav Amit5aca3722014-11-02 11:54:50 +0200944FASTOP2R(cmp, cmp_r);
945
Nadav Amit900efe22015-03-30 15:39:21 +0300946static int em_bsf_c(struct x86_emulate_ctxt *ctxt)
947{
948 /* If src is zero, do not writeback, but update flags */
949 if (ctxt->src.val == 0)
950 ctxt->dst.type = OP_NONE;
951 return fastop(ctxt, em_bsf);
952}
953
954static int em_bsr_c(struct x86_emulate_ctxt *ctxt)
955{
956 /* If src is zero, do not writeback, but update flags */
957 if (ctxt->src.val == 0)
958 ctxt->dst.type = OP_NONE;
959 return fastop(ctxt, em_bsr);
960}
961
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200962static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300963{
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200964 u8 rc;
965 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300966
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200967 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +0200968 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200969 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
970 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300971}
972
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300973static void fetch_register_operand(struct operand *op)
974{
975 switch (op->bytes) {
976 case 1:
977 op->val = *(u8 *)op->addr.reg;
978 break;
979 case 2:
980 op->val = *(u16 *)op->addr.reg;
981 break;
982 case 4:
983 op->val = *(u32 *)op->addr.reg;
984 break;
985 case 8:
986 op->val = *(u64 *)op->addr.reg;
987 break;
988 }
989}
990
Avi Kivity1253791d2011-03-29 11:41:27 +0200991static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
992{
993 ctxt->ops->get_fpu(ctxt);
994 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200995 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
996 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
997 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
998 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
999 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
1000 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
1001 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
1002 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001003#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001004 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
1005 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
1006 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
1007 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
1008 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
1009 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
1010 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
1011 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001012#endif
1013 default: BUG();
1014 }
1015 ctxt->ops->put_fpu(ctxt);
1016}
1017
1018static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
1019 int reg)
1020{
1021 ctxt->ops->get_fpu(ctxt);
1022 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001023 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
1024 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
1025 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
1026 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
1027 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
1028 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
1029 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
1030 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001031#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001032 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
1033 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
1034 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
1035 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
1036 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
1037 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
1038 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
1039 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001040#endif
1041 default: BUG();
1042 }
1043 ctxt->ops->put_fpu(ctxt);
1044}
1045
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001046static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1047{
1048 ctxt->ops->get_fpu(ctxt);
1049 switch (reg) {
1050 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1051 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1052 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1053 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1054 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1055 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1056 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1057 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1058 default: BUG();
1059 }
1060 ctxt->ops->put_fpu(ctxt);
1061}
1062
1063static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1064{
1065 ctxt->ops->get_fpu(ctxt);
1066 switch (reg) {
1067 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1068 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1069 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1070 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1071 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1072 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1073 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1074 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1075 default: BUG();
1076 }
1077 ctxt->ops->put_fpu(ctxt);
1078}
1079
Gleb Natapov045a2822012-12-20 16:57:43 +02001080static int em_fninit(struct x86_emulate_ctxt *ctxt)
1081{
1082 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1083 return emulate_nm(ctxt);
1084
1085 ctxt->ops->get_fpu(ctxt);
1086 asm volatile("fninit");
1087 ctxt->ops->put_fpu(ctxt);
1088 return X86EMUL_CONTINUE;
1089}
1090
1091static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1092{
1093 u16 fcw;
1094
1095 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1096 return emulate_nm(ctxt);
1097
1098 ctxt->ops->get_fpu(ctxt);
1099 asm volatile("fnstcw %0": "+m"(fcw));
1100 ctxt->ops->put_fpu(ctxt);
1101
Gleb Natapov045a2822012-12-20 16:57:43 +02001102 ctxt->dst.val = fcw;
1103
1104 return X86EMUL_CONTINUE;
1105}
1106
1107static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1108{
1109 u16 fsw;
1110
1111 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1112 return emulate_nm(ctxt);
1113
1114 ctxt->ops->get_fpu(ctxt);
1115 asm volatile("fnstsw %0": "+m"(fsw));
1116 ctxt->ops->put_fpu(ctxt);
1117
Gleb Natapov045a2822012-12-20 16:57:43 +02001118 ctxt->dst.val = fsw;
1119
1120 return X86EMUL_CONTINUE;
1121}
1122
Avi Kivity1253791d2011-03-29 11:41:27 +02001123static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001124 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001125{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001126 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001127
Avi Kivity9dac77f2011-06-01 15:34:25 +03001128 if (!(ctxt->d & ModRM))
1129 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity1253791d2011-03-29 11:41:27 +02001130
Avi Kivity9dac77f2011-06-01 15:34:25 +03001131 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001132 op->type = OP_XMM;
1133 op->bytes = 16;
1134 op->addr.xmm = reg;
1135 read_sse_reg(ctxt, &op->vec_val, reg);
1136 return;
1137 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001138 if (ctxt->d & Mmx) {
1139 reg &= 7;
1140 op->type = OP_MM;
1141 op->bytes = 8;
1142 op->addr.mm = reg;
1143 return;
1144 }
Avi Kivity1253791d2011-03-29 11:41:27 +02001145
Avi Kivity3c118e22007-10-31 10:27:04 +02001146 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001147 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1148 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1149
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001150 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001151 op->orig_val = op->val;
1152}
1153
Avi Kivitya6e34072012-06-10 17:15:39 +03001154static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1155{
1156 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1157 ctxt->modrm_seg = VCPU_SREG_SS;
1158}
1159
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001160static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001161 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001162{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001163 u8 sib;
Bandan Das02357bd2014-04-16 12:46:11 -04001164 int index_reg, base_reg, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001165 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001166 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001167
Bandan Das02357bd2014-04-16 12:46:11 -04001168 ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */
1169 index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */
1170 base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001171
Bandan Das02357bd2014-04-16 12:46:11 -04001172 ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001173 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
Bandan Das02357bd2014-04-16 12:46:11 -04001174 ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001175 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001176
Nadav Amit9b88ae92014-05-25 23:05:21 +03001177 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001178 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001179 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001180 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001181 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001182 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001183 op->type = OP_XMM;
1184 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001185 op->addr.xmm = ctxt->modrm_rm;
1186 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001187 return rc;
1188 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001189 if (ctxt->d & Mmx) {
1190 op->type = OP_MM;
1191 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001192 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001193 return rc;
1194 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001195 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001196 return rc;
1197 }
1198
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001199 op->type = OP_MEM;
1200
Avi Kivity9dac77f2011-06-01 15:34:25 +03001201 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001202 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1203 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1204 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1205 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001206
1207 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001208 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001209 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001210 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001211 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001212 break;
1213 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001214 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001215 break;
1216 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001217 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001218 break;
1219 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001220 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001221 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001222 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001223 break;
1224 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001225 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001226 break;
1227 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001228 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001229 break;
1230 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001231 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001232 break;
1233 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001234 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001235 break;
1236 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001237 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001238 break;
1239 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001240 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001241 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001242 break;
1243 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001244 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001245 break;
1246 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001247 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1248 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1249 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001250 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001251 } else {
1252 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001253 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001254 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001255 index_reg |= (sib >> 3) & 7;
1256 base_reg |= sib & 7;
1257 scale = sib >> 6;
1258
Avi Kivity9dac77f2011-06-01 15:34:25 +03001259 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001260 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001261 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001262 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001263 adjust_modrm_seg(ctxt, base_reg);
Nadav Amitab708092014-12-25 02:52:21 +02001264 /* Increment ESP on POP [ESP] */
1265 if ((ctxt->d & IncSP) &&
1266 base_reg == VCPU_REGS_RSP)
1267 modrm_ea += ctxt->op_bytes;
Avi Kivitya6e34072012-06-10 17:15:39 +03001268 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001269 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001270 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001271 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Nadav Amit5b38ab82014-11-02 11:54:41 +02001272 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity84411d82008-06-15 21:53:26 -07001273 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001274 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001275 } else {
1276 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001277 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001278 adjust_modrm_seg(ctxt, base_reg);
1279 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001280 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001281 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001282 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001283 break;
1284 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001285 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001286 break;
1287 }
1288 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001289 op->addr.mem.ea = modrm_ea;
Bandan Das41061cd2014-04-16 12:46:14 -04001290 if (ctxt->ad_bytes != 8)
1291 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
1292
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001293done:
1294 return rc;
1295}
1296
1297static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001298 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001299{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001300 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001301
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001302 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001303 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001304 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001305 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001306 break;
1307 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001308 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001309 break;
1310 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001311 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001312 break;
1313 }
1314done:
1315 return rc;
1316}
1317
Avi Kivity9dac77f2011-06-01 15:34:25 +03001318static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001319{
Sheng Yang7129eec2010-09-28 16:33:32 +08001320 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001321
Avi Kivity9dac77f2011-06-01 15:34:25 +03001322 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001323 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001324
Avi Kivity9dac77f2011-06-01 15:34:25 +03001325 if (ctxt->src.bytes == 2)
1326 sv = (s16)ctxt->src.val & (s16)mask;
1327 else if (ctxt->src.bytes == 4)
1328 sv = (s32)ctxt->src.val & (s32)mask;
Nadav Amit7dec5602014-06-15 16:12:57 +03001329 else
1330 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001331
Nadav Amit1c1c35a2014-11-19 17:43:09 +02001332 ctxt->dst.addr.mem.ea = address_mask(ctxt,
1333 ctxt->dst.addr.mem.ea + (sv >> 3));
Wei Yongjun35c843c2010-08-09 11:34:56 +08001334 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001335
1336 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001337 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001338}
1339
Gleb Natapov9de41572010-04-28 19:15:22 +03001340static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001341 unsigned long addr, void *dest, unsigned size)
1342{
1343 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001344 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001345
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001346 if (mc->pos < mc->end)
1347 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001348
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001349 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001350
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001351 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1352 &ctxt->exception);
1353 if (rc != X86EMUL_CONTINUE)
1354 return rc;
1355
1356 mc->end += size;
1357
1358read_cached:
1359 memcpy(dest, mc->data + mc->pos, size);
1360 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001361 return X86EMUL_CONTINUE;
1362}
1363
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001364static int segmented_read(struct x86_emulate_ctxt *ctxt,
1365 struct segmented_address addr,
1366 void *data,
1367 unsigned size)
1368{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001369 int rc;
1370 ulong linear;
1371
Avi Kivity83b87952011-04-03 11:31:19 +03001372 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001373 if (rc != X86EMUL_CONTINUE)
1374 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001375 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001376}
1377
1378static int segmented_write(struct x86_emulate_ctxt *ctxt,
1379 struct segmented_address addr,
1380 const void *data,
1381 unsigned size)
1382{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001383 int rc;
1384 ulong linear;
1385
Avi Kivity83b87952011-04-03 11:31:19 +03001386 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001387 if (rc != X86EMUL_CONTINUE)
1388 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001389 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1390 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001391}
1392
1393static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1394 struct segmented_address addr,
1395 const void *orig_data, const void *data,
1396 unsigned size)
1397{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001398 int rc;
1399 ulong linear;
1400
Avi Kivity83b87952011-04-03 11:31:19 +03001401 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001402 if (rc != X86EMUL_CONTINUE)
1403 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001404 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1405 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001406}
1407
Gleb Natapov7b262e92010-03-18 15:20:27 +02001408static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001409 unsigned int size, unsigned short port,
1410 void *dest)
1411{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001412 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001413
1414 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001415 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001416 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001417 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Nadav Amit0efb0442015-03-29 16:33:03 +03001418 in_page = (ctxt->eflags & X86_EFLAGS_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001419 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1420 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Mark Rustadb55a8142014-07-25 06:27:05 -07001421 n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001422 if (n == 0)
1423 n = 1;
1424 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001425 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001426 return 0;
1427 rc->end = n * size;
1428 }
1429
Nadav Amite6e39f02014-04-18 03:35:10 +03001430 if (ctxt->rep_prefix && (ctxt->d & String) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03001431 !(ctxt->eflags & X86_EFLAGS_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001432 ctxt->dst.data = rc->data + rc->pos;
1433 ctxt->dst.type = OP_MEM_STR;
1434 ctxt->dst.count = (rc->end - rc->pos) / size;
1435 rc->pos = rc->end;
1436 } else {
1437 memcpy(dest, rc->data + rc->pos, size);
1438 rc->pos += size;
1439 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001440 return 1;
1441}
1442
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001443static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1444 u16 index, struct desc_struct *desc)
1445{
1446 struct desc_ptr dt;
1447 ulong addr;
1448
1449 ctxt->ops->get_idt(ctxt, &dt);
1450
1451 if (dt.size < index * 8 + 7)
1452 return emulate_gp(ctxt, index << 3 | 0x2);
1453
1454 addr = dt.address + index * 8;
1455 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1456 &ctxt->exception);
1457}
1458
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001459static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001460 u16 selector, struct desc_ptr *dt)
1461{
Mathias Krause0225fb52012-08-30 01:30:16 +02001462 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001463 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001464
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001465 if (selector & 1 << 2) {
1466 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001467 u16 sel;
1468
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001469 memset (dt, 0, sizeof *dt);
Nadav Amit2eedcac2014-06-02 18:34:05 +03001470 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1471 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001472 return;
1473
1474 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001475 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001476 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001477 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001478}
1479
Nadav Amitedccda72014-12-25 02:52:23 +02001480static int get_descriptor_ptr(struct x86_emulate_ctxt *ctxt,
1481 u16 selector, ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001482{
1483 struct desc_ptr dt;
1484 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001485 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001486
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001487 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001488
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001489 if (dt.size < index * 8 + 7)
1490 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001491
1492 addr = dt.address + index * 8;
Nadav Amitedccda72014-12-25 02:52:23 +02001493
1494#ifdef CONFIG_X86_64
1495 if (addr >> 32 != 0) {
1496 u64 efer = 0;
1497
1498 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1499 if (!(efer & EFER_LMA))
1500 addr &= (u32)-1;
1501 }
1502#endif
1503
1504 *desc_addr_p = addr;
1505 return X86EMUL_CONTINUE;
1506}
1507
1508/* allowed just for 8 bytes segments */
1509static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1510 u16 selector, struct desc_struct *desc,
1511 ulong *desc_addr_p)
1512{
1513 int rc;
1514
1515 rc = get_descriptor_ptr(ctxt, selector, desc_addr_p);
1516 if (rc != X86EMUL_CONTINUE)
1517 return rc;
1518
1519 return ctxt->ops->read_std(ctxt, *desc_addr_p, desc, sizeof(*desc),
1520 &ctxt->exception);
1521}
1522
1523/* allowed just for 8 bytes segments */
1524static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1525 u16 selector, struct desc_struct *desc)
1526{
1527 int rc;
1528 ulong addr;
1529
1530 rc = get_descriptor_ptr(ctxt, selector, &addr);
1531 if (rc != X86EMUL_CONTINUE)
1532 return rc;
1533
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001534 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1535 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001536}
1537
Gleb Natapov5601d052011-03-07 14:55:06 +02001538/* Does not support long mode */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001539static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Nadav Amitd1442d82014-09-18 22:39:39 +03001540 u16 selector, int seg, u8 cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001541 enum x86_transfer_type transfer,
Nadav Amitd1442d82014-09-18 22:39:39 +03001542 struct desc_struct *desc)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001543{
Avi Kivity869be992012-06-13 16:30:53 +03001544 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001545 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001546 unsigned err_vec = GP_VECTOR;
1547 u32 err_code = 0;
1548 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001549 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001550 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001551 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001552 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001553
1554 memset(&seg_desc, 0, sizeof seg_desc);
1555
Kevin Wolff8da94e2013-04-11 14:06:03 +02001556 if (ctxt->mode == X86EMUL_MODE_REAL) {
1557 /* set real mode segment descriptor (keep limit etc. for
1558 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001559 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001560 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001561 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001562 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1563 /* VM86 needs a clean new segment descriptor */
1564 set_desc_base(&seg_desc, selector << 4);
1565 set_desc_limit(&seg_desc, 0xffff);
1566 seg_desc.type = 3;
1567 seg_desc.p = 1;
1568 seg_desc.s = 1;
1569 seg_desc.dpl = 3;
1570 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001571 }
1572
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001573 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001574
1575 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1576 if ((seg == VCPU_SREG_CS
1577 || (seg == VCPU_SREG_SS
1578 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1579 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001580 && null_selector)
1581 goto exception;
1582
1583 /* TR should be in GDT only */
1584 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1585 goto exception;
1586
1587 if (null_selector) /* for NULL selector skip all following checks */
1588 goto load;
1589
Avi Kivitye9194642012-06-13 16:29:39 +03001590 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001591 if (ret != X86EMUL_CONTINUE)
1592 return ret;
1593
1594 err_code = selector & 0xfffc;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001595 err_vec = (transfer == X86_TRANSFER_TASK_SWITCH) ? TS_VECTOR :
1596 GP_VECTOR;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001597
Guo Chaofc058682012-06-28 15:19:51 +08001598 /* can't load system descriptor into segment selector */
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001599 if (seg <= VCPU_SREG_GS && !seg_desc.s) {
1600 if (transfer == X86_TRANSFER_CALL_JMP)
1601 return X86EMUL_UNHANDLEABLE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001602 goto exception;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001603 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001604
1605 if (!seg_desc.p) {
1606 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1607 goto exception;
1608 }
1609
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001610 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001611
1612 switch (seg) {
1613 case VCPU_SREG_SS:
1614 /*
1615 * segment is not a writable data segment or segment
1616 * selector's RPL != CPL or segment selector's RPL != CPL
1617 */
1618 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1619 goto exception;
1620 break;
1621 case VCPU_SREG_CS:
1622 if (!(seg_desc.type & 8))
1623 goto exception;
1624
1625 if (seg_desc.type & 4) {
1626 /* conforming */
1627 if (dpl > cpl)
1628 goto exception;
1629 } else {
1630 /* nonconforming */
1631 if (rpl > cpl || dpl != cpl)
1632 goto exception;
1633 }
Nadav Amit040c8dc2014-09-18 22:39:43 +03001634 /* in long-mode d/b must be clear if l is set */
1635 if (seg_desc.d && seg_desc.l) {
1636 u64 efer = 0;
1637
1638 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1639 if (efer & EFER_LMA)
1640 goto exception;
1641 }
1642
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001643 /* CS(RPL) <- CPL */
1644 selector = (selector & 0xfffc) | cpl;
1645 break;
1646 case VCPU_SREG_TR:
1647 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1648 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001649 old_desc = seg_desc;
1650 seg_desc.type |= 2; /* busy */
1651 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1652 sizeof(seg_desc), &ctxt->exception);
1653 if (ret != X86EMUL_CONTINUE)
1654 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001655 break;
1656 case VCPU_SREG_LDTR:
1657 if (seg_desc.s || seg_desc.type != 2)
1658 goto exception;
1659 break;
1660 default: /* DS, ES, FS, or GS */
1661 /*
1662 * segment is not a data or readable code segment or
1663 * ((segment is a data or nonconforming code segment)
1664 * and (both RPL and CPL > DPL))
1665 */
1666 if ((seg_desc.type & 0xa) == 0x8 ||
1667 (((seg_desc.type & 0xc) != 0xc) &&
1668 (rpl > dpl && cpl > dpl)))
1669 goto exception;
1670 break;
1671 }
1672
1673 if (seg_desc.s) {
1674 /* mark segment as accessed */
Nadav Amite2cefa72014-12-25 02:52:22 +02001675 if (!(seg_desc.type & 1)) {
1676 seg_desc.type |= 1;
1677 ret = write_segment_descriptor(ctxt, selector,
1678 &seg_desc);
1679 if (ret != X86EMUL_CONTINUE)
1680 return ret;
1681 }
Nadav Amite37a75a2014-06-02 18:34:04 +03001682 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
1683 ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3,
1684 sizeof(base3), &ctxt->exception);
1685 if (ret != X86EMUL_CONTINUE)
1686 return ret;
Nadav Amit9a9abf62014-11-02 11:54:56 +02001687 if (is_noncanonical_address(get_desc_base(&seg_desc) |
1688 ((u64)base3 << 32)))
1689 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001690 }
1691load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001692 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Nadav Amitd1442d82014-09-18 22:39:39 +03001693 if (desc)
1694 *desc = seg_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001695 return X86EMUL_CONTINUE;
1696exception:
Paolo Bonzini592f0852014-08-20 10:05:08 +02001697 return emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001698}
1699
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001700static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1701 u16 selector, int seg)
1702{
1703 u8 cpl = ctxt->ops->cpl(ctxt);
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001704 return __load_segment_descriptor(ctxt, selector, seg, cpl,
1705 X86_TRANSFER_NONE, NULL);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001706}
1707
Wei Yongjun31be40b2010-08-17 09:17:30 +08001708static void write_register_operand(struct operand *op)
1709{
Nadav Amit6fd8e122015-03-30 15:39:20 +03001710 return assign_register(op->addr.reg, op->val, op->bytes);
Wei Yongjun31be40b2010-08-17 09:17:30 +08001711}
1712
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001713static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001714{
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001715 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001716 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001717 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001718 break;
1719 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001720 if (ctxt->lock_prefix)
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001721 return segmented_cmpxchg(ctxt,
1722 op->addr.mem,
1723 &op->orig_val,
1724 &op->val,
1725 op->bytes);
1726 else
1727 return segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001728 op->addr.mem,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001729 &op->val,
1730 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001731 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001732 case OP_MEM_STR:
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001733 return segmented_write(ctxt,
1734 op->addr.mem,
1735 op->data,
1736 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001737 break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001738 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001739 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001740 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001741 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001742 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001743 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001744 case OP_NONE:
1745 /* no writeback */
1746 break;
1747 default:
1748 break;
1749 }
1750 return X86EMUL_CONTINUE;
1751}
1752
Avi Kivity51ddff52012-06-12 20:19:40 +03001753static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001754{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001755 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001756
Avi Kivity5ad105e2012-08-19 14:34:31 +03001757 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001758 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001759 addr.seg = VCPU_SREG_SS;
1760
Avi Kivity51ddff52012-06-12 20:19:40 +03001761 return segmented_write(ctxt, addr, data, bytes);
1762}
1763
1764static int em_push(struct x86_emulate_ctxt *ctxt)
1765{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001766 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001767 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001768 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001769}
1770
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001771static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001772 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001773{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001774 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001775 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001776
Avi Kivitydd856ef2012-08-27 23:46:17 +03001777 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001778 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001779 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001780 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001781 return rc;
1782
Avi Kivity5ad105e2012-08-19 14:34:31 +03001783 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001784 return rc;
1785}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001786
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001787static int em_pop(struct x86_emulate_ctxt *ctxt)
1788{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001789 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001790}
1791
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001792static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001793 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001794{
1795 int rc;
1796 unsigned long val, change_mask;
Nadav Amit0efb0442015-03-29 16:33:03 +03001797 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001798 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001799
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001800 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001801 if (rc != X86EMUL_CONTINUE)
1802 return rc;
1803
Nadav Amit0efb0442015-03-29 16:33:03 +03001804 change_mask = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
1805 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF |
1806 X86_EFLAGS_TF | X86_EFLAGS_DF | X86_EFLAGS_NT |
1807 X86_EFLAGS_AC | X86_EFLAGS_ID;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001808
1809 switch(ctxt->mode) {
1810 case X86EMUL_MODE_PROT64:
1811 case X86EMUL_MODE_PROT32:
1812 case X86EMUL_MODE_PROT16:
1813 if (cpl == 0)
Nadav Amit0efb0442015-03-29 16:33:03 +03001814 change_mask |= X86_EFLAGS_IOPL;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001815 if (cpl <= iopl)
Nadav Amit0efb0442015-03-29 16:33:03 +03001816 change_mask |= X86_EFLAGS_IF;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001817 break;
1818 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001819 if (iopl < 3)
1820 return emulate_gp(ctxt, 0);
Nadav Amit0efb0442015-03-29 16:33:03 +03001821 change_mask |= X86_EFLAGS_IF;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001822 break;
1823 default: /* real mode */
Nadav Amit0efb0442015-03-29 16:33:03 +03001824 change_mask |= (X86_EFLAGS_IOPL | X86_EFLAGS_IF);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001825 break;
1826 }
1827
1828 *(unsigned long *)dest =
1829 (ctxt->eflags & ~change_mask) | (val & change_mask);
1830
1831 return rc;
1832}
1833
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001834static int em_popf(struct x86_emulate_ctxt *ctxt)
1835{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001836 ctxt->dst.type = OP_REG;
1837 ctxt->dst.addr.reg = &ctxt->eflags;
1838 ctxt->dst.bytes = ctxt->op_bytes;
1839 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001840}
1841
Avi Kivity612e89f2012-06-12 20:03:23 +03001842static int em_enter(struct x86_emulate_ctxt *ctxt)
1843{
1844 int rc;
1845 unsigned frame_size = ctxt->src.val;
1846 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001847 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001848
1849 if (nesting_level)
1850 return X86EMUL_UNHANDLEABLE;
1851
Avi Kivitydd856ef2012-08-27 23:46:17 +03001852 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1853 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001854 if (rc != X86EMUL_CONTINUE)
1855 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001856 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001857 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001858 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1859 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001860 stack_mask(ctxt));
1861 return X86EMUL_CONTINUE;
1862}
1863
Avi Kivityf47cfa32012-06-07 17:49:24 +03001864static int em_leave(struct x86_emulate_ctxt *ctxt)
1865{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001866 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001867 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001868 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001869}
1870
Avi Kivity1cd196e2011-09-13 10:45:51 +03001871static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001872{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001873 int seg = ctxt->src2.val;
1874
Avi Kivity9dac77f2011-06-01 15:34:25 +03001875 ctxt->src.val = get_segment_selector(ctxt, seg);
Nadav Amit0fcc2072014-11-02 11:54:51 +02001876 if (ctxt->op_bytes == 4) {
1877 rsp_increment(ctxt, -2);
1878 ctxt->op_bytes = 2;
1879 }
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001880
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001881 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001882}
1883
Avi Kivity1cd196e2011-09-13 10:45:51 +03001884static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001885{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001886 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001887 unsigned long selector;
1888 int rc;
1889
Nadav Amit3313bc42014-12-25 02:52:17 +02001890 rc = emulate_pop(ctxt, &selector, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001891 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001892 return rc;
1893
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001894 if (ctxt->modrm_reg == VCPU_SREG_SS)
1895 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Nadav Amit3313bc42014-12-25 02:52:17 +02001896 if (ctxt->op_bytes > 2)
1897 rsp_increment(ctxt, ctxt->op_bytes - 2);
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001898
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001899 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001900 return rc;
1901}
1902
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001903static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001904{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001905 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001906 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001907 int reg = VCPU_REGS_RAX;
1908
1909 while (reg <= VCPU_REGS_RDI) {
1910 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001911 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001912
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001913 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001914 if (rc != X86EMUL_CONTINUE)
1915 return rc;
1916
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001917 ++reg;
1918 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001919
Wei Yongjunc37eda12010-06-15 09:03:33 +08001920 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001921}
1922
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001923static int em_pushf(struct x86_emulate_ctxt *ctxt)
1924{
Nadav Amit0efb0442015-03-29 16:33:03 +03001925 ctxt->src.val = (unsigned long)ctxt->eflags & ~X86_EFLAGS_VM;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001926 return em_push(ctxt);
1927}
1928
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001929static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001930{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001931 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001932 int reg = VCPU_REGS_RDI;
Nadav Amit6fd8e122015-03-30 15:39:20 +03001933 u32 val;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001934
1935 while (reg >= VCPU_REGS_RAX) {
1936 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001937 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001938 --reg;
1939 }
1940
Nadav Amit6fd8e122015-03-30 15:39:20 +03001941 rc = emulate_pop(ctxt, &val, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001942 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001943 break;
Nadav Amit6fd8e122015-03-30 15:39:20 +03001944 assign_register(reg_rmw(ctxt, reg), val, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001945 --reg;
1946 }
1947 return rc;
1948}
1949
Avi Kivitydd856ef2012-08-27 23:46:17 +03001950static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001951{
Mathias Krause0225fb52012-08-30 01:30:16 +02001952 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001953 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001954 struct desc_ptr dt;
1955 gva_t cs_addr;
1956 gva_t eip_addr;
1957 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001958
1959 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001960 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001961 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001962 if (rc != X86EMUL_CONTINUE)
1963 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001964
Nadav Amit0efb0442015-03-29 16:33:03 +03001965 ctxt->eflags &= ~(X86_EFLAGS_IF | X86_EFLAGS_TF | X86_EFLAGS_AC);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001966
Avi Kivity9dac77f2011-06-01 15:34:25 +03001967 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001968 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001969 if (rc != X86EMUL_CONTINUE)
1970 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001971
Avi Kivity9dac77f2011-06-01 15:34:25 +03001972 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001973 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001974 if (rc != X86EMUL_CONTINUE)
1975 return rc;
1976
Avi Kivity4bff1e862011-04-20 13:37:53 +03001977 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001978
1979 eip_addr = dt.address + (irq << 2);
1980 cs_addr = dt.address + (irq << 2) + 2;
1981
Avi Kivity0f65dd72011-04-20 13:37:53 +03001982 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001983 if (rc != X86EMUL_CONTINUE)
1984 return rc;
1985
Avi Kivity0f65dd72011-04-20 13:37:53 +03001986 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001987 if (rc != X86EMUL_CONTINUE)
1988 return rc;
1989
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001990 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001991 if (rc != X86EMUL_CONTINUE)
1992 return rc;
1993
Avi Kivity9dac77f2011-06-01 15:34:25 +03001994 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001995
1996 return rc;
1997}
1998
Avi Kivitydd856ef2012-08-27 23:46:17 +03001999int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
2000{
2001 int rc;
2002
2003 invalidate_registers(ctxt);
2004 rc = __emulate_int_real(ctxt, irq);
2005 if (rc == X86EMUL_CONTINUE)
2006 writeback_registers(ctxt);
2007 return rc;
2008}
2009
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002010static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002011{
2012 switch(ctxt->mode) {
2013 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03002014 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002015 case X86EMUL_MODE_VM86:
2016 case X86EMUL_MODE_PROT16:
2017 case X86EMUL_MODE_PROT32:
2018 case X86EMUL_MODE_PROT64:
2019 default:
2020 /* Protected mode interrupts unimplemented yet */
2021 return X86EMUL_UNHANDLEABLE;
2022 }
2023}
2024
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002025static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002026{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002027 int rc = X86EMUL_CONTINUE;
2028 unsigned long temp_eip = 0;
2029 unsigned long temp_eflags = 0;
2030 unsigned long cs = 0;
Nadav Amit0efb0442015-03-29 16:33:03 +03002031 unsigned long mask = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
2032 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_TF |
2033 X86_EFLAGS_IF | X86_EFLAGS_DF | X86_EFLAGS_OF |
2034 X86_EFLAGS_IOPL | X86_EFLAGS_NT | X86_EFLAGS_RF |
2035 X86_EFLAGS_AC | X86_EFLAGS_ID |
2036 X86_EFLAGS_FIXED_BIT;
2037 unsigned long vm86_mask = X86_EFLAGS_VM | X86_EFLAGS_VIF |
2038 X86_EFLAGS_VIP;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002039
2040 /* TODO: Add stack limit check */
2041
Avi Kivity9dac77f2011-06-01 15:34:25 +03002042 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002043
2044 if (rc != X86EMUL_CONTINUE)
2045 return rc;
2046
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002047 if (temp_eip & ~0xffff)
2048 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002049
Avi Kivity9dac77f2011-06-01 15:34:25 +03002050 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002051
2052 if (rc != X86EMUL_CONTINUE)
2053 return rc;
2054
Avi Kivity9dac77f2011-06-01 15:34:25 +03002055 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002056
2057 if (rc != X86EMUL_CONTINUE)
2058 return rc;
2059
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002060 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002061
2062 if (rc != X86EMUL_CONTINUE)
2063 return rc;
2064
Avi Kivity9dac77f2011-06-01 15:34:25 +03002065 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002066
Avi Kivity9dac77f2011-06-01 15:34:25 +03002067 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002068 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03002069 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002070 ctxt->eflags &= ~0xffff;
2071 ctxt->eflags |= temp_eflags;
2072 }
2073
2074 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
Nadav Amit0efb0442015-03-29 16:33:03 +03002075 ctxt->eflags |= X86_EFLAGS_FIXED_BIT;
Nadav Amit801806d2015-01-26 09:32:23 +02002076 ctxt->ops->set_nmi_mask(ctxt, false);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002077
2078 return rc;
2079}
2080
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002081static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002082{
2083 switch(ctxt->mode) {
2084 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002085 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002086 case X86EMUL_MODE_VM86:
2087 case X86EMUL_MODE_PROT16:
2088 case X86EMUL_MODE_PROT32:
2089 case X86EMUL_MODE_PROT64:
2090 default:
2091 /* iret from protected mode unimplemented yet */
2092 return X86EMUL_UNHANDLEABLE;
2093 }
2094}
2095
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002096static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2097{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002098 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002099 unsigned short sel, old_sel;
2100 struct desc_struct old_desc, new_desc;
2101 const struct x86_emulate_ops *ops = ctxt->ops;
2102 u8 cpl = ctxt->ops->cpl(ctxt);
2103
2104 /* Assignment of RIP may only fail in 64-bit mode */
2105 if (ctxt->mode == X86EMUL_MODE_PROT64)
2106 ops->get_segment(ctxt, &old_sel, &old_desc, NULL,
2107 VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002108
Avi Kivity9dac77f2011-06-01 15:34:25 +03002109 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002110
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002111 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
2112 X86_TRANSFER_CALL_JMP,
Nadav Amitd1442d82014-09-18 22:39:39 +03002113 &new_desc);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002114 if (rc != X86EMUL_CONTINUE)
2115 return rc;
2116
Nadav Amitd50eaa12014-11-19 17:43:11 +02002117 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03002118 if (rc != X86EMUL_CONTINUE) {
Nadav Amit7e46ddd2014-10-28 00:03:43 +02002119 WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64);
Nadav Amitd1442d82014-09-18 22:39:39 +03002120 /* assigning eip failed; restore the old cs */
2121 ops->set_segment(ctxt, old_sel, &old_desc, 0, VCPU_SREG_CS);
2122 return rc;
2123 }
2124 return rc;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002125}
2126
Nadav Amitf7784042014-09-18 22:39:41 +03002127static int em_jmp_abs(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002128{
Nadav Amitf7784042014-09-18 22:39:41 +03002129 return assign_eip_near(ctxt, ctxt->src.val);
2130}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002131
Nadav Amitf7784042014-09-18 22:39:41 +03002132static int em_call_near_abs(struct x86_emulate_ctxt *ctxt)
2133{
2134 int rc;
2135 long int old_eip;
2136
2137 old_eip = ctxt->_eip;
2138 rc = assign_eip_near(ctxt, ctxt->src.val);
2139 if (rc != X86EMUL_CONTINUE)
2140 return rc;
2141 ctxt->src.val = old_eip;
2142 rc = em_push(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002143 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002144}
2145
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002146static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002147{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002148 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002149
Nadav Amitaaa05f22014-06-02 18:34:10 +03002150 if (ctxt->dst.bytes == 16)
2151 return X86EMUL_UNHANDLEABLE;
2152
Avi Kivitydd856ef2012-08-27 23:46:17 +03002153 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2154 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2155 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2156 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Nadav Amit0efb0442015-03-29 16:33:03 +03002157 ctxt->eflags &= ~X86_EFLAGS_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002158 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002159 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2160 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002161
Nadav Amit0efb0442015-03-29 16:33:03 +03002162 ctxt->eflags |= X86_EFLAGS_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002163 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002164 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002165}
2166
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002167static int em_ret(struct x86_emulate_ctxt *ctxt)
2168{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002169 int rc;
2170 unsigned long eip;
2171
2172 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
2173 if (rc != X86EMUL_CONTINUE)
2174 return rc;
2175
2176 return assign_eip_near(ctxt, eip);
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002177}
2178
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002179static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002180{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002181 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002182 unsigned long eip, cs;
2183 u16 old_cs;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002184 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amitd1442d82014-09-18 22:39:39 +03002185 struct desc_struct old_desc, new_desc;
2186 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002187
Nadav Amitd1442d82014-09-18 22:39:39 +03002188 if (ctxt->mode == X86EMUL_MODE_PROT64)
2189 ops->get_segment(ctxt, &old_cs, &old_desc, NULL,
2190 VCPU_SREG_CS);
2191
2192 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002193 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002194 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002195 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002196 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002197 return rc;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002198 /* Outer-privilege level return is not implemented */
2199 if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2200 return X86EMUL_UNHANDLEABLE;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002201 rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, cpl,
2202 X86_TRANSFER_RET,
Nadav Amitd1442d82014-09-18 22:39:39 +03002203 &new_desc);
2204 if (rc != X86EMUL_CONTINUE)
2205 return rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +02002206 rc = assign_eip_far(ctxt, eip, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03002207 if (rc != X86EMUL_CONTINUE) {
Nadav Amit7e46ddd2014-10-28 00:03:43 +02002208 WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64);
Nadav Amitd1442d82014-09-18 22:39:39 +03002209 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
2210 }
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002211 return rc;
2212}
2213
Bruce Rogers32611072013-09-09 09:40:20 -06002214static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2215{
2216 int rc;
2217
2218 rc = em_ret_far(ctxt);
2219 if (rc != X86EMUL_CONTINUE)
2220 return rc;
2221 rsp_increment(ctxt, ctxt->src.val);
2222 return X86EMUL_CONTINUE;
2223}
2224
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002225static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2226{
2227 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002228 ctxt->dst.orig_val = ctxt->dst.val;
2229 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002230 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002231 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002232 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002233
Nadav Amit0efb0442015-03-29 16:33:03 +03002234 if (ctxt->eflags & X86_EFLAGS_ZF) {
Nadav Amit2fcf5c82015-01-26 09:32:21 +02002235 /* Success: write back to memory; no update of EAX */
2236 ctxt->src.type = OP_NONE;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002237 ctxt->dst.val = ctxt->src.orig_val;
2238 } else {
2239 /* Failure: write the value we saw to EAX. */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02002240 ctxt->src.type = OP_REG;
2241 ctxt->src.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
2242 ctxt->src.val = ctxt->dst.orig_val;
2243 /* Create write-cycle to dest by writing the same value */
Nadav Amit37c564f2014-06-02 18:34:07 +03002244 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002245 }
2246 return X86EMUL_CONTINUE;
2247}
2248
Avi Kivityd4b43252011-09-13 10:45:50 +03002249static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002250{
Avi Kivityd4b43252011-09-13 10:45:50 +03002251 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002252 unsigned short sel;
2253 int rc;
2254
Avi Kivity9dac77f2011-06-01 15:34:25 +03002255 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002256
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002257 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002258 if (rc != X86EMUL_CONTINUE)
2259 return rc;
2260
Avi Kivity9dac77f2011-06-01 15:34:25 +03002261 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002262 return rc;
2263}
2264
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002265static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002266setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002267 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002268{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002269 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002270 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002271 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002272 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002273 cs->type = 0x0b; /* Read, Execute, Accessed */
2274 cs->s = 1;
2275 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002276 cs->p = 1;
2277 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002278 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002279
Gleb Natapov79168fd2010-04-28 19:15:30 +03002280 set_desc_base(ss, 0); /* flat segment */
2281 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002282 ss->g = 1; /* 4kb granularity */
2283 ss->s = 1;
2284 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002285 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002286 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002287 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002288 ss->l = 0;
2289 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002290}
2291
Avi Kivity1a18a692012-02-01 12:23:21 +02002292static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2293{
2294 u32 eax, ebx, ecx, edx;
2295
2296 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002297 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2298 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002299 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2300 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2301}
2302
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002303static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2304{
Mathias Krause0225fb52012-08-30 01:30:16 +02002305 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002306 u32 eax, ebx, ecx, edx;
2307
2308 /*
2309 * syscall should always be enabled in longmode - so only become
2310 * vendor specific (cpuid) if other modes are active...
2311 */
2312 if (ctxt->mode == X86EMUL_MODE_PROT64)
2313 return true;
2314
2315 eax = 0x00000000;
2316 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002317 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2318 /*
2319 * Intel ("GenuineIntel")
2320 * remark: Intel CPUs only support "syscall" in 64bit
2321 * longmode. Also an 64bit guest with a
2322 * 32bit compat-app running will #UD !! While this
2323 * behaviour can be fixed (by emulating) into AMD
2324 * response - CPUs of AMD can't behave like Intel.
2325 */
2326 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2327 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2328 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2329 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002330
Avi Kivity0017f932012-06-07 14:10:16 +03002331 /* AMD ("AuthenticAMD") */
2332 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2333 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2334 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2335 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002336
Avi Kivity0017f932012-06-07 14:10:16 +03002337 /* AMD ("AMDisbetter!") */
2338 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2339 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2340 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2341 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002342
2343 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2344 return false;
2345}
2346
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002347static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002348{
Mathias Krause0225fb52012-08-30 01:30:16 +02002349 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002350 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002351 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002352 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002353 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002354
2355 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002356 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002357 ctxt->mode == X86EMUL_MODE_VM86)
2358 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002359
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002360 if (!(em_syscall_is_enabled(ctxt)))
2361 return emulate_ud(ctxt);
2362
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002363 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002364 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002365
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002366 if (!(efer & EFER_SCE))
2367 return emulate_ud(ctxt);
2368
Avi Kivity717746e2011-04-20 13:37:53 +03002369 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002370 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002371 cs_sel = (u16)(msr_data & 0xfffc);
2372 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002373
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002374 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002375 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002376 cs.l = 1;
2377 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002378 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2379 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002380
Avi Kivitydd856ef2012-08-27 23:46:17 +03002381 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002382 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002383#ifdef CONFIG_X86_64
Nadav Amit6c6cb692014-07-21 14:37:30 +03002384 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002385
Avi Kivity717746e2011-04-20 13:37:53 +03002386 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002387 ctxt->mode == X86EMUL_MODE_PROT64 ?
2388 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002389 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002390
Avi Kivity717746e2011-04-20 13:37:53 +03002391 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Nadav Amit6c6cb692014-07-21 14:37:30 +03002392 ctxt->eflags &= ~msr_data;
Nadav Amit0efb0442015-03-29 16:33:03 +03002393 ctxt->eflags |= X86_EFLAGS_FIXED_BIT;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002394#endif
2395 } else {
2396 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002397 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002398 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002399
Nadav Amit0efb0442015-03-29 16:33:03 +03002400 ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002401 }
2402
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002403 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002404}
2405
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002406static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002407{
Mathias Krause0225fb52012-08-30 01:30:16 +02002408 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002409 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002410 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002411 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002412 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002413
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002414 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002415 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002416 if (ctxt->mode == X86EMUL_MODE_REAL)
2417 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002418
Avi Kivity1a18a692012-02-01 12:23:21 +02002419 /*
2420 * Not recognized on AMD in compat mode (but is recognized in legacy
2421 * mode).
2422 */
Nadav Amitf3747372015-01-01 23:11:11 +02002423 if ((ctxt->mode != X86EMUL_MODE_PROT64) && (efer & EFER_LMA)
Avi Kivity1a18a692012-02-01 12:23:21 +02002424 && !vendor_intel(ctxt))
2425 return emulate_ud(ctxt);
2426
Nadav Amitb2c9d432014-11-02 11:55:01 +02002427 /* sysenter/sysexit have not been tested in 64bit mode. */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002428 if (ctxt->mode == X86EMUL_MODE_PROT64)
Nadav Amitb2c9d432014-11-02 11:55:01 +02002429 return X86EMUL_UNHANDLEABLE;
Andre Przywara8c604352009-06-18 12:56:01 +02002430
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002431 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002432
Avi Kivity717746e2011-04-20 13:37:53 +03002433 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002434 if ((msr_data & 0xfffc) == 0x0)
2435 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002436
Nadav Amit0efb0442015-03-29 16:33:03 +03002437 ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
Nadav Amitb32a9912015-03-29 16:33:04 +03002438 cs_sel = (u16)msr_data & ~SEGMENT_RPL_MASK;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002439 ss_sel = cs_sel + 8;
Nadav Amitf3747372015-01-01 23:11:11 +02002440 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002441 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002442 cs.l = 1;
2443 }
2444
Avi Kivity1aa36612011-04-27 13:20:30 +03002445 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2446 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002447
Avi Kivity717746e2011-04-20 13:37:53 +03002448 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002449 ctxt->_eip = (efer & EFER_LMA) ? msr_data : (u32)msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002450
Avi Kivity717746e2011-04-20 13:37:53 +03002451 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002452 *reg_write(ctxt, VCPU_REGS_RSP) = (efer & EFER_LMA) ? msr_data :
2453 (u32)msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002454
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002455 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002456}
2457
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002458static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002459{
Mathias Krause0225fb52012-08-30 01:30:16 +02002460 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002461 struct desc_struct cs, ss;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002462 u64 msr_data, rcx, rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002463 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002464 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002465
Gleb Natapova0044752010-02-10 14:21:31 +02002466 /* inject #GP if in real mode or Virtual 8086 mode */
2467 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002468 ctxt->mode == X86EMUL_MODE_VM86)
2469 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002470
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002471 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002472
Avi Kivity9dac77f2011-06-01 15:34:25 +03002473 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002474 usermode = X86EMUL_MODE_PROT64;
2475 else
2476 usermode = X86EMUL_MODE_PROT32;
2477
Nadav Amit234f3ce2014-09-18 22:39:38 +03002478 rcx = reg_read(ctxt, VCPU_REGS_RCX);
2479 rdx = reg_read(ctxt, VCPU_REGS_RDX);
2480
Andre Przywara4668f052009-06-18 12:56:02 +02002481 cs.dpl = 3;
2482 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002483 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002484 switch (usermode) {
2485 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002486 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002487 if ((msr_data & 0xfffc) == 0x0)
2488 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002489 ss_sel = (u16)(msr_data + 24);
Nadav Amitbf0b6822014-09-18 22:39:45 +03002490 rcx = (u32)rcx;
2491 rdx = (u32)rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002492 break;
2493 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002494 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002495 if (msr_data == 0x0)
2496 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002497 ss_sel = cs_sel + 8;
2498 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002499 cs.l = 1;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002500 if (is_noncanonical_address(rcx) ||
2501 is_noncanonical_address(rdx))
2502 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002503 break;
2504 }
Nadav Amitb32a9912015-03-29 16:33:04 +03002505 cs_sel |= SEGMENT_RPL_MASK;
2506 ss_sel |= SEGMENT_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002507
Avi Kivity1aa36612011-04-27 13:20:30 +03002508 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2509 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002510
Nadav Amit234f3ce2014-09-18 22:39:38 +03002511 ctxt->_eip = rdx;
2512 *reg_write(ctxt, VCPU_REGS_RSP) = rcx;
Andre Przywara4668f052009-06-18 12:56:02 +02002513
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002514 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002515}
2516
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002517static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002518{
2519 int iopl;
2520 if (ctxt->mode == X86EMUL_MODE_REAL)
2521 return false;
2522 if (ctxt->mode == X86EMUL_MODE_VM86)
2523 return true;
Nadav Amit0efb0442015-03-29 16:33:03 +03002524 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002525 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002526}
2527
2528static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002529 u16 port, u16 len)
2530{
Mathias Krause0225fb52012-08-30 01:30:16 +02002531 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002532 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002533 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002534 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002535 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002536 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002537 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002538
Avi Kivity1aa36612011-04-27 13:20:30 +03002539 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002540 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002541 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002542 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002543 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002544 base = get_desc_base(&tr_seg);
2545#ifdef CONFIG_X86_64
2546 base |= ((u64)base3) << 32;
2547#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002548 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002549 if (r != X86EMUL_CONTINUE)
2550 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002551 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002552 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002553 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002554 if (r != X86EMUL_CONTINUE)
2555 return false;
2556 if ((perm >> bit_idx) & mask)
2557 return false;
2558 return true;
2559}
2560
2561static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002562 u16 port, u16 len)
2563{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002564 if (ctxt->perm_ok)
2565 return true;
2566
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002567 if (emulator_bad_iopl(ctxt))
2568 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002569 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002570
2571 ctxt->perm_ok = true;
2572
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002573 return true;
2574}
2575
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002576static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002577 struct tss_segment_16 *tss)
2578{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002579 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002580 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002581 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2582 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2583 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2584 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2585 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2586 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2587 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2588 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002589
Avi Kivity1aa36612011-04-27 13:20:30 +03002590 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2591 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2592 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2593 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2594 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002595}
2596
2597static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002598 struct tss_segment_16 *tss)
2599{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002600 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002601 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002602
Avi Kivity9dac77f2011-06-01 15:34:25 +03002603 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002604 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002605 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2606 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2607 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2608 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2609 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2610 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2611 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2612 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002613
2614 /*
2615 * SDM says that segment selectors are loaded before segment
2616 * descriptors
2617 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002618 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2619 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2620 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2621 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2622 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002623
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002624 cpl = tss->cs & 3;
2625
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002626 /*
Guo Chaofc058682012-06-28 15:19:51 +08002627 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002628 * it is handled in a context of new task
2629 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002630 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002631 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002632 if (ret != X86EMUL_CONTINUE)
2633 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002634 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002635 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002636 if (ret != X86EMUL_CONTINUE)
2637 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002638 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002639 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002640 if (ret != X86EMUL_CONTINUE)
2641 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002642 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002643 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002644 if (ret != X86EMUL_CONTINUE)
2645 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002646 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002647 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002648 if (ret != X86EMUL_CONTINUE)
2649 return ret;
2650
2651 return X86EMUL_CONTINUE;
2652}
2653
2654static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002655 u16 tss_selector, u16 old_tss_sel,
2656 ulong old_tss_base, struct desc_struct *new_desc)
2657{
Mathias Krause0225fb52012-08-30 01:30:16 +02002658 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002659 struct tss_segment_16 tss_seg;
2660 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002661 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002662
Avi Kivity0f65dd72011-04-20 13:37:53 +03002663 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002664 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002665 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002666 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002667
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002668 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002669
Avi Kivity0f65dd72011-04-20 13:37:53 +03002670 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002671 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002672 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002673 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002674
Avi Kivity0f65dd72011-04-20 13:37:53 +03002675 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002676 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002677 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002678 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002679
2680 if (old_tss_sel != 0xffff) {
2681 tss_seg.prev_task_link = old_tss_sel;
2682
Avi Kivity0f65dd72011-04-20 13:37:53 +03002683 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002684 &tss_seg.prev_task_link,
2685 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002686 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002687 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002688 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002689 }
2690
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002691 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002692}
2693
2694static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002695 struct tss_segment_32 *tss)
2696{
Nadav Amit5c7411e2014-04-07 18:37:47 +03002697 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002698 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002699 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002700 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2701 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2702 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2703 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2704 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2705 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2706 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2707 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002708
Avi Kivity1aa36612011-04-27 13:20:30 +03002709 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2710 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2711 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2712 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2713 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2714 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002715}
2716
2717static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002718 struct tss_segment_32 *tss)
2719{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002720 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002721 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002722
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002723 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002724 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002725 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002726 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002727
2728 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002729 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2730 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2731 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2732 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2733 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2734 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2735 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2736 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002737
2738 /*
2739 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002740 * descriptors. This is important because CPL checks will
2741 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002742 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002743 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2744 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2745 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2746 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2747 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2748 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2749 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002750
2751 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002752 * If we're switching between Protected Mode and VM86, we need to make
2753 * sure to update the mode before loading the segment descriptors so
2754 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01002755 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002756 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002757 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002758 cpl = 3;
2759 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002760 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002761 cpl = tss->cs & 3;
2762 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01002763
2764 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002765 * Now load segment descriptors. If fault happenes at this stage
2766 * it is handled in a context of new task
2767 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002768 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002769 cpl, X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002770 if (ret != X86EMUL_CONTINUE)
2771 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002772 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002773 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002774 if (ret != X86EMUL_CONTINUE)
2775 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002776 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002777 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002778 if (ret != X86EMUL_CONTINUE)
2779 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002780 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002781 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002782 if (ret != X86EMUL_CONTINUE)
2783 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002784 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002785 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002786 if (ret != X86EMUL_CONTINUE)
2787 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002788 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002789 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002790 if (ret != X86EMUL_CONTINUE)
2791 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002792 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002793 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002794
Eugene Korenevsky2f729b12015-03-29 01:27:17 +03002795 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002796}
2797
2798static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002799 u16 tss_selector, u16 old_tss_sel,
2800 ulong old_tss_base, struct desc_struct *new_desc)
2801{
Mathias Krause0225fb52012-08-30 01:30:16 +02002802 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002803 struct tss_segment_32 tss_seg;
2804 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002805 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03002806 u32 eip_offset = offsetof(struct tss_segment_32, eip);
2807 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002808
Avi Kivity0f65dd72011-04-20 13:37:53 +03002809 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002810 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002811 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002812 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002813
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002814 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002815
Nadav Amit5c7411e2014-04-07 18:37:47 +03002816 /* Only GP registers and segment selectors are saved */
2817 ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
2818 ldt_sel_offset - eip_offset, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002819 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002820 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002821
Avi Kivity0f65dd72011-04-20 13:37:53 +03002822 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002823 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002824 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002825 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002826
2827 if (old_tss_sel != 0xffff) {
2828 tss_seg.prev_task_link = old_tss_sel;
2829
Avi Kivity0f65dd72011-04-20 13:37:53 +03002830 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002831 &tss_seg.prev_task_link,
2832 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002833 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002834 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002835 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002836 }
2837
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002838 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002839}
2840
2841static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002842 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002843 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002844{
Mathias Krause0225fb52012-08-30 01:30:16 +02002845 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002846 struct desc_struct curr_tss_desc, next_tss_desc;
2847 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002848 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002849 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002850 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002851 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002852 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002853
2854 /* FIXME: old_tss_base == ~0 ? */
2855
Avi Kivitye9194642012-06-13 16:29:39 +03002856 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002857 if (ret != X86EMUL_CONTINUE)
2858 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002859 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002860 if (ret != X86EMUL_CONTINUE)
2861 return ret;
2862
2863 /* FIXME: check that next_tss_desc is tss */
2864
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002865 /*
2866 * Check privileges. The three cases are task switch caused by...
2867 *
2868 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2869 * 2. Exception/IRQ/iret: No check is performed
Nadav Amit2c2ca2d2014-11-02 11:54:57 +02002870 * 3. jmp/call to TSS/task-gate: No check is performed since the
2871 * hardware checks it before exiting.
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002872 */
2873 if (reason == TASK_SWITCH_GATE) {
2874 if (idt_index != -1) {
2875 /* Software interrupts */
2876 struct desc_struct task_gate_desc;
2877 int dpl;
2878
2879 ret = read_interrupt_descriptor(ctxt, idt_index,
2880 &task_gate_desc);
2881 if (ret != X86EMUL_CONTINUE)
2882 return ret;
2883
2884 dpl = task_gate_desc.dpl;
2885 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2886 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2887 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002888 }
2889
Gleb Natapovceffb452010-03-18 15:20:19 +02002890 desc_limit = desc_limit_scaled(&next_tss_desc);
2891 if (!next_tss_desc.p ||
2892 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2893 desc_limit < 0x2b)) {
Paolo Bonzini592f0852014-08-20 10:05:08 +02002894 return emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002895 }
2896
2897 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2898 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002899 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002900 }
2901
2902 if (reason == TASK_SWITCH_IRET)
2903 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2904
2905 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002906 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002907 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2908 old_tss_sel = 0xffff;
2909
2910 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002911 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002912 old_tss_base, &next_tss_desc);
2913 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002914 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002915 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002916 if (ret != X86EMUL_CONTINUE)
2917 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002918
2919 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2920 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2921
2922 if (reason != TASK_SWITCH_IRET) {
2923 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002924 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002925 }
2926
Avi Kivity717746e2011-04-20 13:37:53 +03002927 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002928 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002929
Jan Kiszkae269fb22010-04-14 15:51:09 +02002930 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002931 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2932 ctxt->lock_prefix = 0;
2933 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002934 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002935 }
2936
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002937 return ret;
2938}
2939
2940int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002941 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002942 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002943{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002944 int rc;
2945
Avi Kivitydd856ef2012-08-27 23:46:17 +03002946 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002947 ctxt->_eip = ctxt->eip;
2948 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002949
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002950 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002951 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002952
Avi Kivitydd856ef2012-08-27 23:46:17 +03002953 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002954 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002955 writeback_registers(ctxt);
2956 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002957
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002958 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002959}
2960
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002961static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2962 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002963{
Nadav Amit0efb0442015-03-29 16:33:03 +03002964 int df = (ctxt->eflags & X86_EFLAGS_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002965
Paolo Bonzini01485a22014-11-19 18:25:08 +01002966 register_address_increment(ctxt, reg, df * op->bytes);
2967 op->addr.mem.ea = register_address(ctxt, reg);
Gleb Natapova682e352010-03-18 15:20:21 +02002968}
2969
Avi Kivity7af04fc2010-08-18 14:16:35 +03002970static int em_das(struct x86_emulate_ctxt *ctxt)
2971{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002972 u8 al, old_al;
2973 bool af, cf, old_cf;
2974
2975 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002976 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002977
2978 old_al = al;
2979 old_cf = cf;
2980 cf = false;
2981 af = ctxt->eflags & X86_EFLAGS_AF;
2982 if ((al & 0x0f) > 9 || af) {
2983 al -= 6;
2984 cf = old_cf | (al >= 250);
2985 af = true;
2986 } else {
2987 af = false;
2988 }
2989 if (old_al > 0x99 || old_cf) {
2990 al -= 0x60;
2991 cf = true;
2992 }
2993
Avi Kivity9dac77f2011-06-01 15:34:25 +03002994 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002995 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002996 ctxt->src.type = OP_IMM;
2997 ctxt->src.val = 0;
2998 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02002999 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03003000 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
3001 if (cf)
3002 ctxt->eflags |= X86_EFLAGS_CF;
3003 if (af)
3004 ctxt->eflags |= X86_EFLAGS_AF;
3005 return X86EMUL_CONTINUE;
3006}
3007
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003008static int em_aam(struct x86_emulate_ctxt *ctxt)
3009{
3010 u8 al, ah;
3011
3012 if (ctxt->src.val == 0)
3013 return emulate_de(ctxt);
3014
3015 al = ctxt->dst.val & 0xff;
3016 ah = al / ctxt->src.val;
3017 al %= ctxt->src.val;
3018
3019 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
3020
3021 /* Set PF, ZF, SF */
3022 ctxt->src.type = OP_IMM;
3023 ctxt->src.val = 0;
3024 ctxt->src.bytes = 1;
3025 fastop(ctxt, em_or);
3026
3027 return X86EMUL_CONTINUE;
3028}
3029
Gleb Natapov7f662272012-12-10 11:42:30 +02003030static int em_aad(struct x86_emulate_ctxt *ctxt)
3031{
3032 u8 al = ctxt->dst.val & 0xff;
3033 u8 ah = (ctxt->dst.val >> 8) & 0xff;
3034
3035 al = (al + (ah * ctxt->src.val)) & 0xff;
3036
3037 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
3038
Gleb Natapovf583c292013-02-13 17:50:39 +02003039 /* Set PF, ZF, SF */
3040 ctxt->src.type = OP_IMM;
3041 ctxt->src.val = 0;
3042 ctxt->src.bytes = 1;
3043 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02003044
3045 return X86EMUL_CONTINUE;
3046}
3047
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003048static int em_call(struct x86_emulate_ctxt *ctxt)
3049{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003050 int rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003051 long rel = ctxt->src.val;
3052
3053 ctxt->src.val = (unsigned long)ctxt->_eip;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003054 rc = jmp_rel(ctxt, rel);
3055 if (rc != X86EMUL_CONTINUE)
3056 return rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003057 return em_push(ctxt);
3058}
3059
Avi Kivity0ef753b2010-08-18 14:51:45 +03003060static int em_call_far(struct x86_emulate_ctxt *ctxt)
3061{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003062 u16 sel, old_cs;
3063 ulong old_eip;
3064 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03003065 struct desc_struct old_desc, new_desc;
3066 const struct x86_emulate_ops *ops = ctxt->ops;
3067 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amit82268082015-01-26 09:32:27 +02003068 enum x86emul_mode prev_mode = ctxt->mode;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003069
Avi Kivity9dac77f2011-06-01 15:34:25 +03003070 old_eip = ctxt->_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003071 ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003072
Avi Kivity9dac77f2011-06-01 15:34:25 +03003073 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003074 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
3075 X86_TRANSFER_CALL_JMP, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003076 if (rc != X86EMUL_CONTINUE)
Nadav Amit80976db2014-12-25 02:52:20 +02003077 return rc;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003078
Nadav Amitd50eaa12014-11-19 17:43:11 +02003079 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003080 if (rc != X86EMUL_CONTINUE)
3081 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003082
Avi Kivity9dac77f2011-06-01 15:34:25 +03003083 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003084 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003085 if (rc != X86EMUL_CONTINUE)
Nadav Amitd1442d82014-09-18 22:39:39 +03003086 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003087
Avi Kivity9dac77f2011-06-01 15:34:25 +03003088 ctxt->src.val = old_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003089 rc = em_push(ctxt);
3090 /* If we failed, we tainted the memory, but the very least we should
3091 restore cs */
Nadav Amit82268082015-01-26 09:32:27 +02003092 if (rc != X86EMUL_CONTINUE) {
3093 pr_warn_once("faulting far call emulation tainted memory\n");
Nadav Amitd1442d82014-09-18 22:39:39 +03003094 goto fail;
Nadav Amit82268082015-01-26 09:32:27 +02003095 }
Nadav Amitd1442d82014-09-18 22:39:39 +03003096 return rc;
3097fail:
3098 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
Nadav Amit82268082015-01-26 09:32:27 +02003099 ctxt->mode = prev_mode;
Nadav Amitd1442d82014-09-18 22:39:39 +03003100 return rc;
3101
Avi Kivity0ef753b2010-08-18 14:51:45 +03003102}
3103
Avi Kivity40ece7c2010-08-18 15:12:09 +03003104static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3105{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003106 int rc;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003107 unsigned long eip;
Avi Kivity40ece7c2010-08-18 15:12:09 +03003108
Nadav Amit234f3ce2014-09-18 22:39:38 +03003109 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
3110 if (rc != X86EMUL_CONTINUE)
3111 return rc;
3112 rc = assign_eip_near(ctxt, eip);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003113 if (rc != X86EMUL_CONTINUE)
3114 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003115 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003116 return X86EMUL_CONTINUE;
3117}
3118
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003119static int em_xchg(struct x86_emulate_ctxt *ctxt)
3120{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003121 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003122 ctxt->src.val = ctxt->dst.val;
3123 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003124
3125 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003126 ctxt->dst.val = ctxt->src.orig_val;
3127 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003128 return X86EMUL_CONTINUE;
3129}
3130
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003131static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3132{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003133 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003134 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003135}
3136
Avi Kivity61429142010-08-19 15:13:00 +03003137static int em_cwd(struct x86_emulate_ctxt *ctxt)
3138{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003139 ctxt->dst.type = OP_REG;
3140 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003141 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003142 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003143
3144 return X86EMUL_CONTINUE;
3145}
3146
Avi Kivity48bb5d32010-08-18 18:54:34 +03003147static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3148{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003149 u64 tsc = 0;
3150
Avi Kivity717746e2011-04-20 13:37:53 +03003151 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003152 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3153 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003154 return X86EMUL_CONTINUE;
3155}
3156
Avi Kivity222d21a2011-11-10 14:57:30 +02003157static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3158{
3159 u64 pmc;
3160
Avi Kivitydd856ef2012-08-27 23:46:17 +03003161 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003162 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003163 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3164 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003165 return X86EMUL_CONTINUE;
3166}
3167
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003168static int em_mov(struct x86_emulate_ctxt *ctxt)
3169{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01003170 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003171 return X86EMUL_CONTINUE;
3172}
3173
Borislav Petkov84cffe42013-10-29 12:54:56 +01003174#define FFL(x) bit(X86_FEATURE_##x)
3175
3176static int em_movbe(struct x86_emulate_ctxt *ctxt)
3177{
3178 u32 ebx, ecx, edx, eax = 1;
3179 u16 tmp;
3180
3181 /*
3182 * Check MOVBE is set in the guest-visible CPUID leaf.
3183 */
3184 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
3185 if (!(ecx & FFL(MOVBE)))
3186 return emulate_ud(ctxt);
3187
3188 switch (ctxt->op_bytes) {
3189 case 2:
3190 /*
3191 * From MOVBE definition: "...When the operand size is 16 bits,
3192 * the upper word of the destination register remains unchanged
3193 * ..."
3194 *
3195 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3196 * rules so we have to do the operation almost per hand.
3197 */
3198 tmp = (u16)ctxt->src.val;
3199 ctxt->dst.val &= ~0xffffUL;
3200 ctxt->dst.val |= (unsigned long)swab16(tmp);
3201 break;
3202 case 4:
3203 ctxt->dst.val = swab32((u32)ctxt->src.val);
3204 break;
3205 case 8:
3206 ctxt->dst.val = swab64(ctxt->src.val);
3207 break;
3208 default:
Paolo Bonzini592f0852014-08-20 10:05:08 +02003209 BUG();
Borislav Petkov84cffe42013-10-29 12:54:56 +01003210 }
3211 return X86EMUL_CONTINUE;
3212}
3213
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003214static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3215{
3216 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3217 return emulate_gp(ctxt, 0);
3218
3219 /* Disable writeback. */
3220 ctxt->dst.type = OP_NONE;
3221 return X86EMUL_CONTINUE;
3222}
3223
3224static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3225{
3226 unsigned long val;
3227
3228 if (ctxt->mode == X86EMUL_MODE_PROT64)
3229 val = ctxt->src.val & ~0ULL;
3230 else
3231 val = ctxt->src.val & ~0U;
3232
3233 /* #UD condition is already handled. */
3234 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3235 return emulate_gp(ctxt, 0);
3236
3237 /* Disable writeback. */
3238 ctxt->dst.type = OP_NONE;
3239 return X86EMUL_CONTINUE;
3240}
3241
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003242static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3243{
3244 u64 msr_data;
3245
Avi Kivitydd856ef2012-08-27 23:46:17 +03003246 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3247 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3248 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003249 return emulate_gp(ctxt, 0);
3250
3251 return X86EMUL_CONTINUE;
3252}
3253
3254static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3255{
3256 u64 msr_data;
3257
Avi Kivitydd856ef2012-08-27 23:46:17 +03003258 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003259 return emulate_gp(ctxt, 0);
3260
Avi Kivitydd856ef2012-08-27 23:46:17 +03003261 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3262 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003263 return X86EMUL_CONTINUE;
3264}
3265
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003266static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3267{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003268 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003269 return emulate_ud(ctxt);
3270
Avi Kivity9dac77f2011-06-01 15:34:25 +03003271 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Nadav Amitb5bbf102014-11-02 11:54:46 +02003272 if (ctxt->dst.bytes == 4 && ctxt->dst.type == OP_MEM)
3273 ctxt->dst.bytes = 2;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003274 return X86EMUL_CONTINUE;
3275}
3276
3277static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3278{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003279 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003280
Avi Kivity9dac77f2011-06-01 15:34:25 +03003281 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003282 return emulate_ud(ctxt);
3283
Avi Kivity9dac77f2011-06-01 15:34:25 +03003284 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003285 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3286
3287 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003288 ctxt->dst.type = OP_NONE;
3289 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003290}
3291
Avi Kivitya14e5792012-06-13 12:28:33 +03003292static int em_lldt(struct x86_emulate_ctxt *ctxt)
3293{
3294 u16 sel = ctxt->src.val;
3295
3296 /* Disable writeback. */
3297 ctxt->dst.type = OP_NONE;
3298 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3299}
3300
Avi Kivity80890002012-06-13 16:33:29 +03003301static int em_ltr(struct x86_emulate_ctxt *ctxt)
3302{
3303 u16 sel = ctxt->src.val;
3304
3305 /* Disable writeback. */
3306 ctxt->dst.type = OP_NONE;
3307 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3308}
3309
Avi Kivity38503912011-03-31 18:48:09 +02003310static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3311{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003312 int rc;
3313 ulong linear;
3314
Avi Kivity9dac77f2011-06-01 15:34:25 +03003315 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003316 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003317 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003318 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003319 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003320 return X86EMUL_CONTINUE;
3321}
3322
Avi Kivity2d04a052011-04-20 15:32:49 +03003323static int em_clts(struct x86_emulate_ctxt *ctxt)
3324{
3325 ulong cr0;
3326
3327 cr0 = ctxt->ops->get_cr(ctxt, 0);
3328 cr0 &= ~X86_CR0_TS;
3329 ctxt->ops->set_cr(ctxt, 0, cr0);
3330 return X86EMUL_CONTINUE;
3331}
3332
Jan Kiszkab34a8052015-03-09 20:27:43 +01003333static int em_hypercall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003334{
Nadav Amit0f54a322014-08-29 11:26:55 +03003335 int rc = ctxt->ops->fix_hypercall(ctxt);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003336
Avi Kivity26d05cc2011-04-21 12:07:59 +03003337 if (rc != X86EMUL_CONTINUE)
3338 return rc;
3339
3340 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003341 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003342 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003343 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003344 return X86EMUL_CONTINUE;
3345}
3346
Avi Kivity96051572012-06-10 17:21:18 +03003347static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3348 void (*get)(struct x86_emulate_ctxt *ctxt,
3349 struct desc_ptr *ptr))
3350{
3351 struct desc_ptr desc_ptr;
3352
3353 if (ctxt->mode == X86EMUL_MODE_PROT64)
3354 ctxt->op_bytes = 8;
3355 get(ctxt, &desc_ptr);
3356 if (ctxt->op_bytes == 2) {
3357 ctxt->op_bytes = 4;
3358 desc_ptr.address &= 0x00ffffff;
3359 }
3360 /* Disable writeback. */
3361 ctxt->dst.type = OP_NONE;
3362 return segmented_write(ctxt, ctxt->dst.addr.mem,
3363 &desc_ptr, 2 + ctxt->op_bytes);
3364}
3365
3366static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3367{
3368 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3369}
3370
3371static int em_sidt(struct x86_emulate_ctxt *ctxt)
3372{
3373 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3374}
3375
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003376static int em_lgdt_lidt(struct x86_emulate_ctxt *ctxt, bool lgdt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003377{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003378 struct desc_ptr desc_ptr;
3379 int rc;
3380
Avi Kivity510425f2012-06-07 17:04:36 +03003381 if (ctxt->mode == X86EMUL_MODE_PROT64)
3382 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003383 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003384 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003385 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003386 if (rc != X86EMUL_CONTINUE)
3387 return rc;
Nadav Amit9a9abf62014-11-02 11:54:56 +02003388 if (ctxt->mode == X86EMUL_MODE_PROT64 &&
3389 is_noncanonical_address(desc_ptr.address))
3390 return emulate_gp(ctxt, 0);
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003391 if (lgdt)
3392 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3393 else
3394 ctxt->ops->set_idt(ctxt, &desc_ptr);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003395 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003396 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003397 return X86EMUL_CONTINUE;
3398}
3399
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003400static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3401{
3402 return em_lgdt_lidt(ctxt, true);
3403}
3404
Avi Kivity26d05cc2011-04-21 12:07:59 +03003405static int em_lidt(struct x86_emulate_ctxt *ctxt)
3406{
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003407 return em_lgdt_lidt(ctxt, false);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003408}
3409
3410static int em_smsw(struct x86_emulate_ctxt *ctxt)
3411{
Nadav Amit32e94d02014-06-02 18:34:11 +03003412 if (ctxt->dst.type == OP_MEM)
3413 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003414 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003415 return X86EMUL_CONTINUE;
3416}
3417
3418static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3419{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003420 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003421 | (ctxt->src.val & 0x0f));
3422 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003423 return X86EMUL_CONTINUE;
3424}
3425
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003426static int em_loop(struct x86_emulate_ctxt *ctxt)
3427{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003428 int rc = X86EMUL_CONTINUE;
3429
Paolo Bonzini01485a22014-11-19 18:25:08 +01003430 register_address_increment(ctxt, VCPU_REGS_RCX, -1);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003431 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003432 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
Nadav Amit234f3ce2014-09-18 22:39:38 +03003433 rc = jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003434
Nadav Amit234f3ce2014-09-18 22:39:38 +03003435 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003436}
3437
3438static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3439{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003440 int rc = X86EMUL_CONTINUE;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003441
Nadav Amit234f3ce2014-09-18 22:39:38 +03003442 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
3443 rc = jmp_rel(ctxt, ctxt->src.val);
3444
3445 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003446}
3447
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003448static int em_in(struct x86_emulate_ctxt *ctxt)
3449{
3450 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3451 &ctxt->dst.val))
3452 return X86EMUL_IO_NEEDED;
3453
3454 return X86EMUL_CONTINUE;
3455}
3456
3457static int em_out(struct x86_emulate_ctxt *ctxt)
3458{
3459 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3460 &ctxt->src.val, 1);
3461 /* Disable writeback. */
3462 ctxt->dst.type = OP_NONE;
3463 return X86EMUL_CONTINUE;
3464}
3465
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003466static int em_cli(struct x86_emulate_ctxt *ctxt)
3467{
3468 if (emulator_bad_iopl(ctxt))
3469 return emulate_gp(ctxt, 0);
3470
3471 ctxt->eflags &= ~X86_EFLAGS_IF;
3472 return X86EMUL_CONTINUE;
3473}
3474
3475static int em_sti(struct x86_emulate_ctxt *ctxt)
3476{
3477 if (emulator_bad_iopl(ctxt))
3478 return emulate_gp(ctxt, 0);
3479
3480 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3481 ctxt->eflags |= X86_EFLAGS_IF;
3482 return X86EMUL_CONTINUE;
3483}
3484
Avi Kivity6d6eede2012-06-07 14:11:36 +03003485static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3486{
3487 u32 eax, ebx, ecx, edx;
3488
Avi Kivitydd856ef2012-08-27 23:46:17 +03003489 eax = reg_read(ctxt, VCPU_REGS_RAX);
3490 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003491 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003492 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3493 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3494 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3495 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003496 return X86EMUL_CONTINUE;
3497}
3498
Paolo Bonzini98f73632013-10-31 11:19:42 +01003499static int em_sahf(struct x86_emulate_ctxt *ctxt)
3500{
3501 u32 flags;
3502
Nadav Amit0efb0442015-03-29 16:33:03 +03003503 flags = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
3504 X86_EFLAGS_SF;
Paolo Bonzini98f73632013-10-31 11:19:42 +01003505 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3506
3507 ctxt->eflags &= ~0xffUL;
3508 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3509 return X86EMUL_CONTINUE;
3510}
3511
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003512static int em_lahf(struct x86_emulate_ctxt *ctxt)
3513{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003514 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3515 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003516 return X86EMUL_CONTINUE;
3517}
3518
Avi Kivity92998362012-06-13 12:25:06 +03003519static int em_bswap(struct x86_emulate_ctxt *ctxt)
3520{
3521 switch (ctxt->op_bytes) {
3522#ifdef CONFIG_X86_64
3523 case 8:
3524 asm("bswap %0" : "+r"(ctxt->dst.val));
3525 break;
3526#endif
3527 default:
3528 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3529 break;
3530 }
3531 return X86EMUL_CONTINUE;
3532}
3533
Nadav Amit13e457e2014-10-13 13:04:13 +03003534static int em_clflush(struct x86_emulate_ctxt *ctxt)
3535{
3536 /* emulating clflush regardless of cpuid */
3537 return X86EMUL_CONTINUE;
3538}
3539
Nadav Amit2276b512015-01-26 09:32:24 +02003540static int em_movsxd(struct x86_emulate_ctxt *ctxt)
3541{
3542 ctxt->dst.val = (s32) ctxt->src.val;
3543 return X86EMUL_CONTINUE;
3544}
3545
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003546static bool valid_cr(int nr)
3547{
3548 switch (nr) {
3549 case 0:
3550 case 2 ... 4:
3551 case 8:
3552 return true;
3553 default:
3554 return false;
3555 }
3556}
3557
3558static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3559{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003560 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003561 return emulate_ud(ctxt);
3562
3563 return X86EMUL_CONTINUE;
3564}
3565
3566static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3567{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003568 u64 new_val = ctxt->src.val64;
3569 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003570 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003571
3572 static u64 cr_reserved_bits[] = {
3573 0xffffffff00000000ULL,
3574 0, 0, 0, /* CR3 checked later */
3575 CR4_RESERVED_BITS,
3576 0, 0, 0,
3577 CR8_RESERVED_BITS,
3578 };
3579
3580 if (!valid_cr(cr))
3581 return emulate_ud(ctxt);
3582
3583 if (new_val & cr_reserved_bits[cr])
3584 return emulate_gp(ctxt, 0);
3585
3586 switch (cr) {
3587 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003588 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003589 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3590 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3591 return emulate_gp(ctxt, 0);
3592
Avi Kivity717746e2011-04-20 13:37:53 +03003593 cr4 = ctxt->ops->get_cr(ctxt, 4);
3594 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003595
3596 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3597 !(cr4 & X86_CR4_PAE))
3598 return emulate_gp(ctxt, 0);
3599
3600 break;
3601 }
3602 case 3: {
3603 u64 rsvd = 0;
3604
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003605 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3606 if (efer & EFER_LMA)
Nadav Amit9d88fca2014-11-02 11:54:52 +02003607 rsvd = CR3_L_MODE_RESERVED_BITS & ~CR3_PCID_INVD;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003608
3609 if (new_val & rsvd)
3610 return emulate_gp(ctxt, 0);
3611
3612 break;
3613 }
3614 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003615 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003616
3617 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3618 return emulate_gp(ctxt, 0);
3619
3620 break;
3621 }
3622 }
3623
3624 return X86EMUL_CONTINUE;
3625}
3626
Joerg Roedel3b88e412011-04-04 12:39:29 +02003627static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3628{
3629 unsigned long dr7;
3630
Avi Kivity717746e2011-04-20 13:37:53 +03003631 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003632
3633 /* Check if DR7.Global_Enable is set */
3634 return dr7 & (1 << 13);
3635}
3636
3637static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3638{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003639 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003640 u64 cr4;
3641
3642 if (dr > 7)
3643 return emulate_ud(ctxt);
3644
Avi Kivity717746e2011-04-20 13:37:53 +03003645 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003646 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3647 return emulate_ud(ctxt);
3648
Nadav Amit6d2a0522014-11-02 11:54:43 +02003649 if (check_dr7_gd(ctxt)) {
3650 ulong dr6;
3651
3652 ctxt->ops->get_dr(ctxt, 6, &dr6);
3653 dr6 &= ~15;
3654 dr6 |= DR6_BD | DR6_RTM;
3655 ctxt->ops->set_dr(ctxt, 6, dr6);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003656 return emulate_db(ctxt);
Nadav Amit6d2a0522014-11-02 11:54:43 +02003657 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02003658
3659 return X86EMUL_CONTINUE;
3660}
3661
3662static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3663{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003664 u64 new_val = ctxt->src.val64;
3665 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003666
3667 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3668 return emulate_gp(ctxt, 0);
3669
3670 return check_dr_read(ctxt);
3671}
3672
Joerg Roedel01de8b02011-04-04 12:39:31 +02003673static int check_svme(struct x86_emulate_ctxt *ctxt)
3674{
3675 u64 efer;
3676
Avi Kivity717746e2011-04-20 13:37:53 +03003677 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003678
3679 if (!(efer & EFER_SVME))
3680 return emulate_ud(ctxt);
3681
3682 return X86EMUL_CONTINUE;
3683}
3684
3685static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3686{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003687 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003688
3689 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003690 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003691 return emulate_gp(ctxt, 0);
3692
3693 return check_svme(ctxt);
3694}
3695
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003696static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3697{
Avi Kivity717746e2011-04-20 13:37:53 +03003698 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003699
Avi Kivity717746e2011-04-20 13:37:53 +03003700 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003701 return emulate_ud(ctxt);
3702
3703 return X86EMUL_CONTINUE;
3704}
3705
Joerg Roedel80612522011-04-04 12:39:33 +02003706static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3707{
Avi Kivity717746e2011-04-20 13:37:53 +03003708 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003709 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003710
Avi Kivity717746e2011-04-20 13:37:53 +03003711 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03003712 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02003713 return emulate_gp(ctxt, 0);
3714
3715 return X86EMUL_CONTINUE;
3716}
3717
Joerg Roedelf6511932011-04-04 12:39:35 +02003718static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3719{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003720 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3721 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003722 return emulate_gp(ctxt, 0);
3723
3724 return X86EMUL_CONTINUE;
3725}
3726
3727static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3728{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003729 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3730 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003731 return emulate_gp(ctxt, 0);
3732
3733 return X86EMUL_CONTINUE;
3734}
3735
Avi Kivity73fba5f2010-07-29 15:11:53 +03003736#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003737#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
3738#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
3739 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003740#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003741#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003742#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3743#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Nadav Amit39f062f2014-11-26 15:47:18 +02003744#define ID(_f, _i) { .flags = ((_f) | InstrDual | ModRM), .u.idual = (_i) }
Nadav Amit2276b512015-01-26 09:32:24 +02003745#define MD(_f, _m) { .flags = ((_f) | ModeDual), .u.mdual = (_m) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003746#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003747#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003748#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003749#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003750 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003751#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003752 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
3753 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003754#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003755
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003756#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003757#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003758#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003759#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003760#define I2bvIP(_f, _e, _i, _p) \
3761 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003762
Avi Kivityfb864fb2013-01-04 16:18:54 +02003763#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3764 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3765 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003766
Nadav Amit0f54a322014-08-29 11:26:55 +03003767static const struct opcode group7_rm0[] = {
3768 N,
Jan Kiszkab34a8052015-03-09 20:27:43 +01003769 I(SrcNone | Priv | EmulateOnUD, em_hypercall),
Nadav Amit0f54a322014-08-29 11:26:55 +03003770 N, N, N, N, N, N,
3771};
3772
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003773static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003774 DI(SrcNone | Priv, monitor),
3775 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003776 N, N, N, N, N, N,
3777};
3778
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003779static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003780 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Jan Kiszkab34a8052015-03-09 20:27:43 +01003781 II(SrcNone | Prot | EmulateOnUD, em_hypercall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003782 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3783 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3784 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3785 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3786 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3787 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003788};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003789
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003790static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003791 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003792 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003793 N, N, N, N, N, N,
3794};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003795
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003796static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003797 F(Lock, em_add),
3798 F(Lock | PageTable, em_or),
3799 F(Lock, em_adc),
3800 F(Lock, em_sbb),
3801 F(Lock | PageTable, em_and),
3802 F(Lock, em_sub),
3803 F(Lock, em_xor),
3804 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003805};
3806
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003807static const struct opcode group1A[] = {
Nadav Amitab708092014-12-25 02:52:21 +02003808 I(DstMem | SrcNone | Mov | Stack | IncSP, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003809};
3810
Avi Kivity007a3b52013-01-19 19:51:51 +02003811static const struct opcode group2[] = {
3812 F(DstMem | ModRM, em_rol),
3813 F(DstMem | ModRM, em_ror),
3814 F(DstMem | ModRM, em_rcl),
3815 F(DstMem | ModRM, em_rcr),
3816 F(DstMem | ModRM, em_shl),
3817 F(DstMem | ModRM, em_shr),
3818 F(DstMem | ModRM, em_shl),
3819 F(DstMem | ModRM, em_sar),
3820};
3821
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003822static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003823 F(DstMem | SrcImm | NoWrite, em_test),
3824 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003825 F(DstMem | SrcNone | Lock, em_not),
3826 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02003827 F(DstXacc | Src2Mem, em_mul_ex),
3828 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02003829 F(DstXacc | Src2Mem, em_div_ex),
3830 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003831};
3832
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003833static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003834 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3835 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003836 N, N, N, N, N, N,
3837};
3838
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003839static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003840 F(DstMem | SrcNone | Lock, em_inc),
3841 F(DstMem | SrcNone | Lock, em_dec),
Nadav Amit58b70752014-10-24 11:35:09 +03003842 I(SrcMem | NearBranch, em_call_near_abs),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003843 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
Nadav Amit58b70752014-10-24 11:35:09 +03003844 I(SrcMem | NearBranch, em_jmp_abs),
Nadav Amitf7784042014-09-18 22:39:41 +03003845 I(SrcMemFAddr | ImplicitOps, em_jmp_far),
3846 I(SrcMem | Stack, em_push), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003847};
3848
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003849static const struct opcode group6[] = {
Nadav Amit63ea0a42015-01-08 11:59:03 +01003850 DI(Prot | DstMem, sldt),
3851 DI(Prot | DstMem, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003852 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003853 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003854 N, N, N, N,
3855};
3856
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003857static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03003858 II(Mov | DstMem, em_sgdt, sgdt),
3859 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003860 II(SrcMem | Priv, em_lgdt, lgdt),
3861 II(SrcMem | Priv, em_lidt, lidt),
3862 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3863 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3864 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003865}, {
Nadav Amit0f54a322014-08-29 11:26:55 +03003866 EXT(0, group7_rm0),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003867 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003868 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003869 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3870 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3871 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003872} };
3873
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003874static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003875 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003876 F(DstMem | SrcImmByte | NoWrite, em_bt),
3877 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3878 F(DstMem | SrcImmByte | Lock, em_btr),
3879 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003880};
3881
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003882static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003883 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003884}, {
3885 N, N, N, N, N, N, N, N,
3886} };
3887
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003888static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003889 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003890 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003891};
3892
Nadav Amit13e457e2014-10-13 13:04:13 +03003893static const struct gprefix pfx_0f_ae_7 = {
Nadav Amit3f6f1482014-10-13 13:04:14 +03003894 I(SrcMem | ByteOp, em_clflush), N, N, N,
Nadav Amit13e457e2014-10-13 13:04:13 +03003895};
3896
3897static const struct group_dual group15 = { {
3898 N, N, N, N, N, N, N, GP(0, &pfx_0f_ae_7),
3899}, {
3900 N, N, N, N, N, N, N, N,
3901} };
3902
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003903static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003904 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003905};
3906
Nadav Amit39f062f2014-11-26 15:47:18 +02003907static const struct instr_dual instr_dual_0f_2b = {
3908 I(0, em_mov), N
3909};
3910
Paolo Bonzinid5b77062014-07-14 12:54:48 +02003911static const struct gprefix pfx_0f_2b = {
Nadav Amit39f062f2014-11-26 15:47:18 +02003912 ID(0, &instr_dual_0f_2b), ID(0, &instr_dual_0f_2b), N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003913};
3914
Igor Mammedov27ce8252014-03-15 21:01:59 +01003915static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01003916 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003917};
3918
Alex Williamson0a370272014-07-11 11:56:31 -06003919static const struct gprefix pfx_0f_e7 = {
3920 N, I(Sse, em_mov), N, N,
3921};
3922
Gleb Natapov045a2822012-12-20 16:57:43 +02003923static const struct escape escape_d9 = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02003924 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstcw),
Gleb Natapov045a2822012-12-20 16:57:43 +02003925}, {
3926 /* 0xC0 - 0xC7 */
3927 N, N, N, N, N, N, N, N,
3928 /* 0xC8 - 0xCF */
3929 N, N, N, N, N, N, N, N,
3930 /* 0xD0 - 0xC7 */
3931 N, N, N, N, N, N, N, N,
3932 /* 0xD8 - 0xDF */
3933 N, N, N, N, N, N, N, N,
3934 /* 0xE0 - 0xE7 */
3935 N, N, N, N, N, N, N, N,
3936 /* 0xE8 - 0xEF */
3937 N, N, N, N, N, N, N, N,
3938 /* 0xF0 - 0xF7 */
3939 N, N, N, N, N, N, N, N,
3940 /* 0xF8 - 0xFF */
3941 N, N, N, N, N, N, N, N,
3942} };
3943
3944static const struct escape escape_db = { {
3945 N, N, N, N, N, N, N, N,
3946}, {
3947 /* 0xC0 - 0xC7 */
3948 N, N, N, N, N, N, N, N,
3949 /* 0xC8 - 0xCF */
3950 N, N, N, N, N, N, N, N,
3951 /* 0xD0 - 0xC7 */
3952 N, N, N, N, N, N, N, N,
3953 /* 0xD8 - 0xDF */
3954 N, N, N, N, N, N, N, N,
3955 /* 0xE0 - 0xE7 */
3956 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3957 /* 0xE8 - 0xEF */
3958 N, N, N, N, N, N, N, N,
3959 /* 0xF0 - 0xF7 */
3960 N, N, N, N, N, N, N, N,
3961 /* 0xF8 - 0xFF */
3962 N, N, N, N, N, N, N, N,
3963} };
3964
3965static const struct escape escape_dd = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02003966 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstsw),
Gleb Natapov045a2822012-12-20 16:57:43 +02003967}, {
3968 /* 0xC0 - 0xC7 */
3969 N, N, N, N, N, N, N, N,
3970 /* 0xC8 - 0xCF */
3971 N, N, N, N, N, N, N, N,
3972 /* 0xD0 - 0xC7 */
3973 N, N, N, N, N, N, N, N,
3974 /* 0xD8 - 0xDF */
3975 N, N, N, N, N, N, N, N,
3976 /* 0xE0 - 0xE7 */
3977 N, N, N, N, N, N, N, N,
3978 /* 0xE8 - 0xEF */
3979 N, N, N, N, N, N, N, N,
3980 /* 0xF0 - 0xF7 */
3981 N, N, N, N, N, N, N, N,
3982 /* 0xF8 - 0xFF */
3983 N, N, N, N, N, N, N, N,
3984} };
3985
Nadav Amit39f062f2014-11-26 15:47:18 +02003986static const struct instr_dual instr_dual_0f_c3 = {
3987 I(DstMem | SrcReg | ModRM | No16 | Mov, em_mov), N
3988};
3989
Nadav Amit2276b512015-01-26 09:32:24 +02003990static const struct mode_dual mode_dual_63 = {
3991 N, I(DstReg | SrcMem32 | ModRM | Mov, em_movsxd)
3992};
3993
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003994static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003995 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003996 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003997 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3998 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003999 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004000 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004001 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
4002 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004003 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004004 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004005 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
4006 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004007 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004008 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004009 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
4010 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004011 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004012 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004013 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004014 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004015 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004016 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004017 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004018 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004019 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02004020 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004021 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03004022 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004023 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09004024 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004025 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09004026 I(ImplicitOps | Stack | No64, em_pusha),
4027 I(ImplicitOps | Stack | No64, em_popa),
Nadav Amit2276b512015-01-26 09:32:24 +02004028 N, MD(ModRM, &mode_dual_63),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004029 N, N, N, N,
4030 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03004031 I(SrcImm | Mov | Stack, em_push),
4032 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03004033 I(SrcImmByte | Mov | Stack, em_push),
4034 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004035 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09004036 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03004037 /* 0x70 - 0x7F */
Nadav Amit58b70752014-10-24 11:35:09 +03004038 X16(D(SrcImmByte | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004039 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004040 G(ByteOp | DstMem | SrcImm, group1),
4041 G(DstMem | SrcImm, group1),
4042 G(ByteOp | DstMem | SrcImm | No64, group1),
4043 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02004044 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004045 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004046 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004047 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004048 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004049 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09004050 D(ModRM | SrcMem | NoAccess | DstReg),
4051 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
4052 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004053 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004054 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004055 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03004056 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08004057 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09004058 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01004059 II(ImplicitOps | Stack, em_popf, popf),
4060 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004061 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004062 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004063 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004064 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004065 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004066 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004067 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004068 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
4069 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004070 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004071 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004072 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004073 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02004074 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004075 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004076 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Nadav Amit58b70752014-10-24 11:35:09 +03004077 I(ImplicitOps | NearBranch | SrcImmU16, em_ret_near_imm),
4078 I(ImplicitOps | NearBranch, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03004079 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
4080 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004081 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004082 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03004083 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Nadav Amit16794aa2015-01-26 09:32:22 +02004084 I(ImplicitOps | SrcImmU16, em_ret_far_imm),
4085 I(ImplicitOps, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02004086 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004087 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004088 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004089 G(Src2One | ByteOp, group2), G(Src2One, group2),
4090 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02004091 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02004092 I(DstAcc | SrcImmUByte | No64, em_aad),
4093 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004094 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004095 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02004096 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004097 /* 0xE0 - 0xE7 */
Nadav Amit58b70752014-10-24 11:35:09 +03004098 X3(I(SrcImmByte | NearBranch, em_loop)),
4099 I(SrcImmByte | NearBranch, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004100 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
4101 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004102 /* 0xE8 - 0xEF */
Nadav Amit58b70752014-10-24 11:35:09 +03004103 I(SrcImm | NearBranch, em_call), D(SrcImm | ImplicitOps | NearBranch),
4104 I(SrcImmFAddr | No64, em_jmp_far),
4105 D(SrcImmByte | ImplicitOps | NearBranch),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004106 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
4107 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004108 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004109 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004110 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4111 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004112 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09004113 D(ImplicitOps), D(ImplicitOps),
4114 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004115 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4116};
4117
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004118static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004119 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004120 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02004121 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004122 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004123 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004124 N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004125 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004126 N, N, N, N, N, N, N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004127 D(ImplicitOps | ModRM | SrcMem | NoAccess),
4128 N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004129 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03004130 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
4131 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
4132 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
4133 check_cr_write),
4134 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
4135 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004136 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004137 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
4138 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004139 N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b),
Avi Kivity3e114eb2012-04-09 18:40:01 +03004140 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004141 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004142 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004143 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004144 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004145 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02004146 I(ImplicitOps | EmulateOnUD, em_sysenter),
4147 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004148 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004149 N, N, N, N, N, N, N, N,
4150 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03004151 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004152 /* 0x50 - 0x5F */
4153 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4154 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004155 N, N, N, N,
4156 N, N, N, N,
4157 N, N, N, N,
4158 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004159 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004160 N, N, N, N,
4161 N, N, N, N,
4162 N, N, N, N,
4163 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004164 /* 0x80 - 0x8F */
Nadav Amit58b70752014-10-24 11:35:09 +03004165 X16(D(SrcImm | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004166 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004167 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004168 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004169 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004170 II(ImplicitOps, em_cpuid, cpuid),
4171 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004172 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4173 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004174 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004175 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004176 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004177 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004178 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4179 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Nadav Amit13e457e2014-10-13 13:04:13 +03004180 GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004181 /* 0xB0 - 0xB7 */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02004182 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable | SrcWrite, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004183 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004184 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004185 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4186 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004187 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004188 /* 0xB8 - 0xBF */
4189 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004190 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004191 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Nadav Amit900efe22015-03-30 15:39:21 +03004192 I(DstReg | SrcMem | ModRM, em_bsf_c),
4193 I(DstReg | SrcMem | ModRM, em_bsr_c),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004194 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004195 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02004196 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Nadav Amit39f062f2014-11-26 15:47:18 +02004197 N, ID(0, &instr_dual_0f_c3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004198 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004199 /* 0xC8 - 0xCF */
4200 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004201 /* 0xD0 - 0xDF */
4202 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4203 /* 0xE0 - 0xEF */
Alex Williamson0a370272014-07-11 11:56:31 -06004204 N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
4205 N, N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004206 /* 0xF0 - 0xFF */
4207 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4208};
4209
Nadav Amit39f062f2014-11-26 15:47:18 +02004210static const struct instr_dual instr_dual_0f_38_f0 = {
4211 I(DstReg | SrcMem | Mov, em_movbe), N
4212};
4213
4214static const struct instr_dual instr_dual_0f_38_f1 = {
4215 I(DstMem | SrcReg | Mov, em_movbe), N
4216};
4217
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004218static const struct gprefix three_byte_0f_38_f0 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004219 ID(0, &instr_dual_0f_38_f0), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004220};
4221
4222static const struct gprefix three_byte_0f_38_f1 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004223 ID(0, &instr_dual_0f_38_f1), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004224};
4225
4226/*
4227 * Insns below are selected by the prefix which indexed by the third opcode
4228 * byte.
4229 */
4230static const struct opcode opcode_map_0f_38[256] = {
4231 /* 0x00 - 0x7f */
4232 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004233 /* 0x80 - 0xef */
4234 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4235 /* 0xf0 - 0xf1 */
Nadav Amit53bb4f72014-12-07 11:49:42 +02004236 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0),
4237 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004238 /* 0xf2 - 0xff */
4239 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004240};
4241
Avi Kivity73fba5f2010-07-29 15:11:53 +03004242#undef D
4243#undef N
4244#undef G
4245#undef GD
4246#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004247#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004248#undef EXT
Nadav Amit2276b512015-01-26 09:32:24 +02004249#undef MD
Nadav Amit2b42fce2015-01-26 09:32:25 +02004250#undef ID
Avi Kivity73fba5f2010-07-29 15:11:53 +03004251
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004252#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004253#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004254#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004255#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004256#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004257
Avi Kivity9dac77f2011-06-01 15:34:25 +03004258static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004259{
4260 unsigned size;
4261
Avi Kivity9dac77f2011-06-01 15:34:25 +03004262 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004263 if (size == 8)
4264 size = 4;
4265 return size;
4266}
4267
4268static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4269 unsigned size, bool sign_extension)
4270{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004271 int rc = X86EMUL_CONTINUE;
4272
4273 op->type = OP_IMM;
4274 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004275 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004276 /* NB. Immediates are sign-extended as necessary. */
4277 switch (op->bytes) {
4278 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004279 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004280 break;
4281 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004282 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004283 break;
4284 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004285 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004286 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004287 case 8:
4288 op->val = insn_fetch(s64, ctxt);
4289 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004290 }
4291 if (!sign_extension) {
4292 switch (op->bytes) {
4293 case 1:
4294 op->val &= 0xff;
4295 break;
4296 case 2:
4297 op->val &= 0xffff;
4298 break;
4299 case 4:
4300 op->val &= 0xffffffff;
4301 break;
4302 }
4303 }
4304done:
4305 return rc;
4306}
4307
Avi Kivitya99455492011-09-13 10:45:41 +03004308static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4309 unsigned d)
4310{
4311 int rc = X86EMUL_CONTINUE;
4312
4313 switch (d) {
4314 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004315 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03004316 break;
4317 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004318 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03004319 break;
4320 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004321 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004322 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03004323 *op = ctxt->memop;
4324 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004325 if (ctxt->d & BitOp)
Avi Kivitya99455492011-09-13 10:45:41 +03004326 fetch_bit_operand(ctxt);
4327 op->orig_val = op->val;
4328 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004329 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004330 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004331 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03004332 case OpAcc:
4333 op->type = OP_REG;
4334 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004335 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03004336 fetch_register_operand(op);
4337 op->orig_val = op->val;
4338 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004339 case OpAccLo:
4340 op->type = OP_REG;
4341 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4342 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4343 fetch_register_operand(op);
4344 op->orig_val = op->val;
4345 break;
4346 case OpAccHi:
4347 if (ctxt->d & ByteOp) {
4348 op->type = OP_NONE;
4349 break;
4350 }
4351 op->type = OP_REG;
4352 op->bytes = ctxt->op_bytes;
4353 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4354 fetch_register_operand(op);
4355 op->orig_val = op->val;
4356 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004357 case OpDI:
4358 op->type = OP_MEM;
4359 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4360 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004361 register_address(ctxt, VCPU_REGS_RDI);
Avi Kivitya99455492011-09-13 10:45:41 +03004362 op->addr.mem.seg = VCPU_SREG_ES;
4363 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004364 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03004365 break;
4366 case OpDX:
4367 op->type = OP_REG;
4368 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004369 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03004370 fetch_register_operand(op);
4371 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004372 case OpCL:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004373 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004374 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004375 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004376 break;
4377 case OpImmByte:
4378 rc = decode_imm(ctxt, op, 1, true);
4379 break;
4380 case OpOne:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004381 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004382 op->bytes = 1;
4383 op->val = 1;
4384 break;
4385 case OpImm:
4386 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4387 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004388 case OpImm64:
4389 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4390 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004391 case OpMem8:
4392 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004393 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004394 ctxt->memop.addr.reg = decode_register(ctxt,
4395 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004396 fetch_register_operand(&ctxt->memop);
4397 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004398 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004399 case OpMem16:
4400 ctxt->memop.bytes = 2;
4401 goto mem_common;
4402 case OpMem32:
4403 ctxt->memop.bytes = 4;
4404 goto mem_common;
4405 case OpImmU16:
4406 rc = decode_imm(ctxt, op, 2, false);
4407 break;
4408 case OpImmU:
4409 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4410 break;
4411 case OpSI:
4412 op->type = OP_MEM;
4413 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4414 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004415 register_address(ctxt, VCPU_REGS_RSI);
Bandan Das573e80f2014-04-16 12:46:13 -04004416 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03004417 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004418 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004419 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004420 case OpXLat:
4421 op->type = OP_MEM;
4422 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4423 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004424 address_mask(ctxt,
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004425 reg_read(ctxt, VCPU_REGS_RBX) +
4426 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04004427 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004428 op->val = 0;
4429 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004430 case OpImmFAddr:
4431 op->type = OP_IMM;
4432 op->addr.mem.ea = ctxt->_eip;
4433 op->bytes = ctxt->op_bytes + 2;
4434 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4435 break;
4436 case OpMemFAddr:
4437 ctxt->memop.bytes = ctxt->op_bytes + 2;
4438 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004439 case OpES:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004440 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004441 op->val = VCPU_SREG_ES;
4442 break;
4443 case OpCS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004444 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004445 op->val = VCPU_SREG_CS;
4446 break;
4447 case OpSS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004448 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004449 op->val = VCPU_SREG_SS;
4450 break;
4451 case OpDS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004452 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004453 op->val = VCPU_SREG_DS;
4454 break;
4455 case OpFS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004456 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004457 op->val = VCPU_SREG_FS;
4458 break;
4459 case OpGS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004460 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004461 op->val = VCPU_SREG_GS;
4462 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004463 case OpImplicit:
4464 /* Special instructions do their own operand decoding. */
4465 default:
4466 op->type = OP_NONE; /* Disable writeback. */
4467 break;
4468 }
4469
4470done:
4471 return rc;
4472}
4473
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004474int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004475{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004476 int rc = X86EMUL_CONTINUE;
4477 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004478 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004479 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04004480 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03004481 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004482
Avi Kivityf09ed832011-09-13 10:45:40 +03004483 ctxt->memop.type = OP_NONE;
4484 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004485 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02004486 ctxt->fetch.ptr = ctxt->fetch.data;
4487 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004488 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01004489 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004490 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004491 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02004492 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004493 if (rc != X86EMUL_CONTINUE)
4494 return rc;
4495 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004496
4497 switch (mode) {
4498 case X86EMUL_MODE_REAL:
4499 case X86EMUL_MODE_VM86:
4500 case X86EMUL_MODE_PROT16:
4501 def_op_bytes = def_ad_bytes = 2;
4502 break;
4503 case X86EMUL_MODE_PROT32:
4504 def_op_bytes = def_ad_bytes = 4;
4505 break;
4506#ifdef CONFIG_X86_64
4507 case X86EMUL_MODE_PROT64:
4508 def_op_bytes = 4;
4509 def_ad_bytes = 8;
4510 break;
4511#endif
4512 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004513 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004514 }
4515
Avi Kivity9dac77f2011-06-01 15:34:25 +03004516 ctxt->op_bytes = def_op_bytes;
4517 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004518
4519 /* Legacy prefixes. */
4520 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004521 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004522 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004523 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004524 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004525 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004526 break;
4527 case 0x67: /* address-size override */
4528 if (mode == X86EMUL_MODE_PROT64)
4529 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004530 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004531 else
4532 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004533 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004534 break;
4535 case 0x26: /* ES override */
4536 case 0x2e: /* CS override */
4537 case 0x36: /* SS override */
4538 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004539 has_seg_override = true;
4540 ctxt->seg_override = (ctxt->b >> 3) & 3;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004541 break;
4542 case 0x64: /* FS override */
4543 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004544 has_seg_override = true;
4545 ctxt->seg_override = ctxt->b & 7;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004546 break;
4547 case 0x40 ... 0x4f: /* REX */
4548 if (mode != X86EMUL_MODE_PROT64)
4549 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004550 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004551 continue;
4552 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004553 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004554 break;
4555 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004556 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004557 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004558 break;
4559 default:
4560 goto done_prefixes;
4561 }
4562
4563 /* Any legacy prefix after a REX prefix nullifies its effect. */
4564
Avi Kivity9dac77f2011-06-01 15:34:25 +03004565 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004566 }
4567
4568done_prefixes:
4569
4570 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004571 if (ctxt->rex_prefix & 8)
4572 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004573
4574 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004575 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004576 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004577 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004578 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004579 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004580 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004581
4582 /* 0F_38 opcode map */
4583 if (ctxt->b == 0x38) {
4584 ctxt->opcode_len = 3;
4585 ctxt->b = insn_fetch(u8, ctxt);
4586 opcode = opcode_map_0f_38[ctxt->b];
4587 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004588 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004589 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004590
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004591 if (ctxt->d & ModRM)
4592 ctxt->modrm = insn_fetch(u8, ctxt);
4593
Nadav Amit7fe864d2014-06-02 18:34:03 +03004594 /* vex-prefix instructions are not implemented */
4595 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
Nadav Amitd14cb5d2014-11-02 11:54:58 +02004596 (mode == X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) == 0xc0)) {
Nadav Amit7fe864d2014-06-02 18:34:03 +03004597 ctxt->d = NotImpl;
4598 }
4599
Avi Kivity9dac77f2011-06-01 15:34:25 +03004600 while (ctxt->d & GroupMask) {
4601 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004602 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004603 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004604 opcode = opcode.u.group[goffset];
4605 break;
4606 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004607 goffset = (ctxt->modrm >> 3) & 7;
4608 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004609 opcode = opcode.u.gdual->mod3[goffset];
4610 else
4611 opcode = opcode.u.gdual->mod012[goffset];
4612 break;
4613 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004614 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004615 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004616 break;
4617 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004618 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004619 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004620 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004621 switch (simd_prefix) {
4622 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4623 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4624 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4625 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4626 }
4627 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004628 case Escape:
4629 if (ctxt->modrm > 0xbf)
4630 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4631 else
4632 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4633 break;
Nadav Amit39f062f2014-11-26 15:47:18 +02004634 case InstrDual:
4635 if ((ctxt->modrm >> 6) == 3)
4636 opcode = opcode.u.idual->mod3;
4637 else
4638 opcode = opcode.u.idual->mod012;
4639 break;
Nadav Amit2276b512015-01-26 09:32:24 +02004640 case ModeDual:
4641 if (ctxt->mode == X86EMUL_MODE_PROT64)
4642 opcode = opcode.u.mdual->mode64;
4643 else
4644 opcode = opcode.u.mdual->mode32;
4645 break;
Avi Kivity46561642011-04-24 14:09:59 +03004646 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004647 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004648 }
Avi Kivity46561642011-04-24 14:09:59 +03004649
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004650 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004651 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004652 }
4653
Paolo Bonzinie24186e2014-03-27 12:00:57 +01004654 /* Unrecognised? */
4655 if (ctxt->d == 0)
4656 return EMULATION_FAILED;
4657
Avi Kivity9dac77f2011-06-01 15:34:25 +03004658 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004659
Nadav Amit3a6095a2014-08-13 16:50:13 +03004660 if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD)))
4661 return EMULATION_FAILED;
4662
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004663 if (unlikely(ctxt->d &
Nadav Amited9aad22014-11-02 11:55:00 +02004664 (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch|
4665 No16))) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004666 /*
4667 * These are copied unconditionally here, and checked unconditionally
4668 * in x86_emulate_insn.
4669 */
4670 ctxt->check_perm = opcode.check_perm;
4671 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004672
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004673 if (ctxt->d & NotImpl)
4674 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004675
Nadav Amit58b70752014-10-24 11:35:09 +03004676 if (mode == X86EMUL_MODE_PROT64) {
4677 if (ctxt->op_bytes == 4 && (ctxt->d & Stack))
4678 ctxt->op_bytes = 8;
4679 else if (ctxt->d & NearBranch)
4680 ctxt->op_bytes = 8;
4681 }
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004682
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004683 if (ctxt->d & Op3264) {
4684 if (mode == X86EMUL_MODE_PROT64)
4685 ctxt->op_bytes = 8;
4686 else
4687 ctxt->op_bytes = 4;
4688 }
4689
Nadav Amited9aad22014-11-02 11:55:00 +02004690 if ((ctxt->d & No16) && ctxt->op_bytes == 2)
4691 ctxt->op_bytes = 4;
4692
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004693 if (ctxt->d & Sse)
4694 ctxt->op_bytes = 16;
4695 else if (ctxt->d & Mmx)
4696 ctxt->op_bytes = 8;
4697 }
Avi Kivity1253791d2011-03-29 11:41:27 +02004698
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004699 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004700 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004701 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04004702 if (!has_seg_override) {
4703 has_seg_override = true;
4704 ctxt->seg_override = ctxt->modrm_seg;
4705 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004706 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004707 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004708 if (rc != X86EMUL_CONTINUE)
4709 goto done;
4710
Bandan Das573e80f2014-04-16 12:46:13 -04004711 if (!has_seg_override)
4712 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004713
Bandan Das573e80f2014-04-16 12:46:13 -04004714 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004715
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004716 /*
4717 * Decode and fetch the source operand: register, memory
4718 * or immediate.
4719 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004720 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004721 if (rc != X86EMUL_CONTINUE)
4722 goto done;
4723
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004724 /*
4725 * Decode and fetch the second source operand: register, memory
4726 * or immediate.
4727 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004728 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004729 if (rc != X86EMUL_CONTINUE)
4730 goto done;
4731
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004732 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03004733 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004734
Bandan Das41061cd2014-04-16 12:46:14 -04004735 if (ctxt->rip_relative)
Nadav Amit1c1c35a2014-11-19 17:43:09 +02004736 ctxt->memopp->addr.mem.ea = address_mask(ctxt,
4737 ctxt->memopp->addr.mem.ea + ctxt->_eip);
Avi Kivitycb16c342011-06-19 19:21:11 +03004738
Paolo Bonzinia430c912014-10-23 14:54:14 +02004739done:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004740 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004741}
4742
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004743bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4744{
4745 return ctxt->d & PageTable;
4746}
4747
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004748static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4749{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004750 /* The second termination condition only applies for REPE
4751 * and REPNE. Test if the repeat string operation prefix is
4752 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4753 * corresponding termination condition according to:
4754 * - if REPE/REPZ and ZF = 0 then done
4755 * - if REPNE/REPNZ and ZF = 1 then done
4756 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004757 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4758 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4759 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03004760 ((ctxt->eflags & X86_EFLAGS_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004761 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03004762 ((ctxt->eflags & X86_EFLAGS_ZF) == X86_EFLAGS_ZF))))
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004763 return true;
4764
4765 return false;
4766}
4767
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004768static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4769{
4770 bool fault = false;
4771
4772 ctxt->ops->get_fpu(ctxt);
4773 asm volatile("1: fwait \n\t"
4774 "2: \n\t"
4775 ".pushsection .fixup,\"ax\" \n\t"
4776 "3: \n\t"
4777 "movb $1, %[fault] \n\t"
4778 "jmp 2b \n\t"
4779 ".popsection \n\t"
4780 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004781 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004782 ctxt->ops->put_fpu(ctxt);
4783
4784 if (unlikely(fault))
4785 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4786
4787 return X86EMUL_CONTINUE;
4788}
4789
4790static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4791 struct operand *op)
4792{
4793 if (op->type == OP_MM)
4794 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4795}
4796
Avi Kivitye28bbd42013-01-04 16:18:48 +02004797static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4798{
4799 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivityb9fa4092013-02-09 11:31:48 +02004800 if (!(ctxt->d & ByteOp))
4801 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Avi Kivitye28bbd42013-01-04 16:18:48 +02004802 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004803 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
4804 [fastop]"+S"(fop)
4805 : "c"(ctxt->src2.val));
Avi Kivitye28bbd42013-01-04 16:18:48 +02004806 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004807 if (!fop) /* exception is returned in fop variable */
4808 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02004809 return X86EMUL_CONTINUE;
4810}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004811
Bandan Das14985072014-04-16 12:46:09 -04004812void init_decode_cache(struct x86_emulate_ctxt *ctxt)
4813{
Bandan Das573e80f2014-04-16 12:46:13 -04004814 memset(&ctxt->rip_relative, 0,
4815 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04004816
Bandan Das14985072014-04-16 12:46:09 -04004817 ctxt->io_read.pos = 0;
4818 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04004819 ctxt->mem_read.end = 0;
4820}
4821
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004822int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004823{
Mathias Krause0225fb52012-08-30 01:30:16 +02004824 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004825 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004826 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004827
Avi Kivity9dac77f2011-06-01 15:34:25 +03004828 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004829
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004830 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004831 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004832 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004833 goto done;
4834 }
4835
Avi Kivity9dac77f2011-06-01 15:34:25 +03004836 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004837 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004838 goto done;
4839 }
4840
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004841 if (unlikely(ctxt->d &
4842 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
4843 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4844 (ctxt->d & Undefined)) {
4845 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004846 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004847 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004848
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004849 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4850 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
4851 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004852 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004853 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004854
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004855 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
4856 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004857 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004858 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004859
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004860 if (ctxt->d & Mmx) {
4861 rc = flush_pending_x87_faults(ctxt);
4862 if (rc != X86EMUL_CONTINUE)
4863 goto done;
4864 /*
4865 * Now that we know the fpu is exception safe, we can fetch
4866 * operands from it.
4867 */
4868 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4869 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4870 if (!(ctxt->d & Mov))
4871 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4872 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004873
Bandan Das685bbf42014-04-16 12:46:10 -04004874 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004875 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4876 X86_ICPT_PRE_EXCEPT);
4877 if (rc != X86EMUL_CONTINUE)
4878 goto done;
4879 }
4880
Nadav Amit64a38292014-12-10 11:19:04 +02004881 /* Instruction can only be executed in protected mode */
4882 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
4883 rc = emulate_ud(ctxt);
4884 goto done;
4885 }
4886
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004887 /* Privileged instruction can be executed only in CPL=0 */
4888 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Nadav Amit68efa762014-06-18 17:19:35 +03004889 if (ctxt->d & PrivUD)
4890 rc = emulate_ud(ctxt);
4891 else
4892 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004893 goto done;
4894 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004895
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004896 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04004897 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004898 rc = ctxt->check_perm(ctxt);
4899 if (rc != X86EMUL_CONTINUE)
4900 goto done;
4901 }
4902
Bandan Das685bbf42014-04-16 12:46:10 -04004903 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004904 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4905 X86_ICPT_POST_EXCEPT);
4906 if (rc != X86EMUL_CONTINUE)
4907 goto done;
4908 }
4909
4910 if (ctxt->rep_prefix && (ctxt->d & String)) {
4911 /* All REP prefixes have the same first termination condition */
4912 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
4913 ctxt->eip = ctxt->_eip;
Nadav Amit0efb0442015-03-29 16:33:03 +03004914 ctxt->eflags &= ~X86_EFLAGS_RF;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004915 goto done;
4916 }
4917 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004918 }
4919
Avi Kivity9dac77f2011-06-01 15:34:25 +03004920 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4921 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4922 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004923 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004924 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004925 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004926 }
4927
Avi Kivity9dac77f2011-06-01 15:34:25 +03004928 if (ctxt->src2.type == OP_MEM) {
4929 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4930 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004931 if (rc != X86EMUL_CONTINUE)
4932 goto done;
4933 }
4934
Avi Kivity9dac77f2011-06-01 15:34:25 +03004935 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004936 goto special_insn;
4937
4938
Avi Kivity9dac77f2011-06-01 15:34:25 +03004939 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004940 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004941 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4942 &ctxt->dst.val, ctxt->dst.bytes);
Nadav Amitc205fb72014-12-25 02:52:16 +02004943 if (rc != X86EMUL_CONTINUE) {
Paolo Bonzinid44e1212015-02-09 10:02:05 +01004944 if (!(ctxt->d & NoWrite) &&
4945 rc == X86EMUL_PROPAGATE_FAULT &&
Nadav Amitc205fb72014-12-25 02:52:16 +02004946 ctxt->exception.vector == PF_VECTOR)
4947 ctxt->exception.error_code |= PFERR_WRITE_MASK;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004948 goto done;
Nadav Amitc205fb72014-12-25 02:52:16 +02004949 }
Avi Kivity038e51d2007-01-22 20:40:40 -08004950 }
Paolo Bonzini4ff6f8e2015-02-12 17:04:47 +01004951 /* Copy full 64-bit value for CMPXCHG8B. */
4952 ctxt->dst.orig_val64 = ctxt->dst.val64;
Avi Kivity038e51d2007-01-22 20:40:40 -08004953
Avi Kivity018a98d2007-11-27 19:30:56 +02004954special_insn:
4955
Bandan Das685bbf42014-04-16 12:46:10 -04004956 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004957 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004958 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004959 if (rc != X86EMUL_CONTINUE)
4960 goto done;
4961 }
4962
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004963 if (ctxt->rep_prefix && (ctxt->d & String))
Nadav Amit0efb0442015-03-29 16:33:03 +03004964 ctxt->eflags |= X86_EFLAGS_RF;
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004965 else
Nadav Amit0efb0442015-03-29 16:33:03 +03004966 ctxt->eflags &= ~X86_EFLAGS_RF;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004967
Avi Kivity9dac77f2011-06-01 15:34:25 +03004968 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004969 if (ctxt->d & Fastop) {
4970 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4971 rc = fastop(ctxt, fop);
4972 if (rc != X86EMUL_CONTINUE)
4973 goto done;
4974 goto writeback;
4975 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004976 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004977 if (rc != X86EMUL_CONTINUE)
4978 goto done;
4979 goto writeback;
4980 }
4981
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004982 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004983 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004984 else if (ctxt->opcode_len == 3)
4985 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004986
Avi Kivity9dac77f2011-06-01 15:34:25 +03004987 switch (ctxt->b) {
Gleb Natapovb2833e32009-04-12 13:36:30 +03004988 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004989 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03004990 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004991 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004992 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004993 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004994 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004995 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004996 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03004997 ctxt->dst.type = OP_NONE;
4998 else
4999 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09005000 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005001 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005002 switch (ctxt->op_bytes) {
5003 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
5004 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
5005 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005006 }
5007 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005008 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005009 rc = emulate_int(ctxt, 3);
5010 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005011 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005012 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005013 break;
5014 case 0xce: /* into */
Nadav Amit0efb0442015-03-29 16:33:03 +03005015 if (ctxt->eflags & X86_EFLAGS_OF)
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005016 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005017 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005018 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09005019 case 0xeb: /* jmp rel short */
Nadav Amit234f3ce2014-09-18 22:39:38 +03005020 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity9dac77f2011-06-01 15:34:25 +03005021 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005022 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005023 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03005024 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03005025 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005026 case 0xf5: /* cmc */
5027 /* complement carry flag from eflags reg */
Nadav Amit0efb0442015-03-29 16:33:03 +03005028 ctxt->eflags ^= X86_EFLAGS_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005029 break;
5030 case 0xf8: /* clc */
Nadav Amit0efb0442015-03-29 16:33:03 +03005031 ctxt->eflags &= ~X86_EFLAGS_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005032 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03005033 case 0xf9: /* stc */
Nadav Amit0efb0442015-03-29 16:33:03 +03005034 ctxt->eflags |= X86_EFLAGS_CF;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03005035 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005036 case 0xfc: /* cld */
Nadav Amit0efb0442015-03-29 16:33:03 +03005037 ctxt->eflags &= ~X86_EFLAGS_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005038 break;
5039 case 0xfd: /* std */
Nadav Amit0efb0442015-03-29 16:33:03 +03005040 ctxt->eflags |= X86_EFLAGS_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005041 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005042 default:
5043 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005044 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005045
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005046 if (rc != X86EMUL_CONTINUE)
5047 goto done;
5048
Avi Kivity018a98d2007-11-27 19:30:56 +02005049writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02005050 if (ctxt->d & SrcWrite) {
5051 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
5052 rc = writeback(ctxt, &ctxt->src);
5053 if (rc != X86EMUL_CONTINUE)
5054 goto done;
5055 }
Nadav Amitee212292014-06-15 16:12:58 +03005056 if (!(ctxt->d & NoWrite)) {
5057 rc = writeback(ctxt, &ctxt->dst);
5058 if (rc != X86EMUL_CONTINUE)
5059 goto done;
5060 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005061
Gleb Natapov5cd21912010-03-18 15:20:26 +02005062 /*
5063 * restore dst type in case the decoding will be reused
5064 * (happens for string instruction )
5065 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005066 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005067
Avi Kivity9dac77f2011-06-01 15:34:25 +03005068 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005069 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02005070
Avi Kivity9dac77f2011-06-01 15:34:25 +03005071 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005072 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02005073
Avi Kivity9dac77f2011-06-01 15:34:25 +03005074 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005075 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005076 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005077 if ((ctxt->d & SrcMask) == SrcSI)
5078 count = ctxt->src.count;
5079 else
5080 count = ctxt->dst.count;
Paolo Bonzini01485a22014-11-19 18:25:08 +01005081 register_address_increment(ctxt, VCPU_REGS_RCX, -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005082
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005083 if (!string_insn_completed(ctxt)) {
5084 /*
5085 * Re-enter guest when pio read ahead buffer is empty
5086 * or, if it is not used, after each 1024 iteration.
5087 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005088 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005089 (r->end == 0 || r->end != r->pos)) {
5090 /*
5091 * Reset read cache. Usually happens before
5092 * decode, but since instruction is restarted
5093 * we have to do it here.
5094 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005095 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005096 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005097 return EMULATION_RESTART;
5098 }
5099 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03005100 }
Nadav Amit0efb0442015-03-29 16:33:03 +03005101 ctxt->eflags &= ~X86_EFLAGS_RF;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005102 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005103
Avi Kivity9dac77f2011-06-01 15:34:25 +03005104 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02005105
5106done:
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005107 if (rc == X86EMUL_PROPAGATE_FAULT) {
5108 WARN_ON(ctxt->exception.vector > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +02005109 ctxt->have_exception = true;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005110 }
Joerg Roedel775fde82011-04-04 12:39:24 +02005111 if (rc == X86EMUL_INTERCEPTED)
5112 return EMULATION_INTERCEPTED;
5113
Avi Kivitydd856ef2012-08-27 23:46:17 +03005114 if (rc == X86EMUL_CONTINUE)
5115 writeback_registers(ctxt);
5116
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005117 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005118
5119twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005120 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02005121 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02005122 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005123 break;
5124 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02005125 case 0x0d: /* GrpP (prefetch) */
5126 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02005127 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02005128 break;
5129 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005130 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02005131 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005132 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005133 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005134 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005135 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03005136 if (test_cc(ctxt->b, ctxt->eflags))
5137 ctxt->dst.val = ctxt->src.val;
Nadav Amitb91aa142015-03-30 15:39:19 +03005138 else if (ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005139 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08005140 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03005141 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03005142 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005143 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005144 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08005145 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005146 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08005147 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005148 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005149 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005150 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03005151 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005152 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005153 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005154 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005155 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03005156 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005157 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005158 default:
5159 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005160 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005161
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005162threebyte_insn:
5163
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005164 if (rc != X86EMUL_CONTINUE)
5165 goto done;
5166
Avi Kivity6aa8b732006-12-10 02:21:36 -08005167 goto writeback;
5168
5169cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02005170 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005171}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005172
5173void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
5174{
5175 invalidate_registers(ctxt);
5176}
5177
5178void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
5179{
5180 writeback_registers(ctxt);
5181}