blob: 736a49f218ff753b2922a06a45fd1abff45fae5d [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);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003210 return X86EMUL_CONTINUE;
3211}
3212
3213static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3214{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003215 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003216
Avi Kivity9dac77f2011-06-01 15:34:25 +03003217 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003218 return emulate_ud(ctxt);
3219
Avi Kivity9dac77f2011-06-01 15:34:25 +03003220 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003221 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3222
3223 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003224 ctxt->dst.type = OP_NONE;
3225 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003226}
3227
Avi Kivitya14e5792012-06-13 12:28:33 +03003228static int em_lldt(struct x86_emulate_ctxt *ctxt)
3229{
3230 u16 sel = ctxt->src.val;
3231
3232 /* Disable writeback. */
3233 ctxt->dst.type = OP_NONE;
3234 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3235}
3236
Avi Kivity80890002012-06-13 16:33:29 +03003237static int em_ltr(struct x86_emulate_ctxt *ctxt)
3238{
3239 u16 sel = ctxt->src.val;
3240
3241 /* Disable writeback. */
3242 ctxt->dst.type = OP_NONE;
3243 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3244}
3245
Avi Kivity38503912011-03-31 18:48:09 +02003246static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3247{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003248 int rc;
3249 ulong linear;
3250
Avi Kivity9dac77f2011-06-01 15:34:25 +03003251 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003252 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003253 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003254 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003255 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003256 return X86EMUL_CONTINUE;
3257}
3258
Avi Kivity2d04a052011-04-20 15:32:49 +03003259static int em_clts(struct x86_emulate_ctxt *ctxt)
3260{
3261 ulong cr0;
3262
3263 cr0 = ctxt->ops->get_cr(ctxt, 0);
3264 cr0 &= ~X86_CR0_TS;
3265 ctxt->ops->set_cr(ctxt, 0, cr0);
3266 return X86EMUL_CONTINUE;
3267}
3268
Avi Kivity26d05cc2011-04-21 12:07:59 +03003269static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3270{
Nadav Amit0f54a322014-08-29 11:26:55 +03003271 int rc = ctxt->ops->fix_hypercall(ctxt);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003272
Avi Kivity26d05cc2011-04-21 12:07:59 +03003273 if (rc != X86EMUL_CONTINUE)
3274 return rc;
3275
3276 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003277 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003278 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003279 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003280 return X86EMUL_CONTINUE;
3281}
3282
Avi Kivity96051572012-06-10 17:21:18 +03003283static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3284 void (*get)(struct x86_emulate_ctxt *ctxt,
3285 struct desc_ptr *ptr))
3286{
3287 struct desc_ptr desc_ptr;
3288
3289 if (ctxt->mode == X86EMUL_MODE_PROT64)
3290 ctxt->op_bytes = 8;
3291 get(ctxt, &desc_ptr);
3292 if (ctxt->op_bytes == 2) {
3293 ctxt->op_bytes = 4;
3294 desc_ptr.address &= 0x00ffffff;
3295 }
3296 /* Disable writeback. */
3297 ctxt->dst.type = OP_NONE;
3298 return segmented_write(ctxt, ctxt->dst.addr.mem,
3299 &desc_ptr, 2 + ctxt->op_bytes);
3300}
3301
3302static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3303{
3304 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3305}
3306
3307static int em_sidt(struct x86_emulate_ctxt *ctxt)
3308{
3309 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3310}
3311
Avi Kivity26d05cc2011-04-21 12:07:59 +03003312static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3313{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003314 struct desc_ptr desc_ptr;
3315 int rc;
3316
Avi Kivity510425f2012-06-07 17:04:36 +03003317 if (ctxt->mode == X86EMUL_MODE_PROT64)
3318 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003319 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003320 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003321 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003322 if (rc != X86EMUL_CONTINUE)
3323 return rc;
3324 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3325 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003326 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003327 return X86EMUL_CONTINUE;
3328}
3329
Avi Kivity5ef39c72011-04-21 12:21:50 +03003330static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003331{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003332 int rc;
3333
Avi Kivity5ef39c72011-04-21 12:21:50 +03003334 rc = ctxt->ops->fix_hypercall(ctxt);
3335
Avi Kivity26d05cc2011-04-21 12:07:59 +03003336 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003337 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003338 return rc;
3339}
3340
3341static int em_lidt(struct x86_emulate_ctxt *ctxt)
3342{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003343 struct desc_ptr desc_ptr;
3344 int rc;
3345
Avi Kivity510425f2012-06-07 17:04:36 +03003346 if (ctxt->mode == X86EMUL_MODE_PROT64)
3347 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003348 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003349 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003350 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003351 if (rc != X86EMUL_CONTINUE)
3352 return rc;
3353 ctxt->ops->set_idt(ctxt, &desc_ptr);
3354 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003355 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003356 return X86EMUL_CONTINUE;
3357}
3358
3359static int em_smsw(struct x86_emulate_ctxt *ctxt)
3360{
Nadav Amit32e94d02014-06-02 18:34:11 +03003361 if (ctxt->dst.type == OP_MEM)
3362 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003363 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003364 return X86EMUL_CONTINUE;
3365}
3366
3367static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3368{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003369 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003370 | (ctxt->src.val & 0x0f));
3371 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003372 return X86EMUL_CONTINUE;
3373}
3374
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003375static int em_loop(struct x86_emulate_ctxt *ctxt)
3376{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003377 int rc = X86EMUL_CONTINUE;
3378
Avi Kivitydd856ef2012-08-27 23:46:17 +03003379 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3380 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003381 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
Nadav Amit234f3ce2014-09-18 22:39:38 +03003382 rc = jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003383
Nadav Amit234f3ce2014-09-18 22:39:38 +03003384 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003385}
3386
3387static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3388{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003389 int rc = X86EMUL_CONTINUE;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003390
Nadav Amit234f3ce2014-09-18 22:39:38 +03003391 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
3392 rc = jmp_rel(ctxt, ctxt->src.val);
3393
3394 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003395}
3396
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003397static int em_in(struct x86_emulate_ctxt *ctxt)
3398{
3399 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3400 &ctxt->dst.val))
3401 return X86EMUL_IO_NEEDED;
3402
3403 return X86EMUL_CONTINUE;
3404}
3405
3406static int em_out(struct x86_emulate_ctxt *ctxt)
3407{
3408 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3409 &ctxt->src.val, 1);
3410 /* Disable writeback. */
3411 ctxt->dst.type = OP_NONE;
3412 return X86EMUL_CONTINUE;
3413}
3414
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003415static int em_cli(struct x86_emulate_ctxt *ctxt)
3416{
3417 if (emulator_bad_iopl(ctxt))
3418 return emulate_gp(ctxt, 0);
3419
3420 ctxt->eflags &= ~X86_EFLAGS_IF;
3421 return X86EMUL_CONTINUE;
3422}
3423
3424static int em_sti(struct x86_emulate_ctxt *ctxt)
3425{
3426 if (emulator_bad_iopl(ctxt))
3427 return emulate_gp(ctxt, 0);
3428
3429 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3430 ctxt->eflags |= X86_EFLAGS_IF;
3431 return X86EMUL_CONTINUE;
3432}
3433
Avi Kivity6d6eede2012-06-07 14:11:36 +03003434static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3435{
3436 u32 eax, ebx, ecx, edx;
3437
Avi Kivitydd856ef2012-08-27 23:46:17 +03003438 eax = reg_read(ctxt, VCPU_REGS_RAX);
3439 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003440 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003441 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3442 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3443 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3444 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003445 return X86EMUL_CONTINUE;
3446}
3447
Paolo Bonzini98f73632013-10-31 11:19:42 +01003448static int em_sahf(struct x86_emulate_ctxt *ctxt)
3449{
3450 u32 flags;
3451
3452 flags = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF;
3453 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3454
3455 ctxt->eflags &= ~0xffUL;
3456 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3457 return X86EMUL_CONTINUE;
3458}
3459
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003460static int em_lahf(struct x86_emulate_ctxt *ctxt)
3461{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003462 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3463 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003464 return X86EMUL_CONTINUE;
3465}
3466
Avi Kivity92998362012-06-13 12:25:06 +03003467static int em_bswap(struct x86_emulate_ctxt *ctxt)
3468{
3469 switch (ctxt->op_bytes) {
3470#ifdef CONFIG_X86_64
3471 case 8:
3472 asm("bswap %0" : "+r"(ctxt->dst.val));
3473 break;
3474#endif
3475 default:
3476 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3477 break;
3478 }
3479 return X86EMUL_CONTINUE;
3480}
3481
Nadav Amit13e457e2014-10-13 13:04:13 +03003482static int em_clflush(struct x86_emulate_ctxt *ctxt)
3483{
3484 /* emulating clflush regardless of cpuid */
3485 return X86EMUL_CONTINUE;
3486}
3487
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003488static bool valid_cr(int nr)
3489{
3490 switch (nr) {
3491 case 0:
3492 case 2 ... 4:
3493 case 8:
3494 return true;
3495 default:
3496 return false;
3497 }
3498}
3499
3500static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3501{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003502 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003503 return emulate_ud(ctxt);
3504
3505 return X86EMUL_CONTINUE;
3506}
3507
3508static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3509{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003510 u64 new_val = ctxt->src.val64;
3511 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003512 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003513
3514 static u64 cr_reserved_bits[] = {
3515 0xffffffff00000000ULL,
3516 0, 0, 0, /* CR3 checked later */
3517 CR4_RESERVED_BITS,
3518 0, 0, 0,
3519 CR8_RESERVED_BITS,
3520 };
3521
3522 if (!valid_cr(cr))
3523 return emulate_ud(ctxt);
3524
3525 if (new_val & cr_reserved_bits[cr])
3526 return emulate_gp(ctxt, 0);
3527
3528 switch (cr) {
3529 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003530 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003531 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3532 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3533 return emulate_gp(ctxt, 0);
3534
Avi Kivity717746e2011-04-20 13:37:53 +03003535 cr4 = ctxt->ops->get_cr(ctxt, 4);
3536 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003537
3538 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3539 !(cr4 & X86_CR4_PAE))
3540 return emulate_gp(ctxt, 0);
3541
3542 break;
3543 }
3544 case 3: {
3545 u64 rsvd = 0;
3546
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003547 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3548 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003549 rsvd = CR3_L_MODE_RESERVED_BITS;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003550
3551 if (new_val & rsvd)
3552 return emulate_gp(ctxt, 0);
3553
3554 break;
3555 }
3556 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003557 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003558
3559 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3560 return emulate_gp(ctxt, 0);
3561
3562 break;
3563 }
3564 }
3565
3566 return X86EMUL_CONTINUE;
3567}
3568
Joerg Roedel3b88e412011-04-04 12:39:29 +02003569static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3570{
3571 unsigned long dr7;
3572
Avi Kivity717746e2011-04-20 13:37:53 +03003573 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003574
3575 /* Check if DR7.Global_Enable is set */
3576 return dr7 & (1 << 13);
3577}
3578
3579static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3580{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003581 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003582 u64 cr4;
3583
3584 if (dr > 7)
3585 return emulate_ud(ctxt);
3586
Avi Kivity717746e2011-04-20 13:37:53 +03003587 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003588 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3589 return emulate_ud(ctxt);
3590
Nadav Amit6d2a0522014-11-02 11:54:43 +02003591 if (check_dr7_gd(ctxt)) {
3592 ulong dr6;
3593
3594 ctxt->ops->get_dr(ctxt, 6, &dr6);
3595 dr6 &= ~15;
3596 dr6 |= DR6_BD | DR6_RTM;
3597 ctxt->ops->set_dr(ctxt, 6, dr6);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003598 return emulate_db(ctxt);
Nadav Amit6d2a0522014-11-02 11:54:43 +02003599 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02003600
3601 return X86EMUL_CONTINUE;
3602}
3603
3604static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3605{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003606 u64 new_val = ctxt->src.val64;
3607 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003608
3609 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3610 return emulate_gp(ctxt, 0);
3611
3612 return check_dr_read(ctxt);
3613}
3614
Joerg Roedel01de8b02011-04-04 12:39:31 +02003615static int check_svme(struct x86_emulate_ctxt *ctxt)
3616{
3617 u64 efer;
3618
Avi Kivity717746e2011-04-20 13:37:53 +03003619 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003620
3621 if (!(efer & EFER_SVME))
3622 return emulate_ud(ctxt);
3623
3624 return X86EMUL_CONTINUE;
3625}
3626
3627static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3628{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003629 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003630
3631 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003632 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003633 return emulate_gp(ctxt, 0);
3634
3635 return check_svme(ctxt);
3636}
3637
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003638static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3639{
Avi Kivity717746e2011-04-20 13:37:53 +03003640 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003641
Avi Kivity717746e2011-04-20 13:37:53 +03003642 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003643 return emulate_ud(ctxt);
3644
3645 return X86EMUL_CONTINUE;
3646}
3647
Joerg Roedel80612522011-04-04 12:39:33 +02003648static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3649{
Avi Kivity717746e2011-04-20 13:37:53 +03003650 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003651 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003652
Avi Kivity717746e2011-04-20 13:37:53 +03003653 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03003654 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02003655 return emulate_gp(ctxt, 0);
3656
3657 return X86EMUL_CONTINUE;
3658}
3659
Joerg Roedelf6511932011-04-04 12:39:35 +02003660static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3661{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003662 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3663 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003664 return emulate_gp(ctxt, 0);
3665
3666 return X86EMUL_CONTINUE;
3667}
3668
3669static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3670{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003671 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3672 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003673 return emulate_gp(ctxt, 0);
3674
3675 return X86EMUL_CONTINUE;
3676}
3677
Avi Kivity73fba5f2010-07-29 15:11:53 +03003678#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003679#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
3680#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
3681 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003682#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003683#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003684#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3685#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003686#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003687#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003688#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003689#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003690 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003691#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003692 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
3693 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003694#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003695
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003696#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003697#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003698#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003699#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003700#define I2bvIP(_f, _e, _i, _p) \
3701 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003702
Avi Kivityfb864fb2013-01-04 16:18:54 +02003703#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3704 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3705 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003706
Nadav Amit0f54a322014-08-29 11:26:55 +03003707static const struct opcode group7_rm0[] = {
3708 N,
3709 I(SrcNone | Priv | EmulateOnUD, em_vmcall),
3710 N, N, N, N, N, N,
3711};
3712
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003713static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003714 DI(SrcNone | Priv, monitor),
3715 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003716 N, N, N, N, N, N,
3717};
3718
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003719static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003720 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003721 II(SrcNone | Prot | EmulateOnUD, em_vmmcall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003722 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3723 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3724 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3725 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3726 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3727 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003728};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003729
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003730static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003731 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003732 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003733 N, N, N, N, N, N,
3734};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003735
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003736static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003737 F(Lock, em_add),
3738 F(Lock | PageTable, em_or),
3739 F(Lock, em_adc),
3740 F(Lock, em_sbb),
3741 F(Lock | PageTable, em_and),
3742 F(Lock, em_sub),
3743 F(Lock, em_xor),
3744 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003745};
3746
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003747static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003748 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003749};
3750
Avi Kivity007a3b52013-01-19 19:51:51 +02003751static const struct opcode group2[] = {
3752 F(DstMem | ModRM, em_rol),
3753 F(DstMem | ModRM, em_ror),
3754 F(DstMem | ModRM, em_rcl),
3755 F(DstMem | ModRM, em_rcr),
3756 F(DstMem | ModRM, em_shl),
3757 F(DstMem | ModRM, em_shr),
3758 F(DstMem | ModRM, em_shl),
3759 F(DstMem | ModRM, em_sar),
3760};
3761
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003762static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003763 F(DstMem | SrcImm | NoWrite, em_test),
3764 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003765 F(DstMem | SrcNone | Lock, em_not),
3766 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02003767 F(DstXacc | Src2Mem, em_mul_ex),
3768 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02003769 F(DstXacc | Src2Mem, em_div_ex),
3770 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003771};
3772
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003773static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003774 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3775 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003776 N, N, N, N, N, N,
3777};
3778
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003779static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003780 F(DstMem | SrcNone | Lock, em_inc),
3781 F(DstMem | SrcNone | Lock, em_dec),
Nadav Amit58b70752014-10-24 11:35:09 +03003782 I(SrcMem | NearBranch, em_call_near_abs),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003783 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
Nadav Amit58b70752014-10-24 11:35:09 +03003784 I(SrcMem | NearBranch, em_jmp_abs),
Nadav Amitf7784042014-09-18 22:39:41 +03003785 I(SrcMemFAddr | ImplicitOps, em_jmp_far),
3786 I(SrcMem | Stack, em_push), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003787};
3788
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003789static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003790 DI(Prot, sldt),
3791 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003792 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003793 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003794 N, N, N, N,
3795};
3796
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003797static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03003798 II(Mov | DstMem, em_sgdt, sgdt),
3799 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003800 II(SrcMem | Priv, em_lgdt, lgdt),
3801 II(SrcMem | Priv, em_lidt, lidt),
3802 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3803 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3804 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003805}, {
Nadav Amit0f54a322014-08-29 11:26:55 +03003806 EXT(0, group7_rm0),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003807 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003808 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003809 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3810 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3811 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003812} };
3813
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003814static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003815 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003816 F(DstMem | SrcImmByte | NoWrite, em_bt),
3817 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3818 F(DstMem | SrcImmByte | Lock, em_btr),
3819 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003820};
3821
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003822static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003823 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003824}, {
3825 N, N, N, N, N, N, N, N,
3826} };
3827
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003828static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003829 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003830 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003831};
3832
Nadav Amit13e457e2014-10-13 13:04:13 +03003833static const struct gprefix pfx_0f_ae_7 = {
Nadav Amit3f6f1482014-10-13 13:04:14 +03003834 I(SrcMem | ByteOp, em_clflush), N, N, N,
Nadav Amit13e457e2014-10-13 13:04:13 +03003835};
3836
3837static const struct group_dual group15 = { {
3838 N, N, N, N, N, N, N, GP(0, &pfx_0f_ae_7),
3839}, {
3840 N, N, N, N, N, N, N, N,
3841} };
3842
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003843static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003844 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003845};
3846
Paolo Bonzinid5b77062014-07-14 12:54:48 +02003847static const struct gprefix pfx_0f_2b = {
3848 I(0, em_mov), I(0, em_mov), N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003849};
3850
Igor Mammedov27ce8252014-03-15 21:01:59 +01003851static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01003852 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003853};
3854
Alex Williamson0a370272014-07-11 11:56:31 -06003855static const struct gprefix pfx_0f_e7 = {
3856 N, I(Sse, em_mov), N, N,
3857};
3858
Gleb Natapov045a2822012-12-20 16:57:43 +02003859static const struct escape escape_d9 = { {
3860 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3861}, {
3862 /* 0xC0 - 0xC7 */
3863 N, N, N, N, N, N, N, N,
3864 /* 0xC8 - 0xCF */
3865 N, N, N, N, N, N, N, N,
3866 /* 0xD0 - 0xC7 */
3867 N, N, N, N, N, N, N, N,
3868 /* 0xD8 - 0xDF */
3869 N, N, N, N, N, N, N, N,
3870 /* 0xE0 - 0xE7 */
3871 N, N, N, N, N, N, N, N,
3872 /* 0xE8 - 0xEF */
3873 N, N, N, N, N, N, N, N,
3874 /* 0xF0 - 0xF7 */
3875 N, N, N, N, N, N, N, N,
3876 /* 0xF8 - 0xFF */
3877 N, N, N, N, N, N, N, N,
3878} };
3879
3880static const struct escape escape_db = { {
3881 N, N, N, N, N, N, N, N,
3882}, {
3883 /* 0xC0 - 0xC7 */
3884 N, N, N, N, N, N, N, N,
3885 /* 0xC8 - 0xCF */
3886 N, N, N, N, N, N, N, N,
3887 /* 0xD0 - 0xC7 */
3888 N, N, N, N, N, N, N, N,
3889 /* 0xD8 - 0xDF */
3890 N, N, N, N, N, N, N, N,
3891 /* 0xE0 - 0xE7 */
3892 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3893 /* 0xE8 - 0xEF */
3894 N, N, N, N, N, N, N, N,
3895 /* 0xF0 - 0xF7 */
3896 N, N, N, N, N, N, N, N,
3897 /* 0xF8 - 0xFF */
3898 N, N, N, N, N, N, N, N,
3899} };
3900
3901static const struct escape escape_dd = { {
3902 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3903}, {
3904 /* 0xC0 - 0xC7 */
3905 N, N, N, N, N, N, N, N,
3906 /* 0xC8 - 0xCF */
3907 N, N, N, N, N, N, N, N,
3908 /* 0xD0 - 0xC7 */
3909 N, N, N, N, N, N, N, N,
3910 /* 0xD8 - 0xDF */
3911 N, N, N, N, N, N, N, N,
3912 /* 0xE0 - 0xE7 */
3913 N, N, N, N, N, N, N, N,
3914 /* 0xE8 - 0xEF */
3915 N, N, N, N, N, N, N, N,
3916 /* 0xF0 - 0xF7 */
3917 N, N, N, N, N, N, N, N,
3918 /* 0xF8 - 0xFF */
3919 N, N, N, N, N, N, N, N,
3920} };
3921
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003922static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003923 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003924 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003925 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3926 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003927 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003928 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003929 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3930 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003931 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003932 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003933 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3934 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003935 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003936 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003937 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3938 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003939 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003940 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003941 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003942 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003943 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003944 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003945 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003946 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003947 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02003948 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003949 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003950 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003951 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003952 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003953 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003954 I(ImplicitOps | Stack | No64, em_pusha),
3955 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003956 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3957 N, N, N, N,
3958 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003959 I(SrcImm | Mov | Stack, em_push),
3960 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003961 I(SrcImmByte | Mov | Stack, em_push),
3962 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003963 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003964 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003965 /* 0x70 - 0x7F */
Nadav Amit58b70752014-10-24 11:35:09 +03003966 X16(D(SrcImmByte | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003967 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003968 G(ByteOp | DstMem | SrcImm, group1),
3969 G(DstMem | SrcImm, group1),
3970 G(ByteOp | DstMem | SrcImm | No64, group1),
3971 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003972 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003973 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003974 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003975 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003976 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003977 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003978 D(ModRM | SrcMem | NoAccess | DstReg),
3979 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3980 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003981 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003982 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003983 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003984 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003985 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003986 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01003987 II(ImplicitOps | Stack, em_popf, popf),
3988 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003989 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003990 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003991 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003992 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003993 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003994 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003995 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003996 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3997 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003998 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003999 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004000 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004001 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02004002 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004003 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004004 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Nadav Amit58b70752014-10-24 11:35:09 +03004005 I(ImplicitOps | NearBranch | SrcImmU16, em_ret_near_imm),
4006 I(ImplicitOps | NearBranch, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03004007 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
4008 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004009 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004010 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03004011 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Bruce Rogers32611072013-09-09 09:40:20 -06004012 I(ImplicitOps | Stack | SrcImmU16, em_ret_far_imm),
4013 I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02004014 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004015 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004016 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004017 G(Src2One | ByteOp, group2), G(Src2One, group2),
4018 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02004019 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02004020 I(DstAcc | SrcImmUByte | No64, em_aad),
4021 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004022 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004023 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02004024 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004025 /* 0xE0 - 0xE7 */
Nadav Amit58b70752014-10-24 11:35:09 +03004026 X3(I(SrcImmByte | NearBranch, em_loop)),
4027 I(SrcImmByte | NearBranch, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004028 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
4029 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004030 /* 0xE8 - 0xEF */
Nadav Amit58b70752014-10-24 11:35:09 +03004031 I(SrcImm | NearBranch, em_call), D(SrcImm | ImplicitOps | NearBranch),
4032 I(SrcImmFAddr | No64, em_jmp_far),
4033 D(SrcImmByte | ImplicitOps | NearBranch),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004034 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
4035 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004036 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004037 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004038 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4039 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004040 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09004041 D(ImplicitOps), D(ImplicitOps),
4042 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004043 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4044};
4045
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004046static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004047 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004048 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02004049 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004050 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004051 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004052 N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004053 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004054 N, N, N, N, N, N, N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004055 D(ImplicitOps | ModRM | SrcMem | NoAccess),
4056 N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004057 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03004058 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
4059 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
4060 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
4061 check_cr_write),
4062 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
4063 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004064 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004065 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
4066 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004067 N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b),
Avi Kivity3e114eb2012-04-09 18:40:01 +03004068 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004069 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004070 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004071 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004072 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004073 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02004074 I(ImplicitOps | EmulateOnUD, em_sysenter),
4075 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004076 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004077 N, N, N, N, N, N, N, N,
4078 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03004079 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004080 /* 0x50 - 0x5F */
4081 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4082 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004083 N, N, N, N,
4084 N, N, N, N,
4085 N, N, N, N,
4086 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004087 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004088 N, N, N, N,
4089 N, N, N, N,
4090 N, N, N, N,
4091 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004092 /* 0x80 - 0x8F */
Nadav Amit58b70752014-10-24 11:35:09 +03004093 X16(D(SrcImm | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004094 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004095 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004096 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004097 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004098 II(ImplicitOps, em_cpuid, cpuid),
4099 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004100 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4101 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004102 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004103 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004104 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004105 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004106 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4107 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Nadav Amit13e457e2014-10-13 13:04:13 +03004108 GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004109 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09004110 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004111 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004112 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004113 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4114 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004115 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004116 /* 0xB8 - 0xBF */
4117 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004118 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004119 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
4120 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004121 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004122 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02004123 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Wei Yongjun92f738a2010-08-17 09:19:34 +08004124 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004125 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004126 /* 0xC8 - 0xCF */
4127 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004128 /* 0xD0 - 0xDF */
4129 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4130 /* 0xE0 - 0xEF */
Alex Williamson0a370272014-07-11 11:56:31 -06004131 N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
4132 N, N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004133 /* 0xF0 - 0xFF */
4134 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4135};
4136
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004137static const struct gprefix three_byte_0f_38_f0 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01004138 I(DstReg | SrcMem | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004139};
4140
4141static const struct gprefix three_byte_0f_38_f1 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01004142 I(DstMem | SrcReg | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004143};
4144
4145/*
4146 * Insns below are selected by the prefix which indexed by the third opcode
4147 * byte.
4148 */
4149static const struct opcode opcode_map_0f_38[256] = {
4150 /* 0x00 - 0x7f */
4151 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004152 /* 0x80 - 0xef */
4153 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4154 /* 0xf0 - 0xf1 */
4155 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f0),
4156 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f1),
4157 /* 0xf2 - 0xff */
4158 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004159};
4160
Avi Kivity73fba5f2010-07-29 15:11:53 +03004161#undef D
4162#undef N
4163#undef G
4164#undef GD
4165#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004166#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004167#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004168
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004169#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004170#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004171#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004172#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004173#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004174
Avi Kivity9dac77f2011-06-01 15:34:25 +03004175static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004176{
4177 unsigned size;
4178
Avi Kivity9dac77f2011-06-01 15:34:25 +03004179 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004180 if (size == 8)
4181 size = 4;
4182 return size;
4183}
4184
4185static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4186 unsigned size, bool sign_extension)
4187{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004188 int rc = X86EMUL_CONTINUE;
4189
4190 op->type = OP_IMM;
4191 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004192 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004193 /* NB. Immediates are sign-extended as necessary. */
4194 switch (op->bytes) {
4195 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004196 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004197 break;
4198 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004199 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004200 break;
4201 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004202 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004203 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004204 case 8:
4205 op->val = insn_fetch(s64, ctxt);
4206 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004207 }
4208 if (!sign_extension) {
4209 switch (op->bytes) {
4210 case 1:
4211 op->val &= 0xff;
4212 break;
4213 case 2:
4214 op->val &= 0xffff;
4215 break;
4216 case 4:
4217 op->val &= 0xffffffff;
4218 break;
4219 }
4220 }
4221done:
4222 return rc;
4223}
4224
Avi Kivitya9945542011-09-13 10:45:41 +03004225static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4226 unsigned d)
4227{
4228 int rc = X86EMUL_CONTINUE;
4229
4230 switch (d) {
4231 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004232 decode_register_operand(ctxt, op);
Avi Kivitya9945542011-09-13 10:45:41 +03004233 break;
4234 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004235 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03004236 break;
4237 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004238 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004239 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03004240 *op = ctxt->memop;
4241 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004242 if (ctxt->d & BitOp)
Avi Kivitya9945542011-09-13 10:45:41 +03004243 fetch_bit_operand(ctxt);
4244 op->orig_val = op->val;
4245 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004246 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004247 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004248 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03004249 case OpAcc:
4250 op->type = OP_REG;
4251 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004252 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya9945542011-09-13 10:45:41 +03004253 fetch_register_operand(op);
4254 op->orig_val = op->val;
4255 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004256 case OpAccLo:
4257 op->type = OP_REG;
4258 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4259 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4260 fetch_register_operand(op);
4261 op->orig_val = op->val;
4262 break;
4263 case OpAccHi:
4264 if (ctxt->d & ByteOp) {
4265 op->type = OP_NONE;
4266 break;
4267 }
4268 op->type = OP_REG;
4269 op->bytes = ctxt->op_bytes;
4270 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4271 fetch_register_operand(op);
4272 op->orig_val = op->val;
4273 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004274 case OpDI:
4275 op->type = OP_MEM;
4276 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4277 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004278 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya9945542011-09-13 10:45:41 +03004279 op->addr.mem.seg = VCPU_SREG_ES;
4280 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004281 op->count = 1;
Avi Kivitya9945542011-09-13 10:45:41 +03004282 break;
4283 case OpDX:
4284 op->type = OP_REG;
4285 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004286 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya9945542011-09-13 10:45:41 +03004287 fetch_register_operand(op);
4288 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004289 case OpCL:
4290 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004291 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004292 break;
4293 case OpImmByte:
4294 rc = decode_imm(ctxt, op, 1, true);
4295 break;
4296 case OpOne:
4297 op->bytes = 1;
4298 op->val = 1;
4299 break;
4300 case OpImm:
4301 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4302 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004303 case OpImm64:
4304 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4305 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004306 case OpMem8:
4307 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004308 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004309 ctxt->memop.addr.reg = decode_register(ctxt,
4310 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004311 fetch_register_operand(&ctxt->memop);
4312 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004313 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004314 case OpMem16:
4315 ctxt->memop.bytes = 2;
4316 goto mem_common;
4317 case OpMem32:
4318 ctxt->memop.bytes = 4;
4319 goto mem_common;
4320 case OpImmU16:
4321 rc = decode_imm(ctxt, op, 2, false);
4322 break;
4323 case OpImmU:
4324 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4325 break;
4326 case OpSI:
4327 op->type = OP_MEM;
4328 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4329 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004330 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Bandan Das573e80f2014-04-16 12:46:13 -04004331 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03004332 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004333 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004334 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004335 case OpXLat:
4336 op->type = OP_MEM;
4337 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4338 op->addr.mem.ea =
4339 register_address(ctxt,
4340 reg_read(ctxt, VCPU_REGS_RBX) +
4341 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04004342 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004343 op->val = 0;
4344 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004345 case OpImmFAddr:
4346 op->type = OP_IMM;
4347 op->addr.mem.ea = ctxt->_eip;
4348 op->bytes = ctxt->op_bytes + 2;
4349 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4350 break;
4351 case OpMemFAddr:
4352 ctxt->memop.bytes = ctxt->op_bytes + 2;
4353 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004354 case OpES:
4355 op->val = VCPU_SREG_ES;
4356 break;
4357 case OpCS:
4358 op->val = VCPU_SREG_CS;
4359 break;
4360 case OpSS:
4361 op->val = VCPU_SREG_SS;
4362 break;
4363 case OpDS:
4364 op->val = VCPU_SREG_DS;
4365 break;
4366 case OpFS:
4367 op->val = VCPU_SREG_FS;
4368 break;
4369 case OpGS:
4370 op->val = VCPU_SREG_GS;
4371 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004372 case OpImplicit:
4373 /* Special instructions do their own operand decoding. */
4374 default:
4375 op->type = OP_NONE; /* Disable writeback. */
4376 break;
4377 }
4378
4379done:
4380 return rc;
4381}
4382
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004383int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004384{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004385 int rc = X86EMUL_CONTINUE;
4386 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004387 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004388 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04004389 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03004390 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004391
Avi Kivityf09ed832011-09-13 10:45:40 +03004392 ctxt->memop.type = OP_NONE;
4393 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004394 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02004395 ctxt->fetch.ptr = ctxt->fetch.data;
4396 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004397 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01004398 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004399 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004400 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02004401 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004402 if (rc != X86EMUL_CONTINUE)
4403 return rc;
4404 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004405
4406 switch (mode) {
4407 case X86EMUL_MODE_REAL:
4408 case X86EMUL_MODE_VM86:
4409 case X86EMUL_MODE_PROT16:
4410 def_op_bytes = def_ad_bytes = 2;
4411 break;
4412 case X86EMUL_MODE_PROT32:
4413 def_op_bytes = def_ad_bytes = 4;
4414 break;
4415#ifdef CONFIG_X86_64
4416 case X86EMUL_MODE_PROT64:
4417 def_op_bytes = 4;
4418 def_ad_bytes = 8;
4419 break;
4420#endif
4421 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004422 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004423 }
4424
Avi Kivity9dac77f2011-06-01 15:34:25 +03004425 ctxt->op_bytes = def_op_bytes;
4426 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004427
4428 /* Legacy prefixes. */
4429 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004430 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004431 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004432 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004433 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004434 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004435 break;
4436 case 0x67: /* address-size override */
4437 if (mode == X86EMUL_MODE_PROT64)
4438 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004439 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004440 else
4441 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004442 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004443 break;
4444 case 0x26: /* ES override */
4445 case 0x2e: /* CS override */
4446 case 0x36: /* SS override */
4447 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004448 has_seg_override = true;
4449 ctxt->seg_override = (ctxt->b >> 3) & 3;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004450 break;
4451 case 0x64: /* FS override */
4452 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004453 has_seg_override = true;
4454 ctxt->seg_override = ctxt->b & 7;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004455 break;
4456 case 0x40 ... 0x4f: /* REX */
4457 if (mode != X86EMUL_MODE_PROT64)
4458 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004459 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004460 continue;
4461 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004462 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004463 break;
4464 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004465 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004466 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004467 break;
4468 default:
4469 goto done_prefixes;
4470 }
4471
4472 /* Any legacy prefix after a REX prefix nullifies its effect. */
4473
Avi Kivity9dac77f2011-06-01 15:34:25 +03004474 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004475 }
4476
4477done_prefixes:
4478
4479 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004480 if (ctxt->rex_prefix & 8)
4481 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004482
4483 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004484 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004485 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004486 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004487 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004488 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004489 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004490
4491 /* 0F_38 opcode map */
4492 if (ctxt->b == 0x38) {
4493 ctxt->opcode_len = 3;
4494 ctxt->b = insn_fetch(u8, ctxt);
4495 opcode = opcode_map_0f_38[ctxt->b];
4496 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004497 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004498 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004499
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004500 if (ctxt->d & ModRM)
4501 ctxt->modrm = insn_fetch(u8, ctxt);
4502
Nadav Amit7fe864d2014-06-02 18:34:03 +03004503 /* vex-prefix instructions are not implemented */
4504 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
4505 (mode == X86EMUL_MODE_PROT64 ||
4506 (mode >= X86EMUL_MODE_PROT16 && (ctxt->modrm & 0x80)))) {
4507 ctxt->d = NotImpl;
4508 }
4509
Avi Kivity9dac77f2011-06-01 15:34:25 +03004510 while (ctxt->d & GroupMask) {
4511 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004512 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004513 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004514 opcode = opcode.u.group[goffset];
4515 break;
4516 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004517 goffset = (ctxt->modrm >> 3) & 7;
4518 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004519 opcode = opcode.u.gdual->mod3[goffset];
4520 else
4521 opcode = opcode.u.gdual->mod012[goffset];
4522 break;
4523 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004524 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004525 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004526 break;
4527 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004528 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004529 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004530 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004531 switch (simd_prefix) {
4532 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4533 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4534 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4535 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4536 }
4537 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004538 case Escape:
4539 if (ctxt->modrm > 0xbf)
4540 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4541 else
4542 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4543 break;
Avi Kivity46561642011-04-24 14:09:59 +03004544 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004545 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004546 }
Avi Kivity46561642011-04-24 14:09:59 +03004547
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004548 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004549 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004550 }
4551
Paolo Bonzinie24186e2014-03-27 12:00:57 +01004552 /* Unrecognised? */
4553 if (ctxt->d == 0)
4554 return EMULATION_FAILED;
4555
Avi Kivity9dac77f2011-06-01 15:34:25 +03004556 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004557
Nadav Amit3a6095a2014-08-13 16:50:13 +03004558 if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD)))
4559 return EMULATION_FAILED;
4560
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004561 if (unlikely(ctxt->d &
Nadav Amit58b70752014-10-24 11:35:09 +03004562 (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch))) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004563 /*
4564 * These are copied unconditionally here, and checked unconditionally
4565 * in x86_emulate_insn.
4566 */
4567 ctxt->check_perm = opcode.check_perm;
4568 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004569
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004570 if (ctxt->d & NotImpl)
4571 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004572
Nadav Amit58b70752014-10-24 11:35:09 +03004573 if (mode == X86EMUL_MODE_PROT64) {
4574 if (ctxt->op_bytes == 4 && (ctxt->d & Stack))
4575 ctxt->op_bytes = 8;
4576 else if (ctxt->d & NearBranch)
4577 ctxt->op_bytes = 8;
4578 }
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004579
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004580 if (ctxt->d & Op3264) {
4581 if (mode == X86EMUL_MODE_PROT64)
4582 ctxt->op_bytes = 8;
4583 else
4584 ctxt->op_bytes = 4;
4585 }
4586
4587 if (ctxt->d & Sse)
4588 ctxt->op_bytes = 16;
4589 else if (ctxt->d & Mmx)
4590 ctxt->op_bytes = 8;
4591 }
Avi Kivity12537912011-03-29 11:41:27 +02004592
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004593 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004594 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004595 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04004596 if (!has_seg_override) {
4597 has_seg_override = true;
4598 ctxt->seg_override = ctxt->modrm_seg;
4599 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004600 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004601 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004602 if (rc != X86EMUL_CONTINUE)
4603 goto done;
4604
Bandan Das573e80f2014-04-16 12:46:13 -04004605 if (!has_seg_override)
4606 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004607
Bandan Das573e80f2014-04-16 12:46:13 -04004608 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004609
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004610 /*
4611 * Decode and fetch the source operand: register, memory
4612 * or immediate.
4613 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004614 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004615 if (rc != X86EMUL_CONTINUE)
4616 goto done;
4617
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004618 /*
4619 * Decode and fetch the second source operand: register, memory
4620 * or immediate.
4621 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004622 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004623 if (rc != X86EMUL_CONTINUE)
4624 goto done;
4625
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004626 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03004627 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004628
Bandan Das41061cd2014-04-16 12:46:14 -04004629 if (ctxt->rip_relative)
Avi Kivityf09ed832011-09-13 10:45:40 +03004630 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004631
Paolo Bonzinia430c912014-10-23 14:54:14 +02004632done:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004633 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004634}
4635
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004636bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4637{
4638 return ctxt->d & PageTable;
4639}
4640
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004641static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4642{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004643 /* The second termination condition only applies for REPE
4644 * and REPNE. Test if the repeat string operation prefix is
4645 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4646 * corresponding termination condition according to:
4647 * - if REPE/REPZ and ZF = 0 then done
4648 * - if REPNE/REPNZ and ZF = 1 then done
4649 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004650 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4651 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4652 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004653 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004654 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004655 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4656 return true;
4657
4658 return false;
4659}
4660
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004661static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4662{
4663 bool fault = false;
4664
4665 ctxt->ops->get_fpu(ctxt);
4666 asm volatile("1: fwait \n\t"
4667 "2: \n\t"
4668 ".pushsection .fixup,\"ax\" \n\t"
4669 "3: \n\t"
4670 "movb $1, %[fault] \n\t"
4671 "jmp 2b \n\t"
4672 ".popsection \n\t"
4673 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004674 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004675 ctxt->ops->put_fpu(ctxt);
4676
4677 if (unlikely(fault))
4678 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4679
4680 return X86EMUL_CONTINUE;
4681}
4682
4683static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4684 struct operand *op)
4685{
4686 if (op->type == OP_MM)
4687 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4688}
4689
Avi Kivitye28bbd42013-01-04 16:18:48 +02004690static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4691{
4692 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivityb9fa4092013-02-09 11:31:48 +02004693 if (!(ctxt->d & ByteOp))
4694 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Avi Kivitye28bbd42013-01-04 16:18:48 +02004695 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004696 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
4697 [fastop]"+S"(fop)
4698 : "c"(ctxt->src2.val));
Avi Kivitye28bbd42013-01-04 16:18:48 +02004699 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004700 if (!fop) /* exception is returned in fop variable */
4701 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02004702 return X86EMUL_CONTINUE;
4703}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004704
Bandan Das14985072014-04-16 12:46:09 -04004705void init_decode_cache(struct x86_emulate_ctxt *ctxt)
4706{
Bandan Das573e80f2014-04-16 12:46:13 -04004707 memset(&ctxt->rip_relative, 0,
4708 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04004709
Bandan Das14985072014-04-16 12:46:09 -04004710 ctxt->io_read.pos = 0;
4711 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04004712 ctxt->mem_read.end = 0;
4713}
4714
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004715int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004716{
Mathias Krause0225fb52012-08-30 01:30:16 +02004717 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004718 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004719 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004720
Avi Kivity9dac77f2011-06-01 15:34:25 +03004721 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004722
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004723 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004724 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004725 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004726 goto done;
4727 }
4728
Avi Kivity9dac77f2011-06-01 15:34:25 +03004729 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004730 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004731 goto done;
4732 }
4733
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004734 if (unlikely(ctxt->d &
4735 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
4736 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4737 (ctxt->d & Undefined)) {
4738 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004739 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004740 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004741
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004742 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4743 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
4744 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004745 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004746 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004747
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004748 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
4749 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004750 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004751 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004752
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004753 if (ctxt->d & Mmx) {
4754 rc = flush_pending_x87_faults(ctxt);
4755 if (rc != X86EMUL_CONTINUE)
4756 goto done;
4757 /*
4758 * Now that we know the fpu is exception safe, we can fetch
4759 * operands from it.
4760 */
4761 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4762 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4763 if (!(ctxt->d & Mov))
4764 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4765 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004766
Bandan Das685bbf42014-04-16 12:46:10 -04004767 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004768 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4769 X86_ICPT_PRE_EXCEPT);
4770 if (rc != X86EMUL_CONTINUE)
4771 goto done;
4772 }
4773
4774 /* Privileged instruction can be executed only in CPL=0 */
4775 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Nadav Amit68efa762014-06-18 17:19:35 +03004776 if (ctxt->d & PrivUD)
4777 rc = emulate_ud(ctxt);
4778 else
4779 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004780 goto done;
4781 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004782
4783 /* Instruction can only be executed in protected mode */
4784 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
4785 rc = emulate_ud(ctxt);
4786 goto done;
4787 }
4788
4789 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04004790 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004791 rc = ctxt->check_perm(ctxt);
4792 if (rc != X86EMUL_CONTINUE)
4793 goto done;
4794 }
4795
Bandan Das685bbf42014-04-16 12:46:10 -04004796 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004797 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4798 X86_ICPT_POST_EXCEPT);
4799 if (rc != X86EMUL_CONTINUE)
4800 goto done;
4801 }
4802
4803 if (ctxt->rep_prefix && (ctxt->d & String)) {
4804 /* All REP prefixes have the same first termination condition */
4805 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
4806 ctxt->eip = ctxt->_eip;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004807 ctxt->eflags &= ~EFLG_RF;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004808 goto done;
4809 }
4810 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004811 }
4812
Avi Kivity9dac77f2011-06-01 15:34:25 +03004813 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4814 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4815 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004816 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004817 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004818 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004819 }
4820
Avi Kivity9dac77f2011-06-01 15:34:25 +03004821 if (ctxt->src2.type == OP_MEM) {
4822 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4823 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004824 if (rc != X86EMUL_CONTINUE)
4825 goto done;
4826 }
4827
Avi Kivity9dac77f2011-06-01 15:34:25 +03004828 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004829 goto special_insn;
4830
4831
Avi Kivity9dac77f2011-06-01 15:34:25 +03004832 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004833 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004834 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4835 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004836 if (rc != X86EMUL_CONTINUE)
4837 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004838 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004839 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004840
Avi Kivity018a98d2007-11-27 19:30:56 +02004841special_insn:
4842
Bandan Das685bbf42014-04-16 12:46:10 -04004843 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004844 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004845 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004846 if (rc != X86EMUL_CONTINUE)
4847 goto done;
4848 }
4849
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004850 if (ctxt->rep_prefix && (ctxt->d & String))
4851 ctxt->eflags |= EFLG_RF;
4852 else
4853 ctxt->eflags &= ~EFLG_RF;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004854
Avi Kivity9dac77f2011-06-01 15:34:25 +03004855 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004856 if (ctxt->d & Fastop) {
4857 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4858 rc = fastop(ctxt, fop);
4859 if (rc != X86EMUL_CONTINUE)
4860 goto done;
4861 goto writeback;
4862 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004863 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004864 if (rc != X86EMUL_CONTINUE)
4865 goto done;
4866 goto writeback;
4867 }
4868
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004869 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004870 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004871 else if (ctxt->opcode_len == 3)
4872 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004873
Avi Kivity9dac77f2011-06-01 15:34:25 +03004874 switch (ctxt->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004875 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004876 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004877 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004878 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004879 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004880 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004881 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03004882 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004883 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004884 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004885 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004886 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004887 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004888 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03004889 ctxt->dst.type = OP_NONE;
4890 else
4891 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004892 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004893 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004894 switch (ctxt->op_bytes) {
4895 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4896 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4897 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004898 }
4899 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004900 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004901 rc = emulate_int(ctxt, 3);
4902 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004903 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004904 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004905 break;
4906 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004907 if (ctxt->eflags & EFLG_OF)
4908 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004909 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004910 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004911 case 0xeb: /* jmp rel short */
Nadav Amit234f3ce2014-09-18 22:39:38 +03004912 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004913 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004914 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004915 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004916 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004917 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004918 case 0xf5: /* cmc */
4919 /* complement carry flag from eflags reg */
4920 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004921 break;
4922 case 0xf8: /* clc */
4923 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004924 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004925 case 0xf9: /* stc */
4926 ctxt->eflags |= EFLG_CF;
4927 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004928 case 0xfc: /* cld */
4929 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004930 break;
4931 case 0xfd: /* std */
4932 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004933 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004934 default:
4935 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004936 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004937
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004938 if (rc != X86EMUL_CONTINUE)
4939 goto done;
4940
Avi Kivity018a98d2007-11-27 19:30:56 +02004941writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02004942 if (ctxt->d & SrcWrite) {
4943 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
4944 rc = writeback(ctxt, &ctxt->src);
4945 if (rc != X86EMUL_CONTINUE)
4946 goto done;
4947 }
Nadav Amitee212292014-06-15 16:12:58 +03004948 if (!(ctxt->d & NoWrite)) {
4949 rc = writeback(ctxt, &ctxt->dst);
4950 if (rc != X86EMUL_CONTINUE)
4951 goto done;
4952 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004953
Gleb Natapov5cd21912010-03-18 15:20:26 +02004954 /*
4955 * restore dst type in case the decoding will be reused
4956 * (happens for string instruction )
4957 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004958 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004959
Avi Kivity9dac77f2011-06-01 15:34:25 +03004960 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004961 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004962
Avi Kivity9dac77f2011-06-01 15:34:25 +03004963 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004964 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004965
Avi Kivity9dac77f2011-06-01 15:34:25 +03004966 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004967 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004968 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004969 if ((ctxt->d & SrcMask) == SrcSI)
4970 count = ctxt->src.count;
4971 else
4972 count = ctxt->dst.count;
4973 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4974 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004975
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004976 if (!string_insn_completed(ctxt)) {
4977 /*
4978 * Re-enter guest when pio read ahead buffer is empty
4979 * or, if it is not used, after each 1024 iteration.
4980 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004981 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004982 (r->end == 0 || r->end != r->pos)) {
4983 /*
4984 * Reset read cache. Usually happens before
4985 * decode, but since instruction is restarted
4986 * we have to do it here.
4987 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004988 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004989 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004990 return EMULATION_RESTART;
4991 }
4992 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004993 }
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004994 ctxt->eflags &= ~EFLG_RF;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004995 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004996
Avi Kivity9dac77f2011-06-01 15:34:25 +03004997 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004998
4999done:
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005000 if (rc == X86EMUL_PROPAGATE_FAULT) {
5001 WARN_ON(ctxt->exception.vector > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +02005002 ctxt->have_exception = true;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005003 }
Joerg Roedel775fde82011-04-04 12:39:24 +02005004 if (rc == X86EMUL_INTERCEPTED)
5005 return EMULATION_INTERCEPTED;
5006
Avi Kivitydd856ef2012-08-27 23:46:17 +03005007 if (rc == X86EMUL_CONTINUE)
5008 writeback_registers(ctxt);
5009
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005010 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005011
5012twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005013 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02005014 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02005015 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005016 break;
5017 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02005018 case 0x0d: /* GrpP (prefetch) */
5019 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02005020 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02005021 break;
5022 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005023 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02005024 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005025 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005026 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005027 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005028 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03005029 if (test_cc(ctxt->b, ctxt->eflags))
5030 ctxt->dst.val = ctxt->src.val;
5031 else if (ctxt->mode != X86EMUL_MODE_PROT64 ||
5032 ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005033 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08005034 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03005035 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03005036 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005037 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005038 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08005039 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005040 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08005041 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005042 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005043 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005044 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03005045 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005046 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005047 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005048 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005049 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03005050 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005051 break;
Sheng Yanga012e652007-10-15 14:24:20 +08005052 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005053 ctxt->dst.bytes = ctxt->op_bytes;
Nadav Amit3b320042014-06-02 18:34:08 +03005054 ctxt->dst.val = (ctxt->op_bytes == 8) ? (u64) ctxt->src.val :
5055 (u32) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08005056 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005057 default:
5058 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005059 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005060
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005061threebyte_insn:
5062
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005063 if (rc != X86EMUL_CONTINUE)
5064 goto done;
5065
Avi Kivity6aa8b732006-12-10 02:21:36 -08005066 goto writeback;
5067
5068cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02005069 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005070}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005071
5072void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
5073{
5074 invalidate_registers(ctxt);
5075}
5076
5077void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
5078{
5079 writeback_registers(ctxt);
5080}