blob: eb74d3b56e1ce41394715a405a8237d24c6ae95b [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 Kivity56e82312009-08-12 15:04:37 +030025#include <asm/kvm_emulate.h>
Avi Kivityb7d491e2013-01-04 16:18:49 +020026#include <linux/stringify.h>
Nadav Amit3db176d2015-04-19 21:12:59 +030027#include <asm/debugreg.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 */
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100174#define Aligned16 ((u64)1 << 55) /* Aligned to 16 byte boundary (e.g. FXSAVE) */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800175
Avi Kivity820207c2013-02-09 11:31:45 +0200176#define DstXacc (DstAccLo | SrcAccHi | SrcWrite)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800177
Avi Kivityd0e53322010-07-29 15:11:54 +0300178#define X2(x...) x, x
179#define X3(x...) X2(x), x
180#define X4(x...) X2(x), X2(x)
181#define X5(x...) X4(x), x
182#define X6(x...) X4(x), X2(x)
183#define X7(x...) X4(x), X3(x)
184#define X8(x...) X4(x), X4(x)
185#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300186
Avi Kivitye28bbd42013-01-04 16:18:48 +0200187#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
188#define FASTOP_SIZE 8
189
190/*
191 * fastop functions have a special calling convention:
192 *
Avi Kivity017da7b2013-02-09 11:31:47 +0200193 * dst: rax (in/out)
194 * src: rdx (in/out)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200195 * src2: rcx (in)
196 * flags: rflags (in/out)
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200197 * ex: rsi (in:fastop pointer, out:zero if exception)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200198 *
199 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
200 * different operand sizes can be reached by calculation, rather than a jump
201 * table (which would be bigger than the code).
202 *
203 * fastop functions are declared as taking a never-defined fastop parameter,
204 * so they can't be called from C directly.
205 */
206
207struct fastop;
208
Avi Kivityd65b1de2010-07-29 15:11:35 +0300209struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300210 u64 flags : 56;
211 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300212 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300213 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200214 const struct opcode *group;
215 const struct group_dual *gdual;
216 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200217 const struct escape *esc;
Nadav Amit39f062f2014-11-26 15:47:18 +0200218 const struct instr_dual *idual;
Nadav Amit2276b512015-01-26 09:32:24 +0200219 const struct mode_dual *mdual;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200220 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300221 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200222 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300223};
224
225struct group_dual {
226 struct opcode mod012[8];
227 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300228};
229
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200230struct gprefix {
231 struct opcode pfx_no;
232 struct opcode pfx_66;
233 struct opcode pfx_f2;
234 struct opcode pfx_f3;
235};
236
Gleb Natapov045a2822012-12-20 16:57:43 +0200237struct escape {
238 struct opcode op[8];
239 struct opcode high[64];
240};
241
Nadav Amit39f062f2014-11-26 15:47:18 +0200242struct instr_dual {
243 struct opcode mod012;
244 struct opcode mod3;
245};
246
Nadav Amit2276b512015-01-26 09:32:24 +0200247struct mode_dual {
248 struct opcode mode32;
249 struct opcode mode64;
250};
251
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300252#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300253
Nadav Amit3dc4bc42014-12-25 02:52:19 +0200254enum x86_transfer_type {
255 X86_TRANSFER_NONE,
256 X86_TRANSFER_CALL_JMP,
257 X86_TRANSFER_RET,
258 X86_TRANSFER_TASK_SWITCH,
259};
260
Avi Kivitydd856ef2012-08-27 23:46:17 +0300261static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
262{
263 if (!(ctxt->regs_valid & (1 << nr))) {
264 ctxt->regs_valid |= 1 << nr;
265 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
266 }
267 return ctxt->_regs[nr];
268}
269
270static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
271{
272 ctxt->regs_valid |= 1 << nr;
273 ctxt->regs_dirty |= 1 << nr;
274 return &ctxt->_regs[nr];
275}
276
277static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
278{
279 reg_read(ctxt, nr);
280 return reg_write(ctxt, nr);
281}
282
283static void writeback_registers(struct x86_emulate_ctxt *ctxt)
284{
285 unsigned reg;
286
287 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
288 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
289}
290
291static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
292{
293 ctxt->regs_dirty = 0;
294 ctxt->regs_valid = 0;
295}
296
Avi Kivity6aa8b732006-12-10 02:21:36 -0800297/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800298 * These EFLAGS bits are restored from saved value during emulation, and
299 * any changes are written back to the saved value after emulation.
300 */
Nadav Amit0efb0442015-03-29 16:33:03 +0300301#define EFLAGS_MASK (X86_EFLAGS_OF|X86_EFLAGS_SF|X86_EFLAGS_ZF|X86_EFLAGS_AF|\
302 X86_EFLAGS_PF|X86_EFLAGS_CF)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800303
Avi Kivitydda96d82008-11-26 15:14:10 +0200304#ifdef CONFIG_X86_64
305#define ON64(x) x
306#else
307#define ON64(x)
308#endif
309
Avi Kivity4d758342013-01-19 19:51:55 +0200310static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *));
311
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600312#define FOP_FUNC(name) \
313 ".align " __stringify(FASTOP_SIZE) " \n\t" \
314 ".type " name ", @function \n\t" \
315 name ":\n\t"
316
Avi Kivityb7d491e2013-01-04 16:18:49 +0200317#define FOP_RET "ret \n\t"
318
319#define FOP_START(op) \
320 extern void em_##op(struct fastop *fake); \
321 asm(".pushsection .text, \"ax\" \n\t" \
322 ".global em_" #op " \n\t" \
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600323 FOP_FUNC("em_" #op)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200324
325#define FOP_END \
326 ".popsection")
327
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600328#define FOPNOP() \
329 FOP_FUNC(__stringify(__UNIQUE_ID(nop))) \
330 FOP_RET
Avi Kivity0bdea062013-01-19 19:51:50 +0200331
Avi Kivityb7d491e2013-01-04 16:18:49 +0200332#define FOP1E(op, dst) \
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600333 FOP_FUNC(#op "_" #dst) \
334 "10: " #op " %" #dst " \n\t" FOP_RET
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200335
336#define FOP1EEX(op, dst) \
337 FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200338
339#define FASTOP1(op) \
340 FOP_START(op) \
341 FOP1E(op##b, al) \
342 FOP1E(op##w, ax) \
343 FOP1E(op##l, eax) \
344 ON64(FOP1E(op##q, rax)) \
345 FOP_END
346
Avi Kivityb9fa4092013-02-09 11:31:48 +0200347/* 1-operand, using src2 (for MUL/DIV r/m) */
348#define FASTOP1SRC2(op, name) \
349 FOP_START(name) \
350 FOP1E(op, cl) \
351 FOP1E(op, cx) \
352 FOP1E(op, ecx) \
353 ON64(FOP1E(op, rcx)) \
354 FOP_END
355
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200356/* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */
357#define FASTOP1SRC2EX(op, name) \
358 FOP_START(name) \
359 FOP1EEX(op, cl) \
360 FOP1EEX(op, cx) \
361 FOP1EEX(op, ecx) \
362 ON64(FOP1EEX(op, rcx)) \
363 FOP_END
364
Avi Kivityf7857f32013-01-04 16:18:53 +0200365#define FOP2E(op, dst, src) \
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600366 FOP_FUNC(#op "_" #dst "_" #src) \
367 #op " %" #src ", %" #dst " \n\t" FOP_RET
Avi Kivityf7857f32013-01-04 16:18:53 +0200368
369#define FASTOP2(op) \
370 FOP_START(op) \
Avi Kivity017da7b2013-02-09 11:31:47 +0200371 FOP2E(op##b, al, dl) \
372 FOP2E(op##w, ax, dx) \
373 FOP2E(op##l, eax, edx) \
374 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivityf7857f32013-01-04 16:18:53 +0200375 FOP_END
376
Avi Kivity11c363b2013-01-19 19:51:54 +0200377/* 2 operand, word only */
378#define FASTOP2W(op) \
379 FOP_START(op) \
380 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200381 FOP2E(op##w, ax, dx) \
382 FOP2E(op##l, eax, edx) \
383 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivity11c363b2013-01-19 19:51:54 +0200384 FOP_END
385
Avi Kivity007a3b52013-01-19 19:51:51 +0200386/* 2 operand, src is CL */
387#define FASTOP2CL(op) \
388 FOP_START(op) \
389 FOP2E(op##b, al, cl) \
390 FOP2E(op##w, ax, cl) \
391 FOP2E(op##l, eax, cl) \
392 ON64(FOP2E(op##q, rax, cl)) \
393 FOP_END
394
Nadav Amit5aca3722014-11-02 11:54:50 +0200395/* 2 operand, src and dest are reversed */
396#define FASTOP2R(op, name) \
397 FOP_START(name) \
398 FOP2E(op##b, dl, al) \
399 FOP2E(op##w, dx, ax) \
400 FOP2E(op##l, edx, eax) \
401 ON64(FOP2E(op##q, rdx, rax)) \
402 FOP_END
403
Avi Kivity0bdea062013-01-19 19:51:50 +0200404#define FOP3E(op, dst, src, src2) \
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600405 FOP_FUNC(#op "_" #dst "_" #src "_" #src2) \
406 #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
Avi Kivity0bdea062013-01-19 19:51:50 +0200407
408/* 3-operand, word-only, src2=cl */
409#define FASTOP3WCL(op) \
410 FOP_START(op) \
411 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200412 FOP3E(op##w, ax, dx, cl) \
413 FOP3E(op##l, eax, edx, cl) \
414 ON64(FOP3E(op##q, rax, rdx, cl)) \
Avi Kivity0bdea062013-01-19 19:51:50 +0200415 FOP_END
416
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200417/* Special case for SETcc - 1 instruction per cc */
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600418#define FOP_SETCC(op) \
419 ".align 4 \n\t" \
420 ".type " #op ", @function \n\t" \
421 #op ": \n\t" \
422 #op " %al \n\t" \
423 FOP_RET
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200424
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200425asm(".global kvm_fastop_exception \n"
426 "kvm_fastop_exception: xor %esi, %esi; ret");
427
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200428FOP_START(setcc)
429FOP_SETCC(seto)
430FOP_SETCC(setno)
431FOP_SETCC(setc)
432FOP_SETCC(setnc)
433FOP_SETCC(setz)
434FOP_SETCC(setnz)
435FOP_SETCC(setbe)
436FOP_SETCC(setnbe)
437FOP_SETCC(sets)
438FOP_SETCC(setns)
439FOP_SETCC(setp)
440FOP_SETCC(setnp)
441FOP_SETCC(setl)
442FOP_SETCC(setnl)
443FOP_SETCC(setle)
444FOP_SETCC(setnle)
445FOP_END;
446
Paolo Bonzini326f5782013-05-09 11:32:51 +0200447FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET
448FOP_END;
449
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200450static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
451 enum x86_intercept intercept,
452 enum x86_intercept_stage stage)
453{
454 struct x86_instruction_info info = {
455 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300456 .rep_prefix = ctxt->rep_prefix,
457 .modrm_mod = ctxt->modrm_mod,
458 .modrm_reg = ctxt->modrm_reg,
459 .modrm_rm = ctxt->modrm_rm,
460 .src_val = ctxt->src.val64,
Jan Kiszka6cbc5f52014-06-30 12:52:55 +0200461 .dst_val = ctxt->dst.val64,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300462 .src_bytes = ctxt->src.bytes,
463 .dst_bytes = ctxt->dst.bytes,
464 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200465 .next_rip = ctxt->eip,
466 };
467
Avi Kivity29535382011-04-20 13:37:53 +0300468 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200469}
470
Avi Kivityf47cfa32012-06-07 17:49:24 +0300471static void assign_masked(ulong *dest, ulong src, ulong mask)
472{
473 *dest = (*dest & ~mask) | (src & mask);
474}
475
Nadav Amit6fd8e122015-03-30 15:39:20 +0300476static void assign_register(unsigned long *reg, u64 val, int bytes)
477{
478 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
479 switch (bytes) {
480 case 1:
481 *(u8 *)reg = (u8)val;
482 break;
483 case 2:
484 *(u16 *)reg = (u16)val;
485 break;
486 case 4:
487 *reg = (u32)val;
488 break; /* 64b: zero-extend */
489 case 8:
490 *reg = val;
491 break;
492 }
493}
494
Avi Kivity9dac77f2011-06-01 15:34:25 +0300495static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800496{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300497 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800498}
499
Avi Kivityf47cfa32012-06-07 17:49:24 +0300500static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
501{
502 u16 sel;
503 struct desc_struct ss;
504
505 if (ctxt->mode == X86EMUL_MODE_PROT64)
506 return ~0UL;
507 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
508 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
509}
510
Avi Kivity612e89f2012-06-12 20:03:23 +0300511static int stack_size(struct x86_emulate_ctxt *ctxt)
512{
513 return (__fls(stack_mask(ctxt)) + 1) >> 3;
514}
515
Avi Kivity6aa8b732006-12-10 02:21:36 -0800516/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800517static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300518address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800519{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300520 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800521 return reg;
522 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300523 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800524}
525
526static inline unsigned long
Paolo Bonzini01485a22014-11-19 18:25:08 +0100527register_address(struct x86_emulate_ctxt *ctxt, int reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800528{
Paolo Bonzini01485a22014-11-19 18:25:08 +0100529 return address_mask(ctxt, reg_read(ctxt, reg));
Harvey Harrisone4706772008-02-19 07:40:38 -0800530}
531
Avi Kivity5ad105e2012-08-19 14:34:31 +0300532static void masked_increment(ulong *reg, ulong mask, int inc)
533{
534 assign_masked(reg, *reg + inc, mask);
535}
536
Harvey Harrison7a9572752008-02-19 07:40:41 -0800537static inline void
Paolo Bonzini01485a22014-11-19 18:25:08 +0100538register_address_increment(struct x86_emulate_ctxt *ctxt, int reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800539{
Nadav Amitee122a72015-04-28 13:06:00 +0300540 ulong *preg = reg_rmw(ctxt, reg);
Avi Kivity5ad105e2012-08-19 14:34:31 +0300541
Nadav Amitee122a72015-04-28 13:06:00 +0300542 assign_register(preg, *preg + inc, ctxt->ad_bytes);
Avi Kivity5ad105e2012-08-19 14:34:31 +0300543}
544
545static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
546{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300547 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800548}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800549
Avi Kivity56697682011-04-03 14:08:51 +0300550static u32 desc_limit_scaled(struct desc_struct *desc)
551{
552 u32 limit = get_desc_limit(desc);
553
554 return desc->g ? (limit << 12) | 0xfff : limit;
555}
556
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900557static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300558{
559 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
560 return 0;
561
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900562 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300563}
564
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200565static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
566 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300567{
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +0200568 WARN_ON(vec > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +0200569 ctxt->exception.vector = vec;
570 ctxt->exception.error_code = error;
571 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200572 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300573}
574
Joerg Roedel3b88e412011-04-04 12:39:29 +0200575static int emulate_db(struct x86_emulate_ctxt *ctxt)
576{
577 return emulate_exception(ctxt, DB_VECTOR, 0, false);
578}
579
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200580static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300581{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200582 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300583}
584
Avi Kivity618ff152011-04-03 12:32:09 +0300585static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
586{
587 return emulate_exception(ctxt, SS_VECTOR, err, true);
588}
589
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200590static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300591{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200592 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300593}
594
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200595static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300596{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200597 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300598}
599
Avi Kivity34d1f492010-08-26 11:59:01 +0300600static int emulate_de(struct x86_emulate_ctxt *ctxt)
601{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200602 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300603}
604
Avi Kivity1253791d2011-03-29 11:41:27 +0200605static int emulate_nm(struct x86_emulate_ctxt *ctxt)
606{
607 return emulate_exception(ctxt, NM_VECTOR, 0, false);
608}
609
Avi Kivity1aa36612011-04-27 13:20:30 +0300610static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
611{
612 u16 selector;
613 struct desc_struct desc;
614
615 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
616 return selector;
617}
618
619static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
620 unsigned seg)
621{
622 u16 dummy;
623 u32 base3;
624 struct desc_struct desc;
625
626 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
627 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
628}
629
Avi Kivity1c11b372012-04-09 18:39:59 +0300630/*
631 * x86 defines three classes of vector instructions: explicitly
632 * aligned, explicitly unaligned, and the rest, which change behaviour
633 * depending on whether they're AVX encoded or not.
634 *
635 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100636 * subject to the same check. FXSAVE and FXRSTOR are checked here too as their
637 * 512 bytes of data must be aligned to a 16 byte boundary.
Avi Kivity1c11b372012-04-09 18:39:59 +0300638 */
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100639static unsigned insn_alignment(struct x86_emulate_ctxt *ctxt, unsigned size)
Avi Kivity1c11b372012-04-09 18:39:59 +0300640{
641 if (likely(size < 16))
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100642 return 1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300643
644 if (ctxt->d & Aligned)
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100645 return size;
Avi Kivity1c11b372012-04-09 18:39:59 +0300646 else if (ctxt->d & Unaligned)
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100647 return 1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300648 else if (ctxt->d & Avx)
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100649 return 1;
650 else if (ctxt->d & Aligned16)
651 return 16;
Avi Kivity1c11b372012-04-09 18:39:59 +0300652 else
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100653 return size;
Avi Kivity1c11b372012-04-09 18:39:59 +0300654}
655
Paolo Bonzinid09155d2014-10-27 14:54:44 +0100656static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
657 struct segmented_address addr,
658 unsigned *max_size, unsigned size,
659 bool write, bool fetch,
Nadav Amitd50eaa12014-11-19 17:43:11 +0200660 enum x86emul_mode mode, ulong *linear)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300661{
Avi Kivity618ff152011-04-03 12:32:09 +0300662 struct desc_struct desc;
663 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300664 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300665 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300666 u16 sel;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300667
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900668 la = seg_base(ctxt, addr.seg) + addr.ea;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100669 *max_size = 0;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200670 switch (mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300671 case X86EMUL_MODE_PROT64:
Paolo Bonzini0c1d77f2016-02-19 18:07:21 +0100672 *linear = la;
Nadav Amit4be4de72014-09-18 22:39:40 +0300673 if (is_noncanonical_address(la))
Nadav Amitabc7d8a2014-11-19 17:43:12 +0200674 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100675
676 *max_size = min_t(u64, ~0u, (1ull << 48) - la);
677 if (size > *max_size)
678 goto bad;
Avi Kivity618ff152011-04-03 12:32:09 +0300679 break;
680 default:
Paolo Bonzini0c1d77f2016-02-19 18:07:21 +0100681 *linear = la = (u32)la;
Avi Kivity1aa36612011-04-27 13:20:30 +0300682 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
683 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300684 if (!usable)
685 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200686 /* code segment in protected mode or read-only data segment */
687 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
688 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300689 goto bad;
690 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400691 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300692 goto bad;
693 lim = desc_limit_scaled(&desc);
Paolo Bonzini997b0412014-11-19 18:33:38 +0100694 if (!(desc.type & 8) && (desc.type & 4)) {
Guo Chaofc058682012-06-28 15:19:51 +0800695 /* expand-down segment */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100696 if (addr.ea <= lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300697 goto bad;
698 lim = desc.d ? 0xffffffff : 0xffff;
Avi Kivity618ff152011-04-03 12:32:09 +0300699 }
Paolo Bonzini997b0412014-11-19 18:33:38 +0100700 if (addr.ea > lim)
701 goto bad;
Nadav Amitbac155312015-01-26 09:32:26 +0200702 if (lim == 0xffffffff)
703 *max_size = ~0u;
704 else {
705 *max_size = (u64)lim + 1 - addr.ea;
706 if (size > *max_size)
707 goto bad;
708 }
Avi Kivity618ff152011-04-03 12:32:09 +0300709 break;
710 }
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100711 if (la & (insn_alignment(ctxt, size) - 1))
Avi Kivity1c11b372012-04-09 18:39:59 +0300712 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300713 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300714bad:
715 if (addr.seg == VCPU_SREG_SS)
Paolo Bonzini36061892014-10-27 14:40:49 +0100716 return emulate_ss(ctxt, 0);
Avi Kivity618ff152011-04-03 12:32:09 +0300717 else
Paolo Bonzini36061892014-10-27 14:40:49 +0100718 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300719}
720
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400721static int linearize(struct x86_emulate_ctxt *ctxt,
722 struct segmented_address addr,
723 unsigned size, bool write,
724 ulong *linear)
725{
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100726 unsigned max_size;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200727 return __linearize(ctxt, addr, &max_size, size, write, false,
728 ctxt->mode, linear);
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400729}
730
Nadav Amitd50eaa12014-11-19 17:43:11 +0200731static inline int assign_eip(struct x86_emulate_ctxt *ctxt, ulong dst,
732 enum x86emul_mode mode)
733{
734 ulong linear;
735 int rc;
736 unsigned max_size;
737 struct segmented_address addr = { .seg = VCPU_SREG_CS,
738 .ea = dst };
739
740 if (ctxt->op_bytes != sizeof(unsigned long))
741 addr.ea = dst & ((1UL << (ctxt->op_bytes << 3)) - 1);
742 rc = __linearize(ctxt, addr, &max_size, 1, false, true, mode, &linear);
743 if (rc == X86EMUL_CONTINUE)
744 ctxt->_eip = addr.ea;
745 return rc;
746}
747
748static inline int assign_eip_near(struct x86_emulate_ctxt *ctxt, ulong dst)
749{
750 return assign_eip(ctxt, dst, ctxt->mode);
751}
752
753static int assign_eip_far(struct x86_emulate_ctxt *ctxt, ulong dst,
754 const struct desc_struct *cs_desc)
755{
756 enum x86emul_mode mode = ctxt->mode;
Nadav Amit82268082015-01-26 09:32:27 +0200757 int rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200758
759#ifdef CONFIG_X86_64
Nadav Amit82268082015-01-26 09:32:27 +0200760 if (ctxt->mode >= X86EMUL_MODE_PROT16) {
761 if (cs_desc->l) {
762 u64 efer = 0;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200763
Nadav Amit82268082015-01-26 09:32:27 +0200764 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
765 if (efer & EFER_LMA)
766 mode = X86EMUL_MODE_PROT64;
767 } else
768 mode = X86EMUL_MODE_PROT32; /* temporary value */
Nadav Amitd50eaa12014-11-19 17:43:11 +0200769 }
770#endif
771 if (mode == X86EMUL_MODE_PROT16 || mode == X86EMUL_MODE_PROT32)
772 mode = cs_desc->d ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
Nadav Amit82268082015-01-26 09:32:27 +0200773 rc = assign_eip(ctxt, dst, mode);
774 if (rc == X86EMUL_CONTINUE)
775 ctxt->mode = mode;
776 return rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200777}
778
779static inline int jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
780{
781 return assign_eip_near(ctxt, ctxt->_eip + rel);
782}
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400783
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200784static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
785 struct segmented_address addr,
786 void *data,
787 unsigned size)
788{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200789 int rc;
790 ulong linear;
791
Avi Kivity83b87952011-04-03 11:31:19 +0300792 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200793 if (rc != X86EMUL_CONTINUE)
794 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300795 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200796}
797
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900798/*
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200799 * Prefetch the remaining bytes of the instruction without crossing page
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900800 * boundary if they are not in fetch_cache yet.
801 */
Paolo Bonzini9506d572014-05-06 13:05:25 +0200802static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
Avi Kivity62266862007-11-20 13:15:52 +0200803{
Avi Kivity62266862007-11-20 13:15:52 +0200804 int rc;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100805 unsigned size, max_size;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200806 unsigned long linear;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200807 int cur_size = ctxt->fetch.end - ctxt->fetch.data;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200808 struct segmented_address addr = { .seg = VCPU_SREG_CS,
Paolo Bonzini17052f12014-05-06 16:33:01 +0200809 .ea = ctxt->eip + cur_size };
810
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100811 /*
812 * We do not know exactly how many bytes will be needed, and
813 * __linearize is expensive, so fetch as much as possible. We
814 * just have to avoid going beyond the 15 byte limit, the end
815 * of the segment, or the end of the page.
816 *
817 * __linearize is called with size 0 so that it does not do any
818 * boundary check itself. Instead, we use max_size to check
819 * against op_size.
820 */
Nadav Amitd50eaa12014-11-19 17:43:11 +0200821 rc = __linearize(ctxt, addr, &max_size, 0, false, true, ctxt->mode,
822 &linear);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200823 if (unlikely(rc != X86EMUL_CONTINUE))
824 return rc;
825
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100826 size = min_t(unsigned, 15UL ^ cur_size, max_size);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200827 size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear));
Paolo Bonzini5cfc7e02014-05-06 13:05:25 +0200828
829 /*
830 * One instruction can only straddle two pages,
831 * and one has been loaded at the beginning of
832 * x86_decode_insn. So, if not enough bytes
833 * still, we must have hit the 15-byte boundary.
834 */
835 if (unlikely(size < op_size))
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100836 return emulate_gp(ctxt, 0);
837
Paolo Bonzini17052f12014-05-06 16:33:01 +0200838 rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end,
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200839 size, &ctxt->exception);
840 if (unlikely(rc != X86EMUL_CONTINUE))
841 return rc;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200842 ctxt->fetch.end += size;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900843 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200844}
845
Paolo Bonzini9506d572014-05-06 13:05:25 +0200846static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt,
847 unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200848{
Nadav Amit08da44a2014-10-03 01:10:04 +0300849 unsigned done_size = ctxt->fetch.end - ctxt->fetch.ptr;
850
851 if (unlikely(done_size < size))
852 return __do_insn_fetch_bytes(ctxt, size - done_size);
Paolo Bonzini9506d572014-05-06 13:05:25 +0200853 else
854 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200855}
856
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900857/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900858#define insn_fetch(_type, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200859({ _type _x; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200860 \
861 rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900862 if (rc != X86EMUL_CONTINUE) \
863 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200864 ctxt->_eip += sizeof(_type); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200865 _x = *(_type __aligned(1) *) ctxt->fetch.ptr; \
866 ctxt->fetch.ptr += sizeof(_type); \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200867 _x; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900868})
869
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900870#define insn_fetch_arr(_arr, _size, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200871({ \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200872 rc = do_insn_fetch_bytes(_ctxt, _size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900873 if (rc != X86EMUL_CONTINUE) \
874 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200875 ctxt->_eip += (_size); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200876 memcpy(_arr, ctxt->fetch.ptr, _size); \
877 ctxt->fetch.ptr += (_size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900878})
879
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000880/*
881 * Given the 'reg' portion of a ModRM byte, and a register block, return a
882 * pointer into the block that addresses the relevant register.
883 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
884 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300885static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200886 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800887{
888 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200889 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300892 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
893 else
894 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800895 return p;
896}
897
898static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200899 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800900 u16 *size, unsigned long *address, int op_bytes)
901{
902 int rc;
903
904 if (op_bytes == 2)
905 op_bytes = 3;
906 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200907 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900908 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800909 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200910 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200911 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912 return rc;
913}
914
Avi Kivity34b77652013-01-19 19:51:56 +0200915FASTOP2(add);
916FASTOP2(or);
917FASTOP2(adc);
918FASTOP2(sbb);
919FASTOP2(and);
920FASTOP2(sub);
921FASTOP2(xor);
922FASTOP2(cmp);
923FASTOP2(test);
924
Avi Kivityb9fa4092013-02-09 11:31:48 +0200925FASTOP1SRC2(mul, mul_ex);
926FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200927FASTOP1SRC2EX(div, div_ex);
928FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +0200929
Avi Kivity34b77652013-01-19 19:51:56 +0200930FASTOP3WCL(shld);
931FASTOP3WCL(shrd);
932
933FASTOP2W(imul);
934
935FASTOP1(not);
936FASTOP1(neg);
937FASTOP1(inc);
938FASTOP1(dec);
939
940FASTOP2CL(rol);
941FASTOP2CL(ror);
942FASTOP2CL(rcl);
943FASTOP2CL(rcr);
944FASTOP2CL(shl);
945FASTOP2CL(shr);
946FASTOP2CL(sar);
947
948FASTOP2W(bsf);
949FASTOP2W(bsr);
950FASTOP2W(bt);
951FASTOP2W(bts);
952FASTOP2W(btr);
953FASTOP2W(btc);
954
Avi Kivitye47a5f52013-02-09 11:31:51 +0200955FASTOP2(xadd);
956
Nadav Amit5aca3722014-11-02 11:54:50 +0200957FASTOP2R(cmp, cmp_r);
958
Nadav Amit900efe22015-03-30 15:39:21 +0300959static int em_bsf_c(struct x86_emulate_ctxt *ctxt)
960{
961 /* If src is zero, do not writeback, but update flags */
962 if (ctxt->src.val == 0)
963 ctxt->dst.type = OP_NONE;
964 return fastop(ctxt, em_bsf);
965}
966
967static int em_bsr_c(struct x86_emulate_ctxt *ctxt)
968{
969 /* If src is zero, do not writeback, but update flags */
970 if (ctxt->src.val == 0)
971 ctxt->dst.type = OP_NONE;
972 return fastop(ctxt, em_bsr);
973}
974
Josh Poimboeufcb7390f2016-01-22 10:16:12 -0600975static __always_inline u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300976{
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200977 u8 rc;
978 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300979
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200980 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +0200981 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200982 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
983 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300984}
985
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300986static void fetch_register_operand(struct operand *op)
987{
988 switch (op->bytes) {
989 case 1:
990 op->val = *(u8 *)op->addr.reg;
991 break;
992 case 2:
993 op->val = *(u16 *)op->addr.reg;
994 break;
995 case 4:
996 op->val = *(u32 *)op->addr.reg;
997 break;
998 case 8:
999 op->val = *(u64 *)op->addr.reg;
1000 break;
1001 }
1002}
1003
Avi Kivity1253791d2011-03-29 11:41:27 +02001004static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
1005{
1006 ctxt->ops->get_fpu(ctxt);
1007 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001008 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
1009 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
1010 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
1011 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
1012 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
1013 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
1014 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
1015 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001016#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001017 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
1018 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
1019 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
1020 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
1021 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
1022 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
1023 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
1024 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001025#endif
1026 default: BUG();
1027 }
1028 ctxt->ops->put_fpu(ctxt);
1029}
1030
1031static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
1032 int reg)
1033{
1034 ctxt->ops->get_fpu(ctxt);
1035 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001036 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
1037 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
1038 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
1039 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
1040 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
1041 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
1042 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
1043 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001044#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001045 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
1046 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
1047 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
1048 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
1049 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
1050 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
1051 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
1052 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001053#endif
1054 default: BUG();
1055 }
1056 ctxt->ops->put_fpu(ctxt);
1057}
1058
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001059static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1060{
1061 ctxt->ops->get_fpu(ctxt);
1062 switch (reg) {
1063 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1064 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1065 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1066 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1067 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1068 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1069 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1070 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1071 default: BUG();
1072 }
1073 ctxt->ops->put_fpu(ctxt);
1074}
1075
1076static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1077{
1078 ctxt->ops->get_fpu(ctxt);
1079 switch (reg) {
1080 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1081 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1082 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1083 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1084 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1085 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1086 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1087 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1088 default: BUG();
1089 }
1090 ctxt->ops->put_fpu(ctxt);
1091}
1092
Gleb Natapov045a2822012-12-20 16:57:43 +02001093static int em_fninit(struct x86_emulate_ctxt *ctxt)
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("fninit");
1100 ctxt->ops->put_fpu(ctxt);
1101 return X86EMUL_CONTINUE;
1102}
1103
1104static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1105{
1106 u16 fcw;
1107
1108 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1109 return emulate_nm(ctxt);
1110
1111 ctxt->ops->get_fpu(ctxt);
1112 asm volatile("fnstcw %0": "+m"(fcw));
1113 ctxt->ops->put_fpu(ctxt);
1114
Gleb Natapov045a2822012-12-20 16:57:43 +02001115 ctxt->dst.val = fcw;
1116
1117 return X86EMUL_CONTINUE;
1118}
1119
1120static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1121{
1122 u16 fsw;
1123
1124 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1125 return emulate_nm(ctxt);
1126
1127 ctxt->ops->get_fpu(ctxt);
1128 asm volatile("fnstsw %0": "+m"(fsw));
1129 ctxt->ops->put_fpu(ctxt);
1130
Gleb Natapov045a2822012-12-20 16:57:43 +02001131 ctxt->dst.val = fsw;
1132
1133 return X86EMUL_CONTINUE;
1134}
1135
Avi Kivity1253791d2011-03-29 11:41:27 +02001136static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001137 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001138{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001139 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001140
Avi Kivity9dac77f2011-06-01 15:34:25 +03001141 if (!(ctxt->d & ModRM))
1142 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity1253791d2011-03-29 11:41:27 +02001143
Avi Kivity9dac77f2011-06-01 15:34:25 +03001144 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001145 op->type = OP_XMM;
1146 op->bytes = 16;
1147 op->addr.xmm = reg;
1148 read_sse_reg(ctxt, &op->vec_val, reg);
1149 return;
1150 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001151 if (ctxt->d & Mmx) {
1152 reg &= 7;
1153 op->type = OP_MM;
1154 op->bytes = 8;
1155 op->addr.mm = reg;
1156 return;
1157 }
Avi Kivity1253791d2011-03-29 11:41:27 +02001158
Avi Kivity3c118e22007-10-31 10:27:04 +02001159 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001160 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1161 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1162
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001163 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001164 op->orig_val = op->val;
1165}
1166
Avi Kivitya6e34072012-06-10 17:15:39 +03001167static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1168{
1169 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1170 ctxt->modrm_seg = VCPU_SREG_SS;
1171}
1172
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001173static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001174 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001175{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001176 u8 sib;
Bandan Das02357bd2014-04-16 12:46:11 -04001177 int index_reg, base_reg, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001178 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001179 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001180
Bandan Das02357bd2014-04-16 12:46:11 -04001181 ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */
1182 index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */
1183 base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001184
Bandan Das02357bd2014-04-16 12:46:11 -04001185 ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001186 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
Bandan Das02357bd2014-04-16 12:46:11 -04001187 ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001188 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001189
Nadav Amit9b88ae92014-05-25 23:05:21 +03001190 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001191 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001192 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001193 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001194 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001195 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001196 op->type = OP_XMM;
1197 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001198 op->addr.xmm = ctxt->modrm_rm;
1199 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001200 return rc;
1201 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001202 if (ctxt->d & Mmx) {
1203 op->type = OP_MM;
1204 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001205 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001206 return rc;
1207 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001208 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001209 return rc;
1210 }
1211
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001212 op->type = OP_MEM;
1213
Avi Kivity9dac77f2011-06-01 15:34:25 +03001214 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001215 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1216 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1217 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1218 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001219
1220 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001221 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001222 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001223 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001224 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001225 break;
1226 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001227 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001228 break;
1229 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001230 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001231 break;
1232 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001233 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001234 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001235 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001236 break;
1237 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001238 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001239 break;
1240 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001241 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001242 break;
1243 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001244 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001245 break;
1246 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001247 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001248 break;
1249 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001250 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001251 break;
1252 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001253 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001254 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001255 break;
1256 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001257 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001258 break;
1259 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001260 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1261 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1262 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001263 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001264 } else {
1265 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001266 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001267 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001268 index_reg |= (sib >> 3) & 7;
1269 base_reg |= sib & 7;
1270 scale = sib >> 6;
1271
Avi Kivity9dac77f2011-06-01 15:34:25 +03001272 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001273 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001274 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001275 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001276 adjust_modrm_seg(ctxt, base_reg);
Nadav Amitab708092014-12-25 02:52:21 +02001277 /* Increment ESP on POP [ESP] */
1278 if ((ctxt->d & IncSP) &&
1279 base_reg == VCPU_REGS_RSP)
1280 modrm_ea += ctxt->op_bytes;
Avi Kivitya6e34072012-06-10 17:15:39 +03001281 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001282 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001283 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001284 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Nadav Amit5b38ab82014-11-02 11:54:41 +02001285 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity84411d82008-06-15 21:53:26 -07001286 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001287 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001288 } else {
1289 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001290 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001291 adjust_modrm_seg(ctxt, base_reg);
1292 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001293 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001294 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001295 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001296 break;
1297 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001298 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001299 break;
1300 }
1301 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001302 op->addr.mem.ea = modrm_ea;
Bandan Das41061cd2014-04-16 12:46:14 -04001303 if (ctxt->ad_bytes != 8)
1304 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
1305
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001306done:
1307 return rc;
1308}
1309
1310static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001311 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001312{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001313 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001314
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001315 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001316 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001317 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001318 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001319 break;
1320 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001321 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001322 break;
1323 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001324 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001325 break;
1326 }
1327done:
1328 return rc;
1329}
1330
Avi Kivity9dac77f2011-06-01 15:34:25 +03001331static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001332{
Sheng Yang7129eec2010-09-28 16:33:32 +08001333 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001334
Avi Kivity9dac77f2011-06-01 15:34:25 +03001335 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001336 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001337
Avi Kivity9dac77f2011-06-01 15:34:25 +03001338 if (ctxt->src.bytes == 2)
1339 sv = (s16)ctxt->src.val & (s16)mask;
1340 else if (ctxt->src.bytes == 4)
1341 sv = (s32)ctxt->src.val & (s32)mask;
Nadav Amit7dec5602014-06-15 16:12:57 +03001342 else
1343 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001344
Nadav Amit1c1c35a2014-11-19 17:43:09 +02001345 ctxt->dst.addr.mem.ea = address_mask(ctxt,
1346 ctxt->dst.addr.mem.ea + (sv >> 3));
Wei Yongjun35c843c2010-08-09 11:34:56 +08001347 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001348
1349 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001350 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001351}
1352
Gleb Natapov9de41572010-04-28 19:15:22 +03001353static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001354 unsigned long addr, void *dest, unsigned size)
1355{
1356 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001357 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001358
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001359 if (mc->pos < mc->end)
1360 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001361
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001362 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001363
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001364 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1365 &ctxt->exception);
1366 if (rc != X86EMUL_CONTINUE)
1367 return rc;
1368
1369 mc->end += size;
1370
1371read_cached:
1372 memcpy(dest, mc->data + mc->pos, size);
1373 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001374 return X86EMUL_CONTINUE;
1375}
1376
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001377static int segmented_read(struct x86_emulate_ctxt *ctxt,
1378 struct segmented_address addr,
1379 void *data,
1380 unsigned size)
1381{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001382 int rc;
1383 ulong linear;
1384
Avi Kivity83b87952011-04-03 11:31:19 +03001385 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001386 if (rc != X86EMUL_CONTINUE)
1387 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001388 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001389}
1390
1391static int segmented_write(struct x86_emulate_ctxt *ctxt,
1392 struct segmented_address addr,
1393 const void *data,
1394 unsigned size)
1395{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001396 int rc;
1397 ulong linear;
1398
Avi Kivity83b87952011-04-03 11:31:19 +03001399 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001400 if (rc != X86EMUL_CONTINUE)
1401 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001402 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1403 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001404}
1405
1406static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1407 struct segmented_address addr,
1408 const void *orig_data, const void *data,
1409 unsigned size)
1410{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001411 int rc;
1412 ulong linear;
1413
Avi Kivity83b87952011-04-03 11:31:19 +03001414 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001415 if (rc != X86EMUL_CONTINUE)
1416 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001417 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1418 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001419}
1420
Gleb Natapov7b262e92010-03-18 15:20:27 +02001421static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001422 unsigned int size, unsigned short port,
1423 void *dest)
1424{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001425 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001426
1427 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001428 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001429 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001430 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Nadav Amit0efb0442015-03-29 16:33:03 +03001431 in_page = (ctxt->eflags & X86_EFLAGS_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001432 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1433 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Mark Rustadb55a8142014-07-25 06:27:05 -07001434 n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001435 if (n == 0)
1436 n = 1;
1437 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001438 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001439 return 0;
1440 rc->end = n * size;
1441 }
1442
Nadav Amite6e39f02014-04-18 03:35:10 +03001443 if (ctxt->rep_prefix && (ctxt->d & String) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03001444 !(ctxt->eflags & X86_EFLAGS_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001445 ctxt->dst.data = rc->data + rc->pos;
1446 ctxt->dst.type = OP_MEM_STR;
1447 ctxt->dst.count = (rc->end - rc->pos) / size;
1448 rc->pos = rc->end;
1449 } else {
1450 memcpy(dest, rc->data + rc->pos, size);
1451 rc->pos += size;
1452 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001453 return 1;
1454}
1455
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001456static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1457 u16 index, struct desc_struct *desc)
1458{
1459 struct desc_ptr dt;
1460 ulong addr;
1461
1462 ctxt->ops->get_idt(ctxt, &dt);
1463
1464 if (dt.size < index * 8 + 7)
1465 return emulate_gp(ctxt, index << 3 | 0x2);
1466
1467 addr = dt.address + index * 8;
1468 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1469 &ctxt->exception);
1470}
1471
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001472static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001473 u16 selector, struct desc_ptr *dt)
1474{
Mathias Krause0225fb52012-08-30 01:30:16 +02001475 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001476 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001477
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001478 if (selector & 1 << 2) {
1479 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001480 u16 sel;
1481
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001482 memset (dt, 0, sizeof *dt);
Nadav Amit2eedcac2014-06-02 18:34:05 +03001483 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1484 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001485 return;
1486
1487 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001488 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001489 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001490 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001491}
1492
Nadav Amitedccda72014-12-25 02:52:23 +02001493static int get_descriptor_ptr(struct x86_emulate_ctxt *ctxt,
1494 u16 selector, ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001495{
1496 struct desc_ptr dt;
1497 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001498 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001499
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001500 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001501
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001502 if (dt.size < index * 8 + 7)
1503 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001504
1505 addr = dt.address + index * 8;
Nadav Amitedccda72014-12-25 02:52:23 +02001506
1507#ifdef CONFIG_X86_64
1508 if (addr >> 32 != 0) {
1509 u64 efer = 0;
1510
1511 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1512 if (!(efer & EFER_LMA))
1513 addr &= (u32)-1;
1514 }
1515#endif
1516
1517 *desc_addr_p = addr;
1518 return X86EMUL_CONTINUE;
1519}
1520
1521/* allowed just for 8 bytes segments */
1522static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1523 u16 selector, struct desc_struct *desc,
1524 ulong *desc_addr_p)
1525{
1526 int rc;
1527
1528 rc = get_descriptor_ptr(ctxt, selector, desc_addr_p);
1529 if (rc != X86EMUL_CONTINUE)
1530 return rc;
1531
1532 return ctxt->ops->read_std(ctxt, *desc_addr_p, desc, sizeof(*desc),
1533 &ctxt->exception);
1534}
1535
1536/* allowed just for 8 bytes segments */
1537static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1538 u16 selector, struct desc_struct *desc)
1539{
1540 int rc;
1541 ulong addr;
1542
1543 rc = get_descriptor_ptr(ctxt, selector, &addr);
1544 if (rc != X86EMUL_CONTINUE)
1545 return rc;
1546
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001547 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1548 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001549}
1550
Gleb Natapov5601d052011-03-07 14:55:06 +02001551/* Does not support long mode */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001552static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Nadav Amitd1442d82014-09-18 22:39:39 +03001553 u16 selector, int seg, u8 cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001554 enum x86_transfer_type transfer,
Nadav Amitd1442d82014-09-18 22:39:39 +03001555 struct desc_struct *desc)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001556{
Avi Kivity869be992012-06-13 16:30:53 +03001557 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001558 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001559 unsigned err_vec = GP_VECTOR;
1560 u32 err_code = 0;
1561 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001562 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001563 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001564 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001565 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001566
1567 memset(&seg_desc, 0, sizeof seg_desc);
1568
Kevin Wolff8da94e2013-04-11 14:06:03 +02001569 if (ctxt->mode == X86EMUL_MODE_REAL) {
1570 /* set real mode segment descriptor (keep limit etc. for
1571 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001572 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001573 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001574 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001575 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1576 /* VM86 needs a clean new segment descriptor */
1577 set_desc_base(&seg_desc, selector << 4);
1578 set_desc_limit(&seg_desc, 0xffff);
1579 seg_desc.type = 3;
1580 seg_desc.p = 1;
1581 seg_desc.s = 1;
1582 seg_desc.dpl = 3;
1583 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001584 }
1585
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001586 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001587
1588 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1589 if ((seg == VCPU_SREG_CS
1590 || (seg == VCPU_SREG_SS
1591 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1592 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001593 && null_selector)
1594 goto exception;
1595
1596 /* TR should be in GDT only */
1597 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1598 goto exception;
1599
1600 if (null_selector) /* for NULL selector skip all following checks */
1601 goto load;
1602
Avi Kivitye9194642012-06-13 16:29:39 +03001603 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001604 if (ret != X86EMUL_CONTINUE)
1605 return ret;
1606
1607 err_code = selector & 0xfffc;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001608 err_vec = (transfer == X86_TRANSFER_TASK_SWITCH) ? TS_VECTOR :
1609 GP_VECTOR;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001610
Guo Chaofc058682012-06-28 15:19:51 +08001611 /* can't load system descriptor into segment selector */
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001612 if (seg <= VCPU_SREG_GS && !seg_desc.s) {
1613 if (transfer == X86_TRANSFER_CALL_JMP)
1614 return X86EMUL_UNHANDLEABLE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001615 goto exception;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001616 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001617
1618 if (!seg_desc.p) {
1619 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1620 goto exception;
1621 }
1622
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001623 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001624
1625 switch (seg) {
1626 case VCPU_SREG_SS:
1627 /*
1628 * segment is not a writable data segment or segment
1629 * selector's RPL != CPL or segment selector's RPL != CPL
1630 */
1631 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1632 goto exception;
1633 break;
1634 case VCPU_SREG_CS:
1635 if (!(seg_desc.type & 8))
1636 goto exception;
1637
1638 if (seg_desc.type & 4) {
1639 /* conforming */
1640 if (dpl > cpl)
1641 goto exception;
1642 } else {
1643 /* nonconforming */
1644 if (rpl > cpl || dpl != cpl)
1645 goto exception;
1646 }
Nadav Amit040c8dc2014-09-18 22:39:43 +03001647 /* in long-mode d/b must be clear if l is set */
1648 if (seg_desc.d && seg_desc.l) {
1649 u64 efer = 0;
1650
1651 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1652 if (efer & EFER_LMA)
1653 goto exception;
1654 }
1655
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001656 /* CS(RPL) <- CPL */
1657 selector = (selector & 0xfffc) | cpl;
1658 break;
1659 case VCPU_SREG_TR:
1660 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1661 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001662 old_desc = seg_desc;
1663 seg_desc.type |= 2; /* busy */
1664 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1665 sizeof(seg_desc), &ctxt->exception);
1666 if (ret != X86EMUL_CONTINUE)
1667 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001668 break;
1669 case VCPU_SREG_LDTR:
1670 if (seg_desc.s || seg_desc.type != 2)
1671 goto exception;
1672 break;
1673 default: /* DS, ES, FS, or GS */
1674 /*
1675 * segment is not a data or readable code segment or
1676 * ((segment is a data or nonconforming code segment)
1677 * and (both RPL and CPL > DPL))
1678 */
1679 if ((seg_desc.type & 0xa) == 0x8 ||
1680 (((seg_desc.type & 0xc) != 0xc) &&
1681 (rpl > dpl && cpl > dpl)))
1682 goto exception;
1683 break;
1684 }
1685
1686 if (seg_desc.s) {
1687 /* mark segment as accessed */
Nadav Amite2cefa72014-12-25 02:52:22 +02001688 if (!(seg_desc.type & 1)) {
1689 seg_desc.type |= 1;
1690 ret = write_segment_descriptor(ctxt, selector,
1691 &seg_desc);
1692 if (ret != X86EMUL_CONTINUE)
1693 return ret;
1694 }
Nadav Amite37a75a2014-06-02 18:34:04 +03001695 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
1696 ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3,
1697 sizeof(base3), &ctxt->exception);
1698 if (ret != X86EMUL_CONTINUE)
1699 return ret;
Nadav Amit9a9abf62014-11-02 11:54:56 +02001700 if (is_noncanonical_address(get_desc_base(&seg_desc) |
1701 ((u64)base3 << 32)))
1702 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001703 }
1704load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001705 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Nadav Amitd1442d82014-09-18 22:39:39 +03001706 if (desc)
1707 *desc = seg_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001708 return X86EMUL_CONTINUE;
1709exception:
Paolo Bonzini592f0852014-08-20 10:05:08 +02001710 return emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001711}
1712
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001713static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1714 u16 selector, int seg)
1715{
1716 u8 cpl = ctxt->ops->cpl(ctxt);
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001717 return __load_segment_descriptor(ctxt, selector, seg, cpl,
1718 X86_TRANSFER_NONE, NULL);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001719}
1720
Wei Yongjun31be40b2010-08-17 09:17:30 +08001721static void write_register_operand(struct operand *op)
1722{
Nadav Amit6fd8e122015-03-30 15:39:20 +03001723 return assign_register(op->addr.reg, op->val, op->bytes);
Wei Yongjun31be40b2010-08-17 09:17:30 +08001724}
1725
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001726static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001727{
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001728 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001729 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001730 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001731 break;
1732 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001733 if (ctxt->lock_prefix)
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001734 return segmented_cmpxchg(ctxt,
1735 op->addr.mem,
1736 &op->orig_val,
1737 &op->val,
1738 op->bytes);
1739 else
1740 return segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001741 op->addr.mem,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001742 &op->val,
1743 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001744 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001745 case OP_MEM_STR:
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001746 return segmented_write(ctxt,
1747 op->addr.mem,
1748 op->data,
1749 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001750 break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001751 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001752 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001753 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001754 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001755 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001756 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001757 case OP_NONE:
1758 /* no writeback */
1759 break;
1760 default:
1761 break;
1762 }
1763 return X86EMUL_CONTINUE;
1764}
1765
Avi Kivity51ddff52012-06-12 20:19:40 +03001766static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001767{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001768 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001769
Avi Kivity5ad105e2012-08-19 14:34:31 +03001770 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001771 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001772 addr.seg = VCPU_SREG_SS;
1773
Avi Kivity51ddff52012-06-12 20:19:40 +03001774 return segmented_write(ctxt, addr, data, bytes);
1775}
1776
1777static int em_push(struct x86_emulate_ctxt *ctxt)
1778{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001779 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001780 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001781 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001782}
1783
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001784static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001785 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001786{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001787 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001788 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001789
Avi Kivitydd856ef2012-08-27 23:46:17 +03001790 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001791 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001792 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001793 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001794 return rc;
1795
Avi Kivity5ad105e2012-08-19 14:34:31 +03001796 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001797 return rc;
1798}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001799
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001800static int em_pop(struct x86_emulate_ctxt *ctxt)
1801{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001802 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001803}
1804
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001805static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001806 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001807{
1808 int rc;
1809 unsigned long val, change_mask;
Nadav Amit0efb0442015-03-29 16:33:03 +03001810 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001811 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001812
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001813 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001814 if (rc != X86EMUL_CONTINUE)
1815 return rc;
1816
Nadav Amit0efb0442015-03-29 16:33:03 +03001817 change_mask = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
1818 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF |
1819 X86_EFLAGS_TF | X86_EFLAGS_DF | X86_EFLAGS_NT |
1820 X86_EFLAGS_AC | X86_EFLAGS_ID;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001821
1822 switch(ctxt->mode) {
1823 case X86EMUL_MODE_PROT64:
1824 case X86EMUL_MODE_PROT32:
1825 case X86EMUL_MODE_PROT16:
1826 if (cpl == 0)
Nadav Amit0efb0442015-03-29 16:33:03 +03001827 change_mask |= X86_EFLAGS_IOPL;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001828 if (cpl <= iopl)
Nadav Amit0efb0442015-03-29 16:33:03 +03001829 change_mask |= X86_EFLAGS_IF;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001830 break;
1831 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001832 if (iopl < 3)
1833 return emulate_gp(ctxt, 0);
Nadav Amit0efb0442015-03-29 16:33:03 +03001834 change_mask |= X86_EFLAGS_IF;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001835 break;
1836 default: /* real mode */
Nadav Amit0efb0442015-03-29 16:33:03 +03001837 change_mask |= (X86_EFLAGS_IOPL | X86_EFLAGS_IF);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001838 break;
1839 }
1840
1841 *(unsigned long *)dest =
1842 (ctxt->eflags & ~change_mask) | (val & change_mask);
1843
1844 return rc;
1845}
1846
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001847static int em_popf(struct x86_emulate_ctxt *ctxt)
1848{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001849 ctxt->dst.type = OP_REG;
1850 ctxt->dst.addr.reg = &ctxt->eflags;
1851 ctxt->dst.bytes = ctxt->op_bytes;
1852 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001853}
1854
Avi Kivity612e89f2012-06-12 20:03:23 +03001855static int em_enter(struct x86_emulate_ctxt *ctxt)
1856{
1857 int rc;
1858 unsigned frame_size = ctxt->src.val;
1859 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001860 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001861
1862 if (nesting_level)
1863 return X86EMUL_UNHANDLEABLE;
1864
Avi Kivitydd856ef2012-08-27 23:46:17 +03001865 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1866 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001867 if (rc != X86EMUL_CONTINUE)
1868 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001869 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001870 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001871 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1872 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001873 stack_mask(ctxt));
1874 return X86EMUL_CONTINUE;
1875}
1876
Avi Kivityf47cfa32012-06-07 17:49:24 +03001877static int em_leave(struct x86_emulate_ctxt *ctxt)
1878{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001879 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001880 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001881 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001882}
1883
Avi Kivity1cd196e2011-09-13 10:45:51 +03001884static int em_push_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;
1887
Avi Kivity9dac77f2011-06-01 15:34:25 +03001888 ctxt->src.val = get_segment_selector(ctxt, seg);
Nadav Amit0fcc2072014-11-02 11:54:51 +02001889 if (ctxt->op_bytes == 4) {
1890 rsp_increment(ctxt, -2);
1891 ctxt->op_bytes = 2;
1892 }
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001893
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001894 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001895}
1896
Avi Kivity1cd196e2011-09-13 10:45:51 +03001897static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001898{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001899 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001900 unsigned long selector;
1901 int rc;
1902
Nadav Amit3313bc42014-12-25 02:52:17 +02001903 rc = emulate_pop(ctxt, &selector, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001904 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001905 return rc;
1906
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001907 if (ctxt->modrm_reg == VCPU_SREG_SS)
1908 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Nadav Amit3313bc42014-12-25 02:52:17 +02001909 if (ctxt->op_bytes > 2)
1910 rsp_increment(ctxt, ctxt->op_bytes - 2);
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001911
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001912 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001913 return rc;
1914}
1915
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001916static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001917{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001918 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001919 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001920 int reg = VCPU_REGS_RAX;
1921
1922 while (reg <= VCPU_REGS_RDI) {
1923 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001924 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001925
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001926 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001927 if (rc != X86EMUL_CONTINUE)
1928 return rc;
1929
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001930 ++reg;
1931 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001932
Wei Yongjunc37eda12010-06-15 09:03:33 +08001933 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001934}
1935
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001936static int em_pushf(struct x86_emulate_ctxt *ctxt)
1937{
Nadav Amit0efb0442015-03-29 16:33:03 +03001938 ctxt->src.val = (unsigned long)ctxt->eflags & ~X86_EFLAGS_VM;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001939 return em_push(ctxt);
1940}
1941
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001942static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001943{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001944 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001945 int reg = VCPU_REGS_RDI;
Nadav Amit6fd8e122015-03-30 15:39:20 +03001946 u32 val;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001947
1948 while (reg >= VCPU_REGS_RAX) {
1949 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001950 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001951 --reg;
1952 }
1953
Nadav Amit6fd8e122015-03-30 15:39:20 +03001954 rc = emulate_pop(ctxt, &val, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001955 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001956 break;
Nadav Amit6fd8e122015-03-30 15:39:20 +03001957 assign_register(reg_rmw(ctxt, reg), val, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001958 --reg;
1959 }
1960 return rc;
1961}
1962
Avi Kivitydd856ef2012-08-27 23:46:17 +03001963static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001964{
Mathias Krause0225fb52012-08-30 01:30:16 +02001965 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001966 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001967 struct desc_ptr dt;
1968 gva_t cs_addr;
1969 gva_t eip_addr;
1970 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001971
1972 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001973 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001974 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001975 if (rc != X86EMUL_CONTINUE)
1976 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001977
Nadav Amit0efb0442015-03-29 16:33:03 +03001978 ctxt->eflags &= ~(X86_EFLAGS_IF | X86_EFLAGS_TF | X86_EFLAGS_AC);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001979
Avi Kivity9dac77f2011-06-01 15:34:25 +03001980 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001981 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001982 if (rc != X86EMUL_CONTINUE)
1983 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001984
Avi Kivity9dac77f2011-06-01 15:34:25 +03001985 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001986 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001987 if (rc != X86EMUL_CONTINUE)
1988 return rc;
1989
Avi Kivity4bff1e862011-04-20 13:37:53 +03001990 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001991
1992 eip_addr = dt.address + (irq << 2);
1993 cs_addr = dt.address + (irq << 2) + 2;
1994
Avi Kivity0f65dd72011-04-20 13:37:53 +03001995 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001996 if (rc != X86EMUL_CONTINUE)
1997 return rc;
1998
Avi Kivity0f65dd72011-04-20 13:37:53 +03001999 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002000 if (rc != X86EMUL_CONTINUE)
2001 return rc;
2002
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002003 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002004 if (rc != X86EMUL_CONTINUE)
2005 return rc;
2006
Avi Kivity9dac77f2011-06-01 15:34:25 +03002007 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002008
2009 return rc;
2010}
2011
Avi Kivitydd856ef2012-08-27 23:46:17 +03002012int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
2013{
2014 int rc;
2015
2016 invalidate_registers(ctxt);
2017 rc = __emulate_int_real(ctxt, irq);
2018 if (rc == X86EMUL_CONTINUE)
2019 writeback_registers(ctxt);
2020 return rc;
2021}
2022
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002023static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002024{
2025 switch(ctxt->mode) {
2026 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03002027 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002028 case X86EMUL_MODE_VM86:
2029 case X86EMUL_MODE_PROT16:
2030 case X86EMUL_MODE_PROT32:
2031 case X86EMUL_MODE_PROT64:
2032 default:
2033 /* Protected mode interrupts unimplemented yet */
2034 return X86EMUL_UNHANDLEABLE;
2035 }
2036}
2037
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002038static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002039{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002040 int rc = X86EMUL_CONTINUE;
2041 unsigned long temp_eip = 0;
2042 unsigned long temp_eflags = 0;
2043 unsigned long cs = 0;
Nadav Amit0efb0442015-03-29 16:33:03 +03002044 unsigned long mask = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
2045 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_TF |
2046 X86_EFLAGS_IF | X86_EFLAGS_DF | X86_EFLAGS_OF |
2047 X86_EFLAGS_IOPL | X86_EFLAGS_NT | X86_EFLAGS_RF |
2048 X86_EFLAGS_AC | X86_EFLAGS_ID |
Wanpeng Li35fd68a2015-04-08 14:08:14 +08002049 X86_EFLAGS_FIXED;
Nadav Amit0efb0442015-03-29 16:33:03 +03002050 unsigned long vm86_mask = X86_EFLAGS_VM | X86_EFLAGS_VIF |
2051 X86_EFLAGS_VIP;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002052
2053 /* TODO: Add stack limit check */
2054
Avi Kivity9dac77f2011-06-01 15:34:25 +03002055 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002056
2057 if (rc != X86EMUL_CONTINUE)
2058 return rc;
2059
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002060 if (temp_eip & ~0xffff)
2061 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002062
Avi Kivity9dac77f2011-06-01 15:34:25 +03002063 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002064
2065 if (rc != X86EMUL_CONTINUE)
2066 return rc;
2067
Avi Kivity9dac77f2011-06-01 15:34:25 +03002068 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002069
2070 if (rc != X86EMUL_CONTINUE)
2071 return rc;
2072
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002073 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002074
2075 if (rc != X86EMUL_CONTINUE)
2076 return rc;
2077
Avi Kivity9dac77f2011-06-01 15:34:25 +03002078 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002079
Avi Kivity9dac77f2011-06-01 15:34:25 +03002080 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002081 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03002082 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002083 ctxt->eflags &= ~0xffff;
2084 ctxt->eflags |= temp_eflags;
2085 }
2086
2087 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
Wanpeng Li35fd68a2015-04-08 14:08:14 +08002088 ctxt->eflags |= X86_EFLAGS_FIXED;
Nadav Amit801806d2015-01-26 09:32:23 +02002089 ctxt->ops->set_nmi_mask(ctxt, false);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002090
2091 return rc;
2092}
2093
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002094static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002095{
2096 switch(ctxt->mode) {
2097 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002098 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002099 case X86EMUL_MODE_VM86:
2100 case X86EMUL_MODE_PROT16:
2101 case X86EMUL_MODE_PROT32:
2102 case X86EMUL_MODE_PROT64:
2103 default:
2104 /* iret from protected mode unimplemented yet */
2105 return X86EMUL_UNHANDLEABLE;
2106 }
2107}
2108
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002109static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2110{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002111 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002112 unsigned short sel, old_sel;
2113 struct desc_struct old_desc, new_desc;
2114 const struct x86_emulate_ops *ops = ctxt->ops;
2115 u8 cpl = ctxt->ops->cpl(ctxt);
2116
2117 /* Assignment of RIP may only fail in 64-bit mode */
2118 if (ctxt->mode == X86EMUL_MODE_PROT64)
2119 ops->get_segment(ctxt, &old_sel, &old_desc, NULL,
2120 VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002121
Avi Kivity9dac77f2011-06-01 15:34:25 +03002122 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002123
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002124 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
2125 X86_TRANSFER_CALL_JMP,
Nadav Amitd1442d82014-09-18 22:39:39 +03002126 &new_desc);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002127 if (rc != X86EMUL_CONTINUE)
2128 return rc;
2129
Nadav Amitd50eaa12014-11-19 17:43:11 +02002130 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03002131 if (rc != X86EMUL_CONTINUE) {
Nadav Amit7e46ddd2014-10-28 00:03:43 +02002132 WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64);
Nadav Amitd1442d82014-09-18 22:39:39 +03002133 /* assigning eip failed; restore the old cs */
2134 ops->set_segment(ctxt, old_sel, &old_desc, 0, VCPU_SREG_CS);
2135 return rc;
2136 }
2137 return rc;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002138}
2139
Nadav Amitf7784042014-09-18 22:39:41 +03002140static int em_jmp_abs(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002141{
Nadav Amitf7784042014-09-18 22:39:41 +03002142 return assign_eip_near(ctxt, ctxt->src.val);
2143}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002144
Nadav Amitf7784042014-09-18 22:39:41 +03002145static int em_call_near_abs(struct x86_emulate_ctxt *ctxt)
2146{
2147 int rc;
2148 long int old_eip;
2149
2150 old_eip = ctxt->_eip;
2151 rc = assign_eip_near(ctxt, ctxt->src.val);
2152 if (rc != X86EMUL_CONTINUE)
2153 return rc;
2154 ctxt->src.val = old_eip;
2155 rc = em_push(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002156 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002157}
2158
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002159static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002160{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002161 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002162
Nadav Amitaaa05f22014-06-02 18:34:10 +03002163 if (ctxt->dst.bytes == 16)
2164 return X86EMUL_UNHANDLEABLE;
2165
Avi Kivitydd856ef2012-08-27 23:46:17 +03002166 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2167 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2168 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2169 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Nadav Amit0efb0442015-03-29 16:33:03 +03002170 ctxt->eflags &= ~X86_EFLAGS_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002171 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002172 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2173 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002174
Nadav Amit0efb0442015-03-29 16:33:03 +03002175 ctxt->eflags |= X86_EFLAGS_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002176 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002177 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002178}
2179
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002180static int em_ret(struct x86_emulate_ctxt *ctxt)
2181{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002182 int rc;
2183 unsigned long eip;
2184
2185 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
2186 if (rc != X86EMUL_CONTINUE)
2187 return rc;
2188
2189 return assign_eip_near(ctxt, eip);
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002190}
2191
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002192static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002193{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002194 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002195 unsigned long eip, cs;
2196 u16 old_cs;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002197 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amitd1442d82014-09-18 22:39:39 +03002198 struct desc_struct old_desc, new_desc;
2199 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002200
Nadav Amitd1442d82014-09-18 22:39:39 +03002201 if (ctxt->mode == X86EMUL_MODE_PROT64)
2202 ops->get_segment(ctxt, &old_cs, &old_desc, NULL,
2203 VCPU_SREG_CS);
2204
2205 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002206 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002207 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002208 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002209 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002210 return rc;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002211 /* Outer-privilege level return is not implemented */
2212 if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2213 return X86EMUL_UNHANDLEABLE;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002214 rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, cpl,
2215 X86_TRANSFER_RET,
Nadav Amitd1442d82014-09-18 22:39:39 +03002216 &new_desc);
2217 if (rc != X86EMUL_CONTINUE)
2218 return rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +02002219 rc = assign_eip_far(ctxt, eip, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03002220 if (rc != X86EMUL_CONTINUE) {
Nadav Amit7e46ddd2014-10-28 00:03:43 +02002221 WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64);
Nadav Amitd1442d82014-09-18 22:39:39 +03002222 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
2223 }
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002224 return rc;
2225}
2226
Bruce Rogers32611072013-09-09 09:40:20 -06002227static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2228{
2229 int rc;
2230
2231 rc = em_ret_far(ctxt);
2232 if (rc != X86EMUL_CONTINUE)
2233 return rc;
2234 rsp_increment(ctxt, ctxt->src.val);
2235 return X86EMUL_CONTINUE;
2236}
2237
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002238static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2239{
2240 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002241 ctxt->dst.orig_val = ctxt->dst.val;
2242 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002243 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002244 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002245 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002246
Nadav Amit0efb0442015-03-29 16:33:03 +03002247 if (ctxt->eflags & X86_EFLAGS_ZF) {
Nadav Amit2fcf5c82015-01-26 09:32:21 +02002248 /* Success: write back to memory; no update of EAX */
2249 ctxt->src.type = OP_NONE;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002250 ctxt->dst.val = ctxt->src.orig_val;
2251 } else {
2252 /* Failure: write the value we saw to EAX. */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02002253 ctxt->src.type = OP_REG;
2254 ctxt->src.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
2255 ctxt->src.val = ctxt->dst.orig_val;
2256 /* Create write-cycle to dest by writing the same value */
Nadav Amit37c564f2014-06-02 18:34:07 +03002257 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002258 }
2259 return X86EMUL_CONTINUE;
2260}
2261
Avi Kivityd4b43252011-09-13 10:45:50 +03002262static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002263{
Avi Kivityd4b43252011-09-13 10:45:50 +03002264 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002265 unsigned short sel;
2266 int rc;
2267
Avi Kivity9dac77f2011-06-01 15:34:25 +03002268 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002269
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002270 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002271 if (rc != X86EMUL_CONTINUE)
2272 return rc;
2273
Avi Kivity9dac77f2011-06-01 15:34:25 +03002274 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002275 return rc;
2276}
2277
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002278static int emulator_has_longmode(struct x86_emulate_ctxt *ctxt)
2279{
2280 u32 eax, ebx, ecx, edx;
2281
2282 eax = 0x80000001;
2283 ecx = 0;
2284 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2285 return edx & bit(X86_FEATURE_LM);
2286}
2287
2288#define GET_SMSTATE(type, smbase, offset) \
2289 ({ \
2290 type __val; \
Radim Krčmářf40606b2015-10-30 16:36:25 +01002291 int r = ctxt->ops->read_phys(ctxt, smbase + offset, &__val, \
2292 sizeof(__val)); \
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002293 if (r != X86EMUL_CONTINUE) \
2294 return X86EMUL_UNHANDLEABLE; \
2295 __val; \
2296 })
2297
2298static void rsm_set_desc_flags(struct desc_struct *desc, u32 flags)
2299{
2300 desc->g = (flags >> 23) & 1;
2301 desc->d = (flags >> 22) & 1;
2302 desc->l = (flags >> 21) & 1;
2303 desc->avl = (flags >> 20) & 1;
2304 desc->p = (flags >> 15) & 1;
2305 desc->dpl = (flags >> 13) & 3;
2306 desc->s = (flags >> 12) & 1;
2307 desc->type = (flags >> 8) & 15;
2308}
2309
2310static int rsm_load_seg_32(struct x86_emulate_ctxt *ctxt, u64 smbase, int n)
2311{
2312 struct desc_struct desc;
2313 int offset;
2314 u16 selector;
2315
2316 selector = GET_SMSTATE(u32, smbase, 0x7fa8 + n * 4);
2317
2318 if (n < 3)
2319 offset = 0x7f84 + n * 12;
2320 else
2321 offset = 0x7f2c + (n - 3) * 12;
2322
2323 set_desc_base(&desc, GET_SMSTATE(u32, smbase, offset + 8));
2324 set_desc_limit(&desc, GET_SMSTATE(u32, smbase, offset + 4));
2325 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smbase, offset));
2326 ctxt->ops->set_segment(ctxt, selector, &desc, 0, n);
2327 return X86EMUL_CONTINUE;
2328}
2329
2330static int rsm_load_seg_64(struct x86_emulate_ctxt *ctxt, u64 smbase, int n)
2331{
2332 struct desc_struct desc;
2333 int offset;
2334 u16 selector;
2335 u32 base3;
2336
2337 offset = 0x7e00 + n * 16;
2338
2339 selector = GET_SMSTATE(u16, smbase, offset);
2340 rsm_set_desc_flags(&desc, GET_SMSTATE(u16, smbase, offset + 2) << 8);
2341 set_desc_limit(&desc, GET_SMSTATE(u32, smbase, offset + 4));
2342 set_desc_base(&desc, GET_SMSTATE(u32, smbase, offset + 8));
2343 base3 = GET_SMSTATE(u32, smbase, offset + 12);
2344
2345 ctxt->ops->set_segment(ctxt, selector, &desc, base3, n);
2346 return X86EMUL_CONTINUE;
2347}
2348
2349static int rsm_enter_protected_mode(struct x86_emulate_ctxt *ctxt,
2350 u64 cr0, u64 cr4)
2351{
2352 int bad;
2353
2354 /*
2355 * First enable PAE, long mode needs it before CR0.PG = 1 is set.
2356 * Then enable protected mode. However, PCID cannot be enabled
2357 * if EFER.LMA=0, so set it separately.
2358 */
2359 bad = ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PCIDE);
2360 if (bad)
2361 return X86EMUL_UNHANDLEABLE;
2362
2363 bad = ctxt->ops->set_cr(ctxt, 0, cr0);
2364 if (bad)
2365 return X86EMUL_UNHANDLEABLE;
2366
2367 if (cr4 & X86_CR4_PCIDE) {
2368 bad = ctxt->ops->set_cr(ctxt, 4, cr4);
2369 if (bad)
2370 return X86EMUL_UNHANDLEABLE;
2371 }
2372
2373 return X86EMUL_CONTINUE;
2374}
2375
2376static int rsm_load_state_32(struct x86_emulate_ctxt *ctxt, u64 smbase)
2377{
2378 struct desc_struct desc;
2379 struct desc_ptr dt;
2380 u16 selector;
2381 u32 val, cr0, cr4;
2382 int i;
2383
2384 cr0 = GET_SMSTATE(u32, smbase, 0x7ffc);
2385 ctxt->ops->set_cr(ctxt, 3, GET_SMSTATE(u32, smbase, 0x7ff8));
2386 ctxt->eflags = GET_SMSTATE(u32, smbase, 0x7ff4) | X86_EFLAGS_FIXED;
2387 ctxt->_eip = GET_SMSTATE(u32, smbase, 0x7ff0);
2388
2389 for (i = 0; i < 8; i++)
2390 *reg_write(ctxt, i) = GET_SMSTATE(u32, smbase, 0x7fd0 + i * 4);
2391
2392 val = GET_SMSTATE(u32, smbase, 0x7fcc);
2393 ctxt->ops->set_dr(ctxt, 6, (val & DR6_VOLATILE) | DR6_FIXED_1);
2394 val = GET_SMSTATE(u32, smbase, 0x7fc8);
2395 ctxt->ops->set_dr(ctxt, 7, (val & DR7_VOLATILE) | DR7_FIXED_1);
2396
2397 selector = GET_SMSTATE(u32, smbase, 0x7fc4);
2398 set_desc_base(&desc, GET_SMSTATE(u32, smbase, 0x7f64));
2399 set_desc_limit(&desc, GET_SMSTATE(u32, smbase, 0x7f60));
2400 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smbase, 0x7f5c));
2401 ctxt->ops->set_segment(ctxt, selector, &desc, 0, VCPU_SREG_TR);
2402
2403 selector = GET_SMSTATE(u32, smbase, 0x7fc0);
2404 set_desc_base(&desc, GET_SMSTATE(u32, smbase, 0x7f80));
2405 set_desc_limit(&desc, GET_SMSTATE(u32, smbase, 0x7f7c));
2406 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smbase, 0x7f78));
2407 ctxt->ops->set_segment(ctxt, selector, &desc, 0, VCPU_SREG_LDTR);
2408
2409 dt.address = GET_SMSTATE(u32, smbase, 0x7f74);
2410 dt.size = GET_SMSTATE(u32, smbase, 0x7f70);
2411 ctxt->ops->set_gdt(ctxt, &dt);
2412
2413 dt.address = GET_SMSTATE(u32, smbase, 0x7f58);
2414 dt.size = GET_SMSTATE(u32, smbase, 0x7f54);
2415 ctxt->ops->set_idt(ctxt, &dt);
2416
2417 for (i = 0; i < 6; i++) {
2418 int r = rsm_load_seg_32(ctxt, smbase, i);
2419 if (r != X86EMUL_CONTINUE)
2420 return r;
2421 }
2422
2423 cr4 = GET_SMSTATE(u32, smbase, 0x7f14);
2424
2425 ctxt->ops->set_smbase(ctxt, GET_SMSTATE(u32, smbase, 0x7ef8));
2426
2427 return rsm_enter_protected_mode(ctxt, cr0, cr4);
2428}
2429
2430static int rsm_load_state_64(struct x86_emulate_ctxt *ctxt, u64 smbase)
2431{
2432 struct desc_struct desc;
2433 struct desc_ptr dt;
2434 u64 val, cr0, cr4;
2435 u32 base3;
2436 u16 selector;
Paolo Bonzinib10d92a2015-10-14 15:25:52 +02002437 int i, r;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002438
2439 for (i = 0; i < 16; i++)
2440 *reg_write(ctxt, i) = GET_SMSTATE(u64, smbase, 0x7ff8 - i * 8);
2441
2442 ctxt->_eip = GET_SMSTATE(u64, smbase, 0x7f78);
2443 ctxt->eflags = GET_SMSTATE(u32, smbase, 0x7f70) | X86_EFLAGS_FIXED;
2444
2445 val = GET_SMSTATE(u32, smbase, 0x7f68);
2446 ctxt->ops->set_dr(ctxt, 6, (val & DR6_VOLATILE) | DR6_FIXED_1);
2447 val = GET_SMSTATE(u32, smbase, 0x7f60);
2448 ctxt->ops->set_dr(ctxt, 7, (val & DR7_VOLATILE) | DR7_FIXED_1);
2449
2450 cr0 = GET_SMSTATE(u64, smbase, 0x7f58);
2451 ctxt->ops->set_cr(ctxt, 3, GET_SMSTATE(u64, smbase, 0x7f50));
2452 cr4 = GET_SMSTATE(u64, smbase, 0x7f48);
2453 ctxt->ops->set_smbase(ctxt, GET_SMSTATE(u32, smbase, 0x7f00));
2454 val = GET_SMSTATE(u64, smbase, 0x7ed0);
2455 ctxt->ops->set_msr(ctxt, MSR_EFER, val & ~EFER_LMA);
2456
2457 selector = GET_SMSTATE(u32, smbase, 0x7e90);
2458 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smbase, 0x7e92) << 8);
2459 set_desc_limit(&desc, GET_SMSTATE(u32, smbase, 0x7e94));
2460 set_desc_base(&desc, GET_SMSTATE(u32, smbase, 0x7e98));
2461 base3 = GET_SMSTATE(u32, smbase, 0x7e9c);
2462 ctxt->ops->set_segment(ctxt, selector, &desc, base3, VCPU_SREG_TR);
2463
2464 dt.size = GET_SMSTATE(u32, smbase, 0x7e84);
2465 dt.address = GET_SMSTATE(u64, smbase, 0x7e88);
2466 ctxt->ops->set_idt(ctxt, &dt);
2467
2468 selector = GET_SMSTATE(u32, smbase, 0x7e70);
2469 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smbase, 0x7e72) << 8);
2470 set_desc_limit(&desc, GET_SMSTATE(u32, smbase, 0x7e74));
2471 set_desc_base(&desc, GET_SMSTATE(u32, smbase, 0x7e78));
2472 base3 = GET_SMSTATE(u32, smbase, 0x7e7c);
2473 ctxt->ops->set_segment(ctxt, selector, &desc, base3, VCPU_SREG_LDTR);
2474
2475 dt.size = GET_SMSTATE(u32, smbase, 0x7e64);
2476 dt.address = GET_SMSTATE(u64, smbase, 0x7e68);
2477 ctxt->ops->set_gdt(ctxt, &dt);
2478
Paolo Bonzinib10d92a2015-10-14 15:25:52 +02002479 r = rsm_enter_protected_mode(ctxt, cr0, cr4);
2480 if (r != X86EMUL_CONTINUE)
2481 return r;
2482
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002483 for (i = 0; i < 6; i++) {
Paolo Bonzinib10d92a2015-10-14 15:25:52 +02002484 r = rsm_load_seg_64(ctxt, smbase, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002485 if (r != X86EMUL_CONTINUE)
2486 return r;
2487 }
2488
Paolo Bonzinib10d92a2015-10-14 15:25:52 +02002489 return X86EMUL_CONTINUE;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002490}
2491
Paolo Bonzini64d60672015-05-07 11:36:11 +02002492static int em_rsm(struct x86_emulate_ctxt *ctxt)
2493{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002494 unsigned long cr0, cr4, efer;
2495 u64 smbase;
2496 int ret;
2497
Paolo Bonzini64d60672015-05-07 11:36:11 +02002498 if ((ctxt->emul_flags & X86EMUL_SMM_MASK) == 0)
2499 return emulate_ud(ctxt);
2500
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002501 /*
2502 * Get back to real mode, to prepare a safe state in which to load
Paolo Bonzini89651a32015-11-03 13:43:05 +01002503 * CR0/CR3/CR4/EFER. It's all a bit more complicated if the vCPU
2504 * supports long mode.
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002505 */
Paolo Bonzini89651a32015-11-03 13:43:05 +01002506 cr4 = ctxt->ops->get_cr(ctxt, 4);
2507 if (emulator_has_longmode(ctxt)) {
2508 struct desc_struct cs_desc;
2509
2510 /* Zero CR4.PCIDE before CR0.PG. */
2511 if (cr4 & X86_CR4_PCIDE) {
2512 ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PCIDE);
2513 cr4 &= ~X86_CR4_PCIDE;
2514 }
2515
2516 /* A 32-bit code segment is required to clear EFER.LMA. */
2517 memset(&cs_desc, 0, sizeof(cs_desc));
2518 cs_desc.type = 0xb;
2519 cs_desc.s = cs_desc.g = cs_desc.p = 1;
2520 ctxt->ops->set_segment(ctxt, 0, &cs_desc, 0, VCPU_SREG_CS);
2521 }
2522
2523 /* For the 64-bit case, this will clear EFER.LMA. */
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002524 cr0 = ctxt->ops->get_cr(ctxt, 0);
2525 if (cr0 & X86_CR0_PE)
2526 ctxt->ops->set_cr(ctxt, 0, cr0 & ~(X86_CR0_PG | X86_CR0_PE));
Paolo Bonzini89651a32015-11-03 13:43:05 +01002527
2528 /* Now clear CR4.PAE (which must be done before clearing EFER.LME). */
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002529 if (cr4 & X86_CR4_PAE)
2530 ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PAE);
Paolo Bonzini89651a32015-11-03 13:43:05 +01002531
2532 /* And finally go back to 32-bit mode. */
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002533 efer = 0;
2534 ctxt->ops->set_msr(ctxt, MSR_EFER, efer);
2535
2536 smbase = ctxt->ops->get_smbase(ctxt);
2537 if (emulator_has_longmode(ctxt))
2538 ret = rsm_load_state_64(ctxt, smbase + 0x8000);
2539 else
2540 ret = rsm_load_state_32(ctxt, smbase + 0x8000);
2541
2542 if (ret != X86EMUL_CONTINUE) {
2543 /* FIXME: should triple fault */
2544 return X86EMUL_UNHANDLEABLE;
2545 }
2546
2547 if ((ctxt->emul_flags & X86EMUL_SMM_INSIDE_NMI_MASK) == 0)
2548 ctxt->ops->set_nmi_mask(ctxt, false);
2549
2550 ctxt->emul_flags &= ~X86EMUL_SMM_INSIDE_NMI_MASK;
2551 ctxt->emul_flags &= ~X86EMUL_SMM_MASK;
2552 return X86EMUL_CONTINUE;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002553}
2554
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002555static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002556setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002557 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002558{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002559 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002560 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002561 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002562 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002563 cs->type = 0x0b; /* Read, Execute, Accessed */
2564 cs->s = 1;
2565 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002566 cs->p = 1;
2567 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002568 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002569
Gleb Natapov79168fd2010-04-28 19:15:30 +03002570 set_desc_base(ss, 0); /* flat segment */
2571 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002572 ss->g = 1; /* 4kb granularity */
2573 ss->s = 1;
2574 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002575 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002576 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002577 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002578 ss->l = 0;
2579 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002580}
2581
Avi Kivity1a18a692012-02-01 12:23:21 +02002582static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2583{
2584 u32 eax, ebx, ecx, edx;
2585
2586 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002587 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2588 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002589 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2590 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2591}
2592
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002593static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2594{
Mathias Krause0225fb52012-08-30 01:30:16 +02002595 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002596 u32 eax, ebx, ecx, edx;
2597
2598 /*
2599 * syscall should always be enabled in longmode - so only become
2600 * vendor specific (cpuid) if other modes are active...
2601 */
2602 if (ctxt->mode == X86EMUL_MODE_PROT64)
2603 return true;
2604
2605 eax = 0x00000000;
2606 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002607 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2608 /*
2609 * Intel ("GenuineIntel")
2610 * remark: Intel CPUs only support "syscall" in 64bit
2611 * longmode. Also an 64bit guest with a
2612 * 32bit compat-app running will #UD !! While this
2613 * behaviour can be fixed (by emulating) into AMD
2614 * response - CPUs of AMD can't behave like Intel.
2615 */
2616 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2617 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2618 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2619 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002620
Avi Kivity0017f932012-06-07 14:10:16 +03002621 /* AMD ("AuthenticAMD") */
2622 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2623 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2624 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2625 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002626
Avi Kivity0017f932012-06-07 14:10:16 +03002627 /* AMD ("AMDisbetter!") */
2628 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2629 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2630 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2631 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002632
2633 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2634 return false;
2635}
2636
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002637static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002638{
Mathias Krause0225fb52012-08-30 01:30:16 +02002639 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002640 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002641 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002642 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002643 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002644
2645 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002646 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002647 ctxt->mode == X86EMUL_MODE_VM86)
2648 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002649
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002650 if (!(em_syscall_is_enabled(ctxt)))
2651 return emulate_ud(ctxt);
2652
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002653 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002654 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002655
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002656 if (!(efer & EFER_SCE))
2657 return emulate_ud(ctxt);
2658
Avi Kivity717746e2011-04-20 13:37:53 +03002659 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002660 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002661 cs_sel = (u16)(msr_data & 0xfffc);
2662 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002663
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002664 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002665 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002666 cs.l = 1;
2667 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002668 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2669 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002670
Avi Kivitydd856ef2012-08-27 23:46:17 +03002671 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002672 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002673#ifdef CONFIG_X86_64
Nadav Amit6c6cb692014-07-21 14:37:30 +03002674 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002675
Avi Kivity717746e2011-04-20 13:37:53 +03002676 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002677 ctxt->mode == X86EMUL_MODE_PROT64 ?
2678 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002679 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002680
Avi Kivity717746e2011-04-20 13:37:53 +03002681 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Nadav Amit6c6cb692014-07-21 14:37:30 +03002682 ctxt->eflags &= ~msr_data;
Wanpeng Li35fd68a2015-04-08 14:08:14 +08002683 ctxt->eflags |= X86_EFLAGS_FIXED;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002684#endif
2685 } else {
2686 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002687 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002688 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002689
Nadav Amit0efb0442015-03-29 16:33:03 +03002690 ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002691 }
2692
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002693 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002694}
2695
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002696static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002697{
Mathias Krause0225fb52012-08-30 01:30:16 +02002698 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002699 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002700 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002701 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002702 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002703
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002704 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002705 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002706 if (ctxt->mode == X86EMUL_MODE_REAL)
2707 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002708
Avi Kivity1a18a692012-02-01 12:23:21 +02002709 /*
2710 * Not recognized on AMD in compat mode (but is recognized in legacy
2711 * mode).
2712 */
Nadav Amitf3747372015-01-01 23:11:11 +02002713 if ((ctxt->mode != X86EMUL_MODE_PROT64) && (efer & EFER_LMA)
Avi Kivity1a18a692012-02-01 12:23:21 +02002714 && !vendor_intel(ctxt))
2715 return emulate_ud(ctxt);
2716
Nadav Amitb2c9d432014-11-02 11:55:01 +02002717 /* sysenter/sysexit have not been tested in 64bit mode. */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002718 if (ctxt->mode == X86EMUL_MODE_PROT64)
Nadav Amitb2c9d432014-11-02 11:55:01 +02002719 return X86EMUL_UNHANDLEABLE;
Andre Przywara8c604352009-06-18 12:56:01 +02002720
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002721 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002722
Avi Kivity717746e2011-04-20 13:37:53 +03002723 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002724 if ((msr_data & 0xfffc) == 0x0)
2725 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002726
Nadav Amit0efb0442015-03-29 16:33:03 +03002727 ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
Nadav Amitb32a9912015-03-29 16:33:04 +03002728 cs_sel = (u16)msr_data & ~SEGMENT_RPL_MASK;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002729 ss_sel = cs_sel + 8;
Nadav Amitf3747372015-01-01 23:11:11 +02002730 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002731 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002732 cs.l = 1;
2733 }
2734
Avi Kivity1aa36612011-04-27 13:20:30 +03002735 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2736 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002737
Avi Kivity717746e2011-04-20 13:37:53 +03002738 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002739 ctxt->_eip = (efer & EFER_LMA) ? msr_data : (u32)msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002740
Avi Kivity717746e2011-04-20 13:37:53 +03002741 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002742 *reg_write(ctxt, VCPU_REGS_RSP) = (efer & EFER_LMA) ? msr_data :
2743 (u32)msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002744
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002745 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002746}
2747
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002748static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002749{
Mathias Krause0225fb52012-08-30 01:30:16 +02002750 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002751 struct desc_struct cs, ss;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002752 u64 msr_data, rcx, rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002753 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002754 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002755
Gleb Natapova0044752010-02-10 14:21:31 +02002756 /* inject #GP if in real mode or Virtual 8086 mode */
2757 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002758 ctxt->mode == X86EMUL_MODE_VM86)
2759 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002760
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002761 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002762
Avi Kivity9dac77f2011-06-01 15:34:25 +03002763 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002764 usermode = X86EMUL_MODE_PROT64;
2765 else
2766 usermode = X86EMUL_MODE_PROT32;
2767
Nadav Amit234f3ce2014-09-18 22:39:38 +03002768 rcx = reg_read(ctxt, VCPU_REGS_RCX);
2769 rdx = reg_read(ctxt, VCPU_REGS_RDX);
2770
Andre Przywara4668f052009-06-18 12:56:02 +02002771 cs.dpl = 3;
2772 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002773 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002774 switch (usermode) {
2775 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002776 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002777 if ((msr_data & 0xfffc) == 0x0)
2778 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002779 ss_sel = (u16)(msr_data + 24);
Nadav Amitbf0b6822014-09-18 22:39:45 +03002780 rcx = (u32)rcx;
2781 rdx = (u32)rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002782 break;
2783 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002784 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002785 if (msr_data == 0x0)
2786 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002787 ss_sel = cs_sel + 8;
2788 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002789 cs.l = 1;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002790 if (is_noncanonical_address(rcx) ||
2791 is_noncanonical_address(rdx))
2792 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002793 break;
2794 }
Nadav Amitb32a9912015-03-29 16:33:04 +03002795 cs_sel |= SEGMENT_RPL_MASK;
2796 ss_sel |= SEGMENT_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002797
Avi Kivity1aa36612011-04-27 13:20:30 +03002798 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2799 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002800
Nadav Amit234f3ce2014-09-18 22:39:38 +03002801 ctxt->_eip = rdx;
2802 *reg_write(ctxt, VCPU_REGS_RSP) = rcx;
Andre Przywara4668f052009-06-18 12:56:02 +02002803
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002804 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002805}
2806
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002807static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002808{
2809 int iopl;
2810 if (ctxt->mode == X86EMUL_MODE_REAL)
2811 return false;
2812 if (ctxt->mode == X86EMUL_MODE_VM86)
2813 return true;
Nadav Amit0efb0442015-03-29 16:33:03 +03002814 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002815 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002816}
2817
2818static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002819 u16 port, u16 len)
2820{
Mathias Krause0225fb52012-08-30 01:30:16 +02002821 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002822 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002823 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002824 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002825 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002826 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002827 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002828
Avi Kivity1aa36612011-04-27 13:20:30 +03002829 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002830 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002831 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002832 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002833 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002834 base = get_desc_base(&tr_seg);
2835#ifdef CONFIG_X86_64
2836 base |= ((u64)base3) << 32;
2837#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002838 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002839 if (r != X86EMUL_CONTINUE)
2840 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002841 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002842 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002843 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002844 if (r != X86EMUL_CONTINUE)
2845 return false;
2846 if ((perm >> bit_idx) & mask)
2847 return false;
2848 return true;
2849}
2850
2851static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002852 u16 port, u16 len)
2853{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002854 if (ctxt->perm_ok)
2855 return true;
2856
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002857 if (emulator_bad_iopl(ctxt))
2858 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002859 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002860
2861 ctxt->perm_ok = true;
2862
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002863 return true;
2864}
2865
Nadav Amit428e3d02015-04-28 13:06:01 +03002866static void string_registers_quirk(struct x86_emulate_ctxt *ctxt)
2867{
2868 /*
2869 * Intel CPUs mask the counter and pointers in quite strange
2870 * manner when ECX is zero due to REP-string optimizations.
2871 */
2872#ifdef CONFIG_X86_64
2873 if (ctxt->ad_bytes != 4 || !vendor_intel(ctxt))
2874 return;
2875
2876 *reg_write(ctxt, VCPU_REGS_RCX) = 0;
2877
2878 switch (ctxt->b) {
2879 case 0xa4: /* movsb */
2880 case 0xa5: /* movsd/w */
2881 *reg_rmw(ctxt, VCPU_REGS_RSI) &= (u32)-1;
2882 /* fall through */
2883 case 0xaa: /* stosb */
2884 case 0xab: /* stosd/w */
2885 *reg_rmw(ctxt, VCPU_REGS_RDI) &= (u32)-1;
2886 }
2887#endif
2888}
2889
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002890static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002891 struct tss_segment_16 *tss)
2892{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002893 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002894 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002895 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2896 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2897 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2898 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2899 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2900 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2901 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2902 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002903
Avi Kivity1aa36612011-04-27 13:20:30 +03002904 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2905 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2906 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2907 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2908 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002909}
2910
2911static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002912 struct tss_segment_16 *tss)
2913{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002914 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002915 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002916
Avi Kivity9dac77f2011-06-01 15:34:25 +03002917 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002918 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002919 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2920 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2921 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2922 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2923 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2924 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2925 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2926 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002927
2928 /*
2929 * SDM says that segment selectors are loaded before segment
2930 * descriptors
2931 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002932 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2933 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2934 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2935 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2936 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002937
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002938 cpl = tss->cs & 3;
2939
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002940 /*
Guo Chaofc058682012-06-28 15:19:51 +08002941 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002942 * it is handled in a context of new task
2943 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002944 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002945 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002946 if (ret != X86EMUL_CONTINUE)
2947 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002948 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002949 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002950 if (ret != X86EMUL_CONTINUE)
2951 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002952 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002953 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002954 if (ret != X86EMUL_CONTINUE)
2955 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002956 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002957 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002958 if (ret != X86EMUL_CONTINUE)
2959 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002960 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002961 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002962 if (ret != X86EMUL_CONTINUE)
2963 return ret;
2964
2965 return X86EMUL_CONTINUE;
2966}
2967
2968static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002969 u16 tss_selector, u16 old_tss_sel,
2970 ulong old_tss_base, struct desc_struct *new_desc)
2971{
Mathias Krause0225fb52012-08-30 01:30:16 +02002972 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002973 struct tss_segment_16 tss_seg;
2974 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002975 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002976
Avi Kivity0f65dd72011-04-20 13:37:53 +03002977 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002978 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002979 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002980 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002981
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002982 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002983
Avi Kivity0f65dd72011-04-20 13:37:53 +03002984 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002985 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002986 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002987 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002988
Avi Kivity0f65dd72011-04-20 13:37:53 +03002989 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002990 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002991 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002992 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002993
2994 if (old_tss_sel != 0xffff) {
2995 tss_seg.prev_task_link = old_tss_sel;
2996
Avi Kivity0f65dd72011-04-20 13:37:53 +03002997 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002998 &tss_seg.prev_task_link,
2999 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03003000 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02003001 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003002 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003003 }
3004
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003005 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003006}
3007
3008static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003009 struct tss_segment_32 *tss)
3010{
Nadav Amit5c7411e2014-04-07 18:37:47 +03003011 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003012 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003013 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003014 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
3015 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
3016 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
3017 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
3018 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
3019 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
3020 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
3021 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003022
Avi Kivity1aa36612011-04-27 13:20:30 +03003023 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
3024 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
3025 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
3026 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
3027 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
3028 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003029}
3030
3031static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003032 struct tss_segment_32 *tss)
3033{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003034 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003035 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003036
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003037 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003038 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003039 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003040 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01003041
3042 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03003043 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
3044 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
3045 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
3046 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
3047 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
3048 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
3049 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
3050 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003051
3052 /*
3053 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003054 * descriptors. This is important because CPL checks will
3055 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003056 */
Avi Kivity1aa36612011-04-27 13:20:30 +03003057 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
3058 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
3059 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
3060 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
3061 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
3062 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
3063 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003064
3065 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01003066 * If we're switching between Protected Mode and VM86, we need to make
3067 * sure to update the mode before loading the segment descriptors so
3068 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01003069 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003070 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01003071 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003072 cpl = 3;
3073 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01003074 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003075 cpl = tss->cs & 3;
3076 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01003077
3078 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003079 * Now load segment descriptors. If fault happenes at this stage
3080 * it is handled in a context of new task
3081 */
Nadav Amitd1442d82014-09-18 22:39:39 +03003082 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003083 cpl, X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003084 if (ret != X86EMUL_CONTINUE)
3085 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003086 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003087 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003088 if (ret != X86EMUL_CONTINUE)
3089 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003090 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003091 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003092 if (ret != X86EMUL_CONTINUE)
3093 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003094 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003095 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003096 if (ret != X86EMUL_CONTINUE)
3097 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003098 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003099 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003100 if (ret != X86EMUL_CONTINUE)
3101 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003102 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003103 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003104 if (ret != X86EMUL_CONTINUE)
3105 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003106 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003107 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003108
Eugene Korenevsky2f729b12015-03-29 01:27:17 +03003109 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003110}
3111
3112static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003113 u16 tss_selector, u16 old_tss_sel,
3114 ulong old_tss_base, struct desc_struct *new_desc)
3115{
Mathias Krause0225fb52012-08-30 01:30:16 +02003116 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003117 struct tss_segment_32 tss_seg;
3118 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003119 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03003120 u32 eip_offset = offsetof(struct tss_segment_32, eip);
3121 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003122
Avi Kivity0f65dd72011-04-20 13:37:53 +03003123 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003124 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02003125 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003126 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003127
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003128 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003129
Nadav Amit5c7411e2014-04-07 18:37:47 +03003130 /* Only GP registers and segment selectors are saved */
3131 ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
3132 ldt_sel_offset - eip_offset, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02003133 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003134 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003135
Avi Kivity0f65dd72011-04-20 13:37:53 +03003136 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003137 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02003138 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003139 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003140
3141 if (old_tss_sel != 0xffff) {
3142 tss_seg.prev_task_link = old_tss_sel;
3143
Avi Kivity0f65dd72011-04-20 13:37:53 +03003144 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003145 &tss_seg.prev_task_link,
3146 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03003147 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02003148 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003149 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003150 }
3151
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003152 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003153}
3154
3155static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003156 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003157 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003158{
Mathias Krause0225fb52012-08-30 01:30:16 +02003159 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003160 struct desc_struct curr_tss_desc, next_tss_desc;
3161 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03003162 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003163 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03003164 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02003165 u32 desc_limit;
Nadav Amit3db176d2015-04-19 21:12:59 +03003166 ulong desc_addr, dr7;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003167
3168 /* FIXME: old_tss_base == ~0 ? */
3169
Avi Kivitye9194642012-06-13 16:29:39 +03003170 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003171 if (ret != X86EMUL_CONTINUE)
3172 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03003173 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003174 if (ret != X86EMUL_CONTINUE)
3175 return ret;
3176
3177 /* FIXME: check that next_tss_desc is tss */
3178
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003179 /*
3180 * Check privileges. The three cases are task switch caused by...
3181 *
3182 * 1. jmp/call/int to task gate: Check against DPL of the task gate
3183 * 2. Exception/IRQ/iret: No check is performed
Nadav Amit2c2ca2d2014-11-02 11:54:57 +02003184 * 3. jmp/call to TSS/task-gate: No check is performed since the
3185 * hardware checks it before exiting.
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003186 */
3187 if (reason == TASK_SWITCH_GATE) {
3188 if (idt_index != -1) {
3189 /* Software interrupts */
3190 struct desc_struct task_gate_desc;
3191 int dpl;
3192
3193 ret = read_interrupt_descriptor(ctxt, idt_index,
3194 &task_gate_desc);
3195 if (ret != X86EMUL_CONTINUE)
3196 return ret;
3197
3198 dpl = task_gate_desc.dpl;
3199 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
3200 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
3201 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003202 }
3203
Gleb Natapovceffb452010-03-18 15:20:19 +02003204 desc_limit = desc_limit_scaled(&next_tss_desc);
3205 if (!next_tss_desc.p ||
3206 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
3207 desc_limit < 0x2b)) {
Paolo Bonzini592f0852014-08-20 10:05:08 +02003208 return emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003209 }
3210
3211 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
3212 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003213 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003214 }
3215
3216 if (reason == TASK_SWITCH_IRET)
3217 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
3218
3219 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08003220 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003221 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
3222 old_tss_sel = 0xffff;
3223
3224 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003225 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003226 old_tss_base, &next_tss_desc);
3227 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003228 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003229 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02003230 if (ret != X86EMUL_CONTINUE)
3231 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003232
3233 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
3234 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
3235
3236 if (reason != TASK_SWITCH_IRET) {
3237 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003238 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003239 }
3240
Avi Kivity717746e2011-04-20 13:37:53 +03003241 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03003242 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003243
Jan Kiszkae269fb22010-04-14 15:51:09 +02003244 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003245 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
3246 ctxt->lock_prefix = 0;
3247 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003248 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02003249 }
3250
Nadav Amit3db176d2015-04-19 21:12:59 +03003251 ops->get_dr(ctxt, 7, &dr7);
3252 ops->set_dr(ctxt, 7, dr7 & ~(DR_LOCAL_ENABLE_MASK | DR_LOCAL_SLOWDOWN));
3253
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003254 return ret;
3255}
3256
3257int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003258 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003259 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003260{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003261 int rc;
3262
Avi Kivitydd856ef2012-08-27 23:46:17 +03003263 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003264 ctxt->_eip = ctxt->eip;
3265 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003266
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003267 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003268 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003269
Avi Kivitydd856ef2012-08-27 23:46:17 +03003270 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003271 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003272 writeback_registers(ctxt);
3273 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003274
Gleb Natapova0c0ab22011-03-28 16:57:49 +02003275 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003276}
3277
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03003278static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
3279 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02003280{
Nadav Amit0efb0442015-03-29 16:33:03 +03003281 int df = (ctxt->eflags & X86_EFLAGS_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02003282
Paolo Bonzini01485a22014-11-19 18:25:08 +01003283 register_address_increment(ctxt, reg, df * op->bytes);
3284 op->addr.mem.ea = register_address(ctxt, reg);
Gleb Natapova682e352010-03-18 15:20:21 +02003285}
3286
Avi Kivity7af04fc2010-08-18 14:16:35 +03003287static int em_das(struct x86_emulate_ctxt *ctxt)
3288{
Avi Kivity7af04fc2010-08-18 14:16:35 +03003289 u8 al, old_al;
3290 bool af, cf, old_cf;
3291
3292 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003293 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03003294
3295 old_al = al;
3296 old_cf = cf;
3297 cf = false;
3298 af = ctxt->eflags & X86_EFLAGS_AF;
3299 if ((al & 0x0f) > 9 || af) {
3300 al -= 6;
3301 cf = old_cf | (al >= 250);
3302 af = true;
3303 } else {
3304 af = false;
3305 }
3306 if (old_al > 0x99 || old_cf) {
3307 al -= 0x60;
3308 cf = true;
3309 }
3310
Avi Kivity9dac77f2011-06-01 15:34:25 +03003311 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03003312 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003313 ctxt->src.type = OP_IMM;
3314 ctxt->src.val = 0;
3315 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02003316 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03003317 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
3318 if (cf)
3319 ctxt->eflags |= X86_EFLAGS_CF;
3320 if (af)
3321 ctxt->eflags |= X86_EFLAGS_AF;
3322 return X86EMUL_CONTINUE;
3323}
3324
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003325static int em_aam(struct x86_emulate_ctxt *ctxt)
3326{
3327 u8 al, ah;
3328
3329 if (ctxt->src.val == 0)
3330 return emulate_de(ctxt);
3331
3332 al = ctxt->dst.val & 0xff;
3333 ah = al / ctxt->src.val;
3334 al %= ctxt->src.val;
3335
3336 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
3337
3338 /* Set PF, ZF, SF */
3339 ctxt->src.type = OP_IMM;
3340 ctxt->src.val = 0;
3341 ctxt->src.bytes = 1;
3342 fastop(ctxt, em_or);
3343
3344 return X86EMUL_CONTINUE;
3345}
3346
Gleb Natapov7f662272012-12-10 11:42:30 +02003347static int em_aad(struct x86_emulate_ctxt *ctxt)
3348{
3349 u8 al = ctxt->dst.val & 0xff;
3350 u8 ah = (ctxt->dst.val >> 8) & 0xff;
3351
3352 al = (al + (ah * ctxt->src.val)) & 0xff;
3353
3354 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
3355
Gleb Natapovf583c292013-02-13 17:50:39 +02003356 /* Set PF, ZF, SF */
3357 ctxt->src.type = OP_IMM;
3358 ctxt->src.val = 0;
3359 ctxt->src.bytes = 1;
3360 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02003361
3362 return X86EMUL_CONTINUE;
3363}
3364
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003365static int em_call(struct x86_emulate_ctxt *ctxt)
3366{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003367 int rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003368 long rel = ctxt->src.val;
3369
3370 ctxt->src.val = (unsigned long)ctxt->_eip;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003371 rc = jmp_rel(ctxt, rel);
3372 if (rc != X86EMUL_CONTINUE)
3373 return rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003374 return em_push(ctxt);
3375}
3376
Avi Kivity0ef753b2010-08-18 14:51:45 +03003377static int em_call_far(struct x86_emulate_ctxt *ctxt)
3378{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003379 u16 sel, old_cs;
3380 ulong old_eip;
3381 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03003382 struct desc_struct old_desc, new_desc;
3383 const struct x86_emulate_ops *ops = ctxt->ops;
3384 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amit82268082015-01-26 09:32:27 +02003385 enum x86emul_mode prev_mode = ctxt->mode;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003386
Avi Kivity9dac77f2011-06-01 15:34:25 +03003387 old_eip = ctxt->_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003388 ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003389
Avi Kivity9dac77f2011-06-01 15:34:25 +03003390 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003391 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
3392 X86_TRANSFER_CALL_JMP, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003393 if (rc != X86EMUL_CONTINUE)
Nadav Amit80976db2014-12-25 02:52:20 +02003394 return rc;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003395
Nadav Amitd50eaa12014-11-19 17:43:11 +02003396 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003397 if (rc != X86EMUL_CONTINUE)
3398 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003399
Avi Kivity9dac77f2011-06-01 15:34:25 +03003400 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003401 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003402 if (rc != X86EMUL_CONTINUE)
Nadav Amitd1442d82014-09-18 22:39:39 +03003403 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003404
Avi Kivity9dac77f2011-06-01 15:34:25 +03003405 ctxt->src.val = old_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003406 rc = em_push(ctxt);
3407 /* If we failed, we tainted the memory, but the very least we should
3408 restore cs */
Nadav Amit82268082015-01-26 09:32:27 +02003409 if (rc != X86EMUL_CONTINUE) {
3410 pr_warn_once("faulting far call emulation tainted memory\n");
Nadav Amitd1442d82014-09-18 22:39:39 +03003411 goto fail;
Nadav Amit82268082015-01-26 09:32:27 +02003412 }
Nadav Amitd1442d82014-09-18 22:39:39 +03003413 return rc;
3414fail:
3415 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
Nadav Amit82268082015-01-26 09:32:27 +02003416 ctxt->mode = prev_mode;
Nadav Amitd1442d82014-09-18 22:39:39 +03003417 return rc;
3418
Avi Kivity0ef753b2010-08-18 14:51:45 +03003419}
3420
Avi Kivity40ece7c2010-08-18 15:12:09 +03003421static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3422{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003423 int rc;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003424 unsigned long eip;
Avi Kivity40ece7c2010-08-18 15:12:09 +03003425
Nadav Amit234f3ce2014-09-18 22:39:38 +03003426 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
3427 if (rc != X86EMUL_CONTINUE)
3428 return rc;
3429 rc = assign_eip_near(ctxt, eip);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003430 if (rc != X86EMUL_CONTINUE)
3431 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003432 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003433 return X86EMUL_CONTINUE;
3434}
3435
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003436static int em_xchg(struct x86_emulate_ctxt *ctxt)
3437{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003438 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003439 ctxt->src.val = ctxt->dst.val;
3440 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003441
3442 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003443 ctxt->dst.val = ctxt->src.orig_val;
3444 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003445 return X86EMUL_CONTINUE;
3446}
3447
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003448static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3449{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003450 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003451 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003452}
3453
Avi Kivity61429142010-08-19 15:13:00 +03003454static int em_cwd(struct x86_emulate_ctxt *ctxt)
3455{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003456 ctxt->dst.type = OP_REG;
3457 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003458 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003459 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003460
3461 return X86EMUL_CONTINUE;
3462}
3463
Avi Kivity48bb5d32010-08-18 18:54:34 +03003464static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3465{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003466 u64 tsc = 0;
3467
Avi Kivity717746e2011-04-20 13:37:53 +03003468 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003469 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3470 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003471 return X86EMUL_CONTINUE;
3472}
3473
Avi Kivity222d21a2011-11-10 14:57:30 +02003474static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3475{
3476 u64 pmc;
3477
Avi Kivitydd856ef2012-08-27 23:46:17 +03003478 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003479 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003480 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3481 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003482 return X86EMUL_CONTINUE;
3483}
3484
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003485static int em_mov(struct x86_emulate_ctxt *ctxt)
3486{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01003487 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003488 return X86EMUL_CONTINUE;
3489}
3490
Borislav Petkov84cffe42013-10-29 12:54:56 +01003491#define FFL(x) bit(X86_FEATURE_##x)
3492
3493static int em_movbe(struct x86_emulate_ctxt *ctxt)
3494{
3495 u32 ebx, ecx, edx, eax = 1;
3496 u16 tmp;
3497
3498 /*
3499 * Check MOVBE is set in the guest-visible CPUID leaf.
3500 */
3501 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
3502 if (!(ecx & FFL(MOVBE)))
3503 return emulate_ud(ctxt);
3504
3505 switch (ctxt->op_bytes) {
3506 case 2:
3507 /*
3508 * From MOVBE definition: "...When the operand size is 16 bits,
3509 * the upper word of the destination register remains unchanged
3510 * ..."
3511 *
3512 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3513 * rules so we have to do the operation almost per hand.
3514 */
3515 tmp = (u16)ctxt->src.val;
3516 ctxt->dst.val &= ~0xffffUL;
3517 ctxt->dst.val |= (unsigned long)swab16(tmp);
3518 break;
3519 case 4:
3520 ctxt->dst.val = swab32((u32)ctxt->src.val);
3521 break;
3522 case 8:
3523 ctxt->dst.val = swab64(ctxt->src.val);
3524 break;
3525 default:
Paolo Bonzini592f0852014-08-20 10:05:08 +02003526 BUG();
Borislav Petkov84cffe42013-10-29 12:54:56 +01003527 }
3528 return X86EMUL_CONTINUE;
3529}
3530
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003531static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3532{
3533 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3534 return emulate_gp(ctxt, 0);
3535
3536 /* Disable writeback. */
3537 ctxt->dst.type = OP_NONE;
3538 return X86EMUL_CONTINUE;
3539}
3540
3541static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3542{
3543 unsigned long val;
3544
3545 if (ctxt->mode == X86EMUL_MODE_PROT64)
3546 val = ctxt->src.val & ~0ULL;
3547 else
3548 val = ctxt->src.val & ~0U;
3549
3550 /* #UD condition is already handled. */
3551 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3552 return emulate_gp(ctxt, 0);
3553
3554 /* Disable writeback. */
3555 ctxt->dst.type = OP_NONE;
3556 return X86EMUL_CONTINUE;
3557}
3558
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003559static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3560{
3561 u64 msr_data;
3562
Avi Kivitydd856ef2012-08-27 23:46:17 +03003563 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3564 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3565 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003566 return emulate_gp(ctxt, 0);
3567
3568 return X86EMUL_CONTINUE;
3569}
3570
3571static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3572{
3573 u64 msr_data;
3574
Avi Kivitydd856ef2012-08-27 23:46:17 +03003575 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003576 return emulate_gp(ctxt, 0);
3577
Avi Kivitydd856ef2012-08-27 23:46:17 +03003578 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3579 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003580 return X86EMUL_CONTINUE;
3581}
3582
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003583static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3584{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003585 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003586 return emulate_ud(ctxt);
3587
Avi Kivity9dac77f2011-06-01 15:34:25 +03003588 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Nadav Amitb5bbf102014-11-02 11:54:46 +02003589 if (ctxt->dst.bytes == 4 && ctxt->dst.type == OP_MEM)
3590 ctxt->dst.bytes = 2;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003591 return X86EMUL_CONTINUE;
3592}
3593
3594static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3595{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003596 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003597
Avi Kivity9dac77f2011-06-01 15:34:25 +03003598 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003599 return emulate_ud(ctxt);
3600
Avi Kivity9dac77f2011-06-01 15:34:25 +03003601 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003602 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3603
3604 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003605 ctxt->dst.type = OP_NONE;
3606 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003607}
3608
Avi Kivitya14e5792012-06-13 12:28:33 +03003609static int em_lldt(struct x86_emulate_ctxt *ctxt)
3610{
3611 u16 sel = ctxt->src.val;
3612
3613 /* Disable writeback. */
3614 ctxt->dst.type = OP_NONE;
3615 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3616}
3617
Avi Kivity80890002012-06-13 16:33:29 +03003618static int em_ltr(struct x86_emulate_ctxt *ctxt)
3619{
3620 u16 sel = ctxt->src.val;
3621
3622 /* Disable writeback. */
3623 ctxt->dst.type = OP_NONE;
3624 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3625}
3626
Avi Kivity38503912011-03-31 18:48:09 +02003627static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3628{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003629 int rc;
3630 ulong linear;
3631
Avi Kivity9dac77f2011-06-01 15:34:25 +03003632 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003633 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003634 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003635 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003636 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003637 return X86EMUL_CONTINUE;
3638}
3639
Avi Kivity2d04a052011-04-20 15:32:49 +03003640static int em_clts(struct x86_emulate_ctxt *ctxt)
3641{
3642 ulong cr0;
3643
3644 cr0 = ctxt->ops->get_cr(ctxt, 0);
3645 cr0 &= ~X86_CR0_TS;
3646 ctxt->ops->set_cr(ctxt, 0, cr0);
3647 return X86EMUL_CONTINUE;
3648}
3649
Jan Kiszkab34a8052015-03-09 20:27:43 +01003650static int em_hypercall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003651{
Nadav Amit0f54a322014-08-29 11:26:55 +03003652 int rc = ctxt->ops->fix_hypercall(ctxt);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003653
Avi Kivity26d05cc2011-04-21 12:07:59 +03003654 if (rc != X86EMUL_CONTINUE)
3655 return rc;
3656
3657 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003658 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003659 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003660 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003661 return X86EMUL_CONTINUE;
3662}
3663
Avi Kivity96051572012-06-10 17:21:18 +03003664static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3665 void (*get)(struct x86_emulate_ctxt *ctxt,
3666 struct desc_ptr *ptr))
3667{
3668 struct desc_ptr desc_ptr;
3669
3670 if (ctxt->mode == X86EMUL_MODE_PROT64)
3671 ctxt->op_bytes = 8;
3672 get(ctxt, &desc_ptr);
3673 if (ctxt->op_bytes == 2) {
3674 ctxt->op_bytes = 4;
3675 desc_ptr.address &= 0x00ffffff;
3676 }
3677 /* Disable writeback. */
3678 ctxt->dst.type = OP_NONE;
3679 return segmented_write(ctxt, ctxt->dst.addr.mem,
3680 &desc_ptr, 2 + ctxt->op_bytes);
3681}
3682
3683static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3684{
3685 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3686}
3687
3688static int em_sidt(struct x86_emulate_ctxt *ctxt)
3689{
3690 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3691}
3692
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003693static int em_lgdt_lidt(struct x86_emulate_ctxt *ctxt, bool lgdt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003694{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003695 struct desc_ptr desc_ptr;
3696 int rc;
3697
Avi Kivity510425f2012-06-07 17:04:36 +03003698 if (ctxt->mode == X86EMUL_MODE_PROT64)
3699 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003700 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003701 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003702 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003703 if (rc != X86EMUL_CONTINUE)
3704 return rc;
Nadav Amit9a9abf62014-11-02 11:54:56 +02003705 if (ctxt->mode == X86EMUL_MODE_PROT64 &&
3706 is_noncanonical_address(desc_ptr.address))
3707 return emulate_gp(ctxt, 0);
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003708 if (lgdt)
3709 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3710 else
3711 ctxt->ops->set_idt(ctxt, &desc_ptr);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003712 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003713 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003714 return X86EMUL_CONTINUE;
3715}
3716
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003717static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3718{
3719 return em_lgdt_lidt(ctxt, true);
3720}
3721
Avi Kivity26d05cc2011-04-21 12:07:59 +03003722static int em_lidt(struct x86_emulate_ctxt *ctxt)
3723{
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003724 return em_lgdt_lidt(ctxt, false);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003725}
3726
3727static int em_smsw(struct x86_emulate_ctxt *ctxt)
3728{
Nadav Amit32e94d02014-06-02 18:34:11 +03003729 if (ctxt->dst.type == OP_MEM)
3730 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003731 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003732 return X86EMUL_CONTINUE;
3733}
3734
3735static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3736{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003737 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003738 | (ctxt->src.val & 0x0f));
3739 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003740 return X86EMUL_CONTINUE;
3741}
3742
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003743static int em_loop(struct x86_emulate_ctxt *ctxt)
3744{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003745 int rc = X86EMUL_CONTINUE;
3746
Paolo Bonzini01485a22014-11-19 18:25:08 +01003747 register_address_increment(ctxt, VCPU_REGS_RCX, -1);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003748 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003749 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
Nadav Amit234f3ce2014-09-18 22:39:38 +03003750 rc = jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003751
Nadav Amit234f3ce2014-09-18 22:39:38 +03003752 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003753}
3754
3755static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3756{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003757 int rc = X86EMUL_CONTINUE;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003758
Nadav Amit234f3ce2014-09-18 22:39:38 +03003759 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
3760 rc = jmp_rel(ctxt, ctxt->src.val);
3761
3762 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003763}
3764
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003765static int em_in(struct x86_emulate_ctxt *ctxt)
3766{
3767 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3768 &ctxt->dst.val))
3769 return X86EMUL_IO_NEEDED;
3770
3771 return X86EMUL_CONTINUE;
3772}
3773
3774static int em_out(struct x86_emulate_ctxt *ctxt)
3775{
3776 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3777 &ctxt->src.val, 1);
3778 /* Disable writeback. */
3779 ctxt->dst.type = OP_NONE;
3780 return X86EMUL_CONTINUE;
3781}
3782
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003783static int em_cli(struct x86_emulate_ctxt *ctxt)
3784{
3785 if (emulator_bad_iopl(ctxt))
3786 return emulate_gp(ctxt, 0);
3787
3788 ctxt->eflags &= ~X86_EFLAGS_IF;
3789 return X86EMUL_CONTINUE;
3790}
3791
3792static int em_sti(struct x86_emulate_ctxt *ctxt)
3793{
3794 if (emulator_bad_iopl(ctxt))
3795 return emulate_gp(ctxt, 0);
3796
3797 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3798 ctxt->eflags |= X86_EFLAGS_IF;
3799 return X86EMUL_CONTINUE;
3800}
3801
Avi Kivity6d6eede2012-06-07 14:11:36 +03003802static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3803{
3804 u32 eax, ebx, ecx, edx;
3805
Avi Kivitydd856ef2012-08-27 23:46:17 +03003806 eax = reg_read(ctxt, VCPU_REGS_RAX);
3807 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003808 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003809 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3810 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3811 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3812 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003813 return X86EMUL_CONTINUE;
3814}
3815
Paolo Bonzini98f73632013-10-31 11:19:42 +01003816static int em_sahf(struct x86_emulate_ctxt *ctxt)
3817{
3818 u32 flags;
3819
Nadav Amit0efb0442015-03-29 16:33:03 +03003820 flags = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
3821 X86_EFLAGS_SF;
Paolo Bonzini98f73632013-10-31 11:19:42 +01003822 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3823
3824 ctxt->eflags &= ~0xffUL;
3825 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3826 return X86EMUL_CONTINUE;
3827}
3828
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003829static int em_lahf(struct x86_emulate_ctxt *ctxt)
3830{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003831 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3832 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003833 return X86EMUL_CONTINUE;
3834}
3835
Avi Kivity92998362012-06-13 12:25:06 +03003836static int em_bswap(struct x86_emulate_ctxt *ctxt)
3837{
3838 switch (ctxt->op_bytes) {
3839#ifdef CONFIG_X86_64
3840 case 8:
3841 asm("bswap %0" : "+r"(ctxt->dst.val));
3842 break;
3843#endif
3844 default:
3845 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3846 break;
3847 }
3848 return X86EMUL_CONTINUE;
3849}
3850
Nadav Amit13e457e2014-10-13 13:04:13 +03003851static int em_clflush(struct x86_emulate_ctxt *ctxt)
3852{
3853 /* emulating clflush regardless of cpuid */
3854 return X86EMUL_CONTINUE;
3855}
3856
Nadav Amit2276b512015-01-26 09:32:24 +02003857static int em_movsxd(struct x86_emulate_ctxt *ctxt)
3858{
3859 ctxt->dst.val = (s32) ctxt->src.val;
3860 return X86EMUL_CONTINUE;
3861}
3862
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003863static bool valid_cr(int nr)
3864{
3865 switch (nr) {
3866 case 0:
3867 case 2 ... 4:
3868 case 8:
3869 return true;
3870 default:
3871 return false;
3872 }
3873}
3874
3875static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3876{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003877 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003878 return emulate_ud(ctxt);
3879
3880 return X86EMUL_CONTINUE;
3881}
3882
3883static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3884{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003885 u64 new_val = ctxt->src.val64;
3886 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003887 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003888
3889 static u64 cr_reserved_bits[] = {
3890 0xffffffff00000000ULL,
3891 0, 0, 0, /* CR3 checked later */
3892 CR4_RESERVED_BITS,
3893 0, 0, 0,
3894 CR8_RESERVED_BITS,
3895 };
3896
3897 if (!valid_cr(cr))
3898 return emulate_ud(ctxt);
3899
3900 if (new_val & cr_reserved_bits[cr])
3901 return emulate_gp(ctxt, 0);
3902
3903 switch (cr) {
3904 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003905 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003906 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3907 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3908 return emulate_gp(ctxt, 0);
3909
Avi Kivity717746e2011-04-20 13:37:53 +03003910 cr4 = ctxt->ops->get_cr(ctxt, 4);
3911 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003912
3913 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3914 !(cr4 & X86_CR4_PAE))
3915 return emulate_gp(ctxt, 0);
3916
3917 break;
3918 }
3919 case 3: {
3920 u64 rsvd = 0;
3921
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003922 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3923 if (efer & EFER_LMA)
Nadav Amit9d88fca2014-11-02 11:54:52 +02003924 rsvd = CR3_L_MODE_RESERVED_BITS & ~CR3_PCID_INVD;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003925
3926 if (new_val & rsvd)
3927 return emulate_gp(ctxt, 0);
3928
3929 break;
3930 }
3931 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003932 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003933
3934 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3935 return emulate_gp(ctxt, 0);
3936
3937 break;
3938 }
3939 }
3940
3941 return X86EMUL_CONTINUE;
3942}
3943
Joerg Roedel3b88e412011-04-04 12:39:29 +02003944static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3945{
3946 unsigned long dr7;
3947
Avi Kivity717746e2011-04-20 13:37:53 +03003948 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003949
3950 /* Check if DR7.Global_Enable is set */
3951 return dr7 & (1 << 13);
3952}
3953
3954static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3955{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003956 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003957 u64 cr4;
3958
3959 if (dr > 7)
3960 return emulate_ud(ctxt);
3961
Avi Kivity717746e2011-04-20 13:37:53 +03003962 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003963 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3964 return emulate_ud(ctxt);
3965
Nadav Amit6d2a0522014-11-02 11:54:43 +02003966 if (check_dr7_gd(ctxt)) {
3967 ulong dr6;
3968
3969 ctxt->ops->get_dr(ctxt, 6, &dr6);
3970 dr6 &= ~15;
3971 dr6 |= DR6_BD | DR6_RTM;
3972 ctxt->ops->set_dr(ctxt, 6, dr6);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003973 return emulate_db(ctxt);
Nadav Amit6d2a0522014-11-02 11:54:43 +02003974 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02003975
3976 return X86EMUL_CONTINUE;
3977}
3978
3979static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3980{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003981 u64 new_val = ctxt->src.val64;
3982 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003983
3984 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3985 return emulate_gp(ctxt, 0);
3986
3987 return check_dr_read(ctxt);
3988}
3989
Joerg Roedel01de8b02011-04-04 12:39:31 +02003990static int check_svme(struct x86_emulate_ctxt *ctxt)
3991{
3992 u64 efer;
3993
Avi Kivity717746e2011-04-20 13:37:53 +03003994 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003995
3996 if (!(efer & EFER_SVME))
3997 return emulate_ud(ctxt);
3998
3999 return X86EMUL_CONTINUE;
4000}
4001
4002static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
4003{
Avi Kivitydd856ef2012-08-27 23:46:17 +03004004 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02004005
4006 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07004007 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02004008 return emulate_gp(ctxt, 0);
4009
4010 return check_svme(ctxt);
4011}
4012
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004013static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
4014{
Avi Kivity717746e2011-04-20 13:37:53 +03004015 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004016
Avi Kivity717746e2011-04-20 13:37:53 +03004017 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004018 return emulate_ud(ctxt);
4019
4020 return X86EMUL_CONTINUE;
4021}
4022
Joerg Roedel80612522011-04-04 12:39:33 +02004023static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
4024{
Avi Kivity717746e2011-04-20 13:37:53 +03004025 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03004026 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02004027
Avi Kivity717746e2011-04-20 13:37:53 +03004028 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03004029 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02004030 return emulate_gp(ctxt, 0);
4031
4032 return X86EMUL_CONTINUE;
4033}
4034
Joerg Roedelf6511932011-04-04 12:39:35 +02004035static int check_perm_in(struct x86_emulate_ctxt *ctxt)
4036{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004037 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
4038 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02004039 return emulate_gp(ctxt, 0);
4040
4041 return X86EMUL_CONTINUE;
4042}
4043
4044static int check_perm_out(struct x86_emulate_ctxt *ctxt)
4045{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004046 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
4047 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02004048 return emulate_gp(ctxt, 0);
4049
4050 return X86EMUL_CONTINUE;
4051}
4052
Avi Kivity73fba5f2010-07-29 15:11:53 +03004053#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004054#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
4055#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
4056 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03004057#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02004058#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004059#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
4060#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Nadav Amit39f062f2014-11-26 15:47:18 +02004061#define ID(_f, _i) { .flags = ((_f) | InstrDual | ModRM), .u.idual = (_i) }
Nadav Amit2276b512015-01-26 09:32:24 +02004062#define MD(_f, _m) { .flags = ((_f) | ModeDual), .u.mdual = (_m) }
Gleb Natapov045a2822012-12-20 16:57:43 +02004063#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03004064#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02004065#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004066#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004067 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004068#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004069 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
4070 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02004071#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03004072
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004073#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02004074#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004075#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02004076#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004077#define I2bvIP(_f, _e, _i, _p) \
4078 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004079
Avi Kivityfb864fb2013-01-04 16:18:54 +02004080#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
4081 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
4082 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03004083
Nadav Amit0f54a322014-08-29 11:26:55 +03004084static const struct opcode group7_rm0[] = {
4085 N,
Jan Kiszkab34a8052015-03-09 20:27:43 +01004086 I(SrcNone | Priv | EmulateOnUD, em_hypercall),
Nadav Amit0f54a322014-08-29 11:26:55 +03004087 N, N, N, N, N, N,
4088};
4089
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004090static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004091 DI(SrcNone | Priv, monitor),
4092 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004093 N, N, N, N, N, N,
4094};
4095
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004096static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004097 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Jan Kiszkab34a8052015-03-09 20:27:43 +01004098 II(SrcNone | Prot | EmulateOnUD, em_hypercall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004099 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
4100 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
4101 DIP(SrcNone | Prot | Priv, stgi, check_svme),
4102 DIP(SrcNone | Prot | Priv, clgi, check_svme),
4103 DIP(SrcNone | Prot | Priv, skinit, check_svme),
4104 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004105};
Avi Kivity6230f7f2010-08-26 18:34:55 +03004106
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004107static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004108 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004109 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004110 N, N, N, N, N, N,
4111};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004112
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004113static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02004114 F(Lock, em_add),
4115 F(Lock | PageTable, em_or),
4116 F(Lock, em_adc),
4117 F(Lock, em_sbb),
4118 F(Lock | PageTable, em_and),
4119 F(Lock, em_sub),
4120 F(Lock, em_xor),
4121 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004122};
4123
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004124static const struct opcode group1A[] = {
Nadav Amitab708092014-12-25 02:52:21 +02004125 I(DstMem | SrcNone | Mov | Stack | IncSP, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004126};
4127
Avi Kivity007a3b52013-01-19 19:51:51 +02004128static const struct opcode group2[] = {
4129 F(DstMem | ModRM, em_rol),
4130 F(DstMem | ModRM, em_ror),
4131 F(DstMem | ModRM, em_rcl),
4132 F(DstMem | ModRM, em_rcr),
4133 F(DstMem | ModRM, em_shl),
4134 F(DstMem | ModRM, em_shr),
4135 F(DstMem | ModRM, em_shl),
4136 F(DstMem | ModRM, em_sar),
4137};
4138
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004139static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02004140 F(DstMem | SrcImm | NoWrite, em_test),
4141 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02004142 F(DstMem | SrcNone | Lock, em_not),
4143 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02004144 F(DstXacc | Src2Mem, em_mul_ex),
4145 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004146 F(DstXacc | Src2Mem, em_div_ex),
4147 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004148};
4149
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004150static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02004151 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
4152 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004153 N, N, N, N, N, N,
4154};
4155
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004156static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02004157 F(DstMem | SrcNone | Lock, em_inc),
4158 F(DstMem | SrcNone | Lock, em_dec),
Nadav Amit58b70752014-10-24 11:35:09 +03004159 I(SrcMem | NearBranch, em_call_near_abs),
Nadav Amitacac6f892015-05-03 20:22:57 +03004160 I(SrcMemFAddr | ImplicitOps, em_call_far),
Nadav Amit58b70752014-10-24 11:35:09 +03004161 I(SrcMem | NearBranch, em_jmp_abs),
Nadav Amitf7784042014-09-18 22:39:41 +03004162 I(SrcMemFAddr | ImplicitOps, em_jmp_far),
4163 I(SrcMem | Stack, em_push), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004164};
4165
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004166static const struct opcode group6[] = {
Nadav Amit63ea0a42015-01-08 11:59:03 +01004167 DI(Prot | DstMem, sldt),
4168 DI(Prot | DstMem, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03004169 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03004170 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004171 N, N, N, N,
4172};
4173
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004174static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03004175 II(Mov | DstMem, em_sgdt, sgdt),
4176 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004177 II(SrcMem | Priv, em_lgdt, lgdt),
4178 II(SrcMem | Priv, em_lidt, lidt),
4179 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
4180 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
4181 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004182}, {
Nadav Amit0f54a322014-08-29 11:26:55 +03004183 EXT(0, group7_rm0),
Avi Kivity5ef39c72011-04-21 12:21:50 +03004184 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004185 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004186 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
4187 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
4188 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004189} };
4190
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004191static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004192 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02004193 F(DstMem | SrcImmByte | NoWrite, em_bt),
4194 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
4195 F(DstMem | SrcImmByte | Lock, em_btr),
4196 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004197};
4198
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004199static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004200 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004201}, {
4202 N, N, N, N, N, N, N, N,
4203} };
4204
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004205static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004206 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004207 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004208};
4209
Nadav Amit13e457e2014-10-13 13:04:13 +03004210static const struct gprefix pfx_0f_ae_7 = {
Nadav Amit3f6f1482014-10-13 13:04:14 +03004211 I(SrcMem | ByteOp, em_clflush), N, N, N,
Nadav Amit13e457e2014-10-13 13:04:13 +03004212};
4213
4214static const struct group_dual group15 = { {
4215 N, N, N, N, N, N, N, GP(0, &pfx_0f_ae_7),
4216}, {
4217 N, N, N, N, N, N, N, N,
4218} };
4219
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004220static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03004221 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02004222};
4223
Nadav Amit39f062f2014-11-26 15:47:18 +02004224static const struct instr_dual instr_dual_0f_2b = {
4225 I(0, em_mov), N
4226};
4227
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004228static const struct gprefix pfx_0f_2b = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004229 ID(0, &instr_dual_0f_2b), ID(0, &instr_dual_0f_2b), N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03004230};
4231
Igor Mammedov27ce8252014-03-15 21:01:59 +01004232static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01004233 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004234};
4235
Alex Williamson0a370272014-07-11 11:56:31 -06004236static const struct gprefix pfx_0f_e7 = {
4237 N, I(Sse, em_mov), N, N,
4238};
4239
Gleb Natapov045a2822012-12-20 16:57:43 +02004240static const struct escape escape_d9 = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02004241 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstcw),
Gleb Natapov045a2822012-12-20 16:57:43 +02004242}, {
4243 /* 0xC0 - 0xC7 */
4244 N, N, N, N, N, N, N, N,
4245 /* 0xC8 - 0xCF */
4246 N, N, N, N, N, N, N, N,
4247 /* 0xD0 - 0xC7 */
4248 N, N, N, N, N, N, N, N,
4249 /* 0xD8 - 0xDF */
4250 N, N, N, N, N, N, N, N,
4251 /* 0xE0 - 0xE7 */
4252 N, N, N, N, N, N, N, N,
4253 /* 0xE8 - 0xEF */
4254 N, N, N, N, N, N, N, N,
4255 /* 0xF0 - 0xF7 */
4256 N, N, N, N, N, N, N, N,
4257 /* 0xF8 - 0xFF */
4258 N, N, N, N, N, N, N, N,
4259} };
4260
4261static const struct escape escape_db = { {
4262 N, N, N, N, N, N, N, N,
4263}, {
4264 /* 0xC0 - 0xC7 */
4265 N, N, N, N, N, N, N, N,
4266 /* 0xC8 - 0xCF */
4267 N, N, N, N, N, N, N, N,
4268 /* 0xD0 - 0xC7 */
4269 N, N, N, N, N, N, N, N,
4270 /* 0xD8 - 0xDF */
4271 N, N, N, N, N, N, N, N,
4272 /* 0xE0 - 0xE7 */
4273 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
4274 /* 0xE8 - 0xEF */
4275 N, N, N, N, N, N, N, N,
4276 /* 0xF0 - 0xF7 */
4277 N, N, N, N, N, N, N, N,
4278 /* 0xF8 - 0xFF */
4279 N, N, N, N, N, N, N, N,
4280} };
4281
4282static const struct escape escape_dd = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02004283 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstsw),
Gleb Natapov045a2822012-12-20 16:57:43 +02004284}, {
4285 /* 0xC0 - 0xC7 */
4286 N, N, N, N, N, N, N, N,
4287 /* 0xC8 - 0xCF */
4288 N, N, N, N, N, N, N, N,
4289 /* 0xD0 - 0xC7 */
4290 N, N, N, N, N, N, N, N,
4291 /* 0xD8 - 0xDF */
4292 N, N, N, N, N, N, N, N,
4293 /* 0xE0 - 0xE7 */
4294 N, N, N, N, N, N, N, N,
4295 /* 0xE8 - 0xEF */
4296 N, N, N, N, N, N, N, N,
4297 /* 0xF0 - 0xF7 */
4298 N, N, N, N, N, N, N, N,
4299 /* 0xF8 - 0xFF */
4300 N, N, N, N, N, N, N, N,
4301} };
4302
Nadav Amit39f062f2014-11-26 15:47:18 +02004303static const struct instr_dual instr_dual_0f_c3 = {
4304 I(DstMem | SrcReg | ModRM | No16 | Mov, em_mov), N
4305};
4306
Nadav Amit2276b512015-01-26 09:32:24 +02004307static const struct mode_dual mode_dual_63 = {
4308 N, I(DstReg | SrcMem32 | ModRM | Mov, em_movsxd)
4309};
4310
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004311static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004312 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004313 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004314 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
4315 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004316 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004317 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004318 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
4319 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004320 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004321 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004322 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
4323 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004324 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004325 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004326 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
4327 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004328 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004329 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004330 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004331 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004332 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004333 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004334 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004335 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004336 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02004337 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004338 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03004339 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004340 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09004341 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004342 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09004343 I(ImplicitOps | Stack | No64, em_pusha),
4344 I(ImplicitOps | Stack | No64, em_popa),
Nadav Amit2276b512015-01-26 09:32:24 +02004345 N, MD(ModRM, &mode_dual_63),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004346 N, N, N, N,
4347 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03004348 I(SrcImm | Mov | Stack, em_push),
4349 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03004350 I(SrcImmByte | Mov | Stack, em_push),
4351 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004352 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09004353 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03004354 /* 0x70 - 0x7F */
Nadav Amit58b70752014-10-24 11:35:09 +03004355 X16(D(SrcImmByte | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004356 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004357 G(ByteOp | DstMem | SrcImm, group1),
4358 G(DstMem | SrcImm, group1),
4359 G(ByteOp | DstMem | SrcImm | No64, group1),
4360 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02004361 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004362 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004363 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004364 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004365 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004366 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09004367 D(ModRM | SrcMem | NoAccess | DstReg),
4368 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
4369 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004370 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004371 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004372 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03004373 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08004374 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09004375 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01004376 II(ImplicitOps | Stack, em_popf, popf),
4377 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004378 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004379 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004380 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004381 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004382 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004383 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004384 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004385 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
4386 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004387 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004388 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004389 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004390 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02004391 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004392 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004393 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Nadav Amit58b70752014-10-24 11:35:09 +03004394 I(ImplicitOps | NearBranch | SrcImmU16, em_ret_near_imm),
4395 I(ImplicitOps | NearBranch, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03004396 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
4397 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004398 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004399 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03004400 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Nadav Amit16794aa2015-01-26 09:32:22 +02004401 I(ImplicitOps | SrcImmU16, em_ret_far_imm),
4402 I(ImplicitOps, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02004403 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004404 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004405 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004406 G(Src2One | ByteOp, group2), G(Src2One, group2),
4407 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02004408 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02004409 I(DstAcc | SrcImmUByte | No64, em_aad),
4410 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004411 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004412 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02004413 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004414 /* 0xE0 - 0xE7 */
Nadav Amit58b70752014-10-24 11:35:09 +03004415 X3(I(SrcImmByte | NearBranch, em_loop)),
4416 I(SrcImmByte | NearBranch, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004417 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
4418 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004419 /* 0xE8 - 0xEF */
Nadav Amit58b70752014-10-24 11:35:09 +03004420 I(SrcImm | NearBranch, em_call), D(SrcImm | ImplicitOps | NearBranch),
4421 I(SrcImmFAddr | No64, em_jmp_far),
4422 D(SrcImmByte | ImplicitOps | NearBranch),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004423 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
4424 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004425 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004426 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004427 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4428 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004429 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09004430 D(ImplicitOps), D(ImplicitOps),
4431 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004432 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4433};
4434
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004435static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004436 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004437 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02004438 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004439 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004440 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004441 N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004442 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004443 N, N, N, N, N, N, N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004444 D(ImplicitOps | ModRM | SrcMem | NoAccess),
4445 N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004446 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03004447 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
4448 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
4449 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
4450 check_cr_write),
4451 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
4452 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004453 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004454 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
4455 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004456 N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b),
Avi Kivity3e114eb2012-04-09 18:40:01 +03004457 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004458 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004459 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004460 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004461 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004462 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02004463 I(ImplicitOps | EmulateOnUD, em_sysenter),
4464 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004465 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004466 N, N, N, N, N, N, N, N,
4467 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03004468 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004469 /* 0x50 - 0x5F */
4470 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4471 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004472 N, N, N, N,
4473 N, N, N, N,
4474 N, N, N, N,
4475 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004476 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004477 N, N, N, N,
4478 N, N, N, N,
4479 N, N, N, N,
4480 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004481 /* 0x80 - 0x8F */
Nadav Amit58b70752014-10-24 11:35:09 +03004482 X16(D(SrcImm | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004483 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004484 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004485 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004486 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004487 II(ImplicitOps, em_cpuid, cpuid),
4488 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004489 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4490 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004491 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004492 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Paolo Bonzini89651a32015-11-03 13:43:05 +01004493 II(EmulateOnUD | ImplicitOps, em_rsm, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004494 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004495 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4496 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Nadav Amit13e457e2014-10-13 13:04:13 +03004497 GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004498 /* 0xB0 - 0xB7 */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02004499 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable | SrcWrite, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004500 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004501 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004502 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4503 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004504 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004505 /* 0xB8 - 0xBF */
4506 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004507 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004508 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Nadav Amit900efe22015-03-30 15:39:21 +03004509 I(DstReg | SrcMem | ModRM, em_bsf_c),
4510 I(DstReg | SrcMem | ModRM, em_bsr_c),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004511 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004512 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02004513 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Nadav Amit39f062f2014-11-26 15:47:18 +02004514 N, ID(0, &instr_dual_0f_c3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004515 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004516 /* 0xC8 - 0xCF */
4517 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004518 /* 0xD0 - 0xDF */
4519 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4520 /* 0xE0 - 0xEF */
Alex Williamson0a370272014-07-11 11:56:31 -06004521 N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
4522 N, N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004523 /* 0xF0 - 0xFF */
4524 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4525};
4526
Nadav Amit39f062f2014-11-26 15:47:18 +02004527static const struct instr_dual instr_dual_0f_38_f0 = {
4528 I(DstReg | SrcMem | Mov, em_movbe), N
4529};
4530
4531static const struct instr_dual instr_dual_0f_38_f1 = {
4532 I(DstMem | SrcReg | Mov, em_movbe), N
4533};
4534
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004535static const struct gprefix three_byte_0f_38_f0 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004536 ID(0, &instr_dual_0f_38_f0), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004537};
4538
4539static const struct gprefix three_byte_0f_38_f1 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004540 ID(0, &instr_dual_0f_38_f1), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004541};
4542
4543/*
4544 * Insns below are selected by the prefix which indexed by the third opcode
4545 * byte.
4546 */
4547static const struct opcode opcode_map_0f_38[256] = {
4548 /* 0x00 - 0x7f */
4549 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004550 /* 0x80 - 0xef */
4551 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4552 /* 0xf0 - 0xf1 */
Nadav Amit53bb4f72014-12-07 11:49:42 +02004553 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0),
4554 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004555 /* 0xf2 - 0xff */
4556 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004557};
4558
Avi Kivity73fba5f2010-07-29 15:11:53 +03004559#undef D
4560#undef N
4561#undef G
4562#undef GD
4563#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004564#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004565#undef EXT
Nadav Amit2276b512015-01-26 09:32:24 +02004566#undef MD
Nadav Amit2b42fce2015-01-26 09:32:25 +02004567#undef ID
Avi Kivity73fba5f2010-07-29 15:11:53 +03004568
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004569#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004570#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004571#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004572#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004573#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004574
Avi Kivity9dac77f2011-06-01 15:34:25 +03004575static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004576{
4577 unsigned size;
4578
Avi Kivity9dac77f2011-06-01 15:34:25 +03004579 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004580 if (size == 8)
4581 size = 4;
4582 return size;
4583}
4584
4585static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4586 unsigned size, bool sign_extension)
4587{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004588 int rc = X86EMUL_CONTINUE;
4589
4590 op->type = OP_IMM;
4591 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004592 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004593 /* NB. Immediates are sign-extended as necessary. */
4594 switch (op->bytes) {
4595 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004596 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004597 break;
4598 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004599 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004600 break;
4601 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004602 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004603 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004604 case 8:
4605 op->val = insn_fetch(s64, ctxt);
4606 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004607 }
4608 if (!sign_extension) {
4609 switch (op->bytes) {
4610 case 1:
4611 op->val &= 0xff;
4612 break;
4613 case 2:
4614 op->val &= 0xffff;
4615 break;
4616 case 4:
4617 op->val &= 0xffffffff;
4618 break;
4619 }
4620 }
4621done:
4622 return rc;
4623}
4624
Avi Kivitya99455492011-09-13 10:45:41 +03004625static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4626 unsigned d)
4627{
4628 int rc = X86EMUL_CONTINUE;
4629
4630 switch (d) {
4631 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004632 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03004633 break;
4634 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004635 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03004636 break;
4637 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004638 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004639 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03004640 *op = ctxt->memop;
4641 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004642 if (ctxt->d & BitOp)
Avi Kivitya99455492011-09-13 10:45:41 +03004643 fetch_bit_operand(ctxt);
4644 op->orig_val = op->val;
4645 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004646 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004647 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004648 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03004649 case OpAcc:
4650 op->type = OP_REG;
4651 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004652 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03004653 fetch_register_operand(op);
4654 op->orig_val = op->val;
4655 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004656 case OpAccLo:
4657 op->type = OP_REG;
4658 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4659 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4660 fetch_register_operand(op);
4661 op->orig_val = op->val;
4662 break;
4663 case OpAccHi:
4664 if (ctxt->d & ByteOp) {
4665 op->type = OP_NONE;
4666 break;
4667 }
4668 op->type = OP_REG;
4669 op->bytes = ctxt->op_bytes;
4670 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4671 fetch_register_operand(op);
4672 op->orig_val = op->val;
4673 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004674 case OpDI:
4675 op->type = OP_MEM;
4676 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4677 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004678 register_address(ctxt, VCPU_REGS_RDI);
Avi Kivitya99455492011-09-13 10:45:41 +03004679 op->addr.mem.seg = VCPU_SREG_ES;
4680 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004681 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03004682 break;
4683 case OpDX:
4684 op->type = OP_REG;
4685 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004686 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03004687 fetch_register_operand(op);
4688 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004689 case OpCL:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004690 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004691 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004692 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004693 break;
4694 case OpImmByte:
4695 rc = decode_imm(ctxt, op, 1, true);
4696 break;
4697 case OpOne:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004698 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004699 op->bytes = 1;
4700 op->val = 1;
4701 break;
4702 case OpImm:
4703 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4704 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004705 case OpImm64:
4706 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4707 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004708 case OpMem8:
4709 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004710 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004711 ctxt->memop.addr.reg = decode_register(ctxt,
4712 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004713 fetch_register_operand(&ctxt->memop);
4714 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004715 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004716 case OpMem16:
4717 ctxt->memop.bytes = 2;
4718 goto mem_common;
4719 case OpMem32:
4720 ctxt->memop.bytes = 4;
4721 goto mem_common;
4722 case OpImmU16:
4723 rc = decode_imm(ctxt, op, 2, false);
4724 break;
4725 case OpImmU:
4726 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4727 break;
4728 case OpSI:
4729 op->type = OP_MEM;
4730 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4731 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004732 register_address(ctxt, VCPU_REGS_RSI);
Bandan Das573e80f2014-04-16 12:46:13 -04004733 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03004734 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004735 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004736 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004737 case OpXLat:
4738 op->type = OP_MEM;
4739 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4740 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004741 address_mask(ctxt,
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004742 reg_read(ctxt, VCPU_REGS_RBX) +
4743 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04004744 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004745 op->val = 0;
4746 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004747 case OpImmFAddr:
4748 op->type = OP_IMM;
4749 op->addr.mem.ea = ctxt->_eip;
4750 op->bytes = ctxt->op_bytes + 2;
4751 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4752 break;
4753 case OpMemFAddr:
4754 ctxt->memop.bytes = ctxt->op_bytes + 2;
4755 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004756 case OpES:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004757 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004758 op->val = VCPU_SREG_ES;
4759 break;
4760 case OpCS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004761 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004762 op->val = VCPU_SREG_CS;
4763 break;
4764 case OpSS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004765 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004766 op->val = VCPU_SREG_SS;
4767 break;
4768 case OpDS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004769 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004770 op->val = VCPU_SREG_DS;
4771 break;
4772 case OpFS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004773 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004774 op->val = VCPU_SREG_FS;
4775 break;
4776 case OpGS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004777 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004778 op->val = VCPU_SREG_GS;
4779 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004780 case OpImplicit:
4781 /* Special instructions do their own operand decoding. */
4782 default:
4783 op->type = OP_NONE; /* Disable writeback. */
4784 break;
4785 }
4786
4787done:
4788 return rc;
4789}
4790
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004791int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004792{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004793 int rc = X86EMUL_CONTINUE;
4794 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004795 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004796 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04004797 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03004798 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004799
Avi Kivityf09ed832011-09-13 10:45:40 +03004800 ctxt->memop.type = OP_NONE;
4801 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004802 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02004803 ctxt->fetch.ptr = ctxt->fetch.data;
4804 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004805 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01004806 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004807 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004808 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02004809 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004810 if (rc != X86EMUL_CONTINUE)
4811 return rc;
4812 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004813
4814 switch (mode) {
4815 case X86EMUL_MODE_REAL:
4816 case X86EMUL_MODE_VM86:
4817 case X86EMUL_MODE_PROT16:
4818 def_op_bytes = def_ad_bytes = 2;
4819 break;
4820 case X86EMUL_MODE_PROT32:
4821 def_op_bytes = def_ad_bytes = 4;
4822 break;
4823#ifdef CONFIG_X86_64
4824 case X86EMUL_MODE_PROT64:
4825 def_op_bytes = 4;
4826 def_ad_bytes = 8;
4827 break;
4828#endif
4829 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004830 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004831 }
4832
Avi Kivity9dac77f2011-06-01 15:34:25 +03004833 ctxt->op_bytes = def_op_bytes;
4834 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004835
4836 /* Legacy prefixes. */
4837 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004838 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004839 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004840 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004841 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004842 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004843 break;
4844 case 0x67: /* address-size override */
4845 if (mode == X86EMUL_MODE_PROT64)
4846 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004847 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004848 else
4849 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004850 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004851 break;
4852 case 0x26: /* ES override */
4853 case 0x2e: /* CS override */
4854 case 0x36: /* SS override */
4855 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004856 has_seg_override = true;
4857 ctxt->seg_override = (ctxt->b >> 3) & 3;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004858 break;
4859 case 0x64: /* FS override */
4860 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004861 has_seg_override = true;
4862 ctxt->seg_override = ctxt->b & 7;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004863 break;
4864 case 0x40 ... 0x4f: /* REX */
4865 if (mode != X86EMUL_MODE_PROT64)
4866 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004867 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004868 continue;
4869 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004870 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004871 break;
4872 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004873 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004874 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004875 break;
4876 default:
4877 goto done_prefixes;
4878 }
4879
4880 /* Any legacy prefix after a REX prefix nullifies its effect. */
4881
Avi Kivity9dac77f2011-06-01 15:34:25 +03004882 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004883 }
4884
4885done_prefixes:
4886
4887 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004888 if (ctxt->rex_prefix & 8)
4889 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004890
4891 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004892 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004893 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004894 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004895 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004896 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004897 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004898
4899 /* 0F_38 opcode map */
4900 if (ctxt->b == 0x38) {
4901 ctxt->opcode_len = 3;
4902 ctxt->b = insn_fetch(u8, ctxt);
4903 opcode = opcode_map_0f_38[ctxt->b];
4904 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004905 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004906 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004907
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004908 if (ctxt->d & ModRM)
4909 ctxt->modrm = insn_fetch(u8, ctxt);
4910
Nadav Amit7fe864d2014-06-02 18:34:03 +03004911 /* vex-prefix instructions are not implemented */
4912 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
Nadav Amitd14cb5d2014-11-02 11:54:58 +02004913 (mode == X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) == 0xc0)) {
Nadav Amit7fe864d2014-06-02 18:34:03 +03004914 ctxt->d = NotImpl;
4915 }
4916
Avi Kivity9dac77f2011-06-01 15:34:25 +03004917 while (ctxt->d & GroupMask) {
4918 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004919 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004920 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004921 opcode = opcode.u.group[goffset];
4922 break;
4923 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004924 goffset = (ctxt->modrm >> 3) & 7;
4925 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004926 opcode = opcode.u.gdual->mod3[goffset];
4927 else
4928 opcode = opcode.u.gdual->mod012[goffset];
4929 break;
4930 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004931 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004932 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004933 break;
4934 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004935 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004936 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004937 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004938 switch (simd_prefix) {
4939 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4940 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4941 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4942 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4943 }
4944 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004945 case Escape:
4946 if (ctxt->modrm > 0xbf)
4947 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4948 else
4949 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4950 break;
Nadav Amit39f062f2014-11-26 15:47:18 +02004951 case InstrDual:
4952 if ((ctxt->modrm >> 6) == 3)
4953 opcode = opcode.u.idual->mod3;
4954 else
4955 opcode = opcode.u.idual->mod012;
4956 break;
Nadav Amit2276b512015-01-26 09:32:24 +02004957 case ModeDual:
4958 if (ctxt->mode == X86EMUL_MODE_PROT64)
4959 opcode = opcode.u.mdual->mode64;
4960 else
4961 opcode = opcode.u.mdual->mode32;
4962 break;
Avi Kivity46561642011-04-24 14:09:59 +03004963 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004964 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004965 }
Avi Kivity46561642011-04-24 14:09:59 +03004966
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004967 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004968 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004969 }
4970
Paolo Bonzinie24186e2014-03-27 12:00:57 +01004971 /* Unrecognised? */
4972 if (ctxt->d == 0)
4973 return EMULATION_FAILED;
4974
Avi Kivity9dac77f2011-06-01 15:34:25 +03004975 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004976
Nadav Amit3a6095a2014-08-13 16:50:13 +03004977 if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD)))
4978 return EMULATION_FAILED;
4979
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004980 if (unlikely(ctxt->d &
Nadav Amited9aad22014-11-02 11:55:00 +02004981 (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch|
4982 No16))) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004983 /*
4984 * These are copied unconditionally here, and checked unconditionally
4985 * in x86_emulate_insn.
4986 */
4987 ctxt->check_perm = opcode.check_perm;
4988 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004989
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004990 if (ctxt->d & NotImpl)
4991 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004992
Nadav Amit58b70752014-10-24 11:35:09 +03004993 if (mode == X86EMUL_MODE_PROT64) {
4994 if (ctxt->op_bytes == 4 && (ctxt->d & Stack))
4995 ctxt->op_bytes = 8;
4996 else if (ctxt->d & NearBranch)
4997 ctxt->op_bytes = 8;
4998 }
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004999
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005000 if (ctxt->d & Op3264) {
5001 if (mode == X86EMUL_MODE_PROT64)
5002 ctxt->op_bytes = 8;
5003 else
5004 ctxt->op_bytes = 4;
5005 }
5006
Nadav Amited9aad22014-11-02 11:55:00 +02005007 if ((ctxt->d & No16) && ctxt->op_bytes == 2)
5008 ctxt->op_bytes = 4;
5009
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005010 if (ctxt->d & Sse)
5011 ctxt->op_bytes = 16;
5012 else if (ctxt->d & Mmx)
5013 ctxt->op_bytes = 8;
5014 }
Avi Kivity1253791d2011-03-29 11:41:27 +02005015
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005016 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005017 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03005018 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04005019 if (!has_seg_override) {
5020 has_seg_override = true;
5021 ctxt->seg_override = ctxt->modrm_seg;
5022 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03005023 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03005024 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005025 if (rc != X86EMUL_CONTINUE)
5026 goto done;
5027
Bandan Das573e80f2014-04-16 12:46:13 -04005028 if (!has_seg_override)
5029 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005030
Bandan Das573e80f2014-04-16 12:46:13 -04005031 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005032
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005033 /*
5034 * Decode and fetch the source operand: register, memory
5035 * or immediate.
5036 */
Avi Kivity0fe59122011-09-13 10:45:47 +03005037 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03005038 if (rc != X86EMUL_CONTINUE)
5039 goto done;
5040
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005041 /*
5042 * Decode and fetch the second source operand: register, memory
5043 * or immediate.
5044 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03005045 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03005046 if (rc != X86EMUL_CONTINUE)
5047 goto done;
5048
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005049 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03005050 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005051
Owen Hofmannd9092f52016-10-27 11:25:52 -07005052 if (ctxt->rip_relative && likely(ctxt->memopp))
Nadav Amit1c1c35a2014-11-19 17:43:09 +02005053 ctxt->memopp->addr.mem.ea = address_mask(ctxt,
5054 ctxt->memopp->addr.mem.ea + ctxt->_eip);
Avi Kivitycb16c342011-06-19 19:21:11 +03005055
Paolo Bonzinia430c912014-10-23 14:54:14 +02005056done:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005057 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005058}
5059
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005060bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
5061{
5062 return ctxt->d & PageTable;
5063}
5064
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005065static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
5066{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005067 /* The second termination condition only applies for REPE
5068 * and REPNE. Test if the repeat string operation prefix is
5069 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
5070 * corresponding termination condition according to:
5071 * - if REPE/REPZ and ZF = 0 then done
5072 * - if REPNE/REPNZ and ZF = 1 then done
5073 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005074 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
5075 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
5076 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03005077 ((ctxt->eflags & X86_EFLAGS_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03005078 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03005079 ((ctxt->eflags & X86_EFLAGS_ZF) == X86_EFLAGS_ZF))))
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005080 return true;
5081
5082 return false;
5083}
5084
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005085static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
5086{
5087 bool fault = false;
5088
5089 ctxt->ops->get_fpu(ctxt);
5090 asm volatile("1: fwait \n\t"
5091 "2: \n\t"
5092 ".pushsection .fixup,\"ax\" \n\t"
5093 "3: \n\t"
5094 "movb $1, %[fault] \n\t"
5095 "jmp 2b \n\t"
5096 ".popsection \n\t"
5097 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03005098 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005099 ctxt->ops->put_fpu(ctxt);
5100
5101 if (unlikely(fault))
5102 return emulate_exception(ctxt, MF_VECTOR, 0, false);
5103
5104 return X86EMUL_CONTINUE;
5105}
5106
5107static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
5108 struct operand *op)
5109{
5110 if (op->type == OP_MM)
5111 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
5112}
5113
Avi Kivitye28bbd42013-01-04 16:18:48 +02005114static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
5115{
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005116 register void *__sp asm(_ASM_SP);
Avi Kivitye28bbd42013-01-04 16:18:48 +02005117 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005118
Avi Kivityb9fa4092013-02-09 11:31:48 +02005119 if (!(ctxt->d & ByteOp))
5120 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005121
Avi Kivitye28bbd42013-01-04 16:18:48 +02005122 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005123 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005124 [fastop]"+S"(fop), "+r"(__sp)
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005125 : "c"(ctxt->src2.val));
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005126
Avi Kivitye28bbd42013-01-04 16:18:48 +02005127 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005128 if (!fop) /* exception is returned in fop variable */
5129 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02005130 return X86EMUL_CONTINUE;
5131}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005132
Bandan Das14985072014-04-16 12:46:09 -04005133void init_decode_cache(struct x86_emulate_ctxt *ctxt)
5134{
Bandan Das573e80f2014-04-16 12:46:13 -04005135 memset(&ctxt->rip_relative, 0,
5136 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04005137
Bandan Das14985072014-04-16 12:46:09 -04005138 ctxt->io_read.pos = 0;
5139 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04005140 ctxt->mem_read.end = 0;
5141}
5142
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09005143int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005144{
Mathias Krause0225fb52012-08-30 01:30:16 +02005145 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09005146 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005147 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005148
Avi Kivity9dac77f2011-06-01 15:34:25 +03005149 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04005150
Gleb Natapovd380a5e2010-02-10 14:21:36 +02005151 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005152 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02005153 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02005154 goto done;
5155 }
5156
Avi Kivity9dac77f2011-06-01 15:34:25 +03005157 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02005158 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03005159 goto done;
5160 }
5161
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005162 if (unlikely(ctxt->d &
5163 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
5164 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
5165 (ctxt->d & Undefined)) {
5166 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005167 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005168 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005169
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005170 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
5171 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
5172 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02005173 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005174 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02005175
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005176 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
5177 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02005178 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005179 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02005180
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005181 if (ctxt->d & Mmx) {
5182 rc = flush_pending_x87_faults(ctxt);
5183 if (rc != X86EMUL_CONTINUE)
5184 goto done;
5185 /*
5186 * Now that we know the fpu is exception safe, we can fetch
5187 * operands from it.
5188 */
5189 fetch_possible_mmx_operand(ctxt, &ctxt->src);
5190 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
5191 if (!(ctxt->d & Mov))
5192 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
5193 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02005194
Paolo Bonzinia5845392015-04-01 18:18:53 +02005195 if (unlikely(ctxt->emul_flags & X86EMUL_GUEST_MASK) && ctxt->intercept) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005196 rc = emulator_check_intercept(ctxt, ctxt->intercept,
5197 X86_ICPT_PRE_EXCEPT);
5198 if (rc != X86EMUL_CONTINUE)
5199 goto done;
5200 }
5201
Nadav Amit64a38292014-12-10 11:19:04 +02005202 /* Instruction can only be executed in protected mode */
5203 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
5204 rc = emulate_ud(ctxt);
5205 goto done;
5206 }
5207
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005208 /* Privileged instruction can be executed only in CPL=0 */
5209 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Nadav Amit68efa762014-06-18 17:19:35 +03005210 if (ctxt->d & PrivUD)
5211 rc = emulate_ud(ctxt);
5212 else
5213 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02005214 goto done;
5215 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005216
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005217 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04005218 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005219 rc = ctxt->check_perm(ctxt);
5220 if (rc != X86EMUL_CONTINUE)
5221 goto done;
5222 }
5223
Paolo Bonzinia5845392015-04-01 18:18:53 +02005224 if (unlikely(ctxt->emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005225 rc = emulator_check_intercept(ctxt, ctxt->intercept,
5226 X86_ICPT_POST_EXCEPT);
5227 if (rc != X86EMUL_CONTINUE)
5228 goto done;
5229 }
5230
5231 if (ctxt->rep_prefix && (ctxt->d & String)) {
5232 /* All REP prefixes have the same first termination condition */
5233 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Nadav Amit428e3d02015-04-28 13:06:01 +03005234 string_registers_quirk(ctxt);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005235 ctxt->eip = ctxt->_eip;
Nadav Amit0efb0442015-03-29 16:33:03 +03005236 ctxt->eflags &= ~X86_EFLAGS_RF;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005237 goto done;
5238 }
5239 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02005240 }
5241
Avi Kivity9dac77f2011-06-01 15:34:25 +03005242 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
5243 rc = segmented_read(ctxt, ctxt->src.addr.mem,
5244 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09005245 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005246 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005247 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005248 }
5249
Avi Kivity9dac77f2011-06-01 15:34:25 +03005250 if (ctxt->src2.type == OP_MEM) {
5251 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
5252 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02005253 if (rc != X86EMUL_CONTINUE)
5254 goto done;
5255 }
5256
Avi Kivity9dac77f2011-06-01 15:34:25 +03005257 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005258 goto special_insn;
5259
5260
Avi Kivity9dac77f2011-06-01 15:34:25 +03005261 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02005262 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005263 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
5264 &ctxt->dst.val, ctxt->dst.bytes);
Nadav Amitc205fb72014-12-25 02:52:16 +02005265 if (rc != X86EMUL_CONTINUE) {
Paolo Bonzinid44e1212015-02-09 10:02:05 +01005266 if (!(ctxt->d & NoWrite) &&
5267 rc == X86EMUL_PROPAGATE_FAULT &&
Nadav Amitc205fb72014-12-25 02:52:16 +02005268 ctxt->exception.vector == PF_VECTOR)
5269 ctxt->exception.error_code |= PFERR_WRITE_MASK;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02005270 goto done;
Nadav Amitc205fb72014-12-25 02:52:16 +02005271 }
Avi Kivity038e51d2007-01-22 20:40:40 -08005272 }
Paolo Bonzini4ff6f8e2015-02-12 17:04:47 +01005273 /* Copy full 64-bit value for CMPXCHG8B. */
5274 ctxt->dst.orig_val64 = ctxt->dst.val64;
Avi Kivity038e51d2007-01-22 20:40:40 -08005275
Avi Kivity018a98d2007-11-27 19:30:56 +02005276special_insn:
5277
Paolo Bonzinia5845392015-04-01 18:18:53 +02005278 if (unlikely(ctxt->emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03005279 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005280 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02005281 if (rc != X86EMUL_CONTINUE)
5282 goto done;
5283 }
5284
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03005285 if (ctxt->rep_prefix && (ctxt->d & String))
Nadav Amit0efb0442015-03-29 16:33:03 +03005286 ctxt->eflags |= X86_EFLAGS_RF;
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03005287 else
Nadav Amit0efb0442015-03-29 16:33:03 +03005288 ctxt->eflags &= ~X86_EFLAGS_RF;
Nadav Amit4467c3f2014-07-21 14:37:29 +03005289
Avi Kivity9dac77f2011-06-01 15:34:25 +03005290 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02005291 if (ctxt->d & Fastop) {
5292 void (*fop)(struct fastop *) = (void *)ctxt->execute;
5293 rc = fastop(ctxt, fop);
5294 if (rc != X86EMUL_CONTINUE)
5295 goto done;
5296 goto writeback;
5297 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03005298 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03005299 if (rc != X86EMUL_CONTINUE)
5300 goto done;
5301 goto writeback;
5302 }
5303
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02005304 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005305 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005306 else if (ctxt->opcode_len == 3)
5307 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005308
Avi Kivity9dac77f2011-06-01 15:34:25 +03005309 switch (ctxt->b) {
Gleb Natapovb2833e32009-04-12 13:36:30 +03005310 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005311 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005312 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005313 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03005314 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005315 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03005316 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03005317 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005318 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03005319 ctxt->dst.type = OP_NONE;
5320 else
5321 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09005322 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005323 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005324 switch (ctxt->op_bytes) {
5325 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
5326 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
5327 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005328 }
5329 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005330 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005331 rc = emulate_int(ctxt, 3);
5332 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005333 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005334 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005335 break;
5336 case 0xce: /* into */
Nadav Amit0efb0442015-03-29 16:33:03 +03005337 if (ctxt->eflags & X86_EFLAGS_OF)
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005338 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005339 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005340 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09005341 case 0xeb: /* jmp rel short */
Nadav Amit234f3ce2014-09-18 22:39:38 +03005342 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity9dac77f2011-06-01 15:34:25 +03005343 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005344 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005345 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03005346 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03005347 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005348 case 0xf5: /* cmc */
5349 /* complement carry flag from eflags reg */
Nadav Amit0efb0442015-03-29 16:33:03 +03005350 ctxt->eflags ^= X86_EFLAGS_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005351 break;
5352 case 0xf8: /* clc */
Nadav Amit0efb0442015-03-29 16:33:03 +03005353 ctxt->eflags &= ~X86_EFLAGS_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005354 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03005355 case 0xf9: /* stc */
Nadav Amit0efb0442015-03-29 16:33:03 +03005356 ctxt->eflags |= X86_EFLAGS_CF;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03005357 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005358 case 0xfc: /* cld */
Nadav Amit0efb0442015-03-29 16:33:03 +03005359 ctxt->eflags &= ~X86_EFLAGS_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005360 break;
5361 case 0xfd: /* std */
Nadav Amit0efb0442015-03-29 16:33:03 +03005362 ctxt->eflags |= X86_EFLAGS_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005363 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005364 default:
5365 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005366 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005367
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005368 if (rc != X86EMUL_CONTINUE)
5369 goto done;
5370
Avi Kivity018a98d2007-11-27 19:30:56 +02005371writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02005372 if (ctxt->d & SrcWrite) {
5373 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
5374 rc = writeback(ctxt, &ctxt->src);
5375 if (rc != X86EMUL_CONTINUE)
5376 goto done;
5377 }
Nadav Amitee212292014-06-15 16:12:58 +03005378 if (!(ctxt->d & NoWrite)) {
5379 rc = writeback(ctxt, &ctxt->dst);
5380 if (rc != X86EMUL_CONTINUE)
5381 goto done;
5382 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005383
Gleb Natapov5cd21912010-03-18 15:20:26 +02005384 /*
5385 * restore dst type in case the decoding will be reused
5386 * (happens for string instruction )
5387 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005388 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005389
Avi Kivity9dac77f2011-06-01 15:34:25 +03005390 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005391 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02005392
Avi Kivity9dac77f2011-06-01 15:34:25 +03005393 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005394 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02005395
Avi Kivity9dac77f2011-06-01 15:34:25 +03005396 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005397 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005398 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005399 if ((ctxt->d & SrcMask) == SrcSI)
5400 count = ctxt->src.count;
5401 else
5402 count = ctxt->dst.count;
Paolo Bonzini01485a22014-11-19 18:25:08 +01005403 register_address_increment(ctxt, VCPU_REGS_RCX, -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005404
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005405 if (!string_insn_completed(ctxt)) {
5406 /*
5407 * Re-enter guest when pio read ahead buffer is empty
5408 * or, if it is not used, after each 1024 iteration.
5409 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005410 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005411 (r->end == 0 || r->end != r->pos)) {
5412 /*
5413 * Reset read cache. Usually happens before
5414 * decode, but since instruction is restarted
5415 * we have to do it here.
5416 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005417 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005418 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005419 return EMULATION_RESTART;
5420 }
5421 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03005422 }
Nadav Amit0efb0442015-03-29 16:33:03 +03005423 ctxt->eflags &= ~X86_EFLAGS_RF;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005424 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005425
Avi Kivity9dac77f2011-06-01 15:34:25 +03005426 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02005427
5428done:
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005429 if (rc == X86EMUL_PROPAGATE_FAULT) {
5430 WARN_ON(ctxt->exception.vector > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +02005431 ctxt->have_exception = true;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005432 }
Joerg Roedel775fde82011-04-04 12:39:24 +02005433 if (rc == X86EMUL_INTERCEPTED)
5434 return EMULATION_INTERCEPTED;
5435
Avi Kivitydd856ef2012-08-27 23:46:17 +03005436 if (rc == X86EMUL_CONTINUE)
5437 writeback_registers(ctxt);
5438
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005439 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005440
5441twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005442 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02005443 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02005444 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005445 break;
5446 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02005447 case 0x0d: /* GrpP (prefetch) */
5448 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02005449 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02005450 break;
5451 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005452 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02005453 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005454 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005455 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005456 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005457 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03005458 if (test_cc(ctxt->b, ctxt->eflags))
5459 ctxt->dst.val = ctxt->src.val;
Nadav Amitb91aa142015-03-30 15:39:19 +03005460 else if (ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005461 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08005462 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03005463 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03005464 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005465 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005466 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08005467 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005468 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08005469 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005470 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005471 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005472 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03005473 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005474 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005475 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005476 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005477 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03005478 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005479 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005480 default:
5481 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005482 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005483
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005484threebyte_insn:
5485
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005486 if (rc != X86EMUL_CONTINUE)
5487 goto done;
5488
Avi Kivity6aa8b732006-12-10 02:21:36 -08005489 goto writeback;
5490
5491cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02005492 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005493}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005494
5495void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
5496{
5497 invalidate_registers(ctxt);
5498}
5499
5500void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
5501{
5502 writeback_registers(ctxt);
5503}