blob: b32a38e6e287c8588ca60aea73583b485f282a0b [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>
Nadav Amit3db176d2015-04-19 21:12:59 +030028#include <asm/debugreg.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080029
Avi Kivity3eeb3282010-01-21 15:31:48 +020030#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020031#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020032
Avi Kivity6aa8b732006-12-10 02:21:36 -080033/*
Avi Kivitya99455492011-09-13 10:45:41 +030034 * Operand types
35 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030036#define OpNone 0ull
37#define OpImplicit 1ull /* No generic decode */
38#define OpReg 2ull /* Register */
39#define OpMem 3ull /* Memory */
40#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
41#define OpDI 5ull /* ES:DI/EDI/RDI */
42#define OpMem64 6ull /* Memory, 64-bit */
43#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
44#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030045#define OpCL 9ull /* CL register (for shifts) */
46#define OpImmByte 10ull /* 8-bit sign extended immediate */
47#define OpOne 11ull /* Implied 1 */
Nadav Amit5e2c6882012-12-06 21:55:10 -020048#define OpImm 12ull /* Sign extended up to 32-bit immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030049#define OpMem16 13ull /* Memory operand (16-bit). */
50#define OpMem32 14ull /* Memory operand (32-bit). */
51#define OpImmU 15ull /* Immediate operand, zero extended */
52#define OpSI 16ull /* SI/ESI/RSI */
53#define OpImmFAddr 17ull /* Immediate far address */
54#define OpMemFAddr 18ull /* Far address in memory */
55#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030056#define OpES 20ull /* ES */
57#define OpCS 21ull /* CS */
58#define OpSS 22ull /* SS */
59#define OpDS 23ull /* DS */
60#define OpFS 24ull /* FS */
61#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020062#define OpMem8 26ull /* 8-bit zero extended memory operand */
Nadav Amit5e2c6882012-12-06 21:55:10 -020063#define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */
Paolo Bonzini7fa57952013-05-09 11:32:50 +020064#define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */
Avi Kivity820207c2013-02-09 11:31:45 +020065#define OpAccLo 29ull /* Low part of extended acc (AX/AX/EAX/RAX) */
66#define OpAccHi 30ull /* High part of extended acc (-/DX/EDX/RDX) */
Avi Kivitya99455492011-09-13 10:45:41 +030067
Avi Kivity0fe59122011-09-13 10:45:47 +030068#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030069#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya99455492011-09-13 10:45:41 +030070
71/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080072 * Opcode effective-address decode tables.
73 * Note that we only emulate instructions that have at least one memory
74 * operand (excluding implicit stack references). We assume that stack
75 * references and instruction fetches will never occur in special memory
76 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
77 * not be handled.
78 */
79
80/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030081#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080082/* Destination operand type. */
Avi Kivitya99455492011-09-13 10:45:41 +030083#define DstShift 1
84#define ImplicitOps (OpImplicit << DstShift)
85#define DstReg (OpReg << DstShift)
86#define DstMem (OpMem << DstShift)
87#define DstAcc (OpAcc << DstShift)
88#define DstDI (OpDI << DstShift)
89#define DstMem64 (OpMem64 << DstShift)
Nadav Amit16bebef2014-12-25 02:52:18 +020090#define DstMem16 (OpMem16 << DstShift)
Avi Kivitya99455492011-09-13 10:45:41 +030091#define DstImmUByte (OpImmUByte << DstShift)
92#define DstDX (OpDX << DstShift)
Avi Kivity820207c2013-02-09 11:31:45 +020093#define DstAccLo (OpAccLo << DstShift)
Avi Kivitya99455492011-09-13 10:45:41 +030094#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080095/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030096#define SrcShift 6
97#define SrcNone (OpNone << SrcShift)
98#define SrcReg (OpReg << SrcShift)
99#define SrcMem (OpMem << SrcShift)
100#define SrcMem16 (OpMem16 << SrcShift)
101#define SrcMem32 (OpMem32 << SrcShift)
102#define SrcImm (OpImm << SrcShift)
103#define SrcImmByte (OpImmByte << SrcShift)
104#define SrcOne (OpOne << SrcShift)
105#define SrcImmUByte (OpImmUByte << SrcShift)
106#define SrcImmU (OpImmU << SrcShift)
107#define SrcSI (OpSI << SrcShift)
Paolo Bonzini7fa57952013-05-09 11:32:50 +0200108#define SrcXLat (OpXLat << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300109#define SrcImmFAddr (OpImmFAddr << SrcShift)
110#define SrcMemFAddr (OpMemFAddr << SrcShift)
111#define SrcAcc (OpAcc << SrcShift)
112#define SrcImmU16 (OpImmU16 << SrcShift)
Nadav Amit5e2c6882012-12-06 21:55:10 -0200113#define SrcImm64 (OpImm64 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300114#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200115#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity820207c2013-02-09 11:31:45 +0200116#define SrcAccHi (OpAccHi << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300117#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300118#define BitOp (1<<11)
119#define MemAbs (1<<12) /* Memory operand is absolute displacement */
120#define String (1<<13) /* String instruction (rep capable) */
121#define Stack (1<<14) /* Stack instruction (push/pop) */
122#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
123#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
124#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
125#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
126#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
Gleb Natapov045a2822012-12-20 16:57:43 +0200127#define Escape (5<<15) /* Escape to coprocessor instruction */
Nadav Amit39f062f2014-11-26 15:47:18 +0200128#define InstrDual (6<<15) /* Alternate instruction decoding of mod == 3 */
Nadav Amit2276b512015-01-26 09:32:24 +0200129#define ModeDual (7<<15) /* Different instruction for 32/64 bit */
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300130#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300131/* Generic ModRM decode. */
132#define ModRM (1<<19)
133/* Destination is only written; never read. */
134#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300135/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200136#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Borislav Petkovb51e9742013-09-22 16:44:52 +0200137#define EmulateOnUD (1<<22) /* Emulate if unsupported by the host */
Avi Kivity5a506b12010-08-01 15:10:29 +0300138#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300139#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300140#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200141#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200142#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300143#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800144#define PageTable (1 << 29) /* instruction used to write page table */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300145#define NotImpl (1 << 30) /* instruction is not implemented */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100146/* Source 2 operand type */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300147#define Src2Shift (31)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300148#define Src2None (OpNone << Src2Shift)
Avi Kivityab2c5ce2013-02-09 11:31:46 +0200149#define Src2Mem (OpMem << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300150#define Src2CL (OpCL << Src2Shift)
151#define Src2ImmByte (OpImmByte << Src2Shift)
152#define Src2One (OpOne << Src2Shift)
153#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300154#define Src2ES (OpES << Src2Shift)
155#define Src2CS (OpCS << Src2Shift)
156#define Src2SS (OpSS << Src2Shift)
157#define Src2DS (OpDS << Src2Shift)
158#define Src2FS (OpFS << Src2Shift)
159#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300160#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300161#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Avi Kivity1c11b372012-04-09 18:39:59 +0300162#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
163#define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */
164#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
Avi Kivitye28bbd42013-01-04 16:18:48 +0200165#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
Avi Kivityb6744dc2013-01-04 16:18:50 +0200166#define NoWrite ((u64)1 << 45) /* No writeback */
Avi Kivityfb32b1e2013-02-09 11:31:44 +0200167#define SrcWrite ((u64)1 << 46) /* Write back src operand */
Nadav Amit9b88ae92014-05-25 23:05:21 +0300168#define NoMod ((u64)1 << 47) /* Mod field is ignored */
Paolo Bonzinid40a6892014-03-27 11:58:02 +0100169#define Intercept ((u64)1 << 48) /* Has valid intercept field */
170#define CheckPerm ((u64)1 << 49) /* Has valid check_perm field */
Nadav Amit68efa762014-06-18 17:19:35 +0300171#define PrivUD ((u64)1 << 51) /* #UD instead of #GP on CPL > 0 */
Nadav Amit58b70752014-10-24 11:35:09 +0300172#define NearBranch ((u64)1 << 52) /* Near branches */
Nadav Amited9aad22014-11-02 11:55:00 +0200173#define No16 ((u64)1 << 53) /* No 16 bit operand */
Nadav Amitab708092014-12-25 02:52:21 +0200174#define IncSP ((u64)1 << 54) /* SP is incremented before ModRM calc */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800175
Avi Kivity820207c2013-02-09 11:31:45 +0200176#define DstXacc (DstAccLo | SrcAccHi | SrcWrite)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800177
Avi Kivityd0e53322010-07-29 15:11:54 +0300178#define X2(x...) x, x
179#define X3(x...) X2(x), x
180#define X4(x...) X2(x), X2(x)
181#define X5(x...) X4(x), x
182#define X6(x...) X4(x), X2(x)
183#define X7(x...) X4(x), X3(x)
184#define X8(x...) X4(x), X4(x)
185#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300186
Avi Kivitye28bbd42013-01-04 16:18:48 +0200187#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
188#define FASTOP_SIZE 8
189
190/*
191 * fastop functions have a special calling convention:
192 *
Avi Kivity017da7b2013-02-09 11:31:47 +0200193 * dst: rax (in/out)
194 * src: rdx (in/out)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200195 * src2: rcx (in)
196 * flags: rflags (in/out)
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200197 * ex: rsi (in:fastop pointer, out:zero if exception)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200198 *
199 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
200 * different operand sizes can be reached by calculation, rather than a jump
201 * table (which would be bigger than the code).
202 *
203 * fastop functions are declared as taking a never-defined fastop parameter,
204 * so they can't be called from C directly.
205 */
206
207struct fastop;
208
Avi Kivityd65b1de2010-07-29 15:11:35 +0300209struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300210 u64 flags : 56;
211 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300212 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300213 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200214 const struct opcode *group;
215 const struct group_dual *gdual;
216 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200217 const struct escape *esc;
Nadav Amit39f062f2014-11-26 15:47:18 +0200218 const struct instr_dual *idual;
Nadav Amit2276b512015-01-26 09:32:24 +0200219 const struct mode_dual *mdual;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200220 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300221 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200222 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300223};
224
225struct group_dual {
226 struct opcode mod012[8];
227 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300228};
229
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200230struct gprefix {
231 struct opcode pfx_no;
232 struct opcode pfx_66;
233 struct opcode pfx_f2;
234 struct opcode pfx_f3;
235};
236
Gleb Natapov045a2822012-12-20 16:57:43 +0200237struct escape {
238 struct opcode op[8];
239 struct opcode high[64];
240};
241
Nadav Amit39f062f2014-11-26 15:47:18 +0200242struct instr_dual {
243 struct opcode mod012;
244 struct opcode mod3;
245};
246
Nadav Amit2276b512015-01-26 09:32:24 +0200247struct mode_dual {
248 struct opcode mode32;
249 struct opcode mode64;
250};
251
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300252#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300253
Nadav Amit3dc4bc42014-12-25 02:52:19 +0200254enum x86_transfer_type {
255 X86_TRANSFER_NONE,
256 X86_TRANSFER_CALL_JMP,
257 X86_TRANSFER_RET,
258 X86_TRANSFER_TASK_SWITCH,
259};
260
Avi Kivitydd856ef2012-08-27 23:46:17 +0300261static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
262{
263 if (!(ctxt->regs_valid & (1 << nr))) {
264 ctxt->regs_valid |= 1 << nr;
265 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
266 }
267 return ctxt->_regs[nr];
268}
269
270static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
271{
272 ctxt->regs_valid |= 1 << nr;
273 ctxt->regs_dirty |= 1 << nr;
274 return &ctxt->_regs[nr];
275}
276
277static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
278{
279 reg_read(ctxt, nr);
280 return reg_write(ctxt, nr);
281}
282
283static void writeback_registers(struct x86_emulate_ctxt *ctxt)
284{
285 unsigned reg;
286
287 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
288 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
289}
290
291static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
292{
293 ctxt->regs_dirty = 0;
294 ctxt->regs_valid = 0;
295}
296
Avi Kivity6aa8b732006-12-10 02:21:36 -0800297/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800298 * These EFLAGS bits are restored from saved value during emulation, and
299 * any changes are written back to the saved value after emulation.
300 */
Nadav Amit0efb0442015-03-29 16:33:03 +0300301#define EFLAGS_MASK (X86_EFLAGS_OF|X86_EFLAGS_SF|X86_EFLAGS_ZF|X86_EFLAGS_AF|\
302 X86_EFLAGS_PF|X86_EFLAGS_CF)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800303
Avi Kivitydda96d82008-11-26 15:14:10 +0200304#ifdef CONFIG_X86_64
305#define ON64(x) x
306#else
307#define ON64(x)
308#endif
309
Avi Kivity4d758342013-01-19 19:51:55 +0200310static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *));
311
Avi Kivityb7d491e2013-01-04 16:18:49 +0200312#define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t"
313#define FOP_RET "ret \n\t"
314
315#define FOP_START(op) \
316 extern void em_##op(struct fastop *fake); \
317 asm(".pushsection .text, \"ax\" \n\t" \
318 ".global em_" #op " \n\t" \
319 FOP_ALIGN \
320 "em_" #op ": \n\t"
321
322#define FOP_END \
323 ".popsection")
324
Avi Kivity0bdea062013-01-19 19:51:50 +0200325#define FOPNOP() FOP_ALIGN FOP_RET
326
Avi Kivityb7d491e2013-01-04 16:18:49 +0200327#define FOP1E(op, dst) \
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200328 FOP_ALIGN "10: " #op " %" #dst " \n\t" FOP_RET
329
330#define FOP1EEX(op, dst) \
331 FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200332
333#define FASTOP1(op) \
334 FOP_START(op) \
335 FOP1E(op##b, al) \
336 FOP1E(op##w, ax) \
337 FOP1E(op##l, eax) \
338 ON64(FOP1E(op##q, rax)) \
339 FOP_END
340
Avi Kivityb9fa4092013-02-09 11:31:48 +0200341/* 1-operand, using src2 (for MUL/DIV r/m) */
342#define FASTOP1SRC2(op, name) \
343 FOP_START(name) \
344 FOP1E(op, cl) \
345 FOP1E(op, cx) \
346 FOP1E(op, ecx) \
347 ON64(FOP1E(op, rcx)) \
348 FOP_END
349
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200350/* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */
351#define FASTOP1SRC2EX(op, name) \
352 FOP_START(name) \
353 FOP1EEX(op, cl) \
354 FOP1EEX(op, cx) \
355 FOP1EEX(op, ecx) \
356 ON64(FOP1EEX(op, rcx)) \
357 FOP_END
358
Avi Kivityf7857f32013-01-04 16:18:53 +0200359#define FOP2E(op, dst, src) \
360 FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET
361
362#define FASTOP2(op) \
363 FOP_START(op) \
Avi Kivity017da7b2013-02-09 11:31:47 +0200364 FOP2E(op##b, al, dl) \
365 FOP2E(op##w, ax, dx) \
366 FOP2E(op##l, eax, edx) \
367 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivityf7857f32013-01-04 16:18:53 +0200368 FOP_END
369
Avi Kivity11c363b2013-01-19 19:51:54 +0200370/* 2 operand, word only */
371#define FASTOP2W(op) \
372 FOP_START(op) \
373 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200374 FOP2E(op##w, ax, dx) \
375 FOP2E(op##l, eax, edx) \
376 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivity11c363b2013-01-19 19:51:54 +0200377 FOP_END
378
Avi Kivity007a3b52013-01-19 19:51:51 +0200379/* 2 operand, src is CL */
380#define FASTOP2CL(op) \
381 FOP_START(op) \
382 FOP2E(op##b, al, cl) \
383 FOP2E(op##w, ax, cl) \
384 FOP2E(op##l, eax, cl) \
385 ON64(FOP2E(op##q, rax, cl)) \
386 FOP_END
387
Nadav Amit5aca3722014-11-02 11:54:50 +0200388/* 2 operand, src and dest are reversed */
389#define FASTOP2R(op, name) \
390 FOP_START(name) \
391 FOP2E(op##b, dl, al) \
392 FOP2E(op##w, dx, ax) \
393 FOP2E(op##l, edx, eax) \
394 ON64(FOP2E(op##q, rdx, rax)) \
395 FOP_END
396
Avi Kivity0bdea062013-01-19 19:51:50 +0200397#define FOP3E(op, dst, src, src2) \
398 FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
399
400/* 3-operand, word-only, src2=cl */
401#define FASTOP3WCL(op) \
402 FOP_START(op) \
403 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200404 FOP3E(op##w, ax, dx, cl) \
405 FOP3E(op##l, eax, edx, cl) \
406 ON64(FOP3E(op##q, rax, rdx, cl)) \
Avi Kivity0bdea062013-01-19 19:51:50 +0200407 FOP_END
408
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200409/* Special case for SETcc - 1 instruction per cc */
410#define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t"
411
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200412asm(".global kvm_fastop_exception \n"
413 "kvm_fastop_exception: xor %esi, %esi; ret");
414
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200415FOP_START(setcc)
416FOP_SETCC(seto)
417FOP_SETCC(setno)
418FOP_SETCC(setc)
419FOP_SETCC(setnc)
420FOP_SETCC(setz)
421FOP_SETCC(setnz)
422FOP_SETCC(setbe)
423FOP_SETCC(setnbe)
424FOP_SETCC(sets)
425FOP_SETCC(setns)
426FOP_SETCC(setp)
427FOP_SETCC(setnp)
428FOP_SETCC(setl)
429FOP_SETCC(setnl)
430FOP_SETCC(setle)
431FOP_SETCC(setnle)
432FOP_END;
433
Paolo Bonzini326f5782013-05-09 11:32:51 +0200434FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET
435FOP_END;
436
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200437static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
438 enum x86_intercept intercept,
439 enum x86_intercept_stage stage)
440{
441 struct x86_instruction_info info = {
442 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300443 .rep_prefix = ctxt->rep_prefix,
444 .modrm_mod = ctxt->modrm_mod,
445 .modrm_reg = ctxt->modrm_reg,
446 .modrm_rm = ctxt->modrm_rm,
447 .src_val = ctxt->src.val64,
Jan Kiszka6cbc5f52014-06-30 12:52:55 +0200448 .dst_val = ctxt->dst.val64,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300449 .src_bytes = ctxt->src.bytes,
450 .dst_bytes = ctxt->dst.bytes,
451 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200452 .next_rip = ctxt->eip,
453 };
454
Avi Kivity29535382011-04-20 13:37:53 +0300455 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200456}
457
Avi Kivityf47cfa32012-06-07 17:49:24 +0300458static void assign_masked(ulong *dest, ulong src, ulong mask)
459{
460 *dest = (*dest & ~mask) | (src & mask);
461}
462
Nadav Amit6fd8e122015-03-30 15:39:20 +0300463static void assign_register(unsigned long *reg, u64 val, int bytes)
464{
465 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
466 switch (bytes) {
467 case 1:
468 *(u8 *)reg = (u8)val;
469 break;
470 case 2:
471 *(u16 *)reg = (u16)val;
472 break;
473 case 4:
474 *reg = (u32)val;
475 break; /* 64b: zero-extend */
476 case 8:
477 *reg = val;
478 break;
479 }
480}
481
Avi Kivity9dac77f2011-06-01 15:34:25 +0300482static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800483{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300484 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800485}
486
Avi Kivityf47cfa32012-06-07 17:49:24 +0300487static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
488{
489 u16 sel;
490 struct desc_struct ss;
491
492 if (ctxt->mode == X86EMUL_MODE_PROT64)
493 return ~0UL;
494 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
495 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
496}
497
Avi Kivity612e89f2012-06-12 20:03:23 +0300498static int stack_size(struct x86_emulate_ctxt *ctxt)
499{
500 return (__fls(stack_mask(ctxt)) + 1) >> 3;
501}
502
Avi Kivity6aa8b732006-12-10 02:21:36 -0800503/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800504static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300505address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800506{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300507 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800508 return reg;
509 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300510 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800511}
512
513static inline unsigned long
Paolo Bonzini01485a22014-11-19 18:25:08 +0100514register_address(struct x86_emulate_ctxt *ctxt, int reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800515{
Paolo Bonzini01485a22014-11-19 18:25:08 +0100516 return address_mask(ctxt, reg_read(ctxt, reg));
Harvey Harrisone4706772008-02-19 07:40:38 -0800517}
518
Avi Kivity5ad105e2012-08-19 14:34:31 +0300519static void masked_increment(ulong *reg, ulong mask, int inc)
520{
521 assign_masked(reg, *reg + inc, mask);
522}
523
Harvey Harrison7a9572752008-02-19 07:40:41 -0800524static inline void
Paolo Bonzini01485a22014-11-19 18:25:08 +0100525register_address_increment(struct x86_emulate_ctxt *ctxt, int reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800526{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300527 ulong mask;
528
Avi Kivity9dac77f2011-06-01 15:34:25 +0300529 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300530 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800531 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300532 mask = ad_mask(ctxt);
Paolo Bonzini01485a22014-11-19 18:25:08 +0100533 masked_increment(reg_rmw(ctxt, reg), mask, inc);
Avi Kivity5ad105e2012-08-19 14:34:31 +0300534}
535
536static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
537{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300538 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800539}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800540
Avi Kivity56697682011-04-03 14:08:51 +0300541static u32 desc_limit_scaled(struct desc_struct *desc)
542{
543 u32 limit = get_desc_limit(desc);
544
545 return desc->g ? (limit << 12) | 0xfff : limit;
546}
547
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900548static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300549{
550 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
551 return 0;
552
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900553 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300554}
555
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200556static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
557 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300558{
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +0200559 WARN_ON(vec > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +0200560 ctxt->exception.vector = vec;
561 ctxt->exception.error_code = error;
562 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200563 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300564}
565
Joerg Roedel3b88e412011-04-04 12:39:29 +0200566static int emulate_db(struct x86_emulate_ctxt *ctxt)
567{
568 return emulate_exception(ctxt, DB_VECTOR, 0, false);
569}
570
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200571static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300572{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200573 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300574}
575
Avi Kivity618ff152011-04-03 12:32:09 +0300576static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
577{
578 return emulate_exception(ctxt, SS_VECTOR, err, true);
579}
580
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200581static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300582{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200583 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300584}
585
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200586static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300587{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200588 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300589}
590
Avi Kivity34d1f492010-08-26 11:59:01 +0300591static int emulate_de(struct x86_emulate_ctxt *ctxt)
592{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200593 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300594}
595
Avi Kivity1253791d2011-03-29 11:41:27 +0200596static int emulate_nm(struct x86_emulate_ctxt *ctxt)
597{
598 return emulate_exception(ctxt, NM_VECTOR, 0, false);
599}
600
Avi Kivity1aa36612011-04-27 13:20:30 +0300601static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
602{
603 u16 selector;
604 struct desc_struct desc;
605
606 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
607 return selector;
608}
609
610static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
611 unsigned seg)
612{
613 u16 dummy;
614 u32 base3;
615 struct desc_struct desc;
616
617 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
618 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
619}
620
Avi Kivity1c11b372012-04-09 18:39:59 +0300621/*
622 * x86 defines three classes of vector instructions: explicitly
623 * aligned, explicitly unaligned, and the rest, which change behaviour
624 * depending on whether they're AVX encoded or not.
625 *
626 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
627 * subject to the same check.
628 */
629static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
630{
631 if (likely(size < 16))
632 return false;
633
634 if (ctxt->d & Aligned)
635 return true;
636 else if (ctxt->d & Unaligned)
637 return false;
638 else if (ctxt->d & Avx)
639 return false;
640 else
641 return true;
642}
643
Paolo Bonzinid09155d2014-10-27 14:54:44 +0100644static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
645 struct segmented_address addr,
646 unsigned *max_size, unsigned size,
647 bool write, bool fetch,
Nadav Amitd50eaa12014-11-19 17:43:11 +0200648 enum x86emul_mode mode, ulong *linear)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300649{
Avi Kivity618ff152011-04-03 12:32:09 +0300650 struct desc_struct desc;
651 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300652 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300653 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300654 u16 sel;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300655
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900656 la = seg_base(ctxt, addr.seg) + addr.ea;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100657 *max_size = 0;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200658 switch (mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300659 case X86EMUL_MODE_PROT64:
Nadav Amit4be4de72014-09-18 22:39:40 +0300660 if (is_noncanonical_address(la))
Nadav Amitabc7d8a2014-11-19 17:43:12 +0200661 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100662
663 *max_size = min_t(u64, ~0u, (1ull << 48) - la);
664 if (size > *max_size)
665 goto bad;
Avi Kivity618ff152011-04-03 12:32:09 +0300666 break;
667 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300668 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
669 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300670 if (!usable)
671 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200672 /* code segment in protected mode or read-only data segment */
673 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
674 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300675 goto bad;
676 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400677 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300678 goto bad;
679 lim = desc_limit_scaled(&desc);
Paolo Bonzini997b0412014-11-19 18:33:38 +0100680 if (!(desc.type & 8) && (desc.type & 4)) {
Guo Chaofc058682012-06-28 15:19:51 +0800681 /* expand-down segment */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100682 if (addr.ea <= lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300683 goto bad;
684 lim = desc.d ? 0xffffffff : 0xffff;
Avi Kivity618ff152011-04-03 12:32:09 +0300685 }
Paolo Bonzini997b0412014-11-19 18:33:38 +0100686 if (addr.ea > lim)
687 goto bad;
Nadav Amitbac155312015-01-26 09:32:26 +0200688 if (lim == 0xffffffff)
689 *max_size = ~0u;
690 else {
691 *max_size = (u64)lim + 1 - addr.ea;
692 if (size > *max_size)
693 goto bad;
694 }
Nadav Amit31ff6482014-11-19 17:43:13 +0200695 la &= (u32)-1;
Avi Kivity618ff152011-04-03 12:32:09 +0300696 break;
697 }
Avi Kivity1c11b372012-04-09 18:39:59 +0300698 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
699 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300700 *linear = la;
701 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300702bad:
703 if (addr.seg == VCPU_SREG_SS)
Paolo Bonzini36061892014-10-27 14:40:49 +0100704 return emulate_ss(ctxt, 0);
Avi Kivity618ff152011-04-03 12:32:09 +0300705 else
Paolo Bonzini36061892014-10-27 14:40:49 +0100706 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300707}
708
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400709static int linearize(struct x86_emulate_ctxt *ctxt,
710 struct segmented_address addr,
711 unsigned size, bool write,
712 ulong *linear)
713{
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100714 unsigned max_size;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200715 return __linearize(ctxt, addr, &max_size, size, write, false,
716 ctxt->mode, linear);
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400717}
718
Nadav Amitd50eaa12014-11-19 17:43:11 +0200719static inline int assign_eip(struct x86_emulate_ctxt *ctxt, ulong dst,
720 enum x86emul_mode mode)
721{
722 ulong linear;
723 int rc;
724 unsigned max_size;
725 struct segmented_address addr = { .seg = VCPU_SREG_CS,
726 .ea = dst };
727
728 if (ctxt->op_bytes != sizeof(unsigned long))
729 addr.ea = dst & ((1UL << (ctxt->op_bytes << 3)) - 1);
730 rc = __linearize(ctxt, addr, &max_size, 1, false, true, mode, &linear);
731 if (rc == X86EMUL_CONTINUE)
732 ctxt->_eip = addr.ea;
733 return rc;
734}
735
736static inline int assign_eip_near(struct x86_emulate_ctxt *ctxt, ulong dst)
737{
738 return assign_eip(ctxt, dst, ctxt->mode);
739}
740
741static int assign_eip_far(struct x86_emulate_ctxt *ctxt, ulong dst,
742 const struct desc_struct *cs_desc)
743{
744 enum x86emul_mode mode = ctxt->mode;
Nadav Amit82268082015-01-26 09:32:27 +0200745 int rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200746
747#ifdef CONFIG_X86_64
Nadav Amit82268082015-01-26 09:32:27 +0200748 if (ctxt->mode >= X86EMUL_MODE_PROT16) {
749 if (cs_desc->l) {
750 u64 efer = 0;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200751
Nadav Amit82268082015-01-26 09:32:27 +0200752 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
753 if (efer & EFER_LMA)
754 mode = X86EMUL_MODE_PROT64;
755 } else
756 mode = X86EMUL_MODE_PROT32; /* temporary value */
Nadav Amitd50eaa12014-11-19 17:43:11 +0200757 }
758#endif
759 if (mode == X86EMUL_MODE_PROT16 || mode == X86EMUL_MODE_PROT32)
760 mode = cs_desc->d ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
Nadav Amit82268082015-01-26 09:32:27 +0200761 rc = assign_eip(ctxt, dst, mode);
762 if (rc == X86EMUL_CONTINUE)
763 ctxt->mode = mode;
764 return rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200765}
766
767static inline int jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
768{
769 return assign_eip_near(ctxt, ctxt->_eip + rel);
770}
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400771
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200772static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
773 struct segmented_address addr,
774 void *data,
775 unsigned size)
776{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200777 int rc;
778 ulong linear;
779
Avi Kivity83b87952011-04-03 11:31:19 +0300780 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200781 if (rc != X86EMUL_CONTINUE)
782 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300783 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200784}
785
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900786/*
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200787 * Prefetch the remaining bytes of the instruction without crossing page
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900788 * boundary if they are not in fetch_cache yet.
789 */
Paolo Bonzini9506d572014-05-06 13:05:25 +0200790static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
Avi Kivity62266862007-11-20 13:15:52 +0200791{
Avi Kivity62266862007-11-20 13:15:52 +0200792 int rc;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100793 unsigned size, max_size;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200794 unsigned long linear;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200795 int cur_size = ctxt->fetch.end - ctxt->fetch.data;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200796 struct segmented_address addr = { .seg = VCPU_SREG_CS,
Paolo Bonzini17052f12014-05-06 16:33:01 +0200797 .ea = ctxt->eip + cur_size };
798
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100799 /*
800 * We do not know exactly how many bytes will be needed, and
801 * __linearize is expensive, so fetch as much as possible. We
802 * just have to avoid going beyond the 15 byte limit, the end
803 * of the segment, or the end of the page.
804 *
805 * __linearize is called with size 0 so that it does not do any
806 * boundary check itself. Instead, we use max_size to check
807 * against op_size.
808 */
Nadav Amitd50eaa12014-11-19 17:43:11 +0200809 rc = __linearize(ctxt, addr, &max_size, 0, false, true, ctxt->mode,
810 &linear);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200811 if (unlikely(rc != X86EMUL_CONTINUE))
812 return rc;
813
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100814 size = min_t(unsigned, 15UL ^ cur_size, max_size);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200815 size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear));
Paolo Bonzini5cfc7e02014-05-06 13:05:25 +0200816
817 /*
818 * One instruction can only straddle two pages,
819 * and one has been loaded at the beginning of
820 * x86_decode_insn. So, if not enough bytes
821 * still, we must have hit the 15-byte boundary.
822 */
823 if (unlikely(size < op_size))
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100824 return emulate_gp(ctxt, 0);
825
Paolo Bonzini17052f12014-05-06 16:33:01 +0200826 rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end,
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200827 size, &ctxt->exception);
828 if (unlikely(rc != X86EMUL_CONTINUE))
829 return rc;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200830 ctxt->fetch.end += size;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900831 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200832}
833
Paolo Bonzini9506d572014-05-06 13:05:25 +0200834static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt,
835 unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200836{
Nadav Amit08da44a2014-10-03 01:10:04 +0300837 unsigned done_size = ctxt->fetch.end - ctxt->fetch.ptr;
838
839 if (unlikely(done_size < size))
840 return __do_insn_fetch_bytes(ctxt, size - done_size);
Paolo Bonzini9506d572014-05-06 13:05:25 +0200841 else
842 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200843}
844
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900845/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900846#define insn_fetch(_type, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200847({ _type _x; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200848 \
849 rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900850 if (rc != X86EMUL_CONTINUE) \
851 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200852 ctxt->_eip += sizeof(_type); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200853 _x = *(_type __aligned(1) *) ctxt->fetch.ptr; \
854 ctxt->fetch.ptr += sizeof(_type); \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200855 _x; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900856})
857
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900858#define insn_fetch_arr(_arr, _size, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200859({ \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200860 rc = do_insn_fetch_bytes(_ctxt, _size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900861 if (rc != X86EMUL_CONTINUE) \
862 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200863 ctxt->_eip += (_size); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200864 memcpy(_arr, ctxt->fetch.ptr, _size); \
865 ctxt->fetch.ptr += (_size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900866})
867
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000868/*
869 * Given the 'reg' portion of a ModRM byte, and a register block, return a
870 * pointer into the block that addresses the relevant register.
871 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
872 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300873static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200874 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875{
876 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200877 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800878
Avi Kivity6aa8b732006-12-10 02:21:36 -0800879 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300880 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
881 else
882 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800883 return p;
884}
885
886static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200887 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800888 u16 *size, unsigned long *address, int op_bytes)
889{
890 int rc;
891
892 if (op_bytes == 2)
893 op_bytes = 3;
894 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200895 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900896 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800897 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200898 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200899 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800900 return rc;
901}
902
Avi Kivity34b77652013-01-19 19:51:56 +0200903FASTOP2(add);
904FASTOP2(or);
905FASTOP2(adc);
906FASTOP2(sbb);
907FASTOP2(and);
908FASTOP2(sub);
909FASTOP2(xor);
910FASTOP2(cmp);
911FASTOP2(test);
912
Avi Kivityb9fa4092013-02-09 11:31:48 +0200913FASTOP1SRC2(mul, mul_ex);
914FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200915FASTOP1SRC2EX(div, div_ex);
916FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +0200917
Avi Kivity34b77652013-01-19 19:51:56 +0200918FASTOP3WCL(shld);
919FASTOP3WCL(shrd);
920
921FASTOP2W(imul);
922
923FASTOP1(not);
924FASTOP1(neg);
925FASTOP1(inc);
926FASTOP1(dec);
927
928FASTOP2CL(rol);
929FASTOP2CL(ror);
930FASTOP2CL(rcl);
931FASTOP2CL(rcr);
932FASTOP2CL(shl);
933FASTOP2CL(shr);
934FASTOP2CL(sar);
935
936FASTOP2W(bsf);
937FASTOP2W(bsr);
938FASTOP2W(bt);
939FASTOP2W(bts);
940FASTOP2W(btr);
941FASTOP2W(btc);
942
Avi Kivitye47a5f52013-02-09 11:31:51 +0200943FASTOP2(xadd);
944
Nadav Amit5aca3722014-11-02 11:54:50 +0200945FASTOP2R(cmp, cmp_r);
946
Nadav Amit900efe22015-03-30 15:39:21 +0300947static int em_bsf_c(struct x86_emulate_ctxt *ctxt)
948{
949 /* If src is zero, do not writeback, but update flags */
950 if (ctxt->src.val == 0)
951 ctxt->dst.type = OP_NONE;
952 return fastop(ctxt, em_bsf);
953}
954
955static int em_bsr_c(struct x86_emulate_ctxt *ctxt)
956{
957 /* If src is zero, do not writeback, but update flags */
958 if (ctxt->src.val == 0)
959 ctxt->dst.type = OP_NONE;
960 return fastop(ctxt, em_bsr);
961}
962
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200963static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300964{
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200965 u8 rc;
966 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300967
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200968 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +0200969 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200970 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
971 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300972}
973
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300974static void fetch_register_operand(struct operand *op)
975{
976 switch (op->bytes) {
977 case 1:
978 op->val = *(u8 *)op->addr.reg;
979 break;
980 case 2:
981 op->val = *(u16 *)op->addr.reg;
982 break;
983 case 4:
984 op->val = *(u32 *)op->addr.reg;
985 break;
986 case 8:
987 op->val = *(u64 *)op->addr.reg;
988 break;
989 }
990}
991
Avi Kivity1253791d2011-03-29 11:41:27 +0200992static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
993{
994 ctxt->ops->get_fpu(ctxt);
995 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200996 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
997 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
998 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
999 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
1000 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
1001 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
1002 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
1003 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001004#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001005 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
1006 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
1007 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
1008 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
1009 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
1010 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
1011 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
1012 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001013#endif
1014 default: BUG();
1015 }
1016 ctxt->ops->put_fpu(ctxt);
1017}
1018
1019static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
1020 int reg)
1021{
1022 ctxt->ops->get_fpu(ctxt);
1023 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001024 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
1025 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
1026 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
1027 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
1028 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
1029 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
1030 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
1031 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001032#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001033 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
1034 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
1035 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
1036 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
1037 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
1038 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
1039 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
1040 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001041#endif
1042 default: BUG();
1043 }
1044 ctxt->ops->put_fpu(ctxt);
1045}
1046
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001047static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1048{
1049 ctxt->ops->get_fpu(ctxt);
1050 switch (reg) {
1051 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1052 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1053 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1054 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1055 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1056 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1057 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1058 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1059 default: BUG();
1060 }
1061 ctxt->ops->put_fpu(ctxt);
1062}
1063
1064static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1065{
1066 ctxt->ops->get_fpu(ctxt);
1067 switch (reg) {
1068 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1069 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1070 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1071 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1072 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1073 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1074 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1075 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1076 default: BUG();
1077 }
1078 ctxt->ops->put_fpu(ctxt);
1079}
1080
Gleb Natapov045a2822012-12-20 16:57:43 +02001081static int em_fninit(struct x86_emulate_ctxt *ctxt)
1082{
1083 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1084 return emulate_nm(ctxt);
1085
1086 ctxt->ops->get_fpu(ctxt);
1087 asm volatile("fninit");
1088 ctxt->ops->put_fpu(ctxt);
1089 return X86EMUL_CONTINUE;
1090}
1091
1092static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1093{
1094 u16 fcw;
1095
1096 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1097 return emulate_nm(ctxt);
1098
1099 ctxt->ops->get_fpu(ctxt);
1100 asm volatile("fnstcw %0": "+m"(fcw));
1101 ctxt->ops->put_fpu(ctxt);
1102
Gleb Natapov045a2822012-12-20 16:57:43 +02001103 ctxt->dst.val = fcw;
1104
1105 return X86EMUL_CONTINUE;
1106}
1107
1108static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1109{
1110 u16 fsw;
1111
1112 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1113 return emulate_nm(ctxt);
1114
1115 ctxt->ops->get_fpu(ctxt);
1116 asm volatile("fnstsw %0": "+m"(fsw));
1117 ctxt->ops->put_fpu(ctxt);
1118
Gleb Natapov045a2822012-12-20 16:57:43 +02001119 ctxt->dst.val = fsw;
1120
1121 return X86EMUL_CONTINUE;
1122}
1123
Avi Kivity1253791d2011-03-29 11:41:27 +02001124static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001125 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001126{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001127 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001128
Avi Kivity9dac77f2011-06-01 15:34:25 +03001129 if (!(ctxt->d & ModRM))
1130 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity1253791d2011-03-29 11:41:27 +02001131
Avi Kivity9dac77f2011-06-01 15:34:25 +03001132 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001133 op->type = OP_XMM;
1134 op->bytes = 16;
1135 op->addr.xmm = reg;
1136 read_sse_reg(ctxt, &op->vec_val, reg);
1137 return;
1138 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001139 if (ctxt->d & Mmx) {
1140 reg &= 7;
1141 op->type = OP_MM;
1142 op->bytes = 8;
1143 op->addr.mm = reg;
1144 return;
1145 }
Avi Kivity1253791d2011-03-29 11:41:27 +02001146
Avi Kivity3c118e22007-10-31 10:27:04 +02001147 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001148 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1149 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1150
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001151 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001152 op->orig_val = op->val;
1153}
1154
Avi Kivitya6e34072012-06-10 17:15:39 +03001155static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1156{
1157 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1158 ctxt->modrm_seg = VCPU_SREG_SS;
1159}
1160
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001161static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001162 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001163{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001164 u8 sib;
Bandan Das02357bd2014-04-16 12:46:11 -04001165 int index_reg, base_reg, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001166 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001167 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001168
Bandan Das02357bd2014-04-16 12:46:11 -04001169 ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */
1170 index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */
1171 base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001172
Bandan Das02357bd2014-04-16 12:46:11 -04001173 ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001174 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
Bandan Das02357bd2014-04-16 12:46:11 -04001175 ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001176 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001177
Nadav Amit9b88ae92014-05-25 23:05:21 +03001178 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001179 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001180 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001181 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001182 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001183 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001184 op->type = OP_XMM;
1185 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001186 op->addr.xmm = ctxt->modrm_rm;
1187 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001188 return rc;
1189 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001190 if (ctxt->d & Mmx) {
1191 op->type = OP_MM;
1192 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001193 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001194 return rc;
1195 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001196 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001197 return rc;
1198 }
1199
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001200 op->type = OP_MEM;
1201
Avi Kivity9dac77f2011-06-01 15:34:25 +03001202 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001203 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1204 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1205 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1206 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001207
1208 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001209 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001210 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001211 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001212 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001213 break;
1214 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001215 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001216 break;
1217 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001218 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001219 break;
1220 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001221 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001222 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001223 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001224 break;
1225 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001226 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001227 break;
1228 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001229 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001230 break;
1231 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001232 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001233 break;
1234 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001235 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001236 break;
1237 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001238 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001239 break;
1240 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001241 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001242 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001243 break;
1244 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001245 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001246 break;
1247 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001248 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1249 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1250 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001251 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001252 } else {
1253 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001254 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001255 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001256 index_reg |= (sib >> 3) & 7;
1257 base_reg |= sib & 7;
1258 scale = sib >> 6;
1259
Avi Kivity9dac77f2011-06-01 15:34:25 +03001260 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001261 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001262 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001263 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001264 adjust_modrm_seg(ctxt, base_reg);
Nadav Amitab708092014-12-25 02:52:21 +02001265 /* Increment ESP on POP [ESP] */
1266 if ((ctxt->d & IncSP) &&
1267 base_reg == VCPU_REGS_RSP)
1268 modrm_ea += ctxt->op_bytes;
Avi Kivitya6e34072012-06-10 17:15:39 +03001269 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001270 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001271 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001272 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Nadav Amit5b38ab82014-11-02 11:54:41 +02001273 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity84411d82008-06-15 21:53:26 -07001274 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001275 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001276 } else {
1277 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001278 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001279 adjust_modrm_seg(ctxt, base_reg);
1280 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001281 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001282 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001283 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001284 break;
1285 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001286 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001287 break;
1288 }
1289 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001290 op->addr.mem.ea = modrm_ea;
Bandan Das41061cd2014-04-16 12:46:14 -04001291 if (ctxt->ad_bytes != 8)
1292 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
1293
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001294done:
1295 return rc;
1296}
1297
1298static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001299 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001300{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001301 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001302
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001303 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001304 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001305 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001306 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001307 break;
1308 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001309 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001310 break;
1311 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001312 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001313 break;
1314 }
1315done:
1316 return rc;
1317}
1318
Avi Kivity9dac77f2011-06-01 15:34:25 +03001319static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001320{
Sheng Yang7129eec2010-09-28 16:33:32 +08001321 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001322
Avi Kivity9dac77f2011-06-01 15:34:25 +03001323 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001324 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001325
Avi Kivity9dac77f2011-06-01 15:34:25 +03001326 if (ctxt->src.bytes == 2)
1327 sv = (s16)ctxt->src.val & (s16)mask;
1328 else if (ctxt->src.bytes == 4)
1329 sv = (s32)ctxt->src.val & (s32)mask;
Nadav Amit7dec5602014-06-15 16:12:57 +03001330 else
1331 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001332
Nadav Amit1c1c35a2014-11-19 17:43:09 +02001333 ctxt->dst.addr.mem.ea = address_mask(ctxt,
1334 ctxt->dst.addr.mem.ea + (sv >> 3));
Wei Yongjun35c843c2010-08-09 11:34:56 +08001335 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001336
1337 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001338 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001339}
1340
Gleb Natapov9de41572010-04-28 19:15:22 +03001341static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001342 unsigned long addr, void *dest, unsigned size)
1343{
1344 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001345 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001346
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001347 if (mc->pos < mc->end)
1348 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001349
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001350 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001351
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001352 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1353 &ctxt->exception);
1354 if (rc != X86EMUL_CONTINUE)
1355 return rc;
1356
1357 mc->end += size;
1358
1359read_cached:
1360 memcpy(dest, mc->data + mc->pos, size);
1361 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001362 return X86EMUL_CONTINUE;
1363}
1364
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001365static int segmented_read(struct x86_emulate_ctxt *ctxt,
1366 struct segmented_address addr,
1367 void *data,
1368 unsigned size)
1369{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001370 int rc;
1371 ulong linear;
1372
Avi Kivity83b87952011-04-03 11:31:19 +03001373 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001374 if (rc != X86EMUL_CONTINUE)
1375 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001376 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001377}
1378
1379static int segmented_write(struct x86_emulate_ctxt *ctxt,
1380 struct segmented_address addr,
1381 const void *data,
1382 unsigned size)
1383{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001384 int rc;
1385 ulong linear;
1386
Avi Kivity83b87952011-04-03 11:31:19 +03001387 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001388 if (rc != X86EMUL_CONTINUE)
1389 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001390 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1391 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001392}
1393
1394static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1395 struct segmented_address addr,
1396 const void *orig_data, const void *data,
1397 unsigned size)
1398{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001399 int rc;
1400 ulong linear;
1401
Avi Kivity83b87952011-04-03 11:31:19 +03001402 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001403 if (rc != X86EMUL_CONTINUE)
1404 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001405 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1406 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001407}
1408
Gleb Natapov7b262e92010-03-18 15:20:27 +02001409static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001410 unsigned int size, unsigned short port,
1411 void *dest)
1412{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001413 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001414
1415 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001416 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001417 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001418 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Nadav Amit0efb0442015-03-29 16:33:03 +03001419 in_page = (ctxt->eflags & X86_EFLAGS_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001420 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1421 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Mark Rustadb55a8142014-07-25 06:27:05 -07001422 n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001423 if (n == 0)
1424 n = 1;
1425 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001426 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001427 return 0;
1428 rc->end = n * size;
1429 }
1430
Nadav Amite6e39f02014-04-18 03:35:10 +03001431 if (ctxt->rep_prefix && (ctxt->d & String) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03001432 !(ctxt->eflags & X86_EFLAGS_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001433 ctxt->dst.data = rc->data + rc->pos;
1434 ctxt->dst.type = OP_MEM_STR;
1435 ctxt->dst.count = (rc->end - rc->pos) / size;
1436 rc->pos = rc->end;
1437 } else {
1438 memcpy(dest, rc->data + rc->pos, size);
1439 rc->pos += size;
1440 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001441 return 1;
1442}
1443
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001444static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1445 u16 index, struct desc_struct *desc)
1446{
1447 struct desc_ptr dt;
1448 ulong addr;
1449
1450 ctxt->ops->get_idt(ctxt, &dt);
1451
1452 if (dt.size < index * 8 + 7)
1453 return emulate_gp(ctxt, index << 3 | 0x2);
1454
1455 addr = dt.address + index * 8;
1456 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1457 &ctxt->exception);
1458}
1459
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001460static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001461 u16 selector, struct desc_ptr *dt)
1462{
Mathias Krause0225fb52012-08-30 01:30:16 +02001463 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001464 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001465
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001466 if (selector & 1 << 2) {
1467 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001468 u16 sel;
1469
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001470 memset (dt, 0, sizeof *dt);
Nadav Amit2eedcac2014-06-02 18:34:05 +03001471 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1472 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001473 return;
1474
1475 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001476 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001477 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001478 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001479}
1480
Nadav Amitedccda72014-12-25 02:52:23 +02001481static int get_descriptor_ptr(struct x86_emulate_ctxt *ctxt,
1482 u16 selector, ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001483{
1484 struct desc_ptr dt;
1485 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001486 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001487
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001488 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001489
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001490 if (dt.size < index * 8 + 7)
1491 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001492
1493 addr = dt.address + index * 8;
Nadav Amitedccda72014-12-25 02:52:23 +02001494
1495#ifdef CONFIG_X86_64
1496 if (addr >> 32 != 0) {
1497 u64 efer = 0;
1498
1499 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1500 if (!(efer & EFER_LMA))
1501 addr &= (u32)-1;
1502 }
1503#endif
1504
1505 *desc_addr_p = addr;
1506 return X86EMUL_CONTINUE;
1507}
1508
1509/* allowed just for 8 bytes segments */
1510static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1511 u16 selector, struct desc_struct *desc,
1512 ulong *desc_addr_p)
1513{
1514 int rc;
1515
1516 rc = get_descriptor_ptr(ctxt, selector, desc_addr_p);
1517 if (rc != X86EMUL_CONTINUE)
1518 return rc;
1519
1520 return ctxt->ops->read_std(ctxt, *desc_addr_p, desc, sizeof(*desc),
1521 &ctxt->exception);
1522}
1523
1524/* allowed just for 8 bytes segments */
1525static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1526 u16 selector, struct desc_struct *desc)
1527{
1528 int rc;
1529 ulong addr;
1530
1531 rc = get_descriptor_ptr(ctxt, selector, &addr);
1532 if (rc != X86EMUL_CONTINUE)
1533 return rc;
1534
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001535 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1536 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001537}
1538
Gleb Natapov5601d052011-03-07 14:55:06 +02001539/* Does not support long mode */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001540static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Nadav Amitd1442d82014-09-18 22:39:39 +03001541 u16 selector, int seg, u8 cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001542 enum x86_transfer_type transfer,
Nadav Amitd1442d82014-09-18 22:39:39 +03001543 struct desc_struct *desc)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001544{
Avi Kivity869be992012-06-13 16:30:53 +03001545 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001546 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001547 unsigned err_vec = GP_VECTOR;
1548 u32 err_code = 0;
1549 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001550 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001551 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001552 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001553 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001554
1555 memset(&seg_desc, 0, sizeof seg_desc);
1556
Kevin Wolff8da94e2013-04-11 14:06:03 +02001557 if (ctxt->mode == X86EMUL_MODE_REAL) {
1558 /* set real mode segment descriptor (keep limit etc. for
1559 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001560 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001561 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001562 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001563 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1564 /* VM86 needs a clean new segment descriptor */
1565 set_desc_base(&seg_desc, selector << 4);
1566 set_desc_limit(&seg_desc, 0xffff);
1567 seg_desc.type = 3;
1568 seg_desc.p = 1;
1569 seg_desc.s = 1;
1570 seg_desc.dpl = 3;
1571 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001572 }
1573
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001574 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001575
1576 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1577 if ((seg == VCPU_SREG_CS
1578 || (seg == VCPU_SREG_SS
1579 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1580 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001581 && null_selector)
1582 goto exception;
1583
1584 /* TR should be in GDT only */
1585 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1586 goto exception;
1587
1588 if (null_selector) /* for NULL selector skip all following checks */
1589 goto load;
1590
Avi Kivitye9194642012-06-13 16:29:39 +03001591 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001592 if (ret != X86EMUL_CONTINUE)
1593 return ret;
1594
1595 err_code = selector & 0xfffc;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001596 err_vec = (transfer == X86_TRANSFER_TASK_SWITCH) ? TS_VECTOR :
1597 GP_VECTOR;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001598
Guo Chaofc058682012-06-28 15:19:51 +08001599 /* can't load system descriptor into segment selector */
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001600 if (seg <= VCPU_SREG_GS && !seg_desc.s) {
1601 if (transfer == X86_TRANSFER_CALL_JMP)
1602 return X86EMUL_UNHANDLEABLE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001603 goto exception;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001604 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001605
1606 if (!seg_desc.p) {
1607 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1608 goto exception;
1609 }
1610
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001611 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001612
1613 switch (seg) {
1614 case VCPU_SREG_SS:
1615 /*
1616 * segment is not a writable data segment or segment
1617 * selector's RPL != CPL or segment selector's RPL != CPL
1618 */
1619 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1620 goto exception;
1621 break;
1622 case VCPU_SREG_CS:
1623 if (!(seg_desc.type & 8))
1624 goto exception;
1625
1626 if (seg_desc.type & 4) {
1627 /* conforming */
1628 if (dpl > cpl)
1629 goto exception;
1630 } else {
1631 /* nonconforming */
1632 if (rpl > cpl || dpl != cpl)
1633 goto exception;
1634 }
Nadav Amit040c8dc2014-09-18 22:39:43 +03001635 /* in long-mode d/b must be clear if l is set */
1636 if (seg_desc.d && seg_desc.l) {
1637 u64 efer = 0;
1638
1639 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1640 if (efer & EFER_LMA)
1641 goto exception;
1642 }
1643
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001644 /* CS(RPL) <- CPL */
1645 selector = (selector & 0xfffc) | cpl;
1646 break;
1647 case VCPU_SREG_TR:
1648 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1649 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001650 old_desc = seg_desc;
1651 seg_desc.type |= 2; /* busy */
1652 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1653 sizeof(seg_desc), &ctxt->exception);
1654 if (ret != X86EMUL_CONTINUE)
1655 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001656 break;
1657 case VCPU_SREG_LDTR:
1658 if (seg_desc.s || seg_desc.type != 2)
1659 goto exception;
1660 break;
1661 default: /* DS, ES, FS, or GS */
1662 /*
1663 * segment is not a data or readable code segment or
1664 * ((segment is a data or nonconforming code segment)
1665 * and (both RPL and CPL > DPL))
1666 */
1667 if ((seg_desc.type & 0xa) == 0x8 ||
1668 (((seg_desc.type & 0xc) != 0xc) &&
1669 (rpl > dpl && cpl > dpl)))
1670 goto exception;
1671 break;
1672 }
1673
1674 if (seg_desc.s) {
1675 /* mark segment as accessed */
Nadav Amite2cefa72014-12-25 02:52:22 +02001676 if (!(seg_desc.type & 1)) {
1677 seg_desc.type |= 1;
1678 ret = write_segment_descriptor(ctxt, selector,
1679 &seg_desc);
1680 if (ret != X86EMUL_CONTINUE)
1681 return ret;
1682 }
Nadav Amite37a75a2014-06-02 18:34:04 +03001683 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
1684 ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3,
1685 sizeof(base3), &ctxt->exception);
1686 if (ret != X86EMUL_CONTINUE)
1687 return ret;
Nadav Amit9a9abf62014-11-02 11:54:56 +02001688 if (is_noncanonical_address(get_desc_base(&seg_desc) |
1689 ((u64)base3 << 32)))
1690 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001691 }
1692load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001693 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Nadav Amitd1442d82014-09-18 22:39:39 +03001694 if (desc)
1695 *desc = seg_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001696 return X86EMUL_CONTINUE;
1697exception:
Paolo Bonzini592f0852014-08-20 10:05:08 +02001698 return emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001699}
1700
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001701static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1702 u16 selector, int seg)
1703{
1704 u8 cpl = ctxt->ops->cpl(ctxt);
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001705 return __load_segment_descriptor(ctxt, selector, seg, cpl,
1706 X86_TRANSFER_NONE, NULL);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001707}
1708
Wei Yongjun31be40b2010-08-17 09:17:30 +08001709static void write_register_operand(struct operand *op)
1710{
Nadav Amit6fd8e122015-03-30 15:39:20 +03001711 return assign_register(op->addr.reg, op->val, op->bytes);
Wei Yongjun31be40b2010-08-17 09:17:30 +08001712}
1713
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001714static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001715{
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001716 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001717 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001718 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001719 break;
1720 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001721 if (ctxt->lock_prefix)
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001722 return segmented_cmpxchg(ctxt,
1723 op->addr.mem,
1724 &op->orig_val,
1725 &op->val,
1726 op->bytes);
1727 else
1728 return segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001729 op->addr.mem,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001730 &op->val,
1731 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001732 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001733 case OP_MEM_STR:
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001734 return segmented_write(ctxt,
1735 op->addr.mem,
1736 op->data,
1737 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001738 break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001739 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001740 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001741 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001742 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001743 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001744 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001745 case OP_NONE:
1746 /* no writeback */
1747 break;
1748 default:
1749 break;
1750 }
1751 return X86EMUL_CONTINUE;
1752}
1753
Avi Kivity51ddff52012-06-12 20:19:40 +03001754static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001755{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001756 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001757
Avi Kivity5ad105e2012-08-19 14:34:31 +03001758 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001759 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001760 addr.seg = VCPU_SREG_SS;
1761
Avi Kivity51ddff52012-06-12 20:19:40 +03001762 return segmented_write(ctxt, addr, data, bytes);
1763}
1764
1765static int em_push(struct x86_emulate_ctxt *ctxt)
1766{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001767 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001768 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001769 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001770}
1771
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001772static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001773 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001774{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001775 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001776 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001777
Avi Kivitydd856ef2012-08-27 23:46:17 +03001778 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001779 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001780 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001781 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001782 return rc;
1783
Avi Kivity5ad105e2012-08-19 14:34:31 +03001784 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001785 return rc;
1786}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001787
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001788static int em_pop(struct x86_emulate_ctxt *ctxt)
1789{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001790 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001791}
1792
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001793static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001794 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001795{
1796 int rc;
1797 unsigned long val, change_mask;
Nadav Amit0efb0442015-03-29 16:33:03 +03001798 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001799 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001800
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001801 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001802 if (rc != X86EMUL_CONTINUE)
1803 return rc;
1804
Nadav Amit0efb0442015-03-29 16:33:03 +03001805 change_mask = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
1806 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF |
1807 X86_EFLAGS_TF | X86_EFLAGS_DF | X86_EFLAGS_NT |
1808 X86_EFLAGS_AC | X86_EFLAGS_ID;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001809
1810 switch(ctxt->mode) {
1811 case X86EMUL_MODE_PROT64:
1812 case X86EMUL_MODE_PROT32:
1813 case X86EMUL_MODE_PROT16:
1814 if (cpl == 0)
Nadav Amit0efb0442015-03-29 16:33:03 +03001815 change_mask |= X86_EFLAGS_IOPL;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001816 if (cpl <= iopl)
Nadav Amit0efb0442015-03-29 16:33:03 +03001817 change_mask |= X86_EFLAGS_IF;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001818 break;
1819 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001820 if (iopl < 3)
1821 return emulate_gp(ctxt, 0);
Nadav Amit0efb0442015-03-29 16:33:03 +03001822 change_mask |= X86_EFLAGS_IF;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001823 break;
1824 default: /* real mode */
Nadav Amit0efb0442015-03-29 16:33:03 +03001825 change_mask |= (X86_EFLAGS_IOPL | X86_EFLAGS_IF);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001826 break;
1827 }
1828
1829 *(unsigned long *)dest =
1830 (ctxt->eflags & ~change_mask) | (val & change_mask);
1831
1832 return rc;
1833}
1834
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001835static int em_popf(struct x86_emulate_ctxt *ctxt)
1836{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001837 ctxt->dst.type = OP_REG;
1838 ctxt->dst.addr.reg = &ctxt->eflags;
1839 ctxt->dst.bytes = ctxt->op_bytes;
1840 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001841}
1842
Avi Kivity612e89f2012-06-12 20:03:23 +03001843static int em_enter(struct x86_emulate_ctxt *ctxt)
1844{
1845 int rc;
1846 unsigned frame_size = ctxt->src.val;
1847 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001848 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001849
1850 if (nesting_level)
1851 return X86EMUL_UNHANDLEABLE;
1852
Avi Kivitydd856ef2012-08-27 23:46:17 +03001853 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1854 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001855 if (rc != X86EMUL_CONTINUE)
1856 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001857 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001858 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001859 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1860 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001861 stack_mask(ctxt));
1862 return X86EMUL_CONTINUE;
1863}
1864
Avi Kivityf47cfa32012-06-07 17:49:24 +03001865static int em_leave(struct x86_emulate_ctxt *ctxt)
1866{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001867 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001868 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001869 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001870}
1871
Avi Kivity1cd196e2011-09-13 10:45:51 +03001872static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001873{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001874 int seg = ctxt->src2.val;
1875
Avi Kivity9dac77f2011-06-01 15:34:25 +03001876 ctxt->src.val = get_segment_selector(ctxt, seg);
Nadav Amit0fcc2072014-11-02 11:54:51 +02001877 if (ctxt->op_bytes == 4) {
1878 rsp_increment(ctxt, -2);
1879 ctxt->op_bytes = 2;
1880 }
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001881
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001882 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001883}
1884
Avi Kivity1cd196e2011-09-13 10:45:51 +03001885static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001886{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001887 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001888 unsigned long selector;
1889 int rc;
1890
Nadav Amit3313bc42014-12-25 02:52:17 +02001891 rc = emulate_pop(ctxt, &selector, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001892 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001893 return rc;
1894
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001895 if (ctxt->modrm_reg == VCPU_SREG_SS)
1896 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Nadav Amit3313bc42014-12-25 02:52:17 +02001897 if (ctxt->op_bytes > 2)
1898 rsp_increment(ctxt, ctxt->op_bytes - 2);
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001899
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001900 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001901 return rc;
1902}
1903
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001904static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001905{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001906 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001907 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001908 int reg = VCPU_REGS_RAX;
1909
1910 while (reg <= VCPU_REGS_RDI) {
1911 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001912 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001913
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001914 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001915 if (rc != X86EMUL_CONTINUE)
1916 return rc;
1917
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001918 ++reg;
1919 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001920
Wei Yongjunc37eda12010-06-15 09:03:33 +08001921 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001922}
1923
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001924static int em_pushf(struct x86_emulate_ctxt *ctxt)
1925{
Nadav Amit0efb0442015-03-29 16:33:03 +03001926 ctxt->src.val = (unsigned long)ctxt->eflags & ~X86_EFLAGS_VM;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001927 return em_push(ctxt);
1928}
1929
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001930static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001931{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001932 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001933 int reg = VCPU_REGS_RDI;
Nadav Amit6fd8e122015-03-30 15:39:20 +03001934 u32 val;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001935
1936 while (reg >= VCPU_REGS_RAX) {
1937 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001938 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001939 --reg;
1940 }
1941
Nadav Amit6fd8e122015-03-30 15:39:20 +03001942 rc = emulate_pop(ctxt, &val, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001943 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001944 break;
Nadav Amit6fd8e122015-03-30 15:39:20 +03001945 assign_register(reg_rmw(ctxt, reg), val, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001946 --reg;
1947 }
1948 return rc;
1949}
1950
Avi Kivitydd856ef2012-08-27 23:46:17 +03001951static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001952{
Mathias Krause0225fb52012-08-30 01:30:16 +02001953 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001954 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001955 struct desc_ptr dt;
1956 gva_t cs_addr;
1957 gva_t eip_addr;
1958 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001959
1960 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001961 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001962 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001963 if (rc != X86EMUL_CONTINUE)
1964 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001965
Nadav Amit0efb0442015-03-29 16:33:03 +03001966 ctxt->eflags &= ~(X86_EFLAGS_IF | X86_EFLAGS_TF | X86_EFLAGS_AC);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001967
Avi Kivity9dac77f2011-06-01 15:34:25 +03001968 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001969 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001970 if (rc != X86EMUL_CONTINUE)
1971 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001972
Avi Kivity9dac77f2011-06-01 15:34:25 +03001973 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001974 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001975 if (rc != X86EMUL_CONTINUE)
1976 return rc;
1977
Avi Kivity4bff1e862011-04-20 13:37:53 +03001978 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001979
1980 eip_addr = dt.address + (irq << 2);
1981 cs_addr = dt.address + (irq << 2) + 2;
1982
Avi Kivity0f65dd72011-04-20 13:37:53 +03001983 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001984 if (rc != X86EMUL_CONTINUE)
1985 return rc;
1986
Avi Kivity0f65dd72011-04-20 13:37:53 +03001987 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001988 if (rc != X86EMUL_CONTINUE)
1989 return rc;
1990
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001991 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001992 if (rc != X86EMUL_CONTINUE)
1993 return rc;
1994
Avi Kivity9dac77f2011-06-01 15:34:25 +03001995 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001996
1997 return rc;
1998}
1999
Avi Kivitydd856ef2012-08-27 23:46:17 +03002000int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
2001{
2002 int rc;
2003
2004 invalidate_registers(ctxt);
2005 rc = __emulate_int_real(ctxt, irq);
2006 if (rc == X86EMUL_CONTINUE)
2007 writeback_registers(ctxt);
2008 return rc;
2009}
2010
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002011static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002012{
2013 switch(ctxt->mode) {
2014 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03002015 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002016 case X86EMUL_MODE_VM86:
2017 case X86EMUL_MODE_PROT16:
2018 case X86EMUL_MODE_PROT32:
2019 case X86EMUL_MODE_PROT64:
2020 default:
2021 /* Protected mode interrupts unimplemented yet */
2022 return X86EMUL_UNHANDLEABLE;
2023 }
2024}
2025
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002026static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002027{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002028 int rc = X86EMUL_CONTINUE;
2029 unsigned long temp_eip = 0;
2030 unsigned long temp_eflags = 0;
2031 unsigned long cs = 0;
Nadav Amit0efb0442015-03-29 16:33:03 +03002032 unsigned long mask = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
2033 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_TF |
2034 X86_EFLAGS_IF | X86_EFLAGS_DF | X86_EFLAGS_OF |
2035 X86_EFLAGS_IOPL | X86_EFLAGS_NT | X86_EFLAGS_RF |
2036 X86_EFLAGS_AC | X86_EFLAGS_ID |
Wanpeng Li35fd68a2015-04-08 14:08:14 +08002037 X86_EFLAGS_FIXED;
Nadav Amit0efb0442015-03-29 16:33:03 +03002038 unsigned long vm86_mask = X86_EFLAGS_VM | X86_EFLAGS_VIF |
2039 X86_EFLAGS_VIP;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002040
2041 /* TODO: Add stack limit check */
2042
Avi Kivity9dac77f2011-06-01 15:34:25 +03002043 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002044
2045 if (rc != X86EMUL_CONTINUE)
2046 return rc;
2047
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002048 if (temp_eip & ~0xffff)
2049 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002050
Avi Kivity9dac77f2011-06-01 15:34:25 +03002051 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002052
2053 if (rc != X86EMUL_CONTINUE)
2054 return rc;
2055
Avi Kivity9dac77f2011-06-01 15:34:25 +03002056 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002057
2058 if (rc != X86EMUL_CONTINUE)
2059 return rc;
2060
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002061 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002062
2063 if (rc != X86EMUL_CONTINUE)
2064 return rc;
2065
Avi Kivity9dac77f2011-06-01 15:34:25 +03002066 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002067
Avi Kivity9dac77f2011-06-01 15:34:25 +03002068 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002069 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03002070 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002071 ctxt->eflags &= ~0xffff;
2072 ctxt->eflags |= temp_eflags;
2073 }
2074
2075 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
Wanpeng Li35fd68a2015-04-08 14:08:14 +08002076 ctxt->eflags |= X86_EFLAGS_FIXED;
Nadav Amit801806d2015-01-26 09:32:23 +02002077 ctxt->ops->set_nmi_mask(ctxt, false);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002078
2079 return rc;
2080}
2081
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002082static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002083{
2084 switch(ctxt->mode) {
2085 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002086 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002087 case X86EMUL_MODE_VM86:
2088 case X86EMUL_MODE_PROT16:
2089 case X86EMUL_MODE_PROT32:
2090 case X86EMUL_MODE_PROT64:
2091 default:
2092 /* iret from protected mode unimplemented yet */
2093 return X86EMUL_UNHANDLEABLE;
2094 }
2095}
2096
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002097static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2098{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002099 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002100 unsigned short sel, old_sel;
2101 struct desc_struct old_desc, new_desc;
2102 const struct x86_emulate_ops *ops = ctxt->ops;
2103 u8 cpl = ctxt->ops->cpl(ctxt);
2104
2105 /* Assignment of RIP may only fail in 64-bit mode */
2106 if (ctxt->mode == X86EMUL_MODE_PROT64)
2107 ops->get_segment(ctxt, &old_sel, &old_desc, NULL,
2108 VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002109
Avi Kivity9dac77f2011-06-01 15:34:25 +03002110 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002111
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002112 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
2113 X86_TRANSFER_CALL_JMP,
Nadav Amitd1442d82014-09-18 22:39:39 +03002114 &new_desc);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002115 if (rc != X86EMUL_CONTINUE)
2116 return rc;
2117
Nadav Amitd50eaa12014-11-19 17:43:11 +02002118 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03002119 if (rc != X86EMUL_CONTINUE) {
Nadav Amit7e46ddd2014-10-28 00:03:43 +02002120 WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64);
Nadav Amitd1442d82014-09-18 22:39:39 +03002121 /* assigning eip failed; restore the old cs */
2122 ops->set_segment(ctxt, old_sel, &old_desc, 0, VCPU_SREG_CS);
2123 return rc;
2124 }
2125 return rc;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002126}
2127
Nadav Amitf7784042014-09-18 22:39:41 +03002128static int em_jmp_abs(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002129{
Nadav Amitf7784042014-09-18 22:39:41 +03002130 return assign_eip_near(ctxt, ctxt->src.val);
2131}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002132
Nadav Amitf7784042014-09-18 22:39:41 +03002133static int em_call_near_abs(struct x86_emulate_ctxt *ctxt)
2134{
2135 int rc;
2136 long int old_eip;
2137
2138 old_eip = ctxt->_eip;
2139 rc = assign_eip_near(ctxt, ctxt->src.val);
2140 if (rc != X86EMUL_CONTINUE)
2141 return rc;
2142 ctxt->src.val = old_eip;
2143 rc = em_push(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002144 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002145}
2146
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002147static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002148{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002149 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002150
Nadav Amitaaa05f22014-06-02 18:34:10 +03002151 if (ctxt->dst.bytes == 16)
2152 return X86EMUL_UNHANDLEABLE;
2153
Avi Kivitydd856ef2012-08-27 23:46:17 +03002154 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2155 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2156 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2157 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Nadav Amit0efb0442015-03-29 16:33:03 +03002158 ctxt->eflags &= ~X86_EFLAGS_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002159 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002160 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2161 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002162
Nadav Amit0efb0442015-03-29 16:33:03 +03002163 ctxt->eflags |= X86_EFLAGS_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002164 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002165 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002166}
2167
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002168static int em_ret(struct x86_emulate_ctxt *ctxt)
2169{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002170 int rc;
2171 unsigned long eip;
2172
2173 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
2174 if (rc != X86EMUL_CONTINUE)
2175 return rc;
2176
2177 return assign_eip_near(ctxt, eip);
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002178}
2179
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002180static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002181{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002182 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002183 unsigned long eip, cs;
2184 u16 old_cs;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002185 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amitd1442d82014-09-18 22:39:39 +03002186 struct desc_struct old_desc, new_desc;
2187 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002188
Nadav Amitd1442d82014-09-18 22:39:39 +03002189 if (ctxt->mode == X86EMUL_MODE_PROT64)
2190 ops->get_segment(ctxt, &old_cs, &old_desc, NULL,
2191 VCPU_SREG_CS);
2192
2193 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002194 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002195 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002196 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002197 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002198 return rc;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002199 /* Outer-privilege level return is not implemented */
2200 if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2201 return X86EMUL_UNHANDLEABLE;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002202 rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, cpl,
2203 X86_TRANSFER_RET,
Nadav Amitd1442d82014-09-18 22:39:39 +03002204 &new_desc);
2205 if (rc != X86EMUL_CONTINUE)
2206 return rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +02002207 rc = assign_eip_far(ctxt, eip, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03002208 if (rc != X86EMUL_CONTINUE) {
Nadav Amit7e46ddd2014-10-28 00:03:43 +02002209 WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64);
Nadav Amitd1442d82014-09-18 22:39:39 +03002210 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
2211 }
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002212 return rc;
2213}
2214
Bruce Rogers32611072013-09-09 09:40:20 -06002215static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2216{
2217 int rc;
2218
2219 rc = em_ret_far(ctxt);
2220 if (rc != X86EMUL_CONTINUE)
2221 return rc;
2222 rsp_increment(ctxt, ctxt->src.val);
2223 return X86EMUL_CONTINUE;
2224}
2225
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002226static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2227{
2228 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002229 ctxt->dst.orig_val = ctxt->dst.val;
2230 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002231 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002232 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002233 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002234
Nadav Amit0efb0442015-03-29 16:33:03 +03002235 if (ctxt->eflags & X86_EFLAGS_ZF) {
Nadav Amit2fcf5c82015-01-26 09:32:21 +02002236 /* Success: write back to memory; no update of EAX */
2237 ctxt->src.type = OP_NONE;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002238 ctxt->dst.val = ctxt->src.orig_val;
2239 } else {
2240 /* Failure: write the value we saw to EAX. */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02002241 ctxt->src.type = OP_REG;
2242 ctxt->src.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
2243 ctxt->src.val = ctxt->dst.orig_val;
2244 /* Create write-cycle to dest by writing the same value */
Nadav Amit37c564f2014-06-02 18:34:07 +03002245 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002246 }
2247 return X86EMUL_CONTINUE;
2248}
2249
Avi Kivityd4b43252011-09-13 10:45:50 +03002250static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002251{
Avi Kivityd4b43252011-09-13 10:45:50 +03002252 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002253 unsigned short sel;
2254 int rc;
2255
Avi Kivity9dac77f2011-06-01 15:34:25 +03002256 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002257
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002258 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002259 if (rc != X86EMUL_CONTINUE)
2260 return rc;
2261
Avi Kivity9dac77f2011-06-01 15:34:25 +03002262 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002263 return rc;
2264}
2265
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002266static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002267setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002268 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002269{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002270 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002271 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002272 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002273 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002274 cs->type = 0x0b; /* Read, Execute, Accessed */
2275 cs->s = 1;
2276 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002277 cs->p = 1;
2278 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002279 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002280
Gleb Natapov79168fd2010-04-28 19:15:30 +03002281 set_desc_base(ss, 0); /* flat segment */
2282 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002283 ss->g = 1; /* 4kb granularity */
2284 ss->s = 1;
2285 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002286 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002287 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002288 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002289 ss->l = 0;
2290 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002291}
2292
Avi Kivity1a18a692012-02-01 12:23:21 +02002293static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2294{
2295 u32 eax, ebx, ecx, edx;
2296
2297 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002298 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2299 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002300 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2301 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2302}
2303
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002304static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2305{
Mathias Krause0225fb52012-08-30 01:30:16 +02002306 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002307 u32 eax, ebx, ecx, edx;
2308
2309 /*
2310 * syscall should always be enabled in longmode - so only become
2311 * vendor specific (cpuid) if other modes are active...
2312 */
2313 if (ctxt->mode == X86EMUL_MODE_PROT64)
2314 return true;
2315
2316 eax = 0x00000000;
2317 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002318 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2319 /*
2320 * Intel ("GenuineIntel")
2321 * remark: Intel CPUs only support "syscall" in 64bit
2322 * longmode. Also an 64bit guest with a
2323 * 32bit compat-app running will #UD !! While this
2324 * behaviour can be fixed (by emulating) into AMD
2325 * response - CPUs of AMD can't behave like Intel.
2326 */
2327 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2328 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2329 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2330 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002331
Avi Kivity0017f932012-06-07 14:10:16 +03002332 /* AMD ("AuthenticAMD") */
2333 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2334 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2335 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2336 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002337
Avi Kivity0017f932012-06-07 14:10:16 +03002338 /* AMD ("AMDisbetter!") */
2339 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2340 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2341 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2342 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002343
2344 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2345 return false;
2346}
2347
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002348static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002349{
Mathias Krause0225fb52012-08-30 01:30:16 +02002350 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002351 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002352 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002353 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002354 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002355
2356 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002357 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002358 ctxt->mode == X86EMUL_MODE_VM86)
2359 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002360
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002361 if (!(em_syscall_is_enabled(ctxt)))
2362 return emulate_ud(ctxt);
2363
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002364 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002365 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002366
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002367 if (!(efer & EFER_SCE))
2368 return emulate_ud(ctxt);
2369
Avi Kivity717746e2011-04-20 13:37:53 +03002370 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002371 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002372 cs_sel = (u16)(msr_data & 0xfffc);
2373 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002374
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002375 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002376 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002377 cs.l = 1;
2378 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002379 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2380 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002381
Avi Kivitydd856ef2012-08-27 23:46:17 +03002382 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002383 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002384#ifdef CONFIG_X86_64
Nadav Amit6c6cb692014-07-21 14:37:30 +03002385 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002386
Avi Kivity717746e2011-04-20 13:37:53 +03002387 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002388 ctxt->mode == X86EMUL_MODE_PROT64 ?
2389 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002390 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002391
Avi Kivity717746e2011-04-20 13:37:53 +03002392 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Nadav Amit6c6cb692014-07-21 14:37:30 +03002393 ctxt->eflags &= ~msr_data;
Wanpeng Li35fd68a2015-04-08 14:08:14 +08002394 ctxt->eflags |= X86_EFLAGS_FIXED;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002395#endif
2396 } else {
2397 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002398 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002399 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002400
Nadav Amit0efb0442015-03-29 16:33:03 +03002401 ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002402 }
2403
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002404 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002405}
2406
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002407static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002408{
Mathias Krause0225fb52012-08-30 01:30:16 +02002409 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002410 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002411 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002412 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002413 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002414
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002415 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002416 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002417 if (ctxt->mode == X86EMUL_MODE_REAL)
2418 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002419
Avi Kivity1a18a692012-02-01 12:23:21 +02002420 /*
2421 * Not recognized on AMD in compat mode (but is recognized in legacy
2422 * mode).
2423 */
Nadav Amitf3747372015-01-01 23:11:11 +02002424 if ((ctxt->mode != X86EMUL_MODE_PROT64) && (efer & EFER_LMA)
Avi Kivity1a18a692012-02-01 12:23:21 +02002425 && !vendor_intel(ctxt))
2426 return emulate_ud(ctxt);
2427
Nadav Amitb2c9d432014-11-02 11:55:01 +02002428 /* sysenter/sysexit have not been tested in 64bit mode. */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002429 if (ctxt->mode == X86EMUL_MODE_PROT64)
Nadav Amitb2c9d432014-11-02 11:55:01 +02002430 return X86EMUL_UNHANDLEABLE;
Andre Przywara8c604352009-06-18 12:56:01 +02002431
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002432 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002433
Avi Kivity717746e2011-04-20 13:37:53 +03002434 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002435 if ((msr_data & 0xfffc) == 0x0)
2436 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002437
Nadav Amit0efb0442015-03-29 16:33:03 +03002438 ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
Nadav Amitb32a9912015-03-29 16:33:04 +03002439 cs_sel = (u16)msr_data & ~SEGMENT_RPL_MASK;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002440 ss_sel = cs_sel + 8;
Nadav Amitf3747372015-01-01 23:11:11 +02002441 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002442 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002443 cs.l = 1;
2444 }
2445
Avi Kivity1aa36612011-04-27 13:20:30 +03002446 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2447 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002448
Avi Kivity717746e2011-04-20 13:37:53 +03002449 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002450 ctxt->_eip = (efer & EFER_LMA) ? msr_data : (u32)msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002451
Avi Kivity717746e2011-04-20 13:37:53 +03002452 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002453 *reg_write(ctxt, VCPU_REGS_RSP) = (efer & EFER_LMA) ? msr_data :
2454 (u32)msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002455
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002456 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002457}
2458
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002459static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002460{
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 cs, ss;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002463 u64 msr_data, rcx, rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002464 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002465 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002466
Gleb Natapova0044752010-02-10 14:21:31 +02002467 /* inject #GP if in real mode or Virtual 8086 mode */
2468 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002469 ctxt->mode == X86EMUL_MODE_VM86)
2470 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002471
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002472 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002473
Avi Kivity9dac77f2011-06-01 15:34:25 +03002474 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002475 usermode = X86EMUL_MODE_PROT64;
2476 else
2477 usermode = X86EMUL_MODE_PROT32;
2478
Nadav Amit234f3ce2014-09-18 22:39:38 +03002479 rcx = reg_read(ctxt, VCPU_REGS_RCX);
2480 rdx = reg_read(ctxt, VCPU_REGS_RDX);
2481
Andre Przywara4668f052009-06-18 12:56:02 +02002482 cs.dpl = 3;
2483 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002484 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002485 switch (usermode) {
2486 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002487 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002488 if ((msr_data & 0xfffc) == 0x0)
2489 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002490 ss_sel = (u16)(msr_data + 24);
Nadav Amitbf0b6822014-09-18 22:39:45 +03002491 rcx = (u32)rcx;
2492 rdx = (u32)rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002493 break;
2494 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002495 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002496 if (msr_data == 0x0)
2497 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002498 ss_sel = cs_sel + 8;
2499 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002500 cs.l = 1;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002501 if (is_noncanonical_address(rcx) ||
2502 is_noncanonical_address(rdx))
2503 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002504 break;
2505 }
Nadav Amitb32a9912015-03-29 16:33:04 +03002506 cs_sel |= SEGMENT_RPL_MASK;
2507 ss_sel |= SEGMENT_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002508
Avi Kivity1aa36612011-04-27 13:20:30 +03002509 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2510 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002511
Nadav Amit234f3ce2014-09-18 22:39:38 +03002512 ctxt->_eip = rdx;
2513 *reg_write(ctxt, VCPU_REGS_RSP) = rcx;
Andre Przywara4668f052009-06-18 12:56:02 +02002514
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002515 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002516}
2517
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002518static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002519{
2520 int iopl;
2521 if (ctxt->mode == X86EMUL_MODE_REAL)
2522 return false;
2523 if (ctxt->mode == X86EMUL_MODE_VM86)
2524 return true;
Nadav Amit0efb0442015-03-29 16:33:03 +03002525 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002526 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002527}
2528
2529static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002530 u16 port, u16 len)
2531{
Mathias Krause0225fb52012-08-30 01:30:16 +02002532 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002533 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002534 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002535 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002536 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002537 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002538 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002539
Avi Kivity1aa36612011-04-27 13:20:30 +03002540 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002541 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002542 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002543 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002544 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002545 base = get_desc_base(&tr_seg);
2546#ifdef CONFIG_X86_64
2547 base |= ((u64)base3) << 32;
2548#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002549 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002550 if (r != X86EMUL_CONTINUE)
2551 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002552 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002553 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002554 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002555 if (r != X86EMUL_CONTINUE)
2556 return false;
2557 if ((perm >> bit_idx) & mask)
2558 return false;
2559 return true;
2560}
2561
2562static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002563 u16 port, u16 len)
2564{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002565 if (ctxt->perm_ok)
2566 return true;
2567
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002568 if (emulator_bad_iopl(ctxt))
2569 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002570 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002571
2572 ctxt->perm_ok = true;
2573
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002574 return true;
2575}
2576
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002577static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002578 struct tss_segment_16 *tss)
2579{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002580 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002581 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002582 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2583 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2584 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2585 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2586 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2587 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2588 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2589 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002590
Avi Kivity1aa36612011-04-27 13:20:30 +03002591 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2592 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2593 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2594 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2595 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002596}
2597
2598static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002599 struct tss_segment_16 *tss)
2600{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002601 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002602 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002603
Avi Kivity9dac77f2011-06-01 15:34:25 +03002604 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002605 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002606 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2607 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2608 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2609 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2610 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2611 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2612 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2613 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002614
2615 /*
2616 * SDM says that segment selectors are loaded before segment
2617 * descriptors
2618 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002619 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2620 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2621 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2622 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2623 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002624
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002625 cpl = tss->cs & 3;
2626
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002627 /*
Guo Chaofc058682012-06-28 15:19:51 +08002628 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002629 * it is handled in a context of new task
2630 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002631 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002632 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002633 if (ret != X86EMUL_CONTINUE)
2634 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002635 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002636 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002637 if (ret != X86EMUL_CONTINUE)
2638 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002639 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002640 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002641 if (ret != X86EMUL_CONTINUE)
2642 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002643 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002644 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002645 if (ret != X86EMUL_CONTINUE)
2646 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002647 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002648 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002649 if (ret != X86EMUL_CONTINUE)
2650 return ret;
2651
2652 return X86EMUL_CONTINUE;
2653}
2654
2655static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002656 u16 tss_selector, u16 old_tss_sel,
2657 ulong old_tss_base, struct desc_struct *new_desc)
2658{
Mathias Krause0225fb52012-08-30 01:30:16 +02002659 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002660 struct tss_segment_16 tss_seg;
2661 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002662 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002663
Avi Kivity0f65dd72011-04-20 13:37:53 +03002664 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002665 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002666 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002667 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002668
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002669 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002670
Avi Kivity0f65dd72011-04-20 13:37:53 +03002671 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002672 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002673 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002674 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002675
Avi Kivity0f65dd72011-04-20 13:37:53 +03002676 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002677 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002678 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002679 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002680
2681 if (old_tss_sel != 0xffff) {
2682 tss_seg.prev_task_link = old_tss_sel;
2683
Avi Kivity0f65dd72011-04-20 13:37:53 +03002684 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002685 &tss_seg.prev_task_link,
2686 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002687 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002688 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002689 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002690 }
2691
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002692 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002693}
2694
2695static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002696 struct tss_segment_32 *tss)
2697{
Nadav Amit5c7411e2014-04-07 18:37:47 +03002698 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002699 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002700 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002701 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2702 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2703 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2704 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2705 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2706 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2707 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2708 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002709
Avi Kivity1aa36612011-04-27 13:20:30 +03002710 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2711 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2712 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2713 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2714 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2715 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002716}
2717
2718static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002719 struct tss_segment_32 *tss)
2720{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002721 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002722 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002723
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002724 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002725 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002726 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002727 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002728
2729 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002730 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2731 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2732 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2733 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2734 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2735 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2736 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2737 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002738
2739 /*
2740 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002741 * descriptors. This is important because CPL checks will
2742 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002743 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002744 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2745 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2746 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2747 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2748 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2749 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2750 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002751
2752 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002753 * If we're switching between Protected Mode and VM86, we need to make
2754 * sure to update the mode before loading the segment descriptors so
2755 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01002756 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002757 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002758 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002759 cpl = 3;
2760 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002761 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002762 cpl = tss->cs & 3;
2763 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01002764
2765 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002766 * Now load segment descriptors. If fault happenes at this stage
2767 * it is handled in a context of new task
2768 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002769 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002770 cpl, X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002771 if (ret != X86EMUL_CONTINUE)
2772 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002773 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002774 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002775 if (ret != X86EMUL_CONTINUE)
2776 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002777 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002778 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002779 if (ret != X86EMUL_CONTINUE)
2780 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002781 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002782 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002783 if (ret != X86EMUL_CONTINUE)
2784 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002785 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002786 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002787 if (ret != X86EMUL_CONTINUE)
2788 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002789 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002790 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002791 if (ret != X86EMUL_CONTINUE)
2792 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002793 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002794 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002795
Eugene Korenevsky2f729b12015-03-29 01:27:17 +03002796 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002797}
2798
2799static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002800 u16 tss_selector, u16 old_tss_sel,
2801 ulong old_tss_base, struct desc_struct *new_desc)
2802{
Mathias Krause0225fb52012-08-30 01:30:16 +02002803 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002804 struct tss_segment_32 tss_seg;
2805 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002806 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03002807 u32 eip_offset = offsetof(struct tss_segment_32, eip);
2808 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002809
Avi Kivity0f65dd72011-04-20 13:37:53 +03002810 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002811 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002812 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002813 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002814
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002815 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002816
Nadav Amit5c7411e2014-04-07 18:37:47 +03002817 /* Only GP registers and segment selectors are saved */
2818 ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
2819 ldt_sel_offset - eip_offset, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002820 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002821 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002822
Avi Kivity0f65dd72011-04-20 13:37:53 +03002823 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002824 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002825 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002826 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002827
2828 if (old_tss_sel != 0xffff) {
2829 tss_seg.prev_task_link = old_tss_sel;
2830
Avi Kivity0f65dd72011-04-20 13:37:53 +03002831 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002832 &tss_seg.prev_task_link,
2833 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002834 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002835 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002836 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002837 }
2838
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002839 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002840}
2841
2842static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002843 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002844 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002845{
Mathias Krause0225fb52012-08-30 01:30:16 +02002846 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002847 struct desc_struct curr_tss_desc, next_tss_desc;
2848 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002849 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002850 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002851 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002852 u32 desc_limit;
Nadav Amit3db176d2015-04-19 21:12:59 +03002853 ulong desc_addr, dr7;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002854
2855 /* FIXME: old_tss_base == ~0 ? */
2856
Avi Kivitye9194642012-06-13 16:29:39 +03002857 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002858 if (ret != X86EMUL_CONTINUE)
2859 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002860 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002861 if (ret != X86EMUL_CONTINUE)
2862 return ret;
2863
2864 /* FIXME: check that next_tss_desc is tss */
2865
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002866 /*
2867 * Check privileges. The three cases are task switch caused by...
2868 *
2869 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2870 * 2. Exception/IRQ/iret: No check is performed
Nadav Amit2c2ca2d2014-11-02 11:54:57 +02002871 * 3. jmp/call to TSS/task-gate: No check is performed since the
2872 * hardware checks it before exiting.
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002873 */
2874 if (reason == TASK_SWITCH_GATE) {
2875 if (idt_index != -1) {
2876 /* Software interrupts */
2877 struct desc_struct task_gate_desc;
2878 int dpl;
2879
2880 ret = read_interrupt_descriptor(ctxt, idt_index,
2881 &task_gate_desc);
2882 if (ret != X86EMUL_CONTINUE)
2883 return ret;
2884
2885 dpl = task_gate_desc.dpl;
2886 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2887 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2888 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002889 }
2890
Gleb Natapovceffb452010-03-18 15:20:19 +02002891 desc_limit = desc_limit_scaled(&next_tss_desc);
2892 if (!next_tss_desc.p ||
2893 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2894 desc_limit < 0x2b)) {
Paolo Bonzini592f0852014-08-20 10:05:08 +02002895 return emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002896 }
2897
2898 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2899 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002900 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002901 }
2902
2903 if (reason == TASK_SWITCH_IRET)
2904 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2905
2906 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002907 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002908 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2909 old_tss_sel = 0xffff;
2910
2911 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002912 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002913 old_tss_base, &next_tss_desc);
2914 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002915 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002916 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002917 if (ret != X86EMUL_CONTINUE)
2918 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002919
2920 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2921 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2922
2923 if (reason != TASK_SWITCH_IRET) {
2924 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002925 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002926 }
2927
Avi Kivity717746e2011-04-20 13:37:53 +03002928 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002929 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002930
Jan Kiszkae269fb22010-04-14 15:51:09 +02002931 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002932 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2933 ctxt->lock_prefix = 0;
2934 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002935 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002936 }
2937
Nadav Amit3db176d2015-04-19 21:12:59 +03002938 ops->get_dr(ctxt, 7, &dr7);
2939 ops->set_dr(ctxt, 7, dr7 & ~(DR_LOCAL_ENABLE_MASK | DR_LOCAL_SLOWDOWN));
2940
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002941 return ret;
2942}
2943
2944int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002945 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002946 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002947{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002948 int rc;
2949
Avi Kivitydd856ef2012-08-27 23:46:17 +03002950 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002951 ctxt->_eip = ctxt->eip;
2952 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002953
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002954 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002955 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002956
Avi Kivitydd856ef2012-08-27 23:46:17 +03002957 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002958 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002959 writeback_registers(ctxt);
2960 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002961
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002962 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002963}
2964
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002965static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2966 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002967{
Nadav Amit0efb0442015-03-29 16:33:03 +03002968 int df = (ctxt->eflags & X86_EFLAGS_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002969
Paolo Bonzini01485a22014-11-19 18:25:08 +01002970 register_address_increment(ctxt, reg, df * op->bytes);
2971 op->addr.mem.ea = register_address(ctxt, reg);
Gleb Natapova682e352010-03-18 15:20:21 +02002972}
2973
Avi Kivity7af04fc2010-08-18 14:16:35 +03002974static int em_das(struct x86_emulate_ctxt *ctxt)
2975{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002976 u8 al, old_al;
2977 bool af, cf, old_cf;
2978
2979 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002980 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002981
2982 old_al = al;
2983 old_cf = cf;
2984 cf = false;
2985 af = ctxt->eflags & X86_EFLAGS_AF;
2986 if ((al & 0x0f) > 9 || af) {
2987 al -= 6;
2988 cf = old_cf | (al >= 250);
2989 af = true;
2990 } else {
2991 af = false;
2992 }
2993 if (old_al > 0x99 || old_cf) {
2994 al -= 0x60;
2995 cf = true;
2996 }
2997
Avi Kivity9dac77f2011-06-01 15:34:25 +03002998 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002999 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003000 ctxt->src.type = OP_IMM;
3001 ctxt->src.val = 0;
3002 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02003003 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03003004 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
3005 if (cf)
3006 ctxt->eflags |= X86_EFLAGS_CF;
3007 if (af)
3008 ctxt->eflags |= X86_EFLAGS_AF;
3009 return X86EMUL_CONTINUE;
3010}
3011
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003012static int em_aam(struct x86_emulate_ctxt *ctxt)
3013{
3014 u8 al, ah;
3015
3016 if (ctxt->src.val == 0)
3017 return emulate_de(ctxt);
3018
3019 al = ctxt->dst.val & 0xff;
3020 ah = al / ctxt->src.val;
3021 al %= ctxt->src.val;
3022
3023 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
3024
3025 /* Set PF, ZF, SF */
3026 ctxt->src.type = OP_IMM;
3027 ctxt->src.val = 0;
3028 ctxt->src.bytes = 1;
3029 fastop(ctxt, em_or);
3030
3031 return X86EMUL_CONTINUE;
3032}
3033
Gleb Natapov7f662272012-12-10 11:42:30 +02003034static int em_aad(struct x86_emulate_ctxt *ctxt)
3035{
3036 u8 al = ctxt->dst.val & 0xff;
3037 u8 ah = (ctxt->dst.val >> 8) & 0xff;
3038
3039 al = (al + (ah * ctxt->src.val)) & 0xff;
3040
3041 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
3042
Gleb Natapovf583c292013-02-13 17:50:39 +02003043 /* Set PF, ZF, SF */
3044 ctxt->src.type = OP_IMM;
3045 ctxt->src.val = 0;
3046 ctxt->src.bytes = 1;
3047 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02003048
3049 return X86EMUL_CONTINUE;
3050}
3051
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003052static int em_call(struct x86_emulate_ctxt *ctxt)
3053{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003054 int rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003055 long rel = ctxt->src.val;
3056
3057 ctxt->src.val = (unsigned long)ctxt->_eip;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003058 rc = jmp_rel(ctxt, rel);
3059 if (rc != X86EMUL_CONTINUE)
3060 return rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003061 return em_push(ctxt);
3062}
3063
Avi Kivity0ef753b2010-08-18 14:51:45 +03003064static int em_call_far(struct x86_emulate_ctxt *ctxt)
3065{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003066 u16 sel, old_cs;
3067 ulong old_eip;
3068 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03003069 struct desc_struct old_desc, new_desc;
3070 const struct x86_emulate_ops *ops = ctxt->ops;
3071 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amit82268082015-01-26 09:32:27 +02003072 enum x86emul_mode prev_mode = ctxt->mode;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003073
Avi Kivity9dac77f2011-06-01 15:34:25 +03003074 old_eip = ctxt->_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003075 ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003076
Avi Kivity9dac77f2011-06-01 15:34:25 +03003077 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003078 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
3079 X86_TRANSFER_CALL_JMP, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003080 if (rc != X86EMUL_CONTINUE)
Nadav Amit80976db2014-12-25 02:52:20 +02003081 return rc;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003082
Nadav Amitd50eaa12014-11-19 17:43:11 +02003083 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003084 if (rc != X86EMUL_CONTINUE)
3085 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003086
Avi Kivity9dac77f2011-06-01 15:34:25 +03003087 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003088 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003089 if (rc != X86EMUL_CONTINUE)
Nadav Amitd1442d82014-09-18 22:39:39 +03003090 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003091
Avi Kivity9dac77f2011-06-01 15:34:25 +03003092 ctxt->src.val = old_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003093 rc = em_push(ctxt);
3094 /* If we failed, we tainted the memory, but the very least we should
3095 restore cs */
Nadav Amit82268082015-01-26 09:32:27 +02003096 if (rc != X86EMUL_CONTINUE) {
3097 pr_warn_once("faulting far call emulation tainted memory\n");
Nadav Amitd1442d82014-09-18 22:39:39 +03003098 goto fail;
Nadav Amit82268082015-01-26 09:32:27 +02003099 }
Nadav Amitd1442d82014-09-18 22:39:39 +03003100 return rc;
3101fail:
3102 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
Nadav Amit82268082015-01-26 09:32:27 +02003103 ctxt->mode = prev_mode;
Nadav Amitd1442d82014-09-18 22:39:39 +03003104 return rc;
3105
Avi Kivity0ef753b2010-08-18 14:51:45 +03003106}
3107
Avi Kivity40ece7c2010-08-18 15:12:09 +03003108static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3109{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003110 int rc;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003111 unsigned long eip;
Avi Kivity40ece7c2010-08-18 15:12:09 +03003112
Nadav Amit234f3ce2014-09-18 22:39:38 +03003113 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
3114 if (rc != X86EMUL_CONTINUE)
3115 return rc;
3116 rc = assign_eip_near(ctxt, eip);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003117 if (rc != X86EMUL_CONTINUE)
3118 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003119 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003120 return X86EMUL_CONTINUE;
3121}
3122
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003123static int em_xchg(struct x86_emulate_ctxt *ctxt)
3124{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003125 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003126 ctxt->src.val = ctxt->dst.val;
3127 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003128
3129 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003130 ctxt->dst.val = ctxt->src.orig_val;
3131 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003132 return X86EMUL_CONTINUE;
3133}
3134
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003135static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3136{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003137 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003138 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003139}
3140
Avi Kivity61429142010-08-19 15:13:00 +03003141static int em_cwd(struct x86_emulate_ctxt *ctxt)
3142{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003143 ctxt->dst.type = OP_REG;
3144 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003145 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003146 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003147
3148 return X86EMUL_CONTINUE;
3149}
3150
Avi Kivity48bb5d32010-08-18 18:54:34 +03003151static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3152{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003153 u64 tsc = 0;
3154
Avi Kivity717746e2011-04-20 13:37:53 +03003155 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003156 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3157 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003158 return X86EMUL_CONTINUE;
3159}
3160
Avi Kivity222d21a2011-11-10 14:57:30 +02003161static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3162{
3163 u64 pmc;
3164
Avi Kivitydd856ef2012-08-27 23:46:17 +03003165 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003166 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003167 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3168 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003169 return X86EMUL_CONTINUE;
3170}
3171
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003172static int em_mov(struct x86_emulate_ctxt *ctxt)
3173{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01003174 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003175 return X86EMUL_CONTINUE;
3176}
3177
Borislav Petkov84cffe42013-10-29 12:54:56 +01003178#define FFL(x) bit(X86_FEATURE_##x)
3179
3180static int em_movbe(struct x86_emulate_ctxt *ctxt)
3181{
3182 u32 ebx, ecx, edx, eax = 1;
3183 u16 tmp;
3184
3185 /*
3186 * Check MOVBE is set in the guest-visible CPUID leaf.
3187 */
3188 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
3189 if (!(ecx & FFL(MOVBE)))
3190 return emulate_ud(ctxt);
3191
3192 switch (ctxt->op_bytes) {
3193 case 2:
3194 /*
3195 * From MOVBE definition: "...When the operand size is 16 bits,
3196 * the upper word of the destination register remains unchanged
3197 * ..."
3198 *
3199 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3200 * rules so we have to do the operation almost per hand.
3201 */
3202 tmp = (u16)ctxt->src.val;
3203 ctxt->dst.val &= ~0xffffUL;
3204 ctxt->dst.val |= (unsigned long)swab16(tmp);
3205 break;
3206 case 4:
3207 ctxt->dst.val = swab32((u32)ctxt->src.val);
3208 break;
3209 case 8:
3210 ctxt->dst.val = swab64(ctxt->src.val);
3211 break;
3212 default:
Paolo Bonzini592f0852014-08-20 10:05:08 +02003213 BUG();
Borislav Petkov84cffe42013-10-29 12:54:56 +01003214 }
3215 return X86EMUL_CONTINUE;
3216}
3217
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003218static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3219{
3220 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3221 return emulate_gp(ctxt, 0);
3222
3223 /* Disable writeback. */
3224 ctxt->dst.type = OP_NONE;
3225 return X86EMUL_CONTINUE;
3226}
3227
3228static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3229{
3230 unsigned long val;
3231
3232 if (ctxt->mode == X86EMUL_MODE_PROT64)
3233 val = ctxt->src.val & ~0ULL;
3234 else
3235 val = ctxt->src.val & ~0U;
3236
3237 /* #UD condition is already handled. */
3238 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3239 return emulate_gp(ctxt, 0);
3240
3241 /* Disable writeback. */
3242 ctxt->dst.type = OP_NONE;
3243 return X86EMUL_CONTINUE;
3244}
3245
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003246static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3247{
3248 u64 msr_data;
3249
Avi Kivitydd856ef2012-08-27 23:46:17 +03003250 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3251 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3252 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003253 return emulate_gp(ctxt, 0);
3254
3255 return X86EMUL_CONTINUE;
3256}
3257
3258static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3259{
3260 u64 msr_data;
3261
Avi Kivitydd856ef2012-08-27 23:46:17 +03003262 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003263 return emulate_gp(ctxt, 0);
3264
Avi Kivitydd856ef2012-08-27 23:46:17 +03003265 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3266 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003267 return X86EMUL_CONTINUE;
3268}
3269
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003270static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3271{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003272 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003273 return emulate_ud(ctxt);
3274
Avi Kivity9dac77f2011-06-01 15:34:25 +03003275 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Nadav Amitb5bbf102014-11-02 11:54:46 +02003276 if (ctxt->dst.bytes == 4 && ctxt->dst.type == OP_MEM)
3277 ctxt->dst.bytes = 2;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003278 return X86EMUL_CONTINUE;
3279}
3280
3281static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3282{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003283 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003284
Avi Kivity9dac77f2011-06-01 15:34:25 +03003285 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003286 return emulate_ud(ctxt);
3287
Avi Kivity9dac77f2011-06-01 15:34:25 +03003288 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003289 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3290
3291 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003292 ctxt->dst.type = OP_NONE;
3293 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003294}
3295
Avi Kivitya14e5792012-06-13 12:28:33 +03003296static int em_lldt(struct x86_emulate_ctxt *ctxt)
3297{
3298 u16 sel = ctxt->src.val;
3299
3300 /* Disable writeback. */
3301 ctxt->dst.type = OP_NONE;
3302 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3303}
3304
Avi Kivity80890002012-06-13 16:33:29 +03003305static int em_ltr(struct x86_emulate_ctxt *ctxt)
3306{
3307 u16 sel = ctxt->src.val;
3308
3309 /* Disable writeback. */
3310 ctxt->dst.type = OP_NONE;
3311 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3312}
3313
Avi Kivity38503912011-03-31 18:48:09 +02003314static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3315{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003316 int rc;
3317 ulong linear;
3318
Avi Kivity9dac77f2011-06-01 15:34:25 +03003319 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003320 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003321 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003322 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003323 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003324 return X86EMUL_CONTINUE;
3325}
3326
Avi Kivity2d04a052011-04-20 15:32:49 +03003327static int em_clts(struct x86_emulate_ctxt *ctxt)
3328{
3329 ulong cr0;
3330
3331 cr0 = ctxt->ops->get_cr(ctxt, 0);
3332 cr0 &= ~X86_CR0_TS;
3333 ctxt->ops->set_cr(ctxt, 0, cr0);
3334 return X86EMUL_CONTINUE;
3335}
3336
Jan Kiszkab34a8052015-03-09 20:27:43 +01003337static int em_hypercall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003338{
Nadav Amit0f54a322014-08-29 11:26:55 +03003339 int rc = ctxt->ops->fix_hypercall(ctxt);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003340
Avi Kivity26d05cc2011-04-21 12:07:59 +03003341 if (rc != X86EMUL_CONTINUE)
3342 return rc;
3343
3344 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003345 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003346 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003347 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003348 return X86EMUL_CONTINUE;
3349}
3350
Avi Kivity96051572012-06-10 17:21:18 +03003351static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3352 void (*get)(struct x86_emulate_ctxt *ctxt,
3353 struct desc_ptr *ptr))
3354{
3355 struct desc_ptr desc_ptr;
3356
3357 if (ctxt->mode == X86EMUL_MODE_PROT64)
3358 ctxt->op_bytes = 8;
3359 get(ctxt, &desc_ptr);
3360 if (ctxt->op_bytes == 2) {
3361 ctxt->op_bytes = 4;
3362 desc_ptr.address &= 0x00ffffff;
3363 }
3364 /* Disable writeback. */
3365 ctxt->dst.type = OP_NONE;
3366 return segmented_write(ctxt, ctxt->dst.addr.mem,
3367 &desc_ptr, 2 + ctxt->op_bytes);
3368}
3369
3370static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3371{
3372 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3373}
3374
3375static int em_sidt(struct x86_emulate_ctxt *ctxt)
3376{
3377 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3378}
3379
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003380static int em_lgdt_lidt(struct x86_emulate_ctxt *ctxt, bool lgdt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003381{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003382 struct desc_ptr desc_ptr;
3383 int rc;
3384
Avi Kivity510425f2012-06-07 17:04:36 +03003385 if (ctxt->mode == X86EMUL_MODE_PROT64)
3386 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003387 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003388 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003389 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003390 if (rc != X86EMUL_CONTINUE)
3391 return rc;
Nadav Amit9a9abf62014-11-02 11:54:56 +02003392 if (ctxt->mode == X86EMUL_MODE_PROT64 &&
3393 is_noncanonical_address(desc_ptr.address))
3394 return emulate_gp(ctxt, 0);
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003395 if (lgdt)
3396 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3397 else
3398 ctxt->ops->set_idt(ctxt, &desc_ptr);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003399 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003400 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003401 return X86EMUL_CONTINUE;
3402}
3403
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003404static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3405{
3406 return em_lgdt_lidt(ctxt, true);
3407}
3408
Avi Kivity26d05cc2011-04-21 12:07:59 +03003409static int em_lidt(struct x86_emulate_ctxt *ctxt)
3410{
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003411 return em_lgdt_lidt(ctxt, false);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003412}
3413
3414static int em_smsw(struct x86_emulate_ctxt *ctxt)
3415{
Nadav Amit32e94d02014-06-02 18:34:11 +03003416 if (ctxt->dst.type == OP_MEM)
3417 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003418 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003419 return X86EMUL_CONTINUE;
3420}
3421
3422static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3423{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003424 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003425 | (ctxt->src.val & 0x0f));
3426 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003427 return X86EMUL_CONTINUE;
3428}
3429
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003430static int em_loop(struct x86_emulate_ctxt *ctxt)
3431{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003432 int rc = X86EMUL_CONTINUE;
3433
Paolo Bonzini01485a22014-11-19 18:25:08 +01003434 register_address_increment(ctxt, VCPU_REGS_RCX, -1);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003435 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003436 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
Nadav Amit234f3ce2014-09-18 22:39:38 +03003437 rc = jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003438
Nadav Amit234f3ce2014-09-18 22:39:38 +03003439 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003440}
3441
3442static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3443{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003444 int rc = X86EMUL_CONTINUE;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003445
Nadav Amit234f3ce2014-09-18 22:39:38 +03003446 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
3447 rc = jmp_rel(ctxt, ctxt->src.val);
3448
3449 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003450}
3451
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003452static int em_in(struct x86_emulate_ctxt *ctxt)
3453{
3454 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3455 &ctxt->dst.val))
3456 return X86EMUL_IO_NEEDED;
3457
3458 return X86EMUL_CONTINUE;
3459}
3460
3461static int em_out(struct x86_emulate_ctxt *ctxt)
3462{
3463 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3464 &ctxt->src.val, 1);
3465 /* Disable writeback. */
3466 ctxt->dst.type = OP_NONE;
3467 return X86EMUL_CONTINUE;
3468}
3469
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003470static int em_cli(struct x86_emulate_ctxt *ctxt)
3471{
3472 if (emulator_bad_iopl(ctxt))
3473 return emulate_gp(ctxt, 0);
3474
3475 ctxt->eflags &= ~X86_EFLAGS_IF;
3476 return X86EMUL_CONTINUE;
3477}
3478
3479static int em_sti(struct x86_emulate_ctxt *ctxt)
3480{
3481 if (emulator_bad_iopl(ctxt))
3482 return emulate_gp(ctxt, 0);
3483
3484 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3485 ctxt->eflags |= X86_EFLAGS_IF;
3486 return X86EMUL_CONTINUE;
3487}
3488
Avi Kivity6d6eede2012-06-07 14:11:36 +03003489static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3490{
3491 u32 eax, ebx, ecx, edx;
3492
Avi Kivitydd856ef2012-08-27 23:46:17 +03003493 eax = reg_read(ctxt, VCPU_REGS_RAX);
3494 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003495 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003496 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3497 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3498 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3499 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003500 return X86EMUL_CONTINUE;
3501}
3502
Paolo Bonzini98f73632013-10-31 11:19:42 +01003503static int em_sahf(struct x86_emulate_ctxt *ctxt)
3504{
3505 u32 flags;
3506
Nadav Amit0efb0442015-03-29 16:33:03 +03003507 flags = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
3508 X86_EFLAGS_SF;
Paolo Bonzini98f73632013-10-31 11:19:42 +01003509 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3510
3511 ctxt->eflags &= ~0xffUL;
3512 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3513 return X86EMUL_CONTINUE;
3514}
3515
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003516static int em_lahf(struct x86_emulate_ctxt *ctxt)
3517{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003518 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3519 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003520 return X86EMUL_CONTINUE;
3521}
3522
Avi Kivity92998362012-06-13 12:25:06 +03003523static int em_bswap(struct x86_emulate_ctxt *ctxt)
3524{
3525 switch (ctxt->op_bytes) {
3526#ifdef CONFIG_X86_64
3527 case 8:
3528 asm("bswap %0" : "+r"(ctxt->dst.val));
3529 break;
3530#endif
3531 default:
3532 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3533 break;
3534 }
3535 return X86EMUL_CONTINUE;
3536}
3537
Nadav Amit13e457e2014-10-13 13:04:13 +03003538static int em_clflush(struct x86_emulate_ctxt *ctxt)
3539{
3540 /* emulating clflush regardless of cpuid */
3541 return X86EMUL_CONTINUE;
3542}
3543
Nadav Amit2276b512015-01-26 09:32:24 +02003544static int em_movsxd(struct x86_emulate_ctxt *ctxt)
3545{
3546 ctxt->dst.val = (s32) ctxt->src.val;
3547 return X86EMUL_CONTINUE;
3548}
3549
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003550static bool valid_cr(int nr)
3551{
3552 switch (nr) {
3553 case 0:
3554 case 2 ... 4:
3555 case 8:
3556 return true;
3557 default:
3558 return false;
3559 }
3560}
3561
3562static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3563{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003564 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003565 return emulate_ud(ctxt);
3566
3567 return X86EMUL_CONTINUE;
3568}
3569
3570static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3571{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003572 u64 new_val = ctxt->src.val64;
3573 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003574 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003575
3576 static u64 cr_reserved_bits[] = {
3577 0xffffffff00000000ULL,
3578 0, 0, 0, /* CR3 checked later */
3579 CR4_RESERVED_BITS,
3580 0, 0, 0,
3581 CR8_RESERVED_BITS,
3582 };
3583
3584 if (!valid_cr(cr))
3585 return emulate_ud(ctxt);
3586
3587 if (new_val & cr_reserved_bits[cr])
3588 return emulate_gp(ctxt, 0);
3589
3590 switch (cr) {
3591 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003592 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003593 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3594 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3595 return emulate_gp(ctxt, 0);
3596
Avi Kivity717746e2011-04-20 13:37:53 +03003597 cr4 = ctxt->ops->get_cr(ctxt, 4);
3598 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003599
3600 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3601 !(cr4 & X86_CR4_PAE))
3602 return emulate_gp(ctxt, 0);
3603
3604 break;
3605 }
3606 case 3: {
3607 u64 rsvd = 0;
3608
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003609 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3610 if (efer & EFER_LMA)
Nadav Amit9d88fca2014-11-02 11:54:52 +02003611 rsvd = CR3_L_MODE_RESERVED_BITS & ~CR3_PCID_INVD;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003612
3613 if (new_val & rsvd)
3614 return emulate_gp(ctxt, 0);
3615
3616 break;
3617 }
3618 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003619 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003620
3621 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3622 return emulate_gp(ctxt, 0);
3623
3624 break;
3625 }
3626 }
3627
3628 return X86EMUL_CONTINUE;
3629}
3630
Joerg Roedel3b88e412011-04-04 12:39:29 +02003631static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3632{
3633 unsigned long dr7;
3634
Avi Kivity717746e2011-04-20 13:37:53 +03003635 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003636
3637 /* Check if DR7.Global_Enable is set */
3638 return dr7 & (1 << 13);
3639}
3640
3641static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3642{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003643 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003644 u64 cr4;
3645
3646 if (dr > 7)
3647 return emulate_ud(ctxt);
3648
Avi Kivity717746e2011-04-20 13:37:53 +03003649 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003650 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3651 return emulate_ud(ctxt);
3652
Nadav Amit6d2a0522014-11-02 11:54:43 +02003653 if (check_dr7_gd(ctxt)) {
3654 ulong dr6;
3655
3656 ctxt->ops->get_dr(ctxt, 6, &dr6);
3657 dr6 &= ~15;
3658 dr6 |= DR6_BD | DR6_RTM;
3659 ctxt->ops->set_dr(ctxt, 6, dr6);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003660 return emulate_db(ctxt);
Nadav Amit6d2a0522014-11-02 11:54:43 +02003661 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02003662
3663 return X86EMUL_CONTINUE;
3664}
3665
3666static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3667{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003668 u64 new_val = ctxt->src.val64;
3669 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003670
3671 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3672 return emulate_gp(ctxt, 0);
3673
3674 return check_dr_read(ctxt);
3675}
3676
Joerg Roedel01de8b02011-04-04 12:39:31 +02003677static int check_svme(struct x86_emulate_ctxt *ctxt)
3678{
3679 u64 efer;
3680
Avi Kivity717746e2011-04-20 13:37:53 +03003681 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003682
3683 if (!(efer & EFER_SVME))
3684 return emulate_ud(ctxt);
3685
3686 return X86EMUL_CONTINUE;
3687}
3688
3689static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3690{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003691 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003692
3693 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003694 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003695 return emulate_gp(ctxt, 0);
3696
3697 return check_svme(ctxt);
3698}
3699
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003700static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3701{
Avi Kivity717746e2011-04-20 13:37:53 +03003702 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003703
Avi Kivity717746e2011-04-20 13:37:53 +03003704 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003705 return emulate_ud(ctxt);
3706
3707 return X86EMUL_CONTINUE;
3708}
3709
Joerg Roedel80612522011-04-04 12:39:33 +02003710static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3711{
Avi Kivity717746e2011-04-20 13:37:53 +03003712 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003713 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003714
Avi Kivity717746e2011-04-20 13:37:53 +03003715 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03003716 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02003717 return emulate_gp(ctxt, 0);
3718
3719 return X86EMUL_CONTINUE;
3720}
3721
Joerg Roedelf6511932011-04-04 12:39:35 +02003722static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3723{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003724 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3725 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003726 return emulate_gp(ctxt, 0);
3727
3728 return X86EMUL_CONTINUE;
3729}
3730
3731static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3732{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003733 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3734 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003735 return emulate_gp(ctxt, 0);
3736
3737 return X86EMUL_CONTINUE;
3738}
3739
Avi Kivity73fba5f2010-07-29 15:11:53 +03003740#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003741#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
3742#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
3743 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003744#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003745#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003746#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3747#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Nadav Amit39f062f2014-11-26 15:47:18 +02003748#define ID(_f, _i) { .flags = ((_f) | InstrDual | ModRM), .u.idual = (_i) }
Nadav Amit2276b512015-01-26 09:32:24 +02003749#define MD(_f, _m) { .flags = ((_f) | ModeDual), .u.mdual = (_m) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003750#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003751#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003752#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003753#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003754 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003755#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003756 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
3757 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003758#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003759
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003760#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003761#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003762#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003763#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003764#define I2bvIP(_f, _e, _i, _p) \
3765 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003766
Avi Kivityfb864fb2013-01-04 16:18:54 +02003767#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3768 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3769 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003770
Nadav Amit0f54a322014-08-29 11:26:55 +03003771static const struct opcode group7_rm0[] = {
3772 N,
Jan Kiszkab34a8052015-03-09 20:27:43 +01003773 I(SrcNone | Priv | EmulateOnUD, em_hypercall),
Nadav Amit0f54a322014-08-29 11:26:55 +03003774 N, N, N, N, N, N,
3775};
3776
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003777static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003778 DI(SrcNone | Priv, monitor),
3779 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003780 N, N, N, N, N, N,
3781};
3782
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003783static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003784 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Jan Kiszkab34a8052015-03-09 20:27:43 +01003785 II(SrcNone | Prot | EmulateOnUD, em_hypercall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003786 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3787 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3788 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3789 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3790 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3791 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003792};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003793
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003794static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003795 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003796 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003797 N, N, N, N, N, N,
3798};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003799
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003800static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003801 F(Lock, em_add),
3802 F(Lock | PageTable, em_or),
3803 F(Lock, em_adc),
3804 F(Lock, em_sbb),
3805 F(Lock | PageTable, em_and),
3806 F(Lock, em_sub),
3807 F(Lock, em_xor),
3808 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003809};
3810
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003811static const struct opcode group1A[] = {
Nadav Amitab708092014-12-25 02:52:21 +02003812 I(DstMem | SrcNone | Mov | Stack | IncSP, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003813};
3814
Avi Kivity007a3b52013-01-19 19:51:51 +02003815static const struct opcode group2[] = {
3816 F(DstMem | ModRM, em_rol),
3817 F(DstMem | ModRM, em_ror),
3818 F(DstMem | ModRM, em_rcl),
3819 F(DstMem | ModRM, em_rcr),
3820 F(DstMem | ModRM, em_shl),
3821 F(DstMem | ModRM, em_shr),
3822 F(DstMem | ModRM, em_shl),
3823 F(DstMem | ModRM, em_sar),
3824};
3825
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003826static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003827 F(DstMem | SrcImm | NoWrite, em_test),
3828 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003829 F(DstMem | SrcNone | Lock, em_not),
3830 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02003831 F(DstXacc | Src2Mem, em_mul_ex),
3832 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02003833 F(DstXacc | Src2Mem, em_div_ex),
3834 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003835};
3836
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003837static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003838 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3839 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003840 N, N, N, N, N, N,
3841};
3842
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003843static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003844 F(DstMem | SrcNone | Lock, em_inc),
3845 F(DstMem | SrcNone | Lock, em_dec),
Nadav Amit58b70752014-10-24 11:35:09 +03003846 I(SrcMem | NearBranch, em_call_near_abs),
Nadav Amitacac6f892015-05-03 20:22:57 +03003847 I(SrcMemFAddr | ImplicitOps, em_call_far),
Nadav Amit58b70752014-10-24 11:35:09 +03003848 I(SrcMem | NearBranch, em_jmp_abs),
Nadav Amitf7784042014-09-18 22:39:41 +03003849 I(SrcMemFAddr | ImplicitOps, em_jmp_far),
3850 I(SrcMem | Stack, em_push), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003851};
3852
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003853static const struct opcode group6[] = {
Nadav Amit63ea0a42015-01-08 11:59:03 +01003854 DI(Prot | DstMem, sldt),
3855 DI(Prot | DstMem, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003856 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003857 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003858 N, N, N, N,
3859};
3860
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003861static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03003862 II(Mov | DstMem, em_sgdt, sgdt),
3863 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003864 II(SrcMem | Priv, em_lgdt, lgdt),
3865 II(SrcMem | Priv, em_lidt, lidt),
3866 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3867 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3868 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003869}, {
Nadav Amit0f54a322014-08-29 11:26:55 +03003870 EXT(0, group7_rm0),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003871 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003872 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003873 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3874 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3875 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003876} };
3877
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003878static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003879 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003880 F(DstMem | SrcImmByte | NoWrite, em_bt),
3881 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3882 F(DstMem | SrcImmByte | Lock, em_btr),
3883 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003884};
3885
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003886static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003887 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003888}, {
3889 N, N, N, N, N, N, N, N,
3890} };
3891
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003892static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003893 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003894 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003895};
3896
Nadav Amit13e457e2014-10-13 13:04:13 +03003897static const struct gprefix pfx_0f_ae_7 = {
Nadav Amit3f6f1482014-10-13 13:04:14 +03003898 I(SrcMem | ByteOp, em_clflush), N, N, N,
Nadav Amit13e457e2014-10-13 13:04:13 +03003899};
3900
3901static const struct group_dual group15 = { {
3902 N, N, N, N, N, N, N, GP(0, &pfx_0f_ae_7),
3903}, {
3904 N, N, N, N, N, N, N, N,
3905} };
3906
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003907static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003908 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003909};
3910
Nadav Amit39f062f2014-11-26 15:47:18 +02003911static const struct instr_dual instr_dual_0f_2b = {
3912 I(0, em_mov), N
3913};
3914
Paolo Bonzinid5b77062014-07-14 12:54:48 +02003915static const struct gprefix pfx_0f_2b = {
Nadav Amit39f062f2014-11-26 15:47:18 +02003916 ID(0, &instr_dual_0f_2b), ID(0, &instr_dual_0f_2b), N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003917};
3918
Igor Mammedov27ce8252014-03-15 21:01:59 +01003919static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01003920 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003921};
3922
Alex Williamson0a370272014-07-11 11:56:31 -06003923static const struct gprefix pfx_0f_e7 = {
3924 N, I(Sse, em_mov), N, N,
3925};
3926
Gleb Natapov045a2822012-12-20 16:57:43 +02003927static const struct escape escape_d9 = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02003928 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstcw),
Gleb Natapov045a2822012-12-20 16:57:43 +02003929}, {
3930 /* 0xC0 - 0xC7 */
3931 N, N, N, N, N, N, N, N,
3932 /* 0xC8 - 0xCF */
3933 N, N, N, N, N, N, N, N,
3934 /* 0xD0 - 0xC7 */
3935 N, N, N, N, N, N, N, N,
3936 /* 0xD8 - 0xDF */
3937 N, N, N, N, N, N, N, N,
3938 /* 0xE0 - 0xE7 */
3939 N, N, N, N, N, N, N, N,
3940 /* 0xE8 - 0xEF */
3941 N, N, N, N, N, N, N, N,
3942 /* 0xF0 - 0xF7 */
3943 N, N, N, N, N, N, N, N,
3944 /* 0xF8 - 0xFF */
3945 N, N, N, N, N, N, N, N,
3946} };
3947
3948static const struct escape escape_db = { {
3949 N, N, N, N, N, N, N, N,
3950}, {
3951 /* 0xC0 - 0xC7 */
3952 N, N, N, N, N, N, N, N,
3953 /* 0xC8 - 0xCF */
3954 N, N, N, N, N, N, N, N,
3955 /* 0xD0 - 0xC7 */
3956 N, N, N, N, N, N, N, N,
3957 /* 0xD8 - 0xDF */
3958 N, N, N, N, N, N, N, N,
3959 /* 0xE0 - 0xE7 */
3960 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3961 /* 0xE8 - 0xEF */
3962 N, N, N, N, N, N, N, N,
3963 /* 0xF0 - 0xF7 */
3964 N, N, N, N, N, N, N, N,
3965 /* 0xF8 - 0xFF */
3966 N, N, N, N, N, N, N, N,
3967} };
3968
3969static const struct escape escape_dd = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02003970 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstsw),
Gleb Natapov045a2822012-12-20 16:57:43 +02003971}, {
3972 /* 0xC0 - 0xC7 */
3973 N, N, N, N, N, N, N, N,
3974 /* 0xC8 - 0xCF */
3975 N, N, N, N, N, N, N, N,
3976 /* 0xD0 - 0xC7 */
3977 N, N, N, N, N, N, N, N,
3978 /* 0xD8 - 0xDF */
3979 N, N, N, N, N, N, N, N,
3980 /* 0xE0 - 0xE7 */
3981 N, N, N, N, N, N, N, N,
3982 /* 0xE8 - 0xEF */
3983 N, N, N, N, N, N, N, N,
3984 /* 0xF0 - 0xF7 */
3985 N, N, N, N, N, N, N, N,
3986 /* 0xF8 - 0xFF */
3987 N, N, N, N, N, N, N, N,
3988} };
3989
Nadav Amit39f062f2014-11-26 15:47:18 +02003990static const struct instr_dual instr_dual_0f_c3 = {
3991 I(DstMem | SrcReg | ModRM | No16 | Mov, em_mov), N
3992};
3993
Nadav Amit2276b512015-01-26 09:32:24 +02003994static const struct mode_dual mode_dual_63 = {
3995 N, I(DstReg | SrcMem32 | ModRM | Mov, em_movsxd)
3996};
3997
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003998static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003999 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004000 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004001 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
4002 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004003 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004004 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004005 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
4006 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004007 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004008 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004009 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
4010 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004011 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004012 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004013 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
4014 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004015 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004016 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004017 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004018 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004019 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004020 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004021 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004022 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004023 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02004024 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004025 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03004026 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004027 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09004028 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004029 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09004030 I(ImplicitOps | Stack | No64, em_pusha),
4031 I(ImplicitOps | Stack | No64, em_popa),
Nadav Amit2276b512015-01-26 09:32:24 +02004032 N, MD(ModRM, &mode_dual_63),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004033 N, N, N, N,
4034 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03004035 I(SrcImm | Mov | Stack, em_push),
4036 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03004037 I(SrcImmByte | Mov | Stack, em_push),
4038 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004039 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09004040 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03004041 /* 0x70 - 0x7F */
Nadav Amit58b70752014-10-24 11:35:09 +03004042 X16(D(SrcImmByte | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004043 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004044 G(ByteOp | DstMem | SrcImm, group1),
4045 G(DstMem | SrcImm, group1),
4046 G(ByteOp | DstMem | SrcImm | No64, group1),
4047 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02004048 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004049 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004050 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004051 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004052 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004053 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09004054 D(ModRM | SrcMem | NoAccess | DstReg),
4055 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
4056 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004057 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004058 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004059 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03004060 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08004061 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09004062 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01004063 II(ImplicitOps | Stack, em_popf, popf),
4064 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004065 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004066 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004067 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004068 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004069 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004070 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004071 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004072 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
4073 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004074 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004075 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004076 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004077 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02004078 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004079 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004080 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Nadav Amit58b70752014-10-24 11:35:09 +03004081 I(ImplicitOps | NearBranch | SrcImmU16, em_ret_near_imm),
4082 I(ImplicitOps | NearBranch, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03004083 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
4084 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004085 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004086 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03004087 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Nadav Amit16794aa2015-01-26 09:32:22 +02004088 I(ImplicitOps | SrcImmU16, em_ret_far_imm),
4089 I(ImplicitOps, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02004090 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004091 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004092 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004093 G(Src2One | ByteOp, group2), G(Src2One, group2),
4094 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02004095 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02004096 I(DstAcc | SrcImmUByte | No64, em_aad),
4097 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004098 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004099 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02004100 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004101 /* 0xE0 - 0xE7 */
Nadav Amit58b70752014-10-24 11:35:09 +03004102 X3(I(SrcImmByte | NearBranch, em_loop)),
4103 I(SrcImmByte | NearBranch, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004104 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
4105 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004106 /* 0xE8 - 0xEF */
Nadav Amit58b70752014-10-24 11:35:09 +03004107 I(SrcImm | NearBranch, em_call), D(SrcImm | ImplicitOps | NearBranch),
4108 I(SrcImmFAddr | No64, em_jmp_far),
4109 D(SrcImmByte | ImplicitOps | NearBranch),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004110 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
4111 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004112 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004113 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004114 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4115 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004116 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09004117 D(ImplicitOps), D(ImplicitOps),
4118 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004119 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4120};
4121
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004122static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004123 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004124 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02004125 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004126 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004127 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004128 N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004129 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004130 N, N, N, N, N, N, N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004131 D(ImplicitOps | ModRM | SrcMem | NoAccess),
4132 N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004133 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03004134 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
4135 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
4136 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
4137 check_cr_write),
4138 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
4139 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004140 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004141 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
4142 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004143 N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b),
Avi Kivity3e114eb2012-04-09 18:40:01 +03004144 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004145 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004146 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004147 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004148 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004149 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02004150 I(ImplicitOps | EmulateOnUD, em_sysenter),
4151 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004152 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004153 N, N, N, N, N, N, N, N,
4154 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03004155 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004156 /* 0x50 - 0x5F */
4157 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4158 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004159 N, N, N, N,
4160 N, N, N, N,
4161 N, N, N, N,
4162 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004163 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004164 N, N, N, N,
4165 N, N, N, N,
4166 N, N, N, N,
4167 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004168 /* 0x80 - 0x8F */
Nadav Amit58b70752014-10-24 11:35:09 +03004169 X16(D(SrcImm | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004170 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004171 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004172 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004173 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004174 II(ImplicitOps, em_cpuid, cpuid),
4175 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004176 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4177 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004178 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004179 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004180 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004181 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004182 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4183 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Nadav Amit13e457e2014-10-13 13:04:13 +03004184 GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004185 /* 0xB0 - 0xB7 */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02004186 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable | SrcWrite, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004187 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004188 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004189 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4190 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004191 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004192 /* 0xB8 - 0xBF */
4193 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004194 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004195 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Nadav Amit900efe22015-03-30 15:39:21 +03004196 I(DstReg | SrcMem | ModRM, em_bsf_c),
4197 I(DstReg | SrcMem | ModRM, em_bsr_c),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004198 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004199 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02004200 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Nadav Amit39f062f2014-11-26 15:47:18 +02004201 N, ID(0, &instr_dual_0f_c3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004202 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004203 /* 0xC8 - 0xCF */
4204 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004205 /* 0xD0 - 0xDF */
4206 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4207 /* 0xE0 - 0xEF */
Alex Williamson0a370272014-07-11 11:56:31 -06004208 N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
4209 N, N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004210 /* 0xF0 - 0xFF */
4211 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4212};
4213
Nadav Amit39f062f2014-11-26 15:47:18 +02004214static const struct instr_dual instr_dual_0f_38_f0 = {
4215 I(DstReg | SrcMem | Mov, em_movbe), N
4216};
4217
4218static const struct instr_dual instr_dual_0f_38_f1 = {
4219 I(DstMem | SrcReg | Mov, em_movbe), N
4220};
4221
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004222static const struct gprefix three_byte_0f_38_f0 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004223 ID(0, &instr_dual_0f_38_f0), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004224};
4225
4226static const struct gprefix three_byte_0f_38_f1 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004227 ID(0, &instr_dual_0f_38_f1), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004228};
4229
4230/*
4231 * Insns below are selected by the prefix which indexed by the third opcode
4232 * byte.
4233 */
4234static const struct opcode opcode_map_0f_38[256] = {
4235 /* 0x00 - 0x7f */
4236 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004237 /* 0x80 - 0xef */
4238 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4239 /* 0xf0 - 0xf1 */
Nadav Amit53bb4f72014-12-07 11:49:42 +02004240 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0),
4241 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004242 /* 0xf2 - 0xff */
4243 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004244};
4245
Avi Kivity73fba5f2010-07-29 15:11:53 +03004246#undef D
4247#undef N
4248#undef G
4249#undef GD
4250#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004251#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004252#undef EXT
Nadav Amit2276b512015-01-26 09:32:24 +02004253#undef MD
Nadav Amit2b42fce2015-01-26 09:32:25 +02004254#undef ID
Avi Kivity73fba5f2010-07-29 15:11:53 +03004255
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004256#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004257#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004258#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004259#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004260#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004261
Avi Kivity9dac77f2011-06-01 15:34:25 +03004262static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004263{
4264 unsigned size;
4265
Avi Kivity9dac77f2011-06-01 15:34:25 +03004266 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004267 if (size == 8)
4268 size = 4;
4269 return size;
4270}
4271
4272static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4273 unsigned size, bool sign_extension)
4274{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004275 int rc = X86EMUL_CONTINUE;
4276
4277 op->type = OP_IMM;
4278 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004279 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004280 /* NB. Immediates are sign-extended as necessary. */
4281 switch (op->bytes) {
4282 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004283 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004284 break;
4285 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004286 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004287 break;
4288 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004289 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004290 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004291 case 8:
4292 op->val = insn_fetch(s64, ctxt);
4293 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004294 }
4295 if (!sign_extension) {
4296 switch (op->bytes) {
4297 case 1:
4298 op->val &= 0xff;
4299 break;
4300 case 2:
4301 op->val &= 0xffff;
4302 break;
4303 case 4:
4304 op->val &= 0xffffffff;
4305 break;
4306 }
4307 }
4308done:
4309 return rc;
4310}
4311
Avi Kivitya99455492011-09-13 10:45:41 +03004312static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4313 unsigned d)
4314{
4315 int rc = X86EMUL_CONTINUE;
4316
4317 switch (d) {
4318 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004319 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03004320 break;
4321 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004322 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03004323 break;
4324 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004325 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004326 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03004327 *op = ctxt->memop;
4328 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004329 if (ctxt->d & BitOp)
Avi Kivitya99455492011-09-13 10:45:41 +03004330 fetch_bit_operand(ctxt);
4331 op->orig_val = op->val;
4332 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004333 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004334 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004335 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03004336 case OpAcc:
4337 op->type = OP_REG;
4338 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004339 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03004340 fetch_register_operand(op);
4341 op->orig_val = op->val;
4342 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004343 case OpAccLo:
4344 op->type = OP_REG;
4345 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4346 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4347 fetch_register_operand(op);
4348 op->orig_val = op->val;
4349 break;
4350 case OpAccHi:
4351 if (ctxt->d & ByteOp) {
4352 op->type = OP_NONE;
4353 break;
4354 }
4355 op->type = OP_REG;
4356 op->bytes = ctxt->op_bytes;
4357 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4358 fetch_register_operand(op);
4359 op->orig_val = op->val;
4360 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004361 case OpDI:
4362 op->type = OP_MEM;
4363 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4364 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004365 register_address(ctxt, VCPU_REGS_RDI);
Avi Kivitya99455492011-09-13 10:45:41 +03004366 op->addr.mem.seg = VCPU_SREG_ES;
4367 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004368 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03004369 break;
4370 case OpDX:
4371 op->type = OP_REG;
4372 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004373 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03004374 fetch_register_operand(op);
4375 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004376 case OpCL:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004377 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004378 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004379 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004380 break;
4381 case OpImmByte:
4382 rc = decode_imm(ctxt, op, 1, true);
4383 break;
4384 case OpOne:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004385 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004386 op->bytes = 1;
4387 op->val = 1;
4388 break;
4389 case OpImm:
4390 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4391 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004392 case OpImm64:
4393 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4394 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004395 case OpMem8:
4396 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004397 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004398 ctxt->memop.addr.reg = decode_register(ctxt,
4399 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004400 fetch_register_operand(&ctxt->memop);
4401 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004402 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004403 case OpMem16:
4404 ctxt->memop.bytes = 2;
4405 goto mem_common;
4406 case OpMem32:
4407 ctxt->memop.bytes = 4;
4408 goto mem_common;
4409 case OpImmU16:
4410 rc = decode_imm(ctxt, op, 2, false);
4411 break;
4412 case OpImmU:
4413 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4414 break;
4415 case OpSI:
4416 op->type = OP_MEM;
4417 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4418 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004419 register_address(ctxt, VCPU_REGS_RSI);
Bandan Das573e80f2014-04-16 12:46:13 -04004420 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03004421 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004422 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004423 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004424 case OpXLat:
4425 op->type = OP_MEM;
4426 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4427 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004428 address_mask(ctxt,
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004429 reg_read(ctxt, VCPU_REGS_RBX) +
4430 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04004431 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004432 op->val = 0;
4433 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004434 case OpImmFAddr:
4435 op->type = OP_IMM;
4436 op->addr.mem.ea = ctxt->_eip;
4437 op->bytes = ctxt->op_bytes + 2;
4438 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4439 break;
4440 case OpMemFAddr:
4441 ctxt->memop.bytes = ctxt->op_bytes + 2;
4442 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004443 case OpES:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004444 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004445 op->val = VCPU_SREG_ES;
4446 break;
4447 case OpCS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004448 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004449 op->val = VCPU_SREG_CS;
4450 break;
4451 case OpSS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004452 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004453 op->val = VCPU_SREG_SS;
4454 break;
4455 case OpDS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004456 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004457 op->val = VCPU_SREG_DS;
4458 break;
4459 case OpFS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004460 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004461 op->val = VCPU_SREG_FS;
4462 break;
4463 case OpGS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004464 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004465 op->val = VCPU_SREG_GS;
4466 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004467 case OpImplicit:
4468 /* Special instructions do their own operand decoding. */
4469 default:
4470 op->type = OP_NONE; /* Disable writeback. */
4471 break;
4472 }
4473
4474done:
4475 return rc;
4476}
4477
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004478int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004479{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004480 int rc = X86EMUL_CONTINUE;
4481 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004482 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004483 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04004484 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03004485 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004486
Avi Kivityf09ed832011-09-13 10:45:40 +03004487 ctxt->memop.type = OP_NONE;
4488 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004489 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02004490 ctxt->fetch.ptr = ctxt->fetch.data;
4491 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004492 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01004493 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004494 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004495 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02004496 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004497 if (rc != X86EMUL_CONTINUE)
4498 return rc;
4499 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004500
4501 switch (mode) {
4502 case X86EMUL_MODE_REAL:
4503 case X86EMUL_MODE_VM86:
4504 case X86EMUL_MODE_PROT16:
4505 def_op_bytes = def_ad_bytes = 2;
4506 break;
4507 case X86EMUL_MODE_PROT32:
4508 def_op_bytes = def_ad_bytes = 4;
4509 break;
4510#ifdef CONFIG_X86_64
4511 case X86EMUL_MODE_PROT64:
4512 def_op_bytes = 4;
4513 def_ad_bytes = 8;
4514 break;
4515#endif
4516 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004517 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004518 }
4519
Avi Kivity9dac77f2011-06-01 15:34:25 +03004520 ctxt->op_bytes = def_op_bytes;
4521 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004522
4523 /* Legacy prefixes. */
4524 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004525 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004526 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004527 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004528 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004529 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004530 break;
4531 case 0x67: /* address-size override */
4532 if (mode == X86EMUL_MODE_PROT64)
4533 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004534 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004535 else
4536 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004537 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004538 break;
4539 case 0x26: /* ES override */
4540 case 0x2e: /* CS override */
4541 case 0x36: /* SS override */
4542 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004543 has_seg_override = true;
4544 ctxt->seg_override = (ctxt->b >> 3) & 3;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004545 break;
4546 case 0x64: /* FS override */
4547 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004548 has_seg_override = true;
4549 ctxt->seg_override = ctxt->b & 7;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004550 break;
4551 case 0x40 ... 0x4f: /* REX */
4552 if (mode != X86EMUL_MODE_PROT64)
4553 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004554 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004555 continue;
4556 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004557 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004558 break;
4559 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004560 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004561 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004562 break;
4563 default:
4564 goto done_prefixes;
4565 }
4566
4567 /* Any legacy prefix after a REX prefix nullifies its effect. */
4568
Avi Kivity9dac77f2011-06-01 15:34:25 +03004569 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004570 }
4571
4572done_prefixes:
4573
4574 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004575 if (ctxt->rex_prefix & 8)
4576 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004577
4578 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004579 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004580 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004581 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004582 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004583 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004584 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004585
4586 /* 0F_38 opcode map */
4587 if (ctxt->b == 0x38) {
4588 ctxt->opcode_len = 3;
4589 ctxt->b = insn_fetch(u8, ctxt);
4590 opcode = opcode_map_0f_38[ctxt->b];
4591 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004592 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004593 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004594
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004595 if (ctxt->d & ModRM)
4596 ctxt->modrm = insn_fetch(u8, ctxt);
4597
Nadav Amit7fe864d2014-06-02 18:34:03 +03004598 /* vex-prefix instructions are not implemented */
4599 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
Nadav Amitd14cb5d2014-11-02 11:54:58 +02004600 (mode == X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) == 0xc0)) {
Nadav Amit7fe864d2014-06-02 18:34:03 +03004601 ctxt->d = NotImpl;
4602 }
4603
Avi Kivity9dac77f2011-06-01 15:34:25 +03004604 while (ctxt->d & GroupMask) {
4605 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004606 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004607 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004608 opcode = opcode.u.group[goffset];
4609 break;
4610 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004611 goffset = (ctxt->modrm >> 3) & 7;
4612 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004613 opcode = opcode.u.gdual->mod3[goffset];
4614 else
4615 opcode = opcode.u.gdual->mod012[goffset];
4616 break;
4617 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004618 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004619 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004620 break;
4621 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004622 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004623 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004624 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004625 switch (simd_prefix) {
4626 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4627 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4628 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4629 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4630 }
4631 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004632 case Escape:
4633 if (ctxt->modrm > 0xbf)
4634 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4635 else
4636 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4637 break;
Nadav Amit39f062f2014-11-26 15:47:18 +02004638 case InstrDual:
4639 if ((ctxt->modrm >> 6) == 3)
4640 opcode = opcode.u.idual->mod3;
4641 else
4642 opcode = opcode.u.idual->mod012;
4643 break;
Nadav Amit2276b512015-01-26 09:32:24 +02004644 case ModeDual:
4645 if (ctxt->mode == X86EMUL_MODE_PROT64)
4646 opcode = opcode.u.mdual->mode64;
4647 else
4648 opcode = opcode.u.mdual->mode32;
4649 break;
Avi Kivity46561642011-04-24 14:09:59 +03004650 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004651 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004652 }
Avi Kivity46561642011-04-24 14:09:59 +03004653
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004654 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004655 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004656 }
4657
Paolo Bonzinie24186e2014-03-27 12:00:57 +01004658 /* Unrecognised? */
4659 if (ctxt->d == 0)
4660 return EMULATION_FAILED;
4661
Avi Kivity9dac77f2011-06-01 15:34:25 +03004662 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004663
Nadav Amit3a6095a2014-08-13 16:50:13 +03004664 if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD)))
4665 return EMULATION_FAILED;
4666
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004667 if (unlikely(ctxt->d &
Nadav Amited9aad22014-11-02 11:55:00 +02004668 (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch|
4669 No16))) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004670 /*
4671 * These are copied unconditionally here, and checked unconditionally
4672 * in x86_emulate_insn.
4673 */
4674 ctxt->check_perm = opcode.check_perm;
4675 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004676
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004677 if (ctxt->d & NotImpl)
4678 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004679
Nadav Amit58b70752014-10-24 11:35:09 +03004680 if (mode == X86EMUL_MODE_PROT64) {
4681 if (ctxt->op_bytes == 4 && (ctxt->d & Stack))
4682 ctxt->op_bytes = 8;
4683 else if (ctxt->d & NearBranch)
4684 ctxt->op_bytes = 8;
4685 }
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004686
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004687 if (ctxt->d & Op3264) {
4688 if (mode == X86EMUL_MODE_PROT64)
4689 ctxt->op_bytes = 8;
4690 else
4691 ctxt->op_bytes = 4;
4692 }
4693
Nadav Amited9aad22014-11-02 11:55:00 +02004694 if ((ctxt->d & No16) && ctxt->op_bytes == 2)
4695 ctxt->op_bytes = 4;
4696
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004697 if (ctxt->d & Sse)
4698 ctxt->op_bytes = 16;
4699 else if (ctxt->d & Mmx)
4700 ctxt->op_bytes = 8;
4701 }
Avi Kivity1253791d2011-03-29 11:41:27 +02004702
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004703 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004704 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004705 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04004706 if (!has_seg_override) {
4707 has_seg_override = true;
4708 ctxt->seg_override = ctxt->modrm_seg;
4709 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004710 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004711 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004712 if (rc != X86EMUL_CONTINUE)
4713 goto done;
4714
Bandan Das573e80f2014-04-16 12:46:13 -04004715 if (!has_seg_override)
4716 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004717
Bandan Das573e80f2014-04-16 12:46:13 -04004718 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004719
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004720 /*
4721 * Decode and fetch the source operand: register, memory
4722 * or immediate.
4723 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004724 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004725 if (rc != X86EMUL_CONTINUE)
4726 goto done;
4727
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004728 /*
4729 * Decode and fetch the second source operand: register, memory
4730 * or immediate.
4731 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004732 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004733 if (rc != X86EMUL_CONTINUE)
4734 goto done;
4735
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004736 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03004737 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004738
Bandan Das41061cd2014-04-16 12:46:14 -04004739 if (ctxt->rip_relative)
Nadav Amit1c1c35a2014-11-19 17:43:09 +02004740 ctxt->memopp->addr.mem.ea = address_mask(ctxt,
4741 ctxt->memopp->addr.mem.ea + ctxt->_eip);
Avi Kivitycb16c342011-06-19 19:21:11 +03004742
Paolo Bonzinia430c912014-10-23 14:54:14 +02004743done:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004744 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004745}
4746
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004747bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4748{
4749 return ctxt->d & PageTable;
4750}
4751
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004752static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4753{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004754 /* The second termination condition only applies for REPE
4755 * and REPNE. Test if the repeat string operation prefix is
4756 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4757 * corresponding termination condition according to:
4758 * - if REPE/REPZ and ZF = 0 then done
4759 * - if REPNE/REPNZ and ZF = 1 then done
4760 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004761 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4762 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4763 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03004764 ((ctxt->eflags & X86_EFLAGS_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004765 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03004766 ((ctxt->eflags & X86_EFLAGS_ZF) == X86_EFLAGS_ZF))))
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004767 return true;
4768
4769 return false;
4770}
4771
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004772static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4773{
4774 bool fault = false;
4775
4776 ctxt->ops->get_fpu(ctxt);
4777 asm volatile("1: fwait \n\t"
4778 "2: \n\t"
4779 ".pushsection .fixup,\"ax\" \n\t"
4780 "3: \n\t"
4781 "movb $1, %[fault] \n\t"
4782 "jmp 2b \n\t"
4783 ".popsection \n\t"
4784 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004785 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004786 ctxt->ops->put_fpu(ctxt);
4787
4788 if (unlikely(fault))
4789 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4790
4791 return X86EMUL_CONTINUE;
4792}
4793
4794static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4795 struct operand *op)
4796{
4797 if (op->type == OP_MM)
4798 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4799}
4800
Avi Kivitye28bbd42013-01-04 16:18:48 +02004801static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4802{
4803 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivityb9fa4092013-02-09 11:31:48 +02004804 if (!(ctxt->d & ByteOp))
4805 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Avi Kivitye28bbd42013-01-04 16:18:48 +02004806 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004807 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
4808 [fastop]"+S"(fop)
4809 : "c"(ctxt->src2.val));
Avi Kivitye28bbd42013-01-04 16:18:48 +02004810 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004811 if (!fop) /* exception is returned in fop variable */
4812 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02004813 return X86EMUL_CONTINUE;
4814}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004815
Bandan Das14985072014-04-16 12:46:09 -04004816void init_decode_cache(struct x86_emulate_ctxt *ctxt)
4817{
Bandan Das573e80f2014-04-16 12:46:13 -04004818 memset(&ctxt->rip_relative, 0,
4819 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04004820
Bandan Das14985072014-04-16 12:46:09 -04004821 ctxt->io_read.pos = 0;
4822 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04004823 ctxt->mem_read.end = 0;
4824}
4825
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004826int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004827{
Mathias Krause0225fb52012-08-30 01:30:16 +02004828 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004829 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004830 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004831
Avi Kivity9dac77f2011-06-01 15:34:25 +03004832 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004833
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004834 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004835 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004836 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004837 goto done;
4838 }
4839
Avi Kivity9dac77f2011-06-01 15:34:25 +03004840 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004841 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004842 goto done;
4843 }
4844
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004845 if (unlikely(ctxt->d &
4846 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
4847 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4848 (ctxt->d & Undefined)) {
4849 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004850 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004851 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004852
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004853 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4854 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
4855 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004856 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004857 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004858
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004859 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
4860 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004861 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004862 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004863
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004864 if (ctxt->d & Mmx) {
4865 rc = flush_pending_x87_faults(ctxt);
4866 if (rc != X86EMUL_CONTINUE)
4867 goto done;
4868 /*
4869 * Now that we know the fpu is exception safe, we can fetch
4870 * operands from it.
4871 */
4872 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4873 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4874 if (!(ctxt->d & Mov))
4875 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4876 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004877
Bandan Das685bbf42014-04-16 12:46:10 -04004878 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004879 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4880 X86_ICPT_PRE_EXCEPT);
4881 if (rc != X86EMUL_CONTINUE)
4882 goto done;
4883 }
4884
Nadav Amit64a38292014-12-10 11:19:04 +02004885 /* Instruction can only be executed in protected mode */
4886 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
4887 rc = emulate_ud(ctxt);
4888 goto done;
4889 }
4890
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004891 /* Privileged instruction can be executed only in CPL=0 */
4892 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Nadav Amit68efa762014-06-18 17:19:35 +03004893 if (ctxt->d & PrivUD)
4894 rc = emulate_ud(ctxt);
4895 else
4896 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004897 goto done;
4898 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004899
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004900 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04004901 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004902 rc = ctxt->check_perm(ctxt);
4903 if (rc != X86EMUL_CONTINUE)
4904 goto done;
4905 }
4906
Bandan Das685bbf42014-04-16 12:46:10 -04004907 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004908 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4909 X86_ICPT_POST_EXCEPT);
4910 if (rc != X86EMUL_CONTINUE)
4911 goto done;
4912 }
4913
4914 if (ctxt->rep_prefix && (ctxt->d & String)) {
4915 /* All REP prefixes have the same first termination condition */
4916 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
4917 ctxt->eip = ctxt->_eip;
Nadav Amit0efb0442015-03-29 16:33:03 +03004918 ctxt->eflags &= ~X86_EFLAGS_RF;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004919 goto done;
4920 }
4921 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004922 }
4923
Avi Kivity9dac77f2011-06-01 15:34:25 +03004924 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4925 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4926 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004927 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004928 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004929 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004930 }
4931
Avi Kivity9dac77f2011-06-01 15:34:25 +03004932 if (ctxt->src2.type == OP_MEM) {
4933 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4934 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004935 if (rc != X86EMUL_CONTINUE)
4936 goto done;
4937 }
4938
Avi Kivity9dac77f2011-06-01 15:34:25 +03004939 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004940 goto special_insn;
4941
4942
Avi Kivity9dac77f2011-06-01 15:34:25 +03004943 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004944 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004945 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4946 &ctxt->dst.val, ctxt->dst.bytes);
Nadav Amitc205fb72014-12-25 02:52:16 +02004947 if (rc != X86EMUL_CONTINUE) {
Paolo Bonzinid44e1212015-02-09 10:02:05 +01004948 if (!(ctxt->d & NoWrite) &&
4949 rc == X86EMUL_PROPAGATE_FAULT &&
Nadav Amitc205fb72014-12-25 02:52:16 +02004950 ctxt->exception.vector == PF_VECTOR)
4951 ctxt->exception.error_code |= PFERR_WRITE_MASK;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004952 goto done;
Nadav Amitc205fb72014-12-25 02:52:16 +02004953 }
Avi Kivity038e51d2007-01-22 20:40:40 -08004954 }
Paolo Bonzini4ff6f8e2015-02-12 17:04:47 +01004955 /* Copy full 64-bit value for CMPXCHG8B. */
4956 ctxt->dst.orig_val64 = ctxt->dst.val64;
Avi Kivity038e51d2007-01-22 20:40:40 -08004957
Avi Kivity018a98d2007-11-27 19:30:56 +02004958special_insn:
4959
Bandan Das685bbf42014-04-16 12:46:10 -04004960 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004961 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004962 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004963 if (rc != X86EMUL_CONTINUE)
4964 goto done;
4965 }
4966
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004967 if (ctxt->rep_prefix && (ctxt->d & String))
Nadav Amit0efb0442015-03-29 16:33:03 +03004968 ctxt->eflags |= X86_EFLAGS_RF;
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004969 else
Nadav Amit0efb0442015-03-29 16:33:03 +03004970 ctxt->eflags &= ~X86_EFLAGS_RF;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004971
Avi Kivity9dac77f2011-06-01 15:34:25 +03004972 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004973 if (ctxt->d & Fastop) {
4974 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4975 rc = fastop(ctxt, fop);
4976 if (rc != X86EMUL_CONTINUE)
4977 goto done;
4978 goto writeback;
4979 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004980 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004981 if (rc != X86EMUL_CONTINUE)
4982 goto done;
4983 goto writeback;
4984 }
4985
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004986 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004987 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004988 else if (ctxt->opcode_len == 3)
4989 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004990
Avi Kivity9dac77f2011-06-01 15:34:25 +03004991 switch (ctxt->b) {
Gleb Natapovb2833e32009-04-12 13:36:30 +03004992 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004993 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03004994 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004995 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004996 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004997 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004998 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004999 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005000 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03005001 ctxt->dst.type = OP_NONE;
5002 else
5003 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09005004 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005005 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005006 switch (ctxt->op_bytes) {
5007 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
5008 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
5009 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005010 }
5011 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005012 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005013 rc = emulate_int(ctxt, 3);
5014 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005015 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005016 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005017 break;
5018 case 0xce: /* into */
Nadav Amit0efb0442015-03-29 16:33:03 +03005019 if (ctxt->eflags & X86_EFLAGS_OF)
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005020 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005021 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005022 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09005023 case 0xeb: /* jmp rel short */
Nadav Amit234f3ce2014-09-18 22:39:38 +03005024 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity9dac77f2011-06-01 15:34:25 +03005025 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005026 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005027 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03005028 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03005029 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005030 case 0xf5: /* cmc */
5031 /* complement carry flag from eflags reg */
Nadav Amit0efb0442015-03-29 16:33:03 +03005032 ctxt->eflags ^= X86_EFLAGS_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005033 break;
5034 case 0xf8: /* clc */
Nadav Amit0efb0442015-03-29 16:33:03 +03005035 ctxt->eflags &= ~X86_EFLAGS_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005036 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03005037 case 0xf9: /* stc */
Nadav Amit0efb0442015-03-29 16:33:03 +03005038 ctxt->eflags |= X86_EFLAGS_CF;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03005039 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005040 case 0xfc: /* cld */
Nadav Amit0efb0442015-03-29 16:33:03 +03005041 ctxt->eflags &= ~X86_EFLAGS_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005042 break;
5043 case 0xfd: /* std */
Nadav Amit0efb0442015-03-29 16:33:03 +03005044 ctxt->eflags |= X86_EFLAGS_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005045 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005046 default:
5047 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005048 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005049
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005050 if (rc != X86EMUL_CONTINUE)
5051 goto done;
5052
Avi Kivity018a98d2007-11-27 19:30:56 +02005053writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02005054 if (ctxt->d & SrcWrite) {
5055 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
5056 rc = writeback(ctxt, &ctxt->src);
5057 if (rc != X86EMUL_CONTINUE)
5058 goto done;
5059 }
Nadav Amitee212292014-06-15 16:12:58 +03005060 if (!(ctxt->d & NoWrite)) {
5061 rc = writeback(ctxt, &ctxt->dst);
5062 if (rc != X86EMUL_CONTINUE)
5063 goto done;
5064 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005065
Gleb Natapov5cd21912010-03-18 15:20:26 +02005066 /*
5067 * restore dst type in case the decoding will be reused
5068 * (happens for string instruction )
5069 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005070 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005071
Avi Kivity9dac77f2011-06-01 15:34:25 +03005072 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005073 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02005074
Avi Kivity9dac77f2011-06-01 15:34:25 +03005075 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005076 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02005077
Avi Kivity9dac77f2011-06-01 15:34:25 +03005078 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005079 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005080 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005081 if ((ctxt->d & SrcMask) == SrcSI)
5082 count = ctxt->src.count;
5083 else
5084 count = ctxt->dst.count;
Paolo Bonzini01485a22014-11-19 18:25:08 +01005085 register_address_increment(ctxt, VCPU_REGS_RCX, -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005086
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005087 if (!string_insn_completed(ctxt)) {
5088 /*
5089 * Re-enter guest when pio read ahead buffer is empty
5090 * or, if it is not used, after each 1024 iteration.
5091 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005092 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005093 (r->end == 0 || r->end != r->pos)) {
5094 /*
5095 * Reset read cache. Usually happens before
5096 * decode, but since instruction is restarted
5097 * we have to do it here.
5098 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005099 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005100 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005101 return EMULATION_RESTART;
5102 }
5103 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03005104 }
Nadav Amit0efb0442015-03-29 16:33:03 +03005105 ctxt->eflags &= ~X86_EFLAGS_RF;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005106 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005107
Avi Kivity9dac77f2011-06-01 15:34:25 +03005108 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02005109
5110done:
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005111 if (rc == X86EMUL_PROPAGATE_FAULT) {
5112 WARN_ON(ctxt->exception.vector > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +02005113 ctxt->have_exception = true;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005114 }
Joerg Roedel775fde82011-04-04 12:39:24 +02005115 if (rc == X86EMUL_INTERCEPTED)
5116 return EMULATION_INTERCEPTED;
5117
Avi Kivitydd856ef2012-08-27 23:46:17 +03005118 if (rc == X86EMUL_CONTINUE)
5119 writeback_registers(ctxt);
5120
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005121 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005122
5123twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005124 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02005125 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02005126 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005127 break;
5128 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02005129 case 0x0d: /* GrpP (prefetch) */
5130 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02005131 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02005132 break;
5133 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005134 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02005135 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005136 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005137 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005138 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005139 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03005140 if (test_cc(ctxt->b, ctxt->eflags))
5141 ctxt->dst.val = ctxt->src.val;
Nadav Amitb91aa142015-03-30 15:39:19 +03005142 else if (ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005143 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08005144 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03005145 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03005146 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005147 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005148 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08005149 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005150 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08005151 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005152 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005153 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005154 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03005155 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005156 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005157 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005158 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005159 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03005160 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005161 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005162 default:
5163 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005164 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005165
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005166threebyte_insn:
5167
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005168 if (rc != X86EMUL_CONTINUE)
5169 goto done;
5170
Avi Kivity6aa8b732006-12-10 02:21:36 -08005171 goto writeback;
5172
5173cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02005174 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005175}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005176
5177void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
5178{
5179 invalidate_registers(ctxt);
5180}
5181
5182void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
5183{
5184 writeback_registers(ctxt);
5185}