blob: 718f7d9afedc498747bf538cde8469a17469cef6 [file] [log] [blame]
Thomas Gleixner20c8ccb2019-06-04 10:11:32 +02001// SPDX-License-Identifier: GPL-2.0-only
Avi Kivity6aa8b732006-12-10 02:21:36 -08002/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03003 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08004 *
5 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
6 *
7 * Copyright (c) 2005 Keir Fraser
8 *
9 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +100010 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080011 *
12 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020013 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080014 *
15 * Avi Kivity <avi@qumranet.com>
16 * Yaniv Kamay <yaniv@qumranet.com>
17 *
Avi Kivity6aa8b732006-12-10 02:21:36 -080018 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
19 */
20
Avi Kivityedf88412007-12-16 11:02:48 +020021#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030022#include "kvm_cache_regs.h"
Avi Kivity56e82312009-08-12 15:04:37 +030023#include <asm/kvm_emulate.h>
Avi Kivityb7d491e2013-01-04 16:18:49 +020024#include <linux/stringify.h>
Nadav Amit3db176d2015-04-19 21:12:59 +030025#include <asm/debugreg.h>
Peter Zijlstra1a29b5b2018-01-25 10:58:13 +010026#include <asm/nospec-branch.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027
Avi Kivity3eeb3282010-01-21 15:31:48 +020028#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020029#include "tss.h"
Yu Zhangd1cd3ce2017-08-24 20:27:53 +080030#include "mmu.h"
Arbel Moshe2d7921c2018-03-12 13:12:53 +020031#include "pmu.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020032
Avi Kivity6aa8b732006-12-10 02:21:36 -080033/*
Avi Kivitya99455492011-09-13 10:45:41 +030034 * Operand types
35 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030036#define OpNone 0ull
37#define OpImplicit 1ull /* No generic decode */
38#define OpReg 2ull /* Register */
39#define OpMem 3ull /* Memory */
40#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
41#define OpDI 5ull /* ES:DI/EDI/RDI */
42#define OpMem64 6ull /* Memory, 64-bit */
43#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
44#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030045#define OpCL 9ull /* CL register (for shifts) */
46#define OpImmByte 10ull /* 8-bit sign extended immediate */
47#define OpOne 11ull /* Implied 1 */
Nadav Amit5e2c6882012-12-06 21:55:10 -020048#define OpImm 12ull /* Sign extended up to 32-bit immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030049#define OpMem16 13ull /* Memory operand (16-bit). */
50#define OpMem32 14ull /* Memory operand (32-bit). */
51#define OpImmU 15ull /* Immediate operand, zero extended */
52#define OpSI 16ull /* SI/ESI/RSI */
53#define OpImmFAddr 17ull /* Immediate far address */
54#define OpMemFAddr 18ull /* Far address in memory */
55#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030056#define OpES 20ull /* ES */
57#define OpCS 21ull /* CS */
58#define OpSS 22ull /* SS */
59#define OpDS 23ull /* DS */
60#define OpFS 24ull /* FS */
61#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020062#define OpMem8 26ull /* 8-bit zero extended memory operand */
Nadav Amit5e2c6882012-12-06 21:55:10 -020063#define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */
Paolo Bonzini7fa57952013-05-09 11:32:50 +020064#define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */
Avi Kivity820207c2013-02-09 11:31:45 +020065#define OpAccLo 29ull /* Low part of extended acc (AX/AX/EAX/RAX) */
66#define OpAccHi 30ull /* High part of extended acc (-/DX/EDX/RDX) */
Avi Kivitya99455492011-09-13 10:45:41 +030067
Avi Kivity0fe59122011-09-13 10:45:47 +030068#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030069#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya99455492011-09-13 10:45:41 +030070
71/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080072 * Opcode effective-address decode tables.
73 * Note that we only emulate instructions that have at least one memory
74 * operand (excluding implicit stack references). We assume that stack
75 * references and instruction fetches will never occur in special memory
76 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
77 * not be handled.
78 */
79
80/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030081#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080082/* Destination operand type. */
Avi Kivitya99455492011-09-13 10:45:41 +030083#define DstShift 1
84#define ImplicitOps (OpImplicit << DstShift)
85#define DstReg (OpReg << DstShift)
86#define DstMem (OpMem << DstShift)
87#define DstAcc (OpAcc << DstShift)
88#define DstDI (OpDI << DstShift)
89#define DstMem64 (OpMem64 << DstShift)
Nadav Amit16bebef2014-12-25 02:52:18 +020090#define DstMem16 (OpMem16 << DstShift)
Avi Kivitya99455492011-09-13 10:45:41 +030091#define DstImmUByte (OpImmUByte << DstShift)
92#define DstDX (OpDX << DstShift)
Avi Kivity820207c2013-02-09 11:31:45 +020093#define DstAccLo (OpAccLo << DstShift)
Avi Kivitya99455492011-09-13 10:45:41 +030094#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080095/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030096#define SrcShift 6
97#define SrcNone (OpNone << SrcShift)
98#define SrcReg (OpReg << SrcShift)
99#define SrcMem (OpMem << SrcShift)
100#define SrcMem16 (OpMem16 << SrcShift)
101#define SrcMem32 (OpMem32 << SrcShift)
102#define SrcImm (OpImm << SrcShift)
103#define SrcImmByte (OpImmByte << SrcShift)
104#define SrcOne (OpOne << SrcShift)
105#define SrcImmUByte (OpImmUByte << SrcShift)
106#define SrcImmU (OpImmU << SrcShift)
107#define SrcSI (OpSI << SrcShift)
Paolo Bonzini7fa57952013-05-09 11:32:50 +0200108#define SrcXLat (OpXLat << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300109#define SrcImmFAddr (OpImmFAddr << SrcShift)
110#define SrcMemFAddr (OpMemFAddr << SrcShift)
111#define SrcAcc (OpAcc << SrcShift)
112#define SrcImmU16 (OpImmU16 << SrcShift)
Nadav Amit5e2c6882012-12-06 21:55:10 -0200113#define SrcImm64 (OpImm64 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300114#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200115#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity820207c2013-02-09 11:31:45 +0200116#define SrcAccHi (OpAccHi << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300117#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300118#define BitOp (1<<11)
119#define MemAbs (1<<12) /* Memory operand is absolute displacement */
120#define String (1<<13) /* String instruction (rep capable) */
121#define Stack (1<<14) /* Stack instruction (push/pop) */
122#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
123#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
124#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
125#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
126#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
Gleb Natapov045a2822012-12-20 16:57:43 +0200127#define Escape (5<<15) /* Escape to coprocessor instruction */
Nadav Amit39f062f2014-11-26 15:47:18 +0200128#define InstrDual (6<<15) /* Alternate instruction decoding of mod == 3 */
Nadav Amit2276b512015-01-26 09:32:24 +0200129#define ModeDual (7<<15) /* Different instruction for 32/64 bit */
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300130#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300131/* Generic ModRM decode. */
132#define ModRM (1<<19)
133/* Destination is only written; never read. */
134#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300135/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200136#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Borislav Petkovb51e9742013-09-22 16:44:52 +0200137#define EmulateOnUD (1<<22) /* Emulate if unsupported by the host */
Avi Kivity5a506b12010-08-01 15:10:29 +0300138#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300139#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300140#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200141#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200142#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300143#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800144#define PageTable (1 << 29) /* instruction used to write page table */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300145#define NotImpl (1 << 30) /* instruction is not implemented */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100146/* Source 2 operand type */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300147#define Src2Shift (31)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300148#define Src2None (OpNone << Src2Shift)
Avi Kivityab2c5ce2013-02-09 11:31:46 +0200149#define Src2Mem (OpMem << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300150#define Src2CL (OpCL << Src2Shift)
151#define Src2ImmByte (OpImmByte << Src2Shift)
152#define Src2One (OpOne << Src2Shift)
153#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300154#define Src2ES (OpES << Src2Shift)
155#define Src2CS (OpCS << Src2Shift)
156#define Src2SS (OpSS << Src2Shift)
157#define Src2DS (OpDS << Src2Shift)
158#define Src2FS (OpFS << Src2Shift)
159#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300160#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300161#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Radim Krčmář48520182016-11-08 20:54:17 +0100162#define AlignMask ((u64)7 << 41)
Avi Kivity1c11b372012-04-09 18:39:59 +0300163#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
Radim Krčmář48520182016-11-08 20:54:17 +0100164#define Unaligned ((u64)2 << 41) /* Explicitly unaligned (e.g. MOVDQU) */
165#define Avx ((u64)3 << 41) /* Advanced Vector Extensions */
166#define Aligned16 ((u64)4 << 41) /* Aligned to 16 byte boundary (e.g. FXSAVE) */
Avi Kivitye28bbd42013-01-04 16:18:48 +0200167#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
Avi Kivityb6744dc2013-01-04 16:18:50 +0200168#define NoWrite ((u64)1 << 45) /* No writeback */
Avi Kivityfb32b1e2013-02-09 11:31:44 +0200169#define SrcWrite ((u64)1 << 46) /* Write back src operand */
Nadav Amit9b88ae92014-05-25 23:05:21 +0300170#define NoMod ((u64)1 << 47) /* Mod field is ignored */
Paolo Bonzinid40a6892014-03-27 11:58:02 +0100171#define Intercept ((u64)1 << 48) /* Has valid intercept field */
172#define CheckPerm ((u64)1 << 49) /* Has valid check_perm field */
Nadav Amit68efa762014-06-18 17:19:35 +0300173#define PrivUD ((u64)1 << 51) /* #UD instead of #GP on CPL > 0 */
Nadav Amit58b70752014-10-24 11:35:09 +0300174#define NearBranch ((u64)1 << 52) /* Near branches */
Nadav Amited9aad22014-11-02 11:55:00 +0200175#define No16 ((u64)1 << 53) /* No 16 bit operand */
Nadav Amitab708092014-12-25 02:52:21 +0200176#define IncSP ((u64)1 << 54) /* SP is incremented before ModRM calc */
Tom Lendacky0f89b202016-12-14 14:59:23 -0500177#define TwoMemOp ((u64)1 << 55) /* Instruction has two memory operand */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800178
Avi Kivity820207c2013-02-09 11:31:45 +0200179#define DstXacc (DstAccLo | SrcAccHi | SrcWrite)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800180
Avi Kivityd0e53322010-07-29 15:11:54 +0300181#define X2(x...) x, x
182#define X3(x...) X2(x), x
183#define X4(x...) X2(x), X2(x)
184#define X5(x...) X4(x), x
185#define X6(x...) X4(x), X2(x)
186#define X7(x...) X4(x), X3(x)
187#define X8(x...) X4(x), X4(x)
188#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300189
Avi Kivitye28bbd42013-01-04 16:18:48 +0200190#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
191#define FASTOP_SIZE 8
192
193/*
194 * fastop functions have a special calling convention:
195 *
Avi Kivity017da7b2013-02-09 11:31:47 +0200196 * dst: rax (in/out)
197 * src: rdx (in/out)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200198 * src2: rcx (in)
199 * flags: rflags (in/out)
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200200 * ex: rsi (in:fastop pointer, out:zero if exception)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200201 *
202 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
203 * different operand sizes can be reached by calculation, rather than a jump
204 * table (which would be bigger than the code).
205 *
206 * fastop functions are declared as taking a never-defined fastop parameter,
207 * so they can't be called from C directly.
208 */
209
210struct fastop;
211
Avi Kivityd65b1de2010-07-29 15:11:35 +0300212struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300213 u64 flags : 56;
214 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300215 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300216 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200217 const struct opcode *group;
218 const struct group_dual *gdual;
219 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200220 const struct escape *esc;
Nadav Amit39f062f2014-11-26 15:47:18 +0200221 const struct instr_dual *idual;
Nadav Amit2276b512015-01-26 09:32:24 +0200222 const struct mode_dual *mdual;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200223 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300224 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200225 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300226};
227
228struct group_dual {
229 struct opcode mod012[8];
230 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300231};
232
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200233struct gprefix {
234 struct opcode pfx_no;
235 struct opcode pfx_66;
236 struct opcode pfx_f2;
237 struct opcode pfx_f3;
238};
239
Gleb Natapov045a2822012-12-20 16:57:43 +0200240struct escape {
241 struct opcode op[8];
242 struct opcode high[64];
243};
244
Nadav Amit39f062f2014-11-26 15:47:18 +0200245struct instr_dual {
246 struct opcode mod012;
247 struct opcode mod3;
248};
249
Nadav Amit2276b512015-01-26 09:32:24 +0200250struct mode_dual {
251 struct opcode mode32;
252 struct opcode mode64;
253};
254
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300255#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300256
Nadav Amit3dc4bc42014-12-25 02:52:19 +0200257enum x86_transfer_type {
258 X86_TRANSFER_NONE,
259 X86_TRANSFER_CALL_JMP,
260 X86_TRANSFER_RET,
261 X86_TRANSFER_TASK_SWITCH,
262};
263
Avi Kivitydd856ef2012-08-27 23:46:17 +0300264static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
265{
266 if (!(ctxt->regs_valid & (1 << nr))) {
267 ctxt->regs_valid |= 1 << nr;
268 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
269 }
270 return ctxt->_regs[nr];
271}
272
273static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
274{
275 ctxt->regs_valid |= 1 << nr;
276 ctxt->regs_dirty |= 1 << nr;
277 return &ctxt->_regs[nr];
278}
279
280static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
281{
282 reg_read(ctxt, nr);
283 return reg_write(ctxt, nr);
284}
285
286static void writeback_registers(struct x86_emulate_ctxt *ctxt)
287{
288 unsigned reg;
289
290 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
291 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
292}
293
294static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
295{
296 ctxt->regs_dirty = 0;
297 ctxt->regs_valid = 0;
298}
299
Avi Kivity6aa8b732006-12-10 02:21:36 -0800300/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800301 * These EFLAGS bits are restored from saved value during emulation, and
302 * any changes are written back to the saved value after emulation.
303 */
Nadav Amit0efb0442015-03-29 16:33:03 +0300304#define EFLAGS_MASK (X86_EFLAGS_OF|X86_EFLAGS_SF|X86_EFLAGS_ZF|X86_EFLAGS_AF|\
305 X86_EFLAGS_PF|X86_EFLAGS_CF)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800306
Avi Kivitydda96d82008-11-26 15:14:10 +0200307#ifdef CONFIG_X86_64
308#define ON64(x) x
309#else
310#define ON64(x)
311#endif
312
Avi Kivity4d758342013-01-19 19:51:55 +0200313static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *));
314
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500315#define __FOP_FUNC(name) \
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600316 ".align " __stringify(FASTOP_SIZE) " \n\t" \
317 ".type " name ", @function \n\t" \
318 name ":\n\t"
319
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500320#define FOP_FUNC(name) \
321 __FOP_FUNC(#name)
322
323#define __FOP_RET(name) \
324 "ret \n\t" \
325 ".size " name ", .-" name "\n\t"
326
327#define FOP_RET(name) \
328 __FOP_RET(#name)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200329
330#define FOP_START(op) \
331 extern void em_##op(struct fastop *fake); \
332 asm(".pushsection .text, \"ax\" \n\t" \
333 ".global em_" #op " \n\t" \
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500334 ".align " __stringify(FASTOP_SIZE) " \n\t" \
335 "em_" #op ":\n\t"
Avi Kivityb7d491e2013-01-04 16:18:49 +0200336
337#define FOP_END \
338 ".popsection")
339
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500340#define __FOPNOP(name) \
341 __FOP_FUNC(name) \
342 __FOP_RET(name)
343
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600344#define FOPNOP() \
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500345 __FOPNOP(__stringify(__UNIQUE_ID(nop)))
Avi Kivity0bdea062013-01-19 19:51:50 +0200346
Avi Kivityb7d491e2013-01-04 16:18:49 +0200347#define FOP1E(op, dst) \
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500348 __FOP_FUNC(#op "_" #dst) \
349 "10: " #op " %" #dst " \n\t" \
350 __FOP_RET(#op "_" #dst)
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200351
352#define FOP1EEX(op, dst) \
353 FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200354
355#define FASTOP1(op) \
356 FOP_START(op) \
357 FOP1E(op##b, al) \
358 FOP1E(op##w, ax) \
359 FOP1E(op##l, eax) \
360 ON64(FOP1E(op##q, rax)) \
361 FOP_END
362
Avi Kivityb9fa4092013-02-09 11:31:48 +0200363/* 1-operand, using src2 (for MUL/DIV r/m) */
364#define FASTOP1SRC2(op, name) \
365 FOP_START(name) \
366 FOP1E(op, cl) \
367 FOP1E(op, cx) \
368 FOP1E(op, ecx) \
369 ON64(FOP1E(op, rcx)) \
370 FOP_END
371
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200372/* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */
373#define FASTOP1SRC2EX(op, name) \
374 FOP_START(name) \
375 FOP1EEX(op, cl) \
376 FOP1EEX(op, cx) \
377 FOP1EEX(op, ecx) \
378 ON64(FOP1EEX(op, rcx)) \
379 FOP_END
380
Avi Kivityf7857f32013-01-04 16:18:53 +0200381#define FOP2E(op, dst, src) \
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500382 __FOP_FUNC(#op "_" #dst "_" #src) \
383 #op " %" #src ", %" #dst " \n\t" \
384 __FOP_RET(#op "_" #dst "_" #src)
Avi Kivityf7857f32013-01-04 16:18:53 +0200385
386#define FASTOP2(op) \
387 FOP_START(op) \
Avi Kivity017da7b2013-02-09 11:31:47 +0200388 FOP2E(op##b, al, dl) \
389 FOP2E(op##w, ax, dx) \
390 FOP2E(op##l, eax, edx) \
391 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivityf7857f32013-01-04 16:18:53 +0200392 FOP_END
393
Avi Kivity11c363b2013-01-19 19:51:54 +0200394/* 2 operand, word only */
395#define FASTOP2W(op) \
396 FOP_START(op) \
397 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200398 FOP2E(op##w, ax, dx) \
399 FOP2E(op##l, eax, edx) \
400 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivity11c363b2013-01-19 19:51:54 +0200401 FOP_END
402
Avi Kivity007a3b52013-01-19 19:51:51 +0200403/* 2 operand, src is CL */
404#define FASTOP2CL(op) \
405 FOP_START(op) \
406 FOP2E(op##b, al, cl) \
407 FOP2E(op##w, ax, cl) \
408 FOP2E(op##l, eax, cl) \
409 ON64(FOP2E(op##q, rax, cl)) \
410 FOP_END
411
Nadav Amit5aca3722014-11-02 11:54:50 +0200412/* 2 operand, src and dest are reversed */
413#define FASTOP2R(op, name) \
414 FOP_START(name) \
415 FOP2E(op##b, dl, al) \
416 FOP2E(op##w, dx, ax) \
417 FOP2E(op##l, edx, eax) \
418 ON64(FOP2E(op##q, rdx, rax)) \
419 FOP_END
420
Avi Kivity0bdea062013-01-19 19:51:50 +0200421#define FOP3E(op, dst, src, src2) \
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500422 __FOP_FUNC(#op "_" #dst "_" #src "_" #src2) \
423 #op " %" #src2 ", %" #src ", %" #dst " \n\t"\
424 __FOP_RET(#op "_" #dst "_" #src "_" #src2)
Avi Kivity0bdea062013-01-19 19:51:50 +0200425
426/* 3-operand, word-only, src2=cl */
427#define FASTOP3WCL(op) \
428 FOP_START(op) \
429 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200430 FOP3E(op##w, ax, dx, cl) \
431 FOP3E(op##l, eax, edx, cl) \
432 ON64(FOP3E(op##q, rax, rdx, cl)) \
Avi Kivity0bdea062013-01-19 19:51:50 +0200433 FOP_END
434
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200435/* Special case for SETcc - 1 instruction per cc */
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600436#define FOP_SETCC(op) \
437 ".align 4 \n\t" \
438 ".type " #op ", @function \n\t" \
439 #op ": \n\t" \
440 #op " %al \n\t" \
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500441 __FOP_RET(#op)
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200442
Josh Poimboeuff26e6012017-10-04 10:39:05 -0500443asm(".pushsection .fixup, \"ax\"\n"
444 ".global kvm_fastop_exception \n"
445 "kvm_fastop_exception: xor %esi, %esi; ret\n"
446 ".popsection");
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200447
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200448FOP_START(setcc)
449FOP_SETCC(seto)
450FOP_SETCC(setno)
451FOP_SETCC(setc)
452FOP_SETCC(setnc)
453FOP_SETCC(setz)
454FOP_SETCC(setnz)
455FOP_SETCC(setbe)
456FOP_SETCC(setnbe)
457FOP_SETCC(sets)
458FOP_SETCC(setns)
459FOP_SETCC(setp)
460FOP_SETCC(setnp)
461FOP_SETCC(setl)
462FOP_SETCC(setnl)
463FOP_SETCC(setle)
464FOP_SETCC(setnle)
465FOP_END;
466
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500467FOP_START(salc)
468FOP_FUNC(salc)
469"pushf; sbb %al, %al; popf \n\t"
470FOP_RET(salc)
Paolo Bonzini326f5782013-05-09 11:32:51 +0200471FOP_END;
472
Radim Krčmářaabba3c2016-11-08 20:54:18 +0100473/*
474 * XXX: inoutclob user must know where the argument is being expanded.
Masahiro Yamadae9666d12018-12-31 00:14:15 +0900475 * Relying on CONFIG_CC_HAS_ASM_GOTO would allow us to remove _fault.
Radim Krčmářaabba3c2016-11-08 20:54:18 +0100476 */
477#define asm_safe(insn, inoutclob...) \
478({ \
479 int _fault = 0; \
480 \
481 asm volatile("1:" insn "\n" \
482 "2:\n" \
483 ".pushsection .fixup, \"ax\"\n" \
484 "3: movl $1, %[_fault]\n" \
485 " jmp 2b\n" \
486 ".popsection\n" \
487 _ASM_EXTABLE(1b, 3b) \
488 : [_fault] "+qm"(_fault) inoutclob ); \
489 \
490 _fault ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE; \
491})
492
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200493static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
494 enum x86_intercept intercept,
495 enum x86_intercept_stage stage)
496{
497 struct x86_instruction_info info = {
498 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300499 .rep_prefix = ctxt->rep_prefix,
500 .modrm_mod = ctxt->modrm_mod,
501 .modrm_reg = ctxt->modrm_reg,
502 .modrm_rm = ctxt->modrm_rm,
503 .src_val = ctxt->src.val64,
Jan Kiszka6cbc5f52014-06-30 12:52:55 +0200504 .dst_val = ctxt->dst.val64,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300505 .src_bytes = ctxt->src.bytes,
506 .dst_bytes = ctxt->dst.bytes,
507 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200508 .next_rip = ctxt->eip,
509 };
510
Avi Kivity29535382011-04-20 13:37:53 +0300511 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200512}
513
Avi Kivityf47cfa32012-06-07 17:49:24 +0300514static void assign_masked(ulong *dest, ulong src, ulong mask)
515{
516 *dest = (*dest & ~mask) | (src & mask);
517}
518
Nadav Amit6fd8e122015-03-30 15:39:20 +0300519static void assign_register(unsigned long *reg, u64 val, int bytes)
520{
521 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
522 switch (bytes) {
523 case 1:
524 *(u8 *)reg = (u8)val;
525 break;
526 case 2:
527 *(u16 *)reg = (u16)val;
528 break;
529 case 4:
530 *reg = (u32)val;
531 break; /* 64b: zero-extend */
532 case 8:
533 *reg = val;
534 break;
535 }
536}
537
Avi Kivity9dac77f2011-06-01 15:34:25 +0300538static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800539{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300540 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800541}
542
Avi Kivityf47cfa32012-06-07 17:49:24 +0300543static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
544{
545 u16 sel;
546 struct desc_struct ss;
547
548 if (ctxt->mode == X86EMUL_MODE_PROT64)
549 return ~0UL;
550 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
551 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
552}
553
Avi Kivity612e89f2012-06-12 20:03:23 +0300554static int stack_size(struct x86_emulate_ctxt *ctxt)
555{
556 return (__fls(stack_mask(ctxt)) + 1) >> 3;
557}
558
Avi Kivity6aa8b732006-12-10 02:21:36 -0800559/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800560static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300561address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800562{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300563 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800564 return reg;
565 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300566 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800567}
568
569static inline unsigned long
Paolo Bonzini01485a22014-11-19 18:25:08 +0100570register_address(struct x86_emulate_ctxt *ctxt, int reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800571{
Paolo Bonzini01485a22014-11-19 18:25:08 +0100572 return address_mask(ctxt, reg_read(ctxt, reg));
Harvey Harrisone4706772008-02-19 07:40:38 -0800573}
574
Avi Kivity5ad105e2012-08-19 14:34:31 +0300575static void masked_increment(ulong *reg, ulong mask, int inc)
576{
577 assign_masked(reg, *reg + inc, mask);
578}
579
Harvey Harrison7a9572752008-02-19 07:40:41 -0800580static inline void
Paolo Bonzini01485a22014-11-19 18:25:08 +0100581register_address_increment(struct x86_emulate_ctxt *ctxt, int reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800582{
Nadav Amitee122a72015-04-28 13:06:00 +0300583 ulong *preg = reg_rmw(ctxt, reg);
Avi Kivity5ad105e2012-08-19 14:34:31 +0300584
Nadav Amitee122a72015-04-28 13:06:00 +0300585 assign_register(preg, *preg + inc, ctxt->ad_bytes);
Avi Kivity5ad105e2012-08-19 14:34:31 +0300586}
587
588static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
589{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300590 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800591}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800592
Avi Kivity56697682011-04-03 14:08:51 +0300593static u32 desc_limit_scaled(struct desc_struct *desc)
594{
595 u32 limit = get_desc_limit(desc);
596
597 return desc->g ? (limit << 12) | 0xfff : limit;
598}
599
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900600static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300601{
602 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
603 return 0;
604
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900605 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300606}
607
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200608static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
609 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300610{
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +0200611 WARN_ON(vec > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +0200612 ctxt->exception.vector = vec;
613 ctxt->exception.error_code = error;
614 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200615 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300616}
617
Joerg Roedel3b88e412011-04-04 12:39:29 +0200618static int emulate_db(struct x86_emulate_ctxt *ctxt)
619{
620 return emulate_exception(ctxt, DB_VECTOR, 0, false);
621}
622
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200623static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300624{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200625 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300626}
627
Avi Kivity618ff152011-04-03 12:32:09 +0300628static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
629{
630 return emulate_exception(ctxt, SS_VECTOR, err, true);
631}
632
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200633static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300634{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200635 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300636}
637
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200638static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300639{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200640 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300641}
642
Avi Kivity34d1f492010-08-26 11:59:01 +0300643static int emulate_de(struct x86_emulate_ctxt *ctxt)
644{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200645 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300646}
647
Avi Kivity1253791d2011-03-29 11:41:27 +0200648static int emulate_nm(struct x86_emulate_ctxt *ctxt)
649{
650 return emulate_exception(ctxt, NM_VECTOR, 0, false);
651}
652
Avi Kivity1aa36612011-04-27 13:20:30 +0300653static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
654{
655 u16 selector;
656 struct desc_struct desc;
657
658 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
659 return selector;
660}
661
662static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
663 unsigned seg)
664{
665 u16 dummy;
666 u32 base3;
667 struct desc_struct desc;
668
669 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
670 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
671}
672
Avi Kivity1c11b372012-04-09 18:39:59 +0300673/*
674 * x86 defines three classes of vector instructions: explicitly
675 * aligned, explicitly unaligned, and the rest, which change behaviour
676 * depending on whether they're AVX encoded or not.
677 *
678 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100679 * subject to the same check. FXSAVE and FXRSTOR are checked here too as their
680 * 512 bytes of data must be aligned to a 16 byte boundary.
Avi Kivity1c11b372012-04-09 18:39:59 +0300681 */
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100682static unsigned insn_alignment(struct x86_emulate_ctxt *ctxt, unsigned size)
Avi Kivity1c11b372012-04-09 18:39:59 +0300683{
Radim Krčmář48520182016-11-08 20:54:17 +0100684 u64 alignment = ctxt->d & AlignMask;
Avi Kivity1c11b372012-04-09 18:39:59 +0300685
Avi Kivity1c11b372012-04-09 18:39:59 +0300686 if (likely(size < 16))
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100687 return 1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300688
Radim Krčmář48520182016-11-08 20:54:17 +0100689 switch (alignment) {
690 case Unaligned:
691 case Avx:
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100692 return 1;
Radim Krčmář48520182016-11-08 20:54:17 +0100693 case Aligned16:
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100694 return 16;
Radim Krčmář48520182016-11-08 20:54:17 +0100695 case Aligned:
696 default:
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100697 return size;
Radim Krčmář48520182016-11-08 20:54:17 +0100698 }
Avi Kivity1c11b372012-04-09 18:39:59 +0300699}
700
Paolo Bonzinid09155d2014-10-27 14:54:44 +0100701static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
702 struct segmented_address addr,
703 unsigned *max_size, unsigned size,
704 bool write, bool fetch,
Nadav Amitd50eaa12014-11-19 17:43:11 +0200705 enum x86emul_mode mode, ulong *linear)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300706{
Avi Kivity618ff152011-04-03 12:32:09 +0300707 struct desc_struct desc;
708 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300709 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300710 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300711 u16 sel;
Yu Zhangfd8cb432017-08-24 20:27:56 +0800712 u8 va_bits;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300713
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900714 la = seg_base(ctxt, addr.seg) + addr.ea;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100715 *max_size = 0;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200716 switch (mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300717 case X86EMUL_MODE_PROT64:
Paolo Bonzini0c1d77f2016-02-19 18:07:21 +0100718 *linear = la;
Yu Zhangfd8cb432017-08-24 20:27:56 +0800719 va_bits = ctxt_virt_addr_bits(ctxt);
720 if (get_canonical(la, va_bits) != la)
Nadav Amitabc7d8a2014-11-19 17:43:12 +0200721 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100722
Yu Zhangfd8cb432017-08-24 20:27:56 +0800723 *max_size = min_t(u64, ~0u, (1ull << va_bits) - la);
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100724 if (size > *max_size)
725 goto bad;
Avi Kivity618ff152011-04-03 12:32:09 +0300726 break;
727 default:
Paolo Bonzini0c1d77f2016-02-19 18:07:21 +0100728 *linear = la = (u32)la;
Avi Kivity1aa36612011-04-27 13:20:30 +0300729 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
730 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300731 if (!usable)
732 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200733 /* code segment in protected mode or read-only data segment */
734 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
735 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300736 goto bad;
737 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400738 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300739 goto bad;
740 lim = desc_limit_scaled(&desc);
Paolo Bonzini997b0412014-11-19 18:33:38 +0100741 if (!(desc.type & 8) && (desc.type & 4)) {
Guo Chaofc058682012-06-28 15:19:51 +0800742 /* expand-down segment */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100743 if (addr.ea <= lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300744 goto bad;
745 lim = desc.d ? 0xffffffff : 0xffff;
Avi Kivity618ff152011-04-03 12:32:09 +0300746 }
Paolo Bonzini997b0412014-11-19 18:33:38 +0100747 if (addr.ea > lim)
748 goto bad;
Nadav Amitbac155312015-01-26 09:32:26 +0200749 if (lim == 0xffffffff)
750 *max_size = ~0u;
751 else {
752 *max_size = (u64)lim + 1 - addr.ea;
753 if (size > *max_size)
754 goto bad;
755 }
Avi Kivity618ff152011-04-03 12:32:09 +0300756 break;
757 }
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100758 if (la & (insn_alignment(ctxt, size) - 1))
Avi Kivity1c11b372012-04-09 18:39:59 +0300759 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300760 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300761bad:
762 if (addr.seg == VCPU_SREG_SS)
Paolo Bonzini36061892014-10-27 14:40:49 +0100763 return emulate_ss(ctxt, 0);
Avi Kivity618ff152011-04-03 12:32:09 +0300764 else
Paolo Bonzini36061892014-10-27 14:40:49 +0100765 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300766}
767
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400768static int linearize(struct x86_emulate_ctxt *ctxt,
769 struct segmented_address addr,
770 unsigned size, bool write,
771 ulong *linear)
772{
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100773 unsigned max_size;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200774 return __linearize(ctxt, addr, &max_size, size, write, false,
775 ctxt->mode, linear);
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400776}
777
Nadav Amitd50eaa12014-11-19 17:43:11 +0200778static inline int assign_eip(struct x86_emulate_ctxt *ctxt, ulong dst,
779 enum x86emul_mode mode)
780{
781 ulong linear;
782 int rc;
783 unsigned max_size;
784 struct segmented_address addr = { .seg = VCPU_SREG_CS,
785 .ea = dst };
786
787 if (ctxt->op_bytes != sizeof(unsigned long))
788 addr.ea = dst & ((1UL << (ctxt->op_bytes << 3)) - 1);
789 rc = __linearize(ctxt, addr, &max_size, 1, false, true, mode, &linear);
790 if (rc == X86EMUL_CONTINUE)
791 ctxt->_eip = addr.ea;
792 return rc;
793}
794
795static inline int assign_eip_near(struct x86_emulate_ctxt *ctxt, ulong dst)
796{
797 return assign_eip(ctxt, dst, ctxt->mode);
798}
799
800static int assign_eip_far(struct x86_emulate_ctxt *ctxt, ulong dst,
801 const struct desc_struct *cs_desc)
802{
803 enum x86emul_mode mode = ctxt->mode;
Nadav Amit82268082015-01-26 09:32:27 +0200804 int rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200805
806#ifdef CONFIG_X86_64
Nadav Amit82268082015-01-26 09:32:27 +0200807 if (ctxt->mode >= X86EMUL_MODE_PROT16) {
808 if (cs_desc->l) {
809 u64 efer = 0;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200810
Nadav Amit82268082015-01-26 09:32:27 +0200811 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
812 if (efer & EFER_LMA)
813 mode = X86EMUL_MODE_PROT64;
814 } else
815 mode = X86EMUL_MODE_PROT32; /* temporary value */
Nadav Amitd50eaa12014-11-19 17:43:11 +0200816 }
817#endif
818 if (mode == X86EMUL_MODE_PROT16 || mode == X86EMUL_MODE_PROT32)
819 mode = cs_desc->d ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
Nadav Amit82268082015-01-26 09:32:27 +0200820 rc = assign_eip(ctxt, dst, mode);
821 if (rc == X86EMUL_CONTINUE)
822 ctxt->mode = mode;
823 return rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200824}
825
826static inline int jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
827{
828 return assign_eip_near(ctxt, ctxt->_eip + rel);
829}
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400830
Paolo Bonzini79367a62018-06-06 16:43:02 +0200831static int linear_read_system(struct x86_emulate_ctxt *ctxt, ulong linear,
832 void *data, unsigned size)
833{
Paolo Bonzini3c9fa242018-06-06 17:38:09 +0200834 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception, true);
Paolo Bonzini79367a62018-06-06 16:43:02 +0200835}
836
837static int linear_write_system(struct x86_emulate_ctxt *ctxt,
838 ulong linear, void *data,
839 unsigned int size)
840{
Paolo Bonzini3c9fa242018-06-06 17:38:09 +0200841 return ctxt->ops->write_std(ctxt, linear, data, size, &ctxt->exception, true);
Paolo Bonzini79367a62018-06-06 16:43:02 +0200842}
843
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200844static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
845 struct segmented_address addr,
846 void *data,
847 unsigned size)
848{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200849 int rc;
850 ulong linear;
851
Avi Kivity83b87952011-04-03 11:31:19 +0300852 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200853 if (rc != X86EMUL_CONTINUE)
854 return rc;
Paolo Bonzini3c9fa242018-06-06 17:38:09 +0200855 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception, false);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200856}
857
Steve Rutherford129a72a2017-01-11 18:28:29 -0800858static int segmented_write_std(struct x86_emulate_ctxt *ctxt,
859 struct segmented_address addr,
860 void *data,
861 unsigned int size)
862{
863 int rc;
864 ulong linear;
865
866 rc = linearize(ctxt, addr, size, true, &linear);
867 if (rc != X86EMUL_CONTINUE)
868 return rc;
Paolo Bonzini3c9fa242018-06-06 17:38:09 +0200869 return ctxt->ops->write_std(ctxt, linear, data, size, &ctxt->exception, false);
Steve Rutherford129a72a2017-01-11 18:28:29 -0800870}
871
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900872/*
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200873 * Prefetch the remaining bytes of the instruction without crossing page
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900874 * boundary if they are not in fetch_cache yet.
875 */
Paolo Bonzini9506d572014-05-06 13:05:25 +0200876static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
Avi Kivity62266862007-11-20 13:15:52 +0200877{
Avi Kivity62266862007-11-20 13:15:52 +0200878 int rc;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100879 unsigned size, max_size;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200880 unsigned long linear;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200881 int cur_size = ctxt->fetch.end - ctxt->fetch.data;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200882 struct segmented_address addr = { .seg = VCPU_SREG_CS,
Paolo Bonzini17052f12014-05-06 16:33:01 +0200883 .ea = ctxt->eip + cur_size };
884
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100885 /*
886 * We do not know exactly how many bytes will be needed, and
887 * __linearize is expensive, so fetch as much as possible. We
888 * just have to avoid going beyond the 15 byte limit, the end
889 * of the segment, or the end of the page.
890 *
891 * __linearize is called with size 0 so that it does not do any
892 * boundary check itself. Instead, we use max_size to check
893 * against op_size.
894 */
Nadav Amitd50eaa12014-11-19 17:43:11 +0200895 rc = __linearize(ctxt, addr, &max_size, 0, false, true, ctxt->mode,
896 &linear);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200897 if (unlikely(rc != X86EMUL_CONTINUE))
898 return rc;
899
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100900 size = min_t(unsigned, 15UL ^ cur_size, max_size);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200901 size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear));
Paolo Bonzini5cfc7e02014-05-06 13:05:25 +0200902
903 /*
904 * One instruction can only straddle two pages,
905 * and one has been loaded at the beginning of
906 * x86_decode_insn. So, if not enough bytes
907 * still, we must have hit the 15-byte boundary.
908 */
909 if (unlikely(size < op_size))
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100910 return emulate_gp(ctxt, 0);
911
Paolo Bonzini17052f12014-05-06 16:33:01 +0200912 rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end,
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200913 size, &ctxt->exception);
914 if (unlikely(rc != X86EMUL_CONTINUE))
915 return rc;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200916 ctxt->fetch.end += size;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900917 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200918}
919
Paolo Bonzini9506d572014-05-06 13:05:25 +0200920static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt,
921 unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200922{
Nadav Amit08da44a2014-10-03 01:10:04 +0300923 unsigned done_size = ctxt->fetch.end - ctxt->fetch.ptr;
924
925 if (unlikely(done_size < size))
926 return __do_insn_fetch_bytes(ctxt, size - done_size);
Paolo Bonzini9506d572014-05-06 13:05:25 +0200927 else
928 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200929}
930
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900931/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900932#define insn_fetch(_type, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200933({ _type _x; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200934 \
935 rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900936 if (rc != X86EMUL_CONTINUE) \
937 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200938 ctxt->_eip += sizeof(_type); \
Nick Desaulniers8616abc2017-06-27 19:37:14 -0700939 memcpy(&_x, ctxt->fetch.ptr, sizeof(_type)); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200940 ctxt->fetch.ptr += sizeof(_type); \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200941 _x; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900942})
943
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900944#define insn_fetch_arr(_arr, _size, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200945({ \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200946 rc = do_insn_fetch_bytes(_ctxt, _size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900947 if (rc != X86EMUL_CONTINUE) \
948 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200949 ctxt->_eip += (_size); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200950 memcpy(_arr, ctxt->fetch.ptr, _size); \
951 ctxt->fetch.ptr += (_size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900952})
953
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000954/*
955 * Given the 'reg' portion of a ModRM byte, and a register block, return a
956 * pointer into the block that addresses the relevant register.
957 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
958 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300959static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200960 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800961{
962 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200963 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800964
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300966 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
967 else
968 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969 return p;
970}
971
972static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200973 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974 u16 *size, unsigned long *address, int op_bytes)
975{
976 int rc;
977
978 if (op_bytes == 2)
979 op_bytes = 3;
980 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200981 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900982 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800983 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200984 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200985 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986 return rc;
987}
988
Avi Kivity34b77652013-01-19 19:51:56 +0200989FASTOP2(add);
990FASTOP2(or);
991FASTOP2(adc);
992FASTOP2(sbb);
993FASTOP2(and);
994FASTOP2(sub);
995FASTOP2(xor);
996FASTOP2(cmp);
997FASTOP2(test);
998
Avi Kivityb9fa4092013-02-09 11:31:48 +0200999FASTOP1SRC2(mul, mul_ex);
1000FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02001001FASTOP1SRC2EX(div, div_ex);
1002FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +02001003
Avi Kivity34b77652013-01-19 19:51:56 +02001004FASTOP3WCL(shld);
1005FASTOP3WCL(shrd);
1006
1007FASTOP2W(imul);
1008
1009FASTOP1(not);
1010FASTOP1(neg);
1011FASTOP1(inc);
1012FASTOP1(dec);
1013
1014FASTOP2CL(rol);
1015FASTOP2CL(ror);
1016FASTOP2CL(rcl);
1017FASTOP2CL(rcr);
1018FASTOP2CL(shl);
1019FASTOP2CL(shr);
1020FASTOP2CL(sar);
1021
1022FASTOP2W(bsf);
1023FASTOP2W(bsr);
1024FASTOP2W(bt);
1025FASTOP2W(bts);
1026FASTOP2W(btr);
1027FASTOP2W(btc);
1028
Avi Kivitye47a5f52013-02-09 11:31:51 +02001029FASTOP2(xadd);
1030
Nadav Amit5aca3722014-11-02 11:54:50 +02001031FASTOP2R(cmp, cmp_r);
1032
Nadav Amit900efe22015-03-30 15:39:21 +03001033static int em_bsf_c(struct x86_emulate_ctxt *ctxt)
1034{
1035 /* If src is zero, do not writeback, but update flags */
1036 if (ctxt->src.val == 0)
1037 ctxt->dst.type = OP_NONE;
1038 return fastop(ctxt, em_bsf);
1039}
1040
1041static int em_bsr_c(struct x86_emulate_ctxt *ctxt)
1042{
1043 /* If src is zero, do not writeback, but update flags */
1044 if (ctxt->src.val == 0)
1045 ctxt->dst.type = OP_NONE;
1046 return fastop(ctxt, em_bsr);
1047}
1048
Josh Poimboeufcb7390f2016-01-22 10:16:12 -06001049static __always_inline u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001050{
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001051 u8 rc;
1052 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001053
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001054 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Peter Zijlstra1a29b5b2018-01-25 10:58:13 +01001055 asm("push %[flags]; popf; " CALL_NOSPEC
1056 : "=a"(rc) : [thunk_target]"r"(fop), [flags]"r"(flags));
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001057 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001058}
1059
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001060static void fetch_register_operand(struct operand *op)
1061{
1062 switch (op->bytes) {
1063 case 1:
1064 op->val = *(u8 *)op->addr.reg;
1065 break;
1066 case 2:
1067 op->val = *(u16 *)op->addr.reg;
1068 break;
1069 case 4:
1070 op->val = *(u32 *)op->addr.reg;
1071 break;
1072 case 8:
1073 op->val = *(u64 *)op->addr.reg;
1074 break;
1075 }
1076}
1077
Avi Kivity1253791d2011-03-29 11:41:27 +02001078static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
1079{
Avi Kivity1253791d2011-03-29 11:41:27 +02001080 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001081 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
1082 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
1083 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
1084 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
1085 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
1086 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
1087 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
1088 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001089#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001090 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
1091 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
1092 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
1093 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
1094 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
1095 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
1096 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
1097 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001098#endif
1099 default: BUG();
1100 }
Avi Kivity1253791d2011-03-29 11:41:27 +02001101}
1102
1103static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
1104 int reg)
1105{
Avi Kivity1253791d2011-03-29 11:41:27 +02001106 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001107 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
1108 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
1109 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
1110 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
1111 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
1112 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
1113 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
1114 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001115#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001116 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
1117 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
1118 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
1119 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
1120 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
1121 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
1122 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
1123 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001124#endif
1125 default: BUG();
1126 }
Avi Kivity1253791d2011-03-29 11:41:27 +02001127}
1128
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001129static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1130{
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001131 switch (reg) {
1132 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1133 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1134 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1135 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1136 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1137 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1138 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1139 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1140 default: BUG();
1141 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001142}
1143
1144static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1145{
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001146 switch (reg) {
1147 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1148 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1149 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1150 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1151 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1152 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1153 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1154 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1155 default: BUG();
1156 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001157}
1158
Gleb Natapov045a2822012-12-20 16:57:43 +02001159static int em_fninit(struct x86_emulate_ctxt *ctxt)
1160{
1161 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1162 return emulate_nm(ctxt);
1163
Gleb Natapov045a2822012-12-20 16:57:43 +02001164 asm volatile("fninit");
Gleb Natapov045a2822012-12-20 16:57:43 +02001165 return X86EMUL_CONTINUE;
1166}
1167
1168static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1169{
1170 u16 fcw;
1171
1172 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1173 return emulate_nm(ctxt);
1174
Gleb Natapov045a2822012-12-20 16:57:43 +02001175 asm volatile("fnstcw %0": "+m"(fcw));
Gleb Natapov045a2822012-12-20 16:57:43 +02001176
Gleb Natapov045a2822012-12-20 16:57:43 +02001177 ctxt->dst.val = fcw;
1178
1179 return X86EMUL_CONTINUE;
1180}
1181
1182static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1183{
1184 u16 fsw;
1185
1186 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1187 return emulate_nm(ctxt);
1188
Gleb Natapov045a2822012-12-20 16:57:43 +02001189 asm volatile("fnstsw %0": "+m"(fsw));
Gleb Natapov045a2822012-12-20 16:57:43 +02001190
Gleb Natapov045a2822012-12-20 16:57:43 +02001191 ctxt->dst.val = fsw;
1192
1193 return X86EMUL_CONTINUE;
1194}
1195
Avi Kivity1253791d2011-03-29 11:41:27 +02001196static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001197 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001198{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001199 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001200
Avi Kivity9dac77f2011-06-01 15:34:25 +03001201 if (!(ctxt->d & ModRM))
1202 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity1253791d2011-03-29 11:41:27 +02001203
Avi Kivity9dac77f2011-06-01 15:34:25 +03001204 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001205 op->type = OP_XMM;
1206 op->bytes = 16;
1207 op->addr.xmm = reg;
1208 read_sse_reg(ctxt, &op->vec_val, reg);
1209 return;
1210 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001211 if (ctxt->d & Mmx) {
1212 reg &= 7;
1213 op->type = OP_MM;
1214 op->bytes = 8;
1215 op->addr.mm = reg;
1216 return;
1217 }
Avi Kivity1253791d2011-03-29 11:41:27 +02001218
Avi Kivity3c118e22007-10-31 10:27:04 +02001219 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001220 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1221 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1222
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001223 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001224 op->orig_val = op->val;
1225}
1226
Avi Kivitya6e34072012-06-10 17:15:39 +03001227static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1228{
1229 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1230 ctxt->modrm_seg = VCPU_SREG_SS;
1231}
1232
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001233static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001234 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001235{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001236 u8 sib;
Bandan Das02357bd2014-04-16 12:46:11 -04001237 int index_reg, base_reg, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001238 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001239 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001240
Bandan Das02357bd2014-04-16 12:46:11 -04001241 ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */
1242 index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */
1243 base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001244
Bandan Das02357bd2014-04-16 12:46:11 -04001245 ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001246 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
Bandan Das02357bd2014-04-16 12:46:11 -04001247 ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001248 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001249
Nadav Amit9b88ae92014-05-25 23:05:21 +03001250 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001251 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001252 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001253 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001254 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001255 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001256 op->type = OP_XMM;
1257 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001258 op->addr.xmm = ctxt->modrm_rm;
1259 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001260 return rc;
1261 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001262 if (ctxt->d & Mmx) {
1263 op->type = OP_MM;
1264 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001265 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001266 return rc;
1267 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001268 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001269 return rc;
1270 }
1271
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001272 op->type = OP_MEM;
1273
Avi Kivity9dac77f2011-06-01 15:34:25 +03001274 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001275 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1276 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1277 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1278 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001279
1280 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001281 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001282 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001283 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001284 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001285 break;
1286 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001287 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001288 break;
1289 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001290 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001291 break;
1292 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001293 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001294 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001295 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001296 break;
1297 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001298 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001299 break;
1300 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001301 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001302 break;
1303 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001304 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001305 break;
1306 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001307 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001308 break;
1309 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001310 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001311 break;
1312 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001313 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001314 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001315 break;
1316 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001317 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001318 break;
1319 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001320 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1321 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1322 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001323 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001324 } else {
1325 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001326 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001327 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001328 index_reg |= (sib >> 3) & 7;
1329 base_reg |= sib & 7;
1330 scale = sib >> 6;
1331
Avi Kivity9dac77f2011-06-01 15:34:25 +03001332 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001333 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001334 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001335 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001336 adjust_modrm_seg(ctxt, base_reg);
Nadav Amitab708092014-12-25 02:52:21 +02001337 /* Increment ESP on POP [ESP] */
1338 if ((ctxt->d & IncSP) &&
1339 base_reg == VCPU_REGS_RSP)
1340 modrm_ea += ctxt->op_bytes;
Avi Kivitya6e34072012-06-10 17:15:39 +03001341 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001342 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001343 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001344 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Nadav Amit5b38ab82014-11-02 11:54:41 +02001345 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity84411d82008-06-15 21:53:26 -07001346 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001347 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001348 } else {
1349 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001350 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001351 adjust_modrm_seg(ctxt, base_reg);
1352 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001353 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001354 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001355 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001356 break;
1357 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001358 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001359 break;
1360 }
1361 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001362 op->addr.mem.ea = modrm_ea;
Bandan Das41061cd2014-04-16 12:46:14 -04001363 if (ctxt->ad_bytes != 8)
1364 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
1365
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001366done:
1367 return rc;
1368}
1369
1370static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001371 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001372{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001373 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001374
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001375 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001376 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001377 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001378 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001379 break;
1380 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001381 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001382 break;
1383 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001384 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001385 break;
1386 }
1387done:
1388 return rc;
1389}
1390
Avi Kivity9dac77f2011-06-01 15:34:25 +03001391static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001392{
Sheng Yang7129eec2010-09-28 16:33:32 +08001393 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001394
Avi Kivity9dac77f2011-06-01 15:34:25 +03001395 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001396 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001397
Avi Kivity9dac77f2011-06-01 15:34:25 +03001398 if (ctxt->src.bytes == 2)
1399 sv = (s16)ctxt->src.val & (s16)mask;
1400 else if (ctxt->src.bytes == 4)
1401 sv = (s32)ctxt->src.val & (s32)mask;
Nadav Amit7dec5602014-06-15 16:12:57 +03001402 else
1403 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001404
Nadav Amit1c1c35a2014-11-19 17:43:09 +02001405 ctxt->dst.addr.mem.ea = address_mask(ctxt,
1406 ctxt->dst.addr.mem.ea + (sv >> 3));
Wei Yongjun35c843c2010-08-09 11:34:56 +08001407 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001408
1409 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001410 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001411}
1412
Gleb Natapov9de41572010-04-28 19:15:22 +03001413static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001414 unsigned long addr, void *dest, unsigned size)
1415{
1416 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001417 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001418
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001419 if (mc->pos < mc->end)
1420 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001421
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001422 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001423
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001424 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1425 &ctxt->exception);
1426 if (rc != X86EMUL_CONTINUE)
1427 return rc;
1428
1429 mc->end += size;
1430
1431read_cached:
1432 memcpy(dest, mc->data + mc->pos, size);
1433 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001434 return X86EMUL_CONTINUE;
1435}
1436
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001437static int segmented_read(struct x86_emulate_ctxt *ctxt,
1438 struct segmented_address addr,
1439 void *data,
1440 unsigned size)
1441{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001442 int rc;
1443 ulong linear;
1444
Avi Kivity83b87952011-04-03 11:31:19 +03001445 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001446 if (rc != X86EMUL_CONTINUE)
1447 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001448 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001449}
1450
1451static int segmented_write(struct x86_emulate_ctxt *ctxt,
1452 struct segmented_address addr,
1453 const void *data,
1454 unsigned size)
1455{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001456 int rc;
1457 ulong linear;
1458
Avi Kivity83b87952011-04-03 11:31:19 +03001459 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001460 if (rc != X86EMUL_CONTINUE)
1461 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001462 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1463 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001464}
1465
1466static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1467 struct segmented_address addr,
1468 const void *orig_data, const void *data,
1469 unsigned size)
1470{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001471 int rc;
1472 ulong linear;
1473
Avi Kivity83b87952011-04-03 11:31:19 +03001474 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001475 if (rc != X86EMUL_CONTINUE)
1476 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001477 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1478 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001479}
1480
Gleb Natapov7b262e92010-03-18 15:20:27 +02001481static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001482 unsigned int size, unsigned short port,
1483 void *dest)
1484{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001485 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001486
1487 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001488 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001489 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001490 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Nadav Amit0efb0442015-03-29 16:33:03 +03001491 in_page = (ctxt->eflags & X86_EFLAGS_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001492 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1493 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Mark Rustadb55a8142014-07-25 06:27:05 -07001494 n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001495 if (n == 0)
1496 n = 1;
1497 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001498 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001499 return 0;
1500 rc->end = n * size;
1501 }
1502
Nadav Amite6e39f02014-04-18 03:35:10 +03001503 if (ctxt->rep_prefix && (ctxt->d & String) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03001504 !(ctxt->eflags & X86_EFLAGS_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001505 ctxt->dst.data = rc->data + rc->pos;
1506 ctxt->dst.type = OP_MEM_STR;
1507 ctxt->dst.count = (rc->end - rc->pos) / size;
1508 rc->pos = rc->end;
1509 } else {
1510 memcpy(dest, rc->data + rc->pos, size);
1511 rc->pos += size;
1512 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001513 return 1;
1514}
1515
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001516static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1517 u16 index, struct desc_struct *desc)
1518{
1519 struct desc_ptr dt;
1520 ulong addr;
1521
1522 ctxt->ops->get_idt(ctxt, &dt);
1523
1524 if (dt.size < index * 8 + 7)
1525 return emulate_gp(ctxt, index << 3 | 0x2);
1526
1527 addr = dt.address + index * 8;
Jordan Borgner0e96f312018-10-28 12:58:28 +00001528 return linear_read_system(ctxt, addr, desc, sizeof(*desc));
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001529}
1530
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001531static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001532 u16 selector, struct desc_ptr *dt)
1533{
Mathias Krause0225fb52012-08-30 01:30:16 +02001534 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001535 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001536
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001537 if (selector & 1 << 2) {
1538 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001539 u16 sel;
1540
Jordan Borgner0e96f312018-10-28 12:58:28 +00001541 memset(dt, 0, sizeof(*dt));
Nadav Amit2eedcac2014-06-02 18:34:05 +03001542 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1543 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001544 return;
1545
1546 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001547 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001548 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001549 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001550}
1551
Nadav Amitedccda72014-12-25 02:52:23 +02001552static int get_descriptor_ptr(struct x86_emulate_ctxt *ctxt,
1553 u16 selector, ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001554{
1555 struct desc_ptr dt;
1556 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001557 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001558
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001559 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001560
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001561 if (dt.size < index * 8 + 7)
1562 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001563
1564 addr = dt.address + index * 8;
Nadav Amitedccda72014-12-25 02:52:23 +02001565
1566#ifdef CONFIG_X86_64
1567 if (addr >> 32 != 0) {
1568 u64 efer = 0;
1569
1570 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1571 if (!(efer & EFER_LMA))
1572 addr &= (u32)-1;
1573 }
1574#endif
1575
1576 *desc_addr_p = addr;
1577 return X86EMUL_CONTINUE;
1578}
1579
1580/* allowed just for 8 bytes segments */
1581static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1582 u16 selector, struct desc_struct *desc,
1583 ulong *desc_addr_p)
1584{
1585 int rc;
1586
1587 rc = get_descriptor_ptr(ctxt, selector, desc_addr_p);
1588 if (rc != X86EMUL_CONTINUE)
1589 return rc;
1590
Paolo Bonzini79367a62018-06-06 16:43:02 +02001591 return linear_read_system(ctxt, *desc_addr_p, desc, sizeof(*desc));
Nadav Amitedccda72014-12-25 02:52:23 +02001592}
1593
1594/* allowed just for 8 bytes segments */
1595static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1596 u16 selector, struct desc_struct *desc)
1597{
1598 int rc;
1599 ulong addr;
1600
1601 rc = get_descriptor_ptr(ctxt, selector, &addr);
1602 if (rc != X86EMUL_CONTINUE)
1603 return rc;
1604
Jordan Borgner0e96f312018-10-28 12:58:28 +00001605 return linear_write_system(ctxt, addr, desc, sizeof(*desc));
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001606}
1607
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001608static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Nadav Amitd1442d82014-09-18 22:39:39 +03001609 u16 selector, int seg, u8 cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001610 enum x86_transfer_type transfer,
Nadav Amitd1442d82014-09-18 22:39:39 +03001611 struct desc_struct *desc)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001612{
Avi Kivity869be992012-06-13 16:30:53 +03001613 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001614 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001615 unsigned err_vec = GP_VECTOR;
1616 u32 err_code = 0;
1617 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001618 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001619 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001620 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001621 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001622
Jordan Borgner0e96f312018-10-28 12:58:28 +00001623 memset(&seg_desc, 0, sizeof(seg_desc));
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001624
Kevin Wolff8da94e2013-04-11 14:06:03 +02001625 if (ctxt->mode == X86EMUL_MODE_REAL) {
1626 /* set real mode segment descriptor (keep limit etc. for
1627 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001628 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001629 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001630 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001631 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1632 /* VM86 needs a clean new segment descriptor */
1633 set_desc_base(&seg_desc, selector << 4);
1634 set_desc_limit(&seg_desc, 0xffff);
1635 seg_desc.type = 3;
1636 seg_desc.p = 1;
1637 seg_desc.s = 1;
1638 seg_desc.dpl = 3;
1639 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001640 }
1641
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001642 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001643
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001644 /* TR should be in GDT only */
1645 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1646 goto exception;
1647
Paolo Bonzini33ab9112017-01-12 15:02:32 +01001648 /* NULL selector is not valid for TR, CS and (except for long mode) SS */
1649 if (null_selector) {
1650 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_TR)
1651 goto exception;
1652
1653 if (seg == VCPU_SREG_SS) {
1654 if (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl)
1655 goto exception;
1656
1657 /*
1658 * ctxt->ops->set_segment expects the CPL to be in
1659 * SS.DPL, so fake an expand-up 32-bit data segment.
1660 */
1661 seg_desc.type = 3;
1662 seg_desc.p = 1;
1663 seg_desc.s = 1;
1664 seg_desc.dpl = cpl;
1665 seg_desc.d = 1;
1666 seg_desc.g = 1;
1667 }
1668
1669 /* Skip all following checks */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001670 goto load;
Paolo Bonzini33ab9112017-01-12 15:02:32 +01001671 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001672
Avi Kivitye9194642012-06-13 16:29:39 +03001673 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001674 if (ret != X86EMUL_CONTINUE)
1675 return ret;
1676
1677 err_code = selector & 0xfffc;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001678 err_vec = (transfer == X86_TRANSFER_TASK_SWITCH) ? TS_VECTOR :
1679 GP_VECTOR;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001680
Guo Chaofc058682012-06-28 15:19:51 +08001681 /* can't load system descriptor into segment selector */
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001682 if (seg <= VCPU_SREG_GS && !seg_desc.s) {
1683 if (transfer == X86_TRANSFER_CALL_JMP)
1684 return X86EMUL_UNHANDLEABLE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001685 goto exception;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001686 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001687
1688 if (!seg_desc.p) {
1689 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1690 goto exception;
1691 }
1692
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001693 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001694
1695 switch (seg) {
1696 case VCPU_SREG_SS:
1697 /*
1698 * segment is not a writable data segment or segment
1699 * selector's RPL != CPL or segment selector's RPL != CPL
1700 */
1701 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1702 goto exception;
1703 break;
1704 case VCPU_SREG_CS:
1705 if (!(seg_desc.type & 8))
1706 goto exception;
1707
1708 if (seg_desc.type & 4) {
1709 /* conforming */
1710 if (dpl > cpl)
1711 goto exception;
1712 } else {
1713 /* nonconforming */
1714 if (rpl > cpl || dpl != cpl)
1715 goto exception;
1716 }
Nadav Amit040c8dc2014-09-18 22:39:43 +03001717 /* in long-mode d/b must be clear if l is set */
1718 if (seg_desc.d && seg_desc.l) {
1719 u64 efer = 0;
1720
1721 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1722 if (efer & EFER_LMA)
1723 goto exception;
1724 }
1725
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001726 /* CS(RPL) <- CPL */
1727 selector = (selector & 0xfffc) | cpl;
1728 break;
1729 case VCPU_SREG_TR:
1730 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1731 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001732 old_desc = seg_desc;
1733 seg_desc.type |= 2; /* busy */
1734 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1735 sizeof(seg_desc), &ctxt->exception);
1736 if (ret != X86EMUL_CONTINUE)
1737 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001738 break;
1739 case VCPU_SREG_LDTR:
1740 if (seg_desc.s || seg_desc.type != 2)
1741 goto exception;
1742 break;
1743 default: /* DS, ES, FS, or GS */
1744 /*
1745 * segment is not a data or readable code segment or
1746 * ((segment is a data or nonconforming code segment)
1747 * and (both RPL and CPL > DPL))
1748 */
1749 if ((seg_desc.type & 0xa) == 0x8 ||
1750 (((seg_desc.type & 0xc) != 0xc) &&
1751 (rpl > dpl && cpl > dpl)))
1752 goto exception;
1753 break;
1754 }
1755
1756 if (seg_desc.s) {
1757 /* mark segment as accessed */
Nadav Amite2cefa72014-12-25 02:52:22 +02001758 if (!(seg_desc.type & 1)) {
1759 seg_desc.type |= 1;
1760 ret = write_segment_descriptor(ctxt, selector,
1761 &seg_desc);
1762 if (ret != X86EMUL_CONTINUE)
1763 return ret;
1764 }
Nadav Amite37a75a2014-06-02 18:34:04 +03001765 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
Paolo Bonzini79367a62018-06-06 16:43:02 +02001766 ret = linear_read_system(ctxt, desc_addr+8, &base3, sizeof(base3));
Nadav Amite37a75a2014-06-02 18:34:04 +03001767 if (ret != X86EMUL_CONTINUE)
1768 return ret;
Yu Zhangfd8cb432017-08-24 20:27:56 +08001769 if (emul_is_noncanonical_address(get_desc_base(&seg_desc) |
1770 ((u64)base3 << 32), ctxt))
Nadav Amit9a9abf62014-11-02 11:54:56 +02001771 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001772 }
1773load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001774 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Nadav Amitd1442d82014-09-18 22:39:39 +03001775 if (desc)
1776 *desc = seg_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001777 return X86EMUL_CONTINUE;
1778exception:
Paolo Bonzini592f0852014-08-20 10:05:08 +02001779 return emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001780}
1781
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001782static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1783 u16 selector, int seg)
1784{
1785 u8 cpl = ctxt->ops->cpl(ctxt);
Paolo Bonzini33ab9112017-01-12 15:02:32 +01001786
1787 /*
1788 * None of MOV, POP and LSS can load a NULL selector in CPL=3, but
1789 * they can load it at CPL<3 (Intel's manual says only LSS can,
1790 * but it's wrong).
1791 *
1792 * However, the Intel manual says that putting IST=1/DPL=3 in
1793 * an interrupt gate will result in SS=3 (the AMD manual instead
1794 * says it doesn't), so allow SS=3 in __load_segment_descriptor
1795 * and only forbid it here.
1796 */
1797 if (seg == VCPU_SREG_SS && selector == 3 &&
1798 ctxt->mode == X86EMUL_MODE_PROT64)
1799 return emulate_exception(ctxt, GP_VECTOR, 0, true);
1800
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001801 return __load_segment_descriptor(ctxt, selector, seg, cpl,
1802 X86_TRANSFER_NONE, NULL);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001803}
1804
Wei Yongjun31be40b2010-08-17 09:17:30 +08001805static void write_register_operand(struct operand *op)
1806{
Nadav Amit6fd8e122015-03-30 15:39:20 +03001807 return assign_register(op->addr.reg, op->val, op->bytes);
Wei Yongjun31be40b2010-08-17 09:17:30 +08001808}
1809
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001810static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001811{
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001812 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001813 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001814 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001815 break;
1816 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001817 if (ctxt->lock_prefix)
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001818 return segmented_cmpxchg(ctxt,
1819 op->addr.mem,
1820 &op->orig_val,
1821 &op->val,
1822 op->bytes);
1823 else
1824 return segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001825 op->addr.mem,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001826 &op->val,
1827 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001828 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001829 case OP_MEM_STR:
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001830 return segmented_write(ctxt,
1831 op->addr.mem,
1832 op->data,
1833 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001834 break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001835 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001836 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001837 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001838 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001839 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001840 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001841 case OP_NONE:
1842 /* no writeback */
1843 break;
1844 default:
1845 break;
1846 }
1847 return X86EMUL_CONTINUE;
1848}
1849
Avi Kivity51ddff52012-06-12 20:19:40 +03001850static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001851{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001852 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001853
Avi Kivity5ad105e2012-08-19 14:34:31 +03001854 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001855 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001856 addr.seg = VCPU_SREG_SS;
1857
Avi Kivity51ddff52012-06-12 20:19:40 +03001858 return segmented_write(ctxt, addr, data, bytes);
1859}
1860
1861static int em_push(struct x86_emulate_ctxt *ctxt)
1862{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001863 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001864 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001865 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001866}
1867
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001868static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001869 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001870{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001871 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001872 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001873
Avi Kivitydd856ef2012-08-27 23:46:17 +03001874 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001875 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001876 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001877 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001878 return rc;
1879
Avi Kivity5ad105e2012-08-19 14:34:31 +03001880 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001881 return rc;
1882}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001883
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001884static int em_pop(struct x86_emulate_ctxt *ctxt)
1885{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001886 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001887}
1888
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001889static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001890 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001891{
1892 int rc;
1893 unsigned long val, change_mask;
Nadav Amit0efb0442015-03-29 16:33:03 +03001894 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001895 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001896
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001897 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001898 if (rc != X86EMUL_CONTINUE)
1899 return rc;
1900
Nadav Amit0efb0442015-03-29 16:33:03 +03001901 change_mask = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
1902 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF |
1903 X86_EFLAGS_TF | X86_EFLAGS_DF | X86_EFLAGS_NT |
1904 X86_EFLAGS_AC | X86_EFLAGS_ID;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001905
1906 switch(ctxt->mode) {
1907 case X86EMUL_MODE_PROT64:
1908 case X86EMUL_MODE_PROT32:
1909 case X86EMUL_MODE_PROT16:
1910 if (cpl == 0)
Nadav Amit0efb0442015-03-29 16:33:03 +03001911 change_mask |= X86_EFLAGS_IOPL;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001912 if (cpl <= iopl)
Nadav Amit0efb0442015-03-29 16:33:03 +03001913 change_mask |= X86_EFLAGS_IF;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001914 break;
1915 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001916 if (iopl < 3)
1917 return emulate_gp(ctxt, 0);
Nadav Amit0efb0442015-03-29 16:33:03 +03001918 change_mask |= X86_EFLAGS_IF;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001919 break;
1920 default: /* real mode */
Nadav Amit0efb0442015-03-29 16:33:03 +03001921 change_mask |= (X86_EFLAGS_IOPL | X86_EFLAGS_IF);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001922 break;
1923 }
1924
1925 *(unsigned long *)dest =
1926 (ctxt->eflags & ~change_mask) | (val & change_mask);
1927
1928 return rc;
1929}
1930
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001931static int em_popf(struct x86_emulate_ctxt *ctxt)
1932{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001933 ctxt->dst.type = OP_REG;
1934 ctxt->dst.addr.reg = &ctxt->eflags;
1935 ctxt->dst.bytes = ctxt->op_bytes;
1936 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001937}
1938
Avi Kivity612e89f2012-06-12 20:03:23 +03001939static int em_enter(struct x86_emulate_ctxt *ctxt)
1940{
1941 int rc;
1942 unsigned frame_size = ctxt->src.val;
1943 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001944 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001945
1946 if (nesting_level)
1947 return X86EMUL_UNHANDLEABLE;
1948
Avi Kivitydd856ef2012-08-27 23:46:17 +03001949 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1950 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001951 if (rc != X86EMUL_CONTINUE)
1952 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001953 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001954 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001955 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1956 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001957 stack_mask(ctxt));
1958 return X86EMUL_CONTINUE;
1959}
1960
Avi Kivityf47cfa32012-06-07 17:49:24 +03001961static int em_leave(struct x86_emulate_ctxt *ctxt)
1962{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001963 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001964 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001965 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001966}
1967
Avi Kivity1cd196e2011-09-13 10:45:51 +03001968static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001969{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001970 int seg = ctxt->src2.val;
1971
Avi Kivity9dac77f2011-06-01 15:34:25 +03001972 ctxt->src.val = get_segment_selector(ctxt, seg);
Nadav Amit0fcc2072014-11-02 11:54:51 +02001973 if (ctxt->op_bytes == 4) {
1974 rsp_increment(ctxt, -2);
1975 ctxt->op_bytes = 2;
1976 }
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001977
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001978 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001979}
1980
Avi Kivity1cd196e2011-09-13 10:45:51 +03001981static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001982{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001983 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001984 unsigned long selector;
1985 int rc;
1986
Nadav Amit3313bc42014-12-25 02:52:17 +02001987 rc = emulate_pop(ctxt, &selector, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001988 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001989 return rc;
1990
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001991 if (ctxt->modrm_reg == VCPU_SREG_SS)
1992 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Nadav Amit3313bc42014-12-25 02:52:17 +02001993 if (ctxt->op_bytes > 2)
1994 rsp_increment(ctxt, ctxt->op_bytes - 2);
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001995
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001996 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001997 return rc;
1998}
1999
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09002000static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002001{
Avi Kivitydd856ef2012-08-27 23:46:17 +03002002 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08002003 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002004 int reg = VCPU_REGS_RAX;
2005
2006 while (reg <= VCPU_REGS_RDI) {
2007 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03002008 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002009
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002010 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08002011 if (rc != X86EMUL_CONTINUE)
2012 return rc;
2013
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002014 ++reg;
2015 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08002016
Wei Yongjunc37eda12010-06-15 09:03:33 +08002017 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002018}
2019
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09002020static int em_pushf(struct x86_emulate_ctxt *ctxt)
2021{
Nadav Amit0efb0442015-03-29 16:33:03 +03002022 ctxt->src.val = (unsigned long)ctxt->eflags & ~X86_EFLAGS_VM;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09002023 return em_push(ctxt);
2024}
2025
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09002026static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002027{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002028 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002029 int reg = VCPU_REGS_RDI;
Nadav Amit6fd8e122015-03-30 15:39:20 +03002030 u32 val;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002031
2032 while (reg >= VCPU_REGS_RAX) {
2033 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03002034 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002035 --reg;
2036 }
2037
Nadav Amit6fd8e122015-03-30 15:39:20 +03002038 rc = emulate_pop(ctxt, &val, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002039 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002040 break;
Nadav Amit6fd8e122015-03-30 15:39:20 +03002041 assign_register(reg_rmw(ctxt, reg), val, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002042 --reg;
2043 }
2044 return rc;
2045}
2046
Avi Kivitydd856ef2012-08-27 23:46:17 +03002047static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002048{
Mathias Krause0225fb52012-08-30 01:30:16 +02002049 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03002050 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002051 struct desc_ptr dt;
2052 gva_t cs_addr;
2053 gva_t eip_addr;
2054 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002055
2056 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002057 ctxt->src.val = ctxt->eflags;
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;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002061
Nadav Amit0efb0442015-03-29 16:33:03 +03002062 ctxt->eflags &= ~(X86_EFLAGS_IF | X86_EFLAGS_TF | X86_EFLAGS_AC);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002063
Avi Kivity9dac77f2011-06-01 15:34:25 +03002064 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002065 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03002066 if (rc != X86EMUL_CONTINUE)
2067 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002068
Avi Kivity9dac77f2011-06-01 15:34:25 +03002069 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002070 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03002071 if (rc != X86EMUL_CONTINUE)
2072 return rc;
2073
Avi Kivity4bff1e862011-04-20 13:37:53 +03002074 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002075
2076 eip_addr = dt.address + (irq << 2);
2077 cs_addr = dt.address + (irq << 2) + 2;
2078
Paolo Bonzini79367a62018-06-06 16:43:02 +02002079 rc = linear_read_system(ctxt, cs_addr, &cs, 2);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002080 if (rc != X86EMUL_CONTINUE)
2081 return rc;
2082
Paolo Bonzini79367a62018-06-06 16:43:02 +02002083 rc = linear_read_system(ctxt, eip_addr, &eip, 2);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002084 if (rc != X86EMUL_CONTINUE)
2085 return rc;
2086
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002087 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002088 if (rc != X86EMUL_CONTINUE)
2089 return rc;
2090
Avi Kivity9dac77f2011-06-01 15:34:25 +03002091 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002092
2093 return rc;
2094}
2095
Avi Kivitydd856ef2012-08-27 23:46:17 +03002096int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
2097{
2098 int rc;
2099
2100 invalidate_registers(ctxt);
2101 rc = __emulate_int_real(ctxt, irq);
2102 if (rc == X86EMUL_CONTINUE)
2103 writeback_registers(ctxt);
2104 return rc;
2105}
2106
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002107static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002108{
2109 switch(ctxt->mode) {
2110 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03002111 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002112 case X86EMUL_MODE_VM86:
2113 case X86EMUL_MODE_PROT16:
2114 case X86EMUL_MODE_PROT32:
2115 case X86EMUL_MODE_PROT64:
2116 default:
2117 /* Protected mode interrupts unimplemented yet */
2118 return X86EMUL_UNHANDLEABLE;
2119 }
2120}
2121
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002122static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002123{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002124 int rc = X86EMUL_CONTINUE;
2125 unsigned long temp_eip = 0;
2126 unsigned long temp_eflags = 0;
2127 unsigned long cs = 0;
Nadav Amit0efb0442015-03-29 16:33:03 +03002128 unsigned long mask = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
2129 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_TF |
2130 X86_EFLAGS_IF | X86_EFLAGS_DF | X86_EFLAGS_OF |
2131 X86_EFLAGS_IOPL | X86_EFLAGS_NT | X86_EFLAGS_RF |
2132 X86_EFLAGS_AC | X86_EFLAGS_ID |
Wanpeng Li35fd68a2015-04-08 14:08:14 +08002133 X86_EFLAGS_FIXED;
Nadav Amit0efb0442015-03-29 16:33:03 +03002134 unsigned long vm86_mask = X86_EFLAGS_VM | X86_EFLAGS_VIF |
2135 X86_EFLAGS_VIP;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002136
2137 /* TODO: Add stack limit check */
2138
Avi Kivity9dac77f2011-06-01 15:34:25 +03002139 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002140
2141 if (rc != X86EMUL_CONTINUE)
2142 return rc;
2143
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002144 if (temp_eip & ~0xffff)
2145 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002146
Avi Kivity9dac77f2011-06-01 15:34:25 +03002147 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002148
2149 if (rc != X86EMUL_CONTINUE)
2150 return rc;
2151
Avi Kivity9dac77f2011-06-01 15:34:25 +03002152 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002153
2154 if (rc != X86EMUL_CONTINUE)
2155 return rc;
2156
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002157 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002158
2159 if (rc != X86EMUL_CONTINUE)
2160 return rc;
2161
Avi Kivity9dac77f2011-06-01 15:34:25 +03002162 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002163
Avi Kivity9dac77f2011-06-01 15:34:25 +03002164 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002165 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03002166 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002167 ctxt->eflags &= ~0xffff;
2168 ctxt->eflags |= temp_eflags;
2169 }
2170
2171 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
Wanpeng Li35fd68a2015-04-08 14:08:14 +08002172 ctxt->eflags |= X86_EFLAGS_FIXED;
Nadav Amit801806d2015-01-26 09:32:23 +02002173 ctxt->ops->set_nmi_mask(ctxt, false);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002174
2175 return rc;
2176}
2177
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002178static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002179{
2180 switch(ctxt->mode) {
2181 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002182 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002183 case X86EMUL_MODE_VM86:
2184 case X86EMUL_MODE_PROT16:
2185 case X86EMUL_MODE_PROT32:
2186 case X86EMUL_MODE_PROT64:
2187 default:
2188 /* iret from protected mode unimplemented yet */
2189 return X86EMUL_UNHANDLEABLE;
2190 }
2191}
2192
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002193static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2194{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002195 int rc;
Radim Krčmář2117d532016-11-23 21:15:00 +01002196 unsigned short sel;
2197 struct desc_struct new_desc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002198 u8 cpl = ctxt->ops->cpl(ctxt);
2199
Avi Kivity9dac77f2011-06-01 15:34:25 +03002200 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002201
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002202 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
2203 X86_TRANSFER_CALL_JMP,
Nadav Amitd1442d82014-09-18 22:39:39 +03002204 &new_desc);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002205 if (rc != X86EMUL_CONTINUE)
2206 return rc;
2207
Nadav Amitd50eaa12014-11-19 17:43:11 +02002208 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Radim Krčmář2117d532016-11-23 21:15:00 +01002209 /* Error handling is not implemented. */
2210 if (rc != X86EMUL_CONTINUE)
2211 return X86EMUL_UNHANDLEABLE;
2212
Nadav Amitd1442d82014-09-18 22:39:39 +03002213 return rc;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002214}
2215
Nadav Amitf7784042014-09-18 22:39:41 +03002216static int em_jmp_abs(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002217{
Nadav Amitf7784042014-09-18 22:39:41 +03002218 return assign_eip_near(ctxt, ctxt->src.val);
2219}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002220
Nadav Amitf7784042014-09-18 22:39:41 +03002221static int em_call_near_abs(struct x86_emulate_ctxt *ctxt)
2222{
2223 int rc;
2224 long int old_eip;
2225
2226 old_eip = ctxt->_eip;
2227 rc = assign_eip_near(ctxt, ctxt->src.val);
2228 if (rc != X86EMUL_CONTINUE)
2229 return rc;
2230 ctxt->src.val = old_eip;
2231 rc = em_push(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002232 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002233}
2234
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002235static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002236{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002237 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002238
Nadav Amitaaa05f22014-06-02 18:34:10 +03002239 if (ctxt->dst.bytes == 16)
2240 return X86EMUL_UNHANDLEABLE;
2241
Avi Kivitydd856ef2012-08-27 23:46:17 +03002242 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2243 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2244 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2245 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Nadav Amit0efb0442015-03-29 16:33:03 +03002246 ctxt->eflags &= ~X86_EFLAGS_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002247 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002248 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2249 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002250
Nadav Amit0efb0442015-03-29 16:33:03 +03002251 ctxt->eflags |= X86_EFLAGS_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002252 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002253 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002254}
2255
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002256static int em_ret(struct x86_emulate_ctxt *ctxt)
2257{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002258 int rc;
2259 unsigned long eip;
2260
2261 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
2262 if (rc != X86EMUL_CONTINUE)
2263 return rc;
2264
2265 return assign_eip_near(ctxt, eip);
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002266}
2267
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002268static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002269{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002270 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002271 unsigned long eip, cs;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002272 int cpl = ctxt->ops->cpl(ctxt);
Radim Krčmář2117d532016-11-23 21:15:00 +01002273 struct desc_struct new_desc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002274
2275 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002276 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002277 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002278 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002279 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002280 return rc;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002281 /* Outer-privilege level return is not implemented */
2282 if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2283 return X86EMUL_UNHANDLEABLE;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002284 rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, cpl,
2285 X86_TRANSFER_RET,
Nadav Amitd1442d82014-09-18 22:39:39 +03002286 &new_desc);
2287 if (rc != X86EMUL_CONTINUE)
2288 return rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +02002289 rc = assign_eip_far(ctxt, eip, &new_desc);
Radim Krčmář2117d532016-11-23 21:15:00 +01002290 /* Error handling is not implemented. */
2291 if (rc != X86EMUL_CONTINUE)
2292 return X86EMUL_UNHANDLEABLE;
2293
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002294 return rc;
2295}
2296
Bruce Rogers32611072013-09-09 09:40:20 -06002297static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2298{
2299 int rc;
2300
2301 rc = em_ret_far(ctxt);
2302 if (rc != X86EMUL_CONTINUE)
2303 return rc;
2304 rsp_increment(ctxt, ctxt->src.val);
2305 return X86EMUL_CONTINUE;
2306}
2307
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002308static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2309{
2310 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002311 ctxt->dst.orig_val = ctxt->dst.val;
2312 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002313 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002314 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002315 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002316
Nadav Amit0efb0442015-03-29 16:33:03 +03002317 if (ctxt->eflags & X86_EFLAGS_ZF) {
Nadav Amit2fcf5c82015-01-26 09:32:21 +02002318 /* Success: write back to memory; no update of EAX */
2319 ctxt->src.type = OP_NONE;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002320 ctxt->dst.val = ctxt->src.orig_val;
2321 } else {
2322 /* Failure: write the value we saw to EAX. */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02002323 ctxt->src.type = OP_REG;
2324 ctxt->src.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
2325 ctxt->src.val = ctxt->dst.orig_val;
2326 /* Create write-cycle to dest by writing the same value */
Nadav Amit37c564f2014-06-02 18:34:07 +03002327 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002328 }
2329 return X86EMUL_CONTINUE;
2330}
2331
Avi Kivityd4b43252011-09-13 10:45:50 +03002332static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002333{
Avi Kivityd4b43252011-09-13 10:45:50 +03002334 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002335 unsigned short sel;
2336 int rc;
2337
Avi Kivity9dac77f2011-06-01 15:34:25 +03002338 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002339
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002340 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002341 if (rc != X86EMUL_CONTINUE)
2342 return rc;
2343
Avi Kivity9dac77f2011-06-01 15:34:25 +03002344 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002345 return rc;
2346}
2347
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002348static int emulator_has_longmode(struct x86_emulate_ctxt *ctxt)
2349{
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002350#ifdef CONFIG_X86_64
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002351 u32 eax, ebx, ecx, edx;
2352
2353 eax = 0x80000001;
2354 ecx = 0;
Yu Zhange911eb32017-08-24 20:27:52 +08002355 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, false);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002356 return edx & bit(X86_FEATURE_LM);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002357#else
2358 return false;
2359#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002360}
2361
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002362static void rsm_set_desc_flags(struct desc_struct *desc, u32 flags)
2363{
2364 desc->g = (flags >> 23) & 1;
2365 desc->d = (flags >> 22) & 1;
2366 desc->l = (flags >> 21) & 1;
2367 desc->avl = (flags >> 20) & 1;
2368 desc->p = (flags >> 15) & 1;
2369 desc->dpl = (flags >> 13) & 3;
2370 desc->s = (flags >> 12) & 1;
2371 desc->type = (flags >> 8) & 15;
2372}
2373
Sean Christophersoned193212019-04-02 08:03:09 -07002374static int rsm_load_seg_32(struct x86_emulate_ctxt *ctxt, const char *smstate,
2375 int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002376{
2377 struct desc_struct desc;
2378 int offset;
2379 u16 selector;
2380
Sean Christophersoned193212019-04-02 08:03:09 -07002381 selector = GET_SMSTATE(u32, smstate, 0x7fa8 + n * 4);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002382
2383 if (n < 3)
2384 offset = 0x7f84 + n * 12;
2385 else
2386 offset = 0x7f2c + (n - 3) * 12;
2387
Sean Christophersoned193212019-04-02 08:03:09 -07002388 set_desc_base(&desc, GET_SMSTATE(u32, smstate, offset + 8));
2389 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, offset + 4));
2390 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smstate, offset));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002391 ctxt->ops->set_segment(ctxt, selector, &desc, 0, n);
2392 return X86EMUL_CONTINUE;
2393}
2394
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002395#ifdef CONFIG_X86_64
Sean Christophersoned193212019-04-02 08:03:09 -07002396static int rsm_load_seg_64(struct x86_emulate_ctxt *ctxt, const char *smstate,
2397 int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002398{
2399 struct desc_struct desc;
2400 int offset;
2401 u16 selector;
2402 u32 base3;
2403
2404 offset = 0x7e00 + n * 16;
2405
Sean Christophersoned193212019-04-02 08:03:09 -07002406 selector = GET_SMSTATE(u16, smstate, offset);
2407 rsm_set_desc_flags(&desc, GET_SMSTATE(u16, smstate, offset + 2) << 8);
2408 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, offset + 4));
2409 set_desc_base(&desc, GET_SMSTATE(u32, smstate, offset + 8));
2410 base3 = GET_SMSTATE(u32, smstate, offset + 12);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002411
2412 ctxt->ops->set_segment(ctxt, selector, &desc, base3, n);
2413 return X86EMUL_CONTINUE;
2414}
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002415#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002416
2417static int rsm_enter_protected_mode(struct x86_emulate_ctxt *ctxt,
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002418 u64 cr0, u64 cr3, u64 cr4)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002419{
2420 int bad;
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002421 u64 pcid;
2422
2423 /* In order to later set CR4.PCIDE, CR3[11:0] must be zero. */
2424 pcid = 0;
2425 if (cr4 & X86_CR4_PCIDE) {
2426 pcid = cr3 & 0xfff;
2427 cr3 &= ~0xfff;
2428 }
2429
2430 bad = ctxt->ops->set_cr(ctxt, 3, cr3);
2431 if (bad)
2432 return X86EMUL_UNHANDLEABLE;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002433
2434 /*
2435 * First enable PAE, long mode needs it before CR0.PG = 1 is set.
2436 * Then enable protected mode. However, PCID cannot be enabled
2437 * if EFER.LMA=0, so set it separately.
2438 */
2439 bad = ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PCIDE);
2440 if (bad)
2441 return X86EMUL_UNHANDLEABLE;
2442
2443 bad = ctxt->ops->set_cr(ctxt, 0, cr0);
2444 if (bad)
2445 return X86EMUL_UNHANDLEABLE;
2446
2447 if (cr4 & X86_CR4_PCIDE) {
2448 bad = ctxt->ops->set_cr(ctxt, 4, cr4);
2449 if (bad)
2450 return X86EMUL_UNHANDLEABLE;
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002451 if (pcid) {
2452 bad = ctxt->ops->set_cr(ctxt, 3, cr3 | pcid);
2453 if (bad)
2454 return X86EMUL_UNHANDLEABLE;
2455 }
2456
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002457 }
2458
2459 return X86EMUL_CONTINUE;
2460}
2461
Sean Christophersoned193212019-04-02 08:03:09 -07002462static int rsm_load_state_32(struct x86_emulate_ctxt *ctxt,
2463 const char *smstate)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002464{
2465 struct desc_struct desc;
2466 struct desc_ptr dt;
2467 u16 selector;
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002468 u32 val, cr0, cr3, cr4;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002469 int i;
2470
Sean Christophersoned193212019-04-02 08:03:09 -07002471 cr0 = GET_SMSTATE(u32, smstate, 0x7ffc);
2472 cr3 = GET_SMSTATE(u32, smstate, 0x7ff8);
2473 ctxt->eflags = GET_SMSTATE(u32, smstate, 0x7ff4) | X86_EFLAGS_FIXED;
2474 ctxt->_eip = GET_SMSTATE(u32, smstate, 0x7ff0);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002475
2476 for (i = 0; i < 8; i++)
Sean Christophersoned193212019-04-02 08:03:09 -07002477 *reg_write(ctxt, i) = GET_SMSTATE(u32, smstate, 0x7fd0 + i * 4);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002478
Sean Christophersoned193212019-04-02 08:03:09 -07002479 val = GET_SMSTATE(u32, smstate, 0x7fcc);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002480 ctxt->ops->set_dr(ctxt, 6, (val & DR6_VOLATILE) | DR6_FIXED_1);
Sean Christophersoned193212019-04-02 08:03:09 -07002481 val = GET_SMSTATE(u32, smstate, 0x7fc8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002482 ctxt->ops->set_dr(ctxt, 7, (val & DR7_VOLATILE) | DR7_FIXED_1);
2483
Sean Christophersoned193212019-04-02 08:03:09 -07002484 selector = GET_SMSTATE(u32, smstate, 0x7fc4);
2485 set_desc_base(&desc, GET_SMSTATE(u32, smstate, 0x7f64));
2486 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, 0x7f60));
2487 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smstate, 0x7f5c));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002488 ctxt->ops->set_segment(ctxt, selector, &desc, 0, VCPU_SREG_TR);
2489
Sean Christophersoned193212019-04-02 08:03:09 -07002490 selector = GET_SMSTATE(u32, smstate, 0x7fc0);
2491 set_desc_base(&desc, GET_SMSTATE(u32, smstate, 0x7f80));
2492 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, 0x7f7c));
2493 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smstate, 0x7f78));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002494 ctxt->ops->set_segment(ctxt, selector, &desc, 0, VCPU_SREG_LDTR);
2495
Sean Christophersoned193212019-04-02 08:03:09 -07002496 dt.address = GET_SMSTATE(u32, smstate, 0x7f74);
2497 dt.size = GET_SMSTATE(u32, smstate, 0x7f70);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002498 ctxt->ops->set_gdt(ctxt, &dt);
2499
Sean Christophersoned193212019-04-02 08:03:09 -07002500 dt.address = GET_SMSTATE(u32, smstate, 0x7f58);
2501 dt.size = GET_SMSTATE(u32, smstate, 0x7f54);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002502 ctxt->ops->set_idt(ctxt, &dt);
2503
2504 for (i = 0; i < 6; i++) {
Sean Christophersoned193212019-04-02 08:03:09 -07002505 int r = rsm_load_seg_32(ctxt, smstate, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002506 if (r != X86EMUL_CONTINUE)
2507 return r;
2508 }
2509
Sean Christophersoned193212019-04-02 08:03:09 -07002510 cr4 = GET_SMSTATE(u32, smstate, 0x7f14);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002511
Sean Christophersoned193212019-04-02 08:03:09 -07002512 ctxt->ops->set_smbase(ctxt, GET_SMSTATE(u32, smstate, 0x7ef8));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002513
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002514 return rsm_enter_protected_mode(ctxt, cr0, cr3, cr4);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002515}
2516
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002517#ifdef CONFIG_X86_64
Sean Christophersoned193212019-04-02 08:03:09 -07002518static int rsm_load_state_64(struct x86_emulate_ctxt *ctxt,
2519 const char *smstate)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002520{
2521 struct desc_struct desc;
2522 struct desc_ptr dt;
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002523 u64 val, cr0, cr3, cr4;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002524 u32 base3;
2525 u16 selector;
Paolo Bonzinib10d92a2015-10-14 15:25:52 +02002526 int i, r;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002527
2528 for (i = 0; i < 16; i++)
Sean Christophersoned193212019-04-02 08:03:09 -07002529 *reg_write(ctxt, i) = GET_SMSTATE(u64, smstate, 0x7ff8 - i * 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002530
Sean Christophersoned193212019-04-02 08:03:09 -07002531 ctxt->_eip = GET_SMSTATE(u64, smstate, 0x7f78);
2532 ctxt->eflags = GET_SMSTATE(u32, smstate, 0x7f70) | X86_EFLAGS_FIXED;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002533
Sean Christophersoned193212019-04-02 08:03:09 -07002534 val = GET_SMSTATE(u32, smstate, 0x7f68);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002535 ctxt->ops->set_dr(ctxt, 6, (val & DR6_VOLATILE) | DR6_FIXED_1);
Sean Christophersoned193212019-04-02 08:03:09 -07002536 val = GET_SMSTATE(u32, smstate, 0x7f60);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002537 ctxt->ops->set_dr(ctxt, 7, (val & DR7_VOLATILE) | DR7_FIXED_1);
2538
Sean Christophersoned193212019-04-02 08:03:09 -07002539 cr0 = GET_SMSTATE(u64, smstate, 0x7f58);
2540 cr3 = GET_SMSTATE(u64, smstate, 0x7f50);
2541 cr4 = GET_SMSTATE(u64, smstate, 0x7f48);
2542 ctxt->ops->set_smbase(ctxt, GET_SMSTATE(u32, smstate, 0x7f00));
2543 val = GET_SMSTATE(u64, smstate, 0x7ed0);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002544 ctxt->ops->set_msr(ctxt, MSR_EFER, val & ~EFER_LMA);
2545
Sean Christophersoned193212019-04-02 08:03:09 -07002546 selector = GET_SMSTATE(u32, smstate, 0x7e90);
2547 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smstate, 0x7e92) << 8);
2548 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, 0x7e94));
2549 set_desc_base(&desc, GET_SMSTATE(u32, smstate, 0x7e98));
2550 base3 = GET_SMSTATE(u32, smstate, 0x7e9c);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002551 ctxt->ops->set_segment(ctxt, selector, &desc, base3, VCPU_SREG_TR);
2552
Sean Christophersoned193212019-04-02 08:03:09 -07002553 dt.size = GET_SMSTATE(u32, smstate, 0x7e84);
2554 dt.address = GET_SMSTATE(u64, smstate, 0x7e88);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002555 ctxt->ops->set_idt(ctxt, &dt);
2556
Sean Christophersoned193212019-04-02 08:03:09 -07002557 selector = GET_SMSTATE(u32, smstate, 0x7e70);
2558 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smstate, 0x7e72) << 8);
2559 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, 0x7e74));
2560 set_desc_base(&desc, GET_SMSTATE(u32, smstate, 0x7e78));
2561 base3 = GET_SMSTATE(u32, smstate, 0x7e7c);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002562 ctxt->ops->set_segment(ctxt, selector, &desc, base3, VCPU_SREG_LDTR);
2563
Sean Christophersoned193212019-04-02 08:03:09 -07002564 dt.size = GET_SMSTATE(u32, smstate, 0x7e64);
2565 dt.address = GET_SMSTATE(u64, smstate, 0x7e68);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002566 ctxt->ops->set_gdt(ctxt, &dt);
2567
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002568 r = rsm_enter_protected_mode(ctxt, cr0, cr3, cr4);
Paolo Bonzinib10d92a2015-10-14 15:25:52 +02002569 if (r != X86EMUL_CONTINUE)
2570 return r;
2571
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002572 for (i = 0; i < 6; i++) {
Sean Christophersoned193212019-04-02 08:03:09 -07002573 r = rsm_load_seg_64(ctxt, smstate, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002574 if (r != X86EMUL_CONTINUE)
2575 return r;
2576 }
2577
Paolo Bonzinib10d92a2015-10-14 15:25:52 +02002578 return X86EMUL_CONTINUE;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002579}
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002580#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002581
Paolo Bonzini64d60672015-05-07 11:36:11 +02002582static int em_rsm(struct x86_emulate_ctxt *ctxt)
2583{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002584 unsigned long cr0, cr4, efer;
Sean Christophersoned193212019-04-02 08:03:09 -07002585 char buf[512];
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002586 u64 smbase;
2587 int ret;
2588
Ladi Prosek6ed071f2017-04-25 16:42:44 +02002589 if ((ctxt->ops->get_hflags(ctxt) & X86EMUL_SMM_MASK) == 0)
Paolo Bonzini64d60672015-05-07 11:36:11 +02002590 return emulate_ud(ctxt);
2591
Sean Christophersoned193212019-04-02 08:03:09 -07002592 smbase = ctxt->ops->get_smbase(ctxt);
2593
2594 ret = ctxt->ops->read_phys(ctxt, smbase + 0xfe00, buf, sizeof(buf));
2595 if (ret != X86EMUL_CONTINUE)
2596 return X86EMUL_UNHANDLEABLE;
2597
Sean Christopherson9ec19492019-04-02 08:03:11 -07002598 if ((ctxt->ops->get_hflags(ctxt) & X86EMUL_SMM_INSIDE_NMI_MASK) == 0)
2599 ctxt->ops->set_nmi_mask(ctxt, false);
2600
2601 ctxt->ops->set_hflags(ctxt, ctxt->ops->get_hflags(ctxt) &
2602 ~(X86EMUL_SMM_INSIDE_NMI_MASK | X86EMUL_SMM_MASK));
2603
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002604 /*
2605 * Get back to real mode, to prepare a safe state in which to load
Paolo Bonzini89651a32015-11-03 13:43:05 +01002606 * CR0/CR3/CR4/EFER. It's all a bit more complicated if the vCPU
2607 * supports long mode.
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002608 */
Paolo Bonzini89651a32015-11-03 13:43:05 +01002609 if (emulator_has_longmode(ctxt)) {
2610 struct desc_struct cs_desc;
2611
2612 /* Zero CR4.PCIDE before CR0.PG. */
Sean Christopherson8f4dc2e2019-04-02 08:10:47 -07002613 cr4 = ctxt->ops->get_cr(ctxt, 4);
2614 if (cr4 & X86_CR4_PCIDE)
Paolo Bonzini89651a32015-11-03 13:43:05 +01002615 ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PCIDE);
Paolo Bonzini89651a32015-11-03 13:43:05 +01002616
2617 /* A 32-bit code segment is required to clear EFER.LMA. */
2618 memset(&cs_desc, 0, sizeof(cs_desc));
2619 cs_desc.type = 0xb;
2620 cs_desc.s = cs_desc.g = cs_desc.p = 1;
2621 ctxt->ops->set_segment(ctxt, 0, &cs_desc, 0, VCPU_SREG_CS);
2622 }
2623
2624 /* For the 64-bit case, this will clear EFER.LMA. */
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002625 cr0 = ctxt->ops->get_cr(ctxt, 0);
2626 if (cr0 & X86_CR0_PE)
2627 ctxt->ops->set_cr(ctxt, 0, cr0 & ~(X86_CR0_PG | X86_CR0_PE));
Paolo Bonzini89651a32015-11-03 13:43:05 +01002628
Sean Christopherson8f4dc2e2019-04-02 08:10:47 -07002629 if (emulator_has_longmode(ctxt)) {
2630 /* Clear CR4.PAE before clearing EFER.LME. */
2631 cr4 = ctxt->ops->get_cr(ctxt, 4);
2632 if (cr4 & X86_CR4_PAE)
2633 ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PAE);
Paolo Bonzini89651a32015-11-03 13:43:05 +01002634
Sean Christopherson8f4dc2e2019-04-02 08:10:47 -07002635 /* And finally go back to 32-bit mode. */
2636 efer = 0;
2637 ctxt->ops->set_msr(ctxt, MSR_EFER, efer);
2638 }
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002639
Ladi Prosek0234bf82017-10-11 16:54:40 +02002640 /*
2641 * Give pre_leave_smm() a chance to make ISA-specific changes to the
2642 * vCPU state (e.g. enter guest mode) before loading state from the SMM
2643 * state-save area.
2644 */
Sean Christophersoned193212019-04-02 08:03:09 -07002645 if (ctxt->ops->pre_leave_smm(ctxt, buf))
Ladi Prosek0234bf82017-10-11 16:54:40 +02002646 return X86EMUL_UNHANDLEABLE;
2647
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002648#ifdef CONFIG_X86_64
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002649 if (emulator_has_longmode(ctxt))
Sean Christophersoned193212019-04-02 08:03:09 -07002650 ret = rsm_load_state_64(ctxt, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002651 else
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002652#endif
Sean Christophersoned193212019-04-02 08:03:09 -07002653 ret = rsm_load_state_32(ctxt, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002654
2655 if (ret != X86EMUL_CONTINUE) {
2656 /* FIXME: should triple fault */
2657 return X86EMUL_UNHANDLEABLE;
2658 }
2659
Sean Christophersonc5833c72019-04-02 08:03:10 -07002660 ctxt->ops->post_leave_smm(ctxt);
2661
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002662 return X86EMUL_CONTINUE;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002663}
2664
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002665static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002666setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002667 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002668{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002669 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002670 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002671 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002672 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002673 cs->type = 0x0b; /* Read, Execute, Accessed */
2674 cs->s = 1;
2675 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002676 cs->p = 1;
2677 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002678 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002679
Gleb Natapov79168fd2010-04-28 19:15:30 +03002680 set_desc_base(ss, 0); /* flat segment */
2681 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002682 ss->g = 1; /* 4kb granularity */
2683 ss->s = 1;
2684 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002685 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002686 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002687 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002688 ss->l = 0;
2689 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002690}
2691
Avi Kivity1a18a692012-02-01 12:23:21 +02002692static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2693{
2694 u32 eax, ebx, ecx, edx;
2695
2696 eax = ecx = 0;
Yu Zhange911eb32017-08-24 20:27:52 +08002697 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, false);
Avi Kivity0017f932012-06-07 14:10:16 +03002698 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002699 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2700 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2701}
2702
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002703static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2704{
Mathias Krause0225fb52012-08-30 01:30:16 +02002705 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002706 u32 eax, ebx, ecx, edx;
2707
2708 /*
2709 * syscall should always be enabled in longmode - so only become
2710 * vendor specific (cpuid) if other modes are active...
2711 */
2712 if (ctxt->mode == X86EMUL_MODE_PROT64)
2713 return true;
2714
2715 eax = 0x00000000;
2716 ecx = 0x00000000;
Yu Zhange911eb32017-08-24 20:27:52 +08002717 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, false);
Avi Kivity0017f932012-06-07 14:10:16 +03002718 /*
2719 * Intel ("GenuineIntel")
2720 * remark: Intel CPUs only support "syscall" in 64bit
2721 * longmode. Also an 64bit guest with a
2722 * 32bit compat-app running will #UD !! While this
2723 * behaviour can be fixed (by emulating) into AMD
2724 * response - CPUs of AMD can't behave like Intel.
2725 */
2726 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2727 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2728 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2729 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002730
Avi Kivity0017f932012-06-07 14:10:16 +03002731 /* AMD ("AuthenticAMD") */
2732 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2733 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2734 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2735 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002736
Avi Kivity0017f932012-06-07 14:10:16 +03002737 /* AMD ("AMDisbetter!") */
2738 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2739 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2740 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2741 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002742
Pu Wenb8f4abb2018-09-23 17:36:31 +08002743 /* Hygon ("HygonGenuine") */
2744 if (ebx == X86EMUL_CPUID_VENDOR_HygonGenuine_ebx &&
2745 ecx == X86EMUL_CPUID_VENDOR_HygonGenuine_ecx &&
2746 edx == X86EMUL_CPUID_VENDOR_HygonGenuine_edx)
2747 return true;
2748
2749 /*
2750 * default: (not Intel, not AMD, not Hygon), apply Intel's
2751 * stricter rules...
2752 */
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002753 return false;
2754}
2755
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002756static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002757{
Mathias Krause0225fb52012-08-30 01:30:16 +02002758 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002759 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002760 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002761 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002762 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002763
2764 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002765 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002766 ctxt->mode == X86EMUL_MODE_VM86)
2767 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002768
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002769 if (!(em_syscall_is_enabled(ctxt)))
2770 return emulate_ud(ctxt);
2771
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002772 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002773 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002774
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002775 if (!(efer & EFER_SCE))
2776 return emulate_ud(ctxt);
2777
Avi Kivity717746e2011-04-20 13:37:53 +03002778 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002779 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002780 cs_sel = (u16)(msr_data & 0xfffc);
2781 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002782
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002783 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002784 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002785 cs.l = 1;
2786 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002787 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2788 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002789
Avi Kivitydd856ef2012-08-27 23:46:17 +03002790 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002791 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002792#ifdef CONFIG_X86_64
Nadav Amit6c6cb692014-07-21 14:37:30 +03002793 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002794
Avi Kivity717746e2011-04-20 13:37:53 +03002795 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002796 ctxt->mode == X86EMUL_MODE_PROT64 ?
2797 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002798 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002799
Avi Kivity717746e2011-04-20 13:37:53 +03002800 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Nadav Amit6c6cb692014-07-21 14:37:30 +03002801 ctxt->eflags &= ~msr_data;
Wanpeng Li35fd68a2015-04-08 14:08:14 +08002802 ctxt->eflags |= X86_EFLAGS_FIXED;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002803#endif
2804 } else {
2805 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002806 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002807 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002808
Nadav Amit0efb0442015-03-29 16:33:03 +03002809 ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002810 }
2811
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02002812 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002813 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002814}
2815
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002816static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002817{
Mathias Krause0225fb52012-08-30 01:30:16 +02002818 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002819 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002820 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002821 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002822 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002823
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002824 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002825 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002826 if (ctxt->mode == X86EMUL_MODE_REAL)
2827 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002828
Avi Kivity1a18a692012-02-01 12:23:21 +02002829 /*
2830 * Not recognized on AMD in compat mode (but is recognized in legacy
2831 * mode).
2832 */
Nadav Amitf3747372015-01-01 23:11:11 +02002833 if ((ctxt->mode != X86EMUL_MODE_PROT64) && (efer & EFER_LMA)
Avi Kivity1a18a692012-02-01 12:23:21 +02002834 && !vendor_intel(ctxt))
2835 return emulate_ud(ctxt);
2836
Nadav Amitb2c9d432014-11-02 11:55:01 +02002837 /* sysenter/sysexit have not been tested in 64bit mode. */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002838 if (ctxt->mode == X86EMUL_MODE_PROT64)
Nadav Amitb2c9d432014-11-02 11:55:01 +02002839 return X86EMUL_UNHANDLEABLE;
Andre Przywara8c604352009-06-18 12:56:01 +02002840
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002841 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002842
Avi Kivity717746e2011-04-20 13:37:53 +03002843 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002844 if ((msr_data & 0xfffc) == 0x0)
2845 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002846
Nadav Amit0efb0442015-03-29 16:33:03 +03002847 ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
Nadav Amitb32a9912015-03-29 16:33:04 +03002848 cs_sel = (u16)msr_data & ~SEGMENT_RPL_MASK;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002849 ss_sel = cs_sel + 8;
Nadav Amitf3747372015-01-01 23:11:11 +02002850 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002851 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002852 cs.l = 1;
2853 }
2854
Avi Kivity1aa36612011-04-27 13:20:30 +03002855 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2856 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002857
Avi Kivity717746e2011-04-20 13:37:53 +03002858 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002859 ctxt->_eip = (efer & EFER_LMA) ? msr_data : (u32)msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002860
Avi Kivity717746e2011-04-20 13:37:53 +03002861 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002862 *reg_write(ctxt, VCPU_REGS_RSP) = (efer & EFER_LMA) ? msr_data :
2863 (u32)msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002864
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002865 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002866}
2867
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002868static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002869{
Mathias Krause0225fb52012-08-30 01:30:16 +02002870 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002871 struct desc_struct cs, ss;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002872 u64 msr_data, rcx, rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002873 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002874 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002875
Gleb Natapova0044752010-02-10 14:21:31 +02002876 /* inject #GP if in real mode or Virtual 8086 mode */
2877 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002878 ctxt->mode == X86EMUL_MODE_VM86)
2879 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002880
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002881 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002882
Avi Kivity9dac77f2011-06-01 15:34:25 +03002883 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002884 usermode = X86EMUL_MODE_PROT64;
2885 else
2886 usermode = X86EMUL_MODE_PROT32;
2887
Nadav Amit234f3ce2014-09-18 22:39:38 +03002888 rcx = reg_read(ctxt, VCPU_REGS_RCX);
2889 rdx = reg_read(ctxt, VCPU_REGS_RDX);
2890
Andre Przywara4668f052009-06-18 12:56:02 +02002891 cs.dpl = 3;
2892 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002893 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002894 switch (usermode) {
2895 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002896 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002897 if ((msr_data & 0xfffc) == 0x0)
2898 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002899 ss_sel = (u16)(msr_data + 24);
Nadav Amitbf0b6822014-09-18 22:39:45 +03002900 rcx = (u32)rcx;
2901 rdx = (u32)rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002902 break;
2903 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002904 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002905 if (msr_data == 0x0)
2906 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002907 ss_sel = cs_sel + 8;
2908 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002909 cs.l = 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08002910 if (emul_is_noncanonical_address(rcx, ctxt) ||
2911 emul_is_noncanonical_address(rdx, ctxt))
Nadav Amit234f3ce2014-09-18 22:39:38 +03002912 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002913 break;
2914 }
Nadav Amitb32a9912015-03-29 16:33:04 +03002915 cs_sel |= SEGMENT_RPL_MASK;
2916 ss_sel |= SEGMENT_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002917
Avi Kivity1aa36612011-04-27 13:20:30 +03002918 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2919 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002920
Nadav Amit234f3ce2014-09-18 22:39:38 +03002921 ctxt->_eip = rdx;
2922 *reg_write(ctxt, VCPU_REGS_RSP) = rcx;
Andre Przywara4668f052009-06-18 12:56:02 +02002923
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002924 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002925}
2926
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002927static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002928{
2929 int iopl;
2930 if (ctxt->mode == X86EMUL_MODE_REAL)
2931 return false;
2932 if (ctxt->mode == X86EMUL_MODE_VM86)
2933 return true;
Nadav Amit0efb0442015-03-29 16:33:03 +03002934 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002935 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002936}
2937
Liran Alon9a29d4492018-03-12 13:12:48 +02002938#define VMWARE_PORT_VMPORT (0x5658)
2939#define VMWARE_PORT_VMRPC (0x5659)
2940
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002941static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002942 u16 port, u16 len)
2943{
Mathias Krause0225fb52012-08-30 01:30:16 +02002944 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002945 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002946 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002947 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002948 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002949 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002950 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002951
Liran Alon9a29d4492018-03-12 13:12:48 +02002952 /*
2953 * VMware allows access to these ports even if denied
2954 * by TSS I/O permission bitmap. Mimic behavior.
2955 */
2956 if (enable_vmware_backdoor &&
2957 ((port == VMWARE_PORT_VMPORT) || (port == VMWARE_PORT_VMRPC)))
2958 return true;
2959
Avi Kivity1aa36612011-04-27 13:20:30 +03002960 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002961 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002962 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002963 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002964 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002965 base = get_desc_base(&tr_seg);
2966#ifdef CONFIG_X86_64
2967 base |= ((u64)base3) << 32;
2968#endif
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02002969 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL, true);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002970 if (r != X86EMUL_CONTINUE)
2971 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002972 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002973 return false;
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02002974 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL, true);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002975 if (r != X86EMUL_CONTINUE)
2976 return false;
2977 if ((perm >> bit_idx) & mask)
2978 return false;
2979 return true;
2980}
2981
2982static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002983 u16 port, u16 len)
2984{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002985 if (ctxt->perm_ok)
2986 return true;
2987
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002988 if (emulator_bad_iopl(ctxt))
2989 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002990 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002991
2992 ctxt->perm_ok = true;
2993
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002994 return true;
2995}
2996
Nadav Amit428e3d02015-04-28 13:06:01 +03002997static void string_registers_quirk(struct x86_emulate_ctxt *ctxt)
2998{
2999 /*
3000 * Intel CPUs mask the counter and pointers in quite strange
3001 * manner when ECX is zero due to REP-string optimizations.
3002 */
3003#ifdef CONFIG_X86_64
3004 if (ctxt->ad_bytes != 4 || !vendor_intel(ctxt))
3005 return;
3006
3007 *reg_write(ctxt, VCPU_REGS_RCX) = 0;
3008
3009 switch (ctxt->b) {
3010 case 0xa4: /* movsb */
3011 case 0xa5: /* movsd/w */
3012 *reg_rmw(ctxt, VCPU_REGS_RSI) &= (u32)-1;
3013 /* fall through */
3014 case 0xaa: /* stosb */
3015 case 0xab: /* stosd/w */
3016 *reg_rmw(ctxt, VCPU_REGS_RDI) &= (u32)-1;
3017 }
3018#endif
3019}
3020
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003021static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003022 struct tss_segment_16 *tss)
3023{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003024 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003025 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003026 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
3027 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
3028 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
3029 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
3030 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
3031 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
3032 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
3033 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003034
Avi Kivity1aa36612011-04-27 13:20:30 +03003035 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
3036 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
3037 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
3038 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
3039 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003040}
3041
3042static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003043 struct tss_segment_16 *tss)
3044{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003045 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003046 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003047
Avi Kivity9dac77f2011-06-01 15:34:25 +03003048 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003049 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003050 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
3051 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
3052 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
3053 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
3054 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
3055 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
3056 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
3057 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003058
3059 /*
3060 * SDM says that segment selectors are loaded before segment
3061 * descriptors
3062 */
Avi Kivity1aa36612011-04-27 13:20:30 +03003063 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
3064 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
3065 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
3066 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
3067 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003068
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003069 cpl = tss->cs & 3;
3070
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003071 /*
Guo Chaofc058682012-06-28 15:19:51 +08003072 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003073 * it is handled in a context of new task
3074 */
Nadav Amitd1442d82014-09-18 22:39:39 +03003075 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003076 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003077 if (ret != X86EMUL_CONTINUE)
3078 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003079 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003080 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003081 if (ret != X86EMUL_CONTINUE)
3082 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003083 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003084 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003085 if (ret != X86EMUL_CONTINUE)
3086 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003087 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003088 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003089 if (ret != X86EMUL_CONTINUE)
3090 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003091 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003092 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003093 if (ret != X86EMUL_CONTINUE)
3094 return ret;
3095
3096 return X86EMUL_CONTINUE;
3097}
3098
3099static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003100 u16 tss_selector, u16 old_tss_sel,
3101 ulong old_tss_base, struct desc_struct *new_desc)
3102{
3103 struct tss_segment_16 tss_seg;
3104 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003105 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003106
Jordan Borgner0e96f312018-10-28 12:58:28 +00003107 ret = linear_read_system(ctxt, old_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003108 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003109 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003110
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003111 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003112
Jordan Borgner0e96f312018-10-28 12:58:28 +00003113 ret = linear_write_system(ctxt, old_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003114 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003115 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003116
Jordan Borgner0e96f312018-10-28 12:58:28 +00003117 ret = linear_read_system(ctxt, new_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003118 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003119 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003120
3121 if (old_tss_sel != 0xffff) {
3122 tss_seg.prev_task_link = old_tss_sel;
3123
Paolo Bonzini79367a62018-06-06 16:43:02 +02003124 ret = linear_write_system(ctxt, new_tss_base,
3125 &tss_seg.prev_task_link,
Jordan Borgner0e96f312018-10-28 12:58:28 +00003126 sizeof(tss_seg.prev_task_link));
Avi Kivitydb297e32010-11-22 17:53:24 +02003127 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003128 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003129 }
3130
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003131 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003132}
3133
3134static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003135 struct tss_segment_32 *tss)
3136{
Nadav Amit5c7411e2014-04-07 18:37:47 +03003137 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003138 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003139 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003140 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
3141 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
3142 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
3143 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
3144 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
3145 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
3146 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
3147 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003148
Avi Kivity1aa36612011-04-27 13:20:30 +03003149 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
3150 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
3151 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
3152 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
3153 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
3154 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003155}
3156
3157static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003158 struct tss_segment_32 *tss)
3159{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003160 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003161 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003162
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003163 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003164 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003165 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003166 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01003167
3168 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03003169 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
3170 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
3171 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
3172 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
3173 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
3174 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
3175 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
3176 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003177
3178 /*
3179 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003180 * descriptors. This is important because CPL checks will
3181 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003182 */
Avi Kivity1aa36612011-04-27 13:20:30 +03003183 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
3184 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
3185 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
3186 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
3187 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
3188 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
3189 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003190
3191 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01003192 * If we're switching between Protected Mode and VM86, we need to make
3193 * sure to update the mode before loading the segment descriptors so
3194 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01003195 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003196 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01003197 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003198 cpl = 3;
3199 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01003200 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003201 cpl = tss->cs & 3;
3202 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01003203
3204 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003205 * Now load segment descriptors. If fault happenes at this stage
3206 * it is handled in a context of new task
3207 */
Nadav Amitd1442d82014-09-18 22:39:39 +03003208 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003209 cpl, X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003210 if (ret != X86EMUL_CONTINUE)
3211 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003212 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003213 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003214 if (ret != X86EMUL_CONTINUE)
3215 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003216 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003217 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003218 if (ret != X86EMUL_CONTINUE)
3219 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003220 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003221 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003222 if (ret != X86EMUL_CONTINUE)
3223 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003224 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003225 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003226 if (ret != X86EMUL_CONTINUE)
3227 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003228 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003229 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003230 if (ret != X86EMUL_CONTINUE)
3231 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003232 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003233 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003234
Eugene Korenevsky2f729b12015-03-29 01:27:17 +03003235 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003236}
3237
3238static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003239 u16 tss_selector, u16 old_tss_sel,
3240 ulong old_tss_base, struct desc_struct *new_desc)
3241{
3242 struct tss_segment_32 tss_seg;
3243 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003244 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03003245 u32 eip_offset = offsetof(struct tss_segment_32, eip);
3246 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003247
Jordan Borgner0e96f312018-10-28 12:58:28 +00003248 ret = linear_read_system(ctxt, old_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003249 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003250 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003251
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003252 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003253
Nadav Amit5c7411e2014-04-07 18:37:47 +03003254 /* Only GP registers and segment selectors are saved */
Paolo Bonzini79367a62018-06-06 16:43:02 +02003255 ret = linear_write_system(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
3256 ldt_sel_offset - eip_offset);
Avi Kivitydb297e32010-11-22 17:53:24 +02003257 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003258 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003259
Jordan Borgner0e96f312018-10-28 12:58:28 +00003260 ret = linear_read_system(ctxt, new_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003261 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003262 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003263
3264 if (old_tss_sel != 0xffff) {
3265 tss_seg.prev_task_link = old_tss_sel;
3266
Paolo Bonzini79367a62018-06-06 16:43:02 +02003267 ret = linear_write_system(ctxt, new_tss_base,
3268 &tss_seg.prev_task_link,
Jordan Borgner0e96f312018-10-28 12:58:28 +00003269 sizeof(tss_seg.prev_task_link));
Avi Kivitydb297e32010-11-22 17:53:24 +02003270 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003271 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003272 }
3273
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003274 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003275}
3276
3277static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003278 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003279 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003280{
Mathias Krause0225fb52012-08-30 01:30:16 +02003281 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003282 struct desc_struct curr_tss_desc, next_tss_desc;
3283 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03003284 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003285 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03003286 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02003287 u32 desc_limit;
Nadav Amit3db176d2015-04-19 21:12:59 +03003288 ulong desc_addr, dr7;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003289
3290 /* FIXME: old_tss_base == ~0 ? */
3291
Avi Kivitye9194642012-06-13 16:29:39 +03003292 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003293 if (ret != X86EMUL_CONTINUE)
3294 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03003295 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003296 if (ret != X86EMUL_CONTINUE)
3297 return ret;
3298
3299 /* FIXME: check that next_tss_desc is tss */
3300
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003301 /*
3302 * Check privileges. The three cases are task switch caused by...
3303 *
3304 * 1. jmp/call/int to task gate: Check against DPL of the task gate
3305 * 2. Exception/IRQ/iret: No check is performed
Nadav Amit2c2ca2d2014-11-02 11:54:57 +02003306 * 3. jmp/call to TSS/task-gate: No check is performed since the
3307 * hardware checks it before exiting.
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003308 */
3309 if (reason == TASK_SWITCH_GATE) {
3310 if (idt_index != -1) {
3311 /* Software interrupts */
3312 struct desc_struct task_gate_desc;
3313 int dpl;
3314
3315 ret = read_interrupt_descriptor(ctxt, idt_index,
3316 &task_gate_desc);
3317 if (ret != X86EMUL_CONTINUE)
3318 return ret;
3319
3320 dpl = task_gate_desc.dpl;
3321 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
3322 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
3323 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003324 }
3325
Gleb Natapovceffb452010-03-18 15:20:19 +02003326 desc_limit = desc_limit_scaled(&next_tss_desc);
3327 if (!next_tss_desc.p ||
3328 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
3329 desc_limit < 0x2b)) {
Paolo Bonzini592f0852014-08-20 10:05:08 +02003330 return emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003331 }
3332
3333 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
3334 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003335 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003336 }
3337
3338 if (reason == TASK_SWITCH_IRET)
3339 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
3340
3341 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08003342 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003343 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
3344 old_tss_sel = 0xffff;
3345
3346 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003347 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003348 old_tss_base, &next_tss_desc);
3349 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003350 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003351 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02003352 if (ret != X86EMUL_CONTINUE)
3353 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003354
3355 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
3356 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
3357
3358 if (reason != TASK_SWITCH_IRET) {
3359 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003360 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003361 }
3362
Avi Kivity717746e2011-04-20 13:37:53 +03003363 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03003364 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003365
Jan Kiszkae269fb22010-04-14 15:51:09 +02003366 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003367 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
3368 ctxt->lock_prefix = 0;
3369 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003370 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02003371 }
3372
Nadav Amit3db176d2015-04-19 21:12:59 +03003373 ops->get_dr(ctxt, 7, &dr7);
3374 ops->set_dr(ctxt, 7, dr7 & ~(DR_LOCAL_ENABLE_MASK | DR_LOCAL_SLOWDOWN));
3375
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003376 return ret;
3377}
3378
3379int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003380 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003381 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003382{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003383 int rc;
3384
Avi Kivitydd856ef2012-08-27 23:46:17 +03003385 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003386 ctxt->_eip = ctxt->eip;
3387 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003388
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003389 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003390 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003391
Avi Kivitydd856ef2012-08-27 23:46:17 +03003392 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003393 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003394 writeback_registers(ctxt);
3395 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003396
Gleb Natapova0c0ab22011-03-28 16:57:49 +02003397 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003398}
3399
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03003400static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
3401 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02003402{
Nadav Amit0efb0442015-03-29 16:33:03 +03003403 int df = (ctxt->eflags & X86_EFLAGS_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02003404
Paolo Bonzini01485a22014-11-19 18:25:08 +01003405 register_address_increment(ctxt, reg, df * op->bytes);
3406 op->addr.mem.ea = register_address(ctxt, reg);
Gleb Natapova682e352010-03-18 15:20:21 +02003407}
3408
Avi Kivity7af04fc2010-08-18 14:16:35 +03003409static int em_das(struct x86_emulate_ctxt *ctxt)
3410{
Avi Kivity7af04fc2010-08-18 14:16:35 +03003411 u8 al, old_al;
3412 bool af, cf, old_cf;
3413
3414 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003415 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03003416
3417 old_al = al;
3418 old_cf = cf;
3419 cf = false;
3420 af = ctxt->eflags & X86_EFLAGS_AF;
3421 if ((al & 0x0f) > 9 || af) {
3422 al -= 6;
3423 cf = old_cf | (al >= 250);
3424 af = true;
3425 } else {
3426 af = false;
3427 }
3428 if (old_al > 0x99 || old_cf) {
3429 al -= 0x60;
3430 cf = true;
3431 }
3432
Avi Kivity9dac77f2011-06-01 15:34:25 +03003433 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03003434 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003435 ctxt->src.type = OP_IMM;
3436 ctxt->src.val = 0;
3437 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02003438 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03003439 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
3440 if (cf)
3441 ctxt->eflags |= X86_EFLAGS_CF;
3442 if (af)
3443 ctxt->eflags |= X86_EFLAGS_AF;
3444 return X86EMUL_CONTINUE;
3445}
3446
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003447static int em_aam(struct x86_emulate_ctxt *ctxt)
3448{
3449 u8 al, ah;
3450
3451 if (ctxt->src.val == 0)
3452 return emulate_de(ctxt);
3453
3454 al = ctxt->dst.val & 0xff;
3455 ah = al / ctxt->src.val;
3456 al %= ctxt->src.val;
3457
3458 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
3459
3460 /* Set PF, ZF, SF */
3461 ctxt->src.type = OP_IMM;
3462 ctxt->src.val = 0;
3463 ctxt->src.bytes = 1;
3464 fastop(ctxt, em_or);
3465
3466 return X86EMUL_CONTINUE;
3467}
3468
Gleb Natapov7f662272012-12-10 11:42:30 +02003469static int em_aad(struct x86_emulate_ctxt *ctxt)
3470{
3471 u8 al = ctxt->dst.val & 0xff;
3472 u8 ah = (ctxt->dst.val >> 8) & 0xff;
3473
3474 al = (al + (ah * ctxt->src.val)) & 0xff;
3475
3476 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
3477
Gleb Natapovf583c292013-02-13 17:50:39 +02003478 /* Set PF, ZF, SF */
3479 ctxt->src.type = OP_IMM;
3480 ctxt->src.val = 0;
3481 ctxt->src.bytes = 1;
3482 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02003483
3484 return X86EMUL_CONTINUE;
3485}
3486
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003487static int em_call(struct x86_emulate_ctxt *ctxt)
3488{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003489 int rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003490 long rel = ctxt->src.val;
3491
3492 ctxt->src.val = (unsigned long)ctxt->_eip;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003493 rc = jmp_rel(ctxt, rel);
3494 if (rc != X86EMUL_CONTINUE)
3495 return rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003496 return em_push(ctxt);
3497}
3498
Avi Kivity0ef753b2010-08-18 14:51:45 +03003499static int em_call_far(struct x86_emulate_ctxt *ctxt)
3500{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003501 u16 sel, old_cs;
3502 ulong old_eip;
3503 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03003504 struct desc_struct old_desc, new_desc;
3505 const struct x86_emulate_ops *ops = ctxt->ops;
3506 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amit82268082015-01-26 09:32:27 +02003507 enum x86emul_mode prev_mode = ctxt->mode;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003508
Avi Kivity9dac77f2011-06-01 15:34:25 +03003509 old_eip = ctxt->_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003510 ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003511
Avi Kivity9dac77f2011-06-01 15:34:25 +03003512 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003513 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
3514 X86_TRANSFER_CALL_JMP, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003515 if (rc != X86EMUL_CONTINUE)
Nadav Amit80976db2014-12-25 02:52:20 +02003516 return rc;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003517
Nadav Amitd50eaa12014-11-19 17:43:11 +02003518 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003519 if (rc != X86EMUL_CONTINUE)
3520 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003521
Avi Kivity9dac77f2011-06-01 15:34:25 +03003522 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003523 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003524 if (rc != X86EMUL_CONTINUE)
Nadav Amitd1442d82014-09-18 22:39:39 +03003525 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003526
Avi Kivity9dac77f2011-06-01 15:34:25 +03003527 ctxt->src.val = old_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003528 rc = em_push(ctxt);
3529 /* If we failed, we tainted the memory, but the very least we should
3530 restore cs */
Nadav Amit82268082015-01-26 09:32:27 +02003531 if (rc != X86EMUL_CONTINUE) {
3532 pr_warn_once("faulting far call emulation tainted memory\n");
Nadav Amitd1442d82014-09-18 22:39:39 +03003533 goto fail;
Nadav Amit82268082015-01-26 09:32:27 +02003534 }
Nadav Amitd1442d82014-09-18 22:39:39 +03003535 return rc;
3536fail:
3537 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
Nadav Amit82268082015-01-26 09:32:27 +02003538 ctxt->mode = prev_mode;
Nadav Amitd1442d82014-09-18 22:39:39 +03003539 return rc;
3540
Avi Kivity0ef753b2010-08-18 14:51:45 +03003541}
3542
Avi Kivity40ece7c2010-08-18 15:12:09 +03003543static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3544{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003545 int rc;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003546 unsigned long eip;
Avi Kivity40ece7c2010-08-18 15:12:09 +03003547
Nadav Amit234f3ce2014-09-18 22:39:38 +03003548 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
3549 if (rc != X86EMUL_CONTINUE)
3550 return rc;
3551 rc = assign_eip_near(ctxt, eip);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003552 if (rc != X86EMUL_CONTINUE)
3553 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003554 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003555 return X86EMUL_CONTINUE;
3556}
3557
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003558static int em_xchg(struct x86_emulate_ctxt *ctxt)
3559{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003560 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003561 ctxt->src.val = ctxt->dst.val;
3562 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003563
3564 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003565 ctxt->dst.val = ctxt->src.orig_val;
3566 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003567 return X86EMUL_CONTINUE;
3568}
3569
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003570static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3571{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003572 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003573 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003574}
3575
Avi Kivity61429142010-08-19 15:13:00 +03003576static int em_cwd(struct x86_emulate_ctxt *ctxt)
3577{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003578 ctxt->dst.type = OP_REG;
3579 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003580 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003581 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003582
3583 return X86EMUL_CONTINUE;
3584}
3585
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02003586static int em_rdpid(struct x86_emulate_ctxt *ctxt)
3587{
3588 u64 tsc_aux = 0;
3589
3590 if (ctxt->ops->get_msr(ctxt, MSR_TSC_AUX, &tsc_aux))
3591 return emulate_gp(ctxt, 0);
3592 ctxt->dst.val = tsc_aux;
3593 return X86EMUL_CONTINUE;
3594}
3595
Avi Kivity48bb5d32010-08-18 18:54:34 +03003596static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3597{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003598 u64 tsc = 0;
3599
Avi Kivity717746e2011-04-20 13:37:53 +03003600 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003601 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3602 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003603 return X86EMUL_CONTINUE;
3604}
3605
Avi Kivity222d21a2011-11-10 14:57:30 +02003606static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3607{
3608 u64 pmc;
3609
Avi Kivitydd856ef2012-08-27 23:46:17 +03003610 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003611 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003612 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3613 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003614 return X86EMUL_CONTINUE;
3615}
3616
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003617static int em_mov(struct x86_emulate_ctxt *ctxt)
3618{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01003619 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003620 return X86EMUL_CONTINUE;
3621}
3622
Borislav Petkov84cffe42013-10-29 12:54:56 +01003623#define FFL(x) bit(X86_FEATURE_##x)
3624
3625static int em_movbe(struct x86_emulate_ctxt *ctxt)
3626{
3627 u32 ebx, ecx, edx, eax = 1;
3628 u16 tmp;
3629
3630 /*
3631 * Check MOVBE is set in the guest-visible CPUID leaf.
3632 */
Yu Zhange911eb32017-08-24 20:27:52 +08003633 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, false);
Borislav Petkov84cffe42013-10-29 12:54:56 +01003634 if (!(ecx & FFL(MOVBE)))
3635 return emulate_ud(ctxt);
3636
3637 switch (ctxt->op_bytes) {
3638 case 2:
3639 /*
3640 * From MOVBE definition: "...When the operand size is 16 bits,
3641 * the upper word of the destination register remains unchanged
3642 * ..."
3643 *
3644 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3645 * rules so we have to do the operation almost per hand.
3646 */
3647 tmp = (u16)ctxt->src.val;
3648 ctxt->dst.val &= ~0xffffUL;
3649 ctxt->dst.val |= (unsigned long)swab16(tmp);
3650 break;
3651 case 4:
3652 ctxt->dst.val = swab32((u32)ctxt->src.val);
3653 break;
3654 case 8:
3655 ctxt->dst.val = swab64(ctxt->src.val);
3656 break;
3657 default:
Paolo Bonzini592f0852014-08-20 10:05:08 +02003658 BUG();
Borislav Petkov84cffe42013-10-29 12:54:56 +01003659 }
3660 return X86EMUL_CONTINUE;
3661}
3662
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003663static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3664{
3665 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3666 return emulate_gp(ctxt, 0);
3667
3668 /* Disable writeback. */
3669 ctxt->dst.type = OP_NONE;
3670 return X86EMUL_CONTINUE;
3671}
3672
3673static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3674{
3675 unsigned long val;
3676
3677 if (ctxt->mode == X86EMUL_MODE_PROT64)
3678 val = ctxt->src.val & ~0ULL;
3679 else
3680 val = ctxt->src.val & ~0U;
3681
3682 /* #UD condition is already handled. */
3683 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3684 return emulate_gp(ctxt, 0);
3685
3686 /* Disable writeback. */
3687 ctxt->dst.type = OP_NONE;
3688 return X86EMUL_CONTINUE;
3689}
3690
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003691static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3692{
3693 u64 msr_data;
3694
Avi Kivitydd856ef2012-08-27 23:46:17 +03003695 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3696 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3697 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003698 return emulate_gp(ctxt, 0);
3699
3700 return X86EMUL_CONTINUE;
3701}
3702
3703static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3704{
3705 u64 msr_data;
3706
Avi Kivitydd856ef2012-08-27 23:46:17 +03003707 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003708 return emulate_gp(ctxt, 0);
3709
Avi Kivitydd856ef2012-08-27 23:46:17 +03003710 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3711 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003712 return X86EMUL_CONTINUE;
3713}
3714
Paolo Bonzinidd307d02016-07-12 10:35:51 +02003715static int em_store_sreg(struct x86_emulate_ctxt *ctxt, int segment)
3716{
3717 if (segment > VCPU_SREG_GS &&
3718 (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_UMIP) &&
3719 ctxt->ops->cpl(ctxt) > 0)
3720 return emulate_gp(ctxt, 0);
3721
3722 ctxt->dst.val = get_segment_selector(ctxt, segment);
3723 if (ctxt->dst.bytes == 4 && ctxt->dst.type == OP_MEM)
3724 ctxt->dst.bytes = 2;
3725 return X86EMUL_CONTINUE;
3726}
3727
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003728static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3729{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003730 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003731 return emulate_ud(ctxt);
3732
Paolo Bonzinidd307d02016-07-12 10:35:51 +02003733 return em_store_sreg(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003734}
3735
3736static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3737{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003738 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003739
Avi Kivity9dac77f2011-06-01 15:34:25 +03003740 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003741 return emulate_ud(ctxt);
3742
Avi Kivity9dac77f2011-06-01 15:34:25 +03003743 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003744 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3745
3746 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003747 ctxt->dst.type = OP_NONE;
3748 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003749}
3750
Paolo Bonzinidd307d02016-07-12 10:35:51 +02003751static int em_sldt(struct x86_emulate_ctxt *ctxt)
3752{
3753 return em_store_sreg(ctxt, VCPU_SREG_LDTR);
3754}
3755
Avi Kivitya14e5792012-06-13 12:28:33 +03003756static int em_lldt(struct x86_emulate_ctxt *ctxt)
3757{
3758 u16 sel = ctxt->src.val;
3759
3760 /* Disable writeback. */
3761 ctxt->dst.type = OP_NONE;
3762 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3763}
3764
Paolo Bonzinidd307d02016-07-12 10:35:51 +02003765static int em_str(struct x86_emulate_ctxt *ctxt)
3766{
3767 return em_store_sreg(ctxt, VCPU_SREG_TR);
3768}
3769
Avi Kivity80890002012-06-13 16:33:29 +03003770static int em_ltr(struct x86_emulate_ctxt *ctxt)
3771{
3772 u16 sel = ctxt->src.val;
3773
3774 /* Disable writeback. */
3775 ctxt->dst.type = OP_NONE;
3776 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3777}
3778
Avi Kivity38503912011-03-31 18:48:09 +02003779static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3780{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003781 int rc;
3782 ulong linear;
3783
Avi Kivity9dac77f2011-06-01 15:34:25 +03003784 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003785 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003786 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003787 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003788 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003789 return X86EMUL_CONTINUE;
3790}
3791
Avi Kivity2d04a052011-04-20 15:32:49 +03003792static int em_clts(struct x86_emulate_ctxt *ctxt)
3793{
3794 ulong cr0;
3795
3796 cr0 = ctxt->ops->get_cr(ctxt, 0);
3797 cr0 &= ~X86_CR0_TS;
3798 ctxt->ops->set_cr(ctxt, 0, cr0);
3799 return X86EMUL_CONTINUE;
3800}
3801
Jan Kiszkab34a8052015-03-09 20:27:43 +01003802static int em_hypercall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003803{
Nadav Amit0f54a322014-08-29 11:26:55 +03003804 int rc = ctxt->ops->fix_hypercall(ctxt);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003805
Avi Kivity26d05cc2011-04-21 12:07:59 +03003806 if (rc != X86EMUL_CONTINUE)
3807 return rc;
3808
3809 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003810 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003811 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003812 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003813 return X86EMUL_CONTINUE;
3814}
3815
Avi Kivity96051572012-06-10 17:21:18 +03003816static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3817 void (*get)(struct x86_emulate_ctxt *ctxt,
3818 struct desc_ptr *ptr))
3819{
3820 struct desc_ptr desc_ptr;
3821
Paolo Bonziniae3e61e2016-07-12 10:36:41 +02003822 if ((ctxt->ops->get_cr(ctxt, 4) & X86_CR4_UMIP) &&
3823 ctxt->ops->cpl(ctxt) > 0)
3824 return emulate_gp(ctxt, 0);
3825
Avi Kivity96051572012-06-10 17:21:18 +03003826 if (ctxt->mode == X86EMUL_MODE_PROT64)
3827 ctxt->op_bytes = 8;
3828 get(ctxt, &desc_ptr);
3829 if (ctxt->op_bytes == 2) {
3830 ctxt->op_bytes = 4;
3831 desc_ptr.address &= 0x00ffffff;
3832 }
3833 /* Disable writeback. */
3834 ctxt->dst.type = OP_NONE;
Steve Rutherford129a72a2017-01-11 18:28:29 -08003835 return segmented_write_std(ctxt, ctxt->dst.addr.mem,
3836 &desc_ptr, 2 + ctxt->op_bytes);
Avi Kivity96051572012-06-10 17:21:18 +03003837}
3838
3839static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3840{
3841 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3842}
3843
3844static int em_sidt(struct x86_emulate_ctxt *ctxt)
3845{
3846 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3847}
3848
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003849static int em_lgdt_lidt(struct x86_emulate_ctxt *ctxt, bool lgdt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003850{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003851 struct desc_ptr desc_ptr;
3852 int rc;
3853
Avi Kivity510425f2012-06-07 17:04:36 +03003854 if (ctxt->mode == X86EMUL_MODE_PROT64)
3855 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003856 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003857 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003858 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003859 if (rc != X86EMUL_CONTINUE)
3860 return rc;
Nadav Amit9a9abf62014-11-02 11:54:56 +02003861 if (ctxt->mode == X86EMUL_MODE_PROT64 &&
Yu Zhangfd8cb432017-08-24 20:27:56 +08003862 emul_is_noncanonical_address(desc_ptr.address, ctxt))
Nadav Amit9a9abf62014-11-02 11:54:56 +02003863 return emulate_gp(ctxt, 0);
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003864 if (lgdt)
3865 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3866 else
3867 ctxt->ops->set_idt(ctxt, &desc_ptr);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003868 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003869 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003870 return X86EMUL_CONTINUE;
3871}
3872
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003873static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3874{
3875 return em_lgdt_lidt(ctxt, true);
3876}
3877
Avi Kivity26d05cc2011-04-21 12:07:59 +03003878static int em_lidt(struct x86_emulate_ctxt *ctxt)
3879{
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003880 return em_lgdt_lidt(ctxt, false);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003881}
3882
3883static int em_smsw(struct x86_emulate_ctxt *ctxt)
3884{
Paolo Bonziniae3e61e2016-07-12 10:36:41 +02003885 if ((ctxt->ops->get_cr(ctxt, 4) & X86_CR4_UMIP) &&
3886 ctxt->ops->cpl(ctxt) > 0)
3887 return emulate_gp(ctxt, 0);
3888
Nadav Amit32e94d02014-06-02 18:34:11 +03003889 if (ctxt->dst.type == OP_MEM)
3890 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003891 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003892 return X86EMUL_CONTINUE;
3893}
3894
3895static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3896{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003897 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003898 | (ctxt->src.val & 0x0f));
3899 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003900 return X86EMUL_CONTINUE;
3901}
3902
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003903static int em_loop(struct x86_emulate_ctxt *ctxt)
3904{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003905 int rc = X86EMUL_CONTINUE;
3906
Paolo Bonzini01485a22014-11-19 18:25:08 +01003907 register_address_increment(ctxt, VCPU_REGS_RCX, -1);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003908 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003909 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
Nadav Amit234f3ce2014-09-18 22:39:38 +03003910 rc = jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003911
Nadav Amit234f3ce2014-09-18 22:39:38 +03003912 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003913}
3914
3915static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3916{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003917 int rc = X86EMUL_CONTINUE;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003918
Nadav Amit234f3ce2014-09-18 22:39:38 +03003919 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
3920 rc = jmp_rel(ctxt, ctxt->src.val);
3921
3922 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003923}
3924
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003925static int em_in(struct x86_emulate_ctxt *ctxt)
3926{
3927 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3928 &ctxt->dst.val))
3929 return X86EMUL_IO_NEEDED;
3930
3931 return X86EMUL_CONTINUE;
3932}
3933
3934static int em_out(struct x86_emulate_ctxt *ctxt)
3935{
3936 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3937 &ctxt->src.val, 1);
3938 /* Disable writeback. */
3939 ctxt->dst.type = OP_NONE;
3940 return X86EMUL_CONTINUE;
3941}
3942
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003943static int em_cli(struct x86_emulate_ctxt *ctxt)
3944{
3945 if (emulator_bad_iopl(ctxt))
3946 return emulate_gp(ctxt, 0);
3947
3948 ctxt->eflags &= ~X86_EFLAGS_IF;
3949 return X86EMUL_CONTINUE;
3950}
3951
3952static int em_sti(struct x86_emulate_ctxt *ctxt)
3953{
3954 if (emulator_bad_iopl(ctxt))
3955 return emulate_gp(ctxt, 0);
3956
3957 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3958 ctxt->eflags |= X86_EFLAGS_IF;
3959 return X86EMUL_CONTINUE;
3960}
3961
Avi Kivity6d6eede2012-06-07 14:11:36 +03003962static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3963{
3964 u32 eax, ebx, ecx, edx;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003965 u64 msr = 0;
3966
3967 ctxt->ops->get_msr(ctxt, MSR_MISC_FEATURES_ENABLES, &msr);
3968 if (msr & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
3969 ctxt->ops->cpl(ctxt)) {
3970 return emulate_gp(ctxt, 0);
3971 }
Avi Kivity6d6eede2012-06-07 14:11:36 +03003972
Avi Kivitydd856ef2012-08-27 23:46:17 +03003973 eax = reg_read(ctxt, VCPU_REGS_RAX);
3974 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Yu Zhange911eb32017-08-24 20:27:52 +08003975 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, true);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003976 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3977 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3978 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3979 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003980 return X86EMUL_CONTINUE;
3981}
3982
Paolo Bonzini98f73632013-10-31 11:19:42 +01003983static int em_sahf(struct x86_emulate_ctxt *ctxt)
3984{
3985 u32 flags;
3986
Nadav Amit0efb0442015-03-29 16:33:03 +03003987 flags = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
3988 X86_EFLAGS_SF;
Paolo Bonzini98f73632013-10-31 11:19:42 +01003989 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3990
3991 ctxt->eflags &= ~0xffUL;
3992 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3993 return X86EMUL_CONTINUE;
3994}
3995
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003996static int em_lahf(struct x86_emulate_ctxt *ctxt)
3997{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003998 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3999 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03004000 return X86EMUL_CONTINUE;
4001}
4002
Avi Kivity92998362012-06-13 12:25:06 +03004003static int em_bswap(struct x86_emulate_ctxt *ctxt)
4004{
4005 switch (ctxt->op_bytes) {
4006#ifdef CONFIG_X86_64
4007 case 8:
4008 asm("bswap %0" : "+r"(ctxt->dst.val));
4009 break;
4010#endif
4011 default:
4012 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
4013 break;
4014 }
4015 return X86EMUL_CONTINUE;
4016}
4017
Nadav Amit13e457e2014-10-13 13:04:13 +03004018static int em_clflush(struct x86_emulate_ctxt *ctxt)
4019{
4020 /* emulating clflush regardless of cpuid */
4021 return X86EMUL_CONTINUE;
4022}
4023
Nadav Amit2276b512015-01-26 09:32:24 +02004024static int em_movsxd(struct x86_emulate_ctxt *ctxt)
4025{
4026 ctxt->dst.val = (s32) ctxt->src.val;
4027 return X86EMUL_CONTINUE;
4028}
4029
Radim Krčmář283c95d2016-11-09 19:07:06 +01004030static int check_fxsr(struct x86_emulate_ctxt *ctxt)
4031{
4032 u32 eax = 1, ebx, ecx = 0, edx;
4033
Yu Zhange911eb32017-08-24 20:27:52 +08004034 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, false);
Radim Krčmář283c95d2016-11-09 19:07:06 +01004035 if (!(edx & FFL(FXSR)))
4036 return emulate_ud(ctxt);
4037
4038 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
4039 return emulate_nm(ctxt);
4040
4041 /*
4042 * Don't emulate a case that should never be hit, instead of working
4043 * around a lack of fxsave64/fxrstor64 on old compilers.
4044 */
4045 if (ctxt->mode >= X86EMUL_MODE_PROT64)
4046 return X86EMUL_UNHANDLEABLE;
4047
4048 return X86EMUL_CONTINUE;
4049}
4050
4051/*
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004052 * Hardware doesn't save and restore XMM 0-7 without CR4.OSFXSR, but does save
4053 * and restore MXCSR.
4054 */
4055static size_t __fxstate_size(int nregs)
4056{
4057 return offsetof(struct fxregs_state, xmm_space[0]) + nregs * 16;
4058}
4059
4060static inline size_t fxstate_size(struct x86_emulate_ctxt *ctxt)
4061{
4062 bool cr4_osfxsr;
4063 if (ctxt->mode == X86EMUL_MODE_PROT64)
4064 return __fxstate_size(16);
4065
4066 cr4_osfxsr = ctxt->ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR;
4067 return __fxstate_size(cr4_osfxsr ? 8 : 0);
4068}
4069
4070/*
Radim Krčmář283c95d2016-11-09 19:07:06 +01004071 * FXSAVE and FXRSTOR have 4 different formats depending on execution mode,
4072 * 1) 16 bit mode
4073 * 2) 32 bit mode
4074 * - like (1), but FIP and FDP (foo) are only 16 bit. At least Intel CPUs
4075 * preserve whole 32 bit values, though, so (1) and (2) are the same wrt.
4076 * save and restore
4077 * 3) 64-bit mode with REX.W prefix
4078 * - like (2), but XMM 8-15 are being saved and restored
4079 * 4) 64-bit mode without REX.W prefix
4080 * - like (3), but FIP and FDP are 64 bit
4081 *
4082 * Emulation uses (3) for (1) and (2) and preserves XMM 8-15 to reach the
4083 * desired result. (4) is not emulated.
4084 *
4085 * Note: Guest and host CPUID.(EAX=07H,ECX=0H):EBX[bit 13] (deprecate FPU CS
4086 * and FPU DS) should match.
4087 */
4088static int em_fxsave(struct x86_emulate_ctxt *ctxt)
4089{
4090 struct fxregs_state fx_state;
Radim Krčmář283c95d2016-11-09 19:07:06 +01004091 int rc;
4092
4093 rc = check_fxsr(ctxt);
4094 if (rc != X86EMUL_CONTINUE)
4095 return rc;
4096
Radim Krčmář283c95d2016-11-09 19:07:06 +01004097 rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_state));
4098
Radim Krčmář283c95d2016-11-09 19:07:06 +01004099 if (rc != X86EMUL_CONTINUE)
4100 return rc;
4101
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004102 return segmented_write_std(ctxt, ctxt->memop.addr.mem, &fx_state,
4103 fxstate_size(ctxt));
Radim Krčmář283c95d2016-11-09 19:07:06 +01004104}
4105
David Hildenbrand4d772cb2017-11-07 18:04:05 +01004106/*
4107 * FXRSTOR might restore XMM registers not provided by the guest. Fill
4108 * in the host registers (via FXSAVE) instead, so they won't be modified.
4109 * (preemption has to stay disabled until FXRSTOR).
4110 *
4111 * Use noinline to keep the stack for other functions called by callers small.
4112 */
4113static noinline int fxregs_fixup(struct fxregs_state *fx_state,
4114 const size_t used_size)
4115{
4116 struct fxregs_state fx_tmp;
4117 int rc;
4118
4119 rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_tmp));
4120 memcpy((void *)fx_state + used_size, (void *)&fx_tmp + used_size,
4121 __fxstate_size(16) - used_size);
4122
4123 return rc;
4124}
4125
Radim Krčmář283c95d2016-11-09 19:07:06 +01004126static int em_fxrstor(struct x86_emulate_ctxt *ctxt)
4127{
4128 struct fxregs_state fx_state;
4129 int rc;
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004130 size_t size;
Radim Krčmář283c95d2016-11-09 19:07:06 +01004131
4132 rc = check_fxsr(ctxt);
4133 if (rc != X86EMUL_CONTINUE)
4134 return rc;
4135
David Hildenbrand4d772cb2017-11-07 18:04:05 +01004136 size = fxstate_size(ctxt);
4137 rc = segmented_read_std(ctxt, ctxt->memop.addr.mem, &fx_state, size);
4138 if (rc != X86EMUL_CONTINUE)
4139 return rc;
4140
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004141 if (size < __fxstate_size(16)) {
David Hildenbrand4d772cb2017-11-07 18:04:05 +01004142 rc = fxregs_fixup(&fx_state, size);
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004143 if (rc != X86EMUL_CONTINUE)
4144 goto out;
4145 }
4146
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004147 if (fx_state.mxcsr >> 16) {
4148 rc = emulate_gp(ctxt, 0);
4149 goto out;
4150 }
Radim Krčmář283c95d2016-11-09 19:07:06 +01004151
4152 if (rc == X86EMUL_CONTINUE)
4153 rc = asm_safe("fxrstor %[fx]", : [fx] "m"(fx_state));
4154
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004155out:
Radim Krčmář283c95d2016-11-09 19:07:06 +01004156 return rc;
4157}
4158
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004159static bool valid_cr(int nr)
4160{
4161 switch (nr) {
4162 case 0:
4163 case 2 ... 4:
4164 case 8:
4165 return true;
4166 default:
4167 return false;
4168 }
4169}
4170
4171static int check_cr_read(struct x86_emulate_ctxt *ctxt)
4172{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004173 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004174 return emulate_ud(ctxt);
4175
4176 return X86EMUL_CONTINUE;
4177}
4178
4179static int check_cr_write(struct x86_emulate_ctxt *ctxt)
4180{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004181 u64 new_val = ctxt->src.val64;
4182 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03004183 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004184
4185 static u64 cr_reserved_bits[] = {
4186 0xffffffff00000000ULL,
4187 0, 0, 0, /* CR3 checked later */
4188 CR4_RESERVED_BITS,
4189 0, 0, 0,
4190 CR8_RESERVED_BITS,
4191 };
4192
4193 if (!valid_cr(cr))
4194 return emulate_ud(ctxt);
4195
4196 if (new_val & cr_reserved_bits[cr])
4197 return emulate_gp(ctxt, 0);
4198
4199 switch (cr) {
4200 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03004201 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004202 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
4203 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
4204 return emulate_gp(ctxt, 0);
4205
Avi Kivity717746e2011-04-20 13:37:53 +03004206 cr4 = ctxt->ops->get_cr(ctxt, 4);
4207 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004208
4209 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
4210 !(cr4 & X86_CR4_PAE))
4211 return emulate_gp(ctxt, 0);
4212
4213 break;
4214 }
4215 case 3: {
4216 u64 rsvd = 0;
4217
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03004218 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08004219 if (efer & EFER_LMA) {
4220 u64 maxphyaddr;
Yu Zhangd6500142017-09-18 18:45:01 +08004221 u32 eax, ebx, ecx, edx;
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08004222
Yu Zhangd6500142017-09-18 18:45:01 +08004223 eax = 0x80000008;
4224 ecx = 0;
4225 if (ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx,
4226 &edx, false))
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08004227 maxphyaddr = eax & 0xff;
4228 else
4229 maxphyaddr = 36;
Wanpeng Lia780a3e2018-05-13 02:24:47 -07004230 rsvd = rsvd_bits(maxphyaddr, 63);
4231 if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PCIDE)
Junaid Shahid208320b2018-06-27 14:59:21 -07004232 rsvd &= ~X86_CR3_PCID_NOFLUSH;
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08004233 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004234
4235 if (new_val & rsvd)
4236 return emulate_gp(ctxt, 0);
4237
4238 break;
4239 }
4240 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03004241 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004242
4243 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
4244 return emulate_gp(ctxt, 0);
4245
4246 break;
4247 }
4248 }
4249
4250 return X86EMUL_CONTINUE;
4251}
4252
Joerg Roedel3b88e412011-04-04 12:39:29 +02004253static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
4254{
4255 unsigned long dr7;
4256
Avi Kivity717746e2011-04-20 13:37:53 +03004257 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02004258
4259 /* Check if DR7.Global_Enable is set */
4260 return dr7 & (1 << 13);
4261}
4262
4263static int check_dr_read(struct x86_emulate_ctxt *ctxt)
4264{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004265 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02004266 u64 cr4;
4267
4268 if (dr > 7)
4269 return emulate_ud(ctxt);
4270
Avi Kivity717746e2011-04-20 13:37:53 +03004271 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02004272 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
4273 return emulate_ud(ctxt);
4274
Nadav Amit6d2a0522014-11-02 11:54:43 +02004275 if (check_dr7_gd(ctxt)) {
4276 ulong dr6;
4277
4278 ctxt->ops->get_dr(ctxt, 6, &dr6);
Liran Alon1fc5d1942019-06-06 01:54:47 +03004279 dr6 &= ~DR_TRAP_BITS;
Nadav Amit6d2a0522014-11-02 11:54:43 +02004280 dr6 |= DR6_BD | DR6_RTM;
4281 ctxt->ops->set_dr(ctxt, 6, dr6);
Joerg Roedel3b88e412011-04-04 12:39:29 +02004282 return emulate_db(ctxt);
Nadav Amit6d2a0522014-11-02 11:54:43 +02004283 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02004284
4285 return X86EMUL_CONTINUE;
4286}
4287
4288static int check_dr_write(struct x86_emulate_ctxt *ctxt)
4289{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004290 u64 new_val = ctxt->src.val64;
4291 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02004292
4293 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
4294 return emulate_gp(ctxt, 0);
4295
4296 return check_dr_read(ctxt);
4297}
4298
Joerg Roedel01de8b02011-04-04 12:39:31 +02004299static int check_svme(struct x86_emulate_ctxt *ctxt)
4300{
Radim Krčmář92ceb762017-05-18 19:37:32 +02004301 u64 efer = 0;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004302
Avi Kivity717746e2011-04-20 13:37:53 +03004303 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02004304
4305 if (!(efer & EFER_SVME))
4306 return emulate_ud(ctxt);
4307
4308 return X86EMUL_CONTINUE;
4309}
4310
4311static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
4312{
Avi Kivitydd856ef2012-08-27 23:46:17 +03004313 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02004314
4315 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07004316 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02004317 return emulate_gp(ctxt, 0);
4318
4319 return check_svme(ctxt);
4320}
4321
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004322static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
4323{
Avi Kivity717746e2011-04-20 13:37:53 +03004324 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004325
Avi Kivity717746e2011-04-20 13:37:53 +03004326 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004327 return emulate_ud(ctxt);
4328
4329 return X86EMUL_CONTINUE;
4330}
4331
Joerg Roedel80612522011-04-04 12:39:33 +02004332static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
4333{
Avi Kivity717746e2011-04-20 13:37:53 +03004334 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03004335 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02004336
Arbel Moshe2d7921c2018-03-12 13:12:53 +02004337 /*
4338 * VMware allows access to these Pseduo-PMCs even when read via RDPMC
4339 * in Ring3 when CR4.PCE=0.
4340 */
4341 if (enable_vmware_backdoor && is_vmware_backdoor_pmc(rcx))
4342 return X86EMUL_CONTINUE;
4343
Avi Kivity717746e2011-04-20 13:37:53 +03004344 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03004345 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02004346 return emulate_gp(ctxt, 0);
4347
4348 return X86EMUL_CONTINUE;
4349}
4350
Joerg Roedelf6511932011-04-04 12:39:35 +02004351static int check_perm_in(struct x86_emulate_ctxt *ctxt)
4352{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004353 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
4354 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02004355 return emulate_gp(ctxt, 0);
4356
4357 return X86EMUL_CONTINUE;
4358}
4359
4360static int check_perm_out(struct x86_emulate_ctxt *ctxt)
4361{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004362 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
4363 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02004364 return emulate_gp(ctxt, 0);
4365
4366 return X86EMUL_CONTINUE;
4367}
4368
Avi Kivity73fba5f2010-07-29 15:11:53 +03004369#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004370#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
4371#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
4372 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03004373#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02004374#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004375#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
4376#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Nadav Amit39f062f2014-11-26 15:47:18 +02004377#define ID(_f, _i) { .flags = ((_f) | InstrDual | ModRM), .u.idual = (_i) }
Nadav Amit2276b512015-01-26 09:32:24 +02004378#define MD(_f, _m) { .flags = ((_f) | ModeDual), .u.mdual = (_m) }
Gleb Natapov045a2822012-12-20 16:57:43 +02004379#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03004380#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02004381#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004382#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004383 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004384#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004385 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
4386 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02004387#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03004388
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004389#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02004390#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004391#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02004392#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004393#define I2bvIP(_f, _e, _i, _p) \
4394 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004395
Avi Kivityfb864fb2013-01-04 16:18:54 +02004396#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
4397 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
4398 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03004399
Nadav Amit0f54a322014-08-29 11:26:55 +03004400static const struct opcode group7_rm0[] = {
4401 N,
Jan Kiszkab34a8052015-03-09 20:27:43 +01004402 I(SrcNone | Priv | EmulateOnUD, em_hypercall),
Nadav Amit0f54a322014-08-29 11:26:55 +03004403 N, N, N, N, N, N,
4404};
4405
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004406static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004407 DI(SrcNone | Priv, monitor),
4408 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004409 N, N, N, N, N, N,
4410};
4411
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004412static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004413 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Jan Kiszkab34a8052015-03-09 20:27:43 +01004414 II(SrcNone | Prot | EmulateOnUD, em_hypercall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004415 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
4416 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
4417 DIP(SrcNone | Prot | Priv, stgi, check_svme),
4418 DIP(SrcNone | Prot | Priv, clgi, check_svme),
4419 DIP(SrcNone | Prot | Priv, skinit, check_svme),
4420 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004421};
Avi Kivity6230f7f2010-08-26 18:34:55 +03004422
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004423static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004424 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004425 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004426 N, N, N, N, N, N,
4427};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004428
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004429static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02004430 F(Lock, em_add),
4431 F(Lock | PageTable, em_or),
4432 F(Lock, em_adc),
4433 F(Lock, em_sbb),
4434 F(Lock | PageTable, em_and),
4435 F(Lock, em_sub),
4436 F(Lock, em_xor),
4437 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004438};
4439
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004440static const struct opcode group1A[] = {
Tom Lendacky0f89b202016-12-14 14:59:23 -05004441 I(DstMem | SrcNone | Mov | Stack | IncSP | TwoMemOp, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004442};
4443
Avi Kivity007a3b52013-01-19 19:51:51 +02004444static const struct opcode group2[] = {
4445 F(DstMem | ModRM, em_rol),
4446 F(DstMem | ModRM, em_ror),
4447 F(DstMem | ModRM, em_rcl),
4448 F(DstMem | ModRM, em_rcr),
4449 F(DstMem | ModRM, em_shl),
4450 F(DstMem | ModRM, em_shr),
4451 F(DstMem | ModRM, em_shl),
4452 F(DstMem | ModRM, em_sar),
4453};
4454
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004455static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02004456 F(DstMem | SrcImm | NoWrite, em_test),
4457 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02004458 F(DstMem | SrcNone | Lock, em_not),
4459 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02004460 F(DstXacc | Src2Mem, em_mul_ex),
4461 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004462 F(DstXacc | Src2Mem, em_div_ex),
4463 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004464};
4465
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004466static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02004467 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
4468 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004469 N, N, N, N, N, N,
4470};
4471
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004472static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02004473 F(DstMem | SrcNone | Lock, em_inc),
4474 F(DstMem | SrcNone | Lock, em_dec),
Nadav Amit58b70752014-10-24 11:35:09 +03004475 I(SrcMem | NearBranch, em_call_near_abs),
Nadav Amitacac6f892015-05-03 20:22:57 +03004476 I(SrcMemFAddr | ImplicitOps, em_call_far),
Nadav Amit58b70752014-10-24 11:35:09 +03004477 I(SrcMem | NearBranch, em_jmp_abs),
Nadav Amitf7784042014-09-18 22:39:41 +03004478 I(SrcMemFAddr | ImplicitOps, em_jmp_far),
Tom Lendacky0f89b202016-12-14 14:59:23 -05004479 I(SrcMem | Stack | TwoMemOp, em_push), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004480};
4481
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004482static const struct opcode group6[] = {
Paolo Bonzinidd307d02016-07-12 10:35:51 +02004483 II(Prot | DstMem, em_sldt, sldt),
4484 II(Prot | DstMem, em_str, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03004485 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03004486 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004487 N, N, N, N,
4488};
4489
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004490static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03004491 II(Mov | DstMem, em_sgdt, sgdt),
4492 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004493 II(SrcMem | Priv, em_lgdt, lgdt),
4494 II(SrcMem | Priv, em_lidt, lidt),
4495 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
4496 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
4497 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004498}, {
Nadav Amit0f54a322014-08-29 11:26:55 +03004499 EXT(0, group7_rm0),
Avi Kivity5ef39c72011-04-21 12:21:50 +03004500 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004501 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004502 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
4503 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
4504 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004505} };
4506
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004507static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004508 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02004509 F(DstMem | SrcImmByte | NoWrite, em_bt),
4510 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
4511 F(DstMem | SrcImmByte | Lock, em_btr),
4512 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004513};
4514
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02004515/*
4516 * The "memory" destination is actually always a register, since we come
4517 * from the register case of group9.
4518 */
4519static const struct gprefix pfx_0f_c7_7 = {
4520 N, N, N, II(DstMem | ModRM | Op3264 | EmulateOnUD, em_rdpid, rdtscp),
4521};
4522
4523
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004524static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004525 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004526}, {
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02004527 N, N, N, N, N, N, N,
4528 GP(0, &pfx_0f_c7_7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004529} };
4530
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004531static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004532 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004533 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004534};
4535
Nadav Amit13e457e2014-10-13 13:04:13 +03004536static const struct gprefix pfx_0f_ae_7 = {
Nadav Amit3f6f1482014-10-13 13:04:14 +03004537 I(SrcMem | ByteOp, em_clflush), N, N, N,
Nadav Amit13e457e2014-10-13 13:04:13 +03004538};
4539
4540static const struct group_dual group15 = { {
Radim Krčmář283c95d2016-11-09 19:07:06 +01004541 I(ModRM | Aligned16, em_fxsave),
4542 I(ModRM | Aligned16, em_fxrstor),
4543 N, N, N, N, N, GP(0, &pfx_0f_ae_7),
Nadav Amit13e457e2014-10-13 13:04:13 +03004544}, {
4545 N, N, N, N, N, N, N, N,
4546} };
4547
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004548static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03004549 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02004550};
4551
Nadav Amit39f062f2014-11-26 15:47:18 +02004552static const struct instr_dual instr_dual_0f_2b = {
4553 I(0, em_mov), N
4554};
4555
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004556static const struct gprefix pfx_0f_2b = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004557 ID(0, &instr_dual_0f_2b), ID(0, &instr_dual_0f_2b), N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03004558};
4559
Stefan Fritsch29916962018-04-01 17:54:44 +02004560static const struct gprefix pfx_0f_10_0f_11 = {
4561 I(Unaligned, em_mov), I(Unaligned, em_mov), N, N,
4562};
4563
Igor Mammedov27ce8252014-03-15 21:01:59 +01004564static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01004565 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004566};
4567
Alex Williamson0a370272014-07-11 11:56:31 -06004568static const struct gprefix pfx_0f_e7 = {
4569 N, I(Sse, em_mov), N, N,
4570};
4571
Gleb Natapov045a2822012-12-20 16:57:43 +02004572static const struct escape escape_d9 = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02004573 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstcw),
Gleb Natapov045a2822012-12-20 16:57:43 +02004574}, {
4575 /* 0xC0 - 0xC7 */
4576 N, N, N, N, N, N, N, N,
4577 /* 0xC8 - 0xCF */
4578 N, N, N, N, N, N, N, N,
4579 /* 0xD0 - 0xC7 */
4580 N, N, N, N, N, N, N, N,
4581 /* 0xD8 - 0xDF */
4582 N, N, N, N, N, N, N, N,
4583 /* 0xE0 - 0xE7 */
4584 N, N, N, N, N, N, N, N,
4585 /* 0xE8 - 0xEF */
4586 N, N, N, N, N, N, N, N,
4587 /* 0xF0 - 0xF7 */
4588 N, N, N, N, N, N, N, N,
4589 /* 0xF8 - 0xFF */
4590 N, N, N, N, N, N, N, N,
4591} };
4592
4593static const struct escape escape_db = { {
4594 N, N, N, N, N, N, N, N,
4595}, {
4596 /* 0xC0 - 0xC7 */
4597 N, N, N, N, N, N, N, N,
4598 /* 0xC8 - 0xCF */
4599 N, N, N, N, N, N, N, N,
4600 /* 0xD0 - 0xC7 */
4601 N, N, N, N, N, N, N, N,
4602 /* 0xD8 - 0xDF */
4603 N, N, N, N, N, N, N, N,
4604 /* 0xE0 - 0xE7 */
4605 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
4606 /* 0xE8 - 0xEF */
4607 N, N, N, N, N, N, N, N,
4608 /* 0xF0 - 0xF7 */
4609 N, N, N, N, N, N, N, N,
4610 /* 0xF8 - 0xFF */
4611 N, N, N, N, N, N, N, N,
4612} };
4613
4614static const struct escape escape_dd = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02004615 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstsw),
Gleb Natapov045a2822012-12-20 16:57:43 +02004616}, {
4617 /* 0xC0 - 0xC7 */
4618 N, N, N, N, N, N, N, N,
4619 /* 0xC8 - 0xCF */
4620 N, N, N, N, N, N, N, N,
4621 /* 0xD0 - 0xC7 */
4622 N, N, N, N, N, N, N, N,
4623 /* 0xD8 - 0xDF */
4624 N, N, N, N, N, N, N, N,
4625 /* 0xE0 - 0xE7 */
4626 N, N, N, N, N, N, N, N,
4627 /* 0xE8 - 0xEF */
4628 N, N, N, N, N, N, N, N,
4629 /* 0xF0 - 0xF7 */
4630 N, N, N, N, N, N, N, N,
4631 /* 0xF8 - 0xFF */
4632 N, N, N, N, N, N, N, N,
4633} };
4634
Nadav Amit39f062f2014-11-26 15:47:18 +02004635static const struct instr_dual instr_dual_0f_c3 = {
4636 I(DstMem | SrcReg | ModRM | No16 | Mov, em_mov), N
4637};
4638
Nadav Amit2276b512015-01-26 09:32:24 +02004639static const struct mode_dual mode_dual_63 = {
4640 N, I(DstReg | SrcMem32 | ModRM | Mov, em_movsxd)
4641};
4642
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004643static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004644 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004645 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004646 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
4647 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004648 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004649 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004650 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
4651 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004652 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004653 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004654 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
4655 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004656 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004657 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004658 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
4659 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004660 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004661 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004662 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004663 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004664 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004665 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004666 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004667 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004668 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02004669 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004670 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03004671 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004672 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09004673 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004674 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09004675 I(ImplicitOps | Stack | No64, em_pusha),
4676 I(ImplicitOps | Stack | No64, em_popa),
Nadav Amit2276b512015-01-26 09:32:24 +02004677 N, MD(ModRM, &mode_dual_63),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004678 N, N, N, N,
4679 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03004680 I(SrcImm | Mov | Stack, em_push),
4681 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03004682 I(SrcImmByte | Mov | Stack, em_push),
4683 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004684 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09004685 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03004686 /* 0x70 - 0x7F */
Nadav Amit58b70752014-10-24 11:35:09 +03004687 X16(D(SrcImmByte | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004688 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004689 G(ByteOp | DstMem | SrcImm, group1),
4690 G(DstMem | SrcImm, group1),
4691 G(ByteOp | DstMem | SrcImm | No64, group1),
4692 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02004693 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004694 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004695 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004696 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004697 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004698 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09004699 D(ModRM | SrcMem | NoAccess | DstReg),
4700 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
4701 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004702 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004703 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004704 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03004705 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08004706 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09004707 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01004708 II(ImplicitOps | Stack, em_popf, popf),
4709 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004710 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004711 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004712 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Tom Lendacky0f89b202016-12-14 14:59:23 -05004713 I2bv(SrcSI | DstDI | Mov | String | TwoMemOp, em_mov),
4714 F2bv(SrcSI | DstDI | String | NoWrite | TwoMemOp, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004715 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004716 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004717 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
4718 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004719 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004720 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004721 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004722 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02004723 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004724 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004725 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Nadav Amit58b70752014-10-24 11:35:09 +03004726 I(ImplicitOps | NearBranch | SrcImmU16, em_ret_near_imm),
4727 I(ImplicitOps | NearBranch, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03004728 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
4729 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004730 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004731 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03004732 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Nadav Amit16794aa2015-01-26 09:32:22 +02004733 I(ImplicitOps | SrcImmU16, em_ret_far_imm),
4734 I(ImplicitOps, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02004735 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004736 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004737 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004738 G(Src2One | ByteOp, group2), G(Src2One, group2),
4739 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02004740 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02004741 I(DstAcc | SrcImmUByte | No64, em_aad),
4742 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004743 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004744 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02004745 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004746 /* 0xE0 - 0xE7 */
Nadav Amit58b70752014-10-24 11:35:09 +03004747 X3(I(SrcImmByte | NearBranch, em_loop)),
4748 I(SrcImmByte | NearBranch, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004749 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
4750 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004751 /* 0xE8 - 0xEF */
Nadav Amit58b70752014-10-24 11:35:09 +03004752 I(SrcImm | NearBranch, em_call), D(SrcImm | ImplicitOps | NearBranch),
4753 I(SrcImmFAddr | No64, em_jmp_far),
4754 D(SrcImmByte | ImplicitOps | NearBranch),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004755 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
4756 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004757 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004758 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004759 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4760 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004761 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09004762 D(ImplicitOps), D(ImplicitOps),
4763 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004764 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4765};
4766
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004767static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004768 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004769 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02004770 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004771 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004772 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004773 N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004774 /* 0x10 - 0x1F */
Stefan Fritsch29916962018-04-01 17:54:44 +02004775 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_10_0f_11),
4776 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_10_0f_11),
4777 N, N, N, N, N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004778 D(ImplicitOps | ModRM | SrcMem | NoAccess),
4779 N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004780 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03004781 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
4782 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
4783 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
4784 check_cr_write),
4785 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
4786 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004787 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004788 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
4789 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004790 N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b),
Avi Kivity3e114eb2012-04-09 18:40:01 +03004791 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004792 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004793 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004794 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004795 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004796 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02004797 I(ImplicitOps | EmulateOnUD, em_sysenter),
4798 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004799 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004800 N, N, N, N, N, N, N, N,
4801 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03004802 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004803 /* 0x50 - 0x5F */
4804 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4805 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004806 N, N, N, N,
4807 N, N, N, N,
4808 N, N, N, N,
4809 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004810 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004811 N, N, N, N,
4812 N, N, N, N,
4813 N, N, N, N,
4814 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004815 /* 0x80 - 0x8F */
Nadav Amit58b70752014-10-24 11:35:09 +03004816 X16(D(SrcImm | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004817 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004818 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004819 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004820 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004821 II(ImplicitOps, em_cpuid, cpuid),
4822 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004823 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4824 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004825 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004826 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Paolo Bonzini89651a32015-11-03 13:43:05 +01004827 II(EmulateOnUD | ImplicitOps, em_rsm, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004828 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004829 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4830 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Nadav Amit13e457e2014-10-13 13:04:13 +03004831 GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004832 /* 0xB0 - 0xB7 */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02004833 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable | SrcWrite, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004834 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004835 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004836 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4837 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004838 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004839 /* 0xB8 - 0xBF */
4840 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004841 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004842 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Nadav Amit900efe22015-03-30 15:39:21 +03004843 I(DstReg | SrcMem | ModRM, em_bsf_c),
4844 I(DstReg | SrcMem | ModRM, em_bsr_c),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004845 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004846 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02004847 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Nadav Amit39f062f2014-11-26 15:47:18 +02004848 N, ID(0, &instr_dual_0f_c3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004849 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004850 /* 0xC8 - 0xCF */
4851 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004852 /* 0xD0 - 0xDF */
4853 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4854 /* 0xE0 - 0xEF */
Alex Williamson0a370272014-07-11 11:56:31 -06004855 N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
4856 N, N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004857 /* 0xF0 - 0xFF */
4858 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4859};
4860
Nadav Amit39f062f2014-11-26 15:47:18 +02004861static const struct instr_dual instr_dual_0f_38_f0 = {
4862 I(DstReg | SrcMem | Mov, em_movbe), N
4863};
4864
4865static const struct instr_dual instr_dual_0f_38_f1 = {
4866 I(DstMem | SrcReg | Mov, em_movbe), N
4867};
4868
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004869static const struct gprefix three_byte_0f_38_f0 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004870 ID(0, &instr_dual_0f_38_f0), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004871};
4872
4873static const struct gprefix three_byte_0f_38_f1 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004874 ID(0, &instr_dual_0f_38_f1), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004875};
4876
4877/*
4878 * Insns below are selected by the prefix which indexed by the third opcode
4879 * byte.
4880 */
4881static const struct opcode opcode_map_0f_38[256] = {
4882 /* 0x00 - 0x7f */
4883 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004884 /* 0x80 - 0xef */
4885 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4886 /* 0xf0 - 0xf1 */
Nadav Amit53bb4f72014-12-07 11:49:42 +02004887 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0),
4888 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004889 /* 0xf2 - 0xff */
4890 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004891};
4892
Avi Kivity73fba5f2010-07-29 15:11:53 +03004893#undef D
4894#undef N
4895#undef G
4896#undef GD
4897#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004898#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004899#undef EXT
Nadav Amit2276b512015-01-26 09:32:24 +02004900#undef MD
Nadav Amit2b42fce2015-01-26 09:32:25 +02004901#undef ID
Avi Kivity73fba5f2010-07-29 15:11:53 +03004902
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004903#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004904#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004905#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004906#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004907#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004908
Avi Kivity9dac77f2011-06-01 15:34:25 +03004909static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004910{
4911 unsigned size;
4912
Avi Kivity9dac77f2011-06-01 15:34:25 +03004913 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004914 if (size == 8)
4915 size = 4;
4916 return size;
4917}
4918
4919static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4920 unsigned size, bool sign_extension)
4921{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004922 int rc = X86EMUL_CONTINUE;
4923
4924 op->type = OP_IMM;
4925 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004926 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004927 /* NB. Immediates are sign-extended as necessary. */
4928 switch (op->bytes) {
4929 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004930 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004931 break;
4932 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004933 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004934 break;
4935 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004936 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004937 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004938 case 8:
4939 op->val = insn_fetch(s64, ctxt);
4940 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004941 }
4942 if (!sign_extension) {
4943 switch (op->bytes) {
4944 case 1:
4945 op->val &= 0xff;
4946 break;
4947 case 2:
4948 op->val &= 0xffff;
4949 break;
4950 case 4:
4951 op->val &= 0xffffffff;
4952 break;
4953 }
4954 }
4955done:
4956 return rc;
4957}
4958
Avi Kivitya99455492011-09-13 10:45:41 +03004959static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4960 unsigned d)
4961{
4962 int rc = X86EMUL_CONTINUE;
4963
4964 switch (d) {
4965 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004966 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03004967 break;
4968 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004969 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03004970 break;
4971 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004972 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004973 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03004974 *op = ctxt->memop;
4975 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004976 if (ctxt->d & BitOp)
Avi Kivitya99455492011-09-13 10:45:41 +03004977 fetch_bit_operand(ctxt);
4978 op->orig_val = op->val;
4979 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004980 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004981 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004982 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03004983 case OpAcc:
4984 op->type = OP_REG;
4985 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004986 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03004987 fetch_register_operand(op);
4988 op->orig_val = op->val;
4989 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004990 case OpAccLo:
4991 op->type = OP_REG;
4992 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4993 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4994 fetch_register_operand(op);
4995 op->orig_val = op->val;
4996 break;
4997 case OpAccHi:
4998 if (ctxt->d & ByteOp) {
4999 op->type = OP_NONE;
5000 break;
5001 }
5002 op->type = OP_REG;
5003 op->bytes = ctxt->op_bytes;
5004 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
5005 fetch_register_operand(op);
5006 op->orig_val = op->val;
5007 break;
Avi Kivitya99455492011-09-13 10:45:41 +03005008 case OpDI:
5009 op->type = OP_MEM;
5010 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
5011 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01005012 register_address(ctxt, VCPU_REGS_RDI);
Avi Kivitya99455492011-09-13 10:45:41 +03005013 op->addr.mem.seg = VCPU_SREG_ES;
5014 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005015 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03005016 break;
5017 case OpDX:
5018 op->type = OP_REG;
5019 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005020 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03005021 fetch_register_operand(op);
5022 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03005023 case OpCL:
Nadav Amitd29b9d72014-11-02 11:54:47 +02005024 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03005025 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005026 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03005027 break;
5028 case OpImmByte:
5029 rc = decode_imm(ctxt, op, 1, true);
5030 break;
5031 case OpOne:
Nadav Amitd29b9d72014-11-02 11:54:47 +02005032 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03005033 op->bytes = 1;
5034 op->val = 1;
5035 break;
5036 case OpImm:
5037 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
5038 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02005039 case OpImm64:
5040 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
5041 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02005042 case OpMem8:
5043 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03005044 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02005045 ctxt->memop.addr.reg = decode_register(ctxt,
5046 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03005047 fetch_register_operand(&ctxt->memop);
5048 }
Avi Kivity28867ce2012-01-16 15:08:44 +02005049 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03005050 case OpMem16:
5051 ctxt->memop.bytes = 2;
5052 goto mem_common;
5053 case OpMem32:
5054 ctxt->memop.bytes = 4;
5055 goto mem_common;
5056 case OpImmU16:
5057 rc = decode_imm(ctxt, op, 2, false);
5058 break;
5059 case OpImmU:
5060 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
5061 break;
5062 case OpSI:
5063 op->type = OP_MEM;
5064 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
5065 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01005066 register_address(ctxt, VCPU_REGS_RSI);
Bandan Das573e80f2014-04-16 12:46:13 -04005067 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03005068 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005069 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03005070 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02005071 case OpXLat:
5072 op->type = OP_MEM;
5073 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
5074 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01005075 address_mask(ctxt,
Paolo Bonzini7fa57952013-05-09 11:32:50 +02005076 reg_read(ctxt, VCPU_REGS_RBX) +
5077 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04005078 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02005079 op->val = 0;
5080 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03005081 case OpImmFAddr:
5082 op->type = OP_IMM;
5083 op->addr.mem.ea = ctxt->_eip;
5084 op->bytes = ctxt->op_bytes + 2;
5085 insn_fetch_arr(op->valptr, op->bytes, ctxt);
5086 break;
5087 case OpMemFAddr:
5088 ctxt->memop.bytes = ctxt->op_bytes + 2;
5089 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03005090 case OpES:
Nadav Amitd29b9d72014-11-02 11:54:47 +02005091 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03005092 op->val = VCPU_SREG_ES;
5093 break;
5094 case OpCS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02005095 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03005096 op->val = VCPU_SREG_CS;
5097 break;
5098 case OpSS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02005099 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03005100 op->val = VCPU_SREG_SS;
5101 break;
5102 case OpDS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02005103 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03005104 op->val = VCPU_SREG_DS;
5105 break;
5106 case OpFS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02005107 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03005108 op->val = VCPU_SREG_FS;
5109 break;
5110 case OpGS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02005111 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03005112 op->val = VCPU_SREG_GS;
5113 break;
Avi Kivitya99455492011-09-13 10:45:41 +03005114 case OpImplicit:
5115 /* Special instructions do their own operand decoding. */
5116 default:
5117 op->type = OP_NONE; /* Disable writeback. */
5118 break;
5119 }
5120
5121done:
5122 return rc;
5123}
5124
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09005125int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005126{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005127 int rc = X86EMUL_CONTINUE;
5128 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03005129 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005130 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04005131 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03005132 struct opcode opcode;
Wanpeng Li3853be22017-11-05 16:54:47 -08005133 u16 dummy;
5134 struct desc_struct desc;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005135
Avi Kivityf09ed832011-09-13 10:45:40 +03005136 ctxt->memop.type = OP_NONE;
5137 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005138 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02005139 ctxt->fetch.ptr = ctxt->fetch.data;
5140 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02005141 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01005142 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005143 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02005144 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02005145 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02005146 if (rc != X86EMUL_CONTINUE)
5147 return rc;
5148 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005149
5150 switch (mode) {
5151 case X86EMUL_MODE_REAL:
5152 case X86EMUL_MODE_VM86:
Wanpeng Li3853be22017-11-05 16:54:47 -08005153 def_op_bytes = def_ad_bytes = 2;
5154 ctxt->ops->get_segment(ctxt, &dummy, &desc, NULL, VCPU_SREG_CS);
5155 if (desc.d)
5156 def_op_bytes = def_ad_bytes = 4;
5157 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005158 case X86EMUL_MODE_PROT16:
5159 def_op_bytes = def_ad_bytes = 2;
5160 break;
5161 case X86EMUL_MODE_PROT32:
5162 def_op_bytes = def_ad_bytes = 4;
5163 break;
5164#ifdef CONFIG_X86_64
5165 case X86EMUL_MODE_PROT64:
5166 def_op_bytes = 4;
5167 def_ad_bytes = 8;
5168 break;
5169#endif
5170 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005171 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005172 }
5173
Avi Kivity9dac77f2011-06-01 15:34:25 +03005174 ctxt->op_bytes = def_op_bytes;
5175 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005176
5177 /* Legacy prefixes. */
5178 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09005179 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005180 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005181 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005182 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005183 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005184 break;
5185 case 0x67: /* address-size override */
5186 if (mode == X86EMUL_MODE_PROT64)
5187 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005188 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005189 else
5190 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005191 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005192 break;
5193 case 0x26: /* ES override */
5194 case 0x2e: /* CS override */
5195 case 0x36: /* SS override */
5196 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04005197 has_seg_override = true;
5198 ctxt->seg_override = (ctxt->b >> 3) & 3;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005199 break;
5200 case 0x64: /* FS override */
5201 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04005202 has_seg_override = true;
5203 ctxt->seg_override = ctxt->b & 7;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005204 break;
5205 case 0x40 ... 0x4f: /* REX */
5206 if (mode != X86EMUL_MODE_PROT64)
5207 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005208 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005209 continue;
5210 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005211 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005212 break;
5213 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005214 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005215 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005216 break;
5217 default:
5218 goto done_prefixes;
5219 }
5220
5221 /* Any legacy prefix after a REX prefix nullifies its effect. */
5222
Avi Kivity9dac77f2011-06-01 15:34:25 +03005223 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005224 }
5225
5226done_prefixes:
5227
5228 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005229 if (ctxt->rex_prefix & 8)
5230 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005231
5232 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005233 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08005234 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005235 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02005236 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09005237 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03005238 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005239
5240 /* 0F_38 opcode map */
5241 if (ctxt->b == 0x38) {
5242 ctxt->opcode_len = 3;
5243 ctxt->b = insn_fetch(u8, ctxt);
5244 opcode = opcode_map_0f_38[ctxt->b];
5245 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005246 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03005247 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005248
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09005249 if (ctxt->d & ModRM)
5250 ctxt->modrm = insn_fetch(u8, ctxt);
5251
Nadav Amit7fe864d2014-06-02 18:34:03 +03005252 /* vex-prefix instructions are not implemented */
5253 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
Nadav Amitd14cb5d2014-11-02 11:54:58 +02005254 (mode == X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) == 0xc0)) {
Nadav Amit7fe864d2014-06-02 18:34:03 +03005255 ctxt->d = NotImpl;
5256 }
5257
Avi Kivity9dac77f2011-06-01 15:34:25 +03005258 while (ctxt->d & GroupMask) {
5259 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03005260 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005261 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03005262 opcode = opcode.u.group[goffset];
5263 break;
5264 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005265 goffset = (ctxt->modrm >> 3) & 7;
5266 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03005267 opcode = opcode.u.gdual->mod3[goffset];
5268 else
5269 opcode = opcode.u.gdual->mod012[goffset];
5270 break;
5271 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005272 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02005273 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03005274 break;
5275 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005276 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005277 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005278 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03005279 switch (simd_prefix) {
5280 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
5281 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
5282 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
5283 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
5284 }
5285 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02005286 case Escape:
5287 if (ctxt->modrm > 0xbf)
5288 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
5289 else
5290 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
5291 break;
Nadav Amit39f062f2014-11-26 15:47:18 +02005292 case InstrDual:
5293 if ((ctxt->modrm >> 6) == 3)
5294 opcode = opcode.u.idual->mod3;
5295 else
5296 opcode = opcode.u.idual->mod012;
5297 break;
Nadav Amit2276b512015-01-26 09:32:24 +02005298 case ModeDual:
5299 if (ctxt->mode == X86EMUL_MODE_PROT64)
5300 opcode = opcode.u.mdual->mode64;
5301 else
5302 opcode = opcode.u.mdual->mode32;
5303 break;
Avi Kivity46561642011-04-24 14:09:59 +03005304 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005305 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005306 }
Avi Kivity46561642011-04-24 14:09:59 +03005307
Avi Kivityb1ea50b2011-09-13 10:45:42 +03005308 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005309 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005310 }
5311
Paolo Bonzinie24186e2014-03-27 12:00:57 +01005312 /* Unrecognised? */
5313 if (ctxt->d == 0)
5314 return EMULATION_FAILED;
5315
Avi Kivity9dac77f2011-06-01 15:34:25 +03005316 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005317
Nadav Amit3a6095a2014-08-13 16:50:13 +03005318 if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD)))
5319 return EMULATION_FAILED;
5320
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005321 if (unlikely(ctxt->d &
Nadav Amited9aad22014-11-02 11:55:00 +02005322 (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch|
5323 No16))) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005324 /*
5325 * These are copied unconditionally here, and checked unconditionally
5326 * in x86_emulate_insn.
5327 */
5328 ctxt->check_perm = opcode.check_perm;
5329 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005330
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005331 if (ctxt->d & NotImpl)
5332 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02005333
Nadav Amit58b70752014-10-24 11:35:09 +03005334 if (mode == X86EMUL_MODE_PROT64) {
5335 if (ctxt->op_bytes == 4 && (ctxt->d & Stack))
5336 ctxt->op_bytes = 8;
5337 else if (ctxt->d & NearBranch)
5338 ctxt->op_bytes = 8;
5339 }
Avi Kivity7f9b4b72010-08-01 14:46:54 +03005340
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005341 if (ctxt->d & Op3264) {
5342 if (mode == X86EMUL_MODE_PROT64)
5343 ctxt->op_bytes = 8;
5344 else
5345 ctxt->op_bytes = 4;
5346 }
5347
Nadav Amited9aad22014-11-02 11:55:00 +02005348 if ((ctxt->d & No16) && ctxt->op_bytes == 2)
5349 ctxt->op_bytes = 4;
5350
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005351 if (ctxt->d & Sse)
5352 ctxt->op_bytes = 16;
5353 else if (ctxt->d & Mmx)
5354 ctxt->op_bytes = 8;
5355 }
Avi Kivity1253791d2011-03-29 11:41:27 +02005356
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005357 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005358 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03005359 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04005360 if (!has_seg_override) {
5361 has_seg_override = true;
5362 ctxt->seg_override = ctxt->modrm_seg;
5363 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03005364 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03005365 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005366 if (rc != X86EMUL_CONTINUE)
5367 goto done;
5368
Bandan Das573e80f2014-04-16 12:46:13 -04005369 if (!has_seg_override)
5370 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005371
Bandan Das573e80f2014-04-16 12:46:13 -04005372 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005373
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005374 /*
5375 * Decode and fetch the source operand: register, memory
5376 * or immediate.
5377 */
Avi Kivity0fe59122011-09-13 10:45:47 +03005378 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03005379 if (rc != X86EMUL_CONTINUE)
5380 goto done;
5381
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005382 /*
5383 * Decode and fetch the second source operand: register, memory
5384 * or immediate.
5385 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03005386 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03005387 if (rc != X86EMUL_CONTINUE)
5388 goto done;
5389
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005390 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03005391 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005392
Owen Hofmannd9092f52016-10-27 11:25:52 -07005393 if (ctxt->rip_relative && likely(ctxt->memopp))
Nadav Amit1c1c35a2014-11-19 17:43:09 +02005394 ctxt->memopp->addr.mem.ea = address_mask(ctxt,
5395 ctxt->memopp->addr.mem.ea + ctxt->_eip);
Avi Kivitycb16c342011-06-19 19:21:11 +03005396
Paolo Bonzinia430c912014-10-23 14:54:14 +02005397done:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005398 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005399}
5400
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005401bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
5402{
5403 return ctxt->d & PageTable;
5404}
5405
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005406static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
5407{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005408 /* The second termination condition only applies for REPE
5409 * and REPNE. Test if the repeat string operation prefix is
5410 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
5411 * corresponding termination condition according to:
5412 * - if REPE/REPZ and ZF = 0 then done
5413 * - if REPNE/REPNZ and ZF = 1 then done
5414 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005415 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
5416 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
5417 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03005418 ((ctxt->eflags & X86_EFLAGS_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03005419 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03005420 ((ctxt->eflags & X86_EFLAGS_ZF) == X86_EFLAGS_ZF))))
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005421 return true;
5422
5423 return false;
5424}
5425
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005426static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
5427{
Radim Krčmářaabba3c2016-11-08 20:54:18 +01005428 int rc;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005429
Radim Krčmářaabba3c2016-11-08 20:54:18 +01005430 rc = asm_safe("fwait");
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005431
Radim Krčmářaabba3c2016-11-08 20:54:18 +01005432 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005433 return emulate_exception(ctxt, MF_VECTOR, 0, false);
5434
5435 return X86EMUL_CONTINUE;
5436}
5437
5438static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
5439 struct operand *op)
5440{
5441 if (op->type == OP_MM)
5442 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
5443}
5444
Avi Kivitye28bbd42013-01-04 16:18:48 +02005445static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
5446{
5447 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005448
Avi Kivityb9fa4092013-02-09 11:31:48 +02005449 if (!(ctxt->d & ByteOp))
5450 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005451
Peter Zijlstra1a29b5b2018-01-25 10:58:13 +01005452 asm("push %[flags]; popf; " CALL_NOSPEC " ; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005453 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
Peter Zijlstra1a29b5b2018-01-25 10:58:13 +01005454 [thunk_target]"+S"(fop), ASM_CALL_CONSTRAINT
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005455 : "c"(ctxt->src2.val));
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005456
Avi Kivitye28bbd42013-01-04 16:18:48 +02005457 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005458 if (!fop) /* exception is returned in fop variable */
5459 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02005460 return X86EMUL_CONTINUE;
5461}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005462
Bandan Das14985072014-04-16 12:46:09 -04005463void init_decode_cache(struct x86_emulate_ctxt *ctxt)
5464{
Bandan Das573e80f2014-04-16 12:46:13 -04005465 memset(&ctxt->rip_relative, 0,
5466 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04005467
Bandan Das14985072014-04-16 12:46:09 -04005468 ctxt->io_read.pos = 0;
5469 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04005470 ctxt->mem_read.end = 0;
5471}
5472
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09005473int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005474{
Mathias Krause0225fb52012-08-30 01:30:16 +02005475 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09005476 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005477 int saved_dst_type = ctxt->dst.type;
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005478 unsigned emul_flags;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005479
Avi Kivity9dac77f2011-06-01 15:34:25 +03005480 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04005481
Gleb Natapovd380a5e2010-02-10 14:21:36 +02005482 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005483 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02005484 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02005485 goto done;
5486 }
5487
Avi Kivity9dac77f2011-06-01 15:34:25 +03005488 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02005489 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03005490 goto done;
5491 }
5492
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005493 emul_flags = ctxt->ops->get_hflags(ctxt);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005494 if (unlikely(ctxt->d &
5495 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
5496 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
5497 (ctxt->d & Undefined)) {
5498 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005499 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005500 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005501
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005502 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
5503 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
5504 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02005505 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005506 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02005507
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005508 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
5509 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02005510 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005511 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02005512
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005513 if (ctxt->d & Mmx) {
5514 rc = flush_pending_x87_faults(ctxt);
5515 if (rc != X86EMUL_CONTINUE)
5516 goto done;
5517 /*
5518 * Now that we know the fpu is exception safe, we can fetch
5519 * operands from it.
5520 */
5521 fetch_possible_mmx_operand(ctxt, &ctxt->src);
5522 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
5523 if (!(ctxt->d & Mov))
5524 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
5525 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02005526
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005527 if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && ctxt->intercept) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005528 rc = emulator_check_intercept(ctxt, ctxt->intercept,
5529 X86_ICPT_PRE_EXCEPT);
5530 if (rc != X86EMUL_CONTINUE)
5531 goto done;
5532 }
5533
Nadav Amit64a38292014-12-10 11:19:04 +02005534 /* Instruction can only be executed in protected mode */
5535 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
5536 rc = emulate_ud(ctxt);
5537 goto done;
5538 }
5539
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005540 /* Privileged instruction can be executed only in CPL=0 */
5541 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Nadav Amit68efa762014-06-18 17:19:35 +03005542 if (ctxt->d & PrivUD)
5543 rc = emulate_ud(ctxt);
5544 else
5545 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02005546 goto done;
5547 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005548
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005549 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04005550 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005551 rc = ctxt->check_perm(ctxt);
5552 if (rc != X86EMUL_CONTINUE)
5553 goto done;
5554 }
5555
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005556 if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005557 rc = emulator_check_intercept(ctxt, ctxt->intercept,
5558 X86_ICPT_POST_EXCEPT);
5559 if (rc != X86EMUL_CONTINUE)
5560 goto done;
5561 }
5562
5563 if (ctxt->rep_prefix && (ctxt->d & String)) {
5564 /* All REP prefixes have the same first termination condition */
5565 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Nadav Amit428e3d02015-04-28 13:06:01 +03005566 string_registers_quirk(ctxt);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005567 ctxt->eip = ctxt->_eip;
Nadav Amit0efb0442015-03-29 16:33:03 +03005568 ctxt->eflags &= ~X86_EFLAGS_RF;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005569 goto done;
5570 }
5571 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02005572 }
5573
Avi Kivity9dac77f2011-06-01 15:34:25 +03005574 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
5575 rc = segmented_read(ctxt, ctxt->src.addr.mem,
5576 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09005577 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005578 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005579 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005580 }
5581
Avi Kivity9dac77f2011-06-01 15:34:25 +03005582 if (ctxt->src2.type == OP_MEM) {
5583 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
5584 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02005585 if (rc != X86EMUL_CONTINUE)
5586 goto done;
5587 }
5588
Avi Kivity9dac77f2011-06-01 15:34:25 +03005589 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005590 goto special_insn;
5591
5592
Avi Kivity9dac77f2011-06-01 15:34:25 +03005593 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02005594 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005595 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
5596 &ctxt->dst.val, ctxt->dst.bytes);
Nadav Amitc205fb72014-12-25 02:52:16 +02005597 if (rc != X86EMUL_CONTINUE) {
Paolo Bonzinid44e1212015-02-09 10:02:05 +01005598 if (!(ctxt->d & NoWrite) &&
5599 rc == X86EMUL_PROPAGATE_FAULT &&
Nadav Amitc205fb72014-12-25 02:52:16 +02005600 ctxt->exception.vector == PF_VECTOR)
5601 ctxt->exception.error_code |= PFERR_WRITE_MASK;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02005602 goto done;
Nadav Amitc205fb72014-12-25 02:52:16 +02005603 }
Avi Kivity038e51d2007-01-22 20:40:40 -08005604 }
Paolo Bonzini4ff6f8e2015-02-12 17:04:47 +01005605 /* Copy full 64-bit value for CMPXCHG8B. */
5606 ctxt->dst.orig_val64 = ctxt->dst.val64;
Avi Kivity038e51d2007-01-22 20:40:40 -08005607
Avi Kivity018a98d2007-11-27 19:30:56 +02005608special_insn:
5609
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005610 if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03005611 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005612 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02005613 if (rc != X86EMUL_CONTINUE)
5614 goto done;
5615 }
5616
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03005617 if (ctxt->rep_prefix && (ctxt->d & String))
Nadav Amit0efb0442015-03-29 16:33:03 +03005618 ctxt->eflags |= X86_EFLAGS_RF;
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03005619 else
Nadav Amit0efb0442015-03-29 16:33:03 +03005620 ctxt->eflags &= ~X86_EFLAGS_RF;
Nadav Amit4467c3f2014-07-21 14:37:29 +03005621
Avi Kivity9dac77f2011-06-01 15:34:25 +03005622 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02005623 if (ctxt->d & Fastop) {
5624 void (*fop)(struct fastop *) = (void *)ctxt->execute;
5625 rc = fastop(ctxt, fop);
5626 if (rc != X86EMUL_CONTINUE)
5627 goto done;
5628 goto writeback;
5629 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03005630 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03005631 if (rc != X86EMUL_CONTINUE)
5632 goto done;
5633 goto writeback;
5634 }
5635
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02005636 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005637 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005638 else if (ctxt->opcode_len == 3)
5639 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005640
Avi Kivity9dac77f2011-06-01 15:34:25 +03005641 switch (ctxt->b) {
Gleb Natapovb2833e32009-04-12 13:36:30 +03005642 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005643 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005644 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005645 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03005646 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005647 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03005648 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03005649 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005650 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03005651 ctxt->dst.type = OP_NONE;
5652 else
5653 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09005654 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005655 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005656 switch (ctxt->op_bytes) {
5657 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
5658 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
5659 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005660 }
5661 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005662 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005663 rc = emulate_int(ctxt, 3);
5664 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005665 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005666 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005667 break;
5668 case 0xce: /* into */
Nadav Amit0efb0442015-03-29 16:33:03 +03005669 if (ctxt->eflags & X86_EFLAGS_OF)
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005670 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005671 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005672 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09005673 case 0xeb: /* jmp rel short */
Nadav Amit234f3ce2014-09-18 22:39:38 +03005674 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity9dac77f2011-06-01 15:34:25 +03005675 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005676 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005677 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03005678 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03005679 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005680 case 0xf5: /* cmc */
5681 /* complement carry flag from eflags reg */
Nadav Amit0efb0442015-03-29 16:33:03 +03005682 ctxt->eflags ^= X86_EFLAGS_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005683 break;
5684 case 0xf8: /* clc */
Nadav Amit0efb0442015-03-29 16:33:03 +03005685 ctxt->eflags &= ~X86_EFLAGS_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005686 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03005687 case 0xf9: /* stc */
Nadav Amit0efb0442015-03-29 16:33:03 +03005688 ctxt->eflags |= X86_EFLAGS_CF;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03005689 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005690 case 0xfc: /* cld */
Nadav Amit0efb0442015-03-29 16:33:03 +03005691 ctxt->eflags &= ~X86_EFLAGS_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005692 break;
5693 case 0xfd: /* std */
Nadav Amit0efb0442015-03-29 16:33:03 +03005694 ctxt->eflags |= X86_EFLAGS_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005695 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005696 default:
5697 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005698 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005699
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005700 if (rc != X86EMUL_CONTINUE)
5701 goto done;
5702
Avi Kivity018a98d2007-11-27 19:30:56 +02005703writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02005704 if (ctxt->d & SrcWrite) {
5705 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
5706 rc = writeback(ctxt, &ctxt->src);
5707 if (rc != X86EMUL_CONTINUE)
5708 goto done;
5709 }
Nadav Amitee212292014-06-15 16:12:58 +03005710 if (!(ctxt->d & NoWrite)) {
5711 rc = writeback(ctxt, &ctxt->dst);
5712 if (rc != X86EMUL_CONTINUE)
5713 goto done;
5714 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005715
Gleb Natapov5cd21912010-03-18 15:20:26 +02005716 /*
5717 * restore dst type in case the decoding will be reused
5718 * (happens for string instruction )
5719 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005720 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005721
Avi Kivity9dac77f2011-06-01 15:34:25 +03005722 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005723 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02005724
Avi Kivity9dac77f2011-06-01 15:34:25 +03005725 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005726 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02005727
Avi Kivity9dac77f2011-06-01 15:34:25 +03005728 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005729 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005730 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005731 if ((ctxt->d & SrcMask) == SrcSI)
5732 count = ctxt->src.count;
5733 else
5734 count = ctxt->dst.count;
Paolo Bonzini01485a22014-11-19 18:25:08 +01005735 register_address_increment(ctxt, VCPU_REGS_RCX, -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005736
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005737 if (!string_insn_completed(ctxt)) {
5738 /*
5739 * Re-enter guest when pio read ahead buffer is empty
5740 * or, if it is not used, after each 1024 iteration.
5741 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005742 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005743 (r->end == 0 || r->end != r->pos)) {
5744 /*
5745 * Reset read cache. Usually happens before
5746 * decode, but since instruction is restarted
5747 * we have to do it here.
5748 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005749 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005750 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005751 return EMULATION_RESTART;
5752 }
5753 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03005754 }
Nadav Amit0efb0442015-03-29 16:33:03 +03005755 ctxt->eflags &= ~X86_EFLAGS_RF;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005756 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005757
Avi Kivity9dac77f2011-06-01 15:34:25 +03005758 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02005759
5760done:
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005761 if (rc == X86EMUL_PROPAGATE_FAULT) {
5762 WARN_ON(ctxt->exception.vector > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +02005763 ctxt->have_exception = true;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005764 }
Joerg Roedel775fde82011-04-04 12:39:24 +02005765 if (rc == X86EMUL_INTERCEPTED)
5766 return EMULATION_INTERCEPTED;
5767
Avi Kivitydd856ef2012-08-27 23:46:17 +03005768 if (rc == X86EMUL_CONTINUE)
5769 writeback_registers(ctxt);
5770
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005771 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005772
5773twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005774 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02005775 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02005776 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005777 break;
5778 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02005779 case 0x0d: /* GrpP (prefetch) */
5780 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02005781 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02005782 break;
5783 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005784 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02005785 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005786 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005787 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005788 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005789 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03005790 if (test_cc(ctxt->b, ctxt->eflags))
5791 ctxt->dst.val = ctxt->src.val;
Nadav Amitb91aa142015-03-30 15:39:19 +03005792 else if (ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005793 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08005794 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03005795 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03005796 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005797 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005798 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08005799 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005800 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08005801 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005802 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005803 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005804 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03005805 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005806 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005807 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005808 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005809 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03005810 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005811 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005812 default:
5813 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005814 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005815
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005816threebyte_insn:
5817
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005818 if (rc != X86EMUL_CONTINUE)
5819 goto done;
5820
Avi Kivity6aa8b732006-12-10 02:21:36 -08005821 goto writeback;
5822
5823cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02005824 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005825}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005826
5827void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
5828{
5829 invalidate_registers(ctxt);
5830}
5831
5832void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
5833{
5834 writeback_registers(ctxt);
5835}
Tom Lendacky0f89b202016-12-14 14:59:23 -05005836
5837bool emulator_can_use_gpa(struct x86_emulate_ctxt *ctxt)
5838{
5839 if (ctxt->rep_prefix && (ctxt->d & String))
5840 return false;
5841
5842 if (ctxt->d & TwoMemOp)
5843 return false;
5844
5845 return true;
5846}