blob: 2d4faefe8dd4358549c02674cdcb8e13608a6649 [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
Sean Christopherson5ae78e92019-12-17 13:32:38 -08002351 return ctxt->ops->guest_has_long_mode(ctxt);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002352#else
2353 return false;
2354#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002355}
2356
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002357static void rsm_set_desc_flags(struct desc_struct *desc, u32 flags)
2358{
2359 desc->g = (flags >> 23) & 1;
2360 desc->d = (flags >> 22) & 1;
2361 desc->l = (flags >> 21) & 1;
2362 desc->avl = (flags >> 20) & 1;
2363 desc->p = (flags >> 15) & 1;
2364 desc->dpl = (flags >> 13) & 3;
2365 desc->s = (flags >> 12) & 1;
2366 desc->type = (flags >> 8) & 15;
2367}
2368
Sean Christophersoned193212019-04-02 08:03:09 -07002369static int rsm_load_seg_32(struct x86_emulate_ctxt *ctxt, const char *smstate,
2370 int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002371{
2372 struct desc_struct desc;
2373 int offset;
2374 u16 selector;
2375
Sean Christophersoned193212019-04-02 08:03:09 -07002376 selector = GET_SMSTATE(u32, smstate, 0x7fa8 + n * 4);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002377
2378 if (n < 3)
2379 offset = 0x7f84 + n * 12;
2380 else
2381 offset = 0x7f2c + (n - 3) * 12;
2382
Sean Christophersoned193212019-04-02 08:03:09 -07002383 set_desc_base(&desc, GET_SMSTATE(u32, smstate, offset + 8));
2384 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, offset + 4));
2385 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smstate, offset));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002386 ctxt->ops->set_segment(ctxt, selector, &desc, 0, n);
2387 return X86EMUL_CONTINUE;
2388}
2389
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002390#ifdef CONFIG_X86_64
Sean Christophersoned193212019-04-02 08:03:09 -07002391static int rsm_load_seg_64(struct x86_emulate_ctxt *ctxt, const char *smstate,
2392 int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002393{
2394 struct desc_struct desc;
2395 int offset;
2396 u16 selector;
2397 u32 base3;
2398
2399 offset = 0x7e00 + n * 16;
2400
Sean Christophersoned193212019-04-02 08:03:09 -07002401 selector = GET_SMSTATE(u16, smstate, offset);
2402 rsm_set_desc_flags(&desc, GET_SMSTATE(u16, smstate, offset + 2) << 8);
2403 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, offset + 4));
2404 set_desc_base(&desc, GET_SMSTATE(u32, smstate, offset + 8));
2405 base3 = GET_SMSTATE(u32, smstate, offset + 12);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002406
2407 ctxt->ops->set_segment(ctxt, selector, &desc, base3, n);
2408 return X86EMUL_CONTINUE;
2409}
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002410#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002411
2412static int rsm_enter_protected_mode(struct x86_emulate_ctxt *ctxt,
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002413 u64 cr0, u64 cr3, u64 cr4)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002414{
2415 int bad;
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002416 u64 pcid;
2417
2418 /* In order to later set CR4.PCIDE, CR3[11:0] must be zero. */
2419 pcid = 0;
2420 if (cr4 & X86_CR4_PCIDE) {
2421 pcid = cr3 & 0xfff;
2422 cr3 &= ~0xfff;
2423 }
2424
2425 bad = ctxt->ops->set_cr(ctxt, 3, cr3);
2426 if (bad)
2427 return X86EMUL_UNHANDLEABLE;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002428
2429 /*
2430 * First enable PAE, long mode needs it before CR0.PG = 1 is set.
2431 * Then enable protected mode. However, PCID cannot be enabled
2432 * if EFER.LMA=0, so set it separately.
2433 */
2434 bad = ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PCIDE);
2435 if (bad)
2436 return X86EMUL_UNHANDLEABLE;
2437
2438 bad = ctxt->ops->set_cr(ctxt, 0, cr0);
2439 if (bad)
2440 return X86EMUL_UNHANDLEABLE;
2441
2442 if (cr4 & X86_CR4_PCIDE) {
2443 bad = ctxt->ops->set_cr(ctxt, 4, cr4);
2444 if (bad)
2445 return X86EMUL_UNHANDLEABLE;
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002446 if (pcid) {
2447 bad = ctxt->ops->set_cr(ctxt, 3, cr3 | pcid);
2448 if (bad)
2449 return X86EMUL_UNHANDLEABLE;
2450 }
2451
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002452 }
2453
2454 return X86EMUL_CONTINUE;
2455}
2456
Sean Christophersoned193212019-04-02 08:03:09 -07002457static int rsm_load_state_32(struct x86_emulate_ctxt *ctxt,
2458 const char *smstate)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002459{
2460 struct desc_struct desc;
2461 struct desc_ptr dt;
2462 u16 selector;
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002463 u32 val, cr0, cr3, cr4;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002464 int i;
2465
Sean Christophersoned193212019-04-02 08:03:09 -07002466 cr0 = GET_SMSTATE(u32, smstate, 0x7ffc);
2467 cr3 = GET_SMSTATE(u32, smstate, 0x7ff8);
2468 ctxt->eflags = GET_SMSTATE(u32, smstate, 0x7ff4) | X86_EFLAGS_FIXED;
2469 ctxt->_eip = GET_SMSTATE(u32, smstate, 0x7ff0);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002470
2471 for (i = 0; i < 8; i++)
Sean Christophersoned193212019-04-02 08:03:09 -07002472 *reg_write(ctxt, i) = GET_SMSTATE(u32, smstate, 0x7fd0 + i * 4);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002473
Sean Christophersoned193212019-04-02 08:03:09 -07002474 val = GET_SMSTATE(u32, smstate, 0x7fcc);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002475 ctxt->ops->set_dr(ctxt, 6, (val & DR6_VOLATILE) | DR6_FIXED_1);
Sean Christophersoned193212019-04-02 08:03:09 -07002476 val = GET_SMSTATE(u32, smstate, 0x7fc8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002477 ctxt->ops->set_dr(ctxt, 7, (val & DR7_VOLATILE) | DR7_FIXED_1);
2478
Sean Christophersoned193212019-04-02 08:03:09 -07002479 selector = GET_SMSTATE(u32, smstate, 0x7fc4);
2480 set_desc_base(&desc, GET_SMSTATE(u32, smstate, 0x7f64));
2481 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, 0x7f60));
2482 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smstate, 0x7f5c));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002483 ctxt->ops->set_segment(ctxt, selector, &desc, 0, VCPU_SREG_TR);
2484
Sean Christophersoned193212019-04-02 08:03:09 -07002485 selector = GET_SMSTATE(u32, smstate, 0x7fc0);
2486 set_desc_base(&desc, GET_SMSTATE(u32, smstate, 0x7f80));
2487 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, 0x7f7c));
2488 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smstate, 0x7f78));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002489 ctxt->ops->set_segment(ctxt, selector, &desc, 0, VCPU_SREG_LDTR);
2490
Sean Christophersoned193212019-04-02 08:03:09 -07002491 dt.address = GET_SMSTATE(u32, smstate, 0x7f74);
2492 dt.size = GET_SMSTATE(u32, smstate, 0x7f70);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002493 ctxt->ops->set_gdt(ctxt, &dt);
2494
Sean Christophersoned193212019-04-02 08:03:09 -07002495 dt.address = GET_SMSTATE(u32, smstate, 0x7f58);
2496 dt.size = GET_SMSTATE(u32, smstate, 0x7f54);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002497 ctxt->ops->set_idt(ctxt, &dt);
2498
2499 for (i = 0; i < 6; i++) {
Sean Christophersoned193212019-04-02 08:03:09 -07002500 int r = rsm_load_seg_32(ctxt, smstate, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002501 if (r != X86EMUL_CONTINUE)
2502 return r;
2503 }
2504
Sean Christophersoned193212019-04-02 08:03:09 -07002505 cr4 = GET_SMSTATE(u32, smstate, 0x7f14);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002506
Sean Christophersoned193212019-04-02 08:03:09 -07002507 ctxt->ops->set_smbase(ctxt, GET_SMSTATE(u32, smstate, 0x7ef8));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002508
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002509 return rsm_enter_protected_mode(ctxt, cr0, cr3, cr4);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002510}
2511
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002512#ifdef CONFIG_X86_64
Sean Christophersoned193212019-04-02 08:03:09 -07002513static int rsm_load_state_64(struct x86_emulate_ctxt *ctxt,
2514 const char *smstate)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002515{
2516 struct desc_struct desc;
2517 struct desc_ptr dt;
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002518 u64 val, cr0, cr3, cr4;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002519 u32 base3;
2520 u16 selector;
Paolo Bonzinib10d92a2015-10-14 15:25:52 +02002521 int i, r;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002522
2523 for (i = 0; i < 16; i++)
Sean Christophersoned193212019-04-02 08:03:09 -07002524 *reg_write(ctxt, i) = GET_SMSTATE(u64, smstate, 0x7ff8 - i * 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002525
Sean Christophersoned193212019-04-02 08:03:09 -07002526 ctxt->_eip = GET_SMSTATE(u64, smstate, 0x7f78);
2527 ctxt->eflags = GET_SMSTATE(u32, smstate, 0x7f70) | X86_EFLAGS_FIXED;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002528
Sean Christophersoned193212019-04-02 08:03:09 -07002529 val = GET_SMSTATE(u32, smstate, 0x7f68);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002530 ctxt->ops->set_dr(ctxt, 6, (val & DR6_VOLATILE) | DR6_FIXED_1);
Sean Christophersoned193212019-04-02 08:03:09 -07002531 val = GET_SMSTATE(u32, smstate, 0x7f60);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002532 ctxt->ops->set_dr(ctxt, 7, (val & DR7_VOLATILE) | DR7_FIXED_1);
2533
Sean Christophersoned193212019-04-02 08:03:09 -07002534 cr0 = GET_SMSTATE(u64, smstate, 0x7f58);
2535 cr3 = GET_SMSTATE(u64, smstate, 0x7f50);
2536 cr4 = GET_SMSTATE(u64, smstate, 0x7f48);
2537 ctxt->ops->set_smbase(ctxt, GET_SMSTATE(u32, smstate, 0x7f00));
2538 val = GET_SMSTATE(u64, smstate, 0x7ed0);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002539 ctxt->ops->set_msr(ctxt, MSR_EFER, val & ~EFER_LMA);
2540
Sean Christophersoned193212019-04-02 08:03:09 -07002541 selector = GET_SMSTATE(u32, smstate, 0x7e90);
2542 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smstate, 0x7e92) << 8);
2543 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, 0x7e94));
2544 set_desc_base(&desc, GET_SMSTATE(u32, smstate, 0x7e98));
2545 base3 = GET_SMSTATE(u32, smstate, 0x7e9c);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002546 ctxt->ops->set_segment(ctxt, selector, &desc, base3, VCPU_SREG_TR);
2547
Sean Christophersoned193212019-04-02 08:03:09 -07002548 dt.size = GET_SMSTATE(u32, smstate, 0x7e84);
2549 dt.address = GET_SMSTATE(u64, smstate, 0x7e88);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002550 ctxt->ops->set_idt(ctxt, &dt);
2551
Sean Christophersoned193212019-04-02 08:03:09 -07002552 selector = GET_SMSTATE(u32, smstate, 0x7e70);
2553 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smstate, 0x7e72) << 8);
2554 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, 0x7e74));
2555 set_desc_base(&desc, GET_SMSTATE(u32, smstate, 0x7e78));
2556 base3 = GET_SMSTATE(u32, smstate, 0x7e7c);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002557 ctxt->ops->set_segment(ctxt, selector, &desc, base3, VCPU_SREG_LDTR);
2558
Sean Christophersoned193212019-04-02 08:03:09 -07002559 dt.size = GET_SMSTATE(u32, smstate, 0x7e64);
2560 dt.address = GET_SMSTATE(u64, smstate, 0x7e68);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002561 ctxt->ops->set_gdt(ctxt, &dt);
2562
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002563 r = rsm_enter_protected_mode(ctxt, cr0, cr3, cr4);
Paolo Bonzinib10d92a2015-10-14 15:25:52 +02002564 if (r != X86EMUL_CONTINUE)
2565 return r;
2566
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002567 for (i = 0; i < 6; i++) {
Sean Christophersoned193212019-04-02 08:03:09 -07002568 r = rsm_load_seg_64(ctxt, smstate, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002569 if (r != X86EMUL_CONTINUE)
2570 return r;
2571 }
2572
Paolo Bonzinib10d92a2015-10-14 15:25:52 +02002573 return X86EMUL_CONTINUE;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002574}
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002575#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002576
Paolo Bonzini64d60672015-05-07 11:36:11 +02002577static int em_rsm(struct x86_emulate_ctxt *ctxt)
2578{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002579 unsigned long cr0, cr4, efer;
Sean Christophersoned193212019-04-02 08:03:09 -07002580 char buf[512];
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002581 u64 smbase;
2582 int ret;
2583
Ladi Prosek6ed071f2017-04-25 16:42:44 +02002584 if ((ctxt->ops->get_hflags(ctxt) & X86EMUL_SMM_MASK) == 0)
Paolo Bonzini64d60672015-05-07 11:36:11 +02002585 return emulate_ud(ctxt);
2586
Sean Christophersoned193212019-04-02 08:03:09 -07002587 smbase = ctxt->ops->get_smbase(ctxt);
2588
2589 ret = ctxt->ops->read_phys(ctxt, smbase + 0xfe00, buf, sizeof(buf));
2590 if (ret != X86EMUL_CONTINUE)
2591 return X86EMUL_UNHANDLEABLE;
2592
Sean Christopherson9ec19492019-04-02 08:03:11 -07002593 if ((ctxt->ops->get_hflags(ctxt) & X86EMUL_SMM_INSIDE_NMI_MASK) == 0)
2594 ctxt->ops->set_nmi_mask(ctxt, false);
2595
2596 ctxt->ops->set_hflags(ctxt, ctxt->ops->get_hflags(ctxt) &
2597 ~(X86EMUL_SMM_INSIDE_NMI_MASK | X86EMUL_SMM_MASK));
2598
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002599 /*
2600 * Get back to real mode, to prepare a safe state in which to load
Paolo Bonzini89651a32015-11-03 13:43:05 +01002601 * CR0/CR3/CR4/EFER. It's all a bit more complicated if the vCPU
2602 * supports long mode.
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002603 */
Paolo Bonzini89651a32015-11-03 13:43:05 +01002604 if (emulator_has_longmode(ctxt)) {
2605 struct desc_struct cs_desc;
2606
2607 /* Zero CR4.PCIDE before CR0.PG. */
Sean Christopherson8f4dc2e2019-04-02 08:10:47 -07002608 cr4 = ctxt->ops->get_cr(ctxt, 4);
2609 if (cr4 & X86_CR4_PCIDE)
Paolo Bonzini89651a32015-11-03 13:43:05 +01002610 ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PCIDE);
Paolo Bonzini89651a32015-11-03 13:43:05 +01002611
2612 /* A 32-bit code segment is required to clear EFER.LMA. */
2613 memset(&cs_desc, 0, sizeof(cs_desc));
2614 cs_desc.type = 0xb;
2615 cs_desc.s = cs_desc.g = cs_desc.p = 1;
2616 ctxt->ops->set_segment(ctxt, 0, &cs_desc, 0, VCPU_SREG_CS);
2617 }
2618
2619 /* For the 64-bit case, this will clear EFER.LMA. */
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002620 cr0 = ctxt->ops->get_cr(ctxt, 0);
2621 if (cr0 & X86_CR0_PE)
2622 ctxt->ops->set_cr(ctxt, 0, cr0 & ~(X86_CR0_PG | X86_CR0_PE));
Paolo Bonzini89651a32015-11-03 13:43:05 +01002623
Sean Christopherson8f4dc2e2019-04-02 08:10:47 -07002624 if (emulator_has_longmode(ctxt)) {
2625 /* Clear CR4.PAE before clearing EFER.LME. */
2626 cr4 = ctxt->ops->get_cr(ctxt, 4);
2627 if (cr4 & X86_CR4_PAE)
2628 ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PAE);
Paolo Bonzini89651a32015-11-03 13:43:05 +01002629
Sean Christopherson8f4dc2e2019-04-02 08:10:47 -07002630 /* And finally go back to 32-bit mode. */
2631 efer = 0;
2632 ctxt->ops->set_msr(ctxt, MSR_EFER, efer);
2633 }
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002634
Ladi Prosek0234bf82017-10-11 16:54:40 +02002635 /*
2636 * Give pre_leave_smm() a chance to make ISA-specific changes to the
2637 * vCPU state (e.g. enter guest mode) before loading state from the SMM
2638 * state-save area.
2639 */
Sean Christophersoned193212019-04-02 08:03:09 -07002640 if (ctxt->ops->pre_leave_smm(ctxt, buf))
Ladi Prosek0234bf82017-10-11 16:54:40 +02002641 return X86EMUL_UNHANDLEABLE;
2642
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002643#ifdef CONFIG_X86_64
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002644 if (emulator_has_longmode(ctxt))
Sean Christophersoned193212019-04-02 08:03:09 -07002645 ret = rsm_load_state_64(ctxt, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002646 else
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002647#endif
Sean Christophersoned193212019-04-02 08:03:09 -07002648 ret = rsm_load_state_32(ctxt, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002649
2650 if (ret != X86EMUL_CONTINUE) {
2651 /* FIXME: should triple fault */
2652 return X86EMUL_UNHANDLEABLE;
2653 }
2654
Sean Christophersonc5833c72019-04-02 08:03:10 -07002655 ctxt->ops->post_leave_smm(ctxt);
2656
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002657 return X86EMUL_CONTINUE;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002658}
2659
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002660static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002661setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002662 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002663{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002664 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002665 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002666 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002667 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002668 cs->type = 0x0b; /* Read, Execute, Accessed */
2669 cs->s = 1;
2670 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002671 cs->p = 1;
2672 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002673 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002674
Gleb Natapov79168fd2010-04-28 19:15:30 +03002675 set_desc_base(ss, 0); /* flat segment */
2676 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002677 ss->g = 1; /* 4kb granularity */
2678 ss->s = 1;
2679 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002680 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002681 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002682 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002683 ss->l = 0;
2684 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002685}
2686
Avi Kivity1a18a692012-02-01 12:23:21 +02002687static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2688{
2689 u32 eax, ebx, ecx, edx;
2690
2691 eax = ecx = 0;
Yu Zhange911eb32017-08-24 20:27:52 +08002692 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, false);
Avi Kivity0017f932012-06-07 14:10:16 +03002693 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002694 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2695 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2696}
2697
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002698static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2699{
Mathias Krause0225fb52012-08-30 01:30:16 +02002700 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002701 u32 eax, ebx, ecx, edx;
2702
2703 /*
2704 * syscall should always be enabled in longmode - so only become
2705 * vendor specific (cpuid) if other modes are active...
2706 */
2707 if (ctxt->mode == X86EMUL_MODE_PROT64)
2708 return true;
2709
2710 eax = 0x00000000;
2711 ecx = 0x00000000;
Yu Zhange911eb32017-08-24 20:27:52 +08002712 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, false);
Avi Kivity0017f932012-06-07 14:10:16 +03002713 /*
2714 * Intel ("GenuineIntel")
2715 * remark: Intel CPUs only support "syscall" in 64bit
2716 * longmode. Also an 64bit guest with a
2717 * 32bit compat-app running will #UD !! While this
2718 * behaviour can be fixed (by emulating) into AMD
2719 * response - CPUs of AMD can't behave like Intel.
2720 */
2721 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2722 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2723 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2724 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002725
Avi Kivity0017f932012-06-07 14:10:16 +03002726 /* AMD ("AuthenticAMD") */
2727 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2728 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2729 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2730 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002731
Avi Kivity0017f932012-06-07 14:10:16 +03002732 /* AMD ("AMDisbetter!") */
2733 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2734 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2735 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2736 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002737
Pu Wenb8f4abb2018-09-23 17:36:31 +08002738 /* Hygon ("HygonGenuine") */
2739 if (ebx == X86EMUL_CPUID_VENDOR_HygonGenuine_ebx &&
2740 ecx == X86EMUL_CPUID_VENDOR_HygonGenuine_ecx &&
2741 edx == X86EMUL_CPUID_VENDOR_HygonGenuine_edx)
2742 return true;
2743
2744 /*
2745 * default: (not Intel, not AMD, not Hygon), apply Intel's
2746 * stricter rules...
2747 */
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002748 return false;
2749}
2750
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002751static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002752{
Mathias Krause0225fb52012-08-30 01:30:16 +02002753 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002754 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002755 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002756 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002757 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002758
2759 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002760 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002761 ctxt->mode == X86EMUL_MODE_VM86)
2762 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002763
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002764 if (!(em_syscall_is_enabled(ctxt)))
2765 return emulate_ud(ctxt);
2766
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002767 ops->get_msr(ctxt, MSR_EFER, &efer);
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002768 if (!(efer & EFER_SCE))
2769 return emulate_ud(ctxt);
2770
Miaohe Lin5b4ce932019-11-09 16:58:54 +08002771 setup_syscalls_segments(ctxt, &cs, &ss);
Avi Kivity717746e2011-04-20 13:37:53 +03002772 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002773 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002774 cs_sel = (u16)(msr_data & 0xfffc);
2775 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002776
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002777 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002778 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002779 cs.l = 1;
2780 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002781 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2782 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002783
Avi Kivitydd856ef2012-08-27 23:46:17 +03002784 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002785 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002786#ifdef CONFIG_X86_64
Nadav Amit6c6cb692014-07-21 14:37:30 +03002787 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002788
Avi Kivity717746e2011-04-20 13:37:53 +03002789 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002790 ctxt->mode == X86EMUL_MODE_PROT64 ?
2791 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002792 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002793
Avi Kivity717746e2011-04-20 13:37:53 +03002794 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Nadav Amit6c6cb692014-07-21 14:37:30 +03002795 ctxt->eflags &= ~msr_data;
Wanpeng Li35fd68a2015-04-08 14:08:14 +08002796 ctxt->eflags |= X86_EFLAGS_FIXED;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002797#endif
2798 } else {
2799 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002800 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002801 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002802
Nadav Amit0efb0442015-03-29 16:33:03 +03002803 ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002804 }
2805
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02002806 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002807 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002808}
2809
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002810static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002811{
Mathias Krause0225fb52012-08-30 01:30:16 +02002812 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002813 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002814 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002815 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002816 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002817
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002818 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002819 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002820 if (ctxt->mode == X86EMUL_MODE_REAL)
2821 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002822
Avi Kivity1a18a692012-02-01 12:23:21 +02002823 /*
2824 * Not recognized on AMD in compat mode (but is recognized in legacy
2825 * mode).
2826 */
Nadav Amitf3747372015-01-01 23:11:11 +02002827 if ((ctxt->mode != X86EMUL_MODE_PROT64) && (efer & EFER_LMA)
Avi Kivity1a18a692012-02-01 12:23:21 +02002828 && !vendor_intel(ctxt))
2829 return emulate_ud(ctxt);
2830
Nadav Amitb2c9d432014-11-02 11:55:01 +02002831 /* sysenter/sysexit have not been tested in 64bit mode. */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002832 if (ctxt->mode == X86EMUL_MODE_PROT64)
Nadav Amitb2c9d432014-11-02 11:55:01 +02002833 return X86EMUL_UNHANDLEABLE;
Andre Przywara8c604352009-06-18 12:56:01 +02002834
Avi Kivity717746e2011-04-20 13:37:53 +03002835 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002836 if ((msr_data & 0xfffc) == 0x0)
2837 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002838
Miaohe Lin5b4ce932019-11-09 16:58:54 +08002839 setup_syscalls_segments(ctxt, &cs, &ss);
Nadav Amit0efb0442015-03-29 16:33:03 +03002840 ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
Nadav Amitb32a9912015-03-29 16:33:04 +03002841 cs_sel = (u16)msr_data & ~SEGMENT_RPL_MASK;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002842 ss_sel = cs_sel + 8;
Nadav Amitf3747372015-01-01 23:11:11 +02002843 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002844 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002845 cs.l = 1;
2846 }
2847
Avi Kivity1aa36612011-04-27 13:20:30 +03002848 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2849 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002850
Avi Kivity717746e2011-04-20 13:37:53 +03002851 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002852 ctxt->_eip = (efer & EFER_LMA) ? msr_data : (u32)msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002853
Avi Kivity717746e2011-04-20 13:37:53 +03002854 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002855 *reg_write(ctxt, VCPU_REGS_RSP) = (efer & EFER_LMA) ? msr_data :
2856 (u32)msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002857
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002858 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002859}
2860
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002861static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002862{
Mathias Krause0225fb52012-08-30 01:30:16 +02002863 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002864 struct desc_struct cs, ss;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002865 u64 msr_data, rcx, rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002866 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002867 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002868
Gleb Natapova0044752010-02-10 14:21:31 +02002869 /* inject #GP if in real mode or Virtual 8086 mode */
2870 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002871 ctxt->mode == X86EMUL_MODE_VM86)
2872 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002873
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002874 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002875
Avi Kivity9dac77f2011-06-01 15:34:25 +03002876 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002877 usermode = X86EMUL_MODE_PROT64;
2878 else
2879 usermode = X86EMUL_MODE_PROT32;
2880
Nadav Amit234f3ce2014-09-18 22:39:38 +03002881 rcx = reg_read(ctxt, VCPU_REGS_RCX);
2882 rdx = reg_read(ctxt, VCPU_REGS_RDX);
2883
Andre Przywara4668f052009-06-18 12:56:02 +02002884 cs.dpl = 3;
2885 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002886 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002887 switch (usermode) {
2888 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002889 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002890 if ((msr_data & 0xfffc) == 0x0)
2891 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002892 ss_sel = (u16)(msr_data + 24);
Nadav Amitbf0b6822014-09-18 22:39:45 +03002893 rcx = (u32)rcx;
2894 rdx = (u32)rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002895 break;
2896 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002897 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002898 if (msr_data == 0x0)
2899 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002900 ss_sel = cs_sel + 8;
2901 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002902 cs.l = 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08002903 if (emul_is_noncanonical_address(rcx, ctxt) ||
2904 emul_is_noncanonical_address(rdx, ctxt))
Nadav Amit234f3ce2014-09-18 22:39:38 +03002905 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002906 break;
2907 }
Nadav Amitb32a9912015-03-29 16:33:04 +03002908 cs_sel |= SEGMENT_RPL_MASK;
2909 ss_sel |= SEGMENT_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002910
Avi Kivity1aa36612011-04-27 13:20:30 +03002911 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2912 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002913
Nadav Amit234f3ce2014-09-18 22:39:38 +03002914 ctxt->_eip = rdx;
2915 *reg_write(ctxt, VCPU_REGS_RSP) = rcx;
Andre Przywara4668f052009-06-18 12:56:02 +02002916
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002917 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002918}
2919
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002920static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002921{
2922 int iopl;
2923 if (ctxt->mode == X86EMUL_MODE_REAL)
2924 return false;
2925 if (ctxt->mode == X86EMUL_MODE_VM86)
2926 return true;
Nadav Amit0efb0442015-03-29 16:33:03 +03002927 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002928 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002929}
2930
Liran Alon9a29d4492018-03-12 13:12:48 +02002931#define VMWARE_PORT_VMPORT (0x5658)
2932#define VMWARE_PORT_VMRPC (0x5659)
2933
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002934static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002935 u16 port, u16 len)
2936{
Mathias Krause0225fb52012-08-30 01:30:16 +02002937 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002938 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002939 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002940 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002941 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002942 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002943 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002944
Liran Alon9a29d4492018-03-12 13:12:48 +02002945 /*
2946 * VMware allows access to these ports even if denied
2947 * by TSS I/O permission bitmap. Mimic behavior.
2948 */
2949 if (enable_vmware_backdoor &&
2950 ((port == VMWARE_PORT_VMPORT) || (port == VMWARE_PORT_VMRPC)))
2951 return true;
2952
Avi Kivity1aa36612011-04-27 13:20:30 +03002953 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002954 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002955 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002956 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002957 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002958 base = get_desc_base(&tr_seg);
2959#ifdef CONFIG_X86_64
2960 base |= ((u64)base3) << 32;
2961#endif
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02002962 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL, true);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002963 if (r != X86EMUL_CONTINUE)
2964 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002965 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002966 return false;
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02002967 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL, true);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002968 if (r != X86EMUL_CONTINUE)
2969 return false;
2970 if ((perm >> bit_idx) & mask)
2971 return false;
2972 return true;
2973}
2974
2975static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002976 u16 port, u16 len)
2977{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002978 if (ctxt->perm_ok)
2979 return true;
2980
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002981 if (emulator_bad_iopl(ctxt))
2982 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002983 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002984
2985 ctxt->perm_ok = true;
2986
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002987 return true;
2988}
2989
Nadav Amit428e3d02015-04-28 13:06:01 +03002990static void string_registers_quirk(struct x86_emulate_ctxt *ctxt)
2991{
2992 /*
2993 * Intel CPUs mask the counter and pointers in quite strange
2994 * manner when ECX is zero due to REP-string optimizations.
2995 */
2996#ifdef CONFIG_X86_64
2997 if (ctxt->ad_bytes != 4 || !vendor_intel(ctxt))
2998 return;
2999
3000 *reg_write(ctxt, VCPU_REGS_RCX) = 0;
3001
3002 switch (ctxt->b) {
3003 case 0xa4: /* movsb */
3004 case 0xa5: /* movsd/w */
3005 *reg_rmw(ctxt, VCPU_REGS_RSI) &= (u32)-1;
3006 /* fall through */
3007 case 0xaa: /* stosb */
3008 case 0xab: /* stosd/w */
3009 *reg_rmw(ctxt, VCPU_REGS_RDI) &= (u32)-1;
3010 }
3011#endif
3012}
3013
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003014static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003015 struct tss_segment_16 *tss)
3016{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003017 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003018 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003019 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
3020 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
3021 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
3022 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
3023 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
3024 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
3025 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
3026 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003027
Avi Kivity1aa36612011-04-27 13:20:30 +03003028 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
3029 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
3030 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
3031 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
3032 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003033}
3034
3035static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003036 struct tss_segment_16 *tss)
3037{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003038 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003039 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003040
Avi Kivity9dac77f2011-06-01 15:34:25 +03003041 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003042 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003043 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
3044 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
3045 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
3046 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
3047 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
3048 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
3049 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
3050 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003051
3052 /*
3053 * SDM says that segment selectors are loaded before segment
3054 * descriptors
3055 */
Avi Kivity1aa36612011-04-27 13:20:30 +03003056 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
3057 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
3058 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
3059 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
3060 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003061
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003062 cpl = tss->cs & 3;
3063
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003064 /*
Guo Chaofc058682012-06-28 15:19:51 +08003065 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003066 * it is handled in a context of new task
3067 */
Nadav Amitd1442d82014-09-18 22:39:39 +03003068 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003069 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003070 if (ret != X86EMUL_CONTINUE)
3071 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003072 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003073 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003074 if (ret != X86EMUL_CONTINUE)
3075 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003076 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003077 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003078 if (ret != X86EMUL_CONTINUE)
3079 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003080 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003081 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003082 if (ret != X86EMUL_CONTINUE)
3083 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003084 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003085 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003086 if (ret != X86EMUL_CONTINUE)
3087 return ret;
3088
3089 return X86EMUL_CONTINUE;
3090}
3091
3092static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003093 u16 tss_selector, u16 old_tss_sel,
3094 ulong old_tss_base, struct desc_struct *new_desc)
3095{
3096 struct tss_segment_16 tss_seg;
3097 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003098 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003099
Jordan Borgner0e96f312018-10-28 12:58:28 +00003100 ret = linear_read_system(ctxt, old_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003101 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003102 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003103
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003104 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003105
Jordan Borgner0e96f312018-10-28 12:58:28 +00003106 ret = linear_write_system(ctxt, old_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003107 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003108 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003109
Jordan Borgner0e96f312018-10-28 12:58:28 +00003110 ret = linear_read_system(ctxt, new_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003111 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003112 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003113
3114 if (old_tss_sel != 0xffff) {
3115 tss_seg.prev_task_link = old_tss_sel;
3116
Paolo Bonzini79367a62018-06-06 16:43:02 +02003117 ret = linear_write_system(ctxt, new_tss_base,
3118 &tss_seg.prev_task_link,
Jordan Borgner0e96f312018-10-28 12:58:28 +00003119 sizeof(tss_seg.prev_task_link));
Avi Kivitydb297e32010-11-22 17:53:24 +02003120 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003121 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003122 }
3123
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003124 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003125}
3126
3127static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003128 struct tss_segment_32 *tss)
3129{
Nadav Amit5c7411e2014-04-07 18:37:47 +03003130 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003131 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003132 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003133 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
3134 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
3135 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
3136 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
3137 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
3138 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
3139 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
3140 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003141
Avi Kivity1aa36612011-04-27 13:20:30 +03003142 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
3143 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
3144 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
3145 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
3146 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
3147 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003148}
3149
3150static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003151 struct tss_segment_32 *tss)
3152{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003153 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003154 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003155
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003156 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003157 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003158 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003159 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01003160
3161 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03003162 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
3163 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
3164 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
3165 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
3166 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
3167 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
3168 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
3169 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003170
3171 /*
3172 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003173 * descriptors. This is important because CPL checks will
3174 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003175 */
Avi Kivity1aa36612011-04-27 13:20:30 +03003176 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
3177 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
3178 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
3179 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
3180 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
3181 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
3182 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003183
3184 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01003185 * If we're switching between Protected Mode and VM86, we need to make
3186 * sure to update the mode before loading the segment descriptors so
3187 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01003188 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003189 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01003190 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003191 cpl = 3;
3192 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01003193 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003194 cpl = tss->cs & 3;
3195 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01003196
3197 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003198 * Now load segment descriptors. If fault happenes at this stage
3199 * it is handled in a context of new task
3200 */
Nadav Amitd1442d82014-09-18 22:39:39 +03003201 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003202 cpl, X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003203 if (ret != X86EMUL_CONTINUE)
3204 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003205 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003206 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003207 if (ret != X86EMUL_CONTINUE)
3208 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003209 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003210 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003211 if (ret != X86EMUL_CONTINUE)
3212 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003213 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003214 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003215 if (ret != X86EMUL_CONTINUE)
3216 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003217 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003218 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003219 if (ret != X86EMUL_CONTINUE)
3220 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003221 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003222 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003223 if (ret != X86EMUL_CONTINUE)
3224 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003225 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003226 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003227
Eugene Korenevsky2f729b12015-03-29 01:27:17 +03003228 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003229}
3230
3231static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003232 u16 tss_selector, u16 old_tss_sel,
3233 ulong old_tss_base, struct desc_struct *new_desc)
3234{
3235 struct tss_segment_32 tss_seg;
3236 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003237 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03003238 u32 eip_offset = offsetof(struct tss_segment_32, eip);
3239 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003240
Jordan Borgner0e96f312018-10-28 12:58:28 +00003241 ret = linear_read_system(ctxt, old_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003242 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003243 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003244
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003245 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003246
Nadav Amit5c7411e2014-04-07 18:37:47 +03003247 /* Only GP registers and segment selectors are saved */
Paolo Bonzini79367a62018-06-06 16:43:02 +02003248 ret = linear_write_system(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
3249 ldt_sel_offset - eip_offset);
Avi Kivitydb297e32010-11-22 17:53:24 +02003250 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003251 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003252
Jordan Borgner0e96f312018-10-28 12:58:28 +00003253 ret = linear_read_system(ctxt, new_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003254 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003255 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003256
3257 if (old_tss_sel != 0xffff) {
3258 tss_seg.prev_task_link = old_tss_sel;
3259
Paolo Bonzini79367a62018-06-06 16:43:02 +02003260 ret = linear_write_system(ctxt, new_tss_base,
3261 &tss_seg.prev_task_link,
Jordan Borgner0e96f312018-10-28 12:58:28 +00003262 sizeof(tss_seg.prev_task_link));
Avi Kivitydb297e32010-11-22 17:53:24 +02003263 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003264 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003265 }
3266
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003267 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003268}
3269
3270static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003271 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003272 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003273{
Mathias Krause0225fb52012-08-30 01:30:16 +02003274 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003275 struct desc_struct curr_tss_desc, next_tss_desc;
3276 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03003277 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003278 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03003279 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02003280 u32 desc_limit;
Nadav Amit3db176d2015-04-19 21:12:59 +03003281 ulong desc_addr, dr7;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003282
3283 /* FIXME: old_tss_base == ~0 ? */
3284
Avi Kivitye9194642012-06-13 16:29:39 +03003285 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003286 if (ret != X86EMUL_CONTINUE)
3287 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03003288 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003289 if (ret != X86EMUL_CONTINUE)
3290 return ret;
3291
3292 /* FIXME: check that next_tss_desc is tss */
3293
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003294 /*
3295 * Check privileges. The three cases are task switch caused by...
3296 *
3297 * 1. jmp/call/int to task gate: Check against DPL of the task gate
3298 * 2. Exception/IRQ/iret: No check is performed
Nadav Amit2c2ca2d2014-11-02 11:54:57 +02003299 * 3. jmp/call to TSS/task-gate: No check is performed since the
3300 * hardware checks it before exiting.
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003301 */
3302 if (reason == TASK_SWITCH_GATE) {
3303 if (idt_index != -1) {
3304 /* Software interrupts */
3305 struct desc_struct task_gate_desc;
3306 int dpl;
3307
3308 ret = read_interrupt_descriptor(ctxt, idt_index,
3309 &task_gate_desc);
3310 if (ret != X86EMUL_CONTINUE)
3311 return ret;
3312
3313 dpl = task_gate_desc.dpl;
3314 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
3315 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
3316 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003317 }
3318
Gleb Natapovceffb452010-03-18 15:20:19 +02003319 desc_limit = desc_limit_scaled(&next_tss_desc);
3320 if (!next_tss_desc.p ||
3321 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
3322 desc_limit < 0x2b)) {
Paolo Bonzini592f0852014-08-20 10:05:08 +02003323 return emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003324 }
3325
3326 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
3327 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003328 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003329 }
3330
3331 if (reason == TASK_SWITCH_IRET)
3332 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
3333
3334 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08003335 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003336 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
3337 old_tss_sel = 0xffff;
3338
3339 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003340 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003341 old_tss_base, &next_tss_desc);
3342 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003343 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003344 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02003345 if (ret != X86EMUL_CONTINUE)
3346 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003347
3348 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
3349 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
3350
3351 if (reason != TASK_SWITCH_IRET) {
3352 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003353 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003354 }
3355
Avi Kivity717746e2011-04-20 13:37:53 +03003356 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03003357 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003358
Jan Kiszkae269fb22010-04-14 15:51:09 +02003359 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003360 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
3361 ctxt->lock_prefix = 0;
3362 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003363 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02003364 }
3365
Nadav Amit3db176d2015-04-19 21:12:59 +03003366 ops->get_dr(ctxt, 7, &dr7);
3367 ops->set_dr(ctxt, 7, dr7 & ~(DR_LOCAL_ENABLE_MASK | DR_LOCAL_SLOWDOWN));
3368
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003369 return ret;
3370}
3371
3372int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003373 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003374 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003375{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003376 int rc;
3377
Avi Kivitydd856ef2012-08-27 23:46:17 +03003378 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003379 ctxt->_eip = ctxt->eip;
3380 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003381
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003382 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003383 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003384
Avi Kivitydd856ef2012-08-27 23:46:17 +03003385 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003386 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003387 writeback_registers(ctxt);
3388 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003389
Gleb Natapova0c0ab22011-03-28 16:57:49 +02003390 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003391}
3392
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03003393static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
3394 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02003395{
Nadav Amit0efb0442015-03-29 16:33:03 +03003396 int df = (ctxt->eflags & X86_EFLAGS_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02003397
Paolo Bonzini01485a22014-11-19 18:25:08 +01003398 register_address_increment(ctxt, reg, df * op->bytes);
3399 op->addr.mem.ea = register_address(ctxt, reg);
Gleb Natapova682e352010-03-18 15:20:21 +02003400}
3401
Avi Kivity7af04fc2010-08-18 14:16:35 +03003402static int em_das(struct x86_emulate_ctxt *ctxt)
3403{
Avi Kivity7af04fc2010-08-18 14:16:35 +03003404 u8 al, old_al;
3405 bool af, cf, old_cf;
3406
3407 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003408 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03003409
3410 old_al = al;
3411 old_cf = cf;
3412 cf = false;
3413 af = ctxt->eflags & X86_EFLAGS_AF;
3414 if ((al & 0x0f) > 9 || af) {
3415 al -= 6;
3416 cf = old_cf | (al >= 250);
3417 af = true;
3418 } else {
3419 af = false;
3420 }
3421 if (old_al > 0x99 || old_cf) {
3422 al -= 0x60;
3423 cf = true;
3424 }
3425
Avi Kivity9dac77f2011-06-01 15:34:25 +03003426 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03003427 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003428 ctxt->src.type = OP_IMM;
3429 ctxt->src.val = 0;
3430 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02003431 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03003432 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
3433 if (cf)
3434 ctxt->eflags |= X86_EFLAGS_CF;
3435 if (af)
3436 ctxt->eflags |= X86_EFLAGS_AF;
3437 return X86EMUL_CONTINUE;
3438}
3439
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003440static int em_aam(struct x86_emulate_ctxt *ctxt)
3441{
3442 u8 al, ah;
3443
3444 if (ctxt->src.val == 0)
3445 return emulate_de(ctxt);
3446
3447 al = ctxt->dst.val & 0xff;
3448 ah = al / ctxt->src.val;
3449 al %= ctxt->src.val;
3450
3451 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
3452
3453 /* Set PF, ZF, SF */
3454 ctxt->src.type = OP_IMM;
3455 ctxt->src.val = 0;
3456 ctxt->src.bytes = 1;
3457 fastop(ctxt, em_or);
3458
3459 return X86EMUL_CONTINUE;
3460}
3461
Gleb Natapov7f662272012-12-10 11:42:30 +02003462static int em_aad(struct x86_emulate_ctxt *ctxt)
3463{
3464 u8 al = ctxt->dst.val & 0xff;
3465 u8 ah = (ctxt->dst.val >> 8) & 0xff;
3466
3467 al = (al + (ah * ctxt->src.val)) & 0xff;
3468
3469 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
3470
Gleb Natapovf583c292013-02-13 17:50:39 +02003471 /* Set PF, ZF, SF */
3472 ctxt->src.type = OP_IMM;
3473 ctxt->src.val = 0;
3474 ctxt->src.bytes = 1;
3475 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02003476
3477 return X86EMUL_CONTINUE;
3478}
3479
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003480static int em_call(struct x86_emulate_ctxt *ctxt)
3481{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003482 int rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003483 long rel = ctxt->src.val;
3484
3485 ctxt->src.val = (unsigned long)ctxt->_eip;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003486 rc = jmp_rel(ctxt, rel);
3487 if (rc != X86EMUL_CONTINUE)
3488 return rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003489 return em_push(ctxt);
3490}
3491
Avi Kivity0ef753b2010-08-18 14:51:45 +03003492static int em_call_far(struct x86_emulate_ctxt *ctxt)
3493{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003494 u16 sel, old_cs;
3495 ulong old_eip;
3496 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03003497 struct desc_struct old_desc, new_desc;
3498 const struct x86_emulate_ops *ops = ctxt->ops;
3499 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amit82268082015-01-26 09:32:27 +02003500 enum x86emul_mode prev_mode = ctxt->mode;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003501
Avi Kivity9dac77f2011-06-01 15:34:25 +03003502 old_eip = ctxt->_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003503 ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003504
Avi Kivity9dac77f2011-06-01 15:34:25 +03003505 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003506 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
3507 X86_TRANSFER_CALL_JMP, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003508 if (rc != X86EMUL_CONTINUE)
Nadav Amit80976db2014-12-25 02:52:20 +02003509 return rc;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003510
Nadav Amitd50eaa12014-11-19 17:43:11 +02003511 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003512 if (rc != X86EMUL_CONTINUE)
3513 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003514
Avi Kivity9dac77f2011-06-01 15:34:25 +03003515 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003516 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003517 if (rc != X86EMUL_CONTINUE)
Nadav Amitd1442d82014-09-18 22:39:39 +03003518 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003519
Avi Kivity9dac77f2011-06-01 15:34:25 +03003520 ctxt->src.val = old_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003521 rc = em_push(ctxt);
3522 /* If we failed, we tainted the memory, but the very least we should
3523 restore cs */
Nadav Amit82268082015-01-26 09:32:27 +02003524 if (rc != X86EMUL_CONTINUE) {
3525 pr_warn_once("faulting far call emulation tainted memory\n");
Nadav Amitd1442d82014-09-18 22:39:39 +03003526 goto fail;
Nadav Amit82268082015-01-26 09:32:27 +02003527 }
Nadav Amitd1442d82014-09-18 22:39:39 +03003528 return rc;
3529fail:
3530 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
Nadav Amit82268082015-01-26 09:32:27 +02003531 ctxt->mode = prev_mode;
Nadav Amitd1442d82014-09-18 22:39:39 +03003532 return rc;
3533
Avi Kivity0ef753b2010-08-18 14:51:45 +03003534}
3535
Avi Kivity40ece7c2010-08-18 15:12:09 +03003536static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3537{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003538 int rc;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003539 unsigned long eip;
Avi Kivity40ece7c2010-08-18 15:12:09 +03003540
Nadav Amit234f3ce2014-09-18 22:39:38 +03003541 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
3542 if (rc != X86EMUL_CONTINUE)
3543 return rc;
3544 rc = assign_eip_near(ctxt, eip);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003545 if (rc != X86EMUL_CONTINUE)
3546 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003547 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003548 return X86EMUL_CONTINUE;
3549}
3550
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003551static int em_xchg(struct x86_emulate_ctxt *ctxt)
3552{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003553 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003554 ctxt->src.val = ctxt->dst.val;
3555 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003556
3557 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003558 ctxt->dst.val = ctxt->src.orig_val;
3559 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003560 return X86EMUL_CONTINUE;
3561}
3562
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003563static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3564{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003565 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003566 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003567}
3568
Avi Kivity61429142010-08-19 15:13:00 +03003569static int em_cwd(struct x86_emulate_ctxt *ctxt)
3570{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003571 ctxt->dst.type = OP_REG;
3572 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003573 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003574 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003575
3576 return X86EMUL_CONTINUE;
3577}
3578
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02003579static int em_rdpid(struct x86_emulate_ctxt *ctxt)
3580{
3581 u64 tsc_aux = 0;
3582
3583 if (ctxt->ops->get_msr(ctxt, MSR_TSC_AUX, &tsc_aux))
3584 return emulate_gp(ctxt, 0);
3585 ctxt->dst.val = tsc_aux;
3586 return X86EMUL_CONTINUE;
3587}
3588
Avi Kivity48bb5d32010-08-18 18:54:34 +03003589static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3590{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003591 u64 tsc = 0;
3592
Avi Kivity717746e2011-04-20 13:37:53 +03003593 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003594 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3595 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003596 return X86EMUL_CONTINUE;
3597}
3598
Avi Kivity222d21a2011-11-10 14:57:30 +02003599static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3600{
3601 u64 pmc;
3602
Avi Kivitydd856ef2012-08-27 23:46:17 +03003603 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003604 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003605 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3606 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003607 return X86EMUL_CONTINUE;
3608}
3609
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003610static int em_mov(struct x86_emulate_ctxt *ctxt)
3611{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01003612 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003613 return X86EMUL_CONTINUE;
3614}
3615
Borislav Petkov84cffe42013-10-29 12:54:56 +01003616static int em_movbe(struct x86_emulate_ctxt *ctxt)
3617{
Borislav Petkov84cffe42013-10-29 12:54:56 +01003618 u16 tmp;
3619
Sean Christopherson5ae78e92019-12-17 13:32:38 -08003620 if (!ctxt->ops->guest_has_movbe(ctxt))
Borislav Petkov84cffe42013-10-29 12:54:56 +01003621 return emulate_ud(ctxt);
3622
3623 switch (ctxt->op_bytes) {
3624 case 2:
3625 /*
3626 * From MOVBE definition: "...When the operand size is 16 bits,
3627 * the upper word of the destination register remains unchanged
3628 * ..."
3629 *
3630 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3631 * rules so we have to do the operation almost per hand.
3632 */
3633 tmp = (u16)ctxt->src.val;
3634 ctxt->dst.val &= ~0xffffUL;
3635 ctxt->dst.val |= (unsigned long)swab16(tmp);
3636 break;
3637 case 4:
3638 ctxt->dst.val = swab32((u32)ctxt->src.val);
3639 break;
3640 case 8:
3641 ctxt->dst.val = swab64(ctxt->src.val);
3642 break;
3643 default:
Paolo Bonzini592f0852014-08-20 10:05:08 +02003644 BUG();
Borislav Petkov84cffe42013-10-29 12:54:56 +01003645 }
3646 return X86EMUL_CONTINUE;
3647}
3648
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003649static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3650{
3651 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3652 return emulate_gp(ctxt, 0);
3653
3654 /* Disable writeback. */
3655 ctxt->dst.type = OP_NONE;
3656 return X86EMUL_CONTINUE;
3657}
3658
3659static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3660{
3661 unsigned long val;
3662
3663 if (ctxt->mode == X86EMUL_MODE_PROT64)
3664 val = ctxt->src.val & ~0ULL;
3665 else
3666 val = ctxt->src.val & ~0U;
3667
3668 /* #UD condition is already handled. */
3669 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3670 return emulate_gp(ctxt, 0);
3671
3672 /* Disable writeback. */
3673 ctxt->dst.type = OP_NONE;
3674 return X86EMUL_CONTINUE;
3675}
3676
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003677static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3678{
3679 u64 msr_data;
3680
Avi Kivitydd856ef2012-08-27 23:46:17 +03003681 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3682 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3683 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003684 return emulate_gp(ctxt, 0);
3685
3686 return X86EMUL_CONTINUE;
3687}
3688
3689static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3690{
3691 u64 msr_data;
3692
Avi Kivitydd856ef2012-08-27 23:46:17 +03003693 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003694 return emulate_gp(ctxt, 0);
3695
Avi Kivitydd856ef2012-08-27 23:46:17 +03003696 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3697 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003698 return X86EMUL_CONTINUE;
3699}
3700
Paolo Bonzinidd307d02016-07-12 10:35:51 +02003701static int em_store_sreg(struct x86_emulate_ctxt *ctxt, int segment)
3702{
3703 if (segment > VCPU_SREG_GS &&
3704 (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_UMIP) &&
3705 ctxt->ops->cpl(ctxt) > 0)
3706 return emulate_gp(ctxt, 0);
3707
3708 ctxt->dst.val = get_segment_selector(ctxt, segment);
3709 if (ctxt->dst.bytes == 4 && ctxt->dst.type == OP_MEM)
3710 ctxt->dst.bytes = 2;
3711 return X86EMUL_CONTINUE;
3712}
3713
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003714static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3715{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003716 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003717 return emulate_ud(ctxt);
3718
Paolo Bonzinidd307d02016-07-12 10:35:51 +02003719 return em_store_sreg(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003720}
3721
3722static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3723{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003724 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003725
Avi Kivity9dac77f2011-06-01 15:34:25 +03003726 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003727 return emulate_ud(ctxt);
3728
Avi Kivity9dac77f2011-06-01 15:34:25 +03003729 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003730 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3731
3732 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003733 ctxt->dst.type = OP_NONE;
3734 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003735}
3736
Paolo Bonzinidd307d02016-07-12 10:35:51 +02003737static int em_sldt(struct x86_emulate_ctxt *ctxt)
3738{
3739 return em_store_sreg(ctxt, VCPU_SREG_LDTR);
3740}
3741
Avi Kivitya14e5792012-06-13 12:28:33 +03003742static int em_lldt(struct x86_emulate_ctxt *ctxt)
3743{
3744 u16 sel = ctxt->src.val;
3745
3746 /* Disable writeback. */
3747 ctxt->dst.type = OP_NONE;
3748 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3749}
3750
Paolo Bonzinidd307d02016-07-12 10:35:51 +02003751static int em_str(struct x86_emulate_ctxt *ctxt)
3752{
3753 return em_store_sreg(ctxt, VCPU_SREG_TR);
3754}
3755
Avi Kivity80890002012-06-13 16:33:29 +03003756static int em_ltr(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_TR);
3763}
3764
Avi Kivity38503912011-03-31 18:48:09 +02003765static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3766{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003767 int rc;
3768 ulong linear;
3769
Avi Kivity9dac77f2011-06-01 15:34:25 +03003770 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003771 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003772 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003773 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003774 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003775 return X86EMUL_CONTINUE;
3776}
3777
Avi Kivity2d04a052011-04-20 15:32:49 +03003778static int em_clts(struct x86_emulate_ctxt *ctxt)
3779{
3780 ulong cr0;
3781
3782 cr0 = ctxt->ops->get_cr(ctxt, 0);
3783 cr0 &= ~X86_CR0_TS;
3784 ctxt->ops->set_cr(ctxt, 0, cr0);
3785 return X86EMUL_CONTINUE;
3786}
3787
Jan Kiszkab34a8052015-03-09 20:27:43 +01003788static int em_hypercall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003789{
Nadav Amit0f54a322014-08-29 11:26:55 +03003790 int rc = ctxt->ops->fix_hypercall(ctxt);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003791
Avi Kivity26d05cc2011-04-21 12:07:59 +03003792 if (rc != X86EMUL_CONTINUE)
3793 return rc;
3794
3795 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003796 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003797 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003798 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003799 return X86EMUL_CONTINUE;
3800}
3801
Avi Kivity96051572012-06-10 17:21:18 +03003802static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3803 void (*get)(struct x86_emulate_ctxt *ctxt,
3804 struct desc_ptr *ptr))
3805{
3806 struct desc_ptr desc_ptr;
3807
Paolo Bonziniae3e61e2016-07-12 10:36:41 +02003808 if ((ctxt->ops->get_cr(ctxt, 4) & X86_CR4_UMIP) &&
3809 ctxt->ops->cpl(ctxt) > 0)
3810 return emulate_gp(ctxt, 0);
3811
Avi Kivity96051572012-06-10 17:21:18 +03003812 if (ctxt->mode == X86EMUL_MODE_PROT64)
3813 ctxt->op_bytes = 8;
3814 get(ctxt, &desc_ptr);
3815 if (ctxt->op_bytes == 2) {
3816 ctxt->op_bytes = 4;
3817 desc_ptr.address &= 0x00ffffff;
3818 }
3819 /* Disable writeback. */
3820 ctxt->dst.type = OP_NONE;
Steve Rutherford129a72a2017-01-11 18:28:29 -08003821 return segmented_write_std(ctxt, ctxt->dst.addr.mem,
3822 &desc_ptr, 2 + ctxt->op_bytes);
Avi Kivity96051572012-06-10 17:21:18 +03003823}
3824
3825static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3826{
3827 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3828}
3829
3830static int em_sidt(struct x86_emulate_ctxt *ctxt)
3831{
3832 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3833}
3834
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003835static int em_lgdt_lidt(struct x86_emulate_ctxt *ctxt, bool lgdt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003836{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003837 struct desc_ptr desc_ptr;
3838 int rc;
3839
Avi Kivity510425f2012-06-07 17:04:36 +03003840 if (ctxt->mode == X86EMUL_MODE_PROT64)
3841 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003842 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003843 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003844 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003845 if (rc != X86EMUL_CONTINUE)
3846 return rc;
Nadav Amit9a9abf62014-11-02 11:54:56 +02003847 if (ctxt->mode == X86EMUL_MODE_PROT64 &&
Yu Zhangfd8cb432017-08-24 20:27:56 +08003848 emul_is_noncanonical_address(desc_ptr.address, ctxt))
Nadav Amit9a9abf62014-11-02 11:54:56 +02003849 return emulate_gp(ctxt, 0);
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003850 if (lgdt)
3851 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3852 else
3853 ctxt->ops->set_idt(ctxt, &desc_ptr);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003854 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003855 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003856 return X86EMUL_CONTINUE;
3857}
3858
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003859static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3860{
3861 return em_lgdt_lidt(ctxt, true);
3862}
3863
Avi Kivity26d05cc2011-04-21 12:07:59 +03003864static int em_lidt(struct x86_emulate_ctxt *ctxt)
3865{
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003866 return em_lgdt_lidt(ctxt, false);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003867}
3868
3869static int em_smsw(struct x86_emulate_ctxt *ctxt)
3870{
Paolo Bonziniae3e61e2016-07-12 10:36:41 +02003871 if ((ctxt->ops->get_cr(ctxt, 4) & X86_CR4_UMIP) &&
3872 ctxt->ops->cpl(ctxt) > 0)
3873 return emulate_gp(ctxt, 0);
3874
Nadav Amit32e94d02014-06-02 18:34:11 +03003875 if (ctxt->dst.type == OP_MEM)
3876 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003877 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003878 return X86EMUL_CONTINUE;
3879}
3880
3881static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3882{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003883 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003884 | (ctxt->src.val & 0x0f));
3885 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003886 return X86EMUL_CONTINUE;
3887}
3888
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003889static int em_loop(struct x86_emulate_ctxt *ctxt)
3890{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003891 int rc = X86EMUL_CONTINUE;
3892
Paolo Bonzini01485a22014-11-19 18:25:08 +01003893 register_address_increment(ctxt, VCPU_REGS_RCX, -1);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003894 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003895 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
Nadav Amit234f3ce2014-09-18 22:39:38 +03003896 rc = jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003897
Nadav Amit234f3ce2014-09-18 22:39:38 +03003898 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003899}
3900
3901static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3902{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003903 int rc = X86EMUL_CONTINUE;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003904
Nadav Amit234f3ce2014-09-18 22:39:38 +03003905 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
3906 rc = jmp_rel(ctxt, ctxt->src.val);
3907
3908 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003909}
3910
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003911static int em_in(struct x86_emulate_ctxt *ctxt)
3912{
3913 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3914 &ctxt->dst.val))
3915 return X86EMUL_IO_NEEDED;
3916
3917 return X86EMUL_CONTINUE;
3918}
3919
3920static int em_out(struct x86_emulate_ctxt *ctxt)
3921{
3922 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3923 &ctxt->src.val, 1);
3924 /* Disable writeback. */
3925 ctxt->dst.type = OP_NONE;
3926 return X86EMUL_CONTINUE;
3927}
3928
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003929static int em_cli(struct x86_emulate_ctxt *ctxt)
3930{
3931 if (emulator_bad_iopl(ctxt))
3932 return emulate_gp(ctxt, 0);
3933
3934 ctxt->eflags &= ~X86_EFLAGS_IF;
3935 return X86EMUL_CONTINUE;
3936}
3937
3938static int em_sti(struct x86_emulate_ctxt *ctxt)
3939{
3940 if (emulator_bad_iopl(ctxt))
3941 return emulate_gp(ctxt, 0);
3942
3943 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3944 ctxt->eflags |= X86_EFLAGS_IF;
3945 return X86EMUL_CONTINUE;
3946}
3947
Avi Kivity6d6eede2012-06-07 14:11:36 +03003948static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3949{
3950 u32 eax, ebx, ecx, edx;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003951 u64 msr = 0;
3952
3953 ctxt->ops->get_msr(ctxt, MSR_MISC_FEATURES_ENABLES, &msr);
3954 if (msr & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
3955 ctxt->ops->cpl(ctxt)) {
3956 return emulate_gp(ctxt, 0);
3957 }
Avi Kivity6d6eede2012-06-07 14:11:36 +03003958
Avi Kivitydd856ef2012-08-27 23:46:17 +03003959 eax = reg_read(ctxt, VCPU_REGS_RAX);
3960 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Yu Zhange911eb32017-08-24 20:27:52 +08003961 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, true);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003962 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3963 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3964 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3965 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003966 return X86EMUL_CONTINUE;
3967}
3968
Paolo Bonzini98f73632013-10-31 11:19:42 +01003969static int em_sahf(struct x86_emulate_ctxt *ctxt)
3970{
3971 u32 flags;
3972
Nadav Amit0efb0442015-03-29 16:33:03 +03003973 flags = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
3974 X86_EFLAGS_SF;
Paolo Bonzini98f73632013-10-31 11:19:42 +01003975 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3976
3977 ctxt->eflags &= ~0xffUL;
3978 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3979 return X86EMUL_CONTINUE;
3980}
3981
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003982static int em_lahf(struct x86_emulate_ctxt *ctxt)
3983{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003984 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3985 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003986 return X86EMUL_CONTINUE;
3987}
3988
Avi Kivity92998362012-06-13 12:25:06 +03003989static int em_bswap(struct x86_emulate_ctxt *ctxt)
3990{
3991 switch (ctxt->op_bytes) {
3992#ifdef CONFIG_X86_64
3993 case 8:
3994 asm("bswap %0" : "+r"(ctxt->dst.val));
3995 break;
3996#endif
3997 default:
3998 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3999 break;
4000 }
4001 return X86EMUL_CONTINUE;
4002}
4003
Nadav Amit13e457e2014-10-13 13:04:13 +03004004static int em_clflush(struct x86_emulate_ctxt *ctxt)
4005{
4006 /* emulating clflush regardless of cpuid */
4007 return X86EMUL_CONTINUE;
4008}
4009
Nadav Amit2276b512015-01-26 09:32:24 +02004010static int em_movsxd(struct x86_emulate_ctxt *ctxt)
4011{
4012 ctxt->dst.val = (s32) ctxt->src.val;
4013 return X86EMUL_CONTINUE;
4014}
4015
Radim Krčmář283c95d2016-11-09 19:07:06 +01004016static int check_fxsr(struct x86_emulate_ctxt *ctxt)
4017{
Sean Christopherson5ae78e92019-12-17 13:32:38 -08004018 if (!ctxt->ops->guest_has_fxsr(ctxt))
Radim Krčmář283c95d2016-11-09 19:07:06 +01004019 return emulate_ud(ctxt);
4020
4021 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
4022 return emulate_nm(ctxt);
4023
4024 /*
4025 * Don't emulate a case that should never be hit, instead of working
4026 * around a lack of fxsave64/fxrstor64 on old compilers.
4027 */
4028 if (ctxt->mode >= X86EMUL_MODE_PROT64)
4029 return X86EMUL_UNHANDLEABLE;
4030
4031 return X86EMUL_CONTINUE;
4032}
4033
4034/*
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004035 * Hardware doesn't save and restore XMM 0-7 without CR4.OSFXSR, but does save
4036 * and restore MXCSR.
4037 */
4038static size_t __fxstate_size(int nregs)
4039{
4040 return offsetof(struct fxregs_state, xmm_space[0]) + nregs * 16;
4041}
4042
4043static inline size_t fxstate_size(struct x86_emulate_ctxt *ctxt)
4044{
4045 bool cr4_osfxsr;
4046 if (ctxt->mode == X86EMUL_MODE_PROT64)
4047 return __fxstate_size(16);
4048
4049 cr4_osfxsr = ctxt->ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR;
4050 return __fxstate_size(cr4_osfxsr ? 8 : 0);
4051}
4052
4053/*
Radim Krčmář283c95d2016-11-09 19:07:06 +01004054 * FXSAVE and FXRSTOR have 4 different formats depending on execution mode,
4055 * 1) 16 bit mode
4056 * 2) 32 bit mode
4057 * - like (1), but FIP and FDP (foo) are only 16 bit. At least Intel CPUs
4058 * preserve whole 32 bit values, though, so (1) and (2) are the same wrt.
4059 * save and restore
4060 * 3) 64-bit mode with REX.W prefix
4061 * - like (2), but XMM 8-15 are being saved and restored
4062 * 4) 64-bit mode without REX.W prefix
4063 * - like (3), but FIP and FDP are 64 bit
4064 *
4065 * Emulation uses (3) for (1) and (2) and preserves XMM 8-15 to reach the
4066 * desired result. (4) is not emulated.
4067 *
4068 * Note: Guest and host CPUID.(EAX=07H,ECX=0H):EBX[bit 13] (deprecate FPU CS
4069 * and FPU DS) should match.
4070 */
4071static int em_fxsave(struct x86_emulate_ctxt *ctxt)
4072{
4073 struct fxregs_state fx_state;
Radim Krčmář283c95d2016-11-09 19:07:06 +01004074 int rc;
4075
4076 rc = check_fxsr(ctxt);
4077 if (rc != X86EMUL_CONTINUE)
4078 return rc;
4079
Radim Krčmář283c95d2016-11-09 19:07:06 +01004080 rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_state));
4081
Radim Krčmář283c95d2016-11-09 19:07:06 +01004082 if (rc != X86EMUL_CONTINUE)
4083 return rc;
4084
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004085 return segmented_write_std(ctxt, ctxt->memop.addr.mem, &fx_state,
4086 fxstate_size(ctxt));
Radim Krčmář283c95d2016-11-09 19:07:06 +01004087}
4088
David Hildenbrand4d772cb2017-11-07 18:04:05 +01004089/*
4090 * FXRSTOR might restore XMM registers not provided by the guest. Fill
4091 * in the host registers (via FXSAVE) instead, so they won't be modified.
4092 * (preemption has to stay disabled until FXRSTOR).
4093 *
4094 * Use noinline to keep the stack for other functions called by callers small.
4095 */
4096static noinline int fxregs_fixup(struct fxregs_state *fx_state,
4097 const size_t used_size)
4098{
4099 struct fxregs_state fx_tmp;
4100 int rc;
4101
4102 rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_tmp));
4103 memcpy((void *)fx_state + used_size, (void *)&fx_tmp + used_size,
4104 __fxstate_size(16) - used_size);
4105
4106 return rc;
4107}
4108
Radim Krčmář283c95d2016-11-09 19:07:06 +01004109static int em_fxrstor(struct x86_emulate_ctxt *ctxt)
4110{
4111 struct fxregs_state fx_state;
4112 int rc;
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004113 size_t size;
Radim Krčmář283c95d2016-11-09 19:07:06 +01004114
4115 rc = check_fxsr(ctxt);
4116 if (rc != X86EMUL_CONTINUE)
4117 return rc;
4118
David Hildenbrand4d772cb2017-11-07 18:04:05 +01004119 size = fxstate_size(ctxt);
4120 rc = segmented_read_std(ctxt, ctxt->memop.addr.mem, &fx_state, size);
4121 if (rc != X86EMUL_CONTINUE)
4122 return rc;
4123
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004124 if (size < __fxstate_size(16)) {
David Hildenbrand4d772cb2017-11-07 18:04:05 +01004125 rc = fxregs_fixup(&fx_state, size);
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004126 if (rc != X86EMUL_CONTINUE)
4127 goto out;
4128 }
4129
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004130 if (fx_state.mxcsr >> 16) {
4131 rc = emulate_gp(ctxt, 0);
4132 goto out;
4133 }
Radim Krčmář283c95d2016-11-09 19:07:06 +01004134
4135 if (rc == X86EMUL_CONTINUE)
4136 rc = asm_safe("fxrstor %[fx]", : [fx] "m"(fx_state));
4137
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004138out:
Radim Krčmář283c95d2016-11-09 19:07:06 +01004139 return rc;
4140}
4141
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02004142static int em_xsetbv(struct x86_emulate_ctxt *ctxt)
4143{
4144 u32 eax, ecx, edx;
4145
4146 eax = reg_read(ctxt, VCPU_REGS_RAX);
4147 edx = reg_read(ctxt, VCPU_REGS_RDX);
4148 ecx = reg_read(ctxt, VCPU_REGS_RCX);
4149
4150 if (ctxt->ops->set_xcr(ctxt, ecx, ((u64)edx << 32) | eax))
4151 return emulate_gp(ctxt, 0);
4152
4153 return X86EMUL_CONTINUE;
4154}
4155
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004156static bool valid_cr(int nr)
4157{
4158 switch (nr) {
4159 case 0:
4160 case 2 ... 4:
4161 case 8:
4162 return true;
4163 default:
4164 return false;
4165 }
4166}
4167
4168static int check_cr_read(struct x86_emulate_ctxt *ctxt)
4169{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004170 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004171 return emulate_ud(ctxt);
4172
4173 return X86EMUL_CONTINUE;
4174}
4175
4176static int check_cr_write(struct x86_emulate_ctxt *ctxt)
4177{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004178 u64 new_val = ctxt->src.val64;
4179 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03004180 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004181
4182 static u64 cr_reserved_bits[] = {
4183 0xffffffff00000000ULL,
4184 0, 0, 0, /* CR3 checked later */
4185 CR4_RESERVED_BITS,
4186 0, 0, 0,
4187 CR8_RESERVED_BITS,
4188 };
4189
4190 if (!valid_cr(cr))
4191 return emulate_ud(ctxt);
4192
4193 if (new_val & cr_reserved_bits[cr])
4194 return emulate_gp(ctxt, 0);
4195
4196 switch (cr) {
4197 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03004198 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004199 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
4200 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
4201 return emulate_gp(ctxt, 0);
4202
Avi Kivity717746e2011-04-20 13:37:53 +03004203 cr4 = ctxt->ops->get_cr(ctxt, 4);
4204 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004205
4206 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
4207 !(cr4 & X86_CR4_PAE))
4208 return emulate_gp(ctxt, 0);
4209
4210 break;
4211 }
4212 case 3: {
4213 u64 rsvd = 0;
4214
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03004215 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08004216 if (efer & EFER_LMA) {
4217 u64 maxphyaddr;
Yu Zhangd6500142017-09-18 18:45:01 +08004218 u32 eax, ebx, ecx, edx;
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08004219
Yu Zhangd6500142017-09-18 18:45:01 +08004220 eax = 0x80000008;
4221 ecx = 0;
4222 if (ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx,
4223 &edx, false))
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08004224 maxphyaddr = eax & 0xff;
4225 else
4226 maxphyaddr = 36;
Wanpeng Lia780a3e2018-05-13 02:24:47 -07004227 rsvd = rsvd_bits(maxphyaddr, 63);
4228 if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PCIDE)
Junaid Shahid208320b2018-06-27 14:59:21 -07004229 rsvd &= ~X86_CR3_PCID_NOFLUSH;
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08004230 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004231
4232 if (new_val & rsvd)
4233 return emulate_gp(ctxt, 0);
4234
4235 break;
4236 }
4237 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03004238 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004239
4240 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
4241 return emulate_gp(ctxt, 0);
4242
4243 break;
4244 }
4245 }
4246
4247 return X86EMUL_CONTINUE;
4248}
4249
Joerg Roedel3b88e412011-04-04 12:39:29 +02004250static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
4251{
4252 unsigned long dr7;
4253
Avi Kivity717746e2011-04-20 13:37:53 +03004254 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02004255
4256 /* Check if DR7.Global_Enable is set */
4257 return dr7 & (1 << 13);
4258}
4259
4260static int check_dr_read(struct x86_emulate_ctxt *ctxt)
4261{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004262 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02004263 u64 cr4;
4264
4265 if (dr > 7)
4266 return emulate_ud(ctxt);
4267
Avi Kivity717746e2011-04-20 13:37:53 +03004268 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02004269 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
4270 return emulate_ud(ctxt);
4271
Nadav Amit6d2a0522014-11-02 11:54:43 +02004272 if (check_dr7_gd(ctxt)) {
4273 ulong dr6;
4274
4275 ctxt->ops->get_dr(ctxt, 6, &dr6);
Liran Alon1fc5d1942019-06-06 01:54:47 +03004276 dr6 &= ~DR_TRAP_BITS;
Nadav Amit6d2a0522014-11-02 11:54:43 +02004277 dr6 |= DR6_BD | DR6_RTM;
4278 ctxt->ops->set_dr(ctxt, 6, dr6);
Joerg Roedel3b88e412011-04-04 12:39:29 +02004279 return emulate_db(ctxt);
Nadav Amit6d2a0522014-11-02 11:54:43 +02004280 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02004281
4282 return X86EMUL_CONTINUE;
4283}
4284
4285static int check_dr_write(struct x86_emulate_ctxt *ctxt)
4286{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004287 u64 new_val = ctxt->src.val64;
4288 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02004289
4290 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
4291 return emulate_gp(ctxt, 0);
4292
4293 return check_dr_read(ctxt);
4294}
4295
Joerg Roedel01de8b02011-04-04 12:39:31 +02004296static int check_svme(struct x86_emulate_ctxt *ctxt)
4297{
Radim Krčmář92ceb762017-05-18 19:37:32 +02004298 u64 efer = 0;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004299
Avi Kivity717746e2011-04-20 13:37:53 +03004300 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02004301
4302 if (!(efer & EFER_SVME))
4303 return emulate_ud(ctxt);
4304
4305 return X86EMUL_CONTINUE;
4306}
4307
4308static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
4309{
Avi Kivitydd856ef2012-08-27 23:46:17 +03004310 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02004311
4312 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07004313 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02004314 return emulate_gp(ctxt, 0);
4315
4316 return check_svme(ctxt);
4317}
4318
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004319static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
4320{
Avi Kivity717746e2011-04-20 13:37:53 +03004321 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004322
Avi Kivity717746e2011-04-20 13:37:53 +03004323 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004324 return emulate_ud(ctxt);
4325
4326 return X86EMUL_CONTINUE;
4327}
4328
Joerg Roedel80612522011-04-04 12:39:33 +02004329static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
4330{
Avi Kivity717746e2011-04-20 13:37:53 +03004331 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03004332 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02004333
Arbel Moshe2d7921c2018-03-12 13:12:53 +02004334 /*
4335 * VMware allows access to these Pseduo-PMCs even when read via RDPMC
4336 * in Ring3 when CR4.PCE=0.
4337 */
4338 if (enable_vmware_backdoor && is_vmware_backdoor_pmc(rcx))
4339 return X86EMUL_CONTINUE;
4340
Avi Kivity717746e2011-04-20 13:37:53 +03004341 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03004342 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02004343 return emulate_gp(ctxt, 0);
4344
4345 return X86EMUL_CONTINUE;
4346}
4347
Joerg Roedelf6511932011-04-04 12:39:35 +02004348static int check_perm_in(struct x86_emulate_ctxt *ctxt)
4349{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004350 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
4351 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02004352 return emulate_gp(ctxt, 0);
4353
4354 return X86EMUL_CONTINUE;
4355}
4356
4357static int check_perm_out(struct x86_emulate_ctxt *ctxt)
4358{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004359 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
4360 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02004361 return emulate_gp(ctxt, 0);
4362
4363 return X86EMUL_CONTINUE;
4364}
4365
Avi Kivity73fba5f2010-07-29 15:11:53 +03004366#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004367#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
4368#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
4369 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03004370#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02004371#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004372#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
4373#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Nadav Amit39f062f2014-11-26 15:47:18 +02004374#define ID(_f, _i) { .flags = ((_f) | InstrDual | ModRM), .u.idual = (_i) }
Nadav Amit2276b512015-01-26 09:32:24 +02004375#define MD(_f, _m) { .flags = ((_f) | ModeDual), .u.mdual = (_m) }
Gleb Natapov045a2822012-12-20 16:57:43 +02004376#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03004377#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02004378#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004379#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004380 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004381#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004382 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
4383 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02004384#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03004385
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004386#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02004387#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004388#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02004389#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004390#define I2bvIP(_f, _e, _i, _p) \
4391 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004392
Avi Kivityfb864fb2013-01-04 16:18:54 +02004393#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
4394 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
4395 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03004396
Nadav Amit0f54a322014-08-29 11:26:55 +03004397static const struct opcode group7_rm0[] = {
4398 N,
Jan Kiszkab34a8052015-03-09 20:27:43 +01004399 I(SrcNone | Priv | EmulateOnUD, em_hypercall),
Nadav Amit0f54a322014-08-29 11:26:55 +03004400 N, N, N, N, N, N,
4401};
4402
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004403static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004404 DI(SrcNone | Priv, monitor),
4405 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004406 N, N, N, N, N, N,
4407};
4408
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02004409static const struct opcode group7_rm2[] = {
4410 N,
4411 II(ImplicitOps | Priv, em_xsetbv, xsetbv),
4412 N, N, N, N, N, N,
4413};
4414
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004415static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004416 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Jan Kiszkab34a8052015-03-09 20:27:43 +01004417 II(SrcNone | Prot | EmulateOnUD, em_hypercall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004418 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
4419 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
4420 DIP(SrcNone | Prot | Priv, stgi, check_svme),
4421 DIP(SrcNone | Prot | Priv, clgi, check_svme),
4422 DIP(SrcNone | Prot | Priv, skinit, check_svme),
4423 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004424};
Avi Kivity6230f7f2010-08-26 18:34:55 +03004425
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004426static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004427 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004428 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004429 N, N, N, N, N, N,
4430};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004431
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004432static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02004433 F(Lock, em_add),
4434 F(Lock | PageTable, em_or),
4435 F(Lock, em_adc),
4436 F(Lock, em_sbb),
4437 F(Lock | PageTable, em_and),
4438 F(Lock, em_sub),
4439 F(Lock, em_xor),
4440 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004441};
4442
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004443static const struct opcode group1A[] = {
Tom Lendacky0f89b202016-12-14 14:59:23 -05004444 I(DstMem | SrcNone | Mov | Stack | IncSP | TwoMemOp, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004445};
4446
Avi Kivity007a3b52013-01-19 19:51:51 +02004447static const struct opcode group2[] = {
4448 F(DstMem | ModRM, em_rol),
4449 F(DstMem | ModRM, em_ror),
4450 F(DstMem | ModRM, em_rcl),
4451 F(DstMem | ModRM, em_rcr),
4452 F(DstMem | ModRM, em_shl),
4453 F(DstMem | ModRM, em_shr),
4454 F(DstMem | ModRM, em_shl),
4455 F(DstMem | ModRM, em_sar),
4456};
4457
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004458static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02004459 F(DstMem | SrcImm | NoWrite, em_test),
4460 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02004461 F(DstMem | SrcNone | Lock, em_not),
4462 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02004463 F(DstXacc | Src2Mem, em_mul_ex),
4464 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004465 F(DstXacc | Src2Mem, em_div_ex),
4466 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004467};
4468
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004469static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02004470 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
4471 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004472 N, N, N, N, N, N,
4473};
4474
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004475static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02004476 F(DstMem | SrcNone | Lock, em_inc),
4477 F(DstMem | SrcNone | Lock, em_dec),
Nadav Amit58b70752014-10-24 11:35:09 +03004478 I(SrcMem | NearBranch, em_call_near_abs),
Nadav Amitacac6f892015-05-03 20:22:57 +03004479 I(SrcMemFAddr | ImplicitOps, em_call_far),
Nadav Amit58b70752014-10-24 11:35:09 +03004480 I(SrcMem | NearBranch, em_jmp_abs),
Nadav Amitf7784042014-09-18 22:39:41 +03004481 I(SrcMemFAddr | ImplicitOps, em_jmp_far),
Tom Lendacky0f89b202016-12-14 14:59:23 -05004482 I(SrcMem | Stack | TwoMemOp, em_push), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004483};
4484
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004485static const struct opcode group6[] = {
Paolo Bonzinidd307d02016-07-12 10:35:51 +02004486 II(Prot | DstMem, em_sldt, sldt),
4487 II(Prot | DstMem, em_str, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03004488 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03004489 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004490 N, N, N, N,
4491};
4492
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004493static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03004494 II(Mov | DstMem, em_sgdt, sgdt),
4495 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004496 II(SrcMem | Priv, em_lgdt, lgdt),
4497 II(SrcMem | Priv, em_lidt, lidt),
4498 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
4499 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
4500 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004501}, {
Nadav Amit0f54a322014-08-29 11:26:55 +03004502 EXT(0, group7_rm0),
Avi Kivity5ef39c72011-04-21 12:21:50 +03004503 EXT(0, group7_rm1),
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02004504 EXT(0, group7_rm2),
4505 EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004506 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
4507 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
4508 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004509} };
4510
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004511static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004512 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02004513 F(DstMem | SrcImmByte | NoWrite, em_bt),
4514 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
4515 F(DstMem | SrcImmByte | Lock, em_btr),
4516 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004517};
4518
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02004519/*
4520 * The "memory" destination is actually always a register, since we come
4521 * from the register case of group9.
4522 */
4523static const struct gprefix pfx_0f_c7_7 = {
4524 N, N, N, II(DstMem | ModRM | Op3264 | EmulateOnUD, em_rdpid, rdtscp),
4525};
4526
4527
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004528static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004529 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004530}, {
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02004531 N, N, N, N, N, N, N,
4532 GP(0, &pfx_0f_c7_7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004533} };
4534
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004535static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004536 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004537 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004538};
4539
Nadav Amit13e457e2014-10-13 13:04:13 +03004540static const struct gprefix pfx_0f_ae_7 = {
Nadav Amit3f6f1482014-10-13 13:04:14 +03004541 I(SrcMem | ByteOp, em_clflush), N, N, N,
Nadav Amit13e457e2014-10-13 13:04:13 +03004542};
4543
4544static const struct group_dual group15 = { {
Radim Krčmář283c95d2016-11-09 19:07:06 +01004545 I(ModRM | Aligned16, em_fxsave),
4546 I(ModRM | Aligned16, em_fxrstor),
4547 N, N, N, N, N, GP(0, &pfx_0f_ae_7),
Nadav Amit13e457e2014-10-13 13:04:13 +03004548}, {
4549 N, N, N, N, N, N, N, N,
4550} };
4551
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004552static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03004553 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02004554};
4555
Nadav Amit39f062f2014-11-26 15:47:18 +02004556static const struct instr_dual instr_dual_0f_2b = {
4557 I(0, em_mov), N
4558};
4559
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004560static const struct gprefix pfx_0f_2b = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004561 ID(0, &instr_dual_0f_2b), ID(0, &instr_dual_0f_2b), N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03004562};
4563
Stefan Fritsch29916962018-04-01 17:54:44 +02004564static const struct gprefix pfx_0f_10_0f_11 = {
4565 I(Unaligned, em_mov), I(Unaligned, em_mov), N, N,
4566};
4567
Igor Mammedov27ce8252014-03-15 21:01:59 +01004568static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01004569 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004570};
4571
Alex Williamson0a370272014-07-11 11:56:31 -06004572static const struct gprefix pfx_0f_e7 = {
4573 N, I(Sse, em_mov), N, N,
4574};
4575
Gleb Natapov045a2822012-12-20 16:57:43 +02004576static const struct escape escape_d9 = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02004577 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstcw),
Gleb Natapov045a2822012-12-20 16:57:43 +02004578}, {
4579 /* 0xC0 - 0xC7 */
4580 N, N, N, N, N, N, N, N,
4581 /* 0xC8 - 0xCF */
4582 N, N, N, N, N, N, N, N,
4583 /* 0xD0 - 0xC7 */
4584 N, N, N, N, N, N, N, N,
4585 /* 0xD8 - 0xDF */
4586 N, N, N, N, N, N, N, N,
4587 /* 0xE0 - 0xE7 */
4588 N, N, N, N, N, N, N, N,
4589 /* 0xE8 - 0xEF */
4590 N, N, N, N, N, N, N, N,
4591 /* 0xF0 - 0xF7 */
4592 N, N, N, N, N, N, N, N,
4593 /* 0xF8 - 0xFF */
4594 N, N, N, N, N, N, N, N,
4595} };
4596
4597static const struct escape escape_db = { {
4598 N, N, N, N, N, N, N, N,
4599}, {
4600 /* 0xC0 - 0xC7 */
4601 N, N, N, N, N, N, N, N,
4602 /* 0xC8 - 0xCF */
4603 N, N, N, N, N, N, N, N,
4604 /* 0xD0 - 0xC7 */
4605 N, N, N, N, N, N, N, N,
4606 /* 0xD8 - 0xDF */
4607 N, N, N, N, N, N, N, N,
4608 /* 0xE0 - 0xE7 */
4609 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
4610 /* 0xE8 - 0xEF */
4611 N, N, N, N, N, N, N, N,
4612 /* 0xF0 - 0xF7 */
4613 N, N, N, N, N, N, N, N,
4614 /* 0xF8 - 0xFF */
4615 N, N, N, N, N, N, N, N,
4616} };
4617
4618static const struct escape escape_dd = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02004619 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstsw),
Gleb Natapov045a2822012-12-20 16:57:43 +02004620}, {
4621 /* 0xC0 - 0xC7 */
4622 N, N, N, N, N, N, N, N,
4623 /* 0xC8 - 0xCF */
4624 N, N, N, N, N, N, N, N,
4625 /* 0xD0 - 0xC7 */
4626 N, N, N, N, N, N, N, N,
4627 /* 0xD8 - 0xDF */
4628 N, N, N, N, N, N, N, N,
4629 /* 0xE0 - 0xE7 */
4630 N, N, N, N, N, N, N, N,
4631 /* 0xE8 - 0xEF */
4632 N, N, N, N, N, N, N, N,
4633 /* 0xF0 - 0xF7 */
4634 N, N, N, N, N, N, N, N,
4635 /* 0xF8 - 0xFF */
4636 N, N, N, N, N, N, N, N,
4637} };
4638
Nadav Amit39f062f2014-11-26 15:47:18 +02004639static const struct instr_dual instr_dual_0f_c3 = {
4640 I(DstMem | SrcReg | ModRM | No16 | Mov, em_mov), N
4641};
4642
Nadav Amit2276b512015-01-26 09:32:24 +02004643static const struct mode_dual mode_dual_63 = {
4644 N, I(DstReg | SrcMem32 | ModRM | Mov, em_movsxd)
4645};
4646
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004647static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004648 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004649 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004650 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
4651 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004652 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004653 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004654 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
4655 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004656 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004657 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004658 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
4659 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004660 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004661 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004662 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
4663 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004664 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004665 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004666 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004667 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004668 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004669 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004670 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004671 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004672 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02004673 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004674 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03004675 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004676 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09004677 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004678 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09004679 I(ImplicitOps | Stack | No64, em_pusha),
4680 I(ImplicitOps | Stack | No64, em_popa),
Nadav Amit2276b512015-01-26 09:32:24 +02004681 N, MD(ModRM, &mode_dual_63),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004682 N, N, N, N,
4683 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03004684 I(SrcImm | Mov | Stack, em_push),
4685 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03004686 I(SrcImmByte | Mov | Stack, em_push),
4687 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004688 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09004689 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03004690 /* 0x70 - 0x7F */
Nadav Amit58b70752014-10-24 11:35:09 +03004691 X16(D(SrcImmByte | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004692 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004693 G(ByteOp | DstMem | SrcImm, group1),
4694 G(DstMem | SrcImm, group1),
4695 G(ByteOp | DstMem | SrcImm | No64, group1),
4696 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02004697 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004698 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004699 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004700 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004701 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004702 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09004703 D(ModRM | SrcMem | NoAccess | DstReg),
4704 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
4705 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004706 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004707 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004708 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03004709 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08004710 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09004711 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01004712 II(ImplicitOps | Stack, em_popf, popf),
4713 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004714 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004715 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004716 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Tom Lendacky0f89b202016-12-14 14:59:23 -05004717 I2bv(SrcSI | DstDI | Mov | String | TwoMemOp, em_mov),
4718 F2bv(SrcSI | DstDI | String | NoWrite | TwoMemOp, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004719 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004720 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004721 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
4722 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004723 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004724 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004725 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004726 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02004727 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004728 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004729 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Nadav Amit58b70752014-10-24 11:35:09 +03004730 I(ImplicitOps | NearBranch | SrcImmU16, em_ret_near_imm),
4731 I(ImplicitOps | NearBranch, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03004732 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
4733 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004734 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004735 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03004736 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Nadav Amit16794aa2015-01-26 09:32:22 +02004737 I(ImplicitOps | SrcImmU16, em_ret_far_imm),
4738 I(ImplicitOps, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02004739 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004740 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004741 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004742 G(Src2One | ByteOp, group2), G(Src2One, group2),
4743 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02004744 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02004745 I(DstAcc | SrcImmUByte | No64, em_aad),
4746 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004747 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004748 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02004749 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004750 /* 0xE0 - 0xE7 */
Nadav Amit58b70752014-10-24 11:35:09 +03004751 X3(I(SrcImmByte | NearBranch, em_loop)),
4752 I(SrcImmByte | NearBranch, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004753 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
4754 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004755 /* 0xE8 - 0xEF */
Nadav Amit58b70752014-10-24 11:35:09 +03004756 I(SrcImm | NearBranch, em_call), D(SrcImm | ImplicitOps | NearBranch),
4757 I(SrcImmFAddr | No64, em_jmp_far),
4758 D(SrcImmByte | ImplicitOps | NearBranch),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004759 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
4760 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004761 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004762 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004763 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4764 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004765 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09004766 D(ImplicitOps), D(ImplicitOps),
4767 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004768 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4769};
4770
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004771static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004772 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004773 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02004774 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004775 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004776 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004777 N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004778 /* 0x10 - 0x1F */
Stefan Fritsch29916962018-04-01 17:54:44 +02004779 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_10_0f_11),
4780 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_10_0f_11),
4781 N, N, N, N, N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004782 D(ImplicitOps | ModRM | SrcMem | NoAccess),
4783 N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004784 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03004785 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
4786 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
4787 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
4788 check_cr_write),
4789 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
4790 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004791 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004792 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
4793 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004794 N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b),
Avi Kivity3e114eb2012-04-09 18:40:01 +03004795 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004796 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004797 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004798 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004799 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004800 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02004801 I(ImplicitOps | EmulateOnUD, em_sysenter),
4802 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004803 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004804 N, N, N, N, N, N, N, N,
4805 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03004806 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004807 /* 0x50 - 0x5F */
4808 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4809 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004810 N, N, N, N,
4811 N, N, N, N,
4812 N, N, N, N,
4813 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004814 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004815 N, N, N, N,
4816 N, N, N, N,
4817 N, N, N, N,
4818 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004819 /* 0x80 - 0x8F */
Nadav Amit58b70752014-10-24 11:35:09 +03004820 X16(D(SrcImm | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004821 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004822 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004823 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004824 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004825 II(ImplicitOps, em_cpuid, cpuid),
4826 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004827 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4828 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004829 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004830 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Paolo Bonzini89651a32015-11-03 13:43:05 +01004831 II(EmulateOnUD | ImplicitOps, em_rsm, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004832 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004833 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4834 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Nadav Amit13e457e2014-10-13 13:04:13 +03004835 GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004836 /* 0xB0 - 0xB7 */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02004837 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable | SrcWrite, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004838 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004839 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004840 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4841 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004842 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004843 /* 0xB8 - 0xBF */
4844 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004845 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004846 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Nadav Amit900efe22015-03-30 15:39:21 +03004847 I(DstReg | SrcMem | ModRM, em_bsf_c),
4848 I(DstReg | SrcMem | ModRM, em_bsr_c),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004849 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004850 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02004851 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Nadav Amit39f062f2014-11-26 15:47:18 +02004852 N, ID(0, &instr_dual_0f_c3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004853 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004854 /* 0xC8 - 0xCF */
4855 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004856 /* 0xD0 - 0xDF */
4857 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4858 /* 0xE0 - 0xEF */
Alex Williamson0a370272014-07-11 11:56:31 -06004859 N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
4860 N, N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004861 /* 0xF0 - 0xFF */
4862 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4863};
4864
Nadav Amit39f062f2014-11-26 15:47:18 +02004865static const struct instr_dual instr_dual_0f_38_f0 = {
4866 I(DstReg | SrcMem | Mov, em_movbe), N
4867};
4868
4869static const struct instr_dual instr_dual_0f_38_f1 = {
4870 I(DstMem | SrcReg | Mov, em_movbe), N
4871};
4872
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004873static const struct gprefix three_byte_0f_38_f0 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004874 ID(0, &instr_dual_0f_38_f0), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004875};
4876
4877static const struct gprefix three_byte_0f_38_f1 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004878 ID(0, &instr_dual_0f_38_f1), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004879};
4880
4881/*
4882 * Insns below are selected by the prefix which indexed by the third opcode
4883 * byte.
4884 */
4885static const struct opcode opcode_map_0f_38[256] = {
4886 /* 0x00 - 0x7f */
4887 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004888 /* 0x80 - 0xef */
4889 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4890 /* 0xf0 - 0xf1 */
Nadav Amit53bb4f72014-12-07 11:49:42 +02004891 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0),
4892 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004893 /* 0xf2 - 0xff */
4894 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004895};
4896
Avi Kivity73fba5f2010-07-29 15:11:53 +03004897#undef D
4898#undef N
4899#undef G
4900#undef GD
4901#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004902#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004903#undef EXT
Nadav Amit2276b512015-01-26 09:32:24 +02004904#undef MD
Nadav Amit2b42fce2015-01-26 09:32:25 +02004905#undef ID
Avi Kivity73fba5f2010-07-29 15:11:53 +03004906
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004907#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004908#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004909#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004910#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004911#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004912
Avi Kivity9dac77f2011-06-01 15:34:25 +03004913static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004914{
4915 unsigned size;
4916
Avi Kivity9dac77f2011-06-01 15:34:25 +03004917 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004918 if (size == 8)
4919 size = 4;
4920 return size;
4921}
4922
4923static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4924 unsigned size, bool sign_extension)
4925{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004926 int rc = X86EMUL_CONTINUE;
4927
4928 op->type = OP_IMM;
4929 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004930 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004931 /* NB. Immediates are sign-extended as necessary. */
4932 switch (op->bytes) {
4933 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004934 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004935 break;
4936 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004937 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004938 break;
4939 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004940 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004941 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004942 case 8:
4943 op->val = insn_fetch(s64, ctxt);
4944 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004945 }
4946 if (!sign_extension) {
4947 switch (op->bytes) {
4948 case 1:
4949 op->val &= 0xff;
4950 break;
4951 case 2:
4952 op->val &= 0xffff;
4953 break;
4954 case 4:
4955 op->val &= 0xffffffff;
4956 break;
4957 }
4958 }
4959done:
4960 return rc;
4961}
4962
Avi Kivitya99455492011-09-13 10:45:41 +03004963static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4964 unsigned d)
4965{
4966 int rc = X86EMUL_CONTINUE;
4967
4968 switch (d) {
4969 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004970 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03004971 break;
4972 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004973 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03004974 break;
4975 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004976 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004977 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03004978 *op = ctxt->memop;
4979 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004980 if (ctxt->d & BitOp)
Avi Kivitya99455492011-09-13 10:45:41 +03004981 fetch_bit_operand(ctxt);
4982 op->orig_val = op->val;
4983 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004984 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004985 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004986 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03004987 case OpAcc:
4988 op->type = OP_REG;
4989 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004990 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03004991 fetch_register_operand(op);
4992 op->orig_val = op->val;
4993 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004994 case OpAccLo:
4995 op->type = OP_REG;
4996 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4997 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4998 fetch_register_operand(op);
4999 op->orig_val = op->val;
5000 break;
5001 case OpAccHi:
5002 if (ctxt->d & ByteOp) {
5003 op->type = OP_NONE;
5004 break;
5005 }
5006 op->type = OP_REG;
5007 op->bytes = ctxt->op_bytes;
5008 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
5009 fetch_register_operand(op);
5010 op->orig_val = op->val;
5011 break;
Avi Kivitya99455492011-09-13 10:45:41 +03005012 case OpDI:
5013 op->type = OP_MEM;
5014 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
5015 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01005016 register_address(ctxt, VCPU_REGS_RDI);
Avi Kivitya99455492011-09-13 10:45:41 +03005017 op->addr.mem.seg = VCPU_SREG_ES;
5018 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005019 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03005020 break;
5021 case OpDX:
5022 op->type = OP_REG;
5023 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005024 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03005025 fetch_register_operand(op);
5026 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03005027 case OpCL:
Nadav Amitd29b9d72014-11-02 11:54:47 +02005028 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03005029 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005030 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03005031 break;
5032 case OpImmByte:
5033 rc = decode_imm(ctxt, op, 1, true);
5034 break;
5035 case OpOne:
Nadav Amitd29b9d72014-11-02 11:54:47 +02005036 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03005037 op->bytes = 1;
5038 op->val = 1;
5039 break;
5040 case OpImm:
5041 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
5042 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02005043 case OpImm64:
5044 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
5045 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02005046 case OpMem8:
5047 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03005048 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02005049 ctxt->memop.addr.reg = decode_register(ctxt,
5050 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03005051 fetch_register_operand(&ctxt->memop);
5052 }
Avi Kivity28867ce2012-01-16 15:08:44 +02005053 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03005054 case OpMem16:
5055 ctxt->memop.bytes = 2;
5056 goto mem_common;
5057 case OpMem32:
5058 ctxt->memop.bytes = 4;
5059 goto mem_common;
5060 case OpImmU16:
5061 rc = decode_imm(ctxt, op, 2, false);
5062 break;
5063 case OpImmU:
5064 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
5065 break;
5066 case OpSI:
5067 op->type = OP_MEM;
5068 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
5069 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01005070 register_address(ctxt, VCPU_REGS_RSI);
Bandan Das573e80f2014-04-16 12:46:13 -04005071 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03005072 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005073 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03005074 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02005075 case OpXLat:
5076 op->type = OP_MEM;
5077 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
5078 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01005079 address_mask(ctxt,
Paolo Bonzini7fa57952013-05-09 11:32:50 +02005080 reg_read(ctxt, VCPU_REGS_RBX) +
5081 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04005082 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02005083 op->val = 0;
5084 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03005085 case OpImmFAddr:
5086 op->type = OP_IMM;
5087 op->addr.mem.ea = ctxt->_eip;
5088 op->bytes = ctxt->op_bytes + 2;
5089 insn_fetch_arr(op->valptr, op->bytes, ctxt);
5090 break;
5091 case OpMemFAddr:
5092 ctxt->memop.bytes = ctxt->op_bytes + 2;
5093 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03005094 case OpES:
Nadav Amitd29b9d72014-11-02 11:54:47 +02005095 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03005096 op->val = VCPU_SREG_ES;
5097 break;
5098 case OpCS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02005099 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03005100 op->val = VCPU_SREG_CS;
5101 break;
5102 case OpSS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02005103 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03005104 op->val = VCPU_SREG_SS;
5105 break;
5106 case OpDS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02005107 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03005108 op->val = VCPU_SREG_DS;
5109 break;
5110 case OpFS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02005111 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03005112 op->val = VCPU_SREG_FS;
5113 break;
5114 case OpGS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02005115 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03005116 op->val = VCPU_SREG_GS;
5117 break;
Avi Kivitya99455492011-09-13 10:45:41 +03005118 case OpImplicit:
5119 /* Special instructions do their own operand decoding. */
5120 default:
5121 op->type = OP_NONE; /* Disable writeback. */
5122 break;
5123 }
5124
5125done:
5126 return rc;
5127}
5128
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09005129int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005130{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005131 int rc = X86EMUL_CONTINUE;
5132 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03005133 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005134 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04005135 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03005136 struct opcode opcode;
Wanpeng Li3853be22017-11-05 16:54:47 -08005137 u16 dummy;
5138 struct desc_struct desc;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005139
Avi Kivityf09ed832011-09-13 10:45:40 +03005140 ctxt->memop.type = OP_NONE;
5141 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005142 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02005143 ctxt->fetch.ptr = ctxt->fetch.data;
5144 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02005145 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01005146 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005147 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02005148 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02005149 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02005150 if (rc != X86EMUL_CONTINUE)
Sean Christopherson016cd752019-08-15 09:20:32 -07005151 goto done;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02005152 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005153
5154 switch (mode) {
5155 case X86EMUL_MODE_REAL:
5156 case X86EMUL_MODE_VM86:
Wanpeng Li3853be22017-11-05 16:54:47 -08005157 def_op_bytes = def_ad_bytes = 2;
5158 ctxt->ops->get_segment(ctxt, &dummy, &desc, NULL, VCPU_SREG_CS);
5159 if (desc.d)
5160 def_op_bytes = def_ad_bytes = 4;
5161 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005162 case X86EMUL_MODE_PROT16:
5163 def_op_bytes = def_ad_bytes = 2;
5164 break;
5165 case X86EMUL_MODE_PROT32:
5166 def_op_bytes = def_ad_bytes = 4;
5167 break;
5168#ifdef CONFIG_X86_64
5169 case X86EMUL_MODE_PROT64:
5170 def_op_bytes = 4;
5171 def_ad_bytes = 8;
5172 break;
5173#endif
5174 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005175 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005176 }
5177
Avi Kivity9dac77f2011-06-01 15:34:25 +03005178 ctxt->op_bytes = def_op_bytes;
5179 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005180
5181 /* Legacy prefixes. */
5182 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09005183 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005184 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005185 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005186 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005187 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005188 break;
5189 case 0x67: /* address-size override */
5190 if (mode == X86EMUL_MODE_PROT64)
5191 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005192 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005193 else
5194 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005195 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005196 break;
5197 case 0x26: /* ES override */
5198 case 0x2e: /* CS override */
5199 case 0x36: /* SS override */
5200 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04005201 has_seg_override = true;
5202 ctxt->seg_override = (ctxt->b >> 3) & 3;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005203 break;
5204 case 0x64: /* FS override */
5205 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04005206 has_seg_override = true;
5207 ctxt->seg_override = ctxt->b & 7;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005208 break;
5209 case 0x40 ... 0x4f: /* REX */
5210 if (mode != X86EMUL_MODE_PROT64)
5211 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005212 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005213 continue;
5214 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005215 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005216 break;
5217 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005218 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005219 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005220 break;
5221 default:
5222 goto done_prefixes;
5223 }
5224
5225 /* Any legacy prefix after a REX prefix nullifies its effect. */
5226
Avi Kivity9dac77f2011-06-01 15:34:25 +03005227 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005228 }
5229
5230done_prefixes:
5231
5232 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005233 if (ctxt->rex_prefix & 8)
5234 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005235
5236 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005237 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08005238 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005239 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02005240 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09005241 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03005242 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005243
5244 /* 0F_38 opcode map */
5245 if (ctxt->b == 0x38) {
5246 ctxt->opcode_len = 3;
5247 ctxt->b = insn_fetch(u8, ctxt);
5248 opcode = opcode_map_0f_38[ctxt->b];
5249 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005250 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03005251 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005252
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09005253 if (ctxt->d & ModRM)
5254 ctxt->modrm = insn_fetch(u8, ctxt);
5255
Nadav Amit7fe864d2014-06-02 18:34:03 +03005256 /* vex-prefix instructions are not implemented */
5257 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
Nadav Amitd14cb5d2014-11-02 11:54:58 +02005258 (mode == X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) == 0xc0)) {
Nadav Amit7fe864d2014-06-02 18:34:03 +03005259 ctxt->d = NotImpl;
5260 }
5261
Avi Kivity9dac77f2011-06-01 15:34:25 +03005262 while (ctxt->d & GroupMask) {
5263 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03005264 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005265 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03005266 opcode = opcode.u.group[goffset];
5267 break;
5268 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005269 goffset = (ctxt->modrm >> 3) & 7;
5270 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03005271 opcode = opcode.u.gdual->mod3[goffset];
5272 else
5273 opcode = opcode.u.gdual->mod012[goffset];
5274 break;
5275 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005276 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02005277 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03005278 break;
5279 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005280 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005281 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005282 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03005283 switch (simd_prefix) {
5284 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
5285 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
5286 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
5287 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
5288 }
5289 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02005290 case Escape:
Marios Pomonis3c9053a22019-12-11 12:47:41 -08005291 if (ctxt->modrm > 0xbf) {
5292 size_t size = ARRAY_SIZE(opcode.u.esc->high);
5293 u32 index = array_index_nospec(
5294 ctxt->modrm - 0xc0, size);
5295
5296 opcode = opcode.u.esc->high[index];
5297 } else {
Gleb Natapov045a2822012-12-20 16:57:43 +02005298 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
Marios Pomonis3c9053a22019-12-11 12:47:41 -08005299 }
Gleb Natapov045a2822012-12-20 16:57:43 +02005300 break;
Nadav Amit39f062f2014-11-26 15:47:18 +02005301 case InstrDual:
5302 if ((ctxt->modrm >> 6) == 3)
5303 opcode = opcode.u.idual->mod3;
5304 else
5305 opcode = opcode.u.idual->mod012;
5306 break;
Nadav Amit2276b512015-01-26 09:32:24 +02005307 case ModeDual:
5308 if (ctxt->mode == X86EMUL_MODE_PROT64)
5309 opcode = opcode.u.mdual->mode64;
5310 else
5311 opcode = opcode.u.mdual->mode32;
5312 break;
Avi Kivity46561642011-04-24 14:09:59 +03005313 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005314 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005315 }
Avi Kivity46561642011-04-24 14:09:59 +03005316
Avi Kivityb1ea50b2011-09-13 10:45:42 +03005317 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005318 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005319 }
5320
Paolo Bonzinie24186e2014-03-27 12:00:57 +01005321 /* Unrecognised? */
5322 if (ctxt->d == 0)
5323 return EMULATION_FAILED;
5324
Avi Kivity9dac77f2011-06-01 15:34:25 +03005325 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005326
Nadav Amit3a6095a2014-08-13 16:50:13 +03005327 if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD)))
5328 return EMULATION_FAILED;
5329
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005330 if (unlikely(ctxt->d &
Nadav Amited9aad22014-11-02 11:55:00 +02005331 (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch|
5332 No16))) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005333 /*
5334 * These are copied unconditionally here, and checked unconditionally
5335 * in x86_emulate_insn.
5336 */
5337 ctxt->check_perm = opcode.check_perm;
5338 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005339
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005340 if (ctxt->d & NotImpl)
5341 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02005342
Nadav Amit58b70752014-10-24 11:35:09 +03005343 if (mode == X86EMUL_MODE_PROT64) {
5344 if (ctxt->op_bytes == 4 && (ctxt->d & Stack))
5345 ctxt->op_bytes = 8;
5346 else if (ctxt->d & NearBranch)
5347 ctxt->op_bytes = 8;
5348 }
Avi Kivity7f9b4b72010-08-01 14:46:54 +03005349
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005350 if (ctxt->d & Op3264) {
5351 if (mode == X86EMUL_MODE_PROT64)
5352 ctxt->op_bytes = 8;
5353 else
5354 ctxt->op_bytes = 4;
5355 }
5356
Nadav Amited9aad22014-11-02 11:55:00 +02005357 if ((ctxt->d & No16) && ctxt->op_bytes == 2)
5358 ctxt->op_bytes = 4;
5359
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005360 if (ctxt->d & Sse)
5361 ctxt->op_bytes = 16;
5362 else if (ctxt->d & Mmx)
5363 ctxt->op_bytes = 8;
5364 }
Avi Kivity1253791d2011-03-29 11:41:27 +02005365
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005366 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005367 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03005368 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04005369 if (!has_seg_override) {
5370 has_seg_override = true;
5371 ctxt->seg_override = ctxt->modrm_seg;
5372 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03005373 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03005374 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005375 if (rc != X86EMUL_CONTINUE)
5376 goto done;
5377
Bandan Das573e80f2014-04-16 12:46:13 -04005378 if (!has_seg_override)
5379 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005380
Bandan Das573e80f2014-04-16 12:46:13 -04005381 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005382
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005383 /*
5384 * Decode and fetch the source operand: register, memory
5385 * or immediate.
5386 */
Avi Kivity0fe59122011-09-13 10:45:47 +03005387 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03005388 if (rc != X86EMUL_CONTINUE)
5389 goto done;
5390
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005391 /*
5392 * Decode and fetch the second source operand: register, memory
5393 * or immediate.
5394 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03005395 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03005396 if (rc != X86EMUL_CONTINUE)
5397 goto done;
5398
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005399 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03005400 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005401
Owen Hofmannd9092f52016-10-27 11:25:52 -07005402 if (ctxt->rip_relative && likely(ctxt->memopp))
Nadav Amit1c1c35a2014-11-19 17:43:09 +02005403 ctxt->memopp->addr.mem.ea = address_mask(ctxt,
5404 ctxt->memopp->addr.mem.ea + ctxt->_eip);
Avi Kivitycb16c342011-06-19 19:21:11 +03005405
Paolo Bonzinia430c912014-10-23 14:54:14 +02005406done:
Jan Dakinevichc8848ce2019-08-27 13:07:08 +00005407 if (rc == X86EMUL_PROPAGATE_FAULT)
5408 ctxt->have_exception = true;
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005409 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005410}
5411
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005412bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
5413{
5414 return ctxt->d & PageTable;
5415}
5416
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005417static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
5418{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005419 /* The second termination condition only applies for REPE
5420 * and REPNE. Test if the repeat string operation prefix is
5421 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
5422 * corresponding termination condition according to:
5423 * - if REPE/REPZ and ZF = 0 then done
5424 * - if REPNE/REPNZ and ZF = 1 then done
5425 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005426 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
5427 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
5428 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03005429 ((ctxt->eflags & X86_EFLAGS_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03005430 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03005431 ((ctxt->eflags & X86_EFLAGS_ZF) == X86_EFLAGS_ZF))))
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005432 return true;
5433
5434 return false;
5435}
5436
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005437static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
5438{
Radim Krčmářaabba3c2016-11-08 20:54:18 +01005439 int rc;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005440
Radim Krčmářaabba3c2016-11-08 20:54:18 +01005441 rc = asm_safe("fwait");
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005442
Radim Krčmářaabba3c2016-11-08 20:54:18 +01005443 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005444 return emulate_exception(ctxt, MF_VECTOR, 0, false);
5445
5446 return X86EMUL_CONTINUE;
5447}
5448
5449static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
5450 struct operand *op)
5451{
5452 if (op->type == OP_MM)
5453 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
5454}
5455
Avi Kivitye28bbd42013-01-04 16:18:48 +02005456static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
5457{
5458 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005459
Avi Kivityb9fa4092013-02-09 11:31:48 +02005460 if (!(ctxt->d & ByteOp))
5461 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005462
Peter Zijlstra1a29b5b2018-01-25 10:58:13 +01005463 asm("push %[flags]; popf; " CALL_NOSPEC " ; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005464 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
Peter Zijlstra1a29b5b2018-01-25 10:58:13 +01005465 [thunk_target]"+S"(fop), ASM_CALL_CONSTRAINT
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005466 : "c"(ctxt->src2.val));
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005467
Avi Kivitye28bbd42013-01-04 16:18:48 +02005468 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005469 if (!fop) /* exception is returned in fop variable */
5470 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02005471 return X86EMUL_CONTINUE;
5472}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005473
Bandan Das14985072014-04-16 12:46:09 -04005474void init_decode_cache(struct x86_emulate_ctxt *ctxt)
5475{
Bandan Das573e80f2014-04-16 12:46:13 -04005476 memset(&ctxt->rip_relative, 0,
5477 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04005478
Bandan Das14985072014-04-16 12:46:09 -04005479 ctxt->io_read.pos = 0;
5480 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04005481 ctxt->mem_read.end = 0;
5482}
5483
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09005484int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005485{
Mathias Krause0225fb52012-08-30 01:30:16 +02005486 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09005487 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005488 int saved_dst_type = ctxt->dst.type;
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005489 unsigned emul_flags;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005490
Avi Kivity9dac77f2011-06-01 15:34:25 +03005491 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04005492
Gleb Natapovd380a5e2010-02-10 14:21:36 +02005493 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005494 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02005495 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02005496 goto done;
5497 }
5498
Avi Kivity9dac77f2011-06-01 15:34:25 +03005499 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02005500 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03005501 goto done;
5502 }
5503
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005504 emul_flags = ctxt->ops->get_hflags(ctxt);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005505 if (unlikely(ctxt->d &
5506 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
5507 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
5508 (ctxt->d & Undefined)) {
5509 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005510 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005511 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005512
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005513 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
5514 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
5515 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02005516 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005517 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02005518
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005519 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
5520 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02005521 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005522 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02005523
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005524 if (ctxt->d & Mmx) {
5525 rc = flush_pending_x87_faults(ctxt);
5526 if (rc != X86EMUL_CONTINUE)
5527 goto done;
5528 /*
5529 * Now that we know the fpu is exception safe, we can fetch
5530 * operands from it.
5531 */
5532 fetch_possible_mmx_operand(ctxt, &ctxt->src);
5533 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
5534 if (!(ctxt->d & Mov))
5535 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
5536 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02005537
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005538 if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && ctxt->intercept) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005539 rc = emulator_check_intercept(ctxt, ctxt->intercept,
5540 X86_ICPT_PRE_EXCEPT);
5541 if (rc != X86EMUL_CONTINUE)
5542 goto done;
5543 }
5544
Nadav Amit64a38292014-12-10 11:19:04 +02005545 /* Instruction can only be executed in protected mode */
5546 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
5547 rc = emulate_ud(ctxt);
5548 goto done;
5549 }
5550
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005551 /* Privileged instruction can be executed only in CPL=0 */
5552 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Nadav Amit68efa762014-06-18 17:19:35 +03005553 if (ctxt->d & PrivUD)
5554 rc = emulate_ud(ctxt);
5555 else
5556 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02005557 goto done;
5558 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005559
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005560 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04005561 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005562 rc = ctxt->check_perm(ctxt);
5563 if (rc != X86EMUL_CONTINUE)
5564 goto done;
5565 }
5566
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005567 if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005568 rc = emulator_check_intercept(ctxt, ctxt->intercept,
5569 X86_ICPT_POST_EXCEPT);
5570 if (rc != X86EMUL_CONTINUE)
5571 goto done;
5572 }
5573
5574 if (ctxt->rep_prefix && (ctxt->d & String)) {
5575 /* All REP prefixes have the same first termination condition */
5576 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Nadav Amit428e3d02015-04-28 13:06:01 +03005577 string_registers_quirk(ctxt);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005578 ctxt->eip = ctxt->_eip;
Nadav Amit0efb0442015-03-29 16:33:03 +03005579 ctxt->eflags &= ~X86_EFLAGS_RF;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005580 goto done;
5581 }
5582 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02005583 }
5584
Avi Kivity9dac77f2011-06-01 15:34:25 +03005585 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
5586 rc = segmented_read(ctxt, ctxt->src.addr.mem,
5587 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09005588 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005589 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005590 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005591 }
5592
Avi Kivity9dac77f2011-06-01 15:34:25 +03005593 if (ctxt->src2.type == OP_MEM) {
5594 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
5595 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02005596 if (rc != X86EMUL_CONTINUE)
5597 goto done;
5598 }
5599
Avi Kivity9dac77f2011-06-01 15:34:25 +03005600 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005601 goto special_insn;
5602
5603
Avi Kivity9dac77f2011-06-01 15:34:25 +03005604 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02005605 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005606 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
5607 &ctxt->dst.val, ctxt->dst.bytes);
Nadav Amitc205fb72014-12-25 02:52:16 +02005608 if (rc != X86EMUL_CONTINUE) {
Paolo Bonzinid44e1212015-02-09 10:02:05 +01005609 if (!(ctxt->d & NoWrite) &&
5610 rc == X86EMUL_PROPAGATE_FAULT &&
Nadav Amitc205fb72014-12-25 02:52:16 +02005611 ctxt->exception.vector == PF_VECTOR)
5612 ctxt->exception.error_code |= PFERR_WRITE_MASK;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02005613 goto done;
Nadav Amitc205fb72014-12-25 02:52:16 +02005614 }
Avi Kivity038e51d2007-01-22 20:40:40 -08005615 }
Paolo Bonzini4ff6f8e2015-02-12 17:04:47 +01005616 /* Copy full 64-bit value for CMPXCHG8B. */
5617 ctxt->dst.orig_val64 = ctxt->dst.val64;
Avi Kivity038e51d2007-01-22 20:40:40 -08005618
Avi Kivity018a98d2007-11-27 19:30:56 +02005619special_insn:
5620
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005621 if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03005622 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005623 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02005624 if (rc != X86EMUL_CONTINUE)
5625 goto done;
5626 }
5627
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03005628 if (ctxt->rep_prefix && (ctxt->d & String))
Nadav Amit0efb0442015-03-29 16:33:03 +03005629 ctxt->eflags |= X86_EFLAGS_RF;
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03005630 else
Nadav Amit0efb0442015-03-29 16:33:03 +03005631 ctxt->eflags &= ~X86_EFLAGS_RF;
Nadav Amit4467c3f2014-07-21 14:37:29 +03005632
Avi Kivity9dac77f2011-06-01 15:34:25 +03005633 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02005634 if (ctxt->d & Fastop) {
5635 void (*fop)(struct fastop *) = (void *)ctxt->execute;
5636 rc = fastop(ctxt, fop);
5637 if (rc != X86EMUL_CONTINUE)
5638 goto done;
5639 goto writeback;
5640 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03005641 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03005642 if (rc != X86EMUL_CONTINUE)
5643 goto done;
5644 goto writeback;
5645 }
5646
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02005647 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005648 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005649 else if (ctxt->opcode_len == 3)
5650 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005651
Avi Kivity9dac77f2011-06-01 15:34:25 +03005652 switch (ctxt->b) {
Gleb Natapovb2833e32009-04-12 13:36:30 +03005653 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005654 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005655 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005656 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03005657 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005658 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03005659 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03005660 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005661 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03005662 ctxt->dst.type = OP_NONE;
5663 else
5664 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09005665 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005666 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005667 switch (ctxt->op_bytes) {
5668 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
5669 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
5670 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005671 }
5672 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005673 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005674 rc = emulate_int(ctxt, 3);
5675 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005676 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005677 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005678 break;
5679 case 0xce: /* into */
Nadav Amit0efb0442015-03-29 16:33:03 +03005680 if (ctxt->eflags & X86_EFLAGS_OF)
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005681 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005682 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005683 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09005684 case 0xeb: /* jmp rel short */
Nadav Amit234f3ce2014-09-18 22:39:38 +03005685 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity9dac77f2011-06-01 15:34:25 +03005686 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005687 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005688 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03005689 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03005690 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005691 case 0xf5: /* cmc */
5692 /* complement carry flag from eflags reg */
Nadav Amit0efb0442015-03-29 16:33:03 +03005693 ctxt->eflags ^= X86_EFLAGS_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005694 break;
5695 case 0xf8: /* clc */
Nadav Amit0efb0442015-03-29 16:33:03 +03005696 ctxt->eflags &= ~X86_EFLAGS_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005697 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03005698 case 0xf9: /* stc */
Nadav Amit0efb0442015-03-29 16:33:03 +03005699 ctxt->eflags |= X86_EFLAGS_CF;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03005700 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005701 case 0xfc: /* cld */
Nadav Amit0efb0442015-03-29 16:33:03 +03005702 ctxt->eflags &= ~X86_EFLAGS_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005703 break;
5704 case 0xfd: /* std */
Nadav Amit0efb0442015-03-29 16:33:03 +03005705 ctxt->eflags |= X86_EFLAGS_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005706 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005707 default:
5708 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005709 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005710
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005711 if (rc != X86EMUL_CONTINUE)
5712 goto done;
5713
Avi Kivity018a98d2007-11-27 19:30:56 +02005714writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02005715 if (ctxt->d & SrcWrite) {
5716 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
5717 rc = writeback(ctxt, &ctxt->src);
5718 if (rc != X86EMUL_CONTINUE)
5719 goto done;
5720 }
Nadav Amitee212292014-06-15 16:12:58 +03005721 if (!(ctxt->d & NoWrite)) {
5722 rc = writeback(ctxt, &ctxt->dst);
5723 if (rc != X86EMUL_CONTINUE)
5724 goto done;
5725 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005726
Gleb Natapov5cd21912010-03-18 15:20:26 +02005727 /*
5728 * restore dst type in case the decoding will be reused
5729 * (happens for string instruction )
5730 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005731 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005732
Avi Kivity9dac77f2011-06-01 15:34:25 +03005733 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005734 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02005735
Avi Kivity9dac77f2011-06-01 15:34:25 +03005736 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005737 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02005738
Avi Kivity9dac77f2011-06-01 15:34:25 +03005739 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005740 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005741 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005742 if ((ctxt->d & SrcMask) == SrcSI)
5743 count = ctxt->src.count;
5744 else
5745 count = ctxt->dst.count;
Paolo Bonzini01485a22014-11-19 18:25:08 +01005746 register_address_increment(ctxt, VCPU_REGS_RCX, -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005747
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005748 if (!string_insn_completed(ctxt)) {
5749 /*
5750 * Re-enter guest when pio read ahead buffer is empty
5751 * or, if it is not used, after each 1024 iteration.
5752 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005753 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005754 (r->end == 0 || r->end != r->pos)) {
5755 /*
5756 * Reset read cache. Usually happens before
5757 * decode, but since instruction is restarted
5758 * we have to do it here.
5759 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005760 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005761 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005762 return EMULATION_RESTART;
5763 }
5764 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03005765 }
Nadav Amit0efb0442015-03-29 16:33:03 +03005766 ctxt->eflags &= ~X86_EFLAGS_RF;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005767 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005768
Avi Kivity9dac77f2011-06-01 15:34:25 +03005769 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02005770
5771done:
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005772 if (rc == X86EMUL_PROPAGATE_FAULT) {
5773 WARN_ON(ctxt->exception.vector > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +02005774 ctxt->have_exception = true;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005775 }
Joerg Roedel775fde82011-04-04 12:39:24 +02005776 if (rc == X86EMUL_INTERCEPTED)
5777 return EMULATION_INTERCEPTED;
5778
Avi Kivitydd856ef2012-08-27 23:46:17 +03005779 if (rc == X86EMUL_CONTINUE)
5780 writeback_registers(ctxt);
5781
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005782 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005783
5784twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005785 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02005786 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02005787 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005788 break;
5789 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02005790 case 0x0d: /* GrpP (prefetch) */
5791 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02005792 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02005793 break;
5794 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005795 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02005796 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005797 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005798 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005799 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005800 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03005801 if (test_cc(ctxt->b, ctxt->eflags))
5802 ctxt->dst.val = ctxt->src.val;
Nadav Amitb91aa142015-03-30 15:39:19 +03005803 else if (ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005804 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08005805 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03005806 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03005807 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005808 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005809 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08005810 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005811 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08005812 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005813 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005814 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005815 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03005816 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005817 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005818 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005819 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005820 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03005821 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005822 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005823 default:
5824 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005825 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005826
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005827threebyte_insn:
5828
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005829 if (rc != X86EMUL_CONTINUE)
5830 goto done;
5831
Avi Kivity6aa8b732006-12-10 02:21:36 -08005832 goto writeback;
5833
5834cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02005835 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005836}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005837
5838void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
5839{
5840 invalidate_registers(ctxt);
5841}
5842
5843void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
5844{
5845 writeback_registers(ctxt);
5846}
Tom Lendacky0f89b202016-12-14 14:59:23 -05005847
5848bool emulator_can_use_gpa(struct x86_emulate_ctxt *ctxt)
5849{
5850 if (ctxt->rep_prefix && (ctxt->d & String))
5851 return false;
5852
5853 if (ctxt->d & TwoMemOp)
5854 return false;
5855
5856 return true;
5857}