blob: ddbc61984227c3dbee763ed3759e9a5351f32606 [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>
Sean Christophersona7baead2020-01-17 11:30:50 -080025#include <asm/fpu/api.h>
Nadav Amit3db176d2015-04-19 21:12:59 +030026#include <asm/debugreg.h>
Peter Zijlstra1a29b5b2018-01-25 10:58:13 +010027#include <asm/nospec-branch.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028
Avi Kivity3eeb3282010-01-21 15:31:48 +020029#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020030#include "tss.h"
Yu Zhangd1cd3ce2017-08-24 20:27:53 +080031#include "mmu.h"
Arbel Moshe2d7921c2018-03-12 13:12:53 +020032#include "pmu.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020033
Avi Kivity6aa8b732006-12-10 02:21:36 -080034/*
Avi Kivitya99455492011-09-13 10:45:41 +030035 * Operand types
36 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030037#define OpNone 0ull
38#define OpImplicit 1ull /* No generic decode */
39#define OpReg 2ull /* Register */
40#define OpMem 3ull /* Memory */
41#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
42#define OpDI 5ull /* ES:DI/EDI/RDI */
43#define OpMem64 6ull /* Memory, 64-bit */
44#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
45#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030046#define OpCL 9ull /* CL register (for shifts) */
47#define OpImmByte 10ull /* 8-bit sign extended immediate */
48#define OpOne 11ull /* Implied 1 */
Nadav Amit5e2c6882012-12-06 21:55:10 -020049#define OpImm 12ull /* Sign extended up to 32-bit immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030050#define OpMem16 13ull /* Memory operand (16-bit). */
51#define OpMem32 14ull /* Memory operand (32-bit). */
52#define OpImmU 15ull /* Immediate operand, zero extended */
53#define OpSI 16ull /* SI/ESI/RSI */
54#define OpImmFAddr 17ull /* Immediate far address */
55#define OpMemFAddr 18ull /* Far address in memory */
56#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030057#define OpES 20ull /* ES */
58#define OpCS 21ull /* CS */
59#define OpSS 22ull /* SS */
60#define OpDS 23ull /* DS */
61#define OpFS 24ull /* FS */
62#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020063#define OpMem8 26ull /* 8-bit zero extended memory operand */
Nadav Amit5e2c6882012-12-06 21:55:10 -020064#define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */
Paolo Bonzini7fa57952013-05-09 11:32:50 +020065#define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */
Avi Kivity820207c2013-02-09 11:31:45 +020066#define OpAccLo 29ull /* Low part of extended acc (AX/AX/EAX/RAX) */
67#define OpAccHi 30ull /* High part of extended acc (-/DX/EDX/RDX) */
Avi Kivitya99455492011-09-13 10:45:41 +030068
Avi Kivity0fe59122011-09-13 10:45:47 +030069#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030070#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya99455492011-09-13 10:45:41 +030071
72/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080073 * Opcode effective-address decode tables.
74 * Note that we only emulate instructions that have at least one memory
75 * operand (excluding implicit stack references). We assume that stack
76 * references and instruction fetches will never occur in special memory
77 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
78 * not be handled.
79 */
80
81/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030082#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080083/* Destination operand type. */
Avi Kivitya99455492011-09-13 10:45:41 +030084#define DstShift 1
85#define ImplicitOps (OpImplicit << DstShift)
86#define DstReg (OpReg << DstShift)
87#define DstMem (OpMem << DstShift)
88#define DstAcc (OpAcc << DstShift)
89#define DstDI (OpDI << DstShift)
90#define DstMem64 (OpMem64 << DstShift)
Nadav Amit16bebef2014-12-25 02:52:18 +020091#define DstMem16 (OpMem16 << DstShift)
Avi Kivitya99455492011-09-13 10:45:41 +030092#define DstImmUByte (OpImmUByte << DstShift)
93#define DstDX (OpDX << DstShift)
Avi Kivity820207c2013-02-09 11:31:45 +020094#define DstAccLo (OpAccLo << DstShift)
Avi Kivitya99455492011-09-13 10:45:41 +030095#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080096/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030097#define SrcShift 6
98#define SrcNone (OpNone << SrcShift)
99#define SrcReg (OpReg << SrcShift)
100#define SrcMem (OpMem << SrcShift)
101#define SrcMem16 (OpMem16 << SrcShift)
102#define SrcMem32 (OpMem32 << SrcShift)
103#define SrcImm (OpImm << SrcShift)
104#define SrcImmByte (OpImmByte << SrcShift)
105#define SrcOne (OpOne << SrcShift)
106#define SrcImmUByte (OpImmUByte << SrcShift)
107#define SrcImmU (OpImmU << SrcShift)
108#define SrcSI (OpSI << SrcShift)
Paolo Bonzini7fa57952013-05-09 11:32:50 +0200109#define SrcXLat (OpXLat << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300110#define SrcImmFAddr (OpImmFAddr << SrcShift)
111#define SrcMemFAddr (OpMemFAddr << SrcShift)
112#define SrcAcc (OpAcc << SrcShift)
113#define SrcImmU16 (OpImmU16 << SrcShift)
Nadav Amit5e2c6882012-12-06 21:55:10 -0200114#define SrcImm64 (OpImm64 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300115#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200116#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity820207c2013-02-09 11:31:45 +0200117#define SrcAccHi (OpAccHi << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300118#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300119#define BitOp (1<<11)
120#define MemAbs (1<<12) /* Memory operand is absolute displacement */
121#define String (1<<13) /* String instruction (rep capable) */
122#define Stack (1<<14) /* Stack instruction (push/pop) */
123#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
124#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
125#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
126#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
127#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
Gleb Natapov045a2822012-12-20 16:57:43 +0200128#define Escape (5<<15) /* Escape to coprocessor instruction */
Nadav Amit39f062f2014-11-26 15:47:18 +0200129#define InstrDual (6<<15) /* Alternate instruction decoding of mod == 3 */
Nadav Amit2276b512015-01-26 09:32:24 +0200130#define ModeDual (7<<15) /* Different instruction for 32/64 bit */
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300131#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300132/* Generic ModRM decode. */
133#define ModRM (1<<19)
134/* Destination is only written; never read. */
135#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300136/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200137#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Borislav Petkovb51e9742013-09-22 16:44:52 +0200138#define EmulateOnUD (1<<22) /* Emulate if unsupported by the host */
Avi Kivity5a506b12010-08-01 15:10:29 +0300139#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300140#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300141#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200142#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200143#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300144#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800145#define PageTable (1 << 29) /* instruction used to write page table */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300146#define NotImpl (1 << 30) /* instruction is not implemented */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100147/* Source 2 operand type */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300148#define Src2Shift (31)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300149#define Src2None (OpNone << Src2Shift)
Avi Kivityab2c5ce2013-02-09 11:31:46 +0200150#define Src2Mem (OpMem << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300151#define Src2CL (OpCL << Src2Shift)
152#define Src2ImmByte (OpImmByte << Src2Shift)
153#define Src2One (OpOne << Src2Shift)
154#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300155#define Src2ES (OpES << Src2Shift)
156#define Src2CS (OpCS << Src2Shift)
157#define Src2SS (OpSS << Src2Shift)
158#define Src2DS (OpDS << Src2Shift)
159#define Src2FS (OpFS << Src2Shift)
160#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300161#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300162#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Radim Krčmář48520182016-11-08 20:54:17 +0100163#define AlignMask ((u64)7 << 41)
Avi Kivity1c11b372012-04-09 18:39:59 +0300164#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
Radim Krčmář48520182016-11-08 20:54:17 +0100165#define Unaligned ((u64)2 << 41) /* Explicitly unaligned (e.g. MOVDQU) */
166#define Avx ((u64)3 << 41) /* Advanced Vector Extensions */
167#define Aligned16 ((u64)4 << 41) /* Aligned to 16 byte boundary (e.g. FXSAVE) */
Avi Kivitye28bbd42013-01-04 16:18:48 +0200168#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
Avi Kivityb6744dc2013-01-04 16:18:50 +0200169#define NoWrite ((u64)1 << 45) /* No writeback */
Avi Kivityfb32b1e2013-02-09 11:31:44 +0200170#define SrcWrite ((u64)1 << 46) /* Write back src operand */
Nadav Amit9b88ae92014-05-25 23:05:21 +0300171#define NoMod ((u64)1 << 47) /* Mod field is ignored */
Paolo Bonzinid40a6892014-03-27 11:58:02 +0100172#define Intercept ((u64)1 << 48) /* Has valid intercept field */
173#define CheckPerm ((u64)1 << 49) /* Has valid check_perm field */
Nadav Amit68efa762014-06-18 17:19:35 +0300174#define PrivUD ((u64)1 << 51) /* #UD instead of #GP on CPL > 0 */
Nadav Amit58b70752014-10-24 11:35:09 +0300175#define NearBranch ((u64)1 << 52) /* Near branches */
Nadav Amited9aad22014-11-02 11:55:00 +0200176#define No16 ((u64)1 << 53) /* No 16 bit operand */
Nadav Amitab708092014-12-25 02:52:21 +0200177#define IncSP ((u64)1 << 54) /* SP is incremented before ModRM calc */
Tom Lendacky0f89b202016-12-14 14:59:23 -0500178#define TwoMemOp ((u64)1 << 55) /* Instruction has two memory operand */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800179
Avi Kivity820207c2013-02-09 11:31:45 +0200180#define DstXacc (DstAccLo | SrcAccHi | SrcWrite)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800181
Avi Kivityd0e53322010-07-29 15:11:54 +0300182#define X2(x...) x, x
183#define X3(x...) X2(x), x
184#define X4(x...) X2(x), X2(x)
185#define X5(x...) X4(x), x
186#define X6(x...) X4(x), X2(x)
187#define X7(x...) X4(x), X3(x)
188#define X8(x...) X4(x), X4(x)
189#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300190
Avi Kivitye28bbd42013-01-04 16:18:48 +0200191#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
192#define FASTOP_SIZE 8
193
194/*
195 * fastop functions have a special calling convention:
196 *
Avi Kivity017da7b2013-02-09 11:31:47 +0200197 * dst: rax (in/out)
198 * src: rdx (in/out)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200199 * src2: rcx (in)
200 * flags: rflags (in/out)
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200201 * ex: rsi (in:fastop pointer, out:zero if exception)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200202 *
203 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
204 * different operand sizes can be reached by calculation, rather than a jump
205 * table (which would be bigger than the code).
206 *
207 * fastop functions are declared as taking a never-defined fastop parameter,
208 * so they can't be called from C directly.
209 */
210
211struct fastop;
212
Avi Kivityd65b1de2010-07-29 15:11:35 +0300213struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300214 u64 flags : 56;
215 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300216 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300217 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200218 const struct opcode *group;
219 const struct group_dual *gdual;
220 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200221 const struct escape *esc;
Nadav Amit39f062f2014-11-26 15:47:18 +0200222 const struct instr_dual *idual;
Nadav Amit2276b512015-01-26 09:32:24 +0200223 const struct mode_dual *mdual;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200224 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300225 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200226 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300227};
228
229struct group_dual {
230 struct opcode mod012[8];
231 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300232};
233
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200234struct gprefix {
235 struct opcode pfx_no;
236 struct opcode pfx_66;
237 struct opcode pfx_f2;
238 struct opcode pfx_f3;
239};
240
Gleb Natapov045a2822012-12-20 16:57:43 +0200241struct escape {
242 struct opcode op[8];
243 struct opcode high[64];
244};
245
Nadav Amit39f062f2014-11-26 15:47:18 +0200246struct instr_dual {
247 struct opcode mod012;
248 struct opcode mod3;
249};
250
Nadav Amit2276b512015-01-26 09:32:24 +0200251struct mode_dual {
252 struct opcode mode32;
253 struct opcode mode64;
254};
255
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300256#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300257
Nadav Amit3dc4bc42014-12-25 02:52:19 +0200258enum x86_transfer_type {
259 X86_TRANSFER_NONE,
260 X86_TRANSFER_CALL_JMP,
261 X86_TRANSFER_RET,
262 X86_TRANSFER_TASK_SWITCH,
263};
264
Avi Kivitydd856ef2012-08-27 23:46:17 +0300265static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
266{
267 if (!(ctxt->regs_valid & (1 << nr))) {
268 ctxt->regs_valid |= 1 << nr;
269 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
270 }
271 return ctxt->_regs[nr];
272}
273
274static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
275{
276 ctxt->regs_valid |= 1 << nr;
277 ctxt->regs_dirty |= 1 << nr;
278 return &ctxt->_regs[nr];
279}
280
281static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
282{
283 reg_read(ctxt, nr);
284 return reg_write(ctxt, nr);
285}
286
287static void writeback_registers(struct x86_emulate_ctxt *ctxt)
288{
289 unsigned reg;
290
291 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
292 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
293}
294
295static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
296{
297 ctxt->regs_dirty = 0;
298 ctxt->regs_valid = 0;
299}
300
Avi Kivity6aa8b732006-12-10 02:21:36 -0800301/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800302 * These EFLAGS bits are restored from saved value during emulation, and
303 * any changes are written back to the saved value after emulation.
304 */
Nadav Amit0efb0442015-03-29 16:33:03 +0300305#define EFLAGS_MASK (X86_EFLAGS_OF|X86_EFLAGS_SF|X86_EFLAGS_ZF|X86_EFLAGS_AF|\
306 X86_EFLAGS_PF|X86_EFLAGS_CF)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800307
Avi Kivitydda96d82008-11-26 15:14:10 +0200308#ifdef CONFIG_X86_64
309#define ON64(x) x
310#else
311#define ON64(x)
312#endif
313
Sean Christopherson3009afc2020-01-21 20:43:39 -0800314typedef void (*fastop_t)(struct fastop *);
315
316static int fastop(struct x86_emulate_ctxt *ctxt, fastop_t fop);
Avi Kivity4d758342013-01-19 19:51:55 +0200317
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500318#define __FOP_FUNC(name) \
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600319 ".align " __stringify(FASTOP_SIZE) " \n\t" \
320 ".type " name ", @function \n\t" \
321 name ":\n\t"
322
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500323#define FOP_FUNC(name) \
324 __FOP_FUNC(#name)
325
326#define __FOP_RET(name) \
327 "ret \n\t" \
328 ".size " name ", .-" name "\n\t"
329
330#define FOP_RET(name) \
331 __FOP_RET(#name)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200332
333#define FOP_START(op) \
334 extern void em_##op(struct fastop *fake); \
335 asm(".pushsection .text, \"ax\" \n\t" \
336 ".global em_" #op " \n\t" \
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500337 ".align " __stringify(FASTOP_SIZE) " \n\t" \
338 "em_" #op ":\n\t"
Avi Kivityb7d491e2013-01-04 16:18:49 +0200339
340#define FOP_END \
341 ".popsection")
342
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500343#define __FOPNOP(name) \
344 __FOP_FUNC(name) \
345 __FOP_RET(name)
346
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600347#define FOPNOP() \
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500348 __FOPNOP(__stringify(__UNIQUE_ID(nop)))
Avi Kivity0bdea062013-01-19 19:51:50 +0200349
Avi Kivityb7d491e2013-01-04 16:18:49 +0200350#define FOP1E(op, dst) \
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500351 __FOP_FUNC(#op "_" #dst) \
352 "10: " #op " %" #dst " \n\t" \
353 __FOP_RET(#op "_" #dst)
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200354
355#define FOP1EEX(op, dst) \
356 FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200357
358#define FASTOP1(op) \
359 FOP_START(op) \
360 FOP1E(op##b, al) \
361 FOP1E(op##w, ax) \
362 FOP1E(op##l, eax) \
363 ON64(FOP1E(op##q, rax)) \
364 FOP_END
365
Avi Kivityb9fa4092013-02-09 11:31:48 +0200366/* 1-operand, using src2 (for MUL/DIV r/m) */
367#define FASTOP1SRC2(op, name) \
368 FOP_START(name) \
369 FOP1E(op, cl) \
370 FOP1E(op, cx) \
371 FOP1E(op, ecx) \
372 ON64(FOP1E(op, rcx)) \
373 FOP_END
374
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200375/* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */
376#define FASTOP1SRC2EX(op, name) \
377 FOP_START(name) \
378 FOP1EEX(op, cl) \
379 FOP1EEX(op, cx) \
380 FOP1EEX(op, ecx) \
381 ON64(FOP1EEX(op, rcx)) \
382 FOP_END
383
Avi Kivityf7857f32013-01-04 16:18:53 +0200384#define FOP2E(op, dst, src) \
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500385 __FOP_FUNC(#op "_" #dst "_" #src) \
386 #op " %" #src ", %" #dst " \n\t" \
387 __FOP_RET(#op "_" #dst "_" #src)
Avi Kivityf7857f32013-01-04 16:18:53 +0200388
389#define FASTOP2(op) \
390 FOP_START(op) \
Avi Kivity017da7b2013-02-09 11:31:47 +0200391 FOP2E(op##b, al, dl) \
392 FOP2E(op##w, ax, dx) \
393 FOP2E(op##l, eax, edx) \
394 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivityf7857f32013-01-04 16:18:53 +0200395 FOP_END
396
Avi Kivity11c363b2013-01-19 19:51:54 +0200397/* 2 operand, word only */
398#define FASTOP2W(op) \
399 FOP_START(op) \
400 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200401 FOP2E(op##w, ax, dx) \
402 FOP2E(op##l, eax, edx) \
403 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivity11c363b2013-01-19 19:51:54 +0200404 FOP_END
405
Avi Kivity007a3b52013-01-19 19:51:51 +0200406/* 2 operand, src is CL */
407#define FASTOP2CL(op) \
408 FOP_START(op) \
409 FOP2E(op##b, al, cl) \
410 FOP2E(op##w, ax, cl) \
411 FOP2E(op##l, eax, cl) \
412 ON64(FOP2E(op##q, rax, cl)) \
413 FOP_END
414
Nadav Amit5aca3722014-11-02 11:54:50 +0200415/* 2 operand, src and dest are reversed */
416#define FASTOP2R(op, name) \
417 FOP_START(name) \
418 FOP2E(op##b, dl, al) \
419 FOP2E(op##w, dx, ax) \
420 FOP2E(op##l, edx, eax) \
421 ON64(FOP2E(op##q, rdx, rax)) \
422 FOP_END
423
Avi Kivity0bdea062013-01-19 19:51:50 +0200424#define FOP3E(op, dst, src, src2) \
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500425 __FOP_FUNC(#op "_" #dst "_" #src "_" #src2) \
426 #op " %" #src2 ", %" #src ", %" #dst " \n\t"\
427 __FOP_RET(#op "_" #dst "_" #src "_" #src2)
Avi Kivity0bdea062013-01-19 19:51:50 +0200428
429/* 3-operand, word-only, src2=cl */
430#define FASTOP3WCL(op) \
431 FOP_START(op) \
432 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200433 FOP3E(op##w, ax, dx, cl) \
434 FOP3E(op##l, eax, edx, cl) \
435 ON64(FOP3E(op##q, rax, rdx, cl)) \
Avi Kivity0bdea062013-01-19 19:51:50 +0200436 FOP_END
437
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200438/* Special case for SETcc - 1 instruction per cc */
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600439#define FOP_SETCC(op) \
440 ".align 4 \n\t" \
441 ".type " #op ", @function \n\t" \
442 #op ": \n\t" \
443 #op " %al \n\t" \
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500444 __FOP_RET(#op)
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200445
Josh Poimboeuff26e6012017-10-04 10:39:05 -0500446asm(".pushsection .fixup, \"ax\"\n"
447 ".global kvm_fastop_exception \n"
448 "kvm_fastop_exception: xor %esi, %esi; ret\n"
449 ".popsection");
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200450
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200451FOP_START(setcc)
452FOP_SETCC(seto)
453FOP_SETCC(setno)
454FOP_SETCC(setc)
455FOP_SETCC(setnc)
456FOP_SETCC(setz)
457FOP_SETCC(setnz)
458FOP_SETCC(setbe)
459FOP_SETCC(setnbe)
460FOP_SETCC(sets)
461FOP_SETCC(setns)
462FOP_SETCC(setp)
463FOP_SETCC(setnp)
464FOP_SETCC(setl)
465FOP_SETCC(setnl)
466FOP_SETCC(setle)
467FOP_SETCC(setnle)
468FOP_END;
469
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500470FOP_START(salc)
471FOP_FUNC(salc)
472"pushf; sbb %al, %al; popf \n\t"
473FOP_RET(salc)
Paolo Bonzini326f5782013-05-09 11:32:51 +0200474FOP_END;
475
Radim Krčmářaabba3c2016-11-08 20:54:18 +0100476/*
477 * XXX: inoutclob user must know where the argument is being expanded.
Masahiro Yamadae9666d12018-12-31 00:14:15 +0900478 * Relying on CONFIG_CC_HAS_ASM_GOTO would allow us to remove _fault.
Radim Krčmářaabba3c2016-11-08 20:54:18 +0100479 */
480#define asm_safe(insn, inoutclob...) \
481({ \
482 int _fault = 0; \
483 \
484 asm volatile("1:" insn "\n" \
485 "2:\n" \
486 ".pushsection .fixup, \"ax\"\n" \
487 "3: movl $1, %[_fault]\n" \
488 " jmp 2b\n" \
489 ".popsection\n" \
490 _ASM_EXTABLE(1b, 3b) \
491 : [_fault] "+qm"(_fault) inoutclob ); \
492 \
493 _fault ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE; \
494})
495
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200496static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
497 enum x86_intercept intercept,
498 enum x86_intercept_stage stage)
499{
500 struct x86_instruction_info info = {
501 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300502 .rep_prefix = ctxt->rep_prefix,
503 .modrm_mod = ctxt->modrm_mod,
504 .modrm_reg = ctxt->modrm_reg,
505 .modrm_rm = ctxt->modrm_rm,
506 .src_val = ctxt->src.val64,
Jan Kiszka6cbc5f52014-06-30 12:52:55 +0200507 .dst_val = ctxt->dst.val64,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300508 .src_bytes = ctxt->src.bytes,
509 .dst_bytes = ctxt->dst.bytes,
510 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200511 .next_rip = ctxt->eip,
512 };
513
Avi Kivity29535382011-04-20 13:37:53 +0300514 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200515}
516
Avi Kivityf47cfa32012-06-07 17:49:24 +0300517static void assign_masked(ulong *dest, ulong src, ulong mask)
518{
519 *dest = (*dest & ~mask) | (src & mask);
520}
521
Nadav Amit6fd8e122015-03-30 15:39:20 +0300522static void assign_register(unsigned long *reg, u64 val, int bytes)
523{
524 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
525 switch (bytes) {
526 case 1:
527 *(u8 *)reg = (u8)val;
528 break;
529 case 2:
530 *(u16 *)reg = (u16)val;
531 break;
532 case 4:
533 *reg = (u32)val;
534 break; /* 64b: zero-extend */
535 case 8:
536 *reg = val;
537 break;
538 }
539}
540
Avi Kivity9dac77f2011-06-01 15:34:25 +0300541static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800542{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300543 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800544}
545
Avi Kivityf47cfa32012-06-07 17:49:24 +0300546static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
547{
548 u16 sel;
549 struct desc_struct ss;
550
551 if (ctxt->mode == X86EMUL_MODE_PROT64)
552 return ~0UL;
553 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
554 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
555}
556
Avi Kivity612e89f2012-06-12 20:03:23 +0300557static int stack_size(struct x86_emulate_ctxt *ctxt)
558{
559 return (__fls(stack_mask(ctxt)) + 1) >> 3;
560}
561
Avi Kivity6aa8b732006-12-10 02:21:36 -0800562/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800563static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300564address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800565{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300566 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800567 return reg;
568 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300569 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800570}
571
572static inline unsigned long
Paolo Bonzini01485a22014-11-19 18:25:08 +0100573register_address(struct x86_emulate_ctxt *ctxt, int reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800574{
Paolo Bonzini01485a22014-11-19 18:25:08 +0100575 return address_mask(ctxt, reg_read(ctxt, reg));
Harvey Harrisone4706772008-02-19 07:40:38 -0800576}
577
Avi Kivity5ad105e2012-08-19 14:34:31 +0300578static void masked_increment(ulong *reg, ulong mask, int inc)
579{
580 assign_masked(reg, *reg + inc, mask);
581}
582
Harvey Harrison7a9572752008-02-19 07:40:41 -0800583static inline void
Paolo Bonzini01485a22014-11-19 18:25:08 +0100584register_address_increment(struct x86_emulate_ctxt *ctxt, int reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800585{
Nadav Amitee122a72015-04-28 13:06:00 +0300586 ulong *preg = reg_rmw(ctxt, reg);
Avi Kivity5ad105e2012-08-19 14:34:31 +0300587
Nadav Amitee122a72015-04-28 13:06:00 +0300588 assign_register(preg, *preg + inc, ctxt->ad_bytes);
Avi Kivity5ad105e2012-08-19 14:34:31 +0300589}
590
591static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
592{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300593 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800594}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800595
Avi Kivity56697682011-04-03 14:08:51 +0300596static u32 desc_limit_scaled(struct desc_struct *desc)
597{
598 u32 limit = get_desc_limit(desc);
599
600 return desc->g ? (limit << 12) | 0xfff : limit;
601}
602
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900603static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300604{
605 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
606 return 0;
607
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900608 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300609}
610
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200611static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
612 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300613{
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +0200614 WARN_ON(vec > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +0200615 ctxt->exception.vector = vec;
616 ctxt->exception.error_code = error;
617 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200618 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300619}
620
Joerg Roedel3b88e412011-04-04 12:39:29 +0200621static int emulate_db(struct x86_emulate_ctxt *ctxt)
622{
623 return emulate_exception(ctxt, DB_VECTOR, 0, false);
624}
625
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200626static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300627{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200628 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300629}
630
Avi Kivity618ff152011-04-03 12:32:09 +0300631static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
632{
633 return emulate_exception(ctxt, SS_VECTOR, err, true);
634}
635
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200636static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300637{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200638 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300639}
640
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200641static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300642{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200643 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300644}
645
Avi Kivity34d1f492010-08-26 11:59:01 +0300646static int emulate_de(struct x86_emulate_ctxt *ctxt)
647{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200648 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300649}
650
Avi Kivity1253791d2011-03-29 11:41:27 +0200651static int emulate_nm(struct x86_emulate_ctxt *ctxt)
652{
653 return emulate_exception(ctxt, NM_VECTOR, 0, false);
654}
655
Avi Kivity1aa36612011-04-27 13:20:30 +0300656static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
657{
658 u16 selector;
659 struct desc_struct desc;
660
661 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
662 return selector;
663}
664
665static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
666 unsigned seg)
667{
668 u16 dummy;
669 u32 base3;
670 struct desc_struct desc;
671
672 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
673 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
674}
675
Avi Kivity1c11b372012-04-09 18:39:59 +0300676/*
677 * x86 defines three classes of vector instructions: explicitly
678 * aligned, explicitly unaligned, and the rest, which change behaviour
679 * depending on whether they're AVX encoded or not.
680 *
681 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100682 * subject to the same check. FXSAVE and FXRSTOR are checked here too as their
683 * 512 bytes of data must be aligned to a 16 byte boundary.
Avi Kivity1c11b372012-04-09 18:39:59 +0300684 */
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100685static unsigned insn_alignment(struct x86_emulate_ctxt *ctxt, unsigned size)
Avi Kivity1c11b372012-04-09 18:39:59 +0300686{
Radim Krčmář48520182016-11-08 20:54:17 +0100687 u64 alignment = ctxt->d & AlignMask;
Avi Kivity1c11b372012-04-09 18:39:59 +0300688
Avi Kivity1c11b372012-04-09 18:39:59 +0300689 if (likely(size < 16))
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100690 return 1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300691
Radim Krčmář48520182016-11-08 20:54:17 +0100692 switch (alignment) {
693 case Unaligned:
694 case Avx:
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100695 return 1;
Radim Krčmář48520182016-11-08 20:54:17 +0100696 case Aligned16:
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100697 return 16;
Radim Krčmář48520182016-11-08 20:54:17 +0100698 case Aligned:
699 default:
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100700 return size;
Radim Krčmář48520182016-11-08 20:54:17 +0100701 }
Avi Kivity1c11b372012-04-09 18:39:59 +0300702}
703
Paolo Bonzinid09155d2014-10-27 14:54:44 +0100704static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
705 struct segmented_address addr,
706 unsigned *max_size, unsigned size,
707 bool write, bool fetch,
Nadav Amitd50eaa12014-11-19 17:43:11 +0200708 enum x86emul_mode mode, ulong *linear)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300709{
Avi Kivity618ff152011-04-03 12:32:09 +0300710 struct desc_struct desc;
711 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300712 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300713 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300714 u16 sel;
Yu Zhangfd8cb432017-08-24 20:27:56 +0800715 u8 va_bits;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300716
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900717 la = seg_base(ctxt, addr.seg) + addr.ea;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100718 *max_size = 0;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200719 switch (mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300720 case X86EMUL_MODE_PROT64:
Paolo Bonzini0c1d77f2016-02-19 18:07:21 +0100721 *linear = la;
Yu Zhangfd8cb432017-08-24 20:27:56 +0800722 va_bits = ctxt_virt_addr_bits(ctxt);
723 if (get_canonical(la, va_bits) != la)
Nadav Amitabc7d8a2014-11-19 17:43:12 +0200724 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100725
Yu Zhangfd8cb432017-08-24 20:27:56 +0800726 *max_size = min_t(u64, ~0u, (1ull << va_bits) - la);
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100727 if (size > *max_size)
728 goto bad;
Avi Kivity618ff152011-04-03 12:32:09 +0300729 break;
730 default:
Paolo Bonzini0c1d77f2016-02-19 18:07:21 +0100731 *linear = la = (u32)la;
Avi Kivity1aa36612011-04-27 13:20:30 +0300732 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
733 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300734 if (!usable)
735 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200736 /* code segment in protected mode or read-only data segment */
737 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
738 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300739 goto bad;
740 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400741 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300742 goto bad;
743 lim = desc_limit_scaled(&desc);
Paolo Bonzini997b0412014-11-19 18:33:38 +0100744 if (!(desc.type & 8) && (desc.type & 4)) {
Guo Chaofc058682012-06-28 15:19:51 +0800745 /* expand-down segment */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100746 if (addr.ea <= lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300747 goto bad;
748 lim = desc.d ? 0xffffffff : 0xffff;
Avi Kivity618ff152011-04-03 12:32:09 +0300749 }
Paolo Bonzini997b0412014-11-19 18:33:38 +0100750 if (addr.ea > lim)
751 goto bad;
Nadav Amitbac155312015-01-26 09:32:26 +0200752 if (lim == 0xffffffff)
753 *max_size = ~0u;
754 else {
755 *max_size = (u64)lim + 1 - addr.ea;
756 if (size > *max_size)
757 goto bad;
758 }
Avi Kivity618ff152011-04-03 12:32:09 +0300759 break;
760 }
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100761 if (la & (insn_alignment(ctxt, size) - 1))
Avi Kivity1c11b372012-04-09 18:39:59 +0300762 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300763 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300764bad:
765 if (addr.seg == VCPU_SREG_SS)
Paolo Bonzini36061892014-10-27 14:40:49 +0100766 return emulate_ss(ctxt, 0);
Avi Kivity618ff152011-04-03 12:32:09 +0300767 else
Paolo Bonzini36061892014-10-27 14:40:49 +0100768 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300769}
770
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400771static int linearize(struct x86_emulate_ctxt *ctxt,
772 struct segmented_address addr,
773 unsigned size, bool write,
774 ulong *linear)
775{
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100776 unsigned max_size;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200777 return __linearize(ctxt, addr, &max_size, size, write, false,
778 ctxt->mode, linear);
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400779}
780
Nadav Amitd50eaa12014-11-19 17:43:11 +0200781static inline int assign_eip(struct x86_emulate_ctxt *ctxt, ulong dst,
782 enum x86emul_mode mode)
783{
784 ulong linear;
785 int rc;
786 unsigned max_size;
787 struct segmented_address addr = { .seg = VCPU_SREG_CS,
788 .ea = dst };
789
790 if (ctxt->op_bytes != sizeof(unsigned long))
791 addr.ea = dst & ((1UL << (ctxt->op_bytes << 3)) - 1);
792 rc = __linearize(ctxt, addr, &max_size, 1, false, true, mode, &linear);
793 if (rc == X86EMUL_CONTINUE)
794 ctxt->_eip = addr.ea;
795 return rc;
796}
797
798static inline int assign_eip_near(struct x86_emulate_ctxt *ctxt, ulong dst)
799{
800 return assign_eip(ctxt, dst, ctxt->mode);
801}
802
803static int assign_eip_far(struct x86_emulate_ctxt *ctxt, ulong dst,
804 const struct desc_struct *cs_desc)
805{
806 enum x86emul_mode mode = ctxt->mode;
Nadav Amit82268082015-01-26 09:32:27 +0200807 int rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200808
809#ifdef CONFIG_X86_64
Nadav Amit82268082015-01-26 09:32:27 +0200810 if (ctxt->mode >= X86EMUL_MODE_PROT16) {
811 if (cs_desc->l) {
812 u64 efer = 0;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200813
Nadav Amit82268082015-01-26 09:32:27 +0200814 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
815 if (efer & EFER_LMA)
816 mode = X86EMUL_MODE_PROT64;
817 } else
818 mode = X86EMUL_MODE_PROT32; /* temporary value */
Nadav Amitd50eaa12014-11-19 17:43:11 +0200819 }
820#endif
821 if (mode == X86EMUL_MODE_PROT16 || mode == X86EMUL_MODE_PROT32)
822 mode = cs_desc->d ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
Nadav Amit82268082015-01-26 09:32:27 +0200823 rc = assign_eip(ctxt, dst, mode);
824 if (rc == X86EMUL_CONTINUE)
825 ctxt->mode = mode;
826 return rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200827}
828
829static inline int jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
830{
831 return assign_eip_near(ctxt, ctxt->_eip + rel);
832}
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400833
Paolo Bonzini79367a62018-06-06 16:43:02 +0200834static int linear_read_system(struct x86_emulate_ctxt *ctxt, ulong linear,
835 void *data, unsigned size)
836{
Paolo Bonzini3c9fa242018-06-06 17:38:09 +0200837 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception, true);
Paolo Bonzini79367a62018-06-06 16:43:02 +0200838}
839
840static int linear_write_system(struct x86_emulate_ctxt *ctxt,
841 ulong linear, void *data,
842 unsigned int size)
843{
Paolo Bonzini3c9fa242018-06-06 17:38:09 +0200844 return ctxt->ops->write_std(ctxt, linear, data, size, &ctxt->exception, true);
Paolo Bonzini79367a62018-06-06 16:43:02 +0200845}
846
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200847static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
848 struct segmented_address addr,
849 void *data,
850 unsigned size)
851{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200852 int rc;
853 ulong linear;
854
Avi Kivity83b87952011-04-03 11:31:19 +0300855 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200856 if (rc != X86EMUL_CONTINUE)
857 return rc;
Paolo Bonzini3c9fa242018-06-06 17:38:09 +0200858 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception, false);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200859}
860
Steve Rutherford129a72a2017-01-11 18:28:29 -0800861static int segmented_write_std(struct x86_emulate_ctxt *ctxt,
862 struct segmented_address addr,
863 void *data,
864 unsigned int size)
865{
866 int rc;
867 ulong linear;
868
869 rc = linearize(ctxt, addr, size, true, &linear);
870 if (rc != X86EMUL_CONTINUE)
871 return rc;
Paolo Bonzini3c9fa242018-06-06 17:38:09 +0200872 return ctxt->ops->write_std(ctxt, linear, data, size, &ctxt->exception, false);
Steve Rutherford129a72a2017-01-11 18:28:29 -0800873}
874
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900875/*
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200876 * Prefetch the remaining bytes of the instruction without crossing page
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900877 * boundary if they are not in fetch_cache yet.
878 */
Paolo Bonzini9506d572014-05-06 13:05:25 +0200879static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
Avi Kivity62266862007-11-20 13:15:52 +0200880{
Avi Kivity62266862007-11-20 13:15:52 +0200881 int rc;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100882 unsigned size, max_size;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200883 unsigned long linear;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200884 int cur_size = ctxt->fetch.end - ctxt->fetch.data;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200885 struct segmented_address addr = { .seg = VCPU_SREG_CS,
Paolo Bonzini17052f12014-05-06 16:33:01 +0200886 .ea = ctxt->eip + cur_size };
887
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100888 /*
889 * We do not know exactly how many bytes will be needed, and
890 * __linearize is expensive, so fetch as much as possible. We
891 * just have to avoid going beyond the 15 byte limit, the end
892 * of the segment, or the end of the page.
893 *
894 * __linearize is called with size 0 so that it does not do any
895 * boundary check itself. Instead, we use max_size to check
896 * against op_size.
897 */
Nadav Amitd50eaa12014-11-19 17:43:11 +0200898 rc = __linearize(ctxt, addr, &max_size, 0, false, true, ctxt->mode,
899 &linear);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200900 if (unlikely(rc != X86EMUL_CONTINUE))
901 return rc;
902
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100903 size = min_t(unsigned, 15UL ^ cur_size, max_size);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200904 size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear));
Paolo Bonzini5cfc7e02014-05-06 13:05:25 +0200905
906 /*
907 * One instruction can only straddle two pages,
908 * and one has been loaded at the beginning of
909 * x86_decode_insn. So, if not enough bytes
910 * still, we must have hit the 15-byte boundary.
911 */
912 if (unlikely(size < op_size))
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100913 return emulate_gp(ctxt, 0);
914
Paolo Bonzini17052f12014-05-06 16:33:01 +0200915 rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end,
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200916 size, &ctxt->exception);
917 if (unlikely(rc != X86EMUL_CONTINUE))
918 return rc;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200919 ctxt->fetch.end += size;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900920 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200921}
922
Paolo Bonzini9506d572014-05-06 13:05:25 +0200923static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt,
924 unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200925{
Nadav Amit08da44a2014-10-03 01:10:04 +0300926 unsigned done_size = ctxt->fetch.end - ctxt->fetch.ptr;
927
928 if (unlikely(done_size < size))
929 return __do_insn_fetch_bytes(ctxt, size - done_size);
Paolo Bonzini9506d572014-05-06 13:05:25 +0200930 else
931 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200932}
933
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900934/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900935#define insn_fetch(_type, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200936({ _type _x; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200937 \
938 rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900939 if (rc != X86EMUL_CONTINUE) \
940 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200941 ctxt->_eip += sizeof(_type); \
Nick Desaulniers8616abc2017-06-27 19:37:14 -0700942 memcpy(&_x, ctxt->fetch.ptr, sizeof(_type)); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200943 ctxt->fetch.ptr += sizeof(_type); \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200944 _x; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900945})
946
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900947#define insn_fetch_arr(_arr, _size, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200948({ \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200949 rc = do_insn_fetch_bytes(_ctxt, _size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900950 if (rc != X86EMUL_CONTINUE) \
951 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200952 ctxt->_eip += (_size); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200953 memcpy(_arr, ctxt->fetch.ptr, _size); \
954 ctxt->fetch.ptr += (_size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900955})
956
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000957/*
958 * Given the 'reg' portion of a ModRM byte, and a register block, return a
959 * pointer into the block that addresses the relevant register.
960 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
961 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300962static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200963 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800964{
965 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200966 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800967
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300969 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
970 else
971 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972 return p;
973}
974
975static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200976 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800977 u16 *size, unsigned long *address, int op_bytes)
978{
979 int rc;
980
981 if (op_bytes == 2)
982 op_bytes = 3;
983 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200984 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900985 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200987 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200988 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800989 return rc;
990}
991
Avi Kivity34b77652013-01-19 19:51:56 +0200992FASTOP2(add);
993FASTOP2(or);
994FASTOP2(adc);
995FASTOP2(sbb);
996FASTOP2(and);
997FASTOP2(sub);
998FASTOP2(xor);
999FASTOP2(cmp);
1000FASTOP2(test);
1001
Avi Kivityb9fa4092013-02-09 11:31:48 +02001002FASTOP1SRC2(mul, mul_ex);
1003FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02001004FASTOP1SRC2EX(div, div_ex);
1005FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +02001006
Avi Kivity34b77652013-01-19 19:51:56 +02001007FASTOP3WCL(shld);
1008FASTOP3WCL(shrd);
1009
1010FASTOP2W(imul);
1011
1012FASTOP1(not);
1013FASTOP1(neg);
1014FASTOP1(inc);
1015FASTOP1(dec);
1016
1017FASTOP2CL(rol);
1018FASTOP2CL(ror);
1019FASTOP2CL(rcl);
1020FASTOP2CL(rcr);
1021FASTOP2CL(shl);
1022FASTOP2CL(shr);
1023FASTOP2CL(sar);
1024
1025FASTOP2W(bsf);
1026FASTOP2W(bsr);
1027FASTOP2W(bt);
1028FASTOP2W(bts);
1029FASTOP2W(btr);
1030FASTOP2W(btc);
1031
Avi Kivitye47a5f52013-02-09 11:31:51 +02001032FASTOP2(xadd);
1033
Nadav Amit5aca3722014-11-02 11:54:50 +02001034FASTOP2R(cmp, cmp_r);
1035
Nadav Amit900efe22015-03-30 15:39:21 +03001036static int em_bsf_c(struct x86_emulate_ctxt *ctxt)
1037{
1038 /* If src is zero, do not writeback, but update flags */
1039 if (ctxt->src.val == 0)
1040 ctxt->dst.type = OP_NONE;
1041 return fastop(ctxt, em_bsf);
1042}
1043
1044static int em_bsr_c(struct x86_emulate_ctxt *ctxt)
1045{
1046 /* If src is zero, do not writeback, but update flags */
1047 if (ctxt->src.val == 0)
1048 ctxt->dst.type = OP_NONE;
1049 return fastop(ctxt, em_bsr);
1050}
1051
Josh Poimboeufcb7390f2016-01-22 10:16:12 -06001052static __always_inline u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001053{
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001054 u8 rc;
1055 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001056
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001057 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Peter Zijlstra1a29b5b2018-01-25 10:58:13 +01001058 asm("push %[flags]; popf; " CALL_NOSPEC
1059 : "=a"(rc) : [thunk_target]"r"(fop), [flags]"r"(flags));
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001060 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001061}
1062
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001063static void fetch_register_operand(struct operand *op)
1064{
1065 switch (op->bytes) {
1066 case 1:
1067 op->val = *(u8 *)op->addr.reg;
1068 break;
1069 case 2:
1070 op->val = *(u16 *)op->addr.reg;
1071 break;
1072 case 4:
1073 op->val = *(u32 *)op->addr.reg;
1074 break;
1075 case 8:
1076 op->val = *(u64 *)op->addr.reg;
1077 break;
1078 }
1079}
1080
Sean Christophersona7baead2020-01-17 11:30:50 -08001081static void emulator_get_fpu(void)
1082{
1083 fpregs_lock();
1084
1085 fpregs_assert_state_consistent();
1086 if (test_thread_flag(TIF_NEED_FPU_LOAD))
1087 switch_fpu_return();
1088}
1089
1090static void emulator_put_fpu(void)
1091{
1092 fpregs_unlock();
1093}
1094
Sean Christophersonc0a21c32020-01-17 11:30:52 -08001095static void read_sse_reg(sse128_t *data, int reg)
Avi Kivity1253791d2011-03-29 11:41:27 +02001096{
Sean Christophersona7baead2020-01-17 11:30:50 -08001097 emulator_get_fpu();
Avi Kivity1253791d2011-03-29 11:41:27 +02001098 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001099 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
1100 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
1101 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
1102 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
1103 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
1104 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
1105 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
1106 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001107#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001108 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
1109 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
1110 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
1111 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
1112 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
1113 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
1114 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
1115 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001116#endif
1117 default: BUG();
1118 }
Sean Christophersona7baead2020-01-17 11:30:50 -08001119 emulator_put_fpu();
Avi Kivity1253791d2011-03-29 11:41:27 +02001120}
1121
Sean Christophersonc0a21c32020-01-17 11:30:52 -08001122static void write_sse_reg(sse128_t *data, int reg)
Avi Kivity1253791d2011-03-29 11:41:27 +02001123{
Sean Christophersona7baead2020-01-17 11:30:50 -08001124 emulator_get_fpu();
Avi Kivity1253791d2011-03-29 11:41:27 +02001125 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001126 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
1127 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
1128 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
1129 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
1130 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
1131 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
1132 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
1133 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001134#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001135 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
1136 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
1137 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
1138 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
1139 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
1140 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
1141 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
1142 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001143#endif
1144 default: BUG();
1145 }
Sean Christophersona7baead2020-01-17 11:30:50 -08001146 emulator_put_fpu();
Avi Kivity1253791d2011-03-29 11:41:27 +02001147}
1148
Sean Christophersonc0a21c32020-01-17 11:30:52 -08001149static void read_mmx_reg(u64 *data, int reg)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001150{
Sean Christophersona7baead2020-01-17 11:30:50 -08001151 emulator_get_fpu();
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001152 switch (reg) {
1153 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1154 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1155 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1156 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1157 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1158 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1159 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1160 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1161 default: BUG();
1162 }
Sean Christophersona7baead2020-01-17 11:30:50 -08001163 emulator_put_fpu();
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001164}
1165
Sean Christophersonc0a21c32020-01-17 11:30:52 -08001166static void write_mmx_reg(u64 *data, int reg)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001167{
Sean Christophersona7baead2020-01-17 11:30:50 -08001168 emulator_get_fpu();
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001169 switch (reg) {
1170 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1171 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1172 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1173 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1174 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1175 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1176 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1177 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1178 default: BUG();
1179 }
Sean Christophersona7baead2020-01-17 11:30:50 -08001180 emulator_put_fpu();
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001181}
1182
Gleb Natapov045a2822012-12-20 16:57:43 +02001183static int em_fninit(struct x86_emulate_ctxt *ctxt)
1184{
1185 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1186 return emulate_nm(ctxt);
1187
Sean Christophersona7baead2020-01-17 11:30:50 -08001188 emulator_get_fpu();
Gleb Natapov045a2822012-12-20 16:57:43 +02001189 asm volatile("fninit");
Sean Christophersona7baead2020-01-17 11:30:50 -08001190 emulator_put_fpu();
Gleb Natapov045a2822012-12-20 16:57:43 +02001191 return X86EMUL_CONTINUE;
1192}
1193
1194static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1195{
1196 u16 fcw;
1197
1198 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1199 return emulate_nm(ctxt);
1200
Sean Christophersona7baead2020-01-17 11:30:50 -08001201 emulator_get_fpu();
Gleb Natapov045a2822012-12-20 16:57:43 +02001202 asm volatile("fnstcw %0": "+m"(fcw));
Sean Christophersona7baead2020-01-17 11:30:50 -08001203 emulator_put_fpu();
Gleb Natapov045a2822012-12-20 16:57:43 +02001204
Gleb Natapov045a2822012-12-20 16:57:43 +02001205 ctxt->dst.val = fcw;
1206
1207 return X86EMUL_CONTINUE;
1208}
1209
1210static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1211{
1212 u16 fsw;
1213
1214 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1215 return emulate_nm(ctxt);
1216
Sean Christophersona7baead2020-01-17 11:30:50 -08001217 emulator_get_fpu();
Gleb Natapov045a2822012-12-20 16:57:43 +02001218 asm volatile("fnstsw %0": "+m"(fsw));
Sean Christophersona7baead2020-01-17 11:30:50 -08001219 emulator_put_fpu();
Gleb Natapov045a2822012-12-20 16:57:43 +02001220
Gleb Natapov045a2822012-12-20 16:57:43 +02001221 ctxt->dst.val = fsw;
1222
1223 return X86EMUL_CONTINUE;
1224}
1225
Avi Kivity1253791d2011-03-29 11:41:27 +02001226static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001227 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001228{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001229 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001230
Avi Kivity9dac77f2011-06-01 15:34:25 +03001231 if (!(ctxt->d & ModRM))
1232 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity1253791d2011-03-29 11:41:27 +02001233
Avi Kivity9dac77f2011-06-01 15:34:25 +03001234 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001235 op->type = OP_XMM;
1236 op->bytes = 16;
1237 op->addr.xmm = reg;
Sean Christophersonc0a21c32020-01-17 11:30:52 -08001238 read_sse_reg(&op->vec_val, reg);
Avi Kivity1253791d2011-03-29 11:41:27 +02001239 return;
1240 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001241 if (ctxt->d & Mmx) {
1242 reg &= 7;
1243 op->type = OP_MM;
1244 op->bytes = 8;
1245 op->addr.mm = reg;
1246 return;
1247 }
Avi Kivity1253791d2011-03-29 11:41:27 +02001248
Avi Kivity3c118e22007-10-31 10:27:04 +02001249 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001250 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1251 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1252
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001253 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001254 op->orig_val = op->val;
1255}
1256
Avi Kivitya6e34072012-06-10 17:15:39 +03001257static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1258{
1259 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1260 ctxt->modrm_seg = VCPU_SREG_SS;
1261}
1262
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001263static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001264 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001265{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001266 u8 sib;
Bandan Das02357bd2014-04-16 12:46:11 -04001267 int index_reg, base_reg, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001268 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001269 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001270
Bandan Das02357bd2014-04-16 12:46:11 -04001271 ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */
1272 index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */
1273 base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001274
Bandan Das02357bd2014-04-16 12:46:11 -04001275 ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001276 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
Bandan Das02357bd2014-04-16 12:46:11 -04001277 ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001278 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001279
Nadav Amit9b88ae92014-05-25 23:05:21 +03001280 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001281 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001282 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001283 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001284 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001285 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001286 op->type = OP_XMM;
1287 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001288 op->addr.xmm = ctxt->modrm_rm;
Sean Christophersonc0a21c32020-01-17 11:30:52 -08001289 read_sse_reg(&op->vec_val, ctxt->modrm_rm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001290 return rc;
1291 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001292 if (ctxt->d & Mmx) {
1293 op->type = OP_MM;
1294 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001295 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001296 return rc;
1297 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001298 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001299 return rc;
1300 }
1301
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001302 op->type = OP_MEM;
1303
Avi Kivity9dac77f2011-06-01 15:34:25 +03001304 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001305 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1306 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1307 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1308 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001309
1310 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001311 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001312 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001313 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001314 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001315 break;
1316 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001317 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001318 break;
1319 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001320 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001321 break;
1322 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001323 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001324 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001325 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001326 break;
1327 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001328 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001329 break;
1330 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001331 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001332 break;
1333 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001334 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001335 break;
1336 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001337 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001338 break;
1339 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001340 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001341 break;
1342 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001343 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001344 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001345 break;
1346 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001347 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001348 break;
1349 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001350 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1351 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1352 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001353 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001354 } else {
1355 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001356 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001357 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001358 index_reg |= (sib >> 3) & 7;
1359 base_reg |= sib & 7;
1360 scale = sib >> 6;
1361
Avi Kivity9dac77f2011-06-01 15:34:25 +03001362 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001363 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001364 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001365 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001366 adjust_modrm_seg(ctxt, base_reg);
Nadav Amitab708092014-12-25 02:52:21 +02001367 /* Increment ESP on POP [ESP] */
1368 if ((ctxt->d & IncSP) &&
1369 base_reg == VCPU_REGS_RSP)
1370 modrm_ea += ctxt->op_bytes;
Avi Kivitya6e34072012-06-10 17:15:39 +03001371 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001372 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001373 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001374 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Nadav Amit5b38ab82014-11-02 11:54:41 +02001375 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity84411d82008-06-15 21:53:26 -07001376 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001377 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001378 } else {
1379 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001380 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001381 adjust_modrm_seg(ctxt, base_reg);
1382 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001383 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001384 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001385 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001386 break;
1387 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001388 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001389 break;
1390 }
1391 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001392 op->addr.mem.ea = modrm_ea;
Bandan Das41061cd2014-04-16 12:46:14 -04001393 if (ctxt->ad_bytes != 8)
1394 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
1395
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001396done:
1397 return rc;
1398}
1399
1400static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001401 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001402{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001403 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001404
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001405 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001406 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001407 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001408 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001409 break;
1410 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001411 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001412 break;
1413 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001414 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001415 break;
1416 }
1417done:
1418 return rc;
1419}
1420
Avi Kivity9dac77f2011-06-01 15:34:25 +03001421static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001422{
Sheng Yang7129eec2010-09-28 16:33:32 +08001423 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001424
Avi Kivity9dac77f2011-06-01 15:34:25 +03001425 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001426 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001427
Avi Kivity9dac77f2011-06-01 15:34:25 +03001428 if (ctxt->src.bytes == 2)
1429 sv = (s16)ctxt->src.val & (s16)mask;
1430 else if (ctxt->src.bytes == 4)
1431 sv = (s32)ctxt->src.val & (s32)mask;
Nadav Amit7dec5602014-06-15 16:12:57 +03001432 else
1433 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001434
Nadav Amit1c1c35a2014-11-19 17:43:09 +02001435 ctxt->dst.addr.mem.ea = address_mask(ctxt,
1436 ctxt->dst.addr.mem.ea + (sv >> 3));
Wei Yongjun35c843c2010-08-09 11:34:56 +08001437 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001438
1439 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001440 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001441}
1442
Gleb Natapov9de41572010-04-28 19:15:22 +03001443static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001444 unsigned long addr, void *dest, unsigned size)
1445{
1446 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001447 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001448
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001449 if (mc->pos < mc->end)
1450 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001451
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001452 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001453
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001454 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1455 &ctxt->exception);
1456 if (rc != X86EMUL_CONTINUE)
1457 return rc;
1458
1459 mc->end += size;
1460
1461read_cached:
1462 memcpy(dest, mc->data + mc->pos, size);
1463 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001464 return X86EMUL_CONTINUE;
1465}
1466
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001467static int segmented_read(struct x86_emulate_ctxt *ctxt,
1468 struct segmented_address addr,
1469 void *data,
1470 unsigned size)
1471{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001472 int rc;
1473 ulong linear;
1474
Avi Kivity83b87952011-04-03 11:31:19 +03001475 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001476 if (rc != X86EMUL_CONTINUE)
1477 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001478 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001479}
1480
1481static int segmented_write(struct x86_emulate_ctxt *ctxt,
1482 struct segmented_address addr,
1483 const void *data,
1484 unsigned size)
1485{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001486 int rc;
1487 ulong linear;
1488
Avi Kivity83b87952011-04-03 11:31:19 +03001489 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001490 if (rc != X86EMUL_CONTINUE)
1491 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001492 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1493 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001494}
1495
1496static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1497 struct segmented_address addr,
1498 const void *orig_data, const void *data,
1499 unsigned size)
1500{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001501 int rc;
1502 ulong linear;
1503
Avi Kivity83b87952011-04-03 11:31:19 +03001504 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001505 if (rc != X86EMUL_CONTINUE)
1506 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001507 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1508 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001509}
1510
Gleb Natapov7b262e92010-03-18 15:20:27 +02001511static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001512 unsigned int size, unsigned short port,
1513 void *dest)
1514{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001515 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001516
1517 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001518 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001519 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001520 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Nadav Amit0efb0442015-03-29 16:33:03 +03001521 in_page = (ctxt->eflags & X86_EFLAGS_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001522 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1523 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Mark Rustadb55a8142014-07-25 06:27:05 -07001524 n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001525 if (n == 0)
1526 n = 1;
1527 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001528 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001529 return 0;
1530 rc->end = n * size;
1531 }
1532
Nadav Amite6e39f02014-04-18 03:35:10 +03001533 if (ctxt->rep_prefix && (ctxt->d & String) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03001534 !(ctxt->eflags & X86_EFLAGS_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001535 ctxt->dst.data = rc->data + rc->pos;
1536 ctxt->dst.type = OP_MEM_STR;
1537 ctxt->dst.count = (rc->end - rc->pos) / size;
1538 rc->pos = rc->end;
1539 } else {
1540 memcpy(dest, rc->data + rc->pos, size);
1541 rc->pos += size;
1542 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001543 return 1;
1544}
1545
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001546static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1547 u16 index, struct desc_struct *desc)
1548{
1549 struct desc_ptr dt;
1550 ulong addr;
1551
1552 ctxt->ops->get_idt(ctxt, &dt);
1553
1554 if (dt.size < index * 8 + 7)
1555 return emulate_gp(ctxt, index << 3 | 0x2);
1556
1557 addr = dt.address + index * 8;
Jordan Borgner0e96f312018-10-28 12:58:28 +00001558 return linear_read_system(ctxt, addr, desc, sizeof(*desc));
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001559}
1560
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001561static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001562 u16 selector, struct desc_ptr *dt)
1563{
Mathias Krause0225fb52012-08-30 01:30:16 +02001564 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001565 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001566
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001567 if (selector & 1 << 2) {
1568 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001569 u16 sel;
1570
Jordan Borgner0e96f312018-10-28 12:58:28 +00001571 memset(dt, 0, sizeof(*dt));
Nadav Amit2eedcac2014-06-02 18:34:05 +03001572 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1573 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001574 return;
1575
1576 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001577 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001578 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001579 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001580}
1581
Nadav Amitedccda72014-12-25 02:52:23 +02001582static int get_descriptor_ptr(struct x86_emulate_ctxt *ctxt,
1583 u16 selector, ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001584{
1585 struct desc_ptr dt;
1586 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001587 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001588
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001589 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001590
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001591 if (dt.size < index * 8 + 7)
1592 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001593
1594 addr = dt.address + index * 8;
Nadav Amitedccda72014-12-25 02:52:23 +02001595
1596#ifdef CONFIG_X86_64
1597 if (addr >> 32 != 0) {
1598 u64 efer = 0;
1599
1600 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1601 if (!(efer & EFER_LMA))
1602 addr &= (u32)-1;
1603 }
1604#endif
1605
1606 *desc_addr_p = addr;
1607 return X86EMUL_CONTINUE;
1608}
1609
1610/* allowed just for 8 bytes segments */
1611static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1612 u16 selector, struct desc_struct *desc,
1613 ulong *desc_addr_p)
1614{
1615 int rc;
1616
1617 rc = get_descriptor_ptr(ctxt, selector, desc_addr_p);
1618 if (rc != X86EMUL_CONTINUE)
1619 return rc;
1620
Paolo Bonzini79367a62018-06-06 16:43:02 +02001621 return linear_read_system(ctxt, *desc_addr_p, desc, sizeof(*desc));
Nadav Amitedccda72014-12-25 02:52:23 +02001622}
1623
1624/* allowed just for 8 bytes segments */
1625static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1626 u16 selector, struct desc_struct *desc)
1627{
1628 int rc;
1629 ulong addr;
1630
1631 rc = get_descriptor_ptr(ctxt, selector, &addr);
1632 if (rc != X86EMUL_CONTINUE)
1633 return rc;
1634
Jordan Borgner0e96f312018-10-28 12:58:28 +00001635 return linear_write_system(ctxt, addr, desc, sizeof(*desc));
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001636}
1637
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001638static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Nadav Amitd1442d82014-09-18 22:39:39 +03001639 u16 selector, int seg, u8 cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001640 enum x86_transfer_type transfer,
Nadav Amitd1442d82014-09-18 22:39:39 +03001641 struct desc_struct *desc)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001642{
Avi Kivity869be992012-06-13 16:30:53 +03001643 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001644 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001645 unsigned err_vec = GP_VECTOR;
1646 u32 err_code = 0;
1647 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001648 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001649 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001650 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001651 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001652
Jordan Borgner0e96f312018-10-28 12:58:28 +00001653 memset(&seg_desc, 0, sizeof(seg_desc));
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001654
Kevin Wolff8da94e2013-04-11 14:06:03 +02001655 if (ctxt->mode == X86EMUL_MODE_REAL) {
1656 /* set real mode segment descriptor (keep limit etc. for
1657 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001658 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001659 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001660 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001661 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1662 /* VM86 needs a clean new segment descriptor */
1663 set_desc_base(&seg_desc, selector << 4);
1664 set_desc_limit(&seg_desc, 0xffff);
1665 seg_desc.type = 3;
1666 seg_desc.p = 1;
1667 seg_desc.s = 1;
1668 seg_desc.dpl = 3;
1669 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001670 }
1671
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001672 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001673
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001674 /* TR should be in GDT only */
1675 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1676 goto exception;
1677
Paolo Bonzini33ab9112017-01-12 15:02:32 +01001678 /* NULL selector is not valid for TR, CS and (except for long mode) SS */
1679 if (null_selector) {
1680 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_TR)
1681 goto exception;
1682
1683 if (seg == VCPU_SREG_SS) {
1684 if (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl)
1685 goto exception;
1686
1687 /*
1688 * ctxt->ops->set_segment expects the CPL to be in
1689 * SS.DPL, so fake an expand-up 32-bit data segment.
1690 */
1691 seg_desc.type = 3;
1692 seg_desc.p = 1;
1693 seg_desc.s = 1;
1694 seg_desc.dpl = cpl;
1695 seg_desc.d = 1;
1696 seg_desc.g = 1;
1697 }
1698
1699 /* Skip all following checks */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001700 goto load;
Paolo Bonzini33ab9112017-01-12 15:02:32 +01001701 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001702
Avi Kivitye9194642012-06-13 16:29:39 +03001703 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001704 if (ret != X86EMUL_CONTINUE)
1705 return ret;
1706
1707 err_code = selector & 0xfffc;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001708 err_vec = (transfer == X86_TRANSFER_TASK_SWITCH) ? TS_VECTOR :
1709 GP_VECTOR;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001710
Guo Chaofc058682012-06-28 15:19:51 +08001711 /* can't load system descriptor into segment selector */
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001712 if (seg <= VCPU_SREG_GS && !seg_desc.s) {
1713 if (transfer == X86_TRANSFER_CALL_JMP)
1714 return X86EMUL_UNHANDLEABLE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001715 goto exception;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001716 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001717
1718 if (!seg_desc.p) {
1719 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1720 goto exception;
1721 }
1722
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001723 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001724
1725 switch (seg) {
1726 case VCPU_SREG_SS:
1727 /*
1728 * segment is not a writable data segment or segment
1729 * selector's RPL != CPL or segment selector's RPL != CPL
1730 */
1731 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1732 goto exception;
1733 break;
1734 case VCPU_SREG_CS:
1735 if (!(seg_desc.type & 8))
1736 goto exception;
1737
1738 if (seg_desc.type & 4) {
1739 /* conforming */
1740 if (dpl > cpl)
1741 goto exception;
1742 } else {
1743 /* nonconforming */
1744 if (rpl > cpl || dpl != cpl)
1745 goto exception;
1746 }
Nadav Amit040c8dc2014-09-18 22:39:43 +03001747 /* in long-mode d/b must be clear if l is set */
1748 if (seg_desc.d && seg_desc.l) {
1749 u64 efer = 0;
1750
1751 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1752 if (efer & EFER_LMA)
1753 goto exception;
1754 }
1755
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001756 /* CS(RPL) <- CPL */
1757 selector = (selector & 0xfffc) | cpl;
1758 break;
1759 case VCPU_SREG_TR:
1760 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1761 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001762 old_desc = seg_desc;
1763 seg_desc.type |= 2; /* busy */
1764 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1765 sizeof(seg_desc), &ctxt->exception);
1766 if (ret != X86EMUL_CONTINUE)
1767 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001768 break;
1769 case VCPU_SREG_LDTR:
1770 if (seg_desc.s || seg_desc.type != 2)
1771 goto exception;
1772 break;
1773 default: /* DS, ES, FS, or GS */
1774 /*
1775 * segment is not a data or readable code segment or
1776 * ((segment is a data or nonconforming code segment)
1777 * and (both RPL and CPL > DPL))
1778 */
1779 if ((seg_desc.type & 0xa) == 0x8 ||
1780 (((seg_desc.type & 0xc) != 0xc) &&
1781 (rpl > dpl && cpl > dpl)))
1782 goto exception;
1783 break;
1784 }
1785
1786 if (seg_desc.s) {
1787 /* mark segment as accessed */
Nadav Amite2cefa72014-12-25 02:52:22 +02001788 if (!(seg_desc.type & 1)) {
1789 seg_desc.type |= 1;
1790 ret = write_segment_descriptor(ctxt, selector,
1791 &seg_desc);
1792 if (ret != X86EMUL_CONTINUE)
1793 return ret;
1794 }
Nadav Amite37a75a2014-06-02 18:34:04 +03001795 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
Paolo Bonzini79367a62018-06-06 16:43:02 +02001796 ret = linear_read_system(ctxt, desc_addr+8, &base3, sizeof(base3));
Nadav Amite37a75a2014-06-02 18:34:04 +03001797 if (ret != X86EMUL_CONTINUE)
1798 return ret;
Yu Zhangfd8cb432017-08-24 20:27:56 +08001799 if (emul_is_noncanonical_address(get_desc_base(&seg_desc) |
1800 ((u64)base3 << 32), ctxt))
Nadav Amit9a9abf62014-11-02 11:54:56 +02001801 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001802 }
1803load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001804 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Nadav Amitd1442d82014-09-18 22:39:39 +03001805 if (desc)
1806 *desc = seg_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001807 return X86EMUL_CONTINUE;
1808exception:
Paolo Bonzini592f0852014-08-20 10:05:08 +02001809 return emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001810}
1811
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001812static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1813 u16 selector, int seg)
1814{
1815 u8 cpl = ctxt->ops->cpl(ctxt);
Paolo Bonzini33ab9112017-01-12 15:02:32 +01001816
1817 /*
1818 * None of MOV, POP and LSS can load a NULL selector in CPL=3, but
1819 * they can load it at CPL<3 (Intel's manual says only LSS can,
1820 * but it's wrong).
1821 *
1822 * However, the Intel manual says that putting IST=1/DPL=3 in
1823 * an interrupt gate will result in SS=3 (the AMD manual instead
1824 * says it doesn't), so allow SS=3 in __load_segment_descriptor
1825 * and only forbid it here.
1826 */
1827 if (seg == VCPU_SREG_SS && selector == 3 &&
1828 ctxt->mode == X86EMUL_MODE_PROT64)
1829 return emulate_exception(ctxt, GP_VECTOR, 0, true);
1830
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001831 return __load_segment_descriptor(ctxt, selector, seg, cpl,
1832 X86_TRANSFER_NONE, NULL);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001833}
1834
Wei Yongjun31be40b2010-08-17 09:17:30 +08001835static void write_register_operand(struct operand *op)
1836{
Nadav Amit6fd8e122015-03-30 15:39:20 +03001837 return assign_register(op->addr.reg, op->val, op->bytes);
Wei Yongjun31be40b2010-08-17 09:17:30 +08001838}
1839
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001840static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001841{
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001842 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001843 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001844 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001845 break;
1846 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001847 if (ctxt->lock_prefix)
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001848 return segmented_cmpxchg(ctxt,
1849 op->addr.mem,
1850 &op->orig_val,
1851 &op->val,
1852 op->bytes);
1853 else
1854 return segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001855 op->addr.mem,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001856 &op->val,
1857 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001858 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001859 case OP_MEM_STR:
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001860 return segmented_write(ctxt,
1861 op->addr.mem,
1862 op->data,
1863 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001864 break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001865 case OP_XMM:
Sean Christophersonc0a21c32020-01-17 11:30:52 -08001866 write_sse_reg(&op->vec_val, op->addr.xmm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001867 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001868 case OP_MM:
Sean Christophersonc0a21c32020-01-17 11:30:52 -08001869 write_mmx_reg(&op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001870 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001871 case OP_NONE:
1872 /* no writeback */
1873 break;
1874 default:
1875 break;
1876 }
1877 return X86EMUL_CONTINUE;
1878}
1879
Avi Kivity51ddff52012-06-12 20:19:40 +03001880static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001881{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001882 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001883
Avi Kivity5ad105e2012-08-19 14:34:31 +03001884 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001885 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001886 addr.seg = VCPU_SREG_SS;
1887
Avi Kivity51ddff52012-06-12 20:19:40 +03001888 return segmented_write(ctxt, addr, data, bytes);
1889}
1890
1891static int em_push(struct x86_emulate_ctxt *ctxt)
1892{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001893 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001894 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001895 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001896}
1897
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001898static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001899 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001900{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001901 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001902 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001903
Avi Kivitydd856ef2012-08-27 23:46:17 +03001904 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001905 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001906 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001907 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001908 return rc;
1909
Avi Kivity5ad105e2012-08-19 14:34:31 +03001910 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001911 return rc;
1912}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001913
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001914static int em_pop(struct x86_emulate_ctxt *ctxt)
1915{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001916 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001917}
1918
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001919static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001920 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001921{
1922 int rc;
1923 unsigned long val, change_mask;
Nadav Amit0efb0442015-03-29 16:33:03 +03001924 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001925 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001926
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001927 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001928 if (rc != X86EMUL_CONTINUE)
1929 return rc;
1930
Nadav Amit0efb0442015-03-29 16:33:03 +03001931 change_mask = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
1932 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF |
1933 X86_EFLAGS_TF | X86_EFLAGS_DF | X86_EFLAGS_NT |
1934 X86_EFLAGS_AC | X86_EFLAGS_ID;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001935
1936 switch(ctxt->mode) {
1937 case X86EMUL_MODE_PROT64:
1938 case X86EMUL_MODE_PROT32:
1939 case X86EMUL_MODE_PROT16:
1940 if (cpl == 0)
Nadav Amit0efb0442015-03-29 16:33:03 +03001941 change_mask |= X86_EFLAGS_IOPL;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001942 if (cpl <= iopl)
Nadav Amit0efb0442015-03-29 16:33:03 +03001943 change_mask |= X86_EFLAGS_IF;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001944 break;
1945 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001946 if (iopl < 3)
1947 return emulate_gp(ctxt, 0);
Nadav Amit0efb0442015-03-29 16:33:03 +03001948 change_mask |= X86_EFLAGS_IF;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001949 break;
1950 default: /* real mode */
Nadav Amit0efb0442015-03-29 16:33:03 +03001951 change_mask |= (X86_EFLAGS_IOPL | X86_EFLAGS_IF);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001952 break;
1953 }
1954
1955 *(unsigned long *)dest =
1956 (ctxt->eflags & ~change_mask) | (val & change_mask);
1957
1958 return rc;
1959}
1960
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001961static int em_popf(struct x86_emulate_ctxt *ctxt)
1962{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001963 ctxt->dst.type = OP_REG;
1964 ctxt->dst.addr.reg = &ctxt->eflags;
1965 ctxt->dst.bytes = ctxt->op_bytes;
1966 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001967}
1968
Avi Kivity612e89f2012-06-12 20:03:23 +03001969static int em_enter(struct x86_emulate_ctxt *ctxt)
1970{
1971 int rc;
1972 unsigned frame_size = ctxt->src.val;
1973 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001974 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001975
1976 if (nesting_level)
1977 return X86EMUL_UNHANDLEABLE;
1978
Avi Kivitydd856ef2012-08-27 23:46:17 +03001979 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1980 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001981 if (rc != X86EMUL_CONTINUE)
1982 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001983 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001984 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001985 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1986 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001987 stack_mask(ctxt));
1988 return X86EMUL_CONTINUE;
1989}
1990
Avi Kivityf47cfa32012-06-07 17:49:24 +03001991static int em_leave(struct x86_emulate_ctxt *ctxt)
1992{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001993 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001994 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001995 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001996}
1997
Avi Kivity1cd196e2011-09-13 10:45:51 +03001998static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001999{
Avi Kivity1cd196e2011-09-13 10:45:51 +03002000 int seg = ctxt->src2.val;
2001
Avi Kivity9dac77f2011-06-01 15:34:25 +03002002 ctxt->src.val = get_segment_selector(ctxt, seg);
Nadav Amit0fcc2072014-11-02 11:54:51 +02002003 if (ctxt->op_bytes == 4) {
2004 rsp_increment(ctxt, -2);
2005 ctxt->op_bytes = 2;
2006 }
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002007
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002008 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002009}
2010
Avi Kivity1cd196e2011-09-13 10:45:51 +03002011static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002012{
Avi Kivity1cd196e2011-09-13 10:45:51 +03002013 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002014 unsigned long selector;
2015 int rc;
2016
Nadav Amit3313bc42014-12-25 02:52:17 +02002017 rc = emulate_pop(ctxt, &selector, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002018 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002019 return rc;
2020
Paolo Bonzinia5457e72014-06-05 17:29:34 +02002021 if (ctxt->modrm_reg == VCPU_SREG_SS)
2022 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Nadav Amit3313bc42014-12-25 02:52:17 +02002023 if (ctxt->op_bytes > 2)
2024 rsp_increment(ctxt, ctxt->op_bytes - 2);
Paolo Bonzinia5457e72014-06-05 17:29:34 +02002025
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002026 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002027 return rc;
2028}
2029
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09002030static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002031{
Avi Kivitydd856ef2012-08-27 23:46:17 +03002032 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08002033 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002034 int reg = VCPU_REGS_RAX;
2035
2036 while (reg <= VCPU_REGS_RDI) {
2037 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03002038 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002039
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002040 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08002041 if (rc != X86EMUL_CONTINUE)
2042 return rc;
2043
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002044 ++reg;
2045 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08002046
Wei Yongjunc37eda12010-06-15 09:03:33 +08002047 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002048}
2049
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09002050static int em_pushf(struct x86_emulate_ctxt *ctxt)
2051{
Nadav Amit0efb0442015-03-29 16:33:03 +03002052 ctxt->src.val = (unsigned long)ctxt->eflags & ~X86_EFLAGS_VM;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09002053 return em_push(ctxt);
2054}
2055
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09002056static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002057{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002058 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002059 int reg = VCPU_REGS_RDI;
Nadav Amit6fd8e122015-03-30 15:39:20 +03002060 u32 val;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002061
2062 while (reg >= VCPU_REGS_RAX) {
2063 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03002064 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002065 --reg;
2066 }
2067
Nadav Amit6fd8e122015-03-30 15:39:20 +03002068 rc = emulate_pop(ctxt, &val, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002069 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002070 break;
Nadav Amit6fd8e122015-03-30 15:39:20 +03002071 assign_register(reg_rmw(ctxt, reg), val, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002072 --reg;
2073 }
2074 return rc;
2075}
2076
Avi Kivitydd856ef2012-08-27 23:46:17 +03002077static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002078{
Mathias Krause0225fb52012-08-30 01:30:16 +02002079 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03002080 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002081 struct desc_ptr dt;
2082 gva_t cs_addr;
2083 gva_t eip_addr;
2084 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002085
2086 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002087 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002088 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03002089 if (rc != X86EMUL_CONTINUE)
2090 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002091
Nadav Amit0efb0442015-03-29 16:33:03 +03002092 ctxt->eflags &= ~(X86_EFLAGS_IF | X86_EFLAGS_TF | X86_EFLAGS_AC);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002093
Avi Kivity9dac77f2011-06-01 15:34:25 +03002094 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002095 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03002096 if (rc != X86EMUL_CONTINUE)
2097 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002098
Avi Kivity9dac77f2011-06-01 15:34:25 +03002099 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002100 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03002101 if (rc != X86EMUL_CONTINUE)
2102 return rc;
2103
Avi Kivity4bff1e862011-04-20 13:37:53 +03002104 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002105
2106 eip_addr = dt.address + (irq << 2);
2107 cs_addr = dt.address + (irq << 2) + 2;
2108
Paolo Bonzini79367a62018-06-06 16:43:02 +02002109 rc = linear_read_system(ctxt, cs_addr, &cs, 2);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002110 if (rc != X86EMUL_CONTINUE)
2111 return rc;
2112
Paolo Bonzini79367a62018-06-06 16:43:02 +02002113 rc = linear_read_system(ctxt, eip_addr, &eip, 2);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002114 if (rc != X86EMUL_CONTINUE)
2115 return rc;
2116
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002117 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002118 if (rc != X86EMUL_CONTINUE)
2119 return rc;
2120
Avi Kivity9dac77f2011-06-01 15:34:25 +03002121 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002122
2123 return rc;
2124}
2125
Avi Kivitydd856ef2012-08-27 23:46:17 +03002126int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
2127{
2128 int rc;
2129
2130 invalidate_registers(ctxt);
2131 rc = __emulate_int_real(ctxt, irq);
2132 if (rc == X86EMUL_CONTINUE)
2133 writeback_registers(ctxt);
2134 return rc;
2135}
2136
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002137static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002138{
2139 switch(ctxt->mode) {
2140 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03002141 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002142 case X86EMUL_MODE_VM86:
2143 case X86EMUL_MODE_PROT16:
2144 case X86EMUL_MODE_PROT32:
2145 case X86EMUL_MODE_PROT64:
2146 default:
2147 /* Protected mode interrupts unimplemented yet */
2148 return X86EMUL_UNHANDLEABLE;
2149 }
2150}
2151
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002152static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002153{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002154 int rc = X86EMUL_CONTINUE;
2155 unsigned long temp_eip = 0;
2156 unsigned long temp_eflags = 0;
2157 unsigned long cs = 0;
Nadav Amit0efb0442015-03-29 16:33:03 +03002158 unsigned long mask = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
2159 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_TF |
2160 X86_EFLAGS_IF | X86_EFLAGS_DF | X86_EFLAGS_OF |
2161 X86_EFLAGS_IOPL | X86_EFLAGS_NT | X86_EFLAGS_RF |
2162 X86_EFLAGS_AC | X86_EFLAGS_ID |
Wanpeng Li35fd68a2015-04-08 14:08:14 +08002163 X86_EFLAGS_FIXED;
Nadav Amit0efb0442015-03-29 16:33:03 +03002164 unsigned long vm86_mask = X86_EFLAGS_VM | X86_EFLAGS_VIF |
2165 X86_EFLAGS_VIP;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002166
2167 /* TODO: Add stack limit check */
2168
Avi Kivity9dac77f2011-06-01 15:34:25 +03002169 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002170
2171 if (rc != X86EMUL_CONTINUE)
2172 return rc;
2173
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002174 if (temp_eip & ~0xffff)
2175 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002176
Avi Kivity9dac77f2011-06-01 15:34:25 +03002177 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002178
2179 if (rc != X86EMUL_CONTINUE)
2180 return rc;
2181
Avi Kivity9dac77f2011-06-01 15:34:25 +03002182 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002183
2184 if (rc != X86EMUL_CONTINUE)
2185 return rc;
2186
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002187 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002188
2189 if (rc != X86EMUL_CONTINUE)
2190 return rc;
2191
Avi Kivity9dac77f2011-06-01 15:34:25 +03002192 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002193
Avi Kivity9dac77f2011-06-01 15:34:25 +03002194 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002195 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03002196 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002197 ctxt->eflags &= ~0xffff;
2198 ctxt->eflags |= temp_eflags;
2199 }
2200
2201 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
Wanpeng Li35fd68a2015-04-08 14:08:14 +08002202 ctxt->eflags |= X86_EFLAGS_FIXED;
Nadav Amit801806d2015-01-26 09:32:23 +02002203 ctxt->ops->set_nmi_mask(ctxt, false);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002204
2205 return rc;
2206}
2207
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002208static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002209{
2210 switch(ctxt->mode) {
2211 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002212 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002213 case X86EMUL_MODE_VM86:
2214 case X86EMUL_MODE_PROT16:
2215 case X86EMUL_MODE_PROT32:
2216 case X86EMUL_MODE_PROT64:
2217 default:
2218 /* iret from protected mode unimplemented yet */
2219 return X86EMUL_UNHANDLEABLE;
2220 }
2221}
2222
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002223static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2224{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002225 int rc;
Radim Krčmář2117d532016-11-23 21:15:00 +01002226 unsigned short sel;
2227 struct desc_struct new_desc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002228 u8 cpl = ctxt->ops->cpl(ctxt);
2229
Avi Kivity9dac77f2011-06-01 15:34:25 +03002230 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002231
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002232 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
2233 X86_TRANSFER_CALL_JMP,
Nadav Amitd1442d82014-09-18 22:39:39 +03002234 &new_desc);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002235 if (rc != X86EMUL_CONTINUE)
2236 return rc;
2237
Nadav Amitd50eaa12014-11-19 17:43:11 +02002238 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Radim Krčmář2117d532016-11-23 21:15:00 +01002239 /* Error handling is not implemented. */
2240 if (rc != X86EMUL_CONTINUE)
2241 return X86EMUL_UNHANDLEABLE;
2242
Nadav Amitd1442d82014-09-18 22:39:39 +03002243 return rc;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002244}
2245
Nadav Amitf7784042014-09-18 22:39:41 +03002246static int em_jmp_abs(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002247{
Nadav Amitf7784042014-09-18 22:39:41 +03002248 return assign_eip_near(ctxt, ctxt->src.val);
2249}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002250
Nadav Amitf7784042014-09-18 22:39:41 +03002251static int em_call_near_abs(struct x86_emulate_ctxt *ctxt)
2252{
2253 int rc;
2254 long int old_eip;
2255
2256 old_eip = ctxt->_eip;
2257 rc = assign_eip_near(ctxt, ctxt->src.val);
2258 if (rc != X86EMUL_CONTINUE)
2259 return rc;
2260 ctxt->src.val = old_eip;
2261 rc = em_push(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002262 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002263}
2264
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002265static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002266{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002267 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002268
Nadav Amitaaa05f22014-06-02 18:34:10 +03002269 if (ctxt->dst.bytes == 16)
2270 return X86EMUL_UNHANDLEABLE;
2271
Avi Kivitydd856ef2012-08-27 23:46:17 +03002272 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2273 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2274 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2275 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Nadav Amit0efb0442015-03-29 16:33:03 +03002276 ctxt->eflags &= ~X86_EFLAGS_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002277 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002278 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2279 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002280
Nadav Amit0efb0442015-03-29 16:33:03 +03002281 ctxt->eflags |= X86_EFLAGS_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002282 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002283 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002284}
2285
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002286static int em_ret(struct x86_emulate_ctxt *ctxt)
2287{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002288 int rc;
2289 unsigned long eip;
2290
2291 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
2292 if (rc != X86EMUL_CONTINUE)
2293 return rc;
2294
2295 return assign_eip_near(ctxt, eip);
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002296}
2297
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002298static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002299{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002300 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002301 unsigned long eip, cs;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002302 int cpl = ctxt->ops->cpl(ctxt);
Radim Krčmář2117d532016-11-23 21:15:00 +01002303 struct desc_struct new_desc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002304
2305 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002306 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002307 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002308 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002309 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002310 return rc;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002311 /* Outer-privilege level return is not implemented */
2312 if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2313 return X86EMUL_UNHANDLEABLE;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002314 rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, cpl,
2315 X86_TRANSFER_RET,
Nadav Amitd1442d82014-09-18 22:39:39 +03002316 &new_desc);
2317 if (rc != X86EMUL_CONTINUE)
2318 return rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +02002319 rc = assign_eip_far(ctxt, eip, &new_desc);
Radim Krčmář2117d532016-11-23 21:15:00 +01002320 /* Error handling is not implemented. */
2321 if (rc != X86EMUL_CONTINUE)
2322 return X86EMUL_UNHANDLEABLE;
2323
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002324 return rc;
2325}
2326
Bruce Rogers32611072013-09-09 09:40:20 -06002327static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2328{
2329 int rc;
2330
2331 rc = em_ret_far(ctxt);
2332 if (rc != X86EMUL_CONTINUE)
2333 return rc;
2334 rsp_increment(ctxt, ctxt->src.val);
2335 return X86EMUL_CONTINUE;
2336}
2337
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002338static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2339{
2340 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002341 ctxt->dst.orig_val = ctxt->dst.val;
2342 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002343 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002344 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002345 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002346
Nadav Amit0efb0442015-03-29 16:33:03 +03002347 if (ctxt->eflags & X86_EFLAGS_ZF) {
Nadav Amit2fcf5c82015-01-26 09:32:21 +02002348 /* Success: write back to memory; no update of EAX */
2349 ctxt->src.type = OP_NONE;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002350 ctxt->dst.val = ctxt->src.orig_val;
2351 } else {
2352 /* Failure: write the value we saw to EAX. */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02002353 ctxt->src.type = OP_REG;
2354 ctxt->src.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
2355 ctxt->src.val = ctxt->dst.orig_val;
2356 /* Create write-cycle to dest by writing the same value */
Nadav Amit37c564f2014-06-02 18:34:07 +03002357 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002358 }
2359 return X86EMUL_CONTINUE;
2360}
2361
Avi Kivityd4b43252011-09-13 10:45:50 +03002362static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002363{
Avi Kivityd4b43252011-09-13 10:45:50 +03002364 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002365 unsigned short sel;
2366 int rc;
2367
Avi Kivity9dac77f2011-06-01 15:34:25 +03002368 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002369
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002370 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002371 if (rc != X86EMUL_CONTINUE)
2372 return rc;
2373
Avi Kivity9dac77f2011-06-01 15:34:25 +03002374 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002375 return rc;
2376}
2377
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002378static int emulator_has_longmode(struct x86_emulate_ctxt *ctxt)
2379{
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002380#ifdef CONFIG_X86_64
Sean Christopherson5ae78e92019-12-17 13:32:38 -08002381 return ctxt->ops->guest_has_long_mode(ctxt);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002382#else
2383 return false;
2384#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002385}
2386
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002387static void rsm_set_desc_flags(struct desc_struct *desc, u32 flags)
2388{
2389 desc->g = (flags >> 23) & 1;
2390 desc->d = (flags >> 22) & 1;
2391 desc->l = (flags >> 21) & 1;
2392 desc->avl = (flags >> 20) & 1;
2393 desc->p = (flags >> 15) & 1;
2394 desc->dpl = (flags >> 13) & 3;
2395 desc->s = (flags >> 12) & 1;
2396 desc->type = (flags >> 8) & 15;
2397}
2398
Sean Christophersoned193212019-04-02 08:03:09 -07002399static int rsm_load_seg_32(struct x86_emulate_ctxt *ctxt, const char *smstate,
2400 int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002401{
2402 struct desc_struct desc;
2403 int offset;
2404 u16 selector;
2405
Sean Christophersoned193212019-04-02 08:03:09 -07002406 selector = GET_SMSTATE(u32, smstate, 0x7fa8 + n * 4);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002407
2408 if (n < 3)
2409 offset = 0x7f84 + n * 12;
2410 else
2411 offset = 0x7f2c + (n - 3) * 12;
2412
Sean Christophersoned193212019-04-02 08:03:09 -07002413 set_desc_base(&desc, GET_SMSTATE(u32, smstate, offset + 8));
2414 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, offset + 4));
2415 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smstate, offset));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002416 ctxt->ops->set_segment(ctxt, selector, &desc, 0, n);
2417 return X86EMUL_CONTINUE;
2418}
2419
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002420#ifdef CONFIG_X86_64
Sean Christophersoned193212019-04-02 08:03:09 -07002421static int rsm_load_seg_64(struct x86_emulate_ctxt *ctxt, const char *smstate,
2422 int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002423{
2424 struct desc_struct desc;
2425 int offset;
2426 u16 selector;
2427 u32 base3;
2428
2429 offset = 0x7e00 + n * 16;
2430
Sean Christophersoned193212019-04-02 08:03:09 -07002431 selector = GET_SMSTATE(u16, smstate, offset);
2432 rsm_set_desc_flags(&desc, GET_SMSTATE(u16, smstate, offset + 2) << 8);
2433 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, offset + 4));
2434 set_desc_base(&desc, GET_SMSTATE(u32, smstate, offset + 8));
2435 base3 = GET_SMSTATE(u32, smstate, offset + 12);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002436
2437 ctxt->ops->set_segment(ctxt, selector, &desc, base3, n);
2438 return X86EMUL_CONTINUE;
2439}
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002440#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002441
2442static int rsm_enter_protected_mode(struct x86_emulate_ctxt *ctxt,
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002443 u64 cr0, u64 cr3, u64 cr4)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002444{
2445 int bad;
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002446 u64 pcid;
2447
2448 /* In order to later set CR4.PCIDE, CR3[11:0] must be zero. */
2449 pcid = 0;
2450 if (cr4 & X86_CR4_PCIDE) {
2451 pcid = cr3 & 0xfff;
2452 cr3 &= ~0xfff;
2453 }
2454
2455 bad = ctxt->ops->set_cr(ctxt, 3, cr3);
2456 if (bad)
2457 return X86EMUL_UNHANDLEABLE;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002458
2459 /*
2460 * First enable PAE, long mode needs it before CR0.PG = 1 is set.
2461 * Then enable protected mode. However, PCID cannot be enabled
2462 * if EFER.LMA=0, so set it separately.
2463 */
2464 bad = ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PCIDE);
2465 if (bad)
2466 return X86EMUL_UNHANDLEABLE;
2467
2468 bad = ctxt->ops->set_cr(ctxt, 0, cr0);
2469 if (bad)
2470 return X86EMUL_UNHANDLEABLE;
2471
2472 if (cr4 & X86_CR4_PCIDE) {
2473 bad = ctxt->ops->set_cr(ctxt, 4, cr4);
2474 if (bad)
2475 return X86EMUL_UNHANDLEABLE;
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002476 if (pcid) {
2477 bad = ctxt->ops->set_cr(ctxt, 3, cr3 | pcid);
2478 if (bad)
2479 return X86EMUL_UNHANDLEABLE;
2480 }
2481
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002482 }
2483
2484 return X86EMUL_CONTINUE;
2485}
2486
Sean Christophersoned193212019-04-02 08:03:09 -07002487static int rsm_load_state_32(struct x86_emulate_ctxt *ctxt,
2488 const char *smstate)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002489{
2490 struct desc_struct desc;
2491 struct desc_ptr dt;
2492 u16 selector;
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002493 u32 val, cr0, cr3, cr4;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002494 int i;
2495
Sean Christophersoned193212019-04-02 08:03:09 -07002496 cr0 = GET_SMSTATE(u32, smstate, 0x7ffc);
2497 cr3 = GET_SMSTATE(u32, smstate, 0x7ff8);
2498 ctxt->eflags = GET_SMSTATE(u32, smstate, 0x7ff4) | X86_EFLAGS_FIXED;
2499 ctxt->_eip = GET_SMSTATE(u32, smstate, 0x7ff0);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002500
2501 for (i = 0; i < 8; i++)
Sean Christophersoned193212019-04-02 08:03:09 -07002502 *reg_write(ctxt, i) = GET_SMSTATE(u32, smstate, 0x7fd0 + i * 4);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002503
Sean Christophersoned193212019-04-02 08:03:09 -07002504 val = GET_SMSTATE(u32, smstate, 0x7fcc);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002505 ctxt->ops->set_dr(ctxt, 6, (val & DR6_VOLATILE) | DR6_FIXED_1);
Sean Christophersoned193212019-04-02 08:03:09 -07002506 val = GET_SMSTATE(u32, smstate, 0x7fc8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002507 ctxt->ops->set_dr(ctxt, 7, (val & DR7_VOLATILE) | DR7_FIXED_1);
2508
Sean Christophersoned193212019-04-02 08:03:09 -07002509 selector = GET_SMSTATE(u32, smstate, 0x7fc4);
2510 set_desc_base(&desc, GET_SMSTATE(u32, smstate, 0x7f64));
2511 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, 0x7f60));
2512 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smstate, 0x7f5c));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002513 ctxt->ops->set_segment(ctxt, selector, &desc, 0, VCPU_SREG_TR);
2514
Sean Christophersoned193212019-04-02 08:03:09 -07002515 selector = GET_SMSTATE(u32, smstate, 0x7fc0);
2516 set_desc_base(&desc, GET_SMSTATE(u32, smstate, 0x7f80));
2517 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, 0x7f7c));
2518 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smstate, 0x7f78));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002519 ctxt->ops->set_segment(ctxt, selector, &desc, 0, VCPU_SREG_LDTR);
2520
Sean Christophersoned193212019-04-02 08:03:09 -07002521 dt.address = GET_SMSTATE(u32, smstate, 0x7f74);
2522 dt.size = GET_SMSTATE(u32, smstate, 0x7f70);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002523 ctxt->ops->set_gdt(ctxt, &dt);
2524
Sean Christophersoned193212019-04-02 08:03:09 -07002525 dt.address = GET_SMSTATE(u32, smstate, 0x7f58);
2526 dt.size = GET_SMSTATE(u32, smstate, 0x7f54);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002527 ctxt->ops->set_idt(ctxt, &dt);
2528
2529 for (i = 0; i < 6; i++) {
Sean Christophersoned193212019-04-02 08:03:09 -07002530 int r = rsm_load_seg_32(ctxt, smstate, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002531 if (r != X86EMUL_CONTINUE)
2532 return r;
2533 }
2534
Sean Christophersoned193212019-04-02 08:03:09 -07002535 cr4 = GET_SMSTATE(u32, smstate, 0x7f14);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002536
Sean Christophersoned193212019-04-02 08:03:09 -07002537 ctxt->ops->set_smbase(ctxt, GET_SMSTATE(u32, smstate, 0x7ef8));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002538
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002539 return rsm_enter_protected_mode(ctxt, cr0, cr3, cr4);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002540}
2541
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002542#ifdef CONFIG_X86_64
Sean Christophersoned193212019-04-02 08:03:09 -07002543static int rsm_load_state_64(struct x86_emulate_ctxt *ctxt,
2544 const char *smstate)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002545{
2546 struct desc_struct desc;
2547 struct desc_ptr dt;
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002548 u64 val, cr0, cr3, cr4;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002549 u32 base3;
2550 u16 selector;
Paolo Bonzinib10d92a2015-10-14 15:25:52 +02002551 int i, r;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002552
2553 for (i = 0; i < 16; i++)
Sean Christophersoned193212019-04-02 08:03:09 -07002554 *reg_write(ctxt, i) = GET_SMSTATE(u64, smstate, 0x7ff8 - i * 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002555
Sean Christophersoned193212019-04-02 08:03:09 -07002556 ctxt->_eip = GET_SMSTATE(u64, smstate, 0x7f78);
2557 ctxt->eflags = GET_SMSTATE(u32, smstate, 0x7f70) | X86_EFLAGS_FIXED;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002558
Sean Christophersoned193212019-04-02 08:03:09 -07002559 val = GET_SMSTATE(u32, smstate, 0x7f68);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002560 ctxt->ops->set_dr(ctxt, 6, (val & DR6_VOLATILE) | DR6_FIXED_1);
Sean Christophersoned193212019-04-02 08:03:09 -07002561 val = GET_SMSTATE(u32, smstate, 0x7f60);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002562 ctxt->ops->set_dr(ctxt, 7, (val & DR7_VOLATILE) | DR7_FIXED_1);
2563
Sean Christophersoned193212019-04-02 08:03:09 -07002564 cr0 = GET_SMSTATE(u64, smstate, 0x7f58);
2565 cr3 = GET_SMSTATE(u64, smstate, 0x7f50);
2566 cr4 = GET_SMSTATE(u64, smstate, 0x7f48);
2567 ctxt->ops->set_smbase(ctxt, GET_SMSTATE(u32, smstate, 0x7f00));
2568 val = GET_SMSTATE(u64, smstate, 0x7ed0);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002569 ctxt->ops->set_msr(ctxt, MSR_EFER, val & ~EFER_LMA);
2570
Sean Christophersoned193212019-04-02 08:03:09 -07002571 selector = GET_SMSTATE(u32, smstate, 0x7e90);
2572 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smstate, 0x7e92) << 8);
2573 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, 0x7e94));
2574 set_desc_base(&desc, GET_SMSTATE(u32, smstate, 0x7e98));
2575 base3 = GET_SMSTATE(u32, smstate, 0x7e9c);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002576 ctxt->ops->set_segment(ctxt, selector, &desc, base3, VCPU_SREG_TR);
2577
Sean Christophersoned193212019-04-02 08:03:09 -07002578 dt.size = GET_SMSTATE(u32, smstate, 0x7e84);
2579 dt.address = GET_SMSTATE(u64, smstate, 0x7e88);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002580 ctxt->ops->set_idt(ctxt, &dt);
2581
Sean Christophersoned193212019-04-02 08:03:09 -07002582 selector = GET_SMSTATE(u32, smstate, 0x7e70);
2583 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smstate, 0x7e72) << 8);
2584 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, 0x7e74));
2585 set_desc_base(&desc, GET_SMSTATE(u32, smstate, 0x7e78));
2586 base3 = GET_SMSTATE(u32, smstate, 0x7e7c);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002587 ctxt->ops->set_segment(ctxt, selector, &desc, base3, VCPU_SREG_LDTR);
2588
Sean Christophersoned193212019-04-02 08:03:09 -07002589 dt.size = GET_SMSTATE(u32, smstate, 0x7e64);
2590 dt.address = GET_SMSTATE(u64, smstate, 0x7e68);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002591 ctxt->ops->set_gdt(ctxt, &dt);
2592
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002593 r = rsm_enter_protected_mode(ctxt, cr0, cr3, cr4);
Paolo Bonzinib10d92a2015-10-14 15:25:52 +02002594 if (r != X86EMUL_CONTINUE)
2595 return r;
2596
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002597 for (i = 0; i < 6; i++) {
Sean Christophersoned193212019-04-02 08:03:09 -07002598 r = rsm_load_seg_64(ctxt, smstate, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002599 if (r != X86EMUL_CONTINUE)
2600 return r;
2601 }
2602
Paolo Bonzinib10d92a2015-10-14 15:25:52 +02002603 return X86EMUL_CONTINUE;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002604}
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002605#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002606
Paolo Bonzini64d60672015-05-07 11:36:11 +02002607static int em_rsm(struct x86_emulate_ctxt *ctxt)
2608{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002609 unsigned long cr0, cr4, efer;
Sean Christophersoned193212019-04-02 08:03:09 -07002610 char buf[512];
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002611 u64 smbase;
2612 int ret;
2613
Ladi Prosek6ed071f2017-04-25 16:42:44 +02002614 if ((ctxt->ops->get_hflags(ctxt) & X86EMUL_SMM_MASK) == 0)
Paolo Bonzini64d60672015-05-07 11:36:11 +02002615 return emulate_ud(ctxt);
2616
Sean Christophersoned193212019-04-02 08:03:09 -07002617 smbase = ctxt->ops->get_smbase(ctxt);
2618
2619 ret = ctxt->ops->read_phys(ctxt, smbase + 0xfe00, buf, sizeof(buf));
2620 if (ret != X86EMUL_CONTINUE)
2621 return X86EMUL_UNHANDLEABLE;
2622
Sean Christopherson9ec19492019-04-02 08:03:11 -07002623 if ((ctxt->ops->get_hflags(ctxt) & X86EMUL_SMM_INSIDE_NMI_MASK) == 0)
2624 ctxt->ops->set_nmi_mask(ctxt, false);
2625
2626 ctxt->ops->set_hflags(ctxt, ctxt->ops->get_hflags(ctxt) &
2627 ~(X86EMUL_SMM_INSIDE_NMI_MASK | X86EMUL_SMM_MASK));
2628
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002629 /*
2630 * Get back to real mode, to prepare a safe state in which to load
Paolo Bonzini89651a32015-11-03 13:43:05 +01002631 * CR0/CR3/CR4/EFER. It's all a bit more complicated if the vCPU
2632 * supports long mode.
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002633 */
Paolo Bonzini89651a32015-11-03 13:43:05 +01002634 if (emulator_has_longmode(ctxt)) {
2635 struct desc_struct cs_desc;
2636
2637 /* Zero CR4.PCIDE before CR0.PG. */
Sean Christopherson8f4dc2e2019-04-02 08:10:47 -07002638 cr4 = ctxt->ops->get_cr(ctxt, 4);
2639 if (cr4 & X86_CR4_PCIDE)
Paolo Bonzini89651a32015-11-03 13:43:05 +01002640 ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PCIDE);
Paolo Bonzini89651a32015-11-03 13:43:05 +01002641
2642 /* A 32-bit code segment is required to clear EFER.LMA. */
2643 memset(&cs_desc, 0, sizeof(cs_desc));
2644 cs_desc.type = 0xb;
2645 cs_desc.s = cs_desc.g = cs_desc.p = 1;
2646 ctxt->ops->set_segment(ctxt, 0, &cs_desc, 0, VCPU_SREG_CS);
2647 }
2648
2649 /* For the 64-bit case, this will clear EFER.LMA. */
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002650 cr0 = ctxt->ops->get_cr(ctxt, 0);
2651 if (cr0 & X86_CR0_PE)
2652 ctxt->ops->set_cr(ctxt, 0, cr0 & ~(X86_CR0_PG | X86_CR0_PE));
Paolo Bonzini89651a32015-11-03 13:43:05 +01002653
Sean Christopherson8f4dc2e2019-04-02 08:10:47 -07002654 if (emulator_has_longmode(ctxt)) {
2655 /* Clear CR4.PAE before clearing EFER.LME. */
2656 cr4 = ctxt->ops->get_cr(ctxt, 4);
2657 if (cr4 & X86_CR4_PAE)
2658 ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PAE);
Paolo Bonzini89651a32015-11-03 13:43:05 +01002659
Sean Christopherson8f4dc2e2019-04-02 08:10:47 -07002660 /* And finally go back to 32-bit mode. */
2661 efer = 0;
2662 ctxt->ops->set_msr(ctxt, MSR_EFER, efer);
2663 }
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002664
Ladi Prosek0234bf82017-10-11 16:54:40 +02002665 /*
2666 * Give pre_leave_smm() a chance to make ISA-specific changes to the
2667 * vCPU state (e.g. enter guest mode) before loading state from the SMM
2668 * state-save area.
2669 */
Sean Christophersoned193212019-04-02 08:03:09 -07002670 if (ctxt->ops->pre_leave_smm(ctxt, buf))
Ladi Prosek0234bf82017-10-11 16:54:40 +02002671 return X86EMUL_UNHANDLEABLE;
2672
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002673#ifdef CONFIG_X86_64
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002674 if (emulator_has_longmode(ctxt))
Sean Christophersoned193212019-04-02 08:03:09 -07002675 ret = rsm_load_state_64(ctxt, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002676 else
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002677#endif
Sean Christophersoned193212019-04-02 08:03:09 -07002678 ret = rsm_load_state_32(ctxt, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002679
2680 if (ret != X86EMUL_CONTINUE) {
2681 /* FIXME: should triple fault */
2682 return X86EMUL_UNHANDLEABLE;
2683 }
2684
Sean Christophersonc5833c72019-04-02 08:03:10 -07002685 ctxt->ops->post_leave_smm(ctxt);
2686
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002687 return X86EMUL_CONTINUE;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002688}
2689
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002690static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002691setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002692 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002693{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002694 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002695 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002696 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002697 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002698 cs->type = 0x0b; /* Read, Execute, Accessed */
2699 cs->s = 1;
2700 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002701 cs->p = 1;
2702 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002703 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002704
Gleb Natapov79168fd2010-04-28 19:15:30 +03002705 set_desc_base(ss, 0); /* flat segment */
2706 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002707 ss->g = 1; /* 4kb granularity */
2708 ss->s = 1;
2709 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002710 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002711 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002712 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002713 ss->l = 0;
2714 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002715}
2716
Avi Kivity1a18a692012-02-01 12:23:21 +02002717static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2718{
2719 u32 eax, ebx, ecx, edx;
2720
2721 eax = ecx = 0;
Yu Zhange911eb32017-08-24 20:27:52 +08002722 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, false);
Avi Kivity0017f932012-06-07 14:10:16 +03002723 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002724 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2725 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2726}
2727
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002728static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2729{
Mathias Krause0225fb52012-08-30 01:30:16 +02002730 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002731 u32 eax, ebx, ecx, edx;
2732
2733 /*
2734 * syscall should always be enabled in longmode - so only become
2735 * vendor specific (cpuid) if other modes are active...
2736 */
2737 if (ctxt->mode == X86EMUL_MODE_PROT64)
2738 return true;
2739
2740 eax = 0x00000000;
2741 ecx = 0x00000000;
Yu Zhange911eb32017-08-24 20:27:52 +08002742 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, false);
Avi Kivity0017f932012-06-07 14:10:16 +03002743 /*
2744 * Intel ("GenuineIntel")
2745 * remark: Intel CPUs only support "syscall" in 64bit
2746 * longmode. Also an 64bit guest with a
2747 * 32bit compat-app running will #UD !! While this
2748 * behaviour can be fixed (by emulating) into AMD
2749 * response - CPUs of AMD can't behave like Intel.
2750 */
2751 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2752 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2753 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2754 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002755
Avi Kivity0017f932012-06-07 14:10:16 +03002756 /* AMD ("AuthenticAMD") */
2757 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2758 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2759 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2760 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002761
Avi Kivity0017f932012-06-07 14:10:16 +03002762 /* AMD ("AMDisbetter!") */
2763 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2764 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2765 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2766 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002767
Pu Wenb8f4abb2018-09-23 17:36:31 +08002768 /* Hygon ("HygonGenuine") */
2769 if (ebx == X86EMUL_CPUID_VENDOR_HygonGenuine_ebx &&
2770 ecx == X86EMUL_CPUID_VENDOR_HygonGenuine_ecx &&
2771 edx == X86EMUL_CPUID_VENDOR_HygonGenuine_edx)
2772 return true;
2773
2774 /*
2775 * default: (not Intel, not AMD, not Hygon), apply Intel's
2776 * stricter rules...
2777 */
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002778 return false;
2779}
2780
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002781static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002782{
Mathias Krause0225fb52012-08-30 01:30:16 +02002783 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002784 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002785 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002786 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002787 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002788
2789 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002790 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002791 ctxt->mode == X86EMUL_MODE_VM86)
2792 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002793
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002794 if (!(em_syscall_is_enabled(ctxt)))
2795 return emulate_ud(ctxt);
2796
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002797 ops->get_msr(ctxt, MSR_EFER, &efer);
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002798 if (!(efer & EFER_SCE))
2799 return emulate_ud(ctxt);
2800
Miaohe Lin5b4ce932019-11-09 16:58:54 +08002801 setup_syscalls_segments(ctxt, &cs, &ss);
Avi Kivity717746e2011-04-20 13:37:53 +03002802 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002803 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002804 cs_sel = (u16)(msr_data & 0xfffc);
2805 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002806
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002807 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002808 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002809 cs.l = 1;
2810 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002811 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2812 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002813
Avi Kivitydd856ef2012-08-27 23:46:17 +03002814 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002815 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002816#ifdef CONFIG_X86_64
Nadav Amit6c6cb692014-07-21 14:37:30 +03002817 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002818
Avi Kivity717746e2011-04-20 13:37:53 +03002819 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002820 ctxt->mode == X86EMUL_MODE_PROT64 ?
2821 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002822 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002823
Avi Kivity717746e2011-04-20 13:37:53 +03002824 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Nadav Amit6c6cb692014-07-21 14:37:30 +03002825 ctxt->eflags &= ~msr_data;
Wanpeng Li35fd68a2015-04-08 14:08:14 +08002826 ctxt->eflags |= X86_EFLAGS_FIXED;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002827#endif
2828 } else {
2829 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002830 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002831 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002832
Nadav Amit0efb0442015-03-29 16:33:03 +03002833 ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002834 }
2835
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02002836 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002837 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002838}
2839
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002840static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002841{
Mathias Krause0225fb52012-08-30 01:30:16 +02002842 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002843 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002844 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002845 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002846 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002847
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002848 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002849 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002850 if (ctxt->mode == X86EMUL_MODE_REAL)
2851 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002852
Avi Kivity1a18a692012-02-01 12:23:21 +02002853 /*
2854 * Not recognized on AMD in compat mode (but is recognized in legacy
2855 * mode).
2856 */
Nadav Amitf3747372015-01-01 23:11:11 +02002857 if ((ctxt->mode != X86EMUL_MODE_PROT64) && (efer & EFER_LMA)
Avi Kivity1a18a692012-02-01 12:23:21 +02002858 && !vendor_intel(ctxt))
2859 return emulate_ud(ctxt);
2860
Nadav Amitb2c9d432014-11-02 11:55:01 +02002861 /* sysenter/sysexit have not been tested in 64bit mode. */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002862 if (ctxt->mode == X86EMUL_MODE_PROT64)
Nadav Amitb2c9d432014-11-02 11:55:01 +02002863 return X86EMUL_UNHANDLEABLE;
Andre Przywara8c604352009-06-18 12:56:01 +02002864
Avi Kivity717746e2011-04-20 13:37:53 +03002865 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002866 if ((msr_data & 0xfffc) == 0x0)
2867 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002868
Miaohe Lin5b4ce932019-11-09 16:58:54 +08002869 setup_syscalls_segments(ctxt, &cs, &ss);
Nadav Amit0efb0442015-03-29 16:33:03 +03002870 ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
Nadav Amitb32a9912015-03-29 16:33:04 +03002871 cs_sel = (u16)msr_data & ~SEGMENT_RPL_MASK;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002872 ss_sel = cs_sel + 8;
Nadav Amitf3747372015-01-01 23:11:11 +02002873 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002874 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002875 cs.l = 1;
2876 }
2877
Avi Kivity1aa36612011-04-27 13:20:30 +03002878 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2879 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002880
Avi Kivity717746e2011-04-20 13:37:53 +03002881 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002882 ctxt->_eip = (efer & EFER_LMA) ? msr_data : (u32)msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002883
Avi Kivity717746e2011-04-20 13:37:53 +03002884 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002885 *reg_write(ctxt, VCPU_REGS_RSP) = (efer & EFER_LMA) ? msr_data :
2886 (u32)msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002887
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002888 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002889}
2890
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002891static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002892{
Mathias Krause0225fb52012-08-30 01:30:16 +02002893 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002894 struct desc_struct cs, ss;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002895 u64 msr_data, rcx, rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002896 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002897 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002898
Gleb Natapova0044752010-02-10 14:21:31 +02002899 /* inject #GP if in real mode or Virtual 8086 mode */
2900 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002901 ctxt->mode == X86EMUL_MODE_VM86)
2902 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002903
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002904 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002905
Avi Kivity9dac77f2011-06-01 15:34:25 +03002906 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002907 usermode = X86EMUL_MODE_PROT64;
2908 else
2909 usermode = X86EMUL_MODE_PROT32;
2910
Nadav Amit234f3ce2014-09-18 22:39:38 +03002911 rcx = reg_read(ctxt, VCPU_REGS_RCX);
2912 rdx = reg_read(ctxt, VCPU_REGS_RDX);
2913
Andre Przywara4668f052009-06-18 12:56:02 +02002914 cs.dpl = 3;
2915 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002916 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002917 switch (usermode) {
2918 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002919 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002920 if ((msr_data & 0xfffc) == 0x0)
2921 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002922 ss_sel = (u16)(msr_data + 24);
Nadav Amitbf0b6822014-09-18 22:39:45 +03002923 rcx = (u32)rcx;
2924 rdx = (u32)rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002925 break;
2926 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002927 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002928 if (msr_data == 0x0)
2929 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002930 ss_sel = cs_sel + 8;
2931 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002932 cs.l = 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08002933 if (emul_is_noncanonical_address(rcx, ctxt) ||
2934 emul_is_noncanonical_address(rdx, ctxt))
Nadav Amit234f3ce2014-09-18 22:39:38 +03002935 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002936 break;
2937 }
Nadav Amitb32a9912015-03-29 16:33:04 +03002938 cs_sel |= SEGMENT_RPL_MASK;
2939 ss_sel |= SEGMENT_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002940
Avi Kivity1aa36612011-04-27 13:20:30 +03002941 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2942 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002943
Nadav Amit234f3ce2014-09-18 22:39:38 +03002944 ctxt->_eip = rdx;
2945 *reg_write(ctxt, VCPU_REGS_RSP) = rcx;
Andre Przywara4668f052009-06-18 12:56:02 +02002946
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002947 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002948}
2949
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002950static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002951{
2952 int iopl;
2953 if (ctxt->mode == X86EMUL_MODE_REAL)
2954 return false;
2955 if (ctxt->mode == X86EMUL_MODE_VM86)
2956 return true;
Nadav Amit0efb0442015-03-29 16:33:03 +03002957 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002958 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002959}
2960
Liran Alon9a29d4492018-03-12 13:12:48 +02002961#define VMWARE_PORT_VMPORT (0x5658)
2962#define VMWARE_PORT_VMRPC (0x5659)
2963
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002964static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002965 u16 port, u16 len)
2966{
Mathias Krause0225fb52012-08-30 01:30:16 +02002967 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002968 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002969 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002970 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002971 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002972 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002973 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002974
Liran Alon9a29d4492018-03-12 13:12:48 +02002975 /*
2976 * VMware allows access to these ports even if denied
2977 * by TSS I/O permission bitmap. Mimic behavior.
2978 */
2979 if (enable_vmware_backdoor &&
2980 ((port == VMWARE_PORT_VMPORT) || (port == VMWARE_PORT_VMRPC)))
2981 return true;
2982
Avi Kivity1aa36612011-04-27 13:20:30 +03002983 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002984 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002985 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002986 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002987 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002988 base = get_desc_base(&tr_seg);
2989#ifdef CONFIG_X86_64
2990 base |= ((u64)base3) << 32;
2991#endif
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02002992 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL, true);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002993 if (r != X86EMUL_CONTINUE)
2994 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002995 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002996 return false;
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02002997 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL, true);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002998 if (r != X86EMUL_CONTINUE)
2999 return false;
3000 if ((perm >> bit_idx) & mask)
3001 return false;
3002 return true;
3003}
3004
3005static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003006 u16 port, u16 len)
3007{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03003008 if (ctxt->perm_ok)
3009 return true;
3010
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003011 if (emulator_bad_iopl(ctxt))
3012 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003013 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03003014
3015 ctxt->perm_ok = true;
3016
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003017 return true;
3018}
3019
Nadav Amit428e3d02015-04-28 13:06:01 +03003020static void string_registers_quirk(struct x86_emulate_ctxt *ctxt)
3021{
3022 /*
3023 * Intel CPUs mask the counter and pointers in quite strange
3024 * manner when ECX is zero due to REP-string optimizations.
3025 */
3026#ifdef CONFIG_X86_64
3027 if (ctxt->ad_bytes != 4 || !vendor_intel(ctxt))
3028 return;
3029
3030 *reg_write(ctxt, VCPU_REGS_RCX) = 0;
3031
3032 switch (ctxt->b) {
3033 case 0xa4: /* movsb */
3034 case 0xa5: /* movsd/w */
3035 *reg_rmw(ctxt, VCPU_REGS_RSI) &= (u32)-1;
3036 /* fall through */
3037 case 0xaa: /* stosb */
3038 case 0xab: /* stosd/w */
3039 *reg_rmw(ctxt, VCPU_REGS_RDI) &= (u32)-1;
3040 }
3041#endif
3042}
3043
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003044static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003045 struct tss_segment_16 *tss)
3046{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003047 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003048 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003049 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
3050 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
3051 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
3052 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
3053 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
3054 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
3055 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
3056 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003057
Avi Kivity1aa36612011-04-27 13:20:30 +03003058 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
3059 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
3060 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
3061 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
3062 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003063}
3064
3065static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003066 struct tss_segment_16 *tss)
3067{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003068 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003069 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003070
Avi Kivity9dac77f2011-06-01 15:34:25 +03003071 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003072 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003073 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
3074 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
3075 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
3076 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
3077 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
3078 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
3079 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
3080 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003081
3082 /*
3083 * SDM says that segment selectors are loaded before segment
3084 * descriptors
3085 */
Avi Kivity1aa36612011-04-27 13:20:30 +03003086 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
3087 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
3088 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
3089 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
3090 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003091
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003092 cpl = tss->cs & 3;
3093
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003094 /*
Guo Chaofc058682012-06-28 15:19:51 +08003095 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003096 * it is handled in a context of new task
3097 */
Nadav Amitd1442d82014-09-18 22:39:39 +03003098 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003099 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003100 if (ret != X86EMUL_CONTINUE)
3101 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003102 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003103 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003104 if (ret != X86EMUL_CONTINUE)
3105 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003106 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003107 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003108 if (ret != X86EMUL_CONTINUE)
3109 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003110 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003111 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003112 if (ret != X86EMUL_CONTINUE)
3113 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003114 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003115 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003116 if (ret != X86EMUL_CONTINUE)
3117 return ret;
3118
3119 return X86EMUL_CONTINUE;
3120}
3121
3122static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003123 u16 tss_selector, u16 old_tss_sel,
3124 ulong old_tss_base, struct desc_struct *new_desc)
3125{
3126 struct tss_segment_16 tss_seg;
3127 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003128 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003129
Jordan Borgner0e96f312018-10-28 12:58:28 +00003130 ret = linear_read_system(ctxt, old_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003131 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003132 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003133
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003134 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003135
Jordan Borgner0e96f312018-10-28 12:58:28 +00003136 ret = linear_write_system(ctxt, old_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003137 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003138 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003139
Jordan Borgner0e96f312018-10-28 12:58:28 +00003140 ret = linear_read_system(ctxt, new_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003141 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003142 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003143
3144 if (old_tss_sel != 0xffff) {
3145 tss_seg.prev_task_link = old_tss_sel;
3146
Paolo Bonzini79367a62018-06-06 16:43:02 +02003147 ret = linear_write_system(ctxt, new_tss_base,
3148 &tss_seg.prev_task_link,
Jordan Borgner0e96f312018-10-28 12:58:28 +00003149 sizeof(tss_seg.prev_task_link));
Avi Kivitydb297e32010-11-22 17:53:24 +02003150 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003151 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003152 }
3153
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003154 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003155}
3156
3157static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003158 struct tss_segment_32 *tss)
3159{
Nadav Amit5c7411e2014-04-07 18:37:47 +03003160 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003161 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003162 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003163 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
3164 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
3165 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
3166 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
3167 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
3168 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
3169 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
3170 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003171
Avi Kivity1aa36612011-04-27 13:20:30 +03003172 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
3173 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
3174 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
3175 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
3176 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
3177 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003178}
3179
3180static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003181 struct tss_segment_32 *tss)
3182{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003183 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003184 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003185
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003186 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003187 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003188 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003189 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01003190
3191 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03003192 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
3193 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
3194 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
3195 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
3196 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
3197 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
3198 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
3199 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003200
3201 /*
3202 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003203 * descriptors. This is important because CPL checks will
3204 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003205 */
Avi Kivity1aa36612011-04-27 13:20:30 +03003206 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
3207 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
3208 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
3209 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
3210 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
3211 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
3212 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003213
3214 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01003215 * If we're switching between Protected Mode and VM86, we need to make
3216 * sure to update the mode before loading the segment descriptors so
3217 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01003218 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003219 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01003220 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003221 cpl = 3;
3222 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01003223 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003224 cpl = tss->cs & 3;
3225 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01003226
3227 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003228 * Now load segment descriptors. If fault happenes at this stage
3229 * it is handled in a context of new task
3230 */
Nadav Amitd1442d82014-09-18 22:39:39 +03003231 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003232 cpl, X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003233 if (ret != X86EMUL_CONTINUE)
3234 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003235 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003236 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003237 if (ret != X86EMUL_CONTINUE)
3238 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003239 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003240 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003241 if (ret != X86EMUL_CONTINUE)
3242 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003243 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003244 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003245 if (ret != X86EMUL_CONTINUE)
3246 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003247 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003248 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003249 if (ret != X86EMUL_CONTINUE)
3250 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003251 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003252 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003253 if (ret != X86EMUL_CONTINUE)
3254 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003255 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003256 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003257
Eugene Korenevsky2f729b12015-03-29 01:27:17 +03003258 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003259}
3260
3261static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003262 u16 tss_selector, u16 old_tss_sel,
3263 ulong old_tss_base, struct desc_struct *new_desc)
3264{
3265 struct tss_segment_32 tss_seg;
3266 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003267 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03003268 u32 eip_offset = offsetof(struct tss_segment_32, eip);
3269 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003270
Jordan Borgner0e96f312018-10-28 12:58:28 +00003271 ret = linear_read_system(ctxt, old_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003272 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003273 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003274
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003275 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003276
Nadav Amit5c7411e2014-04-07 18:37:47 +03003277 /* Only GP registers and segment selectors are saved */
Paolo Bonzini79367a62018-06-06 16:43:02 +02003278 ret = linear_write_system(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
3279 ldt_sel_offset - eip_offset);
Avi Kivitydb297e32010-11-22 17:53:24 +02003280 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003281 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003282
Jordan Borgner0e96f312018-10-28 12:58:28 +00003283 ret = linear_read_system(ctxt, new_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003284 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003285 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003286
3287 if (old_tss_sel != 0xffff) {
3288 tss_seg.prev_task_link = old_tss_sel;
3289
Paolo Bonzini79367a62018-06-06 16:43:02 +02003290 ret = linear_write_system(ctxt, new_tss_base,
3291 &tss_seg.prev_task_link,
Jordan Borgner0e96f312018-10-28 12:58:28 +00003292 sizeof(tss_seg.prev_task_link));
Avi Kivitydb297e32010-11-22 17:53:24 +02003293 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003294 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003295 }
3296
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003297 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003298}
3299
3300static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003301 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003302 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003303{
Mathias Krause0225fb52012-08-30 01:30:16 +02003304 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003305 struct desc_struct curr_tss_desc, next_tss_desc;
3306 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03003307 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003308 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03003309 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02003310 u32 desc_limit;
Nadav Amit3db176d2015-04-19 21:12:59 +03003311 ulong desc_addr, dr7;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003312
3313 /* FIXME: old_tss_base == ~0 ? */
3314
Avi Kivitye9194642012-06-13 16:29:39 +03003315 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003316 if (ret != X86EMUL_CONTINUE)
3317 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03003318 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003319 if (ret != X86EMUL_CONTINUE)
3320 return ret;
3321
3322 /* FIXME: check that next_tss_desc is tss */
3323
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003324 /*
3325 * Check privileges. The three cases are task switch caused by...
3326 *
3327 * 1. jmp/call/int to task gate: Check against DPL of the task gate
3328 * 2. Exception/IRQ/iret: No check is performed
Nadav Amit2c2ca2d2014-11-02 11:54:57 +02003329 * 3. jmp/call to TSS/task-gate: No check is performed since the
3330 * hardware checks it before exiting.
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003331 */
3332 if (reason == TASK_SWITCH_GATE) {
3333 if (idt_index != -1) {
3334 /* Software interrupts */
3335 struct desc_struct task_gate_desc;
3336 int dpl;
3337
3338 ret = read_interrupt_descriptor(ctxt, idt_index,
3339 &task_gate_desc);
3340 if (ret != X86EMUL_CONTINUE)
3341 return ret;
3342
3343 dpl = task_gate_desc.dpl;
3344 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
3345 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
3346 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003347 }
3348
Gleb Natapovceffb452010-03-18 15:20:19 +02003349 desc_limit = desc_limit_scaled(&next_tss_desc);
3350 if (!next_tss_desc.p ||
3351 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
3352 desc_limit < 0x2b)) {
Paolo Bonzini592f0852014-08-20 10:05:08 +02003353 return emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003354 }
3355
3356 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
3357 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003358 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003359 }
3360
3361 if (reason == TASK_SWITCH_IRET)
3362 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
3363
3364 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08003365 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003366 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
3367 old_tss_sel = 0xffff;
3368
3369 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003370 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003371 old_tss_base, &next_tss_desc);
3372 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003373 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003374 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02003375 if (ret != X86EMUL_CONTINUE)
3376 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003377
3378 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
3379 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
3380
3381 if (reason != TASK_SWITCH_IRET) {
3382 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003383 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003384 }
3385
Avi Kivity717746e2011-04-20 13:37:53 +03003386 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03003387 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003388
Jan Kiszkae269fb22010-04-14 15:51:09 +02003389 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003390 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
3391 ctxt->lock_prefix = 0;
3392 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003393 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02003394 }
3395
Nadav Amit3db176d2015-04-19 21:12:59 +03003396 ops->get_dr(ctxt, 7, &dr7);
3397 ops->set_dr(ctxt, 7, dr7 & ~(DR_LOCAL_ENABLE_MASK | DR_LOCAL_SLOWDOWN));
3398
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003399 return ret;
3400}
3401
3402int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003403 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003404 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003405{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003406 int rc;
3407
Avi Kivitydd856ef2012-08-27 23:46:17 +03003408 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003409 ctxt->_eip = ctxt->eip;
3410 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003411
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003412 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003413 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003414
Avi Kivitydd856ef2012-08-27 23:46:17 +03003415 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003416 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003417 writeback_registers(ctxt);
3418 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003419
Gleb Natapova0c0ab22011-03-28 16:57:49 +02003420 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003421}
3422
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03003423static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
3424 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02003425{
Nadav Amit0efb0442015-03-29 16:33:03 +03003426 int df = (ctxt->eflags & X86_EFLAGS_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02003427
Paolo Bonzini01485a22014-11-19 18:25:08 +01003428 register_address_increment(ctxt, reg, df * op->bytes);
3429 op->addr.mem.ea = register_address(ctxt, reg);
Gleb Natapova682e352010-03-18 15:20:21 +02003430}
3431
Avi Kivity7af04fc2010-08-18 14:16:35 +03003432static int em_das(struct x86_emulate_ctxt *ctxt)
3433{
Avi Kivity7af04fc2010-08-18 14:16:35 +03003434 u8 al, old_al;
3435 bool af, cf, old_cf;
3436
3437 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003438 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03003439
3440 old_al = al;
3441 old_cf = cf;
3442 cf = false;
3443 af = ctxt->eflags & X86_EFLAGS_AF;
3444 if ((al & 0x0f) > 9 || af) {
3445 al -= 6;
3446 cf = old_cf | (al >= 250);
3447 af = true;
3448 } else {
3449 af = false;
3450 }
3451 if (old_al > 0x99 || old_cf) {
3452 al -= 0x60;
3453 cf = true;
3454 }
3455
Avi Kivity9dac77f2011-06-01 15:34:25 +03003456 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03003457 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003458 ctxt->src.type = OP_IMM;
3459 ctxt->src.val = 0;
3460 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02003461 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03003462 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
3463 if (cf)
3464 ctxt->eflags |= X86_EFLAGS_CF;
3465 if (af)
3466 ctxt->eflags |= X86_EFLAGS_AF;
3467 return X86EMUL_CONTINUE;
3468}
3469
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003470static int em_aam(struct x86_emulate_ctxt *ctxt)
3471{
3472 u8 al, ah;
3473
3474 if (ctxt->src.val == 0)
3475 return emulate_de(ctxt);
3476
3477 al = ctxt->dst.val & 0xff;
3478 ah = al / ctxt->src.val;
3479 al %= ctxt->src.val;
3480
3481 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
3482
3483 /* Set PF, ZF, SF */
3484 ctxt->src.type = OP_IMM;
3485 ctxt->src.val = 0;
3486 ctxt->src.bytes = 1;
3487 fastop(ctxt, em_or);
3488
3489 return X86EMUL_CONTINUE;
3490}
3491
Gleb Natapov7f662272012-12-10 11:42:30 +02003492static int em_aad(struct x86_emulate_ctxt *ctxt)
3493{
3494 u8 al = ctxt->dst.val & 0xff;
3495 u8 ah = (ctxt->dst.val >> 8) & 0xff;
3496
3497 al = (al + (ah * ctxt->src.val)) & 0xff;
3498
3499 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
3500
Gleb Natapovf583c292013-02-13 17:50:39 +02003501 /* Set PF, ZF, SF */
3502 ctxt->src.type = OP_IMM;
3503 ctxt->src.val = 0;
3504 ctxt->src.bytes = 1;
3505 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02003506
3507 return X86EMUL_CONTINUE;
3508}
3509
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003510static int em_call(struct x86_emulate_ctxt *ctxt)
3511{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003512 int rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003513 long rel = ctxt->src.val;
3514
3515 ctxt->src.val = (unsigned long)ctxt->_eip;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003516 rc = jmp_rel(ctxt, rel);
3517 if (rc != X86EMUL_CONTINUE)
3518 return rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003519 return em_push(ctxt);
3520}
3521
Avi Kivity0ef753b2010-08-18 14:51:45 +03003522static int em_call_far(struct x86_emulate_ctxt *ctxt)
3523{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003524 u16 sel, old_cs;
3525 ulong old_eip;
3526 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03003527 struct desc_struct old_desc, new_desc;
3528 const struct x86_emulate_ops *ops = ctxt->ops;
3529 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amit82268082015-01-26 09:32:27 +02003530 enum x86emul_mode prev_mode = ctxt->mode;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003531
Avi Kivity9dac77f2011-06-01 15:34:25 +03003532 old_eip = ctxt->_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003533 ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003534
Avi Kivity9dac77f2011-06-01 15:34:25 +03003535 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003536 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
3537 X86_TRANSFER_CALL_JMP, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003538 if (rc != X86EMUL_CONTINUE)
Nadav Amit80976db2014-12-25 02:52:20 +02003539 return rc;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003540
Nadav Amitd50eaa12014-11-19 17:43:11 +02003541 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003542 if (rc != X86EMUL_CONTINUE)
3543 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003544
Avi Kivity9dac77f2011-06-01 15:34:25 +03003545 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003546 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003547 if (rc != X86EMUL_CONTINUE)
Nadav Amitd1442d82014-09-18 22:39:39 +03003548 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003549
Avi Kivity9dac77f2011-06-01 15:34:25 +03003550 ctxt->src.val = old_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003551 rc = em_push(ctxt);
3552 /* If we failed, we tainted the memory, but the very least we should
3553 restore cs */
Nadav Amit82268082015-01-26 09:32:27 +02003554 if (rc != X86EMUL_CONTINUE) {
3555 pr_warn_once("faulting far call emulation tainted memory\n");
Nadav Amitd1442d82014-09-18 22:39:39 +03003556 goto fail;
Nadav Amit82268082015-01-26 09:32:27 +02003557 }
Nadav Amitd1442d82014-09-18 22:39:39 +03003558 return rc;
3559fail:
3560 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
Nadav Amit82268082015-01-26 09:32:27 +02003561 ctxt->mode = prev_mode;
Nadav Amitd1442d82014-09-18 22:39:39 +03003562 return rc;
3563
Avi Kivity0ef753b2010-08-18 14:51:45 +03003564}
3565
Avi Kivity40ece7c2010-08-18 15:12:09 +03003566static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3567{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003568 int rc;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003569 unsigned long eip;
Avi Kivity40ece7c2010-08-18 15:12:09 +03003570
Nadav Amit234f3ce2014-09-18 22:39:38 +03003571 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
3572 if (rc != X86EMUL_CONTINUE)
3573 return rc;
3574 rc = assign_eip_near(ctxt, eip);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003575 if (rc != X86EMUL_CONTINUE)
3576 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003577 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003578 return X86EMUL_CONTINUE;
3579}
3580
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003581static int em_xchg(struct x86_emulate_ctxt *ctxt)
3582{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003583 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003584 ctxt->src.val = ctxt->dst.val;
3585 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003586
3587 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003588 ctxt->dst.val = ctxt->src.orig_val;
3589 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003590 return X86EMUL_CONTINUE;
3591}
3592
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003593static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3594{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003595 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003596 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003597}
3598
Avi Kivity61429142010-08-19 15:13:00 +03003599static int em_cwd(struct x86_emulate_ctxt *ctxt)
3600{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003601 ctxt->dst.type = OP_REG;
3602 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003603 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003604 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003605
3606 return X86EMUL_CONTINUE;
3607}
3608
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02003609static int em_rdpid(struct x86_emulate_ctxt *ctxt)
3610{
3611 u64 tsc_aux = 0;
3612
3613 if (ctxt->ops->get_msr(ctxt, MSR_TSC_AUX, &tsc_aux))
3614 return emulate_gp(ctxt, 0);
3615 ctxt->dst.val = tsc_aux;
3616 return X86EMUL_CONTINUE;
3617}
3618
Avi Kivity48bb5d32010-08-18 18:54:34 +03003619static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3620{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003621 u64 tsc = 0;
3622
Avi Kivity717746e2011-04-20 13:37:53 +03003623 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003624 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3625 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003626 return X86EMUL_CONTINUE;
3627}
3628
Avi Kivity222d21a2011-11-10 14:57:30 +02003629static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3630{
3631 u64 pmc;
3632
Avi Kivitydd856ef2012-08-27 23:46:17 +03003633 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003634 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003635 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3636 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003637 return X86EMUL_CONTINUE;
3638}
3639
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003640static int em_mov(struct x86_emulate_ctxt *ctxt)
3641{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01003642 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003643 return X86EMUL_CONTINUE;
3644}
3645
Borislav Petkov84cffe42013-10-29 12:54:56 +01003646static int em_movbe(struct x86_emulate_ctxt *ctxt)
3647{
Borislav Petkov84cffe42013-10-29 12:54:56 +01003648 u16 tmp;
3649
Sean Christopherson5ae78e92019-12-17 13:32:38 -08003650 if (!ctxt->ops->guest_has_movbe(ctxt))
Borislav Petkov84cffe42013-10-29 12:54:56 +01003651 return emulate_ud(ctxt);
3652
3653 switch (ctxt->op_bytes) {
3654 case 2:
3655 /*
3656 * From MOVBE definition: "...When the operand size is 16 bits,
3657 * the upper word of the destination register remains unchanged
3658 * ..."
3659 *
3660 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3661 * rules so we have to do the operation almost per hand.
3662 */
3663 tmp = (u16)ctxt->src.val;
3664 ctxt->dst.val &= ~0xffffUL;
3665 ctxt->dst.val |= (unsigned long)swab16(tmp);
3666 break;
3667 case 4:
3668 ctxt->dst.val = swab32((u32)ctxt->src.val);
3669 break;
3670 case 8:
3671 ctxt->dst.val = swab64(ctxt->src.val);
3672 break;
3673 default:
Paolo Bonzini592f0852014-08-20 10:05:08 +02003674 BUG();
Borislav Petkov84cffe42013-10-29 12:54:56 +01003675 }
3676 return X86EMUL_CONTINUE;
3677}
3678
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003679static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3680{
3681 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3682 return emulate_gp(ctxt, 0);
3683
3684 /* Disable writeback. */
3685 ctxt->dst.type = OP_NONE;
3686 return X86EMUL_CONTINUE;
3687}
3688
3689static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3690{
3691 unsigned long val;
3692
3693 if (ctxt->mode == X86EMUL_MODE_PROT64)
3694 val = ctxt->src.val & ~0ULL;
3695 else
3696 val = ctxt->src.val & ~0U;
3697
3698 /* #UD condition is already handled. */
3699 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3700 return emulate_gp(ctxt, 0);
3701
3702 /* Disable writeback. */
3703 ctxt->dst.type = OP_NONE;
3704 return X86EMUL_CONTINUE;
3705}
3706
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003707static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3708{
3709 u64 msr_data;
3710
Avi Kivitydd856ef2012-08-27 23:46:17 +03003711 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3712 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3713 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003714 return emulate_gp(ctxt, 0);
3715
3716 return X86EMUL_CONTINUE;
3717}
3718
3719static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3720{
3721 u64 msr_data;
3722
Avi Kivitydd856ef2012-08-27 23:46:17 +03003723 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003724 return emulate_gp(ctxt, 0);
3725
Avi Kivitydd856ef2012-08-27 23:46:17 +03003726 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3727 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003728 return X86EMUL_CONTINUE;
3729}
3730
Paolo Bonzinidd307d02016-07-12 10:35:51 +02003731static int em_store_sreg(struct x86_emulate_ctxt *ctxt, int segment)
3732{
3733 if (segment > VCPU_SREG_GS &&
3734 (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_UMIP) &&
3735 ctxt->ops->cpl(ctxt) > 0)
3736 return emulate_gp(ctxt, 0);
3737
3738 ctxt->dst.val = get_segment_selector(ctxt, segment);
3739 if (ctxt->dst.bytes == 4 && ctxt->dst.type == OP_MEM)
3740 ctxt->dst.bytes = 2;
3741 return X86EMUL_CONTINUE;
3742}
3743
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003744static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3745{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003746 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003747 return emulate_ud(ctxt);
3748
Paolo Bonzinidd307d02016-07-12 10:35:51 +02003749 return em_store_sreg(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003750}
3751
3752static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3753{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003754 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003755
Avi Kivity9dac77f2011-06-01 15:34:25 +03003756 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003757 return emulate_ud(ctxt);
3758
Avi Kivity9dac77f2011-06-01 15:34:25 +03003759 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003760 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3761
3762 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003763 ctxt->dst.type = OP_NONE;
3764 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003765}
3766
Paolo Bonzinidd307d02016-07-12 10:35:51 +02003767static int em_sldt(struct x86_emulate_ctxt *ctxt)
3768{
3769 return em_store_sreg(ctxt, VCPU_SREG_LDTR);
3770}
3771
Avi Kivitya14e5792012-06-13 12:28:33 +03003772static int em_lldt(struct x86_emulate_ctxt *ctxt)
3773{
3774 u16 sel = ctxt->src.val;
3775
3776 /* Disable writeback. */
3777 ctxt->dst.type = OP_NONE;
3778 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3779}
3780
Paolo Bonzinidd307d02016-07-12 10:35:51 +02003781static int em_str(struct x86_emulate_ctxt *ctxt)
3782{
3783 return em_store_sreg(ctxt, VCPU_SREG_TR);
3784}
3785
Avi Kivity80890002012-06-13 16:33:29 +03003786static int em_ltr(struct x86_emulate_ctxt *ctxt)
3787{
3788 u16 sel = ctxt->src.val;
3789
3790 /* Disable writeback. */
3791 ctxt->dst.type = OP_NONE;
3792 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3793}
3794
Avi Kivity38503912011-03-31 18:48:09 +02003795static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3796{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003797 int rc;
3798 ulong linear;
3799
Avi Kivity9dac77f2011-06-01 15:34:25 +03003800 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003801 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003802 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003803 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003804 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003805 return X86EMUL_CONTINUE;
3806}
3807
Avi Kivity2d04a052011-04-20 15:32:49 +03003808static int em_clts(struct x86_emulate_ctxt *ctxt)
3809{
3810 ulong cr0;
3811
3812 cr0 = ctxt->ops->get_cr(ctxt, 0);
3813 cr0 &= ~X86_CR0_TS;
3814 ctxt->ops->set_cr(ctxt, 0, cr0);
3815 return X86EMUL_CONTINUE;
3816}
3817
Jan Kiszkab34a8052015-03-09 20:27:43 +01003818static int em_hypercall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003819{
Nadav Amit0f54a322014-08-29 11:26:55 +03003820 int rc = ctxt->ops->fix_hypercall(ctxt);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003821
Avi Kivity26d05cc2011-04-21 12:07:59 +03003822 if (rc != X86EMUL_CONTINUE)
3823 return rc;
3824
3825 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003826 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003827 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003828 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003829 return X86EMUL_CONTINUE;
3830}
3831
Avi Kivity96051572012-06-10 17:21:18 +03003832static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3833 void (*get)(struct x86_emulate_ctxt *ctxt,
3834 struct desc_ptr *ptr))
3835{
3836 struct desc_ptr desc_ptr;
3837
Paolo Bonziniae3e61e2016-07-12 10:36:41 +02003838 if ((ctxt->ops->get_cr(ctxt, 4) & X86_CR4_UMIP) &&
3839 ctxt->ops->cpl(ctxt) > 0)
3840 return emulate_gp(ctxt, 0);
3841
Avi Kivity96051572012-06-10 17:21:18 +03003842 if (ctxt->mode == X86EMUL_MODE_PROT64)
3843 ctxt->op_bytes = 8;
3844 get(ctxt, &desc_ptr);
3845 if (ctxt->op_bytes == 2) {
3846 ctxt->op_bytes = 4;
3847 desc_ptr.address &= 0x00ffffff;
3848 }
3849 /* Disable writeback. */
3850 ctxt->dst.type = OP_NONE;
Steve Rutherford129a72a2017-01-11 18:28:29 -08003851 return segmented_write_std(ctxt, ctxt->dst.addr.mem,
3852 &desc_ptr, 2 + ctxt->op_bytes);
Avi Kivity96051572012-06-10 17:21:18 +03003853}
3854
3855static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3856{
3857 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3858}
3859
3860static int em_sidt(struct x86_emulate_ctxt *ctxt)
3861{
3862 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3863}
3864
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003865static int em_lgdt_lidt(struct x86_emulate_ctxt *ctxt, bool lgdt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003866{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003867 struct desc_ptr desc_ptr;
3868 int rc;
3869
Avi Kivity510425f2012-06-07 17:04:36 +03003870 if (ctxt->mode == X86EMUL_MODE_PROT64)
3871 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003872 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003873 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003874 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003875 if (rc != X86EMUL_CONTINUE)
3876 return rc;
Nadav Amit9a9abf62014-11-02 11:54:56 +02003877 if (ctxt->mode == X86EMUL_MODE_PROT64 &&
Yu Zhangfd8cb432017-08-24 20:27:56 +08003878 emul_is_noncanonical_address(desc_ptr.address, ctxt))
Nadav Amit9a9abf62014-11-02 11:54:56 +02003879 return emulate_gp(ctxt, 0);
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003880 if (lgdt)
3881 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3882 else
3883 ctxt->ops->set_idt(ctxt, &desc_ptr);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003884 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003885 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003886 return X86EMUL_CONTINUE;
3887}
3888
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003889static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3890{
3891 return em_lgdt_lidt(ctxt, true);
3892}
3893
Avi Kivity26d05cc2011-04-21 12:07:59 +03003894static int em_lidt(struct x86_emulate_ctxt *ctxt)
3895{
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003896 return em_lgdt_lidt(ctxt, false);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003897}
3898
3899static int em_smsw(struct x86_emulate_ctxt *ctxt)
3900{
Paolo Bonziniae3e61e2016-07-12 10:36:41 +02003901 if ((ctxt->ops->get_cr(ctxt, 4) & X86_CR4_UMIP) &&
3902 ctxt->ops->cpl(ctxt) > 0)
3903 return emulate_gp(ctxt, 0);
3904
Nadav Amit32e94d02014-06-02 18:34:11 +03003905 if (ctxt->dst.type == OP_MEM)
3906 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003907 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003908 return X86EMUL_CONTINUE;
3909}
3910
3911static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3912{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003913 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003914 | (ctxt->src.val & 0x0f));
3915 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003916 return X86EMUL_CONTINUE;
3917}
3918
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003919static int em_loop(struct x86_emulate_ctxt *ctxt)
3920{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003921 int rc = X86EMUL_CONTINUE;
3922
Paolo Bonzini01485a22014-11-19 18:25:08 +01003923 register_address_increment(ctxt, VCPU_REGS_RCX, -1);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003924 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003925 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
Nadav Amit234f3ce2014-09-18 22:39:38 +03003926 rc = jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003927
Nadav Amit234f3ce2014-09-18 22:39:38 +03003928 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003929}
3930
3931static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3932{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003933 int rc = X86EMUL_CONTINUE;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003934
Nadav Amit234f3ce2014-09-18 22:39:38 +03003935 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
3936 rc = jmp_rel(ctxt, ctxt->src.val);
3937
3938 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003939}
3940
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003941static int em_in(struct x86_emulate_ctxt *ctxt)
3942{
3943 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3944 &ctxt->dst.val))
3945 return X86EMUL_IO_NEEDED;
3946
3947 return X86EMUL_CONTINUE;
3948}
3949
3950static int em_out(struct x86_emulate_ctxt *ctxt)
3951{
3952 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3953 &ctxt->src.val, 1);
3954 /* Disable writeback. */
3955 ctxt->dst.type = OP_NONE;
3956 return X86EMUL_CONTINUE;
3957}
3958
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003959static int em_cli(struct x86_emulate_ctxt *ctxt)
3960{
3961 if (emulator_bad_iopl(ctxt))
3962 return emulate_gp(ctxt, 0);
3963
3964 ctxt->eflags &= ~X86_EFLAGS_IF;
3965 return X86EMUL_CONTINUE;
3966}
3967
3968static int em_sti(struct x86_emulate_ctxt *ctxt)
3969{
3970 if (emulator_bad_iopl(ctxt))
3971 return emulate_gp(ctxt, 0);
3972
3973 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3974 ctxt->eflags |= X86_EFLAGS_IF;
3975 return X86EMUL_CONTINUE;
3976}
3977
Avi Kivity6d6eede2012-06-07 14:11:36 +03003978static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3979{
3980 u32 eax, ebx, ecx, edx;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003981 u64 msr = 0;
3982
3983 ctxt->ops->get_msr(ctxt, MSR_MISC_FEATURES_ENABLES, &msr);
3984 if (msr & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
3985 ctxt->ops->cpl(ctxt)) {
3986 return emulate_gp(ctxt, 0);
3987 }
Avi Kivity6d6eede2012-06-07 14:11:36 +03003988
Avi Kivitydd856ef2012-08-27 23:46:17 +03003989 eax = reg_read(ctxt, VCPU_REGS_RAX);
3990 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Yu Zhange911eb32017-08-24 20:27:52 +08003991 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, true);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003992 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3993 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3994 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3995 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003996 return X86EMUL_CONTINUE;
3997}
3998
Paolo Bonzini98f73632013-10-31 11:19:42 +01003999static int em_sahf(struct x86_emulate_ctxt *ctxt)
4000{
4001 u32 flags;
4002
Nadav Amit0efb0442015-03-29 16:33:03 +03004003 flags = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
4004 X86_EFLAGS_SF;
Paolo Bonzini98f73632013-10-31 11:19:42 +01004005 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
4006
4007 ctxt->eflags &= ~0xffUL;
4008 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
4009 return X86EMUL_CONTINUE;
4010}
4011
Avi Kivity2dd7caa2012-06-11 13:09:07 +03004012static int em_lahf(struct x86_emulate_ctxt *ctxt)
4013{
Avi Kivitydd856ef2012-08-27 23:46:17 +03004014 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
4015 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03004016 return X86EMUL_CONTINUE;
4017}
4018
Avi Kivity92998362012-06-13 12:25:06 +03004019static int em_bswap(struct x86_emulate_ctxt *ctxt)
4020{
4021 switch (ctxt->op_bytes) {
4022#ifdef CONFIG_X86_64
4023 case 8:
4024 asm("bswap %0" : "+r"(ctxt->dst.val));
4025 break;
4026#endif
4027 default:
4028 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
4029 break;
4030 }
4031 return X86EMUL_CONTINUE;
4032}
4033
Nadav Amit13e457e2014-10-13 13:04:13 +03004034static int em_clflush(struct x86_emulate_ctxt *ctxt)
4035{
4036 /* emulating clflush regardless of cpuid */
4037 return X86EMUL_CONTINUE;
4038}
4039
Nadav Amit2276b512015-01-26 09:32:24 +02004040static int em_movsxd(struct x86_emulate_ctxt *ctxt)
4041{
4042 ctxt->dst.val = (s32) ctxt->src.val;
4043 return X86EMUL_CONTINUE;
4044}
4045
Radim Krčmář283c95d2016-11-09 19:07:06 +01004046static int check_fxsr(struct x86_emulate_ctxt *ctxt)
4047{
Sean Christopherson5ae78e92019-12-17 13:32:38 -08004048 if (!ctxt->ops->guest_has_fxsr(ctxt))
Radim Krčmář283c95d2016-11-09 19:07:06 +01004049 return emulate_ud(ctxt);
4050
4051 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
4052 return emulate_nm(ctxt);
4053
4054 /*
4055 * Don't emulate a case that should never be hit, instead of working
4056 * around a lack of fxsave64/fxrstor64 on old compilers.
4057 */
4058 if (ctxt->mode >= X86EMUL_MODE_PROT64)
4059 return X86EMUL_UNHANDLEABLE;
4060
4061 return X86EMUL_CONTINUE;
4062}
4063
4064/*
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004065 * Hardware doesn't save and restore XMM 0-7 without CR4.OSFXSR, but does save
4066 * and restore MXCSR.
4067 */
4068static size_t __fxstate_size(int nregs)
4069{
4070 return offsetof(struct fxregs_state, xmm_space[0]) + nregs * 16;
4071}
4072
4073static inline size_t fxstate_size(struct x86_emulate_ctxt *ctxt)
4074{
4075 bool cr4_osfxsr;
4076 if (ctxt->mode == X86EMUL_MODE_PROT64)
4077 return __fxstate_size(16);
4078
4079 cr4_osfxsr = ctxt->ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR;
4080 return __fxstate_size(cr4_osfxsr ? 8 : 0);
4081}
4082
4083/*
Radim Krčmář283c95d2016-11-09 19:07:06 +01004084 * FXSAVE and FXRSTOR have 4 different formats depending on execution mode,
4085 * 1) 16 bit mode
4086 * 2) 32 bit mode
4087 * - like (1), but FIP and FDP (foo) are only 16 bit. At least Intel CPUs
4088 * preserve whole 32 bit values, though, so (1) and (2) are the same wrt.
4089 * save and restore
4090 * 3) 64-bit mode with REX.W prefix
4091 * - like (2), but XMM 8-15 are being saved and restored
4092 * 4) 64-bit mode without REX.W prefix
4093 * - like (3), but FIP and FDP are 64 bit
4094 *
4095 * Emulation uses (3) for (1) and (2) and preserves XMM 8-15 to reach the
4096 * desired result. (4) is not emulated.
4097 *
4098 * Note: Guest and host CPUID.(EAX=07H,ECX=0H):EBX[bit 13] (deprecate FPU CS
4099 * and FPU DS) should match.
4100 */
4101static int em_fxsave(struct x86_emulate_ctxt *ctxt)
4102{
4103 struct fxregs_state fx_state;
Radim Krčmář283c95d2016-11-09 19:07:06 +01004104 int rc;
4105
4106 rc = check_fxsr(ctxt);
4107 if (rc != X86EMUL_CONTINUE)
4108 return rc;
4109
Sean Christophersona7baead2020-01-17 11:30:50 -08004110 emulator_get_fpu();
4111
Radim Krčmář283c95d2016-11-09 19:07:06 +01004112 rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_state));
4113
Sean Christophersona7baead2020-01-17 11:30:50 -08004114 emulator_put_fpu();
4115
Radim Krčmář283c95d2016-11-09 19:07:06 +01004116 if (rc != X86EMUL_CONTINUE)
4117 return rc;
4118
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004119 return segmented_write_std(ctxt, ctxt->memop.addr.mem, &fx_state,
4120 fxstate_size(ctxt));
Radim Krčmář283c95d2016-11-09 19:07:06 +01004121}
4122
David Hildenbrand4d772cb2017-11-07 18:04:05 +01004123/*
4124 * FXRSTOR might restore XMM registers not provided by the guest. Fill
4125 * in the host registers (via FXSAVE) instead, so they won't be modified.
4126 * (preemption has to stay disabled until FXRSTOR).
4127 *
4128 * Use noinline to keep the stack for other functions called by callers small.
4129 */
4130static noinline int fxregs_fixup(struct fxregs_state *fx_state,
4131 const size_t used_size)
4132{
4133 struct fxregs_state fx_tmp;
4134 int rc;
4135
4136 rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_tmp));
4137 memcpy((void *)fx_state + used_size, (void *)&fx_tmp + used_size,
4138 __fxstate_size(16) - used_size);
4139
4140 return rc;
4141}
4142
Radim Krčmář283c95d2016-11-09 19:07:06 +01004143static int em_fxrstor(struct x86_emulate_ctxt *ctxt)
4144{
4145 struct fxregs_state fx_state;
4146 int rc;
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004147 size_t size;
Radim Krčmář283c95d2016-11-09 19:07:06 +01004148
4149 rc = check_fxsr(ctxt);
4150 if (rc != X86EMUL_CONTINUE)
4151 return rc;
4152
David Hildenbrand4d772cb2017-11-07 18:04:05 +01004153 size = fxstate_size(ctxt);
4154 rc = segmented_read_std(ctxt, ctxt->memop.addr.mem, &fx_state, size);
4155 if (rc != X86EMUL_CONTINUE)
4156 return rc;
4157
Sean Christophersona7baead2020-01-17 11:30:50 -08004158 emulator_get_fpu();
4159
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004160 if (size < __fxstate_size(16)) {
David Hildenbrand4d772cb2017-11-07 18:04:05 +01004161 rc = fxregs_fixup(&fx_state, size);
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004162 if (rc != X86EMUL_CONTINUE)
4163 goto out;
4164 }
4165
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004166 if (fx_state.mxcsr >> 16) {
4167 rc = emulate_gp(ctxt, 0);
4168 goto out;
4169 }
Radim Krčmář283c95d2016-11-09 19:07:06 +01004170
4171 if (rc == X86EMUL_CONTINUE)
4172 rc = asm_safe("fxrstor %[fx]", : [fx] "m"(fx_state));
4173
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004174out:
Sean Christophersona7baead2020-01-17 11:30:50 -08004175 emulator_put_fpu();
4176
Radim Krčmář283c95d2016-11-09 19:07:06 +01004177 return rc;
4178}
4179
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02004180static int em_xsetbv(struct x86_emulate_ctxt *ctxt)
4181{
4182 u32 eax, ecx, edx;
4183
4184 eax = reg_read(ctxt, VCPU_REGS_RAX);
4185 edx = reg_read(ctxt, VCPU_REGS_RDX);
4186 ecx = reg_read(ctxt, VCPU_REGS_RCX);
4187
4188 if (ctxt->ops->set_xcr(ctxt, ecx, ((u64)edx << 32) | eax))
4189 return emulate_gp(ctxt, 0);
4190
4191 return X86EMUL_CONTINUE;
4192}
4193
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004194static bool valid_cr(int nr)
4195{
4196 switch (nr) {
4197 case 0:
4198 case 2 ... 4:
4199 case 8:
4200 return true;
4201 default:
4202 return false;
4203 }
4204}
4205
4206static int check_cr_read(struct x86_emulate_ctxt *ctxt)
4207{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004208 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004209 return emulate_ud(ctxt);
4210
4211 return X86EMUL_CONTINUE;
4212}
4213
4214static int check_cr_write(struct x86_emulate_ctxt *ctxt)
4215{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004216 u64 new_val = ctxt->src.val64;
4217 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03004218 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004219
4220 static u64 cr_reserved_bits[] = {
4221 0xffffffff00000000ULL,
4222 0, 0, 0, /* CR3 checked later */
4223 CR4_RESERVED_BITS,
4224 0, 0, 0,
4225 CR8_RESERVED_BITS,
4226 };
4227
4228 if (!valid_cr(cr))
4229 return emulate_ud(ctxt);
4230
4231 if (new_val & cr_reserved_bits[cr])
4232 return emulate_gp(ctxt, 0);
4233
4234 switch (cr) {
4235 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03004236 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004237 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
4238 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
4239 return emulate_gp(ctxt, 0);
4240
Avi Kivity717746e2011-04-20 13:37:53 +03004241 cr4 = ctxt->ops->get_cr(ctxt, 4);
4242 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004243
4244 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
4245 !(cr4 & X86_CR4_PAE))
4246 return emulate_gp(ctxt, 0);
4247
4248 break;
4249 }
4250 case 3: {
4251 u64 rsvd = 0;
4252
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03004253 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08004254 if (efer & EFER_LMA) {
4255 u64 maxphyaddr;
Yu Zhangd6500142017-09-18 18:45:01 +08004256 u32 eax, ebx, ecx, edx;
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08004257
Yu Zhangd6500142017-09-18 18:45:01 +08004258 eax = 0x80000008;
4259 ecx = 0;
4260 if (ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx,
4261 &edx, false))
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08004262 maxphyaddr = eax & 0xff;
4263 else
4264 maxphyaddr = 36;
Wanpeng Lia780a3e2018-05-13 02:24:47 -07004265 rsvd = rsvd_bits(maxphyaddr, 63);
4266 if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PCIDE)
Junaid Shahid208320b2018-06-27 14:59:21 -07004267 rsvd &= ~X86_CR3_PCID_NOFLUSH;
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08004268 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004269
4270 if (new_val & rsvd)
4271 return emulate_gp(ctxt, 0);
4272
4273 break;
4274 }
4275 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03004276 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004277
4278 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
4279 return emulate_gp(ctxt, 0);
4280
4281 break;
4282 }
4283 }
4284
4285 return X86EMUL_CONTINUE;
4286}
4287
Joerg Roedel3b88e412011-04-04 12:39:29 +02004288static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
4289{
4290 unsigned long dr7;
4291
Avi Kivity717746e2011-04-20 13:37:53 +03004292 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02004293
4294 /* Check if DR7.Global_Enable is set */
4295 return dr7 & (1 << 13);
4296}
4297
4298static int check_dr_read(struct x86_emulate_ctxt *ctxt)
4299{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004300 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02004301 u64 cr4;
4302
4303 if (dr > 7)
4304 return emulate_ud(ctxt);
4305
Avi Kivity717746e2011-04-20 13:37:53 +03004306 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02004307 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
4308 return emulate_ud(ctxt);
4309
Nadav Amit6d2a0522014-11-02 11:54:43 +02004310 if (check_dr7_gd(ctxt)) {
4311 ulong dr6;
4312
4313 ctxt->ops->get_dr(ctxt, 6, &dr6);
Liran Alon1fc5d1942019-06-06 01:54:47 +03004314 dr6 &= ~DR_TRAP_BITS;
Nadav Amit6d2a0522014-11-02 11:54:43 +02004315 dr6 |= DR6_BD | DR6_RTM;
4316 ctxt->ops->set_dr(ctxt, 6, dr6);
Joerg Roedel3b88e412011-04-04 12:39:29 +02004317 return emulate_db(ctxt);
Nadav Amit6d2a0522014-11-02 11:54:43 +02004318 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02004319
4320 return X86EMUL_CONTINUE;
4321}
4322
4323static int check_dr_write(struct x86_emulate_ctxt *ctxt)
4324{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004325 u64 new_val = ctxt->src.val64;
4326 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02004327
4328 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
4329 return emulate_gp(ctxt, 0);
4330
4331 return check_dr_read(ctxt);
4332}
4333
Joerg Roedel01de8b02011-04-04 12:39:31 +02004334static int check_svme(struct x86_emulate_ctxt *ctxt)
4335{
Radim Krčmář92ceb762017-05-18 19:37:32 +02004336 u64 efer = 0;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004337
Avi Kivity717746e2011-04-20 13:37:53 +03004338 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02004339
4340 if (!(efer & EFER_SVME))
4341 return emulate_ud(ctxt);
4342
4343 return X86EMUL_CONTINUE;
4344}
4345
4346static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
4347{
Avi Kivitydd856ef2012-08-27 23:46:17 +03004348 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02004349
4350 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07004351 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02004352 return emulate_gp(ctxt, 0);
4353
4354 return check_svme(ctxt);
4355}
4356
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004357static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
4358{
Avi Kivity717746e2011-04-20 13:37:53 +03004359 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004360
Avi Kivity717746e2011-04-20 13:37:53 +03004361 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004362 return emulate_ud(ctxt);
4363
4364 return X86EMUL_CONTINUE;
4365}
4366
Joerg Roedel80612522011-04-04 12:39:33 +02004367static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
4368{
Avi Kivity717746e2011-04-20 13:37:53 +03004369 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03004370 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02004371
Arbel Moshe2d7921c2018-03-12 13:12:53 +02004372 /*
4373 * VMware allows access to these Pseduo-PMCs even when read via RDPMC
4374 * in Ring3 when CR4.PCE=0.
4375 */
4376 if (enable_vmware_backdoor && is_vmware_backdoor_pmc(rcx))
4377 return X86EMUL_CONTINUE;
4378
Avi Kivity717746e2011-04-20 13:37:53 +03004379 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03004380 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02004381 return emulate_gp(ctxt, 0);
4382
4383 return X86EMUL_CONTINUE;
4384}
4385
Joerg Roedelf6511932011-04-04 12:39:35 +02004386static int check_perm_in(struct x86_emulate_ctxt *ctxt)
4387{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004388 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
4389 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02004390 return emulate_gp(ctxt, 0);
4391
4392 return X86EMUL_CONTINUE;
4393}
4394
4395static int check_perm_out(struct x86_emulate_ctxt *ctxt)
4396{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004397 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
4398 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02004399 return emulate_gp(ctxt, 0);
4400
4401 return X86EMUL_CONTINUE;
4402}
4403
Avi Kivity73fba5f2010-07-29 15:11:53 +03004404#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004405#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
4406#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
4407 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03004408#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02004409#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004410#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
4411#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Nadav Amit39f062f2014-11-26 15:47:18 +02004412#define ID(_f, _i) { .flags = ((_f) | InstrDual | ModRM), .u.idual = (_i) }
Nadav Amit2276b512015-01-26 09:32:24 +02004413#define MD(_f, _m) { .flags = ((_f) | ModeDual), .u.mdual = (_m) }
Gleb Natapov045a2822012-12-20 16:57:43 +02004414#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03004415#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02004416#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004417#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004418 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004419#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004420 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
4421 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02004422#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03004423
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004424#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02004425#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004426#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02004427#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004428#define I2bvIP(_f, _e, _i, _p) \
4429 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004430
Avi Kivityfb864fb2013-01-04 16:18:54 +02004431#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
4432 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
4433 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03004434
Nadav Amit0f54a322014-08-29 11:26:55 +03004435static const struct opcode group7_rm0[] = {
4436 N,
Jan Kiszkab34a8052015-03-09 20:27:43 +01004437 I(SrcNone | Priv | EmulateOnUD, em_hypercall),
Nadav Amit0f54a322014-08-29 11:26:55 +03004438 N, N, N, N, N, N,
4439};
4440
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004441static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004442 DI(SrcNone | Priv, monitor),
4443 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004444 N, N, N, N, N, N,
4445};
4446
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02004447static const struct opcode group7_rm2[] = {
4448 N,
4449 II(ImplicitOps | Priv, em_xsetbv, xsetbv),
4450 N, N, N, N, N, N,
4451};
4452
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004453static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004454 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Jan Kiszkab34a8052015-03-09 20:27:43 +01004455 II(SrcNone | Prot | EmulateOnUD, em_hypercall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004456 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
4457 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
4458 DIP(SrcNone | Prot | Priv, stgi, check_svme),
4459 DIP(SrcNone | Prot | Priv, clgi, check_svme),
4460 DIP(SrcNone | Prot | Priv, skinit, check_svme),
4461 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004462};
Avi Kivity6230f7f2010-08-26 18:34:55 +03004463
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004464static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004465 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004466 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004467 N, N, N, N, N, N,
4468};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004469
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004470static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02004471 F(Lock, em_add),
4472 F(Lock | PageTable, em_or),
4473 F(Lock, em_adc),
4474 F(Lock, em_sbb),
4475 F(Lock | PageTable, em_and),
4476 F(Lock, em_sub),
4477 F(Lock, em_xor),
4478 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004479};
4480
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004481static const struct opcode group1A[] = {
Tom Lendacky0f89b202016-12-14 14:59:23 -05004482 I(DstMem | SrcNone | Mov | Stack | IncSP | TwoMemOp, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004483};
4484
Avi Kivity007a3b52013-01-19 19:51:51 +02004485static const struct opcode group2[] = {
4486 F(DstMem | ModRM, em_rol),
4487 F(DstMem | ModRM, em_ror),
4488 F(DstMem | ModRM, em_rcl),
4489 F(DstMem | ModRM, em_rcr),
4490 F(DstMem | ModRM, em_shl),
4491 F(DstMem | ModRM, em_shr),
4492 F(DstMem | ModRM, em_shl),
4493 F(DstMem | ModRM, em_sar),
4494};
4495
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004496static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02004497 F(DstMem | SrcImm | NoWrite, em_test),
4498 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02004499 F(DstMem | SrcNone | Lock, em_not),
4500 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02004501 F(DstXacc | Src2Mem, em_mul_ex),
4502 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004503 F(DstXacc | Src2Mem, em_div_ex),
4504 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004505};
4506
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004507static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02004508 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
4509 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004510 N, N, N, N, N, N,
4511};
4512
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004513static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02004514 F(DstMem | SrcNone | Lock, em_inc),
4515 F(DstMem | SrcNone | Lock, em_dec),
Nadav Amit58b70752014-10-24 11:35:09 +03004516 I(SrcMem | NearBranch, em_call_near_abs),
Nadav Amitacac6f892015-05-03 20:22:57 +03004517 I(SrcMemFAddr | ImplicitOps, em_call_far),
Nadav Amit58b70752014-10-24 11:35:09 +03004518 I(SrcMem | NearBranch, em_jmp_abs),
Nadav Amitf7784042014-09-18 22:39:41 +03004519 I(SrcMemFAddr | ImplicitOps, em_jmp_far),
Tom Lendacky0f89b202016-12-14 14:59:23 -05004520 I(SrcMem | Stack | TwoMemOp, em_push), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004521};
4522
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004523static const struct opcode group6[] = {
Paolo Bonzinidd307d02016-07-12 10:35:51 +02004524 II(Prot | DstMem, em_sldt, sldt),
4525 II(Prot | DstMem, em_str, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03004526 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03004527 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004528 N, N, N, N,
4529};
4530
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004531static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03004532 II(Mov | DstMem, em_sgdt, sgdt),
4533 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004534 II(SrcMem | Priv, em_lgdt, lgdt),
4535 II(SrcMem | Priv, em_lidt, lidt),
4536 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
4537 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
4538 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004539}, {
Nadav Amit0f54a322014-08-29 11:26:55 +03004540 EXT(0, group7_rm0),
Avi Kivity5ef39c72011-04-21 12:21:50 +03004541 EXT(0, group7_rm1),
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02004542 EXT(0, group7_rm2),
4543 EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004544 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
4545 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
4546 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004547} };
4548
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004549static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004550 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02004551 F(DstMem | SrcImmByte | NoWrite, em_bt),
4552 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
4553 F(DstMem | SrcImmByte | Lock, em_btr),
4554 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004555};
4556
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02004557/*
4558 * The "memory" destination is actually always a register, since we come
4559 * from the register case of group9.
4560 */
4561static const struct gprefix pfx_0f_c7_7 = {
4562 N, N, N, II(DstMem | ModRM | Op3264 | EmulateOnUD, em_rdpid, rdtscp),
4563};
4564
4565
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004566static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004567 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004568}, {
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02004569 N, N, N, N, N, N, N,
4570 GP(0, &pfx_0f_c7_7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004571} };
4572
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004573static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004574 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004575 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004576};
4577
Nadav Amit13e457e2014-10-13 13:04:13 +03004578static const struct gprefix pfx_0f_ae_7 = {
Nadav Amit3f6f1482014-10-13 13:04:14 +03004579 I(SrcMem | ByteOp, em_clflush), N, N, N,
Nadav Amit13e457e2014-10-13 13:04:13 +03004580};
4581
4582static const struct group_dual group15 = { {
Radim Krčmář283c95d2016-11-09 19:07:06 +01004583 I(ModRM | Aligned16, em_fxsave),
4584 I(ModRM | Aligned16, em_fxrstor),
4585 N, N, N, N, N, GP(0, &pfx_0f_ae_7),
Nadav Amit13e457e2014-10-13 13:04:13 +03004586}, {
4587 N, N, N, N, N, N, N, N,
4588} };
4589
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004590static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03004591 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02004592};
4593
Nadav Amit39f062f2014-11-26 15:47:18 +02004594static const struct instr_dual instr_dual_0f_2b = {
4595 I(0, em_mov), N
4596};
4597
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004598static const struct gprefix pfx_0f_2b = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004599 ID(0, &instr_dual_0f_2b), ID(0, &instr_dual_0f_2b), N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03004600};
4601
Stefan Fritsch29916962018-04-01 17:54:44 +02004602static const struct gprefix pfx_0f_10_0f_11 = {
4603 I(Unaligned, em_mov), I(Unaligned, em_mov), N, N,
4604};
4605
Igor Mammedov27ce8252014-03-15 21:01:59 +01004606static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01004607 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004608};
4609
Alex Williamson0a370272014-07-11 11:56:31 -06004610static const struct gprefix pfx_0f_e7 = {
4611 N, I(Sse, em_mov), N, N,
4612};
4613
Gleb Natapov045a2822012-12-20 16:57:43 +02004614static const struct escape escape_d9 = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02004615 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstcw),
Gleb Natapov045a2822012-12-20 16:57:43 +02004616}, {
4617 /* 0xC0 - 0xC7 */
4618 N, N, N, N, N, N, N, N,
4619 /* 0xC8 - 0xCF */
4620 N, N, N, N, N, N, N, N,
4621 /* 0xD0 - 0xC7 */
4622 N, N, N, N, N, N, N, N,
4623 /* 0xD8 - 0xDF */
4624 N, N, N, N, N, N, N, N,
4625 /* 0xE0 - 0xE7 */
4626 N, N, N, N, N, N, N, N,
4627 /* 0xE8 - 0xEF */
4628 N, N, N, N, N, N, N, N,
4629 /* 0xF0 - 0xF7 */
4630 N, N, N, N, N, N, N, N,
4631 /* 0xF8 - 0xFF */
4632 N, N, N, N, N, N, N, N,
4633} };
4634
4635static const struct escape escape_db = { {
4636 N, N, N, N, N, N, N, N,
4637}, {
4638 /* 0xC0 - 0xC7 */
4639 N, N, N, N, N, N, N, N,
4640 /* 0xC8 - 0xCF */
4641 N, N, N, N, N, N, N, N,
4642 /* 0xD0 - 0xC7 */
4643 N, N, N, N, N, N, N, N,
4644 /* 0xD8 - 0xDF */
4645 N, N, N, N, N, N, N, N,
4646 /* 0xE0 - 0xE7 */
4647 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
4648 /* 0xE8 - 0xEF */
4649 N, N, N, N, N, N, N, N,
4650 /* 0xF0 - 0xF7 */
4651 N, N, N, N, N, N, N, N,
4652 /* 0xF8 - 0xFF */
4653 N, N, N, N, N, N, N, N,
4654} };
4655
4656static const struct escape escape_dd = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02004657 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstsw),
Gleb Natapov045a2822012-12-20 16:57:43 +02004658}, {
4659 /* 0xC0 - 0xC7 */
4660 N, N, N, N, N, N, N, N,
4661 /* 0xC8 - 0xCF */
4662 N, N, N, N, N, N, N, N,
4663 /* 0xD0 - 0xC7 */
4664 N, N, N, N, N, N, N, N,
4665 /* 0xD8 - 0xDF */
4666 N, N, N, N, N, N, N, N,
4667 /* 0xE0 - 0xE7 */
4668 N, N, N, N, N, N, N, N,
4669 /* 0xE8 - 0xEF */
4670 N, N, N, N, N, N, N, N,
4671 /* 0xF0 - 0xF7 */
4672 N, N, N, N, N, N, N, N,
4673 /* 0xF8 - 0xFF */
4674 N, N, N, N, N, N, N, N,
4675} };
4676
Nadav Amit39f062f2014-11-26 15:47:18 +02004677static const struct instr_dual instr_dual_0f_c3 = {
4678 I(DstMem | SrcReg | ModRM | No16 | Mov, em_mov), N
4679};
4680
Nadav Amit2276b512015-01-26 09:32:24 +02004681static const struct mode_dual mode_dual_63 = {
4682 N, I(DstReg | SrcMem32 | ModRM | Mov, em_movsxd)
4683};
4684
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004685static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004686 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004687 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004688 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
4689 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004690 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004691 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004692 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
4693 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004694 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004695 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004696 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
4697 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004698 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004699 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004700 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
4701 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004702 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004703 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004704 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004705 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004706 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004707 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004708 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004709 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004710 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02004711 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004712 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03004713 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004714 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09004715 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004716 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09004717 I(ImplicitOps | Stack | No64, em_pusha),
4718 I(ImplicitOps | Stack | No64, em_popa),
Nadav Amit2276b512015-01-26 09:32:24 +02004719 N, MD(ModRM, &mode_dual_63),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004720 N, N, N, N,
4721 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03004722 I(SrcImm | Mov | Stack, em_push),
4723 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03004724 I(SrcImmByte | Mov | Stack, em_push),
4725 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004726 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09004727 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03004728 /* 0x70 - 0x7F */
Nadav Amit58b70752014-10-24 11:35:09 +03004729 X16(D(SrcImmByte | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004730 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004731 G(ByteOp | DstMem | SrcImm, group1),
4732 G(DstMem | SrcImm, group1),
4733 G(ByteOp | DstMem | SrcImm | No64, group1),
4734 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02004735 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004736 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004737 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004738 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004739 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004740 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09004741 D(ModRM | SrcMem | NoAccess | DstReg),
4742 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
4743 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004744 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004745 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004746 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03004747 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08004748 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09004749 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01004750 II(ImplicitOps | Stack, em_popf, popf),
4751 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004752 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004753 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004754 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Tom Lendacky0f89b202016-12-14 14:59:23 -05004755 I2bv(SrcSI | DstDI | Mov | String | TwoMemOp, em_mov),
4756 F2bv(SrcSI | DstDI | String | NoWrite | TwoMemOp, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004757 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004758 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004759 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
4760 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004761 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004762 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004763 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004764 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02004765 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004766 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004767 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Nadav Amit58b70752014-10-24 11:35:09 +03004768 I(ImplicitOps | NearBranch | SrcImmU16, em_ret_near_imm),
4769 I(ImplicitOps | NearBranch, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03004770 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
4771 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004772 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004773 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03004774 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Nadav Amit16794aa2015-01-26 09:32:22 +02004775 I(ImplicitOps | SrcImmU16, em_ret_far_imm),
4776 I(ImplicitOps, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02004777 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004778 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004779 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004780 G(Src2One | ByteOp, group2), G(Src2One, group2),
4781 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02004782 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02004783 I(DstAcc | SrcImmUByte | No64, em_aad),
4784 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004785 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004786 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02004787 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004788 /* 0xE0 - 0xE7 */
Nadav Amit58b70752014-10-24 11:35:09 +03004789 X3(I(SrcImmByte | NearBranch, em_loop)),
4790 I(SrcImmByte | NearBranch, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004791 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
4792 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004793 /* 0xE8 - 0xEF */
Nadav Amit58b70752014-10-24 11:35:09 +03004794 I(SrcImm | NearBranch, em_call), D(SrcImm | ImplicitOps | NearBranch),
4795 I(SrcImmFAddr | No64, em_jmp_far),
4796 D(SrcImmByte | ImplicitOps | NearBranch),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004797 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
4798 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004799 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004800 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004801 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4802 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004803 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09004804 D(ImplicitOps), D(ImplicitOps),
4805 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004806 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4807};
4808
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004809static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004810 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004811 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02004812 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004813 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004814 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004815 N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004816 /* 0x10 - 0x1F */
Stefan Fritsch29916962018-04-01 17:54:44 +02004817 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_10_0f_11),
4818 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_10_0f_11),
4819 N, N, N, N, N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004820 D(ImplicitOps | ModRM | SrcMem | NoAccess),
4821 N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004822 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03004823 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
4824 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
4825 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
4826 check_cr_write),
4827 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
4828 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004829 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004830 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
4831 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004832 N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b),
Avi Kivity3e114eb2012-04-09 18:40:01 +03004833 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004834 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004835 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004836 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004837 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004838 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02004839 I(ImplicitOps | EmulateOnUD, em_sysenter),
4840 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004841 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004842 N, N, N, N, N, N, N, N,
4843 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03004844 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004845 /* 0x50 - 0x5F */
4846 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4847 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004848 N, N, N, N,
4849 N, N, N, N,
4850 N, N, N, N,
4851 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004852 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004853 N, N, N, N,
4854 N, N, N, N,
4855 N, N, N, N,
4856 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004857 /* 0x80 - 0x8F */
Nadav Amit58b70752014-10-24 11:35:09 +03004858 X16(D(SrcImm | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004859 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004860 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004861 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004862 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004863 II(ImplicitOps, em_cpuid, cpuid),
4864 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004865 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4866 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004867 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004868 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Paolo Bonzini89651a32015-11-03 13:43:05 +01004869 II(EmulateOnUD | ImplicitOps, em_rsm, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004870 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004871 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4872 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Nadav Amit13e457e2014-10-13 13:04:13 +03004873 GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004874 /* 0xB0 - 0xB7 */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02004875 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable | SrcWrite, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004876 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004877 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004878 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4879 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004880 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004881 /* 0xB8 - 0xBF */
4882 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004883 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004884 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Nadav Amit900efe22015-03-30 15:39:21 +03004885 I(DstReg | SrcMem | ModRM, em_bsf_c),
4886 I(DstReg | SrcMem | ModRM, em_bsr_c),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004887 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004888 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02004889 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Nadav Amit39f062f2014-11-26 15:47:18 +02004890 N, ID(0, &instr_dual_0f_c3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004891 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004892 /* 0xC8 - 0xCF */
4893 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004894 /* 0xD0 - 0xDF */
4895 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4896 /* 0xE0 - 0xEF */
Alex Williamson0a370272014-07-11 11:56:31 -06004897 N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
4898 N, N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004899 /* 0xF0 - 0xFF */
4900 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4901};
4902
Nadav Amit39f062f2014-11-26 15:47:18 +02004903static const struct instr_dual instr_dual_0f_38_f0 = {
4904 I(DstReg | SrcMem | Mov, em_movbe), N
4905};
4906
4907static const struct instr_dual instr_dual_0f_38_f1 = {
4908 I(DstMem | SrcReg | Mov, em_movbe), N
4909};
4910
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004911static const struct gprefix three_byte_0f_38_f0 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004912 ID(0, &instr_dual_0f_38_f0), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004913};
4914
4915static const struct gprefix three_byte_0f_38_f1 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004916 ID(0, &instr_dual_0f_38_f1), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004917};
4918
4919/*
4920 * Insns below are selected by the prefix which indexed by the third opcode
4921 * byte.
4922 */
4923static const struct opcode opcode_map_0f_38[256] = {
4924 /* 0x00 - 0x7f */
4925 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004926 /* 0x80 - 0xef */
4927 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4928 /* 0xf0 - 0xf1 */
Nadav Amit53bb4f72014-12-07 11:49:42 +02004929 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0),
4930 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004931 /* 0xf2 - 0xff */
4932 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004933};
4934
Avi Kivity73fba5f2010-07-29 15:11:53 +03004935#undef D
4936#undef N
4937#undef G
4938#undef GD
4939#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004940#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004941#undef EXT
Nadav Amit2276b512015-01-26 09:32:24 +02004942#undef MD
Nadav Amit2b42fce2015-01-26 09:32:25 +02004943#undef ID
Avi Kivity73fba5f2010-07-29 15:11:53 +03004944
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004945#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004946#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004947#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004948#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004949#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004950
Avi Kivity9dac77f2011-06-01 15:34:25 +03004951static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004952{
4953 unsigned size;
4954
Avi Kivity9dac77f2011-06-01 15:34:25 +03004955 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004956 if (size == 8)
4957 size = 4;
4958 return size;
4959}
4960
4961static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4962 unsigned size, bool sign_extension)
4963{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004964 int rc = X86EMUL_CONTINUE;
4965
4966 op->type = OP_IMM;
4967 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004968 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004969 /* NB. Immediates are sign-extended as necessary. */
4970 switch (op->bytes) {
4971 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004972 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004973 break;
4974 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004975 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004976 break;
4977 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004978 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004979 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004980 case 8:
4981 op->val = insn_fetch(s64, ctxt);
4982 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004983 }
4984 if (!sign_extension) {
4985 switch (op->bytes) {
4986 case 1:
4987 op->val &= 0xff;
4988 break;
4989 case 2:
4990 op->val &= 0xffff;
4991 break;
4992 case 4:
4993 op->val &= 0xffffffff;
4994 break;
4995 }
4996 }
4997done:
4998 return rc;
4999}
5000
Avi Kivitya99455492011-09-13 10:45:41 +03005001static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
5002 unsigned d)
5003{
5004 int rc = X86EMUL_CONTINUE;
5005
5006 switch (d) {
5007 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02005008 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03005009 break;
5010 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03005011 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03005012 break;
5013 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03005014 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03005015 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03005016 *op = ctxt->memop;
5017 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02005018 if (ctxt->d & BitOp)
Avi Kivitya99455492011-09-13 10:45:41 +03005019 fetch_bit_operand(ctxt);
5020 op->orig_val = op->val;
5021 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03005022 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03005023 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03005024 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03005025 case OpAcc:
5026 op->type = OP_REG;
5027 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005028 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03005029 fetch_register_operand(op);
5030 op->orig_val = op->val;
5031 break;
Avi Kivity820207c2013-02-09 11:31:45 +02005032 case OpAccLo:
5033 op->type = OP_REG;
5034 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
5035 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
5036 fetch_register_operand(op);
5037 op->orig_val = op->val;
5038 break;
5039 case OpAccHi:
5040 if (ctxt->d & ByteOp) {
5041 op->type = OP_NONE;
5042 break;
5043 }
5044 op->type = OP_REG;
5045 op->bytes = ctxt->op_bytes;
5046 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
5047 fetch_register_operand(op);
5048 op->orig_val = op->val;
5049 break;
Avi Kivitya99455492011-09-13 10:45:41 +03005050 case OpDI:
5051 op->type = OP_MEM;
5052 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
5053 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01005054 register_address(ctxt, VCPU_REGS_RDI);
Avi Kivitya99455492011-09-13 10:45:41 +03005055 op->addr.mem.seg = VCPU_SREG_ES;
5056 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005057 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03005058 break;
5059 case OpDX:
5060 op->type = OP_REG;
5061 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005062 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03005063 fetch_register_operand(op);
5064 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03005065 case OpCL:
Nadav Amitd29b9d72014-11-02 11:54:47 +02005066 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03005067 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005068 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03005069 break;
5070 case OpImmByte:
5071 rc = decode_imm(ctxt, op, 1, true);
5072 break;
5073 case OpOne:
Nadav Amitd29b9d72014-11-02 11:54:47 +02005074 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03005075 op->bytes = 1;
5076 op->val = 1;
5077 break;
5078 case OpImm:
5079 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
5080 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02005081 case OpImm64:
5082 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
5083 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02005084 case OpMem8:
5085 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03005086 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02005087 ctxt->memop.addr.reg = decode_register(ctxt,
5088 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03005089 fetch_register_operand(&ctxt->memop);
5090 }
Avi Kivity28867ce2012-01-16 15:08:44 +02005091 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03005092 case OpMem16:
5093 ctxt->memop.bytes = 2;
5094 goto mem_common;
5095 case OpMem32:
5096 ctxt->memop.bytes = 4;
5097 goto mem_common;
5098 case OpImmU16:
5099 rc = decode_imm(ctxt, op, 2, false);
5100 break;
5101 case OpImmU:
5102 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
5103 break;
5104 case OpSI:
5105 op->type = OP_MEM;
5106 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
5107 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01005108 register_address(ctxt, VCPU_REGS_RSI);
Bandan Das573e80f2014-04-16 12:46:13 -04005109 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03005110 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005111 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03005112 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02005113 case OpXLat:
5114 op->type = OP_MEM;
5115 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
5116 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01005117 address_mask(ctxt,
Paolo Bonzini7fa57952013-05-09 11:32:50 +02005118 reg_read(ctxt, VCPU_REGS_RBX) +
5119 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04005120 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02005121 op->val = 0;
5122 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03005123 case OpImmFAddr:
5124 op->type = OP_IMM;
5125 op->addr.mem.ea = ctxt->_eip;
5126 op->bytes = ctxt->op_bytes + 2;
5127 insn_fetch_arr(op->valptr, op->bytes, ctxt);
5128 break;
5129 case OpMemFAddr:
5130 ctxt->memop.bytes = ctxt->op_bytes + 2;
5131 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03005132 case OpES:
Nadav Amitd29b9d72014-11-02 11:54:47 +02005133 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03005134 op->val = VCPU_SREG_ES;
5135 break;
5136 case OpCS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02005137 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03005138 op->val = VCPU_SREG_CS;
5139 break;
5140 case OpSS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02005141 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03005142 op->val = VCPU_SREG_SS;
5143 break;
5144 case OpDS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02005145 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03005146 op->val = VCPU_SREG_DS;
5147 break;
5148 case OpFS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02005149 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03005150 op->val = VCPU_SREG_FS;
5151 break;
5152 case OpGS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02005153 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03005154 op->val = VCPU_SREG_GS;
5155 break;
Avi Kivitya99455492011-09-13 10:45:41 +03005156 case OpImplicit:
5157 /* Special instructions do their own operand decoding. */
5158 default:
5159 op->type = OP_NONE; /* Disable writeback. */
5160 break;
5161 }
5162
5163done:
5164 return rc;
5165}
5166
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09005167int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005168{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005169 int rc = X86EMUL_CONTINUE;
5170 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03005171 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005172 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04005173 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03005174 struct opcode opcode;
Wanpeng Li3853be22017-11-05 16:54:47 -08005175 u16 dummy;
5176 struct desc_struct desc;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005177
Avi Kivityf09ed832011-09-13 10:45:40 +03005178 ctxt->memop.type = OP_NONE;
5179 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005180 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02005181 ctxt->fetch.ptr = ctxt->fetch.data;
5182 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02005183 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01005184 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005185 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02005186 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02005187 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02005188 if (rc != X86EMUL_CONTINUE)
Sean Christopherson016cd752019-08-15 09:20:32 -07005189 goto done;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02005190 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005191
5192 switch (mode) {
5193 case X86EMUL_MODE_REAL:
5194 case X86EMUL_MODE_VM86:
Wanpeng Li3853be22017-11-05 16:54:47 -08005195 def_op_bytes = def_ad_bytes = 2;
5196 ctxt->ops->get_segment(ctxt, &dummy, &desc, NULL, VCPU_SREG_CS);
5197 if (desc.d)
5198 def_op_bytes = def_ad_bytes = 4;
5199 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005200 case X86EMUL_MODE_PROT16:
5201 def_op_bytes = def_ad_bytes = 2;
5202 break;
5203 case X86EMUL_MODE_PROT32:
5204 def_op_bytes = def_ad_bytes = 4;
5205 break;
5206#ifdef CONFIG_X86_64
5207 case X86EMUL_MODE_PROT64:
5208 def_op_bytes = 4;
5209 def_ad_bytes = 8;
5210 break;
5211#endif
5212 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005213 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005214 }
5215
Avi Kivity9dac77f2011-06-01 15:34:25 +03005216 ctxt->op_bytes = def_op_bytes;
5217 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005218
5219 /* Legacy prefixes. */
5220 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09005221 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005222 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005223 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005224 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005225 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005226 break;
5227 case 0x67: /* address-size override */
5228 if (mode == X86EMUL_MODE_PROT64)
5229 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005230 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005231 else
5232 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005233 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005234 break;
5235 case 0x26: /* ES override */
Marios Pomonis125ffc52019-12-11 12:47:50 -08005236 has_seg_override = true;
5237 ctxt->seg_override = VCPU_SREG_ES;
5238 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005239 case 0x2e: /* CS override */
Marios Pomonis125ffc52019-12-11 12:47:50 -08005240 has_seg_override = true;
5241 ctxt->seg_override = VCPU_SREG_CS;
5242 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005243 case 0x36: /* SS override */
Marios Pomonis125ffc52019-12-11 12:47:50 -08005244 has_seg_override = true;
5245 ctxt->seg_override = VCPU_SREG_SS;
5246 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005247 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04005248 has_seg_override = true;
Marios Pomonis125ffc52019-12-11 12:47:50 -08005249 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005250 break;
5251 case 0x64: /* FS override */
Marios Pomonis125ffc52019-12-11 12:47:50 -08005252 has_seg_override = true;
5253 ctxt->seg_override = VCPU_SREG_FS;
5254 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005255 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04005256 has_seg_override = true;
Marios Pomonis125ffc52019-12-11 12:47:50 -08005257 ctxt->seg_override = VCPU_SREG_GS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005258 break;
5259 case 0x40 ... 0x4f: /* REX */
5260 if (mode != X86EMUL_MODE_PROT64)
5261 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005262 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005263 continue;
5264 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005265 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005266 break;
5267 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005268 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005269 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005270 break;
5271 default:
5272 goto done_prefixes;
5273 }
5274
5275 /* Any legacy prefix after a REX prefix nullifies its effect. */
5276
Avi Kivity9dac77f2011-06-01 15:34:25 +03005277 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005278 }
5279
5280done_prefixes:
5281
5282 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005283 if (ctxt->rex_prefix & 8)
5284 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005285
5286 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005287 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08005288 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005289 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02005290 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09005291 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03005292 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005293
5294 /* 0F_38 opcode map */
5295 if (ctxt->b == 0x38) {
5296 ctxt->opcode_len = 3;
5297 ctxt->b = insn_fetch(u8, ctxt);
5298 opcode = opcode_map_0f_38[ctxt->b];
5299 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005300 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03005301 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005302
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09005303 if (ctxt->d & ModRM)
5304 ctxt->modrm = insn_fetch(u8, ctxt);
5305
Nadav Amit7fe864d2014-06-02 18:34:03 +03005306 /* vex-prefix instructions are not implemented */
5307 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
Nadav Amitd14cb5d2014-11-02 11:54:58 +02005308 (mode == X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) == 0xc0)) {
Nadav Amit7fe864d2014-06-02 18:34:03 +03005309 ctxt->d = NotImpl;
5310 }
5311
Avi Kivity9dac77f2011-06-01 15:34:25 +03005312 while (ctxt->d & GroupMask) {
5313 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03005314 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005315 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03005316 opcode = opcode.u.group[goffset];
5317 break;
5318 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005319 goffset = (ctxt->modrm >> 3) & 7;
5320 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03005321 opcode = opcode.u.gdual->mod3[goffset];
5322 else
5323 opcode = opcode.u.gdual->mod012[goffset];
5324 break;
5325 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005326 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02005327 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03005328 break;
5329 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005330 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005331 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005332 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03005333 switch (simd_prefix) {
5334 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
5335 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
5336 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
5337 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
5338 }
5339 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02005340 case Escape:
Marios Pomonis3c9053a22019-12-11 12:47:41 -08005341 if (ctxt->modrm > 0xbf) {
5342 size_t size = ARRAY_SIZE(opcode.u.esc->high);
5343 u32 index = array_index_nospec(
5344 ctxt->modrm - 0xc0, size);
5345
5346 opcode = opcode.u.esc->high[index];
5347 } else {
Gleb Natapov045a2822012-12-20 16:57:43 +02005348 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
Marios Pomonis3c9053a22019-12-11 12:47:41 -08005349 }
Gleb Natapov045a2822012-12-20 16:57:43 +02005350 break;
Nadav Amit39f062f2014-11-26 15:47:18 +02005351 case InstrDual:
5352 if ((ctxt->modrm >> 6) == 3)
5353 opcode = opcode.u.idual->mod3;
5354 else
5355 opcode = opcode.u.idual->mod012;
5356 break;
Nadav Amit2276b512015-01-26 09:32:24 +02005357 case ModeDual:
5358 if (ctxt->mode == X86EMUL_MODE_PROT64)
5359 opcode = opcode.u.mdual->mode64;
5360 else
5361 opcode = opcode.u.mdual->mode32;
5362 break;
Avi Kivity46561642011-04-24 14:09:59 +03005363 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005364 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005365 }
Avi Kivity46561642011-04-24 14:09:59 +03005366
Avi Kivityb1ea50b2011-09-13 10:45:42 +03005367 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005368 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005369 }
5370
Paolo Bonzinie24186e2014-03-27 12:00:57 +01005371 /* Unrecognised? */
5372 if (ctxt->d == 0)
5373 return EMULATION_FAILED;
5374
Avi Kivity9dac77f2011-06-01 15:34:25 +03005375 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005376
Nadav Amit3a6095a2014-08-13 16:50:13 +03005377 if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD)))
5378 return EMULATION_FAILED;
5379
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005380 if (unlikely(ctxt->d &
Nadav Amited9aad22014-11-02 11:55:00 +02005381 (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch|
5382 No16))) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005383 /*
5384 * These are copied unconditionally here, and checked unconditionally
5385 * in x86_emulate_insn.
5386 */
5387 ctxt->check_perm = opcode.check_perm;
5388 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005389
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005390 if (ctxt->d & NotImpl)
5391 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02005392
Nadav Amit58b70752014-10-24 11:35:09 +03005393 if (mode == X86EMUL_MODE_PROT64) {
5394 if (ctxt->op_bytes == 4 && (ctxt->d & Stack))
5395 ctxt->op_bytes = 8;
5396 else if (ctxt->d & NearBranch)
5397 ctxt->op_bytes = 8;
5398 }
Avi Kivity7f9b4b72010-08-01 14:46:54 +03005399
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005400 if (ctxt->d & Op3264) {
5401 if (mode == X86EMUL_MODE_PROT64)
5402 ctxt->op_bytes = 8;
5403 else
5404 ctxt->op_bytes = 4;
5405 }
5406
Nadav Amited9aad22014-11-02 11:55:00 +02005407 if ((ctxt->d & No16) && ctxt->op_bytes == 2)
5408 ctxt->op_bytes = 4;
5409
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005410 if (ctxt->d & Sse)
5411 ctxt->op_bytes = 16;
5412 else if (ctxt->d & Mmx)
5413 ctxt->op_bytes = 8;
5414 }
Avi Kivity1253791d2011-03-29 11:41:27 +02005415
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005416 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005417 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03005418 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04005419 if (!has_seg_override) {
5420 has_seg_override = true;
5421 ctxt->seg_override = ctxt->modrm_seg;
5422 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03005423 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03005424 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005425 if (rc != X86EMUL_CONTINUE)
5426 goto done;
5427
Bandan Das573e80f2014-04-16 12:46:13 -04005428 if (!has_seg_override)
5429 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005430
Bandan Das573e80f2014-04-16 12:46:13 -04005431 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005432
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005433 /*
5434 * Decode and fetch the source operand: register, memory
5435 * or immediate.
5436 */
Avi Kivity0fe59122011-09-13 10:45:47 +03005437 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03005438 if (rc != X86EMUL_CONTINUE)
5439 goto done;
5440
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005441 /*
5442 * Decode and fetch the second source operand: register, memory
5443 * or immediate.
5444 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03005445 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03005446 if (rc != X86EMUL_CONTINUE)
5447 goto done;
5448
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005449 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03005450 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005451
Owen Hofmannd9092f52016-10-27 11:25:52 -07005452 if (ctxt->rip_relative && likely(ctxt->memopp))
Nadav Amit1c1c35a2014-11-19 17:43:09 +02005453 ctxt->memopp->addr.mem.ea = address_mask(ctxt,
5454 ctxt->memopp->addr.mem.ea + ctxt->_eip);
Avi Kivitycb16c342011-06-19 19:21:11 +03005455
Paolo Bonzinia430c912014-10-23 14:54:14 +02005456done:
Jan Dakinevichc8848ce2019-08-27 13:07:08 +00005457 if (rc == X86EMUL_PROPAGATE_FAULT)
5458 ctxt->have_exception = true;
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005459 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005460}
5461
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005462bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
5463{
5464 return ctxt->d & PageTable;
5465}
5466
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005467static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
5468{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005469 /* The second termination condition only applies for REPE
5470 * and REPNE. Test if the repeat string operation prefix is
5471 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
5472 * corresponding termination condition according to:
5473 * - if REPE/REPZ and ZF = 0 then done
5474 * - if REPNE/REPNZ and ZF = 1 then done
5475 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005476 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
5477 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
5478 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03005479 ((ctxt->eflags & X86_EFLAGS_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03005480 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03005481 ((ctxt->eflags & X86_EFLAGS_ZF) == X86_EFLAGS_ZF))))
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005482 return true;
5483
5484 return false;
5485}
5486
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005487static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
5488{
Radim Krčmářaabba3c2016-11-08 20:54:18 +01005489 int rc;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005490
Sean Christophersona7baead2020-01-17 11:30:50 -08005491 emulator_get_fpu();
Radim Krčmářaabba3c2016-11-08 20:54:18 +01005492 rc = asm_safe("fwait");
Sean Christophersona7baead2020-01-17 11:30:50 -08005493 emulator_put_fpu();
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005494
Radim Krčmářaabba3c2016-11-08 20:54:18 +01005495 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005496 return emulate_exception(ctxt, MF_VECTOR, 0, false);
5497
5498 return X86EMUL_CONTINUE;
5499}
5500
Sean Christophersonc0a21c32020-01-17 11:30:52 -08005501static void fetch_possible_mmx_operand(struct operand *op)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005502{
5503 if (op->type == OP_MM)
Sean Christophersonc0a21c32020-01-17 11:30:52 -08005504 read_mmx_reg(&op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005505}
5506
Sean Christopherson3009afc2020-01-21 20:43:39 -08005507static int fastop(struct x86_emulate_ctxt *ctxt, fastop_t fop)
Avi Kivitye28bbd42013-01-04 16:18:48 +02005508{
5509 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005510
Avi Kivityb9fa4092013-02-09 11:31:48 +02005511 if (!(ctxt->d & ByteOp))
5512 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005513
Peter Zijlstra1a29b5b2018-01-25 10:58:13 +01005514 asm("push %[flags]; popf; " CALL_NOSPEC " ; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005515 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
Peter Zijlstra1a29b5b2018-01-25 10:58:13 +01005516 [thunk_target]"+S"(fop), ASM_CALL_CONSTRAINT
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005517 : "c"(ctxt->src2.val));
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005518
Avi Kivitye28bbd42013-01-04 16:18:48 +02005519 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005520 if (!fop) /* exception is returned in fop variable */
5521 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02005522 return X86EMUL_CONTINUE;
5523}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005524
Bandan Das14985072014-04-16 12:46:09 -04005525void init_decode_cache(struct x86_emulate_ctxt *ctxt)
5526{
Bandan Das573e80f2014-04-16 12:46:13 -04005527 memset(&ctxt->rip_relative, 0,
5528 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04005529
Bandan Das14985072014-04-16 12:46:09 -04005530 ctxt->io_read.pos = 0;
5531 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04005532 ctxt->mem_read.end = 0;
5533}
5534
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09005535int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005536{
Mathias Krause0225fb52012-08-30 01:30:16 +02005537 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09005538 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005539 int saved_dst_type = ctxt->dst.type;
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005540 unsigned emul_flags;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005541
Avi Kivity9dac77f2011-06-01 15:34:25 +03005542 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04005543
Gleb Natapovd380a5e2010-02-10 14:21:36 +02005544 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005545 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02005546 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02005547 goto done;
5548 }
5549
Avi Kivity9dac77f2011-06-01 15:34:25 +03005550 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02005551 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03005552 goto done;
5553 }
5554
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005555 emul_flags = ctxt->ops->get_hflags(ctxt);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005556 if (unlikely(ctxt->d &
5557 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
5558 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
5559 (ctxt->d & Undefined)) {
5560 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005561 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005562 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005563
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005564 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
5565 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
5566 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02005567 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005568 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02005569
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005570 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
5571 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02005572 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005573 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02005574
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005575 if (ctxt->d & Mmx) {
5576 rc = flush_pending_x87_faults(ctxt);
5577 if (rc != X86EMUL_CONTINUE)
5578 goto done;
5579 /*
5580 * Now that we know the fpu is exception safe, we can fetch
5581 * operands from it.
5582 */
Sean Christophersonc0a21c32020-01-17 11:30:52 -08005583 fetch_possible_mmx_operand(&ctxt->src);
5584 fetch_possible_mmx_operand(&ctxt->src2);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005585 if (!(ctxt->d & Mov))
Sean Christophersonc0a21c32020-01-17 11:30:52 -08005586 fetch_possible_mmx_operand(&ctxt->dst);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005587 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02005588
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005589 if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && ctxt->intercept) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005590 rc = emulator_check_intercept(ctxt, ctxt->intercept,
5591 X86_ICPT_PRE_EXCEPT);
5592 if (rc != X86EMUL_CONTINUE)
5593 goto done;
5594 }
5595
Nadav Amit64a38292014-12-10 11:19:04 +02005596 /* Instruction can only be executed in protected mode */
5597 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
5598 rc = emulate_ud(ctxt);
5599 goto done;
5600 }
5601
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005602 /* Privileged instruction can be executed only in CPL=0 */
5603 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Nadav Amit68efa762014-06-18 17:19:35 +03005604 if (ctxt->d & PrivUD)
5605 rc = emulate_ud(ctxt);
5606 else
5607 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02005608 goto done;
5609 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005610
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005611 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04005612 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005613 rc = ctxt->check_perm(ctxt);
5614 if (rc != X86EMUL_CONTINUE)
5615 goto done;
5616 }
5617
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005618 if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005619 rc = emulator_check_intercept(ctxt, ctxt->intercept,
5620 X86_ICPT_POST_EXCEPT);
5621 if (rc != X86EMUL_CONTINUE)
5622 goto done;
5623 }
5624
5625 if (ctxt->rep_prefix && (ctxt->d & String)) {
5626 /* All REP prefixes have the same first termination condition */
5627 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Nadav Amit428e3d02015-04-28 13:06:01 +03005628 string_registers_quirk(ctxt);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005629 ctxt->eip = ctxt->_eip;
Nadav Amit0efb0442015-03-29 16:33:03 +03005630 ctxt->eflags &= ~X86_EFLAGS_RF;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005631 goto done;
5632 }
5633 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02005634 }
5635
Avi Kivity9dac77f2011-06-01 15:34:25 +03005636 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
5637 rc = segmented_read(ctxt, ctxt->src.addr.mem,
5638 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09005639 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005640 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005641 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005642 }
5643
Avi Kivity9dac77f2011-06-01 15:34:25 +03005644 if (ctxt->src2.type == OP_MEM) {
5645 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
5646 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02005647 if (rc != X86EMUL_CONTINUE)
5648 goto done;
5649 }
5650
Avi Kivity9dac77f2011-06-01 15:34:25 +03005651 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005652 goto special_insn;
5653
5654
Avi Kivity9dac77f2011-06-01 15:34:25 +03005655 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02005656 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005657 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
5658 &ctxt->dst.val, ctxt->dst.bytes);
Nadav Amitc205fb72014-12-25 02:52:16 +02005659 if (rc != X86EMUL_CONTINUE) {
Paolo Bonzinid44e1212015-02-09 10:02:05 +01005660 if (!(ctxt->d & NoWrite) &&
5661 rc == X86EMUL_PROPAGATE_FAULT &&
Nadav Amitc205fb72014-12-25 02:52:16 +02005662 ctxt->exception.vector == PF_VECTOR)
5663 ctxt->exception.error_code |= PFERR_WRITE_MASK;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02005664 goto done;
Nadav Amitc205fb72014-12-25 02:52:16 +02005665 }
Avi Kivity038e51d2007-01-22 20:40:40 -08005666 }
Paolo Bonzini4ff6f8e2015-02-12 17:04:47 +01005667 /* Copy full 64-bit value for CMPXCHG8B. */
5668 ctxt->dst.orig_val64 = ctxt->dst.val64;
Avi Kivity038e51d2007-01-22 20:40:40 -08005669
Avi Kivity018a98d2007-11-27 19:30:56 +02005670special_insn:
5671
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005672 if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03005673 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005674 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02005675 if (rc != X86EMUL_CONTINUE)
5676 goto done;
5677 }
5678
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03005679 if (ctxt->rep_prefix && (ctxt->d & String))
Nadav Amit0efb0442015-03-29 16:33:03 +03005680 ctxt->eflags |= X86_EFLAGS_RF;
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03005681 else
Nadav Amit0efb0442015-03-29 16:33:03 +03005682 ctxt->eflags &= ~X86_EFLAGS_RF;
Nadav Amit4467c3f2014-07-21 14:37:29 +03005683
Avi Kivity9dac77f2011-06-01 15:34:25 +03005684 if (ctxt->execute) {
Sean Christopherson3009afc2020-01-21 20:43:39 -08005685 if (ctxt->d & Fastop)
5686 rc = fastop(ctxt, (fastop_t)ctxt->execute);
5687 else
Miaohe Lin52db3692020-01-22 11:21:44 +08005688 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03005689 if (rc != X86EMUL_CONTINUE)
5690 goto done;
5691 goto writeback;
5692 }
5693
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02005694 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005695 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005696 else if (ctxt->opcode_len == 3)
5697 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005698
Avi Kivity9dac77f2011-06-01 15:34:25 +03005699 switch (ctxt->b) {
Gleb Natapovb2833e32009-04-12 13:36:30 +03005700 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005701 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005702 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005703 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03005704 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005705 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03005706 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03005707 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005708 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03005709 ctxt->dst.type = OP_NONE;
5710 else
5711 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09005712 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005713 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005714 switch (ctxt->op_bytes) {
5715 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
5716 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
5717 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005718 }
5719 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005720 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005721 rc = emulate_int(ctxt, 3);
5722 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005723 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005724 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005725 break;
5726 case 0xce: /* into */
Nadav Amit0efb0442015-03-29 16:33:03 +03005727 if (ctxt->eflags & X86_EFLAGS_OF)
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005728 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005729 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005730 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09005731 case 0xeb: /* jmp rel short */
Nadav Amit234f3ce2014-09-18 22:39:38 +03005732 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity9dac77f2011-06-01 15:34:25 +03005733 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005734 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005735 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03005736 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03005737 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005738 case 0xf5: /* cmc */
5739 /* complement carry flag from eflags reg */
Nadav Amit0efb0442015-03-29 16:33:03 +03005740 ctxt->eflags ^= X86_EFLAGS_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005741 break;
5742 case 0xf8: /* clc */
Nadav Amit0efb0442015-03-29 16:33:03 +03005743 ctxt->eflags &= ~X86_EFLAGS_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005744 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03005745 case 0xf9: /* stc */
Nadav Amit0efb0442015-03-29 16:33:03 +03005746 ctxt->eflags |= X86_EFLAGS_CF;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03005747 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005748 case 0xfc: /* cld */
Nadav Amit0efb0442015-03-29 16:33:03 +03005749 ctxt->eflags &= ~X86_EFLAGS_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005750 break;
5751 case 0xfd: /* std */
Nadav Amit0efb0442015-03-29 16:33:03 +03005752 ctxt->eflags |= X86_EFLAGS_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005753 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005754 default:
5755 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005756 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005757
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005758 if (rc != X86EMUL_CONTINUE)
5759 goto done;
5760
Avi Kivity018a98d2007-11-27 19:30:56 +02005761writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02005762 if (ctxt->d & SrcWrite) {
5763 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
5764 rc = writeback(ctxt, &ctxt->src);
5765 if (rc != X86EMUL_CONTINUE)
5766 goto done;
5767 }
Nadav Amitee212292014-06-15 16:12:58 +03005768 if (!(ctxt->d & NoWrite)) {
5769 rc = writeback(ctxt, &ctxt->dst);
5770 if (rc != X86EMUL_CONTINUE)
5771 goto done;
5772 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005773
Gleb Natapov5cd21912010-03-18 15:20:26 +02005774 /*
5775 * restore dst type in case the decoding will be reused
5776 * (happens for string instruction )
5777 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005778 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005779
Avi Kivity9dac77f2011-06-01 15:34:25 +03005780 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005781 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02005782
Avi Kivity9dac77f2011-06-01 15:34:25 +03005783 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005784 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02005785
Avi Kivity9dac77f2011-06-01 15:34:25 +03005786 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005787 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005788 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005789 if ((ctxt->d & SrcMask) == SrcSI)
5790 count = ctxt->src.count;
5791 else
5792 count = ctxt->dst.count;
Paolo Bonzini01485a22014-11-19 18:25:08 +01005793 register_address_increment(ctxt, VCPU_REGS_RCX, -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005794
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005795 if (!string_insn_completed(ctxt)) {
5796 /*
5797 * Re-enter guest when pio read ahead buffer is empty
5798 * or, if it is not used, after each 1024 iteration.
5799 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005800 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005801 (r->end == 0 || r->end != r->pos)) {
5802 /*
5803 * Reset read cache. Usually happens before
5804 * decode, but since instruction is restarted
5805 * we have to do it here.
5806 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005807 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005808 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005809 return EMULATION_RESTART;
5810 }
5811 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03005812 }
Nadav Amit0efb0442015-03-29 16:33:03 +03005813 ctxt->eflags &= ~X86_EFLAGS_RF;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005814 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005815
Avi Kivity9dac77f2011-06-01 15:34:25 +03005816 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02005817
5818done:
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005819 if (rc == X86EMUL_PROPAGATE_FAULT) {
5820 WARN_ON(ctxt->exception.vector > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +02005821 ctxt->have_exception = true;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005822 }
Joerg Roedel775fde82011-04-04 12:39:24 +02005823 if (rc == X86EMUL_INTERCEPTED)
5824 return EMULATION_INTERCEPTED;
5825
Avi Kivitydd856ef2012-08-27 23:46:17 +03005826 if (rc == X86EMUL_CONTINUE)
5827 writeback_registers(ctxt);
5828
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005829 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005830
5831twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005832 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02005833 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02005834 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005835 break;
5836 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02005837 case 0x0d: /* GrpP (prefetch) */
5838 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02005839 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02005840 break;
5841 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005842 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02005843 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005844 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005845 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005846 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005847 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03005848 if (test_cc(ctxt->b, ctxt->eflags))
5849 ctxt->dst.val = ctxt->src.val;
Nadav Amitb91aa142015-03-30 15:39:19 +03005850 else if (ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005851 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08005852 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03005853 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03005854 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005855 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005856 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08005857 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005858 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08005859 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005860 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005861 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005862 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03005863 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005864 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005865 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005866 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005867 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03005868 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005869 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005870 default:
5871 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005872 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005873
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005874threebyte_insn:
5875
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005876 if (rc != X86EMUL_CONTINUE)
5877 goto done;
5878
Avi Kivity6aa8b732006-12-10 02:21:36 -08005879 goto writeback;
5880
5881cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02005882 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005883}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005884
5885void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
5886{
5887 invalidate_registers(ctxt);
5888}
5889
5890void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
5891{
5892 writeback_registers(ctxt);
5893}
Tom Lendacky0f89b202016-12-14 14:59:23 -05005894
5895bool emulator_can_use_gpa(struct x86_emulate_ctxt *ctxt)
5896{
5897 if (ctxt->rep_prefix && (ctxt->d & String))
5898 return false;
5899
5900 if (ctxt->d & TwoMemOp)
5901 return false;
5902
5903 return true;
5904}