blob: 5719d8cfdbd90aff763e306da68262429847f13f [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"
Sean Christopherson2f728d62020-02-18 15:29:49 -080023#include "kvm_emulate.h"
Avi Kivityb7d491e2013-01-04 16:18:49 +020024#include <linux/stringify.h>
Nadav Amit3db176d2015-04-19 21:12:59 +030025#include <asm/debugreg.h>
Peter Zijlstra1a29b5b2018-01-25 10:58:13 +010026#include <asm/nospec-branch.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027
Avi Kivity3eeb3282010-01-21 15:31:48 +020028#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020029#include "tss.h"
Yu Zhangd1cd3ce2017-08-24 20:27:53 +080030#include "mmu.h"
Arbel Moshe2d7921c2018-03-12 13:12:53 +020031#include "pmu.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020032
Avi Kivity6aa8b732006-12-10 02:21:36 -080033/*
Avi Kivitya99455492011-09-13 10:45:41 +030034 * Operand types
35 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030036#define OpNone 0ull
37#define OpImplicit 1ull /* No generic decode */
38#define OpReg 2ull /* Register */
39#define OpMem 3ull /* Memory */
40#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
41#define OpDI 5ull /* ES:DI/EDI/RDI */
42#define OpMem64 6ull /* Memory, 64-bit */
43#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
44#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030045#define OpCL 9ull /* CL register (for shifts) */
46#define OpImmByte 10ull /* 8-bit sign extended immediate */
47#define OpOne 11ull /* Implied 1 */
Nadav Amit5e2c6882012-12-06 21:55:10 -020048#define OpImm 12ull /* Sign extended up to 32-bit immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030049#define OpMem16 13ull /* Memory operand (16-bit). */
50#define OpMem32 14ull /* Memory operand (32-bit). */
51#define OpImmU 15ull /* Immediate operand, zero extended */
52#define OpSI 16ull /* SI/ESI/RSI */
53#define OpImmFAddr 17ull /* Immediate far address */
54#define OpMemFAddr 18ull /* Far address in memory */
55#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030056#define OpES 20ull /* ES */
57#define OpCS 21ull /* CS */
58#define OpSS 22ull /* SS */
59#define OpDS 23ull /* DS */
60#define OpFS 24ull /* FS */
61#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020062#define OpMem8 26ull /* 8-bit zero extended memory operand */
Nadav Amit5e2c6882012-12-06 21:55:10 -020063#define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */
Paolo Bonzini7fa57952013-05-09 11:32:50 +020064#define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */
Avi Kivity820207c2013-02-09 11:31:45 +020065#define OpAccLo 29ull /* Low part of extended acc (AX/AX/EAX/RAX) */
66#define OpAccHi 30ull /* High part of extended acc (-/DX/EDX/RDX) */
Avi Kivitya99455492011-09-13 10:45:41 +030067
Avi Kivity0fe59122011-09-13 10:45:47 +030068#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030069#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya99455492011-09-13 10:45:41 +030070
71/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080072 * Opcode effective-address decode tables.
73 * Note that we only emulate instructions that have at least one memory
74 * operand (excluding implicit stack references). We assume that stack
75 * references and instruction fetches will never occur in special memory
76 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
77 * not be handled.
78 */
79
80/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030081#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080082/* Destination operand type. */
Avi Kivitya99455492011-09-13 10:45:41 +030083#define DstShift 1
84#define ImplicitOps (OpImplicit << DstShift)
85#define DstReg (OpReg << DstShift)
86#define DstMem (OpMem << DstShift)
87#define DstAcc (OpAcc << DstShift)
88#define DstDI (OpDI << DstShift)
89#define DstMem64 (OpMem64 << DstShift)
Nadav Amit16bebef2014-12-25 02:52:18 +020090#define DstMem16 (OpMem16 << DstShift)
Avi Kivitya99455492011-09-13 10:45:41 +030091#define DstImmUByte (OpImmUByte << DstShift)
92#define DstDX (OpDX << DstShift)
Avi Kivity820207c2013-02-09 11:31:45 +020093#define DstAccLo (OpAccLo << DstShift)
Avi Kivitya99455492011-09-13 10:45:41 +030094#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080095/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030096#define SrcShift 6
97#define SrcNone (OpNone << SrcShift)
98#define SrcReg (OpReg << SrcShift)
99#define SrcMem (OpMem << SrcShift)
100#define SrcMem16 (OpMem16 << SrcShift)
101#define SrcMem32 (OpMem32 << SrcShift)
102#define SrcImm (OpImm << SrcShift)
103#define SrcImmByte (OpImmByte << SrcShift)
104#define SrcOne (OpOne << SrcShift)
105#define SrcImmUByte (OpImmUByte << SrcShift)
106#define SrcImmU (OpImmU << SrcShift)
107#define SrcSI (OpSI << SrcShift)
Paolo Bonzini7fa57952013-05-09 11:32:50 +0200108#define SrcXLat (OpXLat << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300109#define SrcImmFAddr (OpImmFAddr << SrcShift)
110#define SrcMemFAddr (OpMemFAddr << SrcShift)
111#define SrcAcc (OpAcc << SrcShift)
112#define SrcImmU16 (OpImmU16 << SrcShift)
Nadav Amit5e2c6882012-12-06 21:55:10 -0200113#define SrcImm64 (OpImm64 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300114#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200115#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity820207c2013-02-09 11:31:45 +0200116#define SrcAccHi (OpAccHi << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300117#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300118#define BitOp (1<<11)
119#define MemAbs (1<<12) /* Memory operand is absolute displacement */
120#define String (1<<13) /* String instruction (rep capable) */
121#define Stack (1<<14) /* Stack instruction (push/pop) */
122#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
123#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
124#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
125#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
126#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
Gleb Natapov045a2822012-12-20 16:57:43 +0200127#define Escape (5<<15) /* Escape to coprocessor instruction */
Nadav Amit39f062f2014-11-26 15:47:18 +0200128#define InstrDual (6<<15) /* Alternate instruction decoding of mod == 3 */
Nadav Amit2276b512015-01-26 09:32:24 +0200129#define ModeDual (7<<15) /* Different instruction for 32/64 bit */
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300130#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300131/* Generic ModRM decode. */
132#define ModRM (1<<19)
133/* Destination is only written; never read. */
134#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300135/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200136#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Borislav Petkovb51e9742013-09-22 16:44:52 +0200137#define EmulateOnUD (1<<22) /* Emulate if unsupported by the host */
Avi Kivity5a506b12010-08-01 15:10:29 +0300138#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300139#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300140#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200141#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200142#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300143#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800144#define PageTable (1 << 29) /* instruction used to write page table */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300145#define NotImpl (1 << 30) /* instruction is not implemented */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100146/* Source 2 operand type */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300147#define Src2Shift (31)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300148#define Src2None (OpNone << Src2Shift)
Avi Kivityab2c5ce2013-02-09 11:31:46 +0200149#define Src2Mem (OpMem << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300150#define Src2CL (OpCL << Src2Shift)
151#define Src2ImmByte (OpImmByte << Src2Shift)
152#define Src2One (OpOne << Src2Shift)
153#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300154#define Src2ES (OpES << Src2Shift)
155#define Src2CS (OpCS << Src2Shift)
156#define Src2SS (OpSS << Src2Shift)
157#define Src2DS (OpDS << Src2Shift)
158#define Src2FS (OpFS << Src2Shift)
159#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300160#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300161#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Radim Krčmář48520182016-11-08 20:54:17 +0100162#define AlignMask ((u64)7 << 41)
Avi Kivity1c11b372012-04-09 18:39:59 +0300163#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
Radim Krčmář48520182016-11-08 20:54:17 +0100164#define Unaligned ((u64)2 << 41) /* Explicitly unaligned (e.g. MOVDQU) */
165#define Avx ((u64)3 << 41) /* Advanced Vector Extensions */
166#define Aligned16 ((u64)4 << 41) /* Aligned to 16 byte boundary (e.g. FXSAVE) */
Avi Kivitye28bbd42013-01-04 16:18:48 +0200167#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
Avi Kivityb6744dc2013-01-04 16:18:50 +0200168#define NoWrite ((u64)1 << 45) /* No writeback */
Avi Kivityfb32b1e2013-02-09 11:31:44 +0200169#define SrcWrite ((u64)1 << 46) /* Write back src operand */
Nadav Amit9b88ae92014-05-25 23:05:21 +0300170#define NoMod ((u64)1 << 47) /* Mod field is ignored */
Paolo Bonzinid40a6892014-03-27 11:58:02 +0100171#define Intercept ((u64)1 << 48) /* Has valid intercept field */
172#define CheckPerm ((u64)1 << 49) /* Has valid check_perm field */
Nadav Amit68efa762014-06-18 17:19:35 +0300173#define PrivUD ((u64)1 << 51) /* #UD instead of #GP on CPL > 0 */
Nadav Amit58b70752014-10-24 11:35:09 +0300174#define NearBranch ((u64)1 << 52) /* Near branches */
Nadav Amited9aad22014-11-02 11:55:00 +0200175#define No16 ((u64)1 << 53) /* No 16 bit operand */
Nadav Amitab708092014-12-25 02:52:21 +0200176#define IncSP ((u64)1 << 54) /* SP is incremented before ModRM calc */
Tom Lendacky0f89b202016-12-14 14:59:23 -0500177#define TwoMemOp ((u64)1 << 55) /* Instruction has two memory operand */
Eric Hankland018d70f2021-11-30 15:42:21 +0800178#define IsBranch ((u64)1 << 56) /* Instruction is considered a branch. */
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
Avi Kivityd65b1de2010-07-29 15:11:35 +0300194struct opcode {
Eric Hankland018d70f2021-11-30 15:42:21 +0800195 u64 flags;
196 u8 intercept;
197 u8 pad[7];
Avi Kivity120df892010-07-29 15:11:39 +0300198 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300199 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200200 const struct opcode *group;
201 const struct group_dual *gdual;
202 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200203 const struct escape *esc;
Nadav Amit39f062f2014-11-26 15:47:18 +0200204 const struct instr_dual *idual;
Nadav Amit2276b512015-01-26 09:32:24 +0200205 const struct mode_dual *mdual;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200206 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300207 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200208 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300209};
210
211struct group_dual {
212 struct opcode mod012[8];
213 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300214};
215
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200216struct gprefix {
217 struct opcode pfx_no;
218 struct opcode pfx_66;
219 struct opcode pfx_f2;
220 struct opcode pfx_f3;
221};
222
Gleb Natapov045a2822012-12-20 16:57:43 +0200223struct escape {
224 struct opcode op[8];
225 struct opcode high[64];
226};
227
Nadav Amit39f062f2014-11-26 15:47:18 +0200228struct instr_dual {
229 struct opcode mod012;
230 struct opcode mod3;
231};
232
Nadav Amit2276b512015-01-26 09:32:24 +0200233struct mode_dual {
234 struct opcode mode32;
235 struct opcode mode64;
236};
237
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300238#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300239
Nadav Amit3dc4bc42014-12-25 02:52:19 +0200240enum x86_transfer_type {
241 X86_TRANSFER_NONE,
242 X86_TRANSFER_CALL_JMP,
243 X86_TRANSFER_RET,
244 X86_TRANSFER_TASK_SWITCH,
245};
246
Avi Kivitydd856ef2012-08-27 23:46:17 +0300247static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
248{
249 if (!(ctxt->regs_valid & (1 << nr))) {
250 ctxt->regs_valid |= 1 << nr;
251 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
252 }
253 return ctxt->_regs[nr];
254}
255
256static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
257{
258 ctxt->regs_valid |= 1 << nr;
259 ctxt->regs_dirty |= 1 << nr;
260 return &ctxt->_regs[nr];
261}
262
263static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
264{
265 reg_read(ctxt, nr);
266 return reg_write(ctxt, nr);
267}
268
269static void writeback_registers(struct x86_emulate_ctxt *ctxt)
270{
271 unsigned reg;
272
273 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
274 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
275}
276
277static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
278{
279 ctxt->regs_dirty = 0;
280 ctxt->regs_valid = 0;
281}
282
Avi Kivity6aa8b732006-12-10 02:21:36 -0800283/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800284 * These EFLAGS bits are restored from saved value during emulation, and
285 * any changes are written back to the saved value after emulation.
286 */
Nadav Amit0efb0442015-03-29 16:33:03 +0300287#define EFLAGS_MASK (X86_EFLAGS_OF|X86_EFLAGS_SF|X86_EFLAGS_ZF|X86_EFLAGS_AF|\
288 X86_EFLAGS_PF|X86_EFLAGS_CF)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800289
Avi Kivitydda96d82008-11-26 15:14:10 +0200290#ifdef CONFIG_X86_64
291#define ON64(x) x
292#else
293#define ON64(x)
294#endif
295
Qian Caib78a8552020-02-17 11:48:26 -0500296/*
297 * fastop functions have a special calling convention:
298 *
299 * dst: rax (in/out)
300 * src: rdx (in/out)
301 * src2: rcx (in)
302 * flags: rflags (in/out)
303 * ex: rsi (in:fastop pointer, out:zero if exception)
304 *
305 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
306 * different operand sizes can be reached by calculation, rather than a jump
307 * table (which would be bigger than the code).
308 */
Sean Christopherson3009afc2020-01-21 20:43:39 -0800309static int fastop(struct x86_emulate_ctxt *ctxt, fastop_t fop);
Avi Kivity4d758342013-01-19 19:51:55 +0200310
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500311#define __FOP_FUNC(name) \
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600312 ".align " __stringify(FASTOP_SIZE) " \n\t" \
313 ".type " name ", @function \n\t" \
314 name ":\n\t"
315
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500316#define FOP_FUNC(name) \
317 __FOP_FUNC(#name)
318
319#define __FOP_RET(name) \
Peter Zijlstrac9a34c32021-11-10 11:01:16 +0100320 "11: " ASM_RET \
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500321 ".size " name ", .-" name "\n\t"
322
323#define FOP_RET(name) \
324 __FOP_RET(#name)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200325
326#define FOP_START(op) \
327 extern void em_##op(struct fastop *fake); \
328 asm(".pushsection .text, \"ax\" \n\t" \
329 ".global em_" #op " \n\t" \
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500330 ".align " __stringify(FASTOP_SIZE) " \n\t" \
331 "em_" #op ":\n\t"
Avi Kivityb7d491e2013-01-04 16:18:49 +0200332
333#define FOP_END \
334 ".popsection")
335
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500336#define __FOPNOP(name) \
337 __FOP_FUNC(name) \
338 __FOP_RET(name)
339
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600340#define FOPNOP() \
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500341 __FOPNOP(__stringify(__UNIQUE_ID(nop)))
Avi Kivity0bdea062013-01-19 19:51:50 +0200342
Avi Kivityb7d491e2013-01-04 16:18:49 +0200343#define FOP1E(op, dst) \
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500344 __FOP_FUNC(#op "_" #dst) \
345 "10: " #op " %" #dst " \n\t" \
346 __FOP_RET(#op "_" #dst)
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200347
348#define FOP1EEX(op, dst) \
Peter Zijlstrac9a34c32021-11-10 11:01:16 +0100349 FOP1E(op, dst) _ASM_EXTABLE_TYPE_REG(10b, 11b, EX_TYPE_ZERO_REG, %%esi)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200350
351#define FASTOP1(op) \
352 FOP_START(op) \
353 FOP1E(op##b, al) \
354 FOP1E(op##w, ax) \
355 FOP1E(op##l, eax) \
356 ON64(FOP1E(op##q, rax)) \
357 FOP_END
358
Avi Kivityb9fa4092013-02-09 11:31:48 +0200359/* 1-operand, using src2 (for MUL/DIV r/m) */
360#define FASTOP1SRC2(op, name) \
361 FOP_START(name) \
362 FOP1E(op, cl) \
363 FOP1E(op, cx) \
364 FOP1E(op, ecx) \
365 ON64(FOP1E(op, rcx)) \
366 FOP_END
367
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200368/* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */
369#define FASTOP1SRC2EX(op, name) \
370 FOP_START(name) \
371 FOP1EEX(op, cl) \
372 FOP1EEX(op, cx) \
373 FOP1EEX(op, ecx) \
374 ON64(FOP1EEX(op, rcx)) \
375 FOP_END
376
Avi Kivityf7857f32013-01-04 16:18:53 +0200377#define FOP2E(op, dst, src) \
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500378 __FOP_FUNC(#op "_" #dst "_" #src) \
379 #op " %" #src ", %" #dst " \n\t" \
380 __FOP_RET(#op "_" #dst "_" #src)
Avi Kivityf7857f32013-01-04 16:18:53 +0200381
382#define FASTOP2(op) \
383 FOP_START(op) \
Avi Kivity017da7b2013-02-09 11:31:47 +0200384 FOP2E(op##b, al, dl) \
385 FOP2E(op##w, ax, dx) \
386 FOP2E(op##l, eax, edx) \
387 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivityf7857f32013-01-04 16:18:53 +0200388 FOP_END
389
Avi Kivity11c363b2013-01-19 19:51:54 +0200390/* 2 operand, word only */
391#define FASTOP2W(op) \
392 FOP_START(op) \
393 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200394 FOP2E(op##w, ax, dx) \
395 FOP2E(op##l, eax, edx) \
396 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivity11c363b2013-01-19 19:51:54 +0200397 FOP_END
398
Avi Kivity007a3b52013-01-19 19:51:51 +0200399/* 2 operand, src is CL */
400#define FASTOP2CL(op) \
401 FOP_START(op) \
402 FOP2E(op##b, al, cl) \
403 FOP2E(op##w, ax, cl) \
404 FOP2E(op##l, eax, cl) \
405 ON64(FOP2E(op##q, rax, cl)) \
406 FOP_END
407
Nadav Amit5aca3722014-11-02 11:54:50 +0200408/* 2 operand, src and dest are reversed */
409#define FASTOP2R(op, name) \
410 FOP_START(name) \
411 FOP2E(op##b, dl, al) \
412 FOP2E(op##w, dx, ax) \
413 FOP2E(op##l, edx, eax) \
414 ON64(FOP2E(op##q, rdx, rax)) \
415 FOP_END
416
Avi Kivity0bdea062013-01-19 19:51:50 +0200417#define FOP3E(op, dst, src, src2) \
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500418 __FOP_FUNC(#op "_" #dst "_" #src "_" #src2) \
419 #op " %" #src2 ", %" #src ", %" #dst " \n\t"\
420 __FOP_RET(#op "_" #dst "_" #src "_" #src2)
Avi Kivity0bdea062013-01-19 19:51:50 +0200421
422/* 3-operand, word-only, src2=cl */
423#define FASTOP3WCL(op) \
424 FOP_START(op) \
425 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200426 FOP3E(op##w, ax, dx, cl) \
427 FOP3E(op##l, eax, edx, cl) \
428 ON64(FOP3E(op##q, rax, rdx, cl)) \
Avi Kivity0bdea062013-01-19 19:51:50 +0200429 FOP_END
430
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200431/* Special case for SETcc - 1 instruction per cc */
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600432#define FOP_SETCC(op) \
433 ".align 4 \n\t" \
434 ".type " #op ", @function \n\t" \
435 #op ": \n\t" \
436 #op " %al \n\t" \
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500437 __FOP_RET(#op)
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200438
439FOP_START(setcc)
440FOP_SETCC(seto)
441FOP_SETCC(setno)
442FOP_SETCC(setc)
443FOP_SETCC(setnc)
444FOP_SETCC(setz)
445FOP_SETCC(setnz)
446FOP_SETCC(setbe)
447FOP_SETCC(setnbe)
448FOP_SETCC(sets)
449FOP_SETCC(setns)
450FOP_SETCC(setp)
451FOP_SETCC(setnp)
452FOP_SETCC(setl)
453FOP_SETCC(setnl)
454FOP_SETCC(setle)
455FOP_SETCC(setnle)
456FOP_END;
457
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500458FOP_START(salc)
459FOP_FUNC(salc)
460"pushf; sbb %al, %al; popf \n\t"
461FOP_RET(salc)
Paolo Bonzini326f5782013-05-09 11:32:51 +0200462FOP_END;
463
Radim Krčmářaabba3c2016-11-08 20:54:18 +0100464/*
465 * XXX: inoutclob user must know where the argument is being expanded.
Masahiro Yamadae9666d12018-12-31 00:14:15 +0900466 * Relying on CONFIG_CC_HAS_ASM_GOTO would allow us to remove _fault.
Radim Krčmářaabba3c2016-11-08 20:54:18 +0100467 */
468#define asm_safe(insn, inoutclob...) \
469({ \
470 int _fault = 0; \
471 \
472 asm volatile("1:" insn "\n" \
473 "2:\n" \
Peter Zijlstrac9a34c32021-11-10 11:01:16 +0100474 _ASM_EXTABLE_TYPE_REG(1b, 2b, EX_TYPE_ONE_REG, %[_fault]) \
475 : [_fault] "+r"(_fault) inoutclob ); \
Radim Krčmářaabba3c2016-11-08 20:54:18 +0100476 \
477 _fault ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE; \
478})
479
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200480static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
481 enum x86_intercept intercept,
482 enum x86_intercept_stage stage)
483{
484 struct x86_instruction_info info = {
485 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300486 .rep_prefix = ctxt->rep_prefix,
487 .modrm_mod = ctxt->modrm_mod,
488 .modrm_reg = ctxt->modrm_reg,
489 .modrm_rm = ctxt->modrm_rm,
490 .src_val = ctxt->src.val64,
Jan Kiszka6cbc5f52014-06-30 12:52:55 +0200491 .dst_val = ctxt->dst.val64,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300492 .src_bytes = ctxt->src.bytes,
493 .dst_bytes = ctxt->dst.bytes,
494 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200495 .next_rip = ctxt->eip,
496 };
497
Avi Kivity29535382011-04-20 13:37:53 +0300498 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200499}
500
Avi Kivityf47cfa32012-06-07 17:49:24 +0300501static void assign_masked(ulong *dest, ulong src, ulong mask)
502{
503 *dest = (*dest & ~mask) | (src & mask);
504}
505
Nadav Amit6fd8e122015-03-30 15:39:20 +0300506static void assign_register(unsigned long *reg, u64 val, int bytes)
507{
508 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
509 switch (bytes) {
510 case 1:
511 *(u8 *)reg = (u8)val;
512 break;
513 case 2:
514 *(u16 *)reg = (u16)val;
515 break;
516 case 4:
517 *reg = (u32)val;
518 break; /* 64b: zero-extend */
519 case 8:
520 *reg = val;
521 break;
522 }
523}
524
Avi Kivity9dac77f2011-06-01 15:34:25 +0300525static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800526{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300527 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800528}
529
Avi Kivityf47cfa32012-06-07 17:49:24 +0300530static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
531{
532 u16 sel;
533 struct desc_struct ss;
534
535 if (ctxt->mode == X86EMUL_MODE_PROT64)
536 return ~0UL;
537 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
538 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
539}
540
Avi Kivity612e89f2012-06-12 20:03:23 +0300541static int stack_size(struct x86_emulate_ctxt *ctxt)
542{
543 return (__fls(stack_mask(ctxt)) + 1) >> 3;
544}
545
Avi Kivity6aa8b732006-12-10 02:21:36 -0800546/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800547static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300548address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800549{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300550 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800551 return reg;
552 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300553 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800554}
555
556static inline unsigned long
Paolo Bonzini01485a22014-11-19 18:25:08 +0100557register_address(struct x86_emulate_ctxt *ctxt, int reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800558{
Paolo Bonzini01485a22014-11-19 18:25:08 +0100559 return address_mask(ctxt, reg_read(ctxt, reg));
Harvey Harrisone4706772008-02-19 07:40:38 -0800560}
561
Avi Kivity5ad105e2012-08-19 14:34:31 +0300562static void masked_increment(ulong *reg, ulong mask, int inc)
563{
564 assign_masked(reg, *reg + inc, mask);
565}
566
Harvey Harrison7a9572752008-02-19 07:40:41 -0800567static inline void
Paolo Bonzini01485a22014-11-19 18:25:08 +0100568register_address_increment(struct x86_emulate_ctxt *ctxt, int reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800569{
Nadav Amitee122a72015-04-28 13:06:00 +0300570 ulong *preg = reg_rmw(ctxt, reg);
Avi Kivity5ad105e2012-08-19 14:34:31 +0300571
Nadav Amitee122a72015-04-28 13:06:00 +0300572 assign_register(preg, *preg + inc, ctxt->ad_bytes);
Avi Kivity5ad105e2012-08-19 14:34:31 +0300573}
574
575static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
576{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300577 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800578}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800579
Avi Kivity56697682011-04-03 14:08:51 +0300580static u32 desc_limit_scaled(struct desc_struct *desc)
581{
582 u32 limit = get_desc_limit(desc);
583
584 return desc->g ? (limit << 12) | 0xfff : limit;
585}
586
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900587static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300588{
589 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
590 return 0;
591
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900592 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300593}
594
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200595static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
596 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300597{
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +0200598 WARN_ON(vec > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +0200599 ctxt->exception.vector = vec;
600 ctxt->exception.error_code = error;
601 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200602 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300603}
604
Joerg Roedel3b88e412011-04-04 12:39:29 +0200605static int emulate_db(struct x86_emulate_ctxt *ctxt)
606{
607 return emulate_exception(ctxt, DB_VECTOR, 0, false);
608}
609
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200610static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300611{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200612 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300613}
614
Avi Kivity618ff152011-04-03 12:32:09 +0300615static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
616{
617 return emulate_exception(ctxt, SS_VECTOR, err, true);
618}
619
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200620static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300621{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200622 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300623}
624
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200625static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300626{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200627 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300628}
629
Avi Kivity34d1f492010-08-26 11:59:01 +0300630static int emulate_de(struct x86_emulate_ctxt *ctxt)
631{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200632 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300633}
634
Avi Kivity1253791d2011-03-29 11:41:27 +0200635static int emulate_nm(struct x86_emulate_ctxt *ctxt)
636{
637 return emulate_exception(ctxt, NM_VECTOR, 0, false);
638}
639
Avi Kivity1aa36612011-04-27 13:20:30 +0300640static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
641{
642 u16 selector;
643 struct desc_struct desc;
644
645 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
646 return selector;
647}
648
649static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
650 unsigned seg)
651{
652 u16 dummy;
653 u32 base3;
654 struct desc_struct desc;
655
656 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
657 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
658}
659
Sean Christophersonf0ed4762020-02-18 15:29:43 -0800660static inline u8 ctxt_virt_addr_bits(struct x86_emulate_ctxt *ctxt)
661{
662 return (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_LA57) ? 57 : 48;
663}
664
665static inline bool emul_is_noncanonical_address(u64 la,
666 struct x86_emulate_ctxt *ctxt)
667{
668 return get_canonical(la, ctxt_virt_addr_bits(ctxt)) != la;
669}
670
Avi Kivity1c11b372012-04-09 18:39:59 +0300671/*
672 * x86 defines three classes of vector instructions: explicitly
673 * aligned, explicitly unaligned, and the rest, which change behaviour
674 * depending on whether they're AVX encoded or not.
675 *
676 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100677 * subject to the same check. FXSAVE and FXRSTOR are checked here too as their
678 * 512 bytes of data must be aligned to a 16 byte boundary.
Avi Kivity1c11b372012-04-09 18:39:59 +0300679 */
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100680static unsigned insn_alignment(struct x86_emulate_ctxt *ctxt, unsigned size)
Avi Kivity1c11b372012-04-09 18:39:59 +0300681{
Radim Krčmář48520182016-11-08 20:54:17 +0100682 u64 alignment = ctxt->d & AlignMask;
Avi Kivity1c11b372012-04-09 18:39:59 +0300683
Avi Kivity1c11b372012-04-09 18:39:59 +0300684 if (likely(size < 16))
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100685 return 1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300686
Radim Krčmář48520182016-11-08 20:54:17 +0100687 switch (alignment) {
688 case Unaligned:
689 case Avx:
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100690 return 1;
Radim Krčmář48520182016-11-08 20:54:17 +0100691 case Aligned16:
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100692 return 16;
Radim Krčmář48520182016-11-08 20:54:17 +0100693 case Aligned:
694 default:
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100695 return size;
Radim Krčmář48520182016-11-08 20:54:17 +0100696 }
Avi Kivity1c11b372012-04-09 18:39:59 +0300697}
698
Paolo Bonzinid09155d2014-10-27 14:54:44 +0100699static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
700 struct segmented_address addr,
701 unsigned *max_size, unsigned size,
702 bool write, bool fetch,
Nadav Amitd50eaa12014-11-19 17:43:11 +0200703 enum x86emul_mode mode, ulong *linear)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300704{
Avi Kivity618ff152011-04-03 12:32:09 +0300705 struct desc_struct desc;
706 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300707 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300708 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300709 u16 sel;
Yu Zhangfd8cb432017-08-24 20:27:56 +0800710 u8 va_bits;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300711
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900712 la = seg_base(ctxt, addr.seg) + addr.ea;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100713 *max_size = 0;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200714 switch (mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300715 case X86EMUL_MODE_PROT64:
Paolo Bonzini0c1d77f2016-02-19 18:07:21 +0100716 *linear = la;
Yu Zhangfd8cb432017-08-24 20:27:56 +0800717 va_bits = ctxt_virt_addr_bits(ctxt);
718 if (get_canonical(la, va_bits) != la)
Nadav Amitabc7d8a2014-11-19 17:43:12 +0200719 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100720
Yu Zhangfd8cb432017-08-24 20:27:56 +0800721 *max_size = min_t(u64, ~0u, (1ull << va_bits) - la);
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100722 if (size > *max_size)
723 goto bad;
Avi Kivity618ff152011-04-03 12:32:09 +0300724 break;
725 default:
Paolo Bonzini0c1d77f2016-02-19 18:07:21 +0100726 *linear = la = (u32)la;
Avi Kivity1aa36612011-04-27 13:20:30 +0300727 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
728 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300729 if (!usable)
730 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200731 /* code segment in protected mode or read-only data segment */
732 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
733 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300734 goto bad;
735 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400736 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300737 goto bad;
738 lim = desc_limit_scaled(&desc);
Paolo Bonzini997b0412014-11-19 18:33:38 +0100739 if (!(desc.type & 8) && (desc.type & 4)) {
Guo Chaofc058682012-06-28 15:19:51 +0800740 /* expand-down segment */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100741 if (addr.ea <= lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300742 goto bad;
743 lim = desc.d ? 0xffffffff : 0xffff;
Avi Kivity618ff152011-04-03 12:32:09 +0300744 }
Paolo Bonzini997b0412014-11-19 18:33:38 +0100745 if (addr.ea > lim)
746 goto bad;
Nadav Amitbac155312015-01-26 09:32:26 +0200747 if (lim == 0xffffffff)
748 *max_size = ~0u;
749 else {
750 *max_size = (u64)lim + 1 - addr.ea;
751 if (size > *max_size)
752 goto bad;
753 }
Avi Kivity618ff152011-04-03 12:32:09 +0300754 break;
755 }
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100756 if (la & (insn_alignment(ctxt, size) - 1))
Avi Kivity1c11b372012-04-09 18:39:59 +0300757 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300758 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300759bad:
760 if (addr.seg == VCPU_SREG_SS)
Paolo Bonzini36061892014-10-27 14:40:49 +0100761 return emulate_ss(ctxt, 0);
Avi Kivity618ff152011-04-03 12:32:09 +0300762 else
Paolo Bonzini36061892014-10-27 14:40:49 +0100763 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300764}
765
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400766static int linearize(struct x86_emulate_ctxt *ctxt,
767 struct segmented_address addr,
768 unsigned size, bool write,
769 ulong *linear)
770{
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100771 unsigned max_size;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200772 return __linearize(ctxt, addr, &max_size, size, write, false,
773 ctxt->mode, linear);
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400774}
775
Nadav Amitd50eaa12014-11-19 17:43:11 +0200776static inline int assign_eip(struct x86_emulate_ctxt *ctxt, ulong dst,
777 enum x86emul_mode mode)
778{
779 ulong linear;
780 int rc;
781 unsigned max_size;
782 struct segmented_address addr = { .seg = VCPU_SREG_CS,
783 .ea = dst };
784
785 if (ctxt->op_bytes != sizeof(unsigned long))
786 addr.ea = dst & ((1UL << (ctxt->op_bytes << 3)) - 1);
787 rc = __linearize(ctxt, addr, &max_size, 1, false, true, mode, &linear);
788 if (rc == X86EMUL_CONTINUE)
789 ctxt->_eip = addr.ea;
790 return rc;
791}
792
793static inline int assign_eip_near(struct x86_emulate_ctxt *ctxt, ulong dst)
794{
795 return assign_eip(ctxt, dst, ctxt->mode);
796}
797
798static int assign_eip_far(struct x86_emulate_ctxt *ctxt, ulong dst,
799 const struct desc_struct *cs_desc)
800{
801 enum x86emul_mode mode = ctxt->mode;
Nadav Amit82268082015-01-26 09:32:27 +0200802 int rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200803
804#ifdef CONFIG_X86_64
Nadav Amit82268082015-01-26 09:32:27 +0200805 if (ctxt->mode >= X86EMUL_MODE_PROT16) {
806 if (cs_desc->l) {
807 u64 efer = 0;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200808
Nadav Amit82268082015-01-26 09:32:27 +0200809 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
810 if (efer & EFER_LMA)
811 mode = X86EMUL_MODE_PROT64;
812 } else
813 mode = X86EMUL_MODE_PROT32; /* temporary value */
Nadav Amitd50eaa12014-11-19 17:43:11 +0200814 }
815#endif
816 if (mode == X86EMUL_MODE_PROT16 || mode == X86EMUL_MODE_PROT32)
817 mode = cs_desc->d ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
Nadav Amit82268082015-01-26 09:32:27 +0200818 rc = assign_eip(ctxt, dst, mode);
819 if (rc == X86EMUL_CONTINUE)
820 ctxt->mode = mode;
821 return rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200822}
823
824static inline int jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
825{
826 return assign_eip_near(ctxt, ctxt->_eip + rel);
827}
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400828
Paolo Bonzini79367a62018-06-06 16:43:02 +0200829static int linear_read_system(struct x86_emulate_ctxt *ctxt, ulong linear,
830 void *data, unsigned size)
831{
Paolo Bonzini3c9fa242018-06-06 17:38:09 +0200832 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception, true);
Paolo Bonzini79367a62018-06-06 16:43:02 +0200833}
834
835static int linear_write_system(struct x86_emulate_ctxt *ctxt,
836 ulong linear, void *data,
837 unsigned int size)
838{
Paolo Bonzini3c9fa242018-06-06 17:38:09 +0200839 return ctxt->ops->write_std(ctxt, linear, data, size, &ctxt->exception, true);
Paolo Bonzini79367a62018-06-06 16:43:02 +0200840}
841
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200842static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
843 struct segmented_address addr,
844 void *data,
845 unsigned size)
846{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200847 int rc;
848 ulong linear;
849
Avi Kivity83b87952011-04-03 11:31:19 +0300850 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200851 if (rc != X86EMUL_CONTINUE)
852 return rc;
Paolo Bonzini3c9fa242018-06-06 17:38:09 +0200853 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception, false);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200854}
855
Steve Rutherford129a72a2017-01-11 18:28:29 -0800856static int segmented_write_std(struct x86_emulate_ctxt *ctxt,
857 struct segmented_address addr,
858 void *data,
859 unsigned int size)
860{
861 int rc;
862 ulong linear;
863
864 rc = linearize(ctxt, addr, size, true, &linear);
865 if (rc != X86EMUL_CONTINUE)
866 return rc;
Paolo Bonzini3c9fa242018-06-06 17:38:09 +0200867 return ctxt->ops->write_std(ctxt, linear, data, size, &ctxt->exception, false);
Steve Rutherford129a72a2017-01-11 18:28:29 -0800868}
869
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900870/*
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200871 * Prefetch the remaining bytes of the instruction without crossing page
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900872 * boundary if they are not in fetch_cache yet.
873 */
Paolo Bonzini9506d572014-05-06 13:05:25 +0200874static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
Avi Kivity62266862007-11-20 13:15:52 +0200875{
Avi Kivity62266862007-11-20 13:15:52 +0200876 int rc;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100877 unsigned size, max_size;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200878 unsigned long linear;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200879 int cur_size = ctxt->fetch.end - ctxt->fetch.data;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200880 struct segmented_address addr = { .seg = VCPU_SREG_CS,
Paolo Bonzini17052f12014-05-06 16:33:01 +0200881 .ea = ctxt->eip + cur_size };
882
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100883 /*
884 * We do not know exactly how many bytes will be needed, and
885 * __linearize is expensive, so fetch as much as possible. We
886 * just have to avoid going beyond the 15 byte limit, the end
887 * of the segment, or the end of the page.
888 *
889 * __linearize is called with size 0 so that it does not do any
890 * boundary check itself. Instead, we use max_size to check
891 * against op_size.
892 */
Nadav Amitd50eaa12014-11-19 17:43:11 +0200893 rc = __linearize(ctxt, addr, &max_size, 0, false, true, ctxt->mode,
894 &linear);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200895 if (unlikely(rc != X86EMUL_CONTINUE))
896 return rc;
897
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100898 size = min_t(unsigned, 15UL ^ cur_size, max_size);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200899 size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear));
Paolo Bonzini5cfc7e02014-05-06 13:05:25 +0200900
901 /*
902 * One instruction can only straddle two pages,
903 * and one has been loaded at the beginning of
904 * x86_decode_insn. So, if not enough bytes
905 * still, we must have hit the 15-byte boundary.
906 */
907 if (unlikely(size < op_size))
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100908 return emulate_gp(ctxt, 0);
909
Paolo Bonzini17052f12014-05-06 16:33:01 +0200910 rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end,
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200911 size, &ctxt->exception);
912 if (unlikely(rc != X86EMUL_CONTINUE))
913 return rc;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200914 ctxt->fetch.end += size;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900915 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200916}
917
Paolo Bonzini9506d572014-05-06 13:05:25 +0200918static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt,
919 unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200920{
Nadav Amit08da44a2014-10-03 01:10:04 +0300921 unsigned done_size = ctxt->fetch.end - ctxt->fetch.ptr;
922
923 if (unlikely(done_size < size))
924 return __do_insn_fetch_bytes(ctxt, size - done_size);
Paolo Bonzini9506d572014-05-06 13:05:25 +0200925 else
926 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200927}
928
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900929/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900930#define insn_fetch(_type, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200931({ _type _x; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200932 \
933 rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900934 if (rc != X86EMUL_CONTINUE) \
935 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200936 ctxt->_eip += sizeof(_type); \
Nick Desaulniers8616abc2017-06-27 19:37:14 -0700937 memcpy(&_x, ctxt->fetch.ptr, sizeof(_type)); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200938 ctxt->fetch.ptr += sizeof(_type); \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200939 _x; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900940})
941
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900942#define insn_fetch_arr(_arr, _size, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200943({ \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200944 rc = do_insn_fetch_bytes(_ctxt, _size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900945 if (rc != X86EMUL_CONTINUE) \
946 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200947 ctxt->_eip += (_size); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200948 memcpy(_arr, ctxt->fetch.ptr, _size); \
949 ctxt->fetch.ptr += (_size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900950})
951
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000952/*
953 * Given the 'reg' portion of a ModRM byte, and a register block, return a
954 * pointer into the block that addresses the relevant register.
955 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
956 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300957static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200958 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800959{
960 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200961 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300964 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
965 else
966 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800967 return p;
968}
969
970static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200971 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972 u16 *size, unsigned long *address, int op_bytes)
973{
974 int rc;
975
976 if (op_bytes == 2)
977 op_bytes = 3;
978 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200979 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900980 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800981 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200982 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200983 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984 return rc;
985}
986
Avi Kivity34b77652013-01-19 19:51:56 +0200987FASTOP2(add);
988FASTOP2(or);
989FASTOP2(adc);
990FASTOP2(sbb);
991FASTOP2(and);
992FASTOP2(sub);
993FASTOP2(xor);
994FASTOP2(cmp);
995FASTOP2(test);
996
Avi Kivityb9fa4092013-02-09 11:31:48 +0200997FASTOP1SRC2(mul, mul_ex);
998FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200999FASTOP1SRC2EX(div, div_ex);
1000FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +02001001
Avi Kivity34b77652013-01-19 19:51:56 +02001002FASTOP3WCL(shld);
1003FASTOP3WCL(shrd);
1004
1005FASTOP2W(imul);
1006
1007FASTOP1(not);
1008FASTOP1(neg);
1009FASTOP1(inc);
1010FASTOP1(dec);
1011
1012FASTOP2CL(rol);
1013FASTOP2CL(ror);
1014FASTOP2CL(rcl);
1015FASTOP2CL(rcr);
1016FASTOP2CL(shl);
1017FASTOP2CL(shr);
1018FASTOP2CL(sar);
1019
1020FASTOP2W(bsf);
1021FASTOP2W(bsr);
1022FASTOP2W(bt);
1023FASTOP2W(bts);
1024FASTOP2W(btr);
1025FASTOP2W(btc);
1026
Avi Kivitye47a5f52013-02-09 11:31:51 +02001027FASTOP2(xadd);
1028
Nadav Amit5aca3722014-11-02 11:54:50 +02001029FASTOP2R(cmp, cmp_r);
1030
Nadav Amit900efe22015-03-30 15:39:21 +03001031static int em_bsf_c(struct x86_emulate_ctxt *ctxt)
1032{
1033 /* If src is zero, do not writeback, but update flags */
1034 if (ctxt->src.val == 0)
1035 ctxt->dst.type = OP_NONE;
1036 return fastop(ctxt, em_bsf);
1037}
1038
1039static int em_bsr_c(struct x86_emulate_ctxt *ctxt)
1040{
1041 /* If src is zero, do not writeback, but update flags */
1042 if (ctxt->src.val == 0)
1043 ctxt->dst.type = OP_NONE;
1044 return fastop(ctxt, em_bsr);
1045}
1046
Josh Poimboeufcb7390f2016-01-22 10:16:12 -06001047static __always_inline u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001048{
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001049 u8 rc;
1050 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001051
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001052 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Peter Zijlstra1a29b5b2018-01-25 10:58:13 +01001053 asm("push %[flags]; popf; " CALL_NOSPEC
1054 : "=a"(rc) : [thunk_target]"r"(fop), [flags]"r"(flags));
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001055 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001056}
1057
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001058static void fetch_register_operand(struct operand *op)
1059{
1060 switch (op->bytes) {
1061 case 1:
1062 op->val = *(u8 *)op->addr.reg;
1063 break;
1064 case 2:
1065 op->val = *(u16 *)op->addr.reg;
1066 break;
1067 case 4:
1068 op->val = *(u32 *)op->addr.reg;
1069 break;
1070 case 8:
1071 op->val = *(u64 *)op->addr.reg;
1072 break;
1073 }
1074}
1075
Gleb Natapov045a2822012-12-20 16:57:43 +02001076static int em_fninit(struct x86_emulate_ctxt *ctxt)
1077{
1078 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1079 return emulate_nm(ctxt);
1080
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02001081 kvm_fpu_get();
Gleb Natapov045a2822012-12-20 16:57:43 +02001082 asm volatile("fninit");
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02001083 kvm_fpu_put();
Gleb Natapov045a2822012-12-20 16:57:43 +02001084 return X86EMUL_CONTINUE;
1085}
1086
1087static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1088{
1089 u16 fcw;
1090
1091 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1092 return emulate_nm(ctxt);
1093
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02001094 kvm_fpu_get();
Gleb Natapov045a2822012-12-20 16:57:43 +02001095 asm volatile("fnstcw %0": "+m"(fcw));
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02001096 kvm_fpu_put();
Gleb Natapov045a2822012-12-20 16:57:43 +02001097
Gleb Natapov045a2822012-12-20 16:57:43 +02001098 ctxt->dst.val = fcw;
1099
1100 return X86EMUL_CONTINUE;
1101}
1102
1103static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1104{
1105 u16 fsw;
1106
1107 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1108 return emulate_nm(ctxt);
1109
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02001110 kvm_fpu_get();
Gleb Natapov045a2822012-12-20 16:57:43 +02001111 asm volatile("fnstsw %0": "+m"(fsw));
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02001112 kvm_fpu_put();
Gleb Natapov045a2822012-12-20 16:57:43 +02001113
Gleb Natapov045a2822012-12-20 16:57:43 +02001114 ctxt->dst.val = fsw;
1115
1116 return X86EMUL_CONTINUE;
1117}
1118
Avi Kivity1253791d2011-03-29 11:41:27 +02001119static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001120 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001121{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001122 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001123
Avi Kivity9dac77f2011-06-01 15:34:25 +03001124 if (!(ctxt->d & ModRM))
1125 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity1253791d2011-03-29 11:41:27 +02001126
Avi Kivity9dac77f2011-06-01 15:34:25 +03001127 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001128 op->type = OP_XMM;
1129 op->bytes = 16;
1130 op->addr.xmm = reg;
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02001131 kvm_read_sse_reg(reg, &op->vec_val);
Avi Kivity1253791d2011-03-29 11:41:27 +02001132 return;
1133 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001134 if (ctxt->d & Mmx) {
1135 reg &= 7;
1136 op->type = OP_MM;
1137 op->bytes = 8;
1138 op->addr.mm = reg;
1139 return;
1140 }
Avi Kivity1253791d2011-03-29 11:41:27 +02001141
Avi Kivity3c118e22007-10-31 10:27:04 +02001142 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001143 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1144 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1145
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001146 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001147 op->orig_val = op->val;
1148}
1149
Avi Kivitya6e34072012-06-10 17:15:39 +03001150static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1151{
1152 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1153 ctxt->modrm_seg = VCPU_SREG_SS;
1154}
1155
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001156static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001157 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001158{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001159 u8 sib;
Bandan Das02357bd2014-04-16 12:46:11 -04001160 int index_reg, base_reg, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001161 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001162 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001163
Bandan Das02357bd2014-04-16 12:46:11 -04001164 ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */
1165 index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */
1166 base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001167
Bandan Das02357bd2014-04-16 12:46:11 -04001168 ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001169 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
Bandan Das02357bd2014-04-16 12:46:11 -04001170 ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001171 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001172
Nadav Amit9b88ae92014-05-25 23:05:21 +03001173 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001174 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001175 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001176 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001177 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001178 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001179 op->type = OP_XMM;
1180 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001181 op->addr.xmm = ctxt->modrm_rm;
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02001182 kvm_read_sse_reg(ctxt->modrm_rm, &op->vec_val);
Avi Kivity1253791d2011-03-29 11:41:27 +02001183 return rc;
1184 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001185 if (ctxt->d & Mmx) {
1186 op->type = OP_MM;
1187 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001188 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001189 return rc;
1190 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001191 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001192 return rc;
1193 }
1194
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001195 op->type = OP_MEM;
1196
Avi Kivity9dac77f2011-06-01 15:34:25 +03001197 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001198 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1199 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1200 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1201 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001202
1203 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001204 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001205 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001206 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001207 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001208 break;
1209 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001210 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001211 break;
1212 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001213 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001214 break;
1215 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001216 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001217 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001218 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001219 break;
1220 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001221 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001222 break;
1223 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001224 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001225 break;
1226 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001227 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001228 break;
1229 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001230 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001231 break;
1232 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001233 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001234 break;
1235 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001236 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001237 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001238 break;
1239 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001240 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001241 break;
1242 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001243 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1244 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1245 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001246 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001247 } else {
1248 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001249 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001250 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001251 index_reg |= (sib >> 3) & 7;
1252 base_reg |= sib & 7;
1253 scale = sib >> 6;
1254
Avi Kivity9dac77f2011-06-01 15:34:25 +03001255 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001256 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001257 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001258 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001259 adjust_modrm_seg(ctxt, base_reg);
Nadav Amitab708092014-12-25 02:52:21 +02001260 /* Increment ESP on POP [ESP] */
1261 if ((ctxt->d & IncSP) &&
1262 base_reg == VCPU_REGS_RSP)
1263 modrm_ea += ctxt->op_bytes;
Avi Kivitya6e34072012-06-10 17:15:39 +03001264 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001265 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001266 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001267 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Nadav Amit5b38ab82014-11-02 11:54:41 +02001268 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity84411d82008-06-15 21:53:26 -07001269 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001270 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001271 } else {
1272 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001273 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001274 adjust_modrm_seg(ctxt, base_reg);
1275 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001276 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001277 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001278 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001279 break;
1280 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001281 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001282 break;
1283 }
1284 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001285 op->addr.mem.ea = modrm_ea;
Bandan Das41061cd2014-04-16 12:46:14 -04001286 if (ctxt->ad_bytes != 8)
1287 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
1288
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001289done:
1290 return rc;
1291}
1292
1293static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001294 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001295{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001296 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001297
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001298 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001299 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001300 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001301 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001302 break;
1303 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001304 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001305 break;
1306 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001307 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001308 break;
1309 }
1310done:
1311 return rc;
1312}
1313
Avi Kivity9dac77f2011-06-01 15:34:25 +03001314static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001315{
Sheng Yang7129eec2010-09-28 16:33:32 +08001316 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001317
Avi Kivity9dac77f2011-06-01 15:34:25 +03001318 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001319 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001320
Avi Kivity9dac77f2011-06-01 15:34:25 +03001321 if (ctxt->src.bytes == 2)
1322 sv = (s16)ctxt->src.val & (s16)mask;
1323 else if (ctxt->src.bytes == 4)
1324 sv = (s32)ctxt->src.val & (s32)mask;
Nadav Amit7dec5602014-06-15 16:12:57 +03001325 else
1326 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001327
Nadav Amit1c1c35a2014-11-19 17:43:09 +02001328 ctxt->dst.addr.mem.ea = address_mask(ctxt,
1329 ctxt->dst.addr.mem.ea + (sv >> 3));
Wei Yongjun35c843c2010-08-09 11:34:56 +08001330 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001331
1332 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001333 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001334}
1335
Gleb Natapov9de41572010-04-28 19:15:22 +03001336static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001337 unsigned long addr, void *dest, unsigned size)
1338{
1339 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001340 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001341
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001342 if (mc->pos < mc->end)
1343 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001344
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001345 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001346
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001347 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1348 &ctxt->exception);
1349 if (rc != X86EMUL_CONTINUE)
1350 return rc;
1351
1352 mc->end += size;
1353
1354read_cached:
1355 memcpy(dest, mc->data + mc->pos, size);
1356 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001357 return X86EMUL_CONTINUE;
1358}
1359
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001360static int segmented_read(struct x86_emulate_ctxt *ctxt,
1361 struct segmented_address addr,
1362 void *data,
1363 unsigned size)
1364{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001365 int rc;
1366 ulong linear;
1367
Avi Kivity83b87952011-04-03 11:31:19 +03001368 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001369 if (rc != X86EMUL_CONTINUE)
1370 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001371 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001372}
1373
1374static int segmented_write(struct x86_emulate_ctxt *ctxt,
1375 struct segmented_address addr,
1376 const void *data,
1377 unsigned size)
1378{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001379 int rc;
1380 ulong linear;
1381
Avi Kivity83b87952011-04-03 11:31:19 +03001382 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001383 if (rc != X86EMUL_CONTINUE)
1384 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001385 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1386 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001387}
1388
1389static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1390 struct segmented_address addr,
1391 const void *orig_data, const void *data,
1392 unsigned size)
1393{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001394 int rc;
1395 ulong linear;
1396
Avi Kivity83b87952011-04-03 11:31:19 +03001397 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001398 if (rc != X86EMUL_CONTINUE)
1399 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001400 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1401 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001402}
1403
Gleb Natapov7b262e92010-03-18 15:20:27 +02001404static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001405 unsigned int size, unsigned short port,
1406 void *dest)
1407{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001408 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001409
1410 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001411 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001412 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001413 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Nadav Amit0efb0442015-03-29 16:33:03 +03001414 in_page = (ctxt->eflags & X86_EFLAGS_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001415 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1416 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Mark Rustadb55a8142014-07-25 06:27:05 -07001417 n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001418 if (n == 0)
1419 n = 1;
1420 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001421 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001422 return 0;
1423 rc->end = n * size;
1424 }
1425
Nadav Amite6e39f02014-04-18 03:35:10 +03001426 if (ctxt->rep_prefix && (ctxt->d & String) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03001427 !(ctxt->eflags & X86_EFLAGS_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001428 ctxt->dst.data = rc->data + rc->pos;
1429 ctxt->dst.type = OP_MEM_STR;
1430 ctxt->dst.count = (rc->end - rc->pos) / size;
1431 rc->pos = rc->end;
1432 } else {
1433 memcpy(dest, rc->data + rc->pos, size);
1434 rc->pos += size;
1435 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001436 return 1;
1437}
1438
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001439static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1440 u16 index, struct desc_struct *desc)
1441{
1442 struct desc_ptr dt;
1443 ulong addr;
1444
1445 ctxt->ops->get_idt(ctxt, &dt);
1446
1447 if (dt.size < index * 8 + 7)
1448 return emulate_gp(ctxt, index << 3 | 0x2);
1449
1450 addr = dt.address + index * 8;
Jordan Borgner0e96f312018-10-28 12:58:28 +00001451 return linear_read_system(ctxt, addr, desc, sizeof(*desc));
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001452}
1453
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001454static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001455 u16 selector, struct desc_ptr *dt)
1456{
Mathias Krause0225fb52012-08-30 01:30:16 +02001457 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001458 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001459
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001460 if (selector & 1 << 2) {
1461 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001462 u16 sel;
1463
Jordan Borgner0e96f312018-10-28 12:58:28 +00001464 memset(dt, 0, sizeof(*dt));
Nadav Amit2eedcac2014-06-02 18:34:05 +03001465 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1466 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001467 return;
1468
1469 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001470 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001471 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001472 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001473}
1474
Nadav Amitedccda72014-12-25 02:52:23 +02001475static int get_descriptor_ptr(struct x86_emulate_ctxt *ctxt,
1476 u16 selector, ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001477{
1478 struct desc_ptr dt;
1479 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001480 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001481
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001482 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001483
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001484 if (dt.size < index * 8 + 7)
1485 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001486
1487 addr = dt.address + index * 8;
Nadav Amitedccda72014-12-25 02:52:23 +02001488
1489#ifdef CONFIG_X86_64
1490 if (addr >> 32 != 0) {
1491 u64 efer = 0;
1492
1493 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1494 if (!(efer & EFER_LMA))
1495 addr &= (u32)-1;
1496 }
1497#endif
1498
1499 *desc_addr_p = addr;
1500 return X86EMUL_CONTINUE;
1501}
1502
1503/* allowed just for 8 bytes segments */
1504static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1505 u16 selector, struct desc_struct *desc,
1506 ulong *desc_addr_p)
1507{
1508 int rc;
1509
1510 rc = get_descriptor_ptr(ctxt, selector, desc_addr_p);
1511 if (rc != X86EMUL_CONTINUE)
1512 return rc;
1513
Paolo Bonzini79367a62018-06-06 16:43:02 +02001514 return linear_read_system(ctxt, *desc_addr_p, desc, sizeof(*desc));
Nadav Amitedccda72014-12-25 02:52:23 +02001515}
1516
1517/* allowed just for 8 bytes segments */
1518static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1519 u16 selector, struct desc_struct *desc)
1520{
1521 int rc;
1522 ulong addr;
1523
1524 rc = get_descriptor_ptr(ctxt, selector, &addr);
1525 if (rc != X86EMUL_CONTINUE)
1526 return rc;
1527
Jordan Borgner0e96f312018-10-28 12:58:28 +00001528 return linear_write_system(ctxt, addr, desc, sizeof(*desc));
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001529}
1530
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001531static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Nadav Amitd1442d82014-09-18 22:39:39 +03001532 u16 selector, int seg, u8 cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001533 enum x86_transfer_type transfer,
Nadav Amitd1442d82014-09-18 22:39:39 +03001534 struct desc_struct *desc)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001535{
Avi Kivity869be992012-06-13 16:30:53 +03001536 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001537 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001538 unsigned err_vec = GP_VECTOR;
1539 u32 err_code = 0;
1540 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001541 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001542 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001543 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001544 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001545
Jordan Borgner0e96f312018-10-28 12:58:28 +00001546 memset(&seg_desc, 0, sizeof(seg_desc));
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001547
Kevin Wolff8da94e2013-04-11 14:06:03 +02001548 if (ctxt->mode == X86EMUL_MODE_REAL) {
1549 /* set real mode segment descriptor (keep limit etc. for
1550 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001551 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001552 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001553 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001554 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1555 /* VM86 needs a clean new segment descriptor */
1556 set_desc_base(&seg_desc, selector << 4);
1557 set_desc_limit(&seg_desc, 0xffff);
1558 seg_desc.type = 3;
1559 seg_desc.p = 1;
1560 seg_desc.s = 1;
1561 seg_desc.dpl = 3;
1562 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001563 }
1564
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001565 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001566
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001567 /* TR should be in GDT only */
1568 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1569 goto exception;
1570
Paolo Bonzini33ab9112017-01-12 15:02:32 +01001571 /* NULL selector is not valid for TR, CS and (except for long mode) SS */
1572 if (null_selector) {
1573 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_TR)
1574 goto exception;
1575
1576 if (seg == VCPU_SREG_SS) {
1577 if (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl)
1578 goto exception;
1579
1580 /*
1581 * ctxt->ops->set_segment expects the CPL to be in
1582 * SS.DPL, so fake an expand-up 32-bit data segment.
1583 */
1584 seg_desc.type = 3;
1585 seg_desc.p = 1;
1586 seg_desc.s = 1;
1587 seg_desc.dpl = cpl;
1588 seg_desc.d = 1;
1589 seg_desc.g = 1;
1590 }
1591
1592 /* Skip all following checks */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001593 goto load;
Paolo Bonzini33ab9112017-01-12 15:02:32 +01001594 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001595
Avi Kivitye9194642012-06-13 16:29:39 +03001596 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001597 if (ret != X86EMUL_CONTINUE)
1598 return ret;
1599
1600 err_code = selector & 0xfffc;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001601 err_vec = (transfer == X86_TRANSFER_TASK_SWITCH) ? TS_VECTOR :
1602 GP_VECTOR;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001603
Guo Chaofc058682012-06-28 15:19:51 +08001604 /* can't load system descriptor into segment selector */
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001605 if (seg <= VCPU_SREG_GS && !seg_desc.s) {
1606 if (transfer == X86_TRANSFER_CALL_JMP)
1607 return X86EMUL_UNHANDLEABLE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001608 goto exception;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001609 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001610
1611 if (!seg_desc.p) {
1612 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1613 goto exception;
1614 }
1615
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001616 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001617
1618 switch (seg) {
1619 case VCPU_SREG_SS:
1620 /*
1621 * segment is not a writable data segment or segment
1622 * selector's RPL != CPL or segment selector's RPL != CPL
1623 */
1624 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1625 goto exception;
1626 break;
1627 case VCPU_SREG_CS:
1628 if (!(seg_desc.type & 8))
1629 goto exception;
1630
1631 if (seg_desc.type & 4) {
1632 /* conforming */
1633 if (dpl > cpl)
1634 goto exception;
1635 } else {
1636 /* nonconforming */
1637 if (rpl > cpl || dpl != cpl)
1638 goto exception;
1639 }
Nadav Amit040c8dc2014-09-18 22:39:43 +03001640 /* in long-mode d/b must be clear if l is set */
1641 if (seg_desc.d && seg_desc.l) {
1642 u64 efer = 0;
1643
1644 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1645 if (efer & EFER_LMA)
1646 goto exception;
1647 }
1648
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001649 /* CS(RPL) <- CPL */
1650 selector = (selector & 0xfffc) | cpl;
1651 break;
1652 case VCPU_SREG_TR:
1653 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1654 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001655 old_desc = seg_desc;
1656 seg_desc.type |= 2; /* busy */
1657 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1658 sizeof(seg_desc), &ctxt->exception);
1659 if (ret != X86EMUL_CONTINUE)
1660 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001661 break;
1662 case VCPU_SREG_LDTR:
1663 if (seg_desc.s || seg_desc.type != 2)
1664 goto exception;
1665 break;
1666 default: /* DS, ES, FS, or GS */
1667 /*
1668 * segment is not a data or readable code segment or
1669 * ((segment is a data or nonconforming code segment)
1670 * and (both RPL and CPL > DPL))
1671 */
1672 if ((seg_desc.type & 0xa) == 0x8 ||
1673 (((seg_desc.type & 0xc) != 0xc) &&
1674 (rpl > dpl && cpl > dpl)))
1675 goto exception;
1676 break;
1677 }
1678
1679 if (seg_desc.s) {
1680 /* mark segment as accessed */
Nadav Amite2cefa72014-12-25 02:52:22 +02001681 if (!(seg_desc.type & 1)) {
1682 seg_desc.type |= 1;
1683 ret = write_segment_descriptor(ctxt, selector,
1684 &seg_desc);
1685 if (ret != X86EMUL_CONTINUE)
1686 return ret;
1687 }
Nadav Amite37a75a2014-06-02 18:34:04 +03001688 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
Paolo Bonzini79367a62018-06-06 16:43:02 +02001689 ret = linear_read_system(ctxt, desc_addr+8, &base3, sizeof(base3));
Nadav Amite37a75a2014-06-02 18:34:04 +03001690 if (ret != X86EMUL_CONTINUE)
1691 return ret;
Yu Zhangfd8cb432017-08-24 20:27:56 +08001692 if (emul_is_noncanonical_address(get_desc_base(&seg_desc) |
1693 ((u64)base3 << 32), ctxt))
Nadav Amit9a9abf62014-11-02 11:54:56 +02001694 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001695 }
1696load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001697 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Nadav Amitd1442d82014-09-18 22:39:39 +03001698 if (desc)
1699 *desc = seg_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001700 return X86EMUL_CONTINUE;
1701exception:
Paolo Bonzini592f0852014-08-20 10:05:08 +02001702 return emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001703}
1704
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001705static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1706 u16 selector, int seg)
1707{
1708 u8 cpl = ctxt->ops->cpl(ctxt);
Paolo Bonzini33ab9112017-01-12 15:02:32 +01001709
1710 /*
1711 * None of MOV, POP and LSS can load a NULL selector in CPL=3, but
1712 * they can load it at CPL<3 (Intel's manual says only LSS can,
1713 * but it's wrong).
1714 *
1715 * However, the Intel manual says that putting IST=1/DPL=3 in
1716 * an interrupt gate will result in SS=3 (the AMD manual instead
1717 * says it doesn't), so allow SS=3 in __load_segment_descriptor
1718 * and only forbid it here.
1719 */
1720 if (seg == VCPU_SREG_SS && selector == 3 &&
1721 ctxt->mode == X86EMUL_MODE_PROT64)
1722 return emulate_exception(ctxt, GP_VECTOR, 0, true);
1723
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001724 return __load_segment_descriptor(ctxt, selector, seg, cpl,
1725 X86_TRANSFER_NONE, NULL);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001726}
1727
Wei Yongjun31be40b2010-08-17 09:17:30 +08001728static void write_register_operand(struct operand *op)
1729{
Nadav Amit6fd8e122015-03-30 15:39:20 +03001730 return assign_register(op->addr.reg, op->val, op->bytes);
Wei Yongjun31be40b2010-08-17 09:17:30 +08001731}
1732
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001733static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001734{
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001735 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001736 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001737 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001738 break;
1739 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001740 if (ctxt->lock_prefix)
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001741 return segmented_cmpxchg(ctxt,
1742 op->addr.mem,
1743 &op->orig_val,
1744 &op->val,
1745 op->bytes);
1746 else
1747 return segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001748 op->addr.mem,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001749 &op->val,
1750 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001751 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001752 case OP_MEM_STR:
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001753 return segmented_write(ctxt,
1754 op->addr.mem,
1755 op->data,
1756 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001757 break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001758 case OP_XMM:
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02001759 kvm_write_sse_reg(op->addr.xmm, &op->vec_val);
Avi Kivity1253791d2011-03-29 11:41:27 +02001760 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001761 case OP_MM:
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02001762 kvm_write_mmx_reg(op->addr.mm, &op->mm_val);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001763 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001764 case OP_NONE:
1765 /* no writeback */
1766 break;
1767 default:
1768 break;
1769 }
1770 return X86EMUL_CONTINUE;
1771}
1772
Avi Kivity51ddff52012-06-12 20:19:40 +03001773static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001774{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001775 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001776
Avi Kivity5ad105e2012-08-19 14:34:31 +03001777 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001778 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001779 addr.seg = VCPU_SREG_SS;
1780
Avi Kivity51ddff52012-06-12 20:19:40 +03001781 return segmented_write(ctxt, addr, data, bytes);
1782}
1783
1784static int em_push(struct x86_emulate_ctxt *ctxt)
1785{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001786 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001787 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001788 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001789}
1790
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001791static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001792 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001793{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001794 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001795 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001796
Avi Kivitydd856ef2012-08-27 23:46:17 +03001797 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001798 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001799 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001800 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001801 return rc;
1802
Avi Kivity5ad105e2012-08-19 14:34:31 +03001803 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001804 return rc;
1805}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001806
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001807static int em_pop(struct x86_emulate_ctxt *ctxt)
1808{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001809 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001810}
1811
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001812static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001813 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001814{
1815 int rc;
1816 unsigned long val, change_mask;
Nadav Amit0efb0442015-03-29 16:33:03 +03001817 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001818 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001819
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001820 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001821 if (rc != X86EMUL_CONTINUE)
1822 return rc;
1823
Nadav Amit0efb0442015-03-29 16:33:03 +03001824 change_mask = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
1825 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF |
1826 X86_EFLAGS_TF | X86_EFLAGS_DF | X86_EFLAGS_NT |
1827 X86_EFLAGS_AC | X86_EFLAGS_ID;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001828
1829 switch(ctxt->mode) {
1830 case X86EMUL_MODE_PROT64:
1831 case X86EMUL_MODE_PROT32:
1832 case X86EMUL_MODE_PROT16:
1833 if (cpl == 0)
Nadav Amit0efb0442015-03-29 16:33:03 +03001834 change_mask |= X86_EFLAGS_IOPL;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001835 if (cpl <= iopl)
Nadav Amit0efb0442015-03-29 16:33:03 +03001836 change_mask |= X86_EFLAGS_IF;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001837 break;
1838 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001839 if (iopl < 3)
1840 return emulate_gp(ctxt, 0);
Nadav Amit0efb0442015-03-29 16:33:03 +03001841 change_mask |= X86_EFLAGS_IF;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001842 break;
1843 default: /* real mode */
Nadav Amit0efb0442015-03-29 16:33:03 +03001844 change_mask |= (X86_EFLAGS_IOPL | X86_EFLAGS_IF);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001845 break;
1846 }
1847
1848 *(unsigned long *)dest =
1849 (ctxt->eflags & ~change_mask) | (val & change_mask);
1850
1851 return rc;
1852}
1853
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001854static int em_popf(struct x86_emulate_ctxt *ctxt)
1855{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001856 ctxt->dst.type = OP_REG;
1857 ctxt->dst.addr.reg = &ctxt->eflags;
1858 ctxt->dst.bytes = ctxt->op_bytes;
1859 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001860}
1861
Avi Kivity612e89f2012-06-12 20:03:23 +03001862static int em_enter(struct x86_emulate_ctxt *ctxt)
1863{
1864 int rc;
1865 unsigned frame_size = ctxt->src.val;
1866 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001867 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001868
1869 if (nesting_level)
1870 return X86EMUL_UNHANDLEABLE;
1871
Avi Kivitydd856ef2012-08-27 23:46:17 +03001872 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1873 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001874 if (rc != X86EMUL_CONTINUE)
1875 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001876 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001877 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001878 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1879 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001880 stack_mask(ctxt));
1881 return X86EMUL_CONTINUE;
1882}
1883
Avi Kivityf47cfa32012-06-07 17:49:24 +03001884static int em_leave(struct x86_emulate_ctxt *ctxt)
1885{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001886 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001887 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001888 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001889}
1890
Avi Kivity1cd196e2011-09-13 10:45:51 +03001891static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001892{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001893 int seg = ctxt->src2.val;
1894
Avi Kivity9dac77f2011-06-01 15:34:25 +03001895 ctxt->src.val = get_segment_selector(ctxt, seg);
Nadav Amit0fcc2072014-11-02 11:54:51 +02001896 if (ctxt->op_bytes == 4) {
1897 rsp_increment(ctxt, -2);
1898 ctxt->op_bytes = 2;
1899 }
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001900
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001901 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001902}
1903
Avi Kivity1cd196e2011-09-13 10:45:51 +03001904static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001905{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001906 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001907 unsigned long selector;
1908 int rc;
1909
Nadav Amit3313bc42014-12-25 02:52:17 +02001910 rc = emulate_pop(ctxt, &selector, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001911 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001912 return rc;
1913
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001914 if (ctxt->modrm_reg == VCPU_SREG_SS)
1915 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Nadav Amit3313bc42014-12-25 02:52:17 +02001916 if (ctxt->op_bytes > 2)
1917 rsp_increment(ctxt, ctxt->op_bytes - 2);
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001918
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001919 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001920 return rc;
1921}
1922
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001923static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001924{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001925 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001926 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001927 int reg = VCPU_REGS_RAX;
1928
1929 while (reg <= VCPU_REGS_RDI) {
1930 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001931 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001932
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001933 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001934 if (rc != X86EMUL_CONTINUE)
1935 return rc;
1936
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001937 ++reg;
1938 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001939
Wei Yongjunc37eda12010-06-15 09:03:33 +08001940 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001941}
1942
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001943static int em_pushf(struct x86_emulate_ctxt *ctxt)
1944{
Nadav Amit0efb0442015-03-29 16:33:03 +03001945 ctxt->src.val = (unsigned long)ctxt->eflags & ~X86_EFLAGS_VM;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001946 return em_push(ctxt);
1947}
1948
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001949static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001950{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001951 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001952 int reg = VCPU_REGS_RDI;
Nadav Amit6fd8e122015-03-30 15:39:20 +03001953 u32 val;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001954
1955 while (reg >= VCPU_REGS_RAX) {
1956 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001957 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001958 --reg;
1959 }
1960
Nadav Amit6fd8e122015-03-30 15:39:20 +03001961 rc = emulate_pop(ctxt, &val, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001962 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001963 break;
Nadav Amit6fd8e122015-03-30 15:39:20 +03001964 assign_register(reg_rmw(ctxt, reg), val, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001965 --reg;
1966 }
1967 return rc;
1968}
1969
Avi Kivitydd856ef2012-08-27 23:46:17 +03001970static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001971{
Mathias Krause0225fb52012-08-30 01:30:16 +02001972 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001973 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001974 struct desc_ptr dt;
1975 gva_t cs_addr;
1976 gva_t eip_addr;
1977 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001978
1979 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001980 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001981 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001982 if (rc != X86EMUL_CONTINUE)
1983 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001984
Nadav Amit0efb0442015-03-29 16:33:03 +03001985 ctxt->eflags &= ~(X86_EFLAGS_IF | X86_EFLAGS_TF | X86_EFLAGS_AC);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001986
Avi Kivity9dac77f2011-06-01 15:34:25 +03001987 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001988 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001989 if (rc != X86EMUL_CONTINUE)
1990 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001991
Avi Kivity9dac77f2011-06-01 15:34:25 +03001992 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001993 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001994 if (rc != X86EMUL_CONTINUE)
1995 return rc;
1996
Avi Kivity4bff1e862011-04-20 13:37:53 +03001997 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001998
1999 eip_addr = dt.address + (irq << 2);
2000 cs_addr = dt.address + (irq << 2) + 2;
2001
Paolo Bonzini79367a62018-06-06 16:43:02 +02002002 rc = linear_read_system(ctxt, cs_addr, &cs, 2);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002003 if (rc != X86EMUL_CONTINUE)
2004 return rc;
2005
Paolo Bonzini79367a62018-06-06 16:43:02 +02002006 rc = linear_read_system(ctxt, eip_addr, &eip, 2);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002007 if (rc != X86EMUL_CONTINUE)
2008 return rc;
2009
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002010 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002011 if (rc != X86EMUL_CONTINUE)
2012 return rc;
2013
Avi Kivity9dac77f2011-06-01 15:34:25 +03002014 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002015
2016 return rc;
2017}
2018
Avi Kivitydd856ef2012-08-27 23:46:17 +03002019int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
2020{
2021 int rc;
2022
2023 invalidate_registers(ctxt);
2024 rc = __emulate_int_real(ctxt, irq);
2025 if (rc == X86EMUL_CONTINUE)
2026 writeback_registers(ctxt);
2027 return rc;
2028}
2029
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002030static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002031{
2032 switch(ctxt->mode) {
2033 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03002034 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002035 case X86EMUL_MODE_VM86:
2036 case X86EMUL_MODE_PROT16:
2037 case X86EMUL_MODE_PROT32:
2038 case X86EMUL_MODE_PROT64:
2039 default:
2040 /* Protected mode interrupts unimplemented yet */
2041 return X86EMUL_UNHANDLEABLE;
2042 }
2043}
2044
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002045static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002046{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002047 int rc = X86EMUL_CONTINUE;
2048 unsigned long temp_eip = 0;
2049 unsigned long temp_eflags = 0;
2050 unsigned long cs = 0;
Nadav Amit0efb0442015-03-29 16:33:03 +03002051 unsigned long mask = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
2052 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_TF |
2053 X86_EFLAGS_IF | X86_EFLAGS_DF | X86_EFLAGS_OF |
2054 X86_EFLAGS_IOPL | X86_EFLAGS_NT | X86_EFLAGS_RF |
2055 X86_EFLAGS_AC | X86_EFLAGS_ID |
Wanpeng Li35fd68a2015-04-08 14:08:14 +08002056 X86_EFLAGS_FIXED;
Nadav Amit0efb0442015-03-29 16:33:03 +03002057 unsigned long vm86_mask = X86_EFLAGS_VM | X86_EFLAGS_VIF |
2058 X86_EFLAGS_VIP;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002059
2060 /* TODO: Add stack limit check */
2061
Avi Kivity9dac77f2011-06-01 15:34:25 +03002062 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002063
2064 if (rc != X86EMUL_CONTINUE)
2065 return rc;
2066
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002067 if (temp_eip & ~0xffff)
2068 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002069
Avi Kivity9dac77f2011-06-01 15:34:25 +03002070 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002071
2072 if (rc != X86EMUL_CONTINUE)
2073 return rc;
2074
Avi Kivity9dac77f2011-06-01 15:34:25 +03002075 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002076
2077 if (rc != X86EMUL_CONTINUE)
2078 return rc;
2079
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002080 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002081
2082 if (rc != X86EMUL_CONTINUE)
2083 return rc;
2084
Avi Kivity9dac77f2011-06-01 15:34:25 +03002085 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002086
Avi Kivity9dac77f2011-06-01 15:34:25 +03002087 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002088 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03002089 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002090 ctxt->eflags &= ~0xffff;
2091 ctxt->eflags |= temp_eflags;
2092 }
2093
2094 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
Wanpeng Li35fd68a2015-04-08 14:08:14 +08002095 ctxt->eflags |= X86_EFLAGS_FIXED;
Nadav Amit801806d2015-01-26 09:32:23 +02002096 ctxt->ops->set_nmi_mask(ctxt, false);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002097
2098 return rc;
2099}
2100
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002101static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002102{
2103 switch(ctxt->mode) {
2104 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002105 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002106 case X86EMUL_MODE_VM86:
2107 case X86EMUL_MODE_PROT16:
2108 case X86EMUL_MODE_PROT32:
2109 case X86EMUL_MODE_PROT64:
2110 default:
2111 /* iret from protected mode unimplemented yet */
2112 return X86EMUL_UNHANDLEABLE;
2113 }
2114}
2115
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002116static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2117{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002118 int rc;
Radim Krčmář2117d532016-11-23 21:15:00 +01002119 unsigned short sel;
2120 struct desc_struct new_desc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002121 u8 cpl = ctxt->ops->cpl(ctxt);
2122
Avi Kivity9dac77f2011-06-01 15:34:25 +03002123 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002124
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002125 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
2126 X86_TRANSFER_CALL_JMP,
Nadav Amitd1442d82014-09-18 22:39:39 +03002127 &new_desc);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002128 if (rc != X86EMUL_CONTINUE)
2129 return rc;
2130
Nadav Amitd50eaa12014-11-19 17:43:11 +02002131 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Radim Krčmář2117d532016-11-23 21:15:00 +01002132 /* Error handling is not implemented. */
2133 if (rc != X86EMUL_CONTINUE)
2134 return X86EMUL_UNHANDLEABLE;
2135
Nadav Amitd1442d82014-09-18 22:39:39 +03002136 return rc;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002137}
2138
Nadav Amitf7784042014-09-18 22:39:41 +03002139static int em_jmp_abs(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002140{
Nadav Amitf7784042014-09-18 22:39:41 +03002141 return assign_eip_near(ctxt, ctxt->src.val);
2142}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002143
Nadav Amitf7784042014-09-18 22:39:41 +03002144static int em_call_near_abs(struct x86_emulate_ctxt *ctxt)
2145{
2146 int rc;
2147 long int old_eip;
2148
2149 old_eip = ctxt->_eip;
2150 rc = assign_eip_near(ctxt, ctxt->src.val);
2151 if (rc != X86EMUL_CONTINUE)
2152 return rc;
2153 ctxt->src.val = old_eip;
2154 rc = em_push(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002155 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002156}
2157
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002158static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002159{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002160 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002161
Nadav Amitaaa05f22014-06-02 18:34:10 +03002162 if (ctxt->dst.bytes == 16)
2163 return X86EMUL_UNHANDLEABLE;
2164
Avi Kivitydd856ef2012-08-27 23:46:17 +03002165 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2166 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2167 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2168 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Nadav Amit0efb0442015-03-29 16:33:03 +03002169 ctxt->eflags &= ~X86_EFLAGS_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002170 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002171 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2172 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002173
Nadav Amit0efb0442015-03-29 16:33:03 +03002174 ctxt->eflags |= X86_EFLAGS_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002175 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002176 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002177}
2178
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002179static int em_ret(struct x86_emulate_ctxt *ctxt)
2180{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002181 int rc;
2182 unsigned long eip;
2183
2184 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
2185 if (rc != X86EMUL_CONTINUE)
2186 return rc;
2187
2188 return assign_eip_near(ctxt, eip);
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002189}
2190
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002191static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002192{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002193 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002194 unsigned long eip, cs;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002195 int cpl = ctxt->ops->cpl(ctxt);
Radim Krčmář2117d532016-11-23 21:15:00 +01002196 struct desc_struct new_desc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002197
2198 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002199 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002200 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002201 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002202 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002203 return rc;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002204 /* Outer-privilege level return is not implemented */
2205 if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2206 return X86EMUL_UNHANDLEABLE;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002207 rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, cpl,
2208 X86_TRANSFER_RET,
Nadav Amitd1442d82014-09-18 22:39:39 +03002209 &new_desc);
2210 if (rc != X86EMUL_CONTINUE)
2211 return rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +02002212 rc = assign_eip_far(ctxt, eip, &new_desc);
Radim Krčmář2117d532016-11-23 21:15:00 +01002213 /* Error handling is not implemented. */
2214 if (rc != X86EMUL_CONTINUE)
2215 return X86EMUL_UNHANDLEABLE;
2216
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002217 return rc;
2218}
2219
Bruce Rogers32611072013-09-09 09:40:20 -06002220static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2221{
2222 int rc;
2223
2224 rc = em_ret_far(ctxt);
2225 if (rc != X86EMUL_CONTINUE)
2226 return rc;
2227 rsp_increment(ctxt, ctxt->src.val);
2228 return X86EMUL_CONTINUE;
2229}
2230
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002231static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2232{
2233 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002234 ctxt->dst.orig_val = ctxt->dst.val;
2235 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002236 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002237 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002238 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002239
Nadav Amit0efb0442015-03-29 16:33:03 +03002240 if (ctxt->eflags & X86_EFLAGS_ZF) {
Nadav Amit2fcf5c82015-01-26 09:32:21 +02002241 /* Success: write back to memory; no update of EAX */
2242 ctxt->src.type = OP_NONE;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002243 ctxt->dst.val = ctxt->src.orig_val;
2244 } else {
2245 /* Failure: write the value we saw to EAX. */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02002246 ctxt->src.type = OP_REG;
2247 ctxt->src.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
2248 ctxt->src.val = ctxt->dst.orig_val;
2249 /* Create write-cycle to dest by writing the same value */
Nadav Amit37c564f2014-06-02 18:34:07 +03002250 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002251 }
2252 return X86EMUL_CONTINUE;
2253}
2254
Avi Kivityd4b43252011-09-13 10:45:50 +03002255static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002256{
Avi Kivityd4b43252011-09-13 10:45:50 +03002257 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002258 unsigned short sel;
2259 int rc;
2260
Avi Kivity9dac77f2011-06-01 15:34:25 +03002261 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002262
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002263 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002264 if (rc != X86EMUL_CONTINUE)
2265 return rc;
2266
Avi Kivity9dac77f2011-06-01 15:34:25 +03002267 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002268 return rc;
2269}
2270
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002271static int emulator_has_longmode(struct x86_emulate_ctxt *ctxt)
2272{
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002273#ifdef CONFIG_X86_64
Sean Christopherson5ae78e92019-12-17 13:32:38 -08002274 return ctxt->ops->guest_has_long_mode(ctxt);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002275#else
2276 return false;
2277#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002278}
2279
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002280static void rsm_set_desc_flags(struct desc_struct *desc, u32 flags)
2281{
2282 desc->g = (flags >> 23) & 1;
2283 desc->d = (flags >> 22) & 1;
2284 desc->l = (flags >> 21) & 1;
2285 desc->avl = (flags >> 20) & 1;
2286 desc->p = (flags >> 15) & 1;
2287 desc->dpl = (flags >> 13) & 3;
2288 desc->s = (flags >> 12) & 1;
2289 desc->type = (flags >> 8) & 15;
2290}
2291
Sean Christophersoned193212019-04-02 08:03:09 -07002292static int rsm_load_seg_32(struct x86_emulate_ctxt *ctxt, const char *smstate,
2293 int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002294{
2295 struct desc_struct desc;
2296 int offset;
2297 u16 selector;
2298
Sean Christophersoned193212019-04-02 08:03:09 -07002299 selector = GET_SMSTATE(u32, smstate, 0x7fa8 + n * 4);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002300
2301 if (n < 3)
2302 offset = 0x7f84 + n * 12;
2303 else
2304 offset = 0x7f2c + (n - 3) * 12;
2305
Sean Christophersoned193212019-04-02 08:03:09 -07002306 set_desc_base(&desc, GET_SMSTATE(u32, smstate, offset + 8));
2307 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, offset + 4));
2308 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smstate, offset));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002309 ctxt->ops->set_segment(ctxt, selector, &desc, 0, n);
2310 return X86EMUL_CONTINUE;
2311}
2312
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002313#ifdef CONFIG_X86_64
Sean Christophersoned193212019-04-02 08:03:09 -07002314static int rsm_load_seg_64(struct x86_emulate_ctxt *ctxt, const char *smstate,
2315 int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002316{
2317 struct desc_struct desc;
2318 int offset;
2319 u16 selector;
2320 u32 base3;
2321
2322 offset = 0x7e00 + n * 16;
2323
Sean Christophersoned193212019-04-02 08:03:09 -07002324 selector = GET_SMSTATE(u16, smstate, offset);
2325 rsm_set_desc_flags(&desc, GET_SMSTATE(u16, smstate, offset + 2) << 8);
2326 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, offset + 4));
2327 set_desc_base(&desc, GET_SMSTATE(u32, smstate, offset + 8));
2328 base3 = GET_SMSTATE(u32, smstate, offset + 12);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002329
2330 ctxt->ops->set_segment(ctxt, selector, &desc, base3, n);
2331 return X86EMUL_CONTINUE;
2332}
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002333#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002334
2335static int rsm_enter_protected_mode(struct x86_emulate_ctxt *ctxt,
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002336 u64 cr0, u64 cr3, u64 cr4)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002337{
2338 int bad;
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002339 u64 pcid;
2340
2341 /* In order to later set CR4.PCIDE, CR3[11:0] must be zero. */
2342 pcid = 0;
2343 if (cr4 & X86_CR4_PCIDE) {
2344 pcid = cr3 & 0xfff;
2345 cr3 &= ~0xfff;
2346 }
2347
2348 bad = ctxt->ops->set_cr(ctxt, 3, cr3);
2349 if (bad)
2350 return X86EMUL_UNHANDLEABLE;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002351
2352 /*
2353 * First enable PAE, long mode needs it before CR0.PG = 1 is set.
2354 * Then enable protected mode. However, PCID cannot be enabled
2355 * if EFER.LMA=0, so set it separately.
2356 */
2357 bad = ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PCIDE);
2358 if (bad)
2359 return X86EMUL_UNHANDLEABLE;
2360
2361 bad = ctxt->ops->set_cr(ctxt, 0, cr0);
2362 if (bad)
2363 return X86EMUL_UNHANDLEABLE;
2364
2365 if (cr4 & X86_CR4_PCIDE) {
2366 bad = ctxt->ops->set_cr(ctxt, 4, cr4);
2367 if (bad)
2368 return X86EMUL_UNHANDLEABLE;
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002369 if (pcid) {
2370 bad = ctxt->ops->set_cr(ctxt, 3, cr3 | pcid);
2371 if (bad)
2372 return X86EMUL_UNHANDLEABLE;
2373 }
2374
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002375 }
2376
2377 return X86EMUL_CONTINUE;
2378}
2379
Sean Christophersoned193212019-04-02 08:03:09 -07002380static int rsm_load_state_32(struct x86_emulate_ctxt *ctxt,
2381 const char *smstate)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002382{
2383 struct desc_struct desc;
2384 struct desc_ptr dt;
2385 u16 selector;
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002386 u32 val, cr0, cr3, cr4;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002387 int i;
2388
Sean Christophersoned193212019-04-02 08:03:09 -07002389 cr0 = GET_SMSTATE(u32, smstate, 0x7ffc);
2390 cr3 = GET_SMSTATE(u32, smstate, 0x7ff8);
2391 ctxt->eflags = GET_SMSTATE(u32, smstate, 0x7ff4) | X86_EFLAGS_FIXED;
2392 ctxt->_eip = GET_SMSTATE(u32, smstate, 0x7ff0);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002393
2394 for (i = 0; i < 8; i++)
Sean Christophersoned193212019-04-02 08:03:09 -07002395 *reg_write(ctxt, i) = GET_SMSTATE(u32, smstate, 0x7fd0 + i * 4);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002396
Sean Christophersoned193212019-04-02 08:03:09 -07002397 val = GET_SMSTATE(u32, smstate, 0x7fcc);
Maxim Levitsky37f66bb2020-08-27 20:11:44 +03002398
Sean Christopherson16d51632021-02-04 17:24:57 -08002399 if (ctxt->ops->set_dr(ctxt, 6, val))
Maxim Levitsky37f66bb2020-08-27 20:11:44 +03002400 return X86EMUL_UNHANDLEABLE;
2401
Sean Christophersoned193212019-04-02 08:03:09 -07002402 val = GET_SMSTATE(u32, smstate, 0x7fc8);
Maxim Levitsky37f66bb2020-08-27 20:11:44 +03002403
Sean Christopherson16d51632021-02-04 17:24:57 -08002404 if (ctxt->ops->set_dr(ctxt, 7, val))
Maxim Levitsky37f66bb2020-08-27 20:11:44 +03002405 return X86EMUL_UNHANDLEABLE;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002406
Sean Christophersoned193212019-04-02 08:03:09 -07002407 selector = GET_SMSTATE(u32, smstate, 0x7fc4);
2408 set_desc_base(&desc, GET_SMSTATE(u32, smstate, 0x7f64));
2409 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, 0x7f60));
2410 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smstate, 0x7f5c));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002411 ctxt->ops->set_segment(ctxt, selector, &desc, 0, VCPU_SREG_TR);
2412
Sean Christophersoned193212019-04-02 08:03:09 -07002413 selector = GET_SMSTATE(u32, smstate, 0x7fc0);
2414 set_desc_base(&desc, GET_SMSTATE(u32, smstate, 0x7f80));
2415 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, 0x7f7c));
2416 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smstate, 0x7f78));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002417 ctxt->ops->set_segment(ctxt, selector, &desc, 0, VCPU_SREG_LDTR);
2418
Sean Christophersoned193212019-04-02 08:03:09 -07002419 dt.address = GET_SMSTATE(u32, smstate, 0x7f74);
2420 dt.size = GET_SMSTATE(u32, smstate, 0x7f70);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002421 ctxt->ops->set_gdt(ctxt, &dt);
2422
Sean Christophersoned193212019-04-02 08:03:09 -07002423 dt.address = GET_SMSTATE(u32, smstate, 0x7f58);
2424 dt.size = GET_SMSTATE(u32, smstate, 0x7f54);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002425 ctxt->ops->set_idt(ctxt, &dt);
2426
2427 for (i = 0; i < 6; i++) {
Sean Christophersoned193212019-04-02 08:03:09 -07002428 int r = rsm_load_seg_32(ctxt, smstate, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002429 if (r != X86EMUL_CONTINUE)
2430 return r;
2431 }
2432
Sean Christophersoned193212019-04-02 08:03:09 -07002433 cr4 = GET_SMSTATE(u32, smstate, 0x7f14);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002434
Sean Christophersoned193212019-04-02 08:03:09 -07002435 ctxt->ops->set_smbase(ctxt, GET_SMSTATE(u32, smstate, 0x7ef8));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002436
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002437 return rsm_enter_protected_mode(ctxt, cr0, cr3, cr4);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002438}
2439
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002440#ifdef CONFIG_X86_64
Sean Christophersoned193212019-04-02 08:03:09 -07002441static int rsm_load_state_64(struct x86_emulate_ctxt *ctxt,
2442 const char *smstate)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002443{
2444 struct desc_struct desc;
2445 struct desc_ptr dt;
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002446 u64 val, cr0, cr3, cr4;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002447 u32 base3;
2448 u16 selector;
Paolo Bonzinib10d92a2015-10-14 15:25:52 +02002449 int i, r;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002450
2451 for (i = 0; i < 16; i++)
Sean Christophersoned193212019-04-02 08:03:09 -07002452 *reg_write(ctxt, i) = GET_SMSTATE(u64, smstate, 0x7ff8 - i * 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002453
Sean Christophersoned193212019-04-02 08:03:09 -07002454 ctxt->_eip = GET_SMSTATE(u64, smstate, 0x7f78);
2455 ctxt->eflags = GET_SMSTATE(u32, smstate, 0x7f70) | X86_EFLAGS_FIXED;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002456
Sean Christopherson26443122021-02-04 17:24:58 -08002457 val = GET_SMSTATE(u64, smstate, 0x7f68);
Maxim Levitsky37f66bb2020-08-27 20:11:44 +03002458
Sean Christopherson16d51632021-02-04 17:24:57 -08002459 if (ctxt->ops->set_dr(ctxt, 6, val))
Maxim Levitsky37f66bb2020-08-27 20:11:44 +03002460 return X86EMUL_UNHANDLEABLE;
2461
Sean Christopherson26443122021-02-04 17:24:58 -08002462 val = GET_SMSTATE(u64, smstate, 0x7f60);
Maxim Levitsky37f66bb2020-08-27 20:11:44 +03002463
Sean Christopherson16d51632021-02-04 17:24:57 -08002464 if (ctxt->ops->set_dr(ctxt, 7, val))
Maxim Levitsky37f66bb2020-08-27 20:11:44 +03002465 return X86EMUL_UNHANDLEABLE;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002466
Sean Christophersoned193212019-04-02 08:03:09 -07002467 cr0 = GET_SMSTATE(u64, smstate, 0x7f58);
2468 cr3 = GET_SMSTATE(u64, smstate, 0x7f50);
2469 cr4 = GET_SMSTATE(u64, smstate, 0x7f48);
2470 ctxt->ops->set_smbase(ctxt, GET_SMSTATE(u32, smstate, 0x7f00));
2471 val = GET_SMSTATE(u64, smstate, 0x7ed0);
Maxim Levitsky37f66bb2020-08-27 20:11:44 +03002472
2473 if (ctxt->ops->set_msr(ctxt, MSR_EFER, val & ~EFER_LMA))
2474 return X86EMUL_UNHANDLEABLE;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002475
Sean Christophersoned193212019-04-02 08:03:09 -07002476 selector = GET_SMSTATE(u32, smstate, 0x7e90);
2477 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smstate, 0x7e92) << 8);
2478 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, 0x7e94));
2479 set_desc_base(&desc, GET_SMSTATE(u32, smstate, 0x7e98));
2480 base3 = GET_SMSTATE(u32, smstate, 0x7e9c);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002481 ctxt->ops->set_segment(ctxt, selector, &desc, base3, VCPU_SREG_TR);
2482
Sean Christophersoned193212019-04-02 08:03:09 -07002483 dt.size = GET_SMSTATE(u32, smstate, 0x7e84);
2484 dt.address = GET_SMSTATE(u64, smstate, 0x7e88);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002485 ctxt->ops->set_idt(ctxt, &dt);
2486
Sean Christophersoned193212019-04-02 08:03:09 -07002487 selector = GET_SMSTATE(u32, smstate, 0x7e70);
2488 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smstate, 0x7e72) << 8);
2489 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, 0x7e74));
2490 set_desc_base(&desc, GET_SMSTATE(u32, smstate, 0x7e78));
2491 base3 = GET_SMSTATE(u32, smstate, 0x7e7c);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002492 ctxt->ops->set_segment(ctxt, selector, &desc, base3, VCPU_SREG_LDTR);
2493
Sean Christophersoned193212019-04-02 08:03:09 -07002494 dt.size = GET_SMSTATE(u32, smstate, 0x7e64);
2495 dt.address = GET_SMSTATE(u64, smstate, 0x7e68);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002496 ctxt->ops->set_gdt(ctxt, &dt);
2497
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002498 r = rsm_enter_protected_mode(ctxt, cr0, cr3, cr4);
Paolo Bonzinib10d92a2015-10-14 15:25:52 +02002499 if (r != X86EMUL_CONTINUE)
2500 return r;
2501
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002502 for (i = 0; i < 6; i++) {
Sean Christophersoned193212019-04-02 08:03:09 -07002503 r = rsm_load_seg_64(ctxt, smstate, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002504 if (r != X86EMUL_CONTINUE)
2505 return r;
2506 }
2507
Paolo Bonzinib10d92a2015-10-14 15:25:52 +02002508 return X86EMUL_CONTINUE;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002509}
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002510#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002511
Paolo Bonzini64d60672015-05-07 11:36:11 +02002512static int em_rsm(struct x86_emulate_ctxt *ctxt)
2513{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002514 unsigned long cr0, cr4, efer;
Sean Christophersoned193212019-04-02 08:03:09 -07002515 char buf[512];
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002516 u64 smbase;
2517 int ret;
2518
Ladi Prosek6ed071f2017-04-25 16:42:44 +02002519 if ((ctxt->ops->get_hflags(ctxt) & X86EMUL_SMM_MASK) == 0)
Paolo Bonzini64d60672015-05-07 11:36:11 +02002520 return emulate_ud(ctxt);
2521
Sean Christophersoned193212019-04-02 08:03:09 -07002522 smbase = ctxt->ops->get_smbase(ctxt);
2523
2524 ret = ctxt->ops->read_phys(ctxt, smbase + 0xfe00, buf, sizeof(buf));
2525 if (ret != X86EMUL_CONTINUE)
2526 return X86EMUL_UNHANDLEABLE;
2527
Sean Christopherson9ec19492019-04-02 08:03:11 -07002528 if ((ctxt->ops->get_hflags(ctxt) & X86EMUL_SMM_INSIDE_NMI_MASK) == 0)
2529 ctxt->ops->set_nmi_mask(ctxt, false);
2530
Sean Christophersonedce4652021-06-09 11:56:13 -07002531 ctxt->ops->exiting_smm(ctxt);
Sean Christopherson9ec19492019-04-02 08:03:11 -07002532
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002533 /*
2534 * Get back to real mode, to prepare a safe state in which to load
Paolo Bonzini89651a32015-11-03 13:43:05 +01002535 * CR0/CR3/CR4/EFER. It's all a bit more complicated if the vCPU
2536 * supports long mode.
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002537 */
Paolo Bonzini89651a32015-11-03 13:43:05 +01002538 if (emulator_has_longmode(ctxt)) {
2539 struct desc_struct cs_desc;
2540
2541 /* Zero CR4.PCIDE before CR0.PG. */
Sean Christopherson8f4dc2e2019-04-02 08:10:47 -07002542 cr4 = ctxt->ops->get_cr(ctxt, 4);
2543 if (cr4 & X86_CR4_PCIDE)
Paolo Bonzini89651a32015-11-03 13:43:05 +01002544 ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PCIDE);
Paolo Bonzini89651a32015-11-03 13:43:05 +01002545
2546 /* A 32-bit code segment is required to clear EFER.LMA. */
2547 memset(&cs_desc, 0, sizeof(cs_desc));
2548 cs_desc.type = 0xb;
2549 cs_desc.s = cs_desc.g = cs_desc.p = 1;
2550 ctxt->ops->set_segment(ctxt, 0, &cs_desc, 0, VCPU_SREG_CS);
2551 }
2552
2553 /* For the 64-bit case, this will clear EFER.LMA. */
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002554 cr0 = ctxt->ops->get_cr(ctxt, 0);
2555 if (cr0 & X86_CR0_PE)
2556 ctxt->ops->set_cr(ctxt, 0, cr0 & ~(X86_CR0_PG | X86_CR0_PE));
Paolo Bonzini89651a32015-11-03 13:43:05 +01002557
Sean Christopherson8f4dc2e2019-04-02 08:10:47 -07002558 if (emulator_has_longmode(ctxt)) {
2559 /* Clear CR4.PAE before clearing EFER.LME. */
2560 cr4 = ctxt->ops->get_cr(ctxt, 4);
2561 if (cr4 & X86_CR4_PAE)
2562 ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PAE);
Paolo Bonzini89651a32015-11-03 13:43:05 +01002563
Sean Christopherson8f4dc2e2019-04-02 08:10:47 -07002564 /* And finally go back to 32-bit mode. */
2565 efer = 0;
2566 ctxt->ops->set_msr(ctxt, MSR_EFER, efer);
2567 }
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002568
Ladi Prosek0234bf82017-10-11 16:54:40 +02002569 /*
Sean Christophersonecc513e2021-06-09 11:56:19 -07002570 * Give leave_smm() a chance to make ISA-specific changes to the vCPU
2571 * state (e.g. enter guest mode) before loading state from the SMM
Ladi Prosek0234bf82017-10-11 16:54:40 +02002572 * state-save area.
2573 */
Sean Christophersonecc513e2021-06-09 11:56:19 -07002574 if (ctxt->ops->leave_smm(ctxt, buf))
Sean Christopherson25b17222021-06-09 11:56:12 -07002575 goto emulate_shutdown;
Ladi Prosek0234bf82017-10-11 16:54:40 +02002576
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002577#ifdef CONFIG_X86_64
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002578 if (emulator_has_longmode(ctxt))
Sean Christophersoned193212019-04-02 08:03:09 -07002579 ret = rsm_load_state_64(ctxt, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002580 else
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002581#endif
Sean Christophersoned193212019-04-02 08:03:09 -07002582 ret = rsm_load_state_32(ctxt, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002583
Sean Christopherson25b17222021-06-09 11:56:12 -07002584 if (ret != X86EMUL_CONTINUE)
2585 goto emulate_shutdown;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002586
Sean Christopherson01281162021-06-09 11:56:18 -07002587 /*
2588 * Note, the ctxt->ops callbacks are responsible for handling side
2589 * effects when writing MSRs and CRs, e.g. MMU context resets, CPUID
2590 * runtime updates, etc... If that changes, e.g. this flow is moved
2591 * out of the emulator to make it look more like enter_smm(), then
2592 * those side effects need to be explicitly handled for both success
2593 * and shutdown.
2594 */
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002595 return X86EMUL_CONTINUE;
Sean Christopherson25b17222021-06-09 11:56:12 -07002596
2597emulate_shutdown:
2598 ctxt->ops->triple_fault(ctxt);
2599 return X86EMUL_CONTINUE;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002600}
2601
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002602static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002603setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002604 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002605{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002606 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002607 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002608 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002609 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002610 cs->type = 0x0b; /* Read, Execute, Accessed */
2611 cs->s = 1;
2612 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002613 cs->p = 1;
2614 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002615 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002616
Gleb Natapov79168fd2010-04-28 19:15:30 +03002617 set_desc_base(ss, 0); /* flat segment */
2618 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002619 ss->g = 1; /* 4kb granularity */
2620 ss->s = 1;
2621 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002622 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002623 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002624 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002625 ss->l = 0;
2626 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002627}
2628
Avi Kivity1a18a692012-02-01 12:23:21 +02002629static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2630{
2631 u32 eax, ebx, ecx, edx;
2632
2633 eax = ecx = 0;
Sean Christophersonf91af512020-03-04 17:34:37 -08002634 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, true);
Sean Christopherson15608ed2020-03-04 17:34:32 -08002635 return is_guest_vendor_intel(ebx, ecx, edx);
Avi Kivity1a18a692012-02-01 12:23:21 +02002636}
2637
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002638static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2639{
Mathias Krause0225fb52012-08-30 01:30:16 +02002640 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002641 u32 eax, ebx, ecx, edx;
2642
2643 /*
2644 * syscall should always be enabled in longmode - so only become
2645 * vendor specific (cpuid) if other modes are active...
2646 */
2647 if (ctxt->mode == X86EMUL_MODE_PROT64)
2648 return true;
2649
2650 eax = 0x00000000;
2651 ecx = 0x00000000;
Sean Christophersonf91af512020-03-04 17:34:37 -08002652 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, true);
Avi Kivity0017f932012-06-07 14:10:16 +03002653 /*
Sean Christopherson15608ed2020-03-04 17:34:32 -08002654 * remark: Intel CPUs only support "syscall" in 64bit longmode. Also a
2655 * 64bit guest with a 32bit compat-app running will #UD !! While this
2656 * behaviour can be fixed (by emulating) into AMD response - CPUs of
2657 * AMD can't behave like Intel.
Avi Kivity0017f932012-06-07 14:10:16 +03002658 */
Sean Christopherson15608ed2020-03-04 17:34:32 -08002659 if (is_guest_vendor_intel(ebx, ecx, edx))
Avi Kivity0017f932012-06-07 14:10:16 +03002660 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002661
Sean Christopherson15608ed2020-03-04 17:34:32 -08002662 if (is_guest_vendor_amd(ebx, ecx, edx) ||
2663 is_guest_vendor_hygon(ebx, ecx, edx))
Pu Wenb8f4abb2018-09-23 17:36:31 +08002664 return true;
2665
2666 /*
2667 * default: (not Intel, not AMD, not Hygon), apply Intel's
2668 * stricter rules...
2669 */
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002670 return false;
2671}
2672
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002673static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002674{
Mathias Krause0225fb52012-08-30 01:30:16 +02002675 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002676 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002677 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002678 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002679 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002680
2681 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002682 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002683 ctxt->mode == X86EMUL_MODE_VM86)
2684 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002685
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002686 if (!(em_syscall_is_enabled(ctxt)))
2687 return emulate_ud(ctxt);
2688
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002689 ops->get_msr(ctxt, MSR_EFER, &efer);
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002690 if (!(efer & EFER_SCE))
2691 return emulate_ud(ctxt);
2692
Miaohe Lin5b4ce932019-11-09 16:58:54 +08002693 setup_syscalls_segments(ctxt, &cs, &ss);
Avi Kivity717746e2011-04-20 13:37:53 +03002694 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002695 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002696 cs_sel = (u16)(msr_data & 0xfffc);
2697 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002698
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002699 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002700 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002701 cs.l = 1;
2702 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002703 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2704 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002705
Avi Kivitydd856ef2012-08-27 23:46:17 +03002706 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002707 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002708#ifdef CONFIG_X86_64
Nadav Amit6c6cb692014-07-21 14:37:30 +03002709 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002710
Avi Kivity717746e2011-04-20 13:37:53 +03002711 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002712 ctxt->mode == X86EMUL_MODE_PROT64 ?
2713 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002714 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002715
Avi Kivity717746e2011-04-20 13:37:53 +03002716 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Nadav Amit6c6cb692014-07-21 14:37:30 +03002717 ctxt->eflags &= ~msr_data;
Wanpeng Li35fd68a2015-04-08 14:08:14 +08002718 ctxt->eflags |= X86_EFLAGS_FIXED;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002719#endif
2720 } else {
2721 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002722 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002723 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002724
Nadav Amit0efb0442015-03-29 16:33:03 +03002725 ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002726 }
2727
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02002728 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002729 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002730}
2731
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002732static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002733{
Mathias Krause0225fb52012-08-30 01:30:16 +02002734 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002735 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002736 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002737 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002738 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002739
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002740 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002741 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002742 if (ctxt->mode == X86EMUL_MODE_REAL)
2743 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002744
Avi Kivity1a18a692012-02-01 12:23:21 +02002745 /*
2746 * Not recognized on AMD in compat mode (but is recognized in legacy
2747 * mode).
2748 */
Nadav Amitf3747372015-01-01 23:11:11 +02002749 if ((ctxt->mode != X86EMUL_MODE_PROT64) && (efer & EFER_LMA)
Avi Kivity1a18a692012-02-01 12:23:21 +02002750 && !vendor_intel(ctxt))
2751 return emulate_ud(ctxt);
2752
Nadav Amitb2c9d432014-11-02 11:55:01 +02002753 /* sysenter/sysexit have not been tested in 64bit mode. */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002754 if (ctxt->mode == X86EMUL_MODE_PROT64)
Nadav Amitb2c9d432014-11-02 11:55:01 +02002755 return X86EMUL_UNHANDLEABLE;
Andre Przywara8c604352009-06-18 12:56:01 +02002756
Avi Kivity717746e2011-04-20 13:37:53 +03002757 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002758 if ((msr_data & 0xfffc) == 0x0)
2759 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002760
Miaohe Lin5b4ce932019-11-09 16:58:54 +08002761 setup_syscalls_segments(ctxt, &cs, &ss);
Nadav Amit0efb0442015-03-29 16:33:03 +03002762 ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
Nadav Amitb32a9912015-03-29 16:33:04 +03002763 cs_sel = (u16)msr_data & ~SEGMENT_RPL_MASK;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002764 ss_sel = cs_sel + 8;
Nadav Amitf3747372015-01-01 23:11:11 +02002765 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002766 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002767 cs.l = 1;
2768 }
2769
Avi Kivity1aa36612011-04-27 13:20:30 +03002770 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2771 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002772
Avi Kivity717746e2011-04-20 13:37:53 +03002773 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002774 ctxt->_eip = (efer & EFER_LMA) ? msr_data : (u32)msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002775
Avi Kivity717746e2011-04-20 13:37:53 +03002776 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002777 *reg_write(ctxt, VCPU_REGS_RSP) = (efer & EFER_LMA) ? msr_data :
2778 (u32)msr_data;
Sean Christopherson943dea82021-02-02 08:55:46 -08002779 if (efer & EFER_LMA)
2780 ctxt->mode = X86EMUL_MODE_PROT64;
Andre Przywara8c604352009-06-18 12:56:01 +02002781
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002782 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002783}
2784
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002785static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002786{
Mathias Krause0225fb52012-08-30 01:30:16 +02002787 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002788 struct desc_struct cs, ss;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002789 u64 msr_data, rcx, rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002790 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002791 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002792
Gleb Natapova0044752010-02-10 14:21:31 +02002793 /* inject #GP if in real mode or Virtual 8086 mode */
2794 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002795 ctxt->mode == X86EMUL_MODE_VM86)
2796 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002797
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002798 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002799
Avi Kivity9dac77f2011-06-01 15:34:25 +03002800 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002801 usermode = X86EMUL_MODE_PROT64;
2802 else
2803 usermode = X86EMUL_MODE_PROT32;
2804
Nadav Amit234f3ce2014-09-18 22:39:38 +03002805 rcx = reg_read(ctxt, VCPU_REGS_RCX);
2806 rdx = reg_read(ctxt, VCPU_REGS_RDX);
2807
Andre Przywara4668f052009-06-18 12:56:02 +02002808 cs.dpl = 3;
2809 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002810 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002811 switch (usermode) {
2812 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002813 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002814 if ((msr_data & 0xfffc) == 0x0)
2815 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002816 ss_sel = (u16)(msr_data + 24);
Nadav Amitbf0b6822014-09-18 22:39:45 +03002817 rcx = (u32)rcx;
2818 rdx = (u32)rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002819 break;
2820 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002821 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002822 if (msr_data == 0x0)
2823 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002824 ss_sel = cs_sel + 8;
2825 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002826 cs.l = 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08002827 if (emul_is_noncanonical_address(rcx, ctxt) ||
2828 emul_is_noncanonical_address(rdx, ctxt))
Nadav Amit234f3ce2014-09-18 22:39:38 +03002829 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002830 break;
2831 }
Nadav Amitb32a9912015-03-29 16:33:04 +03002832 cs_sel |= SEGMENT_RPL_MASK;
2833 ss_sel |= SEGMENT_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002834
Avi Kivity1aa36612011-04-27 13:20:30 +03002835 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2836 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002837
Nadav Amit234f3ce2014-09-18 22:39:38 +03002838 ctxt->_eip = rdx;
2839 *reg_write(ctxt, VCPU_REGS_RSP) = rcx;
Andre Przywara4668f052009-06-18 12:56:02 +02002840
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002841 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002842}
2843
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002844static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002845{
2846 int iopl;
2847 if (ctxt->mode == X86EMUL_MODE_REAL)
2848 return false;
2849 if (ctxt->mode == X86EMUL_MODE_VM86)
2850 return true;
Nadav Amit0efb0442015-03-29 16:33:03 +03002851 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002852 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002853}
2854
Liran Alon9a29d4492018-03-12 13:12:48 +02002855#define VMWARE_PORT_VMPORT (0x5658)
2856#define VMWARE_PORT_VMRPC (0x5659)
2857
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002858static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002859 u16 port, u16 len)
2860{
Mathias Krause0225fb52012-08-30 01:30:16 +02002861 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002862 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002863 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002864 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002865 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002866 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002867 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002868
Liran Alon9a29d4492018-03-12 13:12:48 +02002869 /*
2870 * VMware allows access to these ports even if denied
2871 * by TSS I/O permission bitmap. Mimic behavior.
2872 */
2873 if (enable_vmware_backdoor &&
2874 ((port == VMWARE_PORT_VMPORT) || (port == VMWARE_PORT_VMRPC)))
2875 return true;
2876
Avi Kivity1aa36612011-04-27 13:20:30 +03002877 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002878 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002879 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002880 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002881 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002882 base = get_desc_base(&tr_seg);
2883#ifdef CONFIG_X86_64
2884 base |= ((u64)base3) << 32;
2885#endif
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02002886 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL, true);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002887 if (r != X86EMUL_CONTINUE)
2888 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002889 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002890 return false;
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02002891 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL, true);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002892 if (r != X86EMUL_CONTINUE)
2893 return false;
2894 if ((perm >> bit_idx) & mask)
2895 return false;
2896 return true;
2897}
2898
2899static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002900 u16 port, u16 len)
2901{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002902 if (ctxt->perm_ok)
2903 return true;
2904
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002905 if (emulator_bad_iopl(ctxt))
2906 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002907 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002908
2909 ctxt->perm_ok = true;
2910
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002911 return true;
2912}
2913
Nadav Amit428e3d02015-04-28 13:06:01 +03002914static void string_registers_quirk(struct x86_emulate_ctxt *ctxt)
2915{
2916 /*
2917 * Intel CPUs mask the counter and pointers in quite strange
2918 * manner when ECX is zero due to REP-string optimizations.
2919 */
2920#ifdef CONFIG_X86_64
2921 if (ctxt->ad_bytes != 4 || !vendor_intel(ctxt))
2922 return;
2923
2924 *reg_write(ctxt, VCPU_REGS_RCX) = 0;
2925
2926 switch (ctxt->b) {
2927 case 0xa4: /* movsb */
2928 case 0xa5: /* movsd/w */
2929 *reg_rmw(ctxt, VCPU_REGS_RSI) &= (u32)-1;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002930 fallthrough;
Nadav Amit428e3d02015-04-28 13:06:01 +03002931 case 0xaa: /* stosb */
2932 case 0xab: /* stosd/w */
2933 *reg_rmw(ctxt, VCPU_REGS_RDI) &= (u32)-1;
2934 }
2935#endif
2936}
2937
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002938static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002939 struct tss_segment_16 *tss)
2940{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002941 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002942 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002943 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2944 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2945 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2946 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2947 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2948 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2949 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2950 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002951
Avi Kivity1aa36612011-04-27 13:20:30 +03002952 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2953 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2954 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2955 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2956 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002957}
2958
2959static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002960 struct tss_segment_16 *tss)
2961{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002962 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002963 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002964
Avi Kivity9dac77f2011-06-01 15:34:25 +03002965 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002966 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002967 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2968 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2969 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2970 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2971 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2972 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2973 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2974 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002975
2976 /*
2977 * SDM says that segment selectors are loaded before segment
2978 * descriptors
2979 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002980 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2981 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2982 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2983 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2984 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002985
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002986 cpl = tss->cs & 3;
2987
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002988 /*
Guo Chaofc058682012-06-28 15:19:51 +08002989 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002990 * it is handled in a context of new task
2991 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002992 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002993 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002994 if (ret != X86EMUL_CONTINUE)
2995 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002996 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002997 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002998 if (ret != X86EMUL_CONTINUE)
2999 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003000 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003001 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003002 if (ret != X86EMUL_CONTINUE)
3003 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003004 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003005 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003006 if (ret != X86EMUL_CONTINUE)
3007 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003008 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003009 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003010 if (ret != X86EMUL_CONTINUE)
3011 return ret;
3012
3013 return X86EMUL_CONTINUE;
3014}
3015
3016static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003017 u16 tss_selector, u16 old_tss_sel,
3018 ulong old_tss_base, struct desc_struct *new_desc)
3019{
3020 struct tss_segment_16 tss_seg;
3021 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003022 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003023
Jordan Borgner0e96f312018-10-28 12:58:28 +00003024 ret = linear_read_system(ctxt, old_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003025 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003026 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003027
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003028 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003029
Jordan Borgner0e96f312018-10-28 12:58:28 +00003030 ret = linear_write_system(ctxt, old_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003031 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003032 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003033
Jordan Borgner0e96f312018-10-28 12:58:28 +00003034 ret = linear_read_system(ctxt, new_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003035 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003036 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003037
3038 if (old_tss_sel != 0xffff) {
3039 tss_seg.prev_task_link = old_tss_sel;
3040
Paolo Bonzini79367a62018-06-06 16:43:02 +02003041 ret = linear_write_system(ctxt, new_tss_base,
3042 &tss_seg.prev_task_link,
Jordan Borgner0e96f312018-10-28 12:58:28 +00003043 sizeof(tss_seg.prev_task_link));
Avi Kivitydb297e32010-11-22 17:53:24 +02003044 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003045 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003046 }
3047
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003048 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003049}
3050
3051static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003052 struct tss_segment_32 *tss)
3053{
Nadav Amit5c7411e2014-04-07 18:37:47 +03003054 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003055 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003056 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003057 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
3058 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
3059 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
3060 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
3061 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
3062 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
3063 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
3064 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003065
Avi Kivity1aa36612011-04-27 13:20:30 +03003066 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
3067 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
3068 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
3069 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
3070 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
3071 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003072}
3073
3074static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003075 struct tss_segment_32 *tss)
3076{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003077 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003078 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003079
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003080 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003081 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003082 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003083 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01003084
3085 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03003086 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
3087 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
3088 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
3089 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
3090 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
3091 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
3092 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
3093 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003094
3095 /*
3096 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003097 * descriptors. This is important because CPL checks will
3098 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003099 */
Avi Kivity1aa36612011-04-27 13:20:30 +03003100 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
3101 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
3102 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
3103 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
3104 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
3105 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
3106 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003107
3108 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01003109 * If we're switching between Protected Mode and VM86, we need to make
3110 * sure to update the mode before loading the segment descriptors so
3111 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01003112 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003113 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01003114 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003115 cpl = 3;
3116 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01003117 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003118 cpl = tss->cs & 3;
3119 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01003120
3121 /*
Ingo Molnard9f6e122021-03-18 15:28:01 +01003122 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003123 * it is handled in a context of new task
3124 */
Nadav Amitd1442d82014-09-18 22:39:39 +03003125 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003126 cpl, X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003127 if (ret != X86EMUL_CONTINUE)
3128 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003129 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003130 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003131 if (ret != X86EMUL_CONTINUE)
3132 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003133 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003134 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003135 if (ret != X86EMUL_CONTINUE)
3136 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003137 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003138 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003139 if (ret != X86EMUL_CONTINUE)
3140 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003141 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003142 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003143 if (ret != X86EMUL_CONTINUE)
3144 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003145 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003146 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003147 if (ret != X86EMUL_CONTINUE)
3148 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003149 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003150 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003151
Eugene Korenevsky2f729b12015-03-29 01:27:17 +03003152 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003153}
3154
3155static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003156 u16 tss_selector, u16 old_tss_sel,
3157 ulong old_tss_base, struct desc_struct *new_desc)
3158{
3159 struct tss_segment_32 tss_seg;
3160 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003161 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03003162 u32 eip_offset = offsetof(struct tss_segment_32, eip);
3163 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003164
Jordan Borgner0e96f312018-10-28 12:58:28 +00003165 ret = linear_read_system(ctxt, old_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003166 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003167 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003168
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003169 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003170
Nadav Amit5c7411e2014-04-07 18:37:47 +03003171 /* Only GP registers and segment selectors are saved */
Paolo Bonzini79367a62018-06-06 16:43:02 +02003172 ret = linear_write_system(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
3173 ldt_sel_offset - eip_offset);
Avi Kivitydb297e32010-11-22 17:53:24 +02003174 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003175 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003176
Jordan Borgner0e96f312018-10-28 12:58:28 +00003177 ret = linear_read_system(ctxt, new_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003178 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003179 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003180
3181 if (old_tss_sel != 0xffff) {
3182 tss_seg.prev_task_link = old_tss_sel;
3183
Paolo Bonzini79367a62018-06-06 16:43:02 +02003184 ret = linear_write_system(ctxt, new_tss_base,
3185 &tss_seg.prev_task_link,
Jordan Borgner0e96f312018-10-28 12:58:28 +00003186 sizeof(tss_seg.prev_task_link));
Avi Kivitydb297e32010-11-22 17:53:24 +02003187 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003188 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003189 }
3190
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003191 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003192}
3193
3194static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003195 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003196 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003197{
Mathias Krause0225fb52012-08-30 01:30:16 +02003198 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003199 struct desc_struct curr_tss_desc, next_tss_desc;
3200 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03003201 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003202 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03003203 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02003204 u32 desc_limit;
Nadav Amit3db176d2015-04-19 21:12:59 +03003205 ulong desc_addr, dr7;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003206
3207 /* FIXME: old_tss_base == ~0 ? */
3208
Avi Kivitye9194642012-06-13 16:29:39 +03003209 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003210 if (ret != X86EMUL_CONTINUE)
3211 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03003212 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003213 if (ret != X86EMUL_CONTINUE)
3214 return ret;
3215
3216 /* FIXME: check that next_tss_desc is tss */
3217
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003218 /*
3219 * Check privileges. The three cases are task switch caused by...
3220 *
3221 * 1. jmp/call/int to task gate: Check against DPL of the task gate
3222 * 2. Exception/IRQ/iret: No check is performed
Nadav Amit2c2ca2d2014-11-02 11:54:57 +02003223 * 3. jmp/call to TSS/task-gate: No check is performed since the
3224 * hardware checks it before exiting.
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003225 */
3226 if (reason == TASK_SWITCH_GATE) {
3227 if (idt_index != -1) {
3228 /* Software interrupts */
3229 struct desc_struct task_gate_desc;
3230 int dpl;
3231
3232 ret = read_interrupt_descriptor(ctxt, idt_index,
3233 &task_gate_desc);
3234 if (ret != X86EMUL_CONTINUE)
3235 return ret;
3236
3237 dpl = task_gate_desc.dpl;
3238 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
3239 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
3240 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003241 }
3242
Gleb Natapovceffb452010-03-18 15:20:19 +02003243 desc_limit = desc_limit_scaled(&next_tss_desc);
3244 if (!next_tss_desc.p ||
3245 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
3246 desc_limit < 0x2b)) {
Paolo Bonzini592f0852014-08-20 10:05:08 +02003247 return emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003248 }
3249
3250 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
3251 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003252 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003253 }
3254
3255 if (reason == TASK_SWITCH_IRET)
3256 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
3257
3258 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08003259 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003260 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
3261 old_tss_sel = 0xffff;
3262
3263 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003264 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003265 old_tss_base, &next_tss_desc);
3266 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003267 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003268 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02003269 if (ret != X86EMUL_CONTINUE)
3270 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003271
3272 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
3273 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
3274
3275 if (reason != TASK_SWITCH_IRET) {
3276 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003277 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003278 }
3279
Avi Kivity717746e2011-04-20 13:37:53 +03003280 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03003281 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003282
Jan Kiszkae269fb22010-04-14 15:51:09 +02003283 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003284 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
3285 ctxt->lock_prefix = 0;
3286 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003287 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02003288 }
3289
Nadav Amit3db176d2015-04-19 21:12:59 +03003290 ops->get_dr(ctxt, 7, &dr7);
3291 ops->set_dr(ctxt, 7, dr7 & ~(DR_LOCAL_ENABLE_MASK | DR_LOCAL_SLOWDOWN));
3292
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003293 return ret;
3294}
3295
3296int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003297 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003298 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003299{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003300 int rc;
3301
Avi Kivitydd856ef2012-08-27 23:46:17 +03003302 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003303 ctxt->_eip = ctxt->eip;
3304 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003305
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003306 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003307 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003308
Avi Kivitydd856ef2012-08-27 23:46:17 +03003309 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003310 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003311 writeback_registers(ctxt);
3312 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003313
Gleb Natapova0c0ab22011-03-28 16:57:49 +02003314 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003315}
3316
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03003317static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
3318 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02003319{
Nadav Amit0efb0442015-03-29 16:33:03 +03003320 int df = (ctxt->eflags & X86_EFLAGS_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02003321
Paolo Bonzini01485a22014-11-19 18:25:08 +01003322 register_address_increment(ctxt, reg, df * op->bytes);
3323 op->addr.mem.ea = register_address(ctxt, reg);
Gleb Natapova682e352010-03-18 15:20:21 +02003324}
3325
Avi Kivity7af04fc2010-08-18 14:16:35 +03003326static int em_das(struct x86_emulate_ctxt *ctxt)
3327{
Avi Kivity7af04fc2010-08-18 14:16:35 +03003328 u8 al, old_al;
3329 bool af, cf, old_cf;
3330
3331 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003332 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03003333
3334 old_al = al;
3335 old_cf = cf;
3336 cf = false;
3337 af = ctxt->eflags & X86_EFLAGS_AF;
3338 if ((al & 0x0f) > 9 || af) {
3339 al -= 6;
3340 cf = old_cf | (al >= 250);
3341 af = true;
3342 } else {
3343 af = false;
3344 }
3345 if (old_al > 0x99 || old_cf) {
3346 al -= 0x60;
3347 cf = true;
3348 }
3349
Avi Kivity9dac77f2011-06-01 15:34:25 +03003350 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03003351 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003352 ctxt->src.type = OP_IMM;
3353 ctxt->src.val = 0;
3354 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02003355 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03003356 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
3357 if (cf)
3358 ctxt->eflags |= X86_EFLAGS_CF;
3359 if (af)
3360 ctxt->eflags |= X86_EFLAGS_AF;
3361 return X86EMUL_CONTINUE;
3362}
3363
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003364static int em_aam(struct x86_emulate_ctxt *ctxt)
3365{
3366 u8 al, ah;
3367
3368 if (ctxt->src.val == 0)
3369 return emulate_de(ctxt);
3370
3371 al = ctxt->dst.val & 0xff;
3372 ah = al / ctxt->src.val;
3373 al %= ctxt->src.val;
3374
3375 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
3376
3377 /* Set PF, ZF, SF */
3378 ctxt->src.type = OP_IMM;
3379 ctxt->src.val = 0;
3380 ctxt->src.bytes = 1;
3381 fastop(ctxt, em_or);
3382
3383 return X86EMUL_CONTINUE;
3384}
3385
Gleb Natapov7f662272012-12-10 11:42:30 +02003386static int em_aad(struct x86_emulate_ctxt *ctxt)
3387{
3388 u8 al = ctxt->dst.val & 0xff;
3389 u8 ah = (ctxt->dst.val >> 8) & 0xff;
3390
3391 al = (al + (ah * ctxt->src.val)) & 0xff;
3392
3393 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
3394
Gleb Natapovf583c292013-02-13 17:50:39 +02003395 /* Set PF, ZF, SF */
3396 ctxt->src.type = OP_IMM;
3397 ctxt->src.val = 0;
3398 ctxt->src.bytes = 1;
3399 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02003400
3401 return X86EMUL_CONTINUE;
3402}
3403
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003404static int em_call(struct x86_emulate_ctxt *ctxt)
3405{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003406 int rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003407 long rel = ctxt->src.val;
3408
3409 ctxt->src.val = (unsigned long)ctxt->_eip;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003410 rc = jmp_rel(ctxt, rel);
3411 if (rc != X86EMUL_CONTINUE)
3412 return rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003413 return em_push(ctxt);
3414}
3415
Avi Kivity0ef753b2010-08-18 14:51:45 +03003416static int em_call_far(struct x86_emulate_ctxt *ctxt)
3417{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003418 u16 sel, old_cs;
3419 ulong old_eip;
3420 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03003421 struct desc_struct old_desc, new_desc;
3422 const struct x86_emulate_ops *ops = ctxt->ops;
3423 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amit82268082015-01-26 09:32:27 +02003424 enum x86emul_mode prev_mode = ctxt->mode;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003425
Avi Kivity9dac77f2011-06-01 15:34:25 +03003426 old_eip = ctxt->_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003427 ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003428
Avi Kivity9dac77f2011-06-01 15:34:25 +03003429 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003430 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
3431 X86_TRANSFER_CALL_JMP, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003432 if (rc != X86EMUL_CONTINUE)
Nadav Amit80976db2014-12-25 02:52:20 +02003433 return rc;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003434
Nadav Amitd50eaa12014-11-19 17:43:11 +02003435 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003436 if (rc != X86EMUL_CONTINUE)
3437 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003438
Avi Kivity9dac77f2011-06-01 15:34:25 +03003439 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003440 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003441 if (rc != X86EMUL_CONTINUE)
Nadav Amitd1442d82014-09-18 22:39:39 +03003442 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003443
Avi Kivity9dac77f2011-06-01 15:34:25 +03003444 ctxt->src.val = old_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003445 rc = em_push(ctxt);
3446 /* If we failed, we tainted the memory, but the very least we should
3447 restore cs */
Nadav Amit82268082015-01-26 09:32:27 +02003448 if (rc != X86EMUL_CONTINUE) {
3449 pr_warn_once("faulting far call emulation tainted memory\n");
Nadav Amitd1442d82014-09-18 22:39:39 +03003450 goto fail;
Nadav Amit82268082015-01-26 09:32:27 +02003451 }
Nadav Amitd1442d82014-09-18 22:39:39 +03003452 return rc;
3453fail:
3454 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
Nadav Amit82268082015-01-26 09:32:27 +02003455 ctxt->mode = prev_mode;
Nadav Amitd1442d82014-09-18 22:39:39 +03003456 return rc;
3457
Avi Kivity0ef753b2010-08-18 14:51:45 +03003458}
3459
Avi Kivity40ece7c2010-08-18 15:12:09 +03003460static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3461{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003462 int rc;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003463 unsigned long eip;
Avi Kivity40ece7c2010-08-18 15:12:09 +03003464
Nadav Amit234f3ce2014-09-18 22:39:38 +03003465 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
3466 if (rc != X86EMUL_CONTINUE)
3467 return rc;
3468 rc = assign_eip_near(ctxt, eip);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003469 if (rc != X86EMUL_CONTINUE)
3470 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003471 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003472 return X86EMUL_CONTINUE;
3473}
3474
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003475static int em_xchg(struct x86_emulate_ctxt *ctxt)
3476{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003477 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003478 ctxt->src.val = ctxt->dst.val;
3479 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003480
3481 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003482 ctxt->dst.val = ctxt->src.orig_val;
3483 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003484 return X86EMUL_CONTINUE;
3485}
3486
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003487static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3488{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003489 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003490 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003491}
3492
Avi Kivity61429142010-08-19 15:13:00 +03003493static int em_cwd(struct x86_emulate_ctxt *ctxt)
3494{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003495 ctxt->dst.type = OP_REG;
3496 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003497 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003498 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003499
3500 return X86EMUL_CONTINUE;
3501}
3502
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02003503static int em_rdpid(struct x86_emulate_ctxt *ctxt)
3504{
3505 u64 tsc_aux = 0;
3506
3507 if (ctxt->ops->get_msr(ctxt, MSR_TSC_AUX, &tsc_aux))
Robert Hooa9e2e0a2020-08-28 10:23:42 +08003508 return emulate_ud(ctxt);
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02003509 ctxt->dst.val = tsc_aux;
3510 return X86EMUL_CONTINUE;
3511}
3512
Avi Kivity48bb5d32010-08-18 18:54:34 +03003513static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3514{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003515 u64 tsc = 0;
3516
Avi Kivity717746e2011-04-20 13:37:53 +03003517 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003518 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3519 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003520 return X86EMUL_CONTINUE;
3521}
3522
Avi Kivity222d21a2011-11-10 14:57:30 +02003523static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3524{
3525 u64 pmc;
3526
Avi Kivitydd856ef2012-08-27 23:46:17 +03003527 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003528 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003529 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3530 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003531 return X86EMUL_CONTINUE;
3532}
3533
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003534static int em_mov(struct x86_emulate_ctxt *ctxt)
3535{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01003536 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003537 return X86EMUL_CONTINUE;
3538}
3539
Borislav Petkov84cffe42013-10-29 12:54:56 +01003540static int em_movbe(struct x86_emulate_ctxt *ctxt)
3541{
Borislav Petkov84cffe42013-10-29 12:54:56 +01003542 u16 tmp;
3543
Sean Christopherson5ae78e92019-12-17 13:32:38 -08003544 if (!ctxt->ops->guest_has_movbe(ctxt))
Borislav Petkov84cffe42013-10-29 12:54:56 +01003545 return emulate_ud(ctxt);
3546
3547 switch (ctxt->op_bytes) {
3548 case 2:
3549 /*
3550 * From MOVBE definition: "...When the operand size is 16 bits,
3551 * the upper word of the destination register remains unchanged
3552 * ..."
3553 *
3554 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3555 * rules so we have to do the operation almost per hand.
3556 */
3557 tmp = (u16)ctxt->src.val;
3558 ctxt->dst.val &= ~0xffffUL;
3559 ctxt->dst.val |= (unsigned long)swab16(tmp);
3560 break;
3561 case 4:
3562 ctxt->dst.val = swab32((u32)ctxt->src.val);
3563 break;
3564 case 8:
3565 ctxt->dst.val = swab64(ctxt->src.val);
3566 break;
3567 default:
Paolo Bonzini592f0852014-08-20 10:05:08 +02003568 BUG();
Borislav Petkov84cffe42013-10-29 12:54:56 +01003569 }
3570 return X86EMUL_CONTINUE;
3571}
3572
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003573static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3574{
3575 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3576 return emulate_gp(ctxt, 0);
3577
3578 /* Disable writeback. */
3579 ctxt->dst.type = OP_NONE;
3580 return X86EMUL_CONTINUE;
3581}
3582
3583static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3584{
3585 unsigned long val;
3586
3587 if (ctxt->mode == X86EMUL_MODE_PROT64)
3588 val = ctxt->src.val & ~0ULL;
3589 else
3590 val = ctxt->src.val & ~0U;
3591
3592 /* #UD condition is already handled. */
3593 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3594 return emulate_gp(ctxt, 0);
3595
3596 /* Disable writeback. */
3597 ctxt->dst.type = OP_NONE;
3598 return X86EMUL_CONTINUE;
3599}
3600
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003601static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3602{
Alexander Graf1ae09952020-09-25 16:34:16 +02003603 u64 msr_index = reg_read(ctxt, VCPU_REGS_RCX);
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003604 u64 msr_data;
Alexander Graf1ae09952020-09-25 16:34:16 +02003605 int r;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003606
Avi Kivitydd856ef2012-08-27 23:46:17 +03003607 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3608 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
Alexander Graf1ae09952020-09-25 16:34:16 +02003609 r = ctxt->ops->set_msr(ctxt, msr_index, msr_data);
3610
3611 if (r == X86EMUL_IO_NEEDED)
3612 return r;
3613
Maxim Levitsky7dffeca2020-10-01 14:29:52 +03003614 if (r > 0)
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003615 return emulate_gp(ctxt, 0);
3616
Maxim Levitsky7dffeca2020-10-01 14:29:52 +03003617 return r < 0 ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003618}
3619
3620static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3621{
Alexander Graf1ae09952020-09-25 16:34:16 +02003622 u64 msr_index = reg_read(ctxt, VCPU_REGS_RCX);
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003623 u64 msr_data;
Alexander Graf1ae09952020-09-25 16:34:16 +02003624 int r;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003625
Alexander Graf1ae09952020-09-25 16:34:16 +02003626 r = ctxt->ops->get_msr(ctxt, msr_index, &msr_data);
3627
3628 if (r == X86EMUL_IO_NEEDED)
3629 return r;
3630
3631 if (r)
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003632 return emulate_gp(ctxt, 0);
3633
Avi Kivitydd856ef2012-08-27 23:46:17 +03003634 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3635 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003636 return X86EMUL_CONTINUE;
3637}
3638
Paolo Bonzinidd307d02016-07-12 10:35:51 +02003639static int em_store_sreg(struct x86_emulate_ctxt *ctxt, int segment)
3640{
3641 if (segment > VCPU_SREG_GS &&
3642 (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_UMIP) &&
3643 ctxt->ops->cpl(ctxt) > 0)
3644 return emulate_gp(ctxt, 0);
3645
3646 ctxt->dst.val = get_segment_selector(ctxt, segment);
3647 if (ctxt->dst.bytes == 4 && ctxt->dst.type == OP_MEM)
3648 ctxt->dst.bytes = 2;
3649 return X86EMUL_CONTINUE;
3650}
3651
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003652static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3653{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003654 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003655 return emulate_ud(ctxt);
3656
Paolo Bonzinidd307d02016-07-12 10:35:51 +02003657 return em_store_sreg(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003658}
3659
3660static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3661{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003662 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003663
Avi Kivity9dac77f2011-06-01 15:34:25 +03003664 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003665 return emulate_ud(ctxt);
3666
Avi Kivity9dac77f2011-06-01 15:34:25 +03003667 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003668 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3669
3670 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003671 ctxt->dst.type = OP_NONE;
3672 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003673}
3674
Paolo Bonzinidd307d02016-07-12 10:35:51 +02003675static int em_sldt(struct x86_emulate_ctxt *ctxt)
3676{
3677 return em_store_sreg(ctxt, VCPU_SREG_LDTR);
3678}
3679
Avi Kivitya14e5792012-06-13 12:28:33 +03003680static int em_lldt(struct x86_emulate_ctxt *ctxt)
3681{
3682 u16 sel = ctxt->src.val;
3683
3684 /* Disable writeback. */
3685 ctxt->dst.type = OP_NONE;
3686 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3687}
3688
Paolo Bonzinidd307d02016-07-12 10:35:51 +02003689static int em_str(struct x86_emulate_ctxt *ctxt)
3690{
3691 return em_store_sreg(ctxt, VCPU_SREG_TR);
3692}
3693
Avi Kivity80890002012-06-13 16:33:29 +03003694static int em_ltr(struct x86_emulate_ctxt *ctxt)
3695{
3696 u16 sel = ctxt->src.val;
3697
3698 /* Disable writeback. */
3699 ctxt->dst.type = OP_NONE;
3700 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3701}
3702
Avi Kivity38503912011-03-31 18:48:09 +02003703static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3704{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003705 int rc;
3706 ulong linear;
3707
Avi Kivity9dac77f2011-06-01 15:34:25 +03003708 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003709 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003710 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003711 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003712 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003713 return X86EMUL_CONTINUE;
3714}
3715
Avi Kivity2d04a052011-04-20 15:32:49 +03003716static int em_clts(struct x86_emulate_ctxt *ctxt)
3717{
3718 ulong cr0;
3719
3720 cr0 = ctxt->ops->get_cr(ctxt, 0);
3721 cr0 &= ~X86_CR0_TS;
3722 ctxt->ops->set_cr(ctxt, 0, cr0);
3723 return X86EMUL_CONTINUE;
3724}
3725
Jan Kiszkab34a8052015-03-09 20:27:43 +01003726static int em_hypercall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003727{
Nadav Amit0f54a322014-08-29 11:26:55 +03003728 int rc = ctxt->ops->fix_hypercall(ctxt);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003729
Avi Kivity26d05cc2011-04-21 12:07:59 +03003730 if (rc != X86EMUL_CONTINUE)
3731 return rc;
3732
3733 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003734 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003735 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003736 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003737 return X86EMUL_CONTINUE;
3738}
3739
Avi Kivity96051572012-06-10 17:21:18 +03003740static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3741 void (*get)(struct x86_emulate_ctxt *ctxt,
3742 struct desc_ptr *ptr))
3743{
3744 struct desc_ptr desc_ptr;
3745
Paolo Bonziniae3e61e2016-07-12 10:36:41 +02003746 if ((ctxt->ops->get_cr(ctxt, 4) & X86_CR4_UMIP) &&
3747 ctxt->ops->cpl(ctxt) > 0)
3748 return emulate_gp(ctxt, 0);
3749
Avi Kivity96051572012-06-10 17:21:18 +03003750 if (ctxt->mode == X86EMUL_MODE_PROT64)
3751 ctxt->op_bytes = 8;
3752 get(ctxt, &desc_ptr);
3753 if (ctxt->op_bytes == 2) {
3754 ctxt->op_bytes = 4;
3755 desc_ptr.address &= 0x00ffffff;
3756 }
3757 /* Disable writeback. */
3758 ctxt->dst.type = OP_NONE;
Steve Rutherford129a72a2017-01-11 18:28:29 -08003759 return segmented_write_std(ctxt, ctxt->dst.addr.mem,
3760 &desc_ptr, 2 + ctxt->op_bytes);
Avi Kivity96051572012-06-10 17:21:18 +03003761}
3762
3763static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3764{
3765 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3766}
3767
3768static int em_sidt(struct x86_emulate_ctxt *ctxt)
3769{
3770 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3771}
3772
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003773static int em_lgdt_lidt(struct x86_emulate_ctxt *ctxt, bool lgdt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003774{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003775 struct desc_ptr desc_ptr;
3776 int rc;
3777
Avi Kivity510425f2012-06-07 17:04:36 +03003778 if (ctxt->mode == X86EMUL_MODE_PROT64)
3779 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003780 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003781 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003782 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003783 if (rc != X86EMUL_CONTINUE)
3784 return rc;
Nadav Amit9a9abf62014-11-02 11:54:56 +02003785 if (ctxt->mode == X86EMUL_MODE_PROT64 &&
Yu Zhangfd8cb432017-08-24 20:27:56 +08003786 emul_is_noncanonical_address(desc_ptr.address, ctxt))
Nadav Amit9a9abf62014-11-02 11:54:56 +02003787 return emulate_gp(ctxt, 0);
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003788 if (lgdt)
3789 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3790 else
3791 ctxt->ops->set_idt(ctxt, &desc_ptr);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003792 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003793 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003794 return X86EMUL_CONTINUE;
3795}
3796
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003797static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3798{
3799 return em_lgdt_lidt(ctxt, true);
3800}
3801
Avi Kivity26d05cc2011-04-21 12:07:59 +03003802static int em_lidt(struct x86_emulate_ctxt *ctxt)
3803{
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003804 return em_lgdt_lidt(ctxt, false);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003805}
3806
3807static int em_smsw(struct x86_emulate_ctxt *ctxt)
3808{
Paolo Bonziniae3e61e2016-07-12 10:36:41 +02003809 if ((ctxt->ops->get_cr(ctxt, 4) & X86_CR4_UMIP) &&
3810 ctxt->ops->cpl(ctxt) > 0)
3811 return emulate_gp(ctxt, 0);
3812
Nadav Amit32e94d02014-06-02 18:34:11 +03003813 if (ctxt->dst.type == OP_MEM)
3814 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003815 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003816 return X86EMUL_CONTINUE;
3817}
3818
3819static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3820{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003821 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003822 | (ctxt->src.val & 0x0f));
3823 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003824 return X86EMUL_CONTINUE;
3825}
3826
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003827static int em_loop(struct x86_emulate_ctxt *ctxt)
3828{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003829 int rc = X86EMUL_CONTINUE;
3830
Paolo Bonzini01485a22014-11-19 18:25:08 +01003831 register_address_increment(ctxt, VCPU_REGS_RCX, -1);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003832 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003833 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
Nadav Amit234f3ce2014-09-18 22:39:38 +03003834 rc = jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003835
Nadav Amit234f3ce2014-09-18 22:39:38 +03003836 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003837}
3838
3839static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3840{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003841 int rc = X86EMUL_CONTINUE;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003842
Nadav Amit234f3ce2014-09-18 22:39:38 +03003843 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
3844 rc = jmp_rel(ctxt, ctxt->src.val);
3845
3846 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003847}
3848
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003849static int em_in(struct x86_emulate_ctxt *ctxt)
3850{
3851 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3852 &ctxt->dst.val))
3853 return X86EMUL_IO_NEEDED;
3854
3855 return X86EMUL_CONTINUE;
3856}
3857
3858static int em_out(struct x86_emulate_ctxt *ctxt)
3859{
3860 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3861 &ctxt->src.val, 1);
3862 /* Disable writeback. */
3863 ctxt->dst.type = OP_NONE;
3864 return X86EMUL_CONTINUE;
3865}
3866
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003867static int em_cli(struct x86_emulate_ctxt *ctxt)
3868{
3869 if (emulator_bad_iopl(ctxt))
3870 return emulate_gp(ctxt, 0);
3871
3872 ctxt->eflags &= ~X86_EFLAGS_IF;
3873 return X86EMUL_CONTINUE;
3874}
3875
3876static int em_sti(struct x86_emulate_ctxt *ctxt)
3877{
3878 if (emulator_bad_iopl(ctxt))
3879 return emulate_gp(ctxt, 0);
3880
3881 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3882 ctxt->eflags |= X86_EFLAGS_IF;
3883 return X86EMUL_CONTINUE;
3884}
3885
Avi Kivity6d6eede2012-06-07 14:11:36 +03003886static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3887{
3888 u32 eax, ebx, ecx, edx;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003889 u64 msr = 0;
3890
3891 ctxt->ops->get_msr(ctxt, MSR_MISC_FEATURES_ENABLES, &msr);
3892 if (msr & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
3893 ctxt->ops->cpl(ctxt)) {
3894 return emulate_gp(ctxt, 0);
3895 }
Avi Kivity6d6eede2012-06-07 14:11:36 +03003896
Avi Kivitydd856ef2012-08-27 23:46:17 +03003897 eax = reg_read(ctxt, VCPU_REGS_RAX);
3898 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Sean Christophersonf91af512020-03-04 17:34:37 -08003899 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, false);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003900 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3901 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3902 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3903 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003904 return X86EMUL_CONTINUE;
3905}
3906
Paolo Bonzini98f73632013-10-31 11:19:42 +01003907static int em_sahf(struct x86_emulate_ctxt *ctxt)
3908{
3909 u32 flags;
3910
Nadav Amit0efb0442015-03-29 16:33:03 +03003911 flags = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
3912 X86_EFLAGS_SF;
Paolo Bonzini98f73632013-10-31 11:19:42 +01003913 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3914
3915 ctxt->eflags &= ~0xffUL;
3916 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3917 return X86EMUL_CONTINUE;
3918}
3919
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003920static int em_lahf(struct x86_emulate_ctxt *ctxt)
3921{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003922 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3923 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003924 return X86EMUL_CONTINUE;
3925}
3926
Avi Kivity92998362012-06-13 12:25:06 +03003927static int em_bswap(struct x86_emulate_ctxt *ctxt)
3928{
3929 switch (ctxt->op_bytes) {
3930#ifdef CONFIG_X86_64
3931 case 8:
3932 asm("bswap %0" : "+r"(ctxt->dst.val));
3933 break;
3934#endif
3935 default:
3936 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3937 break;
3938 }
3939 return X86EMUL_CONTINUE;
3940}
3941
Nadav Amit13e457e2014-10-13 13:04:13 +03003942static int em_clflush(struct x86_emulate_ctxt *ctxt)
3943{
3944 /* emulating clflush regardless of cpuid */
3945 return X86EMUL_CONTINUE;
3946}
3947
David Edmondson51b958e2020-11-03 12:04:00 +00003948static int em_clflushopt(struct x86_emulate_ctxt *ctxt)
3949{
3950 /* emulating clflushopt regardless of cpuid */
3951 return X86EMUL_CONTINUE;
3952}
3953
Nadav Amit2276b512015-01-26 09:32:24 +02003954static int em_movsxd(struct x86_emulate_ctxt *ctxt)
3955{
3956 ctxt->dst.val = (s32) ctxt->src.val;
3957 return X86EMUL_CONTINUE;
3958}
3959
Radim Krčmář283c95d2016-11-09 19:07:06 +01003960static int check_fxsr(struct x86_emulate_ctxt *ctxt)
3961{
Sean Christopherson5ae78e92019-12-17 13:32:38 -08003962 if (!ctxt->ops->guest_has_fxsr(ctxt))
Radim Krčmář283c95d2016-11-09 19:07:06 +01003963 return emulate_ud(ctxt);
3964
3965 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
3966 return emulate_nm(ctxt);
3967
3968 /*
3969 * Don't emulate a case that should never be hit, instead of working
3970 * around a lack of fxsave64/fxrstor64 on old compilers.
3971 */
3972 if (ctxt->mode >= X86EMUL_MODE_PROT64)
3973 return X86EMUL_UNHANDLEABLE;
3974
3975 return X86EMUL_CONTINUE;
3976}
3977
3978/*
Nick Desaulniers9d643f62017-05-30 20:08:38 -07003979 * Hardware doesn't save and restore XMM 0-7 without CR4.OSFXSR, but does save
3980 * and restore MXCSR.
3981 */
3982static size_t __fxstate_size(int nregs)
3983{
3984 return offsetof(struct fxregs_state, xmm_space[0]) + nregs * 16;
3985}
3986
3987static inline size_t fxstate_size(struct x86_emulate_ctxt *ctxt)
3988{
3989 bool cr4_osfxsr;
3990 if (ctxt->mode == X86EMUL_MODE_PROT64)
3991 return __fxstate_size(16);
3992
3993 cr4_osfxsr = ctxt->ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR;
3994 return __fxstate_size(cr4_osfxsr ? 8 : 0);
3995}
3996
3997/*
Radim Krčmář283c95d2016-11-09 19:07:06 +01003998 * FXSAVE and FXRSTOR have 4 different formats depending on execution mode,
3999 * 1) 16 bit mode
4000 * 2) 32 bit mode
4001 * - like (1), but FIP and FDP (foo) are only 16 bit. At least Intel CPUs
4002 * preserve whole 32 bit values, though, so (1) and (2) are the same wrt.
4003 * save and restore
4004 * 3) 64-bit mode with REX.W prefix
4005 * - like (2), but XMM 8-15 are being saved and restored
4006 * 4) 64-bit mode without REX.W prefix
4007 * - like (3), but FIP and FDP are 64 bit
4008 *
4009 * Emulation uses (3) for (1) and (2) and preserves XMM 8-15 to reach the
4010 * desired result. (4) is not emulated.
4011 *
4012 * Note: Guest and host CPUID.(EAX=07H,ECX=0H):EBX[bit 13] (deprecate FPU CS
4013 * and FPU DS) should match.
4014 */
4015static int em_fxsave(struct x86_emulate_ctxt *ctxt)
4016{
4017 struct fxregs_state fx_state;
Radim Krčmář283c95d2016-11-09 19:07:06 +01004018 int rc;
4019
4020 rc = check_fxsr(ctxt);
4021 if (rc != X86EMUL_CONTINUE)
4022 return rc;
4023
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02004024 kvm_fpu_get();
Sean Christophersona7baead2020-01-17 11:30:50 -08004025
Radim Krčmář283c95d2016-11-09 19:07:06 +01004026 rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_state));
4027
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02004028 kvm_fpu_put();
Sean Christophersona7baead2020-01-17 11:30:50 -08004029
Radim Krčmář283c95d2016-11-09 19:07:06 +01004030 if (rc != X86EMUL_CONTINUE)
4031 return rc;
4032
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004033 return segmented_write_std(ctxt, ctxt->memop.addr.mem, &fx_state,
4034 fxstate_size(ctxt));
Radim Krčmář283c95d2016-11-09 19:07:06 +01004035}
4036
David Hildenbrand4d772cb2017-11-07 18:04:05 +01004037/*
4038 * FXRSTOR might restore XMM registers not provided by the guest. Fill
4039 * in the host registers (via FXSAVE) instead, so they won't be modified.
4040 * (preemption has to stay disabled until FXRSTOR).
4041 *
4042 * Use noinline to keep the stack for other functions called by callers small.
4043 */
4044static noinline int fxregs_fixup(struct fxregs_state *fx_state,
4045 const size_t used_size)
4046{
4047 struct fxregs_state fx_tmp;
4048 int rc;
4049
4050 rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_tmp));
4051 memcpy((void *)fx_state + used_size, (void *)&fx_tmp + used_size,
4052 __fxstate_size(16) - used_size);
4053
4054 return rc;
4055}
4056
Radim Krčmář283c95d2016-11-09 19:07:06 +01004057static int em_fxrstor(struct x86_emulate_ctxt *ctxt)
4058{
4059 struct fxregs_state fx_state;
4060 int rc;
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004061 size_t size;
Radim Krčmář283c95d2016-11-09 19:07:06 +01004062
4063 rc = check_fxsr(ctxt);
4064 if (rc != X86EMUL_CONTINUE)
4065 return rc;
4066
David Hildenbrand4d772cb2017-11-07 18:04:05 +01004067 size = fxstate_size(ctxt);
4068 rc = segmented_read_std(ctxt, ctxt->memop.addr.mem, &fx_state, size);
4069 if (rc != X86EMUL_CONTINUE)
4070 return rc;
4071
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02004072 kvm_fpu_get();
Sean Christophersona7baead2020-01-17 11:30:50 -08004073
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004074 if (size < __fxstate_size(16)) {
David Hildenbrand4d772cb2017-11-07 18:04:05 +01004075 rc = fxregs_fixup(&fx_state, size);
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004076 if (rc != X86EMUL_CONTINUE)
4077 goto out;
4078 }
4079
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004080 if (fx_state.mxcsr >> 16) {
4081 rc = emulate_gp(ctxt, 0);
4082 goto out;
4083 }
Radim Krčmář283c95d2016-11-09 19:07:06 +01004084
4085 if (rc == X86EMUL_CONTINUE)
4086 rc = asm_safe("fxrstor %[fx]", : [fx] "m"(fx_state));
4087
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004088out:
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02004089 kvm_fpu_put();
Sean Christophersona7baead2020-01-17 11:30:50 -08004090
Radim Krčmář283c95d2016-11-09 19:07:06 +01004091 return rc;
4092}
4093
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02004094static int em_xsetbv(struct x86_emulate_ctxt *ctxt)
4095{
4096 u32 eax, ecx, edx;
4097
4098 eax = reg_read(ctxt, VCPU_REGS_RAX);
4099 edx = reg_read(ctxt, VCPU_REGS_RDX);
4100 ecx = reg_read(ctxt, VCPU_REGS_RCX);
4101
4102 if (ctxt->ops->set_xcr(ctxt, ecx, ((u64)edx << 32) | eax))
4103 return emulate_gp(ctxt, 0);
4104
4105 return X86EMUL_CONTINUE;
4106}
4107
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004108static bool valid_cr(int nr)
4109{
4110 switch (nr) {
4111 case 0:
4112 case 2 ... 4:
4113 case 8:
4114 return true;
4115 default:
4116 return false;
4117 }
4118}
4119
Sean Christophersond0fe7b62021-04-21 19:21:20 -07004120static int check_cr_access(struct x86_emulate_ctxt *ctxt)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004121{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004122 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004123 return emulate_ud(ctxt);
4124
4125 return X86EMUL_CONTINUE;
4126}
4127
Joerg Roedel3b88e412011-04-04 12:39:29 +02004128static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
4129{
4130 unsigned long dr7;
4131
Avi Kivity717746e2011-04-20 13:37:53 +03004132 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02004133
4134 /* Check if DR7.Global_Enable is set */
4135 return dr7 & (1 << 13);
4136}
4137
4138static int check_dr_read(struct x86_emulate_ctxt *ctxt)
4139{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004140 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02004141 u64 cr4;
4142
4143 if (dr > 7)
4144 return emulate_ud(ctxt);
4145
Avi Kivity717746e2011-04-20 13:37:53 +03004146 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02004147 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
4148 return emulate_ud(ctxt);
4149
Nadav Amit6d2a0522014-11-02 11:54:43 +02004150 if (check_dr7_gd(ctxt)) {
4151 ulong dr6;
4152
4153 ctxt->ops->get_dr(ctxt, 6, &dr6);
Liran Alon1fc5d1942019-06-06 01:54:47 +03004154 dr6 &= ~DR_TRAP_BITS;
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08004155 dr6 |= DR6_BD | DR6_ACTIVE_LOW;
Nadav Amit6d2a0522014-11-02 11:54:43 +02004156 ctxt->ops->set_dr(ctxt, 6, dr6);
Joerg Roedel3b88e412011-04-04 12:39:29 +02004157 return emulate_db(ctxt);
Nadav Amit6d2a0522014-11-02 11:54:43 +02004158 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02004159
4160 return X86EMUL_CONTINUE;
4161}
4162
4163static int check_dr_write(struct x86_emulate_ctxt *ctxt)
4164{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004165 u64 new_val = ctxt->src.val64;
4166 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02004167
4168 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
4169 return emulate_gp(ctxt, 0);
4170
4171 return check_dr_read(ctxt);
4172}
4173
Joerg Roedel01de8b02011-04-04 12:39:31 +02004174static int check_svme(struct x86_emulate_ctxt *ctxt)
4175{
Radim Krčmář92ceb762017-05-18 19:37:32 +02004176 u64 efer = 0;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004177
Avi Kivity717746e2011-04-20 13:37:53 +03004178 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02004179
4180 if (!(efer & EFER_SVME))
4181 return emulate_ud(ctxt);
4182
4183 return X86EMUL_CONTINUE;
4184}
4185
4186static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
4187{
Avi Kivitydd856ef2012-08-27 23:46:17 +03004188 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02004189
4190 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07004191 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02004192 return emulate_gp(ctxt, 0);
4193
4194 return check_svme(ctxt);
4195}
4196
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004197static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
4198{
Avi Kivity717746e2011-04-20 13:37:53 +03004199 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004200
Avi Kivity717746e2011-04-20 13:37:53 +03004201 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Hou Wenlonge9337c82021-08-18 11:36:31 +08004202 return emulate_gp(ctxt, 0);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004203
4204 return X86EMUL_CONTINUE;
4205}
4206
Joerg Roedel80612522011-04-04 12:39:33 +02004207static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
4208{
Avi Kivity717746e2011-04-20 13:37:53 +03004209 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03004210 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02004211
Arbel Moshe2d7921c2018-03-12 13:12:53 +02004212 /*
4213 * VMware allows access to these Pseduo-PMCs even when read via RDPMC
4214 * in Ring3 when CR4.PCE=0.
4215 */
4216 if (enable_vmware_backdoor && is_vmware_backdoor_pmc(rcx))
4217 return X86EMUL_CONTINUE;
4218
Wanpeng Li9ae7f6c2021-10-20 03:13:56 -07004219 /*
4220 * If CR4.PCE is set, the SDM requires CPL=0 or CR0.PE=0. The CR0.PE
4221 * check however is unnecessary because CPL is always 0 outside
4222 * protected mode.
4223 */
Avi Kivity717746e2011-04-20 13:37:53 +03004224 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03004225 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02004226 return emulate_gp(ctxt, 0);
4227
4228 return X86EMUL_CONTINUE;
4229}
4230
Joerg Roedelf6511932011-04-04 12:39:35 +02004231static int check_perm_in(struct x86_emulate_ctxt *ctxt)
4232{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004233 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
4234 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02004235 return emulate_gp(ctxt, 0);
4236
4237 return X86EMUL_CONTINUE;
4238}
4239
4240static int check_perm_out(struct x86_emulate_ctxt *ctxt)
4241{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004242 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
4243 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02004244 return emulate_gp(ctxt, 0);
4245
4246 return X86EMUL_CONTINUE;
4247}
4248
Avi Kivity73fba5f2010-07-29 15:11:53 +03004249#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004250#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
4251#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
4252 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03004253#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02004254#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004255#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
4256#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Nadav Amit39f062f2014-11-26 15:47:18 +02004257#define ID(_f, _i) { .flags = ((_f) | InstrDual | ModRM), .u.idual = (_i) }
Nadav Amit2276b512015-01-26 09:32:24 +02004258#define MD(_f, _m) { .flags = ((_f) | ModeDual), .u.mdual = (_m) }
Gleb Natapov045a2822012-12-20 16:57:43 +02004259#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03004260#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02004261#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004262#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004263 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004264#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004265 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
4266 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02004267#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03004268
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004269#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02004270#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004271#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02004272#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004273#define I2bvIP(_f, _e, _i, _p) \
4274 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004275
Avi Kivityfb864fb2013-01-04 16:18:54 +02004276#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
4277 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
4278 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03004279
Nadav Amit0f54a322014-08-29 11:26:55 +03004280static const struct opcode group7_rm0[] = {
4281 N,
Jan Kiszkab34a8052015-03-09 20:27:43 +01004282 I(SrcNone | Priv | EmulateOnUD, em_hypercall),
Nadav Amit0f54a322014-08-29 11:26:55 +03004283 N, N, N, N, N, N,
4284};
4285
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004286static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004287 DI(SrcNone | Priv, monitor),
4288 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004289 N, N, N, N, N, N,
4290};
4291
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02004292static const struct opcode group7_rm2[] = {
4293 N,
4294 II(ImplicitOps | Priv, em_xsetbv, xsetbv),
4295 N, N, N, N, N, N,
4296};
4297
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004298static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004299 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Jan Kiszkab34a8052015-03-09 20:27:43 +01004300 II(SrcNone | Prot | EmulateOnUD, em_hypercall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004301 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
4302 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
4303 DIP(SrcNone | Prot | Priv, stgi, check_svme),
4304 DIP(SrcNone | Prot | Priv, clgi, check_svme),
4305 DIP(SrcNone | Prot | Priv, skinit, check_svme),
4306 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004307};
Avi Kivity6230f7f2010-08-26 18:34:55 +03004308
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004309static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004310 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004311 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004312 N, N, N, N, N, N,
4313};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004314
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004315static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02004316 F(Lock, em_add),
4317 F(Lock | PageTable, em_or),
4318 F(Lock, em_adc),
4319 F(Lock, em_sbb),
4320 F(Lock | PageTable, em_and),
4321 F(Lock, em_sub),
4322 F(Lock, em_xor),
4323 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004324};
4325
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004326static const struct opcode group1A[] = {
Tom Lendacky0f89b202016-12-14 14:59:23 -05004327 I(DstMem | SrcNone | Mov | Stack | IncSP | TwoMemOp, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004328};
4329
Avi Kivity007a3b52013-01-19 19:51:51 +02004330static const struct opcode group2[] = {
4331 F(DstMem | ModRM, em_rol),
4332 F(DstMem | ModRM, em_ror),
4333 F(DstMem | ModRM, em_rcl),
4334 F(DstMem | ModRM, em_rcr),
4335 F(DstMem | ModRM, em_shl),
4336 F(DstMem | ModRM, em_shr),
4337 F(DstMem | ModRM, em_shl),
4338 F(DstMem | ModRM, em_sar),
4339};
4340
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004341static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02004342 F(DstMem | SrcImm | NoWrite, em_test),
4343 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02004344 F(DstMem | SrcNone | Lock, em_not),
4345 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02004346 F(DstXacc | Src2Mem, em_mul_ex),
4347 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004348 F(DstXacc | Src2Mem, em_div_ex),
4349 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004350};
4351
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004352static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02004353 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
4354 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004355 N, N, N, N, N, N,
4356};
4357
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004358static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02004359 F(DstMem | SrcNone | Lock, em_inc),
4360 F(DstMem | SrcNone | Lock, em_dec),
Eric Hankland018d70f2021-11-30 15:42:21 +08004361 I(SrcMem | NearBranch | IsBranch, em_call_near_abs),
4362 I(SrcMemFAddr | ImplicitOps | IsBranch, em_call_far),
4363 I(SrcMem | NearBranch | IsBranch, em_jmp_abs),
4364 I(SrcMemFAddr | ImplicitOps | IsBranch, em_jmp_far),
Tom Lendacky0f89b202016-12-14 14:59:23 -05004365 I(SrcMem | Stack | TwoMemOp, em_push), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004366};
4367
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004368static const struct opcode group6[] = {
Paolo Bonzinidd307d02016-07-12 10:35:51 +02004369 II(Prot | DstMem, em_sldt, sldt),
4370 II(Prot | DstMem, em_str, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03004371 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03004372 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004373 N, N, N, N,
4374};
4375
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004376static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03004377 II(Mov | DstMem, em_sgdt, sgdt),
4378 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004379 II(SrcMem | Priv, em_lgdt, lgdt),
4380 II(SrcMem | Priv, em_lidt, lidt),
4381 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
4382 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
4383 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004384}, {
Nadav Amit0f54a322014-08-29 11:26:55 +03004385 EXT(0, group7_rm0),
Avi Kivity5ef39c72011-04-21 12:21:50 +03004386 EXT(0, group7_rm1),
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02004387 EXT(0, group7_rm2),
4388 EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004389 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
4390 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
4391 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004392} };
4393
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004394static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004395 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02004396 F(DstMem | SrcImmByte | NoWrite, em_bt),
4397 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
4398 F(DstMem | SrcImmByte | Lock, em_btr),
4399 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004400};
4401
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02004402/*
4403 * The "memory" destination is actually always a register, since we come
4404 * from the register case of group9.
4405 */
4406static const struct gprefix pfx_0f_c7_7 = {
Sean Christopherson2183de42021-05-04 10:17:23 -07004407 N, N, N, II(DstMem | ModRM | Op3264 | EmulateOnUD, em_rdpid, rdpid),
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02004408};
4409
4410
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004411static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004412 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004413}, {
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02004414 N, N, N, N, N, N, N,
4415 GP(0, &pfx_0f_c7_7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004416} };
4417
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004418static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004419 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004420 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004421};
4422
Nadav Amit13e457e2014-10-13 13:04:13 +03004423static const struct gprefix pfx_0f_ae_7 = {
David Edmondson51b958e2020-11-03 12:04:00 +00004424 I(SrcMem | ByteOp, em_clflush), I(SrcMem | ByteOp, em_clflushopt), N, N,
Nadav Amit13e457e2014-10-13 13:04:13 +03004425};
4426
4427static const struct group_dual group15 = { {
Radim Krčmář283c95d2016-11-09 19:07:06 +01004428 I(ModRM | Aligned16, em_fxsave),
4429 I(ModRM | Aligned16, em_fxrstor),
4430 N, N, N, N, N, GP(0, &pfx_0f_ae_7),
Nadav Amit13e457e2014-10-13 13:04:13 +03004431}, {
4432 N, N, N, N, N, N, N, N,
4433} };
4434
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004435static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03004436 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02004437};
4438
Nadav Amit39f062f2014-11-26 15:47:18 +02004439static const struct instr_dual instr_dual_0f_2b = {
4440 I(0, em_mov), N
4441};
4442
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004443static const struct gprefix pfx_0f_2b = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004444 ID(0, &instr_dual_0f_2b), ID(0, &instr_dual_0f_2b), N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03004445};
4446
Stefan Fritsch29916962018-04-01 17:54:44 +02004447static const struct gprefix pfx_0f_10_0f_11 = {
4448 I(Unaligned, em_mov), I(Unaligned, em_mov), N, N,
4449};
4450
Igor Mammedov27ce8252014-03-15 21:01:59 +01004451static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01004452 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004453};
4454
Alex Williamson0a370272014-07-11 11:56:31 -06004455static const struct gprefix pfx_0f_e7 = {
4456 N, I(Sse, em_mov), N, N,
4457};
4458
Gleb Natapov045a2822012-12-20 16:57:43 +02004459static const struct escape escape_d9 = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02004460 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstcw),
Gleb Natapov045a2822012-12-20 16:57:43 +02004461}, {
4462 /* 0xC0 - 0xC7 */
4463 N, N, N, N, N, N, N, N,
4464 /* 0xC8 - 0xCF */
4465 N, N, N, N, N, N, N, N,
4466 /* 0xD0 - 0xC7 */
4467 N, N, N, N, N, N, N, N,
4468 /* 0xD8 - 0xDF */
4469 N, N, N, N, N, N, N, N,
4470 /* 0xE0 - 0xE7 */
4471 N, N, N, N, N, N, N, N,
4472 /* 0xE8 - 0xEF */
4473 N, N, N, N, N, N, N, N,
4474 /* 0xF0 - 0xF7 */
4475 N, N, N, N, N, N, N, N,
4476 /* 0xF8 - 0xFF */
4477 N, N, N, N, N, N, N, N,
4478} };
4479
4480static const struct escape escape_db = { {
4481 N, N, N, N, N, N, N, N,
4482}, {
4483 /* 0xC0 - 0xC7 */
4484 N, N, N, N, N, N, N, N,
4485 /* 0xC8 - 0xCF */
4486 N, N, N, N, N, N, N, N,
4487 /* 0xD0 - 0xC7 */
4488 N, N, N, N, N, N, N, N,
4489 /* 0xD8 - 0xDF */
4490 N, N, N, N, N, N, N, N,
4491 /* 0xE0 - 0xE7 */
4492 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
4493 /* 0xE8 - 0xEF */
4494 N, N, N, N, N, N, N, N,
4495 /* 0xF0 - 0xF7 */
4496 N, N, N, N, N, N, N, N,
4497 /* 0xF8 - 0xFF */
4498 N, N, N, N, N, N, N, N,
4499} };
4500
4501static const struct escape escape_dd = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02004502 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstsw),
Gleb Natapov045a2822012-12-20 16:57:43 +02004503}, {
4504 /* 0xC0 - 0xC7 */
4505 N, N, N, N, N, N, N, N,
4506 /* 0xC8 - 0xCF */
4507 N, N, N, N, N, N, N, N,
4508 /* 0xD0 - 0xC7 */
4509 N, N, N, N, N, N, N, N,
4510 /* 0xD8 - 0xDF */
4511 N, N, N, N, N, N, N, N,
4512 /* 0xE0 - 0xE7 */
4513 N, N, N, N, N, N, N, N,
4514 /* 0xE8 - 0xEF */
4515 N, N, N, N, N, N, N, N,
4516 /* 0xF0 - 0xF7 */
4517 N, N, N, N, N, N, N, N,
4518 /* 0xF8 - 0xFF */
4519 N, N, N, N, N, N, N, N,
4520} };
4521
Nadav Amit39f062f2014-11-26 15:47:18 +02004522static const struct instr_dual instr_dual_0f_c3 = {
4523 I(DstMem | SrcReg | ModRM | No16 | Mov, em_mov), N
4524};
4525
Nadav Amit2276b512015-01-26 09:32:24 +02004526static const struct mode_dual mode_dual_63 = {
4527 N, I(DstReg | SrcMem32 | ModRM | Mov, em_movsxd)
4528};
4529
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004530static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004531 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004532 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004533 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
4534 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004535 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004536 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004537 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
4538 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004539 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004540 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004541 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
4542 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004543 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004544 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004545 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
4546 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004547 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004548 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004549 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004550 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004551 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004552 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004553 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004554 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004555 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02004556 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004557 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03004558 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004559 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09004560 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004561 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09004562 I(ImplicitOps | Stack | No64, em_pusha),
4563 I(ImplicitOps | Stack | No64, em_popa),
Nadav Amit2276b512015-01-26 09:32:24 +02004564 N, MD(ModRM, &mode_dual_63),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004565 N, N, N, N,
4566 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03004567 I(SrcImm | Mov | Stack, em_push),
4568 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03004569 I(SrcImmByte | Mov | Stack, em_push),
4570 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004571 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09004572 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03004573 /* 0x70 - 0x7F */
Eric Hankland018d70f2021-11-30 15:42:21 +08004574 X16(D(SrcImmByte | NearBranch | IsBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004575 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004576 G(ByteOp | DstMem | SrcImm, group1),
4577 G(DstMem | SrcImm, group1),
4578 G(ByteOp | DstMem | SrcImm | No64, group1),
4579 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02004580 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004581 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004582 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004583 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004584 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004585 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09004586 D(ModRM | SrcMem | NoAccess | DstReg),
4587 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
4588 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004589 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004590 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004591 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03004592 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Eric Hankland018d70f2021-11-30 15:42:21 +08004593 I(SrcImmFAddr | No64 | IsBranch, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09004594 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01004595 II(ImplicitOps | Stack, em_popf, popf),
4596 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004597 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004598 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004599 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Tom Lendacky0f89b202016-12-14 14:59:23 -05004600 I2bv(SrcSI | DstDI | Mov | String | TwoMemOp, em_mov),
4601 F2bv(SrcSI | DstDI | String | NoWrite | TwoMemOp, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004602 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004603 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004604 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
4605 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004606 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004607 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004608 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004609 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02004610 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004611 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004612 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Eric Hankland018d70f2021-11-30 15:42:21 +08004613 I(ImplicitOps | NearBranch | SrcImmU16 | IsBranch, em_ret_near_imm),
4614 I(ImplicitOps | NearBranch | IsBranch, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03004615 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
4616 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004617 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004618 /* 0xC8 - 0xCF */
Eric Hankland018d70f2021-11-30 15:42:21 +08004619 I(Stack | SrcImmU16 | Src2ImmByte | IsBranch, em_enter),
4620 I(Stack | IsBranch, em_leave),
4621 I(ImplicitOps | SrcImmU16 | IsBranch, em_ret_far_imm),
4622 I(ImplicitOps | IsBranch, em_ret_far),
4623 D(ImplicitOps | IsBranch), DI(SrcImmByte | IsBranch, intn),
4624 D(ImplicitOps | No64 | IsBranch),
4625 II(ImplicitOps | IsBranch, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004626 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004627 G(Src2One | ByteOp, group2), G(Src2One, group2),
4628 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02004629 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02004630 I(DstAcc | SrcImmUByte | No64, em_aad),
4631 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004632 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004633 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02004634 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004635 /* 0xE0 - 0xE7 */
Eric Hankland018d70f2021-11-30 15:42:21 +08004636 X3(I(SrcImmByte | NearBranch | IsBranch, em_loop)),
4637 I(SrcImmByte | NearBranch | IsBranch, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004638 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
4639 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004640 /* 0xE8 - 0xEF */
Eric Hankland018d70f2021-11-30 15:42:21 +08004641 I(SrcImm | NearBranch | IsBranch, em_call),
4642 D(SrcImm | ImplicitOps | NearBranch | IsBranch),
4643 I(SrcImmFAddr | No64 | IsBranch, em_jmp_far),
4644 D(SrcImmByte | ImplicitOps | NearBranch | IsBranch),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004645 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
4646 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004647 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004648 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004649 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4650 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004651 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09004652 D(ImplicitOps), D(ImplicitOps),
4653 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004654 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4655};
4656
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004657static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004658 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004659 G(0, group6), GD(0, &group7), N, N,
Eric Hankland018d70f2021-11-30 15:42:21 +08004660 N, I(ImplicitOps | EmulateOnUD | IsBranch, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004661 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004662 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004663 N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004664 /* 0x10 - 0x1F */
Stefan Fritsch29916962018-04-01 17:54:44 +02004665 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_10_0f_11),
4666 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_10_0f_11),
4667 N, N, N, N, N, N,
Paolo Bonzini34d26182020-05-15 12:18:14 -04004668 D(ImplicitOps | ModRM | SrcMem | NoAccess), /* 4 * prefetch + 4 * reserved NOP */
4669 D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
4670 D(ImplicitOps | ModRM | SrcMem | NoAccess), /* 8 * reserved NOP */
4671 D(ImplicitOps | ModRM | SrcMem | NoAccess), /* 8 * reserved NOP */
4672 D(ImplicitOps | ModRM | SrcMem | NoAccess), /* 8 * reserved NOP */
4673 D(ImplicitOps | ModRM | SrcMem | NoAccess), /* NOP + 7 * reserved NOP */
Avi Kivity73fba5f2010-07-29 15:11:53 +03004674 /* 0x20 - 0x2F */
Sean Christophersond0fe7b62021-04-21 19:21:20 -07004675 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_access),
Nadav Amit9b88ae92014-05-25 23:05:21 +03004676 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
4677 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
Sean Christophersond0fe7b62021-04-21 19:21:20 -07004678 check_cr_access),
Nadav Amit9b88ae92014-05-25 23:05:21 +03004679 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
4680 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004681 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004682 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
4683 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004684 N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b),
Avi Kivity3e114eb2012-04-09 18:40:01 +03004685 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004686 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004687 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004688 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004689 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004690 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Eric Hankland018d70f2021-11-30 15:42:21 +08004691 I(ImplicitOps | EmulateOnUD | IsBranch, em_sysenter),
4692 I(ImplicitOps | Priv | EmulateOnUD | IsBranch, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004693 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004694 N, N, N, N, N, N, N, N,
4695 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03004696 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004697 /* 0x50 - 0x5F */
4698 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4699 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004700 N, N, N, N,
4701 N, N, N, N,
4702 N, N, N, N,
4703 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004704 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004705 N, N, N, N,
4706 N, N, N, N,
4707 N, N, N, N,
4708 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004709 /* 0x80 - 0x8F */
Eric Hankland018d70f2021-11-30 15:42:21 +08004710 X16(D(SrcImm | NearBranch | IsBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004711 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004712 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004713 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004714 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004715 II(ImplicitOps, em_cpuid, cpuid),
4716 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004717 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4718 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004719 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004720 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Paolo Bonzini89651a32015-11-03 13:43:05 +01004721 II(EmulateOnUD | ImplicitOps, em_rsm, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004722 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004723 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4724 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Nadav Amit13e457e2014-10-13 13:04:13 +03004725 GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004726 /* 0xB0 - 0xB7 */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02004727 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable | SrcWrite, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004728 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004729 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004730 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4731 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004732 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004733 /* 0xB8 - 0xBF */
4734 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004735 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004736 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Nadav Amit900efe22015-03-30 15:39:21 +03004737 I(DstReg | SrcMem | ModRM, em_bsf_c),
4738 I(DstReg | SrcMem | ModRM, em_bsr_c),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004739 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004740 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02004741 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Nadav Amit39f062f2014-11-26 15:47:18 +02004742 N, ID(0, &instr_dual_0f_c3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004743 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004744 /* 0xC8 - 0xCF */
4745 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004746 /* 0xD0 - 0xDF */
4747 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4748 /* 0xE0 - 0xEF */
Alex Williamson0a370272014-07-11 11:56:31 -06004749 N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
4750 N, N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004751 /* 0xF0 - 0xFF */
4752 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4753};
4754
Nadav Amit39f062f2014-11-26 15:47:18 +02004755static const struct instr_dual instr_dual_0f_38_f0 = {
4756 I(DstReg | SrcMem | Mov, em_movbe), N
4757};
4758
4759static const struct instr_dual instr_dual_0f_38_f1 = {
4760 I(DstMem | SrcReg | Mov, em_movbe), N
4761};
4762
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004763static const struct gprefix three_byte_0f_38_f0 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004764 ID(0, &instr_dual_0f_38_f0), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004765};
4766
4767static const struct gprefix three_byte_0f_38_f1 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004768 ID(0, &instr_dual_0f_38_f1), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004769};
4770
4771/*
4772 * Insns below are selected by the prefix which indexed by the third opcode
4773 * byte.
4774 */
4775static const struct opcode opcode_map_0f_38[256] = {
4776 /* 0x00 - 0x7f */
4777 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004778 /* 0x80 - 0xef */
4779 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4780 /* 0xf0 - 0xf1 */
Nadav Amit53bb4f72014-12-07 11:49:42 +02004781 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0),
4782 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004783 /* 0xf2 - 0xff */
4784 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004785};
4786
Avi Kivity73fba5f2010-07-29 15:11:53 +03004787#undef D
4788#undef N
4789#undef G
4790#undef GD
4791#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004792#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004793#undef EXT
Nadav Amit2276b512015-01-26 09:32:24 +02004794#undef MD
Nadav Amit2b42fce2015-01-26 09:32:25 +02004795#undef ID
Avi Kivity73fba5f2010-07-29 15:11:53 +03004796
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004797#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004798#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004799#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004800#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004801#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004802
Avi Kivity9dac77f2011-06-01 15:34:25 +03004803static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004804{
4805 unsigned size;
4806
Avi Kivity9dac77f2011-06-01 15:34:25 +03004807 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004808 if (size == 8)
4809 size = 4;
4810 return size;
4811}
4812
4813static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4814 unsigned size, bool sign_extension)
4815{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004816 int rc = X86EMUL_CONTINUE;
4817
4818 op->type = OP_IMM;
4819 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004820 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004821 /* NB. Immediates are sign-extended as necessary. */
4822 switch (op->bytes) {
4823 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004824 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004825 break;
4826 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004827 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004828 break;
4829 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004830 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004831 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004832 case 8:
4833 op->val = insn_fetch(s64, ctxt);
4834 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004835 }
4836 if (!sign_extension) {
4837 switch (op->bytes) {
4838 case 1:
4839 op->val &= 0xff;
4840 break;
4841 case 2:
4842 op->val &= 0xffff;
4843 break;
4844 case 4:
4845 op->val &= 0xffffffff;
4846 break;
4847 }
4848 }
4849done:
4850 return rc;
4851}
4852
Avi Kivitya99455492011-09-13 10:45:41 +03004853static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4854 unsigned d)
4855{
4856 int rc = X86EMUL_CONTINUE;
4857
4858 switch (d) {
4859 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004860 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03004861 break;
4862 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004863 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03004864 break;
4865 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004866 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004867 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03004868 *op = ctxt->memop;
4869 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004870 if (ctxt->d & BitOp)
Avi Kivitya99455492011-09-13 10:45:41 +03004871 fetch_bit_operand(ctxt);
4872 op->orig_val = op->val;
4873 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004874 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004875 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004876 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03004877 case OpAcc:
4878 op->type = OP_REG;
4879 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004880 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03004881 fetch_register_operand(op);
4882 op->orig_val = op->val;
4883 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004884 case OpAccLo:
4885 op->type = OP_REG;
4886 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4887 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4888 fetch_register_operand(op);
4889 op->orig_val = op->val;
4890 break;
4891 case OpAccHi:
4892 if (ctxt->d & ByteOp) {
4893 op->type = OP_NONE;
4894 break;
4895 }
4896 op->type = OP_REG;
4897 op->bytes = ctxt->op_bytes;
4898 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4899 fetch_register_operand(op);
4900 op->orig_val = op->val;
4901 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004902 case OpDI:
4903 op->type = OP_MEM;
4904 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4905 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004906 register_address(ctxt, VCPU_REGS_RDI);
Avi Kivitya99455492011-09-13 10:45:41 +03004907 op->addr.mem.seg = VCPU_SREG_ES;
4908 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004909 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03004910 break;
4911 case OpDX:
4912 op->type = OP_REG;
4913 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004914 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03004915 fetch_register_operand(op);
4916 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004917 case OpCL:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004918 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004919 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004920 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004921 break;
4922 case OpImmByte:
4923 rc = decode_imm(ctxt, op, 1, true);
4924 break;
4925 case OpOne:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004926 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004927 op->bytes = 1;
4928 op->val = 1;
4929 break;
4930 case OpImm:
4931 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4932 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004933 case OpImm64:
4934 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4935 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004936 case OpMem8:
4937 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004938 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004939 ctxt->memop.addr.reg = decode_register(ctxt,
4940 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004941 fetch_register_operand(&ctxt->memop);
4942 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004943 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004944 case OpMem16:
4945 ctxt->memop.bytes = 2;
4946 goto mem_common;
4947 case OpMem32:
4948 ctxt->memop.bytes = 4;
4949 goto mem_common;
4950 case OpImmU16:
4951 rc = decode_imm(ctxt, op, 2, false);
4952 break;
4953 case OpImmU:
4954 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4955 break;
4956 case OpSI:
4957 op->type = OP_MEM;
4958 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4959 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004960 register_address(ctxt, VCPU_REGS_RSI);
Bandan Das573e80f2014-04-16 12:46:13 -04004961 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03004962 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004963 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004964 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004965 case OpXLat:
4966 op->type = OP_MEM;
4967 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4968 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004969 address_mask(ctxt,
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004970 reg_read(ctxt, VCPU_REGS_RBX) +
4971 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04004972 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004973 op->val = 0;
4974 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004975 case OpImmFAddr:
4976 op->type = OP_IMM;
4977 op->addr.mem.ea = ctxt->_eip;
4978 op->bytes = ctxt->op_bytes + 2;
4979 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4980 break;
4981 case OpMemFAddr:
4982 ctxt->memop.bytes = ctxt->op_bytes + 2;
4983 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004984 case OpES:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004985 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004986 op->val = VCPU_SREG_ES;
4987 break;
4988 case OpCS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004989 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004990 op->val = VCPU_SREG_CS;
4991 break;
4992 case OpSS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004993 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004994 op->val = VCPU_SREG_SS;
4995 break;
4996 case OpDS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004997 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004998 op->val = VCPU_SREG_DS;
4999 break;
5000 case OpFS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02005001 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03005002 op->val = VCPU_SREG_FS;
5003 break;
5004 case OpGS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02005005 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03005006 op->val = VCPU_SREG_GS;
5007 break;
Avi Kivitya99455492011-09-13 10:45:41 +03005008 case OpImplicit:
5009 /* Special instructions do their own operand decoding. */
5010 default:
5011 op->type = OP_NONE; /* Disable writeback. */
5012 break;
5013 }
5014
5015done:
5016 return rc;
5017}
5018
Wanpeng Lib35491e2021-05-27 17:01:37 -07005019int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len, int emulation_type)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005020{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005021 int rc = X86EMUL_CONTINUE;
5022 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03005023 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005024 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04005025 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03005026 struct opcode opcode;
Wanpeng Li3853be22017-11-05 16:54:47 -08005027 u16 dummy;
5028 struct desc_struct desc;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005029
Avi Kivityf09ed832011-09-13 10:45:40 +03005030 ctxt->memop.type = OP_NONE;
5031 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005032 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02005033 ctxt->fetch.ptr = ctxt->fetch.data;
5034 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02005035 ctxt->opcode_len = 1;
Vitaly Kuznetsov342993f2020-03-03 15:33:15 +01005036 ctxt->intercept = x86_intercept_none;
Andre Przywaradc25e892010-12-21 11:12:07 +01005037 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005038 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02005039 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02005040 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02005041 if (rc != X86EMUL_CONTINUE)
Sean Christopherson016cd752019-08-15 09:20:32 -07005042 goto done;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02005043 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005044
5045 switch (mode) {
5046 case X86EMUL_MODE_REAL:
5047 case X86EMUL_MODE_VM86:
Wanpeng Li3853be22017-11-05 16:54:47 -08005048 def_op_bytes = def_ad_bytes = 2;
5049 ctxt->ops->get_segment(ctxt, &dummy, &desc, NULL, VCPU_SREG_CS);
5050 if (desc.d)
5051 def_op_bytes = def_ad_bytes = 4;
5052 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005053 case X86EMUL_MODE_PROT16:
5054 def_op_bytes = def_ad_bytes = 2;
5055 break;
5056 case X86EMUL_MODE_PROT32:
5057 def_op_bytes = def_ad_bytes = 4;
5058 break;
5059#ifdef CONFIG_X86_64
5060 case X86EMUL_MODE_PROT64:
5061 def_op_bytes = 4;
5062 def_ad_bytes = 8;
5063 break;
5064#endif
5065 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005066 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005067 }
5068
Avi Kivity9dac77f2011-06-01 15:34:25 +03005069 ctxt->op_bytes = def_op_bytes;
5070 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005071
5072 /* Legacy prefixes. */
5073 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09005074 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005075 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005076 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005077 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005078 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005079 break;
5080 case 0x67: /* address-size override */
5081 if (mode == X86EMUL_MODE_PROT64)
5082 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005083 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005084 else
5085 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005086 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005087 break;
5088 case 0x26: /* ES override */
Marios Pomonis125ffc52019-12-11 12:47:50 -08005089 has_seg_override = true;
5090 ctxt->seg_override = VCPU_SREG_ES;
5091 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005092 case 0x2e: /* CS override */
Marios Pomonis125ffc52019-12-11 12:47:50 -08005093 has_seg_override = true;
5094 ctxt->seg_override = VCPU_SREG_CS;
5095 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005096 case 0x36: /* SS override */
Marios Pomonis125ffc52019-12-11 12:47:50 -08005097 has_seg_override = true;
5098 ctxt->seg_override = VCPU_SREG_SS;
5099 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005100 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04005101 has_seg_override = true;
Marios Pomonis125ffc52019-12-11 12:47:50 -08005102 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005103 break;
5104 case 0x64: /* FS override */
Marios Pomonis125ffc52019-12-11 12:47:50 -08005105 has_seg_override = true;
5106 ctxt->seg_override = VCPU_SREG_FS;
5107 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005108 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04005109 has_seg_override = true;
Marios Pomonis125ffc52019-12-11 12:47:50 -08005110 ctxt->seg_override = VCPU_SREG_GS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005111 break;
5112 case 0x40 ... 0x4f: /* REX */
5113 if (mode != X86EMUL_MODE_PROT64)
5114 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005115 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005116 continue;
5117 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005118 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005119 break;
5120 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005121 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005122 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005123 break;
5124 default:
5125 goto done_prefixes;
5126 }
5127
5128 /* Any legacy prefix after a REX prefix nullifies its effect. */
5129
Avi Kivity9dac77f2011-06-01 15:34:25 +03005130 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005131 }
5132
5133done_prefixes:
5134
5135 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005136 if (ctxt->rex_prefix & 8)
5137 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005138
5139 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005140 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08005141 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005142 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02005143 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09005144 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03005145 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005146
5147 /* 0F_38 opcode map */
5148 if (ctxt->b == 0x38) {
5149 ctxt->opcode_len = 3;
5150 ctxt->b = insn_fetch(u8, ctxt);
5151 opcode = opcode_map_0f_38[ctxt->b];
5152 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005153 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03005154 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005155
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09005156 if (ctxt->d & ModRM)
5157 ctxt->modrm = insn_fetch(u8, ctxt);
5158
Nadav Amit7fe864d2014-06-02 18:34:03 +03005159 /* vex-prefix instructions are not implemented */
5160 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
Nadav Amitd14cb5d2014-11-02 11:54:58 +02005161 (mode == X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) == 0xc0)) {
Nadav Amit7fe864d2014-06-02 18:34:03 +03005162 ctxt->d = NotImpl;
5163 }
5164
Avi Kivity9dac77f2011-06-01 15:34:25 +03005165 while (ctxt->d & GroupMask) {
5166 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03005167 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005168 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03005169 opcode = opcode.u.group[goffset];
5170 break;
5171 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005172 goffset = (ctxt->modrm >> 3) & 7;
5173 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03005174 opcode = opcode.u.gdual->mod3[goffset];
5175 else
5176 opcode = opcode.u.gdual->mod012[goffset];
5177 break;
5178 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005179 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02005180 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03005181 break;
5182 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005183 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005184 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005185 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03005186 switch (simd_prefix) {
5187 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
5188 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
5189 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
5190 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
5191 }
5192 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02005193 case Escape:
Marios Pomonis3c9053a22019-12-11 12:47:41 -08005194 if (ctxt->modrm > 0xbf) {
5195 size_t size = ARRAY_SIZE(opcode.u.esc->high);
5196 u32 index = array_index_nospec(
5197 ctxt->modrm - 0xc0, size);
5198
5199 opcode = opcode.u.esc->high[index];
5200 } else {
Gleb Natapov045a2822012-12-20 16:57:43 +02005201 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
Marios Pomonis3c9053a22019-12-11 12:47:41 -08005202 }
Gleb Natapov045a2822012-12-20 16:57:43 +02005203 break;
Nadav Amit39f062f2014-11-26 15:47:18 +02005204 case InstrDual:
5205 if ((ctxt->modrm >> 6) == 3)
5206 opcode = opcode.u.idual->mod3;
5207 else
5208 opcode = opcode.u.idual->mod012;
5209 break;
Nadav Amit2276b512015-01-26 09:32:24 +02005210 case ModeDual:
5211 if (ctxt->mode == X86EMUL_MODE_PROT64)
5212 opcode = opcode.u.mdual->mode64;
5213 else
5214 opcode = opcode.u.mdual->mode32;
5215 break;
Avi Kivity46561642011-04-24 14:09:59 +03005216 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005217 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005218 }
Avi Kivity46561642011-04-24 14:09:59 +03005219
Avi Kivityb1ea50b2011-09-13 10:45:42 +03005220 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005221 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005222 }
5223
Eric Hankland018d70f2021-11-30 15:42:21 +08005224 ctxt->is_branch = opcode.flags & IsBranch;
5225
Paolo Bonzinie24186e2014-03-27 12:00:57 +01005226 /* Unrecognised? */
5227 if (ctxt->d == 0)
5228 return EMULATION_FAILED;
5229
Avi Kivity9dac77f2011-06-01 15:34:25 +03005230 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005231
Wanpeng Lib35491e2021-05-27 17:01:37 -07005232 if (unlikely(emulation_type & EMULTYPE_TRAP_UD) &&
5233 likely(!(ctxt->d & EmulateOnUD)))
Nadav Amit3a6095a2014-08-13 16:50:13 +03005234 return EMULATION_FAILED;
5235
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005236 if (unlikely(ctxt->d &
Nadav Amited9aad22014-11-02 11:55:00 +02005237 (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch|
5238 No16))) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005239 /*
5240 * These are copied unconditionally here, and checked unconditionally
5241 * in x86_emulate_insn.
5242 */
5243 ctxt->check_perm = opcode.check_perm;
5244 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005245
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005246 if (ctxt->d & NotImpl)
5247 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02005248
Nadav Amit58b70752014-10-24 11:35:09 +03005249 if (mode == X86EMUL_MODE_PROT64) {
5250 if (ctxt->op_bytes == 4 && (ctxt->d & Stack))
5251 ctxt->op_bytes = 8;
5252 else if (ctxt->d & NearBranch)
5253 ctxt->op_bytes = 8;
5254 }
Avi Kivity7f9b4b72010-08-01 14:46:54 +03005255
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005256 if (ctxt->d & Op3264) {
5257 if (mode == X86EMUL_MODE_PROT64)
5258 ctxt->op_bytes = 8;
5259 else
5260 ctxt->op_bytes = 4;
5261 }
5262
Nadav Amited9aad22014-11-02 11:55:00 +02005263 if ((ctxt->d & No16) && ctxt->op_bytes == 2)
5264 ctxt->op_bytes = 4;
5265
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005266 if (ctxt->d & Sse)
5267 ctxt->op_bytes = 16;
5268 else if (ctxt->d & Mmx)
5269 ctxt->op_bytes = 8;
5270 }
Avi Kivity1253791d2011-03-29 11:41:27 +02005271
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005272 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005273 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03005274 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04005275 if (!has_seg_override) {
5276 has_seg_override = true;
5277 ctxt->seg_override = ctxt->modrm_seg;
5278 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03005279 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03005280 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005281 if (rc != X86EMUL_CONTINUE)
5282 goto done;
5283
Bandan Das573e80f2014-04-16 12:46:13 -04005284 if (!has_seg_override)
5285 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005286
Bandan Das573e80f2014-04-16 12:46:13 -04005287 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005288
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005289 /*
5290 * Decode and fetch the source operand: register, memory
5291 * or immediate.
5292 */
Avi Kivity0fe59122011-09-13 10:45:47 +03005293 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03005294 if (rc != X86EMUL_CONTINUE)
5295 goto done;
5296
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005297 /*
5298 * Decode and fetch the second source operand: register, memory
5299 * or immediate.
5300 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03005301 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03005302 if (rc != X86EMUL_CONTINUE)
5303 goto done;
5304
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005305 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03005306 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005307
Owen Hofmannd9092f52016-10-27 11:25:52 -07005308 if (ctxt->rip_relative && likely(ctxt->memopp))
Nadav Amit1c1c35a2014-11-19 17:43:09 +02005309 ctxt->memopp->addr.mem.ea = address_mask(ctxt,
5310 ctxt->memopp->addr.mem.ea + ctxt->_eip);
Avi Kivitycb16c342011-06-19 19:21:11 +03005311
Paolo Bonzinia430c912014-10-23 14:54:14 +02005312done:
Jan Dakinevichc8848ce2019-08-27 13:07:08 +00005313 if (rc == X86EMUL_PROPAGATE_FAULT)
5314 ctxt->have_exception = true;
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005315 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005316}
5317
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005318bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
5319{
5320 return ctxt->d & PageTable;
5321}
5322
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005323static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
5324{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005325 /* The second termination condition only applies for REPE
5326 * and REPNE. Test if the repeat string operation prefix is
5327 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
5328 * corresponding termination condition according to:
5329 * - if REPE/REPZ and ZF = 0 then done
5330 * - if REPNE/REPNZ and ZF = 1 then done
5331 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005332 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
5333 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
5334 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03005335 ((ctxt->eflags & X86_EFLAGS_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03005336 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03005337 ((ctxt->eflags & X86_EFLAGS_ZF) == X86_EFLAGS_ZF))))
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005338 return true;
5339
5340 return false;
5341}
5342
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005343static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
5344{
Radim Krčmářaabba3c2016-11-08 20:54:18 +01005345 int rc;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005346
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02005347 kvm_fpu_get();
Radim Krčmářaabba3c2016-11-08 20:54:18 +01005348 rc = asm_safe("fwait");
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02005349 kvm_fpu_put();
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005350
Radim Krčmářaabba3c2016-11-08 20:54:18 +01005351 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005352 return emulate_exception(ctxt, MF_VECTOR, 0, false);
5353
5354 return X86EMUL_CONTINUE;
5355}
5356
Sean Christophersonc0a21c32020-01-17 11:30:52 -08005357static void fetch_possible_mmx_operand(struct operand *op)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005358{
5359 if (op->type == OP_MM)
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02005360 kvm_read_mmx_reg(op->addr.mm, &op->mm_val);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005361}
5362
Sean Christopherson3009afc2020-01-21 20:43:39 -08005363static int fastop(struct x86_emulate_ctxt *ctxt, fastop_t fop)
Avi Kivitye28bbd42013-01-04 16:18:48 +02005364{
5365 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005366
Avi Kivityb9fa4092013-02-09 11:31:48 +02005367 if (!(ctxt->d & ByteOp))
5368 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005369
Peter Zijlstra1a29b5b2018-01-25 10:58:13 +01005370 asm("push %[flags]; popf; " CALL_NOSPEC " ; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005371 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
Peter Zijlstra1a29b5b2018-01-25 10:58:13 +01005372 [thunk_target]"+S"(fop), ASM_CALL_CONSTRAINT
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005373 : "c"(ctxt->src2.val));
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005374
Avi Kivitye28bbd42013-01-04 16:18:48 +02005375 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005376 if (!fop) /* exception is returned in fop variable */
5377 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02005378 return X86EMUL_CONTINUE;
5379}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005380
Bandan Das14985072014-04-16 12:46:09 -04005381void init_decode_cache(struct x86_emulate_ctxt *ctxt)
5382{
Bandan Das573e80f2014-04-16 12:46:13 -04005383 memset(&ctxt->rip_relative, 0,
5384 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04005385
Bandan Das14985072014-04-16 12:46:09 -04005386 ctxt->io_read.pos = 0;
5387 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04005388 ctxt->mem_read.end = 0;
5389}
5390
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09005391int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005392{
Mathias Krause0225fb52012-08-30 01:30:16 +02005393 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09005394 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005395 int saved_dst_type = ctxt->dst.type;
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005396 unsigned emul_flags;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005397
Avi Kivity9dac77f2011-06-01 15:34:25 +03005398 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04005399
Gleb Natapovd380a5e2010-02-10 14:21:36 +02005400 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005401 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02005402 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02005403 goto done;
5404 }
5405
Avi Kivity9dac77f2011-06-01 15:34:25 +03005406 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02005407 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03005408 goto done;
5409 }
5410
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005411 emul_flags = ctxt->ops->get_hflags(ctxt);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005412 if (unlikely(ctxt->d &
5413 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
5414 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
5415 (ctxt->d & Undefined)) {
5416 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005417 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005418 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005419
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005420 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
5421 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
5422 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02005423 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005424 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02005425
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005426 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
5427 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02005428 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005429 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02005430
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005431 if (ctxt->d & Mmx) {
5432 rc = flush_pending_x87_faults(ctxt);
5433 if (rc != X86EMUL_CONTINUE)
5434 goto done;
5435 /*
5436 * Now that we know the fpu is exception safe, we can fetch
5437 * operands from it.
5438 */
Sean Christophersonc0a21c32020-01-17 11:30:52 -08005439 fetch_possible_mmx_operand(&ctxt->src);
5440 fetch_possible_mmx_operand(&ctxt->src2);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005441 if (!(ctxt->d & Mov))
Sean Christophersonc0a21c32020-01-17 11:30:52 -08005442 fetch_possible_mmx_operand(&ctxt->dst);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005443 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02005444
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005445 if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && ctxt->intercept) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005446 rc = emulator_check_intercept(ctxt, ctxt->intercept,
5447 X86_ICPT_PRE_EXCEPT);
5448 if (rc != X86EMUL_CONTINUE)
5449 goto done;
5450 }
5451
Nadav Amit64a38292014-12-10 11:19:04 +02005452 /* Instruction can only be executed in protected mode */
5453 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
5454 rc = emulate_ud(ctxt);
5455 goto done;
5456 }
5457
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005458 /* Privileged instruction can be executed only in CPL=0 */
5459 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Nadav Amit68efa762014-06-18 17:19:35 +03005460 if (ctxt->d & PrivUD)
5461 rc = emulate_ud(ctxt);
5462 else
5463 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02005464 goto done;
5465 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005466
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005467 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04005468 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005469 rc = ctxt->check_perm(ctxt);
5470 if (rc != X86EMUL_CONTINUE)
5471 goto done;
5472 }
5473
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005474 if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005475 rc = emulator_check_intercept(ctxt, ctxt->intercept,
5476 X86_ICPT_POST_EXCEPT);
5477 if (rc != X86EMUL_CONTINUE)
5478 goto done;
5479 }
5480
5481 if (ctxt->rep_prefix && (ctxt->d & String)) {
5482 /* All REP prefixes have the same first termination condition */
5483 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Nadav Amit428e3d02015-04-28 13:06:01 +03005484 string_registers_quirk(ctxt);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005485 ctxt->eip = ctxt->_eip;
Nadav Amit0efb0442015-03-29 16:33:03 +03005486 ctxt->eflags &= ~X86_EFLAGS_RF;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005487 goto done;
5488 }
5489 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02005490 }
5491
Avi Kivity9dac77f2011-06-01 15:34:25 +03005492 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
5493 rc = segmented_read(ctxt, ctxt->src.addr.mem,
5494 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09005495 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005496 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005497 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005498 }
5499
Avi Kivity9dac77f2011-06-01 15:34:25 +03005500 if (ctxt->src2.type == OP_MEM) {
5501 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
5502 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02005503 if (rc != X86EMUL_CONTINUE)
5504 goto done;
5505 }
5506
Avi Kivity9dac77f2011-06-01 15:34:25 +03005507 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005508 goto special_insn;
5509
5510
Avi Kivity9dac77f2011-06-01 15:34:25 +03005511 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02005512 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005513 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
5514 &ctxt->dst.val, ctxt->dst.bytes);
Nadav Amitc205fb72014-12-25 02:52:16 +02005515 if (rc != X86EMUL_CONTINUE) {
Paolo Bonzinid44e1212015-02-09 10:02:05 +01005516 if (!(ctxt->d & NoWrite) &&
5517 rc == X86EMUL_PROPAGATE_FAULT &&
Nadav Amitc205fb72014-12-25 02:52:16 +02005518 ctxt->exception.vector == PF_VECTOR)
5519 ctxt->exception.error_code |= PFERR_WRITE_MASK;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02005520 goto done;
Nadav Amitc205fb72014-12-25 02:52:16 +02005521 }
Avi Kivity038e51d2007-01-22 20:40:40 -08005522 }
Paolo Bonzini4ff6f8e2015-02-12 17:04:47 +01005523 /* Copy full 64-bit value for CMPXCHG8B. */
5524 ctxt->dst.orig_val64 = ctxt->dst.val64;
Avi Kivity038e51d2007-01-22 20:40:40 -08005525
Avi Kivity018a98d2007-11-27 19:30:56 +02005526special_insn:
5527
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005528 if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03005529 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005530 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02005531 if (rc != X86EMUL_CONTINUE)
5532 goto done;
5533 }
5534
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03005535 if (ctxt->rep_prefix && (ctxt->d & String))
Nadav Amit0efb0442015-03-29 16:33:03 +03005536 ctxt->eflags |= X86_EFLAGS_RF;
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03005537 else
Nadav Amit0efb0442015-03-29 16:33:03 +03005538 ctxt->eflags &= ~X86_EFLAGS_RF;
Nadav Amit4467c3f2014-07-21 14:37:29 +03005539
Avi Kivity9dac77f2011-06-01 15:34:25 +03005540 if (ctxt->execute) {
Sean Christopherson3009afc2020-01-21 20:43:39 -08005541 if (ctxt->d & Fastop)
Qian Caib78a8552020-02-17 11:48:26 -05005542 rc = fastop(ctxt, ctxt->fop);
Sean Christopherson3009afc2020-01-21 20:43:39 -08005543 else
Miaohe Lin52db3692020-01-22 11:21:44 +08005544 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03005545 if (rc != X86EMUL_CONTINUE)
5546 goto done;
5547 goto writeback;
5548 }
5549
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02005550 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005551 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005552 else if (ctxt->opcode_len == 3)
5553 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005554
Avi Kivity9dac77f2011-06-01 15:34:25 +03005555 switch (ctxt->b) {
Gleb Natapovb2833e32009-04-12 13:36:30 +03005556 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005557 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005558 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005559 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03005560 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005561 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03005562 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03005563 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005564 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03005565 ctxt->dst.type = OP_NONE;
5566 else
5567 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09005568 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005569 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005570 switch (ctxt->op_bytes) {
5571 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
5572 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
5573 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005574 }
5575 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005576 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005577 rc = emulate_int(ctxt, 3);
5578 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005579 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005580 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005581 break;
5582 case 0xce: /* into */
Nadav Amit0efb0442015-03-29 16:33:03 +03005583 if (ctxt->eflags & X86_EFLAGS_OF)
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005584 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005585 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005586 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09005587 case 0xeb: /* jmp rel short */
Nadav Amit234f3ce2014-09-18 22:39:38 +03005588 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity9dac77f2011-06-01 15:34:25 +03005589 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005590 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005591 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03005592 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03005593 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005594 case 0xf5: /* cmc */
5595 /* complement carry flag from eflags reg */
Nadav Amit0efb0442015-03-29 16:33:03 +03005596 ctxt->eflags ^= X86_EFLAGS_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005597 break;
5598 case 0xf8: /* clc */
Nadav Amit0efb0442015-03-29 16:33:03 +03005599 ctxt->eflags &= ~X86_EFLAGS_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005600 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03005601 case 0xf9: /* stc */
Nadav Amit0efb0442015-03-29 16:33:03 +03005602 ctxt->eflags |= X86_EFLAGS_CF;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03005603 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005604 case 0xfc: /* cld */
Nadav Amit0efb0442015-03-29 16:33:03 +03005605 ctxt->eflags &= ~X86_EFLAGS_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005606 break;
5607 case 0xfd: /* std */
Nadav Amit0efb0442015-03-29 16:33:03 +03005608 ctxt->eflags |= X86_EFLAGS_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005609 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005610 default:
5611 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005612 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005613
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005614 if (rc != X86EMUL_CONTINUE)
5615 goto done;
5616
Avi Kivity018a98d2007-11-27 19:30:56 +02005617writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02005618 if (ctxt->d & SrcWrite) {
5619 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
5620 rc = writeback(ctxt, &ctxt->src);
5621 if (rc != X86EMUL_CONTINUE)
5622 goto done;
5623 }
Nadav Amitee212292014-06-15 16:12:58 +03005624 if (!(ctxt->d & NoWrite)) {
5625 rc = writeback(ctxt, &ctxt->dst);
5626 if (rc != X86EMUL_CONTINUE)
5627 goto done;
5628 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005629
Gleb Natapov5cd21912010-03-18 15:20:26 +02005630 /*
5631 * restore dst type in case the decoding will be reused
5632 * (happens for string instruction )
5633 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005634 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005635
Avi Kivity9dac77f2011-06-01 15:34:25 +03005636 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005637 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02005638
Avi Kivity9dac77f2011-06-01 15:34:25 +03005639 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005640 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02005641
Avi Kivity9dac77f2011-06-01 15:34:25 +03005642 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005643 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005644 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005645 if ((ctxt->d & SrcMask) == SrcSI)
5646 count = ctxt->src.count;
5647 else
5648 count = ctxt->dst.count;
Paolo Bonzini01485a22014-11-19 18:25:08 +01005649 register_address_increment(ctxt, VCPU_REGS_RCX, -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005650
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005651 if (!string_insn_completed(ctxt)) {
5652 /*
5653 * Re-enter guest when pio read ahead buffer is empty
5654 * or, if it is not used, after each 1024 iteration.
5655 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005656 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005657 (r->end == 0 || r->end != r->pos)) {
5658 /*
5659 * Reset read cache. Usually happens before
5660 * decode, but since instruction is restarted
5661 * we have to do it here.
5662 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005663 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005664 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005665 return EMULATION_RESTART;
5666 }
5667 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03005668 }
Nadav Amit0efb0442015-03-29 16:33:03 +03005669 ctxt->eflags &= ~X86_EFLAGS_RF;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005670 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005671
Avi Kivity9dac77f2011-06-01 15:34:25 +03005672 ctxt->eip = ctxt->_eip;
Paolo Bonzinifede8072020-04-27 11:55:59 -04005673 if (ctxt->mode != X86EMUL_MODE_PROT64)
5674 ctxt->eip = (u32)ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02005675
5676done:
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005677 if (rc == X86EMUL_PROPAGATE_FAULT) {
5678 WARN_ON(ctxt->exception.vector > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +02005679 ctxt->have_exception = true;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005680 }
Joerg Roedel775fde82011-04-04 12:39:24 +02005681 if (rc == X86EMUL_INTERCEPTED)
5682 return EMULATION_INTERCEPTED;
5683
Avi Kivitydd856ef2012-08-27 23:46:17 +03005684 if (rc == X86EMUL_CONTINUE)
5685 writeback_registers(ctxt);
5686
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005687 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005688
5689twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005690 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02005691 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02005692 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005693 break;
5694 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02005695 case 0x0d: /* GrpP (prefetch) */
5696 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02005697 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02005698 break;
5699 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005700 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02005701 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005702 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005703 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005704 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005705 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03005706 if (test_cc(ctxt->b, ctxt->eflags))
5707 ctxt->dst.val = ctxt->src.val;
Nadav Amitb91aa142015-03-30 15:39:19 +03005708 else if (ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005709 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08005710 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03005711 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03005712 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005713 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005714 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08005715 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005716 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08005717 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005718 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005719 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005720 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03005721 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005722 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005723 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005724 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005725 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03005726 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005727 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005728 default:
5729 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005730 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005731
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005732threebyte_insn:
5733
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005734 if (rc != X86EMUL_CONTINUE)
5735 goto done;
5736
Avi Kivity6aa8b732006-12-10 02:21:36 -08005737 goto writeback;
5738
5739cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02005740 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005741}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005742
5743void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
5744{
5745 invalidate_registers(ctxt);
5746}
5747
5748void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
5749{
5750 writeback_registers(ctxt);
5751}
Tom Lendacky0f89b202016-12-14 14:59:23 -05005752
5753bool emulator_can_use_gpa(struct x86_emulate_ctxt *ctxt)
5754{
5755 if (ctxt->rep_prefix && (ctxt->d & String))
5756 return false;
5757
5758 if (ctxt->d & TwoMemOp)
5759 return false;
5760
5761 return true;
5762}