blob: 63f9ca1c0ce063c68c6b1e36f26d1e046e49da2d [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 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800178
Avi Kivity820207c2013-02-09 11:31:45 +0200179#define DstXacc (DstAccLo | SrcAccHi | SrcWrite)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800180
Avi Kivityd0e53322010-07-29 15:11:54 +0300181#define X2(x...) x, x
182#define X3(x...) X2(x), x
183#define X4(x...) X2(x), X2(x)
184#define X5(x...) X4(x), x
185#define X6(x...) X4(x), X2(x)
186#define X7(x...) X4(x), X3(x)
187#define X8(x...) X4(x), X4(x)
188#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300189
Avi Kivitye28bbd42013-01-04 16:18:48 +0200190#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
191#define FASTOP_SIZE 8
192
Avi Kivityd65b1de2010-07-29 15:11:35 +0300193struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300194 u64 flags : 56;
195 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300196 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300197 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200198 const struct opcode *group;
199 const struct group_dual *gdual;
200 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200201 const struct escape *esc;
Nadav Amit39f062f2014-11-26 15:47:18 +0200202 const struct instr_dual *idual;
Nadav Amit2276b512015-01-26 09:32:24 +0200203 const struct mode_dual *mdual;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200204 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300205 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200206 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300207};
208
209struct group_dual {
210 struct opcode mod012[8];
211 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300212};
213
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200214struct gprefix {
215 struct opcode pfx_no;
216 struct opcode pfx_66;
217 struct opcode pfx_f2;
218 struct opcode pfx_f3;
219};
220
Gleb Natapov045a2822012-12-20 16:57:43 +0200221struct escape {
222 struct opcode op[8];
223 struct opcode high[64];
224};
225
Nadav Amit39f062f2014-11-26 15:47:18 +0200226struct instr_dual {
227 struct opcode mod012;
228 struct opcode mod3;
229};
230
Nadav Amit2276b512015-01-26 09:32:24 +0200231struct mode_dual {
232 struct opcode mode32;
233 struct opcode mode64;
234};
235
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300236#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300237
Nadav Amit3dc4bc42014-12-25 02:52:19 +0200238enum x86_transfer_type {
239 X86_TRANSFER_NONE,
240 X86_TRANSFER_CALL_JMP,
241 X86_TRANSFER_RET,
242 X86_TRANSFER_TASK_SWITCH,
243};
244
Avi Kivitydd856ef2012-08-27 23:46:17 +0300245static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
246{
247 if (!(ctxt->regs_valid & (1 << nr))) {
248 ctxt->regs_valid |= 1 << nr;
249 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
250 }
251 return ctxt->_regs[nr];
252}
253
254static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
255{
256 ctxt->regs_valid |= 1 << nr;
257 ctxt->regs_dirty |= 1 << nr;
258 return &ctxt->_regs[nr];
259}
260
261static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
262{
263 reg_read(ctxt, nr);
264 return reg_write(ctxt, nr);
265}
266
267static void writeback_registers(struct x86_emulate_ctxt *ctxt)
268{
269 unsigned reg;
270
271 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
272 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
273}
274
275static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
276{
277 ctxt->regs_dirty = 0;
278 ctxt->regs_valid = 0;
279}
280
Avi Kivity6aa8b732006-12-10 02:21:36 -0800281/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800282 * These EFLAGS bits are restored from saved value during emulation, and
283 * any changes are written back to the saved value after emulation.
284 */
Nadav Amit0efb0442015-03-29 16:33:03 +0300285#define EFLAGS_MASK (X86_EFLAGS_OF|X86_EFLAGS_SF|X86_EFLAGS_ZF|X86_EFLAGS_AF|\
286 X86_EFLAGS_PF|X86_EFLAGS_CF)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800287
Avi Kivitydda96d82008-11-26 15:14:10 +0200288#ifdef CONFIG_X86_64
289#define ON64(x) x
290#else
291#define ON64(x)
292#endif
293
Qian Caib78a8552020-02-17 11:48:26 -0500294/*
295 * fastop functions have a special calling convention:
296 *
297 * dst: rax (in/out)
298 * src: rdx (in/out)
299 * src2: rcx (in)
300 * flags: rflags (in/out)
301 * ex: rsi (in:fastop pointer, out:zero if exception)
302 *
303 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
304 * different operand sizes can be reached by calculation, rather than a jump
305 * table (which would be bigger than the code).
306 */
Sean Christopherson3009afc2020-01-21 20:43:39 -0800307static int fastop(struct x86_emulate_ctxt *ctxt, fastop_t fop);
Avi Kivity4d758342013-01-19 19:51:55 +0200308
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500309#define __FOP_FUNC(name) \
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600310 ".align " __stringify(FASTOP_SIZE) " \n\t" \
311 ".type " name ", @function \n\t" \
312 name ":\n\t"
313
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500314#define FOP_FUNC(name) \
315 __FOP_FUNC(#name)
316
317#define __FOP_RET(name) \
318 "ret \n\t" \
319 ".size " name ", .-" name "\n\t"
320
321#define FOP_RET(name) \
322 __FOP_RET(#name)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200323
324#define FOP_START(op) \
325 extern void em_##op(struct fastop *fake); \
326 asm(".pushsection .text, \"ax\" \n\t" \
327 ".global em_" #op " \n\t" \
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500328 ".align " __stringify(FASTOP_SIZE) " \n\t" \
329 "em_" #op ":\n\t"
Avi Kivityb7d491e2013-01-04 16:18:49 +0200330
331#define FOP_END \
332 ".popsection")
333
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500334#define __FOPNOP(name) \
335 __FOP_FUNC(name) \
336 __FOP_RET(name)
337
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600338#define FOPNOP() \
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500339 __FOPNOP(__stringify(__UNIQUE_ID(nop)))
Avi Kivity0bdea062013-01-19 19:51:50 +0200340
Avi Kivityb7d491e2013-01-04 16:18:49 +0200341#define FOP1E(op, dst) \
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500342 __FOP_FUNC(#op "_" #dst) \
343 "10: " #op " %" #dst " \n\t" \
344 __FOP_RET(#op "_" #dst)
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200345
346#define FOP1EEX(op, dst) \
347 FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200348
349#define FASTOP1(op) \
350 FOP_START(op) \
351 FOP1E(op##b, al) \
352 FOP1E(op##w, ax) \
353 FOP1E(op##l, eax) \
354 ON64(FOP1E(op##q, rax)) \
355 FOP_END
356
Avi Kivityb9fa4092013-02-09 11:31:48 +0200357/* 1-operand, using src2 (for MUL/DIV r/m) */
358#define FASTOP1SRC2(op, name) \
359 FOP_START(name) \
360 FOP1E(op, cl) \
361 FOP1E(op, cx) \
362 FOP1E(op, ecx) \
363 ON64(FOP1E(op, rcx)) \
364 FOP_END
365
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200366/* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */
367#define FASTOP1SRC2EX(op, name) \
368 FOP_START(name) \
369 FOP1EEX(op, cl) \
370 FOP1EEX(op, cx) \
371 FOP1EEX(op, ecx) \
372 ON64(FOP1EEX(op, rcx)) \
373 FOP_END
374
Avi Kivityf7857f32013-01-04 16:18:53 +0200375#define FOP2E(op, dst, src) \
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500376 __FOP_FUNC(#op "_" #dst "_" #src) \
377 #op " %" #src ", %" #dst " \n\t" \
378 __FOP_RET(#op "_" #dst "_" #src)
Avi Kivityf7857f32013-01-04 16:18:53 +0200379
380#define FASTOP2(op) \
381 FOP_START(op) \
Avi Kivity017da7b2013-02-09 11:31:47 +0200382 FOP2E(op##b, al, dl) \
383 FOP2E(op##w, ax, dx) \
384 FOP2E(op##l, eax, edx) \
385 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivityf7857f32013-01-04 16:18:53 +0200386 FOP_END
387
Avi Kivity11c363b2013-01-19 19:51:54 +0200388/* 2 operand, word only */
389#define FASTOP2W(op) \
390 FOP_START(op) \
391 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200392 FOP2E(op##w, ax, dx) \
393 FOP2E(op##l, eax, edx) \
394 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivity11c363b2013-01-19 19:51:54 +0200395 FOP_END
396
Avi Kivity007a3b52013-01-19 19:51:51 +0200397/* 2 operand, src is CL */
398#define FASTOP2CL(op) \
399 FOP_START(op) \
400 FOP2E(op##b, al, cl) \
401 FOP2E(op##w, ax, cl) \
402 FOP2E(op##l, eax, cl) \
403 ON64(FOP2E(op##q, rax, cl)) \
404 FOP_END
405
Nadav Amit5aca3722014-11-02 11:54:50 +0200406/* 2 operand, src and dest are reversed */
407#define FASTOP2R(op, name) \
408 FOP_START(name) \
409 FOP2E(op##b, dl, al) \
410 FOP2E(op##w, dx, ax) \
411 FOP2E(op##l, edx, eax) \
412 ON64(FOP2E(op##q, rdx, rax)) \
413 FOP_END
414
Avi Kivity0bdea062013-01-19 19:51:50 +0200415#define FOP3E(op, dst, src, src2) \
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500416 __FOP_FUNC(#op "_" #dst "_" #src "_" #src2) \
417 #op " %" #src2 ", %" #src ", %" #dst " \n\t"\
418 __FOP_RET(#op "_" #dst "_" #src "_" #src2)
Avi Kivity0bdea062013-01-19 19:51:50 +0200419
420/* 3-operand, word-only, src2=cl */
421#define FASTOP3WCL(op) \
422 FOP_START(op) \
423 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200424 FOP3E(op##w, ax, dx, cl) \
425 FOP3E(op##l, eax, edx, cl) \
426 ON64(FOP3E(op##q, rax, rdx, cl)) \
Avi Kivity0bdea062013-01-19 19:51:50 +0200427 FOP_END
428
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200429/* Special case for SETcc - 1 instruction per cc */
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600430#define FOP_SETCC(op) \
431 ".align 4 \n\t" \
432 ".type " #op ", @function \n\t" \
433 #op ": \n\t" \
434 #op " %al \n\t" \
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500435 __FOP_RET(#op)
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200436
Josh Poimboeuff26e6012017-10-04 10:39:05 -0500437asm(".pushsection .fixup, \"ax\"\n"
438 ".global kvm_fastop_exception \n"
439 "kvm_fastop_exception: xor %esi, %esi; ret\n"
440 ".popsection");
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200441
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200442FOP_START(setcc)
443FOP_SETCC(seto)
444FOP_SETCC(setno)
445FOP_SETCC(setc)
446FOP_SETCC(setnc)
447FOP_SETCC(setz)
448FOP_SETCC(setnz)
449FOP_SETCC(setbe)
450FOP_SETCC(setnbe)
451FOP_SETCC(sets)
452FOP_SETCC(setns)
453FOP_SETCC(setp)
454FOP_SETCC(setnp)
455FOP_SETCC(setl)
456FOP_SETCC(setnl)
457FOP_SETCC(setle)
458FOP_SETCC(setnle)
459FOP_END;
460
Josh Poimboeufd99a6ce2019-07-17 20:36:37 -0500461FOP_START(salc)
462FOP_FUNC(salc)
463"pushf; sbb %al, %al; popf \n\t"
464FOP_RET(salc)
Paolo Bonzini326f5782013-05-09 11:32:51 +0200465FOP_END;
466
Radim Krčmářaabba3c2016-11-08 20:54:18 +0100467/*
468 * XXX: inoutclob user must know where the argument is being expanded.
Masahiro Yamadae9666d12018-12-31 00:14:15 +0900469 * Relying on CONFIG_CC_HAS_ASM_GOTO would allow us to remove _fault.
Radim Krčmářaabba3c2016-11-08 20:54:18 +0100470 */
471#define asm_safe(insn, inoutclob...) \
472({ \
473 int _fault = 0; \
474 \
475 asm volatile("1:" insn "\n" \
476 "2:\n" \
477 ".pushsection .fixup, \"ax\"\n" \
478 "3: movl $1, %[_fault]\n" \
479 " jmp 2b\n" \
480 ".popsection\n" \
481 _ASM_EXTABLE(1b, 3b) \
482 : [_fault] "+qm"(_fault) inoutclob ); \
483 \
484 _fault ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE; \
485})
486
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200487static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
488 enum x86_intercept intercept,
489 enum x86_intercept_stage stage)
490{
491 struct x86_instruction_info info = {
492 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300493 .rep_prefix = ctxt->rep_prefix,
494 .modrm_mod = ctxt->modrm_mod,
495 .modrm_reg = ctxt->modrm_reg,
496 .modrm_rm = ctxt->modrm_rm,
497 .src_val = ctxt->src.val64,
Jan Kiszka6cbc5f52014-06-30 12:52:55 +0200498 .dst_val = ctxt->dst.val64,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300499 .src_bytes = ctxt->src.bytes,
500 .dst_bytes = ctxt->dst.bytes,
501 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200502 .next_rip = ctxt->eip,
503 };
504
Avi Kivity29535382011-04-20 13:37:53 +0300505 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200506}
507
Avi Kivityf47cfa32012-06-07 17:49:24 +0300508static void assign_masked(ulong *dest, ulong src, ulong mask)
509{
510 *dest = (*dest & ~mask) | (src & mask);
511}
512
Nadav Amit6fd8e122015-03-30 15:39:20 +0300513static void assign_register(unsigned long *reg, u64 val, int bytes)
514{
515 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
516 switch (bytes) {
517 case 1:
518 *(u8 *)reg = (u8)val;
519 break;
520 case 2:
521 *(u16 *)reg = (u16)val;
522 break;
523 case 4:
524 *reg = (u32)val;
525 break; /* 64b: zero-extend */
526 case 8:
527 *reg = val;
528 break;
529 }
530}
531
Avi Kivity9dac77f2011-06-01 15:34:25 +0300532static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800533{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300534 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800535}
536
Avi Kivityf47cfa32012-06-07 17:49:24 +0300537static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
538{
539 u16 sel;
540 struct desc_struct ss;
541
542 if (ctxt->mode == X86EMUL_MODE_PROT64)
543 return ~0UL;
544 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
545 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
546}
547
Avi Kivity612e89f2012-06-12 20:03:23 +0300548static int stack_size(struct x86_emulate_ctxt *ctxt)
549{
550 return (__fls(stack_mask(ctxt)) + 1) >> 3;
551}
552
Avi Kivity6aa8b732006-12-10 02:21:36 -0800553/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800554static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300555address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800556{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300557 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800558 return reg;
559 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300560 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800561}
562
563static inline unsigned long
Paolo Bonzini01485a22014-11-19 18:25:08 +0100564register_address(struct x86_emulate_ctxt *ctxt, int reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800565{
Paolo Bonzini01485a22014-11-19 18:25:08 +0100566 return address_mask(ctxt, reg_read(ctxt, reg));
Harvey Harrisone4706772008-02-19 07:40:38 -0800567}
568
Avi Kivity5ad105e2012-08-19 14:34:31 +0300569static void masked_increment(ulong *reg, ulong mask, int inc)
570{
571 assign_masked(reg, *reg + inc, mask);
572}
573
Harvey Harrison7a9572752008-02-19 07:40:41 -0800574static inline void
Paolo Bonzini01485a22014-11-19 18:25:08 +0100575register_address_increment(struct x86_emulate_ctxt *ctxt, int reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800576{
Nadav Amitee122a72015-04-28 13:06:00 +0300577 ulong *preg = reg_rmw(ctxt, reg);
Avi Kivity5ad105e2012-08-19 14:34:31 +0300578
Nadav Amitee122a72015-04-28 13:06:00 +0300579 assign_register(preg, *preg + inc, ctxt->ad_bytes);
Avi Kivity5ad105e2012-08-19 14:34:31 +0300580}
581
582static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
583{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300584 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800585}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800586
Avi Kivity56697682011-04-03 14:08:51 +0300587static u32 desc_limit_scaled(struct desc_struct *desc)
588{
589 u32 limit = get_desc_limit(desc);
590
591 return desc->g ? (limit << 12) | 0xfff : limit;
592}
593
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900594static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300595{
596 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
597 return 0;
598
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900599 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300600}
601
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200602static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
603 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300604{
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +0200605 WARN_ON(vec > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +0200606 ctxt->exception.vector = vec;
607 ctxt->exception.error_code = error;
608 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200609 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300610}
611
Joerg Roedel3b88e412011-04-04 12:39:29 +0200612static int emulate_db(struct x86_emulate_ctxt *ctxt)
613{
614 return emulate_exception(ctxt, DB_VECTOR, 0, false);
615}
616
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200617static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300618{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200619 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300620}
621
Avi Kivity618ff152011-04-03 12:32:09 +0300622static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
623{
624 return emulate_exception(ctxt, SS_VECTOR, err, true);
625}
626
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200627static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300628{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200629 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300630}
631
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200632static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300633{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200634 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300635}
636
Avi Kivity34d1f492010-08-26 11:59:01 +0300637static int emulate_de(struct x86_emulate_ctxt *ctxt)
638{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200639 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300640}
641
Avi Kivity1253791d2011-03-29 11:41:27 +0200642static int emulate_nm(struct x86_emulate_ctxt *ctxt)
643{
644 return emulate_exception(ctxt, NM_VECTOR, 0, false);
645}
646
Avi Kivity1aa36612011-04-27 13:20:30 +0300647static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
648{
649 u16 selector;
650 struct desc_struct desc;
651
652 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
653 return selector;
654}
655
656static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
657 unsigned seg)
658{
659 u16 dummy;
660 u32 base3;
661 struct desc_struct desc;
662
663 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
664 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
665}
666
Sean Christophersonf0ed4762020-02-18 15:29:43 -0800667static inline u8 ctxt_virt_addr_bits(struct x86_emulate_ctxt *ctxt)
668{
669 return (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_LA57) ? 57 : 48;
670}
671
672static inline bool emul_is_noncanonical_address(u64 la,
673 struct x86_emulate_ctxt *ctxt)
674{
675 return get_canonical(la, ctxt_virt_addr_bits(ctxt)) != la;
676}
677
Avi Kivity1c11b372012-04-09 18:39:59 +0300678/*
679 * x86 defines three classes of vector instructions: explicitly
680 * aligned, explicitly unaligned, and the rest, which change behaviour
681 * depending on whether they're AVX encoded or not.
682 *
683 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100684 * subject to the same check. FXSAVE and FXRSTOR are checked here too as their
685 * 512 bytes of data must be aligned to a 16 byte boundary.
Avi Kivity1c11b372012-04-09 18:39:59 +0300686 */
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100687static unsigned insn_alignment(struct x86_emulate_ctxt *ctxt, unsigned size)
Avi Kivity1c11b372012-04-09 18:39:59 +0300688{
Radim Krčmář48520182016-11-08 20:54:17 +0100689 u64 alignment = ctxt->d & AlignMask;
Avi Kivity1c11b372012-04-09 18:39:59 +0300690
Avi Kivity1c11b372012-04-09 18:39:59 +0300691 if (likely(size < 16))
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100692 return 1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300693
Radim Krčmář48520182016-11-08 20:54:17 +0100694 switch (alignment) {
695 case Unaligned:
696 case Avx:
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100697 return 1;
Radim Krčmář48520182016-11-08 20:54:17 +0100698 case Aligned16:
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100699 return 16;
Radim Krčmář48520182016-11-08 20:54:17 +0100700 case Aligned:
701 default:
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100702 return size;
Radim Krčmář48520182016-11-08 20:54:17 +0100703 }
Avi Kivity1c11b372012-04-09 18:39:59 +0300704}
705
Paolo Bonzinid09155d2014-10-27 14:54:44 +0100706static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
707 struct segmented_address addr,
708 unsigned *max_size, unsigned size,
709 bool write, bool fetch,
Nadav Amitd50eaa12014-11-19 17:43:11 +0200710 enum x86emul_mode mode, ulong *linear)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300711{
Avi Kivity618ff152011-04-03 12:32:09 +0300712 struct desc_struct desc;
713 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300714 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300715 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300716 u16 sel;
Yu Zhangfd8cb432017-08-24 20:27:56 +0800717 u8 va_bits;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300718
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900719 la = seg_base(ctxt, addr.seg) + addr.ea;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100720 *max_size = 0;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200721 switch (mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300722 case X86EMUL_MODE_PROT64:
Paolo Bonzini0c1d77f2016-02-19 18:07:21 +0100723 *linear = la;
Yu Zhangfd8cb432017-08-24 20:27:56 +0800724 va_bits = ctxt_virt_addr_bits(ctxt);
725 if (get_canonical(la, va_bits) != la)
Nadav Amitabc7d8a2014-11-19 17:43:12 +0200726 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100727
Yu Zhangfd8cb432017-08-24 20:27:56 +0800728 *max_size = min_t(u64, ~0u, (1ull << va_bits) - la);
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100729 if (size > *max_size)
730 goto bad;
Avi Kivity618ff152011-04-03 12:32:09 +0300731 break;
732 default:
Paolo Bonzini0c1d77f2016-02-19 18:07:21 +0100733 *linear = la = (u32)la;
Avi Kivity1aa36612011-04-27 13:20:30 +0300734 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
735 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300736 if (!usable)
737 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200738 /* code segment in protected mode or read-only data segment */
739 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
740 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300741 goto bad;
742 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400743 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300744 goto bad;
745 lim = desc_limit_scaled(&desc);
Paolo Bonzini997b0412014-11-19 18:33:38 +0100746 if (!(desc.type & 8) && (desc.type & 4)) {
Guo Chaofc058682012-06-28 15:19:51 +0800747 /* expand-down segment */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100748 if (addr.ea <= lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300749 goto bad;
750 lim = desc.d ? 0xffffffff : 0xffff;
Avi Kivity618ff152011-04-03 12:32:09 +0300751 }
Paolo Bonzini997b0412014-11-19 18:33:38 +0100752 if (addr.ea > lim)
753 goto bad;
Nadav Amitbac155312015-01-26 09:32:26 +0200754 if (lim == 0xffffffff)
755 *max_size = ~0u;
756 else {
757 *max_size = (u64)lim + 1 - addr.ea;
758 if (size > *max_size)
759 goto bad;
760 }
Avi Kivity618ff152011-04-03 12:32:09 +0300761 break;
762 }
Radim Krčmářd3fe9592016-11-08 20:54:16 +0100763 if (la & (insn_alignment(ctxt, size) - 1))
Avi Kivity1c11b372012-04-09 18:39:59 +0300764 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300765 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300766bad:
767 if (addr.seg == VCPU_SREG_SS)
Paolo Bonzini36061892014-10-27 14:40:49 +0100768 return emulate_ss(ctxt, 0);
Avi Kivity618ff152011-04-03 12:32:09 +0300769 else
Paolo Bonzini36061892014-10-27 14:40:49 +0100770 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300771}
772
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400773static int linearize(struct x86_emulate_ctxt *ctxt,
774 struct segmented_address addr,
775 unsigned size, bool write,
776 ulong *linear)
777{
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100778 unsigned max_size;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200779 return __linearize(ctxt, addr, &max_size, size, write, false,
780 ctxt->mode, linear);
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400781}
782
Nadav Amitd50eaa12014-11-19 17:43:11 +0200783static inline int assign_eip(struct x86_emulate_ctxt *ctxt, ulong dst,
784 enum x86emul_mode mode)
785{
786 ulong linear;
787 int rc;
788 unsigned max_size;
789 struct segmented_address addr = { .seg = VCPU_SREG_CS,
790 .ea = dst };
791
792 if (ctxt->op_bytes != sizeof(unsigned long))
793 addr.ea = dst & ((1UL << (ctxt->op_bytes << 3)) - 1);
794 rc = __linearize(ctxt, addr, &max_size, 1, false, true, mode, &linear);
795 if (rc == X86EMUL_CONTINUE)
796 ctxt->_eip = addr.ea;
797 return rc;
798}
799
800static inline int assign_eip_near(struct x86_emulate_ctxt *ctxt, ulong dst)
801{
802 return assign_eip(ctxt, dst, ctxt->mode);
803}
804
805static int assign_eip_far(struct x86_emulate_ctxt *ctxt, ulong dst,
806 const struct desc_struct *cs_desc)
807{
808 enum x86emul_mode mode = ctxt->mode;
Nadav Amit82268082015-01-26 09:32:27 +0200809 int rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200810
811#ifdef CONFIG_X86_64
Nadav Amit82268082015-01-26 09:32:27 +0200812 if (ctxt->mode >= X86EMUL_MODE_PROT16) {
813 if (cs_desc->l) {
814 u64 efer = 0;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200815
Nadav Amit82268082015-01-26 09:32:27 +0200816 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
817 if (efer & EFER_LMA)
818 mode = X86EMUL_MODE_PROT64;
819 } else
820 mode = X86EMUL_MODE_PROT32; /* temporary value */
Nadav Amitd50eaa12014-11-19 17:43:11 +0200821 }
822#endif
823 if (mode == X86EMUL_MODE_PROT16 || mode == X86EMUL_MODE_PROT32)
824 mode = cs_desc->d ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
Nadav Amit82268082015-01-26 09:32:27 +0200825 rc = assign_eip(ctxt, dst, mode);
826 if (rc == X86EMUL_CONTINUE)
827 ctxt->mode = mode;
828 return rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200829}
830
831static inline int jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
832{
833 return assign_eip_near(ctxt, ctxt->_eip + rel);
834}
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400835
Paolo Bonzini79367a62018-06-06 16:43:02 +0200836static int linear_read_system(struct x86_emulate_ctxt *ctxt, ulong linear,
837 void *data, unsigned size)
838{
Paolo Bonzini3c9fa242018-06-06 17:38:09 +0200839 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception, true);
Paolo Bonzini79367a62018-06-06 16:43:02 +0200840}
841
842static int linear_write_system(struct x86_emulate_ctxt *ctxt,
843 ulong linear, void *data,
844 unsigned int size)
845{
Paolo Bonzini3c9fa242018-06-06 17:38:09 +0200846 return ctxt->ops->write_std(ctxt, linear, data, size, &ctxt->exception, true);
Paolo Bonzini79367a62018-06-06 16:43:02 +0200847}
848
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200849static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
850 struct segmented_address addr,
851 void *data,
852 unsigned size)
853{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200854 int rc;
855 ulong linear;
856
Avi Kivity83b87952011-04-03 11:31:19 +0300857 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200858 if (rc != X86EMUL_CONTINUE)
859 return rc;
Paolo Bonzini3c9fa242018-06-06 17:38:09 +0200860 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception, false);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200861}
862
Steve Rutherford129a72a2017-01-11 18:28:29 -0800863static int segmented_write_std(struct x86_emulate_ctxt *ctxt,
864 struct segmented_address addr,
865 void *data,
866 unsigned int size)
867{
868 int rc;
869 ulong linear;
870
871 rc = linearize(ctxt, addr, size, true, &linear);
872 if (rc != X86EMUL_CONTINUE)
873 return rc;
Paolo Bonzini3c9fa242018-06-06 17:38:09 +0200874 return ctxt->ops->write_std(ctxt, linear, data, size, &ctxt->exception, false);
Steve Rutherford129a72a2017-01-11 18:28:29 -0800875}
876
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900877/*
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200878 * Prefetch the remaining bytes of the instruction without crossing page
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900879 * boundary if they are not in fetch_cache yet.
880 */
Paolo Bonzini9506d572014-05-06 13:05:25 +0200881static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
Avi Kivity62266862007-11-20 13:15:52 +0200882{
Avi Kivity62266862007-11-20 13:15:52 +0200883 int rc;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100884 unsigned size, max_size;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200885 unsigned long linear;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200886 int cur_size = ctxt->fetch.end - ctxt->fetch.data;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200887 struct segmented_address addr = { .seg = VCPU_SREG_CS,
Paolo Bonzini17052f12014-05-06 16:33:01 +0200888 .ea = ctxt->eip + cur_size };
889
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100890 /*
891 * We do not know exactly how many bytes will be needed, and
892 * __linearize is expensive, so fetch as much as possible. We
893 * just have to avoid going beyond the 15 byte limit, the end
894 * of the segment, or the end of the page.
895 *
896 * __linearize is called with size 0 so that it does not do any
897 * boundary check itself. Instead, we use max_size to check
898 * against op_size.
899 */
Nadav Amitd50eaa12014-11-19 17:43:11 +0200900 rc = __linearize(ctxt, addr, &max_size, 0, false, true, ctxt->mode,
901 &linear);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200902 if (unlikely(rc != X86EMUL_CONTINUE))
903 return rc;
904
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100905 size = min_t(unsigned, 15UL ^ cur_size, max_size);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200906 size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear));
Paolo Bonzini5cfc7e02014-05-06 13:05:25 +0200907
908 /*
909 * One instruction can only straddle two pages,
910 * and one has been loaded at the beginning of
911 * x86_decode_insn. So, if not enough bytes
912 * still, we must have hit the 15-byte boundary.
913 */
914 if (unlikely(size < op_size))
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100915 return emulate_gp(ctxt, 0);
916
Paolo Bonzini17052f12014-05-06 16:33:01 +0200917 rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end,
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200918 size, &ctxt->exception);
919 if (unlikely(rc != X86EMUL_CONTINUE))
920 return rc;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200921 ctxt->fetch.end += size;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900922 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200923}
924
Paolo Bonzini9506d572014-05-06 13:05:25 +0200925static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt,
926 unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200927{
Nadav Amit08da44a2014-10-03 01:10:04 +0300928 unsigned done_size = ctxt->fetch.end - ctxt->fetch.ptr;
929
930 if (unlikely(done_size < size))
931 return __do_insn_fetch_bytes(ctxt, size - done_size);
Paolo Bonzini9506d572014-05-06 13:05:25 +0200932 else
933 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200934}
935
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900936/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900937#define insn_fetch(_type, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200938({ _type _x; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200939 \
940 rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900941 if (rc != X86EMUL_CONTINUE) \
942 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200943 ctxt->_eip += sizeof(_type); \
Nick Desaulniers8616abc2017-06-27 19:37:14 -0700944 memcpy(&_x, ctxt->fetch.ptr, sizeof(_type)); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200945 ctxt->fetch.ptr += sizeof(_type); \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200946 _x; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900947})
948
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900949#define insn_fetch_arr(_arr, _size, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200950({ \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200951 rc = do_insn_fetch_bytes(_ctxt, _size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900952 if (rc != X86EMUL_CONTINUE) \
953 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200954 ctxt->_eip += (_size); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200955 memcpy(_arr, ctxt->fetch.ptr, _size); \
956 ctxt->fetch.ptr += (_size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900957})
958
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000959/*
960 * Given the 'reg' portion of a ModRM byte, and a register block, return a
961 * pointer into the block that addresses the relevant register.
962 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
963 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300964static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200965 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966{
967 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200968 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300971 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
972 else
973 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974 return p;
975}
976
977static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200978 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800979 u16 *size, unsigned long *address, int op_bytes)
980{
981 int rc;
982
983 if (op_bytes == 2)
984 op_bytes = 3;
985 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200986 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900987 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200989 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200990 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991 return rc;
992}
993
Avi Kivity34b77652013-01-19 19:51:56 +0200994FASTOP2(add);
995FASTOP2(or);
996FASTOP2(adc);
997FASTOP2(sbb);
998FASTOP2(and);
999FASTOP2(sub);
1000FASTOP2(xor);
1001FASTOP2(cmp);
1002FASTOP2(test);
1003
Avi Kivityb9fa4092013-02-09 11:31:48 +02001004FASTOP1SRC2(mul, mul_ex);
1005FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02001006FASTOP1SRC2EX(div, div_ex);
1007FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +02001008
Avi Kivity34b77652013-01-19 19:51:56 +02001009FASTOP3WCL(shld);
1010FASTOP3WCL(shrd);
1011
1012FASTOP2W(imul);
1013
1014FASTOP1(not);
1015FASTOP1(neg);
1016FASTOP1(inc);
1017FASTOP1(dec);
1018
1019FASTOP2CL(rol);
1020FASTOP2CL(ror);
1021FASTOP2CL(rcl);
1022FASTOP2CL(rcr);
1023FASTOP2CL(shl);
1024FASTOP2CL(shr);
1025FASTOP2CL(sar);
1026
1027FASTOP2W(bsf);
1028FASTOP2W(bsr);
1029FASTOP2W(bt);
1030FASTOP2W(bts);
1031FASTOP2W(btr);
1032FASTOP2W(btc);
1033
Avi Kivitye47a5f52013-02-09 11:31:51 +02001034FASTOP2(xadd);
1035
Nadav Amit5aca3722014-11-02 11:54:50 +02001036FASTOP2R(cmp, cmp_r);
1037
Nadav Amit900efe22015-03-30 15:39:21 +03001038static int em_bsf_c(struct x86_emulate_ctxt *ctxt)
1039{
1040 /* If src is zero, do not writeback, but update flags */
1041 if (ctxt->src.val == 0)
1042 ctxt->dst.type = OP_NONE;
1043 return fastop(ctxt, em_bsf);
1044}
1045
1046static int em_bsr_c(struct x86_emulate_ctxt *ctxt)
1047{
1048 /* If src is zero, do not writeback, but update flags */
1049 if (ctxt->src.val == 0)
1050 ctxt->dst.type = OP_NONE;
1051 return fastop(ctxt, em_bsr);
1052}
1053
Josh Poimboeufcb7390f2016-01-22 10:16:12 -06001054static __always_inline u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001055{
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001056 u8 rc;
1057 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001058
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001059 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Peter Zijlstra1a29b5b2018-01-25 10:58:13 +01001060 asm("push %[flags]; popf; " CALL_NOSPEC
1061 : "=a"(rc) : [thunk_target]"r"(fop), [flags]"r"(flags));
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001062 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001063}
1064
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001065static void fetch_register_operand(struct operand *op)
1066{
1067 switch (op->bytes) {
1068 case 1:
1069 op->val = *(u8 *)op->addr.reg;
1070 break;
1071 case 2:
1072 op->val = *(u16 *)op->addr.reg;
1073 break;
1074 case 4:
1075 op->val = *(u32 *)op->addr.reg;
1076 break;
1077 case 8:
1078 op->val = *(u64 *)op->addr.reg;
1079 break;
1080 }
1081}
1082
Gleb Natapov045a2822012-12-20 16:57:43 +02001083static int em_fninit(struct x86_emulate_ctxt *ctxt)
1084{
1085 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1086 return emulate_nm(ctxt);
1087
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02001088 kvm_fpu_get();
Gleb Natapov045a2822012-12-20 16:57:43 +02001089 asm volatile("fninit");
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02001090 kvm_fpu_put();
Gleb Natapov045a2822012-12-20 16:57:43 +02001091 return X86EMUL_CONTINUE;
1092}
1093
1094static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1095{
1096 u16 fcw;
1097
1098 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1099 return emulate_nm(ctxt);
1100
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02001101 kvm_fpu_get();
Gleb Natapov045a2822012-12-20 16:57:43 +02001102 asm volatile("fnstcw %0": "+m"(fcw));
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02001103 kvm_fpu_put();
Gleb Natapov045a2822012-12-20 16:57:43 +02001104
Gleb Natapov045a2822012-12-20 16:57:43 +02001105 ctxt->dst.val = fcw;
1106
1107 return X86EMUL_CONTINUE;
1108}
1109
1110static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1111{
1112 u16 fsw;
1113
1114 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1115 return emulate_nm(ctxt);
1116
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02001117 kvm_fpu_get();
Gleb Natapov045a2822012-12-20 16:57:43 +02001118 asm volatile("fnstsw %0": "+m"(fsw));
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02001119 kvm_fpu_put();
Gleb Natapov045a2822012-12-20 16:57:43 +02001120
Gleb Natapov045a2822012-12-20 16:57:43 +02001121 ctxt->dst.val = fsw;
1122
1123 return X86EMUL_CONTINUE;
1124}
1125
Avi Kivity1253791d2011-03-29 11:41:27 +02001126static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001127 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001128{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001129 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001130
Avi Kivity9dac77f2011-06-01 15:34:25 +03001131 if (!(ctxt->d & ModRM))
1132 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity1253791d2011-03-29 11:41:27 +02001133
Avi Kivity9dac77f2011-06-01 15:34:25 +03001134 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001135 op->type = OP_XMM;
1136 op->bytes = 16;
1137 op->addr.xmm = reg;
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02001138 kvm_read_sse_reg(reg, &op->vec_val);
Avi Kivity1253791d2011-03-29 11:41:27 +02001139 return;
1140 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001141 if (ctxt->d & Mmx) {
1142 reg &= 7;
1143 op->type = OP_MM;
1144 op->bytes = 8;
1145 op->addr.mm = reg;
1146 return;
1147 }
Avi Kivity1253791d2011-03-29 11:41:27 +02001148
Avi Kivity3c118e22007-10-31 10:27:04 +02001149 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001150 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1151 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1152
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001153 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001154 op->orig_val = op->val;
1155}
1156
Avi Kivitya6e34072012-06-10 17:15:39 +03001157static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1158{
1159 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1160 ctxt->modrm_seg = VCPU_SREG_SS;
1161}
1162
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001163static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001164 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001165{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001166 u8 sib;
Bandan Das02357bd2014-04-16 12:46:11 -04001167 int index_reg, base_reg, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001168 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001169 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001170
Bandan Das02357bd2014-04-16 12:46:11 -04001171 ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */
1172 index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */
1173 base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001174
Bandan Das02357bd2014-04-16 12:46:11 -04001175 ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001176 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
Bandan Das02357bd2014-04-16 12:46:11 -04001177 ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001178 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001179
Nadav Amit9b88ae92014-05-25 23:05:21 +03001180 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001181 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001182 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001183 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001184 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001185 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001186 op->type = OP_XMM;
1187 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001188 op->addr.xmm = ctxt->modrm_rm;
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02001189 kvm_read_sse_reg(ctxt->modrm_rm, &op->vec_val);
Avi Kivity1253791d2011-03-29 11:41:27 +02001190 return rc;
1191 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001192 if (ctxt->d & Mmx) {
1193 op->type = OP_MM;
1194 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001195 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001196 return rc;
1197 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001198 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001199 return rc;
1200 }
1201
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001202 op->type = OP_MEM;
1203
Avi Kivity9dac77f2011-06-01 15:34:25 +03001204 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001205 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1206 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1207 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1208 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001209
1210 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001211 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001212 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001213 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001214 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001215 break;
1216 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001217 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001218 break;
1219 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001220 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001221 break;
1222 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001223 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001224 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001225 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001226 break;
1227 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001228 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001229 break;
1230 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001231 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001232 break;
1233 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001234 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001235 break;
1236 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001237 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001238 break;
1239 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001240 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001241 break;
1242 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001243 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001244 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001245 break;
1246 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001247 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001248 break;
1249 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001250 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1251 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1252 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001253 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001254 } else {
1255 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001256 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001257 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001258 index_reg |= (sib >> 3) & 7;
1259 base_reg |= sib & 7;
1260 scale = sib >> 6;
1261
Avi Kivity9dac77f2011-06-01 15:34:25 +03001262 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001263 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001264 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001265 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001266 adjust_modrm_seg(ctxt, base_reg);
Nadav Amitab708092014-12-25 02:52:21 +02001267 /* Increment ESP on POP [ESP] */
1268 if ((ctxt->d & IncSP) &&
1269 base_reg == VCPU_REGS_RSP)
1270 modrm_ea += ctxt->op_bytes;
Avi Kivitya6e34072012-06-10 17:15:39 +03001271 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001272 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001273 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001274 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Nadav Amit5b38ab82014-11-02 11:54:41 +02001275 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity84411d82008-06-15 21:53:26 -07001276 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001277 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001278 } else {
1279 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001280 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001281 adjust_modrm_seg(ctxt, base_reg);
1282 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001283 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001284 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001285 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001286 break;
1287 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001288 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001289 break;
1290 }
1291 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001292 op->addr.mem.ea = modrm_ea;
Bandan Das41061cd2014-04-16 12:46:14 -04001293 if (ctxt->ad_bytes != 8)
1294 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
1295
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001296done:
1297 return rc;
1298}
1299
1300static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001301 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001302{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001303 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001304
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001305 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001306 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001307 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001308 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001309 break;
1310 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001311 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001312 break;
1313 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001314 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001315 break;
1316 }
1317done:
1318 return rc;
1319}
1320
Avi Kivity9dac77f2011-06-01 15:34:25 +03001321static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001322{
Sheng Yang7129eec2010-09-28 16:33:32 +08001323 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001324
Avi Kivity9dac77f2011-06-01 15:34:25 +03001325 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001326 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001327
Avi Kivity9dac77f2011-06-01 15:34:25 +03001328 if (ctxt->src.bytes == 2)
1329 sv = (s16)ctxt->src.val & (s16)mask;
1330 else if (ctxt->src.bytes == 4)
1331 sv = (s32)ctxt->src.val & (s32)mask;
Nadav Amit7dec5602014-06-15 16:12:57 +03001332 else
1333 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001334
Nadav Amit1c1c35a2014-11-19 17:43:09 +02001335 ctxt->dst.addr.mem.ea = address_mask(ctxt,
1336 ctxt->dst.addr.mem.ea + (sv >> 3));
Wei Yongjun35c843c2010-08-09 11:34:56 +08001337 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001338
1339 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001340 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001341}
1342
Gleb Natapov9de41572010-04-28 19:15:22 +03001343static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001344 unsigned long addr, void *dest, unsigned size)
1345{
1346 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001347 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001348
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001349 if (mc->pos < mc->end)
1350 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001351
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001352 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001353
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001354 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1355 &ctxt->exception);
1356 if (rc != X86EMUL_CONTINUE)
1357 return rc;
1358
1359 mc->end += size;
1360
1361read_cached:
1362 memcpy(dest, mc->data + mc->pos, size);
1363 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001364 return X86EMUL_CONTINUE;
1365}
1366
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001367static int segmented_read(struct x86_emulate_ctxt *ctxt,
1368 struct segmented_address addr,
1369 void *data,
1370 unsigned size)
1371{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001372 int rc;
1373 ulong linear;
1374
Avi Kivity83b87952011-04-03 11:31:19 +03001375 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001376 if (rc != X86EMUL_CONTINUE)
1377 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001378 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001379}
1380
1381static int segmented_write(struct x86_emulate_ctxt *ctxt,
1382 struct segmented_address addr,
1383 const void *data,
1384 unsigned size)
1385{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001386 int rc;
1387 ulong linear;
1388
Avi Kivity83b87952011-04-03 11:31:19 +03001389 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001390 if (rc != X86EMUL_CONTINUE)
1391 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001392 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1393 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001394}
1395
1396static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1397 struct segmented_address addr,
1398 const void *orig_data, const void *data,
1399 unsigned size)
1400{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001401 int rc;
1402 ulong linear;
1403
Avi Kivity83b87952011-04-03 11:31:19 +03001404 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001405 if (rc != X86EMUL_CONTINUE)
1406 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001407 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1408 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001409}
1410
Gleb Natapov7b262e92010-03-18 15:20:27 +02001411static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001412 unsigned int size, unsigned short port,
1413 void *dest)
1414{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001415 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001416
1417 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001418 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001419 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001420 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Nadav Amit0efb0442015-03-29 16:33:03 +03001421 in_page = (ctxt->eflags & X86_EFLAGS_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001422 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1423 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Mark Rustadb55a8142014-07-25 06:27:05 -07001424 n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001425 if (n == 0)
1426 n = 1;
1427 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001428 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001429 return 0;
1430 rc->end = n * size;
1431 }
1432
Nadav Amite6e39f02014-04-18 03:35:10 +03001433 if (ctxt->rep_prefix && (ctxt->d & String) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03001434 !(ctxt->eflags & X86_EFLAGS_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001435 ctxt->dst.data = rc->data + rc->pos;
1436 ctxt->dst.type = OP_MEM_STR;
1437 ctxt->dst.count = (rc->end - rc->pos) / size;
1438 rc->pos = rc->end;
1439 } else {
1440 memcpy(dest, rc->data + rc->pos, size);
1441 rc->pos += size;
1442 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001443 return 1;
1444}
1445
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001446static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1447 u16 index, struct desc_struct *desc)
1448{
1449 struct desc_ptr dt;
1450 ulong addr;
1451
1452 ctxt->ops->get_idt(ctxt, &dt);
1453
1454 if (dt.size < index * 8 + 7)
1455 return emulate_gp(ctxt, index << 3 | 0x2);
1456
1457 addr = dt.address + index * 8;
Jordan Borgner0e96f312018-10-28 12:58:28 +00001458 return linear_read_system(ctxt, addr, desc, sizeof(*desc));
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001459}
1460
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001461static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001462 u16 selector, struct desc_ptr *dt)
1463{
Mathias Krause0225fb52012-08-30 01:30:16 +02001464 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001465 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001466
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001467 if (selector & 1 << 2) {
1468 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001469 u16 sel;
1470
Jordan Borgner0e96f312018-10-28 12:58:28 +00001471 memset(dt, 0, sizeof(*dt));
Nadav Amit2eedcac2014-06-02 18:34:05 +03001472 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1473 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001474 return;
1475
1476 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001477 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001478 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001479 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001480}
1481
Nadav Amitedccda72014-12-25 02:52:23 +02001482static int get_descriptor_ptr(struct x86_emulate_ctxt *ctxt,
1483 u16 selector, ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001484{
1485 struct desc_ptr dt;
1486 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001487 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001488
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001489 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001490
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001491 if (dt.size < index * 8 + 7)
1492 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001493
1494 addr = dt.address + index * 8;
Nadav Amitedccda72014-12-25 02:52:23 +02001495
1496#ifdef CONFIG_X86_64
1497 if (addr >> 32 != 0) {
1498 u64 efer = 0;
1499
1500 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1501 if (!(efer & EFER_LMA))
1502 addr &= (u32)-1;
1503 }
1504#endif
1505
1506 *desc_addr_p = addr;
1507 return X86EMUL_CONTINUE;
1508}
1509
1510/* allowed just for 8 bytes segments */
1511static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1512 u16 selector, struct desc_struct *desc,
1513 ulong *desc_addr_p)
1514{
1515 int rc;
1516
1517 rc = get_descriptor_ptr(ctxt, selector, desc_addr_p);
1518 if (rc != X86EMUL_CONTINUE)
1519 return rc;
1520
Paolo Bonzini79367a62018-06-06 16:43:02 +02001521 return linear_read_system(ctxt, *desc_addr_p, desc, sizeof(*desc));
Nadav Amitedccda72014-12-25 02:52:23 +02001522}
1523
1524/* allowed just for 8 bytes segments */
1525static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1526 u16 selector, struct desc_struct *desc)
1527{
1528 int rc;
1529 ulong addr;
1530
1531 rc = get_descriptor_ptr(ctxt, selector, &addr);
1532 if (rc != X86EMUL_CONTINUE)
1533 return rc;
1534
Jordan Borgner0e96f312018-10-28 12:58:28 +00001535 return linear_write_system(ctxt, addr, desc, sizeof(*desc));
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001536}
1537
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001538static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Nadav Amitd1442d82014-09-18 22:39:39 +03001539 u16 selector, int seg, u8 cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001540 enum x86_transfer_type transfer,
Nadav Amitd1442d82014-09-18 22:39:39 +03001541 struct desc_struct *desc)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001542{
Avi Kivity869be992012-06-13 16:30:53 +03001543 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001544 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001545 unsigned err_vec = GP_VECTOR;
1546 u32 err_code = 0;
1547 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001548 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001549 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001550 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001551 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001552
Jordan Borgner0e96f312018-10-28 12:58:28 +00001553 memset(&seg_desc, 0, sizeof(seg_desc));
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001554
Kevin Wolff8da94e2013-04-11 14:06:03 +02001555 if (ctxt->mode == X86EMUL_MODE_REAL) {
1556 /* set real mode segment descriptor (keep limit etc. for
1557 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001558 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001559 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001560 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001561 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1562 /* VM86 needs a clean new segment descriptor */
1563 set_desc_base(&seg_desc, selector << 4);
1564 set_desc_limit(&seg_desc, 0xffff);
1565 seg_desc.type = 3;
1566 seg_desc.p = 1;
1567 seg_desc.s = 1;
1568 seg_desc.dpl = 3;
1569 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001570 }
1571
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001572 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001573
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001574 /* TR should be in GDT only */
1575 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1576 goto exception;
1577
Paolo Bonzini33ab9112017-01-12 15:02:32 +01001578 /* NULL selector is not valid for TR, CS and (except for long mode) SS */
1579 if (null_selector) {
1580 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_TR)
1581 goto exception;
1582
1583 if (seg == VCPU_SREG_SS) {
1584 if (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl)
1585 goto exception;
1586
1587 /*
1588 * ctxt->ops->set_segment expects the CPL to be in
1589 * SS.DPL, so fake an expand-up 32-bit data segment.
1590 */
1591 seg_desc.type = 3;
1592 seg_desc.p = 1;
1593 seg_desc.s = 1;
1594 seg_desc.dpl = cpl;
1595 seg_desc.d = 1;
1596 seg_desc.g = 1;
1597 }
1598
1599 /* Skip all following checks */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001600 goto load;
Paolo Bonzini33ab9112017-01-12 15:02:32 +01001601 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001602
Avi Kivitye9194642012-06-13 16:29:39 +03001603 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001604 if (ret != X86EMUL_CONTINUE)
1605 return ret;
1606
1607 err_code = selector & 0xfffc;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001608 err_vec = (transfer == X86_TRANSFER_TASK_SWITCH) ? TS_VECTOR :
1609 GP_VECTOR;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001610
Guo Chaofc058682012-06-28 15:19:51 +08001611 /* can't load system descriptor into segment selector */
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001612 if (seg <= VCPU_SREG_GS && !seg_desc.s) {
1613 if (transfer == X86_TRANSFER_CALL_JMP)
1614 return X86EMUL_UNHANDLEABLE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001615 goto exception;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001616 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001617
1618 if (!seg_desc.p) {
1619 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1620 goto exception;
1621 }
1622
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001623 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001624
1625 switch (seg) {
1626 case VCPU_SREG_SS:
1627 /*
1628 * segment is not a writable data segment or segment
1629 * selector's RPL != CPL or segment selector's RPL != CPL
1630 */
1631 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1632 goto exception;
1633 break;
1634 case VCPU_SREG_CS:
1635 if (!(seg_desc.type & 8))
1636 goto exception;
1637
1638 if (seg_desc.type & 4) {
1639 /* conforming */
1640 if (dpl > cpl)
1641 goto exception;
1642 } else {
1643 /* nonconforming */
1644 if (rpl > cpl || dpl != cpl)
1645 goto exception;
1646 }
Nadav Amit040c8dc2014-09-18 22:39:43 +03001647 /* in long-mode d/b must be clear if l is set */
1648 if (seg_desc.d && seg_desc.l) {
1649 u64 efer = 0;
1650
1651 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1652 if (efer & EFER_LMA)
1653 goto exception;
1654 }
1655
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001656 /* CS(RPL) <- CPL */
1657 selector = (selector & 0xfffc) | cpl;
1658 break;
1659 case VCPU_SREG_TR:
1660 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1661 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001662 old_desc = seg_desc;
1663 seg_desc.type |= 2; /* busy */
1664 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1665 sizeof(seg_desc), &ctxt->exception);
1666 if (ret != X86EMUL_CONTINUE)
1667 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001668 break;
1669 case VCPU_SREG_LDTR:
1670 if (seg_desc.s || seg_desc.type != 2)
1671 goto exception;
1672 break;
1673 default: /* DS, ES, FS, or GS */
1674 /*
1675 * segment is not a data or readable code segment or
1676 * ((segment is a data or nonconforming code segment)
1677 * and (both RPL and CPL > DPL))
1678 */
1679 if ((seg_desc.type & 0xa) == 0x8 ||
1680 (((seg_desc.type & 0xc) != 0xc) &&
1681 (rpl > dpl && cpl > dpl)))
1682 goto exception;
1683 break;
1684 }
1685
1686 if (seg_desc.s) {
1687 /* mark segment as accessed */
Nadav Amite2cefa72014-12-25 02:52:22 +02001688 if (!(seg_desc.type & 1)) {
1689 seg_desc.type |= 1;
1690 ret = write_segment_descriptor(ctxt, selector,
1691 &seg_desc);
1692 if (ret != X86EMUL_CONTINUE)
1693 return ret;
1694 }
Nadav Amite37a75a2014-06-02 18:34:04 +03001695 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
Paolo Bonzini79367a62018-06-06 16:43:02 +02001696 ret = linear_read_system(ctxt, desc_addr+8, &base3, sizeof(base3));
Nadav Amite37a75a2014-06-02 18:34:04 +03001697 if (ret != X86EMUL_CONTINUE)
1698 return ret;
Yu Zhangfd8cb432017-08-24 20:27:56 +08001699 if (emul_is_noncanonical_address(get_desc_base(&seg_desc) |
1700 ((u64)base3 << 32), ctxt))
Nadav Amit9a9abf62014-11-02 11:54:56 +02001701 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001702 }
1703load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001704 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Nadav Amitd1442d82014-09-18 22:39:39 +03001705 if (desc)
1706 *desc = seg_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001707 return X86EMUL_CONTINUE;
1708exception:
Paolo Bonzini592f0852014-08-20 10:05:08 +02001709 return emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001710}
1711
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001712static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1713 u16 selector, int seg)
1714{
1715 u8 cpl = ctxt->ops->cpl(ctxt);
Paolo Bonzini33ab9112017-01-12 15:02:32 +01001716
1717 /*
1718 * None of MOV, POP and LSS can load a NULL selector in CPL=3, but
1719 * they can load it at CPL<3 (Intel's manual says only LSS can,
1720 * but it's wrong).
1721 *
1722 * However, the Intel manual says that putting IST=1/DPL=3 in
1723 * an interrupt gate will result in SS=3 (the AMD manual instead
1724 * says it doesn't), so allow SS=3 in __load_segment_descriptor
1725 * and only forbid it here.
1726 */
1727 if (seg == VCPU_SREG_SS && selector == 3 &&
1728 ctxt->mode == X86EMUL_MODE_PROT64)
1729 return emulate_exception(ctxt, GP_VECTOR, 0, true);
1730
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001731 return __load_segment_descriptor(ctxt, selector, seg, cpl,
1732 X86_TRANSFER_NONE, NULL);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001733}
1734
Wei Yongjun31be40b2010-08-17 09:17:30 +08001735static void write_register_operand(struct operand *op)
1736{
Nadav Amit6fd8e122015-03-30 15:39:20 +03001737 return assign_register(op->addr.reg, op->val, op->bytes);
Wei Yongjun31be40b2010-08-17 09:17:30 +08001738}
1739
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001740static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001741{
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001742 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001743 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001744 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001745 break;
1746 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001747 if (ctxt->lock_prefix)
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001748 return segmented_cmpxchg(ctxt,
1749 op->addr.mem,
1750 &op->orig_val,
1751 &op->val,
1752 op->bytes);
1753 else
1754 return segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001755 op->addr.mem,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001756 &op->val,
1757 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001758 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001759 case OP_MEM_STR:
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001760 return segmented_write(ctxt,
1761 op->addr.mem,
1762 op->data,
1763 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001764 break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001765 case OP_XMM:
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02001766 kvm_write_sse_reg(op->addr.xmm, &op->vec_val);
Avi Kivity1253791d2011-03-29 11:41:27 +02001767 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001768 case OP_MM:
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02001769 kvm_write_mmx_reg(op->addr.mm, &op->mm_val);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001770 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001771 case OP_NONE:
1772 /* no writeback */
1773 break;
1774 default:
1775 break;
1776 }
1777 return X86EMUL_CONTINUE;
1778}
1779
Avi Kivity51ddff52012-06-12 20:19:40 +03001780static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001781{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001782 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001783
Avi Kivity5ad105e2012-08-19 14:34:31 +03001784 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001785 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001786 addr.seg = VCPU_SREG_SS;
1787
Avi Kivity51ddff52012-06-12 20:19:40 +03001788 return segmented_write(ctxt, addr, data, bytes);
1789}
1790
1791static int em_push(struct x86_emulate_ctxt *ctxt)
1792{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001793 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001794 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001795 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001796}
1797
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001798static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001799 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001800{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001801 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001802 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001803
Avi Kivitydd856ef2012-08-27 23:46:17 +03001804 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001805 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001806 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001807 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001808 return rc;
1809
Avi Kivity5ad105e2012-08-19 14:34:31 +03001810 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001811 return rc;
1812}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001813
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001814static int em_pop(struct x86_emulate_ctxt *ctxt)
1815{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001816 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001817}
1818
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001819static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001820 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001821{
1822 int rc;
1823 unsigned long val, change_mask;
Nadav Amit0efb0442015-03-29 16:33:03 +03001824 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001825 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001826
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001827 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001828 if (rc != X86EMUL_CONTINUE)
1829 return rc;
1830
Nadav Amit0efb0442015-03-29 16:33:03 +03001831 change_mask = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
1832 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF |
1833 X86_EFLAGS_TF | X86_EFLAGS_DF | X86_EFLAGS_NT |
1834 X86_EFLAGS_AC | X86_EFLAGS_ID;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001835
1836 switch(ctxt->mode) {
1837 case X86EMUL_MODE_PROT64:
1838 case X86EMUL_MODE_PROT32:
1839 case X86EMUL_MODE_PROT16:
1840 if (cpl == 0)
Nadav Amit0efb0442015-03-29 16:33:03 +03001841 change_mask |= X86_EFLAGS_IOPL;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001842 if (cpl <= iopl)
Nadav Amit0efb0442015-03-29 16:33:03 +03001843 change_mask |= X86_EFLAGS_IF;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001844 break;
1845 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001846 if (iopl < 3)
1847 return emulate_gp(ctxt, 0);
Nadav Amit0efb0442015-03-29 16:33:03 +03001848 change_mask |= X86_EFLAGS_IF;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001849 break;
1850 default: /* real mode */
Nadav Amit0efb0442015-03-29 16:33:03 +03001851 change_mask |= (X86_EFLAGS_IOPL | X86_EFLAGS_IF);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001852 break;
1853 }
1854
1855 *(unsigned long *)dest =
1856 (ctxt->eflags & ~change_mask) | (val & change_mask);
1857
1858 return rc;
1859}
1860
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001861static int em_popf(struct x86_emulate_ctxt *ctxt)
1862{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001863 ctxt->dst.type = OP_REG;
1864 ctxt->dst.addr.reg = &ctxt->eflags;
1865 ctxt->dst.bytes = ctxt->op_bytes;
1866 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001867}
1868
Avi Kivity612e89f2012-06-12 20:03:23 +03001869static int em_enter(struct x86_emulate_ctxt *ctxt)
1870{
1871 int rc;
1872 unsigned frame_size = ctxt->src.val;
1873 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001874 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001875
1876 if (nesting_level)
1877 return X86EMUL_UNHANDLEABLE;
1878
Avi Kivitydd856ef2012-08-27 23:46:17 +03001879 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1880 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001881 if (rc != X86EMUL_CONTINUE)
1882 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001883 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001884 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001885 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1886 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001887 stack_mask(ctxt));
1888 return X86EMUL_CONTINUE;
1889}
1890
Avi Kivityf47cfa32012-06-07 17:49:24 +03001891static int em_leave(struct x86_emulate_ctxt *ctxt)
1892{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001893 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001894 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001895 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001896}
1897
Avi Kivity1cd196e2011-09-13 10:45:51 +03001898static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001899{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001900 int seg = ctxt->src2.val;
1901
Avi Kivity9dac77f2011-06-01 15:34:25 +03001902 ctxt->src.val = get_segment_selector(ctxt, seg);
Nadav Amit0fcc2072014-11-02 11:54:51 +02001903 if (ctxt->op_bytes == 4) {
1904 rsp_increment(ctxt, -2);
1905 ctxt->op_bytes = 2;
1906 }
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001907
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001908 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001909}
1910
Avi Kivity1cd196e2011-09-13 10:45:51 +03001911static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001912{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001913 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001914 unsigned long selector;
1915 int rc;
1916
Nadav Amit3313bc42014-12-25 02:52:17 +02001917 rc = emulate_pop(ctxt, &selector, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001918 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001919 return rc;
1920
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001921 if (ctxt->modrm_reg == VCPU_SREG_SS)
1922 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Nadav Amit3313bc42014-12-25 02:52:17 +02001923 if (ctxt->op_bytes > 2)
1924 rsp_increment(ctxt, ctxt->op_bytes - 2);
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001925
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001926 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001927 return rc;
1928}
1929
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001930static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001931{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001932 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001933 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001934 int reg = VCPU_REGS_RAX;
1935
1936 while (reg <= VCPU_REGS_RDI) {
1937 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001938 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001939
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001940 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001941 if (rc != X86EMUL_CONTINUE)
1942 return rc;
1943
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001944 ++reg;
1945 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001946
Wei Yongjunc37eda12010-06-15 09:03:33 +08001947 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001948}
1949
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001950static int em_pushf(struct x86_emulate_ctxt *ctxt)
1951{
Nadav Amit0efb0442015-03-29 16:33:03 +03001952 ctxt->src.val = (unsigned long)ctxt->eflags & ~X86_EFLAGS_VM;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001953 return em_push(ctxt);
1954}
1955
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001956static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001957{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001958 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001959 int reg = VCPU_REGS_RDI;
Nadav Amit6fd8e122015-03-30 15:39:20 +03001960 u32 val;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001961
1962 while (reg >= VCPU_REGS_RAX) {
1963 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001964 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001965 --reg;
1966 }
1967
Nadav Amit6fd8e122015-03-30 15:39:20 +03001968 rc = emulate_pop(ctxt, &val, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001969 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001970 break;
Nadav Amit6fd8e122015-03-30 15:39:20 +03001971 assign_register(reg_rmw(ctxt, reg), val, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001972 --reg;
1973 }
1974 return rc;
1975}
1976
Avi Kivitydd856ef2012-08-27 23:46:17 +03001977static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001978{
Mathias Krause0225fb52012-08-30 01:30:16 +02001979 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001980 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001981 struct desc_ptr dt;
1982 gva_t cs_addr;
1983 gva_t eip_addr;
1984 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001985
1986 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001987 ctxt->src.val = ctxt->eflags;
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
Nadav Amit0efb0442015-03-29 16:33:03 +03001992 ctxt->eflags &= ~(X86_EFLAGS_IF | X86_EFLAGS_TF | X86_EFLAGS_AC);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001993
Avi Kivity9dac77f2011-06-01 15:34:25 +03001994 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001995 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001996 if (rc != X86EMUL_CONTINUE)
1997 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001998
Avi Kivity9dac77f2011-06-01 15:34:25 +03001999 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002000 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03002001 if (rc != X86EMUL_CONTINUE)
2002 return rc;
2003
Avi Kivity4bff1e862011-04-20 13:37:53 +03002004 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002005
2006 eip_addr = dt.address + (irq << 2);
2007 cs_addr = dt.address + (irq << 2) + 2;
2008
Paolo Bonzini79367a62018-06-06 16:43:02 +02002009 rc = linear_read_system(ctxt, cs_addr, &cs, 2);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002010 if (rc != X86EMUL_CONTINUE)
2011 return rc;
2012
Paolo Bonzini79367a62018-06-06 16:43:02 +02002013 rc = linear_read_system(ctxt, eip_addr, &eip, 2);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002014 if (rc != X86EMUL_CONTINUE)
2015 return rc;
2016
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002017 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002018 if (rc != X86EMUL_CONTINUE)
2019 return rc;
2020
Avi Kivity9dac77f2011-06-01 15:34:25 +03002021 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002022
2023 return rc;
2024}
2025
Avi Kivitydd856ef2012-08-27 23:46:17 +03002026int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
2027{
2028 int rc;
2029
2030 invalidate_registers(ctxt);
2031 rc = __emulate_int_real(ctxt, irq);
2032 if (rc == X86EMUL_CONTINUE)
2033 writeback_registers(ctxt);
2034 return rc;
2035}
2036
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002037static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002038{
2039 switch(ctxt->mode) {
2040 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03002041 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002042 case X86EMUL_MODE_VM86:
2043 case X86EMUL_MODE_PROT16:
2044 case X86EMUL_MODE_PROT32:
2045 case X86EMUL_MODE_PROT64:
2046 default:
2047 /* Protected mode interrupts unimplemented yet */
2048 return X86EMUL_UNHANDLEABLE;
2049 }
2050}
2051
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002052static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002053{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002054 int rc = X86EMUL_CONTINUE;
2055 unsigned long temp_eip = 0;
2056 unsigned long temp_eflags = 0;
2057 unsigned long cs = 0;
Nadav Amit0efb0442015-03-29 16:33:03 +03002058 unsigned long mask = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
2059 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_TF |
2060 X86_EFLAGS_IF | X86_EFLAGS_DF | X86_EFLAGS_OF |
2061 X86_EFLAGS_IOPL | X86_EFLAGS_NT | X86_EFLAGS_RF |
2062 X86_EFLAGS_AC | X86_EFLAGS_ID |
Wanpeng Li35fd68a2015-04-08 14:08:14 +08002063 X86_EFLAGS_FIXED;
Nadav Amit0efb0442015-03-29 16:33:03 +03002064 unsigned long vm86_mask = X86_EFLAGS_VM | X86_EFLAGS_VIF |
2065 X86_EFLAGS_VIP;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002066
2067 /* TODO: Add stack limit check */
2068
Avi Kivity9dac77f2011-06-01 15:34:25 +03002069 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002070
2071 if (rc != X86EMUL_CONTINUE)
2072 return rc;
2073
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002074 if (temp_eip & ~0xffff)
2075 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002076
Avi Kivity9dac77f2011-06-01 15:34:25 +03002077 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002078
2079 if (rc != X86EMUL_CONTINUE)
2080 return rc;
2081
Avi Kivity9dac77f2011-06-01 15:34:25 +03002082 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002083
2084 if (rc != X86EMUL_CONTINUE)
2085 return rc;
2086
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002087 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002088
2089 if (rc != X86EMUL_CONTINUE)
2090 return rc;
2091
Avi Kivity9dac77f2011-06-01 15:34:25 +03002092 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002093
Avi Kivity9dac77f2011-06-01 15:34:25 +03002094 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002095 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03002096 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002097 ctxt->eflags &= ~0xffff;
2098 ctxt->eflags |= temp_eflags;
2099 }
2100
2101 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
Wanpeng Li35fd68a2015-04-08 14:08:14 +08002102 ctxt->eflags |= X86_EFLAGS_FIXED;
Nadav Amit801806d2015-01-26 09:32:23 +02002103 ctxt->ops->set_nmi_mask(ctxt, false);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002104
2105 return rc;
2106}
2107
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002108static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002109{
2110 switch(ctxt->mode) {
2111 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002112 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002113 case X86EMUL_MODE_VM86:
2114 case X86EMUL_MODE_PROT16:
2115 case X86EMUL_MODE_PROT32:
2116 case X86EMUL_MODE_PROT64:
2117 default:
2118 /* iret from protected mode unimplemented yet */
2119 return X86EMUL_UNHANDLEABLE;
2120 }
2121}
2122
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002123static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2124{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002125 int rc;
Radim Krčmář2117d532016-11-23 21:15:00 +01002126 unsigned short sel;
2127 struct desc_struct new_desc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002128 u8 cpl = ctxt->ops->cpl(ctxt);
2129
Avi Kivity9dac77f2011-06-01 15:34:25 +03002130 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002131
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002132 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
2133 X86_TRANSFER_CALL_JMP,
Nadav Amitd1442d82014-09-18 22:39:39 +03002134 &new_desc);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002135 if (rc != X86EMUL_CONTINUE)
2136 return rc;
2137
Nadav Amitd50eaa12014-11-19 17:43:11 +02002138 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Radim Krčmář2117d532016-11-23 21:15:00 +01002139 /* Error handling is not implemented. */
2140 if (rc != X86EMUL_CONTINUE)
2141 return X86EMUL_UNHANDLEABLE;
2142
Nadav Amitd1442d82014-09-18 22:39:39 +03002143 return rc;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002144}
2145
Nadav Amitf7784042014-09-18 22:39:41 +03002146static int em_jmp_abs(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002147{
Nadav Amitf7784042014-09-18 22:39:41 +03002148 return assign_eip_near(ctxt, ctxt->src.val);
2149}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002150
Nadav Amitf7784042014-09-18 22:39:41 +03002151static int em_call_near_abs(struct x86_emulate_ctxt *ctxt)
2152{
2153 int rc;
2154 long int old_eip;
2155
2156 old_eip = ctxt->_eip;
2157 rc = assign_eip_near(ctxt, ctxt->src.val);
2158 if (rc != X86EMUL_CONTINUE)
2159 return rc;
2160 ctxt->src.val = old_eip;
2161 rc = em_push(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002162 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002163}
2164
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002165static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002166{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002167 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002168
Nadav Amitaaa05f22014-06-02 18:34:10 +03002169 if (ctxt->dst.bytes == 16)
2170 return X86EMUL_UNHANDLEABLE;
2171
Avi Kivitydd856ef2012-08-27 23:46:17 +03002172 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2173 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2174 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2175 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Nadav Amit0efb0442015-03-29 16:33:03 +03002176 ctxt->eflags &= ~X86_EFLAGS_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002177 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002178 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2179 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002180
Nadav Amit0efb0442015-03-29 16:33:03 +03002181 ctxt->eflags |= X86_EFLAGS_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002182 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002183 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002184}
2185
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002186static int em_ret(struct x86_emulate_ctxt *ctxt)
2187{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002188 int rc;
2189 unsigned long eip;
2190
2191 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
2192 if (rc != X86EMUL_CONTINUE)
2193 return rc;
2194
2195 return assign_eip_near(ctxt, eip);
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002196}
2197
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002198static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002199{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002200 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002201 unsigned long eip, cs;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002202 int cpl = ctxt->ops->cpl(ctxt);
Radim Krčmář2117d532016-11-23 21:15:00 +01002203 struct desc_struct new_desc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002204
2205 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002206 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002207 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002208 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002209 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002210 return rc;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002211 /* Outer-privilege level return is not implemented */
2212 if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2213 return X86EMUL_UNHANDLEABLE;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002214 rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, cpl,
2215 X86_TRANSFER_RET,
Nadav Amitd1442d82014-09-18 22:39:39 +03002216 &new_desc);
2217 if (rc != X86EMUL_CONTINUE)
2218 return rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +02002219 rc = assign_eip_far(ctxt, eip, &new_desc);
Radim Krčmář2117d532016-11-23 21:15:00 +01002220 /* Error handling is not implemented. */
2221 if (rc != X86EMUL_CONTINUE)
2222 return X86EMUL_UNHANDLEABLE;
2223
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002224 return rc;
2225}
2226
Bruce Rogers32611072013-09-09 09:40:20 -06002227static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2228{
2229 int rc;
2230
2231 rc = em_ret_far(ctxt);
2232 if (rc != X86EMUL_CONTINUE)
2233 return rc;
2234 rsp_increment(ctxt, ctxt->src.val);
2235 return X86EMUL_CONTINUE;
2236}
2237
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002238static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2239{
2240 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002241 ctxt->dst.orig_val = ctxt->dst.val;
2242 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002243 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002244 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002245 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002246
Nadav Amit0efb0442015-03-29 16:33:03 +03002247 if (ctxt->eflags & X86_EFLAGS_ZF) {
Nadav Amit2fcf5c82015-01-26 09:32:21 +02002248 /* Success: write back to memory; no update of EAX */
2249 ctxt->src.type = OP_NONE;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002250 ctxt->dst.val = ctxt->src.orig_val;
2251 } else {
2252 /* Failure: write the value we saw to EAX. */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02002253 ctxt->src.type = OP_REG;
2254 ctxt->src.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
2255 ctxt->src.val = ctxt->dst.orig_val;
2256 /* Create write-cycle to dest by writing the same value */
Nadav Amit37c564f2014-06-02 18:34:07 +03002257 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002258 }
2259 return X86EMUL_CONTINUE;
2260}
2261
Avi Kivityd4b43252011-09-13 10:45:50 +03002262static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002263{
Avi Kivityd4b43252011-09-13 10:45:50 +03002264 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002265 unsigned short sel;
2266 int rc;
2267
Avi Kivity9dac77f2011-06-01 15:34:25 +03002268 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002269
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002270 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002271 if (rc != X86EMUL_CONTINUE)
2272 return rc;
2273
Avi Kivity9dac77f2011-06-01 15:34:25 +03002274 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002275 return rc;
2276}
2277
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002278static int emulator_has_longmode(struct x86_emulate_ctxt *ctxt)
2279{
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002280#ifdef CONFIG_X86_64
Sean Christopherson5ae78e92019-12-17 13:32:38 -08002281 return ctxt->ops->guest_has_long_mode(ctxt);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002282#else
2283 return false;
2284#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002285}
2286
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002287static void rsm_set_desc_flags(struct desc_struct *desc, u32 flags)
2288{
2289 desc->g = (flags >> 23) & 1;
2290 desc->d = (flags >> 22) & 1;
2291 desc->l = (flags >> 21) & 1;
2292 desc->avl = (flags >> 20) & 1;
2293 desc->p = (flags >> 15) & 1;
2294 desc->dpl = (flags >> 13) & 3;
2295 desc->s = (flags >> 12) & 1;
2296 desc->type = (flags >> 8) & 15;
2297}
2298
Sean Christophersoned193212019-04-02 08:03:09 -07002299static int rsm_load_seg_32(struct x86_emulate_ctxt *ctxt, const char *smstate,
2300 int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002301{
2302 struct desc_struct desc;
2303 int offset;
2304 u16 selector;
2305
Sean Christophersoned193212019-04-02 08:03:09 -07002306 selector = GET_SMSTATE(u32, smstate, 0x7fa8 + n * 4);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002307
2308 if (n < 3)
2309 offset = 0x7f84 + n * 12;
2310 else
2311 offset = 0x7f2c + (n - 3) * 12;
2312
Sean Christophersoned193212019-04-02 08:03:09 -07002313 set_desc_base(&desc, GET_SMSTATE(u32, smstate, offset + 8));
2314 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, offset + 4));
2315 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smstate, offset));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002316 ctxt->ops->set_segment(ctxt, selector, &desc, 0, n);
2317 return X86EMUL_CONTINUE;
2318}
2319
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002320#ifdef CONFIG_X86_64
Sean Christophersoned193212019-04-02 08:03:09 -07002321static int rsm_load_seg_64(struct x86_emulate_ctxt *ctxt, const char *smstate,
2322 int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002323{
2324 struct desc_struct desc;
2325 int offset;
2326 u16 selector;
2327 u32 base3;
2328
2329 offset = 0x7e00 + n * 16;
2330
Sean Christophersoned193212019-04-02 08:03:09 -07002331 selector = GET_SMSTATE(u16, smstate, offset);
2332 rsm_set_desc_flags(&desc, GET_SMSTATE(u16, smstate, offset + 2) << 8);
2333 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, offset + 4));
2334 set_desc_base(&desc, GET_SMSTATE(u32, smstate, offset + 8));
2335 base3 = GET_SMSTATE(u32, smstate, offset + 12);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002336
2337 ctxt->ops->set_segment(ctxt, selector, &desc, base3, n);
2338 return X86EMUL_CONTINUE;
2339}
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002340#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002341
2342static int rsm_enter_protected_mode(struct x86_emulate_ctxt *ctxt,
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002343 u64 cr0, u64 cr3, u64 cr4)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002344{
2345 int bad;
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002346 u64 pcid;
2347
2348 /* In order to later set CR4.PCIDE, CR3[11:0] must be zero. */
2349 pcid = 0;
2350 if (cr4 & X86_CR4_PCIDE) {
2351 pcid = cr3 & 0xfff;
2352 cr3 &= ~0xfff;
2353 }
2354
2355 bad = ctxt->ops->set_cr(ctxt, 3, cr3);
2356 if (bad)
2357 return X86EMUL_UNHANDLEABLE;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002358
2359 /*
2360 * First enable PAE, long mode needs it before CR0.PG = 1 is set.
2361 * Then enable protected mode. However, PCID cannot be enabled
2362 * if EFER.LMA=0, so set it separately.
2363 */
2364 bad = ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PCIDE);
2365 if (bad)
2366 return X86EMUL_UNHANDLEABLE;
2367
2368 bad = ctxt->ops->set_cr(ctxt, 0, cr0);
2369 if (bad)
2370 return X86EMUL_UNHANDLEABLE;
2371
2372 if (cr4 & X86_CR4_PCIDE) {
2373 bad = ctxt->ops->set_cr(ctxt, 4, cr4);
2374 if (bad)
2375 return X86EMUL_UNHANDLEABLE;
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002376 if (pcid) {
2377 bad = ctxt->ops->set_cr(ctxt, 3, cr3 | pcid);
2378 if (bad)
2379 return X86EMUL_UNHANDLEABLE;
2380 }
2381
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002382 }
2383
2384 return X86EMUL_CONTINUE;
2385}
2386
Sean Christophersoned193212019-04-02 08:03:09 -07002387static int rsm_load_state_32(struct x86_emulate_ctxt *ctxt,
2388 const char *smstate)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002389{
2390 struct desc_struct desc;
2391 struct desc_ptr dt;
2392 u16 selector;
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002393 u32 val, cr0, cr3, cr4;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002394 int i;
2395
Sean Christophersoned193212019-04-02 08:03:09 -07002396 cr0 = GET_SMSTATE(u32, smstate, 0x7ffc);
2397 cr3 = GET_SMSTATE(u32, smstate, 0x7ff8);
2398 ctxt->eflags = GET_SMSTATE(u32, smstate, 0x7ff4) | X86_EFLAGS_FIXED;
2399 ctxt->_eip = GET_SMSTATE(u32, smstate, 0x7ff0);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002400
2401 for (i = 0; i < 8; i++)
Sean Christophersoned193212019-04-02 08:03:09 -07002402 *reg_write(ctxt, i) = GET_SMSTATE(u32, smstate, 0x7fd0 + i * 4);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002403
Sean Christophersoned193212019-04-02 08:03:09 -07002404 val = GET_SMSTATE(u32, smstate, 0x7fcc);
Maxim Levitsky37f66bb2020-08-27 20:11:44 +03002405
Sean Christopherson16d51632021-02-04 17:24:57 -08002406 if (ctxt->ops->set_dr(ctxt, 6, val))
Maxim Levitsky37f66bb2020-08-27 20:11:44 +03002407 return X86EMUL_UNHANDLEABLE;
2408
Sean Christophersoned193212019-04-02 08:03:09 -07002409 val = GET_SMSTATE(u32, smstate, 0x7fc8);
Maxim Levitsky37f66bb2020-08-27 20:11:44 +03002410
Sean Christopherson16d51632021-02-04 17:24:57 -08002411 if (ctxt->ops->set_dr(ctxt, 7, val))
Maxim Levitsky37f66bb2020-08-27 20:11:44 +03002412 return X86EMUL_UNHANDLEABLE;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002413
Sean Christophersoned193212019-04-02 08:03:09 -07002414 selector = GET_SMSTATE(u32, smstate, 0x7fc4);
2415 set_desc_base(&desc, GET_SMSTATE(u32, smstate, 0x7f64));
2416 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, 0x7f60));
2417 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smstate, 0x7f5c));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002418 ctxt->ops->set_segment(ctxt, selector, &desc, 0, VCPU_SREG_TR);
2419
Sean Christophersoned193212019-04-02 08:03:09 -07002420 selector = GET_SMSTATE(u32, smstate, 0x7fc0);
2421 set_desc_base(&desc, GET_SMSTATE(u32, smstate, 0x7f80));
2422 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, 0x7f7c));
2423 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smstate, 0x7f78));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002424 ctxt->ops->set_segment(ctxt, selector, &desc, 0, VCPU_SREG_LDTR);
2425
Sean Christophersoned193212019-04-02 08:03:09 -07002426 dt.address = GET_SMSTATE(u32, smstate, 0x7f74);
2427 dt.size = GET_SMSTATE(u32, smstate, 0x7f70);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002428 ctxt->ops->set_gdt(ctxt, &dt);
2429
Sean Christophersoned193212019-04-02 08:03:09 -07002430 dt.address = GET_SMSTATE(u32, smstate, 0x7f58);
2431 dt.size = GET_SMSTATE(u32, smstate, 0x7f54);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002432 ctxt->ops->set_idt(ctxt, &dt);
2433
2434 for (i = 0; i < 6; i++) {
Sean Christophersoned193212019-04-02 08:03:09 -07002435 int r = rsm_load_seg_32(ctxt, smstate, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002436 if (r != X86EMUL_CONTINUE)
2437 return r;
2438 }
2439
Sean Christophersoned193212019-04-02 08:03:09 -07002440 cr4 = GET_SMSTATE(u32, smstate, 0x7f14);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002441
Sean Christophersoned193212019-04-02 08:03:09 -07002442 ctxt->ops->set_smbase(ctxt, GET_SMSTATE(u32, smstate, 0x7ef8));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002443
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002444 return rsm_enter_protected_mode(ctxt, cr0, cr3, cr4);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002445}
2446
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002447#ifdef CONFIG_X86_64
Sean Christophersoned193212019-04-02 08:03:09 -07002448static int rsm_load_state_64(struct x86_emulate_ctxt *ctxt,
2449 const char *smstate)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002450{
2451 struct desc_struct desc;
2452 struct desc_ptr dt;
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002453 u64 val, cr0, cr3, cr4;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002454 u32 base3;
2455 u16 selector;
Paolo Bonzinib10d92a2015-10-14 15:25:52 +02002456 int i, r;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002457
2458 for (i = 0; i < 16; i++)
Sean Christophersoned193212019-04-02 08:03:09 -07002459 *reg_write(ctxt, i) = GET_SMSTATE(u64, smstate, 0x7ff8 - i * 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002460
Sean Christophersoned193212019-04-02 08:03:09 -07002461 ctxt->_eip = GET_SMSTATE(u64, smstate, 0x7f78);
2462 ctxt->eflags = GET_SMSTATE(u32, smstate, 0x7f70) | X86_EFLAGS_FIXED;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002463
Sean Christopherson26443122021-02-04 17:24:58 -08002464 val = GET_SMSTATE(u64, smstate, 0x7f68);
Maxim Levitsky37f66bb2020-08-27 20:11:44 +03002465
Sean Christopherson16d51632021-02-04 17:24:57 -08002466 if (ctxt->ops->set_dr(ctxt, 6, val))
Maxim Levitsky37f66bb2020-08-27 20:11:44 +03002467 return X86EMUL_UNHANDLEABLE;
2468
Sean Christopherson26443122021-02-04 17:24:58 -08002469 val = GET_SMSTATE(u64, smstate, 0x7f60);
Maxim Levitsky37f66bb2020-08-27 20:11:44 +03002470
Sean Christopherson16d51632021-02-04 17:24:57 -08002471 if (ctxt->ops->set_dr(ctxt, 7, val))
Maxim Levitsky37f66bb2020-08-27 20:11:44 +03002472 return X86EMUL_UNHANDLEABLE;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002473
Sean Christophersoned193212019-04-02 08:03:09 -07002474 cr0 = GET_SMSTATE(u64, smstate, 0x7f58);
2475 cr3 = GET_SMSTATE(u64, smstate, 0x7f50);
2476 cr4 = GET_SMSTATE(u64, smstate, 0x7f48);
2477 ctxt->ops->set_smbase(ctxt, GET_SMSTATE(u32, smstate, 0x7f00));
2478 val = GET_SMSTATE(u64, smstate, 0x7ed0);
Maxim Levitsky37f66bb2020-08-27 20:11:44 +03002479
2480 if (ctxt->ops->set_msr(ctxt, MSR_EFER, val & ~EFER_LMA))
2481 return X86EMUL_UNHANDLEABLE;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002482
Sean Christophersoned193212019-04-02 08:03:09 -07002483 selector = GET_SMSTATE(u32, smstate, 0x7e90);
2484 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smstate, 0x7e92) << 8);
2485 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, 0x7e94));
2486 set_desc_base(&desc, GET_SMSTATE(u32, smstate, 0x7e98));
2487 base3 = GET_SMSTATE(u32, smstate, 0x7e9c);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002488 ctxt->ops->set_segment(ctxt, selector, &desc, base3, VCPU_SREG_TR);
2489
Sean Christophersoned193212019-04-02 08:03:09 -07002490 dt.size = GET_SMSTATE(u32, smstate, 0x7e84);
2491 dt.address = GET_SMSTATE(u64, smstate, 0x7e88);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002492 ctxt->ops->set_idt(ctxt, &dt);
2493
Sean Christophersoned193212019-04-02 08:03:09 -07002494 selector = GET_SMSTATE(u32, smstate, 0x7e70);
2495 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smstate, 0x7e72) << 8);
2496 set_desc_limit(&desc, GET_SMSTATE(u32, smstate, 0x7e74));
2497 set_desc_base(&desc, GET_SMSTATE(u32, smstate, 0x7e78));
2498 base3 = GET_SMSTATE(u32, smstate, 0x7e7c);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002499 ctxt->ops->set_segment(ctxt, selector, &desc, base3, VCPU_SREG_LDTR);
2500
Sean Christophersoned193212019-04-02 08:03:09 -07002501 dt.size = GET_SMSTATE(u32, smstate, 0x7e64);
2502 dt.address = GET_SMSTATE(u64, smstate, 0x7e68);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002503 ctxt->ops->set_gdt(ctxt, &dt);
2504
Paolo Bonzinifae1a3e2017-12-21 00:49:14 +01002505 r = rsm_enter_protected_mode(ctxt, cr0, cr3, cr4);
Paolo Bonzinib10d92a2015-10-14 15:25:52 +02002506 if (r != X86EMUL_CONTINUE)
2507 return r;
2508
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002509 for (i = 0; i < 6; i++) {
Sean Christophersoned193212019-04-02 08:03:09 -07002510 r = rsm_load_seg_64(ctxt, smstate, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002511 if (r != X86EMUL_CONTINUE)
2512 return r;
2513 }
2514
Paolo Bonzinib10d92a2015-10-14 15:25:52 +02002515 return X86EMUL_CONTINUE;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002516}
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002517#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002518
Paolo Bonzini64d60672015-05-07 11:36:11 +02002519static int em_rsm(struct x86_emulate_ctxt *ctxt)
2520{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002521 unsigned long cr0, cr4, efer;
Sean Christophersoned193212019-04-02 08:03:09 -07002522 char buf[512];
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002523 u64 smbase;
2524 int ret;
2525
Ladi Prosek6ed071f2017-04-25 16:42:44 +02002526 if ((ctxt->ops->get_hflags(ctxt) & X86EMUL_SMM_MASK) == 0)
Paolo Bonzini64d60672015-05-07 11:36:11 +02002527 return emulate_ud(ctxt);
2528
Sean Christophersoned193212019-04-02 08:03:09 -07002529 smbase = ctxt->ops->get_smbase(ctxt);
2530
2531 ret = ctxt->ops->read_phys(ctxt, smbase + 0xfe00, buf, sizeof(buf));
2532 if (ret != X86EMUL_CONTINUE)
2533 return X86EMUL_UNHANDLEABLE;
2534
Sean Christopherson9ec19492019-04-02 08:03:11 -07002535 if ((ctxt->ops->get_hflags(ctxt) & X86EMUL_SMM_INSIDE_NMI_MASK) == 0)
2536 ctxt->ops->set_nmi_mask(ctxt, false);
2537
2538 ctxt->ops->set_hflags(ctxt, ctxt->ops->get_hflags(ctxt) &
2539 ~(X86EMUL_SMM_INSIDE_NMI_MASK | X86EMUL_SMM_MASK));
2540
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002541 /*
2542 * Get back to real mode, to prepare a safe state in which to load
Paolo Bonzini89651a32015-11-03 13:43:05 +01002543 * CR0/CR3/CR4/EFER. It's all a bit more complicated if the vCPU
2544 * supports long mode.
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002545 */
Paolo Bonzini89651a32015-11-03 13:43:05 +01002546 if (emulator_has_longmode(ctxt)) {
2547 struct desc_struct cs_desc;
2548
2549 /* Zero CR4.PCIDE before CR0.PG. */
Sean Christopherson8f4dc2e2019-04-02 08:10:47 -07002550 cr4 = ctxt->ops->get_cr(ctxt, 4);
2551 if (cr4 & X86_CR4_PCIDE)
Paolo Bonzini89651a32015-11-03 13:43:05 +01002552 ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PCIDE);
Paolo Bonzini89651a32015-11-03 13:43:05 +01002553
2554 /* A 32-bit code segment is required to clear EFER.LMA. */
2555 memset(&cs_desc, 0, sizeof(cs_desc));
2556 cs_desc.type = 0xb;
2557 cs_desc.s = cs_desc.g = cs_desc.p = 1;
2558 ctxt->ops->set_segment(ctxt, 0, &cs_desc, 0, VCPU_SREG_CS);
2559 }
2560
2561 /* For the 64-bit case, this will clear EFER.LMA. */
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002562 cr0 = ctxt->ops->get_cr(ctxt, 0);
2563 if (cr0 & X86_CR0_PE)
2564 ctxt->ops->set_cr(ctxt, 0, cr0 & ~(X86_CR0_PG | X86_CR0_PE));
Paolo Bonzini89651a32015-11-03 13:43:05 +01002565
Sean Christopherson8f4dc2e2019-04-02 08:10:47 -07002566 if (emulator_has_longmode(ctxt)) {
2567 /* Clear CR4.PAE before clearing EFER.LME. */
2568 cr4 = ctxt->ops->get_cr(ctxt, 4);
2569 if (cr4 & X86_CR4_PAE)
2570 ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PAE);
Paolo Bonzini89651a32015-11-03 13:43:05 +01002571
Sean Christopherson8f4dc2e2019-04-02 08:10:47 -07002572 /* And finally go back to 32-bit mode. */
2573 efer = 0;
2574 ctxt->ops->set_msr(ctxt, MSR_EFER, efer);
2575 }
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002576
Ladi Prosek0234bf82017-10-11 16:54:40 +02002577 /*
2578 * Give pre_leave_smm() a chance to make ISA-specific changes to the
2579 * vCPU state (e.g. enter guest mode) before loading state from the SMM
2580 * state-save area.
2581 */
Sean Christophersoned193212019-04-02 08:03:09 -07002582 if (ctxt->ops->pre_leave_smm(ctxt, buf))
Sean Christopherson25b17222021-06-09 11:56:12 -07002583 goto emulate_shutdown;
Ladi Prosek0234bf82017-10-11 16:54:40 +02002584
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002585#ifdef CONFIG_X86_64
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002586 if (emulator_has_longmode(ctxt))
Sean Christophersoned193212019-04-02 08:03:09 -07002587 ret = rsm_load_state_64(ctxt, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002588 else
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002589#endif
Sean Christophersoned193212019-04-02 08:03:09 -07002590 ret = rsm_load_state_32(ctxt, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002591
Sean Christopherson25b17222021-06-09 11:56:12 -07002592 if (ret != X86EMUL_CONTINUE)
2593 goto emulate_shutdown;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002594
Sean Christophersonc5833c72019-04-02 08:03:10 -07002595 ctxt->ops->post_leave_smm(ctxt);
2596
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002597 return X86EMUL_CONTINUE;
Sean Christopherson25b17222021-06-09 11:56:12 -07002598
2599emulate_shutdown:
2600 ctxt->ops->triple_fault(ctxt);
2601 return X86EMUL_CONTINUE;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002602}
2603
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002604static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002605setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002606 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002607{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002608 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002609 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002610 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002611 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002612 cs->type = 0x0b; /* Read, Execute, Accessed */
2613 cs->s = 1;
2614 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002615 cs->p = 1;
2616 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002617 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002618
Gleb Natapov79168fd2010-04-28 19:15:30 +03002619 set_desc_base(ss, 0); /* flat segment */
2620 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002621 ss->g = 1; /* 4kb granularity */
2622 ss->s = 1;
2623 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002624 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002625 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002626 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002627 ss->l = 0;
2628 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002629}
2630
Avi Kivity1a18a692012-02-01 12:23:21 +02002631static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2632{
2633 u32 eax, ebx, ecx, edx;
2634
2635 eax = ecx = 0;
Sean Christophersonf91af512020-03-04 17:34:37 -08002636 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, true);
Sean Christopherson15608ed2020-03-04 17:34:32 -08002637 return is_guest_vendor_intel(ebx, ecx, edx);
Avi Kivity1a18a692012-02-01 12:23:21 +02002638}
2639
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002640static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2641{
Mathias Krause0225fb52012-08-30 01:30:16 +02002642 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002643 u32 eax, ebx, ecx, edx;
2644
2645 /*
2646 * syscall should always be enabled in longmode - so only become
2647 * vendor specific (cpuid) if other modes are active...
2648 */
2649 if (ctxt->mode == X86EMUL_MODE_PROT64)
2650 return true;
2651
2652 eax = 0x00000000;
2653 ecx = 0x00000000;
Sean Christophersonf91af512020-03-04 17:34:37 -08002654 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, true);
Avi Kivity0017f932012-06-07 14:10:16 +03002655 /*
Sean Christopherson15608ed2020-03-04 17:34:32 -08002656 * remark: Intel CPUs only support "syscall" in 64bit longmode. Also a
2657 * 64bit guest with a 32bit compat-app running will #UD !! While this
2658 * behaviour can be fixed (by emulating) into AMD response - CPUs of
2659 * AMD can't behave like Intel.
Avi Kivity0017f932012-06-07 14:10:16 +03002660 */
Sean Christopherson15608ed2020-03-04 17:34:32 -08002661 if (is_guest_vendor_intel(ebx, ecx, edx))
Avi Kivity0017f932012-06-07 14:10:16 +03002662 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002663
Sean Christopherson15608ed2020-03-04 17:34:32 -08002664 if (is_guest_vendor_amd(ebx, ecx, edx) ||
2665 is_guest_vendor_hygon(ebx, ecx, edx))
Pu Wenb8f4abb2018-09-23 17:36:31 +08002666 return true;
2667
2668 /*
2669 * default: (not Intel, not AMD, not Hygon), apply Intel's
2670 * stricter rules...
2671 */
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002672 return false;
2673}
2674
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002675static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002676{
Mathias Krause0225fb52012-08-30 01:30:16 +02002677 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002678 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002679 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002680 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002681 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002682
2683 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002684 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002685 ctxt->mode == X86EMUL_MODE_VM86)
2686 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002687
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002688 if (!(em_syscall_is_enabled(ctxt)))
2689 return emulate_ud(ctxt);
2690
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002691 ops->get_msr(ctxt, MSR_EFER, &efer);
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002692 if (!(efer & EFER_SCE))
2693 return emulate_ud(ctxt);
2694
Miaohe Lin5b4ce932019-11-09 16:58:54 +08002695 setup_syscalls_segments(ctxt, &cs, &ss);
Avi Kivity717746e2011-04-20 13:37:53 +03002696 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002697 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002698 cs_sel = (u16)(msr_data & 0xfffc);
2699 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002700
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002701 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002702 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002703 cs.l = 1;
2704 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002705 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2706 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002707
Avi Kivitydd856ef2012-08-27 23:46:17 +03002708 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002709 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002710#ifdef CONFIG_X86_64
Nadav Amit6c6cb692014-07-21 14:37:30 +03002711 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002712
Avi Kivity717746e2011-04-20 13:37:53 +03002713 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002714 ctxt->mode == X86EMUL_MODE_PROT64 ?
2715 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002716 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002717
Avi Kivity717746e2011-04-20 13:37:53 +03002718 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Nadav Amit6c6cb692014-07-21 14:37:30 +03002719 ctxt->eflags &= ~msr_data;
Wanpeng Li35fd68a2015-04-08 14:08:14 +08002720 ctxt->eflags |= X86_EFLAGS_FIXED;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002721#endif
2722 } else {
2723 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002724 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002725 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002726
Nadav Amit0efb0442015-03-29 16:33:03 +03002727 ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002728 }
2729
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02002730 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002731 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002732}
2733
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002734static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002735{
Mathias Krause0225fb52012-08-30 01:30:16 +02002736 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002737 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002738 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002739 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002740 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002741
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002742 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002743 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002744 if (ctxt->mode == X86EMUL_MODE_REAL)
2745 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002746
Avi Kivity1a18a692012-02-01 12:23:21 +02002747 /*
2748 * Not recognized on AMD in compat mode (but is recognized in legacy
2749 * mode).
2750 */
Nadav Amitf3747372015-01-01 23:11:11 +02002751 if ((ctxt->mode != X86EMUL_MODE_PROT64) && (efer & EFER_LMA)
Avi Kivity1a18a692012-02-01 12:23:21 +02002752 && !vendor_intel(ctxt))
2753 return emulate_ud(ctxt);
2754
Nadav Amitb2c9d432014-11-02 11:55:01 +02002755 /* sysenter/sysexit have not been tested in 64bit mode. */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002756 if (ctxt->mode == X86EMUL_MODE_PROT64)
Nadav Amitb2c9d432014-11-02 11:55:01 +02002757 return X86EMUL_UNHANDLEABLE;
Andre Przywara8c604352009-06-18 12:56:01 +02002758
Avi Kivity717746e2011-04-20 13:37:53 +03002759 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002760 if ((msr_data & 0xfffc) == 0x0)
2761 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002762
Miaohe Lin5b4ce932019-11-09 16:58:54 +08002763 setup_syscalls_segments(ctxt, &cs, &ss);
Nadav Amit0efb0442015-03-29 16:33:03 +03002764 ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
Nadav Amitb32a9912015-03-29 16:33:04 +03002765 cs_sel = (u16)msr_data & ~SEGMENT_RPL_MASK;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002766 ss_sel = cs_sel + 8;
Nadav Amitf3747372015-01-01 23:11:11 +02002767 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002768 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002769 cs.l = 1;
2770 }
2771
Avi Kivity1aa36612011-04-27 13:20:30 +03002772 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2773 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002774
Avi Kivity717746e2011-04-20 13:37:53 +03002775 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002776 ctxt->_eip = (efer & EFER_LMA) ? msr_data : (u32)msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002777
Avi Kivity717746e2011-04-20 13:37:53 +03002778 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002779 *reg_write(ctxt, VCPU_REGS_RSP) = (efer & EFER_LMA) ? msr_data :
2780 (u32)msr_data;
Sean Christopherson943dea82021-02-02 08:55:46 -08002781 if (efer & EFER_LMA)
2782 ctxt->mode = X86EMUL_MODE_PROT64;
Andre Przywara8c604352009-06-18 12:56:01 +02002783
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002784 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002785}
2786
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002787static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002788{
Mathias Krause0225fb52012-08-30 01:30:16 +02002789 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002790 struct desc_struct cs, ss;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002791 u64 msr_data, rcx, rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002792 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002793 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002794
Gleb Natapova0044752010-02-10 14:21:31 +02002795 /* inject #GP if in real mode or Virtual 8086 mode */
2796 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002797 ctxt->mode == X86EMUL_MODE_VM86)
2798 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002799
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002800 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002801
Avi Kivity9dac77f2011-06-01 15:34:25 +03002802 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002803 usermode = X86EMUL_MODE_PROT64;
2804 else
2805 usermode = X86EMUL_MODE_PROT32;
2806
Nadav Amit234f3ce2014-09-18 22:39:38 +03002807 rcx = reg_read(ctxt, VCPU_REGS_RCX);
2808 rdx = reg_read(ctxt, VCPU_REGS_RDX);
2809
Andre Przywara4668f052009-06-18 12:56:02 +02002810 cs.dpl = 3;
2811 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002812 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002813 switch (usermode) {
2814 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002815 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002816 if ((msr_data & 0xfffc) == 0x0)
2817 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002818 ss_sel = (u16)(msr_data + 24);
Nadav Amitbf0b6822014-09-18 22:39:45 +03002819 rcx = (u32)rcx;
2820 rdx = (u32)rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002821 break;
2822 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002823 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002824 if (msr_data == 0x0)
2825 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002826 ss_sel = cs_sel + 8;
2827 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002828 cs.l = 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08002829 if (emul_is_noncanonical_address(rcx, ctxt) ||
2830 emul_is_noncanonical_address(rdx, ctxt))
Nadav Amit234f3ce2014-09-18 22:39:38 +03002831 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002832 break;
2833 }
Nadav Amitb32a9912015-03-29 16:33:04 +03002834 cs_sel |= SEGMENT_RPL_MASK;
2835 ss_sel |= SEGMENT_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002836
Avi Kivity1aa36612011-04-27 13:20:30 +03002837 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2838 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002839
Nadav Amit234f3ce2014-09-18 22:39:38 +03002840 ctxt->_eip = rdx;
2841 *reg_write(ctxt, VCPU_REGS_RSP) = rcx;
Andre Przywara4668f052009-06-18 12:56:02 +02002842
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002843 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002844}
2845
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002846static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002847{
2848 int iopl;
2849 if (ctxt->mode == X86EMUL_MODE_REAL)
2850 return false;
2851 if (ctxt->mode == X86EMUL_MODE_VM86)
2852 return true;
Nadav Amit0efb0442015-03-29 16:33:03 +03002853 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002854 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002855}
2856
Liran Alon9a29d4492018-03-12 13:12:48 +02002857#define VMWARE_PORT_VMPORT (0x5658)
2858#define VMWARE_PORT_VMRPC (0x5659)
2859
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002860static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002861 u16 port, u16 len)
2862{
Mathias Krause0225fb52012-08-30 01:30:16 +02002863 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002864 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002865 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002866 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002867 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002868 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002869 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002870
Liran Alon9a29d4492018-03-12 13:12:48 +02002871 /*
2872 * VMware allows access to these ports even if denied
2873 * by TSS I/O permission bitmap. Mimic behavior.
2874 */
2875 if (enable_vmware_backdoor &&
2876 ((port == VMWARE_PORT_VMPORT) || (port == VMWARE_PORT_VMRPC)))
2877 return true;
2878
Avi Kivity1aa36612011-04-27 13:20:30 +03002879 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002880 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002881 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002882 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002883 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002884 base = get_desc_base(&tr_seg);
2885#ifdef CONFIG_X86_64
2886 base |= ((u64)base3) << 32;
2887#endif
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02002888 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL, true);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002889 if (r != X86EMUL_CONTINUE)
2890 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002891 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002892 return false;
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02002893 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL, true);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002894 if (r != X86EMUL_CONTINUE)
2895 return false;
2896 if ((perm >> bit_idx) & mask)
2897 return false;
2898 return true;
2899}
2900
2901static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002902 u16 port, u16 len)
2903{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002904 if (ctxt->perm_ok)
2905 return true;
2906
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002907 if (emulator_bad_iopl(ctxt))
2908 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002909 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002910
2911 ctxt->perm_ok = true;
2912
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002913 return true;
2914}
2915
Nadav Amit428e3d02015-04-28 13:06:01 +03002916static void string_registers_quirk(struct x86_emulate_ctxt *ctxt)
2917{
2918 /*
2919 * Intel CPUs mask the counter and pointers in quite strange
2920 * manner when ECX is zero due to REP-string optimizations.
2921 */
2922#ifdef CONFIG_X86_64
2923 if (ctxt->ad_bytes != 4 || !vendor_intel(ctxt))
2924 return;
2925
2926 *reg_write(ctxt, VCPU_REGS_RCX) = 0;
2927
2928 switch (ctxt->b) {
2929 case 0xa4: /* movsb */
2930 case 0xa5: /* movsd/w */
2931 *reg_rmw(ctxt, VCPU_REGS_RSI) &= (u32)-1;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002932 fallthrough;
Nadav Amit428e3d02015-04-28 13:06:01 +03002933 case 0xaa: /* stosb */
2934 case 0xab: /* stosd/w */
2935 *reg_rmw(ctxt, VCPU_REGS_RDI) &= (u32)-1;
2936 }
2937#endif
2938}
2939
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002940static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002941 struct tss_segment_16 *tss)
2942{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002943 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002944 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002945 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2946 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2947 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2948 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2949 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2950 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2951 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2952 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002953
Avi Kivity1aa36612011-04-27 13:20:30 +03002954 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2955 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2956 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2957 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2958 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002959}
2960
2961static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002962 struct tss_segment_16 *tss)
2963{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002964 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002965 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002966
Avi Kivity9dac77f2011-06-01 15:34:25 +03002967 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002968 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002969 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2970 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2971 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2972 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2973 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2974 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2975 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2976 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002977
2978 /*
2979 * SDM says that segment selectors are loaded before segment
2980 * descriptors
2981 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002982 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2983 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2984 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2985 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2986 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002987
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002988 cpl = tss->cs & 3;
2989
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002990 /*
Guo Chaofc058682012-06-28 15:19:51 +08002991 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002992 * it is handled in a context of new task
2993 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002994 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002995 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002996 if (ret != X86EMUL_CONTINUE)
2997 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002998 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002999 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003000 if (ret != X86EMUL_CONTINUE)
3001 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003002 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003003 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003004 if (ret != X86EMUL_CONTINUE)
3005 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003006 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003007 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003008 if (ret != X86EMUL_CONTINUE)
3009 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003010 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003011 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003012 if (ret != X86EMUL_CONTINUE)
3013 return ret;
3014
3015 return X86EMUL_CONTINUE;
3016}
3017
3018static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003019 u16 tss_selector, u16 old_tss_sel,
3020 ulong old_tss_base, struct desc_struct *new_desc)
3021{
3022 struct tss_segment_16 tss_seg;
3023 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003024 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003025
Jordan Borgner0e96f312018-10-28 12:58:28 +00003026 ret = linear_read_system(ctxt, old_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003027 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003028 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003029
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003030 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003031
Jordan Borgner0e96f312018-10-28 12:58:28 +00003032 ret = linear_write_system(ctxt, old_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003033 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003034 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003035
Jordan Borgner0e96f312018-10-28 12:58:28 +00003036 ret = linear_read_system(ctxt, new_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003037 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003038 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003039
3040 if (old_tss_sel != 0xffff) {
3041 tss_seg.prev_task_link = old_tss_sel;
3042
Paolo Bonzini79367a62018-06-06 16:43:02 +02003043 ret = linear_write_system(ctxt, new_tss_base,
3044 &tss_seg.prev_task_link,
Jordan Borgner0e96f312018-10-28 12:58:28 +00003045 sizeof(tss_seg.prev_task_link));
Avi Kivitydb297e32010-11-22 17:53:24 +02003046 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003047 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003048 }
3049
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003050 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003051}
3052
3053static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003054 struct tss_segment_32 *tss)
3055{
Nadav Amit5c7411e2014-04-07 18:37:47 +03003056 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003057 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003058 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003059 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
3060 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
3061 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
3062 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
3063 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
3064 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
3065 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
3066 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003067
Avi Kivity1aa36612011-04-27 13:20:30 +03003068 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
3069 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
3070 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
3071 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
3072 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
3073 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003074}
3075
3076static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003077 struct tss_segment_32 *tss)
3078{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003079 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003080 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003081
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003082 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003083 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003084 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003085 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01003086
3087 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03003088 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
3089 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
3090 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
3091 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
3092 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
3093 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
3094 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
3095 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003096
3097 /*
3098 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003099 * descriptors. This is important because CPL checks will
3100 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003101 */
Avi Kivity1aa36612011-04-27 13:20:30 +03003102 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
3103 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
3104 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
3105 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
3106 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
3107 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
3108 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003109
3110 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01003111 * If we're switching between Protected Mode and VM86, we need to make
3112 * sure to update the mode before loading the segment descriptors so
3113 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01003114 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003115 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01003116 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003117 cpl = 3;
3118 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01003119 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003120 cpl = tss->cs & 3;
3121 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01003122
3123 /*
Ingo Molnard9f6e122021-03-18 15:28:01 +01003124 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003125 * it is handled in a context of new task
3126 */
Nadav Amitd1442d82014-09-18 22:39:39 +03003127 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003128 cpl, X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003129 if (ret != X86EMUL_CONTINUE)
3130 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003131 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003132 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003133 if (ret != X86EMUL_CONTINUE)
3134 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003135 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003136 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003137 if (ret != X86EMUL_CONTINUE)
3138 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003139 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003140 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003141 if (ret != X86EMUL_CONTINUE)
3142 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003143 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003144 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003145 if (ret != X86EMUL_CONTINUE)
3146 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003147 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003148 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003149 if (ret != X86EMUL_CONTINUE)
3150 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003151 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003152 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003153
Eugene Korenevsky2f729b12015-03-29 01:27:17 +03003154 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003155}
3156
3157static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003158 u16 tss_selector, u16 old_tss_sel,
3159 ulong old_tss_base, struct desc_struct *new_desc)
3160{
3161 struct tss_segment_32 tss_seg;
3162 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003163 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03003164 u32 eip_offset = offsetof(struct tss_segment_32, eip);
3165 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003166
Jordan Borgner0e96f312018-10-28 12:58:28 +00003167 ret = linear_read_system(ctxt, old_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003168 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003169 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003170
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003171 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003172
Nadav Amit5c7411e2014-04-07 18:37:47 +03003173 /* Only GP registers and segment selectors are saved */
Paolo Bonzini79367a62018-06-06 16:43:02 +02003174 ret = linear_write_system(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
3175 ldt_sel_offset - eip_offset);
Avi Kivitydb297e32010-11-22 17:53:24 +02003176 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003177 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003178
Jordan Borgner0e96f312018-10-28 12:58:28 +00003179 ret = linear_read_system(ctxt, new_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003180 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003181 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003182
3183 if (old_tss_sel != 0xffff) {
3184 tss_seg.prev_task_link = old_tss_sel;
3185
Paolo Bonzini79367a62018-06-06 16:43:02 +02003186 ret = linear_write_system(ctxt, new_tss_base,
3187 &tss_seg.prev_task_link,
Jordan Borgner0e96f312018-10-28 12:58:28 +00003188 sizeof(tss_seg.prev_task_link));
Avi Kivitydb297e32010-11-22 17:53:24 +02003189 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003190 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003191 }
3192
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003193 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003194}
3195
3196static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003197 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003198 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003199{
Mathias Krause0225fb52012-08-30 01:30:16 +02003200 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003201 struct desc_struct curr_tss_desc, next_tss_desc;
3202 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03003203 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003204 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03003205 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02003206 u32 desc_limit;
Nadav Amit3db176d2015-04-19 21:12:59 +03003207 ulong desc_addr, dr7;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003208
3209 /* FIXME: old_tss_base == ~0 ? */
3210
Avi Kivitye9194642012-06-13 16:29:39 +03003211 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003212 if (ret != X86EMUL_CONTINUE)
3213 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03003214 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003215 if (ret != X86EMUL_CONTINUE)
3216 return ret;
3217
3218 /* FIXME: check that next_tss_desc is tss */
3219
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003220 /*
3221 * Check privileges. The three cases are task switch caused by...
3222 *
3223 * 1. jmp/call/int to task gate: Check against DPL of the task gate
3224 * 2. Exception/IRQ/iret: No check is performed
Nadav Amit2c2ca2d2014-11-02 11:54:57 +02003225 * 3. jmp/call to TSS/task-gate: No check is performed since the
3226 * hardware checks it before exiting.
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003227 */
3228 if (reason == TASK_SWITCH_GATE) {
3229 if (idt_index != -1) {
3230 /* Software interrupts */
3231 struct desc_struct task_gate_desc;
3232 int dpl;
3233
3234 ret = read_interrupt_descriptor(ctxt, idt_index,
3235 &task_gate_desc);
3236 if (ret != X86EMUL_CONTINUE)
3237 return ret;
3238
3239 dpl = task_gate_desc.dpl;
3240 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
3241 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
3242 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003243 }
3244
Gleb Natapovceffb452010-03-18 15:20:19 +02003245 desc_limit = desc_limit_scaled(&next_tss_desc);
3246 if (!next_tss_desc.p ||
3247 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
3248 desc_limit < 0x2b)) {
Paolo Bonzini592f0852014-08-20 10:05:08 +02003249 return emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003250 }
3251
3252 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
3253 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003254 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003255 }
3256
3257 if (reason == TASK_SWITCH_IRET)
3258 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
3259
3260 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08003261 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003262 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
3263 old_tss_sel = 0xffff;
3264
3265 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003266 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003267 old_tss_base, &next_tss_desc);
3268 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003269 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003270 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02003271 if (ret != X86EMUL_CONTINUE)
3272 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003273
3274 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
3275 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
3276
3277 if (reason != TASK_SWITCH_IRET) {
3278 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003279 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003280 }
3281
Avi Kivity717746e2011-04-20 13:37:53 +03003282 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03003283 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003284
Jan Kiszkae269fb22010-04-14 15:51:09 +02003285 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003286 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
3287 ctxt->lock_prefix = 0;
3288 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003289 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02003290 }
3291
Nadav Amit3db176d2015-04-19 21:12:59 +03003292 ops->get_dr(ctxt, 7, &dr7);
3293 ops->set_dr(ctxt, 7, dr7 & ~(DR_LOCAL_ENABLE_MASK | DR_LOCAL_SLOWDOWN));
3294
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003295 return ret;
3296}
3297
3298int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003299 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003300 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003301{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003302 int rc;
3303
Avi Kivitydd856ef2012-08-27 23:46:17 +03003304 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003305 ctxt->_eip = ctxt->eip;
3306 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003307
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003308 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003309 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003310
Avi Kivitydd856ef2012-08-27 23:46:17 +03003311 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003312 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003313 writeback_registers(ctxt);
3314 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003315
Gleb Natapova0c0ab22011-03-28 16:57:49 +02003316 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003317}
3318
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03003319static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
3320 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02003321{
Nadav Amit0efb0442015-03-29 16:33:03 +03003322 int df = (ctxt->eflags & X86_EFLAGS_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02003323
Paolo Bonzini01485a22014-11-19 18:25:08 +01003324 register_address_increment(ctxt, reg, df * op->bytes);
3325 op->addr.mem.ea = register_address(ctxt, reg);
Gleb Natapova682e352010-03-18 15:20:21 +02003326}
3327
Avi Kivity7af04fc2010-08-18 14:16:35 +03003328static int em_das(struct x86_emulate_ctxt *ctxt)
3329{
Avi Kivity7af04fc2010-08-18 14:16:35 +03003330 u8 al, old_al;
3331 bool af, cf, old_cf;
3332
3333 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003334 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03003335
3336 old_al = al;
3337 old_cf = cf;
3338 cf = false;
3339 af = ctxt->eflags & X86_EFLAGS_AF;
3340 if ((al & 0x0f) > 9 || af) {
3341 al -= 6;
3342 cf = old_cf | (al >= 250);
3343 af = true;
3344 } else {
3345 af = false;
3346 }
3347 if (old_al > 0x99 || old_cf) {
3348 al -= 0x60;
3349 cf = true;
3350 }
3351
Avi Kivity9dac77f2011-06-01 15:34:25 +03003352 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03003353 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003354 ctxt->src.type = OP_IMM;
3355 ctxt->src.val = 0;
3356 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02003357 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03003358 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
3359 if (cf)
3360 ctxt->eflags |= X86_EFLAGS_CF;
3361 if (af)
3362 ctxt->eflags |= X86_EFLAGS_AF;
3363 return X86EMUL_CONTINUE;
3364}
3365
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003366static int em_aam(struct x86_emulate_ctxt *ctxt)
3367{
3368 u8 al, ah;
3369
3370 if (ctxt->src.val == 0)
3371 return emulate_de(ctxt);
3372
3373 al = ctxt->dst.val & 0xff;
3374 ah = al / ctxt->src.val;
3375 al %= ctxt->src.val;
3376
3377 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
3378
3379 /* Set PF, ZF, SF */
3380 ctxt->src.type = OP_IMM;
3381 ctxt->src.val = 0;
3382 ctxt->src.bytes = 1;
3383 fastop(ctxt, em_or);
3384
3385 return X86EMUL_CONTINUE;
3386}
3387
Gleb Natapov7f662272012-12-10 11:42:30 +02003388static int em_aad(struct x86_emulate_ctxt *ctxt)
3389{
3390 u8 al = ctxt->dst.val & 0xff;
3391 u8 ah = (ctxt->dst.val >> 8) & 0xff;
3392
3393 al = (al + (ah * ctxt->src.val)) & 0xff;
3394
3395 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
3396
Gleb Natapovf583c292013-02-13 17:50:39 +02003397 /* Set PF, ZF, SF */
3398 ctxt->src.type = OP_IMM;
3399 ctxt->src.val = 0;
3400 ctxt->src.bytes = 1;
3401 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02003402
3403 return X86EMUL_CONTINUE;
3404}
3405
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003406static int em_call(struct x86_emulate_ctxt *ctxt)
3407{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003408 int rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003409 long rel = ctxt->src.val;
3410
3411 ctxt->src.val = (unsigned long)ctxt->_eip;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003412 rc = jmp_rel(ctxt, rel);
3413 if (rc != X86EMUL_CONTINUE)
3414 return rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003415 return em_push(ctxt);
3416}
3417
Avi Kivity0ef753b2010-08-18 14:51:45 +03003418static int em_call_far(struct x86_emulate_ctxt *ctxt)
3419{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003420 u16 sel, old_cs;
3421 ulong old_eip;
3422 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03003423 struct desc_struct old_desc, new_desc;
3424 const struct x86_emulate_ops *ops = ctxt->ops;
3425 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amit82268082015-01-26 09:32:27 +02003426 enum x86emul_mode prev_mode = ctxt->mode;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003427
Avi Kivity9dac77f2011-06-01 15:34:25 +03003428 old_eip = ctxt->_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003429 ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003430
Avi Kivity9dac77f2011-06-01 15:34:25 +03003431 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003432 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
3433 X86_TRANSFER_CALL_JMP, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003434 if (rc != X86EMUL_CONTINUE)
Nadav Amit80976db2014-12-25 02:52:20 +02003435 return rc;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003436
Nadav Amitd50eaa12014-11-19 17:43:11 +02003437 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003438 if (rc != X86EMUL_CONTINUE)
3439 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003440
Avi Kivity9dac77f2011-06-01 15:34:25 +03003441 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003442 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003443 if (rc != X86EMUL_CONTINUE)
Nadav Amitd1442d82014-09-18 22:39:39 +03003444 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003445
Avi Kivity9dac77f2011-06-01 15:34:25 +03003446 ctxt->src.val = old_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003447 rc = em_push(ctxt);
3448 /* If we failed, we tainted the memory, but the very least we should
3449 restore cs */
Nadav Amit82268082015-01-26 09:32:27 +02003450 if (rc != X86EMUL_CONTINUE) {
3451 pr_warn_once("faulting far call emulation tainted memory\n");
Nadav Amitd1442d82014-09-18 22:39:39 +03003452 goto fail;
Nadav Amit82268082015-01-26 09:32:27 +02003453 }
Nadav Amitd1442d82014-09-18 22:39:39 +03003454 return rc;
3455fail:
3456 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
Nadav Amit82268082015-01-26 09:32:27 +02003457 ctxt->mode = prev_mode;
Nadav Amitd1442d82014-09-18 22:39:39 +03003458 return rc;
3459
Avi Kivity0ef753b2010-08-18 14:51:45 +03003460}
3461
Avi Kivity40ece7c2010-08-18 15:12:09 +03003462static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3463{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003464 int rc;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003465 unsigned long eip;
Avi Kivity40ece7c2010-08-18 15:12:09 +03003466
Nadav Amit234f3ce2014-09-18 22:39:38 +03003467 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
3468 if (rc != X86EMUL_CONTINUE)
3469 return rc;
3470 rc = assign_eip_near(ctxt, eip);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003471 if (rc != X86EMUL_CONTINUE)
3472 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003473 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003474 return X86EMUL_CONTINUE;
3475}
3476
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003477static int em_xchg(struct x86_emulate_ctxt *ctxt)
3478{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003479 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003480 ctxt->src.val = ctxt->dst.val;
3481 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003482
3483 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003484 ctxt->dst.val = ctxt->src.orig_val;
3485 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003486 return X86EMUL_CONTINUE;
3487}
3488
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003489static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3490{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003491 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003492 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003493}
3494
Avi Kivity61429142010-08-19 15:13:00 +03003495static int em_cwd(struct x86_emulate_ctxt *ctxt)
3496{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003497 ctxt->dst.type = OP_REG;
3498 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003499 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003500 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003501
3502 return X86EMUL_CONTINUE;
3503}
3504
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02003505static int em_rdpid(struct x86_emulate_ctxt *ctxt)
3506{
3507 u64 tsc_aux = 0;
3508
3509 if (ctxt->ops->get_msr(ctxt, MSR_TSC_AUX, &tsc_aux))
Robert Hooa9e2e0a2020-08-28 10:23:42 +08003510 return emulate_ud(ctxt);
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02003511 ctxt->dst.val = tsc_aux;
3512 return X86EMUL_CONTINUE;
3513}
3514
Avi Kivity48bb5d32010-08-18 18:54:34 +03003515static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3516{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003517 u64 tsc = 0;
3518
Avi Kivity717746e2011-04-20 13:37:53 +03003519 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003520 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3521 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003522 return X86EMUL_CONTINUE;
3523}
3524
Avi Kivity222d21a2011-11-10 14:57:30 +02003525static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3526{
3527 u64 pmc;
3528
Avi Kivitydd856ef2012-08-27 23:46:17 +03003529 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003530 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003531 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3532 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003533 return X86EMUL_CONTINUE;
3534}
3535
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003536static int em_mov(struct x86_emulate_ctxt *ctxt)
3537{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01003538 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003539 return X86EMUL_CONTINUE;
3540}
3541
Borislav Petkov84cffe42013-10-29 12:54:56 +01003542static int em_movbe(struct x86_emulate_ctxt *ctxt)
3543{
Borislav Petkov84cffe42013-10-29 12:54:56 +01003544 u16 tmp;
3545
Sean Christopherson5ae78e92019-12-17 13:32:38 -08003546 if (!ctxt->ops->guest_has_movbe(ctxt))
Borislav Petkov84cffe42013-10-29 12:54:56 +01003547 return emulate_ud(ctxt);
3548
3549 switch (ctxt->op_bytes) {
3550 case 2:
3551 /*
3552 * From MOVBE definition: "...When the operand size is 16 bits,
3553 * the upper word of the destination register remains unchanged
3554 * ..."
3555 *
3556 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3557 * rules so we have to do the operation almost per hand.
3558 */
3559 tmp = (u16)ctxt->src.val;
3560 ctxt->dst.val &= ~0xffffUL;
3561 ctxt->dst.val |= (unsigned long)swab16(tmp);
3562 break;
3563 case 4:
3564 ctxt->dst.val = swab32((u32)ctxt->src.val);
3565 break;
3566 case 8:
3567 ctxt->dst.val = swab64(ctxt->src.val);
3568 break;
3569 default:
Paolo Bonzini592f0852014-08-20 10:05:08 +02003570 BUG();
Borislav Petkov84cffe42013-10-29 12:54:56 +01003571 }
3572 return X86EMUL_CONTINUE;
3573}
3574
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003575static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3576{
3577 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3578 return emulate_gp(ctxt, 0);
3579
3580 /* Disable writeback. */
3581 ctxt->dst.type = OP_NONE;
3582 return X86EMUL_CONTINUE;
3583}
3584
3585static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3586{
3587 unsigned long val;
3588
3589 if (ctxt->mode == X86EMUL_MODE_PROT64)
3590 val = ctxt->src.val & ~0ULL;
3591 else
3592 val = ctxt->src.val & ~0U;
3593
3594 /* #UD condition is already handled. */
3595 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3596 return emulate_gp(ctxt, 0);
3597
3598 /* Disable writeback. */
3599 ctxt->dst.type = OP_NONE;
3600 return X86EMUL_CONTINUE;
3601}
3602
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003603static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3604{
Alexander Graf1ae09952020-09-25 16:34:16 +02003605 u64 msr_index = reg_read(ctxt, VCPU_REGS_RCX);
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003606 u64 msr_data;
Alexander Graf1ae09952020-09-25 16:34:16 +02003607 int r;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003608
Avi Kivitydd856ef2012-08-27 23:46:17 +03003609 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3610 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
Alexander Graf1ae09952020-09-25 16:34:16 +02003611 r = ctxt->ops->set_msr(ctxt, msr_index, msr_data);
3612
3613 if (r == X86EMUL_IO_NEEDED)
3614 return r;
3615
Maxim Levitsky7dffeca2020-10-01 14:29:52 +03003616 if (r > 0)
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003617 return emulate_gp(ctxt, 0);
3618
Maxim Levitsky7dffeca2020-10-01 14:29:52 +03003619 return r < 0 ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003620}
3621
3622static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3623{
Alexander Graf1ae09952020-09-25 16:34:16 +02003624 u64 msr_index = reg_read(ctxt, VCPU_REGS_RCX);
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003625 u64 msr_data;
Alexander Graf1ae09952020-09-25 16:34:16 +02003626 int r;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003627
Alexander Graf1ae09952020-09-25 16:34:16 +02003628 r = ctxt->ops->get_msr(ctxt, msr_index, &msr_data);
3629
3630 if (r == X86EMUL_IO_NEEDED)
3631 return r;
3632
3633 if (r)
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003634 return emulate_gp(ctxt, 0);
3635
Avi Kivitydd856ef2012-08-27 23:46:17 +03003636 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3637 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003638 return X86EMUL_CONTINUE;
3639}
3640
Paolo Bonzinidd307d02016-07-12 10:35:51 +02003641static int em_store_sreg(struct x86_emulate_ctxt *ctxt, int segment)
3642{
3643 if (segment > VCPU_SREG_GS &&
3644 (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_UMIP) &&
3645 ctxt->ops->cpl(ctxt) > 0)
3646 return emulate_gp(ctxt, 0);
3647
3648 ctxt->dst.val = get_segment_selector(ctxt, segment);
3649 if (ctxt->dst.bytes == 4 && ctxt->dst.type == OP_MEM)
3650 ctxt->dst.bytes = 2;
3651 return X86EMUL_CONTINUE;
3652}
3653
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003654static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3655{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003656 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003657 return emulate_ud(ctxt);
3658
Paolo Bonzinidd307d02016-07-12 10:35:51 +02003659 return em_store_sreg(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003660}
3661
3662static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3663{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003664 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003665
Avi Kivity9dac77f2011-06-01 15:34:25 +03003666 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003667 return emulate_ud(ctxt);
3668
Avi Kivity9dac77f2011-06-01 15:34:25 +03003669 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003670 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3671
3672 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003673 ctxt->dst.type = OP_NONE;
3674 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003675}
3676
Paolo Bonzinidd307d02016-07-12 10:35:51 +02003677static int em_sldt(struct x86_emulate_ctxt *ctxt)
3678{
3679 return em_store_sreg(ctxt, VCPU_SREG_LDTR);
3680}
3681
Avi Kivitya14e5792012-06-13 12:28:33 +03003682static int em_lldt(struct x86_emulate_ctxt *ctxt)
3683{
3684 u16 sel = ctxt->src.val;
3685
3686 /* Disable writeback. */
3687 ctxt->dst.type = OP_NONE;
3688 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3689}
3690
Paolo Bonzinidd307d02016-07-12 10:35:51 +02003691static int em_str(struct x86_emulate_ctxt *ctxt)
3692{
3693 return em_store_sreg(ctxt, VCPU_SREG_TR);
3694}
3695
Avi Kivity80890002012-06-13 16:33:29 +03003696static int em_ltr(struct x86_emulate_ctxt *ctxt)
3697{
3698 u16 sel = ctxt->src.val;
3699
3700 /* Disable writeback. */
3701 ctxt->dst.type = OP_NONE;
3702 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3703}
3704
Avi Kivity38503912011-03-31 18:48:09 +02003705static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3706{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003707 int rc;
3708 ulong linear;
3709
Avi Kivity9dac77f2011-06-01 15:34:25 +03003710 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003711 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003712 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003713 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003714 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003715 return X86EMUL_CONTINUE;
3716}
3717
Avi Kivity2d04a052011-04-20 15:32:49 +03003718static int em_clts(struct x86_emulate_ctxt *ctxt)
3719{
3720 ulong cr0;
3721
3722 cr0 = ctxt->ops->get_cr(ctxt, 0);
3723 cr0 &= ~X86_CR0_TS;
3724 ctxt->ops->set_cr(ctxt, 0, cr0);
3725 return X86EMUL_CONTINUE;
3726}
3727
Jan Kiszkab34a8052015-03-09 20:27:43 +01003728static int em_hypercall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003729{
Nadav Amit0f54a322014-08-29 11:26:55 +03003730 int rc = ctxt->ops->fix_hypercall(ctxt);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003731
Avi Kivity26d05cc2011-04-21 12:07:59 +03003732 if (rc != X86EMUL_CONTINUE)
3733 return rc;
3734
3735 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003736 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003737 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003738 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003739 return X86EMUL_CONTINUE;
3740}
3741
Avi Kivity96051572012-06-10 17:21:18 +03003742static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3743 void (*get)(struct x86_emulate_ctxt *ctxt,
3744 struct desc_ptr *ptr))
3745{
3746 struct desc_ptr desc_ptr;
3747
Paolo Bonziniae3e61e2016-07-12 10:36:41 +02003748 if ((ctxt->ops->get_cr(ctxt, 4) & X86_CR4_UMIP) &&
3749 ctxt->ops->cpl(ctxt) > 0)
3750 return emulate_gp(ctxt, 0);
3751
Avi Kivity96051572012-06-10 17:21:18 +03003752 if (ctxt->mode == X86EMUL_MODE_PROT64)
3753 ctxt->op_bytes = 8;
3754 get(ctxt, &desc_ptr);
3755 if (ctxt->op_bytes == 2) {
3756 ctxt->op_bytes = 4;
3757 desc_ptr.address &= 0x00ffffff;
3758 }
3759 /* Disable writeback. */
3760 ctxt->dst.type = OP_NONE;
Steve Rutherford129a72a2017-01-11 18:28:29 -08003761 return segmented_write_std(ctxt, ctxt->dst.addr.mem,
3762 &desc_ptr, 2 + ctxt->op_bytes);
Avi Kivity96051572012-06-10 17:21:18 +03003763}
3764
3765static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3766{
3767 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3768}
3769
3770static int em_sidt(struct x86_emulate_ctxt *ctxt)
3771{
3772 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3773}
3774
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003775static int em_lgdt_lidt(struct x86_emulate_ctxt *ctxt, bool lgdt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003776{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003777 struct desc_ptr desc_ptr;
3778 int rc;
3779
Avi Kivity510425f2012-06-07 17:04:36 +03003780 if (ctxt->mode == X86EMUL_MODE_PROT64)
3781 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003782 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003783 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003784 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003785 if (rc != X86EMUL_CONTINUE)
3786 return rc;
Nadav Amit9a9abf62014-11-02 11:54:56 +02003787 if (ctxt->mode == X86EMUL_MODE_PROT64 &&
Yu Zhangfd8cb432017-08-24 20:27:56 +08003788 emul_is_noncanonical_address(desc_ptr.address, ctxt))
Nadav Amit9a9abf62014-11-02 11:54:56 +02003789 return emulate_gp(ctxt, 0);
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003790 if (lgdt)
3791 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3792 else
3793 ctxt->ops->set_idt(ctxt, &desc_ptr);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003794 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003795 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003796 return X86EMUL_CONTINUE;
3797}
3798
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003799static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3800{
3801 return em_lgdt_lidt(ctxt, true);
3802}
3803
Avi Kivity26d05cc2011-04-21 12:07:59 +03003804static int em_lidt(struct x86_emulate_ctxt *ctxt)
3805{
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003806 return em_lgdt_lidt(ctxt, false);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003807}
3808
3809static int em_smsw(struct x86_emulate_ctxt *ctxt)
3810{
Paolo Bonziniae3e61e2016-07-12 10:36:41 +02003811 if ((ctxt->ops->get_cr(ctxt, 4) & X86_CR4_UMIP) &&
3812 ctxt->ops->cpl(ctxt) > 0)
3813 return emulate_gp(ctxt, 0);
3814
Nadav Amit32e94d02014-06-02 18:34:11 +03003815 if (ctxt->dst.type == OP_MEM)
3816 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003817 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003818 return X86EMUL_CONTINUE;
3819}
3820
3821static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3822{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003823 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003824 | (ctxt->src.val & 0x0f));
3825 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003826 return X86EMUL_CONTINUE;
3827}
3828
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003829static int em_loop(struct x86_emulate_ctxt *ctxt)
3830{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003831 int rc = X86EMUL_CONTINUE;
3832
Paolo Bonzini01485a22014-11-19 18:25:08 +01003833 register_address_increment(ctxt, VCPU_REGS_RCX, -1);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003834 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003835 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
Nadav Amit234f3ce2014-09-18 22:39:38 +03003836 rc = jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003837
Nadav Amit234f3ce2014-09-18 22:39:38 +03003838 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003839}
3840
3841static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3842{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003843 int rc = X86EMUL_CONTINUE;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003844
Nadav Amit234f3ce2014-09-18 22:39:38 +03003845 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
3846 rc = jmp_rel(ctxt, ctxt->src.val);
3847
3848 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003849}
3850
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003851static int em_in(struct x86_emulate_ctxt *ctxt)
3852{
3853 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3854 &ctxt->dst.val))
3855 return X86EMUL_IO_NEEDED;
3856
3857 return X86EMUL_CONTINUE;
3858}
3859
3860static int em_out(struct x86_emulate_ctxt *ctxt)
3861{
3862 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3863 &ctxt->src.val, 1);
3864 /* Disable writeback. */
3865 ctxt->dst.type = OP_NONE;
3866 return X86EMUL_CONTINUE;
3867}
3868
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003869static int em_cli(struct x86_emulate_ctxt *ctxt)
3870{
3871 if (emulator_bad_iopl(ctxt))
3872 return emulate_gp(ctxt, 0);
3873
3874 ctxt->eflags &= ~X86_EFLAGS_IF;
3875 return X86EMUL_CONTINUE;
3876}
3877
3878static int em_sti(struct x86_emulate_ctxt *ctxt)
3879{
3880 if (emulator_bad_iopl(ctxt))
3881 return emulate_gp(ctxt, 0);
3882
3883 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3884 ctxt->eflags |= X86_EFLAGS_IF;
3885 return X86EMUL_CONTINUE;
3886}
3887
Avi Kivity6d6eede2012-06-07 14:11:36 +03003888static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3889{
3890 u32 eax, ebx, ecx, edx;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003891 u64 msr = 0;
3892
3893 ctxt->ops->get_msr(ctxt, MSR_MISC_FEATURES_ENABLES, &msr);
3894 if (msr & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
3895 ctxt->ops->cpl(ctxt)) {
3896 return emulate_gp(ctxt, 0);
3897 }
Avi Kivity6d6eede2012-06-07 14:11:36 +03003898
Avi Kivitydd856ef2012-08-27 23:46:17 +03003899 eax = reg_read(ctxt, VCPU_REGS_RAX);
3900 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Sean Christophersonf91af512020-03-04 17:34:37 -08003901 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, false);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003902 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3903 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3904 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3905 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003906 return X86EMUL_CONTINUE;
3907}
3908
Paolo Bonzini98f73632013-10-31 11:19:42 +01003909static int em_sahf(struct x86_emulate_ctxt *ctxt)
3910{
3911 u32 flags;
3912
Nadav Amit0efb0442015-03-29 16:33:03 +03003913 flags = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
3914 X86_EFLAGS_SF;
Paolo Bonzini98f73632013-10-31 11:19:42 +01003915 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3916
3917 ctxt->eflags &= ~0xffUL;
3918 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3919 return X86EMUL_CONTINUE;
3920}
3921
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003922static int em_lahf(struct x86_emulate_ctxt *ctxt)
3923{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003924 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3925 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003926 return X86EMUL_CONTINUE;
3927}
3928
Avi Kivity92998362012-06-13 12:25:06 +03003929static int em_bswap(struct x86_emulate_ctxt *ctxt)
3930{
3931 switch (ctxt->op_bytes) {
3932#ifdef CONFIG_X86_64
3933 case 8:
3934 asm("bswap %0" : "+r"(ctxt->dst.val));
3935 break;
3936#endif
3937 default:
3938 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3939 break;
3940 }
3941 return X86EMUL_CONTINUE;
3942}
3943
Nadav Amit13e457e2014-10-13 13:04:13 +03003944static int em_clflush(struct x86_emulate_ctxt *ctxt)
3945{
3946 /* emulating clflush regardless of cpuid */
3947 return X86EMUL_CONTINUE;
3948}
3949
David Edmondson51b958e2020-11-03 12:04:00 +00003950static int em_clflushopt(struct x86_emulate_ctxt *ctxt)
3951{
3952 /* emulating clflushopt regardless of cpuid */
3953 return X86EMUL_CONTINUE;
3954}
3955
Nadav Amit2276b512015-01-26 09:32:24 +02003956static int em_movsxd(struct x86_emulate_ctxt *ctxt)
3957{
3958 ctxt->dst.val = (s32) ctxt->src.val;
3959 return X86EMUL_CONTINUE;
3960}
3961
Radim Krčmář283c95d2016-11-09 19:07:06 +01003962static int check_fxsr(struct x86_emulate_ctxt *ctxt)
3963{
Sean Christopherson5ae78e92019-12-17 13:32:38 -08003964 if (!ctxt->ops->guest_has_fxsr(ctxt))
Radim Krčmář283c95d2016-11-09 19:07:06 +01003965 return emulate_ud(ctxt);
3966
3967 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
3968 return emulate_nm(ctxt);
3969
3970 /*
3971 * Don't emulate a case that should never be hit, instead of working
3972 * around a lack of fxsave64/fxrstor64 on old compilers.
3973 */
3974 if (ctxt->mode >= X86EMUL_MODE_PROT64)
3975 return X86EMUL_UNHANDLEABLE;
3976
3977 return X86EMUL_CONTINUE;
3978}
3979
3980/*
Nick Desaulniers9d643f62017-05-30 20:08:38 -07003981 * Hardware doesn't save and restore XMM 0-7 without CR4.OSFXSR, but does save
3982 * and restore MXCSR.
3983 */
3984static size_t __fxstate_size(int nregs)
3985{
3986 return offsetof(struct fxregs_state, xmm_space[0]) + nregs * 16;
3987}
3988
3989static inline size_t fxstate_size(struct x86_emulate_ctxt *ctxt)
3990{
3991 bool cr4_osfxsr;
3992 if (ctxt->mode == X86EMUL_MODE_PROT64)
3993 return __fxstate_size(16);
3994
3995 cr4_osfxsr = ctxt->ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR;
3996 return __fxstate_size(cr4_osfxsr ? 8 : 0);
3997}
3998
3999/*
Radim Krčmář283c95d2016-11-09 19:07:06 +01004000 * FXSAVE and FXRSTOR have 4 different formats depending on execution mode,
4001 * 1) 16 bit mode
4002 * 2) 32 bit mode
4003 * - like (1), but FIP and FDP (foo) are only 16 bit. At least Intel CPUs
4004 * preserve whole 32 bit values, though, so (1) and (2) are the same wrt.
4005 * save and restore
4006 * 3) 64-bit mode with REX.W prefix
4007 * - like (2), but XMM 8-15 are being saved and restored
4008 * 4) 64-bit mode without REX.W prefix
4009 * - like (3), but FIP and FDP are 64 bit
4010 *
4011 * Emulation uses (3) for (1) and (2) and preserves XMM 8-15 to reach the
4012 * desired result. (4) is not emulated.
4013 *
4014 * Note: Guest and host CPUID.(EAX=07H,ECX=0H):EBX[bit 13] (deprecate FPU CS
4015 * and FPU DS) should match.
4016 */
4017static int em_fxsave(struct x86_emulate_ctxt *ctxt)
4018{
4019 struct fxregs_state fx_state;
Radim Krčmář283c95d2016-11-09 19:07:06 +01004020 int rc;
4021
4022 rc = check_fxsr(ctxt);
4023 if (rc != X86EMUL_CONTINUE)
4024 return rc;
4025
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02004026 kvm_fpu_get();
Sean Christophersona7baead2020-01-17 11:30:50 -08004027
Radim Krčmář283c95d2016-11-09 19:07:06 +01004028 rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_state));
4029
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02004030 kvm_fpu_put();
Sean Christophersona7baead2020-01-17 11:30:50 -08004031
Radim Krčmář283c95d2016-11-09 19:07:06 +01004032 if (rc != X86EMUL_CONTINUE)
4033 return rc;
4034
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004035 return segmented_write_std(ctxt, ctxt->memop.addr.mem, &fx_state,
4036 fxstate_size(ctxt));
Radim Krčmář283c95d2016-11-09 19:07:06 +01004037}
4038
David Hildenbrand4d772cb2017-11-07 18:04:05 +01004039/*
4040 * FXRSTOR might restore XMM registers not provided by the guest. Fill
4041 * in the host registers (via FXSAVE) instead, so they won't be modified.
4042 * (preemption has to stay disabled until FXRSTOR).
4043 *
4044 * Use noinline to keep the stack for other functions called by callers small.
4045 */
4046static noinline int fxregs_fixup(struct fxregs_state *fx_state,
4047 const size_t used_size)
4048{
4049 struct fxregs_state fx_tmp;
4050 int rc;
4051
4052 rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_tmp));
4053 memcpy((void *)fx_state + used_size, (void *)&fx_tmp + used_size,
4054 __fxstate_size(16) - used_size);
4055
4056 return rc;
4057}
4058
Radim Krčmář283c95d2016-11-09 19:07:06 +01004059static int em_fxrstor(struct x86_emulate_ctxt *ctxt)
4060{
4061 struct fxregs_state fx_state;
4062 int rc;
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004063 size_t size;
Radim Krčmář283c95d2016-11-09 19:07:06 +01004064
4065 rc = check_fxsr(ctxt);
4066 if (rc != X86EMUL_CONTINUE)
4067 return rc;
4068
David Hildenbrand4d772cb2017-11-07 18:04:05 +01004069 size = fxstate_size(ctxt);
4070 rc = segmented_read_std(ctxt, ctxt->memop.addr.mem, &fx_state, size);
4071 if (rc != X86EMUL_CONTINUE)
4072 return rc;
4073
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02004074 kvm_fpu_get();
Sean Christophersona7baead2020-01-17 11:30:50 -08004075
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004076 if (size < __fxstate_size(16)) {
David Hildenbrand4d772cb2017-11-07 18:04:05 +01004077 rc = fxregs_fixup(&fx_state, size);
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004078 if (rc != X86EMUL_CONTINUE)
4079 goto out;
4080 }
4081
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004082 if (fx_state.mxcsr >> 16) {
4083 rc = emulate_gp(ctxt, 0);
4084 goto out;
4085 }
Radim Krčmář283c95d2016-11-09 19:07:06 +01004086
4087 if (rc == X86EMUL_CONTINUE)
4088 rc = asm_safe("fxrstor %[fx]", : [fx] "m"(fx_state));
4089
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004090out:
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02004091 kvm_fpu_put();
Sean Christophersona7baead2020-01-17 11:30:50 -08004092
Radim Krčmář283c95d2016-11-09 19:07:06 +01004093 return rc;
4094}
4095
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02004096static int em_xsetbv(struct x86_emulate_ctxt *ctxt)
4097{
4098 u32 eax, ecx, edx;
4099
4100 eax = reg_read(ctxt, VCPU_REGS_RAX);
4101 edx = reg_read(ctxt, VCPU_REGS_RDX);
4102 ecx = reg_read(ctxt, VCPU_REGS_RCX);
4103
4104 if (ctxt->ops->set_xcr(ctxt, ecx, ((u64)edx << 32) | eax))
4105 return emulate_gp(ctxt, 0);
4106
4107 return X86EMUL_CONTINUE;
4108}
4109
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004110static bool valid_cr(int nr)
4111{
4112 switch (nr) {
4113 case 0:
4114 case 2 ... 4:
4115 case 8:
4116 return true;
4117 default:
4118 return false;
4119 }
4120}
4121
Sean Christophersond0fe7b62021-04-21 19:21:20 -07004122static int check_cr_access(struct x86_emulate_ctxt *ctxt)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004123{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004124 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004125 return emulate_ud(ctxt);
4126
4127 return X86EMUL_CONTINUE;
4128}
4129
Joerg Roedel3b88e412011-04-04 12:39:29 +02004130static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
4131{
4132 unsigned long dr7;
4133
Avi Kivity717746e2011-04-20 13:37:53 +03004134 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02004135
4136 /* Check if DR7.Global_Enable is set */
4137 return dr7 & (1 << 13);
4138}
4139
4140static int check_dr_read(struct x86_emulate_ctxt *ctxt)
4141{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004142 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02004143 u64 cr4;
4144
4145 if (dr > 7)
4146 return emulate_ud(ctxt);
4147
Avi Kivity717746e2011-04-20 13:37:53 +03004148 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02004149 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
4150 return emulate_ud(ctxt);
4151
Nadav Amit6d2a0522014-11-02 11:54:43 +02004152 if (check_dr7_gd(ctxt)) {
4153 ulong dr6;
4154
4155 ctxt->ops->get_dr(ctxt, 6, &dr6);
Liran Alon1fc5d1942019-06-06 01:54:47 +03004156 dr6 &= ~DR_TRAP_BITS;
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08004157 dr6 |= DR6_BD | DR6_ACTIVE_LOW;
Nadav Amit6d2a0522014-11-02 11:54:43 +02004158 ctxt->ops->set_dr(ctxt, 6, dr6);
Joerg Roedel3b88e412011-04-04 12:39:29 +02004159 return emulate_db(ctxt);
Nadav Amit6d2a0522014-11-02 11:54:43 +02004160 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02004161
4162 return X86EMUL_CONTINUE;
4163}
4164
4165static int check_dr_write(struct x86_emulate_ctxt *ctxt)
4166{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004167 u64 new_val = ctxt->src.val64;
4168 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02004169
4170 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
4171 return emulate_gp(ctxt, 0);
4172
4173 return check_dr_read(ctxt);
4174}
4175
Joerg Roedel01de8b02011-04-04 12:39:31 +02004176static int check_svme(struct x86_emulate_ctxt *ctxt)
4177{
Radim Krčmář92ceb762017-05-18 19:37:32 +02004178 u64 efer = 0;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004179
Avi Kivity717746e2011-04-20 13:37:53 +03004180 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02004181
4182 if (!(efer & EFER_SVME))
4183 return emulate_ud(ctxt);
4184
4185 return X86EMUL_CONTINUE;
4186}
4187
4188static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
4189{
Avi Kivitydd856ef2012-08-27 23:46:17 +03004190 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02004191
4192 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07004193 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02004194 return emulate_gp(ctxt, 0);
4195
4196 return check_svme(ctxt);
4197}
4198
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004199static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
4200{
Avi Kivity717746e2011-04-20 13:37:53 +03004201 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004202
Avi Kivity717746e2011-04-20 13:37:53 +03004203 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004204 return emulate_ud(ctxt);
4205
4206 return X86EMUL_CONTINUE;
4207}
4208
Joerg Roedel80612522011-04-04 12:39:33 +02004209static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
4210{
Avi Kivity717746e2011-04-20 13:37:53 +03004211 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03004212 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02004213
Arbel Moshe2d7921c2018-03-12 13:12:53 +02004214 /*
4215 * VMware allows access to these Pseduo-PMCs even when read via RDPMC
4216 * in Ring3 when CR4.PCE=0.
4217 */
4218 if (enable_vmware_backdoor && is_vmware_backdoor_pmc(rcx))
4219 return X86EMUL_CONTINUE;
4220
Avi Kivity717746e2011-04-20 13:37:53 +03004221 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03004222 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02004223 return emulate_gp(ctxt, 0);
4224
4225 return X86EMUL_CONTINUE;
4226}
4227
Joerg Roedelf6511932011-04-04 12:39:35 +02004228static int check_perm_in(struct x86_emulate_ctxt *ctxt)
4229{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004230 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
4231 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02004232 return emulate_gp(ctxt, 0);
4233
4234 return X86EMUL_CONTINUE;
4235}
4236
4237static int check_perm_out(struct x86_emulate_ctxt *ctxt)
4238{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004239 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
4240 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02004241 return emulate_gp(ctxt, 0);
4242
4243 return X86EMUL_CONTINUE;
4244}
4245
Avi Kivity73fba5f2010-07-29 15:11:53 +03004246#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004247#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
4248#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
4249 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03004250#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02004251#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004252#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
4253#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Nadav Amit39f062f2014-11-26 15:47:18 +02004254#define ID(_f, _i) { .flags = ((_f) | InstrDual | ModRM), .u.idual = (_i) }
Nadav Amit2276b512015-01-26 09:32:24 +02004255#define MD(_f, _m) { .flags = ((_f) | ModeDual), .u.mdual = (_m) }
Gleb Natapov045a2822012-12-20 16:57:43 +02004256#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03004257#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02004258#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004259#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004260 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004261#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004262 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
4263 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02004264#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03004265
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004266#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02004267#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004268#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02004269#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004270#define I2bvIP(_f, _e, _i, _p) \
4271 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004272
Avi Kivityfb864fb2013-01-04 16:18:54 +02004273#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
4274 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
4275 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03004276
Nadav Amit0f54a322014-08-29 11:26:55 +03004277static const struct opcode group7_rm0[] = {
4278 N,
Jan Kiszkab34a8052015-03-09 20:27:43 +01004279 I(SrcNone | Priv | EmulateOnUD, em_hypercall),
Nadav Amit0f54a322014-08-29 11:26:55 +03004280 N, N, N, N, N, N,
4281};
4282
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004283static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004284 DI(SrcNone | Priv, monitor),
4285 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004286 N, N, N, N, N, N,
4287};
4288
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02004289static const struct opcode group7_rm2[] = {
4290 N,
4291 II(ImplicitOps | Priv, em_xsetbv, xsetbv),
4292 N, N, N, N, N, N,
4293};
4294
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004295static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004296 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Jan Kiszkab34a8052015-03-09 20:27:43 +01004297 II(SrcNone | Prot | EmulateOnUD, em_hypercall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004298 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
4299 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
4300 DIP(SrcNone | Prot | Priv, stgi, check_svme),
4301 DIP(SrcNone | Prot | Priv, clgi, check_svme),
4302 DIP(SrcNone | Prot | Priv, skinit, check_svme),
4303 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004304};
Avi Kivity6230f7f2010-08-26 18:34:55 +03004305
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004306static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004307 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004308 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004309 N, N, N, N, N, N,
4310};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004311
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004312static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02004313 F(Lock, em_add),
4314 F(Lock | PageTable, em_or),
4315 F(Lock, em_adc),
4316 F(Lock, em_sbb),
4317 F(Lock | PageTable, em_and),
4318 F(Lock, em_sub),
4319 F(Lock, em_xor),
4320 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004321};
4322
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004323static const struct opcode group1A[] = {
Tom Lendacky0f89b202016-12-14 14:59:23 -05004324 I(DstMem | SrcNone | Mov | Stack | IncSP | TwoMemOp, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004325};
4326
Avi Kivity007a3b52013-01-19 19:51:51 +02004327static const struct opcode group2[] = {
4328 F(DstMem | ModRM, em_rol),
4329 F(DstMem | ModRM, em_ror),
4330 F(DstMem | ModRM, em_rcl),
4331 F(DstMem | ModRM, em_rcr),
4332 F(DstMem | ModRM, em_shl),
4333 F(DstMem | ModRM, em_shr),
4334 F(DstMem | ModRM, em_shl),
4335 F(DstMem | ModRM, em_sar),
4336};
4337
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004338static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02004339 F(DstMem | SrcImm | NoWrite, em_test),
4340 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02004341 F(DstMem | SrcNone | Lock, em_not),
4342 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02004343 F(DstXacc | Src2Mem, em_mul_ex),
4344 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004345 F(DstXacc | Src2Mem, em_div_ex),
4346 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004347};
4348
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004349static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02004350 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
4351 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004352 N, N, N, N, N, N,
4353};
4354
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004355static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02004356 F(DstMem | SrcNone | Lock, em_inc),
4357 F(DstMem | SrcNone | Lock, em_dec),
Nadav Amit58b70752014-10-24 11:35:09 +03004358 I(SrcMem | NearBranch, em_call_near_abs),
Nadav Amitacac6f892015-05-03 20:22:57 +03004359 I(SrcMemFAddr | ImplicitOps, em_call_far),
Nadav Amit58b70752014-10-24 11:35:09 +03004360 I(SrcMem | NearBranch, em_jmp_abs),
Nadav Amitf7784042014-09-18 22:39:41 +03004361 I(SrcMemFAddr | ImplicitOps, em_jmp_far),
Tom Lendacky0f89b202016-12-14 14:59:23 -05004362 I(SrcMem | Stack | TwoMemOp, em_push), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004363};
4364
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004365static const struct opcode group6[] = {
Paolo Bonzinidd307d02016-07-12 10:35:51 +02004366 II(Prot | DstMem, em_sldt, sldt),
4367 II(Prot | DstMem, em_str, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03004368 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03004369 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004370 N, N, N, N,
4371};
4372
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004373static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03004374 II(Mov | DstMem, em_sgdt, sgdt),
4375 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004376 II(SrcMem | Priv, em_lgdt, lgdt),
4377 II(SrcMem | Priv, em_lidt, lidt),
4378 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
4379 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
4380 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004381}, {
Nadav Amit0f54a322014-08-29 11:26:55 +03004382 EXT(0, group7_rm0),
Avi Kivity5ef39c72011-04-21 12:21:50 +03004383 EXT(0, group7_rm1),
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02004384 EXT(0, group7_rm2),
4385 EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004386 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
4387 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
4388 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004389} };
4390
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004391static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004392 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02004393 F(DstMem | SrcImmByte | NoWrite, em_bt),
4394 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
4395 F(DstMem | SrcImmByte | Lock, em_btr),
4396 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004397};
4398
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02004399/*
4400 * The "memory" destination is actually always a register, since we come
4401 * from the register case of group9.
4402 */
4403static const struct gprefix pfx_0f_c7_7 = {
Sean Christopherson2183de42021-05-04 10:17:23 -07004404 N, N, N, II(DstMem | ModRM | Op3264 | EmulateOnUD, em_rdpid, rdpid),
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02004405};
4406
4407
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004408static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004409 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004410}, {
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02004411 N, N, N, N, N, N, N,
4412 GP(0, &pfx_0f_c7_7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004413} };
4414
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004415static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004416 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004417 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004418};
4419
Nadav Amit13e457e2014-10-13 13:04:13 +03004420static const struct gprefix pfx_0f_ae_7 = {
David Edmondson51b958e2020-11-03 12:04:00 +00004421 I(SrcMem | ByteOp, em_clflush), I(SrcMem | ByteOp, em_clflushopt), N, N,
Nadav Amit13e457e2014-10-13 13:04:13 +03004422};
4423
4424static const struct group_dual group15 = { {
Radim Krčmář283c95d2016-11-09 19:07:06 +01004425 I(ModRM | Aligned16, em_fxsave),
4426 I(ModRM | Aligned16, em_fxrstor),
4427 N, N, N, N, N, GP(0, &pfx_0f_ae_7),
Nadav Amit13e457e2014-10-13 13:04:13 +03004428}, {
4429 N, N, N, N, N, N, N, N,
4430} };
4431
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004432static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03004433 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02004434};
4435
Nadav Amit39f062f2014-11-26 15:47:18 +02004436static const struct instr_dual instr_dual_0f_2b = {
4437 I(0, em_mov), N
4438};
4439
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004440static const struct gprefix pfx_0f_2b = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004441 ID(0, &instr_dual_0f_2b), ID(0, &instr_dual_0f_2b), N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03004442};
4443
Stefan Fritsch29916962018-04-01 17:54:44 +02004444static const struct gprefix pfx_0f_10_0f_11 = {
4445 I(Unaligned, em_mov), I(Unaligned, em_mov), N, N,
4446};
4447
Igor Mammedov27ce8252014-03-15 21:01:59 +01004448static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01004449 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004450};
4451
Alex Williamson0a370272014-07-11 11:56:31 -06004452static const struct gprefix pfx_0f_e7 = {
4453 N, I(Sse, em_mov), N, N,
4454};
4455
Gleb Natapov045a2822012-12-20 16:57:43 +02004456static const struct escape escape_d9 = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02004457 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstcw),
Gleb Natapov045a2822012-12-20 16:57:43 +02004458}, {
4459 /* 0xC0 - 0xC7 */
4460 N, N, N, N, N, N, N, N,
4461 /* 0xC8 - 0xCF */
4462 N, N, N, N, N, N, N, N,
4463 /* 0xD0 - 0xC7 */
4464 N, N, N, N, N, N, N, N,
4465 /* 0xD8 - 0xDF */
4466 N, N, N, N, N, N, N, N,
4467 /* 0xE0 - 0xE7 */
4468 N, N, N, N, N, N, N, N,
4469 /* 0xE8 - 0xEF */
4470 N, N, N, N, N, N, N, N,
4471 /* 0xF0 - 0xF7 */
4472 N, N, N, N, N, N, N, N,
4473 /* 0xF8 - 0xFF */
4474 N, N, N, N, N, N, N, N,
4475} };
4476
4477static const struct escape escape_db = { {
4478 N, N, N, N, N, N, N, N,
4479}, {
4480 /* 0xC0 - 0xC7 */
4481 N, N, N, N, N, N, N, N,
4482 /* 0xC8 - 0xCF */
4483 N, N, N, N, N, N, N, N,
4484 /* 0xD0 - 0xC7 */
4485 N, N, N, N, N, N, N, N,
4486 /* 0xD8 - 0xDF */
4487 N, N, N, N, N, N, N, N,
4488 /* 0xE0 - 0xE7 */
4489 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
4490 /* 0xE8 - 0xEF */
4491 N, N, N, N, N, N, N, N,
4492 /* 0xF0 - 0xF7 */
4493 N, N, N, N, N, N, N, N,
4494 /* 0xF8 - 0xFF */
4495 N, N, N, N, N, N, N, N,
4496} };
4497
4498static const struct escape escape_dd = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02004499 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstsw),
Gleb Natapov045a2822012-12-20 16:57:43 +02004500}, {
4501 /* 0xC0 - 0xC7 */
4502 N, N, N, N, N, N, N, N,
4503 /* 0xC8 - 0xCF */
4504 N, N, N, N, N, N, N, N,
4505 /* 0xD0 - 0xC7 */
4506 N, N, N, N, N, N, N, N,
4507 /* 0xD8 - 0xDF */
4508 N, N, N, N, N, N, N, N,
4509 /* 0xE0 - 0xE7 */
4510 N, N, N, N, N, N, N, N,
4511 /* 0xE8 - 0xEF */
4512 N, N, N, N, N, N, N, N,
4513 /* 0xF0 - 0xF7 */
4514 N, N, N, N, N, N, N, N,
4515 /* 0xF8 - 0xFF */
4516 N, N, N, N, N, N, N, N,
4517} };
4518
Nadav Amit39f062f2014-11-26 15:47:18 +02004519static const struct instr_dual instr_dual_0f_c3 = {
4520 I(DstMem | SrcReg | ModRM | No16 | Mov, em_mov), N
4521};
4522
Nadav Amit2276b512015-01-26 09:32:24 +02004523static const struct mode_dual mode_dual_63 = {
4524 N, I(DstReg | SrcMem32 | ModRM | Mov, em_movsxd)
4525};
4526
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004527static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004528 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004529 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004530 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
4531 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004532 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004533 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004534 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
4535 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004536 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004537 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004538 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
4539 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004540 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004541 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004542 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
4543 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004544 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004545 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004546 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004547 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004548 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004549 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004550 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004551 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004552 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02004553 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004554 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03004555 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004556 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09004557 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004558 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09004559 I(ImplicitOps | Stack | No64, em_pusha),
4560 I(ImplicitOps | Stack | No64, em_popa),
Nadav Amit2276b512015-01-26 09:32:24 +02004561 N, MD(ModRM, &mode_dual_63),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004562 N, N, N, N,
4563 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03004564 I(SrcImm | Mov | Stack, em_push),
4565 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03004566 I(SrcImmByte | Mov | Stack, em_push),
4567 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004568 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09004569 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03004570 /* 0x70 - 0x7F */
Nadav Amit58b70752014-10-24 11:35:09 +03004571 X16(D(SrcImmByte | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004572 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004573 G(ByteOp | DstMem | SrcImm, group1),
4574 G(DstMem | SrcImm, group1),
4575 G(ByteOp | DstMem | SrcImm | No64, group1),
4576 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02004577 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004578 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004579 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004580 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004581 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004582 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09004583 D(ModRM | SrcMem | NoAccess | DstReg),
4584 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
4585 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004586 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004587 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004588 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03004589 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08004590 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09004591 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01004592 II(ImplicitOps | Stack, em_popf, popf),
4593 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004594 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004595 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004596 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Tom Lendacky0f89b202016-12-14 14:59:23 -05004597 I2bv(SrcSI | DstDI | Mov | String | TwoMemOp, em_mov),
4598 F2bv(SrcSI | DstDI | String | NoWrite | TwoMemOp, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004599 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004600 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004601 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
4602 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004603 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004604 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004605 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004606 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02004607 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004608 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004609 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Nadav Amit58b70752014-10-24 11:35:09 +03004610 I(ImplicitOps | NearBranch | SrcImmU16, em_ret_near_imm),
4611 I(ImplicitOps | NearBranch, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03004612 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
4613 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004614 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004615 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03004616 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Nadav Amit16794aa2015-01-26 09:32:22 +02004617 I(ImplicitOps | SrcImmU16, em_ret_far_imm),
4618 I(ImplicitOps, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02004619 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004620 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004621 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004622 G(Src2One | ByteOp, group2), G(Src2One, group2),
4623 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02004624 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02004625 I(DstAcc | SrcImmUByte | No64, em_aad),
4626 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004627 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004628 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02004629 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004630 /* 0xE0 - 0xE7 */
Nadav Amit58b70752014-10-24 11:35:09 +03004631 X3(I(SrcImmByte | NearBranch, em_loop)),
4632 I(SrcImmByte | NearBranch, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004633 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
4634 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004635 /* 0xE8 - 0xEF */
Nadav Amit58b70752014-10-24 11:35:09 +03004636 I(SrcImm | NearBranch, em_call), D(SrcImm | ImplicitOps | NearBranch),
4637 I(SrcImmFAddr | No64, em_jmp_far),
4638 D(SrcImmByte | ImplicitOps | NearBranch),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004639 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
4640 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004641 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004642 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004643 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4644 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004645 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09004646 D(ImplicitOps), D(ImplicitOps),
4647 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004648 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4649};
4650
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004651static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004652 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004653 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02004654 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004655 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004656 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004657 N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004658 /* 0x10 - 0x1F */
Stefan Fritsch29916962018-04-01 17:54:44 +02004659 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_10_0f_11),
4660 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_10_0f_11),
4661 N, N, N, N, N, N,
Paolo Bonzini34d26182020-05-15 12:18:14 -04004662 D(ImplicitOps | ModRM | SrcMem | NoAccess), /* 4 * prefetch + 4 * reserved NOP */
4663 D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
4664 D(ImplicitOps | ModRM | SrcMem | NoAccess), /* 8 * reserved NOP */
4665 D(ImplicitOps | ModRM | SrcMem | NoAccess), /* 8 * reserved NOP */
4666 D(ImplicitOps | ModRM | SrcMem | NoAccess), /* 8 * reserved NOP */
4667 D(ImplicitOps | ModRM | SrcMem | NoAccess), /* NOP + 7 * reserved NOP */
Avi Kivity73fba5f2010-07-29 15:11:53 +03004668 /* 0x20 - 0x2F */
Sean Christophersond0fe7b62021-04-21 19:21:20 -07004669 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_access),
Nadav Amit9b88ae92014-05-25 23:05:21 +03004670 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
4671 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
Sean Christophersond0fe7b62021-04-21 19:21:20 -07004672 check_cr_access),
Nadav Amit9b88ae92014-05-25 23:05:21 +03004673 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
4674 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004675 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004676 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
4677 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004678 N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b),
Avi Kivity3e114eb2012-04-09 18:40:01 +03004679 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004680 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004681 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004682 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004683 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004684 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02004685 I(ImplicitOps | EmulateOnUD, em_sysenter),
4686 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004687 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004688 N, N, N, N, N, N, N, N,
4689 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03004690 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004691 /* 0x50 - 0x5F */
4692 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4693 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004694 N, N, N, N,
4695 N, N, N, N,
4696 N, N, N, N,
4697 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004698 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004699 N, N, N, N,
4700 N, N, N, N,
4701 N, N, N, N,
4702 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004703 /* 0x80 - 0x8F */
Nadav Amit58b70752014-10-24 11:35:09 +03004704 X16(D(SrcImm | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004705 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004706 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004707 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004708 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004709 II(ImplicitOps, em_cpuid, cpuid),
4710 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004711 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4712 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004713 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004714 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Paolo Bonzini89651a32015-11-03 13:43:05 +01004715 II(EmulateOnUD | ImplicitOps, em_rsm, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004716 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004717 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4718 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Nadav Amit13e457e2014-10-13 13:04:13 +03004719 GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004720 /* 0xB0 - 0xB7 */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02004721 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable | SrcWrite, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004722 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004723 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004724 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4725 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004726 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004727 /* 0xB8 - 0xBF */
4728 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004729 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004730 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Nadav Amit900efe22015-03-30 15:39:21 +03004731 I(DstReg | SrcMem | ModRM, em_bsf_c),
4732 I(DstReg | SrcMem | ModRM, em_bsr_c),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004733 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004734 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02004735 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Nadav Amit39f062f2014-11-26 15:47:18 +02004736 N, ID(0, &instr_dual_0f_c3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004737 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004738 /* 0xC8 - 0xCF */
4739 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004740 /* 0xD0 - 0xDF */
4741 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4742 /* 0xE0 - 0xEF */
Alex Williamson0a370272014-07-11 11:56:31 -06004743 N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
4744 N, N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004745 /* 0xF0 - 0xFF */
4746 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4747};
4748
Nadav Amit39f062f2014-11-26 15:47:18 +02004749static const struct instr_dual instr_dual_0f_38_f0 = {
4750 I(DstReg | SrcMem | Mov, em_movbe), N
4751};
4752
4753static const struct instr_dual instr_dual_0f_38_f1 = {
4754 I(DstMem | SrcReg | Mov, em_movbe), N
4755};
4756
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004757static const struct gprefix three_byte_0f_38_f0 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004758 ID(0, &instr_dual_0f_38_f0), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004759};
4760
4761static const struct gprefix three_byte_0f_38_f1 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004762 ID(0, &instr_dual_0f_38_f1), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004763};
4764
4765/*
4766 * Insns below are selected by the prefix which indexed by the third opcode
4767 * byte.
4768 */
4769static const struct opcode opcode_map_0f_38[256] = {
4770 /* 0x00 - 0x7f */
4771 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004772 /* 0x80 - 0xef */
4773 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4774 /* 0xf0 - 0xf1 */
Nadav Amit53bb4f72014-12-07 11:49:42 +02004775 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0),
4776 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004777 /* 0xf2 - 0xff */
4778 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004779};
4780
Avi Kivity73fba5f2010-07-29 15:11:53 +03004781#undef D
4782#undef N
4783#undef G
4784#undef GD
4785#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004786#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004787#undef EXT
Nadav Amit2276b512015-01-26 09:32:24 +02004788#undef MD
Nadav Amit2b42fce2015-01-26 09:32:25 +02004789#undef ID
Avi Kivity73fba5f2010-07-29 15:11:53 +03004790
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004791#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004792#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004793#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004794#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004795#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004796
Avi Kivity9dac77f2011-06-01 15:34:25 +03004797static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004798{
4799 unsigned size;
4800
Avi Kivity9dac77f2011-06-01 15:34:25 +03004801 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004802 if (size == 8)
4803 size = 4;
4804 return size;
4805}
4806
4807static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4808 unsigned size, bool sign_extension)
4809{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004810 int rc = X86EMUL_CONTINUE;
4811
4812 op->type = OP_IMM;
4813 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004814 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004815 /* NB. Immediates are sign-extended as necessary. */
4816 switch (op->bytes) {
4817 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004818 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004819 break;
4820 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004821 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004822 break;
4823 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004824 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004825 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004826 case 8:
4827 op->val = insn_fetch(s64, ctxt);
4828 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004829 }
4830 if (!sign_extension) {
4831 switch (op->bytes) {
4832 case 1:
4833 op->val &= 0xff;
4834 break;
4835 case 2:
4836 op->val &= 0xffff;
4837 break;
4838 case 4:
4839 op->val &= 0xffffffff;
4840 break;
4841 }
4842 }
4843done:
4844 return rc;
4845}
4846
Avi Kivitya99455492011-09-13 10:45:41 +03004847static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4848 unsigned d)
4849{
4850 int rc = X86EMUL_CONTINUE;
4851
4852 switch (d) {
4853 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004854 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03004855 break;
4856 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004857 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03004858 break;
4859 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004860 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004861 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03004862 *op = ctxt->memop;
4863 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004864 if (ctxt->d & BitOp)
Avi Kivitya99455492011-09-13 10:45:41 +03004865 fetch_bit_operand(ctxt);
4866 op->orig_val = op->val;
4867 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004868 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004869 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004870 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03004871 case OpAcc:
4872 op->type = OP_REG;
4873 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004874 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03004875 fetch_register_operand(op);
4876 op->orig_val = op->val;
4877 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004878 case OpAccLo:
4879 op->type = OP_REG;
4880 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4881 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4882 fetch_register_operand(op);
4883 op->orig_val = op->val;
4884 break;
4885 case OpAccHi:
4886 if (ctxt->d & ByteOp) {
4887 op->type = OP_NONE;
4888 break;
4889 }
4890 op->type = OP_REG;
4891 op->bytes = ctxt->op_bytes;
4892 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4893 fetch_register_operand(op);
4894 op->orig_val = op->val;
4895 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004896 case OpDI:
4897 op->type = OP_MEM;
4898 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4899 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004900 register_address(ctxt, VCPU_REGS_RDI);
Avi Kivitya99455492011-09-13 10:45:41 +03004901 op->addr.mem.seg = VCPU_SREG_ES;
4902 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004903 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03004904 break;
4905 case OpDX:
4906 op->type = OP_REG;
4907 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004908 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03004909 fetch_register_operand(op);
4910 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004911 case OpCL:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004912 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004913 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004914 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004915 break;
4916 case OpImmByte:
4917 rc = decode_imm(ctxt, op, 1, true);
4918 break;
4919 case OpOne:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004920 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004921 op->bytes = 1;
4922 op->val = 1;
4923 break;
4924 case OpImm:
4925 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4926 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004927 case OpImm64:
4928 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4929 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004930 case OpMem8:
4931 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004932 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004933 ctxt->memop.addr.reg = decode_register(ctxt,
4934 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004935 fetch_register_operand(&ctxt->memop);
4936 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004937 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004938 case OpMem16:
4939 ctxt->memop.bytes = 2;
4940 goto mem_common;
4941 case OpMem32:
4942 ctxt->memop.bytes = 4;
4943 goto mem_common;
4944 case OpImmU16:
4945 rc = decode_imm(ctxt, op, 2, false);
4946 break;
4947 case OpImmU:
4948 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4949 break;
4950 case OpSI:
4951 op->type = OP_MEM;
4952 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4953 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004954 register_address(ctxt, VCPU_REGS_RSI);
Bandan Das573e80f2014-04-16 12:46:13 -04004955 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03004956 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004957 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004958 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004959 case OpXLat:
4960 op->type = OP_MEM;
4961 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4962 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004963 address_mask(ctxt,
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004964 reg_read(ctxt, VCPU_REGS_RBX) +
4965 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04004966 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004967 op->val = 0;
4968 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004969 case OpImmFAddr:
4970 op->type = OP_IMM;
4971 op->addr.mem.ea = ctxt->_eip;
4972 op->bytes = ctxt->op_bytes + 2;
4973 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4974 break;
4975 case OpMemFAddr:
4976 ctxt->memop.bytes = ctxt->op_bytes + 2;
4977 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004978 case OpES:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004979 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004980 op->val = VCPU_SREG_ES;
4981 break;
4982 case OpCS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004983 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004984 op->val = VCPU_SREG_CS;
4985 break;
4986 case OpSS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004987 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004988 op->val = VCPU_SREG_SS;
4989 break;
4990 case OpDS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004991 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004992 op->val = VCPU_SREG_DS;
4993 break;
4994 case OpFS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004995 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004996 op->val = VCPU_SREG_FS;
4997 break;
4998 case OpGS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004999 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03005000 op->val = VCPU_SREG_GS;
5001 break;
Avi Kivitya99455492011-09-13 10:45:41 +03005002 case OpImplicit:
5003 /* Special instructions do their own operand decoding. */
5004 default:
5005 op->type = OP_NONE; /* Disable writeback. */
5006 break;
5007 }
5008
5009done:
5010 return rc;
5011}
5012
Wanpeng Lib35491e2021-05-27 17:01:37 -07005013int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len, int emulation_type)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005014{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005015 int rc = X86EMUL_CONTINUE;
5016 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03005017 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005018 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04005019 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03005020 struct opcode opcode;
Wanpeng Li3853be22017-11-05 16:54:47 -08005021 u16 dummy;
5022 struct desc_struct desc;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005023
Avi Kivityf09ed832011-09-13 10:45:40 +03005024 ctxt->memop.type = OP_NONE;
5025 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005026 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02005027 ctxt->fetch.ptr = ctxt->fetch.data;
5028 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02005029 ctxt->opcode_len = 1;
Vitaly Kuznetsov342993f2020-03-03 15:33:15 +01005030 ctxt->intercept = x86_intercept_none;
Andre Przywaradc25e892010-12-21 11:12:07 +01005031 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005032 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02005033 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02005034 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02005035 if (rc != X86EMUL_CONTINUE)
Sean Christopherson016cd752019-08-15 09:20:32 -07005036 goto done;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02005037 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005038
5039 switch (mode) {
5040 case X86EMUL_MODE_REAL:
5041 case X86EMUL_MODE_VM86:
Wanpeng Li3853be22017-11-05 16:54:47 -08005042 def_op_bytes = def_ad_bytes = 2;
5043 ctxt->ops->get_segment(ctxt, &dummy, &desc, NULL, VCPU_SREG_CS);
5044 if (desc.d)
5045 def_op_bytes = def_ad_bytes = 4;
5046 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005047 case X86EMUL_MODE_PROT16:
5048 def_op_bytes = def_ad_bytes = 2;
5049 break;
5050 case X86EMUL_MODE_PROT32:
5051 def_op_bytes = def_ad_bytes = 4;
5052 break;
5053#ifdef CONFIG_X86_64
5054 case X86EMUL_MODE_PROT64:
5055 def_op_bytes = 4;
5056 def_ad_bytes = 8;
5057 break;
5058#endif
5059 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005060 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005061 }
5062
Avi Kivity9dac77f2011-06-01 15:34:25 +03005063 ctxt->op_bytes = def_op_bytes;
5064 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005065
5066 /* Legacy prefixes. */
5067 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09005068 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005069 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005070 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005071 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005072 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005073 break;
5074 case 0x67: /* address-size override */
5075 if (mode == X86EMUL_MODE_PROT64)
5076 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005077 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005078 else
5079 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005080 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005081 break;
5082 case 0x26: /* ES override */
Marios Pomonis125ffc52019-12-11 12:47:50 -08005083 has_seg_override = true;
5084 ctxt->seg_override = VCPU_SREG_ES;
5085 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005086 case 0x2e: /* CS override */
Marios Pomonis125ffc52019-12-11 12:47:50 -08005087 has_seg_override = true;
5088 ctxt->seg_override = VCPU_SREG_CS;
5089 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005090 case 0x36: /* SS override */
Marios Pomonis125ffc52019-12-11 12:47:50 -08005091 has_seg_override = true;
5092 ctxt->seg_override = VCPU_SREG_SS;
5093 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005094 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04005095 has_seg_override = true;
Marios Pomonis125ffc52019-12-11 12:47:50 -08005096 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005097 break;
5098 case 0x64: /* FS override */
Marios Pomonis125ffc52019-12-11 12:47:50 -08005099 has_seg_override = true;
5100 ctxt->seg_override = VCPU_SREG_FS;
5101 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005102 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04005103 has_seg_override = true;
Marios Pomonis125ffc52019-12-11 12:47:50 -08005104 ctxt->seg_override = VCPU_SREG_GS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005105 break;
5106 case 0x40 ... 0x4f: /* REX */
5107 if (mode != X86EMUL_MODE_PROT64)
5108 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005109 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005110 continue;
5111 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005112 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005113 break;
5114 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005115 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005116 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005117 break;
5118 default:
5119 goto done_prefixes;
5120 }
5121
5122 /* Any legacy prefix after a REX prefix nullifies its effect. */
5123
Avi Kivity9dac77f2011-06-01 15:34:25 +03005124 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005125 }
5126
5127done_prefixes:
5128
5129 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005130 if (ctxt->rex_prefix & 8)
5131 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005132
5133 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005134 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08005135 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005136 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02005137 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09005138 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03005139 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005140
5141 /* 0F_38 opcode map */
5142 if (ctxt->b == 0x38) {
5143 ctxt->opcode_len = 3;
5144 ctxt->b = insn_fetch(u8, ctxt);
5145 opcode = opcode_map_0f_38[ctxt->b];
5146 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005147 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03005148 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005149
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09005150 if (ctxt->d & ModRM)
5151 ctxt->modrm = insn_fetch(u8, ctxt);
5152
Nadav Amit7fe864d2014-06-02 18:34:03 +03005153 /* vex-prefix instructions are not implemented */
5154 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
Nadav Amitd14cb5d2014-11-02 11:54:58 +02005155 (mode == X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) == 0xc0)) {
Nadav Amit7fe864d2014-06-02 18:34:03 +03005156 ctxt->d = NotImpl;
5157 }
5158
Avi Kivity9dac77f2011-06-01 15:34:25 +03005159 while (ctxt->d & GroupMask) {
5160 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03005161 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005162 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03005163 opcode = opcode.u.group[goffset];
5164 break;
5165 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005166 goffset = (ctxt->modrm >> 3) & 7;
5167 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03005168 opcode = opcode.u.gdual->mod3[goffset];
5169 else
5170 opcode = opcode.u.gdual->mod012[goffset];
5171 break;
5172 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005173 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02005174 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03005175 break;
5176 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005177 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005178 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005179 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03005180 switch (simd_prefix) {
5181 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
5182 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
5183 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
5184 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
5185 }
5186 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02005187 case Escape:
Marios Pomonis3c9053a22019-12-11 12:47:41 -08005188 if (ctxt->modrm > 0xbf) {
5189 size_t size = ARRAY_SIZE(opcode.u.esc->high);
5190 u32 index = array_index_nospec(
5191 ctxt->modrm - 0xc0, size);
5192
5193 opcode = opcode.u.esc->high[index];
5194 } else {
Gleb Natapov045a2822012-12-20 16:57:43 +02005195 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
Marios Pomonis3c9053a22019-12-11 12:47:41 -08005196 }
Gleb Natapov045a2822012-12-20 16:57:43 +02005197 break;
Nadav Amit39f062f2014-11-26 15:47:18 +02005198 case InstrDual:
5199 if ((ctxt->modrm >> 6) == 3)
5200 opcode = opcode.u.idual->mod3;
5201 else
5202 opcode = opcode.u.idual->mod012;
5203 break;
Nadav Amit2276b512015-01-26 09:32:24 +02005204 case ModeDual:
5205 if (ctxt->mode == X86EMUL_MODE_PROT64)
5206 opcode = opcode.u.mdual->mode64;
5207 else
5208 opcode = opcode.u.mdual->mode32;
5209 break;
Avi Kivity46561642011-04-24 14:09:59 +03005210 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005211 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005212 }
Avi Kivity46561642011-04-24 14:09:59 +03005213
Avi Kivityb1ea50b2011-09-13 10:45:42 +03005214 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005215 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005216 }
5217
Paolo Bonzinie24186e2014-03-27 12:00:57 +01005218 /* Unrecognised? */
5219 if (ctxt->d == 0)
5220 return EMULATION_FAILED;
5221
Avi Kivity9dac77f2011-06-01 15:34:25 +03005222 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005223
Wanpeng Lib35491e2021-05-27 17:01:37 -07005224 if (unlikely(emulation_type & EMULTYPE_TRAP_UD) &&
5225 likely(!(ctxt->d & EmulateOnUD)))
Nadav Amit3a6095a2014-08-13 16:50:13 +03005226 return EMULATION_FAILED;
5227
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005228 if (unlikely(ctxt->d &
Nadav Amited9aad22014-11-02 11:55:00 +02005229 (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch|
5230 No16))) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005231 /*
5232 * These are copied unconditionally here, and checked unconditionally
5233 * in x86_emulate_insn.
5234 */
5235 ctxt->check_perm = opcode.check_perm;
5236 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005237
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005238 if (ctxt->d & NotImpl)
5239 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02005240
Nadav Amit58b70752014-10-24 11:35:09 +03005241 if (mode == X86EMUL_MODE_PROT64) {
5242 if (ctxt->op_bytes == 4 && (ctxt->d & Stack))
5243 ctxt->op_bytes = 8;
5244 else if (ctxt->d & NearBranch)
5245 ctxt->op_bytes = 8;
5246 }
Avi Kivity7f9b4b72010-08-01 14:46:54 +03005247
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005248 if (ctxt->d & Op3264) {
5249 if (mode == X86EMUL_MODE_PROT64)
5250 ctxt->op_bytes = 8;
5251 else
5252 ctxt->op_bytes = 4;
5253 }
5254
Nadav Amited9aad22014-11-02 11:55:00 +02005255 if ((ctxt->d & No16) && ctxt->op_bytes == 2)
5256 ctxt->op_bytes = 4;
5257
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005258 if (ctxt->d & Sse)
5259 ctxt->op_bytes = 16;
5260 else if (ctxt->d & Mmx)
5261 ctxt->op_bytes = 8;
5262 }
Avi Kivity1253791d2011-03-29 11:41:27 +02005263
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005264 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005265 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03005266 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04005267 if (!has_seg_override) {
5268 has_seg_override = true;
5269 ctxt->seg_override = ctxt->modrm_seg;
5270 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03005271 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03005272 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005273 if (rc != X86EMUL_CONTINUE)
5274 goto done;
5275
Bandan Das573e80f2014-04-16 12:46:13 -04005276 if (!has_seg_override)
5277 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005278
Bandan Das573e80f2014-04-16 12:46:13 -04005279 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005280
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005281 /*
5282 * Decode and fetch the source operand: register, memory
5283 * or immediate.
5284 */
Avi Kivity0fe59122011-09-13 10:45:47 +03005285 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03005286 if (rc != X86EMUL_CONTINUE)
5287 goto done;
5288
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005289 /*
5290 * Decode and fetch the second source operand: register, memory
5291 * or immediate.
5292 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03005293 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & 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 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03005298 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005299
Owen Hofmannd9092f52016-10-27 11:25:52 -07005300 if (ctxt->rip_relative && likely(ctxt->memopp))
Nadav Amit1c1c35a2014-11-19 17:43:09 +02005301 ctxt->memopp->addr.mem.ea = address_mask(ctxt,
5302 ctxt->memopp->addr.mem.ea + ctxt->_eip);
Avi Kivitycb16c342011-06-19 19:21:11 +03005303
Paolo Bonzinia430c912014-10-23 14:54:14 +02005304done:
Jan Dakinevichc8848ce2019-08-27 13:07:08 +00005305 if (rc == X86EMUL_PROPAGATE_FAULT)
5306 ctxt->have_exception = true;
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005307 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005308}
5309
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005310bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
5311{
5312 return ctxt->d & PageTable;
5313}
5314
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005315static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
5316{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005317 /* The second termination condition only applies for REPE
5318 * and REPNE. Test if the repeat string operation prefix is
5319 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
5320 * corresponding termination condition according to:
5321 * - if REPE/REPZ and ZF = 0 then done
5322 * - if REPNE/REPNZ and ZF = 1 then done
5323 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005324 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
5325 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
5326 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03005327 ((ctxt->eflags & X86_EFLAGS_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03005328 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03005329 ((ctxt->eflags & X86_EFLAGS_ZF) == X86_EFLAGS_ZF))))
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005330 return true;
5331
5332 return false;
5333}
5334
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005335static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
5336{
Radim Krčmářaabba3c2016-11-08 20:54:18 +01005337 int rc;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005338
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02005339 kvm_fpu_get();
Radim Krčmářaabba3c2016-11-08 20:54:18 +01005340 rc = asm_safe("fwait");
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02005341 kvm_fpu_put();
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005342
Radim Krčmářaabba3c2016-11-08 20:54:18 +01005343 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005344 return emulate_exception(ctxt, MF_VECTOR, 0, false);
5345
5346 return X86EMUL_CONTINUE;
5347}
5348
Sean Christophersonc0a21c32020-01-17 11:30:52 -08005349static void fetch_possible_mmx_operand(struct operand *op)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005350{
5351 if (op->type == OP_MM)
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02005352 kvm_read_mmx_reg(op->addr.mm, &op->mm_val);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005353}
5354
Sean Christopherson3009afc2020-01-21 20:43:39 -08005355static int fastop(struct x86_emulate_ctxt *ctxt, fastop_t fop)
Avi Kivitye28bbd42013-01-04 16:18:48 +02005356{
5357 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005358
Avi Kivityb9fa4092013-02-09 11:31:48 +02005359 if (!(ctxt->d & ByteOp))
5360 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005361
Peter Zijlstra1a29b5b2018-01-25 10:58:13 +01005362 asm("push %[flags]; popf; " CALL_NOSPEC " ; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005363 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
Peter Zijlstra1a29b5b2018-01-25 10:58:13 +01005364 [thunk_target]"+S"(fop), ASM_CALL_CONSTRAINT
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005365 : "c"(ctxt->src2.val));
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005366
Avi Kivitye28bbd42013-01-04 16:18:48 +02005367 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005368 if (!fop) /* exception is returned in fop variable */
5369 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02005370 return X86EMUL_CONTINUE;
5371}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005372
Bandan Das14985072014-04-16 12:46:09 -04005373void init_decode_cache(struct x86_emulate_ctxt *ctxt)
5374{
Bandan Das573e80f2014-04-16 12:46:13 -04005375 memset(&ctxt->rip_relative, 0,
5376 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04005377
Bandan Das14985072014-04-16 12:46:09 -04005378 ctxt->io_read.pos = 0;
5379 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04005380 ctxt->mem_read.end = 0;
5381}
5382
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09005383int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005384{
Mathias Krause0225fb52012-08-30 01:30:16 +02005385 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09005386 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005387 int saved_dst_type = ctxt->dst.type;
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005388 unsigned emul_flags;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005389
Avi Kivity9dac77f2011-06-01 15:34:25 +03005390 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04005391
Gleb Natapovd380a5e2010-02-10 14:21:36 +02005392 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005393 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02005394 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02005395 goto done;
5396 }
5397
Avi Kivity9dac77f2011-06-01 15:34:25 +03005398 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02005399 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03005400 goto done;
5401 }
5402
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005403 emul_flags = ctxt->ops->get_hflags(ctxt);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005404 if (unlikely(ctxt->d &
5405 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
5406 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
5407 (ctxt->d & Undefined)) {
5408 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005409 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005410 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005411
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005412 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
5413 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
5414 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02005415 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005416 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02005417
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005418 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
5419 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02005420 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005421 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02005422
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005423 if (ctxt->d & Mmx) {
5424 rc = flush_pending_x87_faults(ctxt);
5425 if (rc != X86EMUL_CONTINUE)
5426 goto done;
5427 /*
5428 * Now that we know the fpu is exception safe, we can fetch
5429 * operands from it.
5430 */
Sean Christophersonc0a21c32020-01-17 11:30:52 -08005431 fetch_possible_mmx_operand(&ctxt->src);
5432 fetch_possible_mmx_operand(&ctxt->src2);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005433 if (!(ctxt->d & Mov))
Sean Christophersonc0a21c32020-01-17 11:30:52 -08005434 fetch_possible_mmx_operand(&ctxt->dst);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005435 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02005436
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005437 if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && ctxt->intercept) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005438 rc = emulator_check_intercept(ctxt, ctxt->intercept,
5439 X86_ICPT_PRE_EXCEPT);
5440 if (rc != X86EMUL_CONTINUE)
5441 goto done;
5442 }
5443
Nadav Amit64a38292014-12-10 11:19:04 +02005444 /* Instruction can only be executed in protected mode */
5445 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
5446 rc = emulate_ud(ctxt);
5447 goto done;
5448 }
5449
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005450 /* Privileged instruction can be executed only in CPL=0 */
5451 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Nadav Amit68efa762014-06-18 17:19:35 +03005452 if (ctxt->d & PrivUD)
5453 rc = emulate_ud(ctxt);
5454 else
5455 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02005456 goto done;
5457 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005458
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005459 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04005460 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005461 rc = ctxt->check_perm(ctxt);
5462 if (rc != X86EMUL_CONTINUE)
5463 goto done;
5464 }
5465
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005466 if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005467 rc = emulator_check_intercept(ctxt, ctxt->intercept,
5468 X86_ICPT_POST_EXCEPT);
5469 if (rc != X86EMUL_CONTINUE)
5470 goto done;
5471 }
5472
5473 if (ctxt->rep_prefix && (ctxt->d & String)) {
5474 /* All REP prefixes have the same first termination condition */
5475 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Nadav Amit428e3d02015-04-28 13:06:01 +03005476 string_registers_quirk(ctxt);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005477 ctxt->eip = ctxt->_eip;
Nadav Amit0efb0442015-03-29 16:33:03 +03005478 ctxt->eflags &= ~X86_EFLAGS_RF;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005479 goto done;
5480 }
5481 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02005482 }
5483
Avi Kivity9dac77f2011-06-01 15:34:25 +03005484 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
5485 rc = segmented_read(ctxt, ctxt->src.addr.mem,
5486 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09005487 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005488 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005489 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005490 }
5491
Avi Kivity9dac77f2011-06-01 15:34:25 +03005492 if (ctxt->src2.type == OP_MEM) {
5493 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
5494 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02005495 if (rc != X86EMUL_CONTINUE)
5496 goto done;
5497 }
5498
Avi Kivity9dac77f2011-06-01 15:34:25 +03005499 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005500 goto special_insn;
5501
5502
Avi Kivity9dac77f2011-06-01 15:34:25 +03005503 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02005504 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005505 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
5506 &ctxt->dst.val, ctxt->dst.bytes);
Nadav Amitc205fb72014-12-25 02:52:16 +02005507 if (rc != X86EMUL_CONTINUE) {
Paolo Bonzinid44e1212015-02-09 10:02:05 +01005508 if (!(ctxt->d & NoWrite) &&
5509 rc == X86EMUL_PROPAGATE_FAULT &&
Nadav Amitc205fb72014-12-25 02:52:16 +02005510 ctxt->exception.vector == PF_VECTOR)
5511 ctxt->exception.error_code |= PFERR_WRITE_MASK;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02005512 goto done;
Nadav Amitc205fb72014-12-25 02:52:16 +02005513 }
Avi Kivity038e51d2007-01-22 20:40:40 -08005514 }
Paolo Bonzini4ff6f8e2015-02-12 17:04:47 +01005515 /* Copy full 64-bit value for CMPXCHG8B. */
5516 ctxt->dst.orig_val64 = ctxt->dst.val64;
Avi Kivity038e51d2007-01-22 20:40:40 -08005517
Avi Kivity018a98d2007-11-27 19:30:56 +02005518special_insn:
5519
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005520 if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03005521 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005522 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02005523 if (rc != X86EMUL_CONTINUE)
5524 goto done;
5525 }
5526
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03005527 if (ctxt->rep_prefix && (ctxt->d & String))
Nadav Amit0efb0442015-03-29 16:33:03 +03005528 ctxt->eflags |= X86_EFLAGS_RF;
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03005529 else
Nadav Amit0efb0442015-03-29 16:33:03 +03005530 ctxt->eflags &= ~X86_EFLAGS_RF;
Nadav Amit4467c3f2014-07-21 14:37:29 +03005531
Avi Kivity9dac77f2011-06-01 15:34:25 +03005532 if (ctxt->execute) {
Sean Christopherson3009afc2020-01-21 20:43:39 -08005533 if (ctxt->d & Fastop)
Qian Caib78a8552020-02-17 11:48:26 -05005534 rc = fastop(ctxt, ctxt->fop);
Sean Christopherson3009afc2020-01-21 20:43:39 -08005535 else
Miaohe Lin52db3692020-01-22 11:21:44 +08005536 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03005537 if (rc != X86EMUL_CONTINUE)
5538 goto done;
5539 goto writeback;
5540 }
5541
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02005542 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005543 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005544 else if (ctxt->opcode_len == 3)
5545 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005546
Avi Kivity9dac77f2011-06-01 15:34:25 +03005547 switch (ctxt->b) {
Gleb Natapovb2833e32009-04-12 13:36:30 +03005548 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005549 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005550 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005551 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03005552 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005553 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03005554 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03005555 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005556 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03005557 ctxt->dst.type = OP_NONE;
5558 else
5559 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09005560 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005561 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005562 switch (ctxt->op_bytes) {
5563 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
5564 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
5565 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005566 }
5567 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005568 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005569 rc = emulate_int(ctxt, 3);
5570 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005571 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005572 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005573 break;
5574 case 0xce: /* into */
Nadav Amit0efb0442015-03-29 16:33:03 +03005575 if (ctxt->eflags & X86_EFLAGS_OF)
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005576 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005577 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005578 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09005579 case 0xeb: /* jmp rel short */
Nadav Amit234f3ce2014-09-18 22:39:38 +03005580 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity9dac77f2011-06-01 15:34:25 +03005581 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005582 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005583 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03005584 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03005585 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005586 case 0xf5: /* cmc */
5587 /* complement carry flag from eflags reg */
Nadav Amit0efb0442015-03-29 16:33:03 +03005588 ctxt->eflags ^= X86_EFLAGS_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005589 break;
5590 case 0xf8: /* clc */
Nadav Amit0efb0442015-03-29 16:33:03 +03005591 ctxt->eflags &= ~X86_EFLAGS_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005592 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03005593 case 0xf9: /* stc */
Nadav Amit0efb0442015-03-29 16:33:03 +03005594 ctxt->eflags |= X86_EFLAGS_CF;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03005595 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005596 case 0xfc: /* cld */
Nadav Amit0efb0442015-03-29 16:33:03 +03005597 ctxt->eflags &= ~X86_EFLAGS_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005598 break;
5599 case 0xfd: /* std */
Nadav Amit0efb0442015-03-29 16:33:03 +03005600 ctxt->eflags |= X86_EFLAGS_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005601 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005602 default:
5603 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005604 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005605
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005606 if (rc != X86EMUL_CONTINUE)
5607 goto done;
5608
Avi Kivity018a98d2007-11-27 19:30:56 +02005609writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02005610 if (ctxt->d & SrcWrite) {
5611 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
5612 rc = writeback(ctxt, &ctxt->src);
5613 if (rc != X86EMUL_CONTINUE)
5614 goto done;
5615 }
Nadav Amitee212292014-06-15 16:12:58 +03005616 if (!(ctxt->d & NoWrite)) {
5617 rc = writeback(ctxt, &ctxt->dst);
5618 if (rc != X86EMUL_CONTINUE)
5619 goto done;
5620 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005621
Gleb Natapov5cd21912010-03-18 15:20:26 +02005622 /*
5623 * restore dst type in case the decoding will be reused
5624 * (happens for string instruction )
5625 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005626 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005627
Avi Kivity9dac77f2011-06-01 15:34:25 +03005628 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005629 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02005630
Avi Kivity9dac77f2011-06-01 15:34:25 +03005631 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005632 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02005633
Avi Kivity9dac77f2011-06-01 15:34:25 +03005634 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005635 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005636 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005637 if ((ctxt->d & SrcMask) == SrcSI)
5638 count = ctxt->src.count;
5639 else
5640 count = ctxt->dst.count;
Paolo Bonzini01485a22014-11-19 18:25:08 +01005641 register_address_increment(ctxt, VCPU_REGS_RCX, -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005642
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005643 if (!string_insn_completed(ctxt)) {
5644 /*
5645 * Re-enter guest when pio read ahead buffer is empty
5646 * or, if it is not used, after each 1024 iteration.
5647 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005648 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005649 (r->end == 0 || r->end != r->pos)) {
5650 /*
5651 * Reset read cache. Usually happens before
5652 * decode, but since instruction is restarted
5653 * we have to do it here.
5654 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005655 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005656 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005657 return EMULATION_RESTART;
5658 }
5659 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03005660 }
Nadav Amit0efb0442015-03-29 16:33:03 +03005661 ctxt->eflags &= ~X86_EFLAGS_RF;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005662 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005663
Avi Kivity9dac77f2011-06-01 15:34:25 +03005664 ctxt->eip = ctxt->_eip;
Paolo Bonzinifede8072020-04-27 11:55:59 -04005665 if (ctxt->mode != X86EMUL_MODE_PROT64)
5666 ctxt->eip = (u32)ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02005667
5668done:
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005669 if (rc == X86EMUL_PROPAGATE_FAULT) {
5670 WARN_ON(ctxt->exception.vector > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +02005671 ctxt->have_exception = true;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005672 }
Joerg Roedel775fde82011-04-04 12:39:24 +02005673 if (rc == X86EMUL_INTERCEPTED)
5674 return EMULATION_INTERCEPTED;
5675
Avi Kivitydd856ef2012-08-27 23:46:17 +03005676 if (rc == X86EMUL_CONTINUE)
5677 writeback_registers(ctxt);
5678
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005679 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005680
5681twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005682 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02005683 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02005684 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005685 break;
5686 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02005687 case 0x0d: /* GrpP (prefetch) */
5688 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02005689 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02005690 break;
5691 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005692 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02005693 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005694 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005695 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005696 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005697 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03005698 if (test_cc(ctxt->b, ctxt->eflags))
5699 ctxt->dst.val = ctxt->src.val;
Nadav Amitb91aa142015-03-30 15:39:19 +03005700 else if (ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005701 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08005702 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03005703 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03005704 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005705 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005706 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08005707 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005708 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08005709 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005710 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005711 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005712 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03005713 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005714 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005715 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005716 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005717 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03005718 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005719 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005720 default:
5721 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005722 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005723
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005724threebyte_insn:
5725
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005726 if (rc != X86EMUL_CONTINUE)
5727 goto done;
5728
Avi Kivity6aa8b732006-12-10 02:21:36 -08005729 goto writeback;
5730
5731cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02005732 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005733}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005734
5735void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
5736{
5737 invalidate_registers(ctxt);
5738}
5739
5740void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
5741{
5742 writeback_registers(ctxt);
5743}
Tom Lendacky0f89b202016-12-14 14:59:23 -05005744
5745bool emulator_can_use_gpa(struct x86_emulate_ctxt *ctxt)
5746{
5747 if (ctxt->rep_prefix && (ctxt->d & String))
5748 return false;
5749
5750 if (ctxt->d & TwoMemOp)
5751 return false;
5752
5753 return true;
5754}