blob: 4996eec7aa79fce12109989639f859c1f0716711 [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
Sean Christophersonedce4652021-06-09 11:56:13 -07002538 ctxt->ops->exiting_smm(ctxt);
Sean Christopherson9ec19492019-04-02 08:03:11 -07002539
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002540 /*
2541 * Get back to real mode, to prepare a safe state in which to load
Paolo Bonzini89651a32015-11-03 13:43:05 +01002542 * CR0/CR3/CR4/EFER. It's all a bit more complicated if the vCPU
2543 * supports long mode.
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002544 */
Paolo Bonzini89651a32015-11-03 13:43:05 +01002545 if (emulator_has_longmode(ctxt)) {
2546 struct desc_struct cs_desc;
2547
2548 /* Zero CR4.PCIDE before CR0.PG. */
Sean Christopherson8f4dc2e2019-04-02 08:10:47 -07002549 cr4 = ctxt->ops->get_cr(ctxt, 4);
2550 if (cr4 & X86_CR4_PCIDE)
Paolo Bonzini89651a32015-11-03 13:43:05 +01002551 ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PCIDE);
Paolo Bonzini89651a32015-11-03 13:43:05 +01002552
2553 /* A 32-bit code segment is required to clear EFER.LMA. */
2554 memset(&cs_desc, 0, sizeof(cs_desc));
2555 cs_desc.type = 0xb;
2556 cs_desc.s = cs_desc.g = cs_desc.p = 1;
2557 ctxt->ops->set_segment(ctxt, 0, &cs_desc, 0, VCPU_SREG_CS);
2558 }
2559
2560 /* For the 64-bit case, this will clear EFER.LMA. */
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002561 cr0 = ctxt->ops->get_cr(ctxt, 0);
2562 if (cr0 & X86_CR0_PE)
2563 ctxt->ops->set_cr(ctxt, 0, cr0 & ~(X86_CR0_PG | X86_CR0_PE));
Paolo Bonzini89651a32015-11-03 13:43:05 +01002564
Sean Christopherson8f4dc2e2019-04-02 08:10:47 -07002565 if (emulator_has_longmode(ctxt)) {
2566 /* Clear CR4.PAE before clearing EFER.LME. */
2567 cr4 = ctxt->ops->get_cr(ctxt, 4);
2568 if (cr4 & X86_CR4_PAE)
2569 ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PAE);
Paolo Bonzini89651a32015-11-03 13:43:05 +01002570
Sean Christopherson8f4dc2e2019-04-02 08:10:47 -07002571 /* And finally go back to 32-bit mode. */
2572 efer = 0;
2573 ctxt->ops->set_msr(ctxt, MSR_EFER, efer);
2574 }
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002575
Ladi Prosek0234bf82017-10-11 16:54:40 +02002576 /*
2577 * Give pre_leave_smm() a chance to make ISA-specific changes to the
2578 * vCPU state (e.g. enter guest mode) before loading state from the SMM
2579 * state-save area.
2580 */
Sean Christophersoned193212019-04-02 08:03:09 -07002581 if (ctxt->ops->pre_leave_smm(ctxt, buf))
Sean Christopherson25b17222021-06-09 11:56:12 -07002582 goto emulate_shutdown;
Ladi Prosek0234bf82017-10-11 16:54:40 +02002583
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002584#ifdef CONFIG_X86_64
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002585 if (emulator_has_longmode(ctxt))
Sean Christophersoned193212019-04-02 08:03:09 -07002586 ret = rsm_load_state_64(ctxt, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002587 else
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07002588#endif
Sean Christophersoned193212019-04-02 08:03:09 -07002589 ret = rsm_load_state_32(ctxt, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002590
Sean Christopherson25b17222021-06-09 11:56:12 -07002591 if (ret != X86EMUL_CONTINUE)
2592 goto emulate_shutdown;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002593
Sean Christophersonc5833c72019-04-02 08:03:10 -07002594 ctxt->ops->post_leave_smm(ctxt);
2595
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002596 return X86EMUL_CONTINUE;
Sean Christopherson25b17222021-06-09 11:56:12 -07002597
2598emulate_shutdown:
2599 ctxt->ops->triple_fault(ctxt);
2600 return X86EMUL_CONTINUE;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002601}
2602
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002603static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002604setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002605 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002606{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002607 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002608 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002609 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002610 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002611 cs->type = 0x0b; /* Read, Execute, Accessed */
2612 cs->s = 1;
2613 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002614 cs->p = 1;
2615 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002616 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002617
Gleb Natapov79168fd2010-04-28 19:15:30 +03002618 set_desc_base(ss, 0); /* flat segment */
2619 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002620 ss->g = 1; /* 4kb granularity */
2621 ss->s = 1;
2622 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002623 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002624 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002625 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002626 ss->l = 0;
2627 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002628}
2629
Avi Kivity1a18a692012-02-01 12:23:21 +02002630static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2631{
2632 u32 eax, ebx, ecx, edx;
2633
2634 eax = ecx = 0;
Sean Christophersonf91af512020-03-04 17:34:37 -08002635 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, true);
Sean Christopherson15608ed2020-03-04 17:34:32 -08002636 return is_guest_vendor_intel(ebx, ecx, edx);
Avi Kivity1a18a692012-02-01 12:23:21 +02002637}
2638
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002639static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2640{
Mathias Krause0225fb52012-08-30 01:30:16 +02002641 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002642 u32 eax, ebx, ecx, edx;
2643
2644 /*
2645 * syscall should always be enabled in longmode - so only become
2646 * vendor specific (cpuid) if other modes are active...
2647 */
2648 if (ctxt->mode == X86EMUL_MODE_PROT64)
2649 return true;
2650
2651 eax = 0x00000000;
2652 ecx = 0x00000000;
Sean Christophersonf91af512020-03-04 17:34:37 -08002653 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, true);
Avi Kivity0017f932012-06-07 14:10:16 +03002654 /*
Sean Christopherson15608ed2020-03-04 17:34:32 -08002655 * remark: Intel CPUs only support "syscall" in 64bit longmode. Also a
2656 * 64bit guest with a 32bit compat-app running will #UD !! While this
2657 * behaviour can be fixed (by emulating) into AMD response - CPUs of
2658 * AMD can't behave like Intel.
Avi Kivity0017f932012-06-07 14:10:16 +03002659 */
Sean Christopherson15608ed2020-03-04 17:34:32 -08002660 if (is_guest_vendor_intel(ebx, ecx, edx))
Avi Kivity0017f932012-06-07 14:10:16 +03002661 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002662
Sean Christopherson15608ed2020-03-04 17:34:32 -08002663 if (is_guest_vendor_amd(ebx, ecx, edx) ||
2664 is_guest_vendor_hygon(ebx, ecx, edx))
Pu Wenb8f4abb2018-09-23 17:36:31 +08002665 return true;
2666
2667 /*
2668 * default: (not Intel, not AMD, not Hygon), apply Intel's
2669 * stricter rules...
2670 */
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002671 return false;
2672}
2673
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002674static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002675{
Mathias Krause0225fb52012-08-30 01:30:16 +02002676 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002677 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002678 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002679 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002680 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002681
2682 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002683 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002684 ctxt->mode == X86EMUL_MODE_VM86)
2685 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002686
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002687 if (!(em_syscall_is_enabled(ctxt)))
2688 return emulate_ud(ctxt);
2689
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002690 ops->get_msr(ctxt, MSR_EFER, &efer);
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002691 if (!(efer & EFER_SCE))
2692 return emulate_ud(ctxt);
2693
Miaohe Lin5b4ce932019-11-09 16:58:54 +08002694 setup_syscalls_segments(ctxt, &cs, &ss);
Avi Kivity717746e2011-04-20 13:37:53 +03002695 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002696 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002697 cs_sel = (u16)(msr_data & 0xfffc);
2698 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002699
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002700 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002701 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002702 cs.l = 1;
2703 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002704 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2705 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002706
Avi Kivitydd856ef2012-08-27 23:46:17 +03002707 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002708 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002709#ifdef CONFIG_X86_64
Nadav Amit6c6cb692014-07-21 14:37:30 +03002710 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002711
Avi Kivity717746e2011-04-20 13:37:53 +03002712 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002713 ctxt->mode == X86EMUL_MODE_PROT64 ?
2714 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002715 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002716
Avi Kivity717746e2011-04-20 13:37:53 +03002717 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Nadav Amit6c6cb692014-07-21 14:37:30 +03002718 ctxt->eflags &= ~msr_data;
Wanpeng Li35fd68a2015-04-08 14:08:14 +08002719 ctxt->eflags |= X86_EFLAGS_FIXED;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002720#endif
2721 } else {
2722 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002723 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002724 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002725
Nadav Amit0efb0442015-03-29 16:33:03 +03002726 ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002727 }
2728
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02002729 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002730 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002731}
2732
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002733static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002734{
Mathias Krause0225fb52012-08-30 01:30:16 +02002735 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002736 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002737 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002738 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002739 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002740
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002741 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002742 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002743 if (ctxt->mode == X86EMUL_MODE_REAL)
2744 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002745
Avi Kivity1a18a692012-02-01 12:23:21 +02002746 /*
2747 * Not recognized on AMD in compat mode (but is recognized in legacy
2748 * mode).
2749 */
Nadav Amitf3747372015-01-01 23:11:11 +02002750 if ((ctxt->mode != X86EMUL_MODE_PROT64) && (efer & EFER_LMA)
Avi Kivity1a18a692012-02-01 12:23:21 +02002751 && !vendor_intel(ctxt))
2752 return emulate_ud(ctxt);
2753
Nadav Amitb2c9d432014-11-02 11:55:01 +02002754 /* sysenter/sysexit have not been tested in 64bit mode. */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002755 if (ctxt->mode == X86EMUL_MODE_PROT64)
Nadav Amitb2c9d432014-11-02 11:55:01 +02002756 return X86EMUL_UNHANDLEABLE;
Andre Przywara8c604352009-06-18 12:56:01 +02002757
Avi Kivity717746e2011-04-20 13:37:53 +03002758 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002759 if ((msr_data & 0xfffc) == 0x0)
2760 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002761
Miaohe Lin5b4ce932019-11-09 16:58:54 +08002762 setup_syscalls_segments(ctxt, &cs, &ss);
Nadav Amit0efb0442015-03-29 16:33:03 +03002763 ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
Nadav Amitb32a9912015-03-29 16:33:04 +03002764 cs_sel = (u16)msr_data & ~SEGMENT_RPL_MASK;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002765 ss_sel = cs_sel + 8;
Nadav Amitf3747372015-01-01 23:11:11 +02002766 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002767 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002768 cs.l = 1;
2769 }
2770
Avi Kivity1aa36612011-04-27 13:20:30 +03002771 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2772 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002773
Avi Kivity717746e2011-04-20 13:37:53 +03002774 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002775 ctxt->_eip = (efer & EFER_LMA) ? msr_data : (u32)msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002776
Avi Kivity717746e2011-04-20 13:37:53 +03002777 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002778 *reg_write(ctxt, VCPU_REGS_RSP) = (efer & EFER_LMA) ? msr_data :
2779 (u32)msr_data;
Sean Christopherson943dea82021-02-02 08:55:46 -08002780 if (efer & EFER_LMA)
2781 ctxt->mode = X86EMUL_MODE_PROT64;
Andre Przywara8c604352009-06-18 12:56:01 +02002782
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002783 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002784}
2785
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002786static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002787{
Mathias Krause0225fb52012-08-30 01:30:16 +02002788 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002789 struct desc_struct cs, ss;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002790 u64 msr_data, rcx, rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002791 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002792 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002793
Gleb Natapova0044752010-02-10 14:21:31 +02002794 /* inject #GP if in real mode or Virtual 8086 mode */
2795 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002796 ctxt->mode == X86EMUL_MODE_VM86)
2797 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002798
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002799 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002800
Avi Kivity9dac77f2011-06-01 15:34:25 +03002801 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002802 usermode = X86EMUL_MODE_PROT64;
2803 else
2804 usermode = X86EMUL_MODE_PROT32;
2805
Nadav Amit234f3ce2014-09-18 22:39:38 +03002806 rcx = reg_read(ctxt, VCPU_REGS_RCX);
2807 rdx = reg_read(ctxt, VCPU_REGS_RDX);
2808
Andre Przywara4668f052009-06-18 12:56:02 +02002809 cs.dpl = 3;
2810 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002811 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002812 switch (usermode) {
2813 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002814 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002815 if ((msr_data & 0xfffc) == 0x0)
2816 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002817 ss_sel = (u16)(msr_data + 24);
Nadav Amitbf0b6822014-09-18 22:39:45 +03002818 rcx = (u32)rcx;
2819 rdx = (u32)rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002820 break;
2821 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002822 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002823 if (msr_data == 0x0)
2824 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002825 ss_sel = cs_sel + 8;
2826 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002827 cs.l = 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08002828 if (emul_is_noncanonical_address(rcx, ctxt) ||
2829 emul_is_noncanonical_address(rdx, ctxt))
Nadav Amit234f3ce2014-09-18 22:39:38 +03002830 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002831 break;
2832 }
Nadav Amitb32a9912015-03-29 16:33:04 +03002833 cs_sel |= SEGMENT_RPL_MASK;
2834 ss_sel |= SEGMENT_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002835
Avi Kivity1aa36612011-04-27 13:20:30 +03002836 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2837 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002838
Nadav Amit234f3ce2014-09-18 22:39:38 +03002839 ctxt->_eip = rdx;
2840 *reg_write(ctxt, VCPU_REGS_RSP) = rcx;
Andre Przywara4668f052009-06-18 12:56:02 +02002841
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002842 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002843}
2844
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002845static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002846{
2847 int iopl;
2848 if (ctxt->mode == X86EMUL_MODE_REAL)
2849 return false;
2850 if (ctxt->mode == X86EMUL_MODE_VM86)
2851 return true;
Nadav Amit0efb0442015-03-29 16:33:03 +03002852 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002853 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002854}
2855
Liran Alon9a29d4492018-03-12 13:12:48 +02002856#define VMWARE_PORT_VMPORT (0x5658)
2857#define VMWARE_PORT_VMRPC (0x5659)
2858
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002859static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002860 u16 port, u16 len)
2861{
Mathias Krause0225fb52012-08-30 01:30:16 +02002862 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002863 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002864 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002865 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002866 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002867 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002868 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002869
Liran Alon9a29d4492018-03-12 13:12:48 +02002870 /*
2871 * VMware allows access to these ports even if denied
2872 * by TSS I/O permission bitmap. Mimic behavior.
2873 */
2874 if (enable_vmware_backdoor &&
2875 ((port == VMWARE_PORT_VMPORT) || (port == VMWARE_PORT_VMRPC)))
2876 return true;
2877
Avi Kivity1aa36612011-04-27 13:20:30 +03002878 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002879 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002880 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002881 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002882 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002883 base = get_desc_base(&tr_seg);
2884#ifdef CONFIG_X86_64
2885 base |= ((u64)base3) << 32;
2886#endif
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02002887 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL, true);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002888 if (r != X86EMUL_CONTINUE)
2889 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002890 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002891 return false;
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02002892 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL, true);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002893 if (r != X86EMUL_CONTINUE)
2894 return false;
2895 if ((perm >> bit_idx) & mask)
2896 return false;
2897 return true;
2898}
2899
2900static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002901 u16 port, u16 len)
2902{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002903 if (ctxt->perm_ok)
2904 return true;
2905
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002906 if (emulator_bad_iopl(ctxt))
2907 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002908 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002909
2910 ctxt->perm_ok = true;
2911
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002912 return true;
2913}
2914
Nadav Amit428e3d02015-04-28 13:06:01 +03002915static void string_registers_quirk(struct x86_emulate_ctxt *ctxt)
2916{
2917 /*
2918 * Intel CPUs mask the counter and pointers in quite strange
2919 * manner when ECX is zero due to REP-string optimizations.
2920 */
2921#ifdef CONFIG_X86_64
2922 if (ctxt->ad_bytes != 4 || !vendor_intel(ctxt))
2923 return;
2924
2925 *reg_write(ctxt, VCPU_REGS_RCX) = 0;
2926
2927 switch (ctxt->b) {
2928 case 0xa4: /* movsb */
2929 case 0xa5: /* movsd/w */
2930 *reg_rmw(ctxt, VCPU_REGS_RSI) &= (u32)-1;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002931 fallthrough;
Nadav Amit428e3d02015-04-28 13:06:01 +03002932 case 0xaa: /* stosb */
2933 case 0xab: /* stosd/w */
2934 *reg_rmw(ctxt, VCPU_REGS_RDI) &= (u32)-1;
2935 }
2936#endif
2937}
2938
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002939static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002940 struct tss_segment_16 *tss)
2941{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002942 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002943 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002944 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2945 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2946 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2947 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2948 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2949 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2950 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2951 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002952
Avi Kivity1aa36612011-04-27 13:20:30 +03002953 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2954 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2955 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2956 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2957 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002958}
2959
2960static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002961 struct tss_segment_16 *tss)
2962{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002963 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002964 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002965
Avi Kivity9dac77f2011-06-01 15:34:25 +03002966 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002967 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002968 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2969 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2970 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2971 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2972 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2973 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2974 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2975 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002976
2977 /*
2978 * SDM says that segment selectors are loaded before segment
2979 * descriptors
2980 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002981 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2982 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2983 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2984 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2985 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002986
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002987 cpl = tss->cs & 3;
2988
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002989 /*
Guo Chaofc058682012-06-28 15:19:51 +08002990 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002991 * it is handled in a context of new task
2992 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002993 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002994 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002995 if (ret != X86EMUL_CONTINUE)
2996 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002997 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002998 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002999 if (ret != X86EMUL_CONTINUE)
3000 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003001 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003002 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003003 if (ret != X86EMUL_CONTINUE)
3004 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003005 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003006 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003007 if (ret != X86EMUL_CONTINUE)
3008 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003009 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003010 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003011 if (ret != X86EMUL_CONTINUE)
3012 return ret;
3013
3014 return X86EMUL_CONTINUE;
3015}
3016
3017static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003018 u16 tss_selector, u16 old_tss_sel,
3019 ulong old_tss_base, struct desc_struct *new_desc)
3020{
3021 struct tss_segment_16 tss_seg;
3022 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003023 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003024
Jordan Borgner0e96f312018-10-28 12:58:28 +00003025 ret = linear_read_system(ctxt, old_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003026 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003027 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003028
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003029 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003030
Jordan Borgner0e96f312018-10-28 12:58:28 +00003031 ret = linear_write_system(ctxt, old_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003032 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003033 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003034
Jordan Borgner0e96f312018-10-28 12:58:28 +00003035 ret = linear_read_system(ctxt, new_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003036 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003037 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003038
3039 if (old_tss_sel != 0xffff) {
3040 tss_seg.prev_task_link = old_tss_sel;
3041
Paolo Bonzini79367a62018-06-06 16:43:02 +02003042 ret = linear_write_system(ctxt, new_tss_base,
3043 &tss_seg.prev_task_link,
Jordan Borgner0e96f312018-10-28 12:58:28 +00003044 sizeof(tss_seg.prev_task_link));
Avi Kivitydb297e32010-11-22 17:53:24 +02003045 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003046 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003047 }
3048
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003049 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003050}
3051
3052static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003053 struct tss_segment_32 *tss)
3054{
Nadav Amit5c7411e2014-04-07 18:37:47 +03003055 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003056 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003057 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003058 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
3059 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
3060 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
3061 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
3062 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
3063 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
3064 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
3065 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003066
Avi Kivity1aa36612011-04-27 13:20:30 +03003067 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
3068 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
3069 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
3070 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
3071 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
3072 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003073}
3074
3075static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003076 struct tss_segment_32 *tss)
3077{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003078 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003079 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003080
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003081 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003082 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003083 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003084 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01003085
3086 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03003087 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
3088 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
3089 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
3090 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
3091 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
3092 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
3093 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
3094 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003095
3096 /*
3097 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003098 * descriptors. This is important because CPL checks will
3099 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003100 */
Avi Kivity1aa36612011-04-27 13:20:30 +03003101 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
3102 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
3103 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
3104 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
3105 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
3106 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
3107 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003108
3109 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01003110 * If we're switching between Protected Mode and VM86, we need to make
3111 * sure to update the mode before loading the segment descriptors so
3112 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01003113 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003114 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01003115 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003116 cpl = 3;
3117 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01003118 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003119 cpl = tss->cs & 3;
3120 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01003121
3122 /*
Ingo Molnard9f6e122021-03-18 15:28:01 +01003123 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003124 * it is handled in a context of new task
3125 */
Nadav Amitd1442d82014-09-18 22:39:39 +03003126 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003127 cpl, X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003128 if (ret != X86EMUL_CONTINUE)
3129 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003130 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003131 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003132 if (ret != X86EMUL_CONTINUE)
3133 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003134 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003135 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003136 if (ret != X86EMUL_CONTINUE)
3137 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003138 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003139 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003140 if (ret != X86EMUL_CONTINUE)
3141 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003142 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003143 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003144 if (ret != X86EMUL_CONTINUE)
3145 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003146 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003147 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003148 if (ret != X86EMUL_CONTINUE)
3149 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003150 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003151 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003152
Eugene Korenevsky2f729b12015-03-29 01:27:17 +03003153 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003154}
3155
3156static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003157 u16 tss_selector, u16 old_tss_sel,
3158 ulong old_tss_base, struct desc_struct *new_desc)
3159{
3160 struct tss_segment_32 tss_seg;
3161 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003162 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03003163 u32 eip_offset = offsetof(struct tss_segment_32, eip);
3164 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003165
Jordan Borgner0e96f312018-10-28 12:58:28 +00003166 ret = linear_read_system(ctxt, old_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003167 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003168 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003169
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003170 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003171
Nadav Amit5c7411e2014-04-07 18:37:47 +03003172 /* Only GP registers and segment selectors are saved */
Paolo Bonzini79367a62018-06-06 16:43:02 +02003173 ret = linear_write_system(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
3174 ldt_sel_offset - eip_offset);
Avi Kivitydb297e32010-11-22 17:53:24 +02003175 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003176 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003177
Jordan Borgner0e96f312018-10-28 12:58:28 +00003178 ret = linear_read_system(ctxt, new_tss_base, &tss_seg, sizeof(tss_seg));
Avi Kivitydb297e32010-11-22 17:53:24 +02003179 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003180 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003181
3182 if (old_tss_sel != 0xffff) {
3183 tss_seg.prev_task_link = old_tss_sel;
3184
Paolo Bonzini79367a62018-06-06 16:43:02 +02003185 ret = linear_write_system(ctxt, new_tss_base,
3186 &tss_seg.prev_task_link,
Jordan Borgner0e96f312018-10-28 12:58:28 +00003187 sizeof(tss_seg.prev_task_link));
Avi Kivitydb297e32010-11-22 17:53:24 +02003188 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003189 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003190 }
3191
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003192 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003193}
3194
3195static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003196 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003197 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003198{
Mathias Krause0225fb52012-08-30 01:30:16 +02003199 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003200 struct desc_struct curr_tss_desc, next_tss_desc;
3201 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03003202 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003203 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03003204 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02003205 u32 desc_limit;
Nadav Amit3db176d2015-04-19 21:12:59 +03003206 ulong desc_addr, dr7;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003207
3208 /* FIXME: old_tss_base == ~0 ? */
3209
Avi Kivitye9194642012-06-13 16:29:39 +03003210 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003211 if (ret != X86EMUL_CONTINUE)
3212 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03003213 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003214 if (ret != X86EMUL_CONTINUE)
3215 return ret;
3216
3217 /* FIXME: check that next_tss_desc is tss */
3218
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003219 /*
3220 * Check privileges. The three cases are task switch caused by...
3221 *
3222 * 1. jmp/call/int to task gate: Check against DPL of the task gate
3223 * 2. Exception/IRQ/iret: No check is performed
Nadav Amit2c2ca2d2014-11-02 11:54:57 +02003224 * 3. jmp/call to TSS/task-gate: No check is performed since the
3225 * hardware checks it before exiting.
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003226 */
3227 if (reason == TASK_SWITCH_GATE) {
3228 if (idt_index != -1) {
3229 /* Software interrupts */
3230 struct desc_struct task_gate_desc;
3231 int dpl;
3232
3233 ret = read_interrupt_descriptor(ctxt, idt_index,
3234 &task_gate_desc);
3235 if (ret != X86EMUL_CONTINUE)
3236 return ret;
3237
3238 dpl = task_gate_desc.dpl;
3239 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
3240 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
3241 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003242 }
3243
Gleb Natapovceffb452010-03-18 15:20:19 +02003244 desc_limit = desc_limit_scaled(&next_tss_desc);
3245 if (!next_tss_desc.p ||
3246 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
3247 desc_limit < 0x2b)) {
Paolo Bonzini592f0852014-08-20 10:05:08 +02003248 return emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003249 }
3250
3251 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
3252 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003253 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003254 }
3255
3256 if (reason == TASK_SWITCH_IRET)
3257 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
3258
3259 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08003260 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003261 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
3262 old_tss_sel = 0xffff;
3263
3264 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003265 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003266 old_tss_base, &next_tss_desc);
3267 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003268 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003269 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02003270 if (ret != X86EMUL_CONTINUE)
3271 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003272
3273 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
3274 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
3275
3276 if (reason != TASK_SWITCH_IRET) {
3277 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003278 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003279 }
3280
Avi Kivity717746e2011-04-20 13:37:53 +03003281 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03003282 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003283
Jan Kiszkae269fb22010-04-14 15:51:09 +02003284 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003285 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
3286 ctxt->lock_prefix = 0;
3287 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003288 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02003289 }
3290
Nadav Amit3db176d2015-04-19 21:12:59 +03003291 ops->get_dr(ctxt, 7, &dr7);
3292 ops->set_dr(ctxt, 7, dr7 & ~(DR_LOCAL_ENABLE_MASK | DR_LOCAL_SLOWDOWN));
3293
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003294 return ret;
3295}
3296
3297int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003298 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003299 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003300{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003301 int rc;
3302
Avi Kivitydd856ef2012-08-27 23:46:17 +03003303 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003304 ctxt->_eip = ctxt->eip;
3305 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003306
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003307 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003308 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003309
Avi Kivitydd856ef2012-08-27 23:46:17 +03003310 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003311 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003312 writeback_registers(ctxt);
3313 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003314
Gleb Natapova0c0ab22011-03-28 16:57:49 +02003315 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003316}
3317
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03003318static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
3319 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02003320{
Nadav Amit0efb0442015-03-29 16:33:03 +03003321 int df = (ctxt->eflags & X86_EFLAGS_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02003322
Paolo Bonzini01485a22014-11-19 18:25:08 +01003323 register_address_increment(ctxt, reg, df * op->bytes);
3324 op->addr.mem.ea = register_address(ctxt, reg);
Gleb Natapova682e352010-03-18 15:20:21 +02003325}
3326
Avi Kivity7af04fc2010-08-18 14:16:35 +03003327static int em_das(struct x86_emulate_ctxt *ctxt)
3328{
Avi Kivity7af04fc2010-08-18 14:16:35 +03003329 u8 al, old_al;
3330 bool af, cf, old_cf;
3331
3332 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003333 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03003334
3335 old_al = al;
3336 old_cf = cf;
3337 cf = false;
3338 af = ctxt->eflags & X86_EFLAGS_AF;
3339 if ((al & 0x0f) > 9 || af) {
3340 al -= 6;
3341 cf = old_cf | (al >= 250);
3342 af = true;
3343 } else {
3344 af = false;
3345 }
3346 if (old_al > 0x99 || old_cf) {
3347 al -= 0x60;
3348 cf = true;
3349 }
3350
Avi Kivity9dac77f2011-06-01 15:34:25 +03003351 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03003352 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003353 ctxt->src.type = OP_IMM;
3354 ctxt->src.val = 0;
3355 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02003356 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03003357 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
3358 if (cf)
3359 ctxt->eflags |= X86_EFLAGS_CF;
3360 if (af)
3361 ctxt->eflags |= X86_EFLAGS_AF;
3362 return X86EMUL_CONTINUE;
3363}
3364
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003365static int em_aam(struct x86_emulate_ctxt *ctxt)
3366{
3367 u8 al, ah;
3368
3369 if (ctxt->src.val == 0)
3370 return emulate_de(ctxt);
3371
3372 al = ctxt->dst.val & 0xff;
3373 ah = al / ctxt->src.val;
3374 al %= ctxt->src.val;
3375
3376 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
3377
3378 /* Set PF, ZF, SF */
3379 ctxt->src.type = OP_IMM;
3380 ctxt->src.val = 0;
3381 ctxt->src.bytes = 1;
3382 fastop(ctxt, em_or);
3383
3384 return X86EMUL_CONTINUE;
3385}
3386
Gleb Natapov7f662272012-12-10 11:42:30 +02003387static int em_aad(struct x86_emulate_ctxt *ctxt)
3388{
3389 u8 al = ctxt->dst.val & 0xff;
3390 u8 ah = (ctxt->dst.val >> 8) & 0xff;
3391
3392 al = (al + (ah * ctxt->src.val)) & 0xff;
3393
3394 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
3395
Gleb Natapovf583c292013-02-13 17:50:39 +02003396 /* Set PF, ZF, SF */
3397 ctxt->src.type = OP_IMM;
3398 ctxt->src.val = 0;
3399 ctxt->src.bytes = 1;
3400 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02003401
3402 return X86EMUL_CONTINUE;
3403}
3404
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003405static int em_call(struct x86_emulate_ctxt *ctxt)
3406{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003407 int rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003408 long rel = ctxt->src.val;
3409
3410 ctxt->src.val = (unsigned long)ctxt->_eip;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003411 rc = jmp_rel(ctxt, rel);
3412 if (rc != X86EMUL_CONTINUE)
3413 return rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003414 return em_push(ctxt);
3415}
3416
Avi Kivity0ef753b2010-08-18 14:51:45 +03003417static int em_call_far(struct x86_emulate_ctxt *ctxt)
3418{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003419 u16 sel, old_cs;
3420 ulong old_eip;
3421 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03003422 struct desc_struct old_desc, new_desc;
3423 const struct x86_emulate_ops *ops = ctxt->ops;
3424 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amit82268082015-01-26 09:32:27 +02003425 enum x86emul_mode prev_mode = ctxt->mode;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003426
Avi Kivity9dac77f2011-06-01 15:34:25 +03003427 old_eip = ctxt->_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003428 ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003429
Avi Kivity9dac77f2011-06-01 15:34:25 +03003430 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003431 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
3432 X86_TRANSFER_CALL_JMP, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003433 if (rc != X86EMUL_CONTINUE)
Nadav Amit80976db2014-12-25 02:52:20 +02003434 return rc;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003435
Nadav Amitd50eaa12014-11-19 17:43:11 +02003436 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003437 if (rc != X86EMUL_CONTINUE)
3438 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003439
Avi Kivity9dac77f2011-06-01 15:34:25 +03003440 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003441 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003442 if (rc != X86EMUL_CONTINUE)
Nadav Amitd1442d82014-09-18 22:39:39 +03003443 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003444
Avi Kivity9dac77f2011-06-01 15:34:25 +03003445 ctxt->src.val = old_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003446 rc = em_push(ctxt);
3447 /* If we failed, we tainted the memory, but the very least we should
3448 restore cs */
Nadav Amit82268082015-01-26 09:32:27 +02003449 if (rc != X86EMUL_CONTINUE) {
3450 pr_warn_once("faulting far call emulation tainted memory\n");
Nadav Amitd1442d82014-09-18 22:39:39 +03003451 goto fail;
Nadav Amit82268082015-01-26 09:32:27 +02003452 }
Nadav Amitd1442d82014-09-18 22:39:39 +03003453 return rc;
3454fail:
3455 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
Nadav Amit82268082015-01-26 09:32:27 +02003456 ctxt->mode = prev_mode;
Nadav Amitd1442d82014-09-18 22:39:39 +03003457 return rc;
3458
Avi Kivity0ef753b2010-08-18 14:51:45 +03003459}
3460
Avi Kivity40ece7c2010-08-18 15:12:09 +03003461static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3462{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003463 int rc;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003464 unsigned long eip;
Avi Kivity40ece7c2010-08-18 15:12:09 +03003465
Nadav Amit234f3ce2014-09-18 22:39:38 +03003466 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
3467 if (rc != X86EMUL_CONTINUE)
3468 return rc;
3469 rc = assign_eip_near(ctxt, eip);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003470 if (rc != X86EMUL_CONTINUE)
3471 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003472 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003473 return X86EMUL_CONTINUE;
3474}
3475
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003476static int em_xchg(struct x86_emulate_ctxt *ctxt)
3477{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003478 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003479 ctxt->src.val = ctxt->dst.val;
3480 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003481
3482 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003483 ctxt->dst.val = ctxt->src.orig_val;
3484 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003485 return X86EMUL_CONTINUE;
3486}
3487
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003488static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3489{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003490 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003491 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003492}
3493
Avi Kivity61429142010-08-19 15:13:00 +03003494static int em_cwd(struct x86_emulate_ctxt *ctxt)
3495{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003496 ctxt->dst.type = OP_REG;
3497 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003498 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003499 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003500
3501 return X86EMUL_CONTINUE;
3502}
3503
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02003504static int em_rdpid(struct x86_emulate_ctxt *ctxt)
3505{
3506 u64 tsc_aux = 0;
3507
3508 if (ctxt->ops->get_msr(ctxt, MSR_TSC_AUX, &tsc_aux))
Robert Hooa9e2e0a2020-08-28 10:23:42 +08003509 return emulate_ud(ctxt);
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02003510 ctxt->dst.val = tsc_aux;
3511 return X86EMUL_CONTINUE;
3512}
3513
Avi Kivity48bb5d32010-08-18 18:54:34 +03003514static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3515{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003516 u64 tsc = 0;
3517
Avi Kivity717746e2011-04-20 13:37:53 +03003518 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003519 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3520 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003521 return X86EMUL_CONTINUE;
3522}
3523
Avi Kivity222d21a2011-11-10 14:57:30 +02003524static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3525{
3526 u64 pmc;
3527
Avi Kivitydd856ef2012-08-27 23:46:17 +03003528 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003529 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003530 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3531 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003532 return X86EMUL_CONTINUE;
3533}
3534
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003535static int em_mov(struct x86_emulate_ctxt *ctxt)
3536{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01003537 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003538 return X86EMUL_CONTINUE;
3539}
3540
Borislav Petkov84cffe42013-10-29 12:54:56 +01003541static int em_movbe(struct x86_emulate_ctxt *ctxt)
3542{
Borislav Petkov84cffe42013-10-29 12:54:56 +01003543 u16 tmp;
3544
Sean Christopherson5ae78e92019-12-17 13:32:38 -08003545 if (!ctxt->ops->guest_has_movbe(ctxt))
Borislav Petkov84cffe42013-10-29 12:54:56 +01003546 return emulate_ud(ctxt);
3547
3548 switch (ctxt->op_bytes) {
3549 case 2:
3550 /*
3551 * From MOVBE definition: "...When the operand size is 16 bits,
3552 * the upper word of the destination register remains unchanged
3553 * ..."
3554 *
3555 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3556 * rules so we have to do the operation almost per hand.
3557 */
3558 tmp = (u16)ctxt->src.val;
3559 ctxt->dst.val &= ~0xffffUL;
3560 ctxt->dst.val |= (unsigned long)swab16(tmp);
3561 break;
3562 case 4:
3563 ctxt->dst.val = swab32((u32)ctxt->src.val);
3564 break;
3565 case 8:
3566 ctxt->dst.val = swab64(ctxt->src.val);
3567 break;
3568 default:
Paolo Bonzini592f0852014-08-20 10:05:08 +02003569 BUG();
Borislav Petkov84cffe42013-10-29 12:54:56 +01003570 }
3571 return X86EMUL_CONTINUE;
3572}
3573
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003574static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3575{
3576 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3577 return emulate_gp(ctxt, 0);
3578
3579 /* Disable writeback. */
3580 ctxt->dst.type = OP_NONE;
3581 return X86EMUL_CONTINUE;
3582}
3583
3584static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3585{
3586 unsigned long val;
3587
3588 if (ctxt->mode == X86EMUL_MODE_PROT64)
3589 val = ctxt->src.val & ~0ULL;
3590 else
3591 val = ctxt->src.val & ~0U;
3592
3593 /* #UD condition is already handled. */
3594 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3595 return emulate_gp(ctxt, 0);
3596
3597 /* Disable writeback. */
3598 ctxt->dst.type = OP_NONE;
3599 return X86EMUL_CONTINUE;
3600}
3601
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003602static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3603{
Alexander Graf1ae09952020-09-25 16:34:16 +02003604 u64 msr_index = reg_read(ctxt, VCPU_REGS_RCX);
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003605 u64 msr_data;
Alexander Graf1ae09952020-09-25 16:34:16 +02003606 int r;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003607
Avi Kivitydd856ef2012-08-27 23:46:17 +03003608 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3609 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
Alexander Graf1ae09952020-09-25 16:34:16 +02003610 r = ctxt->ops->set_msr(ctxt, msr_index, msr_data);
3611
3612 if (r == X86EMUL_IO_NEEDED)
3613 return r;
3614
Maxim Levitsky7dffeca2020-10-01 14:29:52 +03003615 if (r > 0)
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003616 return emulate_gp(ctxt, 0);
3617
Maxim Levitsky7dffeca2020-10-01 14:29:52 +03003618 return r < 0 ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003619}
3620
3621static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3622{
Alexander Graf1ae09952020-09-25 16:34:16 +02003623 u64 msr_index = reg_read(ctxt, VCPU_REGS_RCX);
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003624 u64 msr_data;
Alexander Graf1ae09952020-09-25 16:34:16 +02003625 int r;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003626
Alexander Graf1ae09952020-09-25 16:34:16 +02003627 r = ctxt->ops->get_msr(ctxt, msr_index, &msr_data);
3628
3629 if (r == X86EMUL_IO_NEEDED)
3630 return r;
3631
3632 if (r)
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003633 return emulate_gp(ctxt, 0);
3634
Avi Kivitydd856ef2012-08-27 23:46:17 +03003635 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3636 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003637 return X86EMUL_CONTINUE;
3638}
3639
Paolo Bonzinidd307d02016-07-12 10:35:51 +02003640static int em_store_sreg(struct x86_emulate_ctxt *ctxt, int segment)
3641{
3642 if (segment > VCPU_SREG_GS &&
3643 (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_UMIP) &&
3644 ctxt->ops->cpl(ctxt) > 0)
3645 return emulate_gp(ctxt, 0);
3646
3647 ctxt->dst.val = get_segment_selector(ctxt, segment);
3648 if (ctxt->dst.bytes == 4 && ctxt->dst.type == OP_MEM)
3649 ctxt->dst.bytes = 2;
3650 return X86EMUL_CONTINUE;
3651}
3652
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003653static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3654{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003655 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003656 return emulate_ud(ctxt);
3657
Paolo Bonzinidd307d02016-07-12 10:35:51 +02003658 return em_store_sreg(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003659}
3660
3661static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3662{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003663 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003664
Avi Kivity9dac77f2011-06-01 15:34:25 +03003665 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003666 return emulate_ud(ctxt);
3667
Avi Kivity9dac77f2011-06-01 15:34:25 +03003668 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003669 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3670
3671 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003672 ctxt->dst.type = OP_NONE;
3673 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003674}
3675
Paolo Bonzinidd307d02016-07-12 10:35:51 +02003676static int em_sldt(struct x86_emulate_ctxt *ctxt)
3677{
3678 return em_store_sreg(ctxt, VCPU_SREG_LDTR);
3679}
3680
Avi Kivitya14e5792012-06-13 12:28:33 +03003681static int em_lldt(struct x86_emulate_ctxt *ctxt)
3682{
3683 u16 sel = ctxt->src.val;
3684
3685 /* Disable writeback. */
3686 ctxt->dst.type = OP_NONE;
3687 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3688}
3689
Paolo Bonzinidd307d02016-07-12 10:35:51 +02003690static int em_str(struct x86_emulate_ctxt *ctxt)
3691{
3692 return em_store_sreg(ctxt, VCPU_SREG_TR);
3693}
3694
Avi Kivity80890002012-06-13 16:33:29 +03003695static int em_ltr(struct x86_emulate_ctxt *ctxt)
3696{
3697 u16 sel = ctxt->src.val;
3698
3699 /* Disable writeback. */
3700 ctxt->dst.type = OP_NONE;
3701 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3702}
3703
Avi Kivity38503912011-03-31 18:48:09 +02003704static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3705{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003706 int rc;
3707 ulong linear;
3708
Avi Kivity9dac77f2011-06-01 15:34:25 +03003709 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003710 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003711 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003712 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003713 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003714 return X86EMUL_CONTINUE;
3715}
3716
Avi Kivity2d04a052011-04-20 15:32:49 +03003717static int em_clts(struct x86_emulate_ctxt *ctxt)
3718{
3719 ulong cr0;
3720
3721 cr0 = ctxt->ops->get_cr(ctxt, 0);
3722 cr0 &= ~X86_CR0_TS;
3723 ctxt->ops->set_cr(ctxt, 0, cr0);
3724 return X86EMUL_CONTINUE;
3725}
3726
Jan Kiszkab34a8052015-03-09 20:27:43 +01003727static int em_hypercall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003728{
Nadav Amit0f54a322014-08-29 11:26:55 +03003729 int rc = ctxt->ops->fix_hypercall(ctxt);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003730
Avi Kivity26d05cc2011-04-21 12:07:59 +03003731 if (rc != X86EMUL_CONTINUE)
3732 return rc;
3733
3734 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003735 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003736 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003737 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003738 return X86EMUL_CONTINUE;
3739}
3740
Avi Kivity96051572012-06-10 17:21:18 +03003741static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3742 void (*get)(struct x86_emulate_ctxt *ctxt,
3743 struct desc_ptr *ptr))
3744{
3745 struct desc_ptr desc_ptr;
3746
Paolo Bonziniae3e61e2016-07-12 10:36:41 +02003747 if ((ctxt->ops->get_cr(ctxt, 4) & X86_CR4_UMIP) &&
3748 ctxt->ops->cpl(ctxt) > 0)
3749 return emulate_gp(ctxt, 0);
3750
Avi Kivity96051572012-06-10 17:21:18 +03003751 if (ctxt->mode == X86EMUL_MODE_PROT64)
3752 ctxt->op_bytes = 8;
3753 get(ctxt, &desc_ptr);
3754 if (ctxt->op_bytes == 2) {
3755 ctxt->op_bytes = 4;
3756 desc_ptr.address &= 0x00ffffff;
3757 }
3758 /* Disable writeback. */
3759 ctxt->dst.type = OP_NONE;
Steve Rutherford129a72a2017-01-11 18:28:29 -08003760 return segmented_write_std(ctxt, ctxt->dst.addr.mem,
3761 &desc_ptr, 2 + ctxt->op_bytes);
Avi Kivity96051572012-06-10 17:21:18 +03003762}
3763
3764static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3765{
3766 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3767}
3768
3769static int em_sidt(struct x86_emulate_ctxt *ctxt)
3770{
3771 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3772}
3773
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003774static int em_lgdt_lidt(struct x86_emulate_ctxt *ctxt, bool lgdt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003775{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003776 struct desc_ptr desc_ptr;
3777 int rc;
3778
Avi Kivity510425f2012-06-07 17:04:36 +03003779 if (ctxt->mode == X86EMUL_MODE_PROT64)
3780 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003781 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003782 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003783 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003784 if (rc != X86EMUL_CONTINUE)
3785 return rc;
Nadav Amit9a9abf62014-11-02 11:54:56 +02003786 if (ctxt->mode == X86EMUL_MODE_PROT64 &&
Yu Zhangfd8cb432017-08-24 20:27:56 +08003787 emul_is_noncanonical_address(desc_ptr.address, ctxt))
Nadav Amit9a9abf62014-11-02 11:54:56 +02003788 return emulate_gp(ctxt, 0);
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003789 if (lgdt)
3790 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3791 else
3792 ctxt->ops->set_idt(ctxt, &desc_ptr);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003793 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003794 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003795 return X86EMUL_CONTINUE;
3796}
3797
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003798static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3799{
3800 return em_lgdt_lidt(ctxt, true);
3801}
3802
Avi Kivity26d05cc2011-04-21 12:07:59 +03003803static int em_lidt(struct x86_emulate_ctxt *ctxt)
3804{
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003805 return em_lgdt_lidt(ctxt, false);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003806}
3807
3808static int em_smsw(struct x86_emulate_ctxt *ctxt)
3809{
Paolo Bonziniae3e61e2016-07-12 10:36:41 +02003810 if ((ctxt->ops->get_cr(ctxt, 4) & X86_CR4_UMIP) &&
3811 ctxt->ops->cpl(ctxt) > 0)
3812 return emulate_gp(ctxt, 0);
3813
Nadav Amit32e94d02014-06-02 18:34:11 +03003814 if (ctxt->dst.type == OP_MEM)
3815 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003816 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003817 return X86EMUL_CONTINUE;
3818}
3819
3820static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3821{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003822 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003823 | (ctxt->src.val & 0x0f));
3824 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003825 return X86EMUL_CONTINUE;
3826}
3827
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003828static int em_loop(struct x86_emulate_ctxt *ctxt)
3829{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003830 int rc = X86EMUL_CONTINUE;
3831
Paolo Bonzini01485a22014-11-19 18:25:08 +01003832 register_address_increment(ctxt, VCPU_REGS_RCX, -1);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003833 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003834 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
Nadav Amit234f3ce2014-09-18 22:39:38 +03003835 rc = jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003836
Nadav Amit234f3ce2014-09-18 22:39:38 +03003837 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003838}
3839
3840static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3841{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003842 int rc = X86EMUL_CONTINUE;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003843
Nadav Amit234f3ce2014-09-18 22:39:38 +03003844 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
3845 rc = jmp_rel(ctxt, ctxt->src.val);
3846
3847 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003848}
3849
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003850static int em_in(struct x86_emulate_ctxt *ctxt)
3851{
3852 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3853 &ctxt->dst.val))
3854 return X86EMUL_IO_NEEDED;
3855
3856 return X86EMUL_CONTINUE;
3857}
3858
3859static int em_out(struct x86_emulate_ctxt *ctxt)
3860{
3861 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3862 &ctxt->src.val, 1);
3863 /* Disable writeback. */
3864 ctxt->dst.type = OP_NONE;
3865 return X86EMUL_CONTINUE;
3866}
3867
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003868static int em_cli(struct x86_emulate_ctxt *ctxt)
3869{
3870 if (emulator_bad_iopl(ctxt))
3871 return emulate_gp(ctxt, 0);
3872
3873 ctxt->eflags &= ~X86_EFLAGS_IF;
3874 return X86EMUL_CONTINUE;
3875}
3876
3877static int em_sti(struct x86_emulate_ctxt *ctxt)
3878{
3879 if (emulator_bad_iopl(ctxt))
3880 return emulate_gp(ctxt, 0);
3881
3882 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3883 ctxt->eflags |= X86_EFLAGS_IF;
3884 return X86EMUL_CONTINUE;
3885}
3886
Avi Kivity6d6eede2012-06-07 14:11:36 +03003887static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3888{
3889 u32 eax, ebx, ecx, edx;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003890 u64 msr = 0;
3891
3892 ctxt->ops->get_msr(ctxt, MSR_MISC_FEATURES_ENABLES, &msr);
3893 if (msr & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
3894 ctxt->ops->cpl(ctxt)) {
3895 return emulate_gp(ctxt, 0);
3896 }
Avi Kivity6d6eede2012-06-07 14:11:36 +03003897
Avi Kivitydd856ef2012-08-27 23:46:17 +03003898 eax = reg_read(ctxt, VCPU_REGS_RAX);
3899 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Sean Christophersonf91af512020-03-04 17:34:37 -08003900 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, false);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003901 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3902 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3903 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3904 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003905 return X86EMUL_CONTINUE;
3906}
3907
Paolo Bonzini98f73632013-10-31 11:19:42 +01003908static int em_sahf(struct x86_emulate_ctxt *ctxt)
3909{
3910 u32 flags;
3911
Nadav Amit0efb0442015-03-29 16:33:03 +03003912 flags = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
3913 X86_EFLAGS_SF;
Paolo Bonzini98f73632013-10-31 11:19:42 +01003914 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3915
3916 ctxt->eflags &= ~0xffUL;
3917 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3918 return X86EMUL_CONTINUE;
3919}
3920
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003921static int em_lahf(struct x86_emulate_ctxt *ctxt)
3922{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003923 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3924 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003925 return X86EMUL_CONTINUE;
3926}
3927
Avi Kivity92998362012-06-13 12:25:06 +03003928static int em_bswap(struct x86_emulate_ctxt *ctxt)
3929{
3930 switch (ctxt->op_bytes) {
3931#ifdef CONFIG_X86_64
3932 case 8:
3933 asm("bswap %0" : "+r"(ctxt->dst.val));
3934 break;
3935#endif
3936 default:
3937 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3938 break;
3939 }
3940 return X86EMUL_CONTINUE;
3941}
3942
Nadav Amit13e457e2014-10-13 13:04:13 +03003943static int em_clflush(struct x86_emulate_ctxt *ctxt)
3944{
3945 /* emulating clflush regardless of cpuid */
3946 return X86EMUL_CONTINUE;
3947}
3948
David Edmondson51b958e2020-11-03 12:04:00 +00003949static int em_clflushopt(struct x86_emulate_ctxt *ctxt)
3950{
3951 /* emulating clflushopt regardless of cpuid */
3952 return X86EMUL_CONTINUE;
3953}
3954
Nadav Amit2276b512015-01-26 09:32:24 +02003955static int em_movsxd(struct x86_emulate_ctxt *ctxt)
3956{
3957 ctxt->dst.val = (s32) ctxt->src.val;
3958 return X86EMUL_CONTINUE;
3959}
3960
Radim Krčmář283c95d2016-11-09 19:07:06 +01003961static int check_fxsr(struct x86_emulate_ctxt *ctxt)
3962{
Sean Christopherson5ae78e92019-12-17 13:32:38 -08003963 if (!ctxt->ops->guest_has_fxsr(ctxt))
Radim Krčmář283c95d2016-11-09 19:07:06 +01003964 return emulate_ud(ctxt);
3965
3966 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
3967 return emulate_nm(ctxt);
3968
3969 /*
3970 * Don't emulate a case that should never be hit, instead of working
3971 * around a lack of fxsave64/fxrstor64 on old compilers.
3972 */
3973 if (ctxt->mode >= X86EMUL_MODE_PROT64)
3974 return X86EMUL_UNHANDLEABLE;
3975
3976 return X86EMUL_CONTINUE;
3977}
3978
3979/*
Nick Desaulniers9d643f62017-05-30 20:08:38 -07003980 * Hardware doesn't save and restore XMM 0-7 without CR4.OSFXSR, but does save
3981 * and restore MXCSR.
3982 */
3983static size_t __fxstate_size(int nregs)
3984{
3985 return offsetof(struct fxregs_state, xmm_space[0]) + nregs * 16;
3986}
3987
3988static inline size_t fxstate_size(struct x86_emulate_ctxt *ctxt)
3989{
3990 bool cr4_osfxsr;
3991 if (ctxt->mode == X86EMUL_MODE_PROT64)
3992 return __fxstate_size(16);
3993
3994 cr4_osfxsr = ctxt->ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR;
3995 return __fxstate_size(cr4_osfxsr ? 8 : 0);
3996}
3997
3998/*
Radim Krčmář283c95d2016-11-09 19:07:06 +01003999 * FXSAVE and FXRSTOR have 4 different formats depending on execution mode,
4000 * 1) 16 bit mode
4001 * 2) 32 bit mode
4002 * - like (1), but FIP and FDP (foo) are only 16 bit. At least Intel CPUs
4003 * preserve whole 32 bit values, though, so (1) and (2) are the same wrt.
4004 * save and restore
4005 * 3) 64-bit mode with REX.W prefix
4006 * - like (2), but XMM 8-15 are being saved and restored
4007 * 4) 64-bit mode without REX.W prefix
4008 * - like (3), but FIP and FDP are 64 bit
4009 *
4010 * Emulation uses (3) for (1) and (2) and preserves XMM 8-15 to reach the
4011 * desired result. (4) is not emulated.
4012 *
4013 * Note: Guest and host CPUID.(EAX=07H,ECX=0H):EBX[bit 13] (deprecate FPU CS
4014 * and FPU DS) should match.
4015 */
4016static int em_fxsave(struct x86_emulate_ctxt *ctxt)
4017{
4018 struct fxregs_state fx_state;
Radim Krčmář283c95d2016-11-09 19:07:06 +01004019 int rc;
4020
4021 rc = check_fxsr(ctxt);
4022 if (rc != X86EMUL_CONTINUE)
4023 return rc;
4024
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02004025 kvm_fpu_get();
Sean Christophersona7baead2020-01-17 11:30:50 -08004026
Radim Krčmář283c95d2016-11-09 19:07:06 +01004027 rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_state));
4028
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02004029 kvm_fpu_put();
Sean Christophersona7baead2020-01-17 11:30:50 -08004030
Radim Krčmář283c95d2016-11-09 19:07:06 +01004031 if (rc != X86EMUL_CONTINUE)
4032 return rc;
4033
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004034 return segmented_write_std(ctxt, ctxt->memop.addr.mem, &fx_state,
4035 fxstate_size(ctxt));
Radim Krčmář283c95d2016-11-09 19:07:06 +01004036}
4037
David Hildenbrand4d772cb2017-11-07 18:04:05 +01004038/*
4039 * FXRSTOR might restore XMM registers not provided by the guest. Fill
4040 * in the host registers (via FXSAVE) instead, so they won't be modified.
4041 * (preemption has to stay disabled until FXRSTOR).
4042 *
4043 * Use noinline to keep the stack for other functions called by callers small.
4044 */
4045static noinline int fxregs_fixup(struct fxregs_state *fx_state,
4046 const size_t used_size)
4047{
4048 struct fxregs_state fx_tmp;
4049 int rc;
4050
4051 rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_tmp));
4052 memcpy((void *)fx_state + used_size, (void *)&fx_tmp + used_size,
4053 __fxstate_size(16) - used_size);
4054
4055 return rc;
4056}
4057
Radim Krčmář283c95d2016-11-09 19:07:06 +01004058static int em_fxrstor(struct x86_emulate_ctxt *ctxt)
4059{
4060 struct fxregs_state fx_state;
4061 int rc;
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004062 size_t size;
Radim Krčmář283c95d2016-11-09 19:07:06 +01004063
4064 rc = check_fxsr(ctxt);
4065 if (rc != X86EMUL_CONTINUE)
4066 return rc;
4067
David Hildenbrand4d772cb2017-11-07 18:04:05 +01004068 size = fxstate_size(ctxt);
4069 rc = segmented_read_std(ctxt, ctxt->memop.addr.mem, &fx_state, size);
4070 if (rc != X86EMUL_CONTINUE)
4071 return rc;
4072
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02004073 kvm_fpu_get();
Sean Christophersona7baead2020-01-17 11:30:50 -08004074
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004075 if (size < __fxstate_size(16)) {
David Hildenbrand4d772cb2017-11-07 18:04:05 +01004076 rc = fxregs_fixup(&fx_state, size);
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004077 if (rc != X86EMUL_CONTINUE)
4078 goto out;
4079 }
4080
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004081 if (fx_state.mxcsr >> 16) {
4082 rc = emulate_gp(ctxt, 0);
4083 goto out;
4084 }
Radim Krčmář283c95d2016-11-09 19:07:06 +01004085
4086 if (rc == X86EMUL_CONTINUE)
4087 rc = asm_safe("fxrstor %[fx]", : [fx] "m"(fx_state));
4088
Nick Desaulniers9d643f62017-05-30 20:08:38 -07004089out:
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02004090 kvm_fpu_put();
Sean Christophersona7baead2020-01-17 11:30:50 -08004091
Radim Krčmář283c95d2016-11-09 19:07:06 +01004092 return rc;
4093}
4094
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02004095static int em_xsetbv(struct x86_emulate_ctxt *ctxt)
4096{
4097 u32 eax, ecx, edx;
4098
4099 eax = reg_read(ctxt, VCPU_REGS_RAX);
4100 edx = reg_read(ctxt, VCPU_REGS_RDX);
4101 ecx = reg_read(ctxt, VCPU_REGS_RCX);
4102
4103 if (ctxt->ops->set_xcr(ctxt, ecx, ((u64)edx << 32) | eax))
4104 return emulate_gp(ctxt, 0);
4105
4106 return X86EMUL_CONTINUE;
4107}
4108
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004109static bool valid_cr(int nr)
4110{
4111 switch (nr) {
4112 case 0:
4113 case 2 ... 4:
4114 case 8:
4115 return true;
4116 default:
4117 return false;
4118 }
4119}
4120
Sean Christophersond0fe7b62021-04-21 19:21:20 -07004121static int check_cr_access(struct x86_emulate_ctxt *ctxt)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004122{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004123 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004124 return emulate_ud(ctxt);
4125
4126 return X86EMUL_CONTINUE;
4127}
4128
Joerg Roedel3b88e412011-04-04 12:39:29 +02004129static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
4130{
4131 unsigned long dr7;
4132
Avi Kivity717746e2011-04-20 13:37:53 +03004133 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02004134
4135 /* Check if DR7.Global_Enable is set */
4136 return dr7 & (1 << 13);
4137}
4138
4139static int check_dr_read(struct x86_emulate_ctxt *ctxt)
4140{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004141 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02004142 u64 cr4;
4143
4144 if (dr > 7)
4145 return emulate_ud(ctxt);
4146
Avi Kivity717746e2011-04-20 13:37:53 +03004147 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02004148 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
4149 return emulate_ud(ctxt);
4150
Nadav Amit6d2a0522014-11-02 11:54:43 +02004151 if (check_dr7_gd(ctxt)) {
4152 ulong dr6;
4153
4154 ctxt->ops->get_dr(ctxt, 6, &dr6);
Liran Alon1fc5d1942019-06-06 01:54:47 +03004155 dr6 &= ~DR_TRAP_BITS;
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08004156 dr6 |= DR6_BD | DR6_ACTIVE_LOW;
Nadav Amit6d2a0522014-11-02 11:54:43 +02004157 ctxt->ops->set_dr(ctxt, 6, dr6);
Joerg Roedel3b88e412011-04-04 12:39:29 +02004158 return emulate_db(ctxt);
Nadav Amit6d2a0522014-11-02 11:54:43 +02004159 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02004160
4161 return X86EMUL_CONTINUE;
4162}
4163
4164static int check_dr_write(struct x86_emulate_ctxt *ctxt)
4165{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004166 u64 new_val = ctxt->src.val64;
4167 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02004168
4169 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
4170 return emulate_gp(ctxt, 0);
4171
4172 return check_dr_read(ctxt);
4173}
4174
Joerg Roedel01de8b02011-04-04 12:39:31 +02004175static int check_svme(struct x86_emulate_ctxt *ctxt)
4176{
Radim Krčmář92ceb762017-05-18 19:37:32 +02004177 u64 efer = 0;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004178
Avi Kivity717746e2011-04-20 13:37:53 +03004179 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02004180
4181 if (!(efer & EFER_SVME))
4182 return emulate_ud(ctxt);
4183
4184 return X86EMUL_CONTINUE;
4185}
4186
4187static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
4188{
Avi Kivitydd856ef2012-08-27 23:46:17 +03004189 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02004190
4191 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07004192 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02004193 return emulate_gp(ctxt, 0);
4194
4195 return check_svme(ctxt);
4196}
4197
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004198static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
4199{
Avi Kivity717746e2011-04-20 13:37:53 +03004200 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004201
Avi Kivity717746e2011-04-20 13:37:53 +03004202 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004203 return emulate_ud(ctxt);
4204
4205 return X86EMUL_CONTINUE;
4206}
4207
Joerg Roedel80612522011-04-04 12:39:33 +02004208static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
4209{
Avi Kivity717746e2011-04-20 13:37:53 +03004210 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03004211 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02004212
Arbel Moshe2d7921c2018-03-12 13:12:53 +02004213 /*
4214 * VMware allows access to these Pseduo-PMCs even when read via RDPMC
4215 * in Ring3 when CR4.PCE=0.
4216 */
4217 if (enable_vmware_backdoor && is_vmware_backdoor_pmc(rcx))
4218 return X86EMUL_CONTINUE;
4219
Avi Kivity717746e2011-04-20 13:37:53 +03004220 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03004221 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02004222 return emulate_gp(ctxt, 0);
4223
4224 return X86EMUL_CONTINUE;
4225}
4226
Joerg Roedelf6511932011-04-04 12:39:35 +02004227static int check_perm_in(struct x86_emulate_ctxt *ctxt)
4228{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004229 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
4230 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02004231 return emulate_gp(ctxt, 0);
4232
4233 return X86EMUL_CONTINUE;
4234}
4235
4236static int check_perm_out(struct x86_emulate_ctxt *ctxt)
4237{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004238 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
4239 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02004240 return emulate_gp(ctxt, 0);
4241
4242 return X86EMUL_CONTINUE;
4243}
4244
Avi Kivity73fba5f2010-07-29 15:11:53 +03004245#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004246#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
4247#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
4248 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03004249#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02004250#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004251#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
4252#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Nadav Amit39f062f2014-11-26 15:47:18 +02004253#define ID(_f, _i) { .flags = ((_f) | InstrDual | ModRM), .u.idual = (_i) }
Nadav Amit2276b512015-01-26 09:32:24 +02004254#define MD(_f, _m) { .flags = ((_f) | ModeDual), .u.mdual = (_m) }
Gleb Natapov045a2822012-12-20 16:57:43 +02004255#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03004256#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02004257#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004258#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004259 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004260#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004261 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
4262 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02004263#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03004264
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004265#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02004266#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004267#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02004268#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004269#define I2bvIP(_f, _e, _i, _p) \
4270 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004271
Avi Kivityfb864fb2013-01-04 16:18:54 +02004272#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
4273 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
4274 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03004275
Nadav Amit0f54a322014-08-29 11:26:55 +03004276static const struct opcode group7_rm0[] = {
4277 N,
Jan Kiszkab34a8052015-03-09 20:27:43 +01004278 I(SrcNone | Priv | EmulateOnUD, em_hypercall),
Nadav Amit0f54a322014-08-29 11:26:55 +03004279 N, N, N, N, N, N,
4280};
4281
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004282static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004283 DI(SrcNone | Priv, monitor),
4284 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004285 N, N, N, N, N, N,
4286};
4287
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02004288static const struct opcode group7_rm2[] = {
4289 N,
4290 II(ImplicitOps | Priv, em_xsetbv, xsetbv),
4291 N, N, N, N, N, N,
4292};
4293
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004294static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004295 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Jan Kiszkab34a8052015-03-09 20:27:43 +01004296 II(SrcNone | Prot | EmulateOnUD, em_hypercall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004297 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
4298 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
4299 DIP(SrcNone | Prot | Priv, stgi, check_svme),
4300 DIP(SrcNone | Prot | Priv, clgi, check_svme),
4301 DIP(SrcNone | Prot | Priv, skinit, check_svme),
4302 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004303};
Avi Kivity6230f7f2010-08-26 18:34:55 +03004304
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004305static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004306 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004307 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004308 N, N, N, N, N, N,
4309};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004310
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004311static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02004312 F(Lock, em_add),
4313 F(Lock | PageTable, em_or),
4314 F(Lock, em_adc),
4315 F(Lock, em_sbb),
4316 F(Lock | PageTable, em_and),
4317 F(Lock, em_sub),
4318 F(Lock, em_xor),
4319 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004320};
4321
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004322static const struct opcode group1A[] = {
Tom Lendacky0f89b202016-12-14 14:59:23 -05004323 I(DstMem | SrcNone | Mov | Stack | IncSP | TwoMemOp, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004324};
4325
Avi Kivity007a3b52013-01-19 19:51:51 +02004326static const struct opcode group2[] = {
4327 F(DstMem | ModRM, em_rol),
4328 F(DstMem | ModRM, em_ror),
4329 F(DstMem | ModRM, em_rcl),
4330 F(DstMem | ModRM, em_rcr),
4331 F(DstMem | ModRM, em_shl),
4332 F(DstMem | ModRM, em_shr),
4333 F(DstMem | ModRM, em_shl),
4334 F(DstMem | ModRM, em_sar),
4335};
4336
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004337static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02004338 F(DstMem | SrcImm | NoWrite, em_test),
4339 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02004340 F(DstMem | SrcNone | Lock, em_not),
4341 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02004342 F(DstXacc | Src2Mem, em_mul_ex),
4343 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004344 F(DstXacc | Src2Mem, em_div_ex),
4345 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004346};
4347
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004348static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02004349 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
4350 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004351 N, N, N, N, N, N,
4352};
4353
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004354static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02004355 F(DstMem | SrcNone | Lock, em_inc),
4356 F(DstMem | SrcNone | Lock, em_dec),
Nadav Amit58b70752014-10-24 11:35:09 +03004357 I(SrcMem | NearBranch, em_call_near_abs),
Nadav Amitacac6f892015-05-03 20:22:57 +03004358 I(SrcMemFAddr | ImplicitOps, em_call_far),
Nadav Amit58b70752014-10-24 11:35:09 +03004359 I(SrcMem | NearBranch, em_jmp_abs),
Nadav Amitf7784042014-09-18 22:39:41 +03004360 I(SrcMemFAddr | ImplicitOps, em_jmp_far),
Tom Lendacky0f89b202016-12-14 14:59:23 -05004361 I(SrcMem | Stack | TwoMemOp, em_push), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004362};
4363
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004364static const struct opcode group6[] = {
Paolo Bonzinidd307d02016-07-12 10:35:51 +02004365 II(Prot | DstMem, em_sldt, sldt),
4366 II(Prot | DstMem, em_str, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03004367 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03004368 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004369 N, N, N, N,
4370};
4371
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004372static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03004373 II(Mov | DstMem, em_sgdt, sgdt),
4374 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004375 II(SrcMem | Priv, em_lgdt, lgdt),
4376 II(SrcMem | Priv, em_lidt, lidt),
4377 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
4378 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
4379 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004380}, {
Nadav Amit0f54a322014-08-29 11:26:55 +03004381 EXT(0, group7_rm0),
Avi Kivity5ef39c72011-04-21 12:21:50 +03004382 EXT(0, group7_rm1),
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02004383 EXT(0, group7_rm2),
4384 EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004385 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
4386 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
4387 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004388} };
4389
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004390static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004391 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02004392 F(DstMem | SrcImmByte | NoWrite, em_bt),
4393 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
4394 F(DstMem | SrcImmByte | Lock, em_btr),
4395 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004396};
4397
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02004398/*
4399 * The "memory" destination is actually always a register, since we come
4400 * from the register case of group9.
4401 */
4402static const struct gprefix pfx_0f_c7_7 = {
Sean Christopherson2183de42021-05-04 10:17:23 -07004403 N, N, N, II(DstMem | ModRM | Op3264 | EmulateOnUD, em_rdpid, rdpid),
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02004404};
4405
4406
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004407static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004408 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004409}, {
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02004410 N, N, N, N, N, N, N,
4411 GP(0, &pfx_0f_c7_7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004412} };
4413
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004414static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004415 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004416 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004417};
4418
Nadav Amit13e457e2014-10-13 13:04:13 +03004419static const struct gprefix pfx_0f_ae_7 = {
David Edmondson51b958e2020-11-03 12:04:00 +00004420 I(SrcMem | ByteOp, em_clflush), I(SrcMem | ByteOp, em_clflushopt), N, N,
Nadav Amit13e457e2014-10-13 13:04:13 +03004421};
4422
4423static const struct group_dual group15 = { {
Radim Krčmář283c95d2016-11-09 19:07:06 +01004424 I(ModRM | Aligned16, em_fxsave),
4425 I(ModRM | Aligned16, em_fxrstor),
4426 N, N, N, N, N, GP(0, &pfx_0f_ae_7),
Nadav Amit13e457e2014-10-13 13:04:13 +03004427}, {
4428 N, N, N, N, N, N, N, N,
4429} };
4430
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004431static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03004432 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02004433};
4434
Nadav Amit39f062f2014-11-26 15:47:18 +02004435static const struct instr_dual instr_dual_0f_2b = {
4436 I(0, em_mov), N
4437};
4438
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004439static const struct gprefix pfx_0f_2b = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004440 ID(0, &instr_dual_0f_2b), ID(0, &instr_dual_0f_2b), N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03004441};
4442
Stefan Fritsch29916962018-04-01 17:54:44 +02004443static const struct gprefix pfx_0f_10_0f_11 = {
4444 I(Unaligned, em_mov), I(Unaligned, em_mov), N, N,
4445};
4446
Igor Mammedov27ce8252014-03-15 21:01:59 +01004447static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01004448 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004449};
4450
Alex Williamson0a370272014-07-11 11:56:31 -06004451static const struct gprefix pfx_0f_e7 = {
4452 N, I(Sse, em_mov), N, N,
4453};
4454
Gleb Natapov045a2822012-12-20 16:57:43 +02004455static const struct escape escape_d9 = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02004456 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstcw),
Gleb Natapov045a2822012-12-20 16:57:43 +02004457}, {
4458 /* 0xC0 - 0xC7 */
4459 N, N, N, N, N, N, N, N,
4460 /* 0xC8 - 0xCF */
4461 N, N, N, N, N, N, N, N,
4462 /* 0xD0 - 0xC7 */
4463 N, N, N, N, N, N, N, N,
4464 /* 0xD8 - 0xDF */
4465 N, N, N, N, N, N, N, N,
4466 /* 0xE0 - 0xE7 */
4467 N, N, N, N, N, N, N, N,
4468 /* 0xE8 - 0xEF */
4469 N, N, N, N, N, N, N, N,
4470 /* 0xF0 - 0xF7 */
4471 N, N, N, N, N, N, N, N,
4472 /* 0xF8 - 0xFF */
4473 N, N, N, N, N, N, N, N,
4474} };
4475
4476static const struct escape escape_db = { {
4477 N, N, N, N, N, N, N, N,
4478}, {
4479 /* 0xC0 - 0xC7 */
4480 N, N, N, N, N, N, N, N,
4481 /* 0xC8 - 0xCF */
4482 N, N, N, N, N, N, N, N,
4483 /* 0xD0 - 0xC7 */
4484 N, N, N, N, N, N, N, N,
4485 /* 0xD8 - 0xDF */
4486 N, N, N, N, N, N, N, N,
4487 /* 0xE0 - 0xE7 */
4488 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
4489 /* 0xE8 - 0xEF */
4490 N, N, N, N, N, N, N, N,
4491 /* 0xF0 - 0xF7 */
4492 N, N, N, N, N, N, N, N,
4493 /* 0xF8 - 0xFF */
4494 N, N, N, N, N, N, N, N,
4495} };
4496
4497static const struct escape escape_dd = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02004498 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstsw),
Gleb Natapov045a2822012-12-20 16:57:43 +02004499}, {
4500 /* 0xC0 - 0xC7 */
4501 N, N, N, N, N, N, N, N,
4502 /* 0xC8 - 0xCF */
4503 N, N, N, N, N, N, N, N,
4504 /* 0xD0 - 0xC7 */
4505 N, N, N, N, N, N, N, N,
4506 /* 0xD8 - 0xDF */
4507 N, N, N, N, N, N, N, N,
4508 /* 0xE0 - 0xE7 */
4509 N, N, N, N, N, N, N, N,
4510 /* 0xE8 - 0xEF */
4511 N, N, N, N, N, N, N, N,
4512 /* 0xF0 - 0xF7 */
4513 N, N, N, N, N, N, N, N,
4514 /* 0xF8 - 0xFF */
4515 N, N, N, N, N, N, N, N,
4516} };
4517
Nadav Amit39f062f2014-11-26 15:47:18 +02004518static const struct instr_dual instr_dual_0f_c3 = {
4519 I(DstMem | SrcReg | ModRM | No16 | Mov, em_mov), N
4520};
4521
Nadav Amit2276b512015-01-26 09:32:24 +02004522static const struct mode_dual mode_dual_63 = {
4523 N, I(DstReg | SrcMem32 | ModRM | Mov, em_movsxd)
4524};
4525
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004526static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004527 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004528 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004529 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
4530 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004531 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004532 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004533 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
4534 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004535 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004536 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004537 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
4538 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004539 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004540 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004541 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
4542 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004543 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004544 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004545 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004546 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004547 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004548 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004549 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004550 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004551 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02004552 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004553 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03004554 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004555 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09004556 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004557 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09004558 I(ImplicitOps | Stack | No64, em_pusha),
4559 I(ImplicitOps | Stack | No64, em_popa),
Nadav Amit2276b512015-01-26 09:32:24 +02004560 N, MD(ModRM, &mode_dual_63),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004561 N, N, N, N,
4562 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03004563 I(SrcImm | Mov | Stack, em_push),
4564 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03004565 I(SrcImmByte | Mov | Stack, em_push),
4566 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004567 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09004568 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03004569 /* 0x70 - 0x7F */
Nadav Amit58b70752014-10-24 11:35:09 +03004570 X16(D(SrcImmByte | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004571 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004572 G(ByteOp | DstMem | SrcImm, group1),
4573 G(DstMem | SrcImm, group1),
4574 G(ByteOp | DstMem | SrcImm | No64, group1),
4575 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02004576 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004577 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004578 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004579 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004580 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004581 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09004582 D(ModRM | SrcMem | NoAccess | DstReg),
4583 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
4584 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004585 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004586 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004587 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03004588 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08004589 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09004590 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01004591 II(ImplicitOps | Stack, em_popf, popf),
4592 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004593 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004594 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004595 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Tom Lendacky0f89b202016-12-14 14:59:23 -05004596 I2bv(SrcSI | DstDI | Mov | String | TwoMemOp, em_mov),
4597 F2bv(SrcSI | DstDI | String | NoWrite | TwoMemOp, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004598 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004599 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004600 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
4601 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004602 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004603 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004604 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004605 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02004606 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004607 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004608 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Nadav Amit58b70752014-10-24 11:35:09 +03004609 I(ImplicitOps | NearBranch | SrcImmU16, em_ret_near_imm),
4610 I(ImplicitOps | NearBranch, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03004611 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
4612 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004613 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004614 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03004615 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Nadav Amit16794aa2015-01-26 09:32:22 +02004616 I(ImplicitOps | SrcImmU16, em_ret_far_imm),
4617 I(ImplicitOps, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02004618 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004619 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004620 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004621 G(Src2One | ByteOp, group2), G(Src2One, group2),
4622 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02004623 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02004624 I(DstAcc | SrcImmUByte | No64, em_aad),
4625 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004626 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004627 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02004628 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004629 /* 0xE0 - 0xE7 */
Nadav Amit58b70752014-10-24 11:35:09 +03004630 X3(I(SrcImmByte | NearBranch, em_loop)),
4631 I(SrcImmByte | NearBranch, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004632 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
4633 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004634 /* 0xE8 - 0xEF */
Nadav Amit58b70752014-10-24 11:35:09 +03004635 I(SrcImm | NearBranch, em_call), D(SrcImm | ImplicitOps | NearBranch),
4636 I(SrcImmFAddr | No64, em_jmp_far),
4637 D(SrcImmByte | ImplicitOps | NearBranch),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004638 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
4639 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004640 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004641 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004642 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4643 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004644 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09004645 D(ImplicitOps), D(ImplicitOps),
4646 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004647 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4648};
4649
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004650static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004651 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004652 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02004653 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004654 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004655 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004656 N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004657 /* 0x10 - 0x1F */
Stefan Fritsch29916962018-04-01 17:54:44 +02004658 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_10_0f_11),
4659 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_10_0f_11),
4660 N, N, N, N, N, N,
Paolo Bonzini34d26182020-05-15 12:18:14 -04004661 D(ImplicitOps | ModRM | SrcMem | NoAccess), /* 4 * prefetch + 4 * reserved NOP */
4662 D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
4663 D(ImplicitOps | ModRM | SrcMem | NoAccess), /* 8 * reserved NOP */
4664 D(ImplicitOps | ModRM | SrcMem | NoAccess), /* 8 * reserved NOP */
4665 D(ImplicitOps | ModRM | SrcMem | NoAccess), /* 8 * reserved NOP */
4666 D(ImplicitOps | ModRM | SrcMem | NoAccess), /* NOP + 7 * reserved NOP */
Avi Kivity73fba5f2010-07-29 15:11:53 +03004667 /* 0x20 - 0x2F */
Sean Christophersond0fe7b62021-04-21 19:21:20 -07004668 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_access),
Nadav Amit9b88ae92014-05-25 23:05:21 +03004669 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
4670 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
Sean Christophersond0fe7b62021-04-21 19:21:20 -07004671 check_cr_access),
Nadav Amit9b88ae92014-05-25 23:05:21 +03004672 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
4673 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004674 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004675 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
4676 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004677 N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b),
Avi Kivity3e114eb2012-04-09 18:40:01 +03004678 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004679 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004680 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004681 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004682 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004683 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02004684 I(ImplicitOps | EmulateOnUD, em_sysenter),
4685 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004686 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004687 N, N, N, N, N, N, N, N,
4688 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03004689 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004690 /* 0x50 - 0x5F */
4691 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4692 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004693 N, N, N, N,
4694 N, N, N, N,
4695 N, N, N, N,
4696 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004697 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004698 N, N, N, N,
4699 N, N, N, N,
4700 N, N, N, N,
4701 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004702 /* 0x80 - 0x8F */
Nadav Amit58b70752014-10-24 11:35:09 +03004703 X16(D(SrcImm | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004704 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004705 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004706 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004707 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004708 II(ImplicitOps, em_cpuid, cpuid),
4709 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004710 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4711 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004712 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004713 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Paolo Bonzini89651a32015-11-03 13:43:05 +01004714 II(EmulateOnUD | ImplicitOps, em_rsm, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004715 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004716 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4717 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Nadav Amit13e457e2014-10-13 13:04:13 +03004718 GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004719 /* 0xB0 - 0xB7 */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02004720 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable | SrcWrite, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004721 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004722 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004723 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4724 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004725 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004726 /* 0xB8 - 0xBF */
4727 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004728 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004729 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Nadav Amit900efe22015-03-30 15:39:21 +03004730 I(DstReg | SrcMem | ModRM, em_bsf_c),
4731 I(DstReg | SrcMem | ModRM, em_bsr_c),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004732 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004733 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02004734 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Nadav Amit39f062f2014-11-26 15:47:18 +02004735 N, ID(0, &instr_dual_0f_c3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004736 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004737 /* 0xC8 - 0xCF */
4738 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004739 /* 0xD0 - 0xDF */
4740 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4741 /* 0xE0 - 0xEF */
Alex Williamson0a370272014-07-11 11:56:31 -06004742 N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
4743 N, N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004744 /* 0xF0 - 0xFF */
4745 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4746};
4747
Nadav Amit39f062f2014-11-26 15:47:18 +02004748static const struct instr_dual instr_dual_0f_38_f0 = {
4749 I(DstReg | SrcMem | Mov, em_movbe), N
4750};
4751
4752static const struct instr_dual instr_dual_0f_38_f1 = {
4753 I(DstMem | SrcReg | Mov, em_movbe), N
4754};
4755
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004756static const struct gprefix three_byte_0f_38_f0 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004757 ID(0, &instr_dual_0f_38_f0), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004758};
4759
4760static const struct gprefix three_byte_0f_38_f1 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004761 ID(0, &instr_dual_0f_38_f1), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004762};
4763
4764/*
4765 * Insns below are selected by the prefix which indexed by the third opcode
4766 * byte.
4767 */
4768static const struct opcode opcode_map_0f_38[256] = {
4769 /* 0x00 - 0x7f */
4770 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004771 /* 0x80 - 0xef */
4772 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4773 /* 0xf0 - 0xf1 */
Nadav Amit53bb4f72014-12-07 11:49:42 +02004774 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0),
4775 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004776 /* 0xf2 - 0xff */
4777 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004778};
4779
Avi Kivity73fba5f2010-07-29 15:11:53 +03004780#undef D
4781#undef N
4782#undef G
4783#undef GD
4784#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004785#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004786#undef EXT
Nadav Amit2276b512015-01-26 09:32:24 +02004787#undef MD
Nadav Amit2b42fce2015-01-26 09:32:25 +02004788#undef ID
Avi Kivity73fba5f2010-07-29 15:11:53 +03004789
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004790#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004791#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004792#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004793#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004794#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004795
Avi Kivity9dac77f2011-06-01 15:34:25 +03004796static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004797{
4798 unsigned size;
4799
Avi Kivity9dac77f2011-06-01 15:34:25 +03004800 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004801 if (size == 8)
4802 size = 4;
4803 return size;
4804}
4805
4806static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4807 unsigned size, bool sign_extension)
4808{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004809 int rc = X86EMUL_CONTINUE;
4810
4811 op->type = OP_IMM;
4812 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004813 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004814 /* NB. Immediates are sign-extended as necessary. */
4815 switch (op->bytes) {
4816 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004817 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004818 break;
4819 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004820 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004821 break;
4822 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004823 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004824 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004825 case 8:
4826 op->val = insn_fetch(s64, ctxt);
4827 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004828 }
4829 if (!sign_extension) {
4830 switch (op->bytes) {
4831 case 1:
4832 op->val &= 0xff;
4833 break;
4834 case 2:
4835 op->val &= 0xffff;
4836 break;
4837 case 4:
4838 op->val &= 0xffffffff;
4839 break;
4840 }
4841 }
4842done:
4843 return rc;
4844}
4845
Avi Kivitya99455492011-09-13 10:45:41 +03004846static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4847 unsigned d)
4848{
4849 int rc = X86EMUL_CONTINUE;
4850
4851 switch (d) {
4852 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004853 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03004854 break;
4855 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004856 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03004857 break;
4858 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004859 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004860 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03004861 *op = ctxt->memop;
4862 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004863 if (ctxt->d & BitOp)
Avi Kivitya99455492011-09-13 10:45:41 +03004864 fetch_bit_operand(ctxt);
4865 op->orig_val = op->val;
4866 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004867 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004868 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004869 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03004870 case OpAcc:
4871 op->type = OP_REG;
4872 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004873 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03004874 fetch_register_operand(op);
4875 op->orig_val = op->val;
4876 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004877 case OpAccLo:
4878 op->type = OP_REG;
4879 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4880 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4881 fetch_register_operand(op);
4882 op->orig_val = op->val;
4883 break;
4884 case OpAccHi:
4885 if (ctxt->d & ByteOp) {
4886 op->type = OP_NONE;
4887 break;
4888 }
4889 op->type = OP_REG;
4890 op->bytes = ctxt->op_bytes;
4891 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4892 fetch_register_operand(op);
4893 op->orig_val = op->val;
4894 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004895 case OpDI:
4896 op->type = OP_MEM;
4897 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4898 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004899 register_address(ctxt, VCPU_REGS_RDI);
Avi Kivitya99455492011-09-13 10:45:41 +03004900 op->addr.mem.seg = VCPU_SREG_ES;
4901 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004902 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03004903 break;
4904 case OpDX:
4905 op->type = OP_REG;
4906 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004907 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03004908 fetch_register_operand(op);
4909 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004910 case OpCL:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004911 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004912 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004913 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004914 break;
4915 case OpImmByte:
4916 rc = decode_imm(ctxt, op, 1, true);
4917 break;
4918 case OpOne:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004919 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004920 op->bytes = 1;
4921 op->val = 1;
4922 break;
4923 case OpImm:
4924 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4925 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004926 case OpImm64:
4927 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4928 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004929 case OpMem8:
4930 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004931 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004932 ctxt->memop.addr.reg = decode_register(ctxt,
4933 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004934 fetch_register_operand(&ctxt->memop);
4935 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004936 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004937 case OpMem16:
4938 ctxt->memop.bytes = 2;
4939 goto mem_common;
4940 case OpMem32:
4941 ctxt->memop.bytes = 4;
4942 goto mem_common;
4943 case OpImmU16:
4944 rc = decode_imm(ctxt, op, 2, false);
4945 break;
4946 case OpImmU:
4947 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4948 break;
4949 case OpSI:
4950 op->type = OP_MEM;
4951 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4952 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004953 register_address(ctxt, VCPU_REGS_RSI);
Bandan Das573e80f2014-04-16 12:46:13 -04004954 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03004955 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004956 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004957 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004958 case OpXLat:
4959 op->type = OP_MEM;
4960 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4961 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004962 address_mask(ctxt,
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004963 reg_read(ctxt, VCPU_REGS_RBX) +
4964 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04004965 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004966 op->val = 0;
4967 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004968 case OpImmFAddr:
4969 op->type = OP_IMM;
4970 op->addr.mem.ea = ctxt->_eip;
4971 op->bytes = ctxt->op_bytes + 2;
4972 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4973 break;
4974 case OpMemFAddr:
4975 ctxt->memop.bytes = ctxt->op_bytes + 2;
4976 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004977 case OpES:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004978 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004979 op->val = VCPU_SREG_ES;
4980 break;
4981 case OpCS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004982 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004983 op->val = VCPU_SREG_CS;
4984 break;
4985 case OpSS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004986 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004987 op->val = VCPU_SREG_SS;
4988 break;
4989 case OpDS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004990 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004991 op->val = VCPU_SREG_DS;
4992 break;
4993 case OpFS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004994 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004995 op->val = VCPU_SREG_FS;
4996 break;
4997 case OpGS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004998 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004999 op->val = VCPU_SREG_GS;
5000 break;
Avi Kivitya99455492011-09-13 10:45:41 +03005001 case OpImplicit:
5002 /* Special instructions do their own operand decoding. */
5003 default:
5004 op->type = OP_NONE; /* Disable writeback. */
5005 break;
5006 }
5007
5008done:
5009 return rc;
5010}
5011
Wanpeng Lib35491e2021-05-27 17:01:37 -07005012int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len, int emulation_type)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005013{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005014 int rc = X86EMUL_CONTINUE;
5015 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03005016 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005017 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04005018 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03005019 struct opcode opcode;
Wanpeng Li3853be22017-11-05 16:54:47 -08005020 u16 dummy;
5021 struct desc_struct desc;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005022
Avi Kivityf09ed832011-09-13 10:45:40 +03005023 ctxt->memop.type = OP_NONE;
5024 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005025 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02005026 ctxt->fetch.ptr = ctxt->fetch.data;
5027 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02005028 ctxt->opcode_len = 1;
Vitaly Kuznetsov342993f2020-03-03 15:33:15 +01005029 ctxt->intercept = x86_intercept_none;
Andre Przywaradc25e892010-12-21 11:12:07 +01005030 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005031 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02005032 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02005033 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02005034 if (rc != X86EMUL_CONTINUE)
Sean Christopherson016cd752019-08-15 09:20:32 -07005035 goto done;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02005036 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005037
5038 switch (mode) {
5039 case X86EMUL_MODE_REAL:
5040 case X86EMUL_MODE_VM86:
Wanpeng Li3853be22017-11-05 16:54:47 -08005041 def_op_bytes = def_ad_bytes = 2;
5042 ctxt->ops->get_segment(ctxt, &dummy, &desc, NULL, VCPU_SREG_CS);
5043 if (desc.d)
5044 def_op_bytes = def_ad_bytes = 4;
5045 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005046 case X86EMUL_MODE_PROT16:
5047 def_op_bytes = def_ad_bytes = 2;
5048 break;
5049 case X86EMUL_MODE_PROT32:
5050 def_op_bytes = def_ad_bytes = 4;
5051 break;
5052#ifdef CONFIG_X86_64
5053 case X86EMUL_MODE_PROT64:
5054 def_op_bytes = 4;
5055 def_ad_bytes = 8;
5056 break;
5057#endif
5058 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005059 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005060 }
5061
Avi Kivity9dac77f2011-06-01 15:34:25 +03005062 ctxt->op_bytes = def_op_bytes;
5063 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005064
5065 /* Legacy prefixes. */
5066 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09005067 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005068 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005069 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005070 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005071 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005072 break;
5073 case 0x67: /* address-size override */
5074 if (mode == X86EMUL_MODE_PROT64)
5075 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005076 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005077 else
5078 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005079 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005080 break;
5081 case 0x26: /* ES override */
Marios Pomonis125ffc52019-12-11 12:47:50 -08005082 has_seg_override = true;
5083 ctxt->seg_override = VCPU_SREG_ES;
5084 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005085 case 0x2e: /* CS override */
Marios Pomonis125ffc52019-12-11 12:47:50 -08005086 has_seg_override = true;
5087 ctxt->seg_override = VCPU_SREG_CS;
5088 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005089 case 0x36: /* SS override */
Marios Pomonis125ffc52019-12-11 12:47:50 -08005090 has_seg_override = true;
5091 ctxt->seg_override = VCPU_SREG_SS;
5092 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005093 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04005094 has_seg_override = true;
Marios Pomonis125ffc52019-12-11 12:47:50 -08005095 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005096 break;
5097 case 0x64: /* FS override */
Marios Pomonis125ffc52019-12-11 12:47:50 -08005098 has_seg_override = true;
5099 ctxt->seg_override = VCPU_SREG_FS;
5100 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005101 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04005102 has_seg_override = true;
Marios Pomonis125ffc52019-12-11 12:47:50 -08005103 ctxt->seg_override = VCPU_SREG_GS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005104 break;
5105 case 0x40 ... 0x4f: /* REX */
5106 if (mode != X86EMUL_MODE_PROT64)
5107 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005108 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005109 continue;
5110 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005111 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005112 break;
5113 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005114 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005115 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005116 break;
5117 default:
5118 goto done_prefixes;
5119 }
5120
5121 /* Any legacy prefix after a REX prefix nullifies its effect. */
5122
Avi Kivity9dac77f2011-06-01 15:34:25 +03005123 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005124 }
5125
5126done_prefixes:
5127
5128 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005129 if (ctxt->rex_prefix & 8)
5130 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005131
5132 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005133 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08005134 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005135 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02005136 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09005137 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03005138 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005139
5140 /* 0F_38 opcode map */
5141 if (ctxt->b == 0x38) {
5142 ctxt->opcode_len = 3;
5143 ctxt->b = insn_fetch(u8, ctxt);
5144 opcode = opcode_map_0f_38[ctxt->b];
5145 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005146 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03005147 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005148
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09005149 if (ctxt->d & ModRM)
5150 ctxt->modrm = insn_fetch(u8, ctxt);
5151
Nadav Amit7fe864d2014-06-02 18:34:03 +03005152 /* vex-prefix instructions are not implemented */
5153 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
Nadav Amitd14cb5d2014-11-02 11:54:58 +02005154 (mode == X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) == 0xc0)) {
Nadav Amit7fe864d2014-06-02 18:34:03 +03005155 ctxt->d = NotImpl;
5156 }
5157
Avi Kivity9dac77f2011-06-01 15:34:25 +03005158 while (ctxt->d & GroupMask) {
5159 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03005160 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005161 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03005162 opcode = opcode.u.group[goffset];
5163 break;
5164 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005165 goffset = (ctxt->modrm >> 3) & 7;
5166 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03005167 opcode = opcode.u.gdual->mod3[goffset];
5168 else
5169 opcode = opcode.u.gdual->mod012[goffset];
5170 break;
5171 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005172 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02005173 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03005174 break;
5175 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005176 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005177 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005178 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03005179 switch (simd_prefix) {
5180 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
5181 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
5182 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
5183 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
5184 }
5185 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02005186 case Escape:
Marios Pomonis3c9053a22019-12-11 12:47:41 -08005187 if (ctxt->modrm > 0xbf) {
5188 size_t size = ARRAY_SIZE(opcode.u.esc->high);
5189 u32 index = array_index_nospec(
5190 ctxt->modrm - 0xc0, size);
5191
5192 opcode = opcode.u.esc->high[index];
5193 } else {
Gleb Natapov045a2822012-12-20 16:57:43 +02005194 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
Marios Pomonis3c9053a22019-12-11 12:47:41 -08005195 }
Gleb Natapov045a2822012-12-20 16:57:43 +02005196 break;
Nadav Amit39f062f2014-11-26 15:47:18 +02005197 case InstrDual:
5198 if ((ctxt->modrm >> 6) == 3)
5199 opcode = opcode.u.idual->mod3;
5200 else
5201 opcode = opcode.u.idual->mod012;
5202 break;
Nadav Amit2276b512015-01-26 09:32:24 +02005203 case ModeDual:
5204 if (ctxt->mode == X86EMUL_MODE_PROT64)
5205 opcode = opcode.u.mdual->mode64;
5206 else
5207 opcode = opcode.u.mdual->mode32;
5208 break;
Avi Kivity46561642011-04-24 14:09:59 +03005209 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005210 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005211 }
Avi Kivity46561642011-04-24 14:09:59 +03005212
Avi Kivityb1ea50b2011-09-13 10:45:42 +03005213 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005214 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005215 }
5216
Paolo Bonzinie24186e2014-03-27 12:00:57 +01005217 /* Unrecognised? */
5218 if (ctxt->d == 0)
5219 return EMULATION_FAILED;
5220
Avi Kivity9dac77f2011-06-01 15:34:25 +03005221 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005222
Wanpeng Lib35491e2021-05-27 17:01:37 -07005223 if (unlikely(emulation_type & EMULTYPE_TRAP_UD) &&
5224 likely(!(ctxt->d & EmulateOnUD)))
Nadav Amit3a6095a2014-08-13 16:50:13 +03005225 return EMULATION_FAILED;
5226
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005227 if (unlikely(ctxt->d &
Nadav Amited9aad22014-11-02 11:55:00 +02005228 (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch|
5229 No16))) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005230 /*
5231 * These are copied unconditionally here, and checked unconditionally
5232 * in x86_emulate_insn.
5233 */
5234 ctxt->check_perm = opcode.check_perm;
5235 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005236
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005237 if (ctxt->d & NotImpl)
5238 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02005239
Nadav Amit58b70752014-10-24 11:35:09 +03005240 if (mode == X86EMUL_MODE_PROT64) {
5241 if (ctxt->op_bytes == 4 && (ctxt->d & Stack))
5242 ctxt->op_bytes = 8;
5243 else if (ctxt->d & NearBranch)
5244 ctxt->op_bytes = 8;
5245 }
Avi Kivity7f9b4b72010-08-01 14:46:54 +03005246
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005247 if (ctxt->d & Op3264) {
5248 if (mode == X86EMUL_MODE_PROT64)
5249 ctxt->op_bytes = 8;
5250 else
5251 ctxt->op_bytes = 4;
5252 }
5253
Nadav Amited9aad22014-11-02 11:55:00 +02005254 if ((ctxt->d & No16) && ctxt->op_bytes == 2)
5255 ctxt->op_bytes = 4;
5256
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005257 if (ctxt->d & Sse)
5258 ctxt->op_bytes = 16;
5259 else if (ctxt->d & Mmx)
5260 ctxt->op_bytes = 8;
5261 }
Avi Kivity1253791d2011-03-29 11:41:27 +02005262
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005263 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005264 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03005265 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04005266 if (!has_seg_override) {
5267 has_seg_override = true;
5268 ctxt->seg_override = ctxt->modrm_seg;
5269 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03005270 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03005271 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005272 if (rc != X86EMUL_CONTINUE)
5273 goto done;
5274
Bandan Das573e80f2014-04-16 12:46:13 -04005275 if (!has_seg_override)
5276 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005277
Bandan Das573e80f2014-04-16 12:46:13 -04005278 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005279
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005280 /*
5281 * Decode and fetch the source operand: register, memory
5282 * or immediate.
5283 */
Avi Kivity0fe59122011-09-13 10:45:47 +03005284 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03005285 if (rc != X86EMUL_CONTINUE)
5286 goto done;
5287
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005288 /*
5289 * Decode and fetch the second source operand: register, memory
5290 * or immediate.
5291 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03005292 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03005293 if (rc != X86EMUL_CONTINUE)
5294 goto done;
5295
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005296 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03005297 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005298
Owen Hofmannd9092f52016-10-27 11:25:52 -07005299 if (ctxt->rip_relative && likely(ctxt->memopp))
Nadav Amit1c1c35a2014-11-19 17:43:09 +02005300 ctxt->memopp->addr.mem.ea = address_mask(ctxt,
5301 ctxt->memopp->addr.mem.ea + ctxt->_eip);
Avi Kivitycb16c342011-06-19 19:21:11 +03005302
Paolo Bonzinia430c912014-10-23 14:54:14 +02005303done:
Jan Dakinevichc8848ce2019-08-27 13:07:08 +00005304 if (rc == X86EMUL_PROPAGATE_FAULT)
5305 ctxt->have_exception = true;
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005306 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005307}
5308
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005309bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
5310{
5311 return ctxt->d & PageTable;
5312}
5313
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005314static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
5315{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005316 /* The second termination condition only applies for REPE
5317 * and REPNE. Test if the repeat string operation prefix is
5318 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
5319 * corresponding termination condition according to:
5320 * - if REPE/REPZ and ZF = 0 then done
5321 * - if REPNE/REPNZ and ZF = 1 then done
5322 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005323 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
5324 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
5325 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03005326 ((ctxt->eflags & X86_EFLAGS_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03005327 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03005328 ((ctxt->eflags & X86_EFLAGS_ZF) == X86_EFLAGS_ZF))))
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005329 return true;
5330
5331 return false;
5332}
5333
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005334static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
5335{
Radim Krčmářaabba3c2016-11-08 20:54:18 +01005336 int rc;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005337
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02005338 kvm_fpu_get();
Radim Krčmářaabba3c2016-11-08 20:54:18 +01005339 rc = asm_safe("fwait");
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02005340 kvm_fpu_put();
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005341
Radim Krčmářaabba3c2016-11-08 20:54:18 +01005342 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005343 return emulate_exception(ctxt, MF_VECTOR, 0, false);
5344
5345 return X86EMUL_CONTINUE;
5346}
5347
Sean Christophersonc0a21c32020-01-17 11:30:52 -08005348static void fetch_possible_mmx_operand(struct operand *op)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005349{
5350 if (op->type == OP_MM)
Siddharth Chandrasekaran43e51462021-05-26 10:56:08 +02005351 kvm_read_mmx_reg(op->addr.mm, &op->mm_val);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005352}
5353
Sean Christopherson3009afc2020-01-21 20:43:39 -08005354static int fastop(struct x86_emulate_ctxt *ctxt, fastop_t fop)
Avi Kivitye28bbd42013-01-04 16:18:48 +02005355{
5356 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005357
Avi Kivityb9fa4092013-02-09 11:31:48 +02005358 if (!(ctxt->d & ByteOp))
5359 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005360
Peter Zijlstra1a29b5b2018-01-25 10:58:13 +01005361 asm("push %[flags]; popf; " CALL_NOSPEC " ; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005362 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
Peter Zijlstra1a29b5b2018-01-25 10:58:13 +01005363 [thunk_target]"+S"(fop), ASM_CALL_CONSTRAINT
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005364 : "c"(ctxt->src2.val));
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005365
Avi Kivitye28bbd42013-01-04 16:18:48 +02005366 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005367 if (!fop) /* exception is returned in fop variable */
5368 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02005369 return X86EMUL_CONTINUE;
5370}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005371
Bandan Das14985072014-04-16 12:46:09 -04005372void init_decode_cache(struct x86_emulate_ctxt *ctxt)
5373{
Bandan Das573e80f2014-04-16 12:46:13 -04005374 memset(&ctxt->rip_relative, 0,
5375 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04005376
Bandan Das14985072014-04-16 12:46:09 -04005377 ctxt->io_read.pos = 0;
5378 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04005379 ctxt->mem_read.end = 0;
5380}
5381
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09005382int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005383{
Mathias Krause0225fb52012-08-30 01:30:16 +02005384 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09005385 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005386 int saved_dst_type = ctxt->dst.type;
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005387 unsigned emul_flags;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005388
Avi Kivity9dac77f2011-06-01 15:34:25 +03005389 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04005390
Gleb Natapovd380a5e2010-02-10 14:21:36 +02005391 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005392 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02005393 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02005394 goto done;
5395 }
5396
Avi Kivity9dac77f2011-06-01 15:34:25 +03005397 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02005398 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03005399 goto done;
5400 }
5401
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005402 emul_flags = ctxt->ops->get_hflags(ctxt);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005403 if (unlikely(ctxt->d &
5404 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
5405 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
5406 (ctxt->d & Undefined)) {
5407 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005408 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005409 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005410
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005411 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
5412 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
5413 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02005414 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005415 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02005416
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005417 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
5418 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02005419 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005420 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02005421
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005422 if (ctxt->d & Mmx) {
5423 rc = flush_pending_x87_faults(ctxt);
5424 if (rc != X86EMUL_CONTINUE)
5425 goto done;
5426 /*
5427 * Now that we know the fpu is exception safe, we can fetch
5428 * operands from it.
5429 */
Sean Christophersonc0a21c32020-01-17 11:30:52 -08005430 fetch_possible_mmx_operand(&ctxt->src);
5431 fetch_possible_mmx_operand(&ctxt->src2);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005432 if (!(ctxt->d & Mov))
Sean Christophersonc0a21c32020-01-17 11:30:52 -08005433 fetch_possible_mmx_operand(&ctxt->dst);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005434 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02005435
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005436 if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && ctxt->intercept) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005437 rc = emulator_check_intercept(ctxt, ctxt->intercept,
5438 X86_ICPT_PRE_EXCEPT);
5439 if (rc != X86EMUL_CONTINUE)
5440 goto done;
5441 }
5442
Nadav Amit64a38292014-12-10 11:19:04 +02005443 /* Instruction can only be executed in protected mode */
5444 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
5445 rc = emulate_ud(ctxt);
5446 goto done;
5447 }
5448
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005449 /* Privileged instruction can be executed only in CPL=0 */
5450 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Nadav Amit68efa762014-06-18 17:19:35 +03005451 if (ctxt->d & PrivUD)
5452 rc = emulate_ud(ctxt);
5453 else
5454 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02005455 goto done;
5456 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005457
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005458 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04005459 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005460 rc = ctxt->check_perm(ctxt);
5461 if (rc != X86EMUL_CONTINUE)
5462 goto done;
5463 }
5464
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005465 if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005466 rc = emulator_check_intercept(ctxt, ctxt->intercept,
5467 X86_ICPT_POST_EXCEPT);
5468 if (rc != X86EMUL_CONTINUE)
5469 goto done;
5470 }
5471
5472 if (ctxt->rep_prefix && (ctxt->d & String)) {
5473 /* All REP prefixes have the same first termination condition */
5474 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Nadav Amit428e3d02015-04-28 13:06:01 +03005475 string_registers_quirk(ctxt);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005476 ctxt->eip = ctxt->_eip;
Nadav Amit0efb0442015-03-29 16:33:03 +03005477 ctxt->eflags &= ~X86_EFLAGS_RF;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005478 goto done;
5479 }
5480 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02005481 }
5482
Avi Kivity9dac77f2011-06-01 15:34:25 +03005483 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
5484 rc = segmented_read(ctxt, ctxt->src.addr.mem,
5485 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09005486 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005487 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005488 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005489 }
5490
Avi Kivity9dac77f2011-06-01 15:34:25 +03005491 if (ctxt->src2.type == OP_MEM) {
5492 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
5493 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02005494 if (rc != X86EMUL_CONTINUE)
5495 goto done;
5496 }
5497
Avi Kivity9dac77f2011-06-01 15:34:25 +03005498 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005499 goto special_insn;
5500
5501
Avi Kivity9dac77f2011-06-01 15:34:25 +03005502 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02005503 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005504 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
5505 &ctxt->dst.val, ctxt->dst.bytes);
Nadav Amitc205fb72014-12-25 02:52:16 +02005506 if (rc != X86EMUL_CONTINUE) {
Paolo Bonzinid44e1212015-02-09 10:02:05 +01005507 if (!(ctxt->d & NoWrite) &&
5508 rc == X86EMUL_PROPAGATE_FAULT &&
Nadav Amitc205fb72014-12-25 02:52:16 +02005509 ctxt->exception.vector == PF_VECTOR)
5510 ctxt->exception.error_code |= PFERR_WRITE_MASK;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02005511 goto done;
Nadav Amitc205fb72014-12-25 02:52:16 +02005512 }
Avi Kivity038e51d2007-01-22 20:40:40 -08005513 }
Paolo Bonzini4ff6f8e2015-02-12 17:04:47 +01005514 /* Copy full 64-bit value for CMPXCHG8B. */
5515 ctxt->dst.orig_val64 = ctxt->dst.val64;
Avi Kivity038e51d2007-01-22 20:40:40 -08005516
Avi Kivity018a98d2007-11-27 19:30:56 +02005517special_insn:
5518
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005519 if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03005520 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005521 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02005522 if (rc != X86EMUL_CONTINUE)
5523 goto done;
5524 }
5525
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03005526 if (ctxt->rep_prefix && (ctxt->d & String))
Nadav Amit0efb0442015-03-29 16:33:03 +03005527 ctxt->eflags |= X86_EFLAGS_RF;
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03005528 else
Nadav Amit0efb0442015-03-29 16:33:03 +03005529 ctxt->eflags &= ~X86_EFLAGS_RF;
Nadav Amit4467c3f2014-07-21 14:37:29 +03005530
Avi Kivity9dac77f2011-06-01 15:34:25 +03005531 if (ctxt->execute) {
Sean Christopherson3009afc2020-01-21 20:43:39 -08005532 if (ctxt->d & Fastop)
Qian Caib78a8552020-02-17 11:48:26 -05005533 rc = fastop(ctxt, ctxt->fop);
Sean Christopherson3009afc2020-01-21 20:43:39 -08005534 else
Miaohe Lin52db3692020-01-22 11:21:44 +08005535 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03005536 if (rc != X86EMUL_CONTINUE)
5537 goto done;
5538 goto writeback;
5539 }
5540
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02005541 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005542 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005543 else if (ctxt->opcode_len == 3)
5544 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005545
Avi Kivity9dac77f2011-06-01 15:34:25 +03005546 switch (ctxt->b) {
Gleb Natapovb2833e32009-04-12 13:36:30 +03005547 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005548 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005549 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005550 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03005551 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005552 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03005553 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03005554 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005555 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03005556 ctxt->dst.type = OP_NONE;
5557 else
5558 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09005559 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005560 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005561 switch (ctxt->op_bytes) {
5562 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
5563 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
5564 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005565 }
5566 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005567 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005568 rc = emulate_int(ctxt, 3);
5569 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005570 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005571 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005572 break;
5573 case 0xce: /* into */
Nadav Amit0efb0442015-03-29 16:33:03 +03005574 if (ctxt->eflags & X86_EFLAGS_OF)
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005575 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005576 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005577 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09005578 case 0xeb: /* jmp rel short */
Nadav Amit234f3ce2014-09-18 22:39:38 +03005579 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity9dac77f2011-06-01 15:34:25 +03005580 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005581 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005582 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03005583 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03005584 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005585 case 0xf5: /* cmc */
5586 /* complement carry flag from eflags reg */
Nadav Amit0efb0442015-03-29 16:33:03 +03005587 ctxt->eflags ^= X86_EFLAGS_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005588 break;
5589 case 0xf8: /* clc */
Nadav Amit0efb0442015-03-29 16:33:03 +03005590 ctxt->eflags &= ~X86_EFLAGS_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005591 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03005592 case 0xf9: /* stc */
Nadav Amit0efb0442015-03-29 16:33:03 +03005593 ctxt->eflags |= X86_EFLAGS_CF;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03005594 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005595 case 0xfc: /* cld */
Nadav Amit0efb0442015-03-29 16:33:03 +03005596 ctxt->eflags &= ~X86_EFLAGS_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005597 break;
5598 case 0xfd: /* std */
Nadav Amit0efb0442015-03-29 16:33:03 +03005599 ctxt->eflags |= X86_EFLAGS_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005600 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005601 default:
5602 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005603 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005604
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005605 if (rc != X86EMUL_CONTINUE)
5606 goto done;
5607
Avi Kivity018a98d2007-11-27 19:30:56 +02005608writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02005609 if (ctxt->d & SrcWrite) {
5610 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
5611 rc = writeback(ctxt, &ctxt->src);
5612 if (rc != X86EMUL_CONTINUE)
5613 goto done;
5614 }
Nadav Amitee212292014-06-15 16:12:58 +03005615 if (!(ctxt->d & NoWrite)) {
5616 rc = writeback(ctxt, &ctxt->dst);
5617 if (rc != X86EMUL_CONTINUE)
5618 goto done;
5619 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005620
Gleb Natapov5cd21912010-03-18 15:20:26 +02005621 /*
5622 * restore dst type in case the decoding will be reused
5623 * (happens for string instruction )
5624 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005625 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005626
Avi Kivity9dac77f2011-06-01 15:34:25 +03005627 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005628 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02005629
Avi Kivity9dac77f2011-06-01 15:34:25 +03005630 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005631 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02005632
Avi Kivity9dac77f2011-06-01 15:34:25 +03005633 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005634 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005635 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005636 if ((ctxt->d & SrcMask) == SrcSI)
5637 count = ctxt->src.count;
5638 else
5639 count = ctxt->dst.count;
Paolo Bonzini01485a22014-11-19 18:25:08 +01005640 register_address_increment(ctxt, VCPU_REGS_RCX, -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005641
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005642 if (!string_insn_completed(ctxt)) {
5643 /*
5644 * Re-enter guest when pio read ahead buffer is empty
5645 * or, if it is not used, after each 1024 iteration.
5646 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005647 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005648 (r->end == 0 || r->end != r->pos)) {
5649 /*
5650 * Reset read cache. Usually happens before
5651 * decode, but since instruction is restarted
5652 * we have to do it here.
5653 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005654 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005655 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005656 return EMULATION_RESTART;
5657 }
5658 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03005659 }
Nadav Amit0efb0442015-03-29 16:33:03 +03005660 ctxt->eflags &= ~X86_EFLAGS_RF;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005661 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005662
Avi Kivity9dac77f2011-06-01 15:34:25 +03005663 ctxt->eip = ctxt->_eip;
Paolo Bonzinifede8072020-04-27 11:55:59 -04005664 if (ctxt->mode != X86EMUL_MODE_PROT64)
5665 ctxt->eip = (u32)ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02005666
5667done:
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005668 if (rc == X86EMUL_PROPAGATE_FAULT) {
5669 WARN_ON(ctxt->exception.vector > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +02005670 ctxt->have_exception = true;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005671 }
Joerg Roedel775fde82011-04-04 12:39:24 +02005672 if (rc == X86EMUL_INTERCEPTED)
5673 return EMULATION_INTERCEPTED;
5674
Avi Kivitydd856ef2012-08-27 23:46:17 +03005675 if (rc == X86EMUL_CONTINUE)
5676 writeback_registers(ctxt);
5677
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005678 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005679
5680twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005681 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02005682 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02005683 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005684 break;
5685 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02005686 case 0x0d: /* GrpP (prefetch) */
5687 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02005688 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02005689 break;
5690 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005691 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02005692 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005693 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005694 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005695 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005696 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03005697 if (test_cc(ctxt->b, ctxt->eflags))
5698 ctxt->dst.val = ctxt->src.val;
Nadav Amitb91aa142015-03-30 15:39:19 +03005699 else if (ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005700 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08005701 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03005702 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03005703 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005704 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005705 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08005706 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005707 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08005708 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005709 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005710 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005711 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03005712 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005713 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005714 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005715 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005716 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03005717 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005718 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005719 default:
5720 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005721 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005722
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005723threebyte_insn:
5724
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005725 if (rc != X86EMUL_CONTINUE)
5726 goto done;
5727
Avi Kivity6aa8b732006-12-10 02:21:36 -08005728 goto writeback;
5729
5730cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02005731 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005732}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005733
5734void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
5735{
5736 invalidate_registers(ctxt);
5737}
5738
5739void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
5740{
5741 writeback_registers(ctxt);
5742}
Tom Lendacky0f89b202016-12-14 14:59:23 -05005743
5744bool emulator_can_use_gpa(struct x86_emulate_ctxt *ctxt)
5745{
5746 if (ctxt->rep_prefix && (ctxt->d & String))
5747 return false;
5748
5749 if (ctxt->d & TwoMemOp)
5750 return false;
5751
5752 return true;
5753}