blob: 713295d913fd431eeac5086d9024c9e0f1e869ec [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 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800170
Avi Kivity820207c2013-02-09 11:31:45 +0200171#define DstXacc (DstAccLo | SrcAccHi | SrcWrite)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800172
Avi Kivityd0e53322010-07-29 15:11:54 +0300173#define X2(x...) x, x
174#define X3(x...) X2(x), x
175#define X4(x...) X2(x), X2(x)
176#define X5(x...) X4(x), x
177#define X6(x...) X4(x), X2(x)
178#define X7(x...) X4(x), X3(x)
179#define X8(x...) X4(x), X4(x)
180#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300181
Avi Kivitye28bbd42013-01-04 16:18:48 +0200182#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
183#define FASTOP_SIZE 8
184
185/*
186 * fastop functions have a special calling convention:
187 *
Avi Kivity017da7b2013-02-09 11:31:47 +0200188 * dst: rax (in/out)
189 * src: rdx (in/out)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200190 * src2: rcx (in)
191 * flags: rflags (in/out)
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200192 * ex: rsi (in:fastop pointer, out:zero if exception)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200193 *
194 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
195 * different operand sizes can be reached by calculation, rather than a jump
196 * table (which would be bigger than the code).
197 *
198 * fastop functions are declared as taking a never-defined fastop parameter,
199 * so they can't be called from C directly.
200 */
201
202struct fastop;
203
Avi Kivityd65b1de2010-07-29 15:11:35 +0300204struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300205 u64 flags : 56;
206 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300207 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300208 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200209 const struct opcode *group;
210 const struct group_dual *gdual;
211 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200212 const struct escape *esc;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200213 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300214 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200215 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300216};
217
218struct group_dual {
219 struct opcode mod012[8];
220 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300221};
222
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200223struct gprefix {
224 struct opcode pfx_no;
225 struct opcode pfx_66;
226 struct opcode pfx_f2;
227 struct opcode pfx_f3;
228};
229
Gleb Natapov045a2822012-12-20 16:57:43 +0200230struct escape {
231 struct opcode op[8];
232 struct opcode high[64];
233};
234
Avi Kivity6aa8b732006-12-10 02:21:36 -0800235/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200236#define EFLG_ID (1<<21)
237#define EFLG_VIP (1<<20)
238#define EFLG_VIF (1<<19)
239#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200240#define EFLG_VM (1<<17)
241#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200242#define EFLG_IOPL (3<<12)
243#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800244#define EFLG_OF (1<<11)
245#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200246#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200247#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800248#define EFLG_SF (1<<7)
249#define EFLG_ZF (1<<6)
250#define EFLG_AF (1<<4)
251#define EFLG_PF (1<<2)
252#define EFLG_CF (1<<0)
253
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300254#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
255#define EFLG_RESERVED_ONE_MASK 2
256
Avi Kivitydd856ef2012-08-27 23:46:17 +0300257static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
258{
259 if (!(ctxt->regs_valid & (1 << nr))) {
260 ctxt->regs_valid |= 1 << nr;
261 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
262 }
263 return ctxt->_regs[nr];
264}
265
266static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
267{
268 ctxt->regs_valid |= 1 << nr;
269 ctxt->regs_dirty |= 1 << nr;
270 return &ctxt->_regs[nr];
271}
272
273static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
274{
275 reg_read(ctxt, nr);
276 return reg_write(ctxt, nr);
277}
278
279static void writeback_registers(struct x86_emulate_ctxt *ctxt)
280{
281 unsigned reg;
282
283 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
284 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
285}
286
287static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
288{
289 ctxt->regs_dirty = 0;
290 ctxt->regs_valid = 0;
291}
292
Avi Kivity6aa8b732006-12-10 02:21:36 -0800293/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800294 * These EFLAGS bits are restored from saved value during emulation, and
295 * any changes are written back to the saved value after emulation.
296 */
297#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
298
Avi Kivitydda96d82008-11-26 15:14:10 +0200299#ifdef CONFIG_X86_64
300#define ON64(x) x
301#else
302#define ON64(x)
303#endif
304
Avi Kivity4d758342013-01-19 19:51:55 +0200305static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *));
306
Avi Kivityb7d491e2013-01-04 16:18:49 +0200307#define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t"
308#define FOP_RET "ret \n\t"
309
310#define FOP_START(op) \
311 extern void em_##op(struct fastop *fake); \
312 asm(".pushsection .text, \"ax\" \n\t" \
313 ".global em_" #op " \n\t" \
314 FOP_ALIGN \
315 "em_" #op ": \n\t"
316
317#define FOP_END \
318 ".popsection")
319
Avi Kivity0bdea062013-01-19 19:51:50 +0200320#define FOPNOP() FOP_ALIGN FOP_RET
321
Avi Kivityb7d491e2013-01-04 16:18:49 +0200322#define FOP1E(op, dst) \
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200323 FOP_ALIGN "10: " #op " %" #dst " \n\t" FOP_RET
324
325#define FOP1EEX(op, dst) \
326 FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200327
328#define FASTOP1(op) \
329 FOP_START(op) \
330 FOP1E(op##b, al) \
331 FOP1E(op##w, ax) \
332 FOP1E(op##l, eax) \
333 ON64(FOP1E(op##q, rax)) \
334 FOP_END
335
Avi Kivityb9fa4092013-02-09 11:31:48 +0200336/* 1-operand, using src2 (for MUL/DIV r/m) */
337#define FASTOP1SRC2(op, name) \
338 FOP_START(name) \
339 FOP1E(op, cl) \
340 FOP1E(op, cx) \
341 FOP1E(op, ecx) \
342 ON64(FOP1E(op, rcx)) \
343 FOP_END
344
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200345/* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */
346#define FASTOP1SRC2EX(op, name) \
347 FOP_START(name) \
348 FOP1EEX(op, cl) \
349 FOP1EEX(op, cx) \
350 FOP1EEX(op, ecx) \
351 ON64(FOP1EEX(op, rcx)) \
352 FOP_END
353
Avi Kivityf7857f32013-01-04 16:18:53 +0200354#define FOP2E(op, dst, src) \
355 FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET
356
357#define FASTOP2(op) \
358 FOP_START(op) \
Avi Kivity017da7b2013-02-09 11:31:47 +0200359 FOP2E(op##b, al, dl) \
360 FOP2E(op##w, ax, dx) \
361 FOP2E(op##l, eax, edx) \
362 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivityf7857f32013-01-04 16:18:53 +0200363 FOP_END
364
Avi Kivity11c363b2013-01-19 19:51:54 +0200365/* 2 operand, word only */
366#define FASTOP2W(op) \
367 FOP_START(op) \
368 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200369 FOP2E(op##w, ax, dx) \
370 FOP2E(op##l, eax, edx) \
371 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivity11c363b2013-01-19 19:51:54 +0200372 FOP_END
373
Avi Kivity007a3b52013-01-19 19:51:51 +0200374/* 2 operand, src is CL */
375#define FASTOP2CL(op) \
376 FOP_START(op) \
377 FOP2E(op##b, al, cl) \
378 FOP2E(op##w, ax, cl) \
379 FOP2E(op##l, eax, cl) \
380 ON64(FOP2E(op##q, rax, cl)) \
381 FOP_END
382
Avi Kivity0bdea062013-01-19 19:51:50 +0200383#define FOP3E(op, dst, src, src2) \
384 FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
385
386/* 3-operand, word-only, src2=cl */
387#define FASTOP3WCL(op) \
388 FOP_START(op) \
389 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200390 FOP3E(op##w, ax, dx, cl) \
391 FOP3E(op##l, eax, edx, cl) \
392 ON64(FOP3E(op##q, rax, rdx, cl)) \
Avi Kivity0bdea062013-01-19 19:51:50 +0200393 FOP_END
394
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200395/* Special case for SETcc - 1 instruction per cc */
396#define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t"
397
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200398asm(".global kvm_fastop_exception \n"
399 "kvm_fastop_exception: xor %esi, %esi; ret");
400
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200401FOP_START(setcc)
402FOP_SETCC(seto)
403FOP_SETCC(setno)
404FOP_SETCC(setc)
405FOP_SETCC(setnc)
406FOP_SETCC(setz)
407FOP_SETCC(setnz)
408FOP_SETCC(setbe)
409FOP_SETCC(setnbe)
410FOP_SETCC(sets)
411FOP_SETCC(setns)
412FOP_SETCC(setp)
413FOP_SETCC(setnp)
414FOP_SETCC(setl)
415FOP_SETCC(setnl)
416FOP_SETCC(setle)
417FOP_SETCC(setnle)
418FOP_END;
419
Paolo Bonzini326f5782013-05-09 11:32:51 +0200420FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET
421FOP_END;
422
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200423static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
424 enum x86_intercept intercept,
425 enum x86_intercept_stage stage)
426{
427 struct x86_instruction_info info = {
428 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300429 .rep_prefix = ctxt->rep_prefix,
430 .modrm_mod = ctxt->modrm_mod,
431 .modrm_reg = ctxt->modrm_reg,
432 .modrm_rm = ctxt->modrm_rm,
433 .src_val = ctxt->src.val64,
Jan Kiszka6cbc5f52014-06-30 12:52:55 +0200434 .dst_val = ctxt->dst.val64,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300435 .src_bytes = ctxt->src.bytes,
436 .dst_bytes = ctxt->dst.bytes,
437 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200438 .next_rip = ctxt->eip,
439 };
440
Avi Kivity29535382011-04-20 13:37:53 +0300441 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200442}
443
Avi Kivityf47cfa32012-06-07 17:49:24 +0300444static void assign_masked(ulong *dest, ulong src, ulong mask)
445{
446 *dest = (*dest & ~mask) | (src & mask);
447}
448
Avi Kivity9dac77f2011-06-01 15:34:25 +0300449static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800450{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300451 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800452}
453
Avi Kivityf47cfa32012-06-07 17:49:24 +0300454static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
455{
456 u16 sel;
457 struct desc_struct ss;
458
459 if (ctxt->mode == X86EMUL_MODE_PROT64)
460 return ~0UL;
461 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
462 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
463}
464
Avi Kivity612e89f2012-06-12 20:03:23 +0300465static int stack_size(struct x86_emulate_ctxt *ctxt)
466{
467 return (__fls(stack_mask(ctxt)) + 1) >> 3;
468}
469
Avi Kivity6aa8b732006-12-10 02:21:36 -0800470/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800471static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300472address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800473{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300474 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800475 return reg;
476 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300477 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800478}
479
480static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300481register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800482{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300483 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800484}
485
Avi Kivity5ad105e2012-08-19 14:34:31 +0300486static void masked_increment(ulong *reg, ulong mask, int inc)
487{
488 assign_masked(reg, *reg + inc, mask);
489}
490
Harvey Harrison7a9572752008-02-19 07:40:41 -0800491static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300492register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800493{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300494 ulong mask;
495
Avi Kivity9dac77f2011-06-01 15:34:25 +0300496 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300497 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800498 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300499 mask = ad_mask(ctxt);
500 masked_increment(reg, mask, inc);
501}
502
503static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
504{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300505 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800506}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800507
Avi Kivity56697682011-04-03 14:08:51 +0300508static u32 desc_limit_scaled(struct desc_struct *desc)
509{
510 u32 limit = get_desc_limit(desc);
511
512 return desc->g ? (limit << 12) | 0xfff : limit;
513}
514
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900515static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300516{
517 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
518 return 0;
519
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900520 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300521}
522
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200523static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
524 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300525{
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +0200526 WARN_ON(vec > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +0200527 ctxt->exception.vector = vec;
528 ctxt->exception.error_code = error;
529 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200530 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300531}
532
Joerg Roedel3b88e412011-04-04 12:39:29 +0200533static int emulate_db(struct x86_emulate_ctxt *ctxt)
534{
535 return emulate_exception(ctxt, DB_VECTOR, 0, false);
536}
537
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200538static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300539{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200540 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300541}
542
Avi Kivity618ff152011-04-03 12:32:09 +0300543static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
544{
545 return emulate_exception(ctxt, SS_VECTOR, err, true);
546}
547
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200548static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300549{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200550 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300551}
552
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200553static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300554{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200555 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300556}
557
Avi Kivity34d1f492010-08-26 11:59:01 +0300558static int emulate_de(struct x86_emulate_ctxt *ctxt)
559{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200560 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300561}
562
Avi Kivity12537912011-03-29 11:41:27 +0200563static int emulate_nm(struct x86_emulate_ctxt *ctxt)
564{
565 return emulate_exception(ctxt, NM_VECTOR, 0, false);
566}
567
Nadav Amit234f3ce2014-09-18 22:39:38 +0300568static inline int assign_eip_far(struct x86_emulate_ctxt *ctxt, ulong dst,
569 int cs_l)
Nadav Amit05c83ec2014-09-18 22:39:37 +0300570{
571 switch (ctxt->op_bytes) {
572 case 2:
573 ctxt->_eip = (u16)dst;
574 break;
575 case 4:
576 ctxt->_eip = (u32)dst;
577 break;
Nadav Amitcd9b8e2c2014-10-28 00:03:43 +0200578#ifdef CONFIG_X86_64
Nadav Amit05c83ec2014-09-18 22:39:37 +0300579 case 8:
Nadav Amit234f3ce2014-09-18 22:39:38 +0300580 if ((cs_l && is_noncanonical_address(dst)) ||
Nadav Amitcd9b8e2c2014-10-28 00:03:43 +0200581 (!cs_l && (dst >> 32) != 0))
Nadav Amit234f3ce2014-09-18 22:39:38 +0300582 return emulate_gp(ctxt, 0);
Nadav Amit05c83ec2014-09-18 22:39:37 +0300583 ctxt->_eip = dst;
584 break;
Nadav Amitcd9b8e2c2014-10-28 00:03:43 +0200585#endif
Nadav Amit05c83ec2014-09-18 22:39:37 +0300586 default:
587 WARN(1, "unsupported eip assignment size\n");
588 }
Nadav Amit234f3ce2014-09-18 22:39:38 +0300589 return X86EMUL_CONTINUE;
Nadav Amit05c83ec2014-09-18 22:39:37 +0300590}
591
Nadav Amit234f3ce2014-09-18 22:39:38 +0300592static inline int assign_eip_near(struct x86_emulate_ctxt *ctxt, ulong dst)
Nadav Amit05c83ec2014-09-18 22:39:37 +0300593{
Nadav Amit234f3ce2014-09-18 22:39:38 +0300594 return assign_eip_far(ctxt, dst, ctxt->mode == X86EMUL_MODE_PROT64);
595}
596
597static inline int jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
598{
599 return assign_eip_near(ctxt, ctxt->_eip + rel);
Nadav Amit05c83ec2014-09-18 22:39:37 +0300600}
601
Avi Kivity1aa36612011-04-27 13:20:30 +0300602static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
603{
604 u16 selector;
605 struct desc_struct desc;
606
607 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
608 return selector;
609}
610
611static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
612 unsigned seg)
613{
614 u16 dummy;
615 u32 base3;
616 struct desc_struct desc;
617
618 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
619 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
620}
621
Avi Kivity1c11b372012-04-09 18:39:59 +0300622/*
623 * x86 defines three classes of vector instructions: explicitly
624 * aligned, explicitly unaligned, and the rest, which change behaviour
625 * depending on whether they're AVX encoded or not.
626 *
627 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
628 * subject to the same check.
629 */
630static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
631{
632 if (likely(size < 16))
633 return false;
634
635 if (ctxt->d & Aligned)
636 return true;
637 else if (ctxt->d & Unaligned)
638 return false;
639 else if (ctxt->d & Avx)
640 return false;
641 else
642 return true;
643}
644
Paolo Bonzinid09155d2014-10-27 14:54:44 +0100645static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
646 struct segmented_address addr,
647 unsigned *max_size, unsigned size,
648 bool write, bool fetch,
649 ulong *linear)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300650{
Avi Kivity618ff152011-04-03 12:32:09 +0300651 struct desc_struct desc;
652 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300653 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300654 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300655 u16 sel;
Gleb Natapov3a78a4f2012-12-20 16:57:42 +0200656 unsigned cpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300657
Nadav Amit518547b2014-09-30 20:49:19 +0300658 la = seg_base(ctxt, addr.seg) +
659 (fetch || ctxt->ad_bytes == 8 ? addr.ea : (u32)addr.ea);
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100660 *max_size = 0;
Avi Kivity618ff152011-04-03 12:32:09 +0300661 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300662 case X86EMUL_MODE_PROT64:
Nadav Amit4be4de72014-09-18 22:39:40 +0300663 if (is_noncanonical_address(la))
Avi Kivity618ff152011-04-03 12:32:09 +0300664 return emulate_gp(ctxt, 0);
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100665
666 *max_size = min_t(u64, ~0u, (1ull << 48) - la);
667 if (size > *max_size)
668 goto bad;
Avi Kivity618ff152011-04-03 12:32:09 +0300669 break;
670 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300671 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
672 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300673 if (!usable)
674 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200675 /* code segment in protected mode or read-only data segment */
676 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
677 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300678 goto bad;
679 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400680 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300681 goto bad;
682 lim = desc_limit_scaled(&desc);
Nadav Amit10e38fc2014-06-18 17:19:34 +0300683 if ((ctxt->mode == X86EMUL_MODE_REAL) && !fetch &&
684 (ctxt->d & NoBigReal)) {
685 /* la is between zero and 0xffff */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100686 if (la > 0xffff)
Nadav Amit10e38fc2014-06-18 17:19:34 +0300687 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100688 *max_size = 0x10000 - la;
Nadav Amit10e38fc2014-06-18 17:19:34 +0300689 } else if ((desc.type & 8) || !(desc.type & 4)) {
Avi Kivity618ff152011-04-03 12:32:09 +0300690 /* expand-up segment */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100691 if (addr.ea > lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300692 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100693 *max_size = min_t(u64, ~0u, (u64)lim + 1 - addr.ea);
Avi Kivity618ff152011-04-03 12:32:09 +0300694 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800695 /* expand-down segment */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100696 if (addr.ea <= lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300697 goto bad;
698 lim = desc.d ? 0xffffffff : 0xffff;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100699 if (addr.ea > lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300700 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100701 *max_size = min_t(u64, ~0u, (u64)lim + 1 - addr.ea);
Avi Kivity618ff152011-04-03 12:32:09 +0300702 }
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100703 if (size > *max_size)
704 goto bad;
Avi Kivity717746e2011-04-20 13:37:53 +0300705 cpl = ctxt->ops->cpl(ctxt);
Nadav Amitc49c7592014-10-03 01:10:03 +0300706 if (!fetch) {
707 /* data segment or readable code segment */
Avi Kivity618ff152011-04-03 12:32:09 +0300708 if (cpl > desc.dpl)
709 goto bad;
710 } else if ((desc.type & 8) && !(desc.type & 4)) {
711 /* nonconforming code segment */
712 if (cpl != desc.dpl)
713 goto bad;
714 } else if ((desc.type & 8) && (desc.type & 4)) {
715 /* conforming code segment */
716 if (cpl < desc.dpl)
717 goto bad;
718 }
719 break;
720 }
Nadav Amit518547b2014-09-30 20:49:19 +0300721 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300722 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300723 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
724 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300725 *linear = la;
726 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300727bad:
728 if (addr.seg == VCPU_SREG_SS)
Paolo Bonzini36061892014-10-27 14:40:49 +0100729 return emulate_ss(ctxt, 0);
Avi Kivity618ff152011-04-03 12:32:09 +0300730 else
Paolo Bonzini36061892014-10-27 14:40:49 +0100731 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300732}
733
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400734static int linearize(struct x86_emulate_ctxt *ctxt,
735 struct segmented_address addr,
736 unsigned size, bool write,
737 ulong *linear)
738{
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100739 unsigned max_size;
740 return __linearize(ctxt, addr, &max_size, size, write, false, linear);
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400741}
742
743
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200744static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
745 struct segmented_address addr,
746 void *data,
747 unsigned size)
748{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200749 int rc;
750 ulong linear;
751
Avi Kivity83b87952011-04-03 11:31:19 +0300752 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200753 if (rc != X86EMUL_CONTINUE)
754 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300755 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200756}
757
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900758/*
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200759 * Prefetch the remaining bytes of the instruction without crossing page
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900760 * boundary if they are not in fetch_cache yet.
761 */
Paolo Bonzini9506d572014-05-06 13:05:25 +0200762static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
Avi Kivity62266862007-11-20 13:15:52 +0200763{
Avi Kivity62266862007-11-20 13:15:52 +0200764 int rc;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100765 unsigned size, max_size;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200766 unsigned long linear;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200767 int cur_size = ctxt->fetch.end - ctxt->fetch.data;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200768 struct segmented_address addr = { .seg = VCPU_SREG_CS,
Paolo Bonzini17052f12014-05-06 16:33:01 +0200769 .ea = ctxt->eip + cur_size };
770
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100771 /*
772 * We do not know exactly how many bytes will be needed, and
773 * __linearize is expensive, so fetch as much as possible. We
774 * just have to avoid going beyond the 15 byte limit, the end
775 * of the segment, or the end of the page.
776 *
777 * __linearize is called with size 0 so that it does not do any
778 * boundary check itself. Instead, we use max_size to check
779 * against op_size.
780 */
781 rc = __linearize(ctxt, addr, &max_size, 0, false, true, &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
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200916static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300917{
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200918 u8 rc;
919 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300920
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200921 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +0200922 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200923 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
924 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300925}
926
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300927static void fetch_register_operand(struct operand *op)
928{
929 switch (op->bytes) {
930 case 1:
931 op->val = *(u8 *)op->addr.reg;
932 break;
933 case 2:
934 op->val = *(u16 *)op->addr.reg;
935 break;
936 case 4:
937 op->val = *(u32 *)op->addr.reg;
938 break;
939 case 8:
940 op->val = *(u64 *)op->addr.reg;
941 break;
942 }
943}
944
Avi Kivity12537912011-03-29 11:41:27 +0200945static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
946{
947 ctxt->ops->get_fpu(ctxt);
948 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200949 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
950 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
951 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
952 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
953 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
954 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
955 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
956 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200957#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200958 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
959 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
960 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
961 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
962 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
963 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
964 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
965 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200966#endif
967 default: BUG();
968 }
969 ctxt->ops->put_fpu(ctxt);
970}
971
972static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
973 int reg)
974{
975 ctxt->ops->get_fpu(ctxt);
976 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200977 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
978 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
979 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
980 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
981 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
982 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
983 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
984 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200985#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200986 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
987 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
988 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
989 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
990 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
991 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
992 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
993 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200994#endif
995 default: BUG();
996 }
997 ctxt->ops->put_fpu(ctxt);
998}
999
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001000static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1001{
1002 ctxt->ops->get_fpu(ctxt);
1003 switch (reg) {
1004 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1005 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1006 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1007 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1008 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1009 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1010 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1011 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1012 default: BUG();
1013 }
1014 ctxt->ops->put_fpu(ctxt);
1015}
1016
1017static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1018{
1019 ctxt->ops->get_fpu(ctxt);
1020 switch (reg) {
1021 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1022 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1023 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1024 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1025 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1026 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1027 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1028 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1029 default: BUG();
1030 }
1031 ctxt->ops->put_fpu(ctxt);
1032}
1033
Gleb Natapov045a2822012-12-20 16:57:43 +02001034static int em_fninit(struct x86_emulate_ctxt *ctxt)
1035{
1036 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1037 return emulate_nm(ctxt);
1038
1039 ctxt->ops->get_fpu(ctxt);
1040 asm volatile("fninit");
1041 ctxt->ops->put_fpu(ctxt);
1042 return X86EMUL_CONTINUE;
1043}
1044
1045static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1046{
1047 u16 fcw;
1048
1049 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1050 return emulate_nm(ctxt);
1051
1052 ctxt->ops->get_fpu(ctxt);
1053 asm volatile("fnstcw %0": "+m"(fcw));
1054 ctxt->ops->put_fpu(ctxt);
1055
1056 /* force 2 byte destination */
1057 ctxt->dst.bytes = 2;
1058 ctxt->dst.val = fcw;
1059
1060 return X86EMUL_CONTINUE;
1061}
1062
1063static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1064{
1065 u16 fsw;
1066
1067 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1068 return emulate_nm(ctxt);
1069
1070 ctxt->ops->get_fpu(ctxt);
1071 asm volatile("fnstsw %0": "+m"(fsw));
1072 ctxt->ops->put_fpu(ctxt);
1073
1074 /* force 2 byte destination */
1075 ctxt->dst.bytes = 2;
1076 ctxt->dst.val = fsw;
1077
1078 return X86EMUL_CONTINUE;
1079}
1080
Avi Kivity12537912011-03-29 11:41:27 +02001081static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001082 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001083{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001084 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001085
Avi Kivity9dac77f2011-06-01 15:34:25 +03001086 if (!(ctxt->d & ModRM))
1087 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +02001088
Avi Kivity9dac77f2011-06-01 15:34:25 +03001089 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001090 op->type = OP_XMM;
1091 op->bytes = 16;
1092 op->addr.xmm = reg;
1093 read_sse_reg(ctxt, &op->vec_val, reg);
1094 return;
1095 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001096 if (ctxt->d & Mmx) {
1097 reg &= 7;
1098 op->type = OP_MM;
1099 op->bytes = 8;
1100 op->addr.mm = reg;
1101 return;
1102 }
Avi Kivity12537912011-03-29 11:41:27 +02001103
Avi Kivity3c118e22007-10-31 10:27:04 +02001104 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001105 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1106 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1107
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001108 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001109 op->orig_val = op->val;
1110}
1111
Avi Kivitya6e34072012-06-10 17:15:39 +03001112static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1113{
1114 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1115 ctxt->modrm_seg = VCPU_SREG_SS;
1116}
1117
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001118static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001119 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001120{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001121 u8 sib;
Bandan Das02357bd2014-04-16 12:46:11 -04001122 int index_reg, base_reg, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001123 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001124 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001125
Bandan Das02357bd2014-04-16 12:46:11 -04001126 ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */
1127 index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */
1128 base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001129
Bandan Das02357bd2014-04-16 12:46:11 -04001130 ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001131 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
Bandan Das02357bd2014-04-16 12:46:11 -04001132 ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001133 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001134
Nadav Amit9b88ae92014-05-25 23:05:21 +03001135 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001136 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001137 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001138 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001139 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001140 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001141 op->type = OP_XMM;
1142 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001143 op->addr.xmm = ctxt->modrm_rm;
1144 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001145 return rc;
1146 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001147 if (ctxt->d & Mmx) {
1148 op->type = OP_MM;
1149 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001150 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001151 return rc;
1152 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001153 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001154 return rc;
1155 }
1156
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001157 op->type = OP_MEM;
1158
Avi Kivity9dac77f2011-06-01 15:34:25 +03001159 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001160 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1161 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1162 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1163 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001164
1165 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001166 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001167 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001168 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001169 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001170 break;
1171 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001172 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001173 break;
1174 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001175 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001176 break;
1177 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001178 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001179 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001180 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001181 break;
1182 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001183 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001184 break;
1185 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001186 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001187 break;
1188 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001189 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001190 break;
1191 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001192 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001193 break;
1194 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001195 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001196 break;
1197 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001198 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001199 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001200 break;
1201 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001202 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001203 break;
1204 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001205 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1206 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1207 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001208 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001209 } else {
1210 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001211 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001212 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001213 index_reg |= (sib >> 3) & 7;
1214 base_reg |= sib & 7;
1215 scale = sib >> 6;
1216
Avi Kivity9dac77f2011-06-01 15:34:25 +03001217 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001218 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001219 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001220 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001221 adjust_modrm_seg(ctxt, base_reg);
1222 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001223 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001224 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001225 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Nadav Amit5b38ab82014-11-02 11:54:41 +02001226 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity84411d82008-06-15 21:53:26 -07001227 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001228 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001229 } else {
1230 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001231 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001232 adjust_modrm_seg(ctxt, base_reg);
1233 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001234 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001235 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001236 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001237 break;
1238 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001239 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001240 break;
1241 }
1242 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001243 op->addr.mem.ea = modrm_ea;
Bandan Das41061cd2014-04-16 12:46:14 -04001244 if (ctxt->ad_bytes != 8)
1245 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
1246
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001247done:
1248 return rc;
1249}
1250
1251static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001252 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001253{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001254 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001255
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001256 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001257 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001258 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001259 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001260 break;
1261 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001262 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001263 break;
1264 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001265 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001266 break;
1267 }
1268done:
1269 return rc;
1270}
1271
Avi Kivity9dac77f2011-06-01 15:34:25 +03001272static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001273{
Sheng Yang7129eec2010-09-28 16:33:32 +08001274 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001275
Avi Kivity9dac77f2011-06-01 15:34:25 +03001276 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001277 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001278
Avi Kivity9dac77f2011-06-01 15:34:25 +03001279 if (ctxt->src.bytes == 2)
1280 sv = (s16)ctxt->src.val & (s16)mask;
1281 else if (ctxt->src.bytes == 4)
1282 sv = (s32)ctxt->src.val & (s32)mask;
Nadav Amit7dec5602014-06-15 16:12:57 +03001283 else
1284 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001285
Avi Kivity9dac77f2011-06-01 15:34:25 +03001286 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001287 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001288
1289 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001290 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001291}
1292
Gleb Natapov9de41572010-04-28 19:15:22 +03001293static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001294 unsigned long addr, void *dest, unsigned size)
1295{
1296 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001297 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001298
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001299 if (mc->pos < mc->end)
1300 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001301
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001302 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001303
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001304 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1305 &ctxt->exception);
1306 if (rc != X86EMUL_CONTINUE)
1307 return rc;
1308
1309 mc->end += size;
1310
1311read_cached:
1312 memcpy(dest, mc->data + mc->pos, size);
1313 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001314 return X86EMUL_CONTINUE;
1315}
1316
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001317static int segmented_read(struct x86_emulate_ctxt *ctxt,
1318 struct segmented_address addr,
1319 void *data,
1320 unsigned size)
1321{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001322 int rc;
1323 ulong linear;
1324
Avi Kivity83b87952011-04-03 11:31:19 +03001325 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001326 if (rc != X86EMUL_CONTINUE)
1327 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001328 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001329}
1330
1331static int segmented_write(struct x86_emulate_ctxt *ctxt,
1332 struct segmented_address addr,
1333 const void *data,
1334 unsigned size)
1335{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001336 int rc;
1337 ulong linear;
1338
Avi Kivity83b87952011-04-03 11:31:19 +03001339 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001340 if (rc != X86EMUL_CONTINUE)
1341 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001342 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1343 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001344}
1345
1346static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1347 struct segmented_address addr,
1348 const void *orig_data, const void *data,
1349 unsigned size)
1350{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001351 int rc;
1352 ulong linear;
1353
Avi Kivity83b87952011-04-03 11:31:19 +03001354 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001355 if (rc != X86EMUL_CONTINUE)
1356 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001357 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1358 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001359}
1360
Gleb Natapov7b262e92010-03-18 15:20:27 +02001361static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001362 unsigned int size, unsigned short port,
1363 void *dest)
1364{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001365 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001366
1367 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001368 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001369 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001370 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001371 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001372 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1373 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Mark Rustadb55a8142014-07-25 06:27:05 -07001374 n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001375 if (n == 0)
1376 n = 1;
1377 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001378 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001379 return 0;
1380 rc->end = n * size;
1381 }
1382
Nadav Amite6e39f02014-04-18 03:35:10 +03001383 if (ctxt->rep_prefix && (ctxt->d & String) &&
1384 !(ctxt->eflags & EFLG_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001385 ctxt->dst.data = rc->data + rc->pos;
1386 ctxt->dst.type = OP_MEM_STR;
1387 ctxt->dst.count = (rc->end - rc->pos) / size;
1388 rc->pos = rc->end;
1389 } else {
1390 memcpy(dest, rc->data + rc->pos, size);
1391 rc->pos += size;
1392 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001393 return 1;
1394}
1395
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001396static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1397 u16 index, struct desc_struct *desc)
1398{
1399 struct desc_ptr dt;
1400 ulong addr;
1401
1402 ctxt->ops->get_idt(ctxt, &dt);
1403
1404 if (dt.size < index * 8 + 7)
1405 return emulate_gp(ctxt, index << 3 | 0x2);
1406
1407 addr = dt.address + index * 8;
1408 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1409 &ctxt->exception);
1410}
1411
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001412static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001413 u16 selector, struct desc_ptr *dt)
1414{
Mathias Krause0225fb52012-08-30 01:30:16 +02001415 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001416 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001417
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001418 if (selector & 1 << 2) {
1419 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001420 u16 sel;
1421
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001422 memset (dt, 0, sizeof *dt);
Nadav Amit2eedcac2014-06-02 18:34:05 +03001423 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1424 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001425 return;
1426
1427 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001428 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001429 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001430 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001431}
1432
1433/* allowed just for 8 bytes segments */
1434static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001435 u16 selector, struct desc_struct *desc,
1436 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001437{
1438 struct desc_ptr dt;
1439 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001440 ulong addr;
1441
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001442 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001443
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001444 if (dt.size < index * 8 + 7)
1445 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001446
Avi Kivitye9194642012-06-13 16:29:39 +03001447 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001448 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1449 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001450}
1451
1452/* allowed just for 8 bytes segments */
1453static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001454 u16 selector, struct desc_struct *desc)
1455{
1456 struct desc_ptr dt;
1457 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001458 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001459
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001460 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001461
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001462 if (dt.size < index * 8 + 7)
1463 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001464
1465 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001466 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1467 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001468}
1469
Gleb Natapov5601d052011-03-07 14:55:06 +02001470/* Does not support long mode */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001471static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Nadav Amitd1442d82014-09-18 22:39:39 +03001472 u16 selector, int seg, u8 cpl,
1473 bool in_task_switch,
1474 struct desc_struct *desc)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001475{
Avi Kivity869be992012-06-13 16:30:53 +03001476 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001477 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001478 unsigned err_vec = GP_VECTOR;
1479 u32 err_code = 0;
1480 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001481 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001482 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001483 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001484 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001485
1486 memset(&seg_desc, 0, sizeof seg_desc);
1487
Kevin Wolff8da94e2013-04-11 14:06:03 +02001488 if (ctxt->mode == X86EMUL_MODE_REAL) {
1489 /* set real mode segment descriptor (keep limit etc. for
1490 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001491 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001492 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001493 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001494 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1495 /* VM86 needs a clean new segment descriptor */
1496 set_desc_base(&seg_desc, selector << 4);
1497 set_desc_limit(&seg_desc, 0xffff);
1498 seg_desc.type = 3;
1499 seg_desc.p = 1;
1500 seg_desc.s = 1;
1501 seg_desc.dpl = 3;
1502 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001503 }
1504
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001505 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001506
1507 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1508 if ((seg == VCPU_SREG_CS
1509 || (seg == VCPU_SREG_SS
1510 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1511 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001512 && null_selector)
1513 goto exception;
1514
1515 /* TR should be in GDT only */
1516 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1517 goto exception;
1518
1519 if (null_selector) /* for NULL selector skip all following checks */
1520 goto load;
1521
Avi Kivitye9194642012-06-13 16:29:39 +03001522 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001523 if (ret != X86EMUL_CONTINUE)
1524 return ret;
1525
1526 err_code = selector & 0xfffc;
Paolo Bonzini15fc0752014-08-18 13:17:00 +02001527 err_vec = in_task_switch ? TS_VECTOR : GP_VECTOR;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001528
Guo Chaofc058682012-06-28 15:19:51 +08001529 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001530 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1531 goto exception;
1532
1533 if (!seg_desc.p) {
1534 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1535 goto exception;
1536 }
1537
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001538 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001539
1540 switch (seg) {
1541 case VCPU_SREG_SS:
1542 /*
1543 * segment is not a writable data segment or segment
1544 * selector's RPL != CPL or segment selector's RPL != CPL
1545 */
1546 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1547 goto exception;
1548 break;
1549 case VCPU_SREG_CS:
1550 if (!(seg_desc.type & 8))
1551 goto exception;
1552
1553 if (seg_desc.type & 4) {
1554 /* conforming */
1555 if (dpl > cpl)
1556 goto exception;
1557 } else {
1558 /* nonconforming */
1559 if (rpl > cpl || dpl != cpl)
1560 goto exception;
1561 }
Nadav Amit040c8dc2014-09-18 22:39:43 +03001562 /* in long-mode d/b must be clear if l is set */
1563 if (seg_desc.d && seg_desc.l) {
1564 u64 efer = 0;
1565
1566 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1567 if (efer & EFER_LMA)
1568 goto exception;
1569 }
1570
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001571 /* CS(RPL) <- CPL */
1572 selector = (selector & 0xfffc) | cpl;
1573 break;
1574 case VCPU_SREG_TR:
1575 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1576 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001577 old_desc = seg_desc;
1578 seg_desc.type |= 2; /* busy */
1579 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1580 sizeof(seg_desc), &ctxt->exception);
1581 if (ret != X86EMUL_CONTINUE)
1582 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001583 break;
1584 case VCPU_SREG_LDTR:
1585 if (seg_desc.s || seg_desc.type != 2)
1586 goto exception;
1587 break;
1588 default: /* DS, ES, FS, or GS */
1589 /*
1590 * segment is not a data or readable code segment or
1591 * ((segment is a data or nonconforming code segment)
1592 * and (both RPL and CPL > DPL))
1593 */
1594 if ((seg_desc.type & 0xa) == 0x8 ||
1595 (((seg_desc.type & 0xc) != 0xc) &&
1596 (rpl > dpl && cpl > dpl)))
1597 goto exception;
1598 break;
1599 }
1600
1601 if (seg_desc.s) {
1602 /* mark segment as accessed */
1603 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001604 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001605 if (ret != X86EMUL_CONTINUE)
1606 return ret;
Nadav Amite37a75a2014-06-02 18:34:04 +03001607 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
1608 ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3,
1609 sizeof(base3), &ctxt->exception);
1610 if (ret != X86EMUL_CONTINUE)
1611 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001612 }
1613load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001614 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Nadav Amitd1442d82014-09-18 22:39:39 +03001615 if (desc)
1616 *desc = seg_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001617 return X86EMUL_CONTINUE;
1618exception:
Paolo Bonzini592f0852014-08-20 10:05:08 +02001619 return emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001620}
1621
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001622static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1623 u16 selector, int seg)
1624{
1625 u8 cpl = ctxt->ops->cpl(ctxt);
Nadav Amitd1442d82014-09-18 22:39:39 +03001626 return __load_segment_descriptor(ctxt, selector, seg, cpl, false, NULL);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001627}
1628
Wei Yongjun31be40b2010-08-17 09:17:30 +08001629static void write_register_operand(struct operand *op)
1630{
1631 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1632 switch (op->bytes) {
1633 case 1:
1634 *(u8 *)op->addr.reg = (u8)op->val;
1635 break;
1636 case 2:
1637 *(u16 *)op->addr.reg = (u16)op->val;
1638 break;
1639 case 4:
1640 *op->addr.reg = (u32)op->val;
1641 break; /* 64b: zero-extend */
1642 case 8:
1643 *op->addr.reg = op->val;
1644 break;
1645 }
1646}
1647
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001648static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001649{
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001650 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001651 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001652 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001653 break;
1654 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001655 if (ctxt->lock_prefix)
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001656 return segmented_cmpxchg(ctxt,
1657 op->addr.mem,
1658 &op->orig_val,
1659 &op->val,
1660 op->bytes);
1661 else
1662 return segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001663 op->addr.mem,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001664 &op->val,
1665 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001666 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001667 case OP_MEM_STR:
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001668 return segmented_write(ctxt,
1669 op->addr.mem,
1670 op->data,
1671 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001672 break;
Avi Kivity12537912011-03-29 11:41:27 +02001673 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001674 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001675 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001676 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001677 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001678 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001679 case OP_NONE:
1680 /* no writeback */
1681 break;
1682 default:
1683 break;
1684 }
1685 return X86EMUL_CONTINUE;
1686}
1687
Avi Kivity51ddff52012-06-12 20:19:40 +03001688static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001689{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001690 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001691
Avi Kivity5ad105e2012-08-19 14:34:31 +03001692 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001693 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001694 addr.seg = VCPU_SREG_SS;
1695
Avi Kivity51ddff52012-06-12 20:19:40 +03001696 return segmented_write(ctxt, addr, data, bytes);
1697}
1698
1699static int em_push(struct x86_emulate_ctxt *ctxt)
1700{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001701 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001702 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001703 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001704}
1705
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001706static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001707 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001708{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001709 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001710 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001711
Avi Kivitydd856ef2012-08-27 23:46:17 +03001712 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001713 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001714 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001715 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001716 return rc;
1717
Avi Kivity5ad105e2012-08-19 14:34:31 +03001718 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001719 return rc;
1720}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001721
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001722static int em_pop(struct x86_emulate_ctxt *ctxt)
1723{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001724 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001725}
1726
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001727static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001728 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001729{
1730 int rc;
1731 unsigned long val, change_mask;
1732 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001733 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001734
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001735 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001736 if (rc != X86EMUL_CONTINUE)
1737 return rc;
1738
1739 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
Nadav Amit163b1352014-07-21 14:37:28 +03001740 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_AC | EFLG_ID;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001741
1742 switch(ctxt->mode) {
1743 case X86EMUL_MODE_PROT64:
1744 case X86EMUL_MODE_PROT32:
1745 case X86EMUL_MODE_PROT16:
1746 if (cpl == 0)
1747 change_mask |= EFLG_IOPL;
1748 if (cpl <= iopl)
1749 change_mask |= EFLG_IF;
1750 break;
1751 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001752 if (iopl < 3)
1753 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001754 change_mask |= EFLG_IF;
1755 break;
1756 default: /* real mode */
1757 change_mask |= (EFLG_IOPL | EFLG_IF);
1758 break;
1759 }
1760
1761 *(unsigned long *)dest =
1762 (ctxt->eflags & ~change_mask) | (val & change_mask);
1763
1764 return rc;
1765}
1766
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001767static int em_popf(struct x86_emulate_ctxt *ctxt)
1768{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001769 ctxt->dst.type = OP_REG;
1770 ctxt->dst.addr.reg = &ctxt->eflags;
1771 ctxt->dst.bytes = ctxt->op_bytes;
1772 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001773}
1774
Avi Kivity612e89f2012-06-12 20:03:23 +03001775static int em_enter(struct x86_emulate_ctxt *ctxt)
1776{
1777 int rc;
1778 unsigned frame_size = ctxt->src.val;
1779 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001780 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001781
1782 if (nesting_level)
1783 return X86EMUL_UNHANDLEABLE;
1784
Avi Kivitydd856ef2012-08-27 23:46:17 +03001785 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1786 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001787 if (rc != X86EMUL_CONTINUE)
1788 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001789 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001790 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001791 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1792 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001793 stack_mask(ctxt));
1794 return X86EMUL_CONTINUE;
1795}
1796
Avi Kivityf47cfa32012-06-07 17:49:24 +03001797static int em_leave(struct x86_emulate_ctxt *ctxt)
1798{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001799 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001800 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001801 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001802}
1803
Avi Kivity1cd196e2011-09-13 10:45:51 +03001804static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001805{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001806 int seg = ctxt->src2.val;
1807
Avi Kivity9dac77f2011-06-01 15:34:25 +03001808 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001809
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001810 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001811}
1812
Avi Kivity1cd196e2011-09-13 10:45:51 +03001813static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001814{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001815 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001816 unsigned long selector;
1817 int rc;
1818
Avi Kivity9dac77f2011-06-01 15:34:25 +03001819 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001820 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001821 return rc;
1822
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001823 if (ctxt->modrm_reg == VCPU_SREG_SS)
1824 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
1825
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001826 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001827 return rc;
1828}
1829
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001830static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001831{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001832 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001833 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001834 int reg = VCPU_REGS_RAX;
1835
1836 while (reg <= VCPU_REGS_RDI) {
1837 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001838 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001839
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001840 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001841 if (rc != X86EMUL_CONTINUE)
1842 return rc;
1843
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001844 ++reg;
1845 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001846
Wei Yongjunc37eda12010-06-15 09:03:33 +08001847 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001848}
1849
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001850static int em_pushf(struct x86_emulate_ctxt *ctxt)
1851{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001852 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001853 return em_push(ctxt);
1854}
1855
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001856static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001857{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001858 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001859 int reg = VCPU_REGS_RDI;
1860
1861 while (reg >= VCPU_REGS_RAX) {
1862 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001863 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001864 --reg;
1865 }
1866
Avi Kivitydd856ef2012-08-27 23:46:17 +03001867 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001868 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001869 break;
1870 --reg;
1871 }
1872 return rc;
1873}
1874
Avi Kivitydd856ef2012-08-27 23:46:17 +03001875static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001876{
Mathias Krause0225fb52012-08-30 01:30:16 +02001877 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001878 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001879 struct desc_ptr dt;
1880 gva_t cs_addr;
1881 gva_t eip_addr;
1882 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001883
1884 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001885 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001886 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001887 if (rc != X86EMUL_CONTINUE)
1888 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001889
1890 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1891
Avi Kivity9dac77f2011-06-01 15:34:25 +03001892 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001893 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001894 if (rc != X86EMUL_CONTINUE)
1895 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001896
Avi Kivity9dac77f2011-06-01 15:34:25 +03001897 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001898 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001899 if (rc != X86EMUL_CONTINUE)
1900 return rc;
1901
Avi Kivity4bff1e862011-04-20 13:37:53 +03001902 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001903
1904 eip_addr = dt.address + (irq << 2);
1905 cs_addr = dt.address + (irq << 2) + 2;
1906
Avi Kivity0f65dd72011-04-20 13:37:53 +03001907 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001908 if (rc != X86EMUL_CONTINUE)
1909 return rc;
1910
Avi Kivity0f65dd72011-04-20 13:37:53 +03001911 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001912 if (rc != X86EMUL_CONTINUE)
1913 return rc;
1914
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001915 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001916 if (rc != X86EMUL_CONTINUE)
1917 return rc;
1918
Avi Kivity9dac77f2011-06-01 15:34:25 +03001919 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001920
1921 return rc;
1922}
1923
Avi Kivitydd856ef2012-08-27 23:46:17 +03001924int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1925{
1926 int rc;
1927
1928 invalidate_registers(ctxt);
1929 rc = __emulate_int_real(ctxt, irq);
1930 if (rc == X86EMUL_CONTINUE)
1931 writeback_registers(ctxt);
1932 return rc;
1933}
1934
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001935static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001936{
1937 switch(ctxt->mode) {
1938 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001939 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001940 case X86EMUL_MODE_VM86:
1941 case X86EMUL_MODE_PROT16:
1942 case X86EMUL_MODE_PROT32:
1943 case X86EMUL_MODE_PROT64:
1944 default:
1945 /* Protected mode interrupts unimplemented yet */
1946 return X86EMUL_UNHANDLEABLE;
1947 }
1948}
1949
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001950static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001951{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001952 int rc = X86EMUL_CONTINUE;
1953 unsigned long temp_eip = 0;
1954 unsigned long temp_eflags = 0;
1955 unsigned long cs = 0;
1956 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1957 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1958 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1959 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1960
1961 /* TODO: Add stack limit check */
1962
Avi Kivity9dac77f2011-06-01 15:34:25 +03001963 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001964
1965 if (rc != X86EMUL_CONTINUE)
1966 return rc;
1967
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001968 if (temp_eip & ~0xffff)
1969 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001970
Avi Kivity9dac77f2011-06-01 15:34:25 +03001971 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001972
1973 if (rc != X86EMUL_CONTINUE)
1974 return rc;
1975
Avi Kivity9dac77f2011-06-01 15:34:25 +03001976 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001977
1978 if (rc != X86EMUL_CONTINUE)
1979 return rc;
1980
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001981 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
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 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001987
1988
Avi Kivity9dac77f2011-06-01 15:34:25 +03001989 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001990 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001991 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001992 ctxt->eflags &= ~0xffff;
1993 ctxt->eflags |= temp_eflags;
1994 }
1995
1996 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1997 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1998
1999 return rc;
2000}
2001
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002002static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002003{
2004 switch(ctxt->mode) {
2005 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002006 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002007 case X86EMUL_MODE_VM86:
2008 case X86EMUL_MODE_PROT16:
2009 case X86EMUL_MODE_PROT32:
2010 case X86EMUL_MODE_PROT64:
2011 default:
2012 /* iret from protected mode unimplemented yet */
2013 return X86EMUL_UNHANDLEABLE;
2014 }
2015}
2016
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002017static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2018{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002019 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002020 unsigned short sel, old_sel;
2021 struct desc_struct old_desc, new_desc;
2022 const struct x86_emulate_ops *ops = ctxt->ops;
2023 u8 cpl = ctxt->ops->cpl(ctxt);
2024
2025 /* Assignment of RIP may only fail in 64-bit mode */
2026 if (ctxt->mode == X86EMUL_MODE_PROT64)
2027 ops->get_segment(ctxt, &old_sel, &old_desc, NULL,
2028 VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002029
Avi Kivity9dac77f2011-06-01 15:34:25 +03002030 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002031
Nadav Amitd1442d82014-09-18 22:39:39 +03002032 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl, false,
2033 &new_desc);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002034 if (rc != X86EMUL_CONTINUE)
2035 return rc;
2036
Nadav Amitd1442d82014-09-18 22:39:39 +03002037 rc = assign_eip_far(ctxt, ctxt->src.val, new_desc.l);
2038 if (rc != X86EMUL_CONTINUE) {
Nadav Amitcd9b8e2c2014-10-28 00:03:43 +02002039 WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64);
Nadav Amitd1442d82014-09-18 22:39:39 +03002040 /* assigning eip failed; restore the old cs */
2041 ops->set_segment(ctxt, old_sel, &old_desc, 0, VCPU_SREG_CS);
2042 return rc;
2043 }
2044 return rc;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002045}
2046
Nadav Amitf7784042014-09-18 22:39:41 +03002047static int em_jmp_abs(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002048{
Nadav Amitf7784042014-09-18 22:39:41 +03002049 return assign_eip_near(ctxt, ctxt->src.val);
2050}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002051
Nadav Amitf7784042014-09-18 22:39:41 +03002052static int em_call_near_abs(struct x86_emulate_ctxt *ctxt)
2053{
2054 int rc;
2055 long int old_eip;
2056
2057 old_eip = ctxt->_eip;
2058 rc = assign_eip_near(ctxt, ctxt->src.val);
2059 if (rc != X86EMUL_CONTINUE)
2060 return rc;
2061 ctxt->src.val = old_eip;
2062 rc = em_push(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002063 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002064}
2065
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002066static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002067{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002068 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002069
Nadav Amitaaa05f22014-06-02 18:34:10 +03002070 if (ctxt->dst.bytes == 16)
2071 return X86EMUL_UNHANDLEABLE;
2072
Avi Kivitydd856ef2012-08-27 23:46:17 +03002073 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2074 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2075 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2076 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002077 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002078 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002079 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2080 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002081
Laurent Vivier05f086f2007-09-24 11:10:55 +02002082 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002083 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002084 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002085}
2086
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002087static int em_ret(struct x86_emulate_ctxt *ctxt)
2088{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002089 int rc;
2090 unsigned long eip;
2091
2092 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
2093 if (rc != X86EMUL_CONTINUE)
2094 return rc;
2095
2096 return assign_eip_near(ctxt, eip);
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002097}
2098
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002099static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002100{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002101 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002102 unsigned long eip, cs;
2103 u16 old_cs;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002104 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amitd1442d82014-09-18 22:39:39 +03002105 struct desc_struct old_desc, new_desc;
2106 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002107
Nadav Amitd1442d82014-09-18 22:39:39 +03002108 if (ctxt->mode == X86EMUL_MODE_PROT64)
2109 ops->get_segment(ctxt, &old_cs, &old_desc, NULL,
2110 VCPU_SREG_CS);
2111
2112 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002113 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002114 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002115 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002116 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002117 return rc;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002118 /* Outer-privilege level return is not implemented */
2119 if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2120 return X86EMUL_UNHANDLEABLE;
Nadav Amitd1442d82014-09-18 22:39:39 +03002121 rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, 0, false,
2122 &new_desc);
2123 if (rc != X86EMUL_CONTINUE)
2124 return rc;
2125 rc = assign_eip_far(ctxt, eip, new_desc.l);
2126 if (rc != X86EMUL_CONTINUE) {
Nadav Amitcd9b8e2c2014-10-28 00:03:43 +02002127 WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64);
Nadav Amitd1442d82014-09-18 22:39:39 +03002128 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
2129 }
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002130 return rc;
2131}
2132
Bruce Rogers32611072013-09-09 09:40:20 -06002133static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2134{
2135 int rc;
2136
2137 rc = em_ret_far(ctxt);
2138 if (rc != X86EMUL_CONTINUE)
2139 return rc;
2140 rsp_increment(ctxt, ctxt->src.val);
2141 return X86EMUL_CONTINUE;
2142}
2143
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002144static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2145{
2146 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002147 ctxt->dst.orig_val = ctxt->dst.val;
2148 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002149 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002150 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002151 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002152
2153 if (ctxt->eflags & EFLG_ZF) {
2154 /* Success: write back to memory. */
2155 ctxt->dst.val = ctxt->src.orig_val;
2156 } else {
2157 /* Failure: write the value we saw to EAX. */
2158 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002159 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Nadav Amit37c564f2014-06-02 18:34:07 +03002160 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002161 }
2162 return X86EMUL_CONTINUE;
2163}
2164
Avi Kivityd4b43252011-09-13 10:45:50 +03002165static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002166{
Avi Kivityd4b43252011-09-13 10:45:50 +03002167 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002168 unsigned short sel;
2169 int rc;
2170
Avi Kivity9dac77f2011-06-01 15:34:25 +03002171 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002172
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002173 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002174 if (rc != X86EMUL_CONTINUE)
2175 return rc;
2176
Avi Kivity9dac77f2011-06-01 15:34:25 +03002177 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002178 return rc;
2179}
2180
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002181static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002182setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002183 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002184{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002185 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002186 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002187 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002188 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002189 cs->type = 0x0b; /* Read, Execute, Accessed */
2190 cs->s = 1;
2191 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002192 cs->p = 1;
2193 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002194 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002195
Gleb Natapov79168fd2010-04-28 19:15:30 +03002196 set_desc_base(ss, 0); /* flat segment */
2197 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002198 ss->g = 1; /* 4kb granularity */
2199 ss->s = 1;
2200 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002201 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002202 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002203 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002204 ss->l = 0;
2205 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002206}
2207
Avi Kivity1a18a692012-02-01 12:23:21 +02002208static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2209{
2210 u32 eax, ebx, ecx, edx;
2211
2212 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002213 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2214 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002215 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2216 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2217}
2218
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002219static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2220{
Mathias Krause0225fb52012-08-30 01:30:16 +02002221 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002222 u32 eax, ebx, ecx, edx;
2223
2224 /*
2225 * syscall should always be enabled in longmode - so only become
2226 * vendor specific (cpuid) if other modes are active...
2227 */
2228 if (ctxt->mode == X86EMUL_MODE_PROT64)
2229 return true;
2230
2231 eax = 0x00000000;
2232 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002233 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2234 /*
2235 * Intel ("GenuineIntel")
2236 * remark: Intel CPUs only support "syscall" in 64bit
2237 * longmode. Also an 64bit guest with a
2238 * 32bit compat-app running will #UD !! While this
2239 * behaviour can be fixed (by emulating) into AMD
2240 * response - CPUs of AMD can't behave like Intel.
2241 */
2242 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2243 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2244 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2245 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002246
Avi Kivity0017f932012-06-07 14:10:16 +03002247 /* AMD ("AuthenticAMD") */
2248 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2249 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2250 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2251 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002252
Avi Kivity0017f932012-06-07 14:10:16 +03002253 /* AMD ("AMDisbetter!") */
2254 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2255 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2256 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2257 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002258
2259 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2260 return false;
2261}
2262
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002263static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002264{
Mathias Krause0225fb52012-08-30 01:30:16 +02002265 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002266 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002267 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002268 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002269 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002270
2271 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002272 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002273 ctxt->mode == X86EMUL_MODE_VM86)
2274 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002275
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002276 if (!(em_syscall_is_enabled(ctxt)))
2277 return emulate_ud(ctxt);
2278
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002279 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002280 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002281
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002282 if (!(efer & EFER_SCE))
2283 return emulate_ud(ctxt);
2284
Avi Kivity717746e2011-04-20 13:37:53 +03002285 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002286 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002287 cs_sel = (u16)(msr_data & 0xfffc);
2288 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002289
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002290 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002291 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002292 cs.l = 1;
2293 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002294 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2295 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002296
Avi Kivitydd856ef2012-08-27 23:46:17 +03002297 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002298 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002299#ifdef CONFIG_X86_64
Nadav Amit6c6cb692014-07-21 14:37:30 +03002300 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002301
Avi Kivity717746e2011-04-20 13:37:53 +03002302 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002303 ctxt->mode == X86EMUL_MODE_PROT64 ?
2304 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002305 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002306
Avi Kivity717746e2011-04-20 13:37:53 +03002307 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Nadav Amit6c6cb692014-07-21 14:37:30 +03002308 ctxt->eflags &= ~msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002309#endif
2310 } else {
2311 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002312 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002313 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002314
Nadav Amit6c6cb692014-07-21 14:37:30 +03002315 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002316 }
2317
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002318 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002319}
2320
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002321static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002322{
Mathias Krause0225fb52012-08-30 01:30:16 +02002323 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002324 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002325 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002326 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002327 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002328
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002329 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002330 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002331 if (ctxt->mode == X86EMUL_MODE_REAL)
2332 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002333
Avi Kivity1a18a692012-02-01 12:23:21 +02002334 /*
2335 * Not recognized on AMD in compat mode (but is recognized in legacy
2336 * mode).
2337 */
2338 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2339 && !vendor_intel(ctxt))
2340 return emulate_ud(ctxt);
2341
Andre Przywara8c604352009-06-18 12:56:01 +02002342 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2343 * Therefore, we inject an #UD.
2344 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002345 if (ctxt->mode == X86EMUL_MODE_PROT64)
2346 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002347
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002348 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002349
Avi Kivity717746e2011-04-20 13:37:53 +03002350 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002351 switch (ctxt->mode) {
2352 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002353 if ((msr_data & 0xfffc) == 0x0)
2354 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002355 break;
2356 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002357 if (msr_data == 0x0)
2358 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002359 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002360 default:
2361 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002362 }
2363
Nadav Amit6c6cb692014-07-21 14:37:30 +03002364 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002365 cs_sel = (u16)msr_data;
2366 cs_sel &= ~SELECTOR_RPL_MASK;
2367 ss_sel = cs_sel + 8;
2368 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002369 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002370 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002371 cs.l = 1;
2372 }
2373
Avi Kivity1aa36612011-04-27 13:20:30 +03002374 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2375 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002376
Avi Kivity717746e2011-04-20 13:37:53 +03002377 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002378 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002379
Avi Kivity717746e2011-04-20 13:37:53 +03002380 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002381 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002382
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002383 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002384}
2385
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002386static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002387{
Mathias Krause0225fb52012-08-30 01:30:16 +02002388 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002389 struct desc_struct cs, ss;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002390 u64 msr_data, rcx, rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002391 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002392 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002393
Gleb Natapova0044752010-02-10 14:21:31 +02002394 /* inject #GP if in real mode or Virtual 8086 mode */
2395 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002396 ctxt->mode == X86EMUL_MODE_VM86)
2397 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002398
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002399 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002400
Avi Kivity9dac77f2011-06-01 15:34:25 +03002401 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002402 usermode = X86EMUL_MODE_PROT64;
2403 else
2404 usermode = X86EMUL_MODE_PROT32;
2405
Nadav Amit234f3ce2014-09-18 22:39:38 +03002406 rcx = reg_read(ctxt, VCPU_REGS_RCX);
2407 rdx = reg_read(ctxt, VCPU_REGS_RDX);
2408
Andre Przywara4668f052009-06-18 12:56:02 +02002409 cs.dpl = 3;
2410 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002411 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002412 switch (usermode) {
2413 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002414 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002415 if ((msr_data & 0xfffc) == 0x0)
2416 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002417 ss_sel = (u16)(msr_data + 24);
Nadav Amitbf0b6822014-09-18 22:39:45 +03002418 rcx = (u32)rcx;
2419 rdx = (u32)rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002420 break;
2421 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002422 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002423 if (msr_data == 0x0)
2424 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002425 ss_sel = cs_sel + 8;
2426 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002427 cs.l = 1;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002428 if (is_noncanonical_address(rcx) ||
2429 is_noncanonical_address(rdx))
2430 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002431 break;
2432 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002433 cs_sel |= SELECTOR_RPL_MASK;
2434 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002435
Avi Kivity1aa36612011-04-27 13:20:30 +03002436 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2437 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002438
Nadav Amit234f3ce2014-09-18 22:39:38 +03002439 ctxt->_eip = rdx;
2440 *reg_write(ctxt, VCPU_REGS_RSP) = rcx;
Andre Przywara4668f052009-06-18 12:56:02 +02002441
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002442 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002443}
2444
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002445static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002446{
2447 int iopl;
2448 if (ctxt->mode == X86EMUL_MODE_REAL)
2449 return false;
2450 if (ctxt->mode == X86EMUL_MODE_VM86)
2451 return true;
2452 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002453 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002454}
2455
2456static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002457 u16 port, u16 len)
2458{
Mathias Krause0225fb52012-08-30 01:30:16 +02002459 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002460 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002461 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002462 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002463 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002464 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002465 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002466
Avi Kivity1aa36612011-04-27 13:20:30 +03002467 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002468 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002469 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002470 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002471 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002472 base = get_desc_base(&tr_seg);
2473#ifdef CONFIG_X86_64
2474 base |= ((u64)base3) << 32;
2475#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002476 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002477 if (r != X86EMUL_CONTINUE)
2478 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002479 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002480 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002481 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002482 if (r != X86EMUL_CONTINUE)
2483 return false;
2484 if ((perm >> bit_idx) & mask)
2485 return false;
2486 return true;
2487}
2488
2489static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002490 u16 port, u16 len)
2491{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002492 if (ctxt->perm_ok)
2493 return true;
2494
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002495 if (emulator_bad_iopl(ctxt))
2496 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002497 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002498
2499 ctxt->perm_ok = true;
2500
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002501 return true;
2502}
2503
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002504static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002505 struct tss_segment_16 *tss)
2506{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002507 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002508 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002509 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2510 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2511 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2512 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2513 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2514 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2515 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2516 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002517
Avi Kivity1aa36612011-04-27 13:20:30 +03002518 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2519 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2520 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2521 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2522 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002523}
2524
2525static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002526 struct tss_segment_16 *tss)
2527{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002528 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002529 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002530
Avi Kivity9dac77f2011-06-01 15:34:25 +03002531 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002532 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002533 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2534 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2535 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2536 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2537 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2538 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2539 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2540 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002541
2542 /*
2543 * SDM says that segment selectors are loaded before segment
2544 * descriptors
2545 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002546 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2547 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2548 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2549 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2550 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002551
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002552 cpl = tss->cs & 3;
2553
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002554 /*
Guo Chaofc058682012-06-28 15:19:51 +08002555 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002556 * it is handled in a context of new task
2557 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002558 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl,
2559 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002560 if (ret != X86EMUL_CONTINUE)
2561 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002562 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
2563 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002564 if (ret != X86EMUL_CONTINUE)
2565 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002566 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
2567 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002568 if (ret != X86EMUL_CONTINUE)
2569 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002570 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
2571 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002572 if (ret != X86EMUL_CONTINUE)
2573 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002574 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
2575 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002576 if (ret != X86EMUL_CONTINUE)
2577 return ret;
2578
2579 return X86EMUL_CONTINUE;
2580}
2581
2582static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002583 u16 tss_selector, u16 old_tss_sel,
2584 ulong old_tss_base, struct desc_struct *new_desc)
2585{
Mathias Krause0225fb52012-08-30 01:30:16 +02002586 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002587 struct tss_segment_16 tss_seg;
2588 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002589 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002590
Avi Kivity0f65dd72011-04-20 13:37:53 +03002591 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002592 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002593 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002594 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002595 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002596
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002597 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002598
Avi Kivity0f65dd72011-04-20 13:37:53 +03002599 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002600 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002601 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002602 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002603 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002604
Avi Kivity0f65dd72011-04-20 13:37:53 +03002605 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002606 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002607 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002608 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002609 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002610
2611 if (old_tss_sel != 0xffff) {
2612 tss_seg.prev_task_link = old_tss_sel;
2613
Avi Kivity0f65dd72011-04-20 13:37:53 +03002614 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002615 &tss_seg.prev_task_link,
2616 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002617 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002618 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002619 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002620 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002621 }
2622
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002623 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002624}
2625
2626static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002627 struct tss_segment_32 *tss)
2628{
Nadav Amit5c7411e2014-04-07 18:37:47 +03002629 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002630 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002631 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002632 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2633 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2634 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2635 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2636 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2637 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2638 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2639 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002640
Avi Kivity1aa36612011-04-27 13:20:30 +03002641 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2642 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2643 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2644 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2645 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2646 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002647}
2648
2649static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002650 struct tss_segment_32 *tss)
2651{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002652 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002653 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002654
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002655 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002656 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002657 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002658 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002659
2660 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002661 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2662 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2663 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2664 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2665 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2666 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2667 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2668 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002669
2670 /*
2671 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002672 * descriptors. This is important because CPL checks will
2673 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002674 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002675 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2676 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2677 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2678 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2679 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2680 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2681 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002682
2683 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002684 * If we're switching between Protected Mode and VM86, we need to make
2685 * sure to update the mode before loading the segment descriptors so
2686 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01002687 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002688 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002689 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002690 cpl = 3;
2691 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002692 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002693 cpl = tss->cs & 3;
2694 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01002695
2696 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002697 * Now load segment descriptors. If fault happenes at this stage
2698 * it is handled in a context of new task
2699 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002700 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR,
2701 cpl, true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002702 if (ret != X86EMUL_CONTINUE)
2703 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002704 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
2705 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002706 if (ret != X86EMUL_CONTINUE)
2707 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002708 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
2709 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002710 if (ret != X86EMUL_CONTINUE)
2711 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002712 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
2713 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002714 if (ret != X86EMUL_CONTINUE)
2715 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002716 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
2717 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002718 if (ret != X86EMUL_CONTINUE)
2719 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002720 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl,
2721 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002722 if (ret != X86EMUL_CONTINUE)
2723 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002724 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl,
2725 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002726 if (ret != X86EMUL_CONTINUE)
2727 return ret;
2728
2729 return X86EMUL_CONTINUE;
2730}
2731
2732static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002733 u16 tss_selector, u16 old_tss_sel,
2734 ulong old_tss_base, struct desc_struct *new_desc)
2735{
Mathias Krause0225fb52012-08-30 01:30:16 +02002736 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002737 struct tss_segment_32 tss_seg;
2738 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002739 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03002740 u32 eip_offset = offsetof(struct tss_segment_32, eip);
2741 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002742
Avi Kivity0f65dd72011-04-20 13:37:53 +03002743 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002744 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002745 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002746 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002747 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002748
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002749 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002750
Nadav Amit5c7411e2014-04-07 18:37:47 +03002751 /* Only GP registers and segment selectors are saved */
2752 ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
2753 ldt_sel_offset - eip_offset, &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
Avi Kivity0f65dd72011-04-20 13:37:53 +03002758 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002759 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002760 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002761 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002762 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002763
2764 if (old_tss_sel != 0xffff) {
2765 tss_seg.prev_task_link = old_tss_sel;
2766
Avi Kivity0f65dd72011-04-20 13:37:53 +03002767 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002768 &tss_seg.prev_task_link,
2769 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002770 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002771 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002772 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002773 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002774 }
2775
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002776 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002777}
2778
2779static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002780 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002781 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002782{
Mathias Krause0225fb52012-08-30 01:30:16 +02002783 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002784 struct desc_struct curr_tss_desc, next_tss_desc;
2785 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002786 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002787 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002788 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002789 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002790 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002791
2792 /* FIXME: old_tss_base == ~0 ? */
2793
Avi Kivitye9194642012-06-13 16:29:39 +03002794 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002795 if (ret != X86EMUL_CONTINUE)
2796 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002797 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002798 if (ret != X86EMUL_CONTINUE)
2799 return ret;
2800
2801 /* FIXME: check that next_tss_desc is tss */
2802
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002803 /*
2804 * Check privileges. The three cases are task switch caused by...
2805 *
2806 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2807 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002808 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002809 */
2810 if (reason == TASK_SWITCH_GATE) {
2811 if (idt_index != -1) {
2812 /* Software interrupts */
2813 struct desc_struct task_gate_desc;
2814 int dpl;
2815
2816 ret = read_interrupt_descriptor(ctxt, idt_index,
2817 &task_gate_desc);
2818 if (ret != X86EMUL_CONTINUE)
2819 return ret;
2820
2821 dpl = task_gate_desc.dpl;
2822 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2823 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2824 }
2825 } else if (reason != TASK_SWITCH_IRET) {
2826 int dpl = next_tss_desc.dpl;
2827 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2828 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002829 }
2830
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002831
Gleb Natapovceffb452010-03-18 15:20:19 +02002832 desc_limit = desc_limit_scaled(&next_tss_desc);
2833 if (!next_tss_desc.p ||
2834 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2835 desc_limit < 0x2b)) {
Paolo Bonzini592f0852014-08-20 10:05:08 +02002836 return emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002837 }
2838
2839 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2840 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002841 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002842 }
2843
2844 if (reason == TASK_SWITCH_IRET)
2845 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2846
2847 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002848 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002849 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2850 old_tss_sel = 0xffff;
2851
2852 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002853 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002854 old_tss_base, &next_tss_desc);
2855 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002856 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002857 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002858 if (ret != X86EMUL_CONTINUE)
2859 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002860
2861 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2862 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2863
2864 if (reason != TASK_SWITCH_IRET) {
2865 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002866 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002867 }
2868
Avi Kivity717746e2011-04-20 13:37:53 +03002869 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002870 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002871
Jan Kiszkae269fb22010-04-14 15:51:09 +02002872 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002873 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2874 ctxt->lock_prefix = 0;
2875 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002876 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002877 }
2878
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002879 return ret;
2880}
2881
2882int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002883 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002884 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002885{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002886 int rc;
2887
Avi Kivitydd856ef2012-08-27 23:46:17 +03002888 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002889 ctxt->_eip = ctxt->eip;
2890 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002891
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002892 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002893 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002894
Avi Kivitydd856ef2012-08-27 23:46:17 +03002895 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002896 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002897 writeback_registers(ctxt);
2898 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002899
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002900 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002901}
2902
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002903static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2904 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002905{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002906 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002907
Avi Kivitydd856ef2012-08-27 23:46:17 +03002908 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2909 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002910}
2911
Avi Kivity7af04fc2010-08-18 14:16:35 +03002912static int em_das(struct x86_emulate_ctxt *ctxt)
2913{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002914 u8 al, old_al;
2915 bool af, cf, old_cf;
2916
2917 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002918 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002919
2920 old_al = al;
2921 old_cf = cf;
2922 cf = false;
2923 af = ctxt->eflags & X86_EFLAGS_AF;
2924 if ((al & 0x0f) > 9 || af) {
2925 al -= 6;
2926 cf = old_cf | (al >= 250);
2927 af = true;
2928 } else {
2929 af = false;
2930 }
2931 if (old_al > 0x99 || old_cf) {
2932 al -= 0x60;
2933 cf = true;
2934 }
2935
Avi Kivity9dac77f2011-06-01 15:34:25 +03002936 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002937 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002938 ctxt->src.type = OP_IMM;
2939 ctxt->src.val = 0;
2940 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02002941 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03002942 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2943 if (cf)
2944 ctxt->eflags |= X86_EFLAGS_CF;
2945 if (af)
2946 ctxt->eflags |= X86_EFLAGS_AF;
2947 return X86EMUL_CONTINUE;
2948}
2949
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02002950static int em_aam(struct x86_emulate_ctxt *ctxt)
2951{
2952 u8 al, ah;
2953
2954 if (ctxt->src.val == 0)
2955 return emulate_de(ctxt);
2956
2957 al = ctxt->dst.val & 0xff;
2958 ah = al / ctxt->src.val;
2959 al %= ctxt->src.val;
2960
2961 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
2962
2963 /* Set PF, ZF, SF */
2964 ctxt->src.type = OP_IMM;
2965 ctxt->src.val = 0;
2966 ctxt->src.bytes = 1;
2967 fastop(ctxt, em_or);
2968
2969 return X86EMUL_CONTINUE;
2970}
2971
Gleb Natapov7f662272012-12-10 11:42:30 +02002972static int em_aad(struct x86_emulate_ctxt *ctxt)
2973{
2974 u8 al = ctxt->dst.val & 0xff;
2975 u8 ah = (ctxt->dst.val >> 8) & 0xff;
2976
2977 al = (al + (ah * ctxt->src.val)) & 0xff;
2978
2979 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
2980
Gleb Natapovf583c292013-02-13 17:50:39 +02002981 /* Set PF, ZF, SF */
2982 ctxt->src.type = OP_IMM;
2983 ctxt->src.val = 0;
2984 ctxt->src.bytes = 1;
2985 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02002986
2987 return X86EMUL_CONTINUE;
2988}
2989
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002990static int em_call(struct x86_emulate_ctxt *ctxt)
2991{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002992 int rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002993 long rel = ctxt->src.val;
2994
2995 ctxt->src.val = (unsigned long)ctxt->_eip;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002996 rc = jmp_rel(ctxt, rel);
2997 if (rc != X86EMUL_CONTINUE)
2998 return rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002999 return em_push(ctxt);
3000}
3001
Avi Kivity0ef753b2010-08-18 14:51:45 +03003002static int em_call_far(struct x86_emulate_ctxt *ctxt)
3003{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003004 u16 sel, old_cs;
3005 ulong old_eip;
3006 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03003007 struct desc_struct old_desc, new_desc;
3008 const struct x86_emulate_ops *ops = ctxt->ops;
3009 int cpl = ctxt->ops->cpl(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003010
Avi Kivity9dac77f2011-06-01 15:34:25 +03003011 old_eip = ctxt->_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003012 ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003013
Avi Kivity9dac77f2011-06-01 15:34:25 +03003014 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Nadav Amitd1442d82014-09-18 22:39:39 +03003015 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl, false,
3016 &new_desc);
3017 if (rc != X86EMUL_CONTINUE)
Avi Kivity0ef753b2010-08-18 14:51:45 +03003018 return X86EMUL_CONTINUE;
3019
Nadav Amitd1442d82014-09-18 22:39:39 +03003020 rc = assign_eip_far(ctxt, ctxt->src.val, new_desc.l);
3021 if (rc != X86EMUL_CONTINUE)
3022 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003023
Avi Kivity9dac77f2011-06-01 15:34:25 +03003024 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003025 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003026 if (rc != X86EMUL_CONTINUE)
Nadav Amitd1442d82014-09-18 22:39:39 +03003027 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003028
Avi Kivity9dac77f2011-06-01 15:34:25 +03003029 ctxt->src.val = old_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003030 rc = em_push(ctxt);
3031 /* If we failed, we tainted the memory, but the very least we should
3032 restore cs */
3033 if (rc != X86EMUL_CONTINUE)
3034 goto fail;
3035 return rc;
3036fail:
3037 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
3038 return rc;
3039
Avi Kivity0ef753b2010-08-18 14:51:45 +03003040}
3041
Avi Kivity40ece7c2010-08-18 15:12:09 +03003042static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3043{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003044 int rc;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003045 unsigned long eip;
Avi Kivity40ece7c2010-08-18 15:12:09 +03003046
Nadav Amit234f3ce2014-09-18 22:39:38 +03003047 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
3048 if (rc != X86EMUL_CONTINUE)
3049 return rc;
3050 rc = assign_eip_near(ctxt, eip);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003051 if (rc != X86EMUL_CONTINUE)
3052 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003053 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003054 return X86EMUL_CONTINUE;
3055}
3056
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003057static int em_xchg(struct x86_emulate_ctxt *ctxt)
3058{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003059 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003060 ctxt->src.val = ctxt->dst.val;
3061 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003062
3063 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003064 ctxt->dst.val = ctxt->src.orig_val;
3065 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003066 return X86EMUL_CONTINUE;
3067}
3068
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003069static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3070{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003071 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003072 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003073}
3074
Avi Kivity61429142010-08-19 15:13:00 +03003075static int em_cwd(struct x86_emulate_ctxt *ctxt)
3076{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003077 ctxt->dst.type = OP_REG;
3078 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003079 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003080 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003081
3082 return X86EMUL_CONTINUE;
3083}
3084
Avi Kivity48bb5d32010-08-18 18:54:34 +03003085static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3086{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003087 u64 tsc = 0;
3088
Avi Kivity717746e2011-04-20 13:37:53 +03003089 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003090 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3091 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003092 return X86EMUL_CONTINUE;
3093}
3094
Avi Kivity222d21a2011-11-10 14:57:30 +02003095static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3096{
3097 u64 pmc;
3098
Avi Kivitydd856ef2012-08-27 23:46:17 +03003099 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003100 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003101 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3102 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003103 return X86EMUL_CONTINUE;
3104}
3105
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003106static int em_mov(struct x86_emulate_ctxt *ctxt)
3107{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01003108 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003109 return X86EMUL_CONTINUE;
3110}
3111
Borislav Petkov84cffe42013-10-29 12:54:56 +01003112#define FFL(x) bit(X86_FEATURE_##x)
3113
3114static int em_movbe(struct x86_emulate_ctxt *ctxt)
3115{
3116 u32 ebx, ecx, edx, eax = 1;
3117 u16 tmp;
3118
3119 /*
3120 * Check MOVBE is set in the guest-visible CPUID leaf.
3121 */
3122 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
3123 if (!(ecx & FFL(MOVBE)))
3124 return emulate_ud(ctxt);
3125
3126 switch (ctxt->op_bytes) {
3127 case 2:
3128 /*
3129 * From MOVBE definition: "...When the operand size is 16 bits,
3130 * the upper word of the destination register remains unchanged
3131 * ..."
3132 *
3133 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3134 * rules so we have to do the operation almost per hand.
3135 */
3136 tmp = (u16)ctxt->src.val;
3137 ctxt->dst.val &= ~0xffffUL;
3138 ctxt->dst.val |= (unsigned long)swab16(tmp);
3139 break;
3140 case 4:
3141 ctxt->dst.val = swab32((u32)ctxt->src.val);
3142 break;
3143 case 8:
3144 ctxt->dst.val = swab64(ctxt->src.val);
3145 break;
3146 default:
Paolo Bonzini592f0852014-08-20 10:05:08 +02003147 BUG();
Borislav Petkov84cffe42013-10-29 12:54:56 +01003148 }
3149 return X86EMUL_CONTINUE;
3150}
3151
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003152static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3153{
3154 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3155 return emulate_gp(ctxt, 0);
3156
3157 /* Disable writeback. */
3158 ctxt->dst.type = OP_NONE;
3159 return X86EMUL_CONTINUE;
3160}
3161
3162static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3163{
3164 unsigned long val;
3165
3166 if (ctxt->mode == X86EMUL_MODE_PROT64)
3167 val = ctxt->src.val & ~0ULL;
3168 else
3169 val = ctxt->src.val & ~0U;
3170
3171 /* #UD condition is already handled. */
3172 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3173 return emulate_gp(ctxt, 0);
3174
3175 /* Disable writeback. */
3176 ctxt->dst.type = OP_NONE;
3177 return X86EMUL_CONTINUE;
3178}
3179
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003180static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3181{
3182 u64 msr_data;
3183
Avi Kivitydd856ef2012-08-27 23:46:17 +03003184 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3185 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3186 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003187 return emulate_gp(ctxt, 0);
3188
3189 return X86EMUL_CONTINUE;
3190}
3191
3192static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3193{
3194 u64 msr_data;
3195
Avi Kivitydd856ef2012-08-27 23:46:17 +03003196 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003197 return emulate_gp(ctxt, 0);
3198
Avi Kivitydd856ef2012-08-27 23:46:17 +03003199 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3200 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003201 return X86EMUL_CONTINUE;
3202}
3203
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003204static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3205{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003206 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003207 return emulate_ud(ctxt);
3208
Avi Kivity9dac77f2011-06-01 15:34:25 +03003209 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Nadav Amitb5bbf102014-11-02 11:54:46 +02003210 if (ctxt->dst.bytes == 4 && ctxt->dst.type == OP_MEM)
3211 ctxt->dst.bytes = 2;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003212 return X86EMUL_CONTINUE;
3213}
3214
3215static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3216{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003217 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003218
Avi Kivity9dac77f2011-06-01 15:34:25 +03003219 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003220 return emulate_ud(ctxt);
3221
Avi Kivity9dac77f2011-06-01 15:34:25 +03003222 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003223 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3224
3225 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003226 ctxt->dst.type = OP_NONE;
3227 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003228}
3229
Avi Kivitya14e5792012-06-13 12:28:33 +03003230static int em_lldt(struct x86_emulate_ctxt *ctxt)
3231{
3232 u16 sel = ctxt->src.val;
3233
3234 /* Disable writeback. */
3235 ctxt->dst.type = OP_NONE;
3236 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3237}
3238
Avi Kivity80890002012-06-13 16:33:29 +03003239static int em_ltr(struct x86_emulate_ctxt *ctxt)
3240{
3241 u16 sel = ctxt->src.val;
3242
3243 /* Disable writeback. */
3244 ctxt->dst.type = OP_NONE;
3245 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3246}
3247
Avi Kivity38503912011-03-31 18:48:09 +02003248static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3249{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003250 int rc;
3251 ulong linear;
3252
Avi Kivity9dac77f2011-06-01 15:34:25 +03003253 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003254 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003255 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003256 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003257 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003258 return X86EMUL_CONTINUE;
3259}
3260
Avi Kivity2d04a052011-04-20 15:32:49 +03003261static int em_clts(struct x86_emulate_ctxt *ctxt)
3262{
3263 ulong cr0;
3264
3265 cr0 = ctxt->ops->get_cr(ctxt, 0);
3266 cr0 &= ~X86_CR0_TS;
3267 ctxt->ops->set_cr(ctxt, 0, cr0);
3268 return X86EMUL_CONTINUE;
3269}
3270
Avi Kivity26d05cc2011-04-21 12:07:59 +03003271static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3272{
Nadav Amit0f54a322014-08-29 11:26:55 +03003273 int rc = ctxt->ops->fix_hypercall(ctxt);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003274
Avi Kivity26d05cc2011-04-21 12:07:59 +03003275 if (rc != X86EMUL_CONTINUE)
3276 return rc;
3277
3278 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003279 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003280 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003281 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003282 return X86EMUL_CONTINUE;
3283}
3284
Avi Kivity96051572012-06-10 17:21:18 +03003285static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3286 void (*get)(struct x86_emulate_ctxt *ctxt,
3287 struct desc_ptr *ptr))
3288{
3289 struct desc_ptr desc_ptr;
3290
3291 if (ctxt->mode == X86EMUL_MODE_PROT64)
3292 ctxt->op_bytes = 8;
3293 get(ctxt, &desc_ptr);
3294 if (ctxt->op_bytes == 2) {
3295 ctxt->op_bytes = 4;
3296 desc_ptr.address &= 0x00ffffff;
3297 }
3298 /* Disable writeback. */
3299 ctxt->dst.type = OP_NONE;
3300 return segmented_write(ctxt, ctxt->dst.addr.mem,
3301 &desc_ptr, 2 + ctxt->op_bytes);
3302}
3303
3304static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3305{
3306 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3307}
3308
3309static int em_sidt(struct x86_emulate_ctxt *ctxt)
3310{
3311 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3312}
3313
Avi Kivity26d05cc2011-04-21 12:07:59 +03003314static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3315{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003316 struct desc_ptr desc_ptr;
3317 int rc;
3318
Avi Kivity510425f2012-06-07 17:04:36 +03003319 if (ctxt->mode == X86EMUL_MODE_PROT64)
3320 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003321 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003322 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003323 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003324 if (rc != X86EMUL_CONTINUE)
3325 return rc;
3326 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3327 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003328 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003329 return X86EMUL_CONTINUE;
3330}
3331
Avi Kivity5ef39c72011-04-21 12:21:50 +03003332static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003333{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003334 int rc;
3335
Avi Kivity5ef39c72011-04-21 12:21:50 +03003336 rc = ctxt->ops->fix_hypercall(ctxt);
3337
Avi Kivity26d05cc2011-04-21 12:07:59 +03003338 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003339 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003340 return rc;
3341}
3342
3343static int em_lidt(struct x86_emulate_ctxt *ctxt)
3344{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003345 struct desc_ptr desc_ptr;
3346 int rc;
3347
Avi Kivity510425f2012-06-07 17:04:36 +03003348 if (ctxt->mode == X86EMUL_MODE_PROT64)
3349 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003350 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003351 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003352 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003353 if (rc != X86EMUL_CONTINUE)
3354 return rc;
3355 ctxt->ops->set_idt(ctxt, &desc_ptr);
3356 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003357 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003358 return X86EMUL_CONTINUE;
3359}
3360
3361static int em_smsw(struct x86_emulate_ctxt *ctxt)
3362{
Nadav Amit32e94d02014-06-02 18:34:11 +03003363 if (ctxt->dst.type == OP_MEM)
3364 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003365 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003366 return X86EMUL_CONTINUE;
3367}
3368
3369static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3370{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003371 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003372 | (ctxt->src.val & 0x0f));
3373 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003374 return X86EMUL_CONTINUE;
3375}
3376
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003377static int em_loop(struct x86_emulate_ctxt *ctxt)
3378{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003379 int rc = X86EMUL_CONTINUE;
3380
Avi Kivitydd856ef2012-08-27 23:46:17 +03003381 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3382 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003383 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
Nadav Amit234f3ce2014-09-18 22:39:38 +03003384 rc = jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003385
Nadav Amit234f3ce2014-09-18 22:39:38 +03003386 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003387}
3388
3389static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3390{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003391 int rc = X86EMUL_CONTINUE;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003392
Nadav Amit234f3ce2014-09-18 22:39:38 +03003393 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
3394 rc = jmp_rel(ctxt, ctxt->src.val);
3395
3396 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003397}
3398
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003399static int em_in(struct x86_emulate_ctxt *ctxt)
3400{
3401 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3402 &ctxt->dst.val))
3403 return X86EMUL_IO_NEEDED;
3404
3405 return X86EMUL_CONTINUE;
3406}
3407
3408static int em_out(struct x86_emulate_ctxt *ctxt)
3409{
3410 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3411 &ctxt->src.val, 1);
3412 /* Disable writeback. */
3413 ctxt->dst.type = OP_NONE;
3414 return X86EMUL_CONTINUE;
3415}
3416
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003417static int em_cli(struct x86_emulate_ctxt *ctxt)
3418{
3419 if (emulator_bad_iopl(ctxt))
3420 return emulate_gp(ctxt, 0);
3421
3422 ctxt->eflags &= ~X86_EFLAGS_IF;
3423 return X86EMUL_CONTINUE;
3424}
3425
3426static int em_sti(struct x86_emulate_ctxt *ctxt)
3427{
3428 if (emulator_bad_iopl(ctxt))
3429 return emulate_gp(ctxt, 0);
3430
3431 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3432 ctxt->eflags |= X86_EFLAGS_IF;
3433 return X86EMUL_CONTINUE;
3434}
3435
Avi Kivity6d6eede2012-06-07 14:11:36 +03003436static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3437{
3438 u32 eax, ebx, ecx, edx;
3439
Avi Kivitydd856ef2012-08-27 23:46:17 +03003440 eax = reg_read(ctxt, VCPU_REGS_RAX);
3441 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003442 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003443 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3444 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3445 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3446 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003447 return X86EMUL_CONTINUE;
3448}
3449
Paolo Bonzini98f73632013-10-31 11:19:42 +01003450static int em_sahf(struct x86_emulate_ctxt *ctxt)
3451{
3452 u32 flags;
3453
3454 flags = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF;
3455 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3456
3457 ctxt->eflags &= ~0xffUL;
3458 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3459 return X86EMUL_CONTINUE;
3460}
3461
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003462static int em_lahf(struct x86_emulate_ctxt *ctxt)
3463{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003464 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3465 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003466 return X86EMUL_CONTINUE;
3467}
3468
Avi Kivity92998362012-06-13 12:25:06 +03003469static int em_bswap(struct x86_emulate_ctxt *ctxt)
3470{
3471 switch (ctxt->op_bytes) {
3472#ifdef CONFIG_X86_64
3473 case 8:
3474 asm("bswap %0" : "+r"(ctxt->dst.val));
3475 break;
3476#endif
3477 default:
3478 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3479 break;
3480 }
3481 return X86EMUL_CONTINUE;
3482}
3483
Nadav Amit13e457e2014-10-13 13:04:13 +03003484static int em_clflush(struct x86_emulate_ctxt *ctxt)
3485{
3486 /* emulating clflush regardless of cpuid */
3487 return X86EMUL_CONTINUE;
3488}
3489
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003490static bool valid_cr(int nr)
3491{
3492 switch (nr) {
3493 case 0:
3494 case 2 ... 4:
3495 case 8:
3496 return true;
3497 default:
3498 return false;
3499 }
3500}
3501
3502static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3503{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003504 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003505 return emulate_ud(ctxt);
3506
3507 return X86EMUL_CONTINUE;
3508}
3509
3510static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3511{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003512 u64 new_val = ctxt->src.val64;
3513 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003514 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003515
3516 static u64 cr_reserved_bits[] = {
3517 0xffffffff00000000ULL,
3518 0, 0, 0, /* CR3 checked later */
3519 CR4_RESERVED_BITS,
3520 0, 0, 0,
3521 CR8_RESERVED_BITS,
3522 };
3523
3524 if (!valid_cr(cr))
3525 return emulate_ud(ctxt);
3526
3527 if (new_val & cr_reserved_bits[cr])
3528 return emulate_gp(ctxt, 0);
3529
3530 switch (cr) {
3531 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003532 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003533 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3534 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3535 return emulate_gp(ctxt, 0);
3536
Avi Kivity717746e2011-04-20 13:37:53 +03003537 cr4 = ctxt->ops->get_cr(ctxt, 4);
3538 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003539
3540 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3541 !(cr4 & X86_CR4_PAE))
3542 return emulate_gp(ctxt, 0);
3543
3544 break;
3545 }
3546 case 3: {
3547 u64 rsvd = 0;
3548
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003549 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3550 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003551 rsvd = CR3_L_MODE_RESERVED_BITS;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003552
3553 if (new_val & rsvd)
3554 return emulate_gp(ctxt, 0);
3555
3556 break;
3557 }
3558 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003559 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003560
3561 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3562 return emulate_gp(ctxt, 0);
3563
3564 break;
3565 }
3566 }
3567
3568 return X86EMUL_CONTINUE;
3569}
3570
Joerg Roedel3b88e412011-04-04 12:39:29 +02003571static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3572{
3573 unsigned long dr7;
3574
Avi Kivity717746e2011-04-20 13:37:53 +03003575 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003576
3577 /* Check if DR7.Global_Enable is set */
3578 return dr7 & (1 << 13);
3579}
3580
3581static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3582{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003583 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003584 u64 cr4;
3585
3586 if (dr > 7)
3587 return emulate_ud(ctxt);
3588
Avi Kivity717746e2011-04-20 13:37:53 +03003589 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003590 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3591 return emulate_ud(ctxt);
3592
Nadav Amit6d2a0522014-11-02 11:54:43 +02003593 if (check_dr7_gd(ctxt)) {
3594 ulong dr6;
3595
3596 ctxt->ops->get_dr(ctxt, 6, &dr6);
3597 dr6 &= ~15;
3598 dr6 |= DR6_BD | DR6_RTM;
3599 ctxt->ops->set_dr(ctxt, 6, dr6);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003600 return emulate_db(ctxt);
Nadav Amit6d2a0522014-11-02 11:54:43 +02003601 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02003602
3603 return X86EMUL_CONTINUE;
3604}
3605
3606static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3607{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003608 u64 new_val = ctxt->src.val64;
3609 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003610
3611 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3612 return emulate_gp(ctxt, 0);
3613
3614 return check_dr_read(ctxt);
3615}
3616
Joerg Roedel01de8b02011-04-04 12:39:31 +02003617static int check_svme(struct x86_emulate_ctxt *ctxt)
3618{
3619 u64 efer;
3620
Avi Kivity717746e2011-04-20 13:37:53 +03003621 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003622
3623 if (!(efer & EFER_SVME))
3624 return emulate_ud(ctxt);
3625
3626 return X86EMUL_CONTINUE;
3627}
3628
3629static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3630{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003631 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003632
3633 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003634 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003635 return emulate_gp(ctxt, 0);
3636
3637 return check_svme(ctxt);
3638}
3639
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003640static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3641{
Avi Kivity717746e2011-04-20 13:37:53 +03003642 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003643
Avi Kivity717746e2011-04-20 13:37:53 +03003644 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003645 return emulate_ud(ctxt);
3646
3647 return X86EMUL_CONTINUE;
3648}
3649
Joerg Roedel80612522011-04-04 12:39:33 +02003650static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3651{
Avi Kivity717746e2011-04-20 13:37:53 +03003652 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003653 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003654
Avi Kivity717746e2011-04-20 13:37:53 +03003655 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03003656 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02003657 return emulate_gp(ctxt, 0);
3658
3659 return X86EMUL_CONTINUE;
3660}
3661
Joerg Roedelf6511932011-04-04 12:39:35 +02003662static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3663{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003664 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3665 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003666 return emulate_gp(ctxt, 0);
3667
3668 return X86EMUL_CONTINUE;
3669}
3670
3671static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3672{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003673 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3674 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003675 return emulate_gp(ctxt, 0);
3676
3677 return X86EMUL_CONTINUE;
3678}
3679
Avi Kivity73fba5f2010-07-29 15:11:53 +03003680#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003681#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
3682#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
3683 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003684#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003685#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003686#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3687#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003688#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003689#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003690#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003691#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003692 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003693#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003694 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
3695 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003696#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003697
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003698#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003699#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003700#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003701#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003702#define I2bvIP(_f, _e, _i, _p) \
3703 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003704
Avi Kivityfb864fb2013-01-04 16:18:54 +02003705#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3706 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3707 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003708
Nadav Amit0f54a322014-08-29 11:26:55 +03003709static const struct opcode group7_rm0[] = {
3710 N,
3711 I(SrcNone | Priv | EmulateOnUD, em_vmcall),
3712 N, N, N, N, N, N,
3713};
3714
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003715static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003716 DI(SrcNone | Priv, monitor),
3717 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003718 N, N, N, N, N, N,
3719};
3720
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003721static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003722 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003723 II(SrcNone | Prot | EmulateOnUD, em_vmmcall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003724 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3725 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3726 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3727 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3728 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3729 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003730};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003731
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003732static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003733 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003734 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003735 N, N, N, N, N, N,
3736};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003737
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003738static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003739 F(Lock, em_add),
3740 F(Lock | PageTable, em_or),
3741 F(Lock, em_adc),
3742 F(Lock, em_sbb),
3743 F(Lock | PageTable, em_and),
3744 F(Lock, em_sub),
3745 F(Lock, em_xor),
3746 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003747};
3748
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003749static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003750 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003751};
3752
Avi Kivity007a3b52013-01-19 19:51:51 +02003753static const struct opcode group2[] = {
3754 F(DstMem | ModRM, em_rol),
3755 F(DstMem | ModRM, em_ror),
3756 F(DstMem | ModRM, em_rcl),
3757 F(DstMem | ModRM, em_rcr),
3758 F(DstMem | ModRM, em_shl),
3759 F(DstMem | ModRM, em_shr),
3760 F(DstMem | ModRM, em_shl),
3761 F(DstMem | ModRM, em_sar),
3762};
3763
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003764static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003765 F(DstMem | SrcImm | NoWrite, em_test),
3766 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003767 F(DstMem | SrcNone | Lock, em_not),
3768 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02003769 F(DstXacc | Src2Mem, em_mul_ex),
3770 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02003771 F(DstXacc | Src2Mem, em_div_ex),
3772 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003773};
3774
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003775static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003776 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3777 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003778 N, N, N, N, N, N,
3779};
3780
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003781static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003782 F(DstMem | SrcNone | Lock, em_inc),
3783 F(DstMem | SrcNone | Lock, em_dec),
Nadav Amit58b70752014-10-24 11:35:09 +03003784 I(SrcMem | NearBranch, em_call_near_abs),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003785 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
Nadav Amit58b70752014-10-24 11:35:09 +03003786 I(SrcMem | NearBranch, em_jmp_abs),
Nadav Amitf7784042014-09-18 22:39:41 +03003787 I(SrcMemFAddr | ImplicitOps, em_jmp_far),
3788 I(SrcMem | Stack, em_push), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003789};
3790
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003791static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003792 DI(Prot, sldt),
3793 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003794 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003795 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003796 N, N, N, N,
3797};
3798
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003799static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03003800 II(Mov | DstMem, em_sgdt, sgdt),
3801 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003802 II(SrcMem | Priv, em_lgdt, lgdt),
3803 II(SrcMem | Priv, em_lidt, lidt),
3804 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3805 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3806 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003807}, {
Nadav Amit0f54a322014-08-29 11:26:55 +03003808 EXT(0, group7_rm0),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003809 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003810 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003811 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3812 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3813 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003814} };
3815
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003816static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003817 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003818 F(DstMem | SrcImmByte | NoWrite, em_bt),
3819 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3820 F(DstMem | SrcImmByte | Lock, em_btr),
3821 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003822};
3823
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003824static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003825 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003826}, {
3827 N, N, N, N, N, N, N, N,
3828} };
3829
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003830static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003831 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003832 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003833};
3834
Nadav Amit13e457e2014-10-13 13:04:13 +03003835static const struct gprefix pfx_0f_ae_7 = {
Nadav Amit3f6f1482014-10-13 13:04:14 +03003836 I(SrcMem | ByteOp, em_clflush), N, N, N,
Nadav Amit13e457e2014-10-13 13:04:13 +03003837};
3838
3839static const struct group_dual group15 = { {
3840 N, N, N, N, N, N, N, GP(0, &pfx_0f_ae_7),
3841}, {
3842 N, N, N, N, N, N, N, N,
3843} };
3844
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003845static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003846 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003847};
3848
Paolo Bonzinid5b77062014-07-14 12:54:48 +02003849static const struct gprefix pfx_0f_2b = {
3850 I(0, em_mov), I(0, em_mov), N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003851};
3852
Igor Mammedov27ce8252014-03-15 21:01:59 +01003853static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01003854 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003855};
3856
Alex Williamson0a370272014-07-11 11:56:31 -06003857static const struct gprefix pfx_0f_e7 = {
3858 N, I(Sse, em_mov), N, N,
3859};
3860
Gleb Natapov045a2822012-12-20 16:57:43 +02003861static const struct escape escape_d9 = { {
3862 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3863}, {
3864 /* 0xC0 - 0xC7 */
3865 N, N, N, N, N, N, N, N,
3866 /* 0xC8 - 0xCF */
3867 N, N, N, N, N, N, N, N,
3868 /* 0xD0 - 0xC7 */
3869 N, N, N, N, N, N, N, N,
3870 /* 0xD8 - 0xDF */
3871 N, N, N, N, N, N, N, N,
3872 /* 0xE0 - 0xE7 */
3873 N, N, N, N, N, N, N, N,
3874 /* 0xE8 - 0xEF */
3875 N, N, N, N, N, N, N, N,
3876 /* 0xF0 - 0xF7 */
3877 N, N, N, N, N, N, N, N,
3878 /* 0xF8 - 0xFF */
3879 N, N, N, N, N, N, N, N,
3880} };
3881
3882static const struct escape escape_db = { {
3883 N, N, N, N, N, N, N, N,
3884}, {
3885 /* 0xC0 - 0xC7 */
3886 N, N, N, N, N, N, N, N,
3887 /* 0xC8 - 0xCF */
3888 N, N, N, N, N, N, N, N,
3889 /* 0xD0 - 0xC7 */
3890 N, N, N, N, N, N, N, N,
3891 /* 0xD8 - 0xDF */
3892 N, N, N, N, N, N, N, N,
3893 /* 0xE0 - 0xE7 */
3894 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3895 /* 0xE8 - 0xEF */
3896 N, N, N, N, N, N, N, N,
3897 /* 0xF0 - 0xF7 */
3898 N, N, N, N, N, N, N, N,
3899 /* 0xF8 - 0xFF */
3900 N, N, N, N, N, N, N, N,
3901} };
3902
3903static const struct escape escape_dd = { {
3904 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3905}, {
3906 /* 0xC0 - 0xC7 */
3907 N, N, N, N, N, N, N, N,
3908 /* 0xC8 - 0xCF */
3909 N, N, N, N, N, N, N, N,
3910 /* 0xD0 - 0xC7 */
3911 N, N, N, N, N, N, N, N,
3912 /* 0xD8 - 0xDF */
3913 N, N, N, N, N, N, N, N,
3914 /* 0xE0 - 0xE7 */
3915 N, N, N, N, N, N, N, N,
3916 /* 0xE8 - 0xEF */
3917 N, N, N, N, N, N, N, N,
3918 /* 0xF0 - 0xF7 */
3919 N, N, N, N, N, N, N, N,
3920 /* 0xF8 - 0xFF */
3921 N, N, N, N, N, N, N, N,
3922} };
3923
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003924static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003925 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003926 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003927 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3928 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003929 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003930 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003931 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3932 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003933 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003934 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003935 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3936 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003937 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003938 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003939 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3940 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003941 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003942 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003943 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003944 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003945 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003946 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003947 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003948 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003949 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02003950 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003951 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003952 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003953 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003954 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003955 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003956 I(ImplicitOps | Stack | No64, em_pusha),
3957 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003958 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3959 N, N, N, N,
3960 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003961 I(SrcImm | Mov | Stack, em_push),
3962 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003963 I(SrcImmByte | Mov | Stack, em_push),
3964 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003965 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003966 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003967 /* 0x70 - 0x7F */
Nadav Amit58b70752014-10-24 11:35:09 +03003968 X16(D(SrcImmByte | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003969 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003970 G(ByteOp | DstMem | SrcImm, group1),
3971 G(DstMem | SrcImm, group1),
3972 G(ByteOp | DstMem | SrcImm | No64, group1),
3973 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003974 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003975 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003976 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003977 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003978 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003979 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003980 D(ModRM | SrcMem | NoAccess | DstReg),
3981 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3982 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003983 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003984 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003985 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003986 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003987 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003988 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01003989 II(ImplicitOps | Stack, em_popf, popf),
3990 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003991 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003992 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003993 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003994 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003995 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003996 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003997 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003998 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3999 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02004000 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004001 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004002 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004003 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02004004 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004005 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004006 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Nadav Amit58b70752014-10-24 11:35:09 +03004007 I(ImplicitOps | NearBranch | SrcImmU16, em_ret_near_imm),
4008 I(ImplicitOps | NearBranch, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03004009 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
4010 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004011 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004012 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03004013 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Bruce Rogers32611072013-09-09 09:40:20 -06004014 I(ImplicitOps | Stack | SrcImmU16, em_ret_far_imm),
4015 I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02004016 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004017 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004018 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004019 G(Src2One | ByteOp, group2), G(Src2One, group2),
4020 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02004021 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02004022 I(DstAcc | SrcImmUByte | No64, em_aad),
4023 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004024 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004025 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02004026 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004027 /* 0xE0 - 0xE7 */
Nadav Amit58b70752014-10-24 11:35:09 +03004028 X3(I(SrcImmByte | NearBranch, em_loop)),
4029 I(SrcImmByte | NearBranch, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004030 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
4031 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004032 /* 0xE8 - 0xEF */
Nadav Amit58b70752014-10-24 11:35:09 +03004033 I(SrcImm | NearBranch, em_call), D(SrcImm | ImplicitOps | NearBranch),
4034 I(SrcImmFAddr | No64, em_jmp_far),
4035 D(SrcImmByte | ImplicitOps | NearBranch),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004036 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
4037 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004038 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004039 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004040 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4041 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004042 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09004043 D(ImplicitOps), D(ImplicitOps),
4044 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004045 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4046};
4047
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004048static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004049 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004050 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02004051 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004052 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004053 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004054 N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004055 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004056 N, N, N, N, N, N, N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004057 D(ImplicitOps | ModRM | SrcMem | NoAccess),
4058 N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004059 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03004060 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
4061 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
4062 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
4063 check_cr_write),
4064 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
4065 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004066 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004067 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
4068 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004069 N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b),
Avi Kivity3e114eb2012-04-09 18:40:01 +03004070 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004071 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004072 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004073 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004074 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004075 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02004076 I(ImplicitOps | EmulateOnUD, em_sysenter),
4077 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004078 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004079 N, N, N, N, N, N, N, N,
4080 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03004081 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004082 /* 0x50 - 0x5F */
4083 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4084 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004085 N, N, N, N,
4086 N, N, N, N,
4087 N, N, N, N,
4088 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004089 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004090 N, N, N, N,
4091 N, N, N, N,
4092 N, N, N, N,
4093 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004094 /* 0x80 - 0x8F */
Nadav Amit58b70752014-10-24 11:35:09 +03004095 X16(D(SrcImm | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004096 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004097 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004098 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004099 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004100 II(ImplicitOps, em_cpuid, cpuid),
4101 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004102 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4103 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004104 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004105 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004106 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004107 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004108 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4109 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Nadav Amit13e457e2014-10-13 13:04:13 +03004110 GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004111 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09004112 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004113 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004114 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004115 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4116 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004117 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004118 /* 0xB8 - 0xBF */
4119 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004120 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004121 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
4122 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004123 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004124 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02004125 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Wei Yongjun92f738a2010-08-17 09:19:34 +08004126 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004127 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004128 /* 0xC8 - 0xCF */
4129 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004130 /* 0xD0 - 0xDF */
4131 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4132 /* 0xE0 - 0xEF */
Alex Williamson0a370272014-07-11 11:56:31 -06004133 N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
4134 N, N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004135 /* 0xF0 - 0xFF */
4136 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4137};
4138
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004139static const struct gprefix three_byte_0f_38_f0 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01004140 I(DstReg | SrcMem | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004141};
4142
4143static const struct gprefix three_byte_0f_38_f1 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01004144 I(DstMem | SrcReg | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004145};
4146
4147/*
4148 * Insns below are selected by the prefix which indexed by the third opcode
4149 * byte.
4150 */
4151static const struct opcode opcode_map_0f_38[256] = {
4152 /* 0x00 - 0x7f */
4153 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004154 /* 0x80 - 0xef */
4155 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4156 /* 0xf0 - 0xf1 */
4157 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f0),
4158 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f1),
4159 /* 0xf2 - 0xff */
4160 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004161};
4162
Avi Kivity73fba5f2010-07-29 15:11:53 +03004163#undef D
4164#undef N
4165#undef G
4166#undef GD
4167#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004168#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004169#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004170
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004171#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004172#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004173#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004174#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004175#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004176
Avi Kivity9dac77f2011-06-01 15:34:25 +03004177static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004178{
4179 unsigned size;
4180
Avi Kivity9dac77f2011-06-01 15:34:25 +03004181 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004182 if (size == 8)
4183 size = 4;
4184 return size;
4185}
4186
4187static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4188 unsigned size, bool sign_extension)
4189{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004190 int rc = X86EMUL_CONTINUE;
4191
4192 op->type = OP_IMM;
4193 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004194 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004195 /* NB. Immediates are sign-extended as necessary. */
4196 switch (op->bytes) {
4197 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004198 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004199 break;
4200 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004201 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004202 break;
4203 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004204 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004205 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004206 case 8:
4207 op->val = insn_fetch(s64, ctxt);
4208 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004209 }
4210 if (!sign_extension) {
4211 switch (op->bytes) {
4212 case 1:
4213 op->val &= 0xff;
4214 break;
4215 case 2:
4216 op->val &= 0xffff;
4217 break;
4218 case 4:
4219 op->val &= 0xffffffff;
4220 break;
4221 }
4222 }
4223done:
4224 return rc;
4225}
4226
Avi Kivitya9945542011-09-13 10:45:41 +03004227static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4228 unsigned d)
4229{
4230 int rc = X86EMUL_CONTINUE;
4231
4232 switch (d) {
4233 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004234 decode_register_operand(ctxt, op);
Avi Kivitya9945542011-09-13 10:45:41 +03004235 break;
4236 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004237 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03004238 break;
4239 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004240 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004241 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03004242 *op = ctxt->memop;
4243 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004244 if (ctxt->d & BitOp)
Avi Kivitya9945542011-09-13 10:45:41 +03004245 fetch_bit_operand(ctxt);
4246 op->orig_val = op->val;
4247 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004248 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004249 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004250 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03004251 case OpAcc:
4252 op->type = OP_REG;
4253 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004254 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya9945542011-09-13 10:45:41 +03004255 fetch_register_operand(op);
4256 op->orig_val = op->val;
4257 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004258 case OpAccLo:
4259 op->type = OP_REG;
4260 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4261 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4262 fetch_register_operand(op);
4263 op->orig_val = op->val;
4264 break;
4265 case OpAccHi:
4266 if (ctxt->d & ByteOp) {
4267 op->type = OP_NONE;
4268 break;
4269 }
4270 op->type = OP_REG;
4271 op->bytes = ctxt->op_bytes;
4272 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4273 fetch_register_operand(op);
4274 op->orig_val = op->val;
4275 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004276 case OpDI:
4277 op->type = OP_MEM;
4278 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4279 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004280 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya9945542011-09-13 10:45:41 +03004281 op->addr.mem.seg = VCPU_SREG_ES;
4282 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004283 op->count = 1;
Avi Kivitya9945542011-09-13 10:45:41 +03004284 break;
4285 case OpDX:
4286 op->type = OP_REG;
4287 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004288 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya9945542011-09-13 10:45:41 +03004289 fetch_register_operand(op);
4290 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004291 case OpCL:
4292 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004293 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004294 break;
4295 case OpImmByte:
4296 rc = decode_imm(ctxt, op, 1, true);
4297 break;
4298 case OpOne:
4299 op->bytes = 1;
4300 op->val = 1;
4301 break;
4302 case OpImm:
4303 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4304 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004305 case OpImm64:
4306 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4307 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004308 case OpMem8:
4309 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004310 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004311 ctxt->memop.addr.reg = decode_register(ctxt,
4312 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004313 fetch_register_operand(&ctxt->memop);
4314 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004315 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004316 case OpMem16:
4317 ctxt->memop.bytes = 2;
4318 goto mem_common;
4319 case OpMem32:
4320 ctxt->memop.bytes = 4;
4321 goto mem_common;
4322 case OpImmU16:
4323 rc = decode_imm(ctxt, op, 2, false);
4324 break;
4325 case OpImmU:
4326 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4327 break;
4328 case OpSI:
4329 op->type = OP_MEM;
4330 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4331 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004332 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Bandan Das573e80f2014-04-16 12:46:13 -04004333 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03004334 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004335 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004336 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004337 case OpXLat:
4338 op->type = OP_MEM;
4339 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4340 op->addr.mem.ea =
4341 register_address(ctxt,
4342 reg_read(ctxt, VCPU_REGS_RBX) +
4343 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04004344 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004345 op->val = 0;
4346 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004347 case OpImmFAddr:
4348 op->type = OP_IMM;
4349 op->addr.mem.ea = ctxt->_eip;
4350 op->bytes = ctxt->op_bytes + 2;
4351 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4352 break;
4353 case OpMemFAddr:
4354 ctxt->memop.bytes = ctxt->op_bytes + 2;
4355 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004356 case OpES:
4357 op->val = VCPU_SREG_ES;
4358 break;
4359 case OpCS:
4360 op->val = VCPU_SREG_CS;
4361 break;
4362 case OpSS:
4363 op->val = VCPU_SREG_SS;
4364 break;
4365 case OpDS:
4366 op->val = VCPU_SREG_DS;
4367 break;
4368 case OpFS:
4369 op->val = VCPU_SREG_FS;
4370 break;
4371 case OpGS:
4372 op->val = VCPU_SREG_GS;
4373 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004374 case OpImplicit:
4375 /* Special instructions do their own operand decoding. */
4376 default:
4377 op->type = OP_NONE; /* Disable writeback. */
4378 break;
4379 }
4380
4381done:
4382 return rc;
4383}
4384
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004385int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004386{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004387 int rc = X86EMUL_CONTINUE;
4388 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004389 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004390 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04004391 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03004392 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004393
Avi Kivityf09ed832011-09-13 10:45:40 +03004394 ctxt->memop.type = OP_NONE;
4395 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004396 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02004397 ctxt->fetch.ptr = ctxt->fetch.data;
4398 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004399 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01004400 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004401 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004402 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02004403 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004404 if (rc != X86EMUL_CONTINUE)
4405 return rc;
4406 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004407
4408 switch (mode) {
4409 case X86EMUL_MODE_REAL:
4410 case X86EMUL_MODE_VM86:
4411 case X86EMUL_MODE_PROT16:
4412 def_op_bytes = def_ad_bytes = 2;
4413 break;
4414 case X86EMUL_MODE_PROT32:
4415 def_op_bytes = def_ad_bytes = 4;
4416 break;
4417#ifdef CONFIG_X86_64
4418 case X86EMUL_MODE_PROT64:
4419 def_op_bytes = 4;
4420 def_ad_bytes = 8;
4421 break;
4422#endif
4423 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004424 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004425 }
4426
Avi Kivity9dac77f2011-06-01 15:34:25 +03004427 ctxt->op_bytes = def_op_bytes;
4428 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004429
4430 /* Legacy prefixes. */
4431 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004432 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004433 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004434 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004435 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004436 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004437 break;
4438 case 0x67: /* address-size override */
4439 if (mode == X86EMUL_MODE_PROT64)
4440 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004441 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004442 else
4443 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004444 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004445 break;
4446 case 0x26: /* ES override */
4447 case 0x2e: /* CS override */
4448 case 0x36: /* SS override */
4449 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004450 has_seg_override = true;
4451 ctxt->seg_override = (ctxt->b >> 3) & 3;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004452 break;
4453 case 0x64: /* FS override */
4454 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004455 has_seg_override = true;
4456 ctxt->seg_override = ctxt->b & 7;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004457 break;
4458 case 0x40 ... 0x4f: /* REX */
4459 if (mode != X86EMUL_MODE_PROT64)
4460 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004461 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004462 continue;
4463 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004464 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004465 break;
4466 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004467 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004468 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004469 break;
4470 default:
4471 goto done_prefixes;
4472 }
4473
4474 /* Any legacy prefix after a REX prefix nullifies its effect. */
4475
Avi Kivity9dac77f2011-06-01 15:34:25 +03004476 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004477 }
4478
4479done_prefixes:
4480
4481 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004482 if (ctxt->rex_prefix & 8)
4483 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004484
4485 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004486 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004487 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004488 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004489 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004490 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004491 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004492
4493 /* 0F_38 opcode map */
4494 if (ctxt->b == 0x38) {
4495 ctxt->opcode_len = 3;
4496 ctxt->b = insn_fetch(u8, ctxt);
4497 opcode = opcode_map_0f_38[ctxt->b];
4498 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004499 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004500 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004501
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004502 if (ctxt->d & ModRM)
4503 ctxt->modrm = insn_fetch(u8, ctxt);
4504
Nadav Amit7fe864d2014-06-02 18:34:03 +03004505 /* vex-prefix instructions are not implemented */
4506 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
4507 (mode == X86EMUL_MODE_PROT64 ||
4508 (mode >= X86EMUL_MODE_PROT16 && (ctxt->modrm & 0x80)))) {
4509 ctxt->d = NotImpl;
4510 }
4511
Avi Kivity9dac77f2011-06-01 15:34:25 +03004512 while (ctxt->d & GroupMask) {
4513 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004514 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004515 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004516 opcode = opcode.u.group[goffset];
4517 break;
4518 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004519 goffset = (ctxt->modrm >> 3) & 7;
4520 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004521 opcode = opcode.u.gdual->mod3[goffset];
4522 else
4523 opcode = opcode.u.gdual->mod012[goffset];
4524 break;
4525 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004526 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004527 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004528 break;
4529 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004530 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004531 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004532 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004533 switch (simd_prefix) {
4534 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4535 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4536 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4537 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4538 }
4539 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004540 case Escape:
4541 if (ctxt->modrm > 0xbf)
4542 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4543 else
4544 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4545 break;
Avi Kivity46561642011-04-24 14:09:59 +03004546 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004547 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004548 }
Avi Kivity46561642011-04-24 14:09:59 +03004549
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004550 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004551 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004552 }
4553
Paolo Bonzinie24186e2014-03-27 12:00:57 +01004554 /* Unrecognised? */
4555 if (ctxt->d == 0)
4556 return EMULATION_FAILED;
4557
Avi Kivity9dac77f2011-06-01 15:34:25 +03004558 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004559
Nadav Amit3a6095a2014-08-13 16:50:13 +03004560 if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD)))
4561 return EMULATION_FAILED;
4562
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004563 if (unlikely(ctxt->d &
Nadav Amit58b70752014-10-24 11:35:09 +03004564 (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch))) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004565 /*
4566 * These are copied unconditionally here, and checked unconditionally
4567 * in x86_emulate_insn.
4568 */
4569 ctxt->check_perm = opcode.check_perm;
4570 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004571
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004572 if (ctxt->d & NotImpl)
4573 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004574
Nadav Amit58b70752014-10-24 11:35:09 +03004575 if (mode == X86EMUL_MODE_PROT64) {
4576 if (ctxt->op_bytes == 4 && (ctxt->d & Stack))
4577 ctxt->op_bytes = 8;
4578 else if (ctxt->d & NearBranch)
4579 ctxt->op_bytes = 8;
4580 }
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004581
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004582 if (ctxt->d & Op3264) {
4583 if (mode == X86EMUL_MODE_PROT64)
4584 ctxt->op_bytes = 8;
4585 else
4586 ctxt->op_bytes = 4;
4587 }
4588
4589 if (ctxt->d & Sse)
4590 ctxt->op_bytes = 16;
4591 else if (ctxt->d & Mmx)
4592 ctxt->op_bytes = 8;
4593 }
Avi Kivity12537912011-03-29 11:41:27 +02004594
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004595 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004596 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004597 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04004598 if (!has_seg_override) {
4599 has_seg_override = true;
4600 ctxt->seg_override = ctxt->modrm_seg;
4601 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004602 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004603 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004604 if (rc != X86EMUL_CONTINUE)
4605 goto done;
4606
Bandan Das573e80f2014-04-16 12:46:13 -04004607 if (!has_seg_override)
4608 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004609
Bandan Das573e80f2014-04-16 12:46:13 -04004610 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004611
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004612 /*
4613 * Decode and fetch the source operand: register, memory
4614 * or immediate.
4615 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004616 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004617 if (rc != X86EMUL_CONTINUE)
4618 goto done;
4619
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004620 /*
4621 * Decode and fetch the second source operand: register, memory
4622 * or immediate.
4623 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004624 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004625 if (rc != X86EMUL_CONTINUE)
4626 goto done;
4627
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004628 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03004629 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004630
Bandan Das41061cd2014-04-16 12:46:14 -04004631 if (ctxt->rip_relative)
Avi Kivityf09ed832011-09-13 10:45:40 +03004632 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004633
Paolo Bonzinia430c912014-10-23 14:54:14 +02004634done:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004635 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004636}
4637
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004638bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4639{
4640 return ctxt->d & PageTable;
4641}
4642
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004643static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4644{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004645 /* The second termination condition only applies for REPE
4646 * and REPNE. Test if the repeat string operation prefix is
4647 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4648 * corresponding termination condition according to:
4649 * - if REPE/REPZ and ZF = 0 then done
4650 * - if REPNE/REPNZ and ZF = 1 then done
4651 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004652 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4653 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4654 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004655 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004656 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004657 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4658 return true;
4659
4660 return false;
4661}
4662
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004663static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4664{
4665 bool fault = false;
4666
4667 ctxt->ops->get_fpu(ctxt);
4668 asm volatile("1: fwait \n\t"
4669 "2: \n\t"
4670 ".pushsection .fixup,\"ax\" \n\t"
4671 "3: \n\t"
4672 "movb $1, %[fault] \n\t"
4673 "jmp 2b \n\t"
4674 ".popsection \n\t"
4675 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004676 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004677 ctxt->ops->put_fpu(ctxt);
4678
4679 if (unlikely(fault))
4680 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4681
4682 return X86EMUL_CONTINUE;
4683}
4684
4685static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4686 struct operand *op)
4687{
4688 if (op->type == OP_MM)
4689 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4690}
4691
Avi Kivitye28bbd42013-01-04 16:18:48 +02004692static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4693{
4694 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivityb9fa4092013-02-09 11:31:48 +02004695 if (!(ctxt->d & ByteOp))
4696 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Avi Kivitye28bbd42013-01-04 16:18:48 +02004697 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004698 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
4699 [fastop]"+S"(fop)
4700 : "c"(ctxt->src2.val));
Avi Kivitye28bbd42013-01-04 16:18:48 +02004701 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004702 if (!fop) /* exception is returned in fop variable */
4703 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02004704 return X86EMUL_CONTINUE;
4705}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004706
Bandan Das14985072014-04-16 12:46:09 -04004707void init_decode_cache(struct x86_emulate_ctxt *ctxt)
4708{
Bandan Das573e80f2014-04-16 12:46:13 -04004709 memset(&ctxt->rip_relative, 0,
4710 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04004711
Bandan Das14985072014-04-16 12:46:09 -04004712 ctxt->io_read.pos = 0;
4713 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04004714 ctxt->mem_read.end = 0;
4715}
4716
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004717int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004718{
Mathias Krause0225fb52012-08-30 01:30:16 +02004719 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004720 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004721 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004722
Avi Kivity9dac77f2011-06-01 15:34:25 +03004723 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004724
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004725 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004726 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004727 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004728 goto done;
4729 }
4730
Avi Kivity9dac77f2011-06-01 15:34:25 +03004731 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004732 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004733 goto done;
4734 }
4735
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004736 if (unlikely(ctxt->d &
4737 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
4738 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4739 (ctxt->d & Undefined)) {
4740 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004741 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004742 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004743
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004744 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4745 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
4746 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004747 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004748 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004749
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004750 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
4751 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004752 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004753 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004754
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004755 if (ctxt->d & Mmx) {
4756 rc = flush_pending_x87_faults(ctxt);
4757 if (rc != X86EMUL_CONTINUE)
4758 goto done;
4759 /*
4760 * Now that we know the fpu is exception safe, we can fetch
4761 * operands from it.
4762 */
4763 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4764 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4765 if (!(ctxt->d & Mov))
4766 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4767 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004768
Bandan Das685bbf42014-04-16 12:46:10 -04004769 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004770 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4771 X86_ICPT_PRE_EXCEPT);
4772 if (rc != X86EMUL_CONTINUE)
4773 goto done;
4774 }
4775
4776 /* Privileged instruction can be executed only in CPL=0 */
4777 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Nadav Amit68efa762014-06-18 17:19:35 +03004778 if (ctxt->d & PrivUD)
4779 rc = emulate_ud(ctxt);
4780 else
4781 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004782 goto done;
4783 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004784
4785 /* Instruction can only be executed in protected mode */
4786 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
4787 rc = emulate_ud(ctxt);
4788 goto done;
4789 }
4790
4791 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04004792 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004793 rc = ctxt->check_perm(ctxt);
4794 if (rc != X86EMUL_CONTINUE)
4795 goto done;
4796 }
4797
Bandan Das685bbf42014-04-16 12:46:10 -04004798 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004799 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4800 X86_ICPT_POST_EXCEPT);
4801 if (rc != X86EMUL_CONTINUE)
4802 goto done;
4803 }
4804
4805 if (ctxt->rep_prefix && (ctxt->d & String)) {
4806 /* All REP prefixes have the same first termination condition */
4807 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
4808 ctxt->eip = ctxt->_eip;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004809 ctxt->eflags &= ~EFLG_RF;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004810 goto done;
4811 }
4812 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004813 }
4814
Avi Kivity9dac77f2011-06-01 15:34:25 +03004815 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4816 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4817 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004818 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004819 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004820 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004821 }
4822
Avi Kivity9dac77f2011-06-01 15:34:25 +03004823 if (ctxt->src2.type == OP_MEM) {
4824 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4825 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004826 if (rc != X86EMUL_CONTINUE)
4827 goto done;
4828 }
4829
Avi Kivity9dac77f2011-06-01 15:34:25 +03004830 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004831 goto special_insn;
4832
4833
Avi Kivity9dac77f2011-06-01 15:34:25 +03004834 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004835 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004836 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4837 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004838 if (rc != X86EMUL_CONTINUE)
4839 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004840 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004841 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004842
Avi Kivity018a98d2007-11-27 19:30:56 +02004843special_insn:
4844
Bandan Das685bbf42014-04-16 12:46:10 -04004845 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004846 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004847 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004848 if (rc != X86EMUL_CONTINUE)
4849 goto done;
4850 }
4851
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004852 if (ctxt->rep_prefix && (ctxt->d & String))
4853 ctxt->eflags |= EFLG_RF;
4854 else
4855 ctxt->eflags &= ~EFLG_RF;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004856
Avi Kivity9dac77f2011-06-01 15:34:25 +03004857 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004858 if (ctxt->d & Fastop) {
4859 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4860 rc = fastop(ctxt, fop);
4861 if (rc != X86EMUL_CONTINUE)
4862 goto done;
4863 goto writeback;
4864 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004865 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004866 if (rc != X86EMUL_CONTINUE)
4867 goto done;
4868 goto writeback;
4869 }
4870
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004871 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004872 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004873 else if (ctxt->opcode_len == 3)
4874 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004875
Avi Kivity9dac77f2011-06-01 15:34:25 +03004876 switch (ctxt->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004877 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004878 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004879 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004880 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004881 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004882 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004883 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03004884 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004885 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004886 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004887 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004888 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004889 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004890 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03004891 ctxt->dst.type = OP_NONE;
4892 else
4893 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004894 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004895 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004896 switch (ctxt->op_bytes) {
4897 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4898 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4899 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004900 }
4901 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004902 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004903 rc = emulate_int(ctxt, 3);
4904 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004905 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004906 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004907 break;
4908 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004909 if (ctxt->eflags & EFLG_OF)
4910 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004911 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004912 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004913 case 0xeb: /* jmp rel short */
Nadav Amit234f3ce2014-09-18 22:39:38 +03004914 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004915 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004916 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004917 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004918 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004919 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004920 case 0xf5: /* cmc */
4921 /* complement carry flag from eflags reg */
4922 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004923 break;
4924 case 0xf8: /* clc */
4925 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004926 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004927 case 0xf9: /* stc */
4928 ctxt->eflags |= EFLG_CF;
4929 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004930 case 0xfc: /* cld */
4931 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004932 break;
4933 case 0xfd: /* std */
4934 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004935 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004936 default:
4937 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004938 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004939
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004940 if (rc != X86EMUL_CONTINUE)
4941 goto done;
4942
Avi Kivity018a98d2007-11-27 19:30:56 +02004943writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02004944 if (ctxt->d & SrcWrite) {
4945 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
4946 rc = writeback(ctxt, &ctxt->src);
4947 if (rc != X86EMUL_CONTINUE)
4948 goto done;
4949 }
Nadav Amitee212292014-06-15 16:12:58 +03004950 if (!(ctxt->d & NoWrite)) {
4951 rc = writeback(ctxt, &ctxt->dst);
4952 if (rc != X86EMUL_CONTINUE)
4953 goto done;
4954 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004955
Gleb Natapov5cd21912010-03-18 15:20:26 +02004956 /*
4957 * restore dst type in case the decoding will be reused
4958 * (happens for string instruction )
4959 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004960 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004961
Avi Kivity9dac77f2011-06-01 15:34:25 +03004962 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004963 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004964
Avi Kivity9dac77f2011-06-01 15:34:25 +03004965 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004966 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004967
Avi Kivity9dac77f2011-06-01 15:34:25 +03004968 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004969 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004970 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004971 if ((ctxt->d & SrcMask) == SrcSI)
4972 count = ctxt->src.count;
4973 else
4974 count = ctxt->dst.count;
4975 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4976 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004977
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004978 if (!string_insn_completed(ctxt)) {
4979 /*
4980 * Re-enter guest when pio read ahead buffer is empty
4981 * or, if it is not used, after each 1024 iteration.
4982 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004983 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004984 (r->end == 0 || r->end != r->pos)) {
4985 /*
4986 * Reset read cache. Usually happens before
4987 * decode, but since instruction is restarted
4988 * we have to do it here.
4989 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004990 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004991 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004992 return EMULATION_RESTART;
4993 }
4994 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004995 }
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004996 ctxt->eflags &= ~EFLG_RF;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004997 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004998
Avi Kivity9dac77f2011-06-01 15:34:25 +03004999 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02005000
5001done:
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005002 if (rc == X86EMUL_PROPAGATE_FAULT) {
5003 WARN_ON(ctxt->exception.vector > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +02005004 ctxt->have_exception = true;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005005 }
Joerg Roedel775fde82011-04-04 12:39:24 +02005006 if (rc == X86EMUL_INTERCEPTED)
5007 return EMULATION_INTERCEPTED;
5008
Avi Kivitydd856ef2012-08-27 23:46:17 +03005009 if (rc == X86EMUL_CONTINUE)
5010 writeback_registers(ctxt);
5011
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005012 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005013
5014twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005015 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02005016 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02005017 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005018 break;
5019 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02005020 case 0x0d: /* GrpP (prefetch) */
5021 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02005022 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02005023 break;
5024 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005025 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02005026 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005027 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005028 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005029 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005030 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03005031 if (test_cc(ctxt->b, ctxt->eflags))
5032 ctxt->dst.val = ctxt->src.val;
5033 else if (ctxt->mode != X86EMUL_MODE_PROT64 ||
5034 ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005035 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08005036 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03005037 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03005038 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005039 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005040 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08005041 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005042 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08005043 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005044 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005045 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005046 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03005047 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005048 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005049 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005050 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005051 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03005052 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005053 break;
Sheng Yanga012e652007-10-15 14:24:20 +08005054 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005055 ctxt->dst.bytes = ctxt->op_bytes;
Nadav Amit3b320042014-06-02 18:34:08 +03005056 ctxt->dst.val = (ctxt->op_bytes == 8) ? (u64) ctxt->src.val :
5057 (u32) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08005058 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005059 default:
5060 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005061 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005062
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005063threebyte_insn:
5064
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005065 if (rc != X86EMUL_CONTINUE)
5066 goto done;
5067
Avi Kivity6aa8b732006-12-10 02:21:36 -08005068 goto writeback;
5069
5070cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02005071 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005072}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005073
5074void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
5075{
5076 invalidate_registers(ctxt);
5077}
5078
5079void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
5080{
5081 writeback_registers(ctxt);
5082}