blob: 10e16a70b361cd6539650d996c5f85520ac371f8 [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))
Ladi Prosek0234bf82017-10-11 16:54:40 +02002583 return X86EMUL_UNHANDLEABLE;
2584
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
2592 if (ret != X86EMUL_CONTINUE) {
2593 /* FIXME: should triple fault */
2594 return X86EMUL_UNHANDLEABLE;
2595 }
2596
Sean Christophersonc5833c72019-04-02 08:03:10 -07002597 ctxt->ops->post_leave_smm(ctxt);
2598
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002599 return X86EMUL_CONTINUE;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002600}
2601
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002602static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002603setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002604 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002605{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002606 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002607 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002608 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002609 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002610 cs->type = 0x0b; /* Read, Execute, Accessed */
2611 cs->s = 1;
2612 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002613 cs->p = 1;
2614 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002615 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002616
Gleb Natapov79168fd2010-04-28 19:15:30 +03002617 set_desc_base(ss, 0); /* flat segment */
2618 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002619 ss->g = 1; /* 4kb granularity */
2620 ss->s = 1;
2621 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002622 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002623 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002624 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002625 ss->l = 0;
2626 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002627}
2628
Avi Kivity1a18a692012-02-01 12:23:21 +02002629static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2630{
2631 u32 eax, ebx, ecx, edx;
2632
2633 eax = ecx = 0;
Sean Christophersonf91af512020-03-04 17:34:37 -08002634 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, true);
Sean Christopherson15608ed2020-03-04 17:34:32 -08002635 return is_guest_vendor_intel(ebx, ecx, edx);
Avi Kivity1a18a692012-02-01 12:23:21 +02002636}
2637
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002638static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2639{
Mathias Krause0225fb52012-08-30 01:30:16 +02002640 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002641 u32 eax, ebx, ecx, edx;
2642
2643 /*
2644 * syscall should always be enabled in longmode - so only become
2645 * vendor specific (cpuid) if other modes are active...
2646 */
2647 if (ctxt->mode == X86EMUL_MODE_PROT64)
2648 return true;
2649
2650 eax = 0x00000000;
2651 ecx = 0x00000000;
Sean Christophersonf91af512020-03-04 17:34:37 -08002652 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, true);
Avi Kivity0017f932012-06-07 14:10:16 +03002653 /*
Sean Christopherson15608ed2020-03-04 17:34:32 -08002654 * remark: Intel CPUs only support "syscall" in 64bit longmode. Also a
2655 * 64bit guest with a 32bit compat-app running will #UD !! While this
2656 * behaviour can be fixed (by emulating) into AMD response - CPUs of
2657 * AMD can't behave like Intel.
Avi Kivity0017f932012-06-07 14:10:16 +03002658 */
Sean Christopherson15608ed2020-03-04 17:34:32 -08002659 if (is_guest_vendor_intel(ebx, ecx, edx))
Avi Kivity0017f932012-06-07 14:10:16 +03002660 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002661
Sean Christopherson15608ed2020-03-04 17:34:32 -08002662 if (is_guest_vendor_amd(ebx, ecx, edx) ||
2663 is_guest_vendor_hygon(ebx, ecx, edx))
Pu Wenb8f4abb2018-09-23 17:36:31 +08002664 return true;
2665
2666 /*
2667 * default: (not Intel, not AMD, not Hygon), apply Intel's
2668 * stricter rules...
2669 */
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002670 return false;
2671}
2672
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002673static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002674{
Mathias Krause0225fb52012-08-30 01:30:16 +02002675 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002676 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002677 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002678 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002679 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002680
2681 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002682 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002683 ctxt->mode == X86EMUL_MODE_VM86)
2684 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002685
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002686 if (!(em_syscall_is_enabled(ctxt)))
2687 return emulate_ud(ctxt);
2688
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002689 ops->get_msr(ctxt, MSR_EFER, &efer);
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002690 if (!(efer & EFER_SCE))
2691 return emulate_ud(ctxt);
2692
Miaohe Lin5b4ce932019-11-09 16:58:54 +08002693 setup_syscalls_segments(ctxt, &cs, &ss);
Avi Kivity717746e2011-04-20 13:37:53 +03002694 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002695 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002696 cs_sel = (u16)(msr_data & 0xfffc);
2697 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002698
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002699 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002700 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002701 cs.l = 1;
2702 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002703 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2704 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002705
Avi Kivitydd856ef2012-08-27 23:46:17 +03002706 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002707 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002708#ifdef CONFIG_X86_64
Nadav Amit6c6cb692014-07-21 14:37:30 +03002709 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002710
Avi Kivity717746e2011-04-20 13:37:53 +03002711 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002712 ctxt->mode == X86EMUL_MODE_PROT64 ?
2713 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002714 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002715
Avi Kivity717746e2011-04-20 13:37:53 +03002716 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Nadav Amit6c6cb692014-07-21 14:37:30 +03002717 ctxt->eflags &= ~msr_data;
Wanpeng Li35fd68a2015-04-08 14:08:14 +08002718 ctxt->eflags |= X86_EFLAGS_FIXED;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002719#endif
2720 } else {
2721 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002722 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002723 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002724
Nadav Amit0efb0442015-03-29 16:33:03 +03002725 ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002726 }
2727
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02002728 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002729 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002730}
2731
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002732static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002733{
Mathias Krause0225fb52012-08-30 01:30:16 +02002734 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002735 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002736 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002737 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002738 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002739
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002740 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002741 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002742 if (ctxt->mode == X86EMUL_MODE_REAL)
2743 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002744
Avi Kivity1a18a692012-02-01 12:23:21 +02002745 /*
2746 * Not recognized on AMD in compat mode (but is recognized in legacy
2747 * mode).
2748 */
Nadav Amitf3747372015-01-01 23:11:11 +02002749 if ((ctxt->mode != X86EMUL_MODE_PROT64) && (efer & EFER_LMA)
Avi Kivity1a18a692012-02-01 12:23:21 +02002750 && !vendor_intel(ctxt))
2751 return emulate_ud(ctxt);
2752
Nadav Amitb2c9d432014-11-02 11:55:01 +02002753 /* sysenter/sysexit have not been tested in 64bit mode. */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002754 if (ctxt->mode == X86EMUL_MODE_PROT64)
Nadav Amitb2c9d432014-11-02 11:55:01 +02002755 return X86EMUL_UNHANDLEABLE;
Andre Przywara8c604352009-06-18 12:56:01 +02002756
Avi Kivity717746e2011-04-20 13:37:53 +03002757 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002758 if ((msr_data & 0xfffc) == 0x0)
2759 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002760
Miaohe Lin5b4ce932019-11-09 16:58:54 +08002761 setup_syscalls_segments(ctxt, &cs, &ss);
Nadav Amit0efb0442015-03-29 16:33:03 +03002762 ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
Nadav Amitb32a9912015-03-29 16:33:04 +03002763 cs_sel = (u16)msr_data & ~SEGMENT_RPL_MASK;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002764 ss_sel = cs_sel + 8;
Nadav Amitf3747372015-01-01 23:11:11 +02002765 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002766 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002767 cs.l = 1;
2768 }
2769
Avi Kivity1aa36612011-04-27 13:20:30 +03002770 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2771 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002772
Avi Kivity717746e2011-04-20 13:37:53 +03002773 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002774 ctxt->_eip = (efer & EFER_LMA) ? msr_data : (u32)msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002775
Avi Kivity717746e2011-04-20 13:37:53 +03002776 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002777 *reg_write(ctxt, VCPU_REGS_RSP) = (efer & EFER_LMA) ? msr_data :
2778 (u32)msr_data;
Sean Christopherson943dea82021-02-02 08:55:46 -08002779 if (efer & EFER_LMA)
2780 ctxt->mode = X86EMUL_MODE_PROT64;
Andre Przywara8c604352009-06-18 12:56:01 +02002781
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002782 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002783}
2784
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002785static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002786{
Mathias Krause0225fb52012-08-30 01:30:16 +02002787 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002788 struct desc_struct cs, ss;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002789 u64 msr_data, rcx, rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002790 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002791 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002792
Gleb Natapova0044752010-02-10 14:21:31 +02002793 /* inject #GP if in real mode or Virtual 8086 mode */
2794 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002795 ctxt->mode == X86EMUL_MODE_VM86)
2796 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002797
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002798 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002799
Avi Kivity9dac77f2011-06-01 15:34:25 +03002800 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002801 usermode = X86EMUL_MODE_PROT64;
2802 else
2803 usermode = X86EMUL_MODE_PROT32;
2804
Nadav Amit234f3ce2014-09-18 22:39:38 +03002805 rcx = reg_read(ctxt, VCPU_REGS_RCX);
2806 rdx = reg_read(ctxt, VCPU_REGS_RDX);
2807
Andre Przywara4668f052009-06-18 12:56:02 +02002808 cs.dpl = 3;
2809 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002810 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002811 switch (usermode) {
2812 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002813 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002814 if ((msr_data & 0xfffc) == 0x0)
2815 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002816 ss_sel = (u16)(msr_data + 24);
Nadav Amitbf0b6822014-09-18 22:39:45 +03002817 rcx = (u32)rcx;
2818 rdx = (u32)rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002819 break;
2820 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002821 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002822 if (msr_data == 0x0)
2823 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002824 ss_sel = cs_sel + 8;
2825 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002826 cs.l = 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08002827 if (emul_is_noncanonical_address(rcx, ctxt) ||
2828 emul_is_noncanonical_address(rdx, ctxt))
Nadav Amit234f3ce2014-09-18 22:39:38 +03002829 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002830 break;
2831 }
Nadav Amitb32a9912015-03-29 16:33:04 +03002832 cs_sel |= SEGMENT_RPL_MASK;
2833 ss_sel |= SEGMENT_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002834
Avi Kivity1aa36612011-04-27 13:20:30 +03002835 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2836 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002837
Nadav Amit234f3ce2014-09-18 22:39:38 +03002838 ctxt->_eip = rdx;
2839 *reg_write(ctxt, VCPU_REGS_RSP) = rcx;
Andre Przywara4668f052009-06-18 12:56:02 +02002840
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002841 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002842}
2843
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002844static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002845{
2846 int iopl;
2847 if (ctxt->mode == X86EMUL_MODE_REAL)
2848 return false;
2849 if (ctxt->mode == X86EMUL_MODE_VM86)
2850 return true;
Nadav Amit0efb0442015-03-29 16:33:03 +03002851 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002852 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002853}
2854
Liran Alon9a29d4492018-03-12 13:12:48 +02002855#define VMWARE_PORT_VMPORT (0x5658)
2856#define VMWARE_PORT_VMRPC (0x5659)
2857
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002858static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002859 u16 port, u16 len)
2860{
Mathias Krause0225fb52012-08-30 01:30:16 +02002861 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002862 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002863 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002864 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002865 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002866 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002867 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002868
Liran Alon9a29d4492018-03-12 13:12:48 +02002869 /*
2870 * VMware allows access to these ports even if denied
2871 * by TSS I/O permission bitmap. Mimic behavior.
2872 */
2873 if (enable_vmware_backdoor &&
2874 ((port == VMWARE_PORT_VMPORT) || (port == VMWARE_PORT_VMRPC)))
2875 return true;
2876
Avi Kivity1aa36612011-04-27 13:20:30 +03002877 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002878 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002879 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002880 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002881 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002882 base = get_desc_base(&tr_seg);
2883#ifdef CONFIG_X86_64
2884 base |= ((u64)base3) << 32;
2885#endif
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02002886 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL, true);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002887 if (r != X86EMUL_CONTINUE)
2888 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002889 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002890 return false;
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02002891 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL, true);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002892 if (r != X86EMUL_CONTINUE)
2893 return false;
2894 if ((perm >> bit_idx) & mask)
2895 return false;
2896 return true;
2897}
2898
2899static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002900 u16 port, u16 len)
2901{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002902 if (ctxt->perm_ok)
2903 return true;
2904
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002905 if (emulator_bad_iopl(ctxt))
2906 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002907 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002908
2909 ctxt->perm_ok = true;
2910
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002911 return true;
2912}
2913
Nadav Amit428e3d02015-04-28 13:06:01 +03002914static void string_registers_quirk(struct x86_emulate_ctxt *ctxt)
2915{
2916 /*
2917 * Intel CPUs mask the counter and pointers in quite strange
2918 * manner when ECX is zero due to REP-string optimizations.
2919 */
2920#ifdef CONFIG_X86_64
2921 if (ctxt->ad_bytes != 4 || !vendor_intel(ctxt))
2922 return;
2923
2924 *reg_write(ctxt, VCPU_REGS_RCX) = 0;
2925
2926 switch (ctxt->b) {
2927 case 0xa4: /* movsb */
2928 case 0xa5: /* movsd/w */
2929 *reg_rmw(ctxt, VCPU_REGS_RSI) &= (u32)-1;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002930 fallthrough;
Nadav Amit428e3d02015-04-28 13:06:01 +03002931 case 0xaa: /* stosb */
2932 case 0xab: /* stosd/w */
2933 *reg_rmw(ctxt, VCPU_REGS_RDI) &= (u32)-1;
2934 }
2935#endif
2936}
2937
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002938static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002939 struct tss_segment_16 *tss)
2940{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002941 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002942 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002943 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2944 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2945 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2946 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2947 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2948 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2949 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2950 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002951
Avi Kivity1aa36612011-04-27 13:20:30 +03002952 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2953 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2954 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2955 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2956 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002957}
2958
2959static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002960 struct tss_segment_16 *tss)
2961{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002962 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002963 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002964
Avi Kivity9dac77f2011-06-01 15:34:25 +03002965 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002966 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002967 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2968 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2969 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2970 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2971 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2972 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2973 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2974 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002975
2976 /*
2977 * SDM says that segment selectors are loaded before segment
2978 * descriptors
2979 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002980 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2981 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2982 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2983 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2984 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002985
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002986 cpl = tss->cs & 3;
2987
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002988 /*
Guo Chaofc058682012-06-28 15:19:51 +08002989 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002990 * it is handled in a context of new task
2991 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002992 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002993 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002994 if (ret != X86EMUL_CONTINUE)
2995 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002996 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002997 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002998 if (ret != X86EMUL_CONTINUE)
2999 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003000 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003001 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003002 if (ret != X86EMUL_CONTINUE)
3003 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003004 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003005 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003006 if (ret != X86EMUL_CONTINUE)
3007 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003008 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003009 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003010 if (ret != X86EMUL_CONTINUE)
3011 return ret;
3012
3013 return X86EMUL_CONTINUE;
3014}
3015
3016static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003017 u16 tss_selector, u16 old_tss_sel,
3018 ulong old_tss_base, struct desc_struct *new_desc)
3019{
3020 struct tss_segment_16 tss_seg;
3021 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003022 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003023
Jordan Borgner0e96f312018-10-28 12:58:28 +00003024 ret = linear_read_system(ctxt, old_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003025 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003026 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003027
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003028 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003029
Jordan Borgner0e96f312018-10-28 12:58:28 +00003030 ret = linear_write_system(ctxt, old_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003031 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003032 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003033
Jordan Borgner0e96f312018-10-28 12:58:28 +00003034 ret = linear_read_system(ctxt, new_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003035 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003036 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003037
3038 if (old_tss_sel != 0xffff) {
3039 tss_seg.prev_task_link = old_tss_sel;
3040
Paolo Bonzini79367a62018-06-06 16:43:02 +02003041 ret = linear_write_system(ctxt, new_tss_base,
3042 &tss_seg.prev_task_link,
Jordan Borgner0e96f312018-10-28 12:58:28 +00003043 sizeof(tss_seg.prev_task_link));
Avi Kivitydb297e32010-11-22 17:53:24 +02003044 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003045 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003046 }
3047
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003048 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003049}
3050
3051static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003052 struct tss_segment_32 *tss)
3053{
Nadav Amit5c7411e2014-04-07 18:37:47 +03003054 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003055 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003056 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003057 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
3058 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
3059 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
3060 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
3061 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
3062 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
3063 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
3064 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003065
Avi Kivity1aa36612011-04-27 13:20:30 +03003066 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
3067 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
3068 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
3069 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
3070 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
3071 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003072}
3073
3074static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003075 struct tss_segment_32 *tss)
3076{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003077 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003078 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003079
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003080 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003081 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003082 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003083 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01003084
3085 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03003086 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
3087 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
3088 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
3089 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
3090 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
3091 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
3092 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
3093 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003094
3095 /*
3096 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003097 * descriptors. This is important because CPL checks will
3098 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003099 */
Avi Kivity1aa36612011-04-27 13:20:30 +03003100 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
3101 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
3102 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
3103 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
3104 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
3105 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
3106 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003107
3108 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01003109 * If we're switching between Protected Mode and VM86, we need to make
3110 * sure to update the mode before loading the segment descriptors so
3111 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01003112 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003113 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01003114 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003115 cpl = 3;
3116 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01003117 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003118 cpl = tss->cs & 3;
3119 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01003120
3121 /*
Ingo Molnard9f6e122021-03-18 15:28:01 +01003122 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003123 * it is handled in a context of new task
3124 */
Nadav Amitd1442d82014-09-18 22:39:39 +03003125 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003126 cpl, X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003127 if (ret != X86EMUL_CONTINUE)
3128 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003129 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003130 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003131 if (ret != X86EMUL_CONTINUE)
3132 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003133 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003134 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003135 if (ret != X86EMUL_CONTINUE)
3136 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003137 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003138 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003139 if (ret != X86EMUL_CONTINUE)
3140 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003141 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003142 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003143 if (ret != X86EMUL_CONTINUE)
3144 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003145 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003146 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003147 if (ret != X86EMUL_CONTINUE)
3148 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003149 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003150 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003151
Eugene Korenevsky2f729b12015-03-29 01:27:17 +03003152 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003153}
3154
3155static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003156 u16 tss_selector, u16 old_tss_sel,
3157 ulong old_tss_base, struct desc_struct *new_desc)
3158{
3159 struct tss_segment_32 tss_seg;
3160 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003161 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03003162 u32 eip_offset = offsetof(struct tss_segment_32, eip);
3163 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003164
Jordan Borgner0e96f312018-10-28 12:58:28 +00003165 ret = linear_read_system(ctxt, old_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003166 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003167 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003168
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003169 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003170
Nadav Amit5c7411e2014-04-07 18:37:47 +03003171 /* Only GP registers and segment selectors are saved */
Paolo Bonzini79367a62018-06-06 16:43:02 +02003172 ret = linear_write_system(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
3173 ldt_sel_offset - eip_offset);
Avi Kivitydb297e32010-11-22 17:53:24 +02003174 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003175 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003176
Jordan Borgner0e96f312018-10-28 12:58:28 +00003177 ret = linear_read_system(ctxt, new_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003178 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003179 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003180
3181 if (old_tss_sel != 0xffff) {
3182 tss_seg.prev_task_link = old_tss_sel;
3183
Paolo Bonzini79367a62018-06-06 16:43:02 +02003184 ret = linear_write_system(ctxt, new_tss_base,
3185 &tss_seg.prev_task_link,
Jordan Borgner0e96f312018-10-28 12:58:28 +00003186 sizeof(tss_seg.prev_task_link));
Avi Kivitydb297e32010-11-22 17:53:24 +02003187 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003188 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003189 }
3190
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003191 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003192}
3193
3194static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003195 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003196 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003197{
Mathias Krause0225fb52012-08-30 01:30:16 +02003198 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003199 struct desc_struct curr_tss_desc, next_tss_desc;
3200 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03003201 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003202 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03003203 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02003204 u32 desc_limit;
Nadav Amit3db176d2015-04-19 21:12:59 +03003205 ulong desc_addr, dr7;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003206
3207 /* FIXME: old_tss_base == ~0 ? */
3208
Avi Kivitye9194642012-06-13 16:29:39 +03003209 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003210 if (ret != X86EMUL_CONTINUE)
3211 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03003212 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003213 if (ret != X86EMUL_CONTINUE)
3214 return ret;
3215
3216 /* FIXME: check that next_tss_desc is tss */
3217
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003218 /*
3219 * Check privileges. The three cases are task switch caused by...
3220 *
3221 * 1. jmp/call/int to task gate: Check against DPL of the task gate
3222 * 2. Exception/IRQ/iret: No check is performed
Nadav Amit2c2ca2d2014-11-02 11:54:57 +02003223 * 3. jmp/call to TSS/task-gate: No check is performed since the
3224 * hardware checks it before exiting.
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003225 */
3226 if (reason == TASK_SWITCH_GATE) {
3227 if (idt_index != -1) {
3228 /* Software interrupts */
3229 struct desc_struct task_gate_desc;
3230 int dpl;
3231
3232 ret = read_interrupt_descriptor(ctxt, idt_index,
3233 &task_gate_desc);
3234 if (ret != X86EMUL_CONTINUE)
3235 return ret;
3236
3237 dpl = task_gate_desc.dpl;
3238 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
3239 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
3240 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003241 }
3242
Gleb Natapovceffb452010-03-18 15:20:19 +02003243 desc_limit = desc_limit_scaled(&next_tss_desc);
3244 if (!next_tss_desc.p ||
3245 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
3246 desc_limit < 0x2b)) {
Paolo Bonzini592f0852014-08-20 10:05:08 +02003247 return emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003248 }
3249
3250 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
3251 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003252 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003253 }
3254
3255 if (reason == TASK_SWITCH_IRET)
3256 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
3257
3258 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08003259 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003260 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
3261 old_tss_sel = 0xffff;
3262
3263 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003264 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003265 old_tss_base, &next_tss_desc);
3266 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003267 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003268 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02003269 if (ret != X86EMUL_CONTINUE)
3270 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003271
3272 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
3273 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
3274
3275 if (reason != TASK_SWITCH_IRET) {
3276 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003277 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003278 }
3279
Avi Kivity717746e2011-04-20 13:37:53 +03003280 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03003281 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003282
Jan Kiszkae269fb22010-04-14 15:51:09 +02003283 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003284 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
3285 ctxt->lock_prefix = 0;
3286 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003287 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02003288 }
3289
Nadav Amit3db176d2015-04-19 21:12:59 +03003290 ops->get_dr(ctxt, 7, &dr7);
3291 ops->set_dr(ctxt, 7, dr7 & ~(DR_LOCAL_ENABLE_MASK | DR_LOCAL_SLOWDOWN));
3292
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003293 return ret;
3294}
3295
3296int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003297 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003298 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003299{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003300 int rc;
3301
Avi Kivitydd856ef2012-08-27 23:46:17 +03003302 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003303 ctxt->_eip = ctxt->eip;
3304 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003305
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003306 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003307 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003308
Avi Kivitydd856ef2012-08-27 23:46:17 +03003309 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003310 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003311 writeback_registers(ctxt);
3312 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003313
Gleb Natapova0c0ab22011-03-28 16:57:49 +02003314 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003315}
3316
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03003317static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
3318 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02003319{
Nadav Amit0efb0442015-03-29 16:33:03 +03003320 int df = (ctxt->eflags & X86_EFLAGS_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02003321
Paolo Bonzini01485a22014-11-19 18:25:08 +01003322 register_address_increment(ctxt, reg, df * op->bytes);
3323 op->addr.mem.ea = register_address(ctxt, reg);
Gleb Natapova682e352010-03-18 15:20:21 +02003324}
3325
Avi Kivity7af04fc2010-08-18 14:16:35 +03003326static int em_das(struct x86_emulate_ctxt *ctxt)
3327{
Avi Kivity7af04fc2010-08-18 14:16:35 +03003328 u8 al, old_al;
3329 bool af, cf, old_cf;
3330
3331 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003332 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03003333
3334 old_al = al;
3335 old_cf = cf;
3336 cf = false;
3337 af = ctxt->eflags & X86_EFLAGS_AF;
3338 if ((al & 0x0f) > 9 || af) {
3339 al -= 6;
3340 cf = old_cf | (al >= 250);
3341 af = true;
3342 } else {
3343 af = false;
3344 }
3345 if (old_al > 0x99 || old_cf) {
3346 al -= 0x60;
3347 cf = true;
3348 }
3349
Avi Kivity9dac77f2011-06-01 15:34:25 +03003350 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03003351 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003352 ctxt->src.type = OP_IMM;
3353 ctxt->src.val = 0;
3354 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02003355 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03003356 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
3357 if (cf)
3358 ctxt->eflags |= X86_EFLAGS_CF;
3359 if (af)
3360 ctxt->eflags |= X86_EFLAGS_AF;
3361 return X86EMUL_CONTINUE;
3362}
3363
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003364static int em_aam(struct x86_emulate_ctxt *ctxt)
3365{
3366 u8 al, ah;
3367
3368 if (ctxt->src.val == 0)
3369 return emulate_de(ctxt);
3370
3371 al = ctxt->dst.val & 0xff;
3372 ah = al / ctxt->src.val;
3373 al %= ctxt->src.val;
3374
3375 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
3376
3377 /* Set PF, ZF, SF */
3378 ctxt->src.type = OP_IMM;
3379 ctxt->src.val = 0;
3380 ctxt->src.bytes = 1;
3381 fastop(ctxt, em_or);
3382
3383 return X86EMUL_CONTINUE;
3384}
3385
Gleb Natapov7f662272012-12-10 11:42:30 +02003386static int em_aad(struct x86_emulate_ctxt *ctxt)
3387{
3388 u8 al = ctxt->dst.val & 0xff;
3389 u8 ah = (ctxt->dst.val >> 8) & 0xff;
3390
3391 al = (al + (ah * ctxt->src.val)) & 0xff;
3392
3393 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
3394
Gleb Natapovf583c292013-02-13 17:50:39 +02003395 /* Set PF, ZF, SF */
3396 ctxt->src.type = OP_IMM;
3397 ctxt->src.val = 0;
3398 ctxt->src.bytes = 1;
3399 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02003400
3401 return X86EMUL_CONTINUE;
3402}
3403
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003404static int em_call(struct x86_emulate_ctxt *ctxt)
3405{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003406 int rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003407 long rel = ctxt->src.val;
3408
3409 ctxt->src.val = (unsigned long)ctxt->_eip;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003410 rc = jmp_rel(ctxt, rel);
3411 if (rc != X86EMUL_CONTINUE)
3412 return rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003413 return em_push(ctxt);
3414}
3415
Avi Kivity0ef753b2010-08-18 14:51:45 +03003416static int em_call_far(struct x86_emulate_ctxt *ctxt)
3417{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003418 u16 sel, old_cs;
3419 ulong old_eip;
3420 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03003421 struct desc_struct old_desc, new_desc;
3422 const struct x86_emulate_ops *ops = ctxt->ops;
3423 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amit82268082015-01-26 09:32:27 +02003424 enum x86emul_mode prev_mode = ctxt->mode;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003425
Avi Kivity9dac77f2011-06-01 15:34:25 +03003426 old_eip = ctxt->_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003427 ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003428
Avi Kivity9dac77f2011-06-01 15:34:25 +03003429 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003430 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
3431 X86_TRANSFER_CALL_JMP, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003432 if (rc != X86EMUL_CONTINUE)
Nadav Amit80976db2014-12-25 02:52:20 +02003433 return rc;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003434
Nadav Amitd50eaa12014-11-19 17:43:11 +02003435 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003436 if (rc != X86EMUL_CONTINUE)
3437 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003438
Avi Kivity9dac77f2011-06-01 15:34:25 +03003439 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003440 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003441 if (rc != X86EMUL_CONTINUE)
Nadav Amitd1442d82014-09-18 22:39:39 +03003442 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003443
Avi Kivity9dac77f2011-06-01 15:34:25 +03003444 ctxt->src.val = old_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003445 rc = em_push(ctxt);
3446 /* If we failed, we tainted the memory, but the very least we should
3447 restore cs */
Nadav Amit82268082015-01-26 09:32:27 +02003448 if (rc != X86EMUL_CONTINUE) {
3449 pr_warn_once("faulting far call emulation tainted memory\n");
Nadav Amitd1442d82014-09-18 22:39:39 +03003450 goto fail;
Nadav Amit82268082015-01-26 09:32:27 +02003451 }
Nadav Amitd1442d82014-09-18 22:39:39 +03003452 return rc;
3453fail:
3454 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
Nadav Amit82268082015-01-26 09:32:27 +02003455 ctxt->mode = prev_mode;
Nadav Amitd1442d82014-09-18 22:39:39 +03003456 return rc;
3457
Avi Kivity0ef753b2010-08-18 14:51:45 +03003458}
3459
Avi Kivity40ece7c2010-08-18 15:12:09 +03003460static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3461{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003462 int rc;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003463 unsigned long eip;
Avi Kivity40ece7c2010-08-18 15:12:09 +03003464
Nadav Amit234f3ce2014-09-18 22:39:38 +03003465 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
3466 if (rc != X86EMUL_CONTINUE)
3467 return rc;
3468 rc = assign_eip_near(ctxt, eip);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003469 if (rc != X86EMUL_CONTINUE)
3470 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003471 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003472 return X86EMUL_CONTINUE;
3473}
3474
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003475static int em_xchg(struct x86_emulate_ctxt *ctxt)
3476{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003477 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003478 ctxt->src.val = ctxt->dst.val;
3479 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003480
3481 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003482 ctxt->dst.val = ctxt->src.orig_val;
3483 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003484 return X86EMUL_CONTINUE;
3485}
3486
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003487static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3488{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003489 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003490 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003491}
3492
Avi Kivity61429142010-08-19 15:13:00 +03003493static int em_cwd(struct x86_emulate_ctxt *ctxt)
3494{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003495 ctxt->dst.type = OP_REG;
3496 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003497 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003498 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003499
3500 return X86EMUL_CONTINUE;
3501}
3502
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02003503static int em_rdpid(struct x86_emulate_ctxt *ctxt)
3504{
3505 u64 tsc_aux = 0;
3506
3507 if (ctxt->ops->get_msr(ctxt, MSR_TSC_AUX, &tsc_aux))
Robert Hooa9e2e0a2020-08-28 10:23:42 +08003508 return emulate_ud(ctxt);
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02003509 ctxt->dst.val = tsc_aux;
3510 return X86EMUL_CONTINUE;
3511}
3512
Avi Kivity48bb5d32010-08-18 18:54:34 +03003513static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3514{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003515 u64 tsc = 0;
3516
Avi Kivity717746e2011-04-20 13:37:53 +03003517 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003518 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3519 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003520 return X86EMUL_CONTINUE;
3521}
3522
Avi Kivity222d21a2011-11-10 14:57:30 +02003523static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3524{
3525 u64 pmc;
3526
Avi Kivitydd856ef2012-08-27 23:46:17 +03003527 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003528 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003529 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3530 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003531 return X86EMUL_CONTINUE;
3532}
3533
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003534static int em_mov(struct x86_emulate_ctxt *ctxt)
3535{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01003536 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003537 return X86EMUL_CONTINUE;
3538}
3539
Borislav Petkov84cffe42013-10-29 12:54:56 +01003540static int em_movbe(struct x86_emulate_ctxt *ctxt)
3541{
Borislav Petkov84cffe42013-10-29 12:54:56 +01003542 u16 tmp;
3543
Sean Christopherson5ae78e92019-12-17 13:32:38 -08003544 if (!ctxt->ops->guest_has_movbe(ctxt))
Borislav Petkov84cffe42013-10-29 12:54:56 +01003545 return emulate_ud(ctxt);
3546
3547 switch (ctxt->op_bytes) {
3548 case 2:
3549 /*
3550 * From MOVBE definition: "...When the operand size is 16 bits,
3551 * the upper word of the destination register remains unchanged
3552 * ..."
3553 *
3554 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3555 * rules so we have to do the operation almost per hand.
3556 */
3557 tmp = (u16)ctxt->src.val;
3558 ctxt->dst.val &= ~0xffffUL;
3559 ctxt->dst.val |= (unsigned long)swab16(tmp);
3560 break;
3561 case 4:
3562 ctxt->dst.val = swab32((u32)ctxt->src.val);
3563 break;
3564 case 8:
3565 ctxt->dst.val = swab64(ctxt->src.val);
3566 break;
3567 default:
Paolo Bonzini592f0852014-08-20 10:05:08 +02003568 BUG();
Borislav Petkov84cffe42013-10-29 12:54:56 +01003569 }
3570 return X86EMUL_CONTINUE;
3571}
3572
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003573static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3574{
3575 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3576 return emulate_gp(ctxt, 0);
3577
3578 /* Disable writeback. */
3579 ctxt->dst.type = OP_NONE;
3580 return X86EMUL_CONTINUE;
3581}
3582
3583static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3584{
3585 unsigned long val;
3586
3587 if (ctxt->mode == X86EMUL_MODE_PROT64)
3588 val = ctxt->src.val & ~0ULL;
3589 else
3590 val = ctxt->src.val & ~0U;
3591
3592 /* #UD condition is already handled. */
3593 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3594 return emulate_gp(ctxt, 0);
3595
3596 /* Disable writeback. */
3597 ctxt->dst.type = OP_NONE;
3598 return X86EMUL_CONTINUE;
3599}
3600
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003601static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3602{
Alexander Graf1ae09952020-09-25 16:34:16 +02003603 u64 msr_index = reg_read(ctxt, VCPU_REGS_RCX);
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003604 u64 msr_data;
Alexander Graf1ae09952020-09-25 16:34:16 +02003605 int r;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003606
Avi Kivitydd856ef2012-08-27 23:46:17 +03003607 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3608 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
Alexander Graf1ae09952020-09-25 16:34:16 +02003609 r = ctxt->ops->set_msr(ctxt, msr_index, msr_data);
3610
3611 if (r == X86EMUL_IO_NEEDED)
3612 return r;
3613
Maxim Levitsky7dffeca2020-10-01 14:29:52 +03003614 if (r > 0)
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003615 return emulate_gp(ctxt, 0);
3616
Maxim Levitsky7dffeca2020-10-01 14:29:52 +03003617 return r < 0 ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003618}
3619
3620static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3621{
Alexander Graf1ae09952020-09-25 16:34:16 +02003622 u64 msr_index = reg_read(ctxt, VCPU_REGS_RCX);
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003623 u64 msr_data;
Alexander Graf1ae09952020-09-25 16:34:16 +02003624 int r;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003625
Alexander Graf1ae09952020-09-25 16:34:16 +02003626 r = ctxt->ops->get_msr(ctxt, msr_index, &msr_data);
3627
3628 if (r == X86EMUL_IO_NEEDED)
3629 return r;
3630
3631 if (r)
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003632 return emulate_gp(ctxt, 0);
3633
Avi Kivitydd856ef2012-08-27 23:46:17 +03003634 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3635 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003636 return X86EMUL_CONTINUE;
3637}
3638
Paolo Bonzinidd307d02016-07-12 10:35:51 +02003639static int em_store_sreg(struct x86_emulate_ctxt *ctxt, int segment)
3640{
3641 if (segment > VCPU_SREG_GS &&
3642 (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_UMIP) &&
3643 ctxt->ops->cpl(ctxt) > 0)
3644 return emulate_gp(ctxt, 0);
3645
3646 ctxt->dst.val = get_segment_selector(ctxt, segment);
3647 if (ctxt->dst.bytes == 4 && ctxt->dst.type == OP_MEM)
3648 ctxt->dst.bytes = 2;
3649 return X86EMUL_CONTINUE;
3650}
3651
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003652static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3653{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003654 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003655 return emulate_ud(ctxt);
3656
Paolo Bonzinidd307d02016-07-12 10:35:51 +02003657 return em_store_sreg(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003658}
3659
3660static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3661{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003662 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003663
Avi Kivity9dac77f2011-06-01 15:34:25 +03003664 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003665 return emulate_ud(ctxt);
3666
Avi Kivity9dac77f2011-06-01 15:34:25 +03003667 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003668 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3669
3670 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003671 ctxt->dst.type = OP_NONE;
3672 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003673}
3674
Paolo Bonzinidd307d02016-07-12 10:35:51 +02003675static int em_sldt(struct x86_emulate_ctxt *ctxt)
3676{
3677 return em_store_sreg(ctxt, VCPU_SREG_LDTR);
3678}
3679
Avi Kivitya14e5792012-06-13 12:28:33 +03003680static int em_lldt(struct x86_emulate_ctxt *ctxt)
3681{
3682 u16 sel = ctxt->src.val;
3683
3684 /* Disable writeback. */
3685 ctxt->dst.type = OP_NONE;
3686 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3687}
3688
Paolo Bonzinidd307d02016-07-12 10:35:51 +02003689static int em_str(struct x86_emulate_ctxt *ctxt)
3690{
3691 return em_store_sreg(ctxt, VCPU_SREG_TR);
3692}
3693
Avi Kivity80890002012-06-13 16:33:29 +03003694static int em_ltr(struct x86_emulate_ctxt *ctxt)
3695{
3696 u16 sel = ctxt->src.val;
3697
3698 /* Disable writeback. */
3699 ctxt->dst.type = OP_NONE;
3700 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3701}
3702
Avi Kivity38503912011-03-31 18:48:09 +02003703static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3704{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003705 int rc;
3706 ulong linear;
3707
Avi Kivity9dac77f2011-06-01 15:34:25 +03003708 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003709 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003710 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003711 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003712 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003713 return X86EMUL_CONTINUE;
3714}
3715
Avi Kivity2d04a052011-04-20 15:32:49 +03003716static int em_clts(struct x86_emulate_ctxt *ctxt)
3717{
3718 ulong cr0;
3719
3720 cr0 = ctxt->ops->get_cr(ctxt, 0);
3721 cr0 &= ~X86_CR0_TS;
3722 ctxt->ops->set_cr(ctxt, 0, cr0);
3723 return X86EMUL_CONTINUE;
3724}
3725
Jan Kiszkab34a8052015-03-09 20:27:43 +01003726static int em_hypercall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003727{
Nadav Amit0f54a322014-08-29 11:26:55 +03003728 int rc = ctxt->ops->fix_hypercall(ctxt);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003729
Avi Kivity26d05cc2011-04-21 12:07:59 +03003730 if (rc != X86EMUL_CONTINUE)
3731 return rc;
3732
3733 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003734 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003735 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003736 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003737 return X86EMUL_CONTINUE;
3738}
3739
Avi Kivity96051572012-06-10 17:21:18 +03003740static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3741 void (*get)(struct x86_emulate_ctxt *ctxt,
3742 struct desc_ptr *ptr))
3743{
3744 struct desc_ptr desc_ptr;
3745
Paolo Bonziniae3e61e2016-07-12 10:36:41 +02003746 if ((ctxt->ops->get_cr(ctxt, 4) & X86_CR4_UMIP) &&
3747 ctxt->ops->cpl(ctxt) > 0)
3748 return emulate_gp(ctxt, 0);
3749
Avi Kivity96051572012-06-10 17:21:18 +03003750 if (ctxt->mode == X86EMUL_MODE_PROT64)
3751 ctxt->op_bytes = 8;
3752 get(ctxt, &desc_ptr);
3753 if (ctxt->op_bytes == 2) {
3754 ctxt->op_bytes = 4;
3755 desc_ptr.address &= 0x00ffffff;
3756 }
3757 /* Disable writeback. */
3758 ctxt->dst.type = OP_NONE;
Steve Rutherford129a72a2017-01-11 18:28:29 -08003759 return segmented_write_std(ctxt, ctxt->dst.addr.mem,
3760 &desc_ptr, 2 + ctxt->op_bytes);
Avi Kivity96051572012-06-10 17:21:18 +03003761}
3762
3763static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3764{
3765 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3766}
3767
3768static int em_sidt(struct x86_emulate_ctxt *ctxt)
3769{
3770 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3771}
3772
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003773static int em_lgdt_lidt(struct x86_emulate_ctxt *ctxt, bool lgdt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003774{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003775 struct desc_ptr desc_ptr;
3776 int rc;
3777
Avi Kivity510425f2012-06-07 17:04:36 +03003778 if (ctxt->mode == X86EMUL_MODE_PROT64)
3779 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003780 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003781 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003782 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003783 if (rc != X86EMUL_CONTINUE)
3784 return rc;
Nadav Amit9a9abf62014-11-02 11:54:56 +02003785 if (ctxt->mode == X86EMUL_MODE_PROT64 &&
Yu Zhangfd8cb432017-08-24 20:27:56 +08003786 emul_is_noncanonical_address(desc_ptr.address, ctxt))
Nadav Amit9a9abf62014-11-02 11:54:56 +02003787 return emulate_gp(ctxt, 0);
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003788 if (lgdt)
3789 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3790 else
3791 ctxt->ops->set_idt(ctxt, &desc_ptr);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003792 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003793 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003794 return X86EMUL_CONTINUE;
3795}
3796
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003797static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3798{
3799 return em_lgdt_lidt(ctxt, true);
3800}
3801
Avi Kivity26d05cc2011-04-21 12:07:59 +03003802static int em_lidt(struct x86_emulate_ctxt *ctxt)
3803{
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003804 return em_lgdt_lidt(ctxt, false);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003805}
3806
3807static int em_smsw(struct x86_emulate_ctxt *ctxt)
3808{
Paolo Bonziniae3e61e2016-07-12 10:36:41 +02003809 if ((ctxt->ops->get_cr(ctxt, 4) & X86_CR4_UMIP) &&
3810 ctxt->ops->cpl(ctxt) > 0)
3811 return emulate_gp(ctxt, 0);
3812
Nadav Amit32e94d02014-06-02 18:34:11 +03003813 if (ctxt->dst.type == OP_MEM)
3814 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003815 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003816 return X86EMUL_CONTINUE;
3817}
3818
3819static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3820{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003821 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003822 | (ctxt->src.val & 0x0f));
3823 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003824 return X86EMUL_CONTINUE;
3825}
3826
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003827static int em_loop(struct x86_emulate_ctxt *ctxt)
3828{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003829 int rc = X86EMUL_CONTINUE;
3830
Paolo Bonzini01485a22014-11-19 18:25:08 +01003831 register_address_increment(ctxt, VCPU_REGS_RCX, -1);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003832 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003833 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
Nadav Amit234f3ce2014-09-18 22:39:38 +03003834 rc = jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003835
Nadav Amit234f3ce2014-09-18 22:39:38 +03003836 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003837}
3838
3839static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3840{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003841 int rc = X86EMUL_CONTINUE;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003842
Nadav Amit234f3ce2014-09-18 22:39:38 +03003843 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
3844 rc = jmp_rel(ctxt, ctxt->src.val);
3845
3846 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003847}
3848
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003849static int em_in(struct x86_emulate_ctxt *ctxt)
3850{
3851 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3852 &ctxt->dst.val))
3853 return X86EMUL_IO_NEEDED;
3854
3855 return X86EMUL_CONTINUE;
3856}
3857
3858static int em_out(struct x86_emulate_ctxt *ctxt)
3859{
3860 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3861 &ctxt->src.val, 1);
3862 /* Disable writeback. */
3863 ctxt->dst.type = OP_NONE;
3864 return X86EMUL_CONTINUE;
3865}
3866
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003867static int em_cli(struct x86_emulate_ctxt *ctxt)
3868{
3869 if (emulator_bad_iopl(ctxt))
3870 return emulate_gp(ctxt, 0);
3871
3872 ctxt->eflags &= ~X86_EFLAGS_IF;
3873 return X86EMUL_CONTINUE;
3874}
3875
3876static int em_sti(struct x86_emulate_ctxt *ctxt)
3877{
3878 if (emulator_bad_iopl(ctxt))
3879 return emulate_gp(ctxt, 0);
3880
3881 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3882 ctxt->eflags |= X86_EFLAGS_IF;
3883 return X86EMUL_CONTINUE;
3884}
3885
Avi Kivity6d6eede2012-06-07 14:11:36 +03003886static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3887{
3888 u32 eax, ebx, ecx, edx;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003889 u64 msr = 0;
3890
3891 ctxt->ops->get_msr(ctxt, MSR_MISC_FEATURES_ENABLES, &msr);
3892 if (msr & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
3893 ctxt->ops->cpl(ctxt)) {
3894 return emulate_gp(ctxt, 0);
3895 }
Avi Kivity6d6eede2012-06-07 14:11:36 +03003896
Avi Kivitydd856ef2012-08-27 23:46:17 +03003897 eax = reg_read(ctxt, VCPU_REGS_RAX);
3898 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Sean Christophersonf91af512020-03-04 17:34:37 -08003899 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, false);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003900 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3901 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3902 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3903 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003904 return X86EMUL_CONTINUE;
3905}
3906
Paolo Bonzini98f73632013-10-31 11:19:42 +01003907static int em_sahf(struct x86_emulate_ctxt *ctxt)
3908{
3909 u32 flags;
3910
Nadav Amit0efb0442015-03-29 16:33:03 +03003911 flags = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
3912 X86_EFLAGS_SF;
Paolo Bonzini98f73632013-10-31 11:19:42 +01003913 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3914
3915 ctxt->eflags &= ~0xffUL;
3916 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3917 return X86EMUL_CONTINUE;
3918}
3919
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003920static int em_lahf(struct x86_emulate_ctxt *ctxt)
3921{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003922 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3923 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003924 return X86EMUL_CONTINUE;
3925}
3926
Avi Kivity92998362012-06-13 12:25:06 +03003927static int em_bswap(struct x86_emulate_ctxt *ctxt)
3928{
3929 switch (ctxt->op_bytes) {
3930#ifdef CONFIG_X86_64
3931 case 8:
3932 asm("bswap %0" : "+r"(ctxt->dst.val));
3933 break;
3934#endif
3935 default:
3936 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3937 break;
3938 }
3939 return X86EMUL_CONTINUE;
3940}
3941
Nadav Amit13e457e2014-10-13 13:04:13 +03003942static int em_clflush(struct x86_emulate_ctxt *ctxt)
3943{
3944 /* emulating clflush regardless of cpuid */
3945 return X86EMUL_CONTINUE;
3946}
3947
David Edmondson51b958e2020-11-03 12:04:00 +00003948static int em_clflushopt(struct x86_emulate_ctxt *ctxt)
3949{
3950 /* emulating clflushopt regardless of cpuid */
3951 return X86EMUL_CONTINUE;
3952}
3953
Nadav Amit2276b512015-01-26 09:32:24 +02003954static int em_movsxd(struct x86_emulate_ctxt *ctxt)
3955{
3956 ctxt->dst.val = (s32) ctxt->src.val;
3957 return X86EMUL_CONTINUE;
3958}
3959
Radim Krčmář283c95d2016-11-09 19:07:06 +01003960static int check_fxsr(struct x86_emulate_ctxt *ctxt)
3961{
Sean Christopherson5ae78e92019-12-17 13:32:38 -08003962 if (!ctxt->ops->guest_has_fxsr(ctxt))
Radim Krčmář283c95d2016-11-09 19:07:06 +01003963 return emulate_ud(ctxt);
3964
3965 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
3966 return emulate_nm(ctxt);
3967
3968 /*
3969 * Don't emulate a case that should never be hit, instead of working
3970 * around a lack of fxsave64/fxrstor64 on old compilers.
3971 */
3972 if (ctxt->mode >= X86EMUL_MODE_PROT64)
3973 return X86EMUL_UNHANDLEABLE;
3974
3975 return X86EMUL_CONTINUE;
3976}
3977
3978/*
Nick Desaulniers9d643f62017-05-30 20:08:38 -07003979 * Hardware doesn't save and restore XMM 0-7 without CR4.OSFXSR, but does save
3980 * and restore MXCSR.
3981 */
3982static size_t __fxstate_size(int nregs)
3983{
3984 return offsetof(struct fxregs_state, xmm_space[0]) + nregs * 16;
3985}
3986
3987static inline size_t fxstate_size(struct x86_emulate_ctxt *ctxt)
3988{
3989 bool cr4_osfxsr;
3990 if (ctxt->mode == X86EMUL_MODE_PROT64)
3991 return __fxstate_size(16);
3992
3993 cr4_osfxsr = ctxt->ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR;
3994 return __fxstate_size(cr4_osfxsr ? 8 : 0);
3995}
3996
3997/*
Radim Krčmář283c95d2016-11-09 19:07:06 +01003998 * FXSAVE and FXRSTOR have 4 different formats depending on execution mode,
3999 * 1) 16 bit mode
4000 * 2) 32 bit mode
4001 * - like (1), but FIP and FDP (foo) are only 16 bit. At least Intel CPUs
4002 * preserve whole 32 bit values, though, so (1) and (2) are the same wrt.
4003 * save and restore
4004 * 3) 64-bit mode with REX.W prefix
4005 * - like (2), but XMM 8-15 are being saved and restored
4006 * 4) 64-bit mode without REX.W prefix
4007 * - like (3), but FIP and FDP are 64 bit
4008 *
4009 * Emulation uses (3) for (1) and (2) and preserves XMM 8-15 to reach the
4010 * desired result. (4) is not emulated.
4011 *
4012 * Note: Guest and host CPUID.(EAX=07H,ECX=0H):EBX[bit 13] (deprecate FPU CS
4013 * and FPU DS) should match.
4014 */
4015static int em_fxsave(struct x86_emulate_ctxt *ctxt)
4016{
4017 struct fxregs_state fx_state;
Radim Krčmář283c95d2016-11-09 19:07:06 +01004018 int rc;
4019
4020 rc = check_fxsr(ctxt);
4021 if (rc != X86EMUL_CONTINUE)
4022 return rc;
4023
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02004024 kvm_fpu_get();
Sean Christophersona7baead2020-01-17 11:30:50 -08004025
Radim Krčmář283c95d2016-11-09 19:07:06 +01004026 rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_state));
4027
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02004028 kvm_fpu_put();
Sean Christophersona7baead2020-01-17 11:30:50 -08004029
Radim Krčmář283c95d2016-11-09 19:07:06 +01004030 if (rc != X86EMUL_CONTINUE)
4031 return rc;
4032
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004033 return segmented_write_std(ctxt, ctxt->memop.addr.mem, &fx_state,
4034 fxstate_size(ctxt));
Radim Krčmář283c95d2016-11-09 19:07:06 +01004035}
4036
David Hildenbrand4d772cb2017-11-07 18:04:05 +01004037/*
4038 * FXRSTOR might restore XMM registers not provided by the guest. Fill
4039 * in the host registers (via FXSAVE) instead, so they won't be modified.
4040 * (preemption has to stay disabled until FXRSTOR).
4041 *
4042 * Use noinline to keep the stack for other functions called by callers small.
4043 */
4044static noinline int fxregs_fixup(struct fxregs_state *fx_state,
4045 const size_t used_size)
4046{
4047 struct fxregs_state fx_tmp;
4048 int rc;
4049
4050 rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_tmp));
4051 memcpy((void *)fx_state + used_size, (void *)&fx_tmp + used_size,
4052 __fxstate_size(16) - used_size);
4053
4054 return rc;
4055}
4056
Radim Krčmář283c95d2016-11-09 19:07:06 +01004057static int em_fxrstor(struct x86_emulate_ctxt *ctxt)
4058{
4059 struct fxregs_state fx_state;
4060 int rc;
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004061 size_t size;
Radim Krčmář283c95d2016-11-09 19:07:06 +01004062
4063 rc = check_fxsr(ctxt);
4064 if (rc != X86EMUL_CONTINUE)
4065 return rc;
4066
David Hildenbrand4d772cb2017-11-07 18:04:05 +01004067 size = fxstate_size(ctxt);
4068 rc = segmented_read_std(ctxt, ctxt->memop.addr.mem, &fx_state, size);
4069 if (rc != X86EMUL_CONTINUE)
4070 return rc;
4071
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02004072 kvm_fpu_get();
Sean Christophersona7baead2020-01-17 11:30:50 -08004073
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004074 if (size < __fxstate_size(16)) {
David Hildenbrand4d772cb2017-11-07 18:04:05 +01004075 rc = fxregs_fixup(&fx_state, size);
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004076 if (rc != X86EMUL_CONTINUE)
4077 goto out;
4078 }
4079
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004080 if (fx_state.mxcsr >> 16) {
4081 rc = emulate_gp(ctxt, 0);
4082 goto out;
4083 }
Radim Krčmář283c95d2016-11-09 19:07:06 +01004084
4085 if (rc == X86EMUL_CONTINUE)
4086 rc = asm_safe("fxrstor %[fx]", : [fx] "m"(fx_state));
4087
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004088out:
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02004089 kvm_fpu_put();
Sean Christophersona7baead2020-01-17 11:30:50 -08004090
Radim Krčmář283c95d2016-11-09 19:07:06 +01004091 return rc;
4092}
4093
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02004094static int em_xsetbv(struct x86_emulate_ctxt *ctxt)
4095{
4096 u32 eax, ecx, edx;
4097
4098 eax = reg_read(ctxt, VCPU_REGS_RAX);
4099 edx = reg_read(ctxt, VCPU_REGS_RDX);
4100 ecx = reg_read(ctxt, VCPU_REGS_RCX);
4101
4102 if (ctxt->ops->set_xcr(ctxt, ecx, ((u64)edx << 32) | eax))
4103 return emulate_gp(ctxt, 0);
4104
4105 return X86EMUL_CONTINUE;
4106}
4107
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004108static bool valid_cr(int nr)
4109{
4110 switch (nr) {
4111 case 0:
4112 case 2 ... 4:
4113 case 8:
4114 return true;
4115 default:
4116 return false;
4117 }
4118}
4119
Sean Christophersond0fe7b62021-04-21 19:21:20 -07004120static int check_cr_access(struct x86_emulate_ctxt *ctxt)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004121{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004122 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004123 return emulate_ud(ctxt);
4124
4125 return X86EMUL_CONTINUE;
4126}
4127
Joerg Roedel3b88e412011-04-04 12:39:29 +02004128static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
4129{
4130 unsigned long dr7;
4131
Avi Kivity717746e2011-04-20 13:37:53 +03004132 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02004133
4134 /* Check if DR7.Global_Enable is set */
4135 return dr7 & (1 << 13);
4136}
4137
4138static int check_dr_read(struct x86_emulate_ctxt *ctxt)
4139{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004140 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02004141 u64 cr4;
4142
4143 if (dr > 7)
4144 return emulate_ud(ctxt);
4145
Avi Kivity717746e2011-04-20 13:37:53 +03004146 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02004147 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
4148 return emulate_ud(ctxt);
4149
Nadav Amit6d2a0522014-11-02 11:54:43 +02004150 if (check_dr7_gd(ctxt)) {
4151 ulong dr6;
4152
4153 ctxt->ops->get_dr(ctxt, 6, &dr6);
Liran Alon1fc5d1942019-06-06 01:54:47 +03004154 dr6 &= ~DR_TRAP_BITS;
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08004155 dr6 |= DR6_BD | DR6_ACTIVE_LOW;
Nadav Amit6d2a0522014-11-02 11:54:43 +02004156 ctxt->ops->set_dr(ctxt, 6, dr6);
Joerg Roedel3b88e412011-04-04 12:39:29 +02004157 return emulate_db(ctxt);
Nadav Amit6d2a0522014-11-02 11:54:43 +02004158 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02004159
4160 return X86EMUL_CONTINUE;
4161}
4162
4163static int check_dr_write(struct x86_emulate_ctxt *ctxt)
4164{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004165 u64 new_val = ctxt->src.val64;
4166 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02004167
4168 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
4169 return emulate_gp(ctxt, 0);
4170
4171 return check_dr_read(ctxt);
4172}
4173
Joerg Roedel01de8b02011-04-04 12:39:31 +02004174static int check_svme(struct x86_emulate_ctxt *ctxt)
4175{
Radim Krčmář92ceb762017-05-18 19:37:32 +02004176 u64 efer = 0;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004177
Avi Kivity717746e2011-04-20 13:37:53 +03004178 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02004179
4180 if (!(efer & EFER_SVME))
4181 return emulate_ud(ctxt);
4182
4183 return X86EMUL_CONTINUE;
4184}
4185
4186static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
4187{
Avi Kivitydd856ef2012-08-27 23:46:17 +03004188 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02004189
4190 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07004191 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02004192 return emulate_gp(ctxt, 0);
4193
4194 return check_svme(ctxt);
4195}
4196
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004197static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
4198{
Avi Kivity717746e2011-04-20 13:37:53 +03004199 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004200
Avi Kivity717746e2011-04-20 13:37:53 +03004201 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004202 return emulate_ud(ctxt);
4203
4204 return X86EMUL_CONTINUE;
4205}
4206
Joerg Roedel80612522011-04-04 12:39:33 +02004207static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
4208{
Avi Kivity717746e2011-04-20 13:37:53 +03004209 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03004210 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02004211
Arbel Moshe2d7921c2018-03-12 13:12:53 +02004212 /*
4213 * VMware allows access to these Pseduo-PMCs even when read via RDPMC
4214 * in Ring3 when CR4.PCE=0.
4215 */
4216 if (enable_vmware_backdoor && is_vmware_backdoor_pmc(rcx))
4217 return X86EMUL_CONTINUE;
4218
Avi Kivity717746e2011-04-20 13:37:53 +03004219 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03004220 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02004221 return emulate_gp(ctxt, 0);
4222
4223 return X86EMUL_CONTINUE;
4224}
4225
Joerg Roedelf6511932011-04-04 12:39:35 +02004226static int check_perm_in(struct x86_emulate_ctxt *ctxt)
4227{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004228 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
4229 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02004230 return emulate_gp(ctxt, 0);
4231
4232 return X86EMUL_CONTINUE;
4233}
4234
4235static int check_perm_out(struct x86_emulate_ctxt *ctxt)
4236{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004237 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
4238 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02004239 return emulate_gp(ctxt, 0);
4240
4241 return X86EMUL_CONTINUE;
4242}
4243
Avi Kivity73fba5f2010-07-29 15:11:53 +03004244#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004245#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
4246#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
4247 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03004248#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02004249#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004250#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
4251#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Nadav Amit39f062f2014-11-26 15:47:18 +02004252#define ID(_f, _i) { .flags = ((_f) | InstrDual | ModRM), .u.idual = (_i) }
Nadav Amit2276b512015-01-26 09:32:24 +02004253#define MD(_f, _m) { .flags = ((_f) | ModeDual), .u.mdual = (_m) }
Gleb Natapov045a2822012-12-20 16:57:43 +02004254#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03004255#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02004256#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004257#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004258 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004259#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004260 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
4261 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02004262#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03004263
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004264#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02004265#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004266#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02004267#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004268#define I2bvIP(_f, _e, _i, _p) \
4269 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004270
Avi Kivityfb864fb2013-01-04 16:18:54 +02004271#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
4272 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
4273 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03004274
Nadav Amit0f54a322014-08-29 11:26:55 +03004275static const struct opcode group7_rm0[] = {
4276 N,
Jan Kiszkab34a8052015-03-09 20:27:43 +01004277 I(SrcNone | Priv | EmulateOnUD, em_hypercall),
Nadav Amit0f54a322014-08-29 11:26:55 +03004278 N, N, N, N, N, N,
4279};
4280
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004281static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004282 DI(SrcNone | Priv, monitor),
4283 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004284 N, N, N, N, N, N,
4285};
4286
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02004287static const struct opcode group7_rm2[] = {
4288 N,
4289 II(ImplicitOps | Priv, em_xsetbv, xsetbv),
4290 N, N, N, N, N, N,
4291};
4292
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004293static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004294 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Jan Kiszkab34a8052015-03-09 20:27:43 +01004295 II(SrcNone | Prot | EmulateOnUD, em_hypercall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004296 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
4297 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
4298 DIP(SrcNone | Prot | Priv, stgi, check_svme),
4299 DIP(SrcNone | Prot | Priv, clgi, check_svme),
4300 DIP(SrcNone | Prot | Priv, skinit, check_svme),
4301 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004302};
Avi Kivity6230f7f2010-08-26 18:34:55 +03004303
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004304static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004305 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004306 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004307 N, N, N, N, N, N,
4308};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004309
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004310static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02004311 F(Lock, em_add),
4312 F(Lock | PageTable, em_or),
4313 F(Lock, em_adc),
4314 F(Lock, em_sbb),
4315 F(Lock | PageTable, em_and),
4316 F(Lock, em_sub),
4317 F(Lock, em_xor),
4318 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004319};
4320
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004321static const struct opcode group1A[] = {
Tom Lendacky0f89b202016-12-14 14:59:23 -05004322 I(DstMem | SrcNone | Mov | Stack | IncSP | TwoMemOp, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004323};
4324
Avi Kivity007a3b52013-01-19 19:51:51 +02004325static const struct opcode group2[] = {
4326 F(DstMem | ModRM, em_rol),
4327 F(DstMem | ModRM, em_ror),
4328 F(DstMem | ModRM, em_rcl),
4329 F(DstMem | ModRM, em_rcr),
4330 F(DstMem | ModRM, em_shl),
4331 F(DstMem | ModRM, em_shr),
4332 F(DstMem | ModRM, em_shl),
4333 F(DstMem | ModRM, em_sar),
4334};
4335
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004336static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02004337 F(DstMem | SrcImm | NoWrite, em_test),
4338 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02004339 F(DstMem | SrcNone | Lock, em_not),
4340 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02004341 F(DstXacc | Src2Mem, em_mul_ex),
4342 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004343 F(DstXacc | Src2Mem, em_div_ex),
4344 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004345};
4346
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004347static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02004348 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
4349 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004350 N, N, N, N, N, N,
4351};
4352
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004353static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02004354 F(DstMem | SrcNone | Lock, em_inc),
4355 F(DstMem | SrcNone | Lock, em_dec),
Nadav Amit58b70752014-10-24 11:35:09 +03004356 I(SrcMem | NearBranch, em_call_near_abs),
Nadav Amitacac6f892015-05-03 20:22:57 +03004357 I(SrcMemFAddr | ImplicitOps, em_call_far),
Nadav Amit58b70752014-10-24 11:35:09 +03004358 I(SrcMem | NearBranch, em_jmp_abs),
Nadav Amitf7784042014-09-18 22:39:41 +03004359 I(SrcMemFAddr | ImplicitOps, em_jmp_far),
Tom Lendacky0f89b202016-12-14 14:59:23 -05004360 I(SrcMem | Stack | TwoMemOp, em_push), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004361};
4362
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004363static const struct opcode group6[] = {
Paolo Bonzinidd307d02016-07-12 10:35:51 +02004364 II(Prot | DstMem, em_sldt, sldt),
4365 II(Prot | DstMem, em_str, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03004366 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03004367 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004368 N, N, N, N,
4369};
4370
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004371static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03004372 II(Mov | DstMem, em_sgdt, sgdt),
4373 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004374 II(SrcMem | Priv, em_lgdt, lgdt),
4375 II(SrcMem | Priv, em_lidt, lidt),
4376 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
4377 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
4378 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004379}, {
Nadav Amit0f54a322014-08-29 11:26:55 +03004380 EXT(0, group7_rm0),
Avi Kivity5ef39c72011-04-21 12:21:50 +03004381 EXT(0, group7_rm1),
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02004382 EXT(0, group7_rm2),
4383 EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004384 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
4385 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
4386 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004387} };
4388
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004389static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004390 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02004391 F(DstMem | SrcImmByte | NoWrite, em_bt),
4392 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
4393 F(DstMem | SrcImmByte | Lock, em_btr),
4394 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004395};
4396
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02004397/*
4398 * The "memory" destination is actually always a register, since we come
4399 * from the register case of group9.
4400 */
4401static const struct gprefix pfx_0f_c7_7 = {
Sean Christopherson2183de42021-05-04 10:17:23 -07004402 N, N, N, II(DstMem | ModRM | Op3264 | EmulateOnUD, em_rdpid, rdpid),
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02004403};
4404
4405
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004406static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004407 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004408}, {
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02004409 N, N, N, N, N, N, N,
4410 GP(0, &pfx_0f_c7_7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004411} };
4412
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004413static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004414 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004415 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004416};
4417
Nadav Amit13e457e2014-10-13 13:04:13 +03004418static const struct gprefix pfx_0f_ae_7 = {
David Edmondson51b958e2020-11-03 12:04:00 +00004419 I(SrcMem | ByteOp, em_clflush), I(SrcMem | ByteOp, em_clflushopt), N, N,
Nadav Amit13e457e2014-10-13 13:04:13 +03004420};
4421
4422static const struct group_dual group15 = { {
Radim Krčmář283c95d2016-11-09 19:07:06 +01004423 I(ModRM | Aligned16, em_fxsave),
4424 I(ModRM | Aligned16, em_fxrstor),
4425 N, N, N, N, N, GP(0, &pfx_0f_ae_7),
Nadav Amit13e457e2014-10-13 13:04:13 +03004426}, {
4427 N, N, N, N, N, N, N, N,
4428} };
4429
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004430static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03004431 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02004432};
4433
Nadav Amit39f062f2014-11-26 15:47:18 +02004434static const struct instr_dual instr_dual_0f_2b = {
4435 I(0, em_mov), N
4436};
4437
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004438static const struct gprefix pfx_0f_2b = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004439 ID(0, &instr_dual_0f_2b), ID(0, &instr_dual_0f_2b), N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03004440};
4441
Stefan Fritsch29916962018-04-01 17:54:44 +02004442static const struct gprefix pfx_0f_10_0f_11 = {
4443 I(Unaligned, em_mov), I(Unaligned, em_mov), N, N,
4444};
4445
Igor Mammedov27ce8252014-03-15 21:01:59 +01004446static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01004447 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004448};
4449
Alex Williamson0a370272014-07-11 11:56:31 -06004450static const struct gprefix pfx_0f_e7 = {
4451 N, I(Sse, em_mov), N, N,
4452};
4453
Gleb Natapov045a2822012-12-20 16:57:43 +02004454static const struct escape escape_d9 = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02004455 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstcw),
Gleb Natapov045a2822012-12-20 16:57:43 +02004456}, {
4457 /* 0xC0 - 0xC7 */
4458 N, N, N, N, N, N, N, N,
4459 /* 0xC8 - 0xCF */
4460 N, N, N, N, N, N, N, N,
4461 /* 0xD0 - 0xC7 */
4462 N, N, N, N, N, N, N, N,
4463 /* 0xD8 - 0xDF */
4464 N, N, N, N, N, N, N, N,
4465 /* 0xE0 - 0xE7 */
4466 N, N, N, N, N, N, N, N,
4467 /* 0xE8 - 0xEF */
4468 N, N, N, N, N, N, N, N,
4469 /* 0xF0 - 0xF7 */
4470 N, N, N, N, N, N, N, N,
4471 /* 0xF8 - 0xFF */
4472 N, N, N, N, N, N, N, N,
4473} };
4474
4475static const struct escape escape_db = { {
4476 N, N, N, N, N, N, N, N,
4477}, {
4478 /* 0xC0 - 0xC7 */
4479 N, N, N, N, N, N, N, N,
4480 /* 0xC8 - 0xCF */
4481 N, N, N, N, N, N, N, N,
4482 /* 0xD0 - 0xC7 */
4483 N, N, N, N, N, N, N, N,
4484 /* 0xD8 - 0xDF */
4485 N, N, N, N, N, N, N, N,
4486 /* 0xE0 - 0xE7 */
4487 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
4488 /* 0xE8 - 0xEF */
4489 N, N, N, N, N, N, N, N,
4490 /* 0xF0 - 0xF7 */
4491 N, N, N, N, N, N, N, N,
4492 /* 0xF8 - 0xFF */
4493 N, N, N, N, N, N, N, N,
4494} };
4495
4496static const struct escape escape_dd = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02004497 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstsw),
Gleb Natapov045a2822012-12-20 16:57:43 +02004498}, {
4499 /* 0xC0 - 0xC7 */
4500 N, N, N, N, N, N, N, N,
4501 /* 0xC8 - 0xCF */
4502 N, N, N, N, N, N, N, N,
4503 /* 0xD0 - 0xC7 */
4504 N, N, N, N, N, N, N, N,
4505 /* 0xD8 - 0xDF */
4506 N, N, N, N, N, N, N, N,
4507 /* 0xE0 - 0xE7 */
4508 N, N, N, N, N, N, N, N,
4509 /* 0xE8 - 0xEF */
4510 N, N, N, N, N, N, N, N,
4511 /* 0xF0 - 0xF7 */
4512 N, N, N, N, N, N, N, N,
4513 /* 0xF8 - 0xFF */
4514 N, N, N, N, N, N, N, N,
4515} };
4516
Nadav Amit39f062f2014-11-26 15:47:18 +02004517static const struct instr_dual instr_dual_0f_c3 = {
4518 I(DstMem | SrcReg | ModRM | No16 | Mov, em_mov), N
4519};
4520
Nadav Amit2276b512015-01-26 09:32:24 +02004521static const struct mode_dual mode_dual_63 = {
4522 N, I(DstReg | SrcMem32 | ModRM | Mov, em_movsxd)
4523};
4524
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004525static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004526 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004527 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004528 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
4529 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004530 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004531 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004532 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
4533 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004534 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004535 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004536 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
4537 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004538 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004539 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004540 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
4541 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004542 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004543 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004544 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004545 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004546 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004547 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004548 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004549 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004550 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02004551 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004552 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03004553 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004554 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09004555 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004556 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09004557 I(ImplicitOps | Stack | No64, em_pusha),
4558 I(ImplicitOps | Stack | No64, em_popa),
Nadav Amit2276b512015-01-26 09:32:24 +02004559 N, MD(ModRM, &mode_dual_63),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004560 N, N, N, N,
4561 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03004562 I(SrcImm | Mov | Stack, em_push),
4563 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03004564 I(SrcImmByte | Mov | Stack, em_push),
4565 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004566 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09004567 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03004568 /* 0x70 - 0x7F */
Nadav Amit58b70752014-10-24 11:35:09 +03004569 X16(D(SrcImmByte | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004570 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004571 G(ByteOp | DstMem | SrcImm, group1),
4572 G(DstMem | SrcImm, group1),
4573 G(ByteOp | DstMem | SrcImm | No64, group1),
4574 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02004575 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004576 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004577 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004578 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004579 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004580 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09004581 D(ModRM | SrcMem | NoAccess | DstReg),
4582 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
4583 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004584 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004585 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004586 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03004587 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08004588 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09004589 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01004590 II(ImplicitOps | Stack, em_popf, popf),
4591 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004592 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004593 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004594 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Tom Lendacky0f89b202016-12-14 14:59:23 -05004595 I2bv(SrcSI | DstDI | Mov | String | TwoMemOp, em_mov),
4596 F2bv(SrcSI | DstDI | String | NoWrite | TwoMemOp, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004597 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004598 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004599 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
4600 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004601 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004602 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004603 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004604 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02004605 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004606 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004607 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Nadav Amit58b70752014-10-24 11:35:09 +03004608 I(ImplicitOps | NearBranch | SrcImmU16, em_ret_near_imm),
4609 I(ImplicitOps | NearBranch, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03004610 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
4611 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004612 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004613 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03004614 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Nadav Amit16794aa2015-01-26 09:32:22 +02004615 I(ImplicitOps | SrcImmU16, em_ret_far_imm),
4616 I(ImplicitOps, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02004617 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004618 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004619 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004620 G(Src2One | ByteOp, group2), G(Src2One, group2),
4621 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02004622 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02004623 I(DstAcc | SrcImmUByte | No64, em_aad),
4624 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004625 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004626 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02004627 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004628 /* 0xE0 - 0xE7 */
Nadav Amit58b70752014-10-24 11:35:09 +03004629 X3(I(SrcImmByte | NearBranch, em_loop)),
4630 I(SrcImmByte | NearBranch, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004631 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
4632 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004633 /* 0xE8 - 0xEF */
Nadav Amit58b70752014-10-24 11:35:09 +03004634 I(SrcImm | NearBranch, em_call), D(SrcImm | ImplicitOps | NearBranch),
4635 I(SrcImmFAddr | No64, em_jmp_far),
4636 D(SrcImmByte | ImplicitOps | NearBranch),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004637 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
4638 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004639 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004640 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004641 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4642 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004643 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09004644 D(ImplicitOps), D(ImplicitOps),
4645 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004646 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4647};
4648
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004649static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004650 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004651 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02004652 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004653 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004654 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004655 N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004656 /* 0x10 - 0x1F */
Stefan Fritsch29916962018-04-01 17:54:44 +02004657 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_10_0f_11),
4658 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_10_0f_11),
4659 N, N, N, N, N, N,
Paolo Bonzini34d26182020-05-15 12:18:14 -04004660 D(ImplicitOps | ModRM | SrcMem | NoAccess), /* 4 * prefetch + 4 * reserved NOP */
4661 D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
4662 D(ImplicitOps | ModRM | SrcMem | NoAccess), /* 8 * reserved NOP */
4663 D(ImplicitOps | ModRM | SrcMem | NoAccess), /* 8 * reserved NOP */
4664 D(ImplicitOps | ModRM | SrcMem | NoAccess), /* 8 * reserved NOP */
4665 D(ImplicitOps | ModRM | SrcMem | NoAccess), /* NOP + 7 * reserved NOP */
Avi Kivity73fba5f2010-07-29 15:11:53 +03004666 /* 0x20 - 0x2F */
Sean Christophersond0fe7b62021-04-21 19:21:20 -07004667 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_access),
Nadav Amit9b88ae92014-05-25 23:05:21 +03004668 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
4669 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
Sean Christophersond0fe7b62021-04-21 19:21:20 -07004670 check_cr_access),
Nadav Amit9b88ae92014-05-25 23:05:21 +03004671 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
4672 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004673 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004674 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
4675 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004676 N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b),
Avi Kivity3e114eb2012-04-09 18:40:01 +03004677 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004678 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004679 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004680 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004681 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004682 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02004683 I(ImplicitOps | EmulateOnUD, em_sysenter),
4684 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004685 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004686 N, N, N, N, N, N, N, N,
4687 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03004688 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004689 /* 0x50 - 0x5F */
4690 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4691 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004692 N, N, N, N,
4693 N, N, N, N,
4694 N, N, N, N,
4695 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004696 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004697 N, N, N, N,
4698 N, N, N, N,
4699 N, N, N, N,
4700 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004701 /* 0x80 - 0x8F */
Nadav Amit58b70752014-10-24 11:35:09 +03004702 X16(D(SrcImm | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004703 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004704 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004705 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004706 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004707 II(ImplicitOps, em_cpuid, cpuid),
4708 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004709 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4710 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004711 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004712 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Paolo Bonzini89651a32015-11-03 13:43:05 +01004713 II(EmulateOnUD | ImplicitOps, em_rsm, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004714 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004715 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4716 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Nadav Amit13e457e2014-10-13 13:04:13 +03004717 GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004718 /* 0xB0 - 0xB7 */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02004719 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable | SrcWrite, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004720 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004721 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004722 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4723 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004724 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004725 /* 0xB8 - 0xBF */
4726 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004727 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004728 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Nadav Amit900efe22015-03-30 15:39:21 +03004729 I(DstReg | SrcMem | ModRM, em_bsf_c),
4730 I(DstReg | SrcMem | ModRM, em_bsr_c),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004731 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004732 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02004733 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Nadav Amit39f062f2014-11-26 15:47:18 +02004734 N, ID(0, &instr_dual_0f_c3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004735 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004736 /* 0xC8 - 0xCF */
4737 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004738 /* 0xD0 - 0xDF */
4739 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4740 /* 0xE0 - 0xEF */
Alex Williamson0a370272014-07-11 11:56:31 -06004741 N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
4742 N, N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004743 /* 0xF0 - 0xFF */
4744 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4745};
4746
Nadav Amit39f062f2014-11-26 15:47:18 +02004747static const struct instr_dual instr_dual_0f_38_f0 = {
4748 I(DstReg | SrcMem | Mov, em_movbe), N
4749};
4750
4751static const struct instr_dual instr_dual_0f_38_f1 = {
4752 I(DstMem | SrcReg | Mov, em_movbe), N
4753};
4754
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004755static const struct gprefix three_byte_0f_38_f0 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004756 ID(0, &instr_dual_0f_38_f0), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004757};
4758
4759static const struct gprefix three_byte_0f_38_f1 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004760 ID(0, &instr_dual_0f_38_f1), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004761};
4762
4763/*
4764 * Insns below are selected by the prefix which indexed by the third opcode
4765 * byte.
4766 */
4767static const struct opcode opcode_map_0f_38[256] = {
4768 /* 0x00 - 0x7f */
4769 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004770 /* 0x80 - 0xef */
4771 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4772 /* 0xf0 - 0xf1 */
Nadav Amit53bb4f72014-12-07 11:49:42 +02004773 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0),
4774 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004775 /* 0xf2 - 0xff */
4776 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004777};
4778
Avi Kivity73fba5f2010-07-29 15:11:53 +03004779#undef D
4780#undef N
4781#undef G
4782#undef GD
4783#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004784#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004785#undef EXT
Nadav Amit2276b512015-01-26 09:32:24 +02004786#undef MD
Nadav Amit2b42fce2015-01-26 09:32:25 +02004787#undef ID
Avi Kivity73fba5f2010-07-29 15:11:53 +03004788
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004789#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004790#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004791#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004792#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004793#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004794
Avi Kivity9dac77f2011-06-01 15:34:25 +03004795static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004796{
4797 unsigned size;
4798
Avi Kivity9dac77f2011-06-01 15:34:25 +03004799 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004800 if (size == 8)
4801 size = 4;
4802 return size;
4803}
4804
4805static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4806 unsigned size, bool sign_extension)
4807{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004808 int rc = X86EMUL_CONTINUE;
4809
4810 op->type = OP_IMM;
4811 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004812 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004813 /* NB. Immediates are sign-extended as necessary. */
4814 switch (op->bytes) {
4815 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004816 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004817 break;
4818 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004819 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004820 break;
4821 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004822 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004823 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004824 case 8:
4825 op->val = insn_fetch(s64, ctxt);
4826 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004827 }
4828 if (!sign_extension) {
4829 switch (op->bytes) {
4830 case 1:
4831 op->val &= 0xff;
4832 break;
4833 case 2:
4834 op->val &= 0xffff;
4835 break;
4836 case 4:
4837 op->val &= 0xffffffff;
4838 break;
4839 }
4840 }
4841done:
4842 return rc;
4843}
4844
Avi Kivitya99455492011-09-13 10:45:41 +03004845static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4846 unsigned d)
4847{
4848 int rc = X86EMUL_CONTINUE;
4849
4850 switch (d) {
4851 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004852 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03004853 break;
4854 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004855 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03004856 break;
4857 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004858 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004859 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03004860 *op = ctxt->memop;
4861 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004862 if (ctxt->d & BitOp)
Avi Kivitya99455492011-09-13 10:45:41 +03004863 fetch_bit_operand(ctxt);
4864 op->orig_val = op->val;
4865 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004866 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004867 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004868 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03004869 case OpAcc:
4870 op->type = OP_REG;
4871 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004872 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03004873 fetch_register_operand(op);
4874 op->orig_val = op->val;
4875 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004876 case OpAccLo:
4877 op->type = OP_REG;
4878 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4879 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4880 fetch_register_operand(op);
4881 op->orig_val = op->val;
4882 break;
4883 case OpAccHi:
4884 if (ctxt->d & ByteOp) {
4885 op->type = OP_NONE;
4886 break;
4887 }
4888 op->type = OP_REG;
4889 op->bytes = ctxt->op_bytes;
4890 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4891 fetch_register_operand(op);
4892 op->orig_val = op->val;
4893 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004894 case OpDI:
4895 op->type = OP_MEM;
4896 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4897 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004898 register_address(ctxt, VCPU_REGS_RDI);
Avi Kivitya99455492011-09-13 10:45:41 +03004899 op->addr.mem.seg = VCPU_SREG_ES;
4900 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004901 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03004902 break;
4903 case OpDX:
4904 op->type = OP_REG;
4905 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004906 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03004907 fetch_register_operand(op);
4908 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004909 case OpCL:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004910 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004911 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004912 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004913 break;
4914 case OpImmByte:
4915 rc = decode_imm(ctxt, op, 1, true);
4916 break;
4917 case OpOne:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004918 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004919 op->bytes = 1;
4920 op->val = 1;
4921 break;
4922 case OpImm:
4923 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4924 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004925 case OpImm64:
4926 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4927 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004928 case OpMem8:
4929 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004930 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004931 ctxt->memop.addr.reg = decode_register(ctxt,
4932 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004933 fetch_register_operand(&ctxt->memop);
4934 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004935 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004936 case OpMem16:
4937 ctxt->memop.bytes = 2;
4938 goto mem_common;
4939 case OpMem32:
4940 ctxt->memop.bytes = 4;
4941 goto mem_common;
4942 case OpImmU16:
4943 rc = decode_imm(ctxt, op, 2, false);
4944 break;
4945 case OpImmU:
4946 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4947 break;
4948 case OpSI:
4949 op->type = OP_MEM;
4950 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4951 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004952 register_address(ctxt, VCPU_REGS_RSI);
Bandan Das573e80f2014-04-16 12:46:13 -04004953 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03004954 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004955 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004956 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004957 case OpXLat:
4958 op->type = OP_MEM;
4959 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4960 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004961 address_mask(ctxt,
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004962 reg_read(ctxt, VCPU_REGS_RBX) +
4963 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04004964 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004965 op->val = 0;
4966 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004967 case OpImmFAddr:
4968 op->type = OP_IMM;
4969 op->addr.mem.ea = ctxt->_eip;
4970 op->bytes = ctxt->op_bytes + 2;
4971 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4972 break;
4973 case OpMemFAddr:
4974 ctxt->memop.bytes = ctxt->op_bytes + 2;
4975 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004976 case OpES:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004977 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004978 op->val = VCPU_SREG_ES;
4979 break;
4980 case OpCS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004981 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004982 op->val = VCPU_SREG_CS;
4983 break;
4984 case OpSS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004985 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004986 op->val = VCPU_SREG_SS;
4987 break;
4988 case OpDS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004989 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004990 op->val = VCPU_SREG_DS;
4991 break;
4992 case OpFS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004993 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004994 op->val = VCPU_SREG_FS;
4995 break;
4996 case OpGS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004997 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004998 op->val = VCPU_SREG_GS;
4999 break;
Avi Kivitya99455492011-09-13 10:45:41 +03005000 case OpImplicit:
5001 /* Special instructions do their own operand decoding. */
5002 default:
5003 op->type = OP_NONE; /* Disable writeback. */
5004 break;
5005 }
5006
5007done:
5008 return rc;
5009}
5010
Wanpeng Lib35491e2021-05-27 17:01:37 -07005011int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len, int emulation_type)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005012{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005013 int rc = X86EMUL_CONTINUE;
5014 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03005015 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005016 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04005017 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03005018 struct opcode opcode;
Wanpeng Li3853be22017-11-05 16:54:47 -08005019 u16 dummy;
5020 struct desc_struct desc;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005021
Avi Kivityf09ed832011-09-13 10:45:40 +03005022 ctxt->memop.type = OP_NONE;
5023 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005024 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02005025 ctxt->fetch.ptr = ctxt->fetch.data;
5026 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02005027 ctxt->opcode_len = 1;
Vitaly Kuznetsov342993f2020-03-03 15:33:15 +01005028 ctxt->intercept = x86_intercept_none;
Andre Przywaradc25e892010-12-21 11:12:07 +01005029 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005030 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02005031 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02005032 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02005033 if (rc != X86EMUL_CONTINUE)
Sean Christopherson016cd752019-08-15 09:20:32 -07005034 goto done;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02005035 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005036
5037 switch (mode) {
5038 case X86EMUL_MODE_REAL:
5039 case X86EMUL_MODE_VM86:
Wanpeng Li3853be22017-11-05 16:54:47 -08005040 def_op_bytes = def_ad_bytes = 2;
5041 ctxt->ops->get_segment(ctxt, &dummy, &desc, NULL, VCPU_SREG_CS);
5042 if (desc.d)
5043 def_op_bytes = def_ad_bytes = 4;
5044 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005045 case X86EMUL_MODE_PROT16:
5046 def_op_bytes = def_ad_bytes = 2;
5047 break;
5048 case X86EMUL_MODE_PROT32:
5049 def_op_bytes = def_ad_bytes = 4;
5050 break;
5051#ifdef CONFIG_X86_64
5052 case X86EMUL_MODE_PROT64:
5053 def_op_bytes = 4;
5054 def_ad_bytes = 8;
5055 break;
5056#endif
5057 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005058 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005059 }
5060
Avi Kivity9dac77f2011-06-01 15:34:25 +03005061 ctxt->op_bytes = def_op_bytes;
5062 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005063
5064 /* Legacy prefixes. */
5065 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09005066 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005067 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005068 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005069 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005070 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005071 break;
5072 case 0x67: /* address-size override */
5073 if (mode == X86EMUL_MODE_PROT64)
5074 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005075 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005076 else
5077 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005078 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005079 break;
5080 case 0x26: /* ES override */
Marios Pomonis125ffc52019-12-11 12:47:50 -08005081 has_seg_override = true;
5082 ctxt->seg_override = VCPU_SREG_ES;
5083 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005084 case 0x2e: /* CS override */
Marios Pomonis125ffc52019-12-11 12:47:50 -08005085 has_seg_override = true;
5086 ctxt->seg_override = VCPU_SREG_CS;
5087 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005088 case 0x36: /* SS override */
Marios Pomonis125ffc52019-12-11 12:47:50 -08005089 has_seg_override = true;
5090 ctxt->seg_override = VCPU_SREG_SS;
5091 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005092 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04005093 has_seg_override = true;
Marios Pomonis125ffc52019-12-11 12:47:50 -08005094 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005095 break;
5096 case 0x64: /* FS override */
Marios Pomonis125ffc52019-12-11 12:47:50 -08005097 has_seg_override = true;
5098 ctxt->seg_override = VCPU_SREG_FS;
5099 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005100 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04005101 has_seg_override = true;
Marios Pomonis125ffc52019-12-11 12:47:50 -08005102 ctxt->seg_override = VCPU_SREG_GS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005103 break;
5104 case 0x40 ... 0x4f: /* REX */
5105 if (mode != X86EMUL_MODE_PROT64)
5106 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005107 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005108 continue;
5109 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005110 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005111 break;
5112 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005113 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005114 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005115 break;
5116 default:
5117 goto done_prefixes;
5118 }
5119
5120 /* Any legacy prefix after a REX prefix nullifies its effect. */
5121
Avi Kivity9dac77f2011-06-01 15:34:25 +03005122 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005123 }
5124
5125done_prefixes:
5126
5127 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005128 if (ctxt->rex_prefix & 8)
5129 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005130
5131 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005132 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08005133 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005134 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02005135 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09005136 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03005137 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005138
5139 /* 0F_38 opcode map */
5140 if (ctxt->b == 0x38) {
5141 ctxt->opcode_len = 3;
5142 ctxt->b = insn_fetch(u8, ctxt);
5143 opcode = opcode_map_0f_38[ctxt->b];
5144 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005145 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03005146 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005147
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09005148 if (ctxt->d & ModRM)
5149 ctxt->modrm = insn_fetch(u8, ctxt);
5150
Nadav Amit7fe864d2014-06-02 18:34:03 +03005151 /* vex-prefix instructions are not implemented */
5152 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
Nadav Amitd14cb5d2014-11-02 11:54:58 +02005153 (mode == X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) == 0xc0)) {
Nadav Amit7fe864d2014-06-02 18:34:03 +03005154 ctxt->d = NotImpl;
5155 }
5156
Avi Kivity9dac77f2011-06-01 15:34:25 +03005157 while (ctxt->d & GroupMask) {
5158 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03005159 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005160 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03005161 opcode = opcode.u.group[goffset];
5162 break;
5163 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005164 goffset = (ctxt->modrm >> 3) & 7;
5165 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03005166 opcode = opcode.u.gdual->mod3[goffset];
5167 else
5168 opcode = opcode.u.gdual->mod012[goffset];
5169 break;
5170 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005171 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02005172 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03005173 break;
5174 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005175 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005176 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005177 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03005178 switch (simd_prefix) {
5179 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
5180 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
5181 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
5182 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
5183 }
5184 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02005185 case Escape:
Marios Pomonis3c9053a22019-12-11 12:47:41 -08005186 if (ctxt->modrm > 0xbf) {
5187 size_t size = ARRAY_SIZE(opcode.u.esc->high);
5188 u32 index = array_index_nospec(
5189 ctxt->modrm - 0xc0, size);
5190
5191 opcode = opcode.u.esc->high[index];
5192 } else {
Gleb Natapov045a2822012-12-20 16:57:43 +02005193 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
Marios Pomonis3c9053a22019-12-11 12:47:41 -08005194 }
Gleb Natapov045a2822012-12-20 16:57:43 +02005195 break;
Nadav Amit39f062f2014-11-26 15:47:18 +02005196 case InstrDual:
5197 if ((ctxt->modrm >> 6) == 3)
5198 opcode = opcode.u.idual->mod3;
5199 else
5200 opcode = opcode.u.idual->mod012;
5201 break;
Nadav Amit2276b512015-01-26 09:32:24 +02005202 case ModeDual:
5203 if (ctxt->mode == X86EMUL_MODE_PROT64)
5204 opcode = opcode.u.mdual->mode64;
5205 else
5206 opcode = opcode.u.mdual->mode32;
5207 break;
Avi Kivity46561642011-04-24 14:09:59 +03005208 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005209 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005210 }
Avi Kivity46561642011-04-24 14:09:59 +03005211
Avi Kivityb1ea50b2011-09-13 10:45:42 +03005212 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005213 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005214 }
5215
Paolo Bonzinie24186e2014-03-27 12:00:57 +01005216 /* Unrecognised? */
5217 if (ctxt->d == 0)
5218 return EMULATION_FAILED;
5219
Avi Kivity9dac77f2011-06-01 15:34:25 +03005220 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005221
Wanpeng Lib35491e2021-05-27 17:01:37 -07005222 if (unlikely(emulation_type & EMULTYPE_TRAP_UD) &&
5223 likely(!(ctxt->d & EmulateOnUD)))
Nadav Amit3a6095a2014-08-13 16:50:13 +03005224 return EMULATION_FAILED;
5225
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005226 if (unlikely(ctxt->d &
Nadav Amited9aad22014-11-02 11:55:00 +02005227 (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch|
5228 No16))) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005229 /*
5230 * These are copied unconditionally here, and checked unconditionally
5231 * in x86_emulate_insn.
5232 */
5233 ctxt->check_perm = opcode.check_perm;
5234 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005235
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005236 if (ctxt->d & NotImpl)
5237 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02005238
Nadav Amit58b70752014-10-24 11:35:09 +03005239 if (mode == X86EMUL_MODE_PROT64) {
5240 if (ctxt->op_bytes == 4 && (ctxt->d & Stack))
5241 ctxt->op_bytes = 8;
5242 else if (ctxt->d & NearBranch)
5243 ctxt->op_bytes = 8;
5244 }
Avi Kivity7f9b4b72010-08-01 14:46:54 +03005245
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005246 if (ctxt->d & Op3264) {
5247 if (mode == X86EMUL_MODE_PROT64)
5248 ctxt->op_bytes = 8;
5249 else
5250 ctxt->op_bytes = 4;
5251 }
5252
Nadav Amited9aad22014-11-02 11:55:00 +02005253 if ((ctxt->d & No16) && ctxt->op_bytes == 2)
5254 ctxt->op_bytes = 4;
5255
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005256 if (ctxt->d & Sse)
5257 ctxt->op_bytes = 16;
5258 else if (ctxt->d & Mmx)
5259 ctxt->op_bytes = 8;
5260 }
Avi Kivity1253791d2011-03-29 11:41:27 +02005261
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005262 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005263 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03005264 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04005265 if (!has_seg_override) {
5266 has_seg_override = true;
5267 ctxt->seg_override = ctxt->modrm_seg;
5268 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03005269 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03005270 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005271 if (rc != X86EMUL_CONTINUE)
5272 goto done;
5273
Bandan Das573e80f2014-04-16 12:46:13 -04005274 if (!has_seg_override)
5275 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005276
Bandan Das573e80f2014-04-16 12:46:13 -04005277 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005278
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005279 /*
5280 * Decode and fetch the source operand: register, memory
5281 * or immediate.
5282 */
Avi Kivity0fe59122011-09-13 10:45:47 +03005283 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03005284 if (rc != X86EMUL_CONTINUE)
5285 goto done;
5286
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005287 /*
5288 * Decode and fetch the second source operand: register, memory
5289 * or immediate.
5290 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03005291 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03005292 if (rc != X86EMUL_CONTINUE)
5293 goto done;
5294
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005295 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03005296 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005297
Owen Hofmannd9092f52016-10-27 11:25:52 -07005298 if (ctxt->rip_relative && likely(ctxt->memopp))
Nadav Amit1c1c35a2014-11-19 17:43:09 +02005299 ctxt->memopp->addr.mem.ea = address_mask(ctxt,
5300 ctxt->memopp->addr.mem.ea + ctxt->_eip);
Avi Kivitycb16c342011-06-19 19:21:11 +03005301
Paolo Bonzinia430c912014-10-23 14:54:14 +02005302done:
Jan Dakinevichc8848ce2019-08-27 13:07:08 +00005303 if (rc == X86EMUL_PROPAGATE_FAULT)
5304 ctxt->have_exception = true;
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005305 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005306}
5307
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005308bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
5309{
5310 return ctxt->d & PageTable;
5311}
5312
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005313static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
5314{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005315 /* The second termination condition only applies for REPE
5316 * and REPNE. Test if the repeat string operation prefix is
5317 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
5318 * corresponding termination condition according to:
5319 * - if REPE/REPZ and ZF = 0 then done
5320 * - if REPNE/REPNZ and ZF = 1 then done
5321 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005322 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
5323 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
5324 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03005325 ((ctxt->eflags & X86_EFLAGS_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03005326 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03005327 ((ctxt->eflags & X86_EFLAGS_ZF) == X86_EFLAGS_ZF))))
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005328 return true;
5329
5330 return false;
5331}
5332
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005333static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
5334{
Radim Krčmářaabba3c2016-11-08 20:54:18 +01005335 int rc;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005336
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02005337 kvm_fpu_get();
Radim Krčmářaabba3c2016-11-08 20:54:18 +01005338 rc = asm_safe("fwait");
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02005339 kvm_fpu_put();
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005340
Radim Krčmářaabba3c2016-11-08 20:54:18 +01005341 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005342 return emulate_exception(ctxt, MF_VECTOR, 0, false);
5343
5344 return X86EMUL_CONTINUE;
5345}
5346
Sean Christophersonc0a21c32020-01-17 11:30:52 -08005347static void fetch_possible_mmx_operand(struct operand *op)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005348{
5349 if (op->type == OP_MM)
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02005350 kvm_read_mmx_reg(op->addr.mm, &op->mm_val);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005351}
5352
Sean Christopherson3009afc2020-01-21 20:43:39 -08005353static int fastop(struct x86_emulate_ctxt *ctxt, fastop_t fop)
Avi Kivitye28bbd42013-01-04 16:18:48 +02005354{
5355 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005356
Avi Kivityb9fa4092013-02-09 11:31:48 +02005357 if (!(ctxt->d & ByteOp))
5358 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005359
Peter Zijlstra1a29b5b2018-01-25 10:58:13 +01005360 asm("push %[flags]; popf; " CALL_NOSPEC " ; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005361 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
Peter Zijlstra1a29b5b2018-01-25 10:58:13 +01005362 [thunk_target]"+S"(fop), ASM_CALL_CONSTRAINT
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005363 : "c"(ctxt->src2.val));
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005364
Avi Kivitye28bbd42013-01-04 16:18:48 +02005365 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005366 if (!fop) /* exception is returned in fop variable */
5367 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02005368 return X86EMUL_CONTINUE;
5369}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005370
Bandan Das14985072014-04-16 12:46:09 -04005371void init_decode_cache(struct x86_emulate_ctxt *ctxt)
5372{
Bandan Das573e80f2014-04-16 12:46:13 -04005373 memset(&ctxt->rip_relative, 0,
5374 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04005375
Bandan Das14985072014-04-16 12:46:09 -04005376 ctxt->io_read.pos = 0;
5377 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04005378 ctxt->mem_read.end = 0;
5379}
5380
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09005381int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005382{
Mathias Krause0225fb52012-08-30 01:30:16 +02005383 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09005384 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005385 int saved_dst_type = ctxt->dst.type;
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005386 unsigned emul_flags;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005387
Avi Kivity9dac77f2011-06-01 15:34:25 +03005388 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04005389
Gleb Natapovd380a5e2010-02-10 14:21:36 +02005390 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005391 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02005392 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02005393 goto done;
5394 }
5395
Avi Kivity9dac77f2011-06-01 15:34:25 +03005396 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02005397 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03005398 goto done;
5399 }
5400
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005401 emul_flags = ctxt->ops->get_hflags(ctxt);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005402 if (unlikely(ctxt->d &
5403 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
5404 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
5405 (ctxt->d & Undefined)) {
5406 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005407 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005408 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005409
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005410 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
5411 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
5412 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02005413 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005414 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02005415
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005416 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
5417 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02005418 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005419 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02005420
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005421 if (ctxt->d & Mmx) {
5422 rc = flush_pending_x87_faults(ctxt);
5423 if (rc != X86EMUL_CONTINUE)
5424 goto done;
5425 /*
5426 * Now that we know the fpu is exception safe, we can fetch
5427 * operands from it.
5428 */
Sean Christophersonc0a21c32020-01-17 11:30:52 -08005429 fetch_possible_mmx_operand(&ctxt->src);
5430 fetch_possible_mmx_operand(&ctxt->src2);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005431 if (!(ctxt->d & Mov))
Sean Christophersonc0a21c32020-01-17 11:30:52 -08005432 fetch_possible_mmx_operand(&ctxt->dst);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005433 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02005434
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005435 if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && ctxt->intercept) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005436 rc = emulator_check_intercept(ctxt, ctxt->intercept,
5437 X86_ICPT_PRE_EXCEPT);
5438 if (rc != X86EMUL_CONTINUE)
5439 goto done;
5440 }
5441
Nadav Amit64a38292014-12-10 11:19:04 +02005442 /* Instruction can only be executed in protected mode */
5443 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
5444 rc = emulate_ud(ctxt);
5445 goto done;
5446 }
5447
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005448 /* Privileged instruction can be executed only in CPL=0 */
5449 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Nadav Amit68efa762014-06-18 17:19:35 +03005450 if (ctxt->d & PrivUD)
5451 rc = emulate_ud(ctxt);
5452 else
5453 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02005454 goto done;
5455 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005456
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005457 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04005458 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005459 rc = ctxt->check_perm(ctxt);
5460 if (rc != X86EMUL_CONTINUE)
5461 goto done;
5462 }
5463
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005464 if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005465 rc = emulator_check_intercept(ctxt, ctxt->intercept,
5466 X86_ICPT_POST_EXCEPT);
5467 if (rc != X86EMUL_CONTINUE)
5468 goto done;
5469 }
5470
5471 if (ctxt->rep_prefix && (ctxt->d & String)) {
5472 /* All REP prefixes have the same first termination condition */
5473 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Nadav Amit428e3d02015-04-28 13:06:01 +03005474 string_registers_quirk(ctxt);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005475 ctxt->eip = ctxt->_eip;
Nadav Amit0efb0442015-03-29 16:33:03 +03005476 ctxt->eflags &= ~X86_EFLAGS_RF;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005477 goto done;
5478 }
5479 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02005480 }
5481
Avi Kivity9dac77f2011-06-01 15:34:25 +03005482 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
5483 rc = segmented_read(ctxt, ctxt->src.addr.mem,
5484 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09005485 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005486 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005487 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005488 }
5489
Avi Kivity9dac77f2011-06-01 15:34:25 +03005490 if (ctxt->src2.type == OP_MEM) {
5491 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
5492 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02005493 if (rc != X86EMUL_CONTINUE)
5494 goto done;
5495 }
5496
Avi Kivity9dac77f2011-06-01 15:34:25 +03005497 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005498 goto special_insn;
5499
5500
Avi Kivity9dac77f2011-06-01 15:34:25 +03005501 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02005502 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005503 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
5504 &ctxt->dst.val, ctxt->dst.bytes);
Nadav Amitc205fb72014-12-25 02:52:16 +02005505 if (rc != X86EMUL_CONTINUE) {
Paolo Bonzinid44e1212015-02-09 10:02:05 +01005506 if (!(ctxt->d & NoWrite) &&
5507 rc == X86EMUL_PROPAGATE_FAULT &&
Nadav Amitc205fb72014-12-25 02:52:16 +02005508 ctxt->exception.vector == PF_VECTOR)
5509 ctxt->exception.error_code |= PFERR_WRITE_MASK;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02005510 goto done;
Nadav Amitc205fb72014-12-25 02:52:16 +02005511 }
Avi Kivity038e51d2007-01-22 20:40:40 -08005512 }
Paolo Bonzini4ff6f8e2015-02-12 17:04:47 +01005513 /* Copy full 64-bit value for CMPXCHG8B. */
5514 ctxt->dst.orig_val64 = ctxt->dst.val64;
Avi Kivity038e51d2007-01-22 20:40:40 -08005515
Avi Kivity018a98d2007-11-27 19:30:56 +02005516special_insn:
5517
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005518 if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03005519 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005520 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02005521 if (rc != X86EMUL_CONTINUE)
5522 goto done;
5523 }
5524
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03005525 if (ctxt->rep_prefix && (ctxt->d & String))
Nadav Amit0efb0442015-03-29 16:33:03 +03005526 ctxt->eflags |= X86_EFLAGS_RF;
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03005527 else
Nadav Amit0efb0442015-03-29 16:33:03 +03005528 ctxt->eflags &= ~X86_EFLAGS_RF;
Nadav Amit4467c3f2014-07-21 14:37:29 +03005529
Avi Kivity9dac77f2011-06-01 15:34:25 +03005530 if (ctxt->execute) {
Sean Christopherson3009afc2020-01-21 20:43:39 -08005531 if (ctxt->d & Fastop)
Qian Caib78a8552020-02-17 11:48:26 -05005532 rc = fastop(ctxt, ctxt->fop);
Sean Christopherson3009afc2020-01-21 20:43:39 -08005533 else
Miaohe Lin52db3692020-01-22 11:21:44 +08005534 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03005535 if (rc != X86EMUL_CONTINUE)
5536 goto done;
5537 goto writeback;
5538 }
5539
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02005540 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005541 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005542 else if (ctxt->opcode_len == 3)
5543 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005544
Avi Kivity9dac77f2011-06-01 15:34:25 +03005545 switch (ctxt->b) {
Gleb Natapovb2833e32009-04-12 13:36:30 +03005546 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005547 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005548 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005549 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03005550 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005551 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03005552 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03005553 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005554 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03005555 ctxt->dst.type = OP_NONE;
5556 else
5557 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09005558 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005559 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005560 switch (ctxt->op_bytes) {
5561 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
5562 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
5563 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005564 }
5565 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005566 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005567 rc = emulate_int(ctxt, 3);
5568 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005569 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005570 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005571 break;
5572 case 0xce: /* into */
Nadav Amit0efb0442015-03-29 16:33:03 +03005573 if (ctxt->eflags & X86_EFLAGS_OF)
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005574 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005575 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005576 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09005577 case 0xeb: /* jmp rel short */
Nadav Amit234f3ce2014-09-18 22:39:38 +03005578 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity9dac77f2011-06-01 15:34:25 +03005579 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005580 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005581 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03005582 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03005583 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005584 case 0xf5: /* cmc */
5585 /* complement carry flag from eflags reg */
Nadav Amit0efb0442015-03-29 16:33:03 +03005586 ctxt->eflags ^= X86_EFLAGS_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005587 break;
5588 case 0xf8: /* clc */
Nadav Amit0efb0442015-03-29 16:33:03 +03005589 ctxt->eflags &= ~X86_EFLAGS_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005590 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03005591 case 0xf9: /* stc */
Nadav Amit0efb0442015-03-29 16:33:03 +03005592 ctxt->eflags |= X86_EFLAGS_CF;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03005593 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005594 case 0xfc: /* cld */
Nadav Amit0efb0442015-03-29 16:33:03 +03005595 ctxt->eflags &= ~X86_EFLAGS_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005596 break;
5597 case 0xfd: /* std */
Nadav Amit0efb0442015-03-29 16:33:03 +03005598 ctxt->eflags |= X86_EFLAGS_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005599 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005600 default:
5601 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005602 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005603
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005604 if (rc != X86EMUL_CONTINUE)
5605 goto done;
5606
Avi Kivity018a98d2007-11-27 19:30:56 +02005607writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02005608 if (ctxt->d & SrcWrite) {
5609 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
5610 rc = writeback(ctxt, &ctxt->src);
5611 if (rc != X86EMUL_CONTINUE)
5612 goto done;
5613 }
Nadav Amitee212292014-06-15 16:12:58 +03005614 if (!(ctxt->d & NoWrite)) {
5615 rc = writeback(ctxt, &ctxt->dst);
5616 if (rc != X86EMUL_CONTINUE)
5617 goto done;
5618 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005619
Gleb Natapov5cd21912010-03-18 15:20:26 +02005620 /*
5621 * restore dst type in case the decoding will be reused
5622 * (happens for string instruction )
5623 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005624 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005625
Avi Kivity9dac77f2011-06-01 15:34:25 +03005626 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005627 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02005628
Avi Kivity9dac77f2011-06-01 15:34:25 +03005629 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005630 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02005631
Avi Kivity9dac77f2011-06-01 15:34:25 +03005632 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005633 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005634 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005635 if ((ctxt->d & SrcMask) == SrcSI)
5636 count = ctxt->src.count;
5637 else
5638 count = ctxt->dst.count;
Paolo Bonzini01485a22014-11-19 18:25:08 +01005639 register_address_increment(ctxt, VCPU_REGS_RCX, -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005640
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005641 if (!string_insn_completed(ctxt)) {
5642 /*
5643 * Re-enter guest when pio read ahead buffer is empty
5644 * or, if it is not used, after each 1024 iteration.
5645 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005646 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005647 (r->end == 0 || r->end != r->pos)) {
5648 /*
5649 * Reset read cache. Usually happens before
5650 * decode, but since instruction is restarted
5651 * we have to do it here.
5652 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005653 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005654 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005655 return EMULATION_RESTART;
5656 }
5657 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03005658 }
Nadav Amit0efb0442015-03-29 16:33:03 +03005659 ctxt->eflags &= ~X86_EFLAGS_RF;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005660 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005661
Avi Kivity9dac77f2011-06-01 15:34:25 +03005662 ctxt->eip = ctxt->_eip;
Paolo Bonzinifede8072020-04-27 11:55:59 -04005663 if (ctxt->mode != X86EMUL_MODE_PROT64)
5664 ctxt->eip = (u32)ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02005665
5666done:
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005667 if (rc == X86EMUL_PROPAGATE_FAULT) {
5668 WARN_ON(ctxt->exception.vector > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +02005669 ctxt->have_exception = true;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005670 }
Joerg Roedel775fde82011-04-04 12:39:24 +02005671 if (rc == X86EMUL_INTERCEPTED)
5672 return EMULATION_INTERCEPTED;
5673
Avi Kivitydd856ef2012-08-27 23:46:17 +03005674 if (rc == X86EMUL_CONTINUE)
5675 writeback_registers(ctxt);
5676
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005677 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005678
5679twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005680 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02005681 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02005682 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005683 break;
5684 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02005685 case 0x0d: /* GrpP (prefetch) */
5686 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02005687 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02005688 break;
5689 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005690 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02005691 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005692 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005693 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005694 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005695 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03005696 if (test_cc(ctxt->b, ctxt->eflags))
5697 ctxt->dst.val = ctxt->src.val;
Nadav Amitb91aa142015-03-30 15:39:19 +03005698 else if (ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005699 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08005700 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03005701 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03005702 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005703 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005704 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08005705 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005706 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08005707 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005708 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005709 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005710 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03005711 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005712 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005713 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005714 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005715 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03005716 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005717 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005718 default:
5719 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005720 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005721
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005722threebyte_insn:
5723
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005724 if (rc != X86EMUL_CONTINUE)
5725 goto done;
5726
Avi Kivity6aa8b732006-12-10 02:21:36 -08005727 goto writeback;
5728
5729cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02005730 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005731}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005732
5733void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
5734{
5735 invalidate_registers(ctxt);
5736}
5737
5738void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
5739{
5740 writeback_registers(ctxt);
5741}
Tom Lendacky0f89b202016-12-14 14:59:23 -05005742
5743bool emulator_can_use_gpa(struct x86_emulate_ctxt *ctxt)
5744{
5745 if (ctxt->rep_prefix && (ctxt->d & String))
5746 return false;
5747
5748 if (ctxt->d & TwoMemOp)
5749 return false;
5750
5751 return true;
5752}