blob: 453d8c99010822222b8a1d9433d0bdfc6dc50fd9 [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 Kivity56e82312009-08-12 15:04:37 +030025#include <asm/kvm_emulate.h>
Avi Kivityb7d491e2013-01-04 16:18:49 +020026#include <linux/stringify.h>
Nadav Amit3db176d2015-04-19 21:12:59 +030027#include <asm/debugreg.h>
Peter Zijlstra1a29b5b2018-01-25 10:58:13 +010028#include <asm/nospec-branch.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"
Yu Zhangd1cd3ce2017-08-24 20:27:53 +080032#include "mmu.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020033
Avi Kivity6aa8b732006-12-10 02:21:36 -080034/*
Avi Kivitya99455492011-09-13 10:45:41 +030035 * Operand types
36 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030037#define OpNone 0ull
38#define OpImplicit 1ull /* No generic decode */
39#define OpReg 2ull /* Register */
40#define OpMem 3ull /* Memory */
41#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
42#define OpDI 5ull /* ES:DI/EDI/RDI */
43#define OpMem64 6ull /* Memory, 64-bit */
44#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
45#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030046#define OpCL 9ull /* CL register (for shifts) */
47#define OpImmByte 10ull /* 8-bit sign extended immediate */
48#define OpOne 11ull /* Implied 1 */
Nadav Amit5e2c6882012-12-06 21:55:10 -020049#define OpImm 12ull /* Sign extended up to 32-bit immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030050#define OpMem16 13ull /* Memory operand (16-bit). */
51#define OpMem32 14ull /* Memory operand (32-bit). */
52#define OpImmU 15ull /* Immediate operand, zero extended */
53#define OpSI 16ull /* SI/ESI/RSI */
54#define OpImmFAddr 17ull /* Immediate far address */
55#define OpMemFAddr 18ull /* Far address in memory */
56#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030057#define OpES 20ull /* ES */
58#define OpCS 21ull /* CS */
59#define OpSS 22ull /* SS */
60#define OpDS 23ull /* DS */
61#define OpFS 24ull /* FS */
62#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020063#define OpMem8 26ull /* 8-bit zero extended memory operand */
Nadav Amit5e2c6882012-12-06 21:55:10 -020064#define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */
Paolo Bonzini7fa57952013-05-09 11:32:50 +020065#define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */
Avi Kivity820207c2013-02-09 11:31:45 +020066#define OpAccLo 29ull /* Low part of extended acc (AX/AX/EAX/RAX) */
67#define OpAccHi 30ull /* High part of extended acc (-/DX/EDX/RDX) */
Avi Kivitya99455492011-09-13 10:45:41 +030068
Avi Kivity0fe59122011-09-13 10:45:47 +030069#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030070#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya99455492011-09-13 10:45:41 +030071
72/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080073 * Opcode effective-address decode tables.
74 * Note that we only emulate instructions that have at least one memory
75 * operand (excluding implicit stack references). We assume that stack
76 * references and instruction fetches will never occur in special memory
77 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
78 * not be handled.
79 */
80
81/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030082#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080083/* Destination operand type. */
Avi Kivitya99455492011-09-13 10:45:41 +030084#define DstShift 1
85#define ImplicitOps (OpImplicit << DstShift)
86#define DstReg (OpReg << DstShift)
87#define DstMem (OpMem << DstShift)
88#define DstAcc (OpAcc << DstShift)
89#define DstDI (OpDI << DstShift)
90#define DstMem64 (OpMem64 << DstShift)
Nadav Amit16bebef2014-12-25 02:52:18 +020091#define DstMem16 (OpMem16 << DstShift)
Avi Kivitya99455492011-09-13 10:45:41 +030092#define DstImmUByte (OpImmUByte << DstShift)
93#define DstDX (OpDX << DstShift)
Avi Kivity820207c2013-02-09 11:31:45 +020094#define DstAccLo (OpAccLo << DstShift)
Avi Kivitya99455492011-09-13 10:45:41 +030095#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080096/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030097#define SrcShift 6
98#define SrcNone (OpNone << SrcShift)
99#define SrcReg (OpReg << SrcShift)
100#define SrcMem (OpMem << SrcShift)
101#define SrcMem16 (OpMem16 << SrcShift)
102#define SrcMem32 (OpMem32 << SrcShift)
103#define SrcImm (OpImm << SrcShift)
104#define SrcImmByte (OpImmByte << SrcShift)
105#define SrcOne (OpOne << SrcShift)
106#define SrcImmUByte (OpImmUByte << SrcShift)
107#define SrcImmU (OpImmU << SrcShift)
108#define SrcSI (OpSI << SrcShift)
Paolo Bonzini7fa57952013-05-09 11:32:50 +0200109#define SrcXLat (OpXLat << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300110#define SrcImmFAddr (OpImmFAddr << SrcShift)
111#define SrcMemFAddr (OpMemFAddr << SrcShift)
112#define SrcAcc (OpAcc << SrcShift)
113#define SrcImmU16 (OpImmU16 << SrcShift)
Nadav Amit5e2c6882012-12-06 21:55:10 -0200114#define SrcImm64 (OpImm64 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300115#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200116#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity820207c2013-02-09 11:31:45 +0200117#define SrcAccHi (OpAccHi << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300118#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300119#define BitOp (1<<11)
120#define MemAbs (1<<12) /* Memory operand is absolute displacement */
121#define String (1<<13) /* String instruction (rep capable) */
122#define Stack (1<<14) /* Stack instruction (push/pop) */
123#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
124#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
125#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
126#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
127#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
Gleb Natapov045a2822012-12-20 16:57:43 +0200128#define Escape (5<<15) /* Escape to coprocessor instruction */
Nadav Amit39f062f2014-11-26 15:47:18 +0200129#define InstrDual (6<<15) /* Alternate instruction decoding of mod == 3 */
Nadav Amit2276b512015-01-26 09:32:24 +0200130#define ModeDual (7<<15) /* Different instruction for 32/64 bit */
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300131#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300132/* Generic ModRM decode. */
133#define ModRM (1<<19)
134/* Destination is only written; never read. */
135#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300136/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200137#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Borislav Petkovb51e9742013-09-22 16:44:52 +0200138#define EmulateOnUD (1<<22) /* Emulate if unsupported by the host */
Avi Kivity5a506b12010-08-01 15:10:29 +0300139#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300140#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300141#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200142#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200143#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300144#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800145#define PageTable (1 << 29) /* instruction used to write page table */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300146#define NotImpl (1 << 30) /* instruction is not implemented */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100147/* Source 2 operand type */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300148#define Src2Shift (31)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300149#define Src2None (OpNone << Src2Shift)
Avi Kivityab2c5ce2013-02-09 11:31:46 +0200150#define Src2Mem (OpMem << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300151#define Src2CL (OpCL << Src2Shift)
152#define Src2ImmByte (OpImmByte << Src2Shift)
153#define Src2One (OpOne << Src2Shift)
154#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300155#define Src2ES (OpES << Src2Shift)
156#define Src2CS (OpCS << Src2Shift)
157#define Src2SS (OpSS << Src2Shift)
158#define Src2DS (OpDS << Src2Shift)
159#define Src2FS (OpFS << Src2Shift)
160#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300161#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300162#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Radim Krčmář48520182016-11-08 20:54:17 +0100163#define AlignMask ((u64)7 << 41)
Avi Kivity1c11b372012-04-09 18:39:59 +0300164#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
Radim Krčmář48520182016-11-08 20:54:17 +0100165#define Unaligned ((u64)2 << 41) /* Explicitly unaligned (e.g. MOVDQU) */
166#define Avx ((u64)3 << 41) /* Advanced Vector Extensions */
167#define Aligned16 ((u64)4 << 41) /* Aligned to 16 byte boundary (e.g. FXSAVE) */
Avi Kivitye28bbd42013-01-04 16:18:48 +0200168#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
Avi Kivityb6744dc2013-01-04 16:18:50 +0200169#define NoWrite ((u64)1 << 45) /* No writeback */
Avi Kivityfb32b1e2013-02-09 11:31:44 +0200170#define SrcWrite ((u64)1 << 46) /* Write back src operand */
Nadav Amit9b88ae92014-05-25 23:05:21 +0300171#define NoMod ((u64)1 << 47) /* Mod field is ignored */
Paolo Bonzinid40a6892014-03-27 11:58:02 +0100172#define Intercept ((u64)1 << 48) /* Has valid intercept field */
173#define CheckPerm ((u64)1 << 49) /* Has valid check_perm field */
Nadav Amit68efa762014-06-18 17:19:35 +0300174#define PrivUD ((u64)1 << 51) /* #UD instead of #GP on CPL > 0 */
Nadav Amit58b70752014-10-24 11:35:09 +0300175#define NearBranch ((u64)1 << 52) /* Near branches */
Nadav Amited9aad22014-11-02 11:55:00 +0200176#define No16 ((u64)1 << 53) /* No 16 bit operand */
Nadav Amitab708092014-12-25 02:52:21 +0200177#define IncSP ((u64)1 << 54) /* SP is incremented before ModRM calc */
Tom Lendacky0f89b202016-12-14 14:59:23 -0500178#define TwoMemOp ((u64)1 << 55) /* Instruction has two memory operand */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800179
Avi Kivity820207c2013-02-09 11:31:45 +0200180#define DstXacc (DstAccLo | SrcAccHi | SrcWrite)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800181
Avi Kivityd0e53322010-07-29 15:11:54 +0300182#define X2(x...) x, x
183#define X3(x...) X2(x), x
184#define X4(x...) X2(x), X2(x)
185#define X5(x...) X4(x), x
186#define X6(x...) X4(x), X2(x)
187#define X7(x...) X4(x), X3(x)
188#define X8(x...) X4(x), X4(x)
189#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300190
Avi Kivitye28bbd42013-01-04 16:18:48 +0200191#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
192#define FASTOP_SIZE 8
193
194/*
195 * fastop functions have a special calling convention:
196 *
Avi Kivity017da7b2013-02-09 11:31:47 +0200197 * dst: rax (in/out)
198 * src: rdx (in/out)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200199 * src2: rcx (in)
200 * flags: rflags (in/out)
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200201 * ex: rsi (in:fastop pointer, out:zero if exception)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200202 *
203 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
204 * different operand sizes can be reached by calculation, rather than a jump
205 * table (which would be bigger than the code).
206 *
207 * fastop functions are declared as taking a never-defined fastop parameter,
208 * so they can't be called from C directly.
209 */
210
211struct fastop;
212
Avi Kivityd65b1de2010-07-29 15:11:35 +0300213struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300214 u64 flags : 56;
215 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300216 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300217 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200218 const struct opcode *group;
219 const struct group_dual *gdual;
220 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200221 const struct escape *esc;
Nadav Amit39f062f2014-11-26 15:47:18 +0200222 const struct instr_dual *idual;
Nadav Amit2276b512015-01-26 09:32:24 +0200223 const struct mode_dual *mdual;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200224 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300225 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200226 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300227};
228
229struct group_dual {
230 struct opcode mod012[8];
231 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300232};
233
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200234struct gprefix {
235 struct opcode pfx_no;
236 struct opcode pfx_66;
237 struct opcode pfx_f2;
238 struct opcode pfx_f3;
239};
240
Gleb Natapov045a2822012-12-20 16:57:43 +0200241struct escape {
242 struct opcode op[8];
243 struct opcode high[64];
244};
245
Nadav Amit39f062f2014-11-26 15:47:18 +0200246struct instr_dual {
247 struct opcode mod012;
248 struct opcode mod3;
249};
250
Nadav Amit2276b512015-01-26 09:32:24 +0200251struct mode_dual {
252 struct opcode mode32;
253 struct opcode mode64;
254};
255
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300256#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300257
Nadav Amit3dc4bc42014-12-25 02:52:19 +0200258enum x86_transfer_type {
259 X86_TRANSFER_NONE,
260 X86_TRANSFER_CALL_JMP,
261 X86_TRANSFER_RET,
262 X86_TRANSFER_TASK_SWITCH,
263};
264
Avi Kivitydd856ef2012-08-27 23:46:17 +0300265static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
266{
267 if (!(ctxt->regs_valid & (1 << nr))) {
268 ctxt->regs_valid |= 1 << nr;
269 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
270 }
271 return ctxt->_regs[nr];
272}
273
274static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
275{
276 ctxt->regs_valid |= 1 << nr;
277 ctxt->regs_dirty |= 1 << nr;
278 return &ctxt->_regs[nr];
279}
280
281static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
282{
283 reg_read(ctxt, nr);
284 return reg_write(ctxt, nr);
285}
286
287static void writeback_registers(struct x86_emulate_ctxt *ctxt)
288{
289 unsigned reg;
290
291 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
292 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
293}
294
295static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
296{
297 ctxt->regs_dirty = 0;
298 ctxt->regs_valid = 0;
299}
300
Avi Kivity6aa8b732006-12-10 02:21:36 -0800301/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800302 * These EFLAGS bits are restored from saved value during emulation, and
303 * any changes are written back to the saved value after emulation.
304 */
Nadav Amit0efb0442015-03-29 16:33:03 +0300305#define EFLAGS_MASK (X86_EFLAGS_OF|X86_EFLAGS_SF|X86_EFLAGS_ZF|X86_EFLAGS_AF|\
306 X86_EFLAGS_PF|X86_EFLAGS_CF)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800307
Avi Kivitydda96d82008-11-26 15:14:10 +0200308#ifdef CONFIG_X86_64
309#define ON64(x) x
310#else
311#define ON64(x)
312#endif
313
Avi Kivity4d758342013-01-19 19:51:55 +0200314static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *));
315
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600316#define FOP_FUNC(name) \
317 ".align " __stringify(FASTOP_SIZE) " \n\t" \
318 ".type " name ", @function \n\t" \
319 name ":\n\t"
320
Avi Kivityb7d491e2013-01-04 16:18:49 +0200321#define FOP_RET "ret \n\t"
322
323#define FOP_START(op) \
324 extern void em_##op(struct fastop *fake); \
325 asm(".pushsection .text, \"ax\" \n\t" \
326 ".global em_" #op " \n\t" \
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600327 FOP_FUNC("em_" #op)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200328
329#define FOP_END \
330 ".popsection")
331
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600332#define FOPNOP() \
333 FOP_FUNC(__stringify(__UNIQUE_ID(nop))) \
334 FOP_RET
Avi Kivity0bdea062013-01-19 19:51:50 +0200335
Avi Kivityb7d491e2013-01-04 16:18:49 +0200336#define FOP1E(op, dst) \
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600337 FOP_FUNC(#op "_" #dst) \
338 "10: " #op " %" #dst " \n\t" FOP_RET
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200339
340#define FOP1EEX(op, dst) \
341 FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200342
343#define FASTOP1(op) \
344 FOP_START(op) \
345 FOP1E(op##b, al) \
346 FOP1E(op##w, ax) \
347 FOP1E(op##l, eax) \
348 ON64(FOP1E(op##q, rax)) \
349 FOP_END
350
Avi Kivityb9fa4092013-02-09 11:31:48 +0200351/* 1-operand, using src2 (for MUL/DIV r/m) */
352#define FASTOP1SRC2(op, name) \
353 FOP_START(name) \
354 FOP1E(op, cl) \
355 FOP1E(op, cx) \
356 FOP1E(op, ecx) \
357 ON64(FOP1E(op, rcx)) \
358 FOP_END
359
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200360/* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */
361#define FASTOP1SRC2EX(op, name) \
362 FOP_START(name) \
363 FOP1EEX(op, cl) \
364 FOP1EEX(op, cx) \
365 FOP1EEX(op, ecx) \
366 ON64(FOP1EEX(op, rcx)) \
367 FOP_END
368
Avi Kivityf7857f32013-01-04 16:18:53 +0200369#define FOP2E(op, dst, src) \
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600370 FOP_FUNC(#op "_" #dst "_" #src) \
371 #op " %" #src ", %" #dst " \n\t" FOP_RET
Avi Kivityf7857f32013-01-04 16:18:53 +0200372
373#define FASTOP2(op) \
374 FOP_START(op) \
Avi Kivity017da7b2013-02-09 11:31:47 +0200375 FOP2E(op##b, al, dl) \
376 FOP2E(op##w, ax, dx) \
377 FOP2E(op##l, eax, edx) \
378 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivityf7857f32013-01-04 16:18:53 +0200379 FOP_END
380
Avi Kivity11c363b2013-01-19 19:51:54 +0200381/* 2 operand, word only */
382#define FASTOP2W(op) \
383 FOP_START(op) \
384 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200385 FOP2E(op##w, ax, dx) \
386 FOP2E(op##l, eax, edx) \
387 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivity11c363b2013-01-19 19:51:54 +0200388 FOP_END
389
Avi Kivity007a3b52013-01-19 19:51:51 +0200390/* 2 operand, src is CL */
391#define FASTOP2CL(op) \
392 FOP_START(op) \
393 FOP2E(op##b, al, cl) \
394 FOP2E(op##w, ax, cl) \
395 FOP2E(op##l, eax, cl) \
396 ON64(FOP2E(op##q, rax, cl)) \
397 FOP_END
398
Nadav Amit5aca3722014-11-02 11:54:50 +0200399/* 2 operand, src and dest are reversed */
400#define FASTOP2R(op, name) \
401 FOP_START(name) \
402 FOP2E(op##b, dl, al) \
403 FOP2E(op##w, dx, ax) \
404 FOP2E(op##l, edx, eax) \
405 ON64(FOP2E(op##q, rdx, rax)) \
406 FOP_END
407
Avi Kivity0bdea062013-01-19 19:51:50 +0200408#define FOP3E(op, dst, src, src2) \
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600409 FOP_FUNC(#op "_" #dst "_" #src "_" #src2) \
410 #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
Avi Kivity0bdea062013-01-19 19:51:50 +0200411
412/* 3-operand, word-only, src2=cl */
413#define FASTOP3WCL(op) \
414 FOP_START(op) \
415 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200416 FOP3E(op##w, ax, dx, cl) \
417 FOP3E(op##l, eax, edx, cl) \
418 ON64(FOP3E(op##q, rax, rdx, cl)) \
Avi Kivity0bdea062013-01-19 19:51:50 +0200419 FOP_END
420
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200421/* Special case for SETcc - 1 instruction per cc */
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600422#define FOP_SETCC(op) \
423 ".align 4 \n\t" \
424 ".type " #op ", @function \n\t" \
425 #op ": \n\t" \
426 #op " %al \n\t" \
427 FOP_RET
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200428
Josh Poimboeuff26e6012017-10-04 10:39:05 -0500429asm(".pushsection .fixup, \"ax\"\n"
430 ".global kvm_fastop_exception \n"
431 "kvm_fastop_exception: xor %esi, %esi; ret\n"
432 ".popsection");
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200433
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200434FOP_START(setcc)
435FOP_SETCC(seto)
436FOP_SETCC(setno)
437FOP_SETCC(setc)
438FOP_SETCC(setnc)
439FOP_SETCC(setz)
440FOP_SETCC(setnz)
441FOP_SETCC(setbe)
442FOP_SETCC(setnbe)
443FOP_SETCC(sets)
444FOP_SETCC(setns)
445FOP_SETCC(setp)
446FOP_SETCC(setnp)
447FOP_SETCC(setl)
448FOP_SETCC(setnl)
449FOP_SETCC(setle)
450FOP_SETCC(setnle)
451FOP_END;
452
Paolo Bonzini326f5782013-05-09 11:32:51 +0200453FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET
454FOP_END;
455
Radim Krčmářaabba3c2016-11-08 20:54:18 +0100456/*
457 * XXX: inoutclob user must know where the argument is being expanded.
458 * Relying on CC_HAVE_ASM_GOTO would allow us to remove _fault.
459 */
460#define asm_safe(insn, inoutclob...) \
461({ \
462 int _fault = 0; \
463 \
464 asm volatile("1:" insn "\n" \
465 "2:\n" \
466 ".pushsection .fixup, \"ax\"\n" \
467 "3: movl $1, %[_fault]\n" \
468 " jmp 2b\n" \
469 ".popsection\n" \
470 _ASM_EXTABLE(1b, 3b) \
471 : [_fault] "+qm"(_fault) inoutclob ); \
472 \
473 _fault ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE; \
474})
475
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200476static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
477 enum x86_intercept intercept,
478 enum x86_intercept_stage stage)
479{
480 struct x86_instruction_info info = {
481 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300482 .rep_prefix = ctxt->rep_prefix,
483 .modrm_mod = ctxt->modrm_mod,
484 .modrm_reg = ctxt->modrm_reg,
485 .modrm_rm = ctxt->modrm_rm,
486 .src_val = ctxt->src.val64,
Jan Kiszka6cbc5f52014-06-30 12:52:55 +0200487 .dst_val = ctxt->dst.val64,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300488 .src_bytes = ctxt->src.bytes,
489 .dst_bytes = ctxt->dst.bytes,
490 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200491 .next_rip = ctxt->eip,
492 };
493
Avi Kivity29535382011-04-20 13:37:53 +0300494 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200495}
496
Avi Kivityf47cfa32012-06-07 17:49:24 +0300497static void assign_masked(ulong *dest, ulong src, ulong mask)
498{
499 *dest = (*dest & ~mask) | (src & mask);
500}
501
Nadav Amit6fd8e122015-03-30 15:39:20 +0300502static void assign_register(unsigned long *reg, u64 val, int bytes)
503{
504 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
505 switch (bytes) {
506 case 1:
507 *(u8 *)reg = (u8)val;
508 break;
509 case 2:
510 *(u16 *)reg = (u16)val;
511 break;
512 case 4:
513 *reg = (u32)val;
514 break; /* 64b: zero-extend */
515 case 8:
516 *reg = val;
517 break;
518 }
519}
520
Avi Kivity9dac77f2011-06-01 15:34:25 +0300521static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800522{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300523 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800524}
525
Avi Kivityf47cfa32012-06-07 17:49:24 +0300526static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
527{
528 u16 sel;
529 struct desc_struct ss;
530
531 if (ctxt->mode == X86EMUL_MODE_PROT64)
532 return ~0UL;
533 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
534 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
535}
536
Avi Kivity612e89f2012-06-12 20:03:23 +0300537static int stack_size(struct x86_emulate_ctxt *ctxt)
538{
539 return (__fls(stack_mask(ctxt)) + 1) >> 3;
540}
541
Avi Kivity6aa8b732006-12-10 02:21:36 -0800542/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800543static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300544address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800545{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300546 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800547 return reg;
548 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300549 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800550}
551
552static inline unsigned long
Paolo Bonzini01485a22014-11-19 18:25:08 +0100553register_address(struct x86_emulate_ctxt *ctxt, int reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800554{
Paolo Bonzini01485a22014-11-19 18:25:08 +0100555 return address_mask(ctxt, reg_read(ctxt, reg));
Harvey Harrisone4706772008-02-19 07:40:38 -0800556}
557
Avi Kivity5ad105e2012-08-19 14:34:31 +0300558static void masked_increment(ulong *reg, ulong mask, int inc)
559{
560 assign_masked(reg, *reg + inc, mask);
561}
562
Harvey Harrison7a9572752008-02-19 07:40:41 -0800563static inline void
Paolo Bonzini01485a22014-11-19 18:25:08 +0100564register_address_increment(struct x86_emulate_ctxt *ctxt, int reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800565{
Nadav Amitee122a72015-04-28 13:06:00 +0300566 ulong *preg = reg_rmw(ctxt, reg);
Avi Kivity5ad105e2012-08-19 14:34:31 +0300567
Nadav Amitee122a72015-04-28 13:06:00 +0300568 assign_register(preg, *preg + inc, ctxt->ad_bytes);
Avi Kivity5ad105e2012-08-19 14:34:31 +0300569}
570
571static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
572{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300573 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800574}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800575
Avi Kivity56697682011-04-03 14:08:51 +0300576static u32 desc_limit_scaled(struct desc_struct *desc)
577{
578 u32 limit = get_desc_limit(desc);
579
580 return desc->g ? (limit << 12) | 0xfff : limit;
581}
582
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900583static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300584{
585 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
586 return 0;
587
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900588 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300589}
590
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200591static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
592 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300593{
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +0200594 WARN_ON(vec > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +0200595 ctxt->exception.vector = vec;
596 ctxt->exception.error_code = error;
597 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200598 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300599}
600
Joerg Roedel3b88e412011-04-04 12:39:29 +0200601static int emulate_db(struct x86_emulate_ctxt *ctxt)
602{
603 return emulate_exception(ctxt, DB_VECTOR, 0, false);
604}
605
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200606static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300607{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200608 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300609}
610
Avi Kivity618ff152011-04-03 12:32:09 +0300611static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
612{
613 return emulate_exception(ctxt, SS_VECTOR, err, true);
614}
615
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200616static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300617{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200618 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300619}
620
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200621static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300622{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200623 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300624}
625
Avi Kivity34d1f492010-08-26 11:59:01 +0300626static int emulate_de(struct x86_emulate_ctxt *ctxt)
627{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200628 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300629}
630
Avi Kivity1253791d2011-03-29 11:41:27 +0200631static int emulate_nm(struct x86_emulate_ctxt *ctxt)
632{
633 return emulate_exception(ctxt, NM_VECTOR, 0, false);
634}
635
Avi Kivity1aa36612011-04-27 13:20:30 +0300636static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
637{
638 u16 selector;
639 struct desc_struct desc;
640
641 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
642 return selector;
643}
644
645static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
646 unsigned seg)
647{
648 u16 dummy;
649 u32 base3;
650 struct desc_struct desc;
651
652 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
653 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
654}
655
Avi Kivity1c11b372012-04-09 18:39:59 +0300656/*
657 * x86 defines three classes of vector instructions: explicitly
658 * aligned, explicitly unaligned, and the rest, which change behaviour
659 * depending on whether they're AVX encoded or not.
660 *
661 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100662 * subject to the same check. FXSAVE and FXRSTOR are checked here too as their
663 * 512 bytes of data must be aligned to a 16 byte boundary.
Avi Kivity1c11b372012-04-09 18:39:59 +0300664 */
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100665static unsigned insn_alignment(struct x86_emulate_ctxt *ctxt, unsigned size)
Avi Kivity1c11b372012-04-09 18:39:59 +0300666{
Radim Krčmář48520182016-11-08 20:54:17 +0100667 u64 alignment = ctxt->d & AlignMask;
Avi Kivity1c11b372012-04-09 18:39:59 +0300668
Avi Kivity1c11b372012-04-09 18:39:59 +0300669 if (likely(size < 16))
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100670 return 1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300671
Radim Krčmář48520182016-11-08 20:54:17 +0100672 switch (alignment) {
673 case Unaligned:
674 case Avx:
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100675 return 1;
Radim Krčmář48520182016-11-08 20:54:17 +0100676 case Aligned16:
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100677 return 16;
Radim Krčmář48520182016-11-08 20:54:17 +0100678 case Aligned:
679 default:
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100680 return size;
Radim Krčmář48520182016-11-08 20:54:17 +0100681 }
Avi Kivity1c11b372012-04-09 18:39:59 +0300682}
683
Paolo Bonzinid09155d2014-10-27 14:54:44 +0100684static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
685 struct segmented_address addr,
686 unsigned *max_size, unsigned size,
687 bool write, bool fetch,
Nadav Amitd50eaa12014-11-19 17:43:11 +0200688 enum x86emul_mode mode, ulong *linear)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300689{
Avi Kivity618ff152011-04-03 12:32:09 +0300690 struct desc_struct desc;
691 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300692 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300693 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300694 u16 sel;
Yu Zhangfd8cb432017-08-24 20:27:56 +0800695 u8 va_bits;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300696
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900697 la = seg_base(ctxt, addr.seg) + addr.ea;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100698 *max_size = 0;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200699 switch (mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300700 case X86EMUL_MODE_PROT64:
Paolo Bonzini0c1d77f2016-02-19 18:07:21 +0100701 *linear = la;
Yu Zhangfd8cb432017-08-24 20:27:56 +0800702 va_bits = ctxt_virt_addr_bits(ctxt);
703 if (get_canonical(la, va_bits) != la)
Nadav Amitabc7d8a2014-11-19 17:43:12 +0200704 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100705
Yu Zhangfd8cb432017-08-24 20:27:56 +0800706 *max_size = min_t(u64, ~0u, (1ull << va_bits) - la);
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100707 if (size > *max_size)
708 goto bad;
Avi Kivity618ff152011-04-03 12:32:09 +0300709 break;
710 default:
Paolo Bonzini0c1d77f2016-02-19 18:07:21 +0100711 *linear = la = (u32)la;
Avi Kivity1aa36612011-04-27 13:20:30 +0300712 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
713 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300714 if (!usable)
715 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200716 /* code segment in protected mode or read-only data segment */
717 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
718 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300719 goto bad;
720 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400721 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300722 goto bad;
723 lim = desc_limit_scaled(&desc);
Paolo Bonzini997b0412014-11-19 18:33:38 +0100724 if (!(desc.type & 8) && (desc.type & 4)) {
Guo Chaofc058682012-06-28 15:19:51 +0800725 /* expand-down segment */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100726 if (addr.ea <= lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300727 goto bad;
728 lim = desc.d ? 0xffffffff : 0xffff;
Avi Kivity618ff152011-04-03 12:32:09 +0300729 }
Paolo Bonzini997b0412014-11-19 18:33:38 +0100730 if (addr.ea > lim)
731 goto bad;
Nadav Amitbac155312015-01-26 09:32:26 +0200732 if (lim == 0xffffffff)
733 *max_size = ~0u;
734 else {
735 *max_size = (u64)lim + 1 - addr.ea;
736 if (size > *max_size)
737 goto bad;
738 }
Avi Kivity618ff152011-04-03 12:32:09 +0300739 break;
740 }
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100741 if (la & (insn_alignment(ctxt, size) - 1))
Avi Kivity1c11b372012-04-09 18:39:59 +0300742 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300743 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300744bad:
745 if (addr.seg == VCPU_SREG_SS)
Paolo Bonzini36061892014-10-27 14:40:49 +0100746 return emulate_ss(ctxt, 0);
Avi Kivity618ff152011-04-03 12:32:09 +0300747 else
Paolo Bonzini36061892014-10-27 14:40:49 +0100748 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300749}
750
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400751static int linearize(struct x86_emulate_ctxt *ctxt,
752 struct segmented_address addr,
753 unsigned size, bool write,
754 ulong *linear)
755{
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100756 unsigned max_size;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200757 return __linearize(ctxt, addr, &max_size, size, write, false,
758 ctxt->mode, linear);
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400759}
760
Nadav Amitd50eaa12014-11-19 17:43:11 +0200761static inline int assign_eip(struct x86_emulate_ctxt *ctxt, ulong dst,
762 enum x86emul_mode mode)
763{
764 ulong linear;
765 int rc;
766 unsigned max_size;
767 struct segmented_address addr = { .seg = VCPU_SREG_CS,
768 .ea = dst };
769
770 if (ctxt->op_bytes != sizeof(unsigned long))
771 addr.ea = dst & ((1UL << (ctxt->op_bytes << 3)) - 1);
772 rc = __linearize(ctxt, addr, &max_size, 1, false, true, mode, &linear);
773 if (rc == X86EMUL_CONTINUE)
774 ctxt->_eip = addr.ea;
775 return rc;
776}
777
778static inline int assign_eip_near(struct x86_emulate_ctxt *ctxt, ulong dst)
779{
780 return assign_eip(ctxt, dst, ctxt->mode);
781}
782
783static int assign_eip_far(struct x86_emulate_ctxt *ctxt, ulong dst,
784 const struct desc_struct *cs_desc)
785{
786 enum x86emul_mode mode = ctxt->mode;
Nadav Amit82268082015-01-26 09:32:27 +0200787 int rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200788
789#ifdef CONFIG_X86_64
Nadav Amit82268082015-01-26 09:32:27 +0200790 if (ctxt->mode >= X86EMUL_MODE_PROT16) {
791 if (cs_desc->l) {
792 u64 efer = 0;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200793
Nadav Amit82268082015-01-26 09:32:27 +0200794 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
795 if (efer & EFER_LMA)
796 mode = X86EMUL_MODE_PROT64;
797 } else
798 mode = X86EMUL_MODE_PROT32; /* temporary value */
Nadav Amitd50eaa12014-11-19 17:43:11 +0200799 }
800#endif
801 if (mode == X86EMUL_MODE_PROT16 || mode == X86EMUL_MODE_PROT32)
802 mode = cs_desc->d ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
Nadav Amit82268082015-01-26 09:32:27 +0200803 rc = assign_eip(ctxt, dst, mode);
804 if (rc == X86EMUL_CONTINUE)
805 ctxt->mode = mode;
806 return rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200807}
808
809static inline int jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
810{
811 return assign_eip_near(ctxt, ctxt->_eip + rel);
812}
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400813
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200814static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
815 struct segmented_address addr,
816 void *data,
817 unsigned size)
818{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200819 int rc;
820 ulong linear;
821
Avi Kivity83b87952011-04-03 11:31:19 +0300822 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200823 if (rc != X86EMUL_CONTINUE)
824 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300825 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200826}
827
Steve Rutherford129a72a2017-01-11 18:28:29 -0800828static int segmented_write_std(struct x86_emulate_ctxt *ctxt,
829 struct segmented_address addr,
830 void *data,
831 unsigned int size)
832{
833 int rc;
834 ulong linear;
835
836 rc = linearize(ctxt, addr, size, true, &linear);
837 if (rc != X86EMUL_CONTINUE)
838 return rc;
839 return ctxt->ops->write_std(ctxt, linear, data, size, &ctxt->exception);
840}
841
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900842/*
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200843 * Prefetch the remaining bytes of the instruction without crossing page
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900844 * boundary if they are not in fetch_cache yet.
845 */
Paolo Bonzini9506d572014-05-06 13:05:25 +0200846static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
Avi Kivity62266862007-11-20 13:15:52 +0200847{
Avi Kivity62266862007-11-20 13:15:52 +0200848 int rc;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100849 unsigned size, max_size;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200850 unsigned long linear;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200851 int cur_size = ctxt->fetch.end - ctxt->fetch.data;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200852 struct segmented_address addr = { .seg = VCPU_SREG_CS,
Paolo Bonzini17052f12014-05-06 16:33:01 +0200853 .ea = ctxt->eip + cur_size };
854
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100855 /*
856 * We do not know exactly how many bytes will be needed, and
857 * __linearize is expensive, so fetch as much as possible. We
858 * just have to avoid going beyond the 15 byte limit, the end
859 * of the segment, or the end of the page.
860 *
861 * __linearize is called with size 0 so that it does not do any
862 * boundary check itself. Instead, we use max_size to check
863 * against op_size.
864 */
Nadav Amitd50eaa12014-11-19 17:43:11 +0200865 rc = __linearize(ctxt, addr, &max_size, 0, false, true, ctxt->mode,
866 &linear);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200867 if (unlikely(rc != X86EMUL_CONTINUE))
868 return rc;
869
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100870 size = min_t(unsigned, 15UL ^ cur_size, max_size);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200871 size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear));
Paolo Bonzini5cfc7e02014-05-06 13:05:25 +0200872
873 /*
874 * One instruction can only straddle two pages,
875 * and one has been loaded at the beginning of
876 * x86_decode_insn. So, if not enough bytes
877 * still, we must have hit the 15-byte boundary.
878 */
879 if (unlikely(size < op_size))
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100880 return emulate_gp(ctxt, 0);
881
Paolo Bonzini17052f12014-05-06 16:33:01 +0200882 rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end,
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200883 size, &ctxt->exception);
884 if (unlikely(rc != X86EMUL_CONTINUE))
885 return rc;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200886 ctxt->fetch.end += size;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900887 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200888}
889
Paolo Bonzini9506d572014-05-06 13:05:25 +0200890static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt,
891 unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200892{
Nadav Amit08da44a2014-10-03 01:10:04 +0300893 unsigned done_size = ctxt->fetch.end - ctxt->fetch.ptr;
894
895 if (unlikely(done_size < size))
896 return __do_insn_fetch_bytes(ctxt, size - done_size);
Paolo Bonzini9506d572014-05-06 13:05:25 +0200897 else
898 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200899}
900
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900901/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900902#define insn_fetch(_type, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200903({ _type _x; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200904 \
905 rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900906 if (rc != X86EMUL_CONTINUE) \
907 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200908 ctxt->_eip += sizeof(_type); \
Nick Desaulniers8616abc2017-06-27 19:37:14 -0700909 memcpy(&_x, ctxt->fetch.ptr, sizeof(_type)); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200910 ctxt->fetch.ptr += sizeof(_type); \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200911 _x; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900912})
913
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900914#define insn_fetch_arr(_arr, _size, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200915({ \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200916 rc = do_insn_fetch_bytes(_ctxt, _size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900917 if (rc != X86EMUL_CONTINUE) \
918 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200919 ctxt->_eip += (_size); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200920 memcpy(_arr, ctxt->fetch.ptr, _size); \
921 ctxt->fetch.ptr += (_size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900922})
923
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000924/*
925 * Given the 'reg' portion of a ModRM byte, and a register block, return a
926 * pointer into the block that addresses the relevant register.
927 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
928 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300929static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200930 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800931{
932 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200933 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934
Avi Kivity6aa8b732006-12-10 02:21:36 -0800935 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300936 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
937 else
938 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800939 return p;
940}
941
942static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200943 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944 u16 *size, unsigned long *address, int op_bytes)
945{
946 int rc;
947
948 if (op_bytes == 2)
949 op_bytes = 3;
950 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200951 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900952 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800953 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200954 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200955 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800956 return rc;
957}
958
Avi Kivity34b77652013-01-19 19:51:56 +0200959FASTOP2(add);
960FASTOP2(or);
961FASTOP2(adc);
962FASTOP2(sbb);
963FASTOP2(and);
964FASTOP2(sub);
965FASTOP2(xor);
966FASTOP2(cmp);
967FASTOP2(test);
968
Avi Kivityb9fa4092013-02-09 11:31:48 +0200969FASTOP1SRC2(mul, mul_ex);
970FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200971FASTOP1SRC2EX(div, div_ex);
972FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +0200973
Avi Kivity34b77652013-01-19 19:51:56 +0200974FASTOP3WCL(shld);
975FASTOP3WCL(shrd);
976
977FASTOP2W(imul);
978
979FASTOP1(not);
980FASTOP1(neg);
981FASTOP1(inc);
982FASTOP1(dec);
983
984FASTOP2CL(rol);
985FASTOP2CL(ror);
986FASTOP2CL(rcl);
987FASTOP2CL(rcr);
988FASTOP2CL(shl);
989FASTOP2CL(shr);
990FASTOP2CL(sar);
991
992FASTOP2W(bsf);
993FASTOP2W(bsr);
994FASTOP2W(bt);
995FASTOP2W(bts);
996FASTOP2W(btr);
997FASTOP2W(btc);
998
Avi Kivitye47a5f52013-02-09 11:31:51 +0200999FASTOP2(xadd);
1000
Nadav Amit5aca3722014-11-02 11:54:50 +02001001FASTOP2R(cmp, cmp_r);
1002
Nadav Amit900efe22015-03-30 15:39:21 +03001003static int em_bsf_c(struct x86_emulate_ctxt *ctxt)
1004{
1005 /* If src is zero, do not writeback, but update flags */
1006 if (ctxt->src.val == 0)
1007 ctxt->dst.type = OP_NONE;
1008 return fastop(ctxt, em_bsf);
1009}
1010
1011static int em_bsr_c(struct x86_emulate_ctxt *ctxt)
1012{
1013 /* If src is zero, do not writeback, but update flags */
1014 if (ctxt->src.val == 0)
1015 ctxt->dst.type = OP_NONE;
1016 return fastop(ctxt, em_bsr);
1017}
1018
Josh Poimboeufcb7390f2016-01-22 10:16:12 -06001019static __always_inline u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001020{
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001021 u8 rc;
1022 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001023
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001024 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Peter Zijlstra1a29b5b2018-01-25 10:58:13 +01001025 asm("push %[flags]; popf; " CALL_NOSPEC
1026 : "=a"(rc) : [thunk_target]"r"(fop), [flags]"r"(flags));
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001027 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001028}
1029
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001030static void fetch_register_operand(struct operand *op)
1031{
1032 switch (op->bytes) {
1033 case 1:
1034 op->val = *(u8 *)op->addr.reg;
1035 break;
1036 case 2:
1037 op->val = *(u16 *)op->addr.reg;
1038 break;
1039 case 4:
1040 op->val = *(u32 *)op->addr.reg;
1041 break;
1042 case 8:
1043 op->val = *(u64 *)op->addr.reg;
1044 break;
1045 }
1046}
1047
Avi Kivity1253791d2011-03-29 11:41:27 +02001048static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
1049{
1050 ctxt->ops->get_fpu(ctxt);
1051 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001052 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
1053 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
1054 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
1055 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
1056 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
1057 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
1058 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
1059 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001060#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001061 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
1062 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
1063 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
1064 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
1065 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
1066 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
1067 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
1068 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001069#endif
1070 default: BUG();
1071 }
1072 ctxt->ops->put_fpu(ctxt);
1073}
1074
1075static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
1076 int reg)
1077{
1078 ctxt->ops->get_fpu(ctxt);
1079 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001080 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
1081 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
1082 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
1083 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
1084 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
1085 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
1086 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
1087 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001088#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001089 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
1090 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
1091 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
1092 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
1093 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
1094 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
1095 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
1096 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001097#endif
1098 default: BUG();
1099 }
1100 ctxt->ops->put_fpu(ctxt);
1101}
1102
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001103static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1104{
1105 ctxt->ops->get_fpu(ctxt);
1106 switch (reg) {
1107 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1108 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1109 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1110 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1111 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1112 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1113 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1114 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1115 default: BUG();
1116 }
1117 ctxt->ops->put_fpu(ctxt);
1118}
1119
1120static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1121{
1122 ctxt->ops->get_fpu(ctxt);
1123 switch (reg) {
1124 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1125 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1126 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1127 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1128 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1129 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1130 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1131 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1132 default: BUG();
1133 }
1134 ctxt->ops->put_fpu(ctxt);
1135}
1136
Gleb Natapov045a2822012-12-20 16:57:43 +02001137static int em_fninit(struct x86_emulate_ctxt *ctxt)
1138{
1139 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1140 return emulate_nm(ctxt);
1141
1142 ctxt->ops->get_fpu(ctxt);
1143 asm volatile("fninit");
1144 ctxt->ops->put_fpu(ctxt);
1145 return X86EMUL_CONTINUE;
1146}
1147
1148static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1149{
1150 u16 fcw;
1151
1152 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1153 return emulate_nm(ctxt);
1154
1155 ctxt->ops->get_fpu(ctxt);
1156 asm volatile("fnstcw %0": "+m"(fcw));
1157 ctxt->ops->put_fpu(ctxt);
1158
Gleb Natapov045a2822012-12-20 16:57:43 +02001159 ctxt->dst.val = fcw;
1160
1161 return X86EMUL_CONTINUE;
1162}
1163
1164static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1165{
1166 u16 fsw;
1167
1168 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1169 return emulate_nm(ctxt);
1170
1171 ctxt->ops->get_fpu(ctxt);
1172 asm volatile("fnstsw %0": "+m"(fsw));
1173 ctxt->ops->put_fpu(ctxt);
1174
Gleb Natapov045a2822012-12-20 16:57:43 +02001175 ctxt->dst.val = fsw;
1176
1177 return X86EMUL_CONTINUE;
1178}
1179
Avi Kivity1253791d2011-03-29 11:41:27 +02001180static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001181 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001182{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001183 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001184
Avi Kivity9dac77f2011-06-01 15:34:25 +03001185 if (!(ctxt->d & ModRM))
1186 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity1253791d2011-03-29 11:41:27 +02001187
Avi Kivity9dac77f2011-06-01 15:34:25 +03001188 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001189 op->type = OP_XMM;
1190 op->bytes = 16;
1191 op->addr.xmm = reg;
1192 read_sse_reg(ctxt, &op->vec_val, reg);
1193 return;
1194 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001195 if (ctxt->d & Mmx) {
1196 reg &= 7;
1197 op->type = OP_MM;
1198 op->bytes = 8;
1199 op->addr.mm = reg;
1200 return;
1201 }
Avi Kivity1253791d2011-03-29 11:41:27 +02001202
Avi Kivity3c118e22007-10-31 10:27:04 +02001203 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001204 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1205 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1206
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001207 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001208 op->orig_val = op->val;
1209}
1210
Avi Kivitya6e34072012-06-10 17:15:39 +03001211static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1212{
1213 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1214 ctxt->modrm_seg = VCPU_SREG_SS;
1215}
1216
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001217static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001218 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001219{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001220 u8 sib;
Bandan Das02357bd2014-04-16 12:46:11 -04001221 int index_reg, base_reg, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001222 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001223 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001224
Bandan Das02357bd2014-04-16 12:46:11 -04001225 ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */
1226 index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */
1227 base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001228
Bandan Das02357bd2014-04-16 12:46:11 -04001229 ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001230 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
Bandan Das02357bd2014-04-16 12:46:11 -04001231 ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001232 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001233
Nadav Amit9b88ae92014-05-25 23:05:21 +03001234 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001235 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001236 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001237 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001238 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001239 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001240 op->type = OP_XMM;
1241 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001242 op->addr.xmm = ctxt->modrm_rm;
1243 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001244 return rc;
1245 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001246 if (ctxt->d & Mmx) {
1247 op->type = OP_MM;
1248 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001249 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001250 return rc;
1251 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001252 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001253 return rc;
1254 }
1255
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001256 op->type = OP_MEM;
1257
Avi Kivity9dac77f2011-06-01 15:34:25 +03001258 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001259 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1260 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1261 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1262 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001263
1264 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001265 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001266 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001267 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001268 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001269 break;
1270 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001271 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001272 break;
1273 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001274 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001275 break;
1276 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001277 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001278 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001279 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001280 break;
1281 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001282 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001283 break;
1284 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001285 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001286 break;
1287 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001288 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001289 break;
1290 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001291 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001292 break;
1293 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001294 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001295 break;
1296 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001297 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001298 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001299 break;
1300 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001301 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001302 break;
1303 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001304 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1305 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1306 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001307 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001308 } else {
1309 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001310 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001311 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001312 index_reg |= (sib >> 3) & 7;
1313 base_reg |= sib & 7;
1314 scale = sib >> 6;
1315
Avi Kivity9dac77f2011-06-01 15:34:25 +03001316 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001317 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001318 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001319 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001320 adjust_modrm_seg(ctxt, base_reg);
Nadav Amitab708092014-12-25 02:52:21 +02001321 /* Increment ESP on POP [ESP] */
1322 if ((ctxt->d & IncSP) &&
1323 base_reg == VCPU_REGS_RSP)
1324 modrm_ea += ctxt->op_bytes;
Avi Kivitya6e34072012-06-10 17:15:39 +03001325 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001326 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001327 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001328 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Nadav Amit5b38ab82014-11-02 11:54:41 +02001329 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity84411d82008-06-15 21:53:26 -07001330 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001331 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001332 } else {
1333 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001334 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001335 adjust_modrm_seg(ctxt, base_reg);
1336 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001337 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001338 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001339 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001340 break;
1341 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001342 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001343 break;
1344 }
1345 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001346 op->addr.mem.ea = modrm_ea;
Bandan Das41061cd2014-04-16 12:46:14 -04001347 if (ctxt->ad_bytes != 8)
1348 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
1349
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001350done:
1351 return rc;
1352}
1353
1354static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001355 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001356{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001357 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001358
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001359 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001360 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001361 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001362 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001363 break;
1364 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001365 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001366 break;
1367 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001368 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001369 break;
1370 }
1371done:
1372 return rc;
1373}
1374
Avi Kivity9dac77f2011-06-01 15:34:25 +03001375static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001376{
Sheng Yang7129eec2010-09-28 16:33:32 +08001377 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001378
Avi Kivity9dac77f2011-06-01 15:34:25 +03001379 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001380 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001381
Avi Kivity9dac77f2011-06-01 15:34:25 +03001382 if (ctxt->src.bytes == 2)
1383 sv = (s16)ctxt->src.val & (s16)mask;
1384 else if (ctxt->src.bytes == 4)
1385 sv = (s32)ctxt->src.val & (s32)mask;
Nadav Amit7dec5602014-06-15 16:12:57 +03001386 else
1387 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001388
Nadav Amit1c1c35a2014-11-19 17:43:09 +02001389 ctxt->dst.addr.mem.ea = address_mask(ctxt,
1390 ctxt->dst.addr.mem.ea + (sv >> 3));
Wei Yongjun35c843c2010-08-09 11:34:56 +08001391 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001392
1393 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001394 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001395}
1396
Gleb Natapov9de41572010-04-28 19:15:22 +03001397static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001398 unsigned long addr, void *dest, unsigned size)
1399{
1400 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001401 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001402
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001403 if (mc->pos < mc->end)
1404 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001405
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001406 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001407
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001408 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1409 &ctxt->exception);
1410 if (rc != X86EMUL_CONTINUE)
1411 return rc;
1412
1413 mc->end += size;
1414
1415read_cached:
1416 memcpy(dest, mc->data + mc->pos, size);
1417 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001418 return X86EMUL_CONTINUE;
1419}
1420
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001421static int segmented_read(struct x86_emulate_ctxt *ctxt,
1422 struct segmented_address addr,
1423 void *data,
1424 unsigned size)
1425{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001426 int rc;
1427 ulong linear;
1428
Avi Kivity83b87952011-04-03 11:31:19 +03001429 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001430 if (rc != X86EMUL_CONTINUE)
1431 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001432 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001433}
1434
1435static int segmented_write(struct x86_emulate_ctxt *ctxt,
1436 struct segmented_address addr,
1437 const void *data,
1438 unsigned size)
1439{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001440 int rc;
1441 ulong linear;
1442
Avi Kivity83b87952011-04-03 11:31:19 +03001443 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001444 if (rc != X86EMUL_CONTINUE)
1445 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001446 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1447 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001448}
1449
1450static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1451 struct segmented_address addr,
1452 const void *orig_data, const void *data,
1453 unsigned size)
1454{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001455 int rc;
1456 ulong linear;
1457
Avi Kivity83b87952011-04-03 11:31:19 +03001458 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001459 if (rc != X86EMUL_CONTINUE)
1460 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001461 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1462 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001463}
1464
Gleb Natapov7b262e92010-03-18 15:20:27 +02001465static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001466 unsigned int size, unsigned short port,
1467 void *dest)
1468{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001469 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001470
1471 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001472 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001473 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001474 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Nadav Amit0efb0442015-03-29 16:33:03 +03001475 in_page = (ctxt->eflags & X86_EFLAGS_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001476 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1477 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Mark Rustadb55a8142014-07-25 06:27:05 -07001478 n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001479 if (n == 0)
1480 n = 1;
1481 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001482 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001483 return 0;
1484 rc->end = n * size;
1485 }
1486
Nadav Amite6e39f02014-04-18 03:35:10 +03001487 if (ctxt->rep_prefix && (ctxt->d & String) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03001488 !(ctxt->eflags & X86_EFLAGS_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001489 ctxt->dst.data = rc->data + rc->pos;
1490 ctxt->dst.type = OP_MEM_STR;
1491 ctxt->dst.count = (rc->end - rc->pos) / size;
1492 rc->pos = rc->end;
1493 } else {
1494 memcpy(dest, rc->data + rc->pos, size);
1495 rc->pos += size;
1496 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001497 return 1;
1498}
1499
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001500static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1501 u16 index, struct desc_struct *desc)
1502{
1503 struct desc_ptr dt;
1504 ulong addr;
1505
1506 ctxt->ops->get_idt(ctxt, &dt);
1507
1508 if (dt.size < index * 8 + 7)
1509 return emulate_gp(ctxt, index << 3 | 0x2);
1510
1511 addr = dt.address + index * 8;
1512 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1513 &ctxt->exception);
1514}
1515
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001516static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001517 u16 selector, struct desc_ptr *dt)
1518{
Mathias Krause0225fb52012-08-30 01:30:16 +02001519 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001520 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001521
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001522 if (selector & 1 << 2) {
1523 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001524 u16 sel;
1525
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001526 memset (dt, 0, sizeof *dt);
Nadav Amit2eedcac2014-06-02 18:34:05 +03001527 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1528 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001529 return;
1530
1531 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001532 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001533 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001534 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001535}
1536
Nadav Amitedccda72014-12-25 02:52:23 +02001537static int get_descriptor_ptr(struct x86_emulate_ctxt *ctxt,
1538 u16 selector, ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001539{
1540 struct desc_ptr dt;
1541 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001542 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001543
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001544 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001545
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001546 if (dt.size < index * 8 + 7)
1547 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001548
1549 addr = dt.address + index * 8;
Nadav Amitedccda72014-12-25 02:52:23 +02001550
1551#ifdef CONFIG_X86_64
1552 if (addr >> 32 != 0) {
1553 u64 efer = 0;
1554
1555 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1556 if (!(efer & EFER_LMA))
1557 addr &= (u32)-1;
1558 }
1559#endif
1560
1561 *desc_addr_p = addr;
1562 return X86EMUL_CONTINUE;
1563}
1564
1565/* allowed just for 8 bytes segments */
1566static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1567 u16 selector, struct desc_struct *desc,
1568 ulong *desc_addr_p)
1569{
1570 int rc;
1571
1572 rc = get_descriptor_ptr(ctxt, selector, desc_addr_p);
1573 if (rc != X86EMUL_CONTINUE)
1574 return rc;
1575
1576 return ctxt->ops->read_std(ctxt, *desc_addr_p, desc, sizeof(*desc),
1577 &ctxt->exception);
1578}
1579
1580/* allowed just for 8 bytes segments */
1581static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1582 u16 selector, struct desc_struct *desc)
1583{
1584 int rc;
1585 ulong addr;
1586
1587 rc = get_descriptor_ptr(ctxt, selector, &addr);
1588 if (rc != X86EMUL_CONTINUE)
1589 return rc;
1590
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001591 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1592 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001593}
1594
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001595static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Nadav Amitd1442d82014-09-18 22:39:39 +03001596 u16 selector, int seg, u8 cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001597 enum x86_transfer_type transfer,
Nadav Amitd1442d82014-09-18 22:39:39 +03001598 struct desc_struct *desc)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001599{
Avi Kivity869be992012-06-13 16:30:53 +03001600 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001601 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001602 unsigned err_vec = GP_VECTOR;
1603 u32 err_code = 0;
1604 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001605 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001606 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001607 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001608 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001609
1610 memset(&seg_desc, 0, sizeof seg_desc);
1611
Kevin Wolff8da94e2013-04-11 14:06:03 +02001612 if (ctxt->mode == X86EMUL_MODE_REAL) {
1613 /* set real mode segment descriptor (keep limit etc. for
1614 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001615 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001616 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001617 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001618 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1619 /* VM86 needs a clean new segment descriptor */
1620 set_desc_base(&seg_desc, selector << 4);
1621 set_desc_limit(&seg_desc, 0xffff);
1622 seg_desc.type = 3;
1623 seg_desc.p = 1;
1624 seg_desc.s = 1;
1625 seg_desc.dpl = 3;
1626 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001627 }
1628
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001629 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001630
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001631 /* TR should be in GDT only */
1632 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1633 goto exception;
1634
Paolo Bonzini33ab9112017-01-12 15:02:32 +01001635 /* NULL selector is not valid for TR, CS and (except for long mode) SS */
1636 if (null_selector) {
1637 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_TR)
1638 goto exception;
1639
1640 if (seg == VCPU_SREG_SS) {
1641 if (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl)
1642 goto exception;
1643
1644 /*
1645 * ctxt->ops->set_segment expects the CPL to be in
1646 * SS.DPL, so fake an expand-up 32-bit data segment.
1647 */
1648 seg_desc.type = 3;
1649 seg_desc.p = 1;
1650 seg_desc.s = 1;
1651 seg_desc.dpl = cpl;
1652 seg_desc.d = 1;
1653 seg_desc.g = 1;
1654 }
1655
1656 /* Skip all following checks */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001657 goto load;
Paolo Bonzini33ab9112017-01-12 15:02:32 +01001658 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001659
Avi Kivitye9194642012-06-13 16:29:39 +03001660 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001661 if (ret != X86EMUL_CONTINUE)
1662 return ret;
1663
1664 err_code = selector & 0xfffc;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001665 err_vec = (transfer == X86_TRANSFER_TASK_SWITCH) ? TS_VECTOR :
1666 GP_VECTOR;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001667
Guo Chaofc058682012-06-28 15:19:51 +08001668 /* can't load system descriptor into segment selector */
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001669 if (seg <= VCPU_SREG_GS && !seg_desc.s) {
1670 if (transfer == X86_TRANSFER_CALL_JMP)
1671 return X86EMUL_UNHANDLEABLE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001672 goto exception;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001673 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001674
1675 if (!seg_desc.p) {
1676 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1677 goto exception;
1678 }
1679
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001680 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001681
1682 switch (seg) {
1683 case VCPU_SREG_SS:
1684 /*
1685 * segment is not a writable data segment or segment
1686 * selector's RPL != CPL or segment selector's RPL != CPL
1687 */
1688 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1689 goto exception;
1690 break;
1691 case VCPU_SREG_CS:
1692 if (!(seg_desc.type & 8))
1693 goto exception;
1694
1695 if (seg_desc.type & 4) {
1696 /* conforming */
1697 if (dpl > cpl)
1698 goto exception;
1699 } else {
1700 /* nonconforming */
1701 if (rpl > cpl || dpl != cpl)
1702 goto exception;
1703 }
Nadav Amit040c8dc2014-09-18 22:39:43 +03001704 /* in long-mode d/b must be clear if l is set */
1705 if (seg_desc.d && seg_desc.l) {
1706 u64 efer = 0;
1707
1708 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1709 if (efer & EFER_LMA)
1710 goto exception;
1711 }
1712
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001713 /* CS(RPL) <- CPL */
1714 selector = (selector & 0xfffc) | cpl;
1715 break;
1716 case VCPU_SREG_TR:
1717 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1718 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001719 old_desc = seg_desc;
1720 seg_desc.type |= 2; /* busy */
1721 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1722 sizeof(seg_desc), &ctxt->exception);
1723 if (ret != X86EMUL_CONTINUE)
1724 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001725 break;
1726 case VCPU_SREG_LDTR:
1727 if (seg_desc.s || seg_desc.type != 2)
1728 goto exception;
1729 break;
1730 default: /* DS, ES, FS, or GS */
1731 /*
1732 * segment is not a data or readable code segment or
1733 * ((segment is a data or nonconforming code segment)
1734 * and (both RPL and CPL > DPL))
1735 */
1736 if ((seg_desc.type & 0xa) == 0x8 ||
1737 (((seg_desc.type & 0xc) != 0xc) &&
1738 (rpl > dpl && cpl > dpl)))
1739 goto exception;
1740 break;
1741 }
1742
1743 if (seg_desc.s) {
1744 /* mark segment as accessed */
Nadav Amite2cefa72014-12-25 02:52:22 +02001745 if (!(seg_desc.type & 1)) {
1746 seg_desc.type |= 1;
1747 ret = write_segment_descriptor(ctxt, selector,
1748 &seg_desc);
1749 if (ret != X86EMUL_CONTINUE)
1750 return ret;
1751 }
Nadav Amite37a75a2014-06-02 18:34:04 +03001752 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
1753 ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3,
1754 sizeof(base3), &ctxt->exception);
1755 if (ret != X86EMUL_CONTINUE)
1756 return ret;
Yu Zhangfd8cb432017-08-24 20:27:56 +08001757 if (emul_is_noncanonical_address(get_desc_base(&seg_desc) |
1758 ((u64)base3 << 32), ctxt))
Nadav Amit9a9abf62014-11-02 11:54:56 +02001759 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001760 }
1761load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001762 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Nadav Amitd1442d82014-09-18 22:39:39 +03001763 if (desc)
1764 *desc = seg_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001765 return X86EMUL_CONTINUE;
1766exception:
Paolo Bonzini592f0852014-08-20 10:05:08 +02001767 return emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001768}
1769
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001770static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1771 u16 selector, int seg)
1772{
1773 u8 cpl = ctxt->ops->cpl(ctxt);
Paolo Bonzini33ab9112017-01-12 15:02:32 +01001774
1775 /*
1776 * None of MOV, POP and LSS can load a NULL selector in CPL=3, but
1777 * they can load it at CPL<3 (Intel's manual says only LSS can,
1778 * but it's wrong).
1779 *
1780 * However, the Intel manual says that putting IST=1/DPL=3 in
1781 * an interrupt gate will result in SS=3 (the AMD manual instead
1782 * says it doesn't), so allow SS=3 in __load_segment_descriptor
1783 * and only forbid it here.
1784 */
1785 if (seg == VCPU_SREG_SS && selector == 3 &&
1786 ctxt->mode == X86EMUL_MODE_PROT64)
1787 return emulate_exception(ctxt, GP_VECTOR, 0, true);
1788
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001789 return __load_segment_descriptor(ctxt, selector, seg, cpl,
1790 X86_TRANSFER_NONE, NULL);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001791}
1792
Wei Yongjun31be40b2010-08-17 09:17:30 +08001793static void write_register_operand(struct operand *op)
1794{
Nadav Amit6fd8e122015-03-30 15:39:20 +03001795 return assign_register(op->addr.reg, op->val, op->bytes);
Wei Yongjun31be40b2010-08-17 09:17:30 +08001796}
1797
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001798static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001799{
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001800 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001801 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001802 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001803 break;
1804 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001805 if (ctxt->lock_prefix)
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001806 return segmented_cmpxchg(ctxt,
1807 op->addr.mem,
1808 &op->orig_val,
1809 &op->val,
1810 op->bytes);
1811 else
1812 return segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001813 op->addr.mem,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001814 &op->val,
1815 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001816 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001817 case OP_MEM_STR:
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001818 return segmented_write(ctxt,
1819 op->addr.mem,
1820 op->data,
1821 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001822 break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001823 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001824 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001825 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001826 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001827 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001828 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001829 case OP_NONE:
1830 /* no writeback */
1831 break;
1832 default:
1833 break;
1834 }
1835 return X86EMUL_CONTINUE;
1836}
1837
Avi Kivity51ddff52012-06-12 20:19:40 +03001838static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001839{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001840 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001841
Avi Kivity5ad105e2012-08-19 14:34:31 +03001842 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001843 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001844 addr.seg = VCPU_SREG_SS;
1845
Avi Kivity51ddff52012-06-12 20:19:40 +03001846 return segmented_write(ctxt, addr, data, bytes);
1847}
1848
1849static int em_push(struct x86_emulate_ctxt *ctxt)
1850{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001851 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001852 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001853 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001854}
1855
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001856static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001857 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001858{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001859 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001860 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001861
Avi Kivitydd856ef2012-08-27 23:46:17 +03001862 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001863 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001864 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001865 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001866 return rc;
1867
Avi Kivity5ad105e2012-08-19 14:34:31 +03001868 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001869 return rc;
1870}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001871
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001872static int em_pop(struct x86_emulate_ctxt *ctxt)
1873{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001874 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001875}
1876
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001877static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001878 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001879{
1880 int rc;
1881 unsigned long val, change_mask;
Nadav Amit0efb0442015-03-29 16:33:03 +03001882 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001883 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001884
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001885 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001886 if (rc != X86EMUL_CONTINUE)
1887 return rc;
1888
Nadav Amit0efb0442015-03-29 16:33:03 +03001889 change_mask = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
1890 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF |
1891 X86_EFLAGS_TF | X86_EFLAGS_DF | X86_EFLAGS_NT |
1892 X86_EFLAGS_AC | X86_EFLAGS_ID;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001893
1894 switch(ctxt->mode) {
1895 case X86EMUL_MODE_PROT64:
1896 case X86EMUL_MODE_PROT32:
1897 case X86EMUL_MODE_PROT16:
1898 if (cpl == 0)
Nadav Amit0efb0442015-03-29 16:33:03 +03001899 change_mask |= X86_EFLAGS_IOPL;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001900 if (cpl <= iopl)
Nadav Amit0efb0442015-03-29 16:33:03 +03001901 change_mask |= X86_EFLAGS_IF;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001902 break;
1903 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001904 if (iopl < 3)
1905 return emulate_gp(ctxt, 0);
Nadav Amit0efb0442015-03-29 16:33:03 +03001906 change_mask |= X86_EFLAGS_IF;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001907 break;
1908 default: /* real mode */
Nadav Amit0efb0442015-03-29 16:33:03 +03001909 change_mask |= (X86_EFLAGS_IOPL | X86_EFLAGS_IF);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001910 break;
1911 }
1912
1913 *(unsigned long *)dest =
1914 (ctxt->eflags & ~change_mask) | (val & change_mask);
1915
1916 return rc;
1917}
1918
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001919static int em_popf(struct x86_emulate_ctxt *ctxt)
1920{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001921 ctxt->dst.type = OP_REG;
1922 ctxt->dst.addr.reg = &ctxt->eflags;
1923 ctxt->dst.bytes = ctxt->op_bytes;
1924 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001925}
1926
Avi Kivity612e89f2012-06-12 20:03:23 +03001927static int em_enter(struct x86_emulate_ctxt *ctxt)
1928{
1929 int rc;
1930 unsigned frame_size = ctxt->src.val;
1931 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001932 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001933
1934 if (nesting_level)
1935 return X86EMUL_UNHANDLEABLE;
1936
Avi Kivitydd856ef2012-08-27 23:46:17 +03001937 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1938 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001939 if (rc != X86EMUL_CONTINUE)
1940 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001941 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001942 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001943 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1944 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001945 stack_mask(ctxt));
1946 return X86EMUL_CONTINUE;
1947}
1948
Avi Kivityf47cfa32012-06-07 17:49:24 +03001949static int em_leave(struct x86_emulate_ctxt *ctxt)
1950{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001951 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001952 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001953 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001954}
1955
Avi Kivity1cd196e2011-09-13 10:45:51 +03001956static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001957{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001958 int seg = ctxt->src2.val;
1959
Avi Kivity9dac77f2011-06-01 15:34:25 +03001960 ctxt->src.val = get_segment_selector(ctxt, seg);
Nadav Amit0fcc2072014-11-02 11:54:51 +02001961 if (ctxt->op_bytes == 4) {
1962 rsp_increment(ctxt, -2);
1963 ctxt->op_bytes = 2;
1964 }
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001965
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001966 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001967}
1968
Avi Kivity1cd196e2011-09-13 10:45:51 +03001969static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001970{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001971 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001972 unsigned long selector;
1973 int rc;
1974
Nadav Amit3313bc42014-12-25 02:52:17 +02001975 rc = emulate_pop(ctxt, &selector, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001976 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001977 return rc;
1978
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001979 if (ctxt->modrm_reg == VCPU_SREG_SS)
1980 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Nadav Amit3313bc42014-12-25 02:52:17 +02001981 if (ctxt->op_bytes > 2)
1982 rsp_increment(ctxt, ctxt->op_bytes - 2);
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001983
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001984 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001985 return rc;
1986}
1987
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001988static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001989{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001990 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001991 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001992 int reg = VCPU_REGS_RAX;
1993
1994 while (reg <= VCPU_REGS_RDI) {
1995 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001996 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001997
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001998 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001999 if (rc != X86EMUL_CONTINUE)
2000 return rc;
2001
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002002 ++reg;
2003 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08002004
Wei Yongjunc37eda12010-06-15 09:03:33 +08002005 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002006}
2007
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09002008static int em_pushf(struct x86_emulate_ctxt *ctxt)
2009{
Nadav Amit0efb0442015-03-29 16:33:03 +03002010 ctxt->src.val = (unsigned long)ctxt->eflags & ~X86_EFLAGS_VM;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09002011 return em_push(ctxt);
2012}
2013
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09002014static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002015{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002016 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002017 int reg = VCPU_REGS_RDI;
Nadav Amit6fd8e122015-03-30 15:39:20 +03002018 u32 val;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002019
2020 while (reg >= VCPU_REGS_RAX) {
2021 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03002022 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002023 --reg;
2024 }
2025
Nadav Amit6fd8e122015-03-30 15:39:20 +03002026 rc = emulate_pop(ctxt, &val, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002027 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002028 break;
Nadav Amit6fd8e122015-03-30 15:39:20 +03002029 assign_register(reg_rmw(ctxt, reg), val, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002030 --reg;
2031 }
2032 return rc;
2033}
2034
Avi Kivitydd856ef2012-08-27 23:46:17 +03002035static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002036{
Mathias Krause0225fb52012-08-30 01:30:16 +02002037 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03002038 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002039 struct desc_ptr dt;
2040 gva_t cs_addr;
2041 gva_t eip_addr;
2042 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002043
2044 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002045 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002046 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03002047 if (rc != X86EMUL_CONTINUE)
2048 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002049
Nadav Amit0efb0442015-03-29 16:33:03 +03002050 ctxt->eflags &= ~(X86_EFLAGS_IF | X86_EFLAGS_TF | X86_EFLAGS_AC);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002051
Avi Kivity9dac77f2011-06-01 15:34:25 +03002052 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002053 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03002054 if (rc != X86EMUL_CONTINUE)
2055 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002056
Avi Kivity9dac77f2011-06-01 15:34:25 +03002057 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002058 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03002059 if (rc != X86EMUL_CONTINUE)
2060 return rc;
2061
Avi Kivity4bff1e862011-04-20 13:37:53 +03002062 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002063
2064 eip_addr = dt.address + (irq << 2);
2065 cs_addr = dt.address + (irq << 2) + 2;
2066
Avi Kivity0f65dd72011-04-20 13:37:53 +03002067 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002068 if (rc != X86EMUL_CONTINUE)
2069 return rc;
2070
Avi Kivity0f65dd72011-04-20 13:37:53 +03002071 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002072 if (rc != X86EMUL_CONTINUE)
2073 return rc;
2074
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002075 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002076 if (rc != X86EMUL_CONTINUE)
2077 return rc;
2078
Avi Kivity9dac77f2011-06-01 15:34:25 +03002079 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002080
2081 return rc;
2082}
2083
Avi Kivitydd856ef2012-08-27 23:46:17 +03002084int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
2085{
2086 int rc;
2087
2088 invalidate_registers(ctxt);
2089 rc = __emulate_int_real(ctxt, irq);
2090 if (rc == X86EMUL_CONTINUE)
2091 writeback_registers(ctxt);
2092 return rc;
2093}
2094
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002095static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002096{
2097 switch(ctxt->mode) {
2098 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03002099 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002100 case X86EMUL_MODE_VM86:
2101 case X86EMUL_MODE_PROT16:
2102 case X86EMUL_MODE_PROT32:
2103 case X86EMUL_MODE_PROT64:
2104 default:
2105 /* Protected mode interrupts unimplemented yet */
2106 return X86EMUL_UNHANDLEABLE;
2107 }
2108}
2109
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002110static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002111{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002112 int rc = X86EMUL_CONTINUE;
2113 unsigned long temp_eip = 0;
2114 unsigned long temp_eflags = 0;
2115 unsigned long cs = 0;
Nadav Amit0efb0442015-03-29 16:33:03 +03002116 unsigned long mask = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
2117 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_TF |
2118 X86_EFLAGS_IF | X86_EFLAGS_DF | X86_EFLAGS_OF |
2119 X86_EFLAGS_IOPL | X86_EFLAGS_NT | X86_EFLAGS_RF |
2120 X86_EFLAGS_AC | X86_EFLAGS_ID |
Wanpeng Li35fd68a2015-04-08 14:08:14 +08002121 X86_EFLAGS_FIXED;
Nadav Amit0efb0442015-03-29 16:33:03 +03002122 unsigned long vm86_mask = X86_EFLAGS_VM | X86_EFLAGS_VIF |
2123 X86_EFLAGS_VIP;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002124
2125 /* TODO: Add stack limit check */
2126
Avi Kivity9dac77f2011-06-01 15:34:25 +03002127 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002128
2129 if (rc != X86EMUL_CONTINUE)
2130 return rc;
2131
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002132 if (temp_eip & ~0xffff)
2133 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002134
Avi Kivity9dac77f2011-06-01 15:34:25 +03002135 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002136
2137 if (rc != X86EMUL_CONTINUE)
2138 return rc;
2139
Avi Kivity9dac77f2011-06-01 15:34:25 +03002140 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002141
2142 if (rc != X86EMUL_CONTINUE)
2143 return rc;
2144
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002145 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002146
2147 if (rc != X86EMUL_CONTINUE)
2148 return rc;
2149
Avi Kivity9dac77f2011-06-01 15:34:25 +03002150 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002151
Avi Kivity9dac77f2011-06-01 15:34:25 +03002152 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002153 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03002154 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002155 ctxt->eflags &= ~0xffff;
2156 ctxt->eflags |= temp_eflags;
2157 }
2158
2159 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
Wanpeng Li35fd68a2015-04-08 14:08:14 +08002160 ctxt->eflags |= X86_EFLAGS_FIXED;
Nadav Amit801806d2015-01-26 09:32:23 +02002161 ctxt->ops->set_nmi_mask(ctxt, false);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002162
2163 return rc;
2164}
2165
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002166static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002167{
2168 switch(ctxt->mode) {
2169 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002170 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002171 case X86EMUL_MODE_VM86:
2172 case X86EMUL_MODE_PROT16:
2173 case X86EMUL_MODE_PROT32:
2174 case X86EMUL_MODE_PROT64:
2175 default:
2176 /* iret from protected mode unimplemented yet */
2177 return X86EMUL_UNHANDLEABLE;
2178 }
2179}
2180
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002181static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2182{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002183 int rc;
Radim Krčmář2117d532016-11-23 21:15:00 +01002184 unsigned short sel;
2185 struct desc_struct new_desc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002186 u8 cpl = ctxt->ops->cpl(ctxt);
2187
Avi Kivity9dac77f2011-06-01 15:34:25 +03002188 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002189
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002190 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
2191 X86_TRANSFER_CALL_JMP,
Nadav Amitd1442d82014-09-18 22:39:39 +03002192 &new_desc);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002193 if (rc != X86EMUL_CONTINUE)
2194 return rc;
2195
Nadav Amitd50eaa12014-11-19 17:43:11 +02002196 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Radim Krčmář2117d532016-11-23 21:15:00 +01002197 /* Error handling is not implemented. */
2198 if (rc != X86EMUL_CONTINUE)
2199 return X86EMUL_UNHANDLEABLE;
2200
Nadav Amitd1442d82014-09-18 22:39:39 +03002201 return rc;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002202}
2203
Nadav Amitf7784042014-09-18 22:39:41 +03002204static int em_jmp_abs(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002205{
Nadav Amitf7784042014-09-18 22:39:41 +03002206 return assign_eip_near(ctxt, ctxt->src.val);
2207}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002208
Nadav Amitf7784042014-09-18 22:39:41 +03002209static int em_call_near_abs(struct x86_emulate_ctxt *ctxt)
2210{
2211 int rc;
2212 long int old_eip;
2213
2214 old_eip = ctxt->_eip;
2215 rc = assign_eip_near(ctxt, ctxt->src.val);
2216 if (rc != X86EMUL_CONTINUE)
2217 return rc;
2218 ctxt->src.val = old_eip;
2219 rc = em_push(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002220 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002221}
2222
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002223static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002224{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002225 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002226
Nadav Amitaaa05f22014-06-02 18:34:10 +03002227 if (ctxt->dst.bytes == 16)
2228 return X86EMUL_UNHANDLEABLE;
2229
Avi Kivitydd856ef2012-08-27 23:46:17 +03002230 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2231 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2232 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2233 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Nadav Amit0efb0442015-03-29 16:33:03 +03002234 ctxt->eflags &= ~X86_EFLAGS_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002235 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002236 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2237 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002238
Nadav Amit0efb0442015-03-29 16:33:03 +03002239 ctxt->eflags |= X86_EFLAGS_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002240 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002241 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002242}
2243
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002244static int em_ret(struct x86_emulate_ctxt *ctxt)
2245{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002246 int rc;
2247 unsigned long eip;
2248
2249 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
2250 if (rc != X86EMUL_CONTINUE)
2251 return rc;
2252
2253 return assign_eip_near(ctxt, eip);
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002254}
2255
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002256static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002257{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002258 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002259 unsigned long eip, cs;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002260 int cpl = ctxt->ops->cpl(ctxt);
Radim Krčmář2117d532016-11-23 21:15:00 +01002261 struct desc_struct new_desc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002262
2263 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002264 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002265 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002266 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002267 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002268 return rc;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002269 /* Outer-privilege level return is not implemented */
2270 if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2271 return X86EMUL_UNHANDLEABLE;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002272 rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, cpl,
2273 X86_TRANSFER_RET,
Nadav Amitd1442d82014-09-18 22:39:39 +03002274 &new_desc);
2275 if (rc != X86EMUL_CONTINUE)
2276 return rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +02002277 rc = assign_eip_far(ctxt, eip, &new_desc);
Radim Krčmář2117d532016-11-23 21:15:00 +01002278 /* Error handling is not implemented. */
2279 if (rc != X86EMUL_CONTINUE)
2280 return X86EMUL_UNHANDLEABLE;
2281
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002282 return rc;
2283}
2284
Bruce Rogers32611072013-09-09 09:40:20 -06002285static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2286{
2287 int rc;
2288
2289 rc = em_ret_far(ctxt);
2290 if (rc != X86EMUL_CONTINUE)
2291 return rc;
2292 rsp_increment(ctxt, ctxt->src.val);
2293 return X86EMUL_CONTINUE;
2294}
2295
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002296static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2297{
2298 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002299 ctxt->dst.orig_val = ctxt->dst.val;
2300 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002301 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002302 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002303 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002304
Nadav Amit0efb0442015-03-29 16:33:03 +03002305 if (ctxt->eflags & X86_EFLAGS_ZF) {
Nadav Amit2fcf5c82015-01-26 09:32:21 +02002306 /* Success: write back to memory; no update of EAX */
2307 ctxt->src.type = OP_NONE;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002308 ctxt->dst.val = ctxt->src.orig_val;
2309 } else {
2310 /* Failure: write the value we saw to EAX. */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02002311 ctxt->src.type = OP_REG;
2312 ctxt->src.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
2313 ctxt->src.val = ctxt->dst.orig_val;
2314 /* Create write-cycle to dest by writing the same value */
Nadav Amit37c564f2014-06-02 18:34:07 +03002315 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002316 }
2317 return X86EMUL_CONTINUE;
2318}
2319
Avi Kivityd4b43252011-09-13 10:45:50 +03002320static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002321{
Avi Kivityd4b43252011-09-13 10:45:50 +03002322 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002323 unsigned short sel;
2324 int rc;
2325
Avi Kivity9dac77f2011-06-01 15:34:25 +03002326 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002327
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002328 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002329 if (rc != X86EMUL_CONTINUE)
2330 return rc;
2331
Avi Kivity9dac77f2011-06-01 15:34:25 +03002332 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002333 return rc;
2334}
2335
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002336static int emulator_has_longmode(struct x86_emulate_ctxt *ctxt)
2337{
2338 u32 eax, ebx, ecx, edx;
2339
2340 eax = 0x80000001;
2341 ecx = 0;
Yu Zhange911eb32017-08-24 20:27:52 +08002342 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, false);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002343 return edx & bit(X86_FEATURE_LM);
2344}
2345
2346#define GET_SMSTATE(type, smbase, offset) \
2347 ({ \
2348 type __val; \
Radim Krčmářf40606b2015-10-30 16:36:25 +01002349 int r = ctxt->ops->read_phys(ctxt, smbase + offset, &__val, \
2350 sizeof(__val)); \
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002351 if (r != X86EMUL_CONTINUE) \
2352 return X86EMUL_UNHANDLEABLE; \
2353 __val; \
2354 })
2355
2356static void rsm_set_desc_flags(struct desc_struct *desc, u32 flags)
2357{
2358 desc->g = (flags >> 23) & 1;
2359 desc->d = (flags >> 22) & 1;
2360 desc->l = (flags >> 21) & 1;
2361 desc->avl = (flags >> 20) & 1;
2362 desc->p = (flags >> 15) & 1;
2363 desc->dpl = (flags >> 13) & 3;
2364 desc->s = (flags >> 12) & 1;
2365 desc->type = (flags >> 8) & 15;
2366}
2367
2368static int rsm_load_seg_32(struct x86_emulate_ctxt *ctxt, u64 smbase, int n)
2369{
2370 struct desc_struct desc;
2371 int offset;
2372 u16 selector;
2373
2374 selector = GET_SMSTATE(u32, smbase, 0x7fa8 + n * 4);
2375
2376 if (n < 3)
2377 offset = 0x7f84 + n * 12;
2378 else
2379 offset = 0x7f2c + (n - 3) * 12;
2380
2381 set_desc_base(&desc, GET_SMSTATE(u32, smbase, offset + 8));
2382 set_desc_limit(&desc, GET_SMSTATE(u32, smbase, offset + 4));
2383 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smbase, offset));
2384 ctxt->ops->set_segment(ctxt, selector, &desc, 0, n);
2385 return X86EMUL_CONTINUE;
2386}
2387
2388static int rsm_load_seg_64(struct x86_emulate_ctxt *ctxt, u64 smbase, int n)
2389{
2390 struct desc_struct desc;
2391 int offset;
2392 u16 selector;
2393 u32 base3;
2394
2395 offset = 0x7e00 + n * 16;
2396
2397 selector = GET_SMSTATE(u16, smbase, offset);
2398 rsm_set_desc_flags(&desc, GET_SMSTATE(u16, smbase, offset + 2) << 8);
2399 set_desc_limit(&desc, GET_SMSTATE(u32, smbase, offset + 4));
2400 set_desc_base(&desc, GET_SMSTATE(u32, smbase, offset + 8));
2401 base3 = GET_SMSTATE(u32, smbase, offset + 12);
2402
2403 ctxt->ops->set_segment(ctxt, selector, &desc, base3, n);
2404 return X86EMUL_CONTINUE;
2405}
2406
2407static int rsm_enter_protected_mode(struct x86_emulate_ctxt *ctxt,
2408 u64 cr0, u64 cr4)
2409{
2410 int bad;
2411
2412 /*
2413 * First enable PAE, long mode needs it before CR0.PG = 1 is set.
2414 * Then enable protected mode. However, PCID cannot be enabled
2415 * if EFER.LMA=0, so set it separately.
2416 */
2417 bad = ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PCIDE);
2418 if (bad)
2419 return X86EMUL_UNHANDLEABLE;
2420
2421 bad = ctxt->ops->set_cr(ctxt, 0, cr0);
2422 if (bad)
2423 return X86EMUL_UNHANDLEABLE;
2424
2425 if (cr4 & X86_CR4_PCIDE) {
2426 bad = ctxt->ops->set_cr(ctxt, 4, cr4);
2427 if (bad)
2428 return X86EMUL_UNHANDLEABLE;
2429 }
2430
2431 return X86EMUL_CONTINUE;
2432}
2433
2434static int rsm_load_state_32(struct x86_emulate_ctxt *ctxt, u64 smbase)
2435{
2436 struct desc_struct desc;
2437 struct desc_ptr dt;
2438 u16 selector;
2439 u32 val, cr0, cr4;
2440 int i;
2441
2442 cr0 = GET_SMSTATE(u32, smbase, 0x7ffc);
2443 ctxt->ops->set_cr(ctxt, 3, GET_SMSTATE(u32, smbase, 0x7ff8));
2444 ctxt->eflags = GET_SMSTATE(u32, smbase, 0x7ff4) | X86_EFLAGS_FIXED;
2445 ctxt->_eip = GET_SMSTATE(u32, smbase, 0x7ff0);
2446
2447 for (i = 0; i < 8; i++)
2448 *reg_write(ctxt, i) = GET_SMSTATE(u32, smbase, 0x7fd0 + i * 4);
2449
2450 val = GET_SMSTATE(u32, smbase, 0x7fcc);
2451 ctxt->ops->set_dr(ctxt, 6, (val & DR6_VOLATILE) | DR6_FIXED_1);
2452 val = GET_SMSTATE(u32, smbase, 0x7fc8);
2453 ctxt->ops->set_dr(ctxt, 7, (val & DR7_VOLATILE) | DR7_FIXED_1);
2454
2455 selector = GET_SMSTATE(u32, smbase, 0x7fc4);
2456 set_desc_base(&desc, GET_SMSTATE(u32, smbase, 0x7f64));
2457 set_desc_limit(&desc, GET_SMSTATE(u32, smbase, 0x7f60));
2458 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smbase, 0x7f5c));
2459 ctxt->ops->set_segment(ctxt, selector, &desc, 0, VCPU_SREG_TR);
2460
2461 selector = GET_SMSTATE(u32, smbase, 0x7fc0);
2462 set_desc_base(&desc, GET_SMSTATE(u32, smbase, 0x7f80));
2463 set_desc_limit(&desc, GET_SMSTATE(u32, smbase, 0x7f7c));
2464 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smbase, 0x7f78));
2465 ctxt->ops->set_segment(ctxt, selector, &desc, 0, VCPU_SREG_LDTR);
2466
2467 dt.address = GET_SMSTATE(u32, smbase, 0x7f74);
2468 dt.size = GET_SMSTATE(u32, smbase, 0x7f70);
2469 ctxt->ops->set_gdt(ctxt, &dt);
2470
2471 dt.address = GET_SMSTATE(u32, smbase, 0x7f58);
2472 dt.size = GET_SMSTATE(u32, smbase, 0x7f54);
2473 ctxt->ops->set_idt(ctxt, &dt);
2474
2475 for (i = 0; i < 6; i++) {
2476 int r = rsm_load_seg_32(ctxt, smbase, i);
2477 if (r != X86EMUL_CONTINUE)
2478 return r;
2479 }
2480
2481 cr4 = GET_SMSTATE(u32, smbase, 0x7f14);
2482
2483 ctxt->ops->set_smbase(ctxt, GET_SMSTATE(u32, smbase, 0x7ef8));
2484
2485 return rsm_enter_protected_mode(ctxt, cr0, cr4);
2486}
2487
2488static int rsm_load_state_64(struct x86_emulate_ctxt *ctxt, u64 smbase)
2489{
2490 struct desc_struct desc;
2491 struct desc_ptr dt;
2492 u64 val, cr0, cr4;
2493 u32 base3;
2494 u16 selector;
Paolo Bonzinib10d92a2015-10-14 15:25:52 +02002495 int i, r;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002496
2497 for (i = 0; i < 16; i++)
2498 *reg_write(ctxt, i) = GET_SMSTATE(u64, smbase, 0x7ff8 - i * 8);
2499
2500 ctxt->_eip = GET_SMSTATE(u64, smbase, 0x7f78);
2501 ctxt->eflags = GET_SMSTATE(u32, smbase, 0x7f70) | X86_EFLAGS_FIXED;
2502
2503 val = GET_SMSTATE(u32, smbase, 0x7f68);
2504 ctxt->ops->set_dr(ctxt, 6, (val & DR6_VOLATILE) | DR6_FIXED_1);
2505 val = GET_SMSTATE(u32, smbase, 0x7f60);
2506 ctxt->ops->set_dr(ctxt, 7, (val & DR7_VOLATILE) | DR7_FIXED_1);
2507
2508 cr0 = GET_SMSTATE(u64, smbase, 0x7f58);
2509 ctxt->ops->set_cr(ctxt, 3, GET_SMSTATE(u64, smbase, 0x7f50));
2510 cr4 = GET_SMSTATE(u64, smbase, 0x7f48);
2511 ctxt->ops->set_smbase(ctxt, GET_SMSTATE(u32, smbase, 0x7f00));
2512 val = GET_SMSTATE(u64, smbase, 0x7ed0);
2513 ctxt->ops->set_msr(ctxt, MSR_EFER, val & ~EFER_LMA);
2514
2515 selector = GET_SMSTATE(u32, smbase, 0x7e90);
2516 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smbase, 0x7e92) << 8);
2517 set_desc_limit(&desc, GET_SMSTATE(u32, smbase, 0x7e94));
2518 set_desc_base(&desc, GET_SMSTATE(u32, smbase, 0x7e98));
2519 base3 = GET_SMSTATE(u32, smbase, 0x7e9c);
2520 ctxt->ops->set_segment(ctxt, selector, &desc, base3, VCPU_SREG_TR);
2521
2522 dt.size = GET_SMSTATE(u32, smbase, 0x7e84);
2523 dt.address = GET_SMSTATE(u64, smbase, 0x7e88);
2524 ctxt->ops->set_idt(ctxt, &dt);
2525
2526 selector = GET_SMSTATE(u32, smbase, 0x7e70);
2527 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smbase, 0x7e72) << 8);
2528 set_desc_limit(&desc, GET_SMSTATE(u32, smbase, 0x7e74));
2529 set_desc_base(&desc, GET_SMSTATE(u32, smbase, 0x7e78));
2530 base3 = GET_SMSTATE(u32, smbase, 0x7e7c);
2531 ctxt->ops->set_segment(ctxt, selector, &desc, base3, VCPU_SREG_LDTR);
2532
2533 dt.size = GET_SMSTATE(u32, smbase, 0x7e64);
2534 dt.address = GET_SMSTATE(u64, smbase, 0x7e68);
2535 ctxt->ops->set_gdt(ctxt, &dt);
2536
Paolo Bonzinib10d92a2015-10-14 15:25:52 +02002537 r = rsm_enter_protected_mode(ctxt, cr0, cr4);
2538 if (r != X86EMUL_CONTINUE)
2539 return r;
2540
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002541 for (i = 0; i < 6; i++) {
Paolo Bonzinib10d92a2015-10-14 15:25:52 +02002542 r = rsm_load_seg_64(ctxt, smbase, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002543 if (r != X86EMUL_CONTINUE)
2544 return r;
2545 }
2546
Paolo Bonzinib10d92a2015-10-14 15:25:52 +02002547 return X86EMUL_CONTINUE;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002548}
2549
Paolo Bonzini64d60672015-05-07 11:36:11 +02002550static int em_rsm(struct x86_emulate_ctxt *ctxt)
2551{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002552 unsigned long cr0, cr4, efer;
2553 u64 smbase;
2554 int ret;
2555
Ladi Prosek6ed071f2017-04-25 16:42:44 +02002556 if ((ctxt->ops->get_hflags(ctxt) & X86EMUL_SMM_MASK) == 0)
Paolo Bonzini64d60672015-05-07 11:36:11 +02002557 return emulate_ud(ctxt);
2558
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002559 /*
2560 * Get back to real mode, to prepare a safe state in which to load
Paolo Bonzini89651a32015-11-03 13:43:05 +01002561 * CR0/CR3/CR4/EFER. It's all a bit more complicated if the vCPU
2562 * supports long mode.
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002563 */
Paolo Bonzini89651a32015-11-03 13:43:05 +01002564 cr4 = ctxt->ops->get_cr(ctxt, 4);
2565 if (emulator_has_longmode(ctxt)) {
2566 struct desc_struct cs_desc;
2567
2568 /* Zero CR4.PCIDE before CR0.PG. */
2569 if (cr4 & X86_CR4_PCIDE) {
2570 ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PCIDE);
2571 cr4 &= ~X86_CR4_PCIDE;
2572 }
2573
2574 /* A 32-bit code segment is required to clear EFER.LMA. */
2575 memset(&cs_desc, 0, sizeof(cs_desc));
2576 cs_desc.type = 0xb;
2577 cs_desc.s = cs_desc.g = cs_desc.p = 1;
2578 ctxt->ops->set_segment(ctxt, 0, &cs_desc, 0, VCPU_SREG_CS);
2579 }
2580
2581 /* For the 64-bit case, this will clear EFER.LMA. */
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002582 cr0 = ctxt->ops->get_cr(ctxt, 0);
2583 if (cr0 & X86_CR0_PE)
2584 ctxt->ops->set_cr(ctxt, 0, cr0 & ~(X86_CR0_PG | X86_CR0_PE));
Paolo Bonzini89651a32015-11-03 13:43:05 +01002585
2586 /* Now clear CR4.PAE (which must be done before clearing EFER.LME). */
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002587 if (cr4 & X86_CR4_PAE)
2588 ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PAE);
Paolo Bonzini89651a32015-11-03 13:43:05 +01002589
2590 /* And finally go back to 32-bit mode. */
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002591 efer = 0;
2592 ctxt->ops->set_msr(ctxt, MSR_EFER, efer);
2593
2594 smbase = ctxt->ops->get_smbase(ctxt);
2595 if (emulator_has_longmode(ctxt))
2596 ret = rsm_load_state_64(ctxt, smbase + 0x8000);
2597 else
2598 ret = rsm_load_state_32(ctxt, smbase + 0x8000);
2599
2600 if (ret != X86EMUL_CONTINUE) {
2601 /* FIXME: should triple fault */
2602 return X86EMUL_UNHANDLEABLE;
2603 }
2604
Ladi Prosek6ed071f2017-04-25 16:42:44 +02002605 if ((ctxt->ops->get_hflags(ctxt) & X86EMUL_SMM_INSIDE_NMI_MASK) == 0)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002606 ctxt->ops->set_nmi_mask(ctxt, false);
2607
Ladi Prosek6ed071f2017-04-25 16:42:44 +02002608 ctxt->ops->set_hflags(ctxt, ctxt->ops->get_hflags(ctxt) &
2609 ~(X86EMUL_SMM_INSIDE_NMI_MASK | X86EMUL_SMM_MASK));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002610 return X86EMUL_CONTINUE;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002611}
2612
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002613static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002614setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002615 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002616{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002617 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002618 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002619 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002620 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002621 cs->type = 0x0b; /* Read, Execute, Accessed */
2622 cs->s = 1;
2623 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002624 cs->p = 1;
2625 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002626 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002627
Gleb Natapov79168fd2010-04-28 19:15:30 +03002628 set_desc_base(ss, 0); /* flat segment */
2629 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002630 ss->g = 1; /* 4kb granularity */
2631 ss->s = 1;
2632 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002633 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002634 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002635 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002636 ss->l = 0;
2637 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002638}
2639
Avi Kivity1a18a692012-02-01 12:23:21 +02002640static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2641{
2642 u32 eax, ebx, ecx, edx;
2643
2644 eax = ecx = 0;
Yu Zhange911eb32017-08-24 20:27:52 +08002645 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, false);
Avi Kivity0017f932012-06-07 14:10:16 +03002646 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002647 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2648 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2649}
2650
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002651static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2652{
Mathias Krause0225fb52012-08-30 01:30:16 +02002653 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002654 u32 eax, ebx, ecx, edx;
2655
2656 /*
2657 * syscall should always be enabled in longmode - so only become
2658 * vendor specific (cpuid) if other modes are active...
2659 */
2660 if (ctxt->mode == X86EMUL_MODE_PROT64)
2661 return true;
2662
2663 eax = 0x00000000;
2664 ecx = 0x00000000;
Yu Zhange911eb32017-08-24 20:27:52 +08002665 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, false);
Avi Kivity0017f932012-06-07 14:10:16 +03002666 /*
2667 * Intel ("GenuineIntel")
2668 * remark: Intel CPUs only support "syscall" in 64bit
2669 * longmode. Also an 64bit guest with a
2670 * 32bit compat-app running will #UD !! While this
2671 * behaviour can be fixed (by emulating) into AMD
2672 * response - CPUs of AMD can't behave like Intel.
2673 */
2674 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2675 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2676 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2677 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002678
Avi Kivity0017f932012-06-07 14:10:16 +03002679 /* AMD ("AuthenticAMD") */
2680 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2681 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2682 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2683 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002684
Avi Kivity0017f932012-06-07 14:10:16 +03002685 /* AMD ("AMDisbetter!") */
2686 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2687 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2688 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2689 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002690
2691 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2692 return false;
2693}
2694
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002695static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002696{
Mathias Krause0225fb52012-08-30 01:30:16 +02002697 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002698 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002699 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002700 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002701 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002702
2703 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002704 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002705 ctxt->mode == X86EMUL_MODE_VM86)
2706 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002707
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002708 if (!(em_syscall_is_enabled(ctxt)))
2709 return emulate_ud(ctxt);
2710
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002711 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002712 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002713
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002714 if (!(efer & EFER_SCE))
2715 return emulate_ud(ctxt);
2716
Avi Kivity717746e2011-04-20 13:37:53 +03002717 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002718 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002719 cs_sel = (u16)(msr_data & 0xfffc);
2720 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002721
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002722 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002723 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002724 cs.l = 1;
2725 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002726 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2727 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002728
Avi Kivitydd856ef2012-08-27 23:46:17 +03002729 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002730 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002731#ifdef CONFIG_X86_64
Nadav Amit6c6cb692014-07-21 14:37:30 +03002732 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002733
Avi Kivity717746e2011-04-20 13:37:53 +03002734 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002735 ctxt->mode == X86EMUL_MODE_PROT64 ?
2736 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002737 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002738
Avi Kivity717746e2011-04-20 13:37:53 +03002739 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Nadav Amit6c6cb692014-07-21 14:37:30 +03002740 ctxt->eflags &= ~msr_data;
Wanpeng Li35fd68a2015-04-08 14:08:14 +08002741 ctxt->eflags |= X86_EFLAGS_FIXED;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002742#endif
2743 } else {
2744 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002745 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002746 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002747
Nadav Amit0efb0442015-03-29 16:33:03 +03002748 ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002749 }
2750
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02002751 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002752 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002753}
2754
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002755static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002756{
Mathias Krause0225fb52012-08-30 01:30:16 +02002757 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002758 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002759 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002760 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002761 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002762
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002763 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002764 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002765 if (ctxt->mode == X86EMUL_MODE_REAL)
2766 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002767
Avi Kivity1a18a692012-02-01 12:23:21 +02002768 /*
2769 * Not recognized on AMD in compat mode (but is recognized in legacy
2770 * mode).
2771 */
Nadav Amitf3747372015-01-01 23:11:11 +02002772 if ((ctxt->mode != X86EMUL_MODE_PROT64) && (efer & EFER_LMA)
Avi Kivity1a18a692012-02-01 12:23:21 +02002773 && !vendor_intel(ctxt))
2774 return emulate_ud(ctxt);
2775
Nadav Amitb2c9d432014-11-02 11:55:01 +02002776 /* sysenter/sysexit have not been tested in 64bit mode. */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002777 if (ctxt->mode == X86EMUL_MODE_PROT64)
Nadav Amitb2c9d432014-11-02 11:55:01 +02002778 return X86EMUL_UNHANDLEABLE;
Andre Przywara8c604352009-06-18 12:56:01 +02002779
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002780 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002781
Avi Kivity717746e2011-04-20 13:37:53 +03002782 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002783 if ((msr_data & 0xfffc) == 0x0)
2784 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002785
Nadav Amit0efb0442015-03-29 16:33:03 +03002786 ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
Nadav Amitb32a9912015-03-29 16:33:04 +03002787 cs_sel = (u16)msr_data & ~SEGMENT_RPL_MASK;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002788 ss_sel = cs_sel + 8;
Nadav Amitf3747372015-01-01 23:11:11 +02002789 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002790 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002791 cs.l = 1;
2792 }
2793
Avi Kivity1aa36612011-04-27 13:20:30 +03002794 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2795 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002796
Avi Kivity717746e2011-04-20 13:37:53 +03002797 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002798 ctxt->_eip = (efer & EFER_LMA) ? msr_data : (u32)msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002799
Avi Kivity717746e2011-04-20 13:37:53 +03002800 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002801 *reg_write(ctxt, VCPU_REGS_RSP) = (efer & EFER_LMA) ? msr_data :
2802 (u32)msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002803
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002804 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002805}
2806
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002807static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002808{
Mathias Krause0225fb52012-08-30 01:30:16 +02002809 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002810 struct desc_struct cs, ss;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002811 u64 msr_data, rcx, rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002812 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002813 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002814
Gleb Natapova0044752010-02-10 14:21:31 +02002815 /* inject #GP if in real mode or Virtual 8086 mode */
2816 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002817 ctxt->mode == X86EMUL_MODE_VM86)
2818 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002819
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002820 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002821
Avi Kivity9dac77f2011-06-01 15:34:25 +03002822 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002823 usermode = X86EMUL_MODE_PROT64;
2824 else
2825 usermode = X86EMUL_MODE_PROT32;
2826
Nadav Amit234f3ce2014-09-18 22:39:38 +03002827 rcx = reg_read(ctxt, VCPU_REGS_RCX);
2828 rdx = reg_read(ctxt, VCPU_REGS_RDX);
2829
Andre Przywara4668f052009-06-18 12:56:02 +02002830 cs.dpl = 3;
2831 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002832 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002833 switch (usermode) {
2834 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002835 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002836 if ((msr_data & 0xfffc) == 0x0)
2837 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002838 ss_sel = (u16)(msr_data + 24);
Nadav Amitbf0b6822014-09-18 22:39:45 +03002839 rcx = (u32)rcx;
2840 rdx = (u32)rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002841 break;
2842 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002843 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002844 if (msr_data == 0x0)
2845 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002846 ss_sel = cs_sel + 8;
2847 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002848 cs.l = 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08002849 if (emul_is_noncanonical_address(rcx, ctxt) ||
2850 emul_is_noncanonical_address(rdx, ctxt))
Nadav Amit234f3ce2014-09-18 22:39:38 +03002851 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002852 break;
2853 }
Nadav Amitb32a9912015-03-29 16:33:04 +03002854 cs_sel |= SEGMENT_RPL_MASK;
2855 ss_sel |= SEGMENT_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002856
Avi Kivity1aa36612011-04-27 13:20:30 +03002857 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2858 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002859
Nadav Amit234f3ce2014-09-18 22:39:38 +03002860 ctxt->_eip = rdx;
2861 *reg_write(ctxt, VCPU_REGS_RSP) = rcx;
Andre Przywara4668f052009-06-18 12:56:02 +02002862
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002863 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002864}
2865
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002866static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002867{
2868 int iopl;
2869 if (ctxt->mode == X86EMUL_MODE_REAL)
2870 return false;
2871 if (ctxt->mode == X86EMUL_MODE_VM86)
2872 return true;
Nadav Amit0efb0442015-03-29 16:33:03 +03002873 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002874 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002875}
2876
2877static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002878 u16 port, u16 len)
2879{
Mathias Krause0225fb52012-08-30 01:30:16 +02002880 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002881 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002882 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002883 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002884 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002885 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002886 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002887
Avi Kivity1aa36612011-04-27 13:20:30 +03002888 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002889 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002890 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002891 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002892 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002893 base = get_desc_base(&tr_seg);
2894#ifdef CONFIG_X86_64
2895 base |= ((u64)base3) << 32;
2896#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002897 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002898 if (r != X86EMUL_CONTINUE)
2899 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002900 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002901 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002902 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002903 if (r != X86EMUL_CONTINUE)
2904 return false;
2905 if ((perm >> bit_idx) & mask)
2906 return false;
2907 return true;
2908}
2909
2910static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002911 u16 port, u16 len)
2912{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002913 if (ctxt->perm_ok)
2914 return true;
2915
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002916 if (emulator_bad_iopl(ctxt))
2917 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002918 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002919
2920 ctxt->perm_ok = true;
2921
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002922 return true;
2923}
2924
Nadav Amit428e3d02015-04-28 13:06:01 +03002925static void string_registers_quirk(struct x86_emulate_ctxt *ctxt)
2926{
2927 /*
2928 * Intel CPUs mask the counter and pointers in quite strange
2929 * manner when ECX is zero due to REP-string optimizations.
2930 */
2931#ifdef CONFIG_X86_64
2932 if (ctxt->ad_bytes != 4 || !vendor_intel(ctxt))
2933 return;
2934
2935 *reg_write(ctxt, VCPU_REGS_RCX) = 0;
2936
2937 switch (ctxt->b) {
2938 case 0xa4: /* movsb */
2939 case 0xa5: /* movsd/w */
2940 *reg_rmw(ctxt, VCPU_REGS_RSI) &= (u32)-1;
2941 /* fall through */
2942 case 0xaa: /* stosb */
2943 case 0xab: /* stosd/w */
2944 *reg_rmw(ctxt, VCPU_REGS_RDI) &= (u32)-1;
2945 }
2946#endif
2947}
2948
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002949static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002950 struct tss_segment_16 *tss)
2951{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002952 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002953 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002954 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2955 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2956 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2957 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2958 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2959 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2960 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2961 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002962
Avi Kivity1aa36612011-04-27 13:20:30 +03002963 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2964 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2965 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2966 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2967 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002968}
2969
2970static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002971 struct tss_segment_16 *tss)
2972{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002973 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002974 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002975
Avi Kivity9dac77f2011-06-01 15:34:25 +03002976 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002977 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002978 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2979 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2980 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2981 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2982 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2983 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2984 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2985 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002986
2987 /*
2988 * SDM says that segment selectors are loaded before segment
2989 * descriptors
2990 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002991 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2992 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2993 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2994 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2995 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002996
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002997 cpl = tss->cs & 3;
2998
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002999 /*
Guo Chaofc058682012-06-28 15:19:51 +08003000 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003001 * it is handled in a context of new task
3002 */
Nadav Amitd1442d82014-09-18 22:39:39 +03003003 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003004 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003005 if (ret != X86EMUL_CONTINUE)
3006 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003007 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003008 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003009 if (ret != X86EMUL_CONTINUE)
3010 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003011 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003012 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003013 if (ret != X86EMUL_CONTINUE)
3014 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003015 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003016 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003017 if (ret != X86EMUL_CONTINUE)
3018 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003019 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003020 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003021 if (ret != X86EMUL_CONTINUE)
3022 return ret;
3023
3024 return X86EMUL_CONTINUE;
3025}
3026
3027static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003028 u16 tss_selector, u16 old_tss_sel,
3029 ulong old_tss_base, struct desc_struct *new_desc)
3030{
Mathias Krause0225fb52012-08-30 01:30:16 +02003031 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003032 struct tss_segment_16 tss_seg;
3033 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003034 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003035
Avi Kivity0f65dd72011-04-20 13:37:53 +03003036 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003037 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02003038 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003039 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003040
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003041 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003042
Avi Kivity0f65dd72011-04-20 13:37:53 +03003043 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003044 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02003045 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003046 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003047
Avi Kivity0f65dd72011-04-20 13:37:53 +03003048 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003049 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02003050 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003051 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003052
3053 if (old_tss_sel != 0xffff) {
3054 tss_seg.prev_task_link = old_tss_sel;
3055
Avi Kivity0f65dd72011-04-20 13:37:53 +03003056 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003057 &tss_seg.prev_task_link,
3058 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03003059 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02003060 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003061 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003062 }
3063
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003064 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003065}
3066
3067static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003068 struct tss_segment_32 *tss)
3069{
Nadav Amit5c7411e2014-04-07 18:37:47 +03003070 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003071 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003072 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003073 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
3074 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
3075 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
3076 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
3077 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
3078 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
3079 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
3080 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003081
Avi Kivity1aa36612011-04-27 13:20:30 +03003082 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
3083 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
3084 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
3085 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
3086 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
3087 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003088}
3089
3090static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003091 struct tss_segment_32 *tss)
3092{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003093 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003094 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003095
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003096 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003097 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003098 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003099 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01003100
3101 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03003102 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
3103 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
3104 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
3105 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
3106 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
3107 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
3108 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
3109 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003110
3111 /*
3112 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003113 * descriptors. This is important because CPL checks will
3114 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003115 */
Avi Kivity1aa36612011-04-27 13:20:30 +03003116 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
3117 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
3118 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
3119 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
3120 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
3121 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
3122 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003123
3124 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01003125 * If we're switching between Protected Mode and VM86, we need to make
3126 * sure to update the mode before loading the segment descriptors so
3127 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01003128 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003129 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01003130 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003131 cpl = 3;
3132 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01003133 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003134 cpl = tss->cs & 3;
3135 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01003136
3137 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003138 * Now load segment descriptors. If fault happenes at this stage
3139 * it is handled in a context of new task
3140 */
Nadav Amitd1442d82014-09-18 22:39:39 +03003141 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003142 cpl, X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003143 if (ret != X86EMUL_CONTINUE)
3144 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003145 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003146 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003147 if (ret != X86EMUL_CONTINUE)
3148 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003149 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003150 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003151 if (ret != X86EMUL_CONTINUE)
3152 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003153 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003154 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003155 if (ret != X86EMUL_CONTINUE)
3156 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003157 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003158 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003159 if (ret != X86EMUL_CONTINUE)
3160 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003161 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003162 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003163 if (ret != X86EMUL_CONTINUE)
3164 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003165 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003166 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003167
Eugene Korenevsky2f729b12015-03-29 01:27:17 +03003168 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003169}
3170
3171static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003172 u16 tss_selector, u16 old_tss_sel,
3173 ulong old_tss_base, struct desc_struct *new_desc)
3174{
Mathias Krause0225fb52012-08-30 01:30:16 +02003175 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003176 struct tss_segment_32 tss_seg;
3177 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003178 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03003179 u32 eip_offset = offsetof(struct tss_segment_32, eip);
3180 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003181
Avi Kivity0f65dd72011-04-20 13:37:53 +03003182 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003183 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02003184 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003185 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003186
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003187 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003188
Nadav Amit5c7411e2014-04-07 18:37:47 +03003189 /* Only GP registers and segment selectors are saved */
3190 ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
3191 ldt_sel_offset - eip_offset, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02003192 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003193 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003194
Avi Kivity0f65dd72011-04-20 13:37:53 +03003195 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003196 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02003197 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003198 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003199
3200 if (old_tss_sel != 0xffff) {
3201 tss_seg.prev_task_link = old_tss_sel;
3202
Avi Kivity0f65dd72011-04-20 13:37:53 +03003203 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003204 &tss_seg.prev_task_link,
3205 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03003206 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02003207 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003208 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003209 }
3210
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003211 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003212}
3213
3214static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003215 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003216 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003217{
Mathias Krause0225fb52012-08-30 01:30:16 +02003218 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003219 struct desc_struct curr_tss_desc, next_tss_desc;
3220 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03003221 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003222 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03003223 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02003224 u32 desc_limit;
Nadav Amit3db176d2015-04-19 21:12:59 +03003225 ulong desc_addr, dr7;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003226
3227 /* FIXME: old_tss_base == ~0 ? */
3228
Avi Kivitye9194642012-06-13 16:29:39 +03003229 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003230 if (ret != X86EMUL_CONTINUE)
3231 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03003232 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003233 if (ret != X86EMUL_CONTINUE)
3234 return ret;
3235
3236 /* FIXME: check that next_tss_desc is tss */
3237
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003238 /*
3239 * Check privileges. The three cases are task switch caused by...
3240 *
3241 * 1. jmp/call/int to task gate: Check against DPL of the task gate
3242 * 2. Exception/IRQ/iret: No check is performed
Nadav Amit2c2ca2d2014-11-02 11:54:57 +02003243 * 3. jmp/call to TSS/task-gate: No check is performed since the
3244 * hardware checks it before exiting.
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003245 */
3246 if (reason == TASK_SWITCH_GATE) {
3247 if (idt_index != -1) {
3248 /* Software interrupts */
3249 struct desc_struct task_gate_desc;
3250 int dpl;
3251
3252 ret = read_interrupt_descriptor(ctxt, idt_index,
3253 &task_gate_desc);
3254 if (ret != X86EMUL_CONTINUE)
3255 return ret;
3256
3257 dpl = task_gate_desc.dpl;
3258 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
3259 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
3260 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003261 }
3262
Gleb Natapovceffb452010-03-18 15:20:19 +02003263 desc_limit = desc_limit_scaled(&next_tss_desc);
3264 if (!next_tss_desc.p ||
3265 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
3266 desc_limit < 0x2b)) {
Paolo Bonzini592f0852014-08-20 10:05:08 +02003267 return emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003268 }
3269
3270 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
3271 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003272 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003273 }
3274
3275 if (reason == TASK_SWITCH_IRET)
3276 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
3277
3278 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08003279 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003280 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
3281 old_tss_sel = 0xffff;
3282
3283 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003284 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003285 old_tss_base, &next_tss_desc);
3286 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003287 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003288 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02003289 if (ret != X86EMUL_CONTINUE)
3290 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003291
3292 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
3293 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
3294
3295 if (reason != TASK_SWITCH_IRET) {
3296 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003297 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003298 }
3299
Avi Kivity717746e2011-04-20 13:37:53 +03003300 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03003301 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003302
Jan Kiszkae269fb22010-04-14 15:51:09 +02003303 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003304 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
3305 ctxt->lock_prefix = 0;
3306 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003307 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02003308 }
3309
Nadav Amit3db176d2015-04-19 21:12:59 +03003310 ops->get_dr(ctxt, 7, &dr7);
3311 ops->set_dr(ctxt, 7, dr7 & ~(DR_LOCAL_ENABLE_MASK | DR_LOCAL_SLOWDOWN));
3312
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003313 return ret;
3314}
3315
3316int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003317 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003318 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003319{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003320 int rc;
3321
Avi Kivitydd856ef2012-08-27 23:46:17 +03003322 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003323 ctxt->_eip = ctxt->eip;
3324 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003325
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003326 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003327 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003328
Avi Kivitydd856ef2012-08-27 23:46:17 +03003329 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003330 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003331 writeback_registers(ctxt);
3332 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003333
Gleb Natapova0c0ab22011-03-28 16:57:49 +02003334 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003335}
3336
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03003337static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
3338 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02003339{
Nadav Amit0efb0442015-03-29 16:33:03 +03003340 int df = (ctxt->eflags & X86_EFLAGS_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02003341
Paolo Bonzini01485a22014-11-19 18:25:08 +01003342 register_address_increment(ctxt, reg, df * op->bytes);
3343 op->addr.mem.ea = register_address(ctxt, reg);
Gleb Natapova682e352010-03-18 15:20:21 +02003344}
3345
Avi Kivity7af04fc2010-08-18 14:16:35 +03003346static int em_das(struct x86_emulate_ctxt *ctxt)
3347{
Avi Kivity7af04fc2010-08-18 14:16:35 +03003348 u8 al, old_al;
3349 bool af, cf, old_cf;
3350
3351 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003352 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03003353
3354 old_al = al;
3355 old_cf = cf;
3356 cf = false;
3357 af = ctxt->eflags & X86_EFLAGS_AF;
3358 if ((al & 0x0f) > 9 || af) {
3359 al -= 6;
3360 cf = old_cf | (al >= 250);
3361 af = true;
3362 } else {
3363 af = false;
3364 }
3365 if (old_al > 0x99 || old_cf) {
3366 al -= 0x60;
3367 cf = true;
3368 }
3369
Avi Kivity9dac77f2011-06-01 15:34:25 +03003370 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03003371 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003372 ctxt->src.type = OP_IMM;
3373 ctxt->src.val = 0;
3374 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02003375 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03003376 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
3377 if (cf)
3378 ctxt->eflags |= X86_EFLAGS_CF;
3379 if (af)
3380 ctxt->eflags |= X86_EFLAGS_AF;
3381 return X86EMUL_CONTINUE;
3382}
3383
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003384static int em_aam(struct x86_emulate_ctxt *ctxt)
3385{
3386 u8 al, ah;
3387
3388 if (ctxt->src.val == 0)
3389 return emulate_de(ctxt);
3390
3391 al = ctxt->dst.val & 0xff;
3392 ah = al / ctxt->src.val;
3393 al %= ctxt->src.val;
3394
3395 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
3396
3397 /* Set PF, ZF, SF */
3398 ctxt->src.type = OP_IMM;
3399 ctxt->src.val = 0;
3400 ctxt->src.bytes = 1;
3401 fastop(ctxt, em_or);
3402
3403 return X86EMUL_CONTINUE;
3404}
3405
Gleb Natapov7f662272012-12-10 11:42:30 +02003406static int em_aad(struct x86_emulate_ctxt *ctxt)
3407{
3408 u8 al = ctxt->dst.val & 0xff;
3409 u8 ah = (ctxt->dst.val >> 8) & 0xff;
3410
3411 al = (al + (ah * ctxt->src.val)) & 0xff;
3412
3413 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
3414
Gleb Natapovf583c292013-02-13 17:50:39 +02003415 /* Set PF, ZF, SF */
3416 ctxt->src.type = OP_IMM;
3417 ctxt->src.val = 0;
3418 ctxt->src.bytes = 1;
3419 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02003420
3421 return X86EMUL_CONTINUE;
3422}
3423
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003424static int em_call(struct x86_emulate_ctxt *ctxt)
3425{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003426 int rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003427 long rel = ctxt->src.val;
3428
3429 ctxt->src.val = (unsigned long)ctxt->_eip;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003430 rc = jmp_rel(ctxt, rel);
3431 if (rc != X86EMUL_CONTINUE)
3432 return rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003433 return em_push(ctxt);
3434}
3435
Avi Kivity0ef753b2010-08-18 14:51:45 +03003436static int em_call_far(struct x86_emulate_ctxt *ctxt)
3437{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003438 u16 sel, old_cs;
3439 ulong old_eip;
3440 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03003441 struct desc_struct old_desc, new_desc;
3442 const struct x86_emulate_ops *ops = ctxt->ops;
3443 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amit82268082015-01-26 09:32:27 +02003444 enum x86emul_mode prev_mode = ctxt->mode;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003445
Avi Kivity9dac77f2011-06-01 15:34:25 +03003446 old_eip = ctxt->_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003447 ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003448
Avi Kivity9dac77f2011-06-01 15:34:25 +03003449 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003450 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
3451 X86_TRANSFER_CALL_JMP, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003452 if (rc != X86EMUL_CONTINUE)
Nadav Amit80976db2014-12-25 02:52:20 +02003453 return rc;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003454
Nadav Amitd50eaa12014-11-19 17:43:11 +02003455 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003456 if (rc != X86EMUL_CONTINUE)
3457 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003458
Avi Kivity9dac77f2011-06-01 15:34:25 +03003459 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003460 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003461 if (rc != X86EMUL_CONTINUE)
Nadav Amitd1442d82014-09-18 22:39:39 +03003462 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003463
Avi Kivity9dac77f2011-06-01 15:34:25 +03003464 ctxt->src.val = old_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003465 rc = em_push(ctxt);
3466 /* If we failed, we tainted the memory, but the very least we should
3467 restore cs */
Nadav Amit82268082015-01-26 09:32:27 +02003468 if (rc != X86EMUL_CONTINUE) {
3469 pr_warn_once("faulting far call emulation tainted memory\n");
Nadav Amitd1442d82014-09-18 22:39:39 +03003470 goto fail;
Nadav Amit82268082015-01-26 09:32:27 +02003471 }
Nadav Amitd1442d82014-09-18 22:39:39 +03003472 return rc;
3473fail:
3474 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
Nadav Amit82268082015-01-26 09:32:27 +02003475 ctxt->mode = prev_mode;
Nadav Amitd1442d82014-09-18 22:39:39 +03003476 return rc;
3477
Avi Kivity0ef753b2010-08-18 14:51:45 +03003478}
3479
Avi Kivity40ece7c2010-08-18 15:12:09 +03003480static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3481{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003482 int rc;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003483 unsigned long eip;
Avi Kivity40ece7c2010-08-18 15:12:09 +03003484
Nadav Amit234f3ce2014-09-18 22:39:38 +03003485 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
3486 if (rc != X86EMUL_CONTINUE)
3487 return rc;
3488 rc = assign_eip_near(ctxt, eip);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003489 if (rc != X86EMUL_CONTINUE)
3490 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003491 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003492 return X86EMUL_CONTINUE;
3493}
3494
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003495static int em_xchg(struct x86_emulate_ctxt *ctxt)
3496{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003497 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003498 ctxt->src.val = ctxt->dst.val;
3499 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003500
3501 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003502 ctxt->dst.val = ctxt->src.orig_val;
3503 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003504 return X86EMUL_CONTINUE;
3505}
3506
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003507static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3508{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003509 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003510 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003511}
3512
Avi Kivity61429142010-08-19 15:13:00 +03003513static int em_cwd(struct x86_emulate_ctxt *ctxt)
3514{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003515 ctxt->dst.type = OP_REG;
3516 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003517 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003518 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003519
3520 return X86EMUL_CONTINUE;
3521}
3522
Avi Kivity48bb5d32010-08-18 18:54:34 +03003523static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3524{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003525 u64 tsc = 0;
3526
Avi Kivity717746e2011-04-20 13:37:53 +03003527 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003528 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3529 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003530 return X86EMUL_CONTINUE;
3531}
3532
Avi Kivity222d21a2011-11-10 14:57:30 +02003533static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3534{
3535 u64 pmc;
3536
Avi Kivitydd856ef2012-08-27 23:46:17 +03003537 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003538 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003539 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3540 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003541 return X86EMUL_CONTINUE;
3542}
3543
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003544static int em_mov(struct x86_emulate_ctxt *ctxt)
3545{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01003546 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003547 return X86EMUL_CONTINUE;
3548}
3549
Borislav Petkov84cffe42013-10-29 12:54:56 +01003550#define FFL(x) bit(X86_FEATURE_##x)
3551
3552static int em_movbe(struct x86_emulate_ctxt *ctxt)
3553{
3554 u32 ebx, ecx, edx, eax = 1;
3555 u16 tmp;
3556
3557 /*
3558 * Check MOVBE is set in the guest-visible CPUID leaf.
3559 */
Yu Zhange911eb32017-08-24 20:27:52 +08003560 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, false);
Borislav Petkov84cffe42013-10-29 12:54:56 +01003561 if (!(ecx & FFL(MOVBE)))
3562 return emulate_ud(ctxt);
3563
3564 switch (ctxt->op_bytes) {
3565 case 2:
3566 /*
3567 * From MOVBE definition: "...When the operand size is 16 bits,
3568 * the upper word of the destination register remains unchanged
3569 * ..."
3570 *
3571 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3572 * rules so we have to do the operation almost per hand.
3573 */
3574 tmp = (u16)ctxt->src.val;
3575 ctxt->dst.val &= ~0xffffUL;
3576 ctxt->dst.val |= (unsigned long)swab16(tmp);
3577 break;
3578 case 4:
3579 ctxt->dst.val = swab32((u32)ctxt->src.val);
3580 break;
3581 case 8:
3582 ctxt->dst.val = swab64(ctxt->src.val);
3583 break;
3584 default:
Paolo Bonzini592f0852014-08-20 10:05:08 +02003585 BUG();
Borislav Petkov84cffe42013-10-29 12:54:56 +01003586 }
3587 return X86EMUL_CONTINUE;
3588}
3589
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003590static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3591{
3592 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3593 return emulate_gp(ctxt, 0);
3594
3595 /* Disable writeback. */
3596 ctxt->dst.type = OP_NONE;
3597 return X86EMUL_CONTINUE;
3598}
3599
3600static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3601{
3602 unsigned long val;
3603
3604 if (ctxt->mode == X86EMUL_MODE_PROT64)
3605 val = ctxt->src.val & ~0ULL;
3606 else
3607 val = ctxt->src.val & ~0U;
3608
3609 /* #UD condition is already handled. */
3610 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3611 return emulate_gp(ctxt, 0);
3612
3613 /* Disable writeback. */
3614 ctxt->dst.type = OP_NONE;
3615 return X86EMUL_CONTINUE;
3616}
3617
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003618static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3619{
3620 u64 msr_data;
3621
Avi Kivitydd856ef2012-08-27 23:46:17 +03003622 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3623 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3624 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003625 return emulate_gp(ctxt, 0);
3626
3627 return X86EMUL_CONTINUE;
3628}
3629
3630static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3631{
3632 u64 msr_data;
3633
Avi Kivitydd856ef2012-08-27 23:46:17 +03003634 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003635 return emulate_gp(ctxt, 0);
3636
Avi Kivitydd856ef2012-08-27 23:46:17 +03003637 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3638 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003639 return X86EMUL_CONTINUE;
3640}
3641
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003642static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3643{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003644 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003645 return emulate_ud(ctxt);
3646
Avi Kivity9dac77f2011-06-01 15:34:25 +03003647 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Nadav Amitb5bbf102014-11-02 11:54:46 +02003648 if (ctxt->dst.bytes == 4 && ctxt->dst.type == OP_MEM)
3649 ctxt->dst.bytes = 2;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003650 return X86EMUL_CONTINUE;
3651}
3652
3653static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3654{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003655 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003656
Avi Kivity9dac77f2011-06-01 15:34:25 +03003657 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003658 return emulate_ud(ctxt);
3659
Avi Kivity9dac77f2011-06-01 15:34:25 +03003660 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003661 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3662
3663 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003664 ctxt->dst.type = OP_NONE;
3665 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003666}
3667
Avi Kivitya14e5792012-06-13 12:28:33 +03003668static int em_lldt(struct x86_emulate_ctxt *ctxt)
3669{
3670 u16 sel = ctxt->src.val;
3671
3672 /* Disable writeback. */
3673 ctxt->dst.type = OP_NONE;
3674 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3675}
3676
Avi Kivity80890002012-06-13 16:33:29 +03003677static int em_ltr(struct x86_emulate_ctxt *ctxt)
3678{
3679 u16 sel = ctxt->src.val;
3680
3681 /* Disable writeback. */
3682 ctxt->dst.type = OP_NONE;
3683 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3684}
3685
Avi Kivity38503912011-03-31 18:48:09 +02003686static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3687{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003688 int rc;
3689 ulong linear;
3690
Avi Kivity9dac77f2011-06-01 15:34:25 +03003691 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003692 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003693 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003694 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003695 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003696 return X86EMUL_CONTINUE;
3697}
3698
Avi Kivity2d04a052011-04-20 15:32:49 +03003699static int em_clts(struct x86_emulate_ctxt *ctxt)
3700{
3701 ulong cr0;
3702
3703 cr0 = ctxt->ops->get_cr(ctxt, 0);
3704 cr0 &= ~X86_CR0_TS;
3705 ctxt->ops->set_cr(ctxt, 0, cr0);
3706 return X86EMUL_CONTINUE;
3707}
3708
Jan Kiszkab34a8052015-03-09 20:27:43 +01003709static int em_hypercall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003710{
Nadav Amit0f54a322014-08-29 11:26:55 +03003711 int rc = ctxt->ops->fix_hypercall(ctxt);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003712
Avi Kivity26d05cc2011-04-21 12:07:59 +03003713 if (rc != X86EMUL_CONTINUE)
3714 return rc;
3715
3716 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003717 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003718 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003719 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003720 return X86EMUL_CONTINUE;
3721}
3722
Avi Kivity96051572012-06-10 17:21:18 +03003723static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3724 void (*get)(struct x86_emulate_ctxt *ctxt,
3725 struct desc_ptr *ptr))
3726{
3727 struct desc_ptr desc_ptr;
3728
3729 if (ctxt->mode == X86EMUL_MODE_PROT64)
3730 ctxt->op_bytes = 8;
3731 get(ctxt, &desc_ptr);
3732 if (ctxt->op_bytes == 2) {
3733 ctxt->op_bytes = 4;
3734 desc_ptr.address &= 0x00ffffff;
3735 }
3736 /* Disable writeback. */
3737 ctxt->dst.type = OP_NONE;
Steve Rutherford129a72a2017-01-11 18:28:29 -08003738 return segmented_write_std(ctxt, ctxt->dst.addr.mem,
3739 &desc_ptr, 2 + ctxt->op_bytes);
Avi Kivity96051572012-06-10 17:21:18 +03003740}
3741
3742static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3743{
3744 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3745}
3746
3747static int em_sidt(struct x86_emulate_ctxt *ctxt)
3748{
3749 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3750}
3751
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003752static int em_lgdt_lidt(struct x86_emulate_ctxt *ctxt, bool lgdt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003753{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003754 struct desc_ptr desc_ptr;
3755 int rc;
3756
Avi Kivity510425f2012-06-07 17:04:36 +03003757 if (ctxt->mode == X86EMUL_MODE_PROT64)
3758 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003759 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003760 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003761 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003762 if (rc != X86EMUL_CONTINUE)
3763 return rc;
Nadav Amit9a9abf62014-11-02 11:54:56 +02003764 if (ctxt->mode == X86EMUL_MODE_PROT64 &&
Yu Zhangfd8cb432017-08-24 20:27:56 +08003765 emul_is_noncanonical_address(desc_ptr.address, ctxt))
Nadav Amit9a9abf62014-11-02 11:54:56 +02003766 return emulate_gp(ctxt, 0);
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003767 if (lgdt)
3768 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3769 else
3770 ctxt->ops->set_idt(ctxt, &desc_ptr);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003771 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003772 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003773 return X86EMUL_CONTINUE;
3774}
3775
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003776static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3777{
3778 return em_lgdt_lidt(ctxt, true);
3779}
3780
Avi Kivity26d05cc2011-04-21 12:07:59 +03003781static int em_lidt(struct x86_emulate_ctxt *ctxt)
3782{
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003783 return em_lgdt_lidt(ctxt, false);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003784}
3785
3786static int em_smsw(struct x86_emulate_ctxt *ctxt)
3787{
Nadav Amit32e94d02014-06-02 18:34:11 +03003788 if (ctxt->dst.type == OP_MEM)
3789 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003790 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003791 return X86EMUL_CONTINUE;
3792}
3793
3794static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3795{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003796 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003797 | (ctxt->src.val & 0x0f));
3798 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003799 return X86EMUL_CONTINUE;
3800}
3801
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003802static int em_loop(struct x86_emulate_ctxt *ctxt)
3803{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003804 int rc = X86EMUL_CONTINUE;
3805
Paolo Bonzini01485a22014-11-19 18:25:08 +01003806 register_address_increment(ctxt, VCPU_REGS_RCX, -1);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003807 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003808 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
Nadav Amit234f3ce2014-09-18 22:39:38 +03003809 rc = jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003810
Nadav Amit234f3ce2014-09-18 22:39:38 +03003811 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003812}
3813
3814static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3815{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003816 int rc = X86EMUL_CONTINUE;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003817
Nadav Amit234f3ce2014-09-18 22:39:38 +03003818 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
3819 rc = jmp_rel(ctxt, ctxt->src.val);
3820
3821 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003822}
3823
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003824static int em_in(struct x86_emulate_ctxt *ctxt)
3825{
3826 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3827 &ctxt->dst.val))
3828 return X86EMUL_IO_NEEDED;
3829
3830 return X86EMUL_CONTINUE;
3831}
3832
3833static int em_out(struct x86_emulate_ctxt *ctxt)
3834{
3835 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3836 &ctxt->src.val, 1);
3837 /* Disable writeback. */
3838 ctxt->dst.type = OP_NONE;
3839 return X86EMUL_CONTINUE;
3840}
3841
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003842static int em_cli(struct x86_emulate_ctxt *ctxt)
3843{
3844 if (emulator_bad_iopl(ctxt))
3845 return emulate_gp(ctxt, 0);
3846
3847 ctxt->eflags &= ~X86_EFLAGS_IF;
3848 return X86EMUL_CONTINUE;
3849}
3850
3851static int em_sti(struct x86_emulate_ctxt *ctxt)
3852{
3853 if (emulator_bad_iopl(ctxt))
3854 return emulate_gp(ctxt, 0);
3855
3856 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3857 ctxt->eflags |= X86_EFLAGS_IF;
3858 return X86EMUL_CONTINUE;
3859}
3860
Avi Kivity6d6eede2012-06-07 14:11:36 +03003861static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3862{
3863 u32 eax, ebx, ecx, edx;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003864 u64 msr = 0;
3865
3866 ctxt->ops->get_msr(ctxt, MSR_MISC_FEATURES_ENABLES, &msr);
3867 if (msr & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
3868 ctxt->ops->cpl(ctxt)) {
3869 return emulate_gp(ctxt, 0);
3870 }
Avi Kivity6d6eede2012-06-07 14:11:36 +03003871
Avi Kivitydd856ef2012-08-27 23:46:17 +03003872 eax = reg_read(ctxt, VCPU_REGS_RAX);
3873 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Yu Zhange911eb32017-08-24 20:27:52 +08003874 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, true);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003875 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3876 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3877 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3878 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003879 return X86EMUL_CONTINUE;
3880}
3881
Paolo Bonzini98f73632013-10-31 11:19:42 +01003882static int em_sahf(struct x86_emulate_ctxt *ctxt)
3883{
3884 u32 flags;
3885
Nadav Amit0efb0442015-03-29 16:33:03 +03003886 flags = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
3887 X86_EFLAGS_SF;
Paolo Bonzini98f73632013-10-31 11:19:42 +01003888 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3889
3890 ctxt->eflags &= ~0xffUL;
3891 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3892 return X86EMUL_CONTINUE;
3893}
3894
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003895static int em_lahf(struct x86_emulate_ctxt *ctxt)
3896{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003897 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3898 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003899 return X86EMUL_CONTINUE;
3900}
3901
Avi Kivity92998362012-06-13 12:25:06 +03003902static int em_bswap(struct x86_emulate_ctxt *ctxt)
3903{
3904 switch (ctxt->op_bytes) {
3905#ifdef CONFIG_X86_64
3906 case 8:
3907 asm("bswap %0" : "+r"(ctxt->dst.val));
3908 break;
3909#endif
3910 default:
3911 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3912 break;
3913 }
3914 return X86EMUL_CONTINUE;
3915}
3916
Nadav Amit13e457e2014-10-13 13:04:13 +03003917static int em_clflush(struct x86_emulate_ctxt *ctxt)
3918{
3919 /* emulating clflush regardless of cpuid */
3920 return X86EMUL_CONTINUE;
3921}
3922
Nadav Amit2276b512015-01-26 09:32:24 +02003923static int em_movsxd(struct x86_emulate_ctxt *ctxt)
3924{
3925 ctxt->dst.val = (s32) ctxt->src.val;
3926 return X86EMUL_CONTINUE;
3927}
3928
Radim Krčmář283c95d2016-11-09 19:07:06 +01003929static int check_fxsr(struct x86_emulate_ctxt *ctxt)
3930{
3931 u32 eax = 1, ebx, ecx = 0, edx;
3932
Yu Zhange911eb32017-08-24 20:27:52 +08003933 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, false);
Radim Krčmář283c95d2016-11-09 19:07:06 +01003934 if (!(edx & FFL(FXSR)))
3935 return emulate_ud(ctxt);
3936
3937 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
3938 return emulate_nm(ctxt);
3939
3940 /*
3941 * Don't emulate a case that should never be hit, instead of working
3942 * around a lack of fxsave64/fxrstor64 on old compilers.
3943 */
3944 if (ctxt->mode >= X86EMUL_MODE_PROT64)
3945 return X86EMUL_UNHANDLEABLE;
3946
3947 return X86EMUL_CONTINUE;
3948}
3949
3950/*
Nick Desaulniers9d643f62017-05-30 20:08:38 -07003951 * Hardware doesn't save and restore XMM 0-7 without CR4.OSFXSR, but does save
3952 * and restore MXCSR.
3953 */
3954static size_t __fxstate_size(int nregs)
3955{
3956 return offsetof(struct fxregs_state, xmm_space[0]) + nregs * 16;
3957}
3958
3959static inline size_t fxstate_size(struct x86_emulate_ctxt *ctxt)
3960{
3961 bool cr4_osfxsr;
3962 if (ctxt->mode == X86EMUL_MODE_PROT64)
3963 return __fxstate_size(16);
3964
3965 cr4_osfxsr = ctxt->ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR;
3966 return __fxstate_size(cr4_osfxsr ? 8 : 0);
3967}
3968
3969/*
Radim Krčmář283c95d2016-11-09 19:07:06 +01003970 * FXSAVE and FXRSTOR have 4 different formats depending on execution mode,
3971 * 1) 16 bit mode
3972 * 2) 32 bit mode
3973 * - like (1), but FIP and FDP (foo) are only 16 bit. At least Intel CPUs
3974 * preserve whole 32 bit values, though, so (1) and (2) are the same wrt.
3975 * save and restore
3976 * 3) 64-bit mode with REX.W prefix
3977 * - like (2), but XMM 8-15 are being saved and restored
3978 * 4) 64-bit mode without REX.W prefix
3979 * - like (3), but FIP and FDP are 64 bit
3980 *
3981 * Emulation uses (3) for (1) and (2) and preserves XMM 8-15 to reach the
3982 * desired result. (4) is not emulated.
3983 *
3984 * Note: Guest and host CPUID.(EAX=07H,ECX=0H):EBX[bit 13] (deprecate FPU CS
3985 * and FPU DS) should match.
3986 */
3987static int em_fxsave(struct x86_emulate_ctxt *ctxt)
3988{
3989 struct fxregs_state fx_state;
Radim Krčmář283c95d2016-11-09 19:07:06 +01003990 int rc;
3991
3992 rc = check_fxsr(ctxt);
3993 if (rc != X86EMUL_CONTINUE)
3994 return rc;
3995
3996 ctxt->ops->get_fpu(ctxt);
3997
3998 rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_state));
3999
4000 ctxt->ops->put_fpu(ctxt);
4001
4002 if (rc != X86EMUL_CONTINUE)
4003 return rc;
4004
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004005 return segmented_write_std(ctxt, ctxt->memop.addr.mem, &fx_state,
4006 fxstate_size(ctxt));
Radim Krčmář283c95d2016-11-09 19:07:06 +01004007}
4008
4009static int em_fxrstor(struct x86_emulate_ctxt *ctxt)
4010{
4011 struct fxregs_state fx_state;
4012 int rc;
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004013 size_t size;
Radim Krčmář283c95d2016-11-09 19:07:06 +01004014
4015 rc = check_fxsr(ctxt);
4016 if (rc != X86EMUL_CONTINUE)
4017 return rc;
4018
Radim Krčmář283c95d2016-11-09 19:07:06 +01004019 ctxt->ops->get_fpu(ctxt);
4020
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004021 size = fxstate_size(ctxt);
4022 if (size < __fxstate_size(16)) {
4023 rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_state));
4024 if (rc != X86EMUL_CONTINUE)
4025 goto out;
4026 }
4027
4028 rc = segmented_read_std(ctxt, ctxt->memop.addr.mem, &fx_state, size);
4029 if (rc != X86EMUL_CONTINUE)
4030 goto out;
4031
4032 if (fx_state.mxcsr >> 16) {
4033 rc = emulate_gp(ctxt, 0);
4034 goto out;
4035 }
Radim Krčmář283c95d2016-11-09 19:07:06 +01004036
4037 if (rc == X86EMUL_CONTINUE)
4038 rc = asm_safe("fxrstor %[fx]", : [fx] "m"(fx_state));
4039
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004040out:
Radim Krčmář283c95d2016-11-09 19:07:06 +01004041 ctxt->ops->put_fpu(ctxt);
4042
4043 return rc;
4044}
4045
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004046static bool valid_cr(int nr)
4047{
4048 switch (nr) {
4049 case 0:
4050 case 2 ... 4:
4051 case 8:
4052 return true;
4053 default:
4054 return false;
4055 }
4056}
4057
4058static int check_cr_read(struct x86_emulate_ctxt *ctxt)
4059{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004060 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004061 return emulate_ud(ctxt);
4062
4063 return X86EMUL_CONTINUE;
4064}
4065
4066static int check_cr_write(struct x86_emulate_ctxt *ctxt)
4067{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004068 u64 new_val = ctxt->src.val64;
4069 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03004070 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004071
4072 static u64 cr_reserved_bits[] = {
4073 0xffffffff00000000ULL,
4074 0, 0, 0, /* CR3 checked later */
4075 CR4_RESERVED_BITS,
4076 0, 0, 0,
4077 CR8_RESERVED_BITS,
4078 };
4079
4080 if (!valid_cr(cr))
4081 return emulate_ud(ctxt);
4082
4083 if (new_val & cr_reserved_bits[cr])
4084 return emulate_gp(ctxt, 0);
4085
4086 switch (cr) {
4087 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03004088 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004089 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
4090 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
4091 return emulate_gp(ctxt, 0);
4092
Avi Kivity717746e2011-04-20 13:37:53 +03004093 cr4 = ctxt->ops->get_cr(ctxt, 4);
4094 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004095
4096 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
4097 !(cr4 & X86_CR4_PAE))
4098 return emulate_gp(ctxt, 0);
4099
4100 break;
4101 }
4102 case 3: {
4103 u64 rsvd = 0;
4104
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03004105 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08004106 if (efer & EFER_LMA) {
4107 u64 maxphyaddr;
Yu Zhangd6500142017-09-18 18:45:01 +08004108 u32 eax, ebx, ecx, edx;
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08004109
Yu Zhangd6500142017-09-18 18:45:01 +08004110 eax = 0x80000008;
4111 ecx = 0;
4112 if (ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx,
4113 &edx, false))
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08004114 maxphyaddr = eax & 0xff;
4115 else
4116 maxphyaddr = 36;
4117 rsvd = rsvd_bits(maxphyaddr, 62);
4118 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004119
4120 if (new_val & rsvd)
4121 return emulate_gp(ctxt, 0);
4122
4123 break;
4124 }
4125 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03004126 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004127
4128 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
4129 return emulate_gp(ctxt, 0);
4130
4131 break;
4132 }
4133 }
4134
4135 return X86EMUL_CONTINUE;
4136}
4137
Joerg Roedel3b88e412011-04-04 12:39:29 +02004138static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
4139{
4140 unsigned long dr7;
4141
Avi Kivity717746e2011-04-20 13:37:53 +03004142 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02004143
4144 /* Check if DR7.Global_Enable is set */
4145 return dr7 & (1 << 13);
4146}
4147
4148static int check_dr_read(struct x86_emulate_ctxt *ctxt)
4149{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004150 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02004151 u64 cr4;
4152
4153 if (dr > 7)
4154 return emulate_ud(ctxt);
4155
Avi Kivity717746e2011-04-20 13:37:53 +03004156 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02004157 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
4158 return emulate_ud(ctxt);
4159
Nadav Amit6d2a0522014-11-02 11:54:43 +02004160 if (check_dr7_gd(ctxt)) {
4161 ulong dr6;
4162
4163 ctxt->ops->get_dr(ctxt, 6, &dr6);
4164 dr6 &= ~15;
4165 dr6 |= DR6_BD | DR6_RTM;
4166 ctxt->ops->set_dr(ctxt, 6, dr6);
Joerg Roedel3b88e412011-04-04 12:39:29 +02004167 return emulate_db(ctxt);
Nadav Amit6d2a0522014-11-02 11:54:43 +02004168 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02004169
4170 return X86EMUL_CONTINUE;
4171}
4172
4173static int check_dr_write(struct x86_emulate_ctxt *ctxt)
4174{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004175 u64 new_val = ctxt->src.val64;
4176 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02004177
4178 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
4179 return emulate_gp(ctxt, 0);
4180
4181 return check_dr_read(ctxt);
4182}
4183
Joerg Roedel01de8b02011-04-04 12:39:31 +02004184static int check_svme(struct x86_emulate_ctxt *ctxt)
4185{
Radim Krčmář92ceb762017-05-18 19:37:32 +02004186 u64 efer = 0;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004187
Avi Kivity717746e2011-04-20 13:37:53 +03004188 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02004189
4190 if (!(efer & EFER_SVME))
4191 return emulate_ud(ctxt);
4192
4193 return X86EMUL_CONTINUE;
4194}
4195
4196static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
4197{
Avi Kivitydd856ef2012-08-27 23:46:17 +03004198 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02004199
4200 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07004201 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02004202 return emulate_gp(ctxt, 0);
4203
4204 return check_svme(ctxt);
4205}
4206
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004207static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
4208{
Avi Kivity717746e2011-04-20 13:37:53 +03004209 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004210
Avi Kivity717746e2011-04-20 13:37:53 +03004211 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004212 return emulate_ud(ctxt);
4213
4214 return X86EMUL_CONTINUE;
4215}
4216
Joerg Roedel80612522011-04-04 12:39:33 +02004217static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
4218{
Avi Kivity717746e2011-04-20 13:37:53 +03004219 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03004220 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02004221
Avi Kivity717746e2011-04-20 13:37:53 +03004222 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03004223 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02004224 return emulate_gp(ctxt, 0);
4225
4226 return X86EMUL_CONTINUE;
4227}
4228
Joerg Roedelf6511932011-04-04 12:39:35 +02004229static int check_perm_in(struct x86_emulate_ctxt *ctxt)
4230{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004231 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
4232 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02004233 return emulate_gp(ctxt, 0);
4234
4235 return X86EMUL_CONTINUE;
4236}
4237
4238static int check_perm_out(struct x86_emulate_ctxt *ctxt)
4239{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004240 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
4241 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02004242 return emulate_gp(ctxt, 0);
4243
4244 return X86EMUL_CONTINUE;
4245}
4246
Avi Kivity73fba5f2010-07-29 15:11:53 +03004247#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004248#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
4249#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
4250 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03004251#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02004252#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004253#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
4254#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Nadav Amit39f062f2014-11-26 15:47:18 +02004255#define ID(_f, _i) { .flags = ((_f) | InstrDual | ModRM), .u.idual = (_i) }
Nadav Amit2276b512015-01-26 09:32:24 +02004256#define MD(_f, _m) { .flags = ((_f) | ModeDual), .u.mdual = (_m) }
Gleb Natapov045a2822012-12-20 16:57:43 +02004257#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03004258#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02004259#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004260#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004261 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004262#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004263 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
4264 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02004265#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03004266
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004267#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02004268#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004269#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02004270#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004271#define I2bvIP(_f, _e, _i, _p) \
4272 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004273
Avi Kivityfb864fb2013-01-04 16:18:54 +02004274#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
4275 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
4276 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03004277
Nadav Amit0f54a322014-08-29 11:26:55 +03004278static const struct opcode group7_rm0[] = {
4279 N,
Jan Kiszkab34a8052015-03-09 20:27:43 +01004280 I(SrcNone | Priv | EmulateOnUD, em_hypercall),
Nadav Amit0f54a322014-08-29 11:26:55 +03004281 N, N, N, N, N, N,
4282};
4283
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004284static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004285 DI(SrcNone | Priv, monitor),
4286 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004287 N, N, N, N, N, N,
4288};
4289
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004290static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004291 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Jan Kiszkab34a8052015-03-09 20:27:43 +01004292 II(SrcNone | Prot | EmulateOnUD, em_hypercall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004293 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
4294 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
4295 DIP(SrcNone | Prot | Priv, stgi, check_svme),
4296 DIP(SrcNone | Prot | Priv, clgi, check_svme),
4297 DIP(SrcNone | Prot | Priv, skinit, check_svme),
4298 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004299};
Avi Kivity6230f7f2010-08-26 18:34:55 +03004300
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004301static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004302 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004303 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004304 N, N, N, N, N, N,
4305};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004306
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004307static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02004308 F(Lock, em_add),
4309 F(Lock | PageTable, em_or),
4310 F(Lock, em_adc),
4311 F(Lock, em_sbb),
4312 F(Lock | PageTable, em_and),
4313 F(Lock, em_sub),
4314 F(Lock, em_xor),
4315 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004316};
4317
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004318static const struct opcode group1A[] = {
Tom Lendacky0f89b202016-12-14 14:59:23 -05004319 I(DstMem | SrcNone | Mov | Stack | IncSP | TwoMemOp, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004320};
4321
Avi Kivity007a3b52013-01-19 19:51:51 +02004322static const struct opcode group2[] = {
4323 F(DstMem | ModRM, em_rol),
4324 F(DstMem | ModRM, em_ror),
4325 F(DstMem | ModRM, em_rcl),
4326 F(DstMem | ModRM, em_rcr),
4327 F(DstMem | ModRM, em_shl),
4328 F(DstMem | ModRM, em_shr),
4329 F(DstMem | ModRM, em_shl),
4330 F(DstMem | ModRM, em_sar),
4331};
4332
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004333static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02004334 F(DstMem | SrcImm | NoWrite, em_test),
4335 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02004336 F(DstMem | SrcNone | Lock, em_not),
4337 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02004338 F(DstXacc | Src2Mem, em_mul_ex),
4339 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004340 F(DstXacc | Src2Mem, em_div_ex),
4341 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004342};
4343
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004344static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02004345 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
4346 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004347 N, N, N, N, N, N,
4348};
4349
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004350static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02004351 F(DstMem | SrcNone | Lock, em_inc),
4352 F(DstMem | SrcNone | Lock, em_dec),
Nadav Amit58b70752014-10-24 11:35:09 +03004353 I(SrcMem | NearBranch, em_call_near_abs),
Nadav Amitacac6f892015-05-03 20:22:57 +03004354 I(SrcMemFAddr | ImplicitOps, em_call_far),
Nadav Amit58b70752014-10-24 11:35:09 +03004355 I(SrcMem | NearBranch, em_jmp_abs),
Nadav Amitf7784042014-09-18 22:39:41 +03004356 I(SrcMemFAddr | ImplicitOps, em_jmp_far),
Tom Lendacky0f89b202016-12-14 14:59:23 -05004357 I(SrcMem | Stack | TwoMemOp, em_push), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004358};
4359
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004360static const struct opcode group6[] = {
Nadav Amit63ea0a42015-01-08 11:59:03 +01004361 DI(Prot | DstMem, sldt),
4362 DI(Prot | DstMem, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03004363 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03004364 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004365 N, N, N, N,
4366};
4367
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004368static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03004369 II(Mov | DstMem, em_sgdt, sgdt),
4370 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004371 II(SrcMem | Priv, em_lgdt, lgdt),
4372 II(SrcMem | Priv, em_lidt, lidt),
4373 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
4374 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
4375 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004376}, {
Nadav Amit0f54a322014-08-29 11:26:55 +03004377 EXT(0, group7_rm0),
Avi Kivity5ef39c72011-04-21 12:21:50 +03004378 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004379 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004380 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
4381 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
4382 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004383} };
4384
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004385static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004386 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02004387 F(DstMem | SrcImmByte | NoWrite, em_bt),
4388 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
4389 F(DstMem | SrcImmByte | Lock, em_btr),
4390 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004391};
4392
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004393static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004394 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004395}, {
4396 N, N, N, N, N, N, N, N,
4397} };
4398
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004399static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004400 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004401 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004402};
4403
Nadav Amit13e457e2014-10-13 13:04:13 +03004404static const struct gprefix pfx_0f_ae_7 = {
Nadav Amit3f6f1482014-10-13 13:04:14 +03004405 I(SrcMem | ByteOp, em_clflush), N, N, N,
Nadav Amit13e457e2014-10-13 13:04:13 +03004406};
4407
4408static const struct group_dual group15 = { {
Radim Krčmář283c95d2016-11-09 19:07:06 +01004409 I(ModRM | Aligned16, em_fxsave),
4410 I(ModRM | Aligned16, em_fxrstor),
4411 N, N, N, N, N, GP(0, &pfx_0f_ae_7),
Nadav Amit13e457e2014-10-13 13:04:13 +03004412}, {
4413 N, N, N, N, N, N, N, N,
4414} };
4415
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004416static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03004417 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02004418};
4419
Nadav Amit39f062f2014-11-26 15:47:18 +02004420static const struct instr_dual instr_dual_0f_2b = {
4421 I(0, em_mov), N
4422};
4423
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004424static const struct gprefix pfx_0f_2b = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004425 ID(0, &instr_dual_0f_2b), ID(0, &instr_dual_0f_2b), N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03004426};
4427
Igor Mammedov27ce8252014-03-15 21:01:59 +01004428static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01004429 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004430};
4431
Alex Williamson0a370272014-07-11 11:56:31 -06004432static const struct gprefix pfx_0f_e7 = {
4433 N, I(Sse, em_mov), N, N,
4434};
4435
Gleb Natapov045a2822012-12-20 16:57:43 +02004436static const struct escape escape_d9 = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02004437 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstcw),
Gleb Natapov045a2822012-12-20 16:57:43 +02004438}, {
4439 /* 0xC0 - 0xC7 */
4440 N, N, N, N, N, N, N, N,
4441 /* 0xC8 - 0xCF */
4442 N, N, N, N, N, N, N, N,
4443 /* 0xD0 - 0xC7 */
4444 N, N, N, N, N, N, N, N,
4445 /* 0xD8 - 0xDF */
4446 N, N, N, N, N, N, N, N,
4447 /* 0xE0 - 0xE7 */
4448 N, N, N, N, N, N, N, N,
4449 /* 0xE8 - 0xEF */
4450 N, N, N, N, N, N, N, N,
4451 /* 0xF0 - 0xF7 */
4452 N, N, N, N, N, N, N, N,
4453 /* 0xF8 - 0xFF */
4454 N, N, N, N, N, N, N, N,
4455} };
4456
4457static const struct escape escape_db = { {
4458 N, N, N, N, N, N, N, N,
4459}, {
4460 /* 0xC0 - 0xC7 */
4461 N, N, N, N, N, N, N, N,
4462 /* 0xC8 - 0xCF */
4463 N, N, N, N, N, N, N, N,
4464 /* 0xD0 - 0xC7 */
4465 N, N, N, N, N, N, N, N,
4466 /* 0xD8 - 0xDF */
4467 N, N, N, N, N, N, N, N,
4468 /* 0xE0 - 0xE7 */
4469 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
4470 /* 0xE8 - 0xEF */
4471 N, N, N, N, N, N, N, N,
4472 /* 0xF0 - 0xF7 */
4473 N, N, N, N, N, N, N, N,
4474 /* 0xF8 - 0xFF */
4475 N, N, N, N, N, N, N, N,
4476} };
4477
4478static const struct escape escape_dd = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02004479 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstsw),
Gleb Natapov045a2822012-12-20 16:57:43 +02004480}, {
4481 /* 0xC0 - 0xC7 */
4482 N, N, N, N, N, N, N, N,
4483 /* 0xC8 - 0xCF */
4484 N, N, N, N, N, N, N, N,
4485 /* 0xD0 - 0xC7 */
4486 N, N, N, N, N, N, N, N,
4487 /* 0xD8 - 0xDF */
4488 N, N, N, N, N, N, N, N,
4489 /* 0xE0 - 0xE7 */
4490 N, N, N, N, N, N, N, N,
4491 /* 0xE8 - 0xEF */
4492 N, N, N, N, N, N, N, N,
4493 /* 0xF0 - 0xF7 */
4494 N, N, N, N, N, N, N, N,
4495 /* 0xF8 - 0xFF */
4496 N, N, N, N, N, N, N, N,
4497} };
4498
Nadav Amit39f062f2014-11-26 15:47:18 +02004499static const struct instr_dual instr_dual_0f_c3 = {
4500 I(DstMem | SrcReg | ModRM | No16 | Mov, em_mov), N
4501};
4502
Nadav Amit2276b512015-01-26 09:32:24 +02004503static const struct mode_dual mode_dual_63 = {
4504 N, I(DstReg | SrcMem32 | ModRM | Mov, em_movsxd)
4505};
4506
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004507static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004508 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004509 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004510 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
4511 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004512 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004513 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004514 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
4515 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004516 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004517 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004518 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
4519 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004520 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004521 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004522 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
4523 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004524 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004525 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004526 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004527 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004528 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004529 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004530 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004531 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004532 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02004533 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004534 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03004535 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004536 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09004537 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004538 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09004539 I(ImplicitOps | Stack | No64, em_pusha),
4540 I(ImplicitOps | Stack | No64, em_popa),
Nadav Amit2276b512015-01-26 09:32:24 +02004541 N, MD(ModRM, &mode_dual_63),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004542 N, N, N, N,
4543 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03004544 I(SrcImm | Mov | Stack, em_push),
4545 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03004546 I(SrcImmByte | Mov | Stack, em_push),
4547 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004548 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09004549 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03004550 /* 0x70 - 0x7F */
Nadav Amit58b70752014-10-24 11:35:09 +03004551 X16(D(SrcImmByte | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004552 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004553 G(ByteOp | DstMem | SrcImm, group1),
4554 G(DstMem | SrcImm, group1),
4555 G(ByteOp | DstMem | SrcImm | No64, group1),
4556 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02004557 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004558 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004559 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004560 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004561 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004562 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09004563 D(ModRM | SrcMem | NoAccess | DstReg),
4564 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
4565 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004566 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004567 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004568 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03004569 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08004570 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09004571 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01004572 II(ImplicitOps | Stack, em_popf, popf),
4573 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004574 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004575 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004576 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Tom Lendacky0f89b202016-12-14 14:59:23 -05004577 I2bv(SrcSI | DstDI | Mov | String | TwoMemOp, em_mov),
4578 F2bv(SrcSI | DstDI | String | NoWrite | TwoMemOp, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004579 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004580 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004581 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
4582 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004583 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004584 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004585 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004586 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02004587 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004588 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004589 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Nadav Amit58b70752014-10-24 11:35:09 +03004590 I(ImplicitOps | NearBranch | SrcImmU16, em_ret_near_imm),
4591 I(ImplicitOps | NearBranch, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03004592 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
4593 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004594 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004595 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03004596 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Nadav Amit16794aa2015-01-26 09:32:22 +02004597 I(ImplicitOps | SrcImmU16, em_ret_far_imm),
4598 I(ImplicitOps, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02004599 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004600 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004601 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004602 G(Src2One | ByteOp, group2), G(Src2One, group2),
4603 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02004604 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02004605 I(DstAcc | SrcImmUByte | No64, em_aad),
4606 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004607 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004608 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02004609 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004610 /* 0xE0 - 0xE7 */
Nadav Amit58b70752014-10-24 11:35:09 +03004611 X3(I(SrcImmByte | NearBranch, em_loop)),
4612 I(SrcImmByte | NearBranch, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004613 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
4614 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004615 /* 0xE8 - 0xEF */
Nadav Amit58b70752014-10-24 11:35:09 +03004616 I(SrcImm | NearBranch, em_call), D(SrcImm | ImplicitOps | NearBranch),
4617 I(SrcImmFAddr | No64, em_jmp_far),
4618 D(SrcImmByte | ImplicitOps | NearBranch),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004619 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
4620 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004621 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004622 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004623 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4624 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004625 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09004626 D(ImplicitOps), D(ImplicitOps),
4627 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004628 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4629};
4630
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004631static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004632 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004633 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02004634 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004635 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004636 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004637 N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004638 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004639 N, N, N, N, N, N, N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004640 D(ImplicitOps | ModRM | SrcMem | NoAccess),
4641 N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004642 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03004643 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
4644 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
4645 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
4646 check_cr_write),
4647 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
4648 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004649 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004650 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
4651 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004652 N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b),
Avi Kivity3e114eb2012-04-09 18:40:01 +03004653 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004654 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004655 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004656 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004657 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004658 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02004659 I(ImplicitOps | EmulateOnUD, em_sysenter),
4660 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004661 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004662 N, N, N, N, N, N, N, N,
4663 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03004664 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004665 /* 0x50 - 0x5F */
4666 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4667 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004668 N, N, N, N,
4669 N, N, N, N,
4670 N, N, N, N,
4671 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004672 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004673 N, N, N, N,
4674 N, N, N, N,
4675 N, N, N, N,
4676 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004677 /* 0x80 - 0x8F */
Nadav Amit58b70752014-10-24 11:35:09 +03004678 X16(D(SrcImm | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004679 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004680 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004681 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004682 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004683 II(ImplicitOps, em_cpuid, cpuid),
4684 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004685 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4686 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004687 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004688 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Paolo Bonzini89651a32015-11-03 13:43:05 +01004689 II(EmulateOnUD | ImplicitOps, em_rsm, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004690 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004691 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4692 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Nadav Amit13e457e2014-10-13 13:04:13 +03004693 GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004694 /* 0xB0 - 0xB7 */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02004695 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable | SrcWrite, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004696 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004697 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004698 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4699 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004700 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004701 /* 0xB8 - 0xBF */
4702 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004703 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004704 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Nadav Amit900efe22015-03-30 15:39:21 +03004705 I(DstReg | SrcMem | ModRM, em_bsf_c),
4706 I(DstReg | SrcMem | ModRM, em_bsr_c),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004707 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004708 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02004709 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Nadav Amit39f062f2014-11-26 15:47:18 +02004710 N, ID(0, &instr_dual_0f_c3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004711 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004712 /* 0xC8 - 0xCF */
4713 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004714 /* 0xD0 - 0xDF */
4715 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4716 /* 0xE0 - 0xEF */
Alex Williamson0a370272014-07-11 11:56:31 -06004717 N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
4718 N, N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004719 /* 0xF0 - 0xFF */
4720 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4721};
4722
Nadav Amit39f062f2014-11-26 15:47:18 +02004723static const struct instr_dual instr_dual_0f_38_f0 = {
4724 I(DstReg | SrcMem | Mov, em_movbe), N
4725};
4726
4727static const struct instr_dual instr_dual_0f_38_f1 = {
4728 I(DstMem | SrcReg | Mov, em_movbe), N
4729};
4730
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004731static const struct gprefix three_byte_0f_38_f0 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004732 ID(0, &instr_dual_0f_38_f0), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004733};
4734
4735static const struct gprefix three_byte_0f_38_f1 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004736 ID(0, &instr_dual_0f_38_f1), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004737};
4738
4739/*
4740 * Insns below are selected by the prefix which indexed by the third opcode
4741 * byte.
4742 */
4743static const struct opcode opcode_map_0f_38[256] = {
4744 /* 0x00 - 0x7f */
4745 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004746 /* 0x80 - 0xef */
4747 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4748 /* 0xf0 - 0xf1 */
Nadav Amit53bb4f72014-12-07 11:49:42 +02004749 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0),
4750 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004751 /* 0xf2 - 0xff */
4752 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004753};
4754
Avi Kivity73fba5f2010-07-29 15:11:53 +03004755#undef D
4756#undef N
4757#undef G
4758#undef GD
4759#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004760#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004761#undef EXT
Nadav Amit2276b512015-01-26 09:32:24 +02004762#undef MD
Nadav Amit2b42fce2015-01-26 09:32:25 +02004763#undef ID
Avi Kivity73fba5f2010-07-29 15:11:53 +03004764
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004765#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004766#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004767#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004768#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004769#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004770
Avi Kivity9dac77f2011-06-01 15:34:25 +03004771static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004772{
4773 unsigned size;
4774
Avi Kivity9dac77f2011-06-01 15:34:25 +03004775 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004776 if (size == 8)
4777 size = 4;
4778 return size;
4779}
4780
4781static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4782 unsigned size, bool sign_extension)
4783{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004784 int rc = X86EMUL_CONTINUE;
4785
4786 op->type = OP_IMM;
4787 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004788 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004789 /* NB. Immediates are sign-extended as necessary. */
4790 switch (op->bytes) {
4791 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004792 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004793 break;
4794 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004795 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004796 break;
4797 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004798 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004799 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004800 case 8:
4801 op->val = insn_fetch(s64, ctxt);
4802 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004803 }
4804 if (!sign_extension) {
4805 switch (op->bytes) {
4806 case 1:
4807 op->val &= 0xff;
4808 break;
4809 case 2:
4810 op->val &= 0xffff;
4811 break;
4812 case 4:
4813 op->val &= 0xffffffff;
4814 break;
4815 }
4816 }
4817done:
4818 return rc;
4819}
4820
Avi Kivitya99455492011-09-13 10:45:41 +03004821static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4822 unsigned d)
4823{
4824 int rc = X86EMUL_CONTINUE;
4825
4826 switch (d) {
4827 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004828 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03004829 break;
4830 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004831 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03004832 break;
4833 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004834 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004835 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03004836 *op = ctxt->memop;
4837 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004838 if (ctxt->d & BitOp)
Avi Kivitya99455492011-09-13 10:45:41 +03004839 fetch_bit_operand(ctxt);
4840 op->orig_val = op->val;
4841 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004842 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004843 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004844 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03004845 case OpAcc:
4846 op->type = OP_REG;
4847 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004848 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03004849 fetch_register_operand(op);
4850 op->orig_val = op->val;
4851 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004852 case OpAccLo:
4853 op->type = OP_REG;
4854 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4855 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4856 fetch_register_operand(op);
4857 op->orig_val = op->val;
4858 break;
4859 case OpAccHi:
4860 if (ctxt->d & ByteOp) {
4861 op->type = OP_NONE;
4862 break;
4863 }
4864 op->type = OP_REG;
4865 op->bytes = ctxt->op_bytes;
4866 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4867 fetch_register_operand(op);
4868 op->orig_val = op->val;
4869 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004870 case OpDI:
4871 op->type = OP_MEM;
4872 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4873 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004874 register_address(ctxt, VCPU_REGS_RDI);
Avi Kivitya99455492011-09-13 10:45:41 +03004875 op->addr.mem.seg = VCPU_SREG_ES;
4876 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004877 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03004878 break;
4879 case OpDX:
4880 op->type = OP_REG;
4881 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004882 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03004883 fetch_register_operand(op);
4884 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004885 case OpCL:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004886 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004887 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004888 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004889 break;
4890 case OpImmByte:
4891 rc = decode_imm(ctxt, op, 1, true);
4892 break;
4893 case OpOne:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004894 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004895 op->bytes = 1;
4896 op->val = 1;
4897 break;
4898 case OpImm:
4899 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4900 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004901 case OpImm64:
4902 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4903 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004904 case OpMem8:
4905 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004906 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004907 ctxt->memop.addr.reg = decode_register(ctxt,
4908 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004909 fetch_register_operand(&ctxt->memop);
4910 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004911 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004912 case OpMem16:
4913 ctxt->memop.bytes = 2;
4914 goto mem_common;
4915 case OpMem32:
4916 ctxt->memop.bytes = 4;
4917 goto mem_common;
4918 case OpImmU16:
4919 rc = decode_imm(ctxt, op, 2, false);
4920 break;
4921 case OpImmU:
4922 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4923 break;
4924 case OpSI:
4925 op->type = OP_MEM;
4926 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4927 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004928 register_address(ctxt, VCPU_REGS_RSI);
Bandan Das573e80f2014-04-16 12:46:13 -04004929 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03004930 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004931 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004932 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004933 case OpXLat:
4934 op->type = OP_MEM;
4935 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4936 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004937 address_mask(ctxt,
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004938 reg_read(ctxt, VCPU_REGS_RBX) +
4939 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04004940 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004941 op->val = 0;
4942 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004943 case OpImmFAddr:
4944 op->type = OP_IMM;
4945 op->addr.mem.ea = ctxt->_eip;
4946 op->bytes = ctxt->op_bytes + 2;
4947 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4948 break;
4949 case OpMemFAddr:
4950 ctxt->memop.bytes = ctxt->op_bytes + 2;
4951 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004952 case OpES:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004953 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004954 op->val = VCPU_SREG_ES;
4955 break;
4956 case OpCS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004957 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004958 op->val = VCPU_SREG_CS;
4959 break;
4960 case OpSS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004961 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004962 op->val = VCPU_SREG_SS;
4963 break;
4964 case OpDS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004965 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004966 op->val = VCPU_SREG_DS;
4967 break;
4968 case OpFS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004969 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004970 op->val = VCPU_SREG_FS;
4971 break;
4972 case OpGS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004973 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004974 op->val = VCPU_SREG_GS;
4975 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004976 case OpImplicit:
4977 /* Special instructions do their own operand decoding. */
4978 default:
4979 op->type = OP_NONE; /* Disable writeback. */
4980 break;
4981 }
4982
4983done:
4984 return rc;
4985}
4986
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004987int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004988{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004989 int rc = X86EMUL_CONTINUE;
4990 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004991 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004992 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04004993 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03004994 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004995
Avi Kivityf09ed832011-09-13 10:45:40 +03004996 ctxt->memop.type = OP_NONE;
4997 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004998 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02004999 ctxt->fetch.ptr = ctxt->fetch.data;
5000 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02005001 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01005002 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005003 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02005004 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02005005 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02005006 if (rc != X86EMUL_CONTINUE)
5007 return rc;
5008 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005009
5010 switch (mode) {
5011 case X86EMUL_MODE_REAL:
5012 case X86EMUL_MODE_VM86:
5013 case X86EMUL_MODE_PROT16:
5014 def_op_bytes = def_ad_bytes = 2;
5015 break;
5016 case X86EMUL_MODE_PROT32:
5017 def_op_bytes = def_ad_bytes = 4;
5018 break;
5019#ifdef CONFIG_X86_64
5020 case X86EMUL_MODE_PROT64:
5021 def_op_bytes = 4;
5022 def_ad_bytes = 8;
5023 break;
5024#endif
5025 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005026 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005027 }
5028
Avi Kivity9dac77f2011-06-01 15:34:25 +03005029 ctxt->op_bytes = def_op_bytes;
5030 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005031
5032 /* Legacy prefixes. */
5033 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09005034 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005035 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005036 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005037 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005038 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005039 break;
5040 case 0x67: /* address-size override */
5041 if (mode == X86EMUL_MODE_PROT64)
5042 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005043 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005044 else
5045 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005046 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005047 break;
5048 case 0x26: /* ES override */
5049 case 0x2e: /* CS override */
5050 case 0x36: /* SS override */
5051 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04005052 has_seg_override = true;
5053 ctxt->seg_override = (ctxt->b >> 3) & 3;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005054 break;
5055 case 0x64: /* FS override */
5056 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04005057 has_seg_override = true;
5058 ctxt->seg_override = ctxt->b & 7;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005059 break;
5060 case 0x40 ... 0x4f: /* REX */
5061 if (mode != X86EMUL_MODE_PROT64)
5062 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005063 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005064 continue;
5065 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005066 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005067 break;
5068 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005069 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005070 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005071 break;
5072 default:
5073 goto done_prefixes;
5074 }
5075
5076 /* Any legacy prefix after a REX prefix nullifies its effect. */
5077
Avi Kivity9dac77f2011-06-01 15:34:25 +03005078 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005079 }
5080
5081done_prefixes:
5082
5083 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005084 if (ctxt->rex_prefix & 8)
5085 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005086
5087 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005088 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08005089 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005090 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02005091 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09005092 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03005093 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005094
5095 /* 0F_38 opcode map */
5096 if (ctxt->b == 0x38) {
5097 ctxt->opcode_len = 3;
5098 ctxt->b = insn_fetch(u8, ctxt);
5099 opcode = opcode_map_0f_38[ctxt->b];
5100 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005101 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03005102 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005103
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09005104 if (ctxt->d & ModRM)
5105 ctxt->modrm = insn_fetch(u8, ctxt);
5106
Nadav Amit7fe864d2014-06-02 18:34:03 +03005107 /* vex-prefix instructions are not implemented */
5108 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
Nadav Amitd14cb5d2014-11-02 11:54:58 +02005109 (mode == X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) == 0xc0)) {
Nadav Amit7fe864d2014-06-02 18:34:03 +03005110 ctxt->d = NotImpl;
5111 }
5112
Avi Kivity9dac77f2011-06-01 15:34:25 +03005113 while (ctxt->d & GroupMask) {
5114 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03005115 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005116 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03005117 opcode = opcode.u.group[goffset];
5118 break;
5119 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005120 goffset = (ctxt->modrm >> 3) & 7;
5121 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03005122 opcode = opcode.u.gdual->mod3[goffset];
5123 else
5124 opcode = opcode.u.gdual->mod012[goffset];
5125 break;
5126 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005127 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02005128 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03005129 break;
5130 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005131 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005132 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005133 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03005134 switch (simd_prefix) {
5135 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
5136 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
5137 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
5138 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
5139 }
5140 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02005141 case Escape:
5142 if (ctxt->modrm > 0xbf)
5143 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
5144 else
5145 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
5146 break;
Nadav Amit39f062f2014-11-26 15:47:18 +02005147 case InstrDual:
5148 if ((ctxt->modrm >> 6) == 3)
5149 opcode = opcode.u.idual->mod3;
5150 else
5151 opcode = opcode.u.idual->mod012;
5152 break;
Nadav Amit2276b512015-01-26 09:32:24 +02005153 case ModeDual:
5154 if (ctxt->mode == X86EMUL_MODE_PROT64)
5155 opcode = opcode.u.mdual->mode64;
5156 else
5157 opcode = opcode.u.mdual->mode32;
5158 break;
Avi Kivity46561642011-04-24 14:09:59 +03005159 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005160 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005161 }
Avi Kivity46561642011-04-24 14:09:59 +03005162
Avi Kivityb1ea50b2011-09-13 10:45:42 +03005163 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005164 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005165 }
5166
Paolo Bonzinie24186e2014-03-27 12:00:57 +01005167 /* Unrecognised? */
5168 if (ctxt->d == 0)
5169 return EMULATION_FAILED;
5170
Avi Kivity9dac77f2011-06-01 15:34:25 +03005171 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005172
Nadav Amit3a6095a2014-08-13 16:50:13 +03005173 if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD)))
5174 return EMULATION_FAILED;
5175
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005176 if (unlikely(ctxt->d &
Nadav Amited9aad22014-11-02 11:55:00 +02005177 (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch|
5178 No16))) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005179 /*
5180 * These are copied unconditionally here, and checked unconditionally
5181 * in x86_emulate_insn.
5182 */
5183 ctxt->check_perm = opcode.check_perm;
5184 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005185
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005186 if (ctxt->d & NotImpl)
5187 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02005188
Nadav Amit58b70752014-10-24 11:35:09 +03005189 if (mode == X86EMUL_MODE_PROT64) {
5190 if (ctxt->op_bytes == 4 && (ctxt->d & Stack))
5191 ctxt->op_bytes = 8;
5192 else if (ctxt->d & NearBranch)
5193 ctxt->op_bytes = 8;
5194 }
Avi Kivity7f9b4b72010-08-01 14:46:54 +03005195
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005196 if (ctxt->d & Op3264) {
5197 if (mode == X86EMUL_MODE_PROT64)
5198 ctxt->op_bytes = 8;
5199 else
5200 ctxt->op_bytes = 4;
5201 }
5202
Nadav Amited9aad22014-11-02 11:55:00 +02005203 if ((ctxt->d & No16) && ctxt->op_bytes == 2)
5204 ctxt->op_bytes = 4;
5205
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005206 if (ctxt->d & Sse)
5207 ctxt->op_bytes = 16;
5208 else if (ctxt->d & Mmx)
5209 ctxt->op_bytes = 8;
5210 }
Avi Kivity1253791d2011-03-29 11:41:27 +02005211
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005212 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005213 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03005214 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04005215 if (!has_seg_override) {
5216 has_seg_override = true;
5217 ctxt->seg_override = ctxt->modrm_seg;
5218 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03005219 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03005220 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005221 if (rc != X86EMUL_CONTINUE)
5222 goto done;
5223
Bandan Das573e80f2014-04-16 12:46:13 -04005224 if (!has_seg_override)
5225 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005226
Bandan Das573e80f2014-04-16 12:46:13 -04005227 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005228
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005229 /*
5230 * Decode and fetch the source operand: register, memory
5231 * or immediate.
5232 */
Avi Kivity0fe59122011-09-13 10:45:47 +03005233 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03005234 if (rc != X86EMUL_CONTINUE)
5235 goto done;
5236
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005237 /*
5238 * Decode and fetch the second source operand: register, memory
5239 * or immediate.
5240 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03005241 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03005242 if (rc != X86EMUL_CONTINUE)
5243 goto done;
5244
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005245 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03005246 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005247
Owen Hofmannd9092f52016-10-27 11:25:52 -07005248 if (ctxt->rip_relative && likely(ctxt->memopp))
Nadav Amit1c1c35a2014-11-19 17:43:09 +02005249 ctxt->memopp->addr.mem.ea = address_mask(ctxt,
5250 ctxt->memopp->addr.mem.ea + ctxt->_eip);
Avi Kivitycb16c342011-06-19 19:21:11 +03005251
Paolo Bonzinia430c912014-10-23 14:54:14 +02005252done:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005253 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005254}
5255
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005256bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
5257{
5258 return ctxt->d & PageTable;
5259}
5260
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005261static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
5262{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005263 /* The second termination condition only applies for REPE
5264 * and REPNE. Test if the repeat string operation prefix is
5265 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
5266 * corresponding termination condition according to:
5267 * - if REPE/REPZ and ZF = 0 then done
5268 * - if REPNE/REPNZ and ZF = 1 then done
5269 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005270 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
5271 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
5272 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03005273 ((ctxt->eflags & X86_EFLAGS_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03005274 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03005275 ((ctxt->eflags & X86_EFLAGS_ZF) == X86_EFLAGS_ZF))))
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005276 return true;
5277
5278 return false;
5279}
5280
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005281static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
5282{
Radim Krčmářaabba3c2016-11-08 20:54:18 +01005283 int rc;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005284
5285 ctxt->ops->get_fpu(ctxt);
Radim Krčmářaabba3c2016-11-08 20:54:18 +01005286 rc = asm_safe("fwait");
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005287 ctxt->ops->put_fpu(ctxt);
5288
Radim Krčmářaabba3c2016-11-08 20:54:18 +01005289 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005290 return emulate_exception(ctxt, MF_VECTOR, 0, false);
5291
5292 return X86EMUL_CONTINUE;
5293}
5294
5295static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
5296 struct operand *op)
5297{
5298 if (op->type == OP_MM)
5299 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
5300}
5301
Avi Kivitye28bbd42013-01-04 16:18:48 +02005302static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
5303{
5304 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005305
Avi Kivityb9fa4092013-02-09 11:31:48 +02005306 if (!(ctxt->d & ByteOp))
5307 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005308
Peter Zijlstra1a29b5b2018-01-25 10:58:13 +01005309 asm("push %[flags]; popf; " CALL_NOSPEC " ; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005310 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
Peter Zijlstra1a29b5b2018-01-25 10:58:13 +01005311 [thunk_target]"+S"(fop), ASM_CALL_CONSTRAINT
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005312 : "c"(ctxt->src2.val));
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005313
Avi Kivitye28bbd42013-01-04 16:18:48 +02005314 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005315 if (!fop) /* exception is returned in fop variable */
5316 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02005317 return X86EMUL_CONTINUE;
5318}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005319
Bandan Das14985072014-04-16 12:46:09 -04005320void init_decode_cache(struct x86_emulate_ctxt *ctxt)
5321{
Bandan Das573e80f2014-04-16 12:46:13 -04005322 memset(&ctxt->rip_relative, 0,
5323 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04005324
Bandan Das14985072014-04-16 12:46:09 -04005325 ctxt->io_read.pos = 0;
5326 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04005327 ctxt->mem_read.end = 0;
5328}
5329
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09005330int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005331{
Mathias Krause0225fb52012-08-30 01:30:16 +02005332 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09005333 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005334 int saved_dst_type = ctxt->dst.type;
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005335 unsigned emul_flags;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005336
Avi Kivity9dac77f2011-06-01 15:34:25 +03005337 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04005338
Gleb Natapovd380a5e2010-02-10 14:21:36 +02005339 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005340 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02005341 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02005342 goto done;
5343 }
5344
Avi Kivity9dac77f2011-06-01 15:34:25 +03005345 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02005346 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03005347 goto done;
5348 }
5349
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005350 emul_flags = ctxt->ops->get_hflags(ctxt);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005351 if (unlikely(ctxt->d &
5352 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
5353 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
5354 (ctxt->d & Undefined)) {
5355 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005356 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005357 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005358
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005359 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
5360 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
5361 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02005362 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005363 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02005364
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005365 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
5366 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02005367 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005368 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02005369
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005370 if (ctxt->d & Mmx) {
5371 rc = flush_pending_x87_faults(ctxt);
5372 if (rc != X86EMUL_CONTINUE)
5373 goto done;
5374 /*
5375 * Now that we know the fpu is exception safe, we can fetch
5376 * operands from it.
5377 */
5378 fetch_possible_mmx_operand(ctxt, &ctxt->src);
5379 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
5380 if (!(ctxt->d & Mov))
5381 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
5382 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02005383
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005384 if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && ctxt->intercept) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005385 rc = emulator_check_intercept(ctxt, ctxt->intercept,
5386 X86_ICPT_PRE_EXCEPT);
5387 if (rc != X86EMUL_CONTINUE)
5388 goto done;
5389 }
5390
Nadav Amit64a38292014-12-10 11:19:04 +02005391 /* Instruction can only be executed in protected mode */
5392 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
5393 rc = emulate_ud(ctxt);
5394 goto done;
5395 }
5396
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005397 /* Privileged instruction can be executed only in CPL=0 */
5398 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Nadav Amit68efa762014-06-18 17:19:35 +03005399 if (ctxt->d & PrivUD)
5400 rc = emulate_ud(ctxt);
5401 else
5402 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02005403 goto done;
5404 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005405
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005406 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04005407 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005408 rc = ctxt->check_perm(ctxt);
5409 if (rc != X86EMUL_CONTINUE)
5410 goto done;
5411 }
5412
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005413 if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005414 rc = emulator_check_intercept(ctxt, ctxt->intercept,
5415 X86_ICPT_POST_EXCEPT);
5416 if (rc != X86EMUL_CONTINUE)
5417 goto done;
5418 }
5419
5420 if (ctxt->rep_prefix && (ctxt->d & String)) {
5421 /* All REP prefixes have the same first termination condition */
5422 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Nadav Amit428e3d02015-04-28 13:06:01 +03005423 string_registers_quirk(ctxt);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005424 ctxt->eip = ctxt->_eip;
Nadav Amit0efb0442015-03-29 16:33:03 +03005425 ctxt->eflags &= ~X86_EFLAGS_RF;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005426 goto done;
5427 }
5428 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02005429 }
5430
Avi Kivity9dac77f2011-06-01 15:34:25 +03005431 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
5432 rc = segmented_read(ctxt, ctxt->src.addr.mem,
5433 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09005434 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005435 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005436 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005437 }
5438
Avi Kivity9dac77f2011-06-01 15:34:25 +03005439 if (ctxt->src2.type == OP_MEM) {
5440 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
5441 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02005442 if (rc != X86EMUL_CONTINUE)
5443 goto done;
5444 }
5445
Avi Kivity9dac77f2011-06-01 15:34:25 +03005446 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005447 goto special_insn;
5448
5449
Avi Kivity9dac77f2011-06-01 15:34:25 +03005450 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02005451 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005452 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
5453 &ctxt->dst.val, ctxt->dst.bytes);
Nadav Amitc205fb72014-12-25 02:52:16 +02005454 if (rc != X86EMUL_CONTINUE) {
Paolo Bonzinid44e1212015-02-09 10:02:05 +01005455 if (!(ctxt->d & NoWrite) &&
5456 rc == X86EMUL_PROPAGATE_FAULT &&
Nadav Amitc205fb72014-12-25 02:52:16 +02005457 ctxt->exception.vector == PF_VECTOR)
5458 ctxt->exception.error_code |= PFERR_WRITE_MASK;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02005459 goto done;
Nadav Amitc205fb72014-12-25 02:52:16 +02005460 }
Avi Kivity038e51d2007-01-22 20:40:40 -08005461 }
Paolo Bonzini4ff6f8e2015-02-12 17:04:47 +01005462 /* Copy full 64-bit value for CMPXCHG8B. */
5463 ctxt->dst.orig_val64 = ctxt->dst.val64;
Avi Kivity038e51d2007-01-22 20:40:40 -08005464
Avi Kivity018a98d2007-11-27 19:30:56 +02005465special_insn:
5466
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005467 if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03005468 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005469 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02005470 if (rc != X86EMUL_CONTINUE)
5471 goto done;
5472 }
5473
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03005474 if (ctxt->rep_prefix && (ctxt->d & String))
Nadav Amit0efb0442015-03-29 16:33:03 +03005475 ctxt->eflags |= X86_EFLAGS_RF;
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03005476 else
Nadav Amit0efb0442015-03-29 16:33:03 +03005477 ctxt->eflags &= ~X86_EFLAGS_RF;
Nadav Amit4467c3f2014-07-21 14:37:29 +03005478
Avi Kivity9dac77f2011-06-01 15:34:25 +03005479 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02005480 if (ctxt->d & Fastop) {
5481 void (*fop)(struct fastop *) = (void *)ctxt->execute;
5482 rc = fastop(ctxt, fop);
5483 if (rc != X86EMUL_CONTINUE)
5484 goto done;
5485 goto writeback;
5486 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03005487 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03005488 if (rc != X86EMUL_CONTINUE)
5489 goto done;
5490 goto writeback;
5491 }
5492
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02005493 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005494 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005495 else if (ctxt->opcode_len == 3)
5496 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005497
Avi Kivity9dac77f2011-06-01 15:34:25 +03005498 switch (ctxt->b) {
Gleb Natapovb2833e32009-04-12 13:36:30 +03005499 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005500 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005501 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005502 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03005503 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005504 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03005505 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03005506 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005507 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03005508 ctxt->dst.type = OP_NONE;
5509 else
5510 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09005511 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005512 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005513 switch (ctxt->op_bytes) {
5514 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
5515 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
5516 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005517 }
5518 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005519 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005520 rc = emulate_int(ctxt, 3);
5521 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005522 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005523 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005524 break;
5525 case 0xce: /* into */
Nadav Amit0efb0442015-03-29 16:33:03 +03005526 if (ctxt->eflags & X86_EFLAGS_OF)
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005527 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005528 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005529 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09005530 case 0xeb: /* jmp rel short */
Nadav Amit234f3ce2014-09-18 22:39:38 +03005531 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity9dac77f2011-06-01 15:34:25 +03005532 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005533 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005534 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03005535 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03005536 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005537 case 0xf5: /* cmc */
5538 /* complement carry flag from eflags reg */
Nadav Amit0efb0442015-03-29 16:33:03 +03005539 ctxt->eflags ^= X86_EFLAGS_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005540 break;
5541 case 0xf8: /* clc */
Nadav Amit0efb0442015-03-29 16:33:03 +03005542 ctxt->eflags &= ~X86_EFLAGS_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005543 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03005544 case 0xf9: /* stc */
Nadav Amit0efb0442015-03-29 16:33:03 +03005545 ctxt->eflags |= X86_EFLAGS_CF;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03005546 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005547 case 0xfc: /* cld */
Nadav Amit0efb0442015-03-29 16:33:03 +03005548 ctxt->eflags &= ~X86_EFLAGS_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005549 break;
5550 case 0xfd: /* std */
Nadav Amit0efb0442015-03-29 16:33:03 +03005551 ctxt->eflags |= X86_EFLAGS_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005552 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005553 default:
5554 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005555 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005556
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005557 if (rc != X86EMUL_CONTINUE)
5558 goto done;
5559
Avi Kivity018a98d2007-11-27 19:30:56 +02005560writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02005561 if (ctxt->d & SrcWrite) {
5562 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
5563 rc = writeback(ctxt, &ctxt->src);
5564 if (rc != X86EMUL_CONTINUE)
5565 goto done;
5566 }
Nadav Amitee212292014-06-15 16:12:58 +03005567 if (!(ctxt->d & NoWrite)) {
5568 rc = writeback(ctxt, &ctxt->dst);
5569 if (rc != X86EMUL_CONTINUE)
5570 goto done;
5571 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005572
Gleb Natapov5cd21912010-03-18 15:20:26 +02005573 /*
5574 * restore dst type in case the decoding will be reused
5575 * (happens for string instruction )
5576 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005577 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005578
Avi Kivity9dac77f2011-06-01 15:34:25 +03005579 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005580 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02005581
Avi Kivity9dac77f2011-06-01 15:34:25 +03005582 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005583 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02005584
Avi Kivity9dac77f2011-06-01 15:34:25 +03005585 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005586 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005587 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005588 if ((ctxt->d & SrcMask) == SrcSI)
5589 count = ctxt->src.count;
5590 else
5591 count = ctxt->dst.count;
Paolo Bonzini01485a22014-11-19 18:25:08 +01005592 register_address_increment(ctxt, VCPU_REGS_RCX, -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005593
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005594 if (!string_insn_completed(ctxt)) {
5595 /*
5596 * Re-enter guest when pio read ahead buffer is empty
5597 * or, if it is not used, after each 1024 iteration.
5598 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005599 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005600 (r->end == 0 || r->end != r->pos)) {
5601 /*
5602 * Reset read cache. Usually happens before
5603 * decode, but since instruction is restarted
5604 * we have to do it here.
5605 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005606 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005607 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005608 return EMULATION_RESTART;
5609 }
5610 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03005611 }
Nadav Amit0efb0442015-03-29 16:33:03 +03005612 ctxt->eflags &= ~X86_EFLAGS_RF;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005613 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005614
Avi Kivity9dac77f2011-06-01 15:34:25 +03005615 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02005616
5617done:
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005618 if (rc == X86EMUL_PROPAGATE_FAULT) {
5619 WARN_ON(ctxt->exception.vector > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +02005620 ctxt->have_exception = true;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005621 }
Joerg Roedel775fde82011-04-04 12:39:24 +02005622 if (rc == X86EMUL_INTERCEPTED)
5623 return EMULATION_INTERCEPTED;
5624
Avi Kivitydd856ef2012-08-27 23:46:17 +03005625 if (rc == X86EMUL_CONTINUE)
5626 writeback_registers(ctxt);
5627
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005628 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005629
5630twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005631 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02005632 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02005633 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005634 break;
5635 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02005636 case 0x0d: /* GrpP (prefetch) */
5637 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02005638 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02005639 break;
5640 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005641 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02005642 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005643 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005644 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005645 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005646 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03005647 if (test_cc(ctxt->b, ctxt->eflags))
5648 ctxt->dst.val = ctxt->src.val;
Nadav Amitb91aa142015-03-30 15:39:19 +03005649 else if (ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005650 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08005651 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03005652 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03005653 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005654 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005655 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08005656 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005657 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08005658 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005659 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005660 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005661 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03005662 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005663 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005664 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005665 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005666 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03005667 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005668 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005669 default:
5670 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005671 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005672
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005673threebyte_insn:
5674
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005675 if (rc != X86EMUL_CONTINUE)
5676 goto done;
5677
Avi Kivity6aa8b732006-12-10 02:21:36 -08005678 goto writeback;
5679
5680cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02005681 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005682}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005683
5684void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
5685{
5686 invalidate_registers(ctxt);
5687}
5688
5689void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
5690{
5691 writeback_registers(ctxt);
5692}
Tom Lendacky0f89b202016-12-14 14:59:23 -05005693
5694bool emulator_can_use_gpa(struct x86_emulate_ctxt *ctxt)
5695{
5696 if (ctxt->rep_prefix && (ctxt->d & String))
5697 return false;
5698
5699 if (ctxt->d & TwoMemOp)
5700 return false;
5701
5702 return true;
5703}