blob: 43a5b432d06f7604fcf2a92de20f59ba1f5ff5d5 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivityb7d491e2013-01-04 16:18:49 +020027#include <linux/stringify.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028
Avi Kivity3eeb3282010-01-21 15:31:48 +020029#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020030#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020031
Avi Kivity6aa8b732006-12-10 02:21:36 -080032/*
Avi Kivitya9945542011-09-13 10:45:41 +030033 * Operand types
34 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030035#define OpNone 0ull
36#define OpImplicit 1ull /* No generic decode */
37#define OpReg 2ull /* Register */
38#define OpMem 3ull /* Memory */
39#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
40#define OpDI 5ull /* ES:DI/EDI/RDI */
41#define OpMem64 6ull /* Memory, 64-bit */
42#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
43#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030044#define OpCL 9ull /* CL register (for shifts) */
45#define OpImmByte 10ull /* 8-bit sign extended immediate */
46#define OpOne 11ull /* Implied 1 */
Nadav Amit5e2c6882012-12-06 21:55:10 -020047#define OpImm 12ull /* Sign extended up to 32-bit immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030048#define OpMem16 13ull /* Memory operand (16-bit). */
49#define OpMem32 14ull /* Memory operand (32-bit). */
50#define OpImmU 15ull /* Immediate operand, zero extended */
51#define OpSI 16ull /* SI/ESI/RSI */
52#define OpImmFAddr 17ull /* Immediate far address */
53#define OpMemFAddr 18ull /* Far address in memory */
54#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030055#define OpES 20ull /* ES */
56#define OpCS 21ull /* CS */
57#define OpSS 22ull /* SS */
58#define OpDS 23ull /* DS */
59#define OpFS 24ull /* FS */
60#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020061#define OpMem8 26ull /* 8-bit zero extended memory operand */
Nadav Amit5e2c6882012-12-06 21:55:10 -020062#define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */
Paolo Bonzini7fa57952013-05-09 11:32:50 +020063#define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */
Avi 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 Kivitya9945542011-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 Kivitya9945542011-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 Kivitya9945542011-09-13 10:45:41 +030082#define DstShift 1
83#define ImplicitOps (OpImplicit << DstShift)
84#define DstReg (OpReg << DstShift)
85#define DstMem (OpMem << DstShift)
86#define DstAcc (OpAcc << DstShift)
87#define DstDI (OpDI << DstShift)
88#define DstMem64 (OpMem64 << DstShift)
89#define DstImmUByte (OpImmUByte << DstShift)
90#define DstDX (OpDX << DstShift)
Avi Kivity820207c2013-02-09 11:31:45 +020091#define DstAccLo (OpAccLo << DstShift)
Avi Kivitya9945542011-09-13 10:45:41 +030092#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080093/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030094#define SrcShift 6
95#define SrcNone (OpNone << SrcShift)
96#define SrcReg (OpReg << SrcShift)
97#define SrcMem (OpMem << SrcShift)
98#define SrcMem16 (OpMem16 << SrcShift)
99#define SrcMem32 (OpMem32 << SrcShift)
100#define SrcImm (OpImm << SrcShift)
101#define SrcImmByte (OpImmByte << SrcShift)
102#define SrcOne (OpOne << SrcShift)
103#define SrcImmUByte (OpImmUByte << SrcShift)
104#define SrcImmU (OpImmU << SrcShift)
105#define SrcSI (OpSI << SrcShift)
Paolo Bonzini7fa57952013-05-09 11:32:50 +0200106#define SrcXLat (OpXLat << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300107#define SrcImmFAddr (OpImmFAddr << SrcShift)
108#define SrcMemFAddr (OpMemFAddr << SrcShift)
109#define SrcAcc (OpAcc << SrcShift)
110#define SrcImmU16 (OpImmU16 << SrcShift)
Nadav Amit5e2c6882012-12-06 21:55:10 -0200111#define SrcImm64 (OpImm64 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300112#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200113#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity820207c2013-02-09 11:31:45 +0200114#define SrcAccHi (OpAccHi << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300115#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300116#define BitOp (1<<11)
117#define MemAbs (1<<12) /* Memory operand is absolute displacement */
118#define String (1<<13) /* String instruction (rep capable) */
119#define Stack (1<<14) /* Stack instruction (push/pop) */
120#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
121#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
122#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
123#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
124#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
Gleb Natapov045a2822012-12-20 16:57:43 +0200125#define Escape (5<<15) /* Escape to coprocessor instruction */
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300126#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300127/* Generic ModRM decode. */
128#define ModRM (1<<19)
129/* Destination is only written; never read. */
130#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300131/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200132#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Borislav Petkovb51e9742013-09-22 16:44:52 +0200133#define EmulateOnUD (1<<22) /* Emulate if unsupported by the host */
Avi Kivity5a506b12010-08-01 15:10:29 +0300134#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300135#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300136#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200137#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200138#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300139#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800140#define PageTable (1 << 29) /* instruction used to write page table */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300141#define NotImpl (1 << 30) /* instruction is not implemented */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100142/* Source 2 operand type */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300143#define Src2Shift (31)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300144#define Src2None (OpNone << Src2Shift)
Avi Kivityab2c5ce2013-02-09 11:31:46 +0200145#define Src2Mem (OpMem << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300146#define Src2CL (OpCL << Src2Shift)
147#define Src2ImmByte (OpImmByte << Src2Shift)
148#define Src2One (OpOne << Src2Shift)
149#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300150#define Src2ES (OpES << Src2Shift)
151#define Src2CS (OpCS << Src2Shift)
152#define Src2SS (OpSS << Src2Shift)
153#define Src2DS (OpDS << Src2Shift)
154#define Src2FS (OpFS << Src2Shift)
155#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300156#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300157#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Avi Kivity1c11b372012-04-09 18:39:59 +0300158#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
159#define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */
160#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
Avi Kivitye28bbd42013-01-04 16:18:48 +0200161#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
Avi Kivityb6744dc2013-01-04 16:18:50 +0200162#define NoWrite ((u64)1 << 45) /* No writeback */
Avi Kivityfb32b1e2013-02-09 11:31:44 +0200163#define SrcWrite ((u64)1 << 46) /* Write back src operand */
Nadav Amit9b88ae92014-05-25 23:05:21 +0300164#define NoMod ((u64)1 << 47) /* Mod field is ignored */
Paolo Bonzinid40a6892014-03-27 11:58:02 +0100165#define Intercept ((u64)1 << 48) /* Has valid intercept field */
166#define CheckPerm ((u64)1 << 49) /* Has valid check_perm field */
Nadav Amit10e38fc2014-06-18 17:19:34 +0300167#define NoBigReal ((u64)1 << 50) /* No big real mode */
Nadav Amit68efa762014-06-18 17:19:35 +0300168#define PrivUD ((u64)1 << 51) /* #UD instead of #GP on CPL > 0 */
Nadav Amit58b70752014-10-24 11:35:09 +0300169#define NearBranch ((u64)1 << 52) /* Near branches */
Nadav Amited9aad22014-11-02 11:55:00 +0200170#define No16 ((u64)1 << 53) /* No 16 bit operand */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800171
Avi Kivity820207c2013-02-09 11:31:45 +0200172#define DstXacc (DstAccLo | SrcAccHi | SrcWrite)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800173
Avi Kivityd0e53322010-07-29 15:11:54 +0300174#define X2(x...) x, x
175#define X3(x...) X2(x), x
176#define X4(x...) X2(x), X2(x)
177#define X5(x...) X4(x), x
178#define X6(x...) X4(x), X2(x)
179#define X7(x...) X4(x), X3(x)
180#define X8(x...) X4(x), X4(x)
181#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300182
Avi Kivitye28bbd42013-01-04 16:18:48 +0200183#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
184#define FASTOP_SIZE 8
185
186/*
187 * fastop functions have a special calling convention:
188 *
Avi Kivity017da7b2013-02-09 11:31:47 +0200189 * dst: rax (in/out)
190 * src: rdx (in/out)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200191 * src2: rcx (in)
192 * flags: rflags (in/out)
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200193 * ex: rsi (in:fastop pointer, out:zero if exception)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200194 *
195 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
196 * different operand sizes can be reached by calculation, rather than a jump
197 * table (which would be bigger than the code).
198 *
199 * fastop functions are declared as taking a never-defined fastop parameter,
200 * so they can't be called from C directly.
201 */
202
203struct fastop;
204
Avi Kivityd65b1de2010-07-29 15:11:35 +0300205struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300206 u64 flags : 56;
207 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300208 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300209 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200210 const struct opcode *group;
211 const struct group_dual *gdual;
212 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200213 const struct escape *esc;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200214 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300215 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200216 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300217};
218
219struct group_dual {
220 struct opcode mod012[8];
221 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300222};
223
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200224struct gprefix {
225 struct opcode pfx_no;
226 struct opcode pfx_66;
227 struct opcode pfx_f2;
228 struct opcode pfx_f3;
229};
230
Gleb Natapov045a2822012-12-20 16:57:43 +0200231struct escape {
232 struct opcode op[8];
233 struct opcode high[64];
234};
235
Avi Kivity6aa8b732006-12-10 02:21:36 -0800236/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200237#define EFLG_ID (1<<21)
238#define EFLG_VIP (1<<20)
239#define EFLG_VIF (1<<19)
240#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200241#define EFLG_VM (1<<17)
242#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200243#define EFLG_IOPL (3<<12)
244#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800245#define EFLG_OF (1<<11)
246#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200247#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200248#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800249#define EFLG_SF (1<<7)
250#define EFLG_ZF (1<<6)
251#define EFLG_AF (1<<4)
252#define EFLG_PF (1<<2)
253#define EFLG_CF (1<<0)
254
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300255#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
256#define EFLG_RESERVED_ONE_MASK 2
257
Avi Kivitydd856ef2012-08-27 23:46:17 +0300258static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
259{
260 if (!(ctxt->regs_valid & (1 << nr))) {
261 ctxt->regs_valid |= 1 << nr;
262 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
263 }
264 return ctxt->_regs[nr];
265}
266
267static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
268{
269 ctxt->regs_valid |= 1 << nr;
270 ctxt->regs_dirty |= 1 << nr;
271 return &ctxt->_regs[nr];
272}
273
274static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
275{
276 reg_read(ctxt, nr);
277 return reg_write(ctxt, nr);
278}
279
280static void writeback_registers(struct x86_emulate_ctxt *ctxt)
281{
282 unsigned reg;
283
284 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
285 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
286}
287
288static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
289{
290 ctxt->regs_dirty = 0;
291 ctxt->regs_valid = 0;
292}
293
Avi Kivity6aa8b732006-12-10 02:21:36 -0800294/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800295 * These EFLAGS bits are restored from saved value during emulation, and
296 * any changes are written back to the saved value after emulation.
297 */
298#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
299
Avi Kivitydda96d82008-11-26 15:14:10 +0200300#ifdef CONFIG_X86_64
301#define ON64(x) x
302#else
303#define ON64(x)
304#endif
305
Avi Kivity4d758342013-01-19 19:51:55 +0200306static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *));
307
Avi Kivityb7d491e2013-01-04 16:18:49 +0200308#define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t"
309#define FOP_RET "ret \n\t"
310
311#define FOP_START(op) \
312 extern void em_##op(struct fastop *fake); \
313 asm(".pushsection .text, \"ax\" \n\t" \
314 ".global em_" #op " \n\t" \
315 FOP_ALIGN \
316 "em_" #op ": \n\t"
317
318#define FOP_END \
319 ".popsection")
320
Avi Kivity0bdea062013-01-19 19:51:50 +0200321#define FOPNOP() FOP_ALIGN FOP_RET
322
Avi Kivityb7d491e2013-01-04 16:18:49 +0200323#define FOP1E(op, dst) \
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200324 FOP_ALIGN "10: " #op " %" #dst " \n\t" FOP_RET
325
326#define FOP1EEX(op, dst) \
327 FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200328
329#define FASTOP1(op) \
330 FOP_START(op) \
331 FOP1E(op##b, al) \
332 FOP1E(op##w, ax) \
333 FOP1E(op##l, eax) \
334 ON64(FOP1E(op##q, rax)) \
335 FOP_END
336
Avi Kivityb9fa4092013-02-09 11:31:48 +0200337/* 1-operand, using src2 (for MUL/DIV r/m) */
338#define FASTOP1SRC2(op, name) \
339 FOP_START(name) \
340 FOP1E(op, cl) \
341 FOP1E(op, cx) \
342 FOP1E(op, ecx) \
343 ON64(FOP1E(op, rcx)) \
344 FOP_END
345
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200346/* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */
347#define FASTOP1SRC2EX(op, name) \
348 FOP_START(name) \
349 FOP1EEX(op, cl) \
350 FOP1EEX(op, cx) \
351 FOP1EEX(op, ecx) \
352 ON64(FOP1EEX(op, rcx)) \
353 FOP_END
354
Avi Kivityf7857f32013-01-04 16:18:53 +0200355#define FOP2E(op, dst, src) \
356 FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET
357
358#define FASTOP2(op) \
359 FOP_START(op) \
Avi Kivity017da7b2013-02-09 11:31:47 +0200360 FOP2E(op##b, al, dl) \
361 FOP2E(op##w, ax, dx) \
362 FOP2E(op##l, eax, edx) \
363 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivityf7857f32013-01-04 16:18:53 +0200364 FOP_END
365
Avi Kivity11c363b2013-01-19 19:51:54 +0200366/* 2 operand, word only */
367#define FASTOP2W(op) \
368 FOP_START(op) \
369 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200370 FOP2E(op##w, ax, dx) \
371 FOP2E(op##l, eax, edx) \
372 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivity11c363b2013-01-19 19:51:54 +0200373 FOP_END
374
Avi Kivity007a3b52013-01-19 19:51:51 +0200375/* 2 operand, src is CL */
376#define FASTOP2CL(op) \
377 FOP_START(op) \
378 FOP2E(op##b, al, cl) \
379 FOP2E(op##w, ax, cl) \
380 FOP2E(op##l, eax, cl) \
381 ON64(FOP2E(op##q, rax, cl)) \
382 FOP_END
383
Nadav Amit5aca3722014-11-02 11:54:50 +0200384/* 2 operand, src and dest are reversed */
385#define FASTOP2R(op, name) \
386 FOP_START(name) \
387 FOP2E(op##b, dl, al) \
388 FOP2E(op##w, dx, ax) \
389 FOP2E(op##l, edx, eax) \
390 ON64(FOP2E(op##q, rdx, rax)) \
391 FOP_END
392
Avi Kivity0bdea062013-01-19 19:51:50 +0200393#define FOP3E(op, dst, src, src2) \
394 FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
395
396/* 3-operand, word-only, src2=cl */
397#define FASTOP3WCL(op) \
398 FOP_START(op) \
399 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200400 FOP3E(op##w, ax, dx, cl) \
401 FOP3E(op##l, eax, edx, cl) \
402 ON64(FOP3E(op##q, rax, rdx, cl)) \
Avi Kivity0bdea062013-01-19 19:51:50 +0200403 FOP_END
404
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200405/* Special case for SETcc - 1 instruction per cc */
406#define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t"
407
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200408asm(".global kvm_fastop_exception \n"
409 "kvm_fastop_exception: xor %esi, %esi; ret");
410
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200411FOP_START(setcc)
412FOP_SETCC(seto)
413FOP_SETCC(setno)
414FOP_SETCC(setc)
415FOP_SETCC(setnc)
416FOP_SETCC(setz)
417FOP_SETCC(setnz)
418FOP_SETCC(setbe)
419FOP_SETCC(setnbe)
420FOP_SETCC(sets)
421FOP_SETCC(setns)
422FOP_SETCC(setp)
423FOP_SETCC(setnp)
424FOP_SETCC(setl)
425FOP_SETCC(setnl)
426FOP_SETCC(setle)
427FOP_SETCC(setnle)
428FOP_END;
429
Paolo Bonzini326f5782013-05-09 11:32:51 +0200430FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET
431FOP_END;
432
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200433static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
434 enum x86_intercept intercept,
435 enum x86_intercept_stage stage)
436{
437 struct x86_instruction_info info = {
438 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300439 .rep_prefix = ctxt->rep_prefix,
440 .modrm_mod = ctxt->modrm_mod,
441 .modrm_reg = ctxt->modrm_reg,
442 .modrm_rm = ctxt->modrm_rm,
443 .src_val = ctxt->src.val64,
Jan Kiszka6cbc5f52014-06-30 12:52:55 +0200444 .dst_val = ctxt->dst.val64,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300445 .src_bytes = ctxt->src.bytes,
446 .dst_bytes = ctxt->dst.bytes,
447 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200448 .next_rip = ctxt->eip,
449 };
450
Avi Kivity29535382011-04-20 13:37:53 +0300451 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200452}
453
Avi Kivityf47cfa32012-06-07 17:49:24 +0300454static void assign_masked(ulong *dest, ulong src, ulong mask)
455{
456 *dest = (*dest & ~mask) | (src & mask);
457}
458
Avi Kivity9dac77f2011-06-01 15:34:25 +0300459static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800460{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300461 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800462}
463
Avi Kivityf47cfa32012-06-07 17:49:24 +0300464static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
465{
466 u16 sel;
467 struct desc_struct ss;
468
469 if (ctxt->mode == X86EMUL_MODE_PROT64)
470 return ~0UL;
471 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
472 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
473}
474
Avi Kivity612e89f2012-06-12 20:03:23 +0300475static int stack_size(struct x86_emulate_ctxt *ctxt)
476{
477 return (__fls(stack_mask(ctxt)) + 1) >> 3;
478}
479
Avi Kivity6aa8b732006-12-10 02:21:36 -0800480/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800481static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300482address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800483{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300484 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800485 return reg;
486 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300487 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800488}
489
490static inline unsigned long
Paolo Bonzini01485a22014-11-19 18:25:08 +0100491register_address(struct x86_emulate_ctxt *ctxt, int reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800492{
Paolo Bonzini01485a22014-11-19 18:25:08 +0100493 return address_mask(ctxt, reg_read(ctxt, reg));
Harvey Harrisone4706772008-02-19 07:40:38 -0800494}
495
Avi Kivity5ad105e2012-08-19 14:34:31 +0300496static void masked_increment(ulong *reg, ulong mask, int inc)
497{
498 assign_masked(reg, *reg + inc, mask);
499}
500
Harvey Harrison7a9572752008-02-19 07:40:41 -0800501static inline void
Paolo Bonzini01485a22014-11-19 18:25:08 +0100502register_address_increment(struct x86_emulate_ctxt *ctxt, int reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800503{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300504 ulong mask;
505
Avi Kivity9dac77f2011-06-01 15:34:25 +0300506 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300507 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800508 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300509 mask = ad_mask(ctxt);
Paolo Bonzini01485a22014-11-19 18:25:08 +0100510 masked_increment(reg_rmw(ctxt, reg), mask, inc);
Avi Kivity5ad105e2012-08-19 14:34:31 +0300511}
512
513static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
514{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300515 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800516}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800517
Avi Kivity56697682011-04-03 14:08:51 +0300518static u32 desc_limit_scaled(struct desc_struct *desc)
519{
520 u32 limit = get_desc_limit(desc);
521
522 return desc->g ? (limit << 12) | 0xfff : limit;
523}
524
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900525static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300526{
527 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
528 return 0;
529
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900530 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300531}
532
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200533static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
534 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300535{
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +0200536 WARN_ON(vec > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +0200537 ctxt->exception.vector = vec;
538 ctxt->exception.error_code = error;
539 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200540 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300541}
542
Joerg Roedel3b88e412011-04-04 12:39:29 +0200543static int emulate_db(struct x86_emulate_ctxt *ctxt)
544{
545 return emulate_exception(ctxt, DB_VECTOR, 0, false);
546}
547
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200548static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300549{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200550 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300551}
552
Avi Kivity618ff152011-04-03 12:32:09 +0300553static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
554{
555 return emulate_exception(ctxt, SS_VECTOR, err, true);
556}
557
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200558static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300559{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200560 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300561}
562
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200563static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300564{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200565 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300566}
567
Avi Kivity34d1f492010-08-26 11:59:01 +0300568static int emulate_de(struct x86_emulate_ctxt *ctxt)
569{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200570 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300571}
572
Avi Kivity12537912011-03-29 11:41:27 +0200573static int emulate_nm(struct x86_emulate_ctxt *ctxt)
574{
575 return emulate_exception(ctxt, NM_VECTOR, 0, false);
576}
577
Avi Kivity1aa36612011-04-27 13:20:30 +0300578static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
579{
580 u16 selector;
581 struct desc_struct desc;
582
583 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
584 return selector;
585}
586
587static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
588 unsigned seg)
589{
590 u16 dummy;
591 u32 base3;
592 struct desc_struct desc;
593
594 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
595 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
596}
597
Avi Kivity1c11b372012-04-09 18:39:59 +0300598/*
599 * x86 defines three classes of vector instructions: explicitly
600 * aligned, explicitly unaligned, and the rest, which change behaviour
601 * depending on whether they're AVX encoded or not.
602 *
603 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
604 * subject to the same check.
605 */
606static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
607{
608 if (likely(size < 16))
609 return false;
610
611 if (ctxt->d & Aligned)
612 return true;
613 else if (ctxt->d & Unaligned)
614 return false;
615 else if (ctxt->d & Avx)
616 return false;
617 else
618 return true;
619}
620
Paolo Bonzinid09155d2014-10-27 14:54:44 +0100621static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
622 struct segmented_address addr,
623 unsigned *max_size, unsigned size,
624 bool write, bool fetch,
Nadav Amitd50eaa12014-11-19 17:43:11 +0200625 enum x86emul_mode mode, ulong *linear)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300626{
Avi Kivity618ff152011-04-03 12:32:09 +0300627 struct desc_struct desc;
628 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300629 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300630 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300631 u16 sel;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300632
Nadav Amit1c1c35a2014-11-19 17:43:09 +0200633 la = seg_base(ctxt, addr.seg) + addr.ea;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100634 *max_size = 0;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200635 switch (mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300636 case X86EMUL_MODE_PROT64:
Nadav Amit4be4de72014-09-18 22:39:40 +0300637 if (is_noncanonical_address(la))
Nadav Amitabc7d8a2014-11-19 17:43:12 +0200638 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100639
640 *max_size = min_t(u64, ~0u, (1ull << 48) - la);
641 if (size > *max_size)
642 goto bad;
Avi Kivity618ff152011-04-03 12:32:09 +0300643 break;
644 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300645 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
646 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300647 if (!usable)
648 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200649 /* code segment in protected mode or read-only data segment */
650 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
651 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300652 goto bad;
653 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400654 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300655 goto bad;
656 lim = desc_limit_scaled(&desc);
Nadav Amit7d882ff2014-11-19 17:43:08 +0200657 if ((desc.type & 8) || !(desc.type & 4)) {
Avi Kivity618ff152011-04-03 12:32:09 +0300658 /* expand-up segment */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100659 if (addr.ea > lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300660 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100661 *max_size = min_t(u64, ~0u, (u64)lim + 1 - addr.ea);
Avi Kivity618ff152011-04-03 12:32:09 +0300662 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800663 /* expand-down segment */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100664 if (addr.ea <= lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300665 goto bad;
666 lim = desc.d ? 0xffffffff : 0xffff;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100667 if (addr.ea > lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300668 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100669 *max_size = min_t(u64, ~0u, (u64)lim + 1 - addr.ea);
Avi Kivity618ff152011-04-03 12:32:09 +0300670 }
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100671 if (size > *max_size)
672 goto bad;
Nadav Amit31ff6482014-11-19 17:43:13 +0200673 la &= (u32)-1;
Avi Kivity618ff152011-04-03 12:32:09 +0300674 break;
675 }
Avi Kivity1c11b372012-04-09 18:39:59 +0300676 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
677 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300678 *linear = la;
679 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300680bad:
681 if (addr.seg == VCPU_SREG_SS)
Paolo Bonzini36061892014-10-27 14:40:49 +0100682 return emulate_ss(ctxt, 0);
Avi Kivity618ff152011-04-03 12:32:09 +0300683 else
Paolo Bonzini36061892014-10-27 14:40:49 +0100684 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300685}
686
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400687static int linearize(struct x86_emulate_ctxt *ctxt,
688 struct segmented_address addr,
689 unsigned size, bool write,
690 ulong *linear)
691{
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100692 unsigned max_size;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200693 return __linearize(ctxt, addr, &max_size, size, write, false,
694 ctxt->mode, linear);
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400695}
696
Nadav Amitd50eaa12014-11-19 17:43:11 +0200697static inline int assign_eip(struct x86_emulate_ctxt *ctxt, ulong dst,
698 enum x86emul_mode mode)
699{
700 ulong linear;
701 int rc;
702 unsigned max_size;
703 struct segmented_address addr = { .seg = VCPU_SREG_CS,
704 .ea = dst };
705
706 if (ctxt->op_bytes != sizeof(unsigned long))
707 addr.ea = dst & ((1UL << (ctxt->op_bytes << 3)) - 1);
708 rc = __linearize(ctxt, addr, &max_size, 1, false, true, mode, &linear);
709 if (rc == X86EMUL_CONTINUE)
710 ctxt->_eip = addr.ea;
711 return rc;
712}
713
714static inline int assign_eip_near(struct x86_emulate_ctxt *ctxt, ulong dst)
715{
716 return assign_eip(ctxt, dst, ctxt->mode);
717}
718
719static int assign_eip_far(struct x86_emulate_ctxt *ctxt, ulong dst,
720 const struct desc_struct *cs_desc)
721{
722 enum x86emul_mode mode = ctxt->mode;
723
724#ifdef CONFIG_X86_64
725 if (ctxt->mode >= X86EMUL_MODE_PROT32 && cs_desc->l) {
726 u64 efer = 0;
727
728 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
729 if (efer & EFER_LMA)
730 mode = X86EMUL_MODE_PROT64;
731 }
732#endif
733 if (mode == X86EMUL_MODE_PROT16 || mode == X86EMUL_MODE_PROT32)
734 mode = cs_desc->d ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
735 return assign_eip(ctxt, dst, mode);
736}
737
738static inline int jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
739{
740 return assign_eip_near(ctxt, ctxt->_eip + rel);
741}
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400742
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200743static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
744 struct segmented_address addr,
745 void *data,
746 unsigned size)
747{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200748 int rc;
749 ulong linear;
750
Avi Kivity83b87952011-04-03 11:31:19 +0300751 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200752 if (rc != X86EMUL_CONTINUE)
753 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300754 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200755}
756
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900757/*
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200758 * Prefetch the remaining bytes of the instruction without crossing page
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900759 * boundary if they are not in fetch_cache yet.
760 */
Paolo Bonzini9506d572014-05-06 13:05:25 +0200761static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
Avi Kivity62266862007-11-20 13:15:52 +0200762{
Avi Kivity62266862007-11-20 13:15:52 +0200763 int rc;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100764 unsigned size, max_size;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200765 unsigned long linear;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200766 int cur_size = ctxt->fetch.end - ctxt->fetch.data;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200767 struct segmented_address addr = { .seg = VCPU_SREG_CS,
Paolo Bonzini17052f12014-05-06 16:33:01 +0200768 .ea = ctxt->eip + cur_size };
769
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100770 /*
771 * We do not know exactly how many bytes will be needed, and
772 * __linearize is expensive, so fetch as much as possible. We
773 * just have to avoid going beyond the 15 byte limit, the end
774 * of the segment, or the end of the page.
775 *
776 * __linearize is called with size 0 so that it does not do any
777 * boundary check itself. Instead, we use max_size to check
778 * against op_size.
779 */
Nadav Amitd50eaa12014-11-19 17:43:11 +0200780 rc = __linearize(ctxt, addr, &max_size, 0, false, true, ctxt->mode,
781 &linear);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200782 if (unlikely(rc != X86EMUL_CONTINUE))
783 return rc;
784
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100785 size = min_t(unsigned, 15UL ^ cur_size, max_size);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200786 size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear));
Paolo Bonzini5cfc7e02014-05-06 13:05:25 +0200787
788 /*
789 * One instruction can only straddle two pages,
790 * and one has been loaded at the beginning of
791 * x86_decode_insn. So, if not enough bytes
792 * still, we must have hit the 15-byte boundary.
793 */
794 if (unlikely(size < op_size))
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100795 return emulate_gp(ctxt, 0);
796
Paolo Bonzini17052f12014-05-06 16:33:01 +0200797 rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end,
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200798 size, &ctxt->exception);
799 if (unlikely(rc != X86EMUL_CONTINUE))
800 return rc;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200801 ctxt->fetch.end += size;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900802 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200803}
804
Paolo Bonzini9506d572014-05-06 13:05:25 +0200805static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt,
806 unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200807{
Nadav Amit08da44a2014-10-03 01:10:04 +0300808 unsigned done_size = ctxt->fetch.end - ctxt->fetch.ptr;
809
810 if (unlikely(done_size < size))
811 return __do_insn_fetch_bytes(ctxt, size - done_size);
Paolo Bonzini9506d572014-05-06 13:05:25 +0200812 else
813 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200814}
815
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900816/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900817#define insn_fetch(_type, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200818({ _type _x; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200819 \
820 rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900821 if (rc != X86EMUL_CONTINUE) \
822 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200823 ctxt->_eip += sizeof(_type); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200824 _x = *(_type __aligned(1) *) ctxt->fetch.ptr; \
825 ctxt->fetch.ptr += sizeof(_type); \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200826 _x; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900827})
828
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900829#define insn_fetch_arr(_arr, _size, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200830({ \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200831 rc = do_insn_fetch_bytes(_ctxt, _size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900832 if (rc != X86EMUL_CONTINUE) \
833 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200834 ctxt->_eip += (_size); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200835 memcpy(_arr, ctxt->fetch.ptr, _size); \
836 ctxt->fetch.ptr += (_size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900837})
838
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000839/*
840 * Given the 'reg' portion of a ModRM byte, and a register block, return a
841 * pointer into the block that addresses the relevant register.
842 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
843 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300844static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200845 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800846{
847 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200848 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800849
Avi Kivity6aa8b732006-12-10 02:21:36 -0800850 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300851 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
852 else
853 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800854 return p;
855}
856
857static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200858 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800859 u16 *size, unsigned long *address, int op_bytes)
860{
861 int rc;
862
863 if (op_bytes == 2)
864 op_bytes = 3;
865 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200866 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900867 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800868 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200869 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200870 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800871 return rc;
872}
873
Avi Kivity34b77652013-01-19 19:51:56 +0200874FASTOP2(add);
875FASTOP2(or);
876FASTOP2(adc);
877FASTOP2(sbb);
878FASTOP2(and);
879FASTOP2(sub);
880FASTOP2(xor);
881FASTOP2(cmp);
882FASTOP2(test);
883
Avi Kivityb9fa4092013-02-09 11:31:48 +0200884FASTOP1SRC2(mul, mul_ex);
885FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200886FASTOP1SRC2EX(div, div_ex);
887FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +0200888
Avi Kivity34b77652013-01-19 19:51:56 +0200889FASTOP3WCL(shld);
890FASTOP3WCL(shrd);
891
892FASTOP2W(imul);
893
894FASTOP1(not);
895FASTOP1(neg);
896FASTOP1(inc);
897FASTOP1(dec);
898
899FASTOP2CL(rol);
900FASTOP2CL(ror);
901FASTOP2CL(rcl);
902FASTOP2CL(rcr);
903FASTOP2CL(shl);
904FASTOP2CL(shr);
905FASTOP2CL(sar);
906
907FASTOP2W(bsf);
908FASTOP2W(bsr);
909FASTOP2W(bt);
910FASTOP2W(bts);
911FASTOP2W(btr);
912FASTOP2W(btc);
913
Avi Kivitye47a5f52013-02-09 11:31:51 +0200914FASTOP2(xadd);
915
Nadav Amit5aca3722014-11-02 11:54:50 +0200916FASTOP2R(cmp, cmp_r);
917
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200918static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300919{
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200920 u8 rc;
921 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300922
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200923 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +0200924 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200925 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
926 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300927}
928
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300929static void fetch_register_operand(struct operand *op)
930{
931 switch (op->bytes) {
932 case 1:
933 op->val = *(u8 *)op->addr.reg;
934 break;
935 case 2:
936 op->val = *(u16 *)op->addr.reg;
937 break;
938 case 4:
939 op->val = *(u32 *)op->addr.reg;
940 break;
941 case 8:
942 op->val = *(u64 *)op->addr.reg;
943 break;
944 }
945}
946
Avi Kivity12537912011-03-29 11:41:27 +0200947static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
948{
949 ctxt->ops->get_fpu(ctxt);
950 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200951 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
952 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
953 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
954 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
955 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
956 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
957 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
958 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200959#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200960 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
961 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
962 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
963 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
964 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
965 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
966 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
967 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200968#endif
969 default: BUG();
970 }
971 ctxt->ops->put_fpu(ctxt);
972}
973
974static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
975 int reg)
976{
977 ctxt->ops->get_fpu(ctxt);
978 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200979 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
980 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
981 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
982 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
983 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
984 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
985 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
986 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200987#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200988 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
989 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
990 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
991 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
992 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
993 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
994 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
995 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200996#endif
997 default: BUG();
998 }
999 ctxt->ops->put_fpu(ctxt);
1000}
1001
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001002static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1003{
1004 ctxt->ops->get_fpu(ctxt);
1005 switch (reg) {
1006 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1007 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1008 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1009 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1010 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1011 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1012 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1013 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1014 default: BUG();
1015 }
1016 ctxt->ops->put_fpu(ctxt);
1017}
1018
1019static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1020{
1021 ctxt->ops->get_fpu(ctxt);
1022 switch (reg) {
1023 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1024 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1025 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1026 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1027 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1028 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1029 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1030 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1031 default: BUG();
1032 }
1033 ctxt->ops->put_fpu(ctxt);
1034}
1035
Gleb Natapov045a2822012-12-20 16:57:43 +02001036static int em_fninit(struct x86_emulate_ctxt *ctxt)
1037{
1038 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1039 return emulate_nm(ctxt);
1040
1041 ctxt->ops->get_fpu(ctxt);
1042 asm volatile("fninit");
1043 ctxt->ops->put_fpu(ctxt);
1044 return X86EMUL_CONTINUE;
1045}
1046
1047static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1048{
1049 u16 fcw;
1050
1051 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1052 return emulate_nm(ctxt);
1053
1054 ctxt->ops->get_fpu(ctxt);
1055 asm volatile("fnstcw %0": "+m"(fcw));
1056 ctxt->ops->put_fpu(ctxt);
1057
1058 /* force 2 byte destination */
1059 ctxt->dst.bytes = 2;
1060 ctxt->dst.val = fcw;
1061
1062 return X86EMUL_CONTINUE;
1063}
1064
1065static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1066{
1067 u16 fsw;
1068
1069 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1070 return emulate_nm(ctxt);
1071
1072 ctxt->ops->get_fpu(ctxt);
1073 asm volatile("fnstsw %0": "+m"(fsw));
1074 ctxt->ops->put_fpu(ctxt);
1075
1076 /* force 2 byte destination */
1077 ctxt->dst.bytes = 2;
1078 ctxt->dst.val = fsw;
1079
1080 return X86EMUL_CONTINUE;
1081}
1082
Avi Kivity12537912011-03-29 11:41:27 +02001083static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001084 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001085{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001086 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001087
Avi Kivity9dac77f2011-06-01 15:34:25 +03001088 if (!(ctxt->d & ModRM))
1089 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +02001090
Avi Kivity9dac77f2011-06-01 15:34:25 +03001091 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001092 op->type = OP_XMM;
1093 op->bytes = 16;
1094 op->addr.xmm = reg;
1095 read_sse_reg(ctxt, &op->vec_val, reg);
1096 return;
1097 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001098 if (ctxt->d & Mmx) {
1099 reg &= 7;
1100 op->type = OP_MM;
1101 op->bytes = 8;
1102 op->addr.mm = reg;
1103 return;
1104 }
Avi Kivity12537912011-03-29 11:41:27 +02001105
Avi Kivity3c118e22007-10-31 10:27:04 +02001106 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001107 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1108 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1109
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001110 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001111 op->orig_val = op->val;
1112}
1113
Avi Kivitya6e34072012-06-10 17:15:39 +03001114static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1115{
1116 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1117 ctxt->modrm_seg = VCPU_SREG_SS;
1118}
1119
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001120static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001121 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001122{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001123 u8 sib;
Bandan Das02357bd2014-04-16 12:46:11 -04001124 int index_reg, base_reg, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001125 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001126 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001127
Bandan Das02357bd2014-04-16 12:46:11 -04001128 ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */
1129 index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */
1130 base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001131
Bandan Das02357bd2014-04-16 12:46:11 -04001132 ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001133 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
Bandan Das02357bd2014-04-16 12:46:11 -04001134 ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001135 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001136
Nadav Amit9b88ae92014-05-25 23:05:21 +03001137 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001138 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001139 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001140 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001141 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001142 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001143 op->type = OP_XMM;
1144 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001145 op->addr.xmm = ctxt->modrm_rm;
1146 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001147 return rc;
1148 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001149 if (ctxt->d & Mmx) {
1150 op->type = OP_MM;
1151 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001152 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001153 return rc;
1154 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001155 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001156 return rc;
1157 }
1158
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001159 op->type = OP_MEM;
1160
Avi Kivity9dac77f2011-06-01 15:34:25 +03001161 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001162 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1163 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1164 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1165 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001166
1167 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001168 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001169 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001170 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001171 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001172 break;
1173 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001174 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001175 break;
1176 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001177 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001178 break;
1179 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001180 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001181 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001182 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001183 break;
1184 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001185 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001186 break;
1187 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001188 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001189 break;
1190 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001191 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001192 break;
1193 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001194 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001195 break;
1196 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001197 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001198 break;
1199 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001200 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001201 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001202 break;
1203 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001204 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001205 break;
1206 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001207 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1208 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1209 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001210 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001211 } else {
1212 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001213 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001214 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001215 index_reg |= (sib >> 3) & 7;
1216 base_reg |= sib & 7;
1217 scale = sib >> 6;
1218
Avi Kivity9dac77f2011-06-01 15:34:25 +03001219 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001220 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001221 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001222 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001223 adjust_modrm_seg(ctxt, base_reg);
1224 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001225 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001226 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001227 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Nadav Amit5b38ab82014-11-02 11:54:41 +02001228 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity84411d82008-06-15 21:53:26 -07001229 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001230 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001231 } else {
1232 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001233 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001234 adjust_modrm_seg(ctxt, base_reg);
1235 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001236 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001237 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001238 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001239 break;
1240 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001241 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001242 break;
1243 }
1244 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001245 op->addr.mem.ea = modrm_ea;
Bandan Das41061cd2014-04-16 12:46:14 -04001246 if (ctxt->ad_bytes != 8)
1247 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
1248
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001249done:
1250 return rc;
1251}
1252
1253static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001254 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001255{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001256 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001257
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001258 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001259 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001260 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001261 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001262 break;
1263 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001264 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001265 break;
1266 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001267 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001268 break;
1269 }
1270done:
1271 return rc;
1272}
1273
Avi Kivity9dac77f2011-06-01 15:34:25 +03001274static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001275{
Sheng Yang7129eec2010-09-28 16:33:32 +08001276 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001277
Avi Kivity9dac77f2011-06-01 15:34:25 +03001278 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001279 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001280
Avi Kivity9dac77f2011-06-01 15:34:25 +03001281 if (ctxt->src.bytes == 2)
1282 sv = (s16)ctxt->src.val & (s16)mask;
1283 else if (ctxt->src.bytes == 4)
1284 sv = (s32)ctxt->src.val & (s32)mask;
Nadav Amit7dec5602014-06-15 16:12:57 +03001285 else
1286 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001287
Nadav Amit1c1c35a2014-11-19 17:43:09 +02001288 ctxt->dst.addr.mem.ea = address_mask(ctxt,
1289 ctxt->dst.addr.mem.ea + (sv >> 3));
Wei Yongjun35c843c2010-08-09 11:34:56 +08001290 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001291
1292 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001293 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001294}
1295
Gleb Natapov9de41572010-04-28 19:15:22 +03001296static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001297 unsigned long addr, void *dest, unsigned size)
1298{
1299 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001300 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001301
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001302 if (mc->pos < mc->end)
1303 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001304
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001305 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001306
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001307 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1308 &ctxt->exception);
1309 if (rc != X86EMUL_CONTINUE)
1310 return rc;
1311
1312 mc->end += size;
1313
1314read_cached:
1315 memcpy(dest, mc->data + mc->pos, size);
1316 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001317 return X86EMUL_CONTINUE;
1318}
1319
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001320static int segmented_read(struct x86_emulate_ctxt *ctxt,
1321 struct segmented_address addr,
1322 void *data,
1323 unsigned size)
1324{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001325 int rc;
1326 ulong linear;
1327
Avi Kivity83b87952011-04-03 11:31:19 +03001328 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001329 if (rc != X86EMUL_CONTINUE)
1330 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001331 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001332}
1333
1334static int segmented_write(struct x86_emulate_ctxt *ctxt,
1335 struct segmented_address addr,
1336 const void *data,
1337 unsigned size)
1338{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001339 int rc;
1340 ulong linear;
1341
Avi Kivity83b87952011-04-03 11:31:19 +03001342 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001343 if (rc != X86EMUL_CONTINUE)
1344 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001345 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1346 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001347}
1348
1349static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1350 struct segmented_address addr,
1351 const void *orig_data, const void *data,
1352 unsigned size)
1353{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001354 int rc;
1355 ulong linear;
1356
Avi Kivity83b87952011-04-03 11:31:19 +03001357 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001358 if (rc != X86EMUL_CONTINUE)
1359 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001360 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1361 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001362}
1363
Gleb Natapov7b262e92010-03-18 15:20:27 +02001364static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001365 unsigned int size, unsigned short port,
1366 void *dest)
1367{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001368 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001369
1370 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001371 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001372 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001373 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001374 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001375 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1376 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Mark Rustadb55a8142014-07-25 06:27:05 -07001377 n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001378 if (n == 0)
1379 n = 1;
1380 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001381 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001382 return 0;
1383 rc->end = n * size;
1384 }
1385
Nadav Amite6e39f02014-04-18 03:35:10 +03001386 if (ctxt->rep_prefix && (ctxt->d & String) &&
1387 !(ctxt->eflags & EFLG_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001388 ctxt->dst.data = rc->data + rc->pos;
1389 ctxt->dst.type = OP_MEM_STR;
1390 ctxt->dst.count = (rc->end - rc->pos) / size;
1391 rc->pos = rc->end;
1392 } else {
1393 memcpy(dest, rc->data + rc->pos, size);
1394 rc->pos += size;
1395 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001396 return 1;
1397}
1398
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001399static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1400 u16 index, struct desc_struct *desc)
1401{
1402 struct desc_ptr dt;
1403 ulong addr;
1404
1405 ctxt->ops->get_idt(ctxt, &dt);
1406
1407 if (dt.size < index * 8 + 7)
1408 return emulate_gp(ctxt, index << 3 | 0x2);
1409
1410 addr = dt.address + index * 8;
1411 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1412 &ctxt->exception);
1413}
1414
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001415static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001416 u16 selector, struct desc_ptr *dt)
1417{
Mathias Krause0225fb52012-08-30 01:30:16 +02001418 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001419 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001420
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001421 if (selector & 1 << 2) {
1422 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001423 u16 sel;
1424
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001425 memset (dt, 0, sizeof *dt);
Nadav Amit2eedcac2014-06-02 18:34:05 +03001426 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1427 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001428 return;
1429
1430 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001431 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001432 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001433 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001434}
1435
1436/* allowed just for 8 bytes segments */
1437static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001438 u16 selector, struct desc_struct *desc,
1439 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001440{
1441 struct desc_ptr dt;
1442 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001443 ulong addr;
1444
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001445 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001446
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001447 if (dt.size < index * 8 + 7)
1448 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001449
Avi Kivitye9194642012-06-13 16:29:39 +03001450 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001451 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1452 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001453}
1454
1455/* allowed just for 8 bytes segments */
1456static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001457 u16 selector, struct desc_struct *desc)
1458{
1459 struct desc_ptr dt;
1460 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001461 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001462
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001463 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001464
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001465 if (dt.size < index * 8 + 7)
1466 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001467
1468 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001469 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1470 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001471}
1472
Gleb Natapov5601d052011-03-07 14:55:06 +02001473/* Does not support long mode */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001474static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Nadav Amitd1442d82014-09-18 22:39:39 +03001475 u16 selector, int seg, u8 cpl,
1476 bool in_task_switch,
1477 struct desc_struct *desc)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001478{
Avi Kivity869be992012-06-13 16:30:53 +03001479 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001480 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001481 unsigned err_vec = GP_VECTOR;
1482 u32 err_code = 0;
1483 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001484 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001485 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001486 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001487 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001488
1489 memset(&seg_desc, 0, sizeof seg_desc);
1490
Kevin Wolff8da94e2013-04-11 14:06:03 +02001491 if (ctxt->mode == X86EMUL_MODE_REAL) {
1492 /* set real mode segment descriptor (keep limit etc. for
1493 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001494 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001495 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001496 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001497 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1498 /* VM86 needs a clean new segment descriptor */
1499 set_desc_base(&seg_desc, selector << 4);
1500 set_desc_limit(&seg_desc, 0xffff);
1501 seg_desc.type = 3;
1502 seg_desc.p = 1;
1503 seg_desc.s = 1;
1504 seg_desc.dpl = 3;
1505 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001506 }
1507
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001508 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001509
1510 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1511 if ((seg == VCPU_SREG_CS
1512 || (seg == VCPU_SREG_SS
1513 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1514 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001515 && null_selector)
1516 goto exception;
1517
1518 /* TR should be in GDT only */
1519 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1520 goto exception;
1521
1522 if (null_selector) /* for NULL selector skip all following checks */
1523 goto load;
1524
Avi Kivitye9194642012-06-13 16:29:39 +03001525 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001526 if (ret != X86EMUL_CONTINUE)
1527 return ret;
1528
1529 err_code = selector & 0xfffc;
Paolo Bonzini15fc0752014-08-18 13:17:00 +02001530 err_vec = in_task_switch ? TS_VECTOR : GP_VECTOR;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001531
Guo Chaofc058682012-06-28 15:19:51 +08001532 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001533 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1534 goto exception;
1535
1536 if (!seg_desc.p) {
1537 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1538 goto exception;
1539 }
1540
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001541 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001542
1543 switch (seg) {
1544 case VCPU_SREG_SS:
1545 /*
1546 * segment is not a writable data segment or segment
1547 * selector's RPL != CPL or segment selector's RPL != CPL
1548 */
1549 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1550 goto exception;
1551 break;
1552 case VCPU_SREG_CS:
1553 if (!(seg_desc.type & 8))
1554 goto exception;
1555
1556 if (seg_desc.type & 4) {
1557 /* conforming */
1558 if (dpl > cpl)
1559 goto exception;
1560 } else {
1561 /* nonconforming */
1562 if (rpl > cpl || dpl != cpl)
1563 goto exception;
1564 }
Nadav Amit040c8dc2014-09-18 22:39:43 +03001565 /* in long-mode d/b must be clear if l is set */
1566 if (seg_desc.d && seg_desc.l) {
1567 u64 efer = 0;
1568
1569 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1570 if (efer & EFER_LMA)
1571 goto exception;
1572 }
1573
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001574 /* CS(RPL) <- CPL */
1575 selector = (selector & 0xfffc) | cpl;
1576 break;
1577 case VCPU_SREG_TR:
1578 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1579 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001580 old_desc = seg_desc;
1581 seg_desc.type |= 2; /* busy */
1582 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1583 sizeof(seg_desc), &ctxt->exception);
1584 if (ret != X86EMUL_CONTINUE)
1585 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001586 break;
1587 case VCPU_SREG_LDTR:
1588 if (seg_desc.s || seg_desc.type != 2)
1589 goto exception;
1590 break;
1591 default: /* DS, ES, FS, or GS */
1592 /*
1593 * segment is not a data or readable code segment or
1594 * ((segment is a data or nonconforming code segment)
1595 * and (both RPL and CPL > DPL))
1596 */
1597 if ((seg_desc.type & 0xa) == 0x8 ||
1598 (((seg_desc.type & 0xc) != 0xc) &&
1599 (rpl > dpl && cpl > dpl)))
1600 goto exception;
1601 break;
1602 }
1603
1604 if (seg_desc.s) {
1605 /* mark segment as accessed */
1606 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001607 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001608 if (ret != X86EMUL_CONTINUE)
1609 return ret;
Nadav Amite37a75a2014-06-02 18:34:04 +03001610 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
1611 ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3,
1612 sizeof(base3), &ctxt->exception);
1613 if (ret != X86EMUL_CONTINUE)
1614 return ret;
Nadav Amit9a9abf62014-11-02 11:54:56 +02001615 if (is_noncanonical_address(get_desc_base(&seg_desc) |
1616 ((u64)base3 << 32)))
1617 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001618 }
1619load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001620 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Nadav Amitd1442d82014-09-18 22:39:39 +03001621 if (desc)
1622 *desc = seg_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001623 return X86EMUL_CONTINUE;
1624exception:
Paolo Bonzini592f0852014-08-20 10:05:08 +02001625 return emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001626}
1627
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001628static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1629 u16 selector, int seg)
1630{
1631 u8 cpl = ctxt->ops->cpl(ctxt);
Nadav Amitd1442d82014-09-18 22:39:39 +03001632 return __load_segment_descriptor(ctxt, selector, seg, cpl, false, NULL);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001633}
1634
Wei Yongjun31be40b2010-08-17 09:17:30 +08001635static void write_register_operand(struct operand *op)
1636{
1637 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1638 switch (op->bytes) {
1639 case 1:
1640 *(u8 *)op->addr.reg = (u8)op->val;
1641 break;
1642 case 2:
1643 *(u16 *)op->addr.reg = (u16)op->val;
1644 break;
1645 case 4:
1646 *op->addr.reg = (u32)op->val;
1647 break; /* 64b: zero-extend */
1648 case 8:
1649 *op->addr.reg = op->val;
1650 break;
1651 }
1652}
1653
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001654static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001655{
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001656 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001657 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001658 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001659 break;
1660 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001661 if (ctxt->lock_prefix)
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001662 return segmented_cmpxchg(ctxt,
1663 op->addr.mem,
1664 &op->orig_val,
1665 &op->val,
1666 op->bytes);
1667 else
1668 return segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001669 op->addr.mem,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001670 &op->val,
1671 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001672 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001673 case OP_MEM_STR:
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001674 return segmented_write(ctxt,
1675 op->addr.mem,
1676 op->data,
1677 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001678 break;
Avi Kivity12537912011-03-29 11:41:27 +02001679 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001680 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001681 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001682 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001683 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001684 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001685 case OP_NONE:
1686 /* no writeback */
1687 break;
1688 default:
1689 break;
1690 }
1691 return X86EMUL_CONTINUE;
1692}
1693
Avi Kivity51ddff52012-06-12 20:19:40 +03001694static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001695{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001696 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001697
Avi Kivity5ad105e2012-08-19 14:34:31 +03001698 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001699 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001700 addr.seg = VCPU_SREG_SS;
1701
Avi Kivity51ddff52012-06-12 20:19:40 +03001702 return segmented_write(ctxt, addr, data, bytes);
1703}
1704
1705static int em_push(struct x86_emulate_ctxt *ctxt)
1706{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001707 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001708 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001709 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001710}
1711
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001712static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001713 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001714{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001715 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001716 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001717
Avi Kivitydd856ef2012-08-27 23:46:17 +03001718 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001719 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001720 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001721 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001722 return rc;
1723
Avi Kivity5ad105e2012-08-19 14:34:31 +03001724 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001725 return rc;
1726}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001727
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001728static int em_pop(struct x86_emulate_ctxt *ctxt)
1729{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001730 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001731}
1732
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001733static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001734 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001735{
1736 int rc;
1737 unsigned long val, change_mask;
1738 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001739 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001740
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001741 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001742 if (rc != X86EMUL_CONTINUE)
1743 return rc;
1744
1745 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
Nadav Amit163b1352014-07-21 14:37:28 +03001746 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_AC | EFLG_ID;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001747
1748 switch(ctxt->mode) {
1749 case X86EMUL_MODE_PROT64:
1750 case X86EMUL_MODE_PROT32:
1751 case X86EMUL_MODE_PROT16:
1752 if (cpl == 0)
1753 change_mask |= EFLG_IOPL;
1754 if (cpl <= iopl)
1755 change_mask |= EFLG_IF;
1756 break;
1757 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001758 if (iopl < 3)
1759 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001760 change_mask |= EFLG_IF;
1761 break;
1762 default: /* real mode */
1763 change_mask |= (EFLG_IOPL | EFLG_IF);
1764 break;
1765 }
1766
1767 *(unsigned long *)dest =
1768 (ctxt->eflags & ~change_mask) | (val & change_mask);
1769
1770 return rc;
1771}
1772
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001773static int em_popf(struct x86_emulate_ctxt *ctxt)
1774{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001775 ctxt->dst.type = OP_REG;
1776 ctxt->dst.addr.reg = &ctxt->eflags;
1777 ctxt->dst.bytes = ctxt->op_bytes;
1778 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001779}
1780
Avi Kivity612e89f2012-06-12 20:03:23 +03001781static int em_enter(struct x86_emulate_ctxt *ctxt)
1782{
1783 int rc;
1784 unsigned frame_size = ctxt->src.val;
1785 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001786 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001787
1788 if (nesting_level)
1789 return X86EMUL_UNHANDLEABLE;
1790
Avi Kivitydd856ef2012-08-27 23:46:17 +03001791 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1792 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001793 if (rc != X86EMUL_CONTINUE)
1794 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001795 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001796 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001797 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1798 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001799 stack_mask(ctxt));
1800 return X86EMUL_CONTINUE;
1801}
1802
Avi Kivityf47cfa32012-06-07 17:49:24 +03001803static int em_leave(struct x86_emulate_ctxt *ctxt)
1804{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001805 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001806 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001807 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001808}
1809
Avi Kivity1cd196e2011-09-13 10:45:51 +03001810static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001811{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001812 int seg = ctxt->src2.val;
1813
Avi Kivity9dac77f2011-06-01 15:34:25 +03001814 ctxt->src.val = get_segment_selector(ctxt, seg);
Nadav Amit0fcc2072014-11-02 11:54:51 +02001815 if (ctxt->op_bytes == 4) {
1816 rsp_increment(ctxt, -2);
1817 ctxt->op_bytes = 2;
1818 }
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001819
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001820 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001821}
1822
Avi Kivity1cd196e2011-09-13 10:45:51 +03001823static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001824{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001825 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001826 unsigned long selector;
1827 int rc;
1828
Avi Kivity9dac77f2011-06-01 15:34:25 +03001829 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001830 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001831 return rc;
1832
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001833 if (ctxt->modrm_reg == VCPU_SREG_SS)
1834 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
1835
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001836 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001837 return rc;
1838}
1839
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001840static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001841{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001842 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001843 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001844 int reg = VCPU_REGS_RAX;
1845
1846 while (reg <= VCPU_REGS_RDI) {
1847 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001848 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001849
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001850 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001851 if (rc != X86EMUL_CONTINUE)
1852 return rc;
1853
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001854 ++reg;
1855 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001856
Wei Yongjunc37eda12010-06-15 09:03:33 +08001857 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001858}
1859
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001860static int em_pushf(struct x86_emulate_ctxt *ctxt)
1861{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001862 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001863 return em_push(ctxt);
1864}
1865
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001866static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001867{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001868 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001869 int reg = VCPU_REGS_RDI;
1870
1871 while (reg >= VCPU_REGS_RAX) {
1872 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001873 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001874 --reg;
1875 }
1876
Avi Kivitydd856ef2012-08-27 23:46:17 +03001877 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001878 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001879 break;
1880 --reg;
1881 }
1882 return rc;
1883}
1884
Avi Kivitydd856ef2012-08-27 23:46:17 +03001885static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001886{
Mathias Krause0225fb52012-08-30 01:30:16 +02001887 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001888 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001889 struct desc_ptr dt;
1890 gva_t cs_addr;
1891 gva_t eip_addr;
1892 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001893
1894 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001895 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001896 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001897 if (rc != X86EMUL_CONTINUE)
1898 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001899
1900 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1901
Avi Kivity9dac77f2011-06-01 15:34:25 +03001902 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001903 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001904 if (rc != X86EMUL_CONTINUE)
1905 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001906
Avi Kivity9dac77f2011-06-01 15:34:25 +03001907 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001908 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001909 if (rc != X86EMUL_CONTINUE)
1910 return rc;
1911
Avi Kivity4bff1e862011-04-20 13:37:53 +03001912 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001913
1914 eip_addr = dt.address + (irq << 2);
1915 cs_addr = dt.address + (irq << 2) + 2;
1916
Avi Kivity0f65dd72011-04-20 13:37:53 +03001917 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001918 if (rc != X86EMUL_CONTINUE)
1919 return rc;
1920
Avi Kivity0f65dd72011-04-20 13:37:53 +03001921 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001922 if (rc != X86EMUL_CONTINUE)
1923 return rc;
1924
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001925 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001926 if (rc != X86EMUL_CONTINUE)
1927 return rc;
1928
Avi Kivity9dac77f2011-06-01 15:34:25 +03001929 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001930
1931 return rc;
1932}
1933
Avi Kivitydd856ef2012-08-27 23:46:17 +03001934int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1935{
1936 int rc;
1937
1938 invalidate_registers(ctxt);
1939 rc = __emulate_int_real(ctxt, irq);
1940 if (rc == X86EMUL_CONTINUE)
1941 writeback_registers(ctxt);
1942 return rc;
1943}
1944
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001945static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001946{
1947 switch(ctxt->mode) {
1948 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001949 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001950 case X86EMUL_MODE_VM86:
1951 case X86EMUL_MODE_PROT16:
1952 case X86EMUL_MODE_PROT32:
1953 case X86EMUL_MODE_PROT64:
1954 default:
1955 /* Protected mode interrupts unimplemented yet */
1956 return X86EMUL_UNHANDLEABLE;
1957 }
1958}
1959
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001960static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001961{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001962 int rc = X86EMUL_CONTINUE;
1963 unsigned long temp_eip = 0;
1964 unsigned long temp_eflags = 0;
1965 unsigned long cs = 0;
1966 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1967 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1968 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1969 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1970
1971 /* TODO: Add stack limit check */
1972
Avi Kivity9dac77f2011-06-01 15:34:25 +03001973 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001974
1975 if (rc != X86EMUL_CONTINUE)
1976 return rc;
1977
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001978 if (temp_eip & ~0xffff)
1979 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001980
Avi Kivity9dac77f2011-06-01 15:34:25 +03001981 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001982
1983 if (rc != X86EMUL_CONTINUE)
1984 return rc;
1985
Avi Kivity9dac77f2011-06-01 15:34:25 +03001986 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001987
1988 if (rc != X86EMUL_CONTINUE)
1989 return rc;
1990
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001991 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001992
1993 if (rc != X86EMUL_CONTINUE)
1994 return rc;
1995
Avi Kivity9dac77f2011-06-01 15:34:25 +03001996 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001997
1998
Avi Kivity9dac77f2011-06-01 15:34:25 +03001999 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002000 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03002001 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002002 ctxt->eflags &= ~0xffff;
2003 ctxt->eflags |= temp_eflags;
2004 }
2005
2006 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
2007 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
2008
2009 return rc;
2010}
2011
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002012static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002013{
2014 switch(ctxt->mode) {
2015 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002016 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002017 case X86EMUL_MODE_VM86:
2018 case X86EMUL_MODE_PROT16:
2019 case X86EMUL_MODE_PROT32:
2020 case X86EMUL_MODE_PROT64:
2021 default:
2022 /* iret from protected mode unimplemented yet */
2023 return X86EMUL_UNHANDLEABLE;
2024 }
2025}
2026
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002027static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2028{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002029 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002030 unsigned short sel, old_sel;
2031 struct desc_struct old_desc, new_desc;
2032 const struct x86_emulate_ops *ops = ctxt->ops;
2033 u8 cpl = ctxt->ops->cpl(ctxt);
2034
2035 /* Assignment of RIP may only fail in 64-bit mode */
2036 if (ctxt->mode == X86EMUL_MODE_PROT64)
2037 ops->get_segment(ctxt, &old_sel, &old_desc, NULL,
2038 VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002039
Avi Kivity9dac77f2011-06-01 15:34:25 +03002040 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002041
Nadav Amitd1442d82014-09-18 22:39:39 +03002042 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl, false,
2043 &new_desc);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002044 if (rc != X86EMUL_CONTINUE)
2045 return rc;
2046
Nadav Amitd50eaa12014-11-19 17:43:11 +02002047 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03002048 if (rc != X86EMUL_CONTINUE) {
Nadav Amitcd9b8e2c2014-10-28 00:03:43 +02002049 WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64);
Nadav Amitd1442d82014-09-18 22:39:39 +03002050 /* assigning eip failed; restore the old cs */
2051 ops->set_segment(ctxt, old_sel, &old_desc, 0, VCPU_SREG_CS);
2052 return rc;
2053 }
2054 return rc;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002055}
2056
Nadav Amitf7784042014-09-18 22:39:41 +03002057static int em_jmp_abs(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002058{
Nadav Amitf7784042014-09-18 22:39:41 +03002059 return assign_eip_near(ctxt, ctxt->src.val);
2060}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002061
Nadav Amitf7784042014-09-18 22:39:41 +03002062static int em_call_near_abs(struct x86_emulate_ctxt *ctxt)
2063{
2064 int rc;
2065 long int old_eip;
2066
2067 old_eip = ctxt->_eip;
2068 rc = assign_eip_near(ctxt, ctxt->src.val);
2069 if (rc != X86EMUL_CONTINUE)
2070 return rc;
2071 ctxt->src.val = old_eip;
2072 rc = em_push(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002073 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002074}
2075
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002076static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002077{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002078 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002079
Nadav Amitaaa05f22014-06-02 18:34:10 +03002080 if (ctxt->dst.bytes == 16)
2081 return X86EMUL_UNHANDLEABLE;
2082
Avi Kivitydd856ef2012-08-27 23:46:17 +03002083 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2084 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2085 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2086 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002087 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002088 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002089 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2090 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002091
Laurent Vivier05f086f2007-09-24 11:10:55 +02002092 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002093 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002094 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002095}
2096
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002097static int em_ret(struct x86_emulate_ctxt *ctxt)
2098{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002099 int rc;
2100 unsigned long eip;
2101
2102 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
2103 if (rc != X86EMUL_CONTINUE)
2104 return rc;
2105
2106 return assign_eip_near(ctxt, eip);
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002107}
2108
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002109static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002110{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002111 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002112 unsigned long eip, cs;
2113 u16 old_cs;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002114 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amitd1442d82014-09-18 22:39:39 +03002115 struct desc_struct old_desc, new_desc;
2116 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002117
Nadav Amitd1442d82014-09-18 22:39:39 +03002118 if (ctxt->mode == X86EMUL_MODE_PROT64)
2119 ops->get_segment(ctxt, &old_cs, &old_desc, NULL,
2120 VCPU_SREG_CS);
2121
2122 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002123 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002124 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002125 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002126 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002127 return rc;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002128 /* Outer-privilege level return is not implemented */
2129 if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2130 return X86EMUL_UNHANDLEABLE;
Nadav Amitd1442d82014-09-18 22:39:39 +03002131 rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, 0, false,
2132 &new_desc);
2133 if (rc != X86EMUL_CONTINUE)
2134 return rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +02002135 rc = assign_eip_far(ctxt, eip, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03002136 if (rc != X86EMUL_CONTINUE) {
Nadav Amitcd9b8e2c2014-10-28 00:03:43 +02002137 WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64);
Nadav Amitd1442d82014-09-18 22:39:39 +03002138 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
2139 }
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002140 return rc;
2141}
2142
Bruce Rogers32611072013-09-09 09:40:20 -06002143static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2144{
2145 int rc;
2146
2147 rc = em_ret_far(ctxt);
2148 if (rc != X86EMUL_CONTINUE)
2149 return rc;
2150 rsp_increment(ctxt, ctxt->src.val);
2151 return X86EMUL_CONTINUE;
2152}
2153
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002154static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2155{
2156 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002157 ctxt->dst.orig_val = ctxt->dst.val;
2158 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002159 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002160 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002161 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002162
2163 if (ctxt->eflags & EFLG_ZF) {
2164 /* Success: write back to memory. */
2165 ctxt->dst.val = ctxt->src.orig_val;
2166 } else {
2167 /* Failure: write the value we saw to EAX. */
2168 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002169 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Nadav Amit37c564f2014-06-02 18:34:07 +03002170 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002171 }
2172 return X86EMUL_CONTINUE;
2173}
2174
Avi Kivityd4b43252011-09-13 10:45:50 +03002175static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002176{
Avi Kivityd4b43252011-09-13 10:45:50 +03002177 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002178 unsigned short sel;
2179 int rc;
2180
Avi Kivity9dac77f2011-06-01 15:34:25 +03002181 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002182
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002183 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002184 if (rc != X86EMUL_CONTINUE)
2185 return rc;
2186
Avi Kivity9dac77f2011-06-01 15:34:25 +03002187 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002188 return rc;
2189}
2190
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002191static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002192setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002193 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002194{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002195 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002196 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002197 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002198 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002199 cs->type = 0x0b; /* Read, Execute, Accessed */
2200 cs->s = 1;
2201 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002202 cs->p = 1;
2203 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002204 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002205
Gleb Natapov79168fd2010-04-28 19:15:30 +03002206 set_desc_base(ss, 0); /* flat segment */
2207 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002208 ss->g = 1; /* 4kb granularity */
2209 ss->s = 1;
2210 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002211 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002212 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002213 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002214 ss->l = 0;
2215 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002216}
2217
Avi Kivity1a18a692012-02-01 12:23:21 +02002218static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2219{
2220 u32 eax, ebx, ecx, edx;
2221
2222 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002223 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2224 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002225 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2226 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2227}
2228
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002229static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2230{
Mathias Krause0225fb52012-08-30 01:30:16 +02002231 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002232 u32 eax, ebx, ecx, edx;
2233
2234 /*
2235 * syscall should always be enabled in longmode - so only become
2236 * vendor specific (cpuid) if other modes are active...
2237 */
2238 if (ctxt->mode == X86EMUL_MODE_PROT64)
2239 return true;
2240
2241 eax = 0x00000000;
2242 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002243 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2244 /*
2245 * Intel ("GenuineIntel")
2246 * remark: Intel CPUs only support "syscall" in 64bit
2247 * longmode. Also an 64bit guest with a
2248 * 32bit compat-app running will #UD !! While this
2249 * behaviour can be fixed (by emulating) into AMD
2250 * response - CPUs of AMD can't behave like Intel.
2251 */
2252 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2253 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2254 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2255 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002256
Avi Kivity0017f932012-06-07 14:10:16 +03002257 /* AMD ("AuthenticAMD") */
2258 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2259 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2260 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2261 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002262
Avi Kivity0017f932012-06-07 14:10:16 +03002263 /* AMD ("AMDisbetter!") */
2264 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2265 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2266 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2267 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002268
2269 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2270 return false;
2271}
2272
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002273static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002274{
Mathias Krause0225fb52012-08-30 01:30:16 +02002275 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002276 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002277 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002278 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002279 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002280
2281 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002282 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002283 ctxt->mode == X86EMUL_MODE_VM86)
2284 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002285
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002286 if (!(em_syscall_is_enabled(ctxt)))
2287 return emulate_ud(ctxt);
2288
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002289 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002290 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002291
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002292 if (!(efer & EFER_SCE))
2293 return emulate_ud(ctxt);
2294
Avi Kivity717746e2011-04-20 13:37:53 +03002295 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002296 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002297 cs_sel = (u16)(msr_data & 0xfffc);
2298 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002299
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002300 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002301 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002302 cs.l = 1;
2303 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002304 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2305 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002306
Avi Kivitydd856ef2012-08-27 23:46:17 +03002307 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002308 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002309#ifdef CONFIG_X86_64
Nadav Amit6c6cb692014-07-21 14:37:30 +03002310 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002311
Avi Kivity717746e2011-04-20 13:37:53 +03002312 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002313 ctxt->mode == X86EMUL_MODE_PROT64 ?
2314 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002315 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002316
Avi Kivity717746e2011-04-20 13:37:53 +03002317 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Nadav Amit6c6cb692014-07-21 14:37:30 +03002318 ctxt->eflags &= ~msr_data;
Nadav Amit807c1422014-11-02 11:54:49 +02002319 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002320#endif
2321 } else {
2322 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002323 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002324 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002325
Nadav Amit6c6cb692014-07-21 14:37:30 +03002326 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002327 }
2328
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002329 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002330}
2331
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002332static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002333{
Mathias Krause0225fb52012-08-30 01:30:16 +02002334 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002335 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002336 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002337 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002338 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002339
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002340 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002341 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002342 if (ctxt->mode == X86EMUL_MODE_REAL)
2343 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002344
Avi Kivity1a18a692012-02-01 12:23:21 +02002345 /*
2346 * Not recognized on AMD in compat mode (but is recognized in legacy
2347 * mode).
2348 */
2349 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2350 && !vendor_intel(ctxt))
2351 return emulate_ud(ctxt);
2352
Nadav Amitb2c9d432014-11-02 11:55:01 +02002353 /* sysenter/sysexit have not been tested in 64bit mode. */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002354 if (ctxt->mode == X86EMUL_MODE_PROT64)
Nadav Amitb2c9d432014-11-02 11:55:01 +02002355 return X86EMUL_UNHANDLEABLE;
Andre Przywara8c604352009-06-18 12:56:01 +02002356
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002357 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002358
Avi Kivity717746e2011-04-20 13:37:53 +03002359 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002360 switch (ctxt->mode) {
2361 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002362 if ((msr_data & 0xfffc) == 0x0)
2363 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002364 break;
2365 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002366 if (msr_data == 0x0)
2367 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002368 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002369 default:
2370 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002371 }
2372
Nadav Amit6c6cb692014-07-21 14:37:30 +03002373 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002374 cs_sel = (u16)msr_data;
2375 cs_sel &= ~SELECTOR_RPL_MASK;
2376 ss_sel = cs_sel + 8;
2377 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002378 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002379 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002380 cs.l = 1;
2381 }
2382
Avi Kivity1aa36612011-04-27 13:20:30 +03002383 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2384 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002385
Avi Kivity717746e2011-04-20 13:37:53 +03002386 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002387 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002388
Avi Kivity717746e2011-04-20 13:37:53 +03002389 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002390 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002391
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002392 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002393}
2394
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002395static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002396{
Mathias Krause0225fb52012-08-30 01:30:16 +02002397 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002398 struct desc_struct cs, ss;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002399 u64 msr_data, rcx, rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002400 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002401 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002402
Gleb Natapova0044752010-02-10 14:21:31 +02002403 /* inject #GP if in real mode or Virtual 8086 mode */
2404 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002405 ctxt->mode == X86EMUL_MODE_VM86)
2406 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002407
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002408 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002409
Avi Kivity9dac77f2011-06-01 15:34:25 +03002410 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002411 usermode = X86EMUL_MODE_PROT64;
2412 else
2413 usermode = X86EMUL_MODE_PROT32;
2414
Nadav Amit234f3ce2014-09-18 22:39:38 +03002415 rcx = reg_read(ctxt, VCPU_REGS_RCX);
2416 rdx = reg_read(ctxt, VCPU_REGS_RDX);
2417
Andre Przywara4668f052009-06-18 12:56:02 +02002418 cs.dpl = 3;
2419 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002420 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002421 switch (usermode) {
2422 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002423 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002424 if ((msr_data & 0xfffc) == 0x0)
2425 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002426 ss_sel = (u16)(msr_data + 24);
Nadav Amitbf0b6822014-09-18 22:39:45 +03002427 rcx = (u32)rcx;
2428 rdx = (u32)rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002429 break;
2430 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002431 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002432 if (msr_data == 0x0)
2433 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002434 ss_sel = cs_sel + 8;
2435 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002436 cs.l = 1;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002437 if (is_noncanonical_address(rcx) ||
2438 is_noncanonical_address(rdx))
2439 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002440 break;
2441 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002442 cs_sel |= SELECTOR_RPL_MASK;
2443 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002444
Avi Kivity1aa36612011-04-27 13:20:30 +03002445 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2446 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002447
Nadav Amit234f3ce2014-09-18 22:39:38 +03002448 ctxt->_eip = rdx;
2449 *reg_write(ctxt, VCPU_REGS_RSP) = rcx;
Andre Przywara4668f052009-06-18 12:56:02 +02002450
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002451 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002452}
2453
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002454static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002455{
2456 int iopl;
2457 if (ctxt->mode == X86EMUL_MODE_REAL)
2458 return false;
2459 if (ctxt->mode == X86EMUL_MODE_VM86)
2460 return true;
2461 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002462 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002463}
2464
2465static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002466 u16 port, u16 len)
2467{
Mathias Krause0225fb52012-08-30 01:30:16 +02002468 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002469 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002470 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002471 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002472 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002473 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002474 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002475
Avi Kivity1aa36612011-04-27 13:20:30 +03002476 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002477 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002478 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002479 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002480 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002481 base = get_desc_base(&tr_seg);
2482#ifdef CONFIG_X86_64
2483 base |= ((u64)base3) << 32;
2484#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002485 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002486 if (r != X86EMUL_CONTINUE)
2487 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002488 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002489 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002490 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002491 if (r != X86EMUL_CONTINUE)
2492 return false;
2493 if ((perm >> bit_idx) & mask)
2494 return false;
2495 return true;
2496}
2497
2498static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002499 u16 port, u16 len)
2500{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002501 if (ctxt->perm_ok)
2502 return true;
2503
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002504 if (emulator_bad_iopl(ctxt))
2505 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002506 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002507
2508 ctxt->perm_ok = true;
2509
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002510 return true;
2511}
2512
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002513static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002514 struct tss_segment_16 *tss)
2515{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002516 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002517 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002518 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2519 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2520 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2521 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2522 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2523 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2524 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2525 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002526
Avi Kivity1aa36612011-04-27 13:20:30 +03002527 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2528 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2529 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2530 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2531 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002532}
2533
2534static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002535 struct tss_segment_16 *tss)
2536{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002537 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002538 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002539
Avi Kivity9dac77f2011-06-01 15:34:25 +03002540 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002541 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002542 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2543 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2544 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2545 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2546 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2547 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2548 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2549 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002550
2551 /*
2552 * SDM says that segment selectors are loaded before segment
2553 * descriptors
2554 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002555 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2556 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2557 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2558 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2559 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002560
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002561 cpl = tss->cs & 3;
2562
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002563 /*
Guo Chaofc058682012-06-28 15:19:51 +08002564 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002565 * it is handled in a context of new task
2566 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002567 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl,
2568 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002569 if (ret != X86EMUL_CONTINUE)
2570 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002571 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
2572 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002573 if (ret != X86EMUL_CONTINUE)
2574 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002575 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
2576 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002577 if (ret != X86EMUL_CONTINUE)
2578 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002579 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
2580 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002581 if (ret != X86EMUL_CONTINUE)
2582 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002583 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
2584 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002585 if (ret != X86EMUL_CONTINUE)
2586 return ret;
2587
2588 return X86EMUL_CONTINUE;
2589}
2590
2591static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002592 u16 tss_selector, u16 old_tss_sel,
2593 ulong old_tss_base, struct desc_struct *new_desc)
2594{
Mathias Krause0225fb52012-08-30 01:30:16 +02002595 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002596 struct tss_segment_16 tss_seg;
2597 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002598 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002599
Avi Kivity0f65dd72011-04-20 13:37:53 +03002600 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002601 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002602 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002603 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002604 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002605
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002606 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002607
Avi Kivity0f65dd72011-04-20 13:37:53 +03002608 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002609 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002610 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002611 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002612 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002613
Avi Kivity0f65dd72011-04-20 13:37:53 +03002614 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002615 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002616 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002617 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002618 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002619
2620 if (old_tss_sel != 0xffff) {
2621 tss_seg.prev_task_link = old_tss_sel;
2622
Avi Kivity0f65dd72011-04-20 13:37:53 +03002623 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002624 &tss_seg.prev_task_link,
2625 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002626 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002627 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002628 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002629 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002630 }
2631
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002632 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002633}
2634
2635static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002636 struct tss_segment_32 *tss)
2637{
Nadav Amit5c7411e2014-04-07 18:37:47 +03002638 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002639 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002640 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002641 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2642 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2643 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2644 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2645 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2646 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2647 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2648 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002649
Avi Kivity1aa36612011-04-27 13:20:30 +03002650 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2651 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2652 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2653 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2654 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2655 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002656}
2657
2658static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002659 struct tss_segment_32 *tss)
2660{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002661 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002662 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002663
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002664 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002665 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002666 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002667 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002668
2669 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002670 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2671 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2672 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2673 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2674 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2675 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2676 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2677 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002678
2679 /*
2680 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002681 * descriptors. This is important because CPL checks will
2682 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002683 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002684 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2685 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2686 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2687 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2688 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2689 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2690 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002691
2692 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002693 * If we're switching between Protected Mode and VM86, we need to make
2694 * sure to update the mode before loading the segment descriptors so
2695 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01002696 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002697 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002698 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002699 cpl = 3;
2700 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002701 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002702 cpl = tss->cs & 3;
2703 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01002704
2705 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002706 * Now load segment descriptors. If fault happenes at this stage
2707 * it is handled in a context of new task
2708 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002709 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR,
2710 cpl, true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002711 if (ret != X86EMUL_CONTINUE)
2712 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002713 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
2714 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002715 if (ret != X86EMUL_CONTINUE)
2716 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002717 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
2718 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002719 if (ret != X86EMUL_CONTINUE)
2720 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002721 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
2722 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002723 if (ret != X86EMUL_CONTINUE)
2724 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002725 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
2726 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002727 if (ret != X86EMUL_CONTINUE)
2728 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002729 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl,
2730 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002731 if (ret != X86EMUL_CONTINUE)
2732 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002733 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl,
2734 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002735 if (ret != X86EMUL_CONTINUE)
2736 return ret;
2737
2738 return X86EMUL_CONTINUE;
2739}
2740
2741static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002742 u16 tss_selector, u16 old_tss_sel,
2743 ulong old_tss_base, struct desc_struct *new_desc)
2744{
Mathias Krause0225fb52012-08-30 01:30:16 +02002745 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002746 struct tss_segment_32 tss_seg;
2747 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002748 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03002749 u32 eip_offset = offsetof(struct tss_segment_32, eip);
2750 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002751
Avi Kivity0f65dd72011-04-20 13:37:53 +03002752 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002753 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002754 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002755 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002756 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002757
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002758 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002759
Nadav Amit5c7411e2014-04-07 18:37:47 +03002760 /* Only GP registers and segment selectors are saved */
2761 ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
2762 ldt_sel_offset - eip_offset, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002763 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002764 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002765 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002766
Avi Kivity0f65dd72011-04-20 13:37:53 +03002767 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002768 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002769 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002770 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002771 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002772
2773 if (old_tss_sel != 0xffff) {
2774 tss_seg.prev_task_link = old_tss_sel;
2775
Avi Kivity0f65dd72011-04-20 13:37:53 +03002776 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002777 &tss_seg.prev_task_link,
2778 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002779 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002780 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002781 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002782 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002783 }
2784
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002785 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002786}
2787
2788static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002789 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002790 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002791{
Mathias Krause0225fb52012-08-30 01:30:16 +02002792 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002793 struct desc_struct curr_tss_desc, next_tss_desc;
2794 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002795 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002796 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002797 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002798 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002799 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002800
2801 /* FIXME: old_tss_base == ~0 ? */
2802
Avi Kivitye9194642012-06-13 16:29:39 +03002803 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002804 if (ret != X86EMUL_CONTINUE)
2805 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002806 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002807 if (ret != X86EMUL_CONTINUE)
2808 return ret;
2809
2810 /* FIXME: check that next_tss_desc is tss */
2811
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002812 /*
2813 * Check privileges. The three cases are task switch caused by...
2814 *
2815 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2816 * 2. Exception/IRQ/iret: No check is performed
Nadav Amit2c2ca2d2014-11-02 11:54:57 +02002817 * 3. jmp/call to TSS/task-gate: No check is performed since the
2818 * hardware checks it before exiting.
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002819 */
2820 if (reason == TASK_SWITCH_GATE) {
2821 if (idt_index != -1) {
2822 /* Software interrupts */
2823 struct desc_struct task_gate_desc;
2824 int dpl;
2825
2826 ret = read_interrupt_descriptor(ctxt, idt_index,
2827 &task_gate_desc);
2828 if (ret != X86EMUL_CONTINUE)
2829 return ret;
2830
2831 dpl = task_gate_desc.dpl;
2832 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2833 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2834 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002835 }
2836
Gleb Natapovceffb452010-03-18 15:20:19 +02002837 desc_limit = desc_limit_scaled(&next_tss_desc);
2838 if (!next_tss_desc.p ||
2839 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2840 desc_limit < 0x2b)) {
Paolo Bonzini592f0852014-08-20 10:05:08 +02002841 return emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002842 }
2843
2844 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2845 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002846 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002847 }
2848
2849 if (reason == TASK_SWITCH_IRET)
2850 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2851
2852 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002853 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002854 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2855 old_tss_sel = 0xffff;
2856
2857 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002858 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002859 old_tss_base, &next_tss_desc);
2860 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002861 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002862 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002863 if (ret != X86EMUL_CONTINUE)
2864 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002865
2866 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2867 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2868
2869 if (reason != TASK_SWITCH_IRET) {
2870 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002871 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002872 }
2873
Avi Kivity717746e2011-04-20 13:37:53 +03002874 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002875 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002876
Jan Kiszkae269fb22010-04-14 15:51:09 +02002877 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002878 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2879 ctxt->lock_prefix = 0;
2880 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002881 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002882 }
2883
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002884 return ret;
2885}
2886
2887int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002888 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002889 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002890{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002891 int rc;
2892
Avi Kivitydd856ef2012-08-27 23:46:17 +03002893 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002894 ctxt->_eip = ctxt->eip;
2895 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002896
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002897 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002898 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002899
Avi Kivitydd856ef2012-08-27 23:46:17 +03002900 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002901 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002902 writeback_registers(ctxt);
2903 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002904
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002905 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002906}
2907
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002908static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2909 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002910{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002911 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002912
Paolo Bonzini01485a22014-11-19 18:25:08 +01002913 register_address_increment(ctxt, reg, df * op->bytes);
2914 op->addr.mem.ea = register_address(ctxt, reg);
Gleb Natapova682e352010-03-18 15:20:21 +02002915}
2916
Avi Kivity7af04fc2010-08-18 14:16:35 +03002917static int em_das(struct x86_emulate_ctxt *ctxt)
2918{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002919 u8 al, old_al;
2920 bool af, cf, old_cf;
2921
2922 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002923 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002924
2925 old_al = al;
2926 old_cf = cf;
2927 cf = false;
2928 af = ctxt->eflags & X86_EFLAGS_AF;
2929 if ((al & 0x0f) > 9 || af) {
2930 al -= 6;
2931 cf = old_cf | (al >= 250);
2932 af = true;
2933 } else {
2934 af = false;
2935 }
2936 if (old_al > 0x99 || old_cf) {
2937 al -= 0x60;
2938 cf = true;
2939 }
2940
Avi Kivity9dac77f2011-06-01 15:34:25 +03002941 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002942 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002943 ctxt->src.type = OP_IMM;
2944 ctxt->src.val = 0;
2945 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02002946 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03002947 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2948 if (cf)
2949 ctxt->eflags |= X86_EFLAGS_CF;
2950 if (af)
2951 ctxt->eflags |= X86_EFLAGS_AF;
2952 return X86EMUL_CONTINUE;
2953}
2954
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02002955static int em_aam(struct x86_emulate_ctxt *ctxt)
2956{
2957 u8 al, ah;
2958
2959 if (ctxt->src.val == 0)
2960 return emulate_de(ctxt);
2961
2962 al = ctxt->dst.val & 0xff;
2963 ah = al / ctxt->src.val;
2964 al %= ctxt->src.val;
2965
2966 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
2967
2968 /* Set PF, ZF, SF */
2969 ctxt->src.type = OP_IMM;
2970 ctxt->src.val = 0;
2971 ctxt->src.bytes = 1;
2972 fastop(ctxt, em_or);
2973
2974 return X86EMUL_CONTINUE;
2975}
2976
Gleb Natapov7f662272012-12-10 11:42:30 +02002977static int em_aad(struct x86_emulate_ctxt *ctxt)
2978{
2979 u8 al = ctxt->dst.val & 0xff;
2980 u8 ah = (ctxt->dst.val >> 8) & 0xff;
2981
2982 al = (al + (ah * ctxt->src.val)) & 0xff;
2983
2984 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
2985
Gleb Natapovf583c292013-02-13 17:50:39 +02002986 /* Set PF, ZF, SF */
2987 ctxt->src.type = OP_IMM;
2988 ctxt->src.val = 0;
2989 ctxt->src.bytes = 1;
2990 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02002991
2992 return X86EMUL_CONTINUE;
2993}
2994
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002995static int em_call(struct x86_emulate_ctxt *ctxt)
2996{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002997 int rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002998 long rel = ctxt->src.val;
2999
3000 ctxt->src.val = (unsigned long)ctxt->_eip;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003001 rc = jmp_rel(ctxt, rel);
3002 if (rc != X86EMUL_CONTINUE)
3003 return rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003004 return em_push(ctxt);
3005}
3006
Avi Kivity0ef753b2010-08-18 14:51:45 +03003007static int em_call_far(struct x86_emulate_ctxt *ctxt)
3008{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003009 u16 sel, old_cs;
3010 ulong old_eip;
3011 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03003012 struct desc_struct old_desc, new_desc;
3013 const struct x86_emulate_ops *ops = ctxt->ops;
3014 int cpl = ctxt->ops->cpl(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003015
Avi Kivity9dac77f2011-06-01 15:34:25 +03003016 old_eip = ctxt->_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003017 ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003018
Avi Kivity9dac77f2011-06-01 15:34:25 +03003019 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Nadav Amitd1442d82014-09-18 22:39:39 +03003020 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl, false,
3021 &new_desc);
3022 if (rc != X86EMUL_CONTINUE)
Avi Kivity0ef753b2010-08-18 14:51:45 +03003023 return X86EMUL_CONTINUE;
3024
Nadav Amitd50eaa12014-11-19 17:43:11 +02003025 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003026 if (rc != X86EMUL_CONTINUE)
3027 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003028
Avi Kivity9dac77f2011-06-01 15:34:25 +03003029 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003030 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003031 if (rc != X86EMUL_CONTINUE)
Nadav Amitd1442d82014-09-18 22:39:39 +03003032 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003033
Avi Kivity9dac77f2011-06-01 15:34:25 +03003034 ctxt->src.val = old_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003035 rc = em_push(ctxt);
3036 /* If we failed, we tainted the memory, but the very least we should
3037 restore cs */
3038 if (rc != X86EMUL_CONTINUE)
3039 goto fail;
3040 return rc;
3041fail:
3042 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
3043 return rc;
3044
Avi Kivity0ef753b2010-08-18 14:51:45 +03003045}
3046
Avi Kivity40ece7c2010-08-18 15:12:09 +03003047static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3048{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003049 int rc;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003050 unsigned long eip;
Avi Kivity40ece7c2010-08-18 15:12:09 +03003051
Nadav Amit234f3ce2014-09-18 22:39:38 +03003052 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
3053 if (rc != X86EMUL_CONTINUE)
3054 return rc;
3055 rc = assign_eip_near(ctxt, eip);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003056 if (rc != X86EMUL_CONTINUE)
3057 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003058 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003059 return X86EMUL_CONTINUE;
3060}
3061
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003062static int em_xchg(struct x86_emulate_ctxt *ctxt)
3063{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003064 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003065 ctxt->src.val = ctxt->dst.val;
3066 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003067
3068 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003069 ctxt->dst.val = ctxt->src.orig_val;
3070 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003071 return X86EMUL_CONTINUE;
3072}
3073
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003074static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3075{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003076 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003077 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003078}
3079
Avi Kivity61429142010-08-19 15:13:00 +03003080static int em_cwd(struct x86_emulate_ctxt *ctxt)
3081{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003082 ctxt->dst.type = OP_REG;
3083 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003084 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003085 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003086
3087 return X86EMUL_CONTINUE;
3088}
3089
Avi Kivity48bb5d32010-08-18 18:54:34 +03003090static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3091{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003092 u64 tsc = 0;
3093
Avi Kivity717746e2011-04-20 13:37:53 +03003094 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003095 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3096 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003097 return X86EMUL_CONTINUE;
3098}
3099
Avi Kivity222d21a2011-11-10 14:57:30 +02003100static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3101{
3102 u64 pmc;
3103
Avi Kivitydd856ef2012-08-27 23:46:17 +03003104 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003105 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003106 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3107 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003108 return X86EMUL_CONTINUE;
3109}
3110
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003111static int em_mov(struct x86_emulate_ctxt *ctxt)
3112{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01003113 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003114 return X86EMUL_CONTINUE;
3115}
3116
Borislav Petkov84cffe42013-10-29 12:54:56 +01003117#define FFL(x) bit(X86_FEATURE_##x)
3118
3119static int em_movbe(struct x86_emulate_ctxt *ctxt)
3120{
3121 u32 ebx, ecx, edx, eax = 1;
3122 u16 tmp;
3123
3124 /*
3125 * Check MOVBE is set in the guest-visible CPUID leaf.
3126 */
3127 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
3128 if (!(ecx & FFL(MOVBE)))
3129 return emulate_ud(ctxt);
3130
3131 switch (ctxt->op_bytes) {
3132 case 2:
3133 /*
3134 * From MOVBE definition: "...When the operand size is 16 bits,
3135 * the upper word of the destination register remains unchanged
3136 * ..."
3137 *
3138 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3139 * rules so we have to do the operation almost per hand.
3140 */
3141 tmp = (u16)ctxt->src.val;
3142 ctxt->dst.val &= ~0xffffUL;
3143 ctxt->dst.val |= (unsigned long)swab16(tmp);
3144 break;
3145 case 4:
3146 ctxt->dst.val = swab32((u32)ctxt->src.val);
3147 break;
3148 case 8:
3149 ctxt->dst.val = swab64(ctxt->src.val);
3150 break;
3151 default:
Paolo Bonzini592f0852014-08-20 10:05:08 +02003152 BUG();
Borislav Petkov84cffe42013-10-29 12:54:56 +01003153 }
3154 return X86EMUL_CONTINUE;
3155}
3156
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003157static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3158{
3159 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3160 return emulate_gp(ctxt, 0);
3161
3162 /* Disable writeback. */
3163 ctxt->dst.type = OP_NONE;
3164 return X86EMUL_CONTINUE;
3165}
3166
3167static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3168{
3169 unsigned long val;
3170
3171 if (ctxt->mode == X86EMUL_MODE_PROT64)
3172 val = ctxt->src.val & ~0ULL;
3173 else
3174 val = ctxt->src.val & ~0U;
3175
3176 /* #UD condition is already handled. */
3177 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3178 return emulate_gp(ctxt, 0);
3179
3180 /* Disable writeback. */
3181 ctxt->dst.type = OP_NONE;
3182 return X86EMUL_CONTINUE;
3183}
3184
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003185static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3186{
3187 u64 msr_data;
3188
Avi Kivitydd856ef2012-08-27 23:46:17 +03003189 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3190 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3191 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003192 return emulate_gp(ctxt, 0);
3193
3194 return X86EMUL_CONTINUE;
3195}
3196
3197static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3198{
3199 u64 msr_data;
3200
Avi Kivitydd856ef2012-08-27 23:46:17 +03003201 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003202 return emulate_gp(ctxt, 0);
3203
Avi Kivitydd856ef2012-08-27 23:46:17 +03003204 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3205 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003206 return X86EMUL_CONTINUE;
3207}
3208
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003209static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3210{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003211 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003212 return emulate_ud(ctxt);
3213
Avi Kivity9dac77f2011-06-01 15:34:25 +03003214 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Nadav Amitb5bbf102014-11-02 11:54:46 +02003215 if (ctxt->dst.bytes == 4 && ctxt->dst.type == OP_MEM)
3216 ctxt->dst.bytes = 2;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003217 return X86EMUL_CONTINUE;
3218}
3219
3220static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3221{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003222 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003223
Avi Kivity9dac77f2011-06-01 15:34:25 +03003224 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003225 return emulate_ud(ctxt);
3226
Avi Kivity9dac77f2011-06-01 15:34:25 +03003227 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003228 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3229
3230 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003231 ctxt->dst.type = OP_NONE;
3232 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003233}
3234
Avi Kivitya14e5792012-06-13 12:28:33 +03003235static int em_lldt(struct x86_emulate_ctxt *ctxt)
3236{
3237 u16 sel = ctxt->src.val;
3238
3239 /* Disable writeback. */
3240 ctxt->dst.type = OP_NONE;
3241 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3242}
3243
Avi Kivity80890002012-06-13 16:33:29 +03003244static int em_ltr(struct x86_emulate_ctxt *ctxt)
3245{
3246 u16 sel = ctxt->src.val;
3247
3248 /* Disable writeback. */
3249 ctxt->dst.type = OP_NONE;
3250 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3251}
3252
Avi Kivity38503912011-03-31 18:48:09 +02003253static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3254{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003255 int rc;
3256 ulong linear;
3257
Avi Kivity9dac77f2011-06-01 15:34:25 +03003258 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003259 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003260 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003261 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003262 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003263 return X86EMUL_CONTINUE;
3264}
3265
Avi Kivity2d04a052011-04-20 15:32:49 +03003266static int em_clts(struct x86_emulate_ctxt *ctxt)
3267{
3268 ulong cr0;
3269
3270 cr0 = ctxt->ops->get_cr(ctxt, 0);
3271 cr0 &= ~X86_CR0_TS;
3272 ctxt->ops->set_cr(ctxt, 0, cr0);
3273 return X86EMUL_CONTINUE;
3274}
3275
Avi Kivity26d05cc2011-04-21 12:07:59 +03003276static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3277{
Nadav Amit0f54a322014-08-29 11:26:55 +03003278 int rc = ctxt->ops->fix_hypercall(ctxt);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003279
Avi Kivity26d05cc2011-04-21 12:07:59 +03003280 if (rc != X86EMUL_CONTINUE)
3281 return rc;
3282
3283 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003284 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003285 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003286 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003287 return X86EMUL_CONTINUE;
3288}
3289
Avi Kivity96051572012-06-10 17:21:18 +03003290static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3291 void (*get)(struct x86_emulate_ctxt *ctxt,
3292 struct desc_ptr *ptr))
3293{
3294 struct desc_ptr desc_ptr;
3295
3296 if (ctxt->mode == X86EMUL_MODE_PROT64)
3297 ctxt->op_bytes = 8;
3298 get(ctxt, &desc_ptr);
3299 if (ctxt->op_bytes == 2) {
3300 ctxt->op_bytes = 4;
3301 desc_ptr.address &= 0x00ffffff;
3302 }
3303 /* Disable writeback. */
3304 ctxt->dst.type = OP_NONE;
3305 return segmented_write(ctxt, ctxt->dst.addr.mem,
3306 &desc_ptr, 2 + ctxt->op_bytes);
3307}
3308
3309static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3310{
3311 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3312}
3313
3314static int em_sidt(struct x86_emulate_ctxt *ctxt)
3315{
3316 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3317}
3318
Nadav Amit5b7f6a12014-11-02 11:54:55 +02003319static int em_lgdt_lidt(struct x86_emulate_ctxt *ctxt, bool lgdt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003320{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003321 struct desc_ptr desc_ptr;
3322 int rc;
3323
Avi Kivity510425f2012-06-07 17:04:36 +03003324 if (ctxt->mode == X86EMUL_MODE_PROT64)
3325 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003326 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003327 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003328 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003329 if (rc != X86EMUL_CONTINUE)
3330 return rc;
Nadav Amit9a9abf62014-11-02 11:54:56 +02003331 if (ctxt->mode == X86EMUL_MODE_PROT64 &&
3332 is_noncanonical_address(desc_ptr.address))
3333 return emulate_gp(ctxt, 0);
Nadav Amit5b7f6a12014-11-02 11:54:55 +02003334 if (lgdt)
3335 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3336 else
3337 ctxt->ops->set_idt(ctxt, &desc_ptr);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003338 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003339 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003340 return X86EMUL_CONTINUE;
3341}
3342
Nadav Amit5b7f6a12014-11-02 11:54:55 +02003343static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3344{
3345 return em_lgdt_lidt(ctxt, true);
3346}
3347
Avi Kivity5ef39c72011-04-21 12:21:50 +03003348static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003349{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003350 int rc;
3351
Avi Kivity5ef39c72011-04-21 12:21:50 +03003352 rc = ctxt->ops->fix_hypercall(ctxt);
3353
Avi Kivity26d05cc2011-04-21 12:07:59 +03003354 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003355 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003356 return rc;
3357}
3358
3359static int em_lidt(struct x86_emulate_ctxt *ctxt)
3360{
Nadav Amit5b7f6a12014-11-02 11:54:55 +02003361 return em_lgdt_lidt(ctxt, false);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003362}
3363
3364static int em_smsw(struct x86_emulate_ctxt *ctxt)
3365{
Nadav Amit32e94d02014-06-02 18:34:11 +03003366 if (ctxt->dst.type == OP_MEM)
3367 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003368 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003369 return X86EMUL_CONTINUE;
3370}
3371
3372static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3373{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003374 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003375 | (ctxt->src.val & 0x0f));
3376 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003377 return X86EMUL_CONTINUE;
3378}
3379
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003380static int em_loop(struct x86_emulate_ctxt *ctxt)
3381{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003382 int rc = X86EMUL_CONTINUE;
3383
Paolo Bonzini01485a22014-11-19 18:25:08 +01003384 register_address_increment(ctxt, VCPU_REGS_RCX, -1);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003385 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003386 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
Nadav Amit234f3ce2014-09-18 22:39:38 +03003387 rc = jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003388
Nadav Amit234f3ce2014-09-18 22:39:38 +03003389 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003390}
3391
3392static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3393{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003394 int rc = X86EMUL_CONTINUE;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003395
Nadav Amit234f3ce2014-09-18 22:39:38 +03003396 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
3397 rc = jmp_rel(ctxt, ctxt->src.val);
3398
3399 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003400}
3401
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003402static int em_in(struct x86_emulate_ctxt *ctxt)
3403{
3404 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3405 &ctxt->dst.val))
3406 return X86EMUL_IO_NEEDED;
3407
3408 return X86EMUL_CONTINUE;
3409}
3410
3411static int em_out(struct x86_emulate_ctxt *ctxt)
3412{
3413 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3414 &ctxt->src.val, 1);
3415 /* Disable writeback. */
3416 ctxt->dst.type = OP_NONE;
3417 return X86EMUL_CONTINUE;
3418}
3419
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003420static int em_cli(struct x86_emulate_ctxt *ctxt)
3421{
3422 if (emulator_bad_iopl(ctxt))
3423 return emulate_gp(ctxt, 0);
3424
3425 ctxt->eflags &= ~X86_EFLAGS_IF;
3426 return X86EMUL_CONTINUE;
3427}
3428
3429static int em_sti(struct x86_emulate_ctxt *ctxt)
3430{
3431 if (emulator_bad_iopl(ctxt))
3432 return emulate_gp(ctxt, 0);
3433
3434 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3435 ctxt->eflags |= X86_EFLAGS_IF;
3436 return X86EMUL_CONTINUE;
3437}
3438
Avi Kivity6d6eede2012-06-07 14:11:36 +03003439static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3440{
3441 u32 eax, ebx, ecx, edx;
3442
Avi Kivitydd856ef2012-08-27 23:46:17 +03003443 eax = reg_read(ctxt, VCPU_REGS_RAX);
3444 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003445 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003446 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3447 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3448 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3449 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003450 return X86EMUL_CONTINUE;
3451}
3452
Paolo Bonzini98f73632013-10-31 11:19:42 +01003453static int em_sahf(struct x86_emulate_ctxt *ctxt)
3454{
3455 u32 flags;
3456
3457 flags = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF;
3458 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3459
3460 ctxt->eflags &= ~0xffUL;
3461 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3462 return X86EMUL_CONTINUE;
3463}
3464
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003465static int em_lahf(struct x86_emulate_ctxt *ctxt)
3466{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003467 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3468 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003469 return X86EMUL_CONTINUE;
3470}
3471
Avi Kivity92998362012-06-13 12:25:06 +03003472static int em_bswap(struct x86_emulate_ctxt *ctxt)
3473{
3474 switch (ctxt->op_bytes) {
3475#ifdef CONFIG_X86_64
3476 case 8:
3477 asm("bswap %0" : "+r"(ctxt->dst.val));
3478 break;
3479#endif
3480 default:
3481 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3482 break;
3483 }
3484 return X86EMUL_CONTINUE;
3485}
3486
Nadav Amit13e457e2014-10-13 13:04:13 +03003487static int em_clflush(struct x86_emulate_ctxt *ctxt)
3488{
3489 /* emulating clflush regardless of cpuid */
3490 return X86EMUL_CONTINUE;
3491}
3492
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003493static bool valid_cr(int nr)
3494{
3495 switch (nr) {
3496 case 0:
3497 case 2 ... 4:
3498 case 8:
3499 return true;
3500 default:
3501 return false;
3502 }
3503}
3504
3505static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3506{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003507 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003508 return emulate_ud(ctxt);
3509
3510 return X86EMUL_CONTINUE;
3511}
3512
3513static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3514{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003515 u64 new_val = ctxt->src.val64;
3516 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003517 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003518
3519 static u64 cr_reserved_bits[] = {
3520 0xffffffff00000000ULL,
3521 0, 0, 0, /* CR3 checked later */
3522 CR4_RESERVED_BITS,
3523 0, 0, 0,
3524 CR8_RESERVED_BITS,
3525 };
3526
3527 if (!valid_cr(cr))
3528 return emulate_ud(ctxt);
3529
3530 if (new_val & cr_reserved_bits[cr])
3531 return emulate_gp(ctxt, 0);
3532
3533 switch (cr) {
3534 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003535 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003536 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3537 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3538 return emulate_gp(ctxt, 0);
3539
Avi Kivity717746e2011-04-20 13:37:53 +03003540 cr4 = ctxt->ops->get_cr(ctxt, 4);
3541 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003542
3543 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3544 !(cr4 & X86_CR4_PAE))
3545 return emulate_gp(ctxt, 0);
3546
3547 break;
3548 }
3549 case 3: {
3550 u64 rsvd = 0;
3551
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003552 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3553 if (efer & EFER_LMA)
Nadav Amit9d88fca2014-11-02 11:54:52 +02003554 rsvd = CR3_L_MODE_RESERVED_BITS & ~CR3_PCID_INVD;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003555
3556 if (new_val & rsvd)
3557 return emulate_gp(ctxt, 0);
3558
3559 break;
3560 }
3561 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003562 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003563
3564 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3565 return emulate_gp(ctxt, 0);
3566
3567 break;
3568 }
3569 }
3570
3571 return X86EMUL_CONTINUE;
3572}
3573
Joerg Roedel3b88e412011-04-04 12:39:29 +02003574static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3575{
3576 unsigned long dr7;
3577
Avi Kivity717746e2011-04-20 13:37:53 +03003578 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003579
3580 /* Check if DR7.Global_Enable is set */
3581 return dr7 & (1 << 13);
3582}
3583
3584static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3585{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003586 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003587 u64 cr4;
3588
3589 if (dr > 7)
3590 return emulate_ud(ctxt);
3591
Avi Kivity717746e2011-04-20 13:37:53 +03003592 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003593 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3594 return emulate_ud(ctxt);
3595
Nadav Amit6d2a0522014-11-02 11:54:43 +02003596 if (check_dr7_gd(ctxt)) {
3597 ulong dr6;
3598
3599 ctxt->ops->get_dr(ctxt, 6, &dr6);
3600 dr6 &= ~15;
3601 dr6 |= DR6_BD | DR6_RTM;
3602 ctxt->ops->set_dr(ctxt, 6, dr6);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003603 return emulate_db(ctxt);
Nadav Amit6d2a0522014-11-02 11:54:43 +02003604 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02003605
3606 return X86EMUL_CONTINUE;
3607}
3608
3609static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3610{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003611 u64 new_val = ctxt->src.val64;
3612 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003613
3614 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3615 return emulate_gp(ctxt, 0);
3616
3617 return check_dr_read(ctxt);
3618}
3619
Joerg Roedel01de8b02011-04-04 12:39:31 +02003620static int check_svme(struct x86_emulate_ctxt *ctxt)
3621{
3622 u64 efer;
3623
Avi Kivity717746e2011-04-20 13:37:53 +03003624 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003625
3626 if (!(efer & EFER_SVME))
3627 return emulate_ud(ctxt);
3628
3629 return X86EMUL_CONTINUE;
3630}
3631
3632static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3633{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003634 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003635
3636 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003637 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003638 return emulate_gp(ctxt, 0);
3639
3640 return check_svme(ctxt);
3641}
3642
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003643static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3644{
Avi Kivity717746e2011-04-20 13:37:53 +03003645 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003646
Avi Kivity717746e2011-04-20 13:37:53 +03003647 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003648 return emulate_ud(ctxt);
3649
3650 return X86EMUL_CONTINUE;
3651}
3652
Joerg Roedel80612522011-04-04 12:39:33 +02003653static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3654{
Avi Kivity717746e2011-04-20 13:37:53 +03003655 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003656 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003657
Avi Kivity717746e2011-04-20 13:37:53 +03003658 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03003659 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02003660 return emulate_gp(ctxt, 0);
3661
3662 return X86EMUL_CONTINUE;
3663}
3664
Joerg Roedelf6511932011-04-04 12:39:35 +02003665static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3666{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003667 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3668 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003669 return emulate_gp(ctxt, 0);
3670
3671 return X86EMUL_CONTINUE;
3672}
3673
3674static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3675{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003676 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3677 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003678 return emulate_gp(ctxt, 0);
3679
3680 return X86EMUL_CONTINUE;
3681}
3682
Avi Kivity73fba5f2010-07-29 15:11:53 +03003683#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003684#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
3685#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
3686 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003687#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003688#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003689#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3690#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003691#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003692#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003693#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003694#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003695 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003696#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003697 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
3698 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003699#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003700
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003701#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003702#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003703#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003704#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003705#define I2bvIP(_f, _e, _i, _p) \
3706 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003707
Avi Kivityfb864fb2013-01-04 16:18:54 +02003708#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3709 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3710 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003711
Nadav Amit0f54a322014-08-29 11:26:55 +03003712static const struct opcode group7_rm0[] = {
3713 N,
3714 I(SrcNone | Priv | EmulateOnUD, em_vmcall),
3715 N, N, N, N, N, N,
3716};
3717
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003718static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003719 DI(SrcNone | Priv, monitor),
3720 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003721 N, N, N, N, N, N,
3722};
3723
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003724static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003725 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003726 II(SrcNone | Prot | EmulateOnUD, em_vmmcall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003727 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3728 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3729 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3730 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3731 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3732 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003733};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003734
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003735static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003736 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003737 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003738 N, N, N, N, N, N,
3739};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003740
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003741static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003742 F(Lock, em_add),
3743 F(Lock | PageTable, em_or),
3744 F(Lock, em_adc),
3745 F(Lock, em_sbb),
3746 F(Lock | PageTable, em_and),
3747 F(Lock, em_sub),
3748 F(Lock, em_xor),
3749 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003750};
3751
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003752static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003753 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003754};
3755
Avi Kivity007a3b52013-01-19 19:51:51 +02003756static const struct opcode group2[] = {
3757 F(DstMem | ModRM, em_rol),
3758 F(DstMem | ModRM, em_ror),
3759 F(DstMem | ModRM, em_rcl),
3760 F(DstMem | ModRM, em_rcr),
3761 F(DstMem | ModRM, em_shl),
3762 F(DstMem | ModRM, em_shr),
3763 F(DstMem | ModRM, em_shl),
3764 F(DstMem | ModRM, em_sar),
3765};
3766
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003767static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003768 F(DstMem | SrcImm | NoWrite, em_test),
3769 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003770 F(DstMem | SrcNone | Lock, em_not),
3771 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02003772 F(DstXacc | Src2Mem, em_mul_ex),
3773 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02003774 F(DstXacc | Src2Mem, em_div_ex),
3775 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003776};
3777
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003778static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003779 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3780 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003781 N, N, N, N, N, N,
3782};
3783
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003784static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003785 F(DstMem | SrcNone | Lock, em_inc),
3786 F(DstMem | SrcNone | Lock, em_dec),
Nadav Amit58b70752014-10-24 11:35:09 +03003787 I(SrcMem | NearBranch, em_call_near_abs),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003788 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
Nadav Amit58b70752014-10-24 11:35:09 +03003789 I(SrcMem | NearBranch, em_jmp_abs),
Nadav Amitf7784042014-09-18 22:39:41 +03003790 I(SrcMemFAddr | ImplicitOps, em_jmp_far),
3791 I(SrcMem | Stack, em_push), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003792};
3793
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003794static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003795 DI(Prot, sldt),
3796 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003797 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003798 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003799 N, N, N, N,
3800};
3801
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003802static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03003803 II(Mov | DstMem, em_sgdt, sgdt),
3804 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003805 II(SrcMem | Priv, em_lgdt, lgdt),
3806 II(SrcMem | Priv, em_lidt, lidt),
3807 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3808 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3809 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003810}, {
Nadav Amit0f54a322014-08-29 11:26:55 +03003811 EXT(0, group7_rm0),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003812 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003813 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003814 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3815 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3816 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003817} };
3818
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003819static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003820 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003821 F(DstMem | SrcImmByte | NoWrite, em_bt),
3822 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3823 F(DstMem | SrcImmByte | Lock, em_btr),
3824 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003825};
3826
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003827static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003828 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003829}, {
3830 N, N, N, N, N, N, N, N,
3831} };
3832
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003833static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003834 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003835 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003836};
3837
Nadav Amit13e457e2014-10-13 13:04:13 +03003838static const struct gprefix pfx_0f_ae_7 = {
Nadav Amit3f6f1482014-10-13 13:04:14 +03003839 I(SrcMem | ByteOp, em_clflush), N, N, N,
Nadav Amit13e457e2014-10-13 13:04:13 +03003840};
3841
3842static const struct group_dual group15 = { {
3843 N, N, N, N, N, N, N, GP(0, &pfx_0f_ae_7),
3844}, {
3845 N, N, N, N, N, N, N, N,
3846} };
3847
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003848static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003849 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003850};
3851
Paolo Bonzinid5b77062014-07-14 12:54:48 +02003852static const struct gprefix pfx_0f_2b = {
3853 I(0, em_mov), I(0, em_mov), N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003854};
3855
Igor Mammedov27ce8252014-03-15 21:01:59 +01003856static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01003857 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003858};
3859
Alex Williamson0a370272014-07-11 11:56:31 -06003860static const struct gprefix pfx_0f_e7 = {
3861 N, I(Sse, em_mov), N, N,
3862};
3863
Gleb Natapov045a2822012-12-20 16:57:43 +02003864static const struct escape escape_d9 = { {
3865 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3866}, {
3867 /* 0xC0 - 0xC7 */
3868 N, N, N, N, N, N, N, N,
3869 /* 0xC8 - 0xCF */
3870 N, N, N, N, N, N, N, N,
3871 /* 0xD0 - 0xC7 */
3872 N, N, N, N, N, N, N, N,
3873 /* 0xD8 - 0xDF */
3874 N, N, N, N, N, N, N, N,
3875 /* 0xE0 - 0xE7 */
3876 N, N, N, N, N, N, N, N,
3877 /* 0xE8 - 0xEF */
3878 N, N, N, N, N, N, N, N,
3879 /* 0xF0 - 0xF7 */
3880 N, N, N, N, N, N, N, N,
3881 /* 0xF8 - 0xFF */
3882 N, N, N, N, N, N, N, N,
3883} };
3884
3885static const struct escape escape_db = { {
3886 N, N, N, N, N, N, N, N,
3887}, {
3888 /* 0xC0 - 0xC7 */
3889 N, N, N, N, N, N, N, N,
3890 /* 0xC8 - 0xCF */
3891 N, N, N, N, N, N, N, N,
3892 /* 0xD0 - 0xC7 */
3893 N, N, N, N, N, N, N, N,
3894 /* 0xD8 - 0xDF */
3895 N, N, N, N, N, N, N, N,
3896 /* 0xE0 - 0xE7 */
3897 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3898 /* 0xE8 - 0xEF */
3899 N, N, N, N, N, N, N, N,
3900 /* 0xF0 - 0xF7 */
3901 N, N, N, N, N, N, N, N,
3902 /* 0xF8 - 0xFF */
3903 N, N, N, N, N, N, N, N,
3904} };
3905
3906static const struct escape escape_dd = { {
3907 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3908}, {
3909 /* 0xC0 - 0xC7 */
3910 N, N, N, N, N, N, N, N,
3911 /* 0xC8 - 0xCF */
3912 N, N, N, N, N, N, N, N,
3913 /* 0xD0 - 0xC7 */
3914 N, N, N, N, N, N, N, N,
3915 /* 0xD8 - 0xDF */
3916 N, N, N, N, N, N, N, N,
3917 /* 0xE0 - 0xE7 */
3918 N, N, N, N, N, N, N, N,
3919 /* 0xE8 - 0xEF */
3920 N, N, N, N, N, N, N, N,
3921 /* 0xF0 - 0xF7 */
3922 N, N, N, N, N, N, N, N,
3923 /* 0xF8 - 0xFF */
3924 N, N, N, N, N, N, N, N,
3925} };
3926
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003927static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003928 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003929 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003930 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3931 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003932 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003933 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003934 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3935 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003936 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003937 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003938 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3939 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003940 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003941 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003942 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3943 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003944 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003945 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003946 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003947 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003948 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003949 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003950 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003951 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003952 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02003953 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003954 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003955 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003956 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003957 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003958 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003959 I(ImplicitOps | Stack | No64, em_pusha),
3960 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003961 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3962 N, N, N, N,
3963 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003964 I(SrcImm | Mov | Stack, em_push),
3965 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003966 I(SrcImmByte | Mov | Stack, em_push),
3967 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003968 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003969 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003970 /* 0x70 - 0x7F */
Nadav Amit58b70752014-10-24 11:35:09 +03003971 X16(D(SrcImmByte | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003972 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003973 G(ByteOp | DstMem | SrcImm, group1),
3974 G(DstMem | SrcImm, group1),
3975 G(ByteOp | DstMem | SrcImm | No64, group1),
3976 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003977 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003978 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003979 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003980 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003981 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003982 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003983 D(ModRM | SrcMem | NoAccess | DstReg),
3984 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3985 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003986 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003987 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003988 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003989 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003990 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003991 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01003992 II(ImplicitOps | Stack, em_popf, popf),
3993 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003994 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003995 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003996 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003997 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02003998 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003999 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004000 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004001 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
4002 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004003 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004004 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004005 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004006 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02004007 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004008 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004009 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Nadav Amit58b70752014-10-24 11:35:09 +03004010 I(ImplicitOps | NearBranch | SrcImmU16, em_ret_near_imm),
4011 I(ImplicitOps | NearBranch, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03004012 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
4013 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004014 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004015 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03004016 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Bruce Rogers32611072013-09-09 09:40:20 -06004017 I(ImplicitOps | Stack | SrcImmU16, em_ret_far_imm),
4018 I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02004019 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004020 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004021 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004022 G(Src2One | ByteOp, group2), G(Src2One, group2),
4023 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02004024 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02004025 I(DstAcc | SrcImmUByte | No64, em_aad),
4026 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004027 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004028 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02004029 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004030 /* 0xE0 - 0xE7 */
Nadav Amit58b70752014-10-24 11:35:09 +03004031 X3(I(SrcImmByte | NearBranch, em_loop)),
4032 I(SrcImmByte | NearBranch, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004033 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
4034 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004035 /* 0xE8 - 0xEF */
Nadav Amit58b70752014-10-24 11:35:09 +03004036 I(SrcImm | NearBranch, em_call), D(SrcImm | ImplicitOps | NearBranch),
4037 I(SrcImmFAddr | No64, em_jmp_far),
4038 D(SrcImmByte | ImplicitOps | NearBranch),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004039 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
4040 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004041 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004042 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004043 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4044 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004045 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09004046 D(ImplicitOps), D(ImplicitOps),
4047 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004048 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4049};
4050
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004051static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004052 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004053 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02004054 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004055 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004056 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004057 N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004058 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004059 N, N, N, N, N, N, N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004060 D(ImplicitOps | ModRM | SrcMem | NoAccess),
4061 N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004062 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03004063 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
4064 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
4065 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
4066 check_cr_write),
4067 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
4068 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004069 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004070 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
4071 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004072 N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b),
Avi Kivity3e114eb2012-04-09 18:40:01 +03004073 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004074 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004075 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004076 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004077 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004078 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02004079 I(ImplicitOps | EmulateOnUD, em_sysenter),
4080 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004081 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004082 N, N, N, N, N, N, N, N,
4083 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03004084 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004085 /* 0x50 - 0x5F */
4086 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4087 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004088 N, N, N, N,
4089 N, N, N, N,
4090 N, N, N, N,
4091 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004092 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004093 N, N, N, N,
4094 N, N, N, N,
4095 N, N, N, N,
4096 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004097 /* 0x80 - 0x8F */
Nadav Amit58b70752014-10-24 11:35:09 +03004098 X16(D(SrcImm | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004099 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004100 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004101 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004102 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004103 II(ImplicitOps, em_cpuid, cpuid),
4104 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004105 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4106 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004107 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004108 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004109 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004110 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004111 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4112 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Nadav Amit13e457e2014-10-13 13:04:13 +03004113 GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004114 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09004115 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004116 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004117 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004118 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4119 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004120 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004121 /* 0xB8 - 0xBF */
4122 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004123 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004124 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
4125 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004126 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004127 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02004128 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Nadav Amited9aad22014-11-02 11:55:00 +02004129 N, I(DstMem | SrcReg | ModRM | No16 | Mov, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004130 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004131 /* 0xC8 - 0xCF */
4132 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004133 /* 0xD0 - 0xDF */
4134 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4135 /* 0xE0 - 0xEF */
Alex Williamson0a370272014-07-11 11:56:31 -06004136 N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
4137 N, N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004138 /* 0xF0 - 0xFF */
4139 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4140};
4141
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004142static const struct gprefix three_byte_0f_38_f0 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01004143 I(DstReg | SrcMem | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004144};
4145
4146static const struct gprefix three_byte_0f_38_f1 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01004147 I(DstMem | SrcReg | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004148};
4149
4150/*
4151 * Insns below are selected by the prefix which indexed by the third opcode
4152 * byte.
4153 */
4154static const struct opcode opcode_map_0f_38[256] = {
4155 /* 0x00 - 0x7f */
4156 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004157 /* 0x80 - 0xef */
4158 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4159 /* 0xf0 - 0xf1 */
4160 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f0),
4161 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f1),
4162 /* 0xf2 - 0xff */
4163 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004164};
4165
Avi Kivity73fba5f2010-07-29 15:11:53 +03004166#undef D
4167#undef N
4168#undef G
4169#undef GD
4170#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004171#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004172#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004173
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004174#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004175#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004176#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004177#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004178#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004179
Avi Kivity9dac77f2011-06-01 15:34:25 +03004180static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004181{
4182 unsigned size;
4183
Avi Kivity9dac77f2011-06-01 15:34:25 +03004184 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004185 if (size == 8)
4186 size = 4;
4187 return size;
4188}
4189
4190static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4191 unsigned size, bool sign_extension)
4192{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004193 int rc = X86EMUL_CONTINUE;
4194
4195 op->type = OP_IMM;
4196 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004197 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004198 /* NB. Immediates are sign-extended as necessary. */
4199 switch (op->bytes) {
4200 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004201 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004202 break;
4203 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004204 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004205 break;
4206 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004207 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004208 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004209 case 8:
4210 op->val = insn_fetch(s64, ctxt);
4211 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004212 }
4213 if (!sign_extension) {
4214 switch (op->bytes) {
4215 case 1:
4216 op->val &= 0xff;
4217 break;
4218 case 2:
4219 op->val &= 0xffff;
4220 break;
4221 case 4:
4222 op->val &= 0xffffffff;
4223 break;
4224 }
4225 }
4226done:
4227 return rc;
4228}
4229
Avi Kivitya9945542011-09-13 10:45:41 +03004230static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4231 unsigned d)
4232{
4233 int rc = X86EMUL_CONTINUE;
4234
4235 switch (d) {
4236 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004237 decode_register_operand(ctxt, op);
Avi Kivitya9945542011-09-13 10:45:41 +03004238 break;
4239 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004240 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03004241 break;
4242 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004243 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004244 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03004245 *op = ctxt->memop;
4246 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004247 if (ctxt->d & BitOp)
Avi Kivitya9945542011-09-13 10:45:41 +03004248 fetch_bit_operand(ctxt);
4249 op->orig_val = op->val;
4250 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004251 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004252 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004253 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03004254 case OpAcc:
4255 op->type = OP_REG;
4256 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004257 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya9945542011-09-13 10:45:41 +03004258 fetch_register_operand(op);
4259 op->orig_val = op->val;
4260 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004261 case OpAccLo:
4262 op->type = OP_REG;
4263 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4264 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4265 fetch_register_operand(op);
4266 op->orig_val = op->val;
4267 break;
4268 case OpAccHi:
4269 if (ctxt->d & ByteOp) {
4270 op->type = OP_NONE;
4271 break;
4272 }
4273 op->type = OP_REG;
4274 op->bytes = ctxt->op_bytes;
4275 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4276 fetch_register_operand(op);
4277 op->orig_val = op->val;
4278 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004279 case OpDI:
4280 op->type = OP_MEM;
4281 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4282 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004283 register_address(ctxt, VCPU_REGS_RDI);
Avi Kivitya9945542011-09-13 10:45:41 +03004284 op->addr.mem.seg = VCPU_SREG_ES;
4285 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004286 op->count = 1;
Avi Kivitya9945542011-09-13 10:45:41 +03004287 break;
4288 case OpDX:
4289 op->type = OP_REG;
4290 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004291 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya9945542011-09-13 10:45:41 +03004292 fetch_register_operand(op);
4293 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004294 case OpCL:
4295 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004296 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004297 break;
4298 case OpImmByte:
4299 rc = decode_imm(ctxt, op, 1, true);
4300 break;
4301 case OpOne:
4302 op->bytes = 1;
4303 op->val = 1;
4304 break;
4305 case OpImm:
4306 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4307 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004308 case OpImm64:
4309 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4310 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004311 case OpMem8:
4312 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004313 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004314 ctxt->memop.addr.reg = decode_register(ctxt,
4315 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004316 fetch_register_operand(&ctxt->memop);
4317 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004318 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004319 case OpMem16:
4320 ctxt->memop.bytes = 2;
4321 goto mem_common;
4322 case OpMem32:
4323 ctxt->memop.bytes = 4;
4324 goto mem_common;
4325 case OpImmU16:
4326 rc = decode_imm(ctxt, op, 2, false);
4327 break;
4328 case OpImmU:
4329 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4330 break;
4331 case OpSI:
4332 op->type = OP_MEM;
4333 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4334 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004335 register_address(ctxt, VCPU_REGS_RSI);
Bandan Das573e80f2014-04-16 12:46:13 -04004336 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03004337 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004338 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004339 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004340 case OpXLat:
4341 op->type = OP_MEM;
4342 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4343 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004344 address_mask(ctxt,
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004345 reg_read(ctxt, VCPU_REGS_RBX) +
4346 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04004347 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004348 op->val = 0;
4349 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004350 case OpImmFAddr:
4351 op->type = OP_IMM;
4352 op->addr.mem.ea = ctxt->_eip;
4353 op->bytes = ctxt->op_bytes + 2;
4354 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4355 break;
4356 case OpMemFAddr:
4357 ctxt->memop.bytes = ctxt->op_bytes + 2;
4358 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004359 case OpES:
4360 op->val = VCPU_SREG_ES;
4361 break;
4362 case OpCS:
4363 op->val = VCPU_SREG_CS;
4364 break;
4365 case OpSS:
4366 op->val = VCPU_SREG_SS;
4367 break;
4368 case OpDS:
4369 op->val = VCPU_SREG_DS;
4370 break;
4371 case OpFS:
4372 op->val = VCPU_SREG_FS;
4373 break;
4374 case OpGS:
4375 op->val = VCPU_SREG_GS;
4376 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004377 case OpImplicit:
4378 /* Special instructions do their own operand decoding. */
4379 default:
4380 op->type = OP_NONE; /* Disable writeback. */
4381 break;
4382 }
4383
4384done:
4385 return rc;
4386}
4387
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004388int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004389{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004390 int rc = X86EMUL_CONTINUE;
4391 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004392 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004393 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04004394 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03004395 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004396
Avi Kivityf09ed832011-09-13 10:45:40 +03004397 ctxt->memop.type = OP_NONE;
4398 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004399 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02004400 ctxt->fetch.ptr = ctxt->fetch.data;
4401 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004402 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01004403 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004404 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004405 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02004406 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004407 if (rc != X86EMUL_CONTINUE)
4408 return rc;
4409 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004410
4411 switch (mode) {
4412 case X86EMUL_MODE_REAL:
4413 case X86EMUL_MODE_VM86:
4414 case X86EMUL_MODE_PROT16:
4415 def_op_bytes = def_ad_bytes = 2;
4416 break;
4417 case X86EMUL_MODE_PROT32:
4418 def_op_bytes = def_ad_bytes = 4;
4419 break;
4420#ifdef CONFIG_X86_64
4421 case X86EMUL_MODE_PROT64:
4422 def_op_bytes = 4;
4423 def_ad_bytes = 8;
4424 break;
4425#endif
4426 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004427 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004428 }
4429
Avi Kivity9dac77f2011-06-01 15:34:25 +03004430 ctxt->op_bytes = def_op_bytes;
4431 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004432
4433 /* Legacy prefixes. */
4434 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004435 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004436 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004437 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004438 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004439 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004440 break;
4441 case 0x67: /* address-size override */
4442 if (mode == X86EMUL_MODE_PROT64)
4443 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004444 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004445 else
4446 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004447 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004448 break;
4449 case 0x26: /* ES override */
4450 case 0x2e: /* CS override */
4451 case 0x36: /* SS override */
4452 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004453 has_seg_override = true;
4454 ctxt->seg_override = (ctxt->b >> 3) & 3;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004455 break;
4456 case 0x64: /* FS override */
4457 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004458 has_seg_override = true;
4459 ctxt->seg_override = ctxt->b & 7;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004460 break;
4461 case 0x40 ... 0x4f: /* REX */
4462 if (mode != X86EMUL_MODE_PROT64)
4463 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004464 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004465 continue;
4466 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004467 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004468 break;
4469 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004470 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004471 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004472 break;
4473 default:
4474 goto done_prefixes;
4475 }
4476
4477 /* Any legacy prefix after a REX prefix nullifies its effect. */
4478
Avi Kivity9dac77f2011-06-01 15:34:25 +03004479 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004480 }
4481
4482done_prefixes:
4483
4484 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004485 if (ctxt->rex_prefix & 8)
4486 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004487
4488 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004489 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004490 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004491 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004492 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004493 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004494 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004495
4496 /* 0F_38 opcode map */
4497 if (ctxt->b == 0x38) {
4498 ctxt->opcode_len = 3;
4499 ctxt->b = insn_fetch(u8, ctxt);
4500 opcode = opcode_map_0f_38[ctxt->b];
4501 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004502 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004503 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004504
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004505 if (ctxt->d & ModRM)
4506 ctxt->modrm = insn_fetch(u8, ctxt);
4507
Nadav Amit7fe864d2014-06-02 18:34:03 +03004508 /* vex-prefix instructions are not implemented */
4509 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
Nadav Amitd14cb5d2014-11-02 11:54:58 +02004510 (mode == X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) == 0xc0)) {
Nadav Amit7fe864d2014-06-02 18:34:03 +03004511 ctxt->d = NotImpl;
4512 }
4513
Avi Kivity9dac77f2011-06-01 15:34:25 +03004514 while (ctxt->d & GroupMask) {
4515 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004516 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004517 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004518 opcode = opcode.u.group[goffset];
4519 break;
4520 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004521 goffset = (ctxt->modrm >> 3) & 7;
4522 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004523 opcode = opcode.u.gdual->mod3[goffset];
4524 else
4525 opcode = opcode.u.gdual->mod012[goffset];
4526 break;
4527 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004528 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004529 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004530 break;
4531 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004532 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004533 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004534 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004535 switch (simd_prefix) {
4536 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4537 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4538 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4539 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4540 }
4541 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004542 case Escape:
4543 if (ctxt->modrm > 0xbf)
4544 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4545 else
4546 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4547 break;
Avi Kivity46561642011-04-24 14:09:59 +03004548 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004549 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004550 }
Avi Kivity46561642011-04-24 14:09:59 +03004551
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004552 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004553 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004554 }
4555
Paolo Bonzinie24186e2014-03-27 12:00:57 +01004556 /* Unrecognised? */
4557 if (ctxt->d == 0)
4558 return EMULATION_FAILED;
4559
Avi Kivity9dac77f2011-06-01 15:34:25 +03004560 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004561
Nadav Amit3a6095a2014-08-13 16:50:13 +03004562 if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD)))
4563 return EMULATION_FAILED;
4564
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004565 if (unlikely(ctxt->d &
Nadav Amited9aad22014-11-02 11:55:00 +02004566 (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch|
4567 No16))) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004568 /*
4569 * These are copied unconditionally here, and checked unconditionally
4570 * in x86_emulate_insn.
4571 */
4572 ctxt->check_perm = opcode.check_perm;
4573 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004574
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004575 if (ctxt->d & NotImpl)
4576 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004577
Nadav Amit58b70752014-10-24 11:35:09 +03004578 if (mode == X86EMUL_MODE_PROT64) {
4579 if (ctxt->op_bytes == 4 && (ctxt->d & Stack))
4580 ctxt->op_bytes = 8;
4581 else if (ctxt->d & NearBranch)
4582 ctxt->op_bytes = 8;
4583 }
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004584
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004585 if (ctxt->d & Op3264) {
4586 if (mode == X86EMUL_MODE_PROT64)
4587 ctxt->op_bytes = 8;
4588 else
4589 ctxt->op_bytes = 4;
4590 }
4591
Nadav Amited9aad22014-11-02 11:55:00 +02004592 if ((ctxt->d & No16) && ctxt->op_bytes == 2)
4593 ctxt->op_bytes = 4;
4594
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004595 if (ctxt->d & Sse)
4596 ctxt->op_bytes = 16;
4597 else if (ctxt->d & Mmx)
4598 ctxt->op_bytes = 8;
4599 }
Avi Kivity12537912011-03-29 11:41:27 +02004600
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004601 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004602 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004603 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04004604 if (!has_seg_override) {
4605 has_seg_override = true;
4606 ctxt->seg_override = ctxt->modrm_seg;
4607 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004608 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004609 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004610 if (rc != X86EMUL_CONTINUE)
4611 goto done;
4612
Bandan Das573e80f2014-04-16 12:46:13 -04004613 if (!has_seg_override)
4614 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004615
Bandan Das573e80f2014-04-16 12:46:13 -04004616 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004617
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004618 /*
4619 * Decode and fetch the source operand: register, memory
4620 * or immediate.
4621 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004622 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004623 if (rc != X86EMUL_CONTINUE)
4624 goto done;
4625
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004626 /*
4627 * Decode and fetch the second source operand: register, memory
4628 * or immediate.
4629 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004630 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004631 if (rc != X86EMUL_CONTINUE)
4632 goto done;
4633
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004634 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03004635 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004636
Bandan Das41061cd2014-04-16 12:46:14 -04004637 if (ctxt->rip_relative)
Nadav Amit1c1c35a2014-11-19 17:43:09 +02004638 ctxt->memopp->addr.mem.ea = address_mask(ctxt,
4639 ctxt->memopp->addr.mem.ea + ctxt->_eip);
Avi Kivitycb16c342011-06-19 19:21:11 +03004640
Paolo Bonzinia430c912014-10-23 14:54:14 +02004641done:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004642 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004643}
4644
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004645bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4646{
4647 return ctxt->d & PageTable;
4648}
4649
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004650static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4651{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004652 /* The second termination condition only applies for REPE
4653 * and REPNE. Test if the repeat string operation prefix is
4654 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4655 * corresponding termination condition according to:
4656 * - if REPE/REPZ and ZF = 0 then done
4657 * - if REPNE/REPNZ and ZF = 1 then done
4658 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004659 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4660 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4661 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004662 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004663 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004664 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4665 return true;
4666
4667 return false;
4668}
4669
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004670static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4671{
4672 bool fault = false;
4673
4674 ctxt->ops->get_fpu(ctxt);
4675 asm volatile("1: fwait \n\t"
4676 "2: \n\t"
4677 ".pushsection .fixup,\"ax\" \n\t"
4678 "3: \n\t"
4679 "movb $1, %[fault] \n\t"
4680 "jmp 2b \n\t"
4681 ".popsection \n\t"
4682 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004683 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004684 ctxt->ops->put_fpu(ctxt);
4685
4686 if (unlikely(fault))
4687 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4688
4689 return X86EMUL_CONTINUE;
4690}
4691
4692static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4693 struct operand *op)
4694{
4695 if (op->type == OP_MM)
4696 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4697}
4698
Avi Kivitye28bbd42013-01-04 16:18:48 +02004699static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4700{
4701 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivityb9fa4092013-02-09 11:31:48 +02004702 if (!(ctxt->d & ByteOp))
4703 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Avi Kivitye28bbd42013-01-04 16:18:48 +02004704 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004705 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
4706 [fastop]"+S"(fop)
4707 : "c"(ctxt->src2.val));
Avi Kivitye28bbd42013-01-04 16:18:48 +02004708 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004709 if (!fop) /* exception is returned in fop variable */
4710 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02004711 return X86EMUL_CONTINUE;
4712}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004713
Bandan Das14985072014-04-16 12:46:09 -04004714void init_decode_cache(struct x86_emulate_ctxt *ctxt)
4715{
Bandan Das573e80f2014-04-16 12:46:13 -04004716 memset(&ctxt->rip_relative, 0,
4717 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04004718
Bandan Das14985072014-04-16 12:46:09 -04004719 ctxt->io_read.pos = 0;
4720 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04004721 ctxt->mem_read.end = 0;
4722}
4723
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004724int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004725{
Mathias Krause0225fb52012-08-30 01:30:16 +02004726 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004727 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004728 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004729
Avi Kivity9dac77f2011-06-01 15:34:25 +03004730 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004731
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004732 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004733 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004734 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004735 goto done;
4736 }
4737
Avi Kivity9dac77f2011-06-01 15:34:25 +03004738 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004739 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004740 goto done;
4741 }
4742
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004743 if (unlikely(ctxt->d &
4744 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
4745 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4746 (ctxt->d & Undefined)) {
4747 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004748 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004749 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004750
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004751 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4752 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
4753 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004754 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004755 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004756
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004757 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
4758 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004759 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004760 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004761
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004762 if (ctxt->d & Mmx) {
4763 rc = flush_pending_x87_faults(ctxt);
4764 if (rc != X86EMUL_CONTINUE)
4765 goto done;
4766 /*
4767 * Now that we know the fpu is exception safe, we can fetch
4768 * operands from it.
4769 */
4770 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4771 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4772 if (!(ctxt->d & Mov))
4773 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4774 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004775
Bandan Das685bbf42014-04-16 12:46:10 -04004776 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004777 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4778 X86_ICPT_PRE_EXCEPT);
4779 if (rc != X86EMUL_CONTINUE)
4780 goto done;
4781 }
4782
4783 /* Privileged instruction can be executed only in CPL=0 */
4784 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Nadav Amit68efa762014-06-18 17:19:35 +03004785 if (ctxt->d & PrivUD)
4786 rc = emulate_ud(ctxt);
4787 else
4788 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004789 goto done;
4790 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004791
4792 /* Instruction can only be executed in protected mode */
4793 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
4794 rc = emulate_ud(ctxt);
4795 goto done;
4796 }
4797
4798 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04004799 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004800 rc = ctxt->check_perm(ctxt);
4801 if (rc != X86EMUL_CONTINUE)
4802 goto done;
4803 }
4804
Bandan Das685bbf42014-04-16 12:46:10 -04004805 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004806 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4807 X86_ICPT_POST_EXCEPT);
4808 if (rc != X86EMUL_CONTINUE)
4809 goto done;
4810 }
4811
4812 if (ctxt->rep_prefix && (ctxt->d & String)) {
4813 /* All REP prefixes have the same first termination condition */
4814 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
4815 ctxt->eip = ctxt->_eip;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004816 ctxt->eflags &= ~EFLG_RF;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004817 goto done;
4818 }
4819 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004820 }
4821
Avi Kivity9dac77f2011-06-01 15:34:25 +03004822 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4823 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4824 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004825 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004826 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004827 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004828 }
4829
Avi Kivity9dac77f2011-06-01 15:34:25 +03004830 if (ctxt->src2.type == OP_MEM) {
4831 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4832 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004833 if (rc != X86EMUL_CONTINUE)
4834 goto done;
4835 }
4836
Avi Kivity9dac77f2011-06-01 15:34:25 +03004837 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004838 goto special_insn;
4839
4840
Avi Kivity9dac77f2011-06-01 15:34:25 +03004841 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004842 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004843 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4844 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004845 if (rc != X86EMUL_CONTINUE)
4846 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004847 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004848 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004849
Avi Kivity018a98d2007-11-27 19:30:56 +02004850special_insn:
4851
Bandan Das685bbf42014-04-16 12:46:10 -04004852 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004853 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004854 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004855 if (rc != X86EMUL_CONTINUE)
4856 goto done;
4857 }
4858
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004859 if (ctxt->rep_prefix && (ctxt->d & String))
4860 ctxt->eflags |= EFLG_RF;
4861 else
4862 ctxt->eflags &= ~EFLG_RF;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004863
Avi Kivity9dac77f2011-06-01 15:34:25 +03004864 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004865 if (ctxt->d & Fastop) {
4866 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4867 rc = fastop(ctxt, fop);
4868 if (rc != X86EMUL_CONTINUE)
4869 goto done;
4870 goto writeback;
4871 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004872 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004873 if (rc != X86EMUL_CONTINUE)
4874 goto done;
4875 goto writeback;
4876 }
4877
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004878 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004879 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004880 else if (ctxt->opcode_len == 3)
4881 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004882
Avi Kivity9dac77f2011-06-01 15:34:25 +03004883 switch (ctxt->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004884 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004885 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004886 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004887 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004888 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004889 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004890 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03004891 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004892 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004893 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004894 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004895 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004896 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004897 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03004898 ctxt->dst.type = OP_NONE;
4899 else
4900 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004901 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004902 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004903 switch (ctxt->op_bytes) {
4904 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4905 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4906 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004907 }
4908 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004909 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004910 rc = emulate_int(ctxt, 3);
4911 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004912 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004913 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004914 break;
4915 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004916 if (ctxt->eflags & EFLG_OF)
4917 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004918 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004919 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004920 case 0xeb: /* jmp rel short */
Nadav Amit234f3ce2014-09-18 22:39:38 +03004921 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004922 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004923 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004924 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004925 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004926 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004927 case 0xf5: /* cmc */
4928 /* complement carry flag from eflags reg */
4929 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004930 break;
4931 case 0xf8: /* clc */
4932 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004933 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004934 case 0xf9: /* stc */
4935 ctxt->eflags |= EFLG_CF;
4936 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004937 case 0xfc: /* cld */
4938 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004939 break;
4940 case 0xfd: /* std */
4941 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004942 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004943 default:
4944 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004945 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004946
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004947 if (rc != X86EMUL_CONTINUE)
4948 goto done;
4949
Avi Kivity018a98d2007-11-27 19:30:56 +02004950writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02004951 if (ctxt->d & SrcWrite) {
4952 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
4953 rc = writeback(ctxt, &ctxt->src);
4954 if (rc != X86EMUL_CONTINUE)
4955 goto done;
4956 }
Nadav Amitee212292014-06-15 16:12:58 +03004957 if (!(ctxt->d & NoWrite)) {
4958 rc = writeback(ctxt, &ctxt->dst);
4959 if (rc != X86EMUL_CONTINUE)
4960 goto done;
4961 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004962
Gleb Natapov5cd21912010-03-18 15:20:26 +02004963 /*
4964 * restore dst type in case the decoding will be reused
4965 * (happens for string instruction )
4966 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004967 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004968
Avi Kivity9dac77f2011-06-01 15:34:25 +03004969 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004970 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004971
Avi Kivity9dac77f2011-06-01 15:34:25 +03004972 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004973 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004974
Avi Kivity9dac77f2011-06-01 15:34:25 +03004975 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004976 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004977 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004978 if ((ctxt->d & SrcMask) == SrcSI)
4979 count = ctxt->src.count;
4980 else
4981 count = ctxt->dst.count;
Paolo Bonzini01485a22014-11-19 18:25:08 +01004982 register_address_increment(ctxt, VCPU_REGS_RCX, -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004983
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004984 if (!string_insn_completed(ctxt)) {
4985 /*
4986 * Re-enter guest when pio read ahead buffer is empty
4987 * or, if it is not used, after each 1024 iteration.
4988 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004989 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004990 (r->end == 0 || r->end != r->pos)) {
4991 /*
4992 * Reset read cache. Usually happens before
4993 * decode, but since instruction is restarted
4994 * we have to do it here.
4995 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004996 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004997 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004998 return EMULATION_RESTART;
4999 }
5000 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03005001 }
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03005002 ctxt->eflags &= ~EFLG_RF;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005003 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005004
Avi Kivity9dac77f2011-06-01 15:34:25 +03005005 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02005006
5007done:
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005008 if (rc == X86EMUL_PROPAGATE_FAULT) {
5009 WARN_ON(ctxt->exception.vector > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +02005010 ctxt->have_exception = true;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005011 }
Joerg Roedel775fde82011-04-04 12:39:24 +02005012 if (rc == X86EMUL_INTERCEPTED)
5013 return EMULATION_INTERCEPTED;
5014
Avi Kivitydd856ef2012-08-27 23:46:17 +03005015 if (rc == X86EMUL_CONTINUE)
5016 writeback_registers(ctxt);
5017
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005018 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005019
5020twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005021 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02005022 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02005023 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005024 break;
5025 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02005026 case 0x0d: /* GrpP (prefetch) */
5027 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02005028 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02005029 break;
5030 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005031 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02005032 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005033 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005034 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005035 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005036 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03005037 if (test_cc(ctxt->b, ctxt->eflags))
5038 ctxt->dst.val = ctxt->src.val;
5039 else if (ctxt->mode != X86EMUL_MODE_PROT64 ||
5040 ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005041 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08005042 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03005043 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03005044 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005045 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005046 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08005047 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005048 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08005049 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005050 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005051 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005052 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03005053 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005054 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005055 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005056 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005057 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03005058 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005059 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005060 default:
5061 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005062 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005063
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005064threebyte_insn:
5065
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005066 if (rc != X86EMUL_CONTINUE)
5067 goto done;
5068
Avi Kivity6aa8b732006-12-10 02:21:36 -08005069 goto writeback;
5070
5071cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02005072 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005073}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005074
5075void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
5076{
5077 invalidate_registers(ctxt);
5078}
5079
5080void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
5081{
5082 writeback_registers(ctxt);
5083}