blob: 058aff86cfba4e170f1c7587c5c3d4d68c6e71e3 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivityb7d491e2013-01-04 16:18:49 +020027#include <linux/stringify.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028
Avi Kivity3eeb3282010-01-21 15:31:48 +020029#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020030#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020031
Avi Kivity6aa8b732006-12-10 02:21:36 -080032/*
Avi Kivitya99455492011-09-13 10:45:41 +030033 * Operand types
34 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030035#define OpNone 0ull
36#define OpImplicit 1ull /* No generic decode */
37#define OpReg 2ull /* Register */
38#define OpMem 3ull /* Memory */
39#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
40#define OpDI 5ull /* ES:DI/EDI/RDI */
41#define OpMem64 6ull /* Memory, 64-bit */
42#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
43#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030044#define OpCL 9ull /* CL register (for shifts) */
45#define OpImmByte 10ull /* 8-bit sign extended immediate */
46#define OpOne 11ull /* Implied 1 */
Nadav Amit5e2c6882012-12-06 21:55:10 -020047#define OpImm 12ull /* Sign extended up to 32-bit immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030048#define OpMem16 13ull /* Memory operand (16-bit). */
49#define OpMem32 14ull /* Memory operand (32-bit). */
50#define OpImmU 15ull /* Immediate operand, zero extended */
51#define OpSI 16ull /* SI/ESI/RSI */
52#define OpImmFAddr 17ull /* Immediate far address */
53#define OpMemFAddr 18ull /* Far address in memory */
54#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030055#define OpES 20ull /* ES */
56#define OpCS 21ull /* CS */
57#define OpSS 22ull /* SS */
58#define OpDS 23ull /* DS */
59#define OpFS 24ull /* FS */
60#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020061#define OpMem8 26ull /* 8-bit zero extended memory operand */
Nadav Amit5e2c6882012-12-06 21:55:10 -020062#define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */
Paolo Bonzini7fa57952013-05-09 11:32:50 +020063#define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */
Avi Kivity820207c2013-02-09 11:31:45 +020064#define OpAccLo 29ull /* Low part of extended acc (AX/AX/EAX/RAX) */
65#define OpAccHi 30ull /* High part of extended acc (-/DX/EDX/RDX) */
Avi Kivitya99455492011-09-13 10:45:41 +030066
Avi Kivity0fe59122011-09-13 10:45:47 +030067#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030068#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya99455492011-09-13 10:45:41 +030069
70/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080071 * Opcode effective-address decode tables.
72 * Note that we only emulate instructions that have at least one memory
73 * operand (excluding implicit stack references). We assume that stack
74 * references and instruction fetches will never occur in special memory
75 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
76 * not be handled.
77 */
78
79/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030080#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080081/* Destination operand type. */
Avi Kivitya99455492011-09-13 10:45:41 +030082#define DstShift 1
83#define ImplicitOps (OpImplicit << DstShift)
84#define DstReg (OpReg << DstShift)
85#define DstMem (OpMem << DstShift)
86#define DstAcc (OpAcc << DstShift)
87#define DstDI (OpDI << DstShift)
88#define DstMem64 (OpMem64 << DstShift)
89#define DstImmUByte (OpImmUByte << DstShift)
90#define DstDX (OpDX << DstShift)
Avi Kivity820207c2013-02-09 11:31:45 +020091#define DstAccLo (OpAccLo << DstShift)
Avi Kivitya99455492011-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 Kivity1253791d2011-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
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900658 la = seg_base(ctxt, addr.seg) + addr.ea;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100659 *max_size = 0;
Avi Kivity618ff152011-04-03 12:32:09 +0300660 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300661 case X86EMUL_MODE_PROT64:
Nadav Amit4be4de72014-09-18 22:39:40 +0300662 if (is_noncanonical_address(la))
Avi Kivity618ff152011-04-03 12:32:09 +0300663 return emulate_gp(ctxt, 0);
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100664
665 *max_size = min_t(u64, ~0u, (1ull << 48) - la);
666 if (size > *max_size)
667 goto bad;
Avi Kivity618ff152011-04-03 12:32:09 +0300668 break;
669 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300670 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
671 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300672 if (!usable)
673 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200674 /* code segment in protected mode or read-only data segment */
675 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
676 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300677 goto bad;
678 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400679 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300680 goto bad;
681 lim = desc_limit_scaled(&desc);
Nadav Amit10e38fc2014-06-18 17:19:34 +0300682 if ((ctxt->mode == X86EMUL_MODE_REAL) && !fetch &&
683 (ctxt->d & NoBigReal)) {
684 /* la is between zero and 0xffff */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100685 if (la > 0xffff)
Nadav Amit10e38fc2014-06-18 17:19:34 +0300686 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100687 *max_size = 0x10000 - la;
Nadav Amit10e38fc2014-06-18 17:19:34 +0300688 } else if ((desc.type & 8) || !(desc.type & 4)) {
Avi Kivity618ff152011-04-03 12:32:09 +0300689 /* expand-up segment */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100690 if (addr.ea > lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300691 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100692 *max_size = min_t(u64, ~0u, (u64)lim + 1 - addr.ea);
Avi Kivity618ff152011-04-03 12:32:09 +0300693 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800694 /* expand-down segment */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100695 if (addr.ea <= lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300696 goto bad;
697 lim = desc.d ? 0xffffffff : 0xffff;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100698 if (addr.ea > lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300699 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100700 *max_size = min_t(u64, ~0u, (u64)lim + 1 - addr.ea);
Avi Kivity618ff152011-04-03 12:32:09 +0300701 }
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100702 if (size > *max_size)
703 goto bad;
Avi Kivity717746e2011-04-20 13:37:53 +0300704 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity618ff152011-04-03 12:32:09 +0300705 if (!(desc.type & 8)) {
706 /* data segment */
707 if (cpl > desc.dpl)
708 goto bad;
709 } else if ((desc.type & 8) && !(desc.type & 4)) {
710 /* nonconforming code segment */
711 if (cpl != desc.dpl)
712 goto bad;
713 } else if ((desc.type & 8) && (desc.type & 4)) {
714 /* conforming code segment */
715 if (cpl < desc.dpl)
716 goto bad;
717 }
718 break;
719 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300720 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300721 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300722 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
723 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300724 *linear = la;
725 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300726bad:
727 if (addr.seg == VCPU_SREG_SS)
Paolo Bonzini36061892014-10-27 14:40:49 +0100728 return emulate_ss(ctxt, 0);
Avi Kivity618ff152011-04-03 12:32:09 +0300729 else
Paolo Bonzini36061892014-10-27 14:40:49 +0100730 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300731}
732
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400733static int linearize(struct x86_emulate_ctxt *ctxt,
734 struct segmented_address addr,
735 unsigned size, bool write,
736 ulong *linear)
737{
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100738 unsigned max_size;
739 return __linearize(ctxt, addr, &max_size, size, write, false, linear);
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400740}
741
742
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200743static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
744 struct segmented_address addr,
745 void *data,
746 unsigned size)
747{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200748 int rc;
749 ulong linear;
750
Avi Kivity83b87952011-04-03 11:31:19 +0300751 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200752 if (rc != X86EMUL_CONTINUE)
753 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300754 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200755}
756
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900757/*
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200758 * Prefetch the remaining bytes of the instruction without crossing page
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900759 * boundary if they are not in fetch_cache yet.
760 */
Paolo Bonzini9506d572014-05-06 13:05:25 +0200761static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
Avi Kivity62266862007-11-20 13:15:52 +0200762{
Avi Kivity62266862007-11-20 13:15:52 +0200763 int rc;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100764 unsigned size, max_size;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200765 unsigned long linear;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200766 int cur_size = ctxt->fetch.end - ctxt->fetch.data;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200767 struct segmented_address addr = { .seg = VCPU_SREG_CS,
Paolo Bonzini17052f12014-05-06 16:33:01 +0200768 .ea = ctxt->eip + cur_size };
769
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100770 /*
771 * We do not know exactly how many bytes will be needed, and
772 * __linearize is expensive, so fetch as much as possible. We
773 * just have to avoid going beyond the 15 byte limit, the end
774 * of the segment, or the end of the page.
775 *
776 * __linearize is called with size 0 so that it does not do any
777 * boundary check itself. Instead, we use max_size to check
778 * against op_size.
779 */
780 rc = __linearize(ctxt, addr, &max_size, 0, false, true, &linear);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200781 if (unlikely(rc != X86EMUL_CONTINUE))
782 return rc;
783
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100784 size = min_t(unsigned, 15UL ^ cur_size, max_size);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200785 size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear));
Paolo Bonzini5cfc7e02014-05-06 13:05:25 +0200786
787 /*
788 * One instruction can only straddle two pages,
789 * and one has been loaded at the beginning of
790 * x86_decode_insn. So, if not enough bytes
791 * still, we must have hit the 15-byte boundary.
792 */
793 if (unlikely(size < op_size))
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100794 return emulate_gp(ctxt, 0);
795
Paolo Bonzini17052f12014-05-06 16:33:01 +0200796 rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end,
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200797 size, &ctxt->exception);
798 if (unlikely(rc != X86EMUL_CONTINUE))
799 return rc;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200800 ctxt->fetch.end += size;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900801 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200802}
803
Paolo Bonzini9506d572014-05-06 13:05:25 +0200804static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt,
805 unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200806{
Nadav Amit08da44a2014-10-03 01:10:04 +0300807 unsigned done_size = ctxt->fetch.end - ctxt->fetch.ptr;
808
809 if (unlikely(done_size < size))
810 return __do_insn_fetch_bytes(ctxt, size - done_size);
Paolo Bonzini9506d572014-05-06 13:05:25 +0200811 else
812 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200813}
814
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900815/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900816#define insn_fetch(_type, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200817({ _type _x; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200818 \
819 rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900820 if (rc != X86EMUL_CONTINUE) \
821 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200822 ctxt->_eip += sizeof(_type); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200823 _x = *(_type __aligned(1) *) ctxt->fetch.ptr; \
824 ctxt->fetch.ptr += sizeof(_type); \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200825 _x; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900826})
827
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900828#define insn_fetch_arr(_arr, _size, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200829({ \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200830 rc = do_insn_fetch_bytes(_ctxt, _size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900831 if (rc != X86EMUL_CONTINUE) \
832 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200833 ctxt->_eip += (_size); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200834 memcpy(_arr, ctxt->fetch.ptr, _size); \
835 ctxt->fetch.ptr += (_size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900836})
837
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000838/*
839 * Given the 'reg' portion of a ModRM byte, and a register block, return a
840 * pointer into the block that addresses the relevant register.
841 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
842 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300843static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200844 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800845{
846 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200847 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848
Avi Kivity6aa8b732006-12-10 02:21:36 -0800849 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300850 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
851 else
852 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800853 return p;
854}
855
856static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200857 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858 u16 *size, unsigned long *address, int op_bytes)
859{
860 int rc;
861
862 if (op_bytes == 2)
863 op_bytes = 3;
864 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200865 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900866 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200868 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200869 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800870 return rc;
871}
872
Avi Kivity34b77652013-01-19 19:51:56 +0200873FASTOP2(add);
874FASTOP2(or);
875FASTOP2(adc);
876FASTOP2(sbb);
877FASTOP2(and);
878FASTOP2(sub);
879FASTOP2(xor);
880FASTOP2(cmp);
881FASTOP2(test);
882
Avi Kivityb9fa4092013-02-09 11:31:48 +0200883FASTOP1SRC2(mul, mul_ex);
884FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200885FASTOP1SRC2EX(div, div_ex);
886FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +0200887
Avi Kivity34b77652013-01-19 19:51:56 +0200888FASTOP3WCL(shld);
889FASTOP3WCL(shrd);
890
891FASTOP2W(imul);
892
893FASTOP1(not);
894FASTOP1(neg);
895FASTOP1(inc);
896FASTOP1(dec);
897
898FASTOP2CL(rol);
899FASTOP2CL(ror);
900FASTOP2CL(rcl);
901FASTOP2CL(rcr);
902FASTOP2CL(shl);
903FASTOP2CL(shr);
904FASTOP2CL(sar);
905
906FASTOP2W(bsf);
907FASTOP2W(bsr);
908FASTOP2W(bt);
909FASTOP2W(bts);
910FASTOP2W(btr);
911FASTOP2W(btc);
912
Avi Kivitye47a5f52013-02-09 11:31:51 +0200913FASTOP2(xadd);
914
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200915static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300916{
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200917 u8 rc;
918 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300919
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200920 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +0200921 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200922 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
923 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300924}
925
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300926static void fetch_register_operand(struct operand *op)
927{
928 switch (op->bytes) {
929 case 1:
930 op->val = *(u8 *)op->addr.reg;
931 break;
932 case 2:
933 op->val = *(u16 *)op->addr.reg;
934 break;
935 case 4:
936 op->val = *(u32 *)op->addr.reg;
937 break;
938 case 8:
939 op->val = *(u64 *)op->addr.reg;
940 break;
941 }
942}
943
Avi Kivity1253791d2011-03-29 11:41:27 +0200944static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
945{
946 ctxt->ops->get_fpu(ctxt);
947 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200948 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
949 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
950 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
951 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
952 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
953 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
954 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
955 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200956#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200957 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
958 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
959 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
960 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
961 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
962 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
963 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
964 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200965#endif
966 default: BUG();
967 }
968 ctxt->ops->put_fpu(ctxt);
969}
970
971static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
972 int reg)
973{
974 ctxt->ops->get_fpu(ctxt);
975 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200976 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
977 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
978 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
979 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
980 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
981 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
982 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
983 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200984#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200985 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
986 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
987 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
988 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
989 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
990 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
991 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
992 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200993#endif
994 default: BUG();
995 }
996 ctxt->ops->put_fpu(ctxt);
997}
998
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300999static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1000{
1001 ctxt->ops->get_fpu(ctxt);
1002 switch (reg) {
1003 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1004 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1005 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1006 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1007 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1008 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1009 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1010 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1011 default: BUG();
1012 }
1013 ctxt->ops->put_fpu(ctxt);
1014}
1015
1016static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1017{
1018 ctxt->ops->get_fpu(ctxt);
1019 switch (reg) {
1020 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1021 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1022 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1023 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1024 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1025 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1026 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1027 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1028 default: BUG();
1029 }
1030 ctxt->ops->put_fpu(ctxt);
1031}
1032
Gleb Natapov045a2822012-12-20 16:57:43 +02001033static int em_fninit(struct x86_emulate_ctxt *ctxt)
1034{
1035 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1036 return emulate_nm(ctxt);
1037
1038 ctxt->ops->get_fpu(ctxt);
1039 asm volatile("fninit");
1040 ctxt->ops->put_fpu(ctxt);
1041 return X86EMUL_CONTINUE;
1042}
1043
1044static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1045{
1046 u16 fcw;
1047
1048 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1049 return emulate_nm(ctxt);
1050
1051 ctxt->ops->get_fpu(ctxt);
1052 asm volatile("fnstcw %0": "+m"(fcw));
1053 ctxt->ops->put_fpu(ctxt);
1054
1055 /* force 2 byte destination */
1056 ctxt->dst.bytes = 2;
1057 ctxt->dst.val = fcw;
1058
1059 return X86EMUL_CONTINUE;
1060}
1061
1062static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1063{
1064 u16 fsw;
1065
1066 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1067 return emulate_nm(ctxt);
1068
1069 ctxt->ops->get_fpu(ctxt);
1070 asm volatile("fnstsw %0": "+m"(fsw));
1071 ctxt->ops->put_fpu(ctxt);
1072
1073 /* force 2 byte destination */
1074 ctxt->dst.bytes = 2;
1075 ctxt->dst.val = fsw;
1076
1077 return X86EMUL_CONTINUE;
1078}
1079
Avi Kivity1253791d2011-03-29 11:41:27 +02001080static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001081 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001082{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001083 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001084
Avi Kivity9dac77f2011-06-01 15:34:25 +03001085 if (!(ctxt->d & ModRM))
1086 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity1253791d2011-03-29 11:41:27 +02001087
Avi Kivity9dac77f2011-06-01 15:34:25 +03001088 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001089 op->type = OP_XMM;
1090 op->bytes = 16;
1091 op->addr.xmm = reg;
1092 read_sse_reg(ctxt, &op->vec_val, reg);
1093 return;
1094 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001095 if (ctxt->d & Mmx) {
1096 reg &= 7;
1097 op->type = OP_MM;
1098 op->bytes = 8;
1099 op->addr.mm = reg;
1100 return;
1101 }
Avi Kivity1253791d2011-03-29 11:41:27 +02001102
Avi Kivity3c118e22007-10-31 10:27:04 +02001103 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001104 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1105 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1106
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001107 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001108 op->orig_val = op->val;
1109}
1110
Avi Kivitya6e34072012-06-10 17:15:39 +03001111static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1112{
1113 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1114 ctxt->modrm_seg = VCPU_SREG_SS;
1115}
1116
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001117static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001118 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001119{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001120 u8 sib;
Bandan Das02357bd2014-04-16 12:46:11 -04001121 int index_reg, base_reg, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001122 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001123 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001124
Bandan Das02357bd2014-04-16 12:46:11 -04001125 ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */
1126 index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */
1127 base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001128
Bandan Das02357bd2014-04-16 12:46:11 -04001129 ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001130 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
Bandan Das02357bd2014-04-16 12:46:11 -04001131 ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001132 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001133
Nadav Amit9b88ae92014-05-25 23:05:21 +03001134 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001135 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001136 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001137 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001138 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001139 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001140 op->type = OP_XMM;
1141 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001142 op->addr.xmm = ctxt->modrm_rm;
1143 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001144 return rc;
1145 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001146 if (ctxt->d & Mmx) {
1147 op->type = OP_MM;
1148 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001149 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001150 return rc;
1151 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001152 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001153 return rc;
1154 }
1155
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001156 op->type = OP_MEM;
1157
Avi Kivity9dac77f2011-06-01 15:34:25 +03001158 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001159 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1160 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1161 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1162 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001163
1164 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001165 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001166 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001167 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001168 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001169 break;
1170 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001171 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001172 break;
1173 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001174 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001175 break;
1176 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001177 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001178 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001179 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001180 break;
1181 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001182 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001183 break;
1184 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001185 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001186 break;
1187 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001188 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001189 break;
1190 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001191 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001192 break;
1193 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001194 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001195 break;
1196 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001197 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001198 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001199 break;
1200 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001201 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001202 break;
1203 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001204 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1205 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1206 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001207 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001208 } else {
1209 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001210 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001211 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001212 index_reg |= (sib >> 3) & 7;
1213 base_reg |= sib & 7;
1214 scale = sib >> 6;
1215
Avi Kivity9dac77f2011-06-01 15:34:25 +03001216 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001217 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001218 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001219 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001220 adjust_modrm_seg(ctxt, base_reg);
1221 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001222 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001223 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001224 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001225 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001226 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001227 } else {
1228 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001229 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001230 adjust_modrm_seg(ctxt, base_reg);
1231 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001232 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001233 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001234 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001235 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001236 break;
1237 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001238 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001239 break;
1240 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001241 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001242 break;
1243 }
1244 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001245 op->addr.mem.ea = modrm_ea;
Bandan Das41061cd2014-04-16 12:46:14 -04001246 if (ctxt->ad_bytes != 8)
1247 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
1248
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001249done:
1250 return rc;
1251}
1252
1253static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001254 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001255{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001256 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001257
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001258 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001259 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001260 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001261 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001262 break;
1263 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001264 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001265 break;
1266 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001267 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001268 break;
1269 }
1270done:
1271 return rc;
1272}
1273
Avi Kivity9dac77f2011-06-01 15:34:25 +03001274static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001275{
Sheng Yang7129eec2010-09-28 16:33:32 +08001276 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001277
Avi Kivity9dac77f2011-06-01 15:34:25 +03001278 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001279 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001280
Avi Kivity9dac77f2011-06-01 15:34:25 +03001281 if (ctxt->src.bytes == 2)
1282 sv = (s16)ctxt->src.val & (s16)mask;
1283 else if (ctxt->src.bytes == 4)
1284 sv = (s32)ctxt->src.val & (s32)mask;
Nadav Amit7dec5602014-06-15 16:12:57 +03001285 else
1286 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001287
Avi Kivity9dac77f2011-06-01 15:34:25 +03001288 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001289 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001290
1291 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001292 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001293}
1294
Gleb Natapov9de41572010-04-28 19:15:22 +03001295static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001296 unsigned long addr, void *dest, unsigned size)
1297{
1298 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001299 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001300
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001301 if (mc->pos < mc->end)
1302 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001303
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001304 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001305
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001306 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1307 &ctxt->exception);
1308 if (rc != X86EMUL_CONTINUE)
1309 return rc;
1310
1311 mc->end += size;
1312
1313read_cached:
1314 memcpy(dest, mc->data + mc->pos, size);
1315 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001316 return X86EMUL_CONTINUE;
1317}
1318
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001319static int segmented_read(struct x86_emulate_ctxt *ctxt,
1320 struct segmented_address addr,
1321 void *data,
1322 unsigned size)
1323{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001324 int rc;
1325 ulong linear;
1326
Avi Kivity83b87952011-04-03 11:31:19 +03001327 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001328 if (rc != X86EMUL_CONTINUE)
1329 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001330 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001331}
1332
1333static int segmented_write(struct x86_emulate_ctxt *ctxt,
1334 struct segmented_address addr,
1335 const void *data,
1336 unsigned size)
1337{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001338 int rc;
1339 ulong linear;
1340
Avi Kivity83b87952011-04-03 11:31:19 +03001341 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001342 if (rc != X86EMUL_CONTINUE)
1343 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001344 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1345 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001346}
1347
1348static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1349 struct segmented_address addr,
1350 const void *orig_data, const void *data,
1351 unsigned size)
1352{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001353 int rc;
1354 ulong linear;
1355
Avi Kivity83b87952011-04-03 11:31:19 +03001356 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001357 if (rc != X86EMUL_CONTINUE)
1358 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001359 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1360 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001361}
1362
Gleb Natapov7b262e92010-03-18 15:20:27 +02001363static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001364 unsigned int size, unsigned short port,
1365 void *dest)
1366{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001367 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001368
1369 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001370 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001371 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001372 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001373 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001374 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1375 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Mark Rustadb55a8142014-07-25 06:27:05 -07001376 n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001377 if (n == 0)
1378 n = 1;
1379 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001380 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001381 return 0;
1382 rc->end = n * size;
1383 }
1384
Nadav Amite6e39f02014-04-18 03:35:10 +03001385 if (ctxt->rep_prefix && (ctxt->d & String) &&
1386 !(ctxt->eflags & EFLG_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001387 ctxt->dst.data = rc->data + rc->pos;
1388 ctxt->dst.type = OP_MEM_STR;
1389 ctxt->dst.count = (rc->end - rc->pos) / size;
1390 rc->pos = rc->end;
1391 } else {
1392 memcpy(dest, rc->data + rc->pos, size);
1393 rc->pos += size;
1394 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001395 return 1;
1396}
1397
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001398static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1399 u16 index, struct desc_struct *desc)
1400{
1401 struct desc_ptr dt;
1402 ulong addr;
1403
1404 ctxt->ops->get_idt(ctxt, &dt);
1405
1406 if (dt.size < index * 8 + 7)
1407 return emulate_gp(ctxt, index << 3 | 0x2);
1408
1409 addr = dt.address + index * 8;
1410 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1411 &ctxt->exception);
1412}
1413
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001414static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001415 u16 selector, struct desc_ptr *dt)
1416{
Mathias Krause0225fb52012-08-30 01:30:16 +02001417 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001418 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001419
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001420 if (selector & 1 << 2) {
1421 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001422 u16 sel;
1423
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001424 memset (dt, 0, sizeof *dt);
Nadav Amit2eedcac2014-06-02 18:34:05 +03001425 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1426 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001427 return;
1428
1429 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001430 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001431 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001432 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001433}
1434
1435/* allowed just for 8 bytes segments */
1436static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001437 u16 selector, struct desc_struct *desc,
1438 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001439{
1440 struct desc_ptr dt;
1441 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001442 ulong addr;
1443
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001444 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001445
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001446 if (dt.size < index * 8 + 7)
1447 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001448
Avi Kivitye9194642012-06-13 16:29:39 +03001449 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001450 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1451 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001452}
1453
1454/* allowed just for 8 bytes segments */
1455static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001456 u16 selector, struct desc_struct *desc)
1457{
1458 struct desc_ptr dt;
1459 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001460 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001461
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001462 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001463
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001464 if (dt.size < index * 8 + 7)
1465 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001466
1467 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001468 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1469 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001470}
1471
Gleb Natapov5601d052011-03-07 14:55:06 +02001472/* Does not support long mode */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001473static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Nadav Amitd1442d82014-09-18 22:39:39 +03001474 u16 selector, int seg, u8 cpl,
1475 bool in_task_switch,
1476 struct desc_struct *desc)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001477{
Avi Kivity869be992012-06-13 16:30:53 +03001478 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001479 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001480 unsigned err_vec = GP_VECTOR;
1481 u32 err_code = 0;
1482 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001483 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001484 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001485 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001486 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001487
1488 memset(&seg_desc, 0, sizeof seg_desc);
1489
Kevin Wolff8da94e2013-04-11 14:06:03 +02001490 if (ctxt->mode == X86EMUL_MODE_REAL) {
1491 /* set real mode segment descriptor (keep limit etc. for
1492 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001493 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001494 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001495 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001496 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1497 /* VM86 needs a clean new segment descriptor */
1498 set_desc_base(&seg_desc, selector << 4);
1499 set_desc_limit(&seg_desc, 0xffff);
1500 seg_desc.type = 3;
1501 seg_desc.p = 1;
1502 seg_desc.s = 1;
1503 seg_desc.dpl = 3;
1504 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001505 }
1506
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001507 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001508
1509 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1510 if ((seg == VCPU_SREG_CS
1511 || (seg == VCPU_SREG_SS
1512 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1513 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001514 && null_selector)
1515 goto exception;
1516
1517 /* TR should be in GDT only */
1518 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1519 goto exception;
1520
1521 if (null_selector) /* for NULL selector skip all following checks */
1522 goto load;
1523
Avi Kivitye9194642012-06-13 16:29:39 +03001524 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001525 if (ret != X86EMUL_CONTINUE)
1526 return ret;
1527
1528 err_code = selector & 0xfffc;
Paolo Bonzini15fc0752014-08-18 13:17:00 +02001529 err_vec = in_task_switch ? TS_VECTOR : GP_VECTOR;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001530
Guo Chaofc058682012-06-28 15:19:51 +08001531 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001532 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1533 goto exception;
1534
1535 if (!seg_desc.p) {
1536 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1537 goto exception;
1538 }
1539
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001540 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001541
1542 switch (seg) {
1543 case VCPU_SREG_SS:
1544 /*
1545 * segment is not a writable data segment or segment
1546 * selector's RPL != CPL or segment selector's RPL != CPL
1547 */
1548 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1549 goto exception;
1550 break;
1551 case VCPU_SREG_CS:
1552 if (!(seg_desc.type & 8))
1553 goto exception;
1554
1555 if (seg_desc.type & 4) {
1556 /* conforming */
1557 if (dpl > cpl)
1558 goto exception;
1559 } else {
1560 /* nonconforming */
1561 if (rpl > cpl || dpl != cpl)
1562 goto exception;
1563 }
Nadav Amit040c8dc2014-09-18 22:39:43 +03001564 /* in long-mode d/b must be clear if l is set */
1565 if (seg_desc.d && seg_desc.l) {
1566 u64 efer = 0;
1567
1568 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1569 if (efer & EFER_LMA)
1570 goto exception;
1571 }
1572
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001573 /* CS(RPL) <- CPL */
1574 selector = (selector & 0xfffc) | cpl;
1575 break;
1576 case VCPU_SREG_TR:
1577 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1578 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001579 old_desc = seg_desc;
1580 seg_desc.type |= 2; /* busy */
1581 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1582 sizeof(seg_desc), &ctxt->exception);
1583 if (ret != X86EMUL_CONTINUE)
1584 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001585 break;
1586 case VCPU_SREG_LDTR:
1587 if (seg_desc.s || seg_desc.type != 2)
1588 goto exception;
1589 break;
1590 default: /* DS, ES, FS, or GS */
1591 /*
1592 * segment is not a data or readable code segment or
1593 * ((segment is a data or nonconforming code segment)
1594 * and (both RPL and CPL > DPL))
1595 */
1596 if ((seg_desc.type & 0xa) == 0x8 ||
1597 (((seg_desc.type & 0xc) != 0xc) &&
1598 (rpl > dpl && cpl > dpl)))
1599 goto exception;
1600 break;
1601 }
1602
1603 if (seg_desc.s) {
1604 /* mark segment as accessed */
1605 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001606 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001607 if (ret != X86EMUL_CONTINUE)
1608 return ret;
Nadav Amite37a75a2014-06-02 18:34:04 +03001609 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
1610 ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3,
1611 sizeof(base3), &ctxt->exception);
1612 if (ret != X86EMUL_CONTINUE)
1613 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001614 }
1615load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001616 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Nadav Amitd1442d82014-09-18 22:39:39 +03001617 if (desc)
1618 *desc = seg_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001619 return X86EMUL_CONTINUE;
1620exception:
Paolo Bonzini592f0852014-08-20 10:05:08 +02001621 return emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001622}
1623
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001624static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1625 u16 selector, int seg)
1626{
1627 u8 cpl = ctxt->ops->cpl(ctxt);
Nadav Amitd1442d82014-09-18 22:39:39 +03001628 return __load_segment_descriptor(ctxt, selector, seg, cpl, false, NULL);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001629}
1630
Wei Yongjun31be40b2010-08-17 09:17:30 +08001631static void write_register_operand(struct operand *op)
1632{
1633 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1634 switch (op->bytes) {
1635 case 1:
1636 *(u8 *)op->addr.reg = (u8)op->val;
1637 break;
1638 case 2:
1639 *(u16 *)op->addr.reg = (u16)op->val;
1640 break;
1641 case 4:
1642 *op->addr.reg = (u32)op->val;
1643 break; /* 64b: zero-extend */
1644 case 8:
1645 *op->addr.reg = op->val;
1646 break;
1647 }
1648}
1649
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001650static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001651{
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001652 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001653 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001654 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001655 break;
1656 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001657 if (ctxt->lock_prefix)
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001658 return segmented_cmpxchg(ctxt,
1659 op->addr.mem,
1660 &op->orig_val,
1661 &op->val,
1662 op->bytes);
1663 else
1664 return segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001665 op->addr.mem,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001666 &op->val,
1667 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001668 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001669 case OP_MEM_STR:
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001670 return segmented_write(ctxt,
1671 op->addr.mem,
1672 op->data,
1673 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001674 break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001675 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001676 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001677 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001678 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001679 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001680 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001681 case OP_NONE:
1682 /* no writeback */
1683 break;
1684 default:
1685 break;
1686 }
1687 return X86EMUL_CONTINUE;
1688}
1689
Avi Kivity51ddff52012-06-12 20:19:40 +03001690static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001691{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001692 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001693
Avi Kivity5ad105e2012-08-19 14:34:31 +03001694 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001695 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001696 addr.seg = VCPU_SREG_SS;
1697
Avi Kivity51ddff52012-06-12 20:19:40 +03001698 return segmented_write(ctxt, addr, data, bytes);
1699}
1700
1701static int em_push(struct x86_emulate_ctxt *ctxt)
1702{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001703 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001704 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001705 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001706}
1707
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001708static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001709 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001710{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001711 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001712 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001713
Avi Kivitydd856ef2012-08-27 23:46:17 +03001714 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001715 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001716 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001717 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001718 return rc;
1719
Avi Kivity5ad105e2012-08-19 14:34:31 +03001720 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001721 return rc;
1722}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001723
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001724static int em_pop(struct x86_emulate_ctxt *ctxt)
1725{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001726 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001727}
1728
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001729static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001730 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001731{
1732 int rc;
1733 unsigned long val, change_mask;
1734 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001735 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001736
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001737 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001738 if (rc != X86EMUL_CONTINUE)
1739 return rc;
1740
1741 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
Nadav Amit163b1352014-07-21 14:37:28 +03001742 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_AC | EFLG_ID;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001743
1744 switch(ctxt->mode) {
1745 case X86EMUL_MODE_PROT64:
1746 case X86EMUL_MODE_PROT32:
1747 case X86EMUL_MODE_PROT16:
1748 if (cpl == 0)
1749 change_mask |= EFLG_IOPL;
1750 if (cpl <= iopl)
1751 change_mask |= EFLG_IF;
1752 break;
1753 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001754 if (iopl < 3)
1755 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001756 change_mask |= EFLG_IF;
1757 break;
1758 default: /* real mode */
1759 change_mask |= (EFLG_IOPL | EFLG_IF);
1760 break;
1761 }
1762
1763 *(unsigned long *)dest =
1764 (ctxt->eflags & ~change_mask) | (val & change_mask);
1765
1766 return rc;
1767}
1768
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001769static int em_popf(struct x86_emulate_ctxt *ctxt)
1770{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001771 ctxt->dst.type = OP_REG;
1772 ctxt->dst.addr.reg = &ctxt->eflags;
1773 ctxt->dst.bytes = ctxt->op_bytes;
1774 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001775}
1776
Avi Kivity612e89f2012-06-12 20:03:23 +03001777static int em_enter(struct x86_emulate_ctxt *ctxt)
1778{
1779 int rc;
1780 unsigned frame_size = ctxt->src.val;
1781 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001782 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001783
1784 if (nesting_level)
1785 return X86EMUL_UNHANDLEABLE;
1786
Avi Kivitydd856ef2012-08-27 23:46:17 +03001787 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1788 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001789 if (rc != X86EMUL_CONTINUE)
1790 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001791 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001792 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001793 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1794 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001795 stack_mask(ctxt));
1796 return X86EMUL_CONTINUE;
1797}
1798
Avi Kivityf47cfa32012-06-07 17:49:24 +03001799static int em_leave(struct x86_emulate_ctxt *ctxt)
1800{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001801 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001802 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001803 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001804}
1805
Avi Kivity1cd196e2011-09-13 10:45:51 +03001806static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001807{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001808 int seg = ctxt->src2.val;
1809
Avi Kivity9dac77f2011-06-01 15:34:25 +03001810 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001811
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001812 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001813}
1814
Avi Kivity1cd196e2011-09-13 10:45:51 +03001815static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001816{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001817 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001818 unsigned long selector;
1819 int rc;
1820
Avi Kivity9dac77f2011-06-01 15:34:25 +03001821 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001822 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001823 return rc;
1824
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001825 if (ctxt->modrm_reg == VCPU_SREG_SS)
1826 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
1827
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001828 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001829 return rc;
1830}
1831
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001832static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001833{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001834 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001835 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001836 int reg = VCPU_REGS_RAX;
1837
1838 while (reg <= VCPU_REGS_RDI) {
1839 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001840 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001841
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001842 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001843 if (rc != X86EMUL_CONTINUE)
1844 return rc;
1845
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001846 ++reg;
1847 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001848
Wei Yongjunc37eda12010-06-15 09:03:33 +08001849 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001850}
1851
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001852static int em_pushf(struct x86_emulate_ctxt *ctxt)
1853{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001854 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001855 return em_push(ctxt);
1856}
1857
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001858static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001859{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001860 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001861 int reg = VCPU_REGS_RDI;
1862
1863 while (reg >= VCPU_REGS_RAX) {
1864 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001865 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001866 --reg;
1867 }
1868
Avi Kivitydd856ef2012-08-27 23:46:17 +03001869 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001870 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001871 break;
1872 --reg;
1873 }
1874 return rc;
1875}
1876
Avi Kivitydd856ef2012-08-27 23:46:17 +03001877static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001878{
Mathias Krause0225fb52012-08-30 01:30:16 +02001879 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001880 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001881 struct desc_ptr dt;
1882 gva_t cs_addr;
1883 gva_t eip_addr;
1884 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001885
1886 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001887 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001888 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001889 if (rc != X86EMUL_CONTINUE)
1890 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001891
1892 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1893
Avi Kivity9dac77f2011-06-01 15:34:25 +03001894 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001895 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001896 if (rc != X86EMUL_CONTINUE)
1897 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001898
Avi Kivity9dac77f2011-06-01 15:34:25 +03001899 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001900 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001901 if (rc != X86EMUL_CONTINUE)
1902 return rc;
1903
Avi Kivity4bff1e862011-04-20 13:37:53 +03001904 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001905
1906 eip_addr = dt.address + (irq << 2);
1907 cs_addr = dt.address + (irq << 2) + 2;
1908
Avi Kivity0f65dd72011-04-20 13:37:53 +03001909 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001910 if (rc != X86EMUL_CONTINUE)
1911 return rc;
1912
Avi Kivity0f65dd72011-04-20 13:37:53 +03001913 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001914 if (rc != X86EMUL_CONTINUE)
1915 return rc;
1916
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001917 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001918 if (rc != X86EMUL_CONTINUE)
1919 return rc;
1920
Avi Kivity9dac77f2011-06-01 15:34:25 +03001921 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001922
1923 return rc;
1924}
1925
Avi Kivitydd856ef2012-08-27 23:46:17 +03001926int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1927{
1928 int rc;
1929
1930 invalidate_registers(ctxt);
1931 rc = __emulate_int_real(ctxt, irq);
1932 if (rc == X86EMUL_CONTINUE)
1933 writeback_registers(ctxt);
1934 return rc;
1935}
1936
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001937static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001938{
1939 switch(ctxt->mode) {
1940 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001941 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001942 case X86EMUL_MODE_VM86:
1943 case X86EMUL_MODE_PROT16:
1944 case X86EMUL_MODE_PROT32:
1945 case X86EMUL_MODE_PROT64:
1946 default:
1947 /* Protected mode interrupts unimplemented yet */
1948 return X86EMUL_UNHANDLEABLE;
1949 }
1950}
1951
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001952static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001953{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001954 int rc = X86EMUL_CONTINUE;
1955 unsigned long temp_eip = 0;
1956 unsigned long temp_eflags = 0;
1957 unsigned long cs = 0;
1958 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1959 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1960 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1961 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1962
1963 /* TODO: Add stack limit check */
1964
Avi Kivity9dac77f2011-06-01 15:34:25 +03001965 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001966
1967 if (rc != X86EMUL_CONTINUE)
1968 return rc;
1969
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001970 if (temp_eip & ~0xffff)
1971 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001972
Avi Kivity9dac77f2011-06-01 15:34:25 +03001973 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001974
1975 if (rc != X86EMUL_CONTINUE)
1976 return rc;
1977
Avi Kivity9dac77f2011-06-01 15:34:25 +03001978 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001979
1980 if (rc != X86EMUL_CONTINUE)
1981 return rc;
1982
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001983 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001984
1985 if (rc != X86EMUL_CONTINUE)
1986 return rc;
1987
Avi Kivity9dac77f2011-06-01 15:34:25 +03001988 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001989
1990
Avi Kivity9dac77f2011-06-01 15:34:25 +03001991 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001992 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001993 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001994 ctxt->eflags &= ~0xffff;
1995 ctxt->eflags |= temp_eflags;
1996 }
1997
1998 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1999 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
2000
2001 return rc;
2002}
2003
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002004static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002005{
2006 switch(ctxt->mode) {
2007 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002008 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002009 case X86EMUL_MODE_VM86:
2010 case X86EMUL_MODE_PROT16:
2011 case X86EMUL_MODE_PROT32:
2012 case X86EMUL_MODE_PROT64:
2013 default:
2014 /* iret from protected mode unimplemented yet */
2015 return X86EMUL_UNHANDLEABLE;
2016 }
2017}
2018
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002019static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2020{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002021 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002022 unsigned short sel, old_sel;
2023 struct desc_struct old_desc, new_desc;
2024 const struct x86_emulate_ops *ops = ctxt->ops;
2025 u8 cpl = ctxt->ops->cpl(ctxt);
2026
2027 /* Assignment of RIP may only fail in 64-bit mode */
2028 if (ctxt->mode == X86EMUL_MODE_PROT64)
2029 ops->get_segment(ctxt, &old_sel, &old_desc, NULL,
2030 VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002031
Avi Kivity9dac77f2011-06-01 15:34:25 +03002032 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002033
Nadav Amitd1442d82014-09-18 22:39:39 +03002034 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl, false,
2035 &new_desc);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002036 if (rc != X86EMUL_CONTINUE)
2037 return rc;
2038
Nadav Amitd1442d82014-09-18 22:39:39 +03002039 rc = assign_eip_far(ctxt, ctxt->src.val, new_desc.l);
2040 if (rc != X86EMUL_CONTINUE) {
Nadav Amitcd9b8e2c2014-10-28 00:03:43 +02002041 WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64);
Nadav Amitd1442d82014-09-18 22:39:39 +03002042 /* assigning eip failed; restore the old cs */
2043 ops->set_segment(ctxt, old_sel, &old_desc, 0, VCPU_SREG_CS);
2044 return rc;
2045 }
2046 return rc;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002047}
2048
Nadav Amitf7784042014-09-18 22:39:41 +03002049static int em_jmp_abs(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002050{
Nadav Amitf7784042014-09-18 22:39:41 +03002051 return assign_eip_near(ctxt, ctxt->src.val);
2052}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002053
Nadav Amitf7784042014-09-18 22:39:41 +03002054static int em_call_near_abs(struct x86_emulate_ctxt *ctxt)
2055{
2056 int rc;
2057 long int old_eip;
2058
2059 old_eip = ctxt->_eip;
2060 rc = assign_eip_near(ctxt, ctxt->src.val);
2061 if (rc != X86EMUL_CONTINUE)
2062 return rc;
2063 ctxt->src.val = old_eip;
2064 rc = em_push(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002065 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002066}
2067
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002068static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002069{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002070 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002071
Nadav Amitaaa05f22014-06-02 18:34:10 +03002072 if (ctxt->dst.bytes == 16)
2073 return X86EMUL_UNHANDLEABLE;
2074
Avi Kivitydd856ef2012-08-27 23:46:17 +03002075 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2076 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2077 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2078 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002079 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002080 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002081 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2082 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002083
Laurent Vivier05f086f2007-09-24 11:10:55 +02002084 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002085 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002086 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002087}
2088
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002089static int em_ret(struct x86_emulate_ctxt *ctxt)
2090{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002091 int rc;
2092 unsigned long eip;
2093
2094 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
2095 if (rc != X86EMUL_CONTINUE)
2096 return rc;
2097
2098 return assign_eip_near(ctxt, eip);
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002099}
2100
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002101static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002102{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002103 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002104 unsigned long eip, cs;
2105 u16 old_cs;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002106 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amitd1442d82014-09-18 22:39:39 +03002107 struct desc_struct old_desc, new_desc;
2108 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002109
Nadav Amitd1442d82014-09-18 22:39:39 +03002110 if (ctxt->mode == X86EMUL_MODE_PROT64)
2111 ops->get_segment(ctxt, &old_cs, &old_desc, NULL,
2112 VCPU_SREG_CS);
2113
2114 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002115 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002116 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002117 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002118 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002119 return rc;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002120 /* Outer-privilege level return is not implemented */
2121 if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2122 return X86EMUL_UNHANDLEABLE;
Nadav Amitd1442d82014-09-18 22:39:39 +03002123 rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, 0, false,
2124 &new_desc);
2125 if (rc != X86EMUL_CONTINUE)
2126 return rc;
2127 rc = assign_eip_far(ctxt, eip, new_desc.l);
2128 if (rc != X86EMUL_CONTINUE) {
Nadav Amitcd9b8e2c2014-10-28 00:03:43 +02002129 WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64);
Nadav Amitd1442d82014-09-18 22:39:39 +03002130 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
2131 }
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002132 return rc;
2133}
2134
Bruce Rogers32611072013-09-09 09:40:20 -06002135static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2136{
2137 int rc;
2138
2139 rc = em_ret_far(ctxt);
2140 if (rc != X86EMUL_CONTINUE)
2141 return rc;
2142 rsp_increment(ctxt, ctxt->src.val);
2143 return X86EMUL_CONTINUE;
2144}
2145
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002146static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2147{
2148 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002149 ctxt->dst.orig_val = ctxt->dst.val;
2150 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002151 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002152 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002153 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002154
2155 if (ctxt->eflags & EFLG_ZF) {
2156 /* Success: write back to memory. */
2157 ctxt->dst.val = ctxt->src.orig_val;
2158 } else {
2159 /* Failure: write the value we saw to EAX. */
2160 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002161 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Nadav Amit37c564f2014-06-02 18:34:07 +03002162 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002163 }
2164 return X86EMUL_CONTINUE;
2165}
2166
Avi Kivityd4b43252011-09-13 10:45:50 +03002167static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002168{
Avi Kivityd4b43252011-09-13 10:45:50 +03002169 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002170 unsigned short sel;
2171 int rc;
2172
Avi Kivity9dac77f2011-06-01 15:34:25 +03002173 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002174
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002175 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002176 if (rc != X86EMUL_CONTINUE)
2177 return rc;
2178
Avi Kivity9dac77f2011-06-01 15:34:25 +03002179 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002180 return rc;
2181}
2182
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002183static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002184setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002185 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002186{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002187 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002188 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002189 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002190 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002191 cs->type = 0x0b; /* Read, Execute, Accessed */
2192 cs->s = 1;
2193 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002194 cs->p = 1;
2195 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002196 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002197
Gleb Natapov79168fd2010-04-28 19:15:30 +03002198 set_desc_base(ss, 0); /* flat segment */
2199 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002200 ss->g = 1; /* 4kb granularity */
2201 ss->s = 1;
2202 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002203 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002204 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002205 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002206 ss->l = 0;
2207 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002208}
2209
Avi Kivity1a18a692012-02-01 12:23:21 +02002210static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2211{
2212 u32 eax, ebx, ecx, edx;
2213
2214 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002215 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2216 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002217 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2218 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2219}
2220
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002221static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2222{
Mathias Krause0225fb52012-08-30 01:30:16 +02002223 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002224 u32 eax, ebx, ecx, edx;
2225
2226 /*
2227 * syscall should always be enabled in longmode - so only become
2228 * vendor specific (cpuid) if other modes are active...
2229 */
2230 if (ctxt->mode == X86EMUL_MODE_PROT64)
2231 return true;
2232
2233 eax = 0x00000000;
2234 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002235 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2236 /*
2237 * Intel ("GenuineIntel")
2238 * remark: Intel CPUs only support "syscall" in 64bit
2239 * longmode. Also an 64bit guest with a
2240 * 32bit compat-app running will #UD !! While this
2241 * behaviour can be fixed (by emulating) into AMD
2242 * response - CPUs of AMD can't behave like Intel.
2243 */
2244 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2245 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2246 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2247 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002248
Avi Kivity0017f932012-06-07 14:10:16 +03002249 /* AMD ("AuthenticAMD") */
2250 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2251 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2252 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2253 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002254
Avi Kivity0017f932012-06-07 14:10:16 +03002255 /* AMD ("AMDisbetter!") */
2256 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2257 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2258 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2259 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002260
2261 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2262 return false;
2263}
2264
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002265static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002266{
Mathias Krause0225fb52012-08-30 01:30:16 +02002267 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002268 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002269 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002270 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002271 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002272
2273 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002274 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002275 ctxt->mode == X86EMUL_MODE_VM86)
2276 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002277
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002278 if (!(em_syscall_is_enabled(ctxt)))
2279 return emulate_ud(ctxt);
2280
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002281 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002282 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002283
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002284 if (!(efer & EFER_SCE))
2285 return emulate_ud(ctxt);
2286
Avi Kivity717746e2011-04-20 13:37:53 +03002287 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002288 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002289 cs_sel = (u16)(msr_data & 0xfffc);
2290 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002291
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002292 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002293 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002294 cs.l = 1;
2295 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002296 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2297 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002298
Avi Kivitydd856ef2012-08-27 23:46:17 +03002299 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002300 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002301#ifdef CONFIG_X86_64
Nadav Amit6c6cb692014-07-21 14:37:30 +03002302 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002303
Avi Kivity717746e2011-04-20 13:37:53 +03002304 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002305 ctxt->mode == X86EMUL_MODE_PROT64 ?
2306 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002307 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002308
Avi Kivity717746e2011-04-20 13:37:53 +03002309 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Nadav Amit6c6cb692014-07-21 14:37:30 +03002310 ctxt->eflags &= ~msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002311#endif
2312 } else {
2313 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002314 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002315 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002316
Nadav Amit6c6cb692014-07-21 14:37:30 +03002317 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002318 }
2319
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002320 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002321}
2322
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002323static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002324{
Mathias Krause0225fb52012-08-30 01:30:16 +02002325 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002326 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002327 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002328 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002329 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002330
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002331 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002332 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002333 if (ctxt->mode == X86EMUL_MODE_REAL)
2334 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002335
Avi Kivity1a18a692012-02-01 12:23:21 +02002336 /*
2337 * Not recognized on AMD in compat mode (but is recognized in legacy
2338 * mode).
2339 */
2340 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2341 && !vendor_intel(ctxt))
2342 return emulate_ud(ctxt);
2343
Andre Przywara8c604352009-06-18 12:56:01 +02002344 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2345 * Therefore, we inject an #UD.
2346 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002347 if (ctxt->mode == X86EMUL_MODE_PROT64)
2348 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002349
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002350 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002351
Avi Kivity717746e2011-04-20 13:37:53 +03002352 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002353 switch (ctxt->mode) {
2354 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002355 if ((msr_data & 0xfffc) == 0x0)
2356 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002357 break;
2358 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002359 if (msr_data == 0x0)
2360 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002361 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002362 default:
2363 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002364 }
2365
Nadav Amit6c6cb692014-07-21 14:37:30 +03002366 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002367 cs_sel = (u16)msr_data;
2368 cs_sel &= ~SELECTOR_RPL_MASK;
2369 ss_sel = cs_sel + 8;
2370 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002371 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002372 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002373 cs.l = 1;
2374 }
2375
Avi Kivity1aa36612011-04-27 13:20:30 +03002376 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2377 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002378
Avi Kivity717746e2011-04-20 13:37:53 +03002379 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002380 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002381
Avi Kivity717746e2011-04-20 13:37:53 +03002382 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002383 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002384
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002385 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002386}
2387
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002388static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002389{
Mathias Krause0225fb52012-08-30 01:30:16 +02002390 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002391 struct desc_struct cs, ss;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002392 u64 msr_data, rcx, rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002393 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002394 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002395
Gleb Natapova0044752010-02-10 14:21:31 +02002396 /* inject #GP if in real mode or Virtual 8086 mode */
2397 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002398 ctxt->mode == X86EMUL_MODE_VM86)
2399 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002400
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002401 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002402
Avi Kivity9dac77f2011-06-01 15:34:25 +03002403 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002404 usermode = X86EMUL_MODE_PROT64;
2405 else
2406 usermode = X86EMUL_MODE_PROT32;
2407
Nadav Amit234f3ce2014-09-18 22:39:38 +03002408 rcx = reg_read(ctxt, VCPU_REGS_RCX);
2409 rdx = reg_read(ctxt, VCPU_REGS_RDX);
2410
Andre Przywara4668f052009-06-18 12:56:02 +02002411 cs.dpl = 3;
2412 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002413 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002414 switch (usermode) {
2415 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002416 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002417 if ((msr_data & 0xfffc) == 0x0)
2418 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002419 ss_sel = (u16)(msr_data + 24);
Nadav Amitbf0b6822014-09-18 22:39:45 +03002420 rcx = (u32)rcx;
2421 rdx = (u32)rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002422 break;
2423 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002424 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002425 if (msr_data == 0x0)
2426 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002427 ss_sel = cs_sel + 8;
2428 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002429 cs.l = 1;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002430 if (is_noncanonical_address(rcx) ||
2431 is_noncanonical_address(rdx))
2432 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002433 break;
2434 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002435 cs_sel |= SELECTOR_RPL_MASK;
2436 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002437
Avi Kivity1aa36612011-04-27 13:20:30 +03002438 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2439 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002440
Nadav Amit234f3ce2014-09-18 22:39:38 +03002441 ctxt->_eip = rdx;
2442 *reg_write(ctxt, VCPU_REGS_RSP) = rcx;
Andre Przywara4668f052009-06-18 12:56:02 +02002443
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002444 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002445}
2446
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002447static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002448{
2449 int iopl;
2450 if (ctxt->mode == X86EMUL_MODE_REAL)
2451 return false;
2452 if (ctxt->mode == X86EMUL_MODE_VM86)
2453 return true;
2454 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002455 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002456}
2457
2458static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002459 u16 port, u16 len)
2460{
Mathias Krause0225fb52012-08-30 01:30:16 +02002461 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002462 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002463 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002464 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002465 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002466 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002467 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002468
Avi Kivity1aa36612011-04-27 13:20:30 +03002469 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002470 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002471 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002472 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002473 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002474 base = get_desc_base(&tr_seg);
2475#ifdef CONFIG_X86_64
2476 base |= ((u64)base3) << 32;
2477#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002478 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002479 if (r != X86EMUL_CONTINUE)
2480 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002481 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002482 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002483 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002484 if (r != X86EMUL_CONTINUE)
2485 return false;
2486 if ((perm >> bit_idx) & mask)
2487 return false;
2488 return true;
2489}
2490
2491static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002492 u16 port, u16 len)
2493{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002494 if (ctxt->perm_ok)
2495 return true;
2496
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002497 if (emulator_bad_iopl(ctxt))
2498 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002499 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002500
2501 ctxt->perm_ok = true;
2502
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002503 return true;
2504}
2505
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002506static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002507 struct tss_segment_16 *tss)
2508{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002509 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002510 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002511 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2512 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2513 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2514 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2515 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2516 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2517 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2518 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002519
Avi Kivity1aa36612011-04-27 13:20:30 +03002520 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2521 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2522 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2523 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2524 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002525}
2526
2527static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002528 struct tss_segment_16 *tss)
2529{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002530 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002531 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002532
Avi Kivity9dac77f2011-06-01 15:34:25 +03002533 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002534 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002535 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2536 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2537 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2538 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2539 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2540 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2541 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2542 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002543
2544 /*
2545 * SDM says that segment selectors are loaded before segment
2546 * descriptors
2547 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002548 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2549 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2550 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2551 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2552 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002553
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002554 cpl = tss->cs & 3;
2555
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002556 /*
Guo Chaofc058682012-06-28 15:19:51 +08002557 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002558 * it is handled in a context of new task
2559 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002560 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl,
2561 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002562 if (ret != X86EMUL_CONTINUE)
2563 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002564 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
2565 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002566 if (ret != X86EMUL_CONTINUE)
2567 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002568 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
2569 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002570 if (ret != X86EMUL_CONTINUE)
2571 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002572 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
2573 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002574 if (ret != X86EMUL_CONTINUE)
2575 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002576 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
2577 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002578 if (ret != X86EMUL_CONTINUE)
2579 return ret;
2580
2581 return X86EMUL_CONTINUE;
2582}
2583
2584static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002585 u16 tss_selector, u16 old_tss_sel,
2586 ulong old_tss_base, struct desc_struct *new_desc)
2587{
Mathias Krause0225fb52012-08-30 01:30:16 +02002588 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002589 struct tss_segment_16 tss_seg;
2590 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002591 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002592
Avi Kivity0f65dd72011-04-20 13:37:53 +03002593 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002594 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002595 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002596 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002597 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002598
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002599 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002600
Avi Kivity0f65dd72011-04-20 13:37:53 +03002601 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002602 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002603 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002604 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002605 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002606
Avi Kivity0f65dd72011-04-20 13:37:53 +03002607 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002608 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002609 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002610 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002611 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002612
2613 if (old_tss_sel != 0xffff) {
2614 tss_seg.prev_task_link = old_tss_sel;
2615
Avi Kivity0f65dd72011-04-20 13:37:53 +03002616 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002617 &tss_seg.prev_task_link,
2618 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002619 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002620 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002621 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002622 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002623 }
2624
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002625 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002626}
2627
2628static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002629 struct tss_segment_32 *tss)
2630{
Nadav Amit5c7411e2014-04-07 18:37:47 +03002631 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002632 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002633 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002634 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2635 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2636 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2637 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2638 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2639 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2640 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2641 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002642
Avi Kivity1aa36612011-04-27 13:20:30 +03002643 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2644 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2645 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2646 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2647 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2648 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002649}
2650
2651static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002652 struct tss_segment_32 *tss)
2653{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002654 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002655 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002656
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002657 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002658 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002659 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002660 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002661
2662 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002663 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2664 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2665 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2666 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2667 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2668 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2669 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2670 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002671
2672 /*
2673 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002674 * descriptors. This is important because CPL checks will
2675 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002676 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002677 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2678 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2679 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2680 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2681 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2682 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2683 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002684
2685 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002686 * If we're switching between Protected Mode and VM86, we need to make
2687 * sure to update the mode before loading the segment descriptors so
2688 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01002689 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002690 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002691 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002692 cpl = 3;
2693 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002694 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002695 cpl = tss->cs & 3;
2696 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01002697
2698 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002699 * Now load segment descriptors. If fault happenes at this stage
2700 * it is handled in a context of new task
2701 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002702 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR,
2703 cpl, true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002704 if (ret != X86EMUL_CONTINUE)
2705 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002706 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
2707 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002708 if (ret != X86EMUL_CONTINUE)
2709 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002710 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
2711 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002712 if (ret != X86EMUL_CONTINUE)
2713 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002714 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
2715 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002716 if (ret != X86EMUL_CONTINUE)
2717 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002718 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
2719 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002720 if (ret != X86EMUL_CONTINUE)
2721 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002722 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl,
2723 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002724 if (ret != X86EMUL_CONTINUE)
2725 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002726 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl,
2727 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002728 if (ret != X86EMUL_CONTINUE)
2729 return ret;
2730
2731 return X86EMUL_CONTINUE;
2732}
2733
2734static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002735 u16 tss_selector, u16 old_tss_sel,
2736 ulong old_tss_base, struct desc_struct *new_desc)
2737{
Mathias Krause0225fb52012-08-30 01:30:16 +02002738 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002739 struct tss_segment_32 tss_seg;
2740 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002741 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03002742 u32 eip_offset = offsetof(struct tss_segment_32, eip);
2743 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002744
Avi Kivity0f65dd72011-04-20 13:37:53 +03002745 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002746 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002747 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002748 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002749 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002750
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002751 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002752
Nadav Amit5c7411e2014-04-07 18:37:47 +03002753 /* Only GP registers and segment selectors are saved */
2754 ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
2755 ldt_sel_offset - eip_offset, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002756 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002757 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002758 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002759
Avi Kivity0f65dd72011-04-20 13:37:53 +03002760 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002761 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002762 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002763 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002764 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002765
2766 if (old_tss_sel != 0xffff) {
2767 tss_seg.prev_task_link = old_tss_sel;
2768
Avi Kivity0f65dd72011-04-20 13:37:53 +03002769 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002770 &tss_seg.prev_task_link,
2771 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002772 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002773 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002774 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002775 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002776 }
2777
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002778 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002779}
2780
2781static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002782 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002783 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002784{
Mathias Krause0225fb52012-08-30 01:30:16 +02002785 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002786 struct desc_struct curr_tss_desc, next_tss_desc;
2787 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002788 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002789 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002790 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002791 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002792 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002793
2794 /* FIXME: old_tss_base == ~0 ? */
2795
Avi Kivitye9194642012-06-13 16:29:39 +03002796 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002797 if (ret != X86EMUL_CONTINUE)
2798 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002799 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002800 if (ret != X86EMUL_CONTINUE)
2801 return ret;
2802
2803 /* FIXME: check that next_tss_desc is tss */
2804
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002805 /*
2806 * Check privileges. The three cases are task switch caused by...
2807 *
2808 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2809 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002810 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002811 */
2812 if (reason == TASK_SWITCH_GATE) {
2813 if (idt_index != -1) {
2814 /* Software interrupts */
2815 struct desc_struct task_gate_desc;
2816 int dpl;
2817
2818 ret = read_interrupt_descriptor(ctxt, idt_index,
2819 &task_gate_desc);
2820 if (ret != X86EMUL_CONTINUE)
2821 return ret;
2822
2823 dpl = task_gate_desc.dpl;
2824 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2825 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2826 }
2827 } else if (reason != TASK_SWITCH_IRET) {
2828 int dpl = next_tss_desc.dpl;
2829 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2830 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002831 }
2832
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002833
Gleb Natapovceffb452010-03-18 15:20:19 +02002834 desc_limit = desc_limit_scaled(&next_tss_desc);
2835 if (!next_tss_desc.p ||
2836 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2837 desc_limit < 0x2b)) {
Paolo Bonzini592f0852014-08-20 10:05:08 +02002838 return emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002839 }
2840
2841 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2842 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002843 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002844 }
2845
2846 if (reason == TASK_SWITCH_IRET)
2847 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2848
2849 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002850 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002851 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2852 old_tss_sel = 0xffff;
2853
2854 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002855 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002856 old_tss_base, &next_tss_desc);
2857 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002858 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002859 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002860 if (ret != X86EMUL_CONTINUE)
2861 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002862
2863 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2864 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2865
2866 if (reason != TASK_SWITCH_IRET) {
2867 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002868 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002869 }
2870
Avi Kivity717746e2011-04-20 13:37:53 +03002871 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002872 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002873
Jan Kiszkae269fb22010-04-14 15:51:09 +02002874 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002875 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2876 ctxt->lock_prefix = 0;
2877 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002878 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002879 }
2880
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002881 return ret;
2882}
2883
2884int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002885 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002886 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002887{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002888 int rc;
2889
Avi Kivitydd856ef2012-08-27 23:46:17 +03002890 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002891 ctxt->_eip = ctxt->eip;
2892 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002893
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002894 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002895 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002896
Avi Kivitydd856ef2012-08-27 23:46:17 +03002897 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002898 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002899 writeback_registers(ctxt);
2900 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002901
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002902 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002903}
2904
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002905static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2906 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002907{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002908 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002909
Avi Kivitydd856ef2012-08-27 23:46:17 +03002910 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2911 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002912}
2913
Avi Kivity7af04fc2010-08-18 14:16:35 +03002914static int em_das(struct x86_emulate_ctxt *ctxt)
2915{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002916 u8 al, old_al;
2917 bool af, cf, old_cf;
2918
2919 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002920 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002921
2922 old_al = al;
2923 old_cf = cf;
2924 cf = false;
2925 af = ctxt->eflags & X86_EFLAGS_AF;
2926 if ((al & 0x0f) > 9 || af) {
2927 al -= 6;
2928 cf = old_cf | (al >= 250);
2929 af = true;
2930 } else {
2931 af = false;
2932 }
2933 if (old_al > 0x99 || old_cf) {
2934 al -= 0x60;
2935 cf = true;
2936 }
2937
Avi Kivity9dac77f2011-06-01 15:34:25 +03002938 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002939 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002940 ctxt->src.type = OP_IMM;
2941 ctxt->src.val = 0;
2942 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02002943 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03002944 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2945 if (cf)
2946 ctxt->eflags |= X86_EFLAGS_CF;
2947 if (af)
2948 ctxt->eflags |= X86_EFLAGS_AF;
2949 return X86EMUL_CONTINUE;
2950}
2951
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02002952static int em_aam(struct x86_emulate_ctxt *ctxt)
2953{
2954 u8 al, ah;
2955
2956 if (ctxt->src.val == 0)
2957 return emulate_de(ctxt);
2958
2959 al = ctxt->dst.val & 0xff;
2960 ah = al / ctxt->src.val;
2961 al %= ctxt->src.val;
2962
2963 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
2964
2965 /* Set PF, ZF, SF */
2966 ctxt->src.type = OP_IMM;
2967 ctxt->src.val = 0;
2968 ctxt->src.bytes = 1;
2969 fastop(ctxt, em_or);
2970
2971 return X86EMUL_CONTINUE;
2972}
2973
Gleb Natapov7f662272012-12-10 11:42:30 +02002974static int em_aad(struct x86_emulate_ctxt *ctxt)
2975{
2976 u8 al = ctxt->dst.val & 0xff;
2977 u8 ah = (ctxt->dst.val >> 8) & 0xff;
2978
2979 al = (al + (ah * ctxt->src.val)) & 0xff;
2980
2981 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
2982
Gleb Natapovf583c292013-02-13 17:50:39 +02002983 /* Set PF, ZF, SF */
2984 ctxt->src.type = OP_IMM;
2985 ctxt->src.val = 0;
2986 ctxt->src.bytes = 1;
2987 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02002988
2989 return X86EMUL_CONTINUE;
2990}
2991
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002992static int em_call(struct x86_emulate_ctxt *ctxt)
2993{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002994 int rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002995 long rel = ctxt->src.val;
2996
2997 ctxt->src.val = (unsigned long)ctxt->_eip;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002998 rc = jmp_rel(ctxt, rel);
2999 if (rc != X86EMUL_CONTINUE)
3000 return rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003001 return em_push(ctxt);
3002}
3003
Avi Kivity0ef753b2010-08-18 14:51:45 +03003004static int em_call_far(struct x86_emulate_ctxt *ctxt)
3005{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003006 u16 sel, old_cs;
3007 ulong old_eip;
3008 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03003009 struct desc_struct old_desc, new_desc;
3010 const struct x86_emulate_ops *ops = ctxt->ops;
3011 int cpl = ctxt->ops->cpl(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003012
Avi Kivity9dac77f2011-06-01 15:34:25 +03003013 old_eip = ctxt->_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003014 ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003015
Avi Kivity9dac77f2011-06-01 15:34:25 +03003016 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Nadav Amitd1442d82014-09-18 22:39:39 +03003017 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl, false,
3018 &new_desc);
3019 if (rc != X86EMUL_CONTINUE)
Avi Kivity0ef753b2010-08-18 14:51:45 +03003020 return X86EMUL_CONTINUE;
3021
Nadav Amitd1442d82014-09-18 22:39:39 +03003022 rc = assign_eip_far(ctxt, ctxt->src.val, new_desc.l);
3023 if (rc != X86EMUL_CONTINUE)
3024 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003025
Avi Kivity9dac77f2011-06-01 15:34:25 +03003026 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003027 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003028 if (rc != X86EMUL_CONTINUE)
Nadav Amitd1442d82014-09-18 22:39:39 +03003029 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003030
Avi Kivity9dac77f2011-06-01 15:34:25 +03003031 ctxt->src.val = old_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003032 rc = em_push(ctxt);
3033 /* If we failed, we tainted the memory, but the very least we should
3034 restore cs */
3035 if (rc != X86EMUL_CONTINUE)
3036 goto fail;
3037 return rc;
3038fail:
3039 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
3040 return rc;
3041
Avi Kivity0ef753b2010-08-18 14:51:45 +03003042}
3043
Avi Kivity40ece7c2010-08-18 15:12:09 +03003044static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3045{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003046 int rc;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003047 unsigned long eip;
Avi Kivity40ece7c2010-08-18 15:12:09 +03003048
Nadav Amit234f3ce2014-09-18 22:39:38 +03003049 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
3050 if (rc != X86EMUL_CONTINUE)
3051 return rc;
3052 rc = assign_eip_near(ctxt, eip);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003053 if (rc != X86EMUL_CONTINUE)
3054 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003055 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003056 return X86EMUL_CONTINUE;
3057}
3058
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003059static int em_xchg(struct x86_emulate_ctxt *ctxt)
3060{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003061 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003062 ctxt->src.val = ctxt->dst.val;
3063 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003064
3065 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003066 ctxt->dst.val = ctxt->src.orig_val;
3067 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003068 return X86EMUL_CONTINUE;
3069}
3070
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003071static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3072{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003073 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003074 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003075}
3076
Avi Kivity61429142010-08-19 15:13:00 +03003077static int em_cwd(struct x86_emulate_ctxt *ctxt)
3078{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003079 ctxt->dst.type = OP_REG;
3080 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003081 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003082 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003083
3084 return X86EMUL_CONTINUE;
3085}
3086
Avi Kivity48bb5d32010-08-18 18:54:34 +03003087static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3088{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003089 u64 tsc = 0;
3090
Avi Kivity717746e2011-04-20 13:37:53 +03003091 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003092 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3093 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003094 return X86EMUL_CONTINUE;
3095}
3096
Avi Kivity222d21a2011-11-10 14:57:30 +02003097static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3098{
3099 u64 pmc;
3100
Avi Kivitydd856ef2012-08-27 23:46:17 +03003101 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003102 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003103 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3104 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003105 return X86EMUL_CONTINUE;
3106}
3107
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003108static int em_mov(struct x86_emulate_ctxt *ctxt)
3109{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01003110 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003111 return X86EMUL_CONTINUE;
3112}
3113
Borislav Petkov84cffe42013-10-29 12:54:56 +01003114#define FFL(x) bit(X86_FEATURE_##x)
3115
3116static int em_movbe(struct x86_emulate_ctxt *ctxt)
3117{
3118 u32 ebx, ecx, edx, eax = 1;
3119 u16 tmp;
3120
3121 /*
3122 * Check MOVBE is set in the guest-visible CPUID leaf.
3123 */
3124 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
3125 if (!(ecx & FFL(MOVBE)))
3126 return emulate_ud(ctxt);
3127
3128 switch (ctxt->op_bytes) {
3129 case 2:
3130 /*
3131 * From MOVBE definition: "...When the operand size is 16 bits,
3132 * the upper word of the destination register remains unchanged
3133 * ..."
3134 *
3135 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3136 * rules so we have to do the operation almost per hand.
3137 */
3138 tmp = (u16)ctxt->src.val;
3139 ctxt->dst.val &= ~0xffffUL;
3140 ctxt->dst.val |= (unsigned long)swab16(tmp);
3141 break;
3142 case 4:
3143 ctxt->dst.val = swab32((u32)ctxt->src.val);
3144 break;
3145 case 8:
3146 ctxt->dst.val = swab64(ctxt->src.val);
3147 break;
3148 default:
Paolo Bonzini592f0852014-08-20 10:05:08 +02003149 BUG();
Borislav Petkov84cffe42013-10-29 12:54:56 +01003150 }
3151 return X86EMUL_CONTINUE;
3152}
3153
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003154static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3155{
3156 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3157 return emulate_gp(ctxt, 0);
3158
3159 /* Disable writeback. */
3160 ctxt->dst.type = OP_NONE;
3161 return X86EMUL_CONTINUE;
3162}
3163
3164static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3165{
3166 unsigned long val;
3167
3168 if (ctxt->mode == X86EMUL_MODE_PROT64)
3169 val = ctxt->src.val & ~0ULL;
3170 else
3171 val = ctxt->src.val & ~0U;
3172
3173 /* #UD condition is already handled. */
3174 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3175 return emulate_gp(ctxt, 0);
3176
3177 /* Disable writeback. */
3178 ctxt->dst.type = OP_NONE;
3179 return X86EMUL_CONTINUE;
3180}
3181
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003182static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3183{
3184 u64 msr_data;
3185
Avi Kivitydd856ef2012-08-27 23:46:17 +03003186 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3187 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3188 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003189 return emulate_gp(ctxt, 0);
3190
3191 return X86EMUL_CONTINUE;
3192}
3193
3194static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3195{
3196 u64 msr_data;
3197
Avi Kivitydd856ef2012-08-27 23:46:17 +03003198 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003199 return emulate_gp(ctxt, 0);
3200
Avi Kivitydd856ef2012-08-27 23:46:17 +03003201 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3202 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003203 return X86EMUL_CONTINUE;
3204}
3205
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003206static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3207{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003208 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003209 return emulate_ud(ctxt);
3210
Avi Kivity9dac77f2011-06-01 15:34:25 +03003211 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003212 return X86EMUL_CONTINUE;
3213}
3214
3215static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3216{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003217 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003218
Avi Kivity9dac77f2011-06-01 15:34:25 +03003219 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003220 return emulate_ud(ctxt);
3221
Avi Kivity9dac77f2011-06-01 15:34:25 +03003222 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003223 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3224
3225 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003226 ctxt->dst.type = OP_NONE;
3227 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003228}
3229
Avi Kivitya14e5792012-06-13 12:28:33 +03003230static int em_lldt(struct x86_emulate_ctxt *ctxt)
3231{
3232 u16 sel = ctxt->src.val;
3233
3234 /* Disable writeback. */
3235 ctxt->dst.type = OP_NONE;
3236 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3237}
3238
Avi Kivity80890002012-06-13 16:33:29 +03003239static int em_ltr(struct x86_emulate_ctxt *ctxt)
3240{
3241 u16 sel = ctxt->src.val;
3242
3243 /* Disable writeback. */
3244 ctxt->dst.type = OP_NONE;
3245 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3246}
3247
Avi Kivity38503912011-03-31 18:48:09 +02003248static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3249{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003250 int rc;
3251 ulong linear;
3252
Avi Kivity9dac77f2011-06-01 15:34:25 +03003253 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003254 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003255 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003256 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003257 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003258 return X86EMUL_CONTINUE;
3259}
3260
Avi Kivity2d04a052011-04-20 15:32:49 +03003261static int em_clts(struct x86_emulate_ctxt *ctxt)
3262{
3263 ulong cr0;
3264
3265 cr0 = ctxt->ops->get_cr(ctxt, 0);
3266 cr0 &= ~X86_CR0_TS;
3267 ctxt->ops->set_cr(ctxt, 0, cr0);
3268 return X86EMUL_CONTINUE;
3269}
3270
Avi Kivity26d05cc2011-04-21 12:07:59 +03003271static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3272{
Nadav Amit0f54a322014-08-29 11:26:55 +03003273 int rc = ctxt->ops->fix_hypercall(ctxt);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003274
Avi Kivity26d05cc2011-04-21 12:07:59 +03003275 if (rc != X86EMUL_CONTINUE)
3276 return rc;
3277
3278 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003279 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003280 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003281 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003282 return X86EMUL_CONTINUE;
3283}
3284
Avi Kivity96051572012-06-10 17:21:18 +03003285static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3286 void (*get)(struct x86_emulate_ctxt *ctxt,
3287 struct desc_ptr *ptr))
3288{
3289 struct desc_ptr desc_ptr;
3290
3291 if (ctxt->mode == X86EMUL_MODE_PROT64)
3292 ctxt->op_bytes = 8;
3293 get(ctxt, &desc_ptr);
3294 if (ctxt->op_bytes == 2) {
3295 ctxt->op_bytes = 4;
3296 desc_ptr.address &= 0x00ffffff;
3297 }
3298 /* Disable writeback. */
3299 ctxt->dst.type = OP_NONE;
3300 return segmented_write(ctxt, ctxt->dst.addr.mem,
3301 &desc_ptr, 2 + ctxt->op_bytes);
3302}
3303
3304static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3305{
3306 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3307}
3308
3309static int em_sidt(struct x86_emulate_ctxt *ctxt)
3310{
3311 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3312}
3313
Avi Kivity26d05cc2011-04-21 12:07:59 +03003314static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3315{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003316 struct desc_ptr desc_ptr;
3317 int rc;
3318
Avi Kivity510425f2012-06-07 17:04:36 +03003319 if (ctxt->mode == X86EMUL_MODE_PROT64)
3320 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003321 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003322 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003323 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003324 if (rc != X86EMUL_CONTINUE)
3325 return rc;
3326 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3327 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003328 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003329 return X86EMUL_CONTINUE;
3330}
3331
Avi Kivity5ef39c72011-04-21 12:21:50 +03003332static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003333{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003334 int rc;
3335
Avi Kivity5ef39c72011-04-21 12:21:50 +03003336 rc = ctxt->ops->fix_hypercall(ctxt);
3337
Avi Kivity26d05cc2011-04-21 12:07:59 +03003338 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003339 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003340 return rc;
3341}
3342
3343static int em_lidt(struct x86_emulate_ctxt *ctxt)
3344{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003345 struct desc_ptr desc_ptr;
3346 int rc;
3347
Avi Kivity510425f2012-06-07 17:04:36 +03003348 if (ctxt->mode == X86EMUL_MODE_PROT64)
3349 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003350 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003351 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003352 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003353 if (rc != X86EMUL_CONTINUE)
3354 return rc;
3355 ctxt->ops->set_idt(ctxt, &desc_ptr);
3356 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003357 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003358 return X86EMUL_CONTINUE;
3359}
3360
3361static int em_smsw(struct x86_emulate_ctxt *ctxt)
3362{
Nadav Amit32e94d02014-06-02 18:34:11 +03003363 if (ctxt->dst.type == OP_MEM)
3364 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003365 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003366 return X86EMUL_CONTINUE;
3367}
3368
3369static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3370{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003371 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003372 | (ctxt->src.val & 0x0f));
3373 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003374 return X86EMUL_CONTINUE;
3375}
3376
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003377static int em_loop(struct x86_emulate_ctxt *ctxt)
3378{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003379 int rc = X86EMUL_CONTINUE;
3380
Avi Kivitydd856ef2012-08-27 23:46:17 +03003381 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3382 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003383 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
Nadav Amit234f3ce2014-09-18 22:39:38 +03003384 rc = jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003385
Nadav Amit234f3ce2014-09-18 22:39:38 +03003386 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003387}
3388
3389static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3390{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003391 int rc = X86EMUL_CONTINUE;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003392
Nadav Amit234f3ce2014-09-18 22:39:38 +03003393 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
3394 rc = jmp_rel(ctxt, ctxt->src.val);
3395
3396 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003397}
3398
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003399static int em_in(struct x86_emulate_ctxt *ctxt)
3400{
3401 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3402 &ctxt->dst.val))
3403 return X86EMUL_IO_NEEDED;
3404
3405 return X86EMUL_CONTINUE;
3406}
3407
3408static int em_out(struct x86_emulate_ctxt *ctxt)
3409{
3410 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3411 &ctxt->src.val, 1);
3412 /* Disable writeback. */
3413 ctxt->dst.type = OP_NONE;
3414 return X86EMUL_CONTINUE;
3415}
3416
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003417static int em_cli(struct x86_emulate_ctxt *ctxt)
3418{
3419 if (emulator_bad_iopl(ctxt))
3420 return emulate_gp(ctxt, 0);
3421
3422 ctxt->eflags &= ~X86_EFLAGS_IF;
3423 return X86EMUL_CONTINUE;
3424}
3425
3426static int em_sti(struct x86_emulate_ctxt *ctxt)
3427{
3428 if (emulator_bad_iopl(ctxt))
3429 return emulate_gp(ctxt, 0);
3430
3431 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3432 ctxt->eflags |= X86_EFLAGS_IF;
3433 return X86EMUL_CONTINUE;
3434}
3435
Avi Kivity6d6eede2012-06-07 14:11:36 +03003436static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3437{
3438 u32 eax, ebx, ecx, edx;
3439
Avi Kivitydd856ef2012-08-27 23:46:17 +03003440 eax = reg_read(ctxt, VCPU_REGS_RAX);
3441 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003442 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003443 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3444 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3445 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3446 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003447 return X86EMUL_CONTINUE;
3448}
3449
Paolo Bonzini98f73632013-10-31 11:19:42 +01003450static int em_sahf(struct x86_emulate_ctxt *ctxt)
3451{
3452 u32 flags;
3453
3454 flags = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF;
3455 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3456
3457 ctxt->eflags &= ~0xffUL;
3458 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3459 return X86EMUL_CONTINUE;
3460}
3461
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003462static int em_lahf(struct x86_emulate_ctxt *ctxt)
3463{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003464 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3465 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003466 return X86EMUL_CONTINUE;
3467}
3468
Avi Kivity92998362012-06-13 12:25:06 +03003469static int em_bswap(struct x86_emulate_ctxt *ctxt)
3470{
3471 switch (ctxt->op_bytes) {
3472#ifdef CONFIG_X86_64
3473 case 8:
3474 asm("bswap %0" : "+r"(ctxt->dst.val));
3475 break;
3476#endif
3477 default:
3478 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3479 break;
3480 }
3481 return X86EMUL_CONTINUE;
3482}
3483
Nadav Amit13e457e2014-10-13 13:04:13 +03003484static int em_clflush(struct x86_emulate_ctxt *ctxt)
3485{
3486 /* emulating clflush regardless of cpuid */
3487 return X86EMUL_CONTINUE;
3488}
3489
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003490static bool valid_cr(int nr)
3491{
3492 switch (nr) {
3493 case 0:
3494 case 2 ... 4:
3495 case 8:
3496 return true;
3497 default:
3498 return false;
3499 }
3500}
3501
3502static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3503{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003504 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003505 return emulate_ud(ctxt);
3506
3507 return X86EMUL_CONTINUE;
3508}
3509
3510static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3511{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003512 u64 new_val = ctxt->src.val64;
3513 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003514 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003515
3516 static u64 cr_reserved_bits[] = {
3517 0xffffffff00000000ULL,
3518 0, 0, 0, /* CR3 checked later */
3519 CR4_RESERVED_BITS,
3520 0, 0, 0,
3521 CR8_RESERVED_BITS,
3522 };
3523
3524 if (!valid_cr(cr))
3525 return emulate_ud(ctxt);
3526
3527 if (new_val & cr_reserved_bits[cr])
3528 return emulate_gp(ctxt, 0);
3529
3530 switch (cr) {
3531 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003532 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003533 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3534 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3535 return emulate_gp(ctxt, 0);
3536
Avi Kivity717746e2011-04-20 13:37:53 +03003537 cr4 = ctxt->ops->get_cr(ctxt, 4);
3538 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003539
3540 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3541 !(cr4 & X86_CR4_PAE))
3542 return emulate_gp(ctxt, 0);
3543
3544 break;
3545 }
3546 case 3: {
3547 u64 rsvd = 0;
3548
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003549 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3550 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003551 rsvd = CR3_L_MODE_RESERVED_BITS;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003552
3553 if (new_val & rsvd)
3554 return emulate_gp(ctxt, 0);
3555
3556 break;
3557 }
3558 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003559 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003560
3561 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3562 return emulate_gp(ctxt, 0);
3563
3564 break;
3565 }
3566 }
3567
3568 return X86EMUL_CONTINUE;
3569}
3570
Joerg Roedel3b88e412011-04-04 12:39:29 +02003571static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3572{
3573 unsigned long dr7;
3574
Avi Kivity717746e2011-04-20 13:37:53 +03003575 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003576
3577 /* Check if DR7.Global_Enable is set */
3578 return dr7 & (1 << 13);
3579}
3580
3581static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3582{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003583 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003584 u64 cr4;
3585
3586 if (dr > 7)
3587 return emulate_ud(ctxt);
3588
Avi Kivity717746e2011-04-20 13:37:53 +03003589 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003590 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3591 return emulate_ud(ctxt);
3592
3593 if (check_dr7_gd(ctxt))
3594 return emulate_db(ctxt);
3595
3596 return X86EMUL_CONTINUE;
3597}
3598
3599static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3600{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003601 u64 new_val = ctxt->src.val64;
3602 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003603
3604 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3605 return emulate_gp(ctxt, 0);
3606
3607 return check_dr_read(ctxt);
3608}
3609
Joerg Roedel01de8b02011-04-04 12:39:31 +02003610static int check_svme(struct x86_emulate_ctxt *ctxt)
3611{
3612 u64 efer;
3613
Avi Kivity717746e2011-04-20 13:37:53 +03003614 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003615
3616 if (!(efer & EFER_SVME))
3617 return emulate_ud(ctxt);
3618
3619 return X86EMUL_CONTINUE;
3620}
3621
3622static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3623{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003624 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003625
3626 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003627 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003628 return emulate_gp(ctxt, 0);
3629
3630 return check_svme(ctxt);
3631}
3632
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003633static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3634{
Avi Kivity717746e2011-04-20 13:37:53 +03003635 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003636
Avi Kivity717746e2011-04-20 13:37:53 +03003637 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003638 return emulate_ud(ctxt);
3639
3640 return X86EMUL_CONTINUE;
3641}
3642
Joerg Roedel80612522011-04-04 12:39:33 +02003643static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3644{
Avi Kivity717746e2011-04-20 13:37:53 +03003645 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003646 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003647
Avi Kivity717746e2011-04-20 13:37:53 +03003648 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03003649 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02003650 return emulate_gp(ctxt, 0);
3651
3652 return X86EMUL_CONTINUE;
3653}
3654
Joerg Roedelf6511932011-04-04 12:39:35 +02003655static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3656{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003657 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3658 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003659 return emulate_gp(ctxt, 0);
3660
3661 return X86EMUL_CONTINUE;
3662}
3663
3664static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3665{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003666 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3667 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003668 return emulate_gp(ctxt, 0);
3669
3670 return X86EMUL_CONTINUE;
3671}
3672
Avi Kivity73fba5f2010-07-29 15:11:53 +03003673#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003674#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
3675#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
3676 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003677#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003678#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003679#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3680#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003681#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003682#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003683#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003684#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003685 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003686#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003687 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
3688 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003689#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003690
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003691#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003692#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003693#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003694#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003695#define I2bvIP(_f, _e, _i, _p) \
3696 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003697
Avi Kivityfb864fb2013-01-04 16:18:54 +02003698#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3699 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3700 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003701
Nadav Amit0f54a322014-08-29 11:26:55 +03003702static const struct opcode group7_rm0[] = {
3703 N,
3704 I(SrcNone | Priv | EmulateOnUD, em_vmcall),
3705 N, N, N, N, N, N,
3706};
3707
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003708static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003709 DI(SrcNone | Priv, monitor),
3710 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003711 N, N, N, N, N, N,
3712};
3713
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003714static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003715 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003716 II(SrcNone | Prot | EmulateOnUD, em_vmmcall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003717 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3718 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3719 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3720 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3721 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3722 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003723};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003724
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003725static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003726 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003727 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003728 N, N, N, N, N, N,
3729};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003730
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003731static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003732 F(Lock, em_add),
3733 F(Lock | PageTable, em_or),
3734 F(Lock, em_adc),
3735 F(Lock, em_sbb),
3736 F(Lock | PageTable, em_and),
3737 F(Lock, em_sub),
3738 F(Lock, em_xor),
3739 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003740};
3741
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003742static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003743 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003744};
3745
Avi Kivity007a3b52013-01-19 19:51:51 +02003746static const struct opcode group2[] = {
3747 F(DstMem | ModRM, em_rol),
3748 F(DstMem | ModRM, em_ror),
3749 F(DstMem | ModRM, em_rcl),
3750 F(DstMem | ModRM, em_rcr),
3751 F(DstMem | ModRM, em_shl),
3752 F(DstMem | ModRM, em_shr),
3753 F(DstMem | ModRM, em_shl),
3754 F(DstMem | ModRM, em_sar),
3755};
3756
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003757static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003758 F(DstMem | SrcImm | NoWrite, em_test),
3759 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003760 F(DstMem | SrcNone | Lock, em_not),
3761 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02003762 F(DstXacc | Src2Mem, em_mul_ex),
3763 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02003764 F(DstXacc | Src2Mem, em_div_ex),
3765 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003766};
3767
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003768static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003769 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3770 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003771 N, N, N, N, N, N,
3772};
3773
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003774static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003775 F(DstMem | SrcNone | Lock, em_inc),
3776 F(DstMem | SrcNone | Lock, em_dec),
Nadav Amit58b70752014-10-24 11:35:09 +03003777 I(SrcMem | NearBranch, em_call_near_abs),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003778 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
Nadav Amit58b70752014-10-24 11:35:09 +03003779 I(SrcMem | NearBranch, em_jmp_abs),
Nadav Amitf7784042014-09-18 22:39:41 +03003780 I(SrcMemFAddr | ImplicitOps, em_jmp_far),
3781 I(SrcMem | Stack, em_push), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003782};
3783
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003784static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003785 DI(Prot, sldt),
3786 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003787 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003788 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003789 N, N, N, N,
3790};
3791
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003792static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03003793 II(Mov | DstMem, em_sgdt, sgdt),
3794 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003795 II(SrcMem | Priv, em_lgdt, lgdt),
3796 II(SrcMem | Priv, em_lidt, lidt),
3797 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3798 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3799 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003800}, {
Nadav Amit0f54a322014-08-29 11:26:55 +03003801 EXT(0, group7_rm0),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003802 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003803 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003804 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3805 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3806 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003807} };
3808
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003809static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003810 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003811 F(DstMem | SrcImmByte | NoWrite, em_bt),
3812 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3813 F(DstMem | SrcImmByte | Lock, em_btr),
3814 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003815};
3816
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003817static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003818 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003819}, {
3820 N, N, N, N, N, N, N, N,
3821} };
3822
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003823static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003824 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003825 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003826};
3827
Nadav Amit13e457e2014-10-13 13:04:13 +03003828static const struct gprefix pfx_0f_ae_7 = {
Nadav Amit3f6f1482014-10-13 13:04:14 +03003829 I(SrcMem | ByteOp, em_clflush), N, N, N,
Nadav Amit13e457e2014-10-13 13:04:13 +03003830};
3831
3832static const struct group_dual group15 = { {
3833 N, N, N, N, N, N, N, GP(0, &pfx_0f_ae_7),
3834}, {
3835 N, N, N, N, N, N, N, N,
3836} };
3837
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003838static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003839 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003840};
3841
Paolo Bonzinid5b77062014-07-14 12:54:48 +02003842static const struct gprefix pfx_0f_2b = {
3843 I(0, em_mov), I(0, em_mov), N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003844};
3845
Igor Mammedov27ce8252014-03-15 21:01:59 +01003846static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01003847 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003848};
3849
Alex Williamson0a370272014-07-11 11:56:31 -06003850static const struct gprefix pfx_0f_e7 = {
3851 N, I(Sse, em_mov), N, N,
3852};
3853
Gleb Natapov045a2822012-12-20 16:57:43 +02003854static const struct escape escape_d9 = { {
3855 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3856}, {
3857 /* 0xC0 - 0xC7 */
3858 N, N, N, N, N, N, N, N,
3859 /* 0xC8 - 0xCF */
3860 N, N, N, N, N, N, N, N,
3861 /* 0xD0 - 0xC7 */
3862 N, N, N, N, N, N, N, N,
3863 /* 0xD8 - 0xDF */
3864 N, N, N, N, N, N, N, N,
3865 /* 0xE0 - 0xE7 */
3866 N, N, N, N, N, N, N, N,
3867 /* 0xE8 - 0xEF */
3868 N, N, N, N, N, N, N, N,
3869 /* 0xF0 - 0xF7 */
3870 N, N, N, N, N, N, N, N,
3871 /* 0xF8 - 0xFF */
3872 N, N, N, N, N, N, N, N,
3873} };
3874
3875static const struct escape escape_db = { {
3876 N, N, N, N, N, N, N, N,
3877}, {
3878 /* 0xC0 - 0xC7 */
3879 N, N, N, N, N, N, N, N,
3880 /* 0xC8 - 0xCF */
3881 N, N, N, N, N, N, N, N,
3882 /* 0xD0 - 0xC7 */
3883 N, N, N, N, N, N, N, N,
3884 /* 0xD8 - 0xDF */
3885 N, N, N, N, N, N, N, N,
3886 /* 0xE0 - 0xE7 */
3887 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3888 /* 0xE8 - 0xEF */
3889 N, N, N, N, N, N, N, N,
3890 /* 0xF0 - 0xF7 */
3891 N, N, N, N, N, N, N, N,
3892 /* 0xF8 - 0xFF */
3893 N, N, N, N, N, N, N, N,
3894} };
3895
3896static const struct escape escape_dd = { {
3897 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3898}, {
3899 /* 0xC0 - 0xC7 */
3900 N, N, N, N, N, N, N, N,
3901 /* 0xC8 - 0xCF */
3902 N, N, N, N, N, N, N, N,
3903 /* 0xD0 - 0xC7 */
3904 N, N, N, N, N, N, N, N,
3905 /* 0xD8 - 0xDF */
3906 N, N, N, N, N, N, N, N,
3907 /* 0xE0 - 0xE7 */
3908 N, N, N, N, N, N, N, N,
3909 /* 0xE8 - 0xEF */
3910 N, N, N, N, N, N, N, N,
3911 /* 0xF0 - 0xF7 */
3912 N, N, N, N, N, N, N, N,
3913 /* 0xF8 - 0xFF */
3914 N, N, N, N, N, N, N, N,
3915} };
3916
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003917static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003918 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003919 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003920 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3921 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003922 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003923 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003924 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3925 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003926 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003927 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003928 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3929 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003930 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003931 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003932 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3933 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003934 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003935 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003936 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003937 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003938 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003939 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003940 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003941 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003942 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02003943 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003944 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003945 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003946 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003947 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003948 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003949 I(ImplicitOps | Stack | No64, em_pusha),
3950 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003951 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3952 N, N, N, N,
3953 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003954 I(SrcImm | Mov | Stack, em_push),
3955 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003956 I(SrcImmByte | Mov | Stack, em_push),
3957 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003958 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003959 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003960 /* 0x70 - 0x7F */
Nadav Amit58b70752014-10-24 11:35:09 +03003961 X16(D(SrcImmByte | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003962 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003963 G(ByteOp | DstMem | SrcImm, group1),
3964 G(DstMem | SrcImm, group1),
3965 G(ByteOp | DstMem | SrcImm | No64, group1),
3966 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003967 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003968 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003969 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003970 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003971 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003972 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003973 D(ModRM | SrcMem | NoAccess | DstReg),
3974 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3975 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003976 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003977 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003978 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003979 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003980 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003981 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01003982 II(ImplicitOps | Stack, em_popf, popf),
3983 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003984 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003985 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003986 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003987 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003988 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003989 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003990 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003991 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3992 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003993 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003994 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003995 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003996 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02003997 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003998 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003999 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Nadav Amit58b70752014-10-24 11:35:09 +03004000 I(ImplicitOps | NearBranch | SrcImmU16, em_ret_near_imm),
4001 I(ImplicitOps | NearBranch, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03004002 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
4003 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004004 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004005 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03004006 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Bruce Rogers32611072013-09-09 09:40:20 -06004007 I(ImplicitOps | Stack | SrcImmU16, em_ret_far_imm),
4008 I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02004009 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004010 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004011 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004012 G(Src2One | ByteOp, group2), G(Src2One, group2),
4013 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02004014 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02004015 I(DstAcc | SrcImmUByte | No64, em_aad),
4016 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004017 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004018 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02004019 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004020 /* 0xE0 - 0xE7 */
Nadav Amit58b70752014-10-24 11:35:09 +03004021 X3(I(SrcImmByte | NearBranch, em_loop)),
4022 I(SrcImmByte | NearBranch, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004023 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
4024 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004025 /* 0xE8 - 0xEF */
Nadav Amit58b70752014-10-24 11:35:09 +03004026 I(SrcImm | NearBranch, em_call), D(SrcImm | ImplicitOps | NearBranch),
4027 I(SrcImmFAddr | No64, em_jmp_far),
4028 D(SrcImmByte | ImplicitOps | NearBranch),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004029 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
4030 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004031 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004032 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004033 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4034 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004035 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09004036 D(ImplicitOps), D(ImplicitOps),
4037 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004038 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4039};
4040
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004041static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004042 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004043 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02004044 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004045 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004046 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004047 N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004048 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004049 N, N, N, N, N, N, N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004050 D(ImplicitOps | ModRM | SrcMem | NoAccess),
4051 N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004052 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03004053 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
4054 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
4055 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
4056 check_cr_write),
4057 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
4058 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004059 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004060 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
4061 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004062 N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b),
Avi Kivity3e114eb2012-04-09 18:40:01 +03004063 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004064 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004065 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004066 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004067 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004068 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02004069 I(ImplicitOps | EmulateOnUD, em_sysenter),
4070 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004071 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004072 N, N, N, N, N, N, N, N,
4073 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03004074 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004075 /* 0x50 - 0x5F */
4076 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4077 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004078 N, N, N, N,
4079 N, N, N, N,
4080 N, N, N, N,
4081 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004082 /* 0x70 - 0x7F */
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(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004087 /* 0x80 - 0x8F */
Nadav Amit58b70752014-10-24 11:35:09 +03004088 X16(D(SrcImm | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004089 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004090 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004091 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004092 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004093 II(ImplicitOps, em_cpuid, cpuid),
4094 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004095 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4096 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004097 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004098 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004099 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004100 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004101 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4102 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Nadav Amit13e457e2014-10-13 13:04:13 +03004103 GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004104 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09004105 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004106 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004107 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004108 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4109 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004110 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004111 /* 0xB8 - 0xBF */
4112 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004113 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004114 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
4115 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004116 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004117 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02004118 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Wei Yongjun92f738a52010-08-17 09:19:34 +08004119 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004120 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004121 /* 0xC8 - 0xCF */
4122 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004123 /* 0xD0 - 0xDF */
4124 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4125 /* 0xE0 - 0xEF */
Alex Williamson0a370272014-07-11 11:56:31 -06004126 N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
4127 N, N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004128 /* 0xF0 - 0xFF */
4129 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4130};
4131
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004132static const struct gprefix three_byte_0f_38_f0 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01004133 I(DstReg | SrcMem | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004134};
4135
4136static const struct gprefix three_byte_0f_38_f1 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01004137 I(DstMem | SrcReg | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004138};
4139
4140/*
4141 * Insns below are selected by the prefix which indexed by the third opcode
4142 * byte.
4143 */
4144static const struct opcode opcode_map_0f_38[256] = {
4145 /* 0x00 - 0x7f */
4146 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004147 /* 0x80 - 0xef */
4148 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4149 /* 0xf0 - 0xf1 */
4150 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f0),
4151 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f1),
4152 /* 0xf2 - 0xff */
4153 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004154};
4155
Avi Kivity73fba5f2010-07-29 15:11:53 +03004156#undef D
4157#undef N
4158#undef G
4159#undef GD
4160#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004161#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004162#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004163
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004164#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004165#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004166#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004167#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004168#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004169
Avi Kivity9dac77f2011-06-01 15:34:25 +03004170static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004171{
4172 unsigned size;
4173
Avi Kivity9dac77f2011-06-01 15:34:25 +03004174 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004175 if (size == 8)
4176 size = 4;
4177 return size;
4178}
4179
4180static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4181 unsigned size, bool sign_extension)
4182{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004183 int rc = X86EMUL_CONTINUE;
4184
4185 op->type = OP_IMM;
4186 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004187 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004188 /* NB. Immediates are sign-extended as necessary. */
4189 switch (op->bytes) {
4190 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004191 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004192 break;
4193 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004194 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004195 break;
4196 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004197 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004198 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004199 case 8:
4200 op->val = insn_fetch(s64, ctxt);
4201 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004202 }
4203 if (!sign_extension) {
4204 switch (op->bytes) {
4205 case 1:
4206 op->val &= 0xff;
4207 break;
4208 case 2:
4209 op->val &= 0xffff;
4210 break;
4211 case 4:
4212 op->val &= 0xffffffff;
4213 break;
4214 }
4215 }
4216done:
4217 return rc;
4218}
4219
Avi Kivitya99455492011-09-13 10:45:41 +03004220static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4221 unsigned d)
4222{
4223 int rc = X86EMUL_CONTINUE;
4224
4225 switch (d) {
4226 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004227 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03004228 break;
4229 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004230 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03004231 break;
4232 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004233 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004234 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03004235 *op = ctxt->memop;
4236 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004237 if (ctxt->d & BitOp)
Avi Kivitya99455492011-09-13 10:45:41 +03004238 fetch_bit_operand(ctxt);
4239 op->orig_val = op->val;
4240 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004241 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004242 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004243 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03004244 case OpAcc:
4245 op->type = OP_REG;
4246 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004247 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03004248 fetch_register_operand(op);
4249 op->orig_val = op->val;
4250 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004251 case OpAccLo:
4252 op->type = OP_REG;
4253 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4254 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4255 fetch_register_operand(op);
4256 op->orig_val = op->val;
4257 break;
4258 case OpAccHi:
4259 if (ctxt->d & ByteOp) {
4260 op->type = OP_NONE;
4261 break;
4262 }
4263 op->type = OP_REG;
4264 op->bytes = ctxt->op_bytes;
4265 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4266 fetch_register_operand(op);
4267 op->orig_val = op->val;
4268 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004269 case OpDI:
4270 op->type = OP_MEM;
4271 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4272 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004273 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya99455492011-09-13 10:45:41 +03004274 op->addr.mem.seg = VCPU_SREG_ES;
4275 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004276 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03004277 break;
4278 case OpDX:
4279 op->type = OP_REG;
4280 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004281 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03004282 fetch_register_operand(op);
4283 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004284 case OpCL:
4285 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004286 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004287 break;
4288 case OpImmByte:
4289 rc = decode_imm(ctxt, op, 1, true);
4290 break;
4291 case OpOne:
4292 op->bytes = 1;
4293 op->val = 1;
4294 break;
4295 case OpImm:
4296 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4297 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004298 case OpImm64:
4299 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4300 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004301 case OpMem8:
4302 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004303 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004304 ctxt->memop.addr.reg = decode_register(ctxt,
4305 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004306 fetch_register_operand(&ctxt->memop);
4307 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004308 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004309 case OpMem16:
4310 ctxt->memop.bytes = 2;
4311 goto mem_common;
4312 case OpMem32:
4313 ctxt->memop.bytes = 4;
4314 goto mem_common;
4315 case OpImmU16:
4316 rc = decode_imm(ctxt, op, 2, false);
4317 break;
4318 case OpImmU:
4319 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4320 break;
4321 case OpSI:
4322 op->type = OP_MEM;
4323 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4324 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004325 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Bandan Das573e80f2014-04-16 12:46:13 -04004326 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03004327 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004328 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004329 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004330 case OpXLat:
4331 op->type = OP_MEM;
4332 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4333 op->addr.mem.ea =
4334 register_address(ctxt,
4335 reg_read(ctxt, VCPU_REGS_RBX) +
4336 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04004337 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004338 op->val = 0;
4339 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004340 case OpImmFAddr:
4341 op->type = OP_IMM;
4342 op->addr.mem.ea = ctxt->_eip;
4343 op->bytes = ctxt->op_bytes + 2;
4344 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4345 break;
4346 case OpMemFAddr:
4347 ctxt->memop.bytes = ctxt->op_bytes + 2;
4348 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004349 case OpES:
4350 op->val = VCPU_SREG_ES;
4351 break;
4352 case OpCS:
4353 op->val = VCPU_SREG_CS;
4354 break;
4355 case OpSS:
4356 op->val = VCPU_SREG_SS;
4357 break;
4358 case OpDS:
4359 op->val = VCPU_SREG_DS;
4360 break;
4361 case OpFS:
4362 op->val = VCPU_SREG_FS;
4363 break;
4364 case OpGS:
4365 op->val = VCPU_SREG_GS;
4366 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004367 case OpImplicit:
4368 /* Special instructions do their own operand decoding. */
4369 default:
4370 op->type = OP_NONE; /* Disable writeback. */
4371 break;
4372 }
4373
4374done:
4375 return rc;
4376}
4377
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004378int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004379{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004380 int rc = X86EMUL_CONTINUE;
4381 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004382 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004383 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04004384 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03004385 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004386
Avi Kivityf09ed832011-09-13 10:45:40 +03004387 ctxt->memop.type = OP_NONE;
4388 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004389 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02004390 ctxt->fetch.ptr = ctxt->fetch.data;
4391 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004392 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01004393 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004394 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004395 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02004396 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004397 if (rc != X86EMUL_CONTINUE)
4398 return rc;
4399 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004400
4401 switch (mode) {
4402 case X86EMUL_MODE_REAL:
4403 case X86EMUL_MODE_VM86:
4404 case X86EMUL_MODE_PROT16:
4405 def_op_bytes = def_ad_bytes = 2;
4406 break;
4407 case X86EMUL_MODE_PROT32:
4408 def_op_bytes = def_ad_bytes = 4;
4409 break;
4410#ifdef CONFIG_X86_64
4411 case X86EMUL_MODE_PROT64:
4412 def_op_bytes = 4;
4413 def_ad_bytes = 8;
4414 break;
4415#endif
4416 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004417 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004418 }
4419
Avi Kivity9dac77f2011-06-01 15:34:25 +03004420 ctxt->op_bytes = def_op_bytes;
4421 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004422
4423 /* Legacy prefixes. */
4424 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004425 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004426 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004427 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004428 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004429 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004430 break;
4431 case 0x67: /* address-size override */
4432 if (mode == X86EMUL_MODE_PROT64)
4433 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004434 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004435 else
4436 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004437 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004438 break;
4439 case 0x26: /* ES override */
4440 case 0x2e: /* CS override */
4441 case 0x36: /* SS override */
4442 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004443 has_seg_override = true;
4444 ctxt->seg_override = (ctxt->b >> 3) & 3;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004445 break;
4446 case 0x64: /* FS override */
4447 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004448 has_seg_override = true;
4449 ctxt->seg_override = ctxt->b & 7;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004450 break;
4451 case 0x40 ... 0x4f: /* REX */
4452 if (mode != X86EMUL_MODE_PROT64)
4453 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004454 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004455 continue;
4456 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004457 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004458 break;
4459 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004460 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004461 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004462 break;
4463 default:
4464 goto done_prefixes;
4465 }
4466
4467 /* Any legacy prefix after a REX prefix nullifies its effect. */
4468
Avi Kivity9dac77f2011-06-01 15:34:25 +03004469 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004470 }
4471
4472done_prefixes:
4473
4474 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004475 if (ctxt->rex_prefix & 8)
4476 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004477
4478 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004479 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004480 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004481 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004482 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004483 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004484 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004485
4486 /* 0F_38 opcode map */
4487 if (ctxt->b == 0x38) {
4488 ctxt->opcode_len = 3;
4489 ctxt->b = insn_fetch(u8, ctxt);
4490 opcode = opcode_map_0f_38[ctxt->b];
4491 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004492 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004493 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004494
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004495 if (ctxt->d & ModRM)
4496 ctxt->modrm = insn_fetch(u8, ctxt);
4497
Nadav Amit7fe864d2014-06-02 18:34:03 +03004498 /* vex-prefix instructions are not implemented */
4499 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
4500 (mode == X86EMUL_MODE_PROT64 ||
4501 (mode >= X86EMUL_MODE_PROT16 && (ctxt->modrm & 0x80)))) {
4502 ctxt->d = NotImpl;
4503 }
4504
Avi Kivity9dac77f2011-06-01 15:34:25 +03004505 while (ctxt->d & GroupMask) {
4506 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004507 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004508 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004509 opcode = opcode.u.group[goffset];
4510 break;
4511 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004512 goffset = (ctxt->modrm >> 3) & 7;
4513 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004514 opcode = opcode.u.gdual->mod3[goffset];
4515 else
4516 opcode = opcode.u.gdual->mod012[goffset];
4517 break;
4518 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004519 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004520 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004521 break;
4522 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004523 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004524 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004525 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004526 switch (simd_prefix) {
4527 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4528 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4529 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4530 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4531 }
4532 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004533 case Escape:
4534 if (ctxt->modrm > 0xbf)
4535 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4536 else
4537 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4538 break;
Avi Kivity46561642011-04-24 14:09:59 +03004539 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004540 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004541 }
Avi Kivity46561642011-04-24 14:09:59 +03004542
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004543 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004544 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004545 }
4546
Paolo Bonzinie24186e2014-03-27 12:00:57 +01004547 /* Unrecognised? */
4548 if (ctxt->d == 0)
4549 return EMULATION_FAILED;
4550
Avi Kivity9dac77f2011-06-01 15:34:25 +03004551 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004552
Nadav Amit3a6095a2014-08-13 16:50:13 +03004553 if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD)))
4554 return EMULATION_FAILED;
4555
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004556 if (unlikely(ctxt->d &
Nadav Amit58b70752014-10-24 11:35:09 +03004557 (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch))) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004558 /*
4559 * These are copied unconditionally here, and checked unconditionally
4560 * in x86_emulate_insn.
4561 */
4562 ctxt->check_perm = opcode.check_perm;
4563 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004564
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004565 if (ctxt->d & NotImpl)
4566 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004567
Nadav Amit58b70752014-10-24 11:35:09 +03004568 if (mode == X86EMUL_MODE_PROT64) {
4569 if (ctxt->op_bytes == 4 && (ctxt->d & Stack))
4570 ctxt->op_bytes = 8;
4571 else if (ctxt->d & NearBranch)
4572 ctxt->op_bytes = 8;
4573 }
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004574
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004575 if (ctxt->d & Op3264) {
4576 if (mode == X86EMUL_MODE_PROT64)
4577 ctxt->op_bytes = 8;
4578 else
4579 ctxt->op_bytes = 4;
4580 }
4581
4582 if (ctxt->d & Sse)
4583 ctxt->op_bytes = 16;
4584 else if (ctxt->d & Mmx)
4585 ctxt->op_bytes = 8;
4586 }
Avi Kivity1253791d2011-03-29 11:41:27 +02004587
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004588 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004589 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004590 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04004591 if (!has_seg_override) {
4592 has_seg_override = true;
4593 ctxt->seg_override = ctxt->modrm_seg;
4594 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004595 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004596 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004597 if (rc != X86EMUL_CONTINUE)
4598 goto done;
4599
Bandan Das573e80f2014-04-16 12:46:13 -04004600 if (!has_seg_override)
4601 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004602
Bandan Das573e80f2014-04-16 12:46:13 -04004603 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004604
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004605 /*
4606 * Decode and fetch the source operand: register, memory
4607 * or immediate.
4608 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004609 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004610 if (rc != X86EMUL_CONTINUE)
4611 goto done;
4612
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004613 /*
4614 * Decode and fetch the second source operand: register, memory
4615 * or immediate.
4616 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004617 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004618 if (rc != X86EMUL_CONTINUE)
4619 goto done;
4620
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004621 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03004622 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004623
Bandan Das41061cd2014-04-16 12:46:14 -04004624 if (ctxt->rip_relative)
Avi Kivityf09ed832011-09-13 10:45:40 +03004625 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004626
Paolo Bonzinia430c912014-10-23 14:54:14 +02004627done:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004628 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004629}
4630
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004631bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4632{
4633 return ctxt->d & PageTable;
4634}
4635
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004636static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4637{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004638 /* The second termination condition only applies for REPE
4639 * and REPNE. Test if the repeat string operation prefix is
4640 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4641 * corresponding termination condition according to:
4642 * - if REPE/REPZ and ZF = 0 then done
4643 * - if REPNE/REPNZ and ZF = 1 then done
4644 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004645 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4646 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4647 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004648 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004649 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004650 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4651 return true;
4652
4653 return false;
4654}
4655
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004656static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4657{
4658 bool fault = false;
4659
4660 ctxt->ops->get_fpu(ctxt);
4661 asm volatile("1: fwait \n\t"
4662 "2: \n\t"
4663 ".pushsection .fixup,\"ax\" \n\t"
4664 "3: \n\t"
4665 "movb $1, %[fault] \n\t"
4666 "jmp 2b \n\t"
4667 ".popsection \n\t"
4668 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004669 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004670 ctxt->ops->put_fpu(ctxt);
4671
4672 if (unlikely(fault))
4673 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4674
4675 return X86EMUL_CONTINUE;
4676}
4677
4678static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4679 struct operand *op)
4680{
4681 if (op->type == OP_MM)
4682 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4683}
4684
Avi Kivitye28bbd42013-01-04 16:18:48 +02004685static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4686{
4687 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivityb9fa4092013-02-09 11:31:48 +02004688 if (!(ctxt->d & ByteOp))
4689 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Avi Kivitye28bbd42013-01-04 16:18:48 +02004690 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004691 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
4692 [fastop]"+S"(fop)
4693 : "c"(ctxt->src2.val));
Avi Kivitye28bbd42013-01-04 16:18:48 +02004694 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004695 if (!fop) /* exception is returned in fop variable */
4696 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02004697 return X86EMUL_CONTINUE;
4698}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004699
Bandan Das14985072014-04-16 12:46:09 -04004700void init_decode_cache(struct x86_emulate_ctxt *ctxt)
4701{
Bandan Das573e80f2014-04-16 12:46:13 -04004702 memset(&ctxt->rip_relative, 0,
4703 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04004704
Bandan Das14985072014-04-16 12:46:09 -04004705 ctxt->io_read.pos = 0;
4706 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04004707 ctxt->mem_read.end = 0;
4708}
4709
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004710int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004711{
Mathias Krause0225fb52012-08-30 01:30:16 +02004712 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004713 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004714 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004715
Avi Kivity9dac77f2011-06-01 15:34:25 +03004716 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004717
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004718 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004719 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004720 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004721 goto done;
4722 }
4723
Avi Kivity9dac77f2011-06-01 15:34:25 +03004724 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004725 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004726 goto done;
4727 }
4728
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004729 if (unlikely(ctxt->d &
4730 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
4731 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4732 (ctxt->d & Undefined)) {
4733 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004734 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004735 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004736
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004737 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4738 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
4739 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004740 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004741 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004742
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004743 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
4744 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004745 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004746 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004747
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004748 if (ctxt->d & Mmx) {
4749 rc = flush_pending_x87_faults(ctxt);
4750 if (rc != X86EMUL_CONTINUE)
4751 goto done;
4752 /*
4753 * Now that we know the fpu is exception safe, we can fetch
4754 * operands from it.
4755 */
4756 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4757 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4758 if (!(ctxt->d & Mov))
4759 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4760 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004761
Bandan Das685bbf42014-04-16 12:46:10 -04004762 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004763 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4764 X86_ICPT_PRE_EXCEPT);
4765 if (rc != X86EMUL_CONTINUE)
4766 goto done;
4767 }
4768
4769 /* Privileged instruction can be executed only in CPL=0 */
4770 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Nadav Amit68efa762014-06-18 17:19:35 +03004771 if (ctxt->d & PrivUD)
4772 rc = emulate_ud(ctxt);
4773 else
4774 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004775 goto done;
4776 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004777
4778 /* Instruction can only be executed in protected mode */
4779 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
4780 rc = emulate_ud(ctxt);
4781 goto done;
4782 }
4783
4784 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04004785 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004786 rc = ctxt->check_perm(ctxt);
4787 if (rc != X86EMUL_CONTINUE)
4788 goto done;
4789 }
4790
Bandan Das685bbf42014-04-16 12:46:10 -04004791 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004792 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4793 X86_ICPT_POST_EXCEPT);
4794 if (rc != X86EMUL_CONTINUE)
4795 goto done;
4796 }
4797
4798 if (ctxt->rep_prefix && (ctxt->d & String)) {
4799 /* All REP prefixes have the same first termination condition */
4800 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
4801 ctxt->eip = ctxt->_eip;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004802 ctxt->eflags &= ~EFLG_RF;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004803 goto done;
4804 }
4805 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004806 }
4807
Avi Kivity9dac77f2011-06-01 15:34:25 +03004808 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4809 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4810 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004811 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004812 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004813 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004814 }
4815
Avi Kivity9dac77f2011-06-01 15:34:25 +03004816 if (ctxt->src2.type == OP_MEM) {
4817 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4818 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004819 if (rc != X86EMUL_CONTINUE)
4820 goto done;
4821 }
4822
Avi Kivity9dac77f2011-06-01 15:34:25 +03004823 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004824 goto special_insn;
4825
4826
Avi Kivity9dac77f2011-06-01 15:34:25 +03004827 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004828 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004829 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4830 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004831 if (rc != X86EMUL_CONTINUE)
4832 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004833 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004834 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004835
Avi Kivity018a98d2007-11-27 19:30:56 +02004836special_insn:
4837
Bandan Das685bbf42014-04-16 12:46:10 -04004838 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004839 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004840 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004841 if (rc != X86EMUL_CONTINUE)
4842 goto done;
4843 }
4844
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004845 if (ctxt->rep_prefix && (ctxt->d & String))
4846 ctxt->eflags |= EFLG_RF;
4847 else
4848 ctxt->eflags &= ~EFLG_RF;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004849
Avi Kivity9dac77f2011-06-01 15:34:25 +03004850 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004851 if (ctxt->d & Fastop) {
4852 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4853 rc = fastop(ctxt, fop);
4854 if (rc != X86EMUL_CONTINUE)
4855 goto done;
4856 goto writeback;
4857 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004858 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004859 if (rc != X86EMUL_CONTINUE)
4860 goto done;
4861 goto writeback;
4862 }
4863
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004864 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004865 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004866 else if (ctxt->opcode_len == 3)
4867 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004868
Avi Kivity9dac77f2011-06-01 15:34:25 +03004869 switch (ctxt->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004870 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004871 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004872 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004873 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004874 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004875 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004876 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03004877 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004878 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004879 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004880 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004881 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004882 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004883 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03004884 ctxt->dst.type = OP_NONE;
4885 else
4886 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004887 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004888 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004889 switch (ctxt->op_bytes) {
4890 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4891 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4892 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004893 }
4894 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004895 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004896 rc = emulate_int(ctxt, 3);
4897 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004898 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004899 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004900 break;
4901 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004902 if (ctxt->eflags & EFLG_OF)
4903 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004904 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004905 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004906 case 0xeb: /* jmp rel short */
Nadav Amit234f3ce2014-09-18 22:39:38 +03004907 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004908 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004909 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004910 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004911 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004912 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004913 case 0xf5: /* cmc */
4914 /* complement carry flag from eflags reg */
4915 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004916 break;
4917 case 0xf8: /* clc */
4918 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004919 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004920 case 0xf9: /* stc */
4921 ctxt->eflags |= EFLG_CF;
4922 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004923 case 0xfc: /* cld */
4924 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004925 break;
4926 case 0xfd: /* std */
4927 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004928 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004929 default:
4930 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004931 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004932
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004933 if (rc != X86EMUL_CONTINUE)
4934 goto done;
4935
Avi Kivity018a98d2007-11-27 19:30:56 +02004936writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02004937 if (ctxt->d & SrcWrite) {
4938 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
4939 rc = writeback(ctxt, &ctxt->src);
4940 if (rc != X86EMUL_CONTINUE)
4941 goto done;
4942 }
Nadav Amitee212292014-06-15 16:12:58 +03004943 if (!(ctxt->d & NoWrite)) {
4944 rc = writeback(ctxt, &ctxt->dst);
4945 if (rc != X86EMUL_CONTINUE)
4946 goto done;
4947 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004948
Gleb Natapov5cd21912010-03-18 15:20:26 +02004949 /*
4950 * restore dst type in case the decoding will be reused
4951 * (happens for string instruction )
4952 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004953 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004954
Avi Kivity9dac77f2011-06-01 15:34:25 +03004955 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004956 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004957
Avi Kivity9dac77f2011-06-01 15:34:25 +03004958 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004959 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004960
Avi Kivity9dac77f2011-06-01 15:34:25 +03004961 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004962 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004963 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004964 if ((ctxt->d & SrcMask) == SrcSI)
4965 count = ctxt->src.count;
4966 else
4967 count = ctxt->dst.count;
4968 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4969 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004970
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004971 if (!string_insn_completed(ctxt)) {
4972 /*
4973 * Re-enter guest when pio read ahead buffer is empty
4974 * or, if it is not used, after each 1024 iteration.
4975 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004976 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004977 (r->end == 0 || r->end != r->pos)) {
4978 /*
4979 * Reset read cache. Usually happens before
4980 * decode, but since instruction is restarted
4981 * we have to do it here.
4982 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004983 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004984 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004985 return EMULATION_RESTART;
4986 }
4987 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004988 }
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004989 ctxt->eflags &= ~EFLG_RF;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004990 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004991
Avi Kivity9dac77f2011-06-01 15:34:25 +03004992 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004993
4994done:
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02004995 if (rc == X86EMUL_PROPAGATE_FAULT) {
4996 WARN_ON(ctxt->exception.vector > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +02004997 ctxt->have_exception = true;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02004998 }
Joerg Roedel775fde82011-04-04 12:39:24 +02004999 if (rc == X86EMUL_INTERCEPTED)
5000 return EMULATION_INTERCEPTED;
5001
Avi Kivitydd856ef2012-08-27 23:46:17 +03005002 if (rc == X86EMUL_CONTINUE)
5003 writeback_registers(ctxt);
5004
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005005 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005006
5007twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005008 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02005009 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02005010 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005011 break;
5012 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02005013 case 0x0d: /* GrpP (prefetch) */
5014 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02005015 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02005016 break;
5017 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005018 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02005019 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005020 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005021 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005022 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005023 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03005024 if (test_cc(ctxt->b, ctxt->eflags))
5025 ctxt->dst.val = ctxt->src.val;
5026 else if (ctxt->mode != X86EMUL_MODE_PROT64 ||
5027 ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005028 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08005029 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03005030 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03005031 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005032 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005033 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08005034 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005035 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08005036 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005037 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005038 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005039 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03005040 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005041 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005042 case 0xbe ... 0xbf: /* movsx */
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) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03005045 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005046 break;
Sheng Yanga012e652007-10-15 14:24:20 +08005047 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005048 ctxt->dst.bytes = ctxt->op_bytes;
Nadav Amit3b320042014-06-02 18:34:08 +03005049 ctxt->dst.val = (ctxt->op_bytes == 8) ? (u64) ctxt->src.val :
5050 (u32) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08005051 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005052 default:
5053 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005054 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005055
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005056threebyte_insn:
5057
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005058 if (rc != X86EMUL_CONTINUE)
5059 goto done;
5060
Avi Kivity6aa8b732006-12-10 02:21:36 -08005061 goto writeback;
5062
5063cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02005064 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005065}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005066
5067void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
5068{
5069 invalidate_registers(ctxt);
5070}
5071
5072void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
5073{
5074 writeback_registers(ctxt);
5075}