blob: a943bf0c06d0d49dfed8c5f2cb87de6cacd38d94 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivityb7d491e2013-01-04 16:18:49 +020027#include <linux/stringify.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028
Avi Kivity3eeb3282010-01-21 15:31:48 +020029#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020030#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020031
Avi Kivity6aa8b732006-12-10 02:21:36 -080032/*
Avi Kivitya99455492011-09-13 10:45:41 +030033 * Operand types
34 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030035#define OpNone 0ull
36#define OpImplicit 1ull /* No generic decode */
37#define OpReg 2ull /* Register */
38#define OpMem 3ull /* Memory */
39#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
40#define OpDI 5ull /* ES:DI/EDI/RDI */
41#define OpMem64 6ull /* Memory, 64-bit */
42#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
43#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030044#define OpCL 9ull /* CL register (for shifts) */
45#define OpImmByte 10ull /* 8-bit sign extended immediate */
46#define OpOne 11ull /* Implied 1 */
Nadav Amit5e2c6882012-12-06 21:55:10 -020047#define OpImm 12ull /* Sign extended up to 32-bit immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030048#define OpMem16 13ull /* Memory operand (16-bit). */
49#define OpMem32 14ull /* Memory operand (32-bit). */
50#define OpImmU 15ull /* Immediate operand, zero extended */
51#define OpSI 16ull /* SI/ESI/RSI */
52#define OpImmFAddr 17ull /* Immediate far address */
53#define OpMemFAddr 18ull /* Far address in memory */
54#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030055#define OpES 20ull /* ES */
56#define OpCS 21ull /* CS */
57#define OpSS 22ull /* SS */
58#define OpDS 23ull /* DS */
59#define OpFS 24ull /* FS */
60#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020061#define OpMem8 26ull /* 8-bit zero extended memory operand */
Nadav Amit5e2c6882012-12-06 21:55:10 -020062#define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */
Paolo Bonzini7fa57952013-05-09 11:32:50 +020063#define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */
Avi Kivity820207c2013-02-09 11:31:45 +020064#define OpAccLo 29ull /* Low part of extended acc (AX/AX/EAX/RAX) */
65#define OpAccHi 30ull /* High part of extended acc (-/DX/EDX/RDX) */
Avi Kivitya99455492011-09-13 10:45:41 +030066
Avi Kivity0fe59122011-09-13 10:45:47 +030067#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030068#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya99455492011-09-13 10:45:41 +030069
70/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080071 * Opcode effective-address decode tables.
72 * Note that we only emulate instructions that have at least one memory
73 * operand (excluding implicit stack references). We assume that stack
74 * references and instruction fetches will never occur in special memory
75 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
76 * not be handled.
77 */
78
79/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030080#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080081/* Destination operand type. */
Avi Kivitya99455492011-09-13 10:45:41 +030082#define DstShift 1
83#define ImplicitOps (OpImplicit << DstShift)
84#define DstReg (OpReg << DstShift)
85#define DstMem (OpMem << DstShift)
86#define DstAcc (OpAcc << DstShift)
87#define DstDI (OpDI << DstShift)
88#define DstMem64 (OpMem64 << DstShift)
Nadav Amit16bebef2014-12-25 02:52:18 +020089#define DstMem16 (OpMem16 << DstShift)
Avi Kivitya99455492011-09-13 10:45:41 +030090#define DstImmUByte (OpImmUByte << DstShift)
91#define DstDX (OpDX << DstShift)
Avi Kivity820207c2013-02-09 11:31:45 +020092#define DstAccLo (OpAccLo << DstShift)
Avi Kivitya99455492011-09-13 10:45:41 +030093#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080094/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030095#define SrcShift 6
96#define SrcNone (OpNone << SrcShift)
97#define SrcReg (OpReg << SrcShift)
98#define SrcMem (OpMem << SrcShift)
99#define SrcMem16 (OpMem16 << SrcShift)
100#define SrcMem32 (OpMem32 << SrcShift)
101#define SrcImm (OpImm << SrcShift)
102#define SrcImmByte (OpImmByte << SrcShift)
103#define SrcOne (OpOne << SrcShift)
104#define SrcImmUByte (OpImmUByte << SrcShift)
105#define SrcImmU (OpImmU << SrcShift)
106#define SrcSI (OpSI << SrcShift)
Paolo Bonzini7fa57952013-05-09 11:32:50 +0200107#define SrcXLat (OpXLat << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300108#define SrcImmFAddr (OpImmFAddr << SrcShift)
109#define SrcMemFAddr (OpMemFAddr << SrcShift)
110#define SrcAcc (OpAcc << SrcShift)
111#define SrcImmU16 (OpImmU16 << SrcShift)
Nadav Amit5e2c6882012-12-06 21:55:10 -0200112#define SrcImm64 (OpImm64 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300113#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200114#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity820207c2013-02-09 11:31:45 +0200115#define SrcAccHi (OpAccHi << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300116#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300117#define BitOp (1<<11)
118#define MemAbs (1<<12) /* Memory operand is absolute displacement */
119#define String (1<<13) /* String instruction (rep capable) */
120#define Stack (1<<14) /* Stack instruction (push/pop) */
121#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
122#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
123#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
124#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
125#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
Gleb Natapov045a2822012-12-20 16:57:43 +0200126#define Escape (5<<15) /* Escape to coprocessor instruction */
Nadav Amit39f062f2014-11-26 15:47:18 +0200127#define InstrDual (6<<15) /* Alternate instruction decoding of mod == 3 */
Nadav Amit2276b512015-01-26 09:32:24 +0200128#define ModeDual (7<<15) /* Different instruction for 32/64 bit */
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300129#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300130/* Generic ModRM decode. */
131#define ModRM (1<<19)
132/* Destination is only written; never read. */
133#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300134/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200135#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Borislav Petkovb51e9742013-09-22 16:44:52 +0200136#define EmulateOnUD (1<<22) /* Emulate if unsupported by the host */
Avi Kivity5a506b12010-08-01 15:10:29 +0300137#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300138#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300139#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200140#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200141#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300142#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800143#define PageTable (1 << 29) /* instruction used to write page table */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300144#define NotImpl (1 << 30) /* instruction is not implemented */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100145/* Source 2 operand type */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300146#define Src2Shift (31)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300147#define Src2None (OpNone << Src2Shift)
Avi Kivityab2c5ce2013-02-09 11:31:46 +0200148#define Src2Mem (OpMem << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300149#define Src2CL (OpCL << Src2Shift)
150#define Src2ImmByte (OpImmByte << Src2Shift)
151#define Src2One (OpOne << Src2Shift)
152#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300153#define Src2ES (OpES << Src2Shift)
154#define Src2CS (OpCS << Src2Shift)
155#define Src2SS (OpSS << Src2Shift)
156#define Src2DS (OpDS << Src2Shift)
157#define Src2FS (OpFS << Src2Shift)
158#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300159#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300160#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Avi Kivity1c11b372012-04-09 18:39:59 +0300161#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
162#define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */
163#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
Avi Kivitye28bbd42013-01-04 16:18:48 +0200164#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
Avi Kivityb6744dc2013-01-04 16:18:50 +0200165#define NoWrite ((u64)1 << 45) /* No writeback */
Avi Kivityfb32b1e2013-02-09 11:31:44 +0200166#define SrcWrite ((u64)1 << 46) /* Write back src operand */
Nadav Amit9b88ae92014-05-25 23:05:21 +0300167#define NoMod ((u64)1 << 47) /* Mod field is ignored */
Paolo Bonzinid40a6892014-03-27 11:58:02 +0100168#define Intercept ((u64)1 << 48) /* Has valid intercept field */
169#define CheckPerm ((u64)1 << 49) /* Has valid check_perm field */
Nadav Amit68efa762014-06-18 17:19:35 +0300170#define PrivUD ((u64)1 << 51) /* #UD instead of #GP on CPL > 0 */
Nadav Amit58b70752014-10-24 11:35:09 +0300171#define NearBranch ((u64)1 << 52) /* Near branches */
Nadav Amited9aad22014-11-02 11:55:00 +0200172#define No16 ((u64)1 << 53) /* No 16 bit operand */
Nadav Amitab708092014-12-25 02:52:21 +0200173#define IncSP ((u64)1 << 54) /* SP is incremented before ModRM calc */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800174
Avi Kivity820207c2013-02-09 11:31:45 +0200175#define DstXacc (DstAccLo | SrcAccHi | SrcWrite)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800176
Avi Kivityd0e53322010-07-29 15:11:54 +0300177#define X2(x...) x, x
178#define X3(x...) X2(x), x
179#define X4(x...) X2(x), X2(x)
180#define X5(x...) X4(x), x
181#define X6(x...) X4(x), X2(x)
182#define X7(x...) X4(x), X3(x)
183#define X8(x...) X4(x), X4(x)
184#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300185
Avi Kivitye28bbd42013-01-04 16:18:48 +0200186#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
187#define FASTOP_SIZE 8
188
189/*
190 * fastop functions have a special calling convention:
191 *
Avi Kivity017da7b2013-02-09 11:31:47 +0200192 * dst: rax (in/out)
193 * src: rdx (in/out)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200194 * src2: rcx (in)
195 * flags: rflags (in/out)
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200196 * ex: rsi (in:fastop pointer, out:zero if exception)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200197 *
198 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
199 * different operand sizes can be reached by calculation, rather than a jump
200 * table (which would be bigger than the code).
201 *
202 * fastop functions are declared as taking a never-defined fastop parameter,
203 * so they can't be called from C directly.
204 */
205
206struct fastop;
207
Avi Kivityd65b1de2010-07-29 15:11:35 +0300208struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300209 u64 flags : 56;
210 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300211 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300212 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200213 const struct opcode *group;
214 const struct group_dual *gdual;
215 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200216 const struct escape *esc;
Nadav Amit39f062f2014-11-26 15:47:18 +0200217 const struct instr_dual *idual;
Nadav Amit2276b512015-01-26 09:32:24 +0200218 const struct mode_dual *mdual;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200219 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300220 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200221 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300222};
223
224struct group_dual {
225 struct opcode mod012[8];
226 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300227};
228
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200229struct gprefix {
230 struct opcode pfx_no;
231 struct opcode pfx_66;
232 struct opcode pfx_f2;
233 struct opcode pfx_f3;
234};
235
Gleb Natapov045a2822012-12-20 16:57:43 +0200236struct escape {
237 struct opcode op[8];
238 struct opcode high[64];
239};
240
Nadav Amit39f062f2014-11-26 15:47:18 +0200241struct instr_dual {
242 struct opcode mod012;
243 struct opcode mod3;
244};
245
Nadav Amit2276b512015-01-26 09:32:24 +0200246struct mode_dual {
247 struct opcode mode32;
248 struct opcode mode64;
249};
250
Avi Kivity6aa8b732006-12-10 02:21:36 -0800251/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200252#define EFLG_ID (1<<21)
253#define EFLG_VIP (1<<20)
254#define EFLG_VIF (1<<19)
255#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200256#define EFLG_VM (1<<17)
257#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200258#define EFLG_IOPL (3<<12)
259#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800260#define EFLG_OF (1<<11)
261#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200262#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200263#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800264#define EFLG_SF (1<<7)
265#define EFLG_ZF (1<<6)
266#define EFLG_AF (1<<4)
267#define EFLG_PF (1<<2)
268#define EFLG_CF (1<<0)
269
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300270#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
271#define EFLG_RESERVED_ONE_MASK 2
272
Nadav Amit3dc4bc42014-12-25 02:52:19 +0200273enum x86_transfer_type {
274 X86_TRANSFER_NONE,
275 X86_TRANSFER_CALL_JMP,
276 X86_TRANSFER_RET,
277 X86_TRANSFER_TASK_SWITCH,
278};
279
Avi Kivitydd856ef2012-08-27 23:46:17 +0300280static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
281{
282 if (!(ctxt->regs_valid & (1 << nr))) {
283 ctxt->regs_valid |= 1 << nr;
284 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
285 }
286 return ctxt->_regs[nr];
287}
288
289static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
290{
291 ctxt->regs_valid |= 1 << nr;
292 ctxt->regs_dirty |= 1 << nr;
293 return &ctxt->_regs[nr];
294}
295
296static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
297{
298 reg_read(ctxt, nr);
299 return reg_write(ctxt, nr);
300}
301
302static void writeback_registers(struct x86_emulate_ctxt *ctxt)
303{
304 unsigned reg;
305
306 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
307 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
308}
309
310static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
311{
312 ctxt->regs_dirty = 0;
313 ctxt->regs_valid = 0;
314}
315
Avi Kivity6aa8b732006-12-10 02:21:36 -0800316/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800317 * These EFLAGS bits are restored from saved value during emulation, and
318 * any changes are written back to the saved value after emulation.
319 */
320#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
321
Avi Kivitydda96d82008-11-26 15:14:10 +0200322#ifdef CONFIG_X86_64
323#define ON64(x) x
324#else
325#define ON64(x)
326#endif
327
Avi Kivity4d758342013-01-19 19:51:55 +0200328static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *));
329
Avi Kivityb7d491e2013-01-04 16:18:49 +0200330#define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t"
331#define FOP_RET "ret \n\t"
332
333#define FOP_START(op) \
334 extern void em_##op(struct fastop *fake); \
335 asm(".pushsection .text, \"ax\" \n\t" \
336 ".global em_" #op " \n\t" \
337 FOP_ALIGN \
338 "em_" #op ": \n\t"
339
340#define FOP_END \
341 ".popsection")
342
Avi Kivity0bdea062013-01-19 19:51:50 +0200343#define FOPNOP() FOP_ALIGN FOP_RET
344
Avi Kivityb7d491e2013-01-04 16:18:49 +0200345#define FOP1E(op, dst) \
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200346 FOP_ALIGN "10: " #op " %" #dst " \n\t" FOP_RET
347
348#define FOP1EEX(op, dst) \
349 FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200350
351#define FASTOP1(op) \
352 FOP_START(op) \
353 FOP1E(op##b, al) \
354 FOP1E(op##w, ax) \
355 FOP1E(op##l, eax) \
356 ON64(FOP1E(op##q, rax)) \
357 FOP_END
358
Avi Kivityb9fa4092013-02-09 11:31:48 +0200359/* 1-operand, using src2 (for MUL/DIV r/m) */
360#define FASTOP1SRC2(op, name) \
361 FOP_START(name) \
362 FOP1E(op, cl) \
363 FOP1E(op, cx) \
364 FOP1E(op, ecx) \
365 ON64(FOP1E(op, rcx)) \
366 FOP_END
367
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200368/* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */
369#define FASTOP1SRC2EX(op, name) \
370 FOP_START(name) \
371 FOP1EEX(op, cl) \
372 FOP1EEX(op, cx) \
373 FOP1EEX(op, ecx) \
374 ON64(FOP1EEX(op, rcx)) \
375 FOP_END
376
Avi Kivityf7857f32013-01-04 16:18:53 +0200377#define FOP2E(op, dst, src) \
378 FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET
379
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) \
416 FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
417
418/* 3-operand, word-only, src2=cl */
419#define FASTOP3WCL(op) \
420 FOP_START(op) \
421 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200422 FOP3E(op##w, ax, dx, cl) \
423 FOP3E(op##l, eax, edx, cl) \
424 ON64(FOP3E(op##q, rax, rdx, cl)) \
Avi Kivity0bdea062013-01-19 19:51:50 +0200425 FOP_END
426
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200427/* Special case for SETcc - 1 instruction per cc */
428#define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t"
429
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200430asm(".global kvm_fastop_exception \n"
431 "kvm_fastop_exception: xor %esi, %esi; ret");
432
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200433FOP_START(setcc)
434FOP_SETCC(seto)
435FOP_SETCC(setno)
436FOP_SETCC(setc)
437FOP_SETCC(setnc)
438FOP_SETCC(setz)
439FOP_SETCC(setnz)
440FOP_SETCC(setbe)
441FOP_SETCC(setnbe)
442FOP_SETCC(sets)
443FOP_SETCC(setns)
444FOP_SETCC(setp)
445FOP_SETCC(setnp)
446FOP_SETCC(setl)
447FOP_SETCC(setnl)
448FOP_SETCC(setle)
449FOP_SETCC(setnle)
450FOP_END;
451
Paolo Bonzini326f5782013-05-09 11:32:51 +0200452FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET
453FOP_END;
454
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200455static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
456 enum x86_intercept intercept,
457 enum x86_intercept_stage stage)
458{
459 struct x86_instruction_info info = {
460 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300461 .rep_prefix = ctxt->rep_prefix,
462 .modrm_mod = ctxt->modrm_mod,
463 .modrm_reg = ctxt->modrm_reg,
464 .modrm_rm = ctxt->modrm_rm,
465 .src_val = ctxt->src.val64,
Jan Kiszka6cbc5f52014-06-30 12:52:55 +0200466 .dst_val = ctxt->dst.val64,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300467 .src_bytes = ctxt->src.bytes,
468 .dst_bytes = ctxt->dst.bytes,
469 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200470 .next_rip = ctxt->eip,
471 };
472
Avi Kivity29535382011-04-20 13:37:53 +0300473 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200474}
475
Avi Kivityf47cfa32012-06-07 17:49:24 +0300476static void assign_masked(ulong *dest, ulong src, ulong mask)
477{
478 *dest = (*dest & ~mask) | (src & mask);
479}
480
Avi Kivity9dac77f2011-06-01 15:34:25 +0300481static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800482{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300483 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800484}
485
Avi Kivityf47cfa32012-06-07 17:49:24 +0300486static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
487{
488 u16 sel;
489 struct desc_struct ss;
490
491 if (ctxt->mode == X86EMUL_MODE_PROT64)
492 return ~0UL;
493 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
494 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
495}
496
Avi Kivity612e89f2012-06-12 20:03:23 +0300497static int stack_size(struct x86_emulate_ctxt *ctxt)
498{
499 return (__fls(stack_mask(ctxt)) + 1) >> 3;
500}
501
Avi Kivity6aa8b732006-12-10 02:21:36 -0800502/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800503static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300504address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800505{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300506 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800507 return reg;
508 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300509 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800510}
511
512static inline unsigned long
Paolo Bonzini01485a22014-11-19 18:25:08 +0100513register_address(struct x86_emulate_ctxt *ctxt, int reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800514{
Paolo Bonzini01485a22014-11-19 18:25:08 +0100515 return address_mask(ctxt, reg_read(ctxt, reg));
Harvey Harrisone4706772008-02-19 07:40:38 -0800516}
517
Avi Kivity5ad105e2012-08-19 14:34:31 +0300518static void masked_increment(ulong *reg, ulong mask, int inc)
519{
520 assign_masked(reg, *reg + inc, mask);
521}
522
Harvey Harrison7a9572752008-02-19 07:40:41 -0800523static inline void
Paolo Bonzini01485a22014-11-19 18:25:08 +0100524register_address_increment(struct x86_emulate_ctxt *ctxt, int reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800525{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300526 ulong mask;
527
Avi Kivity9dac77f2011-06-01 15:34:25 +0300528 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300529 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800530 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300531 mask = ad_mask(ctxt);
Paolo Bonzini01485a22014-11-19 18:25:08 +0100532 masked_increment(reg_rmw(ctxt, reg), mask, inc);
Avi Kivity5ad105e2012-08-19 14:34:31 +0300533}
534
535static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
536{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300537 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800538}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800539
Avi Kivity56697682011-04-03 14:08:51 +0300540static u32 desc_limit_scaled(struct desc_struct *desc)
541{
542 u32 limit = get_desc_limit(desc);
543
544 return desc->g ? (limit << 12) | 0xfff : limit;
545}
546
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900547static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300548{
549 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
550 return 0;
551
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900552 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300553}
554
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200555static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
556 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300557{
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +0200558 WARN_ON(vec > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +0200559 ctxt->exception.vector = vec;
560 ctxt->exception.error_code = error;
561 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200562 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300563}
564
Joerg Roedel3b88e412011-04-04 12:39:29 +0200565static int emulate_db(struct x86_emulate_ctxt *ctxt)
566{
567 return emulate_exception(ctxt, DB_VECTOR, 0, false);
568}
569
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200570static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300571{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200572 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300573}
574
Avi Kivity618ff152011-04-03 12:32:09 +0300575static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
576{
577 return emulate_exception(ctxt, SS_VECTOR, err, true);
578}
579
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200580static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300581{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200582 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300583}
584
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200585static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300586{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200587 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300588}
589
Avi Kivity34d1f492010-08-26 11:59:01 +0300590static int emulate_de(struct x86_emulate_ctxt *ctxt)
591{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200592 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300593}
594
Avi Kivity1253791d2011-03-29 11:41:27 +0200595static int emulate_nm(struct x86_emulate_ctxt *ctxt)
596{
597 return emulate_exception(ctxt, NM_VECTOR, 0, false);
598}
599
Avi Kivity1aa36612011-04-27 13:20:30 +0300600static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
601{
602 u16 selector;
603 struct desc_struct desc;
604
605 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
606 return selector;
607}
608
609static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
610 unsigned seg)
611{
612 u16 dummy;
613 u32 base3;
614 struct desc_struct desc;
615
616 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
617 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
618}
619
Avi Kivity1c11b372012-04-09 18:39:59 +0300620/*
621 * x86 defines three classes of vector instructions: explicitly
622 * aligned, explicitly unaligned, and the rest, which change behaviour
623 * depending on whether they're AVX encoded or not.
624 *
625 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
626 * subject to the same check.
627 */
628static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
629{
630 if (likely(size < 16))
631 return false;
632
633 if (ctxt->d & Aligned)
634 return true;
635 else if (ctxt->d & Unaligned)
636 return false;
637 else if (ctxt->d & Avx)
638 return false;
639 else
640 return true;
641}
642
Paolo Bonzinid09155d2014-10-27 14:54:44 +0100643static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
644 struct segmented_address addr,
645 unsigned *max_size, unsigned size,
646 bool write, bool fetch,
Nadav Amitd50eaa12014-11-19 17:43:11 +0200647 enum x86emul_mode mode, ulong *linear)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300648{
Avi Kivity618ff152011-04-03 12:32:09 +0300649 struct desc_struct desc;
650 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300651 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300652 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300653 u16 sel;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300654
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900655 la = seg_base(ctxt, addr.seg) + addr.ea;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100656 *max_size = 0;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200657 switch (mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300658 case X86EMUL_MODE_PROT64:
Nadav Amit4be4de72014-09-18 22:39:40 +0300659 if (is_noncanonical_address(la))
Nadav Amitabc7d8a2014-11-19 17:43:12 +0200660 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100661
662 *max_size = min_t(u64, ~0u, (1ull << 48) - la);
663 if (size > *max_size)
664 goto bad;
Avi Kivity618ff152011-04-03 12:32:09 +0300665 break;
666 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300667 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
668 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300669 if (!usable)
670 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200671 /* code segment in protected mode or read-only data segment */
672 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
673 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300674 goto bad;
675 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400676 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300677 goto bad;
678 lim = desc_limit_scaled(&desc);
Paolo Bonzini997b0412014-11-19 18:33:38 +0100679 if (!(desc.type & 8) && (desc.type & 4)) {
Guo Chaofc058682012-06-28 15:19:51 +0800680 /* expand-down segment */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100681 if (addr.ea <= lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300682 goto bad;
683 lim = desc.d ? 0xffffffff : 0xffff;
Avi Kivity618ff152011-04-03 12:32:09 +0300684 }
Paolo Bonzini997b0412014-11-19 18:33:38 +0100685 if (addr.ea > lim)
686 goto bad;
Nadav Amitbac155312015-01-26 09:32:26 +0200687 if (lim == 0xffffffff)
688 *max_size = ~0u;
689 else {
690 *max_size = (u64)lim + 1 - addr.ea;
691 if (size > *max_size)
692 goto bad;
693 }
Nadav Amit31ff6482014-11-19 17:43:13 +0200694 la &= (u32)-1;
Avi Kivity618ff152011-04-03 12:32:09 +0300695 break;
696 }
Avi Kivity1c11b372012-04-09 18:39:59 +0300697 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
698 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300699 *linear = la;
700 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300701bad:
702 if (addr.seg == VCPU_SREG_SS)
Paolo Bonzini36061892014-10-27 14:40:49 +0100703 return emulate_ss(ctxt, 0);
Avi Kivity618ff152011-04-03 12:32:09 +0300704 else
Paolo Bonzini36061892014-10-27 14:40:49 +0100705 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300706}
707
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400708static int linearize(struct x86_emulate_ctxt *ctxt,
709 struct segmented_address addr,
710 unsigned size, bool write,
711 ulong *linear)
712{
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100713 unsigned max_size;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200714 return __linearize(ctxt, addr, &max_size, size, write, false,
715 ctxt->mode, linear);
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400716}
717
Nadav Amitd50eaa12014-11-19 17:43:11 +0200718static inline int assign_eip(struct x86_emulate_ctxt *ctxt, ulong dst,
719 enum x86emul_mode mode)
720{
721 ulong linear;
722 int rc;
723 unsigned max_size;
724 struct segmented_address addr = { .seg = VCPU_SREG_CS,
725 .ea = dst };
726
727 if (ctxt->op_bytes != sizeof(unsigned long))
728 addr.ea = dst & ((1UL << (ctxt->op_bytes << 3)) - 1);
729 rc = __linearize(ctxt, addr, &max_size, 1, false, true, mode, &linear);
730 if (rc == X86EMUL_CONTINUE)
731 ctxt->_eip = addr.ea;
732 return rc;
733}
734
735static inline int assign_eip_near(struct x86_emulate_ctxt *ctxt, ulong dst)
736{
737 return assign_eip(ctxt, dst, ctxt->mode);
738}
739
740static int assign_eip_far(struct x86_emulate_ctxt *ctxt, ulong dst,
741 const struct desc_struct *cs_desc)
742{
743 enum x86emul_mode mode = ctxt->mode;
Nadav Amit82268082015-01-26 09:32:27 +0200744 int rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200745
746#ifdef CONFIG_X86_64
Nadav Amit82268082015-01-26 09:32:27 +0200747 if (ctxt->mode >= X86EMUL_MODE_PROT16) {
748 if (cs_desc->l) {
749 u64 efer = 0;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200750
Nadav Amit82268082015-01-26 09:32:27 +0200751 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
752 if (efer & EFER_LMA)
753 mode = X86EMUL_MODE_PROT64;
754 } else
755 mode = X86EMUL_MODE_PROT32; /* temporary value */
Nadav Amitd50eaa12014-11-19 17:43:11 +0200756 }
757#endif
758 if (mode == X86EMUL_MODE_PROT16 || mode == X86EMUL_MODE_PROT32)
759 mode = cs_desc->d ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
Nadav Amit82268082015-01-26 09:32:27 +0200760 rc = assign_eip(ctxt, dst, mode);
761 if (rc == X86EMUL_CONTINUE)
762 ctxt->mode = mode;
763 return rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200764}
765
766static inline int jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
767{
768 return assign_eip_near(ctxt, ctxt->_eip + rel);
769}
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400770
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200771static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
772 struct segmented_address addr,
773 void *data,
774 unsigned size)
775{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200776 int rc;
777 ulong linear;
778
Avi Kivity83b87952011-04-03 11:31:19 +0300779 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200780 if (rc != X86EMUL_CONTINUE)
781 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300782 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200783}
784
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900785/*
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200786 * Prefetch the remaining bytes of the instruction without crossing page
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900787 * boundary if they are not in fetch_cache yet.
788 */
Paolo Bonzini9506d572014-05-06 13:05:25 +0200789static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
Avi Kivity62266862007-11-20 13:15:52 +0200790{
Avi Kivity62266862007-11-20 13:15:52 +0200791 int rc;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100792 unsigned size, max_size;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200793 unsigned long linear;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200794 int cur_size = ctxt->fetch.end - ctxt->fetch.data;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200795 struct segmented_address addr = { .seg = VCPU_SREG_CS,
Paolo Bonzini17052f12014-05-06 16:33:01 +0200796 .ea = ctxt->eip + cur_size };
797
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100798 /*
799 * We do not know exactly how many bytes will be needed, and
800 * __linearize is expensive, so fetch as much as possible. We
801 * just have to avoid going beyond the 15 byte limit, the end
802 * of the segment, or the end of the page.
803 *
804 * __linearize is called with size 0 so that it does not do any
805 * boundary check itself. Instead, we use max_size to check
806 * against op_size.
807 */
Nadav Amitd50eaa12014-11-19 17:43:11 +0200808 rc = __linearize(ctxt, addr, &max_size, 0, false, true, ctxt->mode,
809 &linear);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200810 if (unlikely(rc != X86EMUL_CONTINUE))
811 return rc;
812
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100813 size = min_t(unsigned, 15UL ^ cur_size, max_size);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200814 size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear));
Paolo Bonzini5cfc7e02014-05-06 13:05:25 +0200815
816 /*
817 * One instruction can only straddle two pages,
818 * and one has been loaded at the beginning of
819 * x86_decode_insn. So, if not enough bytes
820 * still, we must have hit the 15-byte boundary.
821 */
822 if (unlikely(size < op_size))
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100823 return emulate_gp(ctxt, 0);
824
Paolo Bonzini17052f12014-05-06 16:33:01 +0200825 rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end,
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200826 size, &ctxt->exception);
827 if (unlikely(rc != X86EMUL_CONTINUE))
828 return rc;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200829 ctxt->fetch.end += size;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900830 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200831}
832
Paolo Bonzini9506d572014-05-06 13:05:25 +0200833static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt,
834 unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200835{
Nadav Amit08da44a2014-10-03 01:10:04 +0300836 unsigned done_size = ctxt->fetch.end - ctxt->fetch.ptr;
837
838 if (unlikely(done_size < size))
839 return __do_insn_fetch_bytes(ctxt, size - done_size);
Paolo Bonzini9506d572014-05-06 13:05:25 +0200840 else
841 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200842}
843
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900844/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900845#define insn_fetch(_type, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200846({ _type _x; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200847 \
848 rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900849 if (rc != X86EMUL_CONTINUE) \
850 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200851 ctxt->_eip += sizeof(_type); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200852 _x = *(_type __aligned(1) *) ctxt->fetch.ptr; \
853 ctxt->fetch.ptr += sizeof(_type); \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200854 _x; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900855})
856
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900857#define insn_fetch_arr(_arr, _size, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200858({ \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200859 rc = do_insn_fetch_bytes(_ctxt, _size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900860 if (rc != X86EMUL_CONTINUE) \
861 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200862 ctxt->_eip += (_size); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200863 memcpy(_arr, ctxt->fetch.ptr, _size); \
864 ctxt->fetch.ptr += (_size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900865})
866
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000867/*
868 * Given the 'reg' portion of a ModRM byte, and a register block, return a
869 * pointer into the block that addresses the relevant register.
870 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
871 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300872static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200873 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800874{
875 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200876 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800877
Avi Kivity6aa8b732006-12-10 02:21:36 -0800878 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300879 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
880 else
881 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800882 return p;
883}
884
885static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200886 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800887 u16 *size, unsigned long *address, int op_bytes)
888{
889 int rc;
890
891 if (op_bytes == 2)
892 op_bytes = 3;
893 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200894 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900895 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800896 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200897 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200898 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899 return rc;
900}
901
Avi Kivity34b77652013-01-19 19:51:56 +0200902FASTOP2(add);
903FASTOP2(or);
904FASTOP2(adc);
905FASTOP2(sbb);
906FASTOP2(and);
907FASTOP2(sub);
908FASTOP2(xor);
909FASTOP2(cmp);
910FASTOP2(test);
911
Avi Kivityb9fa4092013-02-09 11:31:48 +0200912FASTOP1SRC2(mul, mul_ex);
913FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200914FASTOP1SRC2EX(div, div_ex);
915FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +0200916
Avi Kivity34b77652013-01-19 19:51:56 +0200917FASTOP3WCL(shld);
918FASTOP3WCL(shrd);
919
920FASTOP2W(imul);
921
922FASTOP1(not);
923FASTOP1(neg);
924FASTOP1(inc);
925FASTOP1(dec);
926
927FASTOP2CL(rol);
928FASTOP2CL(ror);
929FASTOP2CL(rcl);
930FASTOP2CL(rcr);
931FASTOP2CL(shl);
932FASTOP2CL(shr);
933FASTOP2CL(sar);
934
935FASTOP2W(bsf);
936FASTOP2W(bsr);
937FASTOP2W(bt);
938FASTOP2W(bts);
939FASTOP2W(btr);
940FASTOP2W(btc);
941
Avi Kivitye47a5f52013-02-09 11:31:51 +0200942FASTOP2(xadd);
943
Nadav Amit5aca3722014-11-02 11:54:50 +0200944FASTOP2R(cmp, cmp_r);
945
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200946static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300947{
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200948 u8 rc;
949 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300950
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200951 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +0200952 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200953 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
954 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300955}
956
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300957static void fetch_register_operand(struct operand *op)
958{
959 switch (op->bytes) {
960 case 1:
961 op->val = *(u8 *)op->addr.reg;
962 break;
963 case 2:
964 op->val = *(u16 *)op->addr.reg;
965 break;
966 case 4:
967 op->val = *(u32 *)op->addr.reg;
968 break;
969 case 8:
970 op->val = *(u64 *)op->addr.reg;
971 break;
972 }
973}
974
Avi Kivity1253791d2011-03-29 11:41:27 +0200975static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
976{
977 ctxt->ops->get_fpu(ctxt);
978 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200979 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
980 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
981 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
982 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
983 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
984 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
985 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
986 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200987#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200988 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
989 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
990 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
991 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
992 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
993 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
994 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
995 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200996#endif
997 default: BUG();
998 }
999 ctxt->ops->put_fpu(ctxt);
1000}
1001
1002static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
1003 int reg)
1004{
1005 ctxt->ops->get_fpu(ctxt);
1006 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001007 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
1008 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
1009 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
1010 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
1011 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
1012 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
1013 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
1014 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001015#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001016 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
1017 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
1018 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
1019 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
1020 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
1021 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
1022 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
1023 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001024#endif
1025 default: BUG();
1026 }
1027 ctxt->ops->put_fpu(ctxt);
1028}
1029
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001030static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1031{
1032 ctxt->ops->get_fpu(ctxt);
1033 switch (reg) {
1034 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1035 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1036 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1037 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1038 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1039 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1040 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1041 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1042 default: BUG();
1043 }
1044 ctxt->ops->put_fpu(ctxt);
1045}
1046
1047static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1048{
1049 ctxt->ops->get_fpu(ctxt);
1050 switch (reg) {
1051 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1052 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1053 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1054 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1055 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1056 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1057 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1058 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1059 default: BUG();
1060 }
1061 ctxt->ops->put_fpu(ctxt);
1062}
1063
Gleb Natapov045a2822012-12-20 16:57:43 +02001064static int em_fninit(struct x86_emulate_ctxt *ctxt)
1065{
1066 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1067 return emulate_nm(ctxt);
1068
1069 ctxt->ops->get_fpu(ctxt);
1070 asm volatile("fninit");
1071 ctxt->ops->put_fpu(ctxt);
1072 return X86EMUL_CONTINUE;
1073}
1074
1075static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1076{
1077 u16 fcw;
1078
1079 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1080 return emulate_nm(ctxt);
1081
1082 ctxt->ops->get_fpu(ctxt);
1083 asm volatile("fnstcw %0": "+m"(fcw));
1084 ctxt->ops->put_fpu(ctxt);
1085
Gleb Natapov045a2822012-12-20 16:57:43 +02001086 ctxt->dst.val = fcw;
1087
1088 return X86EMUL_CONTINUE;
1089}
1090
1091static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1092{
1093 u16 fsw;
1094
1095 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1096 return emulate_nm(ctxt);
1097
1098 ctxt->ops->get_fpu(ctxt);
1099 asm volatile("fnstsw %0": "+m"(fsw));
1100 ctxt->ops->put_fpu(ctxt);
1101
Gleb Natapov045a2822012-12-20 16:57:43 +02001102 ctxt->dst.val = fsw;
1103
1104 return X86EMUL_CONTINUE;
1105}
1106
Avi Kivity1253791d2011-03-29 11:41:27 +02001107static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001108 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001109{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001110 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001111
Avi Kivity9dac77f2011-06-01 15:34:25 +03001112 if (!(ctxt->d & ModRM))
1113 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity1253791d2011-03-29 11:41:27 +02001114
Avi Kivity9dac77f2011-06-01 15:34:25 +03001115 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001116 op->type = OP_XMM;
1117 op->bytes = 16;
1118 op->addr.xmm = reg;
1119 read_sse_reg(ctxt, &op->vec_val, reg);
1120 return;
1121 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001122 if (ctxt->d & Mmx) {
1123 reg &= 7;
1124 op->type = OP_MM;
1125 op->bytes = 8;
1126 op->addr.mm = reg;
1127 return;
1128 }
Avi Kivity1253791d2011-03-29 11:41:27 +02001129
Avi Kivity3c118e22007-10-31 10:27:04 +02001130 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001131 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1132 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1133
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001134 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001135 op->orig_val = op->val;
1136}
1137
Avi Kivitya6e34072012-06-10 17:15:39 +03001138static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1139{
1140 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1141 ctxt->modrm_seg = VCPU_SREG_SS;
1142}
1143
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001144static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001145 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001146{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001147 u8 sib;
Bandan Das02357bd2014-04-16 12:46:11 -04001148 int index_reg, base_reg, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001149 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001150 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001151
Bandan Das02357bd2014-04-16 12:46:11 -04001152 ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */
1153 index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */
1154 base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001155
Bandan Das02357bd2014-04-16 12:46:11 -04001156 ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001157 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
Bandan Das02357bd2014-04-16 12:46:11 -04001158 ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001159 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001160
Nadav Amit9b88ae92014-05-25 23:05:21 +03001161 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001162 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001163 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001164 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001165 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001166 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001167 op->type = OP_XMM;
1168 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001169 op->addr.xmm = ctxt->modrm_rm;
1170 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001171 return rc;
1172 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001173 if (ctxt->d & Mmx) {
1174 op->type = OP_MM;
1175 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001176 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001177 return rc;
1178 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001179 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001180 return rc;
1181 }
1182
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001183 op->type = OP_MEM;
1184
Avi Kivity9dac77f2011-06-01 15:34:25 +03001185 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001186 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1187 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1188 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1189 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001190
1191 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001192 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001193 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001194 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001195 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001196 break;
1197 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001198 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001199 break;
1200 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001201 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001202 break;
1203 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001204 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001205 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001206 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001207 break;
1208 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001209 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001210 break;
1211 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001212 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001213 break;
1214 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001215 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001216 break;
1217 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001218 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001219 break;
1220 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001221 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001222 break;
1223 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001224 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001225 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001226 break;
1227 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001228 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001229 break;
1230 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001231 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1232 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1233 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001234 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001235 } else {
1236 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001237 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001238 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001239 index_reg |= (sib >> 3) & 7;
1240 base_reg |= sib & 7;
1241 scale = sib >> 6;
1242
Avi Kivity9dac77f2011-06-01 15:34:25 +03001243 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001244 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001245 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001246 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001247 adjust_modrm_seg(ctxt, base_reg);
Nadav Amitab708092014-12-25 02:52:21 +02001248 /* Increment ESP on POP [ESP] */
1249 if ((ctxt->d & IncSP) &&
1250 base_reg == VCPU_REGS_RSP)
1251 modrm_ea += ctxt->op_bytes;
Avi Kivitya6e34072012-06-10 17:15:39 +03001252 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001253 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001254 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001255 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Nadav Amit5b38ab82014-11-02 11:54:41 +02001256 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity84411d82008-06-15 21:53:26 -07001257 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001258 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001259 } else {
1260 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001261 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001262 adjust_modrm_seg(ctxt, base_reg);
1263 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001264 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001265 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001266 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001267 break;
1268 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001269 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001270 break;
1271 }
1272 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001273 op->addr.mem.ea = modrm_ea;
Bandan Das41061cd2014-04-16 12:46:14 -04001274 if (ctxt->ad_bytes != 8)
1275 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
1276
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001277done:
1278 return rc;
1279}
1280
1281static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001282 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001283{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001284 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001285
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001286 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001287 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001288 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001289 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001290 break;
1291 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001292 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001293 break;
1294 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001295 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001296 break;
1297 }
1298done:
1299 return rc;
1300}
1301
Avi Kivity9dac77f2011-06-01 15:34:25 +03001302static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001303{
Sheng Yang7129eec2010-09-28 16:33:32 +08001304 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001305
Avi Kivity9dac77f2011-06-01 15:34:25 +03001306 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001307 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001308
Avi Kivity9dac77f2011-06-01 15:34:25 +03001309 if (ctxt->src.bytes == 2)
1310 sv = (s16)ctxt->src.val & (s16)mask;
1311 else if (ctxt->src.bytes == 4)
1312 sv = (s32)ctxt->src.val & (s32)mask;
Nadav Amit7dec5602014-06-15 16:12:57 +03001313 else
1314 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001315
Nadav Amit1c1c35a2014-11-19 17:43:09 +02001316 ctxt->dst.addr.mem.ea = address_mask(ctxt,
1317 ctxt->dst.addr.mem.ea + (sv >> 3));
Wei Yongjun35c843c2010-08-09 11:34:56 +08001318 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001319
1320 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001321 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001322}
1323
Gleb Natapov9de41572010-04-28 19:15:22 +03001324static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001325 unsigned long addr, void *dest, unsigned size)
1326{
1327 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001328 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001329
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001330 if (mc->pos < mc->end)
1331 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001332
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001333 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001334
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001335 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1336 &ctxt->exception);
1337 if (rc != X86EMUL_CONTINUE)
1338 return rc;
1339
1340 mc->end += size;
1341
1342read_cached:
1343 memcpy(dest, mc->data + mc->pos, size);
1344 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001345 return X86EMUL_CONTINUE;
1346}
1347
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001348static int segmented_read(struct x86_emulate_ctxt *ctxt,
1349 struct segmented_address addr,
1350 void *data,
1351 unsigned size)
1352{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001353 int rc;
1354 ulong linear;
1355
Avi Kivity83b87952011-04-03 11:31:19 +03001356 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001357 if (rc != X86EMUL_CONTINUE)
1358 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001359 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001360}
1361
1362static int segmented_write(struct x86_emulate_ctxt *ctxt,
1363 struct segmented_address addr,
1364 const void *data,
1365 unsigned size)
1366{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001367 int rc;
1368 ulong linear;
1369
Avi Kivity83b87952011-04-03 11:31:19 +03001370 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001371 if (rc != X86EMUL_CONTINUE)
1372 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001373 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1374 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001375}
1376
1377static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1378 struct segmented_address addr,
1379 const void *orig_data, const void *data,
1380 unsigned size)
1381{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001382 int rc;
1383 ulong linear;
1384
Avi Kivity83b87952011-04-03 11:31:19 +03001385 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001386 if (rc != X86EMUL_CONTINUE)
1387 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001388 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1389 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001390}
1391
Gleb Natapov7b262e92010-03-18 15:20:27 +02001392static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001393 unsigned int size, unsigned short port,
1394 void *dest)
1395{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001396 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001397
1398 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001399 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001400 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001401 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001402 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001403 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1404 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Mark Rustadb55a8142014-07-25 06:27:05 -07001405 n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001406 if (n == 0)
1407 n = 1;
1408 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001409 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001410 return 0;
1411 rc->end = n * size;
1412 }
1413
Nadav Amite6e39f02014-04-18 03:35:10 +03001414 if (ctxt->rep_prefix && (ctxt->d & String) &&
1415 !(ctxt->eflags & EFLG_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001416 ctxt->dst.data = rc->data + rc->pos;
1417 ctxt->dst.type = OP_MEM_STR;
1418 ctxt->dst.count = (rc->end - rc->pos) / size;
1419 rc->pos = rc->end;
1420 } else {
1421 memcpy(dest, rc->data + rc->pos, size);
1422 rc->pos += size;
1423 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001424 return 1;
1425}
1426
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001427static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1428 u16 index, struct desc_struct *desc)
1429{
1430 struct desc_ptr dt;
1431 ulong addr;
1432
1433 ctxt->ops->get_idt(ctxt, &dt);
1434
1435 if (dt.size < index * 8 + 7)
1436 return emulate_gp(ctxt, index << 3 | 0x2);
1437
1438 addr = dt.address + index * 8;
1439 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1440 &ctxt->exception);
1441}
1442
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001443static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001444 u16 selector, struct desc_ptr *dt)
1445{
Mathias Krause0225fb52012-08-30 01:30:16 +02001446 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001447 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001448
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001449 if (selector & 1 << 2) {
1450 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001451 u16 sel;
1452
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001453 memset (dt, 0, sizeof *dt);
Nadav Amit2eedcac2014-06-02 18:34:05 +03001454 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1455 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001456 return;
1457
1458 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001459 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001460 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001461 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001462}
1463
Nadav Amitedccda72014-12-25 02:52:23 +02001464static int get_descriptor_ptr(struct x86_emulate_ctxt *ctxt,
1465 u16 selector, ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001466{
1467 struct desc_ptr dt;
1468 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001469 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001470
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001471 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001472
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001473 if (dt.size < index * 8 + 7)
1474 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001475
1476 addr = dt.address + index * 8;
Nadav Amitedccda72014-12-25 02:52:23 +02001477
1478#ifdef CONFIG_X86_64
1479 if (addr >> 32 != 0) {
1480 u64 efer = 0;
1481
1482 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1483 if (!(efer & EFER_LMA))
1484 addr &= (u32)-1;
1485 }
1486#endif
1487
1488 *desc_addr_p = addr;
1489 return X86EMUL_CONTINUE;
1490}
1491
1492/* allowed just for 8 bytes segments */
1493static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1494 u16 selector, struct desc_struct *desc,
1495 ulong *desc_addr_p)
1496{
1497 int rc;
1498
1499 rc = get_descriptor_ptr(ctxt, selector, desc_addr_p);
1500 if (rc != X86EMUL_CONTINUE)
1501 return rc;
1502
1503 return ctxt->ops->read_std(ctxt, *desc_addr_p, desc, sizeof(*desc),
1504 &ctxt->exception);
1505}
1506
1507/* allowed just for 8 bytes segments */
1508static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1509 u16 selector, struct desc_struct *desc)
1510{
1511 int rc;
1512 ulong addr;
1513
1514 rc = get_descriptor_ptr(ctxt, selector, &addr);
1515 if (rc != X86EMUL_CONTINUE)
1516 return rc;
1517
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001518 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1519 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001520}
1521
Gleb Natapov5601d052011-03-07 14:55:06 +02001522/* Does not support long mode */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001523static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Nadav Amitd1442d82014-09-18 22:39:39 +03001524 u16 selector, int seg, u8 cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001525 enum x86_transfer_type transfer,
Nadav Amitd1442d82014-09-18 22:39:39 +03001526 struct desc_struct *desc)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001527{
Avi Kivity869be992012-06-13 16:30:53 +03001528 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001529 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001530 unsigned err_vec = GP_VECTOR;
1531 u32 err_code = 0;
1532 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001533 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001534 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001535 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001536 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001537
1538 memset(&seg_desc, 0, sizeof seg_desc);
1539
Kevin Wolff8da94e2013-04-11 14:06:03 +02001540 if (ctxt->mode == X86EMUL_MODE_REAL) {
1541 /* set real mode segment descriptor (keep limit etc. for
1542 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001543 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001544 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001545 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001546 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1547 /* VM86 needs a clean new segment descriptor */
1548 set_desc_base(&seg_desc, selector << 4);
1549 set_desc_limit(&seg_desc, 0xffff);
1550 seg_desc.type = 3;
1551 seg_desc.p = 1;
1552 seg_desc.s = 1;
1553 seg_desc.dpl = 3;
1554 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001555 }
1556
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001557 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001558
1559 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1560 if ((seg == VCPU_SREG_CS
1561 || (seg == VCPU_SREG_SS
1562 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1563 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001564 && null_selector)
1565 goto exception;
1566
1567 /* TR should be in GDT only */
1568 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1569 goto exception;
1570
1571 if (null_selector) /* for NULL selector skip all following checks */
1572 goto load;
1573
Avi Kivitye9194642012-06-13 16:29:39 +03001574 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001575 if (ret != X86EMUL_CONTINUE)
1576 return ret;
1577
1578 err_code = selector & 0xfffc;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001579 err_vec = (transfer == X86_TRANSFER_TASK_SWITCH) ? TS_VECTOR :
1580 GP_VECTOR;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001581
Guo Chaofc058682012-06-28 15:19:51 +08001582 /* can't load system descriptor into segment selector */
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001583 if (seg <= VCPU_SREG_GS && !seg_desc.s) {
1584 if (transfer == X86_TRANSFER_CALL_JMP)
1585 return X86EMUL_UNHANDLEABLE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001586 goto exception;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001587 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001588
1589 if (!seg_desc.p) {
1590 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1591 goto exception;
1592 }
1593
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001594 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001595
1596 switch (seg) {
1597 case VCPU_SREG_SS:
1598 /*
1599 * segment is not a writable data segment or segment
1600 * selector's RPL != CPL or segment selector's RPL != CPL
1601 */
1602 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1603 goto exception;
1604 break;
1605 case VCPU_SREG_CS:
1606 if (!(seg_desc.type & 8))
1607 goto exception;
1608
1609 if (seg_desc.type & 4) {
1610 /* conforming */
1611 if (dpl > cpl)
1612 goto exception;
1613 } else {
1614 /* nonconforming */
1615 if (rpl > cpl || dpl != cpl)
1616 goto exception;
1617 }
Nadav Amit040c8dc2014-09-18 22:39:43 +03001618 /* in long-mode d/b must be clear if l is set */
1619 if (seg_desc.d && seg_desc.l) {
1620 u64 efer = 0;
1621
1622 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1623 if (efer & EFER_LMA)
1624 goto exception;
1625 }
1626
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001627 /* CS(RPL) <- CPL */
1628 selector = (selector & 0xfffc) | cpl;
1629 break;
1630 case VCPU_SREG_TR:
1631 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1632 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001633 old_desc = seg_desc;
1634 seg_desc.type |= 2; /* busy */
1635 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1636 sizeof(seg_desc), &ctxt->exception);
1637 if (ret != X86EMUL_CONTINUE)
1638 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001639 break;
1640 case VCPU_SREG_LDTR:
1641 if (seg_desc.s || seg_desc.type != 2)
1642 goto exception;
1643 break;
1644 default: /* DS, ES, FS, or GS */
1645 /*
1646 * segment is not a data or readable code segment or
1647 * ((segment is a data or nonconforming code segment)
1648 * and (both RPL and CPL > DPL))
1649 */
1650 if ((seg_desc.type & 0xa) == 0x8 ||
1651 (((seg_desc.type & 0xc) != 0xc) &&
1652 (rpl > dpl && cpl > dpl)))
1653 goto exception;
1654 break;
1655 }
1656
1657 if (seg_desc.s) {
1658 /* mark segment as accessed */
Nadav Amite2cefa72014-12-25 02:52:22 +02001659 if (!(seg_desc.type & 1)) {
1660 seg_desc.type |= 1;
1661 ret = write_segment_descriptor(ctxt, selector,
1662 &seg_desc);
1663 if (ret != X86EMUL_CONTINUE)
1664 return ret;
1665 }
Nadav Amite37a75a2014-06-02 18:34:04 +03001666 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
1667 ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3,
1668 sizeof(base3), &ctxt->exception);
1669 if (ret != X86EMUL_CONTINUE)
1670 return ret;
Nadav Amit9a9abf62014-11-02 11:54:56 +02001671 if (is_noncanonical_address(get_desc_base(&seg_desc) |
1672 ((u64)base3 << 32)))
1673 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001674 }
1675load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001676 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Nadav Amitd1442d82014-09-18 22:39:39 +03001677 if (desc)
1678 *desc = seg_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001679 return X86EMUL_CONTINUE;
1680exception:
Paolo Bonzini592f0852014-08-20 10:05:08 +02001681 return emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001682}
1683
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001684static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1685 u16 selector, int seg)
1686{
1687 u8 cpl = ctxt->ops->cpl(ctxt);
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001688 return __load_segment_descriptor(ctxt, selector, seg, cpl,
1689 X86_TRANSFER_NONE, NULL);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001690}
1691
Wei Yongjun31be40b2010-08-17 09:17:30 +08001692static void write_register_operand(struct operand *op)
1693{
1694 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1695 switch (op->bytes) {
1696 case 1:
1697 *(u8 *)op->addr.reg = (u8)op->val;
1698 break;
1699 case 2:
1700 *(u16 *)op->addr.reg = (u16)op->val;
1701 break;
1702 case 4:
1703 *op->addr.reg = (u32)op->val;
1704 break; /* 64b: zero-extend */
1705 case 8:
1706 *op->addr.reg = op->val;
1707 break;
1708 }
1709}
1710
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001711static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001712{
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001713 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001714 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001715 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001716 break;
1717 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001718 if (ctxt->lock_prefix)
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001719 return segmented_cmpxchg(ctxt,
1720 op->addr.mem,
1721 &op->orig_val,
1722 &op->val,
1723 op->bytes);
1724 else
1725 return segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001726 op->addr.mem,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001727 &op->val,
1728 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001729 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001730 case OP_MEM_STR:
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001731 return segmented_write(ctxt,
1732 op->addr.mem,
1733 op->data,
1734 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001735 break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001736 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001737 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001738 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001739 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001740 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001741 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001742 case OP_NONE:
1743 /* no writeback */
1744 break;
1745 default:
1746 break;
1747 }
1748 return X86EMUL_CONTINUE;
1749}
1750
Avi Kivity51ddff52012-06-12 20:19:40 +03001751static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001752{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001753 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001754
Avi Kivity5ad105e2012-08-19 14:34:31 +03001755 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001756 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001757 addr.seg = VCPU_SREG_SS;
1758
Avi Kivity51ddff52012-06-12 20:19:40 +03001759 return segmented_write(ctxt, addr, data, bytes);
1760}
1761
1762static int em_push(struct x86_emulate_ctxt *ctxt)
1763{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001764 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001765 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001766 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001767}
1768
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001769static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001770 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001771{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001772 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001773 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001774
Avi Kivitydd856ef2012-08-27 23:46:17 +03001775 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001776 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001777 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001778 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001779 return rc;
1780
Avi Kivity5ad105e2012-08-19 14:34:31 +03001781 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001782 return rc;
1783}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001784
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001785static int em_pop(struct x86_emulate_ctxt *ctxt)
1786{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001787 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001788}
1789
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001790static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001791 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001792{
1793 int rc;
1794 unsigned long val, change_mask;
1795 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001796 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001797
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001798 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001799 if (rc != X86EMUL_CONTINUE)
1800 return rc;
1801
1802 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
Nadav Amit163b1352014-07-21 14:37:28 +03001803 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_AC | EFLG_ID;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001804
1805 switch(ctxt->mode) {
1806 case X86EMUL_MODE_PROT64:
1807 case X86EMUL_MODE_PROT32:
1808 case X86EMUL_MODE_PROT16:
1809 if (cpl == 0)
1810 change_mask |= EFLG_IOPL;
1811 if (cpl <= iopl)
1812 change_mask |= EFLG_IF;
1813 break;
1814 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001815 if (iopl < 3)
1816 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001817 change_mask |= EFLG_IF;
1818 break;
1819 default: /* real mode */
1820 change_mask |= (EFLG_IOPL | EFLG_IF);
1821 break;
1822 }
1823
1824 *(unsigned long *)dest =
1825 (ctxt->eflags & ~change_mask) | (val & change_mask);
1826
1827 return rc;
1828}
1829
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001830static int em_popf(struct x86_emulate_ctxt *ctxt)
1831{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001832 ctxt->dst.type = OP_REG;
1833 ctxt->dst.addr.reg = &ctxt->eflags;
1834 ctxt->dst.bytes = ctxt->op_bytes;
1835 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001836}
1837
Avi Kivity612e89f2012-06-12 20:03:23 +03001838static int em_enter(struct x86_emulate_ctxt *ctxt)
1839{
1840 int rc;
1841 unsigned frame_size = ctxt->src.val;
1842 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001843 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001844
1845 if (nesting_level)
1846 return X86EMUL_UNHANDLEABLE;
1847
Avi Kivitydd856ef2012-08-27 23:46:17 +03001848 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1849 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001850 if (rc != X86EMUL_CONTINUE)
1851 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001852 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001853 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001854 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1855 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001856 stack_mask(ctxt));
1857 return X86EMUL_CONTINUE;
1858}
1859
Avi Kivityf47cfa32012-06-07 17:49:24 +03001860static int em_leave(struct x86_emulate_ctxt *ctxt)
1861{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001862 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001863 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001864 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001865}
1866
Avi Kivity1cd196e2011-09-13 10:45:51 +03001867static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001868{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001869 int seg = ctxt->src2.val;
1870
Avi Kivity9dac77f2011-06-01 15:34:25 +03001871 ctxt->src.val = get_segment_selector(ctxt, seg);
Nadav Amit0fcc2072014-11-02 11:54:51 +02001872 if (ctxt->op_bytes == 4) {
1873 rsp_increment(ctxt, -2);
1874 ctxt->op_bytes = 2;
1875 }
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001876
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001877 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001878}
1879
Avi Kivity1cd196e2011-09-13 10:45:51 +03001880static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001881{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001882 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001883 unsigned long selector;
1884 int rc;
1885
Nadav Amit3313bc42014-12-25 02:52:17 +02001886 rc = emulate_pop(ctxt, &selector, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001887 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001888 return rc;
1889
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001890 if (ctxt->modrm_reg == VCPU_SREG_SS)
1891 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Nadav Amit3313bc42014-12-25 02:52:17 +02001892 if (ctxt->op_bytes > 2)
1893 rsp_increment(ctxt, ctxt->op_bytes - 2);
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001894
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001895 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001896 return rc;
1897}
1898
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001899static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001900{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001901 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001902 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001903 int reg = VCPU_REGS_RAX;
1904
1905 while (reg <= VCPU_REGS_RDI) {
1906 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001907 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001908
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001909 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001910 if (rc != X86EMUL_CONTINUE)
1911 return rc;
1912
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001913 ++reg;
1914 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001915
Wei Yongjunc37eda12010-06-15 09:03:33 +08001916 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001917}
1918
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001919static int em_pushf(struct x86_emulate_ctxt *ctxt)
1920{
Nadav Amitbc397a62014-12-10 11:19:03 +02001921 ctxt->src.val = (unsigned long)ctxt->eflags & ~EFLG_VM;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001922 return em_push(ctxt);
1923}
1924
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001925static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001926{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001927 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001928 int reg = VCPU_REGS_RDI;
1929
1930 while (reg >= VCPU_REGS_RAX) {
1931 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001932 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001933 --reg;
1934 }
1935
Avi Kivitydd856ef2012-08-27 23:46:17 +03001936 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001937 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001938 break;
1939 --reg;
1940 }
1941 return rc;
1942}
1943
Avi Kivitydd856ef2012-08-27 23:46:17 +03001944static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001945{
Mathias Krause0225fb52012-08-30 01:30:16 +02001946 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001947 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001948 struct desc_ptr dt;
1949 gva_t cs_addr;
1950 gva_t eip_addr;
1951 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001952
1953 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001954 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001955 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001956 if (rc != X86EMUL_CONTINUE)
1957 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001958
1959 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1960
Avi Kivity9dac77f2011-06-01 15:34:25 +03001961 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001962 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001963 if (rc != X86EMUL_CONTINUE)
1964 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001965
Avi Kivity9dac77f2011-06-01 15:34:25 +03001966 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001967 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001968 if (rc != X86EMUL_CONTINUE)
1969 return rc;
1970
Avi Kivity4bff1e862011-04-20 13:37:53 +03001971 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001972
1973 eip_addr = dt.address + (irq << 2);
1974 cs_addr = dt.address + (irq << 2) + 2;
1975
Avi Kivity0f65dd72011-04-20 13:37:53 +03001976 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001977 if (rc != X86EMUL_CONTINUE)
1978 return rc;
1979
Avi Kivity0f65dd72011-04-20 13:37:53 +03001980 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001981 if (rc != X86EMUL_CONTINUE)
1982 return rc;
1983
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001984 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001985 if (rc != X86EMUL_CONTINUE)
1986 return rc;
1987
Avi Kivity9dac77f2011-06-01 15:34:25 +03001988 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001989
1990 return rc;
1991}
1992
Avi Kivitydd856ef2012-08-27 23:46:17 +03001993int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1994{
1995 int rc;
1996
1997 invalidate_registers(ctxt);
1998 rc = __emulate_int_real(ctxt, irq);
1999 if (rc == X86EMUL_CONTINUE)
2000 writeback_registers(ctxt);
2001 return rc;
2002}
2003
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002004static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002005{
2006 switch(ctxt->mode) {
2007 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03002008 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002009 case X86EMUL_MODE_VM86:
2010 case X86EMUL_MODE_PROT16:
2011 case X86EMUL_MODE_PROT32:
2012 case X86EMUL_MODE_PROT64:
2013 default:
2014 /* Protected mode interrupts unimplemented yet */
2015 return X86EMUL_UNHANDLEABLE;
2016 }
2017}
2018
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002019static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002020{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002021 int rc = X86EMUL_CONTINUE;
2022 unsigned long temp_eip = 0;
2023 unsigned long temp_eflags = 0;
2024 unsigned long cs = 0;
2025 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
2026 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
2027 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
2028 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
2029
2030 /* TODO: Add stack limit check */
2031
Avi Kivity9dac77f2011-06-01 15:34:25 +03002032 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002033
2034 if (rc != X86EMUL_CONTINUE)
2035 return rc;
2036
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002037 if (temp_eip & ~0xffff)
2038 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002039
Avi Kivity9dac77f2011-06-01 15:34:25 +03002040 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002041
2042 if (rc != X86EMUL_CONTINUE)
2043 return rc;
2044
Avi Kivity9dac77f2011-06-01 15:34:25 +03002045 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002046
2047 if (rc != X86EMUL_CONTINUE)
2048 return rc;
2049
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002050 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002051
2052 if (rc != X86EMUL_CONTINUE)
2053 return rc;
2054
Avi Kivity9dac77f2011-06-01 15:34:25 +03002055 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002056
2057
Avi Kivity9dac77f2011-06-01 15:34:25 +03002058 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002059 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03002060 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002061 ctxt->eflags &= ~0xffff;
2062 ctxt->eflags |= temp_eflags;
2063 }
2064
2065 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
2066 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
Nadav Amit801806d2015-01-26 09:32:23 +02002067 ctxt->ops->set_nmi_mask(ctxt, false);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002068
2069 return rc;
2070}
2071
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002072static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002073{
2074 switch(ctxt->mode) {
2075 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002076 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002077 case X86EMUL_MODE_VM86:
2078 case X86EMUL_MODE_PROT16:
2079 case X86EMUL_MODE_PROT32:
2080 case X86EMUL_MODE_PROT64:
2081 default:
2082 /* iret from protected mode unimplemented yet */
2083 return X86EMUL_UNHANDLEABLE;
2084 }
2085}
2086
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002087static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2088{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002089 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002090 unsigned short sel, old_sel;
2091 struct desc_struct old_desc, new_desc;
2092 const struct x86_emulate_ops *ops = ctxt->ops;
2093 u8 cpl = ctxt->ops->cpl(ctxt);
2094
2095 /* Assignment of RIP may only fail in 64-bit mode */
2096 if (ctxt->mode == X86EMUL_MODE_PROT64)
2097 ops->get_segment(ctxt, &old_sel, &old_desc, NULL,
2098 VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002099
Avi Kivity9dac77f2011-06-01 15:34:25 +03002100 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002101
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002102 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
2103 X86_TRANSFER_CALL_JMP,
Nadav Amitd1442d82014-09-18 22:39:39 +03002104 &new_desc);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002105 if (rc != X86EMUL_CONTINUE)
2106 return rc;
2107
Nadav Amitd50eaa12014-11-19 17:43:11 +02002108 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03002109 if (rc != X86EMUL_CONTINUE) {
Nadav Amit7e46ddd2014-10-28 00:03:43 +02002110 WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64);
Nadav Amitd1442d82014-09-18 22:39:39 +03002111 /* assigning eip failed; restore the old cs */
2112 ops->set_segment(ctxt, old_sel, &old_desc, 0, VCPU_SREG_CS);
2113 return rc;
2114 }
2115 return rc;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002116}
2117
Nadav Amitf7784042014-09-18 22:39:41 +03002118static int em_jmp_abs(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002119{
Nadav Amitf7784042014-09-18 22:39:41 +03002120 return assign_eip_near(ctxt, ctxt->src.val);
2121}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002122
Nadav Amitf7784042014-09-18 22:39:41 +03002123static int em_call_near_abs(struct x86_emulate_ctxt *ctxt)
2124{
2125 int rc;
2126 long int old_eip;
2127
2128 old_eip = ctxt->_eip;
2129 rc = assign_eip_near(ctxt, ctxt->src.val);
2130 if (rc != X86EMUL_CONTINUE)
2131 return rc;
2132 ctxt->src.val = old_eip;
2133 rc = em_push(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002134 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002135}
2136
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002137static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002138{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002139 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002140
Nadav Amitaaa05f22014-06-02 18:34:10 +03002141 if (ctxt->dst.bytes == 16)
2142 return X86EMUL_UNHANDLEABLE;
2143
Avi Kivitydd856ef2012-08-27 23:46:17 +03002144 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2145 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2146 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2147 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002148 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002149 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002150 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2151 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002152
Laurent Vivier05f086f2007-09-24 11:10:55 +02002153 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002154 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002155 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002156}
2157
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002158static int em_ret(struct x86_emulate_ctxt *ctxt)
2159{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002160 int rc;
2161 unsigned long eip;
2162
2163 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
2164 if (rc != X86EMUL_CONTINUE)
2165 return rc;
2166
2167 return assign_eip_near(ctxt, eip);
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002168}
2169
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002170static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002171{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002172 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002173 unsigned long eip, cs;
2174 u16 old_cs;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002175 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amitd1442d82014-09-18 22:39:39 +03002176 struct desc_struct old_desc, new_desc;
2177 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002178
Nadav Amitd1442d82014-09-18 22:39:39 +03002179 if (ctxt->mode == X86EMUL_MODE_PROT64)
2180 ops->get_segment(ctxt, &old_cs, &old_desc, NULL,
2181 VCPU_SREG_CS);
2182
2183 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002184 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002185 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002186 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002187 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002188 return rc;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002189 /* Outer-privilege level return is not implemented */
2190 if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2191 return X86EMUL_UNHANDLEABLE;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002192 rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, cpl,
2193 X86_TRANSFER_RET,
Nadav Amitd1442d82014-09-18 22:39:39 +03002194 &new_desc);
2195 if (rc != X86EMUL_CONTINUE)
2196 return rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +02002197 rc = assign_eip_far(ctxt, eip, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03002198 if (rc != X86EMUL_CONTINUE) {
Nadav Amit7e46ddd2014-10-28 00:03:43 +02002199 WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64);
Nadav Amitd1442d82014-09-18 22:39:39 +03002200 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
2201 }
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002202 return rc;
2203}
2204
Bruce Rogers32611072013-09-09 09:40:20 -06002205static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2206{
2207 int rc;
2208
2209 rc = em_ret_far(ctxt);
2210 if (rc != X86EMUL_CONTINUE)
2211 return rc;
2212 rsp_increment(ctxt, ctxt->src.val);
2213 return X86EMUL_CONTINUE;
2214}
2215
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002216static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2217{
2218 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002219 ctxt->dst.orig_val = ctxt->dst.val;
2220 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002221 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002222 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002223 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002224
2225 if (ctxt->eflags & EFLG_ZF) {
Nadav Amit2fcf5c82015-01-26 09:32:21 +02002226 /* Success: write back to memory; no update of EAX */
2227 ctxt->src.type = OP_NONE;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002228 ctxt->dst.val = ctxt->src.orig_val;
2229 } else {
2230 /* Failure: write the value we saw to EAX. */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02002231 ctxt->src.type = OP_REG;
2232 ctxt->src.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
2233 ctxt->src.val = ctxt->dst.orig_val;
2234 /* Create write-cycle to dest by writing the same value */
Nadav Amit37c564f2014-06-02 18:34:07 +03002235 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002236 }
2237 return X86EMUL_CONTINUE;
2238}
2239
Avi Kivityd4b43252011-09-13 10:45:50 +03002240static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002241{
Avi Kivityd4b43252011-09-13 10:45:50 +03002242 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002243 unsigned short sel;
2244 int rc;
2245
Avi Kivity9dac77f2011-06-01 15:34:25 +03002246 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002247
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002248 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002249 if (rc != X86EMUL_CONTINUE)
2250 return rc;
2251
Avi Kivity9dac77f2011-06-01 15:34:25 +03002252 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002253 return rc;
2254}
2255
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002256static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002257setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002258 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002259{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002260 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002261 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002262 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002263 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002264 cs->type = 0x0b; /* Read, Execute, Accessed */
2265 cs->s = 1;
2266 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002267 cs->p = 1;
2268 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002269 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002270
Gleb Natapov79168fd2010-04-28 19:15:30 +03002271 set_desc_base(ss, 0); /* flat segment */
2272 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002273 ss->g = 1; /* 4kb granularity */
2274 ss->s = 1;
2275 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002276 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002277 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002278 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002279 ss->l = 0;
2280 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002281}
2282
Avi Kivity1a18a692012-02-01 12:23:21 +02002283static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2284{
2285 u32 eax, ebx, ecx, edx;
2286
2287 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002288 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2289 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002290 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2291 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2292}
2293
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002294static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2295{
Mathias Krause0225fb52012-08-30 01:30:16 +02002296 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002297 u32 eax, ebx, ecx, edx;
2298
2299 /*
2300 * syscall should always be enabled in longmode - so only become
2301 * vendor specific (cpuid) if other modes are active...
2302 */
2303 if (ctxt->mode == X86EMUL_MODE_PROT64)
2304 return true;
2305
2306 eax = 0x00000000;
2307 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002308 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2309 /*
2310 * Intel ("GenuineIntel")
2311 * remark: Intel CPUs only support "syscall" in 64bit
2312 * longmode. Also an 64bit guest with a
2313 * 32bit compat-app running will #UD !! While this
2314 * behaviour can be fixed (by emulating) into AMD
2315 * response - CPUs of AMD can't behave like Intel.
2316 */
2317 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2318 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2319 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2320 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002321
Avi Kivity0017f932012-06-07 14:10:16 +03002322 /* AMD ("AuthenticAMD") */
2323 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2324 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2325 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2326 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002327
Avi Kivity0017f932012-06-07 14:10:16 +03002328 /* AMD ("AMDisbetter!") */
2329 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2330 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2331 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2332 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002333
2334 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2335 return false;
2336}
2337
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002338static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002339{
Mathias Krause0225fb52012-08-30 01:30:16 +02002340 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002341 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002342 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002343 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002344 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002345
2346 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002347 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002348 ctxt->mode == X86EMUL_MODE_VM86)
2349 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002350
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002351 if (!(em_syscall_is_enabled(ctxt)))
2352 return emulate_ud(ctxt);
2353
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002354 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002355 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002356
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002357 if (!(efer & EFER_SCE))
2358 return emulate_ud(ctxt);
2359
Avi Kivity717746e2011-04-20 13:37:53 +03002360 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002361 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002362 cs_sel = (u16)(msr_data & 0xfffc);
2363 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002364
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002365 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002366 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002367 cs.l = 1;
2368 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002369 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2370 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002371
Avi Kivitydd856ef2012-08-27 23:46:17 +03002372 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002373 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002374#ifdef CONFIG_X86_64
Nadav Amit6c6cb692014-07-21 14:37:30 +03002375 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002376
Avi Kivity717746e2011-04-20 13:37:53 +03002377 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002378 ctxt->mode == X86EMUL_MODE_PROT64 ?
2379 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002380 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002381
Avi Kivity717746e2011-04-20 13:37:53 +03002382 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Nadav Amit6c6cb692014-07-21 14:37:30 +03002383 ctxt->eflags &= ~msr_data;
Nadav Amit807c1422014-11-02 11:54:49 +02002384 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002385#endif
2386 } else {
2387 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002388 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002389 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002390
Nadav Amit6c6cb692014-07-21 14:37:30 +03002391 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002392 }
2393
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002394 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002395}
2396
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002397static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002398{
Mathias Krause0225fb52012-08-30 01:30:16 +02002399 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002400 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002401 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002402 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002403 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002404
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002405 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002406 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002407 if (ctxt->mode == X86EMUL_MODE_REAL)
2408 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002409
Avi Kivity1a18a692012-02-01 12:23:21 +02002410 /*
2411 * Not recognized on AMD in compat mode (but is recognized in legacy
2412 * mode).
2413 */
2414 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2415 && !vendor_intel(ctxt))
2416 return emulate_ud(ctxt);
2417
Nadav Amitb2c9d432014-11-02 11:55:01 +02002418 /* sysenter/sysexit have not been tested in 64bit mode. */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002419 if (ctxt->mode == X86EMUL_MODE_PROT64)
Nadav Amitb2c9d432014-11-02 11:55:01 +02002420 return X86EMUL_UNHANDLEABLE;
Andre Przywara8c604352009-06-18 12:56:01 +02002421
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002422 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002423
Avi Kivity717746e2011-04-20 13:37:53 +03002424 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002425 switch (ctxt->mode) {
2426 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002427 if ((msr_data & 0xfffc) == 0x0)
2428 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002429 break;
2430 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002431 if (msr_data == 0x0)
2432 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002433 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002434 default:
2435 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002436 }
2437
Nadav Amit6c6cb692014-07-21 14:37:30 +03002438 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002439 cs_sel = (u16)msr_data;
2440 cs_sel &= ~SELECTOR_RPL_MASK;
2441 ss_sel = cs_sel + 8;
2442 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002443 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002444 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002445 cs.l = 1;
2446 }
2447
Avi Kivity1aa36612011-04-27 13:20:30 +03002448 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2449 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002450
Avi Kivity717746e2011-04-20 13:37:53 +03002451 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002452 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002453
Avi Kivity717746e2011-04-20 13:37:53 +03002454 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002455 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002456
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002457 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002458}
2459
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002460static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002461{
Mathias Krause0225fb52012-08-30 01:30:16 +02002462 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002463 struct desc_struct cs, ss;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002464 u64 msr_data, rcx, rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002465 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002466 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002467
Gleb Natapova0044752010-02-10 14:21:31 +02002468 /* inject #GP if in real mode or Virtual 8086 mode */
2469 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002470 ctxt->mode == X86EMUL_MODE_VM86)
2471 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002472
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002473 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002474
Avi Kivity9dac77f2011-06-01 15:34:25 +03002475 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002476 usermode = X86EMUL_MODE_PROT64;
2477 else
2478 usermode = X86EMUL_MODE_PROT32;
2479
Nadav Amit234f3ce2014-09-18 22:39:38 +03002480 rcx = reg_read(ctxt, VCPU_REGS_RCX);
2481 rdx = reg_read(ctxt, VCPU_REGS_RDX);
2482
Andre Przywara4668f052009-06-18 12:56:02 +02002483 cs.dpl = 3;
2484 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002485 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002486 switch (usermode) {
2487 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002488 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002489 if ((msr_data & 0xfffc) == 0x0)
2490 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002491 ss_sel = (u16)(msr_data + 24);
Nadav Amitbf0b6822014-09-18 22:39:45 +03002492 rcx = (u32)rcx;
2493 rdx = (u32)rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002494 break;
2495 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002496 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002497 if (msr_data == 0x0)
2498 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002499 ss_sel = cs_sel + 8;
2500 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002501 cs.l = 1;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002502 if (is_noncanonical_address(rcx) ||
2503 is_noncanonical_address(rdx))
2504 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002505 break;
2506 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002507 cs_sel |= SELECTOR_RPL_MASK;
2508 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002509
Avi Kivity1aa36612011-04-27 13:20:30 +03002510 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2511 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002512
Nadav Amit234f3ce2014-09-18 22:39:38 +03002513 ctxt->_eip = rdx;
2514 *reg_write(ctxt, VCPU_REGS_RSP) = rcx;
Andre Przywara4668f052009-06-18 12:56:02 +02002515
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002516 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002517}
2518
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002519static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002520{
2521 int iopl;
2522 if (ctxt->mode == X86EMUL_MODE_REAL)
2523 return false;
2524 if (ctxt->mode == X86EMUL_MODE_VM86)
2525 return true;
2526 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002527 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002528}
2529
2530static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002531 u16 port, u16 len)
2532{
Mathias Krause0225fb52012-08-30 01:30:16 +02002533 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002534 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002535 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002536 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002537 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002538 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002539 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002540
Avi Kivity1aa36612011-04-27 13:20:30 +03002541 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002542 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002543 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002544 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002545 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002546 base = get_desc_base(&tr_seg);
2547#ifdef CONFIG_X86_64
2548 base |= ((u64)base3) << 32;
2549#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002550 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002551 if (r != X86EMUL_CONTINUE)
2552 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002553 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002554 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002555 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002556 if (r != X86EMUL_CONTINUE)
2557 return false;
2558 if ((perm >> bit_idx) & mask)
2559 return false;
2560 return true;
2561}
2562
2563static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002564 u16 port, u16 len)
2565{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002566 if (ctxt->perm_ok)
2567 return true;
2568
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002569 if (emulator_bad_iopl(ctxt))
2570 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002571 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002572
2573 ctxt->perm_ok = true;
2574
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002575 return true;
2576}
2577
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002578static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002579 struct tss_segment_16 *tss)
2580{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002581 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002582 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002583 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2584 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2585 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2586 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2587 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2588 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2589 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2590 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002591
Avi Kivity1aa36612011-04-27 13:20:30 +03002592 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2593 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2594 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2595 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2596 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002597}
2598
2599static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002600 struct tss_segment_16 *tss)
2601{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002602 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002603 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002604
Avi Kivity9dac77f2011-06-01 15:34:25 +03002605 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002606 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002607 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2608 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2609 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2610 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2611 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2612 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2613 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2614 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002615
2616 /*
2617 * SDM says that segment selectors are loaded before segment
2618 * descriptors
2619 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002620 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2621 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2622 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2623 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2624 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002625
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002626 cpl = tss->cs & 3;
2627
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002628 /*
Guo Chaofc058682012-06-28 15:19:51 +08002629 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002630 * it is handled in a context of new task
2631 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002632 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002633 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002634 if (ret != X86EMUL_CONTINUE)
2635 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002636 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002637 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002638 if (ret != X86EMUL_CONTINUE)
2639 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002640 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002641 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002642 if (ret != X86EMUL_CONTINUE)
2643 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002644 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002645 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002646 if (ret != X86EMUL_CONTINUE)
2647 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002648 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002649 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002650 if (ret != X86EMUL_CONTINUE)
2651 return ret;
2652
2653 return X86EMUL_CONTINUE;
2654}
2655
2656static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002657 u16 tss_selector, u16 old_tss_sel,
2658 ulong old_tss_base, struct desc_struct *new_desc)
2659{
Mathias Krause0225fb52012-08-30 01:30:16 +02002660 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002661 struct tss_segment_16 tss_seg;
2662 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002663 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002664
Avi Kivity0f65dd72011-04-20 13:37:53 +03002665 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002666 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002667 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002668 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002669
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002670 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002671
Avi Kivity0f65dd72011-04-20 13:37:53 +03002672 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002673 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002674 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002675 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002676
Avi Kivity0f65dd72011-04-20 13:37:53 +03002677 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002678 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002679 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002680 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002681
2682 if (old_tss_sel != 0xffff) {
2683 tss_seg.prev_task_link = old_tss_sel;
2684
Avi Kivity0f65dd72011-04-20 13:37:53 +03002685 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002686 &tss_seg.prev_task_link,
2687 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002688 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002689 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002690 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002691 }
2692
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002693 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002694}
2695
2696static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002697 struct tss_segment_32 *tss)
2698{
Nadav Amit5c7411e2014-04-07 18:37:47 +03002699 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002700 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002701 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002702 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2703 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2704 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2705 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2706 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2707 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2708 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2709 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002710
Avi Kivity1aa36612011-04-27 13:20:30 +03002711 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2712 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2713 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2714 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2715 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2716 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002717}
2718
2719static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002720 struct tss_segment_32 *tss)
2721{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002722 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002723 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002724
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002725 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002726 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002727 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002728 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002729
2730 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002731 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2732 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2733 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2734 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2735 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2736 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2737 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2738 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002739
2740 /*
2741 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002742 * descriptors. This is important because CPL checks will
2743 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002744 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002745 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2746 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2747 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2748 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2749 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2750 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2751 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002752
2753 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002754 * If we're switching between Protected Mode and VM86, we need to make
2755 * sure to update the mode before loading the segment descriptors so
2756 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01002757 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002758 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002759 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002760 cpl = 3;
2761 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002762 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002763 cpl = tss->cs & 3;
2764 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01002765
2766 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002767 * Now load segment descriptors. If fault happenes at this stage
2768 * it is handled in a context of new task
2769 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002770 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002771 cpl, X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002772 if (ret != X86EMUL_CONTINUE)
2773 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002774 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002775 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002776 if (ret != X86EMUL_CONTINUE)
2777 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002778 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002779 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002780 if (ret != X86EMUL_CONTINUE)
2781 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002782 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002783 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002784 if (ret != X86EMUL_CONTINUE)
2785 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002786 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002787 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002788 if (ret != X86EMUL_CONTINUE)
2789 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002790 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002791 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002792 if (ret != X86EMUL_CONTINUE)
2793 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002794 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002795 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002796 if (ret != X86EMUL_CONTINUE)
2797 return ret;
2798
2799 return X86EMUL_CONTINUE;
2800}
2801
2802static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002803 u16 tss_selector, u16 old_tss_sel,
2804 ulong old_tss_base, struct desc_struct *new_desc)
2805{
Mathias Krause0225fb52012-08-30 01:30:16 +02002806 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002807 struct tss_segment_32 tss_seg;
2808 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002809 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03002810 u32 eip_offset = offsetof(struct tss_segment_32, eip);
2811 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002812
Avi Kivity0f65dd72011-04-20 13:37:53 +03002813 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002814 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002815 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002816 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002817
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002818 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002819
Nadav Amit5c7411e2014-04-07 18:37:47 +03002820 /* Only GP registers and segment selectors are saved */
2821 ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
2822 ldt_sel_offset - eip_offset, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002823 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002824 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002825
Avi Kivity0f65dd72011-04-20 13:37:53 +03002826 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002827 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002828 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002829 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002830
2831 if (old_tss_sel != 0xffff) {
2832 tss_seg.prev_task_link = old_tss_sel;
2833
Avi Kivity0f65dd72011-04-20 13:37:53 +03002834 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002835 &tss_seg.prev_task_link,
2836 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002837 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002838 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002839 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002840 }
2841
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002842 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002843}
2844
2845static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002846 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002847 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002848{
Mathias Krause0225fb52012-08-30 01:30:16 +02002849 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002850 struct desc_struct curr_tss_desc, next_tss_desc;
2851 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002852 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002853 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002854 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002855 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002856 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002857
2858 /* FIXME: old_tss_base == ~0 ? */
2859
Avi Kivitye9194642012-06-13 16:29:39 +03002860 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002861 if (ret != X86EMUL_CONTINUE)
2862 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002863 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002864 if (ret != X86EMUL_CONTINUE)
2865 return ret;
2866
2867 /* FIXME: check that next_tss_desc is tss */
2868
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002869 /*
2870 * Check privileges. The three cases are task switch caused by...
2871 *
2872 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2873 * 2. Exception/IRQ/iret: No check is performed
Nadav Amit2c2ca2d2014-11-02 11:54:57 +02002874 * 3. jmp/call to TSS/task-gate: No check is performed since the
2875 * hardware checks it before exiting.
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002876 */
2877 if (reason == TASK_SWITCH_GATE) {
2878 if (idt_index != -1) {
2879 /* Software interrupts */
2880 struct desc_struct task_gate_desc;
2881 int dpl;
2882
2883 ret = read_interrupt_descriptor(ctxt, idt_index,
2884 &task_gate_desc);
2885 if (ret != X86EMUL_CONTINUE)
2886 return ret;
2887
2888 dpl = task_gate_desc.dpl;
2889 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2890 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2891 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002892 }
2893
Gleb Natapovceffb452010-03-18 15:20:19 +02002894 desc_limit = desc_limit_scaled(&next_tss_desc);
2895 if (!next_tss_desc.p ||
2896 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2897 desc_limit < 0x2b)) {
Paolo Bonzini592f0852014-08-20 10:05:08 +02002898 return emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002899 }
2900
2901 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2902 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002903 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002904 }
2905
2906 if (reason == TASK_SWITCH_IRET)
2907 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2908
2909 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002910 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002911 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2912 old_tss_sel = 0xffff;
2913
2914 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002915 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002916 old_tss_base, &next_tss_desc);
2917 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002918 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002919 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002920 if (ret != X86EMUL_CONTINUE)
2921 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002922
2923 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2924 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2925
2926 if (reason != TASK_SWITCH_IRET) {
2927 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002928 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002929 }
2930
Avi Kivity717746e2011-04-20 13:37:53 +03002931 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002932 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002933
Jan Kiszkae269fb22010-04-14 15:51:09 +02002934 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002935 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2936 ctxt->lock_prefix = 0;
2937 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002938 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002939 }
2940
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002941 return ret;
2942}
2943
2944int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002945 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002946 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002947{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002948 int rc;
2949
Avi Kivitydd856ef2012-08-27 23:46:17 +03002950 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002951 ctxt->_eip = ctxt->eip;
2952 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002953
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002954 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002955 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002956
Avi Kivitydd856ef2012-08-27 23:46:17 +03002957 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002958 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002959 writeback_registers(ctxt);
2960 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002961
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002962 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002963}
2964
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002965static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2966 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002967{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002968 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002969
Paolo Bonzini01485a22014-11-19 18:25:08 +01002970 register_address_increment(ctxt, reg, df * op->bytes);
2971 op->addr.mem.ea = register_address(ctxt, reg);
Gleb Natapova682e352010-03-18 15:20:21 +02002972}
2973
Avi Kivity7af04fc2010-08-18 14:16:35 +03002974static int em_das(struct x86_emulate_ctxt *ctxt)
2975{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002976 u8 al, old_al;
2977 bool af, cf, old_cf;
2978
2979 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002980 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002981
2982 old_al = al;
2983 old_cf = cf;
2984 cf = false;
2985 af = ctxt->eflags & X86_EFLAGS_AF;
2986 if ((al & 0x0f) > 9 || af) {
2987 al -= 6;
2988 cf = old_cf | (al >= 250);
2989 af = true;
2990 } else {
2991 af = false;
2992 }
2993 if (old_al > 0x99 || old_cf) {
2994 al -= 0x60;
2995 cf = true;
2996 }
2997
Avi Kivity9dac77f2011-06-01 15:34:25 +03002998 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002999 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003000 ctxt->src.type = OP_IMM;
3001 ctxt->src.val = 0;
3002 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02003003 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03003004 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
3005 if (cf)
3006 ctxt->eflags |= X86_EFLAGS_CF;
3007 if (af)
3008 ctxt->eflags |= X86_EFLAGS_AF;
3009 return X86EMUL_CONTINUE;
3010}
3011
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003012static int em_aam(struct x86_emulate_ctxt *ctxt)
3013{
3014 u8 al, ah;
3015
3016 if (ctxt->src.val == 0)
3017 return emulate_de(ctxt);
3018
3019 al = ctxt->dst.val & 0xff;
3020 ah = al / ctxt->src.val;
3021 al %= ctxt->src.val;
3022
3023 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
3024
3025 /* Set PF, ZF, SF */
3026 ctxt->src.type = OP_IMM;
3027 ctxt->src.val = 0;
3028 ctxt->src.bytes = 1;
3029 fastop(ctxt, em_or);
3030
3031 return X86EMUL_CONTINUE;
3032}
3033
Gleb Natapov7f662272012-12-10 11:42:30 +02003034static int em_aad(struct x86_emulate_ctxt *ctxt)
3035{
3036 u8 al = ctxt->dst.val & 0xff;
3037 u8 ah = (ctxt->dst.val >> 8) & 0xff;
3038
3039 al = (al + (ah * ctxt->src.val)) & 0xff;
3040
3041 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
3042
Gleb Natapovf583c292013-02-13 17:50:39 +02003043 /* Set PF, ZF, SF */
3044 ctxt->src.type = OP_IMM;
3045 ctxt->src.val = 0;
3046 ctxt->src.bytes = 1;
3047 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02003048
3049 return X86EMUL_CONTINUE;
3050}
3051
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003052static int em_call(struct x86_emulate_ctxt *ctxt)
3053{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003054 int rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003055 long rel = ctxt->src.val;
3056
3057 ctxt->src.val = (unsigned long)ctxt->_eip;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003058 rc = jmp_rel(ctxt, rel);
3059 if (rc != X86EMUL_CONTINUE)
3060 return rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003061 return em_push(ctxt);
3062}
3063
Avi Kivity0ef753b2010-08-18 14:51:45 +03003064static int em_call_far(struct x86_emulate_ctxt *ctxt)
3065{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003066 u16 sel, old_cs;
3067 ulong old_eip;
3068 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03003069 struct desc_struct old_desc, new_desc;
3070 const struct x86_emulate_ops *ops = ctxt->ops;
3071 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amit82268082015-01-26 09:32:27 +02003072 enum x86emul_mode prev_mode = ctxt->mode;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003073
Avi Kivity9dac77f2011-06-01 15:34:25 +03003074 old_eip = ctxt->_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003075 ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003076
Avi Kivity9dac77f2011-06-01 15:34:25 +03003077 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003078 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
3079 X86_TRANSFER_CALL_JMP, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003080 if (rc != X86EMUL_CONTINUE)
Nadav Amit80976db2014-12-25 02:52:20 +02003081 return rc;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003082
Nadav Amitd50eaa12014-11-19 17:43:11 +02003083 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003084 if (rc != X86EMUL_CONTINUE)
3085 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003086
Avi Kivity9dac77f2011-06-01 15:34:25 +03003087 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003088 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003089 if (rc != X86EMUL_CONTINUE)
Nadav Amitd1442d82014-09-18 22:39:39 +03003090 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003091
Avi Kivity9dac77f2011-06-01 15:34:25 +03003092 ctxt->src.val = old_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003093 rc = em_push(ctxt);
3094 /* If we failed, we tainted the memory, but the very least we should
3095 restore cs */
Nadav Amit82268082015-01-26 09:32:27 +02003096 if (rc != X86EMUL_CONTINUE) {
3097 pr_warn_once("faulting far call emulation tainted memory\n");
Nadav Amitd1442d82014-09-18 22:39:39 +03003098 goto fail;
Nadav Amit82268082015-01-26 09:32:27 +02003099 }
Nadav Amitd1442d82014-09-18 22:39:39 +03003100 return rc;
3101fail:
3102 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
Nadav Amit82268082015-01-26 09:32:27 +02003103 ctxt->mode = prev_mode;
Nadav Amitd1442d82014-09-18 22:39:39 +03003104 return rc;
3105
Avi Kivity0ef753b2010-08-18 14:51:45 +03003106}
3107
Avi Kivity40ece7c2010-08-18 15:12:09 +03003108static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3109{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003110 int rc;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003111 unsigned long eip;
Avi Kivity40ece7c2010-08-18 15:12:09 +03003112
Nadav Amit234f3ce2014-09-18 22:39:38 +03003113 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
3114 if (rc != X86EMUL_CONTINUE)
3115 return rc;
3116 rc = assign_eip_near(ctxt, eip);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003117 if (rc != X86EMUL_CONTINUE)
3118 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003119 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003120 return X86EMUL_CONTINUE;
3121}
3122
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003123static int em_xchg(struct x86_emulate_ctxt *ctxt)
3124{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003125 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003126 ctxt->src.val = ctxt->dst.val;
3127 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003128
3129 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003130 ctxt->dst.val = ctxt->src.orig_val;
3131 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003132 return X86EMUL_CONTINUE;
3133}
3134
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003135static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3136{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003137 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003138 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003139}
3140
Avi Kivity61429142010-08-19 15:13:00 +03003141static int em_cwd(struct x86_emulate_ctxt *ctxt)
3142{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003143 ctxt->dst.type = OP_REG;
3144 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003145 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003146 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003147
3148 return X86EMUL_CONTINUE;
3149}
3150
Avi Kivity48bb5d32010-08-18 18:54:34 +03003151static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3152{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003153 u64 tsc = 0;
3154
Avi Kivity717746e2011-04-20 13:37:53 +03003155 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003156 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3157 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003158 return X86EMUL_CONTINUE;
3159}
3160
Avi Kivity222d21a2011-11-10 14:57:30 +02003161static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3162{
3163 u64 pmc;
3164
Avi Kivitydd856ef2012-08-27 23:46:17 +03003165 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003166 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003167 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3168 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003169 return X86EMUL_CONTINUE;
3170}
3171
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003172static int em_mov(struct x86_emulate_ctxt *ctxt)
3173{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01003174 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003175 return X86EMUL_CONTINUE;
3176}
3177
Borislav Petkov84cffe42013-10-29 12:54:56 +01003178#define FFL(x) bit(X86_FEATURE_##x)
3179
3180static int em_movbe(struct x86_emulate_ctxt *ctxt)
3181{
3182 u32 ebx, ecx, edx, eax = 1;
3183 u16 tmp;
3184
3185 /*
3186 * Check MOVBE is set in the guest-visible CPUID leaf.
3187 */
3188 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
3189 if (!(ecx & FFL(MOVBE)))
3190 return emulate_ud(ctxt);
3191
3192 switch (ctxt->op_bytes) {
3193 case 2:
3194 /*
3195 * From MOVBE definition: "...When the operand size is 16 bits,
3196 * the upper word of the destination register remains unchanged
3197 * ..."
3198 *
3199 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3200 * rules so we have to do the operation almost per hand.
3201 */
3202 tmp = (u16)ctxt->src.val;
3203 ctxt->dst.val &= ~0xffffUL;
3204 ctxt->dst.val |= (unsigned long)swab16(tmp);
3205 break;
3206 case 4:
3207 ctxt->dst.val = swab32((u32)ctxt->src.val);
3208 break;
3209 case 8:
3210 ctxt->dst.val = swab64(ctxt->src.val);
3211 break;
3212 default:
Paolo Bonzini592f0852014-08-20 10:05:08 +02003213 BUG();
Borislav Petkov84cffe42013-10-29 12:54:56 +01003214 }
3215 return X86EMUL_CONTINUE;
3216}
3217
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003218static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3219{
3220 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3221 return emulate_gp(ctxt, 0);
3222
3223 /* Disable writeback. */
3224 ctxt->dst.type = OP_NONE;
3225 return X86EMUL_CONTINUE;
3226}
3227
3228static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3229{
3230 unsigned long val;
3231
3232 if (ctxt->mode == X86EMUL_MODE_PROT64)
3233 val = ctxt->src.val & ~0ULL;
3234 else
3235 val = ctxt->src.val & ~0U;
3236
3237 /* #UD condition is already handled. */
3238 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3239 return emulate_gp(ctxt, 0);
3240
3241 /* Disable writeback. */
3242 ctxt->dst.type = OP_NONE;
3243 return X86EMUL_CONTINUE;
3244}
3245
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003246static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3247{
3248 u64 msr_data;
3249
Avi Kivitydd856ef2012-08-27 23:46:17 +03003250 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3251 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3252 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003253 return emulate_gp(ctxt, 0);
3254
3255 return X86EMUL_CONTINUE;
3256}
3257
3258static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3259{
3260 u64 msr_data;
3261
Avi Kivitydd856ef2012-08-27 23:46:17 +03003262 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003263 return emulate_gp(ctxt, 0);
3264
Avi Kivitydd856ef2012-08-27 23:46:17 +03003265 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3266 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003267 return X86EMUL_CONTINUE;
3268}
3269
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003270static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3271{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003272 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003273 return emulate_ud(ctxt);
3274
Avi Kivity9dac77f2011-06-01 15:34:25 +03003275 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Nadav Amitb5bbf102014-11-02 11:54:46 +02003276 if (ctxt->dst.bytes == 4 && ctxt->dst.type == OP_MEM)
3277 ctxt->dst.bytes = 2;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003278 return X86EMUL_CONTINUE;
3279}
3280
3281static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3282{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003283 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003284
Avi Kivity9dac77f2011-06-01 15:34:25 +03003285 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003286 return emulate_ud(ctxt);
3287
Avi Kivity9dac77f2011-06-01 15:34:25 +03003288 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003289 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3290
3291 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003292 ctxt->dst.type = OP_NONE;
3293 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003294}
3295
Avi Kivitya14e5792012-06-13 12:28:33 +03003296static int em_lldt(struct x86_emulate_ctxt *ctxt)
3297{
3298 u16 sel = ctxt->src.val;
3299
3300 /* Disable writeback. */
3301 ctxt->dst.type = OP_NONE;
3302 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3303}
3304
Avi Kivity80890002012-06-13 16:33:29 +03003305static int em_ltr(struct x86_emulate_ctxt *ctxt)
3306{
3307 u16 sel = ctxt->src.val;
3308
3309 /* Disable writeback. */
3310 ctxt->dst.type = OP_NONE;
3311 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3312}
3313
Avi Kivity38503912011-03-31 18:48:09 +02003314static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3315{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003316 int rc;
3317 ulong linear;
3318
Avi Kivity9dac77f2011-06-01 15:34:25 +03003319 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003320 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003321 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003322 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003323 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003324 return X86EMUL_CONTINUE;
3325}
3326
Avi Kivity2d04a052011-04-20 15:32:49 +03003327static int em_clts(struct x86_emulate_ctxt *ctxt)
3328{
3329 ulong cr0;
3330
3331 cr0 = ctxt->ops->get_cr(ctxt, 0);
3332 cr0 &= ~X86_CR0_TS;
3333 ctxt->ops->set_cr(ctxt, 0, cr0);
3334 return X86EMUL_CONTINUE;
3335}
3336
Avi Kivity26d05cc2011-04-21 12:07:59 +03003337static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3338{
Nadav Amit0f54a322014-08-29 11:26:55 +03003339 int rc = ctxt->ops->fix_hypercall(ctxt);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003340
Avi Kivity26d05cc2011-04-21 12:07:59 +03003341 if (rc != X86EMUL_CONTINUE)
3342 return rc;
3343
3344 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003345 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003346 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003347 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003348 return X86EMUL_CONTINUE;
3349}
3350
Avi Kivity96051572012-06-10 17:21:18 +03003351static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3352 void (*get)(struct x86_emulate_ctxt *ctxt,
3353 struct desc_ptr *ptr))
3354{
3355 struct desc_ptr desc_ptr;
3356
3357 if (ctxt->mode == X86EMUL_MODE_PROT64)
3358 ctxt->op_bytes = 8;
3359 get(ctxt, &desc_ptr);
3360 if (ctxt->op_bytes == 2) {
3361 ctxt->op_bytes = 4;
3362 desc_ptr.address &= 0x00ffffff;
3363 }
3364 /* Disable writeback. */
3365 ctxt->dst.type = OP_NONE;
3366 return segmented_write(ctxt, ctxt->dst.addr.mem,
3367 &desc_ptr, 2 + ctxt->op_bytes);
3368}
3369
3370static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3371{
3372 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3373}
3374
3375static int em_sidt(struct x86_emulate_ctxt *ctxt)
3376{
3377 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3378}
3379
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003380static int em_lgdt_lidt(struct x86_emulate_ctxt *ctxt, bool lgdt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003381{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003382 struct desc_ptr desc_ptr;
3383 int rc;
3384
Avi Kivity510425f2012-06-07 17:04:36 +03003385 if (ctxt->mode == X86EMUL_MODE_PROT64)
3386 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003387 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003388 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003389 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003390 if (rc != X86EMUL_CONTINUE)
3391 return rc;
Nadav Amit9a9abf62014-11-02 11:54:56 +02003392 if (ctxt->mode == X86EMUL_MODE_PROT64 &&
3393 is_noncanonical_address(desc_ptr.address))
3394 return emulate_gp(ctxt, 0);
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003395 if (lgdt)
3396 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3397 else
3398 ctxt->ops->set_idt(ctxt, &desc_ptr);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003399 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003400 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003401 return X86EMUL_CONTINUE;
3402}
3403
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003404static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3405{
3406 return em_lgdt_lidt(ctxt, true);
3407}
3408
Avi Kivity5ef39c72011-04-21 12:21:50 +03003409static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003410{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003411 int rc;
3412
Avi Kivity5ef39c72011-04-21 12:21:50 +03003413 rc = ctxt->ops->fix_hypercall(ctxt);
3414
Avi Kivity26d05cc2011-04-21 12:07:59 +03003415 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003416 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003417 return rc;
3418}
3419
3420static int em_lidt(struct x86_emulate_ctxt *ctxt)
3421{
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003422 return em_lgdt_lidt(ctxt, false);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003423}
3424
3425static int em_smsw(struct x86_emulate_ctxt *ctxt)
3426{
Nadav Amit32e94d02014-06-02 18:34:11 +03003427 if (ctxt->dst.type == OP_MEM)
3428 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003429 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003430 return X86EMUL_CONTINUE;
3431}
3432
3433static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3434{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003435 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003436 | (ctxt->src.val & 0x0f));
3437 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003438 return X86EMUL_CONTINUE;
3439}
3440
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003441static int em_loop(struct x86_emulate_ctxt *ctxt)
3442{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003443 int rc = X86EMUL_CONTINUE;
3444
Paolo Bonzini01485a22014-11-19 18:25:08 +01003445 register_address_increment(ctxt, VCPU_REGS_RCX, -1);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003446 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003447 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
Nadav Amit234f3ce2014-09-18 22:39:38 +03003448 rc = jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003449
Nadav Amit234f3ce2014-09-18 22:39:38 +03003450 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003451}
3452
3453static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3454{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003455 int rc = X86EMUL_CONTINUE;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003456
Nadav Amit234f3ce2014-09-18 22:39:38 +03003457 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
3458 rc = jmp_rel(ctxt, ctxt->src.val);
3459
3460 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003461}
3462
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003463static int em_in(struct x86_emulate_ctxt *ctxt)
3464{
3465 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3466 &ctxt->dst.val))
3467 return X86EMUL_IO_NEEDED;
3468
3469 return X86EMUL_CONTINUE;
3470}
3471
3472static int em_out(struct x86_emulate_ctxt *ctxt)
3473{
3474 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3475 &ctxt->src.val, 1);
3476 /* Disable writeback. */
3477 ctxt->dst.type = OP_NONE;
3478 return X86EMUL_CONTINUE;
3479}
3480
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003481static int em_cli(struct x86_emulate_ctxt *ctxt)
3482{
3483 if (emulator_bad_iopl(ctxt))
3484 return emulate_gp(ctxt, 0);
3485
3486 ctxt->eflags &= ~X86_EFLAGS_IF;
3487 return X86EMUL_CONTINUE;
3488}
3489
3490static int em_sti(struct x86_emulate_ctxt *ctxt)
3491{
3492 if (emulator_bad_iopl(ctxt))
3493 return emulate_gp(ctxt, 0);
3494
3495 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3496 ctxt->eflags |= X86_EFLAGS_IF;
3497 return X86EMUL_CONTINUE;
3498}
3499
Avi Kivity6d6eede2012-06-07 14:11:36 +03003500static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3501{
3502 u32 eax, ebx, ecx, edx;
3503
Avi Kivitydd856ef2012-08-27 23:46:17 +03003504 eax = reg_read(ctxt, VCPU_REGS_RAX);
3505 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003506 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003507 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3508 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3509 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3510 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003511 return X86EMUL_CONTINUE;
3512}
3513
Paolo Bonzini98f73632013-10-31 11:19:42 +01003514static int em_sahf(struct x86_emulate_ctxt *ctxt)
3515{
3516 u32 flags;
3517
3518 flags = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF;
3519 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3520
3521 ctxt->eflags &= ~0xffUL;
3522 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3523 return X86EMUL_CONTINUE;
3524}
3525
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003526static int em_lahf(struct x86_emulate_ctxt *ctxt)
3527{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003528 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3529 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003530 return X86EMUL_CONTINUE;
3531}
3532
Avi Kivity92998362012-06-13 12:25:06 +03003533static int em_bswap(struct x86_emulate_ctxt *ctxt)
3534{
3535 switch (ctxt->op_bytes) {
3536#ifdef CONFIG_X86_64
3537 case 8:
3538 asm("bswap %0" : "+r"(ctxt->dst.val));
3539 break;
3540#endif
3541 default:
3542 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3543 break;
3544 }
3545 return X86EMUL_CONTINUE;
3546}
3547
Nadav Amit13e457e2014-10-13 13:04:13 +03003548static int em_clflush(struct x86_emulate_ctxt *ctxt)
3549{
3550 /* emulating clflush regardless of cpuid */
3551 return X86EMUL_CONTINUE;
3552}
3553
Nadav Amit2276b512015-01-26 09:32:24 +02003554static int em_movsxd(struct x86_emulate_ctxt *ctxt)
3555{
3556 ctxt->dst.val = (s32) ctxt->src.val;
3557 return X86EMUL_CONTINUE;
3558}
3559
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003560static bool valid_cr(int nr)
3561{
3562 switch (nr) {
3563 case 0:
3564 case 2 ... 4:
3565 case 8:
3566 return true;
3567 default:
3568 return false;
3569 }
3570}
3571
3572static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3573{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003574 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003575 return emulate_ud(ctxt);
3576
3577 return X86EMUL_CONTINUE;
3578}
3579
3580static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3581{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003582 u64 new_val = ctxt->src.val64;
3583 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003584 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003585
3586 static u64 cr_reserved_bits[] = {
3587 0xffffffff00000000ULL,
3588 0, 0, 0, /* CR3 checked later */
3589 CR4_RESERVED_BITS,
3590 0, 0, 0,
3591 CR8_RESERVED_BITS,
3592 };
3593
3594 if (!valid_cr(cr))
3595 return emulate_ud(ctxt);
3596
3597 if (new_val & cr_reserved_bits[cr])
3598 return emulate_gp(ctxt, 0);
3599
3600 switch (cr) {
3601 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003602 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003603 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3604 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3605 return emulate_gp(ctxt, 0);
3606
Avi Kivity717746e2011-04-20 13:37:53 +03003607 cr4 = ctxt->ops->get_cr(ctxt, 4);
3608 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003609
3610 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3611 !(cr4 & X86_CR4_PAE))
3612 return emulate_gp(ctxt, 0);
3613
3614 break;
3615 }
3616 case 3: {
3617 u64 rsvd = 0;
3618
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003619 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3620 if (efer & EFER_LMA)
Nadav Amit9d88fca2014-11-02 11:54:52 +02003621 rsvd = CR3_L_MODE_RESERVED_BITS & ~CR3_PCID_INVD;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003622
3623 if (new_val & rsvd)
3624 return emulate_gp(ctxt, 0);
3625
3626 break;
3627 }
3628 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003629 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003630
3631 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3632 return emulate_gp(ctxt, 0);
3633
3634 break;
3635 }
3636 }
3637
3638 return X86EMUL_CONTINUE;
3639}
3640
Joerg Roedel3b88e412011-04-04 12:39:29 +02003641static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3642{
3643 unsigned long dr7;
3644
Avi Kivity717746e2011-04-20 13:37:53 +03003645 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003646
3647 /* Check if DR7.Global_Enable is set */
3648 return dr7 & (1 << 13);
3649}
3650
3651static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3652{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003653 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003654 u64 cr4;
3655
3656 if (dr > 7)
3657 return emulate_ud(ctxt);
3658
Avi Kivity717746e2011-04-20 13:37:53 +03003659 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003660 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3661 return emulate_ud(ctxt);
3662
Nadav Amit6d2a0522014-11-02 11:54:43 +02003663 if (check_dr7_gd(ctxt)) {
3664 ulong dr6;
3665
3666 ctxt->ops->get_dr(ctxt, 6, &dr6);
3667 dr6 &= ~15;
3668 dr6 |= DR6_BD | DR6_RTM;
3669 ctxt->ops->set_dr(ctxt, 6, dr6);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003670 return emulate_db(ctxt);
Nadav Amit6d2a0522014-11-02 11:54:43 +02003671 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02003672
3673 return X86EMUL_CONTINUE;
3674}
3675
3676static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3677{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003678 u64 new_val = ctxt->src.val64;
3679 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003680
3681 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3682 return emulate_gp(ctxt, 0);
3683
3684 return check_dr_read(ctxt);
3685}
3686
Joerg Roedel01de8b02011-04-04 12:39:31 +02003687static int check_svme(struct x86_emulate_ctxt *ctxt)
3688{
3689 u64 efer;
3690
Avi Kivity717746e2011-04-20 13:37:53 +03003691 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003692
3693 if (!(efer & EFER_SVME))
3694 return emulate_ud(ctxt);
3695
3696 return X86EMUL_CONTINUE;
3697}
3698
3699static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3700{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003701 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003702
3703 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003704 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003705 return emulate_gp(ctxt, 0);
3706
3707 return check_svme(ctxt);
3708}
3709
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003710static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3711{
Avi Kivity717746e2011-04-20 13:37:53 +03003712 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003713
Avi Kivity717746e2011-04-20 13:37:53 +03003714 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003715 return emulate_ud(ctxt);
3716
3717 return X86EMUL_CONTINUE;
3718}
3719
Joerg Roedel80612522011-04-04 12:39:33 +02003720static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3721{
Avi Kivity717746e2011-04-20 13:37:53 +03003722 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003723 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003724
Avi Kivity717746e2011-04-20 13:37:53 +03003725 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03003726 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02003727 return emulate_gp(ctxt, 0);
3728
3729 return X86EMUL_CONTINUE;
3730}
3731
Joerg Roedelf6511932011-04-04 12:39:35 +02003732static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3733{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003734 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3735 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003736 return emulate_gp(ctxt, 0);
3737
3738 return X86EMUL_CONTINUE;
3739}
3740
3741static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3742{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003743 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3744 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003745 return emulate_gp(ctxt, 0);
3746
3747 return X86EMUL_CONTINUE;
3748}
3749
Avi Kivity73fba5f2010-07-29 15:11:53 +03003750#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003751#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
3752#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
3753 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003754#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003755#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003756#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3757#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Nadav Amit39f062f2014-11-26 15:47:18 +02003758#define ID(_f, _i) { .flags = ((_f) | InstrDual | ModRM), .u.idual = (_i) }
Nadav Amit2276b512015-01-26 09:32:24 +02003759#define MD(_f, _m) { .flags = ((_f) | ModeDual), .u.mdual = (_m) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003760#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003761#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003762#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003763#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003764 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003765#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003766 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
3767 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003768#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003769
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003770#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003771#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003772#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003773#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003774#define I2bvIP(_f, _e, _i, _p) \
3775 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003776
Avi Kivityfb864fb2013-01-04 16:18:54 +02003777#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3778 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3779 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003780
Nadav Amit0f54a322014-08-29 11:26:55 +03003781static const struct opcode group7_rm0[] = {
3782 N,
3783 I(SrcNone | Priv | EmulateOnUD, em_vmcall),
3784 N, N, N, N, N, N,
3785};
3786
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003787static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003788 DI(SrcNone | Priv, monitor),
3789 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003790 N, N, N, N, N, N,
3791};
3792
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003793static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003794 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003795 II(SrcNone | Prot | EmulateOnUD, em_vmmcall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003796 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3797 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3798 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3799 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3800 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3801 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003802};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003803
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003804static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003805 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003806 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003807 N, N, N, N, N, N,
3808};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003809
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003810static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003811 F(Lock, em_add),
3812 F(Lock | PageTable, em_or),
3813 F(Lock, em_adc),
3814 F(Lock, em_sbb),
3815 F(Lock | PageTable, em_and),
3816 F(Lock, em_sub),
3817 F(Lock, em_xor),
3818 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003819};
3820
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003821static const struct opcode group1A[] = {
Nadav Amitab708092014-12-25 02:52:21 +02003822 I(DstMem | SrcNone | Mov | Stack | IncSP, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003823};
3824
Avi Kivity007a3b52013-01-19 19:51:51 +02003825static const struct opcode group2[] = {
3826 F(DstMem | ModRM, em_rol),
3827 F(DstMem | ModRM, em_ror),
3828 F(DstMem | ModRM, em_rcl),
3829 F(DstMem | ModRM, em_rcr),
3830 F(DstMem | ModRM, em_shl),
3831 F(DstMem | ModRM, em_shr),
3832 F(DstMem | ModRM, em_shl),
3833 F(DstMem | ModRM, em_sar),
3834};
3835
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003836static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003837 F(DstMem | SrcImm | NoWrite, em_test),
3838 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003839 F(DstMem | SrcNone | Lock, em_not),
3840 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02003841 F(DstXacc | Src2Mem, em_mul_ex),
3842 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02003843 F(DstXacc | Src2Mem, em_div_ex),
3844 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003845};
3846
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003847static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003848 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3849 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003850 N, N, N, N, N, N,
3851};
3852
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003853static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003854 F(DstMem | SrcNone | Lock, em_inc),
3855 F(DstMem | SrcNone | Lock, em_dec),
Nadav Amit58b70752014-10-24 11:35:09 +03003856 I(SrcMem | NearBranch, em_call_near_abs),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003857 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
Nadav Amit58b70752014-10-24 11:35:09 +03003858 I(SrcMem | NearBranch, em_jmp_abs),
Nadav Amitf7784042014-09-18 22:39:41 +03003859 I(SrcMemFAddr | ImplicitOps, em_jmp_far),
3860 I(SrcMem | Stack, em_push), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003861};
3862
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003863static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003864 DI(Prot, sldt),
3865 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003866 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003867 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003868 N, N, N, N,
3869};
3870
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003871static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03003872 II(Mov | DstMem, em_sgdt, sgdt),
3873 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003874 II(SrcMem | Priv, em_lgdt, lgdt),
3875 II(SrcMem | Priv, em_lidt, lidt),
3876 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3877 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3878 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003879}, {
Nadav Amit0f54a322014-08-29 11:26:55 +03003880 EXT(0, group7_rm0),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003881 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003882 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003883 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3884 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3885 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003886} };
3887
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003888static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003889 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003890 F(DstMem | SrcImmByte | NoWrite, em_bt),
3891 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3892 F(DstMem | SrcImmByte | Lock, em_btr),
3893 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003894};
3895
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003896static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003897 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003898}, {
3899 N, N, N, N, N, N, N, N,
3900} };
3901
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003902static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003903 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003904 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003905};
3906
Nadav Amit13e457e2014-10-13 13:04:13 +03003907static const struct gprefix pfx_0f_ae_7 = {
Nadav Amit3f6f1482014-10-13 13:04:14 +03003908 I(SrcMem | ByteOp, em_clflush), N, N, N,
Nadav Amit13e457e2014-10-13 13:04:13 +03003909};
3910
3911static const struct group_dual group15 = { {
3912 N, N, N, N, N, N, N, GP(0, &pfx_0f_ae_7),
3913}, {
3914 N, N, N, N, N, N, N, N,
3915} };
3916
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003917static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003918 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003919};
3920
Nadav Amit39f062f2014-11-26 15:47:18 +02003921static const struct instr_dual instr_dual_0f_2b = {
3922 I(0, em_mov), N
3923};
3924
Paolo Bonzinid5b77062014-07-14 12:54:48 +02003925static const struct gprefix pfx_0f_2b = {
Nadav Amit39f062f2014-11-26 15:47:18 +02003926 ID(0, &instr_dual_0f_2b), ID(0, &instr_dual_0f_2b), N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003927};
3928
Igor Mammedov27ce8252014-03-15 21:01:59 +01003929static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01003930 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003931};
3932
Alex Williamson0a370272014-07-11 11:56:31 -06003933static const struct gprefix pfx_0f_e7 = {
3934 N, I(Sse, em_mov), N, N,
3935};
3936
Gleb Natapov045a2822012-12-20 16:57:43 +02003937static const struct escape escape_d9 = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02003938 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstcw),
Gleb Natapov045a2822012-12-20 16:57:43 +02003939}, {
3940 /* 0xC0 - 0xC7 */
3941 N, N, N, N, N, N, N, N,
3942 /* 0xC8 - 0xCF */
3943 N, N, N, N, N, N, N, N,
3944 /* 0xD0 - 0xC7 */
3945 N, N, N, N, N, N, N, N,
3946 /* 0xD8 - 0xDF */
3947 N, N, N, N, N, N, N, N,
3948 /* 0xE0 - 0xE7 */
3949 N, N, N, N, N, N, N, N,
3950 /* 0xE8 - 0xEF */
3951 N, N, N, N, N, N, N, N,
3952 /* 0xF0 - 0xF7 */
3953 N, N, N, N, N, N, N, N,
3954 /* 0xF8 - 0xFF */
3955 N, N, N, N, N, N, N, N,
3956} };
3957
3958static const struct escape escape_db = { {
3959 N, N, N, N, N, N, N, N,
3960}, {
3961 /* 0xC0 - 0xC7 */
3962 N, N, N, N, N, N, N, N,
3963 /* 0xC8 - 0xCF */
3964 N, N, N, N, N, N, N, N,
3965 /* 0xD0 - 0xC7 */
3966 N, N, N, N, N, N, N, N,
3967 /* 0xD8 - 0xDF */
3968 N, N, N, N, N, N, N, N,
3969 /* 0xE0 - 0xE7 */
3970 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3971 /* 0xE8 - 0xEF */
3972 N, N, N, N, N, N, N, N,
3973 /* 0xF0 - 0xF7 */
3974 N, N, N, N, N, N, N, N,
3975 /* 0xF8 - 0xFF */
3976 N, N, N, N, N, N, N, N,
3977} };
3978
3979static const struct escape escape_dd = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02003980 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstsw),
Gleb Natapov045a2822012-12-20 16:57:43 +02003981}, {
3982 /* 0xC0 - 0xC7 */
3983 N, N, N, N, N, N, N, N,
3984 /* 0xC8 - 0xCF */
3985 N, N, N, N, N, N, N, N,
3986 /* 0xD0 - 0xC7 */
3987 N, N, N, N, N, N, N, N,
3988 /* 0xD8 - 0xDF */
3989 N, N, N, N, N, N, N, N,
3990 /* 0xE0 - 0xE7 */
3991 N, N, N, N, N, N, N, N,
3992 /* 0xE8 - 0xEF */
3993 N, N, N, N, N, N, N, N,
3994 /* 0xF0 - 0xF7 */
3995 N, N, N, N, N, N, N, N,
3996 /* 0xF8 - 0xFF */
3997 N, N, N, N, N, N, N, N,
3998} };
3999
Nadav Amit39f062f2014-11-26 15:47:18 +02004000static const struct instr_dual instr_dual_0f_c3 = {
4001 I(DstMem | SrcReg | ModRM | No16 | Mov, em_mov), N
4002};
4003
Nadav Amit2276b512015-01-26 09:32:24 +02004004static const struct mode_dual mode_dual_63 = {
4005 N, I(DstReg | SrcMem32 | ModRM | Mov, em_movsxd)
4006};
4007
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004008static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004009 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004010 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004011 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
4012 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004013 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004014 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004015 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
4016 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004017 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004018 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004019 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
4020 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004021 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004022 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004023 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
4024 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004025 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004026 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004027 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004028 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004029 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004030 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004031 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004032 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004033 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02004034 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004035 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03004036 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004037 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09004038 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004039 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09004040 I(ImplicitOps | Stack | No64, em_pusha),
4041 I(ImplicitOps | Stack | No64, em_popa),
Nadav Amit2276b512015-01-26 09:32:24 +02004042 N, MD(ModRM, &mode_dual_63),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004043 N, N, N, N,
4044 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03004045 I(SrcImm | Mov | Stack, em_push),
4046 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03004047 I(SrcImmByte | Mov | Stack, em_push),
4048 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004049 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09004050 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03004051 /* 0x70 - 0x7F */
Nadav Amit58b70752014-10-24 11:35:09 +03004052 X16(D(SrcImmByte | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004053 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004054 G(ByteOp | DstMem | SrcImm, group1),
4055 G(DstMem | SrcImm, group1),
4056 G(ByteOp | DstMem | SrcImm | No64, group1),
4057 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02004058 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004059 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004060 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004061 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004062 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004063 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09004064 D(ModRM | SrcMem | NoAccess | DstReg),
4065 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
4066 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004067 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004068 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004069 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03004070 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08004071 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09004072 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01004073 II(ImplicitOps | Stack, em_popf, popf),
4074 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004075 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004076 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004077 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004078 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004079 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004080 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004081 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004082 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
4083 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004084 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004085 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004086 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004087 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02004088 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004089 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004090 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Nadav Amit58b70752014-10-24 11:35:09 +03004091 I(ImplicitOps | NearBranch | SrcImmU16, em_ret_near_imm),
4092 I(ImplicitOps | NearBranch, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03004093 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
4094 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004095 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004096 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03004097 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Nadav Amit16794aa2015-01-26 09:32:22 +02004098 I(ImplicitOps | SrcImmU16, em_ret_far_imm),
4099 I(ImplicitOps, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02004100 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004101 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004102 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004103 G(Src2One | ByteOp, group2), G(Src2One, group2),
4104 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02004105 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02004106 I(DstAcc | SrcImmUByte | No64, em_aad),
4107 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004108 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004109 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02004110 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004111 /* 0xE0 - 0xE7 */
Nadav Amit58b70752014-10-24 11:35:09 +03004112 X3(I(SrcImmByte | NearBranch, em_loop)),
4113 I(SrcImmByte | NearBranch, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004114 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
4115 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004116 /* 0xE8 - 0xEF */
Nadav Amit58b70752014-10-24 11:35:09 +03004117 I(SrcImm | NearBranch, em_call), D(SrcImm | ImplicitOps | NearBranch),
4118 I(SrcImmFAddr | No64, em_jmp_far),
4119 D(SrcImmByte | ImplicitOps | NearBranch),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004120 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
4121 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004122 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004123 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004124 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4125 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004126 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09004127 D(ImplicitOps), D(ImplicitOps),
4128 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004129 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4130};
4131
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004132static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004133 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004134 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02004135 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004136 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004137 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004138 N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004139 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004140 N, N, N, N, N, N, N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004141 D(ImplicitOps | ModRM | SrcMem | NoAccess),
4142 N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004143 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03004144 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
4145 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
4146 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
4147 check_cr_write),
4148 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
4149 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004150 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004151 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
4152 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004153 N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b),
Avi Kivity3e114eb2012-04-09 18:40:01 +03004154 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004155 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004156 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004157 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004158 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004159 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02004160 I(ImplicitOps | EmulateOnUD, em_sysenter),
4161 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004162 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004163 N, N, N, N, N, N, N, N,
4164 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03004165 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004166 /* 0x50 - 0x5F */
4167 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4168 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004169 N, N, N, N,
4170 N, N, N, N,
4171 N, N, N, N,
4172 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004173 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004174 N, N, N, N,
4175 N, N, N, N,
4176 N, N, N, N,
4177 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004178 /* 0x80 - 0x8F */
Nadav Amit58b70752014-10-24 11:35:09 +03004179 X16(D(SrcImm | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004180 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004181 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004182 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004183 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004184 II(ImplicitOps, em_cpuid, cpuid),
4185 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004186 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4187 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004188 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004189 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004190 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004191 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004192 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4193 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Nadav Amit13e457e2014-10-13 13:04:13 +03004194 GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004195 /* 0xB0 - 0xB7 */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02004196 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable | SrcWrite, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004197 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004198 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004199 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4200 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004201 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004202 /* 0xB8 - 0xBF */
4203 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004204 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004205 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
4206 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004207 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004208 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02004209 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Nadav Amit39f062f2014-11-26 15:47:18 +02004210 N, ID(0, &instr_dual_0f_c3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004211 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004212 /* 0xC8 - 0xCF */
4213 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004214 /* 0xD0 - 0xDF */
4215 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4216 /* 0xE0 - 0xEF */
Alex Williamson0a370272014-07-11 11:56:31 -06004217 N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
4218 N, N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004219 /* 0xF0 - 0xFF */
4220 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4221};
4222
Nadav Amit39f062f2014-11-26 15:47:18 +02004223static const struct instr_dual instr_dual_0f_38_f0 = {
4224 I(DstReg | SrcMem | Mov, em_movbe), N
4225};
4226
4227static const struct instr_dual instr_dual_0f_38_f1 = {
4228 I(DstMem | SrcReg | Mov, em_movbe), N
4229};
4230
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004231static const struct gprefix three_byte_0f_38_f0 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004232 ID(0, &instr_dual_0f_38_f0), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004233};
4234
4235static const struct gprefix three_byte_0f_38_f1 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004236 ID(0, &instr_dual_0f_38_f1), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004237};
4238
4239/*
4240 * Insns below are selected by the prefix which indexed by the third opcode
4241 * byte.
4242 */
4243static const struct opcode opcode_map_0f_38[256] = {
4244 /* 0x00 - 0x7f */
4245 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004246 /* 0x80 - 0xef */
4247 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4248 /* 0xf0 - 0xf1 */
Nadav Amit53bb4f72014-12-07 11:49:42 +02004249 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0),
4250 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004251 /* 0xf2 - 0xff */
4252 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004253};
4254
Avi Kivity73fba5f2010-07-29 15:11:53 +03004255#undef D
4256#undef N
4257#undef G
4258#undef GD
4259#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004260#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004261#undef EXT
Nadav Amit2276b512015-01-26 09:32:24 +02004262#undef MD
Nadav Amit2b42fce2015-01-26 09:32:25 +02004263#undef ID
Avi Kivity73fba5f2010-07-29 15:11:53 +03004264
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004265#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004266#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004267#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004268#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004269#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004270
Avi Kivity9dac77f2011-06-01 15:34:25 +03004271static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004272{
4273 unsigned size;
4274
Avi Kivity9dac77f2011-06-01 15:34:25 +03004275 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004276 if (size == 8)
4277 size = 4;
4278 return size;
4279}
4280
4281static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4282 unsigned size, bool sign_extension)
4283{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004284 int rc = X86EMUL_CONTINUE;
4285
4286 op->type = OP_IMM;
4287 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004288 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004289 /* NB. Immediates are sign-extended as necessary. */
4290 switch (op->bytes) {
4291 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004292 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004293 break;
4294 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004295 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004296 break;
4297 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004298 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004299 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004300 case 8:
4301 op->val = insn_fetch(s64, ctxt);
4302 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004303 }
4304 if (!sign_extension) {
4305 switch (op->bytes) {
4306 case 1:
4307 op->val &= 0xff;
4308 break;
4309 case 2:
4310 op->val &= 0xffff;
4311 break;
4312 case 4:
4313 op->val &= 0xffffffff;
4314 break;
4315 }
4316 }
4317done:
4318 return rc;
4319}
4320
Avi Kivitya99455492011-09-13 10:45:41 +03004321static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4322 unsigned d)
4323{
4324 int rc = X86EMUL_CONTINUE;
4325
4326 switch (d) {
4327 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004328 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03004329 break;
4330 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004331 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03004332 break;
4333 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004334 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004335 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03004336 *op = ctxt->memop;
4337 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004338 if (ctxt->d & BitOp)
Avi Kivitya99455492011-09-13 10:45:41 +03004339 fetch_bit_operand(ctxt);
4340 op->orig_val = op->val;
4341 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004342 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004343 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004344 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03004345 case OpAcc:
4346 op->type = OP_REG;
4347 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004348 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03004349 fetch_register_operand(op);
4350 op->orig_val = op->val;
4351 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004352 case OpAccLo:
4353 op->type = OP_REG;
4354 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4355 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4356 fetch_register_operand(op);
4357 op->orig_val = op->val;
4358 break;
4359 case OpAccHi:
4360 if (ctxt->d & ByteOp) {
4361 op->type = OP_NONE;
4362 break;
4363 }
4364 op->type = OP_REG;
4365 op->bytes = ctxt->op_bytes;
4366 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4367 fetch_register_operand(op);
4368 op->orig_val = op->val;
4369 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004370 case OpDI:
4371 op->type = OP_MEM;
4372 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4373 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004374 register_address(ctxt, VCPU_REGS_RDI);
Avi Kivitya99455492011-09-13 10:45:41 +03004375 op->addr.mem.seg = VCPU_SREG_ES;
4376 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004377 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03004378 break;
4379 case OpDX:
4380 op->type = OP_REG;
4381 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004382 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03004383 fetch_register_operand(op);
4384 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004385 case OpCL:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004386 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004387 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004388 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004389 break;
4390 case OpImmByte:
4391 rc = decode_imm(ctxt, op, 1, true);
4392 break;
4393 case OpOne:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004394 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004395 op->bytes = 1;
4396 op->val = 1;
4397 break;
4398 case OpImm:
4399 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4400 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004401 case OpImm64:
4402 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4403 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004404 case OpMem8:
4405 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004406 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004407 ctxt->memop.addr.reg = decode_register(ctxt,
4408 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004409 fetch_register_operand(&ctxt->memop);
4410 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004411 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004412 case OpMem16:
4413 ctxt->memop.bytes = 2;
4414 goto mem_common;
4415 case OpMem32:
4416 ctxt->memop.bytes = 4;
4417 goto mem_common;
4418 case OpImmU16:
4419 rc = decode_imm(ctxt, op, 2, false);
4420 break;
4421 case OpImmU:
4422 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4423 break;
4424 case OpSI:
4425 op->type = OP_MEM;
4426 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4427 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004428 register_address(ctxt, VCPU_REGS_RSI);
Bandan Das573e80f2014-04-16 12:46:13 -04004429 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03004430 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004431 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004432 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004433 case OpXLat:
4434 op->type = OP_MEM;
4435 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4436 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004437 address_mask(ctxt,
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004438 reg_read(ctxt, VCPU_REGS_RBX) +
4439 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04004440 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004441 op->val = 0;
4442 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004443 case OpImmFAddr:
4444 op->type = OP_IMM;
4445 op->addr.mem.ea = ctxt->_eip;
4446 op->bytes = ctxt->op_bytes + 2;
4447 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4448 break;
4449 case OpMemFAddr:
4450 ctxt->memop.bytes = ctxt->op_bytes + 2;
4451 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004452 case OpES:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004453 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004454 op->val = VCPU_SREG_ES;
4455 break;
4456 case OpCS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004457 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004458 op->val = VCPU_SREG_CS;
4459 break;
4460 case OpSS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004461 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004462 op->val = VCPU_SREG_SS;
4463 break;
4464 case OpDS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004465 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004466 op->val = VCPU_SREG_DS;
4467 break;
4468 case OpFS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004469 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004470 op->val = VCPU_SREG_FS;
4471 break;
4472 case OpGS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004473 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004474 op->val = VCPU_SREG_GS;
4475 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004476 case OpImplicit:
4477 /* Special instructions do their own operand decoding. */
4478 default:
4479 op->type = OP_NONE; /* Disable writeback. */
4480 break;
4481 }
4482
4483done:
4484 return rc;
4485}
4486
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004487int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004488{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004489 int rc = X86EMUL_CONTINUE;
4490 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004491 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004492 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04004493 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03004494 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004495
Avi Kivityf09ed832011-09-13 10:45:40 +03004496 ctxt->memop.type = OP_NONE;
4497 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004498 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02004499 ctxt->fetch.ptr = ctxt->fetch.data;
4500 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004501 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01004502 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004503 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004504 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02004505 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004506 if (rc != X86EMUL_CONTINUE)
4507 return rc;
4508 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004509
4510 switch (mode) {
4511 case X86EMUL_MODE_REAL:
4512 case X86EMUL_MODE_VM86:
4513 case X86EMUL_MODE_PROT16:
4514 def_op_bytes = def_ad_bytes = 2;
4515 break;
4516 case X86EMUL_MODE_PROT32:
4517 def_op_bytes = def_ad_bytes = 4;
4518 break;
4519#ifdef CONFIG_X86_64
4520 case X86EMUL_MODE_PROT64:
4521 def_op_bytes = 4;
4522 def_ad_bytes = 8;
4523 break;
4524#endif
4525 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004526 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004527 }
4528
Avi Kivity9dac77f2011-06-01 15:34:25 +03004529 ctxt->op_bytes = def_op_bytes;
4530 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004531
4532 /* Legacy prefixes. */
4533 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004534 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004535 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004536 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004537 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004538 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004539 break;
4540 case 0x67: /* address-size override */
4541 if (mode == X86EMUL_MODE_PROT64)
4542 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004543 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004544 else
4545 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004546 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004547 break;
4548 case 0x26: /* ES override */
4549 case 0x2e: /* CS override */
4550 case 0x36: /* SS override */
4551 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004552 has_seg_override = true;
4553 ctxt->seg_override = (ctxt->b >> 3) & 3;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004554 break;
4555 case 0x64: /* FS override */
4556 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004557 has_seg_override = true;
4558 ctxt->seg_override = ctxt->b & 7;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004559 break;
4560 case 0x40 ... 0x4f: /* REX */
4561 if (mode != X86EMUL_MODE_PROT64)
4562 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004563 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004564 continue;
4565 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004566 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004567 break;
4568 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004569 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004570 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004571 break;
4572 default:
4573 goto done_prefixes;
4574 }
4575
4576 /* Any legacy prefix after a REX prefix nullifies its effect. */
4577
Avi Kivity9dac77f2011-06-01 15:34:25 +03004578 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004579 }
4580
4581done_prefixes:
4582
4583 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004584 if (ctxt->rex_prefix & 8)
4585 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004586
4587 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004588 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004589 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004590 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004591 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004592 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004593 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004594
4595 /* 0F_38 opcode map */
4596 if (ctxt->b == 0x38) {
4597 ctxt->opcode_len = 3;
4598 ctxt->b = insn_fetch(u8, ctxt);
4599 opcode = opcode_map_0f_38[ctxt->b];
4600 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004601 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004602 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004603
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004604 if (ctxt->d & ModRM)
4605 ctxt->modrm = insn_fetch(u8, ctxt);
4606
Nadav Amit7fe864d2014-06-02 18:34:03 +03004607 /* vex-prefix instructions are not implemented */
4608 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
Nadav Amitd14cb5d2014-11-02 11:54:58 +02004609 (mode == X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) == 0xc0)) {
Nadav Amit7fe864d2014-06-02 18:34:03 +03004610 ctxt->d = NotImpl;
4611 }
4612
Avi Kivity9dac77f2011-06-01 15:34:25 +03004613 while (ctxt->d & GroupMask) {
4614 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004615 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004616 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004617 opcode = opcode.u.group[goffset];
4618 break;
4619 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004620 goffset = (ctxt->modrm >> 3) & 7;
4621 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004622 opcode = opcode.u.gdual->mod3[goffset];
4623 else
4624 opcode = opcode.u.gdual->mod012[goffset];
4625 break;
4626 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004627 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004628 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004629 break;
4630 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004631 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004632 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004633 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004634 switch (simd_prefix) {
4635 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4636 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4637 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4638 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4639 }
4640 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004641 case Escape:
4642 if (ctxt->modrm > 0xbf)
4643 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4644 else
4645 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4646 break;
Nadav Amit39f062f2014-11-26 15:47:18 +02004647 case InstrDual:
4648 if ((ctxt->modrm >> 6) == 3)
4649 opcode = opcode.u.idual->mod3;
4650 else
4651 opcode = opcode.u.idual->mod012;
4652 break;
Nadav Amit2276b512015-01-26 09:32:24 +02004653 case ModeDual:
4654 if (ctxt->mode == X86EMUL_MODE_PROT64)
4655 opcode = opcode.u.mdual->mode64;
4656 else
4657 opcode = opcode.u.mdual->mode32;
4658 break;
Avi Kivity46561642011-04-24 14:09:59 +03004659 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004660 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004661 }
Avi Kivity46561642011-04-24 14:09:59 +03004662
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004663 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004664 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004665 }
4666
Paolo Bonzinie24186e2014-03-27 12:00:57 +01004667 /* Unrecognised? */
4668 if (ctxt->d == 0)
4669 return EMULATION_FAILED;
4670
Avi Kivity9dac77f2011-06-01 15:34:25 +03004671 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004672
Nadav Amit3a6095a2014-08-13 16:50:13 +03004673 if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD)))
4674 return EMULATION_FAILED;
4675
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004676 if (unlikely(ctxt->d &
Nadav Amited9aad22014-11-02 11:55:00 +02004677 (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch|
4678 No16))) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004679 /*
4680 * These are copied unconditionally here, and checked unconditionally
4681 * in x86_emulate_insn.
4682 */
4683 ctxt->check_perm = opcode.check_perm;
4684 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004685
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004686 if (ctxt->d & NotImpl)
4687 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004688
Nadav Amit58b70752014-10-24 11:35:09 +03004689 if (mode == X86EMUL_MODE_PROT64) {
4690 if (ctxt->op_bytes == 4 && (ctxt->d & Stack))
4691 ctxt->op_bytes = 8;
4692 else if (ctxt->d & NearBranch)
4693 ctxt->op_bytes = 8;
4694 }
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004695
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004696 if (ctxt->d & Op3264) {
4697 if (mode == X86EMUL_MODE_PROT64)
4698 ctxt->op_bytes = 8;
4699 else
4700 ctxt->op_bytes = 4;
4701 }
4702
Nadav Amited9aad22014-11-02 11:55:00 +02004703 if ((ctxt->d & No16) && ctxt->op_bytes == 2)
4704 ctxt->op_bytes = 4;
4705
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004706 if (ctxt->d & Sse)
4707 ctxt->op_bytes = 16;
4708 else if (ctxt->d & Mmx)
4709 ctxt->op_bytes = 8;
4710 }
Avi Kivity1253791d2011-03-29 11:41:27 +02004711
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004712 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004713 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004714 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04004715 if (!has_seg_override) {
4716 has_seg_override = true;
4717 ctxt->seg_override = ctxt->modrm_seg;
4718 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004719 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004720 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004721 if (rc != X86EMUL_CONTINUE)
4722 goto done;
4723
Bandan Das573e80f2014-04-16 12:46:13 -04004724 if (!has_seg_override)
4725 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004726
Bandan Das573e80f2014-04-16 12:46:13 -04004727 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004728
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004729 /*
4730 * Decode and fetch the source operand: register, memory
4731 * or immediate.
4732 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004733 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004734 if (rc != X86EMUL_CONTINUE)
4735 goto done;
4736
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004737 /*
4738 * Decode and fetch the second source operand: register, memory
4739 * or immediate.
4740 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004741 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004742 if (rc != X86EMUL_CONTINUE)
4743 goto done;
4744
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004745 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03004746 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004747
Bandan Das41061cd2014-04-16 12:46:14 -04004748 if (ctxt->rip_relative)
Nadav Amit1c1c35a2014-11-19 17:43:09 +02004749 ctxt->memopp->addr.mem.ea = address_mask(ctxt,
4750 ctxt->memopp->addr.mem.ea + ctxt->_eip);
Avi Kivitycb16c342011-06-19 19:21:11 +03004751
Paolo Bonzinia430c912014-10-23 14:54:14 +02004752done:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004753 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004754}
4755
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004756bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4757{
4758 return ctxt->d & PageTable;
4759}
4760
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004761static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4762{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004763 /* The second termination condition only applies for REPE
4764 * and REPNE. Test if the repeat string operation prefix is
4765 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4766 * corresponding termination condition according to:
4767 * - if REPE/REPZ and ZF = 0 then done
4768 * - if REPNE/REPNZ and ZF = 1 then done
4769 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004770 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4771 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4772 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004773 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004774 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004775 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4776 return true;
4777
4778 return false;
4779}
4780
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004781static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4782{
4783 bool fault = false;
4784
4785 ctxt->ops->get_fpu(ctxt);
4786 asm volatile("1: fwait \n\t"
4787 "2: \n\t"
4788 ".pushsection .fixup,\"ax\" \n\t"
4789 "3: \n\t"
4790 "movb $1, %[fault] \n\t"
4791 "jmp 2b \n\t"
4792 ".popsection \n\t"
4793 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004794 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004795 ctxt->ops->put_fpu(ctxt);
4796
4797 if (unlikely(fault))
4798 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4799
4800 return X86EMUL_CONTINUE;
4801}
4802
4803static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4804 struct operand *op)
4805{
4806 if (op->type == OP_MM)
4807 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4808}
4809
Avi Kivitye28bbd42013-01-04 16:18:48 +02004810static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4811{
4812 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivityb9fa4092013-02-09 11:31:48 +02004813 if (!(ctxt->d & ByteOp))
4814 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Avi Kivitye28bbd42013-01-04 16:18:48 +02004815 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004816 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
4817 [fastop]"+S"(fop)
4818 : "c"(ctxt->src2.val));
Avi Kivitye28bbd42013-01-04 16:18:48 +02004819 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004820 if (!fop) /* exception is returned in fop variable */
4821 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02004822 return X86EMUL_CONTINUE;
4823}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004824
Bandan Das14985072014-04-16 12:46:09 -04004825void init_decode_cache(struct x86_emulate_ctxt *ctxt)
4826{
Bandan Das573e80f2014-04-16 12:46:13 -04004827 memset(&ctxt->rip_relative, 0,
4828 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04004829
Bandan Das14985072014-04-16 12:46:09 -04004830 ctxt->io_read.pos = 0;
4831 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04004832 ctxt->mem_read.end = 0;
4833}
4834
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004835int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004836{
Mathias Krause0225fb52012-08-30 01:30:16 +02004837 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004838 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004839 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004840
Avi Kivity9dac77f2011-06-01 15:34:25 +03004841 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004842
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004843 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004844 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004845 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004846 goto done;
4847 }
4848
Avi Kivity9dac77f2011-06-01 15:34:25 +03004849 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004850 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004851 goto done;
4852 }
4853
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004854 if (unlikely(ctxt->d &
4855 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
4856 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4857 (ctxt->d & Undefined)) {
4858 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004859 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004860 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004861
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004862 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4863 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
4864 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004865 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004866 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004867
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004868 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
4869 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004870 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004871 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004872
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004873 if (ctxt->d & Mmx) {
4874 rc = flush_pending_x87_faults(ctxt);
4875 if (rc != X86EMUL_CONTINUE)
4876 goto done;
4877 /*
4878 * Now that we know the fpu is exception safe, we can fetch
4879 * operands from it.
4880 */
4881 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4882 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4883 if (!(ctxt->d & Mov))
4884 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4885 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004886
Bandan Das685bbf42014-04-16 12:46:10 -04004887 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004888 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4889 X86_ICPT_PRE_EXCEPT);
4890 if (rc != X86EMUL_CONTINUE)
4891 goto done;
4892 }
4893
Nadav Amit64a38292014-12-10 11:19:04 +02004894 /* Instruction can only be executed in protected mode */
4895 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
4896 rc = emulate_ud(ctxt);
4897 goto done;
4898 }
4899
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004900 /* Privileged instruction can be executed only in CPL=0 */
4901 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Nadav Amit68efa762014-06-18 17:19:35 +03004902 if (ctxt->d & PrivUD)
4903 rc = emulate_ud(ctxt);
4904 else
4905 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004906 goto done;
4907 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004908
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004909 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04004910 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004911 rc = ctxt->check_perm(ctxt);
4912 if (rc != X86EMUL_CONTINUE)
4913 goto done;
4914 }
4915
Bandan Das685bbf42014-04-16 12:46:10 -04004916 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004917 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4918 X86_ICPT_POST_EXCEPT);
4919 if (rc != X86EMUL_CONTINUE)
4920 goto done;
4921 }
4922
4923 if (ctxt->rep_prefix && (ctxt->d & String)) {
4924 /* All REP prefixes have the same first termination condition */
4925 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
4926 ctxt->eip = ctxt->_eip;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004927 ctxt->eflags &= ~EFLG_RF;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004928 goto done;
4929 }
4930 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004931 }
4932
Avi Kivity9dac77f2011-06-01 15:34:25 +03004933 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4934 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4935 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004936 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004937 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004938 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004939 }
4940
Avi Kivity9dac77f2011-06-01 15:34:25 +03004941 if (ctxt->src2.type == OP_MEM) {
4942 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4943 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004944 if (rc != X86EMUL_CONTINUE)
4945 goto done;
4946 }
4947
Avi Kivity9dac77f2011-06-01 15:34:25 +03004948 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004949 goto special_insn;
4950
4951
Avi Kivity9dac77f2011-06-01 15:34:25 +03004952 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004953 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004954 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4955 &ctxt->dst.val, ctxt->dst.bytes);
Nadav Amitc205fb72014-12-25 02:52:16 +02004956 if (rc != X86EMUL_CONTINUE) {
Paolo Bonzinid44e1212015-02-09 10:02:05 +01004957 if (!(ctxt->d & NoWrite) &&
4958 rc == X86EMUL_PROPAGATE_FAULT &&
Nadav Amitc205fb72014-12-25 02:52:16 +02004959 ctxt->exception.vector == PF_VECTOR)
4960 ctxt->exception.error_code |= PFERR_WRITE_MASK;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004961 goto done;
Nadav Amitc205fb72014-12-25 02:52:16 +02004962 }
Avi Kivity038e51d2007-01-22 20:40:40 -08004963 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004964 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004965
Avi Kivity018a98d2007-11-27 19:30:56 +02004966special_insn:
4967
Bandan Das685bbf42014-04-16 12:46:10 -04004968 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004969 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004970 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004971 if (rc != X86EMUL_CONTINUE)
4972 goto done;
4973 }
4974
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004975 if (ctxt->rep_prefix && (ctxt->d & String))
4976 ctxt->eflags |= EFLG_RF;
4977 else
4978 ctxt->eflags &= ~EFLG_RF;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004979
Avi Kivity9dac77f2011-06-01 15:34:25 +03004980 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004981 if (ctxt->d & Fastop) {
4982 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4983 rc = fastop(ctxt, fop);
4984 if (rc != X86EMUL_CONTINUE)
4985 goto done;
4986 goto writeback;
4987 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004988 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004989 if (rc != X86EMUL_CONTINUE)
4990 goto done;
4991 goto writeback;
4992 }
4993
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004994 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004995 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004996 else if (ctxt->opcode_len == 3)
4997 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004998
Avi Kivity9dac77f2011-06-01 15:34:25 +03004999 switch (ctxt->b) {
Gleb Natapovb2833e32009-04-12 13:36:30 +03005000 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005001 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005002 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005003 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03005004 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005005 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03005006 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03005007 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005008 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03005009 ctxt->dst.type = OP_NONE;
5010 else
5011 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09005012 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005013 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005014 switch (ctxt->op_bytes) {
5015 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
5016 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
5017 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005018 }
5019 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005020 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005021 rc = emulate_int(ctxt, 3);
5022 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005023 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005024 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005025 break;
5026 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005027 if (ctxt->eflags & EFLG_OF)
5028 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005029 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005030 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09005031 case 0xeb: /* jmp rel short */
Nadav Amit234f3ce2014-09-18 22:39:38 +03005032 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity9dac77f2011-06-01 15:34:25 +03005033 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005034 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005035 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03005036 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03005037 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005038 case 0xf5: /* cmc */
5039 /* complement carry flag from eflags reg */
5040 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005041 break;
5042 case 0xf8: /* clc */
5043 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005044 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03005045 case 0xf9: /* stc */
5046 ctxt->eflags |= EFLG_CF;
5047 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005048 case 0xfc: /* cld */
5049 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005050 break;
5051 case 0xfd: /* std */
5052 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005053 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005054 default:
5055 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005056 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005057
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005058 if (rc != X86EMUL_CONTINUE)
5059 goto done;
5060
Avi Kivity018a98d2007-11-27 19:30:56 +02005061writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02005062 if (ctxt->d & SrcWrite) {
5063 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
5064 rc = writeback(ctxt, &ctxt->src);
5065 if (rc != X86EMUL_CONTINUE)
5066 goto done;
5067 }
Nadav Amitee212292014-06-15 16:12:58 +03005068 if (!(ctxt->d & NoWrite)) {
5069 rc = writeback(ctxt, &ctxt->dst);
5070 if (rc != X86EMUL_CONTINUE)
5071 goto done;
5072 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005073
Gleb Natapov5cd21912010-03-18 15:20:26 +02005074 /*
5075 * restore dst type in case the decoding will be reused
5076 * (happens for string instruction )
5077 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005078 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005079
Avi Kivity9dac77f2011-06-01 15:34:25 +03005080 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005081 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02005082
Avi Kivity9dac77f2011-06-01 15:34:25 +03005083 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005084 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02005085
Avi Kivity9dac77f2011-06-01 15:34:25 +03005086 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005087 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005088 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005089 if ((ctxt->d & SrcMask) == SrcSI)
5090 count = ctxt->src.count;
5091 else
5092 count = ctxt->dst.count;
Paolo Bonzini01485a22014-11-19 18:25:08 +01005093 register_address_increment(ctxt, VCPU_REGS_RCX, -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005094
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005095 if (!string_insn_completed(ctxt)) {
5096 /*
5097 * Re-enter guest when pio read ahead buffer is empty
5098 * or, if it is not used, after each 1024 iteration.
5099 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005100 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005101 (r->end == 0 || r->end != r->pos)) {
5102 /*
5103 * Reset read cache. Usually happens before
5104 * decode, but since instruction is restarted
5105 * we have to do it here.
5106 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005107 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005108 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005109 return EMULATION_RESTART;
5110 }
5111 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03005112 }
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03005113 ctxt->eflags &= ~EFLG_RF;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005114 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005115
Avi Kivity9dac77f2011-06-01 15:34:25 +03005116 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02005117
5118done:
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005119 if (rc == X86EMUL_PROPAGATE_FAULT) {
5120 WARN_ON(ctxt->exception.vector > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +02005121 ctxt->have_exception = true;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005122 }
Joerg Roedel775fde82011-04-04 12:39:24 +02005123 if (rc == X86EMUL_INTERCEPTED)
5124 return EMULATION_INTERCEPTED;
5125
Avi Kivitydd856ef2012-08-27 23:46:17 +03005126 if (rc == X86EMUL_CONTINUE)
5127 writeback_registers(ctxt);
5128
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005129 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005130
5131twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005132 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02005133 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02005134 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005135 break;
5136 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02005137 case 0x0d: /* GrpP (prefetch) */
5138 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02005139 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02005140 break;
5141 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005142 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02005143 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005144 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005145 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005146 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005147 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03005148 if (test_cc(ctxt->b, ctxt->eflags))
5149 ctxt->dst.val = ctxt->src.val;
5150 else if (ctxt->mode != X86EMUL_MODE_PROT64 ||
5151 ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005152 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08005153 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03005154 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03005155 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005156 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005157 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08005158 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005159 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08005160 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005161 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005162 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005163 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03005164 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005165 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005166 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005167 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005168 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03005169 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005170 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005171 default:
5172 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005173 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005174
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005175threebyte_insn:
5176
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005177 if (rc != X86EMUL_CONTINUE)
5178 goto done;
5179
Avi Kivity6aa8b732006-12-10 02:21:36 -08005180 goto writeback;
5181
5182cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02005183 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005184}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005185
5186void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
5187{
5188 invalidate_registers(ctxt);
5189}
5190
5191void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
5192{
5193 writeback_registers(ctxt);
5194}