blob: 4961dc5eb303744a7c5af9b571f99b86e16f3070 [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
Nadav Amit6fd8e122015-03-30 15:39:20 +0300481static void assign_register(unsigned long *reg, u64 val, int bytes)
482{
483 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
484 switch (bytes) {
485 case 1:
486 *(u8 *)reg = (u8)val;
487 break;
488 case 2:
489 *(u16 *)reg = (u16)val;
490 break;
491 case 4:
492 *reg = (u32)val;
493 break; /* 64b: zero-extend */
494 case 8:
495 *reg = val;
496 break;
497 }
498}
499
Avi Kivity9dac77f2011-06-01 15:34:25 +0300500static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800501{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300502 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800503}
504
Avi Kivityf47cfa32012-06-07 17:49:24 +0300505static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
506{
507 u16 sel;
508 struct desc_struct ss;
509
510 if (ctxt->mode == X86EMUL_MODE_PROT64)
511 return ~0UL;
512 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
513 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
514}
515
Avi Kivity612e89f2012-06-12 20:03:23 +0300516static int stack_size(struct x86_emulate_ctxt *ctxt)
517{
518 return (__fls(stack_mask(ctxt)) + 1) >> 3;
519}
520
Avi Kivity6aa8b732006-12-10 02:21:36 -0800521/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800522static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300523address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800524{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300525 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800526 return reg;
527 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300528 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800529}
530
531static inline unsigned long
Paolo Bonzini01485a22014-11-19 18:25:08 +0100532register_address(struct x86_emulate_ctxt *ctxt, int reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800533{
Paolo Bonzini01485a22014-11-19 18:25:08 +0100534 return address_mask(ctxt, reg_read(ctxt, reg));
Harvey Harrisone4706772008-02-19 07:40:38 -0800535}
536
Avi Kivity5ad105e2012-08-19 14:34:31 +0300537static void masked_increment(ulong *reg, ulong mask, int inc)
538{
539 assign_masked(reg, *reg + inc, mask);
540}
541
Harvey Harrison7a9572752008-02-19 07:40:41 -0800542static inline void
Paolo Bonzini01485a22014-11-19 18:25:08 +0100543register_address_increment(struct x86_emulate_ctxt *ctxt, int reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800544{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300545 ulong mask;
546
Avi Kivity9dac77f2011-06-01 15:34:25 +0300547 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300548 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800549 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300550 mask = ad_mask(ctxt);
Paolo Bonzini01485a22014-11-19 18:25:08 +0100551 masked_increment(reg_rmw(ctxt, reg), mask, inc);
Avi Kivity5ad105e2012-08-19 14:34:31 +0300552}
553
554static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
555{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300556 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800557}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800558
Avi Kivity56697682011-04-03 14:08:51 +0300559static u32 desc_limit_scaled(struct desc_struct *desc)
560{
561 u32 limit = get_desc_limit(desc);
562
563 return desc->g ? (limit << 12) | 0xfff : limit;
564}
565
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900566static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300567{
568 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
569 return 0;
570
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900571 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300572}
573
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200574static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
575 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300576{
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +0200577 WARN_ON(vec > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +0200578 ctxt->exception.vector = vec;
579 ctxt->exception.error_code = error;
580 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200581 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300582}
583
Joerg Roedel3b88e412011-04-04 12:39:29 +0200584static int emulate_db(struct x86_emulate_ctxt *ctxt)
585{
586 return emulate_exception(ctxt, DB_VECTOR, 0, false);
587}
588
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200589static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300590{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200591 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300592}
593
Avi Kivity618ff152011-04-03 12:32:09 +0300594static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
595{
596 return emulate_exception(ctxt, SS_VECTOR, err, true);
597}
598
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200599static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300600{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200601 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300602}
603
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200604static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300605{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200606 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300607}
608
Avi Kivity34d1f492010-08-26 11:59:01 +0300609static int emulate_de(struct x86_emulate_ctxt *ctxt)
610{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200611 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300612}
613
Avi Kivity1253791d2011-03-29 11:41:27 +0200614static int emulate_nm(struct x86_emulate_ctxt *ctxt)
615{
616 return emulate_exception(ctxt, NM_VECTOR, 0, false);
617}
618
Avi Kivity1aa36612011-04-27 13:20:30 +0300619static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
620{
621 u16 selector;
622 struct desc_struct desc;
623
624 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
625 return selector;
626}
627
628static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
629 unsigned seg)
630{
631 u16 dummy;
632 u32 base3;
633 struct desc_struct desc;
634
635 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
636 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
637}
638
Avi Kivity1c11b372012-04-09 18:39:59 +0300639/*
640 * x86 defines three classes of vector instructions: explicitly
641 * aligned, explicitly unaligned, and the rest, which change behaviour
642 * depending on whether they're AVX encoded or not.
643 *
644 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
645 * subject to the same check.
646 */
647static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
648{
649 if (likely(size < 16))
650 return false;
651
652 if (ctxt->d & Aligned)
653 return true;
654 else if (ctxt->d & Unaligned)
655 return false;
656 else if (ctxt->d & Avx)
657 return false;
658 else
659 return true;
660}
661
Paolo Bonzinid09155d2014-10-27 14:54:44 +0100662static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
663 struct segmented_address addr,
664 unsigned *max_size, unsigned size,
665 bool write, bool fetch,
Nadav Amitd50eaa12014-11-19 17:43:11 +0200666 enum x86emul_mode mode, ulong *linear)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300667{
Avi Kivity618ff152011-04-03 12:32:09 +0300668 struct desc_struct desc;
669 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300670 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300671 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300672 u16 sel;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300673
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900674 la = seg_base(ctxt, addr.seg) + addr.ea;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100675 *max_size = 0;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200676 switch (mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300677 case X86EMUL_MODE_PROT64:
Nadav Amit4be4de72014-09-18 22:39:40 +0300678 if (is_noncanonical_address(la))
Nadav Amitabc7d8a2014-11-19 17:43:12 +0200679 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100680
681 *max_size = min_t(u64, ~0u, (1ull << 48) - la);
682 if (size > *max_size)
683 goto bad;
Avi Kivity618ff152011-04-03 12:32:09 +0300684 break;
685 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300686 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
687 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300688 if (!usable)
689 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200690 /* code segment in protected mode or read-only data segment */
691 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
692 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300693 goto bad;
694 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400695 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300696 goto bad;
697 lim = desc_limit_scaled(&desc);
Paolo Bonzini997b0412014-11-19 18:33:38 +0100698 if (!(desc.type & 8) && (desc.type & 4)) {
Guo Chaofc058682012-06-28 15:19:51 +0800699 /* expand-down segment */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100700 if (addr.ea <= lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300701 goto bad;
702 lim = desc.d ? 0xffffffff : 0xffff;
Avi Kivity618ff152011-04-03 12:32:09 +0300703 }
Paolo Bonzini997b0412014-11-19 18:33:38 +0100704 if (addr.ea > lim)
705 goto bad;
Nadav Amitbac155312015-01-26 09:32:26 +0200706 if (lim == 0xffffffff)
707 *max_size = ~0u;
708 else {
709 *max_size = (u64)lim + 1 - addr.ea;
710 if (size > *max_size)
711 goto bad;
712 }
Nadav Amit31ff6482014-11-19 17:43:13 +0200713 la &= (u32)-1;
Avi Kivity618ff152011-04-03 12:32:09 +0300714 break;
715 }
Avi Kivity1c11b372012-04-09 18:39:59 +0300716 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
717 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300718 *linear = la;
719 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300720bad:
721 if (addr.seg == VCPU_SREG_SS)
Paolo Bonzini36061892014-10-27 14:40:49 +0100722 return emulate_ss(ctxt, 0);
Avi Kivity618ff152011-04-03 12:32:09 +0300723 else
Paolo Bonzini36061892014-10-27 14:40:49 +0100724 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300725}
726
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400727static int linearize(struct x86_emulate_ctxt *ctxt,
728 struct segmented_address addr,
729 unsigned size, bool write,
730 ulong *linear)
731{
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100732 unsigned max_size;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200733 return __linearize(ctxt, addr, &max_size, size, write, false,
734 ctxt->mode, linear);
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400735}
736
Nadav Amitd50eaa12014-11-19 17:43:11 +0200737static inline int assign_eip(struct x86_emulate_ctxt *ctxt, ulong dst,
738 enum x86emul_mode mode)
739{
740 ulong linear;
741 int rc;
742 unsigned max_size;
743 struct segmented_address addr = { .seg = VCPU_SREG_CS,
744 .ea = dst };
745
746 if (ctxt->op_bytes != sizeof(unsigned long))
747 addr.ea = dst & ((1UL << (ctxt->op_bytes << 3)) - 1);
748 rc = __linearize(ctxt, addr, &max_size, 1, false, true, mode, &linear);
749 if (rc == X86EMUL_CONTINUE)
750 ctxt->_eip = addr.ea;
751 return rc;
752}
753
754static inline int assign_eip_near(struct x86_emulate_ctxt *ctxt, ulong dst)
755{
756 return assign_eip(ctxt, dst, ctxt->mode);
757}
758
759static int assign_eip_far(struct x86_emulate_ctxt *ctxt, ulong dst,
760 const struct desc_struct *cs_desc)
761{
762 enum x86emul_mode mode = ctxt->mode;
Nadav Amit82268082015-01-26 09:32:27 +0200763 int rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200764
765#ifdef CONFIG_X86_64
Nadav Amit82268082015-01-26 09:32:27 +0200766 if (ctxt->mode >= X86EMUL_MODE_PROT16) {
767 if (cs_desc->l) {
768 u64 efer = 0;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200769
Nadav Amit82268082015-01-26 09:32:27 +0200770 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
771 if (efer & EFER_LMA)
772 mode = X86EMUL_MODE_PROT64;
773 } else
774 mode = X86EMUL_MODE_PROT32; /* temporary value */
Nadav Amitd50eaa12014-11-19 17:43:11 +0200775 }
776#endif
777 if (mode == X86EMUL_MODE_PROT16 || mode == X86EMUL_MODE_PROT32)
778 mode = cs_desc->d ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
Nadav Amit82268082015-01-26 09:32:27 +0200779 rc = assign_eip(ctxt, dst, mode);
780 if (rc == X86EMUL_CONTINUE)
781 ctxt->mode = mode;
782 return rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200783}
784
785static inline int jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
786{
787 return assign_eip_near(ctxt, ctxt->_eip + rel);
788}
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400789
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200790static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
791 struct segmented_address addr,
792 void *data,
793 unsigned size)
794{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200795 int rc;
796 ulong linear;
797
Avi Kivity83b87952011-04-03 11:31:19 +0300798 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200799 if (rc != X86EMUL_CONTINUE)
800 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300801 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200802}
803
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900804/*
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200805 * Prefetch the remaining bytes of the instruction without crossing page
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900806 * boundary if they are not in fetch_cache yet.
807 */
Paolo Bonzini9506d572014-05-06 13:05:25 +0200808static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
Avi Kivity62266862007-11-20 13:15:52 +0200809{
Avi Kivity62266862007-11-20 13:15:52 +0200810 int rc;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100811 unsigned size, max_size;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200812 unsigned long linear;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200813 int cur_size = ctxt->fetch.end - ctxt->fetch.data;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200814 struct segmented_address addr = { .seg = VCPU_SREG_CS,
Paolo Bonzini17052f12014-05-06 16:33:01 +0200815 .ea = ctxt->eip + cur_size };
816
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100817 /*
818 * We do not know exactly how many bytes will be needed, and
819 * __linearize is expensive, so fetch as much as possible. We
820 * just have to avoid going beyond the 15 byte limit, the end
821 * of the segment, or the end of the page.
822 *
823 * __linearize is called with size 0 so that it does not do any
824 * boundary check itself. Instead, we use max_size to check
825 * against op_size.
826 */
Nadav Amitd50eaa12014-11-19 17:43:11 +0200827 rc = __linearize(ctxt, addr, &max_size, 0, false, true, ctxt->mode,
828 &linear);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200829 if (unlikely(rc != X86EMUL_CONTINUE))
830 return rc;
831
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100832 size = min_t(unsigned, 15UL ^ cur_size, max_size);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200833 size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear));
Paolo Bonzini5cfc7e02014-05-06 13:05:25 +0200834
835 /*
836 * One instruction can only straddle two pages,
837 * and one has been loaded at the beginning of
838 * x86_decode_insn. So, if not enough bytes
839 * still, we must have hit the 15-byte boundary.
840 */
841 if (unlikely(size < op_size))
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100842 return emulate_gp(ctxt, 0);
843
Paolo Bonzini17052f12014-05-06 16:33:01 +0200844 rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end,
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200845 size, &ctxt->exception);
846 if (unlikely(rc != X86EMUL_CONTINUE))
847 return rc;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200848 ctxt->fetch.end += size;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900849 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200850}
851
Paolo Bonzini9506d572014-05-06 13:05:25 +0200852static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt,
853 unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200854{
Nadav Amit08da44a2014-10-03 01:10:04 +0300855 unsigned done_size = ctxt->fetch.end - ctxt->fetch.ptr;
856
857 if (unlikely(done_size < size))
858 return __do_insn_fetch_bytes(ctxt, size - done_size);
Paolo Bonzini9506d572014-05-06 13:05:25 +0200859 else
860 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200861}
862
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900863/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900864#define insn_fetch(_type, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200865({ _type _x; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200866 \
867 rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900868 if (rc != X86EMUL_CONTINUE) \
869 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200870 ctxt->_eip += sizeof(_type); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200871 _x = *(_type __aligned(1) *) ctxt->fetch.ptr; \
872 ctxt->fetch.ptr += sizeof(_type); \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200873 _x; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900874})
875
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900876#define insn_fetch_arr(_arr, _size, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200877({ \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200878 rc = do_insn_fetch_bytes(_ctxt, _size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900879 if (rc != X86EMUL_CONTINUE) \
880 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200881 ctxt->_eip += (_size); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200882 memcpy(_arr, ctxt->fetch.ptr, _size); \
883 ctxt->fetch.ptr += (_size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900884})
885
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000886/*
887 * Given the 'reg' portion of a ModRM byte, and a register block, return a
888 * pointer into the block that addresses the relevant register.
889 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
890 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300891static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200892 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800893{
894 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200895 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800896
Avi Kivity6aa8b732006-12-10 02:21:36 -0800897 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300898 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
899 else
900 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800901 return p;
902}
903
904static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200905 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800906 u16 *size, unsigned long *address, int op_bytes)
907{
908 int rc;
909
910 if (op_bytes == 2)
911 op_bytes = 3;
912 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200913 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900914 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800915 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200916 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200917 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800918 return rc;
919}
920
Avi Kivity34b77652013-01-19 19:51:56 +0200921FASTOP2(add);
922FASTOP2(or);
923FASTOP2(adc);
924FASTOP2(sbb);
925FASTOP2(and);
926FASTOP2(sub);
927FASTOP2(xor);
928FASTOP2(cmp);
929FASTOP2(test);
930
Avi Kivityb9fa4092013-02-09 11:31:48 +0200931FASTOP1SRC2(mul, mul_ex);
932FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200933FASTOP1SRC2EX(div, div_ex);
934FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +0200935
Avi Kivity34b77652013-01-19 19:51:56 +0200936FASTOP3WCL(shld);
937FASTOP3WCL(shrd);
938
939FASTOP2W(imul);
940
941FASTOP1(not);
942FASTOP1(neg);
943FASTOP1(inc);
944FASTOP1(dec);
945
946FASTOP2CL(rol);
947FASTOP2CL(ror);
948FASTOP2CL(rcl);
949FASTOP2CL(rcr);
950FASTOP2CL(shl);
951FASTOP2CL(shr);
952FASTOP2CL(sar);
953
954FASTOP2W(bsf);
955FASTOP2W(bsr);
956FASTOP2W(bt);
957FASTOP2W(bts);
958FASTOP2W(btr);
959FASTOP2W(btc);
960
Avi Kivitye47a5f52013-02-09 11:31:51 +0200961FASTOP2(xadd);
962
Nadav Amit5aca3722014-11-02 11:54:50 +0200963FASTOP2R(cmp, cmp_r);
964
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200965static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300966{
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200967 u8 rc;
968 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300969
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200970 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +0200971 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200972 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
973 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300974}
975
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300976static void fetch_register_operand(struct operand *op)
977{
978 switch (op->bytes) {
979 case 1:
980 op->val = *(u8 *)op->addr.reg;
981 break;
982 case 2:
983 op->val = *(u16 *)op->addr.reg;
984 break;
985 case 4:
986 op->val = *(u32 *)op->addr.reg;
987 break;
988 case 8:
989 op->val = *(u64 *)op->addr.reg;
990 break;
991 }
992}
993
Avi Kivity1253791d2011-03-29 11:41:27 +0200994static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
995{
996 ctxt->ops->get_fpu(ctxt);
997 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200998 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
999 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
1000 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
1001 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
1002 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
1003 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
1004 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
1005 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001006#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001007 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
1008 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
1009 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
1010 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
1011 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
1012 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
1013 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
1014 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001015#endif
1016 default: BUG();
1017 }
1018 ctxt->ops->put_fpu(ctxt);
1019}
1020
1021static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
1022 int reg)
1023{
1024 ctxt->ops->get_fpu(ctxt);
1025 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001026 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
1027 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
1028 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
1029 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
1030 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
1031 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
1032 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
1033 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001034#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001035 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
1036 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
1037 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
1038 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
1039 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
1040 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
1041 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
1042 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001043#endif
1044 default: BUG();
1045 }
1046 ctxt->ops->put_fpu(ctxt);
1047}
1048
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001049static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1050{
1051 ctxt->ops->get_fpu(ctxt);
1052 switch (reg) {
1053 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1054 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1055 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1056 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1057 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1058 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1059 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1060 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1061 default: BUG();
1062 }
1063 ctxt->ops->put_fpu(ctxt);
1064}
1065
1066static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1067{
1068 ctxt->ops->get_fpu(ctxt);
1069 switch (reg) {
1070 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1071 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1072 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1073 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1074 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1075 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1076 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1077 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1078 default: BUG();
1079 }
1080 ctxt->ops->put_fpu(ctxt);
1081}
1082
Gleb Natapov045a2822012-12-20 16:57:43 +02001083static int em_fninit(struct x86_emulate_ctxt *ctxt)
1084{
1085 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1086 return emulate_nm(ctxt);
1087
1088 ctxt->ops->get_fpu(ctxt);
1089 asm volatile("fninit");
1090 ctxt->ops->put_fpu(ctxt);
1091 return X86EMUL_CONTINUE;
1092}
1093
1094static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1095{
1096 u16 fcw;
1097
1098 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1099 return emulate_nm(ctxt);
1100
1101 ctxt->ops->get_fpu(ctxt);
1102 asm volatile("fnstcw %0": "+m"(fcw));
1103 ctxt->ops->put_fpu(ctxt);
1104
Gleb Natapov045a2822012-12-20 16:57:43 +02001105 ctxt->dst.val = fcw;
1106
1107 return X86EMUL_CONTINUE;
1108}
1109
1110static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1111{
1112 u16 fsw;
1113
1114 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1115 return emulate_nm(ctxt);
1116
1117 ctxt->ops->get_fpu(ctxt);
1118 asm volatile("fnstsw %0": "+m"(fsw));
1119 ctxt->ops->put_fpu(ctxt);
1120
Gleb Natapov045a2822012-12-20 16:57:43 +02001121 ctxt->dst.val = fsw;
1122
1123 return X86EMUL_CONTINUE;
1124}
1125
Avi Kivity1253791d2011-03-29 11:41:27 +02001126static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001127 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001128{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001129 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001130
Avi Kivity9dac77f2011-06-01 15:34:25 +03001131 if (!(ctxt->d & ModRM))
1132 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity1253791d2011-03-29 11:41:27 +02001133
Avi Kivity9dac77f2011-06-01 15:34:25 +03001134 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001135 op->type = OP_XMM;
1136 op->bytes = 16;
1137 op->addr.xmm = reg;
1138 read_sse_reg(ctxt, &op->vec_val, reg);
1139 return;
1140 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001141 if (ctxt->d & Mmx) {
1142 reg &= 7;
1143 op->type = OP_MM;
1144 op->bytes = 8;
1145 op->addr.mm = reg;
1146 return;
1147 }
Avi Kivity1253791d2011-03-29 11:41:27 +02001148
Avi Kivity3c118e22007-10-31 10:27:04 +02001149 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001150 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1151 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1152
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001153 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001154 op->orig_val = op->val;
1155}
1156
Avi Kivitya6e34072012-06-10 17:15:39 +03001157static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1158{
1159 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1160 ctxt->modrm_seg = VCPU_SREG_SS;
1161}
1162
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001163static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001164 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001165{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001166 u8 sib;
Bandan Das02357bd2014-04-16 12:46:11 -04001167 int index_reg, base_reg, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001168 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001169 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001170
Bandan Das02357bd2014-04-16 12:46:11 -04001171 ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */
1172 index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */
1173 base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001174
Bandan Das02357bd2014-04-16 12:46:11 -04001175 ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001176 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
Bandan Das02357bd2014-04-16 12:46:11 -04001177 ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001178 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001179
Nadav Amit9b88ae92014-05-25 23:05:21 +03001180 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001181 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001182 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001183 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001184 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001185 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001186 op->type = OP_XMM;
1187 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001188 op->addr.xmm = ctxt->modrm_rm;
1189 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001190 return rc;
1191 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001192 if (ctxt->d & Mmx) {
1193 op->type = OP_MM;
1194 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001195 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001196 return rc;
1197 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001198 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001199 return rc;
1200 }
1201
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001202 op->type = OP_MEM;
1203
Avi Kivity9dac77f2011-06-01 15:34:25 +03001204 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001205 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1206 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1207 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1208 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001209
1210 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001211 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001212 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001213 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001214 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001215 break;
1216 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001217 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001218 break;
1219 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001220 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001221 break;
1222 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001223 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001224 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001225 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001226 break;
1227 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001228 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001229 break;
1230 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001231 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001232 break;
1233 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001234 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001235 break;
1236 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001237 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001238 break;
1239 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001240 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001241 break;
1242 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001243 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001244 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001245 break;
1246 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001247 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001248 break;
1249 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001250 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1251 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1252 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001253 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001254 } else {
1255 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001256 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001257 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001258 index_reg |= (sib >> 3) & 7;
1259 base_reg |= sib & 7;
1260 scale = sib >> 6;
1261
Avi Kivity9dac77f2011-06-01 15:34:25 +03001262 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001263 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001264 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001265 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001266 adjust_modrm_seg(ctxt, base_reg);
Nadav Amitab708092014-12-25 02:52:21 +02001267 /* Increment ESP on POP [ESP] */
1268 if ((ctxt->d & IncSP) &&
1269 base_reg == VCPU_REGS_RSP)
1270 modrm_ea += ctxt->op_bytes;
Avi Kivitya6e34072012-06-10 17:15:39 +03001271 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001272 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001273 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001274 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Nadav Amit5b38ab82014-11-02 11:54:41 +02001275 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity84411d82008-06-15 21:53:26 -07001276 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001277 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001278 } else {
1279 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001280 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001281 adjust_modrm_seg(ctxt, base_reg);
1282 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001283 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001284 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001285 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001286 break;
1287 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001288 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001289 break;
1290 }
1291 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001292 op->addr.mem.ea = modrm_ea;
Bandan Das41061cd2014-04-16 12:46:14 -04001293 if (ctxt->ad_bytes != 8)
1294 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
1295
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001296done:
1297 return rc;
1298}
1299
1300static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001301 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001302{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001303 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001304
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001305 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001306 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001307 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001308 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001309 break;
1310 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001311 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001312 break;
1313 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001314 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001315 break;
1316 }
1317done:
1318 return rc;
1319}
1320
Avi Kivity9dac77f2011-06-01 15:34:25 +03001321static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001322{
Sheng Yang7129eec2010-09-28 16:33:32 +08001323 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001324
Avi Kivity9dac77f2011-06-01 15:34:25 +03001325 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001326 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001327
Avi Kivity9dac77f2011-06-01 15:34:25 +03001328 if (ctxt->src.bytes == 2)
1329 sv = (s16)ctxt->src.val & (s16)mask;
1330 else if (ctxt->src.bytes == 4)
1331 sv = (s32)ctxt->src.val & (s32)mask;
Nadav Amit7dec5602014-06-15 16:12:57 +03001332 else
1333 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001334
Nadav Amit1c1c35a2014-11-19 17:43:09 +02001335 ctxt->dst.addr.mem.ea = address_mask(ctxt,
1336 ctxt->dst.addr.mem.ea + (sv >> 3));
Wei Yongjun35c843c2010-08-09 11:34:56 +08001337 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001338
1339 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001340 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001341}
1342
Gleb Natapov9de41572010-04-28 19:15:22 +03001343static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001344 unsigned long addr, void *dest, unsigned size)
1345{
1346 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001347 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001348
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001349 if (mc->pos < mc->end)
1350 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001351
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001352 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001353
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001354 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1355 &ctxt->exception);
1356 if (rc != X86EMUL_CONTINUE)
1357 return rc;
1358
1359 mc->end += size;
1360
1361read_cached:
1362 memcpy(dest, mc->data + mc->pos, size);
1363 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001364 return X86EMUL_CONTINUE;
1365}
1366
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001367static int segmented_read(struct x86_emulate_ctxt *ctxt,
1368 struct segmented_address addr,
1369 void *data,
1370 unsigned size)
1371{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001372 int rc;
1373 ulong linear;
1374
Avi Kivity83b87952011-04-03 11:31:19 +03001375 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001376 if (rc != X86EMUL_CONTINUE)
1377 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001378 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001379}
1380
1381static int segmented_write(struct x86_emulate_ctxt *ctxt,
1382 struct segmented_address addr,
1383 const void *data,
1384 unsigned size)
1385{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001386 int rc;
1387 ulong linear;
1388
Avi Kivity83b87952011-04-03 11:31:19 +03001389 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001390 if (rc != X86EMUL_CONTINUE)
1391 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001392 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1393 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001394}
1395
1396static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1397 struct segmented_address addr,
1398 const void *orig_data, const void *data,
1399 unsigned size)
1400{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001401 int rc;
1402 ulong linear;
1403
Avi Kivity83b87952011-04-03 11:31:19 +03001404 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001405 if (rc != X86EMUL_CONTINUE)
1406 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001407 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1408 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001409}
1410
Gleb Natapov7b262e92010-03-18 15:20:27 +02001411static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001412 unsigned int size, unsigned short port,
1413 void *dest)
1414{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001415 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001416
1417 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001418 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001419 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001420 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001421 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001422 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1423 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Mark Rustadb55a8142014-07-25 06:27:05 -07001424 n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001425 if (n == 0)
1426 n = 1;
1427 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001428 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001429 return 0;
1430 rc->end = n * size;
1431 }
1432
Nadav Amite6e39f02014-04-18 03:35:10 +03001433 if (ctxt->rep_prefix && (ctxt->d & String) &&
1434 !(ctxt->eflags & EFLG_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001435 ctxt->dst.data = rc->data + rc->pos;
1436 ctxt->dst.type = OP_MEM_STR;
1437 ctxt->dst.count = (rc->end - rc->pos) / size;
1438 rc->pos = rc->end;
1439 } else {
1440 memcpy(dest, rc->data + rc->pos, size);
1441 rc->pos += size;
1442 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001443 return 1;
1444}
1445
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001446static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1447 u16 index, struct desc_struct *desc)
1448{
1449 struct desc_ptr dt;
1450 ulong addr;
1451
1452 ctxt->ops->get_idt(ctxt, &dt);
1453
1454 if (dt.size < index * 8 + 7)
1455 return emulate_gp(ctxt, index << 3 | 0x2);
1456
1457 addr = dt.address + index * 8;
1458 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1459 &ctxt->exception);
1460}
1461
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001462static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001463 u16 selector, struct desc_ptr *dt)
1464{
Mathias Krause0225fb52012-08-30 01:30:16 +02001465 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001466 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001467
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001468 if (selector & 1 << 2) {
1469 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001470 u16 sel;
1471
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001472 memset (dt, 0, sizeof *dt);
Nadav Amit2eedcac2014-06-02 18:34:05 +03001473 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1474 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001475 return;
1476
1477 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001478 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001479 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001480 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001481}
1482
Nadav Amitedccda72014-12-25 02:52:23 +02001483static int get_descriptor_ptr(struct x86_emulate_ctxt *ctxt,
1484 u16 selector, ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001485{
1486 struct desc_ptr dt;
1487 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001488 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001489
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001490 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001491
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001492 if (dt.size < index * 8 + 7)
1493 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001494
1495 addr = dt.address + index * 8;
Nadav Amitedccda72014-12-25 02:52:23 +02001496
1497#ifdef CONFIG_X86_64
1498 if (addr >> 32 != 0) {
1499 u64 efer = 0;
1500
1501 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1502 if (!(efer & EFER_LMA))
1503 addr &= (u32)-1;
1504 }
1505#endif
1506
1507 *desc_addr_p = addr;
1508 return X86EMUL_CONTINUE;
1509}
1510
1511/* allowed just for 8 bytes segments */
1512static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1513 u16 selector, struct desc_struct *desc,
1514 ulong *desc_addr_p)
1515{
1516 int rc;
1517
1518 rc = get_descriptor_ptr(ctxt, selector, desc_addr_p);
1519 if (rc != X86EMUL_CONTINUE)
1520 return rc;
1521
1522 return ctxt->ops->read_std(ctxt, *desc_addr_p, desc, sizeof(*desc),
1523 &ctxt->exception);
1524}
1525
1526/* allowed just for 8 bytes segments */
1527static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1528 u16 selector, struct desc_struct *desc)
1529{
1530 int rc;
1531 ulong addr;
1532
1533 rc = get_descriptor_ptr(ctxt, selector, &addr);
1534 if (rc != X86EMUL_CONTINUE)
1535 return rc;
1536
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001537 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1538 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001539}
1540
Gleb Natapov5601d052011-03-07 14:55:06 +02001541/* Does not support long mode */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001542static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Nadav Amitd1442d82014-09-18 22:39:39 +03001543 u16 selector, int seg, u8 cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001544 enum x86_transfer_type transfer,
Nadav Amitd1442d82014-09-18 22:39:39 +03001545 struct desc_struct *desc)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001546{
Avi Kivity869be992012-06-13 16:30:53 +03001547 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001548 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001549 unsigned err_vec = GP_VECTOR;
1550 u32 err_code = 0;
1551 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001552 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001553 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001554 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001555 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001556
1557 memset(&seg_desc, 0, sizeof seg_desc);
1558
Kevin Wolff8da94e2013-04-11 14:06:03 +02001559 if (ctxt->mode == X86EMUL_MODE_REAL) {
1560 /* set real mode segment descriptor (keep limit etc. for
1561 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001562 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001563 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001564 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001565 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1566 /* VM86 needs a clean new segment descriptor */
1567 set_desc_base(&seg_desc, selector << 4);
1568 set_desc_limit(&seg_desc, 0xffff);
1569 seg_desc.type = 3;
1570 seg_desc.p = 1;
1571 seg_desc.s = 1;
1572 seg_desc.dpl = 3;
1573 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001574 }
1575
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001576 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001577
1578 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1579 if ((seg == VCPU_SREG_CS
1580 || (seg == VCPU_SREG_SS
1581 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1582 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001583 && null_selector)
1584 goto exception;
1585
1586 /* TR should be in GDT only */
1587 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1588 goto exception;
1589
1590 if (null_selector) /* for NULL selector skip all following checks */
1591 goto load;
1592
Avi Kivitye9194642012-06-13 16:29:39 +03001593 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001594 if (ret != X86EMUL_CONTINUE)
1595 return ret;
1596
1597 err_code = selector & 0xfffc;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001598 err_vec = (transfer == X86_TRANSFER_TASK_SWITCH) ? TS_VECTOR :
1599 GP_VECTOR;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001600
Guo Chaofc058682012-06-28 15:19:51 +08001601 /* can't load system descriptor into segment selector */
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001602 if (seg <= VCPU_SREG_GS && !seg_desc.s) {
1603 if (transfer == X86_TRANSFER_CALL_JMP)
1604 return X86EMUL_UNHANDLEABLE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001605 goto exception;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001606 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001607
1608 if (!seg_desc.p) {
1609 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1610 goto exception;
1611 }
1612
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001613 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001614
1615 switch (seg) {
1616 case VCPU_SREG_SS:
1617 /*
1618 * segment is not a writable data segment or segment
1619 * selector's RPL != CPL or segment selector's RPL != CPL
1620 */
1621 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1622 goto exception;
1623 break;
1624 case VCPU_SREG_CS:
1625 if (!(seg_desc.type & 8))
1626 goto exception;
1627
1628 if (seg_desc.type & 4) {
1629 /* conforming */
1630 if (dpl > cpl)
1631 goto exception;
1632 } else {
1633 /* nonconforming */
1634 if (rpl > cpl || dpl != cpl)
1635 goto exception;
1636 }
Nadav Amit040c8dc2014-09-18 22:39:43 +03001637 /* in long-mode d/b must be clear if l is set */
1638 if (seg_desc.d && seg_desc.l) {
1639 u64 efer = 0;
1640
1641 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1642 if (efer & EFER_LMA)
1643 goto exception;
1644 }
1645
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001646 /* CS(RPL) <- CPL */
1647 selector = (selector & 0xfffc) | cpl;
1648 break;
1649 case VCPU_SREG_TR:
1650 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1651 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001652 old_desc = seg_desc;
1653 seg_desc.type |= 2; /* busy */
1654 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1655 sizeof(seg_desc), &ctxt->exception);
1656 if (ret != X86EMUL_CONTINUE)
1657 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001658 break;
1659 case VCPU_SREG_LDTR:
1660 if (seg_desc.s || seg_desc.type != 2)
1661 goto exception;
1662 break;
1663 default: /* DS, ES, FS, or GS */
1664 /*
1665 * segment is not a data or readable code segment or
1666 * ((segment is a data or nonconforming code segment)
1667 * and (both RPL and CPL > DPL))
1668 */
1669 if ((seg_desc.type & 0xa) == 0x8 ||
1670 (((seg_desc.type & 0xc) != 0xc) &&
1671 (rpl > dpl && cpl > dpl)))
1672 goto exception;
1673 break;
1674 }
1675
1676 if (seg_desc.s) {
1677 /* mark segment as accessed */
Nadav Amite2cefa72014-12-25 02:52:22 +02001678 if (!(seg_desc.type & 1)) {
1679 seg_desc.type |= 1;
1680 ret = write_segment_descriptor(ctxt, selector,
1681 &seg_desc);
1682 if (ret != X86EMUL_CONTINUE)
1683 return ret;
1684 }
Nadav Amite37a75a2014-06-02 18:34:04 +03001685 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
1686 ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3,
1687 sizeof(base3), &ctxt->exception);
1688 if (ret != X86EMUL_CONTINUE)
1689 return ret;
Nadav Amit9a9abf62014-11-02 11:54:56 +02001690 if (is_noncanonical_address(get_desc_base(&seg_desc) |
1691 ((u64)base3 << 32)))
1692 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001693 }
1694load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001695 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Nadav Amitd1442d82014-09-18 22:39:39 +03001696 if (desc)
1697 *desc = seg_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001698 return X86EMUL_CONTINUE;
1699exception:
Paolo Bonzini592f0852014-08-20 10:05:08 +02001700 return emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001701}
1702
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001703static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1704 u16 selector, int seg)
1705{
1706 u8 cpl = ctxt->ops->cpl(ctxt);
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001707 return __load_segment_descriptor(ctxt, selector, seg, cpl,
1708 X86_TRANSFER_NONE, NULL);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001709}
1710
Wei Yongjun31be40b2010-08-17 09:17:30 +08001711static void write_register_operand(struct operand *op)
1712{
Nadav Amit6fd8e122015-03-30 15:39:20 +03001713 return assign_register(op->addr.reg, op->val, op->bytes);
Wei Yongjun31be40b2010-08-17 09:17:30 +08001714}
1715
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001716static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001717{
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001718 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001719 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001720 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001721 break;
1722 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001723 if (ctxt->lock_prefix)
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001724 return segmented_cmpxchg(ctxt,
1725 op->addr.mem,
1726 &op->orig_val,
1727 &op->val,
1728 op->bytes);
1729 else
1730 return segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001731 op->addr.mem,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001732 &op->val,
1733 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001734 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001735 case OP_MEM_STR:
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001736 return segmented_write(ctxt,
1737 op->addr.mem,
1738 op->data,
1739 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001740 break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001741 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001742 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001743 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001744 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001745 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001746 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001747 case OP_NONE:
1748 /* no writeback */
1749 break;
1750 default:
1751 break;
1752 }
1753 return X86EMUL_CONTINUE;
1754}
1755
Avi Kivity51ddff52012-06-12 20:19:40 +03001756static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001757{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001758 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001759
Avi Kivity5ad105e2012-08-19 14:34:31 +03001760 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001761 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001762 addr.seg = VCPU_SREG_SS;
1763
Avi Kivity51ddff52012-06-12 20:19:40 +03001764 return segmented_write(ctxt, addr, data, bytes);
1765}
1766
1767static int em_push(struct x86_emulate_ctxt *ctxt)
1768{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001769 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001770 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001771 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001772}
1773
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001774static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001775 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001776{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001777 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001778 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001779
Avi Kivitydd856ef2012-08-27 23:46:17 +03001780 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001781 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001782 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001783 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001784 return rc;
1785
Avi Kivity5ad105e2012-08-19 14:34:31 +03001786 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001787 return rc;
1788}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001789
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001790static int em_pop(struct x86_emulate_ctxt *ctxt)
1791{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001792 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001793}
1794
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001795static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001796 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001797{
1798 int rc;
1799 unsigned long val, change_mask;
1800 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001801 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001802
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001803 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001804 if (rc != X86EMUL_CONTINUE)
1805 return rc;
1806
1807 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
Nadav Amit163b1352014-07-21 14:37:28 +03001808 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_AC | EFLG_ID;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001809
1810 switch(ctxt->mode) {
1811 case X86EMUL_MODE_PROT64:
1812 case X86EMUL_MODE_PROT32:
1813 case X86EMUL_MODE_PROT16:
1814 if (cpl == 0)
1815 change_mask |= EFLG_IOPL;
1816 if (cpl <= iopl)
1817 change_mask |= EFLG_IF;
1818 break;
1819 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001820 if (iopl < 3)
1821 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001822 change_mask |= EFLG_IF;
1823 break;
1824 default: /* real mode */
1825 change_mask |= (EFLG_IOPL | EFLG_IF);
1826 break;
1827 }
1828
1829 *(unsigned long *)dest =
1830 (ctxt->eflags & ~change_mask) | (val & change_mask);
1831
1832 return rc;
1833}
1834
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001835static int em_popf(struct x86_emulate_ctxt *ctxt)
1836{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001837 ctxt->dst.type = OP_REG;
1838 ctxt->dst.addr.reg = &ctxt->eflags;
1839 ctxt->dst.bytes = ctxt->op_bytes;
1840 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001841}
1842
Avi Kivity612e89f2012-06-12 20:03:23 +03001843static int em_enter(struct x86_emulate_ctxt *ctxt)
1844{
1845 int rc;
1846 unsigned frame_size = ctxt->src.val;
1847 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001848 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001849
1850 if (nesting_level)
1851 return X86EMUL_UNHANDLEABLE;
1852
Avi Kivitydd856ef2012-08-27 23:46:17 +03001853 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1854 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001855 if (rc != X86EMUL_CONTINUE)
1856 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001857 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001858 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001859 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1860 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001861 stack_mask(ctxt));
1862 return X86EMUL_CONTINUE;
1863}
1864
Avi Kivityf47cfa32012-06-07 17:49:24 +03001865static int em_leave(struct x86_emulate_ctxt *ctxt)
1866{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001867 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001868 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001869 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001870}
1871
Avi Kivity1cd196e2011-09-13 10:45:51 +03001872static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001873{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001874 int seg = ctxt->src2.val;
1875
Avi Kivity9dac77f2011-06-01 15:34:25 +03001876 ctxt->src.val = get_segment_selector(ctxt, seg);
Nadav Amit0fcc2072014-11-02 11:54:51 +02001877 if (ctxt->op_bytes == 4) {
1878 rsp_increment(ctxt, -2);
1879 ctxt->op_bytes = 2;
1880 }
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001881
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001882 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001883}
1884
Avi Kivity1cd196e2011-09-13 10:45:51 +03001885static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001886{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001887 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001888 unsigned long selector;
1889 int rc;
1890
Nadav Amit3313bc42014-12-25 02:52:17 +02001891 rc = emulate_pop(ctxt, &selector, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001892 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001893 return rc;
1894
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001895 if (ctxt->modrm_reg == VCPU_SREG_SS)
1896 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Nadav Amit3313bc42014-12-25 02:52:17 +02001897 if (ctxt->op_bytes > 2)
1898 rsp_increment(ctxt, ctxt->op_bytes - 2);
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001899
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001900 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001901 return rc;
1902}
1903
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001904static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001905{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001906 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001907 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001908 int reg = VCPU_REGS_RAX;
1909
1910 while (reg <= VCPU_REGS_RDI) {
1911 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001912 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001913
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001914 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001915 if (rc != X86EMUL_CONTINUE)
1916 return rc;
1917
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001918 ++reg;
1919 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001920
Wei Yongjunc37eda12010-06-15 09:03:33 +08001921 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001922}
1923
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001924static int em_pushf(struct x86_emulate_ctxt *ctxt)
1925{
Nadav Amitbc397a62014-12-10 11:19:03 +02001926 ctxt->src.val = (unsigned long)ctxt->eflags & ~EFLG_VM;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001927 return em_push(ctxt);
1928}
1929
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001930static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001931{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001932 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001933 int reg = VCPU_REGS_RDI;
Nadav Amit6fd8e122015-03-30 15:39:20 +03001934 u32 val;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001935
1936 while (reg >= VCPU_REGS_RAX) {
1937 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001938 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001939 --reg;
1940 }
1941
Nadav Amit6fd8e122015-03-30 15:39:20 +03001942 rc = emulate_pop(ctxt, &val, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001943 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001944 break;
Nadav Amit6fd8e122015-03-30 15:39:20 +03001945 assign_register(reg_rmw(ctxt, reg), val, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001946 --reg;
1947 }
1948 return rc;
1949}
1950
Avi Kivitydd856ef2012-08-27 23:46:17 +03001951static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001952{
Mathias Krause0225fb52012-08-30 01:30:16 +02001953 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001954 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001955 struct desc_ptr dt;
1956 gva_t cs_addr;
1957 gva_t eip_addr;
1958 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001959
1960 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001961 ctxt->src.val = ctxt->eflags;
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
1966 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1967
Avi Kivity9dac77f2011-06-01 15:34:25 +03001968 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001969 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001970 if (rc != X86EMUL_CONTINUE)
1971 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001972
Avi Kivity9dac77f2011-06-01 15:34:25 +03001973 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001974 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001975 if (rc != X86EMUL_CONTINUE)
1976 return rc;
1977
Avi Kivity4bff1e862011-04-20 13:37:53 +03001978 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001979
1980 eip_addr = dt.address + (irq << 2);
1981 cs_addr = dt.address + (irq << 2) + 2;
1982
Avi Kivity0f65dd72011-04-20 13:37:53 +03001983 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001984 if (rc != X86EMUL_CONTINUE)
1985 return rc;
1986
Avi Kivity0f65dd72011-04-20 13:37:53 +03001987 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001988 if (rc != X86EMUL_CONTINUE)
1989 return rc;
1990
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001991 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001992 if (rc != X86EMUL_CONTINUE)
1993 return rc;
1994
Avi Kivity9dac77f2011-06-01 15:34:25 +03001995 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001996
1997 return rc;
1998}
1999
Avi Kivitydd856ef2012-08-27 23:46:17 +03002000int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
2001{
2002 int rc;
2003
2004 invalidate_registers(ctxt);
2005 rc = __emulate_int_real(ctxt, irq);
2006 if (rc == X86EMUL_CONTINUE)
2007 writeback_registers(ctxt);
2008 return rc;
2009}
2010
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002011static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002012{
2013 switch(ctxt->mode) {
2014 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03002015 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002016 case X86EMUL_MODE_VM86:
2017 case X86EMUL_MODE_PROT16:
2018 case X86EMUL_MODE_PROT32:
2019 case X86EMUL_MODE_PROT64:
2020 default:
2021 /* Protected mode interrupts unimplemented yet */
2022 return X86EMUL_UNHANDLEABLE;
2023 }
2024}
2025
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002026static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002027{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002028 int rc = X86EMUL_CONTINUE;
2029 unsigned long temp_eip = 0;
2030 unsigned long temp_eflags = 0;
2031 unsigned long cs = 0;
2032 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
2033 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
2034 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
2035 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
2036
2037 /* TODO: Add stack limit check */
2038
Avi Kivity9dac77f2011-06-01 15:34:25 +03002039 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002040
2041 if (rc != X86EMUL_CONTINUE)
2042 return rc;
2043
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002044 if (temp_eip & ~0xffff)
2045 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002046
Avi Kivity9dac77f2011-06-01 15:34:25 +03002047 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002048
2049 if (rc != X86EMUL_CONTINUE)
2050 return rc;
2051
Avi Kivity9dac77f2011-06-01 15:34:25 +03002052 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002053
2054 if (rc != X86EMUL_CONTINUE)
2055 return rc;
2056
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002057 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002058
2059 if (rc != X86EMUL_CONTINUE)
2060 return rc;
2061
Avi Kivity9dac77f2011-06-01 15:34:25 +03002062 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002063
2064
Avi Kivity9dac77f2011-06-01 15:34:25 +03002065 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002066 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03002067 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002068 ctxt->eflags &= ~0xffff;
2069 ctxt->eflags |= temp_eflags;
2070 }
2071
2072 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
2073 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
Nadav Amit801806d2015-01-26 09:32:23 +02002074 ctxt->ops->set_nmi_mask(ctxt, false);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002075
2076 return rc;
2077}
2078
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002079static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002080{
2081 switch(ctxt->mode) {
2082 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002083 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002084 case X86EMUL_MODE_VM86:
2085 case X86EMUL_MODE_PROT16:
2086 case X86EMUL_MODE_PROT32:
2087 case X86EMUL_MODE_PROT64:
2088 default:
2089 /* iret from protected mode unimplemented yet */
2090 return X86EMUL_UNHANDLEABLE;
2091 }
2092}
2093
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002094static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2095{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002096 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002097 unsigned short sel, old_sel;
2098 struct desc_struct old_desc, new_desc;
2099 const struct x86_emulate_ops *ops = ctxt->ops;
2100 u8 cpl = ctxt->ops->cpl(ctxt);
2101
2102 /* Assignment of RIP may only fail in 64-bit mode */
2103 if (ctxt->mode == X86EMUL_MODE_PROT64)
2104 ops->get_segment(ctxt, &old_sel, &old_desc, NULL,
2105 VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002106
Avi Kivity9dac77f2011-06-01 15:34:25 +03002107 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002108
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002109 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
2110 X86_TRANSFER_CALL_JMP,
Nadav Amitd1442d82014-09-18 22:39:39 +03002111 &new_desc);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002112 if (rc != X86EMUL_CONTINUE)
2113 return rc;
2114
Nadav Amitd50eaa12014-11-19 17:43:11 +02002115 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03002116 if (rc != X86EMUL_CONTINUE) {
Nadav Amit7e46ddd2014-10-28 00:03:43 +02002117 WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64);
Nadav Amitd1442d82014-09-18 22:39:39 +03002118 /* assigning eip failed; restore the old cs */
2119 ops->set_segment(ctxt, old_sel, &old_desc, 0, VCPU_SREG_CS);
2120 return rc;
2121 }
2122 return rc;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002123}
2124
Nadav Amitf7784042014-09-18 22:39:41 +03002125static int em_jmp_abs(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002126{
Nadav Amitf7784042014-09-18 22:39:41 +03002127 return assign_eip_near(ctxt, ctxt->src.val);
2128}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002129
Nadav Amitf7784042014-09-18 22:39:41 +03002130static int em_call_near_abs(struct x86_emulate_ctxt *ctxt)
2131{
2132 int rc;
2133 long int old_eip;
2134
2135 old_eip = ctxt->_eip;
2136 rc = assign_eip_near(ctxt, ctxt->src.val);
2137 if (rc != X86EMUL_CONTINUE)
2138 return rc;
2139 ctxt->src.val = old_eip;
2140 rc = em_push(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002141 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002142}
2143
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002144static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002145{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002146 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002147
Nadav Amitaaa05f22014-06-02 18:34:10 +03002148 if (ctxt->dst.bytes == 16)
2149 return X86EMUL_UNHANDLEABLE;
2150
Avi Kivitydd856ef2012-08-27 23:46:17 +03002151 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2152 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2153 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2154 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002155 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002156 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002157 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2158 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002159
Laurent Vivier05f086f2007-09-24 11:10:55 +02002160 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002161 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002162 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002163}
2164
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002165static int em_ret(struct x86_emulate_ctxt *ctxt)
2166{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002167 int rc;
2168 unsigned long eip;
2169
2170 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
2171 if (rc != X86EMUL_CONTINUE)
2172 return rc;
2173
2174 return assign_eip_near(ctxt, eip);
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002175}
2176
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002177static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002178{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002179 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002180 unsigned long eip, cs;
2181 u16 old_cs;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002182 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amitd1442d82014-09-18 22:39:39 +03002183 struct desc_struct old_desc, new_desc;
2184 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002185
Nadav Amitd1442d82014-09-18 22:39:39 +03002186 if (ctxt->mode == X86EMUL_MODE_PROT64)
2187 ops->get_segment(ctxt, &old_cs, &old_desc, NULL,
2188 VCPU_SREG_CS);
2189
2190 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002191 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002192 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002193 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002194 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002195 return rc;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002196 /* Outer-privilege level return is not implemented */
2197 if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2198 return X86EMUL_UNHANDLEABLE;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002199 rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, cpl,
2200 X86_TRANSFER_RET,
Nadav Amitd1442d82014-09-18 22:39:39 +03002201 &new_desc);
2202 if (rc != X86EMUL_CONTINUE)
2203 return rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +02002204 rc = assign_eip_far(ctxt, eip, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03002205 if (rc != X86EMUL_CONTINUE) {
Nadav Amit7e46ddd2014-10-28 00:03:43 +02002206 WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64);
Nadav Amitd1442d82014-09-18 22:39:39 +03002207 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
2208 }
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002209 return rc;
2210}
2211
Bruce Rogers32611072013-09-09 09:40:20 -06002212static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2213{
2214 int rc;
2215
2216 rc = em_ret_far(ctxt);
2217 if (rc != X86EMUL_CONTINUE)
2218 return rc;
2219 rsp_increment(ctxt, ctxt->src.val);
2220 return X86EMUL_CONTINUE;
2221}
2222
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002223static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2224{
2225 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002226 ctxt->dst.orig_val = ctxt->dst.val;
2227 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002228 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002229 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002230 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002231
2232 if (ctxt->eflags & EFLG_ZF) {
Nadav Amit2fcf5c82015-01-26 09:32:21 +02002233 /* Success: write back to memory; no update of EAX */
2234 ctxt->src.type = OP_NONE;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002235 ctxt->dst.val = ctxt->src.orig_val;
2236 } else {
2237 /* Failure: write the value we saw to EAX. */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02002238 ctxt->src.type = OP_REG;
2239 ctxt->src.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
2240 ctxt->src.val = ctxt->dst.orig_val;
2241 /* Create write-cycle to dest by writing the same value */
Nadav Amit37c564f2014-06-02 18:34:07 +03002242 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002243 }
2244 return X86EMUL_CONTINUE;
2245}
2246
Avi Kivityd4b43252011-09-13 10:45:50 +03002247static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002248{
Avi Kivityd4b43252011-09-13 10:45:50 +03002249 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002250 unsigned short sel;
2251 int rc;
2252
Avi Kivity9dac77f2011-06-01 15:34:25 +03002253 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002254
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002255 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002256 if (rc != X86EMUL_CONTINUE)
2257 return rc;
2258
Avi Kivity9dac77f2011-06-01 15:34:25 +03002259 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002260 return rc;
2261}
2262
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002263static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002264setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002265 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002266{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002267 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002268 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002269 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002270 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002271 cs->type = 0x0b; /* Read, Execute, Accessed */
2272 cs->s = 1;
2273 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002274 cs->p = 1;
2275 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002276 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002277
Gleb Natapov79168fd2010-04-28 19:15:30 +03002278 set_desc_base(ss, 0); /* flat segment */
2279 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002280 ss->g = 1; /* 4kb granularity */
2281 ss->s = 1;
2282 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002283 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002284 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002285 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002286 ss->l = 0;
2287 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002288}
2289
Avi Kivity1a18a692012-02-01 12:23:21 +02002290static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2291{
2292 u32 eax, ebx, ecx, edx;
2293
2294 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002295 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2296 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002297 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2298 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2299}
2300
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002301static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2302{
Mathias Krause0225fb52012-08-30 01:30:16 +02002303 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002304 u32 eax, ebx, ecx, edx;
2305
2306 /*
2307 * syscall should always be enabled in longmode - so only become
2308 * vendor specific (cpuid) if other modes are active...
2309 */
2310 if (ctxt->mode == X86EMUL_MODE_PROT64)
2311 return true;
2312
2313 eax = 0x00000000;
2314 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002315 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2316 /*
2317 * Intel ("GenuineIntel")
2318 * remark: Intel CPUs only support "syscall" in 64bit
2319 * longmode. Also an 64bit guest with a
2320 * 32bit compat-app running will #UD !! While this
2321 * behaviour can be fixed (by emulating) into AMD
2322 * response - CPUs of AMD can't behave like Intel.
2323 */
2324 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2325 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2326 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2327 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002328
Avi Kivity0017f932012-06-07 14:10:16 +03002329 /* AMD ("AuthenticAMD") */
2330 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2331 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2332 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2333 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002334
Avi Kivity0017f932012-06-07 14:10:16 +03002335 /* AMD ("AMDisbetter!") */
2336 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2337 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2338 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2339 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002340
2341 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2342 return false;
2343}
2344
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002345static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002346{
Mathias Krause0225fb52012-08-30 01:30:16 +02002347 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002348 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002349 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002350 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002351 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002352
2353 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002354 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002355 ctxt->mode == X86EMUL_MODE_VM86)
2356 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002357
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002358 if (!(em_syscall_is_enabled(ctxt)))
2359 return emulate_ud(ctxt);
2360
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002361 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002362 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002363
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002364 if (!(efer & EFER_SCE))
2365 return emulate_ud(ctxt);
2366
Avi Kivity717746e2011-04-20 13:37:53 +03002367 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002368 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002369 cs_sel = (u16)(msr_data & 0xfffc);
2370 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002371
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002372 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002373 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002374 cs.l = 1;
2375 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002376 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2377 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002378
Avi Kivitydd856ef2012-08-27 23:46:17 +03002379 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002380 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002381#ifdef CONFIG_X86_64
Nadav Amit6c6cb692014-07-21 14:37:30 +03002382 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002383
Avi Kivity717746e2011-04-20 13:37:53 +03002384 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002385 ctxt->mode == X86EMUL_MODE_PROT64 ?
2386 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002387 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002388
Avi Kivity717746e2011-04-20 13:37:53 +03002389 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Nadav Amit6c6cb692014-07-21 14:37:30 +03002390 ctxt->eflags &= ~msr_data;
Nadav Amit807c1422014-11-02 11:54:49 +02002391 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002392#endif
2393 } else {
2394 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002395 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002396 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002397
Nadav Amit6c6cb692014-07-21 14:37:30 +03002398 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002399 }
2400
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002401 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002402}
2403
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002404static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002405{
Mathias Krause0225fb52012-08-30 01:30:16 +02002406 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002407 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002408 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002409 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002410 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002411
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002412 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002413 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002414 if (ctxt->mode == X86EMUL_MODE_REAL)
2415 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002416
Avi Kivity1a18a692012-02-01 12:23:21 +02002417 /*
2418 * Not recognized on AMD in compat mode (but is recognized in legacy
2419 * mode).
2420 */
Nadav Amitf3747372015-01-01 23:11:11 +02002421 if ((ctxt->mode != X86EMUL_MODE_PROT64) && (efer & EFER_LMA)
Avi Kivity1a18a692012-02-01 12:23:21 +02002422 && !vendor_intel(ctxt))
2423 return emulate_ud(ctxt);
2424
Nadav Amitb2c9d432014-11-02 11:55:01 +02002425 /* sysenter/sysexit have not been tested in 64bit mode. */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002426 if (ctxt->mode == X86EMUL_MODE_PROT64)
Nadav Amitb2c9d432014-11-02 11:55:01 +02002427 return X86EMUL_UNHANDLEABLE;
Andre Przywara8c604352009-06-18 12:56:01 +02002428
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002429 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002430
Avi Kivity717746e2011-04-20 13:37:53 +03002431 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002432 if ((msr_data & 0xfffc) == 0x0)
2433 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002434
Nadav Amit6c6cb692014-07-21 14:37:30 +03002435 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Nadav Amitf3747372015-01-01 23:11:11 +02002436 cs_sel = (u16)msr_data & ~SELECTOR_RPL_MASK;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002437 ss_sel = cs_sel + 8;
Nadav Amitf3747372015-01-01 23:11:11 +02002438 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002439 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002440 cs.l = 1;
2441 }
2442
Avi Kivity1aa36612011-04-27 13:20:30 +03002443 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2444 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002445
Avi Kivity717746e2011-04-20 13:37:53 +03002446 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002447 ctxt->_eip = (efer & EFER_LMA) ? msr_data : (u32)msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002448
Avi Kivity717746e2011-04-20 13:37:53 +03002449 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002450 *reg_write(ctxt, VCPU_REGS_RSP) = (efer & EFER_LMA) ? msr_data :
2451 (u32)msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002452
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002453 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002454}
2455
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002456static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002457{
Mathias Krause0225fb52012-08-30 01:30:16 +02002458 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002459 struct desc_struct cs, ss;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002460 u64 msr_data, rcx, rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002461 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002462 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002463
Gleb Natapova0044752010-02-10 14:21:31 +02002464 /* inject #GP if in real mode or Virtual 8086 mode */
2465 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002466 ctxt->mode == X86EMUL_MODE_VM86)
2467 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002468
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002469 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002470
Avi Kivity9dac77f2011-06-01 15:34:25 +03002471 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002472 usermode = X86EMUL_MODE_PROT64;
2473 else
2474 usermode = X86EMUL_MODE_PROT32;
2475
Nadav Amit234f3ce2014-09-18 22:39:38 +03002476 rcx = reg_read(ctxt, VCPU_REGS_RCX);
2477 rdx = reg_read(ctxt, VCPU_REGS_RDX);
2478
Andre Przywara4668f052009-06-18 12:56:02 +02002479 cs.dpl = 3;
2480 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002481 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002482 switch (usermode) {
2483 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002484 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002485 if ((msr_data & 0xfffc) == 0x0)
2486 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002487 ss_sel = (u16)(msr_data + 24);
Nadav Amitbf0b6822014-09-18 22:39:45 +03002488 rcx = (u32)rcx;
2489 rdx = (u32)rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002490 break;
2491 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002492 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002493 if (msr_data == 0x0)
2494 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002495 ss_sel = cs_sel + 8;
2496 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002497 cs.l = 1;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002498 if (is_noncanonical_address(rcx) ||
2499 is_noncanonical_address(rdx))
2500 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002501 break;
2502 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002503 cs_sel |= SELECTOR_RPL_MASK;
2504 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002505
Avi Kivity1aa36612011-04-27 13:20:30 +03002506 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2507 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002508
Nadav Amit234f3ce2014-09-18 22:39:38 +03002509 ctxt->_eip = rdx;
2510 *reg_write(ctxt, VCPU_REGS_RSP) = rcx;
Andre Przywara4668f052009-06-18 12:56:02 +02002511
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002512 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002513}
2514
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002515static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002516{
2517 int iopl;
2518 if (ctxt->mode == X86EMUL_MODE_REAL)
2519 return false;
2520 if (ctxt->mode == X86EMUL_MODE_VM86)
2521 return true;
2522 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002523 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002524}
2525
2526static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002527 u16 port, u16 len)
2528{
Mathias Krause0225fb52012-08-30 01:30:16 +02002529 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002530 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002531 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002532 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002533 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002534 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002535 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002536
Avi Kivity1aa36612011-04-27 13:20:30 +03002537 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002538 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002539 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002540 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002541 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002542 base = get_desc_base(&tr_seg);
2543#ifdef CONFIG_X86_64
2544 base |= ((u64)base3) << 32;
2545#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002546 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002547 if (r != X86EMUL_CONTINUE)
2548 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002549 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002550 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002551 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002552 if (r != X86EMUL_CONTINUE)
2553 return false;
2554 if ((perm >> bit_idx) & mask)
2555 return false;
2556 return true;
2557}
2558
2559static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002560 u16 port, u16 len)
2561{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002562 if (ctxt->perm_ok)
2563 return true;
2564
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002565 if (emulator_bad_iopl(ctxt))
2566 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002567 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002568
2569 ctxt->perm_ok = true;
2570
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002571 return true;
2572}
2573
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002574static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002575 struct tss_segment_16 *tss)
2576{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002577 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002578 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002579 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2580 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2581 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2582 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2583 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2584 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2585 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2586 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002587
Avi Kivity1aa36612011-04-27 13:20:30 +03002588 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2589 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2590 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2591 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2592 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002593}
2594
2595static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002596 struct tss_segment_16 *tss)
2597{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002598 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002599 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002600
Avi Kivity9dac77f2011-06-01 15:34:25 +03002601 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002602 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002603 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2604 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2605 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2606 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2607 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2608 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2609 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2610 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002611
2612 /*
2613 * SDM says that segment selectors are loaded before segment
2614 * descriptors
2615 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002616 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2617 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2618 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2619 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2620 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002621
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002622 cpl = tss->cs & 3;
2623
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002624 /*
Guo Chaofc058682012-06-28 15:19:51 +08002625 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002626 * it is handled in a context of new task
2627 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002628 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002629 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002630 if (ret != X86EMUL_CONTINUE)
2631 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002632 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, 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->cs, VCPU_SREG_CS, 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->ss, VCPU_SREG_SS, 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->ds, VCPU_SREG_DS, 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;
2648
2649 return X86EMUL_CONTINUE;
2650}
2651
2652static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002653 u16 tss_selector, u16 old_tss_sel,
2654 ulong old_tss_base, struct desc_struct *new_desc)
2655{
Mathias Krause0225fb52012-08-30 01:30:16 +02002656 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002657 struct tss_segment_16 tss_seg;
2658 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002659 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002660
Avi Kivity0f65dd72011-04-20 13:37:53 +03002661 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002662 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002663 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002664 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002665
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002666 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002667
Avi Kivity0f65dd72011-04-20 13:37:53 +03002668 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002669 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002670 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002671 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002672
Avi Kivity0f65dd72011-04-20 13:37:53 +03002673 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002674 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002675 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002676 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002677
2678 if (old_tss_sel != 0xffff) {
2679 tss_seg.prev_task_link = old_tss_sel;
2680
Avi Kivity0f65dd72011-04-20 13:37:53 +03002681 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002682 &tss_seg.prev_task_link,
2683 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002684 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002685 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002686 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002687 }
2688
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002689 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002690}
2691
2692static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002693 struct tss_segment_32 *tss)
2694{
Nadav Amit5c7411e2014-04-07 18:37:47 +03002695 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002696 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002697 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002698 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2699 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2700 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2701 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2702 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2703 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2704 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2705 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002706
Avi Kivity1aa36612011-04-27 13:20:30 +03002707 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2708 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2709 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2710 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2711 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2712 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002713}
2714
2715static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002716 struct tss_segment_32 *tss)
2717{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002718 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002719 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002720
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002721 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002722 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002723 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002724 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002725
2726 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002727 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2728 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2729 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2730 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2731 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2732 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2733 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2734 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002735
2736 /*
2737 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002738 * descriptors. This is important because CPL checks will
2739 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002740 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002741 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2742 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2743 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2744 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2745 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2746 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2747 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002748
2749 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002750 * If we're switching between Protected Mode and VM86, we need to make
2751 * sure to update the mode before loading the segment descriptors so
2752 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01002753 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002754 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002755 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002756 cpl = 3;
2757 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002758 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002759 cpl = tss->cs & 3;
2760 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01002761
2762 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002763 * Now load segment descriptors. If fault happenes at this stage
2764 * it is handled in a context of new task
2765 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002766 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002767 cpl, X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002768 if (ret != X86EMUL_CONTINUE)
2769 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002770 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002771 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->cs, VCPU_SREG_CS, 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->ss, VCPU_SREG_SS, 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->ds, VCPU_SREG_DS, 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->fs, VCPU_SREG_FS, 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->gs, VCPU_SREG_GS, 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;
2794
2795 return X86EMUL_CONTINUE;
2796}
2797
2798static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002799 u16 tss_selector, u16 old_tss_sel,
2800 ulong old_tss_base, struct desc_struct *new_desc)
2801{
Mathias Krause0225fb52012-08-30 01:30:16 +02002802 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002803 struct tss_segment_32 tss_seg;
2804 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002805 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03002806 u32 eip_offset = offsetof(struct tss_segment_32, eip);
2807 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002808
Avi Kivity0f65dd72011-04-20 13:37:53 +03002809 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002810 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002811 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002812 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002813
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002814 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002815
Nadav Amit5c7411e2014-04-07 18:37:47 +03002816 /* Only GP registers and segment selectors are saved */
2817 ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
2818 ldt_sel_offset - eip_offset, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002819 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002820 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002821
Avi Kivity0f65dd72011-04-20 13:37:53 +03002822 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002823 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002824 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002825 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002826
2827 if (old_tss_sel != 0xffff) {
2828 tss_seg.prev_task_link = old_tss_sel;
2829
Avi Kivity0f65dd72011-04-20 13:37:53 +03002830 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002831 &tss_seg.prev_task_link,
2832 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002833 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002834 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002835 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002836 }
2837
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002838 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002839}
2840
2841static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002842 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002843 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002844{
Mathias Krause0225fb52012-08-30 01:30:16 +02002845 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002846 struct desc_struct curr_tss_desc, next_tss_desc;
2847 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002848 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002849 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002850 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002851 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002852 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002853
2854 /* FIXME: old_tss_base == ~0 ? */
2855
Avi Kivitye9194642012-06-13 16:29:39 +03002856 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002857 if (ret != X86EMUL_CONTINUE)
2858 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002859 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002860 if (ret != X86EMUL_CONTINUE)
2861 return ret;
2862
2863 /* FIXME: check that next_tss_desc is tss */
2864
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002865 /*
2866 * Check privileges. The three cases are task switch caused by...
2867 *
2868 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2869 * 2. Exception/IRQ/iret: No check is performed
Nadav Amit2c2ca2d2014-11-02 11:54:57 +02002870 * 3. jmp/call to TSS/task-gate: No check is performed since the
2871 * hardware checks it before exiting.
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002872 */
2873 if (reason == TASK_SWITCH_GATE) {
2874 if (idt_index != -1) {
2875 /* Software interrupts */
2876 struct desc_struct task_gate_desc;
2877 int dpl;
2878
2879 ret = read_interrupt_descriptor(ctxt, idt_index,
2880 &task_gate_desc);
2881 if (ret != X86EMUL_CONTINUE)
2882 return ret;
2883
2884 dpl = task_gate_desc.dpl;
2885 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2886 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2887 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002888 }
2889
Gleb Natapovceffb452010-03-18 15:20:19 +02002890 desc_limit = desc_limit_scaled(&next_tss_desc);
2891 if (!next_tss_desc.p ||
2892 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2893 desc_limit < 0x2b)) {
Paolo Bonzini592f0852014-08-20 10:05:08 +02002894 return emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002895 }
2896
2897 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2898 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002899 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002900 }
2901
2902 if (reason == TASK_SWITCH_IRET)
2903 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2904
2905 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002906 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002907 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2908 old_tss_sel = 0xffff;
2909
2910 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002911 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002912 old_tss_base, &next_tss_desc);
2913 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002914 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002915 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002916 if (ret != X86EMUL_CONTINUE)
2917 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002918
2919 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2920 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2921
2922 if (reason != TASK_SWITCH_IRET) {
2923 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002924 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002925 }
2926
Avi Kivity717746e2011-04-20 13:37:53 +03002927 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002928 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002929
Jan Kiszkae269fb22010-04-14 15:51:09 +02002930 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002931 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2932 ctxt->lock_prefix = 0;
2933 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002934 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002935 }
2936
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002937 return ret;
2938}
2939
2940int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002941 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002942 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002943{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002944 int rc;
2945
Avi Kivitydd856ef2012-08-27 23:46:17 +03002946 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002947 ctxt->_eip = ctxt->eip;
2948 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002949
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002950 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002951 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002952
Avi Kivitydd856ef2012-08-27 23:46:17 +03002953 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002954 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002955 writeback_registers(ctxt);
2956 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002957
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002958 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002959}
2960
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002961static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2962 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002963{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002964 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002965
Paolo Bonzini01485a22014-11-19 18:25:08 +01002966 register_address_increment(ctxt, reg, df * op->bytes);
2967 op->addr.mem.ea = register_address(ctxt, reg);
Gleb Natapova682e352010-03-18 15:20:21 +02002968}
2969
Avi Kivity7af04fc2010-08-18 14:16:35 +03002970static int em_das(struct x86_emulate_ctxt *ctxt)
2971{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002972 u8 al, old_al;
2973 bool af, cf, old_cf;
2974
2975 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002976 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002977
2978 old_al = al;
2979 old_cf = cf;
2980 cf = false;
2981 af = ctxt->eflags & X86_EFLAGS_AF;
2982 if ((al & 0x0f) > 9 || af) {
2983 al -= 6;
2984 cf = old_cf | (al >= 250);
2985 af = true;
2986 } else {
2987 af = false;
2988 }
2989 if (old_al > 0x99 || old_cf) {
2990 al -= 0x60;
2991 cf = true;
2992 }
2993
Avi Kivity9dac77f2011-06-01 15:34:25 +03002994 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002995 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002996 ctxt->src.type = OP_IMM;
2997 ctxt->src.val = 0;
2998 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02002999 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03003000 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
3001 if (cf)
3002 ctxt->eflags |= X86_EFLAGS_CF;
3003 if (af)
3004 ctxt->eflags |= X86_EFLAGS_AF;
3005 return X86EMUL_CONTINUE;
3006}
3007
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003008static int em_aam(struct x86_emulate_ctxt *ctxt)
3009{
3010 u8 al, ah;
3011
3012 if (ctxt->src.val == 0)
3013 return emulate_de(ctxt);
3014
3015 al = ctxt->dst.val & 0xff;
3016 ah = al / ctxt->src.val;
3017 al %= ctxt->src.val;
3018
3019 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
3020
3021 /* Set PF, ZF, SF */
3022 ctxt->src.type = OP_IMM;
3023 ctxt->src.val = 0;
3024 ctxt->src.bytes = 1;
3025 fastop(ctxt, em_or);
3026
3027 return X86EMUL_CONTINUE;
3028}
3029
Gleb Natapov7f662272012-12-10 11:42:30 +02003030static int em_aad(struct x86_emulate_ctxt *ctxt)
3031{
3032 u8 al = ctxt->dst.val & 0xff;
3033 u8 ah = (ctxt->dst.val >> 8) & 0xff;
3034
3035 al = (al + (ah * ctxt->src.val)) & 0xff;
3036
3037 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
3038
Gleb Natapovf583c292013-02-13 17:50:39 +02003039 /* Set PF, ZF, SF */
3040 ctxt->src.type = OP_IMM;
3041 ctxt->src.val = 0;
3042 ctxt->src.bytes = 1;
3043 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02003044
3045 return X86EMUL_CONTINUE;
3046}
3047
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003048static int em_call(struct x86_emulate_ctxt *ctxt)
3049{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003050 int rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003051 long rel = ctxt->src.val;
3052
3053 ctxt->src.val = (unsigned long)ctxt->_eip;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003054 rc = jmp_rel(ctxt, rel);
3055 if (rc != X86EMUL_CONTINUE)
3056 return rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003057 return em_push(ctxt);
3058}
3059
Avi Kivity0ef753b2010-08-18 14:51:45 +03003060static int em_call_far(struct x86_emulate_ctxt *ctxt)
3061{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003062 u16 sel, old_cs;
3063 ulong old_eip;
3064 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03003065 struct desc_struct old_desc, new_desc;
3066 const struct x86_emulate_ops *ops = ctxt->ops;
3067 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amit82268082015-01-26 09:32:27 +02003068 enum x86emul_mode prev_mode = ctxt->mode;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003069
Avi Kivity9dac77f2011-06-01 15:34:25 +03003070 old_eip = ctxt->_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003071 ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003072
Avi Kivity9dac77f2011-06-01 15:34:25 +03003073 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003074 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
3075 X86_TRANSFER_CALL_JMP, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003076 if (rc != X86EMUL_CONTINUE)
Nadav Amit80976db2014-12-25 02:52:20 +02003077 return rc;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003078
Nadav Amitd50eaa12014-11-19 17:43:11 +02003079 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003080 if (rc != X86EMUL_CONTINUE)
3081 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003082
Avi Kivity9dac77f2011-06-01 15:34:25 +03003083 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003084 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003085 if (rc != X86EMUL_CONTINUE)
Nadav Amitd1442d82014-09-18 22:39:39 +03003086 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003087
Avi Kivity9dac77f2011-06-01 15:34:25 +03003088 ctxt->src.val = old_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003089 rc = em_push(ctxt);
3090 /* If we failed, we tainted the memory, but the very least we should
3091 restore cs */
Nadav Amit82268082015-01-26 09:32:27 +02003092 if (rc != X86EMUL_CONTINUE) {
3093 pr_warn_once("faulting far call emulation tainted memory\n");
Nadav Amitd1442d82014-09-18 22:39:39 +03003094 goto fail;
Nadav Amit82268082015-01-26 09:32:27 +02003095 }
Nadav Amitd1442d82014-09-18 22:39:39 +03003096 return rc;
3097fail:
3098 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
Nadav Amit82268082015-01-26 09:32:27 +02003099 ctxt->mode = prev_mode;
Nadav Amitd1442d82014-09-18 22:39:39 +03003100 return rc;
3101
Avi Kivity0ef753b2010-08-18 14:51:45 +03003102}
3103
Avi Kivity40ece7c2010-08-18 15:12:09 +03003104static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3105{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003106 int rc;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003107 unsigned long eip;
Avi Kivity40ece7c2010-08-18 15:12:09 +03003108
Nadav Amit234f3ce2014-09-18 22:39:38 +03003109 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
3110 if (rc != X86EMUL_CONTINUE)
3111 return rc;
3112 rc = assign_eip_near(ctxt, eip);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003113 if (rc != X86EMUL_CONTINUE)
3114 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003115 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003116 return X86EMUL_CONTINUE;
3117}
3118
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003119static int em_xchg(struct x86_emulate_ctxt *ctxt)
3120{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003121 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003122 ctxt->src.val = ctxt->dst.val;
3123 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003124
3125 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003126 ctxt->dst.val = ctxt->src.orig_val;
3127 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003128 return X86EMUL_CONTINUE;
3129}
3130
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003131static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3132{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003133 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003134 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003135}
3136
Avi Kivity61429142010-08-19 15:13:00 +03003137static int em_cwd(struct x86_emulate_ctxt *ctxt)
3138{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003139 ctxt->dst.type = OP_REG;
3140 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003141 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003142 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003143
3144 return X86EMUL_CONTINUE;
3145}
3146
Avi Kivity48bb5d32010-08-18 18:54:34 +03003147static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3148{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003149 u64 tsc = 0;
3150
Avi Kivity717746e2011-04-20 13:37:53 +03003151 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003152 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3153 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003154 return X86EMUL_CONTINUE;
3155}
3156
Avi Kivity222d21a2011-11-10 14:57:30 +02003157static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3158{
3159 u64 pmc;
3160
Avi Kivitydd856ef2012-08-27 23:46:17 +03003161 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003162 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003163 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3164 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003165 return X86EMUL_CONTINUE;
3166}
3167
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003168static int em_mov(struct x86_emulate_ctxt *ctxt)
3169{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01003170 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003171 return X86EMUL_CONTINUE;
3172}
3173
Borislav Petkov84cffe42013-10-29 12:54:56 +01003174#define FFL(x) bit(X86_FEATURE_##x)
3175
3176static int em_movbe(struct x86_emulate_ctxt *ctxt)
3177{
3178 u32 ebx, ecx, edx, eax = 1;
3179 u16 tmp;
3180
3181 /*
3182 * Check MOVBE is set in the guest-visible CPUID leaf.
3183 */
3184 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
3185 if (!(ecx & FFL(MOVBE)))
3186 return emulate_ud(ctxt);
3187
3188 switch (ctxt->op_bytes) {
3189 case 2:
3190 /*
3191 * From MOVBE definition: "...When the operand size is 16 bits,
3192 * the upper word of the destination register remains unchanged
3193 * ..."
3194 *
3195 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3196 * rules so we have to do the operation almost per hand.
3197 */
3198 tmp = (u16)ctxt->src.val;
3199 ctxt->dst.val &= ~0xffffUL;
3200 ctxt->dst.val |= (unsigned long)swab16(tmp);
3201 break;
3202 case 4:
3203 ctxt->dst.val = swab32((u32)ctxt->src.val);
3204 break;
3205 case 8:
3206 ctxt->dst.val = swab64(ctxt->src.val);
3207 break;
3208 default:
Paolo Bonzini592f0852014-08-20 10:05:08 +02003209 BUG();
Borislav Petkov84cffe42013-10-29 12:54:56 +01003210 }
3211 return X86EMUL_CONTINUE;
3212}
3213
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003214static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3215{
3216 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3217 return emulate_gp(ctxt, 0);
3218
3219 /* Disable writeback. */
3220 ctxt->dst.type = OP_NONE;
3221 return X86EMUL_CONTINUE;
3222}
3223
3224static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3225{
3226 unsigned long val;
3227
3228 if (ctxt->mode == X86EMUL_MODE_PROT64)
3229 val = ctxt->src.val & ~0ULL;
3230 else
3231 val = ctxt->src.val & ~0U;
3232
3233 /* #UD condition is already handled. */
3234 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3235 return emulate_gp(ctxt, 0);
3236
3237 /* Disable writeback. */
3238 ctxt->dst.type = OP_NONE;
3239 return X86EMUL_CONTINUE;
3240}
3241
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003242static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3243{
3244 u64 msr_data;
3245
Avi Kivitydd856ef2012-08-27 23:46:17 +03003246 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3247 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3248 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003249 return emulate_gp(ctxt, 0);
3250
3251 return X86EMUL_CONTINUE;
3252}
3253
3254static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3255{
3256 u64 msr_data;
3257
Avi Kivitydd856ef2012-08-27 23:46:17 +03003258 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003259 return emulate_gp(ctxt, 0);
3260
Avi Kivitydd856ef2012-08-27 23:46:17 +03003261 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3262 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003263 return X86EMUL_CONTINUE;
3264}
3265
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003266static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3267{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003268 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003269 return emulate_ud(ctxt);
3270
Avi Kivity9dac77f2011-06-01 15:34:25 +03003271 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Nadav Amitb5bbf102014-11-02 11:54:46 +02003272 if (ctxt->dst.bytes == 4 && ctxt->dst.type == OP_MEM)
3273 ctxt->dst.bytes = 2;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003274 return X86EMUL_CONTINUE;
3275}
3276
3277static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3278{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003279 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003280
Avi Kivity9dac77f2011-06-01 15:34:25 +03003281 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003282 return emulate_ud(ctxt);
3283
Avi Kivity9dac77f2011-06-01 15:34:25 +03003284 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003285 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3286
3287 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003288 ctxt->dst.type = OP_NONE;
3289 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003290}
3291
Avi Kivitya14e5792012-06-13 12:28:33 +03003292static int em_lldt(struct x86_emulate_ctxt *ctxt)
3293{
3294 u16 sel = ctxt->src.val;
3295
3296 /* Disable writeback. */
3297 ctxt->dst.type = OP_NONE;
3298 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3299}
3300
Avi Kivity80890002012-06-13 16:33:29 +03003301static int em_ltr(struct x86_emulate_ctxt *ctxt)
3302{
3303 u16 sel = ctxt->src.val;
3304
3305 /* Disable writeback. */
3306 ctxt->dst.type = OP_NONE;
3307 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3308}
3309
Avi Kivity38503912011-03-31 18:48:09 +02003310static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3311{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003312 int rc;
3313 ulong linear;
3314
Avi Kivity9dac77f2011-06-01 15:34:25 +03003315 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003316 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003317 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003318 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003319 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003320 return X86EMUL_CONTINUE;
3321}
3322
Avi Kivity2d04a052011-04-20 15:32:49 +03003323static int em_clts(struct x86_emulate_ctxt *ctxt)
3324{
3325 ulong cr0;
3326
3327 cr0 = ctxt->ops->get_cr(ctxt, 0);
3328 cr0 &= ~X86_CR0_TS;
3329 ctxt->ops->set_cr(ctxt, 0, cr0);
3330 return X86EMUL_CONTINUE;
3331}
3332
Jan Kiszkab34a8052015-03-09 20:27:43 +01003333static int em_hypercall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003334{
Nadav Amit0f54a322014-08-29 11:26:55 +03003335 int rc = ctxt->ops->fix_hypercall(ctxt);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003336
Avi Kivity26d05cc2011-04-21 12:07:59 +03003337 if (rc != X86EMUL_CONTINUE)
3338 return rc;
3339
3340 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003341 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003342 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003343 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003344 return X86EMUL_CONTINUE;
3345}
3346
Avi Kivity96051572012-06-10 17:21:18 +03003347static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3348 void (*get)(struct x86_emulate_ctxt *ctxt,
3349 struct desc_ptr *ptr))
3350{
3351 struct desc_ptr desc_ptr;
3352
3353 if (ctxt->mode == X86EMUL_MODE_PROT64)
3354 ctxt->op_bytes = 8;
3355 get(ctxt, &desc_ptr);
3356 if (ctxt->op_bytes == 2) {
3357 ctxt->op_bytes = 4;
3358 desc_ptr.address &= 0x00ffffff;
3359 }
3360 /* Disable writeback. */
3361 ctxt->dst.type = OP_NONE;
3362 return segmented_write(ctxt, ctxt->dst.addr.mem,
3363 &desc_ptr, 2 + ctxt->op_bytes);
3364}
3365
3366static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3367{
3368 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3369}
3370
3371static int em_sidt(struct x86_emulate_ctxt *ctxt)
3372{
3373 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3374}
3375
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003376static int em_lgdt_lidt(struct x86_emulate_ctxt *ctxt, bool lgdt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003377{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003378 struct desc_ptr desc_ptr;
3379 int rc;
3380
Avi Kivity510425f2012-06-07 17:04:36 +03003381 if (ctxt->mode == X86EMUL_MODE_PROT64)
3382 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003383 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003384 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003385 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003386 if (rc != X86EMUL_CONTINUE)
3387 return rc;
Nadav Amit9a9abf62014-11-02 11:54:56 +02003388 if (ctxt->mode == X86EMUL_MODE_PROT64 &&
3389 is_noncanonical_address(desc_ptr.address))
3390 return emulate_gp(ctxt, 0);
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003391 if (lgdt)
3392 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3393 else
3394 ctxt->ops->set_idt(ctxt, &desc_ptr);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003395 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003396 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003397 return X86EMUL_CONTINUE;
3398}
3399
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003400static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3401{
3402 return em_lgdt_lidt(ctxt, true);
3403}
3404
Avi Kivity26d05cc2011-04-21 12:07:59 +03003405static int em_lidt(struct x86_emulate_ctxt *ctxt)
3406{
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003407 return em_lgdt_lidt(ctxt, false);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003408}
3409
3410static int em_smsw(struct x86_emulate_ctxt *ctxt)
3411{
Nadav Amit32e94d02014-06-02 18:34:11 +03003412 if (ctxt->dst.type == OP_MEM)
3413 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003414 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003415 return X86EMUL_CONTINUE;
3416}
3417
3418static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3419{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003420 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003421 | (ctxt->src.val & 0x0f));
3422 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003423 return X86EMUL_CONTINUE;
3424}
3425
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003426static int em_loop(struct x86_emulate_ctxt *ctxt)
3427{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003428 int rc = X86EMUL_CONTINUE;
3429
Paolo Bonzini01485a22014-11-19 18:25:08 +01003430 register_address_increment(ctxt, VCPU_REGS_RCX, -1);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003431 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003432 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
Nadav Amit234f3ce2014-09-18 22:39:38 +03003433 rc = jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003434
Nadav Amit234f3ce2014-09-18 22:39:38 +03003435 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003436}
3437
3438static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3439{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003440 int rc = X86EMUL_CONTINUE;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003441
Nadav Amit234f3ce2014-09-18 22:39:38 +03003442 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
3443 rc = jmp_rel(ctxt, ctxt->src.val);
3444
3445 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003446}
3447
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003448static int em_in(struct x86_emulate_ctxt *ctxt)
3449{
3450 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3451 &ctxt->dst.val))
3452 return X86EMUL_IO_NEEDED;
3453
3454 return X86EMUL_CONTINUE;
3455}
3456
3457static int em_out(struct x86_emulate_ctxt *ctxt)
3458{
3459 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3460 &ctxt->src.val, 1);
3461 /* Disable writeback. */
3462 ctxt->dst.type = OP_NONE;
3463 return X86EMUL_CONTINUE;
3464}
3465
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003466static int em_cli(struct x86_emulate_ctxt *ctxt)
3467{
3468 if (emulator_bad_iopl(ctxt))
3469 return emulate_gp(ctxt, 0);
3470
3471 ctxt->eflags &= ~X86_EFLAGS_IF;
3472 return X86EMUL_CONTINUE;
3473}
3474
3475static int em_sti(struct x86_emulate_ctxt *ctxt)
3476{
3477 if (emulator_bad_iopl(ctxt))
3478 return emulate_gp(ctxt, 0);
3479
3480 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3481 ctxt->eflags |= X86_EFLAGS_IF;
3482 return X86EMUL_CONTINUE;
3483}
3484
Avi Kivity6d6eede2012-06-07 14:11:36 +03003485static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3486{
3487 u32 eax, ebx, ecx, edx;
3488
Avi Kivitydd856ef2012-08-27 23:46:17 +03003489 eax = reg_read(ctxt, VCPU_REGS_RAX);
3490 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003491 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003492 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3493 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3494 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3495 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003496 return X86EMUL_CONTINUE;
3497}
3498
Paolo Bonzini98f73632013-10-31 11:19:42 +01003499static int em_sahf(struct x86_emulate_ctxt *ctxt)
3500{
3501 u32 flags;
3502
3503 flags = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF;
3504 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3505
3506 ctxt->eflags &= ~0xffUL;
3507 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3508 return X86EMUL_CONTINUE;
3509}
3510
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003511static int em_lahf(struct x86_emulate_ctxt *ctxt)
3512{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003513 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3514 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003515 return X86EMUL_CONTINUE;
3516}
3517
Avi Kivity92998362012-06-13 12:25:06 +03003518static int em_bswap(struct x86_emulate_ctxt *ctxt)
3519{
3520 switch (ctxt->op_bytes) {
3521#ifdef CONFIG_X86_64
3522 case 8:
3523 asm("bswap %0" : "+r"(ctxt->dst.val));
3524 break;
3525#endif
3526 default:
3527 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3528 break;
3529 }
3530 return X86EMUL_CONTINUE;
3531}
3532
Nadav Amit13e457e2014-10-13 13:04:13 +03003533static int em_clflush(struct x86_emulate_ctxt *ctxt)
3534{
3535 /* emulating clflush regardless of cpuid */
3536 return X86EMUL_CONTINUE;
3537}
3538
Nadav Amit2276b512015-01-26 09:32:24 +02003539static int em_movsxd(struct x86_emulate_ctxt *ctxt)
3540{
3541 ctxt->dst.val = (s32) ctxt->src.val;
3542 return X86EMUL_CONTINUE;
3543}
3544
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003545static bool valid_cr(int nr)
3546{
3547 switch (nr) {
3548 case 0:
3549 case 2 ... 4:
3550 case 8:
3551 return true;
3552 default:
3553 return false;
3554 }
3555}
3556
3557static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3558{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003559 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003560 return emulate_ud(ctxt);
3561
3562 return X86EMUL_CONTINUE;
3563}
3564
3565static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3566{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003567 u64 new_val = ctxt->src.val64;
3568 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003569 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003570
3571 static u64 cr_reserved_bits[] = {
3572 0xffffffff00000000ULL,
3573 0, 0, 0, /* CR3 checked later */
3574 CR4_RESERVED_BITS,
3575 0, 0, 0,
3576 CR8_RESERVED_BITS,
3577 };
3578
3579 if (!valid_cr(cr))
3580 return emulate_ud(ctxt);
3581
3582 if (new_val & cr_reserved_bits[cr])
3583 return emulate_gp(ctxt, 0);
3584
3585 switch (cr) {
3586 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003587 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003588 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3589 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3590 return emulate_gp(ctxt, 0);
3591
Avi Kivity717746e2011-04-20 13:37:53 +03003592 cr4 = ctxt->ops->get_cr(ctxt, 4);
3593 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003594
3595 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3596 !(cr4 & X86_CR4_PAE))
3597 return emulate_gp(ctxt, 0);
3598
3599 break;
3600 }
3601 case 3: {
3602 u64 rsvd = 0;
3603
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003604 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3605 if (efer & EFER_LMA)
Nadav Amit9d88fca2014-11-02 11:54:52 +02003606 rsvd = CR3_L_MODE_RESERVED_BITS & ~CR3_PCID_INVD;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003607
3608 if (new_val & rsvd)
3609 return emulate_gp(ctxt, 0);
3610
3611 break;
3612 }
3613 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003614 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003615
3616 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3617 return emulate_gp(ctxt, 0);
3618
3619 break;
3620 }
3621 }
3622
3623 return X86EMUL_CONTINUE;
3624}
3625
Joerg Roedel3b88e412011-04-04 12:39:29 +02003626static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3627{
3628 unsigned long dr7;
3629
Avi Kivity717746e2011-04-20 13:37:53 +03003630 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003631
3632 /* Check if DR7.Global_Enable is set */
3633 return dr7 & (1 << 13);
3634}
3635
3636static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3637{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003638 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003639 u64 cr4;
3640
3641 if (dr > 7)
3642 return emulate_ud(ctxt);
3643
Avi Kivity717746e2011-04-20 13:37:53 +03003644 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003645 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3646 return emulate_ud(ctxt);
3647
Nadav Amit6d2a0522014-11-02 11:54:43 +02003648 if (check_dr7_gd(ctxt)) {
3649 ulong dr6;
3650
3651 ctxt->ops->get_dr(ctxt, 6, &dr6);
3652 dr6 &= ~15;
3653 dr6 |= DR6_BD | DR6_RTM;
3654 ctxt->ops->set_dr(ctxt, 6, dr6);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003655 return emulate_db(ctxt);
Nadav Amit6d2a0522014-11-02 11:54:43 +02003656 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02003657
3658 return X86EMUL_CONTINUE;
3659}
3660
3661static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3662{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003663 u64 new_val = ctxt->src.val64;
3664 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003665
3666 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3667 return emulate_gp(ctxt, 0);
3668
3669 return check_dr_read(ctxt);
3670}
3671
Joerg Roedel01de8b02011-04-04 12:39:31 +02003672static int check_svme(struct x86_emulate_ctxt *ctxt)
3673{
3674 u64 efer;
3675
Avi Kivity717746e2011-04-20 13:37:53 +03003676 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003677
3678 if (!(efer & EFER_SVME))
3679 return emulate_ud(ctxt);
3680
3681 return X86EMUL_CONTINUE;
3682}
3683
3684static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3685{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003686 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003687
3688 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003689 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003690 return emulate_gp(ctxt, 0);
3691
3692 return check_svme(ctxt);
3693}
3694
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003695static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3696{
Avi Kivity717746e2011-04-20 13:37:53 +03003697 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003698
Avi Kivity717746e2011-04-20 13:37:53 +03003699 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003700 return emulate_ud(ctxt);
3701
3702 return X86EMUL_CONTINUE;
3703}
3704
Joerg Roedel80612522011-04-04 12:39:33 +02003705static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3706{
Avi Kivity717746e2011-04-20 13:37:53 +03003707 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003708 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003709
Avi Kivity717746e2011-04-20 13:37:53 +03003710 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03003711 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02003712 return emulate_gp(ctxt, 0);
3713
3714 return X86EMUL_CONTINUE;
3715}
3716
Joerg Roedelf6511932011-04-04 12:39:35 +02003717static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3718{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003719 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3720 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003721 return emulate_gp(ctxt, 0);
3722
3723 return X86EMUL_CONTINUE;
3724}
3725
3726static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3727{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003728 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3729 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003730 return emulate_gp(ctxt, 0);
3731
3732 return X86EMUL_CONTINUE;
3733}
3734
Avi Kivity73fba5f2010-07-29 15:11:53 +03003735#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003736#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
3737#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
3738 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003739#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003740#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003741#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3742#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Nadav Amit39f062f2014-11-26 15:47:18 +02003743#define ID(_f, _i) { .flags = ((_f) | InstrDual | ModRM), .u.idual = (_i) }
Nadav Amit2276b512015-01-26 09:32:24 +02003744#define MD(_f, _m) { .flags = ((_f) | ModeDual), .u.mdual = (_m) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003745#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003746#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003747#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003748#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003749 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003750#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003751 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
3752 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003753#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003754
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003755#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003756#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003757#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003758#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003759#define I2bvIP(_f, _e, _i, _p) \
3760 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003761
Avi Kivityfb864fb2013-01-04 16:18:54 +02003762#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3763 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3764 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003765
Nadav Amit0f54a322014-08-29 11:26:55 +03003766static const struct opcode group7_rm0[] = {
3767 N,
Jan Kiszkab34a8052015-03-09 20:27:43 +01003768 I(SrcNone | Priv | EmulateOnUD, em_hypercall),
Nadav Amit0f54a322014-08-29 11:26:55 +03003769 N, N, N, N, N, N,
3770};
3771
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003772static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003773 DI(SrcNone | Priv, monitor),
3774 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003775 N, N, N, N, N, N,
3776};
3777
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003778static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003779 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Jan Kiszkab34a8052015-03-09 20:27:43 +01003780 II(SrcNone | Prot | EmulateOnUD, em_hypercall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003781 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3782 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3783 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3784 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3785 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3786 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003787};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003788
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003789static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003790 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003791 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003792 N, N, N, N, N, N,
3793};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003794
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003795static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003796 F(Lock, em_add),
3797 F(Lock | PageTable, em_or),
3798 F(Lock, em_adc),
3799 F(Lock, em_sbb),
3800 F(Lock | PageTable, em_and),
3801 F(Lock, em_sub),
3802 F(Lock, em_xor),
3803 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003804};
3805
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003806static const struct opcode group1A[] = {
Nadav Amitab708092014-12-25 02:52:21 +02003807 I(DstMem | SrcNone | Mov | Stack | IncSP, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003808};
3809
Avi Kivity007a3b52013-01-19 19:51:51 +02003810static const struct opcode group2[] = {
3811 F(DstMem | ModRM, em_rol),
3812 F(DstMem | ModRM, em_ror),
3813 F(DstMem | ModRM, em_rcl),
3814 F(DstMem | ModRM, em_rcr),
3815 F(DstMem | ModRM, em_shl),
3816 F(DstMem | ModRM, em_shr),
3817 F(DstMem | ModRM, em_shl),
3818 F(DstMem | ModRM, em_sar),
3819};
3820
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003821static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003822 F(DstMem | SrcImm | NoWrite, em_test),
3823 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003824 F(DstMem | SrcNone | Lock, em_not),
3825 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02003826 F(DstXacc | Src2Mem, em_mul_ex),
3827 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02003828 F(DstXacc | Src2Mem, em_div_ex),
3829 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003830};
3831
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003832static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003833 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3834 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003835 N, N, N, N, N, N,
3836};
3837
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003838static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003839 F(DstMem | SrcNone | Lock, em_inc),
3840 F(DstMem | SrcNone | Lock, em_dec),
Nadav Amit58b70752014-10-24 11:35:09 +03003841 I(SrcMem | NearBranch, em_call_near_abs),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003842 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
Nadav Amit58b70752014-10-24 11:35:09 +03003843 I(SrcMem | NearBranch, em_jmp_abs),
Nadav Amitf7784042014-09-18 22:39:41 +03003844 I(SrcMemFAddr | ImplicitOps, em_jmp_far),
3845 I(SrcMem | Stack, em_push), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003846};
3847
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003848static const struct opcode group6[] = {
Nadav Amit63ea0a42015-01-08 11:59:03 +01003849 DI(Prot | DstMem, sldt),
3850 DI(Prot | DstMem, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003851 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003852 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003853 N, N, N, N,
3854};
3855
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003856static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03003857 II(Mov | DstMem, em_sgdt, sgdt),
3858 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003859 II(SrcMem | Priv, em_lgdt, lgdt),
3860 II(SrcMem | Priv, em_lidt, lidt),
3861 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3862 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3863 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003864}, {
Nadav Amit0f54a322014-08-29 11:26:55 +03003865 EXT(0, group7_rm0),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003866 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003867 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003868 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3869 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3870 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003871} };
3872
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003873static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003874 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003875 F(DstMem | SrcImmByte | NoWrite, em_bt),
3876 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3877 F(DstMem | SrcImmByte | Lock, em_btr),
3878 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003879};
3880
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003881static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003882 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003883}, {
3884 N, N, N, N, N, N, N, N,
3885} };
3886
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003887static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003888 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003889 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003890};
3891
Nadav Amit13e457e2014-10-13 13:04:13 +03003892static const struct gprefix pfx_0f_ae_7 = {
Nadav Amit3f6f1482014-10-13 13:04:14 +03003893 I(SrcMem | ByteOp, em_clflush), N, N, N,
Nadav Amit13e457e2014-10-13 13:04:13 +03003894};
3895
3896static const struct group_dual group15 = { {
3897 N, N, N, N, N, N, N, GP(0, &pfx_0f_ae_7),
3898}, {
3899 N, N, N, N, N, N, N, N,
3900} };
3901
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003902static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003903 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003904};
3905
Nadav Amit39f062f2014-11-26 15:47:18 +02003906static const struct instr_dual instr_dual_0f_2b = {
3907 I(0, em_mov), N
3908};
3909
Paolo Bonzinid5b77062014-07-14 12:54:48 +02003910static const struct gprefix pfx_0f_2b = {
Nadav Amit39f062f2014-11-26 15:47:18 +02003911 ID(0, &instr_dual_0f_2b), ID(0, &instr_dual_0f_2b), N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003912};
3913
Igor Mammedov27ce8252014-03-15 21:01:59 +01003914static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01003915 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003916};
3917
Alex Williamson0a370272014-07-11 11:56:31 -06003918static const struct gprefix pfx_0f_e7 = {
3919 N, I(Sse, em_mov), N, N,
3920};
3921
Gleb Natapov045a2822012-12-20 16:57:43 +02003922static const struct escape escape_d9 = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02003923 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstcw),
Gleb Natapov045a2822012-12-20 16:57:43 +02003924}, {
3925 /* 0xC0 - 0xC7 */
3926 N, N, N, N, N, N, N, N,
3927 /* 0xC8 - 0xCF */
3928 N, N, N, N, N, N, N, N,
3929 /* 0xD0 - 0xC7 */
3930 N, N, N, N, N, N, N, N,
3931 /* 0xD8 - 0xDF */
3932 N, N, N, N, N, N, N, N,
3933 /* 0xE0 - 0xE7 */
3934 N, N, N, N, N, N, N, N,
3935 /* 0xE8 - 0xEF */
3936 N, N, N, N, N, N, N, N,
3937 /* 0xF0 - 0xF7 */
3938 N, N, N, N, N, N, N, N,
3939 /* 0xF8 - 0xFF */
3940 N, N, N, N, N, N, N, N,
3941} };
3942
3943static const struct escape escape_db = { {
3944 N, N, N, N, N, N, N, N,
3945}, {
3946 /* 0xC0 - 0xC7 */
3947 N, N, N, N, N, N, N, N,
3948 /* 0xC8 - 0xCF */
3949 N, N, N, N, N, N, N, N,
3950 /* 0xD0 - 0xC7 */
3951 N, N, N, N, N, N, N, N,
3952 /* 0xD8 - 0xDF */
3953 N, N, N, N, N, N, N, N,
3954 /* 0xE0 - 0xE7 */
3955 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3956 /* 0xE8 - 0xEF */
3957 N, N, N, N, N, N, N, N,
3958 /* 0xF0 - 0xF7 */
3959 N, N, N, N, N, N, N, N,
3960 /* 0xF8 - 0xFF */
3961 N, N, N, N, N, N, N, N,
3962} };
3963
3964static const struct escape escape_dd = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02003965 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstsw),
Gleb Natapov045a2822012-12-20 16:57:43 +02003966}, {
3967 /* 0xC0 - 0xC7 */
3968 N, N, N, N, N, N, N, N,
3969 /* 0xC8 - 0xCF */
3970 N, N, N, N, N, N, N, N,
3971 /* 0xD0 - 0xC7 */
3972 N, N, N, N, N, N, N, N,
3973 /* 0xD8 - 0xDF */
3974 N, N, N, N, N, N, N, N,
3975 /* 0xE0 - 0xE7 */
3976 N, N, N, N, N, N, N, N,
3977 /* 0xE8 - 0xEF */
3978 N, N, N, N, N, N, N, N,
3979 /* 0xF0 - 0xF7 */
3980 N, N, N, N, N, N, N, N,
3981 /* 0xF8 - 0xFF */
3982 N, N, N, N, N, N, N, N,
3983} };
3984
Nadav Amit39f062f2014-11-26 15:47:18 +02003985static const struct instr_dual instr_dual_0f_c3 = {
3986 I(DstMem | SrcReg | ModRM | No16 | Mov, em_mov), N
3987};
3988
Nadav Amit2276b512015-01-26 09:32:24 +02003989static const struct mode_dual mode_dual_63 = {
3990 N, I(DstReg | SrcMem32 | ModRM | Mov, em_movsxd)
3991};
3992
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003993static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003994 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003995 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003996 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3997 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003998 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003999 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004000 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
4001 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004002 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004003 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004004 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
4005 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004006 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004007 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004008 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
4009 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004010 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004011 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004012 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004013 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004014 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004015 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004016 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004017 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004018 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02004019 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004020 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03004021 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004022 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09004023 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004024 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09004025 I(ImplicitOps | Stack | No64, em_pusha),
4026 I(ImplicitOps | Stack | No64, em_popa),
Nadav Amit2276b512015-01-26 09:32:24 +02004027 N, MD(ModRM, &mode_dual_63),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004028 N, N, N, N,
4029 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03004030 I(SrcImm | Mov | Stack, em_push),
4031 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03004032 I(SrcImmByte | Mov | Stack, em_push),
4033 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004034 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09004035 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03004036 /* 0x70 - 0x7F */
Nadav Amit58b70752014-10-24 11:35:09 +03004037 X16(D(SrcImmByte | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004038 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004039 G(ByteOp | DstMem | SrcImm, group1),
4040 G(DstMem | SrcImm, group1),
4041 G(ByteOp | DstMem | SrcImm | No64, group1),
4042 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02004043 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004044 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004045 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004046 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004047 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004048 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09004049 D(ModRM | SrcMem | NoAccess | DstReg),
4050 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
4051 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004052 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004053 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004054 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03004055 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08004056 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09004057 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01004058 II(ImplicitOps | Stack, em_popf, popf),
4059 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004060 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004061 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004062 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004063 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004064 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004065 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004066 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004067 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
4068 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004069 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004070 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004071 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004072 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02004073 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004074 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004075 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Nadav Amit58b70752014-10-24 11:35:09 +03004076 I(ImplicitOps | NearBranch | SrcImmU16, em_ret_near_imm),
4077 I(ImplicitOps | NearBranch, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03004078 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
4079 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004080 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004081 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03004082 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Nadav Amit16794aa2015-01-26 09:32:22 +02004083 I(ImplicitOps | SrcImmU16, em_ret_far_imm),
4084 I(ImplicitOps, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02004085 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004086 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004087 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004088 G(Src2One | ByteOp, group2), G(Src2One, group2),
4089 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02004090 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02004091 I(DstAcc | SrcImmUByte | No64, em_aad),
4092 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004093 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004094 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02004095 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004096 /* 0xE0 - 0xE7 */
Nadav Amit58b70752014-10-24 11:35:09 +03004097 X3(I(SrcImmByte | NearBranch, em_loop)),
4098 I(SrcImmByte | NearBranch, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004099 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
4100 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004101 /* 0xE8 - 0xEF */
Nadav Amit58b70752014-10-24 11:35:09 +03004102 I(SrcImm | NearBranch, em_call), D(SrcImm | ImplicitOps | NearBranch),
4103 I(SrcImmFAddr | No64, em_jmp_far),
4104 D(SrcImmByte | ImplicitOps | NearBranch),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004105 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
4106 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004107 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004108 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004109 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4110 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004111 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09004112 D(ImplicitOps), D(ImplicitOps),
4113 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004114 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4115};
4116
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004117static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004118 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004119 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02004120 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004121 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004122 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004123 N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004124 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004125 N, N, N, N, N, N, N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004126 D(ImplicitOps | ModRM | SrcMem | NoAccess),
4127 N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004128 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03004129 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
4130 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
4131 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
4132 check_cr_write),
4133 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
4134 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004135 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004136 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
4137 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004138 N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b),
Avi Kivity3e114eb2012-04-09 18:40:01 +03004139 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004140 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004141 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004142 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004143 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004144 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02004145 I(ImplicitOps | EmulateOnUD, em_sysenter),
4146 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004147 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004148 N, N, N, N, N, N, N, N,
4149 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03004150 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004151 /* 0x50 - 0x5F */
4152 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4153 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004154 N, N, N, N,
4155 N, N, N, N,
4156 N, N, N, N,
4157 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004158 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004159 N, N, N, N,
4160 N, N, N, N,
4161 N, N, N, N,
4162 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004163 /* 0x80 - 0x8F */
Nadav Amit58b70752014-10-24 11:35:09 +03004164 X16(D(SrcImm | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004165 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004166 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004167 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004168 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004169 II(ImplicitOps, em_cpuid, cpuid),
4170 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004171 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4172 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004173 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004174 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004175 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004176 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004177 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4178 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Nadav Amit13e457e2014-10-13 13:04:13 +03004179 GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004180 /* 0xB0 - 0xB7 */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02004181 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable | SrcWrite, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004182 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004183 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004184 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4185 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004186 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004187 /* 0xB8 - 0xBF */
4188 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004189 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004190 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
4191 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004192 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004193 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02004194 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Nadav Amit39f062f2014-11-26 15:47:18 +02004195 N, ID(0, &instr_dual_0f_c3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004196 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004197 /* 0xC8 - 0xCF */
4198 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004199 /* 0xD0 - 0xDF */
4200 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4201 /* 0xE0 - 0xEF */
Alex Williamson0a370272014-07-11 11:56:31 -06004202 N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
4203 N, N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004204 /* 0xF0 - 0xFF */
4205 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4206};
4207
Nadav Amit39f062f2014-11-26 15:47:18 +02004208static const struct instr_dual instr_dual_0f_38_f0 = {
4209 I(DstReg | SrcMem | Mov, em_movbe), N
4210};
4211
4212static const struct instr_dual instr_dual_0f_38_f1 = {
4213 I(DstMem | SrcReg | Mov, em_movbe), N
4214};
4215
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004216static const struct gprefix three_byte_0f_38_f0 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004217 ID(0, &instr_dual_0f_38_f0), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004218};
4219
4220static const struct gprefix three_byte_0f_38_f1 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004221 ID(0, &instr_dual_0f_38_f1), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004222};
4223
4224/*
4225 * Insns below are selected by the prefix which indexed by the third opcode
4226 * byte.
4227 */
4228static const struct opcode opcode_map_0f_38[256] = {
4229 /* 0x00 - 0x7f */
4230 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004231 /* 0x80 - 0xef */
4232 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4233 /* 0xf0 - 0xf1 */
Nadav Amit53bb4f72014-12-07 11:49:42 +02004234 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0),
4235 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004236 /* 0xf2 - 0xff */
4237 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004238};
4239
Avi Kivity73fba5f2010-07-29 15:11:53 +03004240#undef D
4241#undef N
4242#undef G
4243#undef GD
4244#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004245#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004246#undef EXT
Nadav Amit2276b512015-01-26 09:32:24 +02004247#undef MD
Nadav Amit2b42fce2015-01-26 09:32:25 +02004248#undef ID
Avi Kivity73fba5f2010-07-29 15:11:53 +03004249
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004250#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004251#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004252#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004253#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004254#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004255
Avi Kivity9dac77f2011-06-01 15:34:25 +03004256static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004257{
4258 unsigned size;
4259
Avi Kivity9dac77f2011-06-01 15:34:25 +03004260 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004261 if (size == 8)
4262 size = 4;
4263 return size;
4264}
4265
4266static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4267 unsigned size, bool sign_extension)
4268{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004269 int rc = X86EMUL_CONTINUE;
4270
4271 op->type = OP_IMM;
4272 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004273 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004274 /* NB. Immediates are sign-extended as necessary. */
4275 switch (op->bytes) {
4276 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004277 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004278 break;
4279 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004280 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004281 break;
4282 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004283 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004284 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004285 case 8:
4286 op->val = insn_fetch(s64, ctxt);
4287 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004288 }
4289 if (!sign_extension) {
4290 switch (op->bytes) {
4291 case 1:
4292 op->val &= 0xff;
4293 break;
4294 case 2:
4295 op->val &= 0xffff;
4296 break;
4297 case 4:
4298 op->val &= 0xffffffff;
4299 break;
4300 }
4301 }
4302done:
4303 return rc;
4304}
4305
Avi Kivitya99455492011-09-13 10:45:41 +03004306static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4307 unsigned d)
4308{
4309 int rc = X86EMUL_CONTINUE;
4310
4311 switch (d) {
4312 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004313 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03004314 break;
4315 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004316 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03004317 break;
4318 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004319 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004320 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03004321 *op = ctxt->memop;
4322 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004323 if (ctxt->d & BitOp)
Avi Kivitya99455492011-09-13 10:45:41 +03004324 fetch_bit_operand(ctxt);
4325 op->orig_val = op->val;
4326 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004327 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004328 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004329 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03004330 case OpAcc:
4331 op->type = OP_REG;
4332 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004333 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03004334 fetch_register_operand(op);
4335 op->orig_val = op->val;
4336 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004337 case OpAccLo:
4338 op->type = OP_REG;
4339 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4340 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4341 fetch_register_operand(op);
4342 op->orig_val = op->val;
4343 break;
4344 case OpAccHi:
4345 if (ctxt->d & ByteOp) {
4346 op->type = OP_NONE;
4347 break;
4348 }
4349 op->type = OP_REG;
4350 op->bytes = ctxt->op_bytes;
4351 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4352 fetch_register_operand(op);
4353 op->orig_val = op->val;
4354 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004355 case OpDI:
4356 op->type = OP_MEM;
4357 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4358 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004359 register_address(ctxt, VCPU_REGS_RDI);
Avi Kivitya99455492011-09-13 10:45:41 +03004360 op->addr.mem.seg = VCPU_SREG_ES;
4361 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004362 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03004363 break;
4364 case OpDX:
4365 op->type = OP_REG;
4366 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004367 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03004368 fetch_register_operand(op);
4369 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004370 case OpCL:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004371 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004372 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004373 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004374 break;
4375 case OpImmByte:
4376 rc = decode_imm(ctxt, op, 1, true);
4377 break;
4378 case OpOne:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004379 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004380 op->bytes = 1;
4381 op->val = 1;
4382 break;
4383 case OpImm:
4384 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4385 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004386 case OpImm64:
4387 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4388 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004389 case OpMem8:
4390 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004391 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004392 ctxt->memop.addr.reg = decode_register(ctxt,
4393 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004394 fetch_register_operand(&ctxt->memop);
4395 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004396 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004397 case OpMem16:
4398 ctxt->memop.bytes = 2;
4399 goto mem_common;
4400 case OpMem32:
4401 ctxt->memop.bytes = 4;
4402 goto mem_common;
4403 case OpImmU16:
4404 rc = decode_imm(ctxt, op, 2, false);
4405 break;
4406 case OpImmU:
4407 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4408 break;
4409 case OpSI:
4410 op->type = OP_MEM;
4411 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4412 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004413 register_address(ctxt, VCPU_REGS_RSI);
Bandan Das573e80f2014-04-16 12:46:13 -04004414 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03004415 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004416 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004417 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004418 case OpXLat:
4419 op->type = OP_MEM;
4420 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4421 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004422 address_mask(ctxt,
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004423 reg_read(ctxt, VCPU_REGS_RBX) +
4424 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04004425 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004426 op->val = 0;
4427 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004428 case OpImmFAddr:
4429 op->type = OP_IMM;
4430 op->addr.mem.ea = ctxt->_eip;
4431 op->bytes = ctxt->op_bytes + 2;
4432 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4433 break;
4434 case OpMemFAddr:
4435 ctxt->memop.bytes = ctxt->op_bytes + 2;
4436 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004437 case OpES:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004438 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004439 op->val = VCPU_SREG_ES;
4440 break;
4441 case OpCS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004442 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004443 op->val = VCPU_SREG_CS;
4444 break;
4445 case OpSS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004446 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004447 op->val = VCPU_SREG_SS;
4448 break;
4449 case OpDS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004450 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004451 op->val = VCPU_SREG_DS;
4452 break;
4453 case OpFS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004454 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004455 op->val = VCPU_SREG_FS;
4456 break;
4457 case OpGS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004458 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004459 op->val = VCPU_SREG_GS;
4460 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004461 case OpImplicit:
4462 /* Special instructions do their own operand decoding. */
4463 default:
4464 op->type = OP_NONE; /* Disable writeback. */
4465 break;
4466 }
4467
4468done:
4469 return rc;
4470}
4471
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004472int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004473{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004474 int rc = X86EMUL_CONTINUE;
4475 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004476 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004477 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04004478 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03004479 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004480
Avi Kivityf09ed832011-09-13 10:45:40 +03004481 ctxt->memop.type = OP_NONE;
4482 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004483 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02004484 ctxt->fetch.ptr = ctxt->fetch.data;
4485 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004486 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01004487 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004488 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004489 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02004490 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004491 if (rc != X86EMUL_CONTINUE)
4492 return rc;
4493 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004494
4495 switch (mode) {
4496 case X86EMUL_MODE_REAL:
4497 case X86EMUL_MODE_VM86:
4498 case X86EMUL_MODE_PROT16:
4499 def_op_bytes = def_ad_bytes = 2;
4500 break;
4501 case X86EMUL_MODE_PROT32:
4502 def_op_bytes = def_ad_bytes = 4;
4503 break;
4504#ifdef CONFIG_X86_64
4505 case X86EMUL_MODE_PROT64:
4506 def_op_bytes = 4;
4507 def_ad_bytes = 8;
4508 break;
4509#endif
4510 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004511 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004512 }
4513
Avi Kivity9dac77f2011-06-01 15:34:25 +03004514 ctxt->op_bytes = def_op_bytes;
4515 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004516
4517 /* Legacy prefixes. */
4518 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004519 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004520 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004521 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004522 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004523 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004524 break;
4525 case 0x67: /* address-size override */
4526 if (mode == X86EMUL_MODE_PROT64)
4527 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004528 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004529 else
4530 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004531 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004532 break;
4533 case 0x26: /* ES override */
4534 case 0x2e: /* CS override */
4535 case 0x36: /* SS override */
4536 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004537 has_seg_override = true;
4538 ctxt->seg_override = (ctxt->b >> 3) & 3;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004539 break;
4540 case 0x64: /* FS override */
4541 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004542 has_seg_override = true;
4543 ctxt->seg_override = ctxt->b & 7;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004544 break;
4545 case 0x40 ... 0x4f: /* REX */
4546 if (mode != X86EMUL_MODE_PROT64)
4547 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004548 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004549 continue;
4550 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004551 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004552 break;
4553 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004554 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004555 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004556 break;
4557 default:
4558 goto done_prefixes;
4559 }
4560
4561 /* Any legacy prefix after a REX prefix nullifies its effect. */
4562
Avi Kivity9dac77f2011-06-01 15:34:25 +03004563 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004564 }
4565
4566done_prefixes:
4567
4568 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004569 if (ctxt->rex_prefix & 8)
4570 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004571
4572 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004573 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004574 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004575 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004576 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004577 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004578 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004579
4580 /* 0F_38 opcode map */
4581 if (ctxt->b == 0x38) {
4582 ctxt->opcode_len = 3;
4583 ctxt->b = insn_fetch(u8, ctxt);
4584 opcode = opcode_map_0f_38[ctxt->b];
4585 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004586 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004587 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004588
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004589 if (ctxt->d & ModRM)
4590 ctxt->modrm = insn_fetch(u8, ctxt);
4591
Nadav Amit7fe864d2014-06-02 18:34:03 +03004592 /* vex-prefix instructions are not implemented */
4593 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
Nadav Amitd14cb5d2014-11-02 11:54:58 +02004594 (mode == X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) == 0xc0)) {
Nadav Amit7fe864d2014-06-02 18:34:03 +03004595 ctxt->d = NotImpl;
4596 }
4597
Avi Kivity9dac77f2011-06-01 15:34:25 +03004598 while (ctxt->d & GroupMask) {
4599 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004600 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004601 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004602 opcode = opcode.u.group[goffset];
4603 break;
4604 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004605 goffset = (ctxt->modrm >> 3) & 7;
4606 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004607 opcode = opcode.u.gdual->mod3[goffset];
4608 else
4609 opcode = opcode.u.gdual->mod012[goffset];
4610 break;
4611 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004612 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004613 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004614 break;
4615 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004616 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004617 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004618 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004619 switch (simd_prefix) {
4620 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4621 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4622 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4623 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4624 }
4625 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004626 case Escape:
4627 if (ctxt->modrm > 0xbf)
4628 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4629 else
4630 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4631 break;
Nadav Amit39f062f2014-11-26 15:47:18 +02004632 case InstrDual:
4633 if ((ctxt->modrm >> 6) == 3)
4634 opcode = opcode.u.idual->mod3;
4635 else
4636 opcode = opcode.u.idual->mod012;
4637 break;
Nadav Amit2276b512015-01-26 09:32:24 +02004638 case ModeDual:
4639 if (ctxt->mode == X86EMUL_MODE_PROT64)
4640 opcode = opcode.u.mdual->mode64;
4641 else
4642 opcode = opcode.u.mdual->mode32;
4643 break;
Avi Kivity46561642011-04-24 14:09:59 +03004644 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004645 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004646 }
Avi Kivity46561642011-04-24 14:09:59 +03004647
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004648 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004649 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004650 }
4651
Paolo Bonzinie24186e2014-03-27 12:00:57 +01004652 /* Unrecognised? */
4653 if (ctxt->d == 0)
4654 return EMULATION_FAILED;
4655
Avi Kivity9dac77f2011-06-01 15:34:25 +03004656 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004657
Nadav Amit3a6095a2014-08-13 16:50:13 +03004658 if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD)))
4659 return EMULATION_FAILED;
4660
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004661 if (unlikely(ctxt->d &
Nadav Amited9aad22014-11-02 11:55:00 +02004662 (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch|
4663 No16))) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004664 /*
4665 * These are copied unconditionally here, and checked unconditionally
4666 * in x86_emulate_insn.
4667 */
4668 ctxt->check_perm = opcode.check_perm;
4669 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004670
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004671 if (ctxt->d & NotImpl)
4672 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004673
Nadav Amit58b70752014-10-24 11:35:09 +03004674 if (mode == X86EMUL_MODE_PROT64) {
4675 if (ctxt->op_bytes == 4 && (ctxt->d & Stack))
4676 ctxt->op_bytes = 8;
4677 else if (ctxt->d & NearBranch)
4678 ctxt->op_bytes = 8;
4679 }
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004680
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004681 if (ctxt->d & Op3264) {
4682 if (mode == X86EMUL_MODE_PROT64)
4683 ctxt->op_bytes = 8;
4684 else
4685 ctxt->op_bytes = 4;
4686 }
4687
Nadav Amited9aad22014-11-02 11:55:00 +02004688 if ((ctxt->d & No16) && ctxt->op_bytes == 2)
4689 ctxt->op_bytes = 4;
4690
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004691 if (ctxt->d & Sse)
4692 ctxt->op_bytes = 16;
4693 else if (ctxt->d & Mmx)
4694 ctxt->op_bytes = 8;
4695 }
Avi Kivity1253791d2011-03-29 11:41:27 +02004696
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004697 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004698 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004699 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04004700 if (!has_seg_override) {
4701 has_seg_override = true;
4702 ctxt->seg_override = ctxt->modrm_seg;
4703 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004704 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004705 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004706 if (rc != X86EMUL_CONTINUE)
4707 goto done;
4708
Bandan Das573e80f2014-04-16 12:46:13 -04004709 if (!has_seg_override)
4710 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004711
Bandan Das573e80f2014-04-16 12:46:13 -04004712 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004713
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004714 /*
4715 * Decode and fetch the source operand: register, memory
4716 * or immediate.
4717 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004718 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004719 if (rc != X86EMUL_CONTINUE)
4720 goto done;
4721
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004722 /*
4723 * Decode and fetch the second source operand: register, memory
4724 * or immediate.
4725 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004726 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004727 if (rc != X86EMUL_CONTINUE)
4728 goto done;
4729
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004730 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03004731 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004732
Bandan Das41061cd2014-04-16 12:46:14 -04004733 if (ctxt->rip_relative)
Nadav Amit1c1c35a2014-11-19 17:43:09 +02004734 ctxt->memopp->addr.mem.ea = address_mask(ctxt,
4735 ctxt->memopp->addr.mem.ea + ctxt->_eip);
Avi Kivitycb16c342011-06-19 19:21:11 +03004736
Paolo Bonzinia430c912014-10-23 14:54:14 +02004737done:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004738 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004739}
4740
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004741bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4742{
4743 return ctxt->d & PageTable;
4744}
4745
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004746static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4747{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004748 /* The second termination condition only applies for REPE
4749 * and REPNE. Test if the repeat string operation prefix is
4750 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4751 * corresponding termination condition according to:
4752 * - if REPE/REPZ and ZF = 0 then done
4753 * - if REPNE/REPNZ and ZF = 1 then done
4754 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004755 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4756 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4757 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004758 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004759 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004760 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4761 return true;
4762
4763 return false;
4764}
4765
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004766static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4767{
4768 bool fault = false;
4769
4770 ctxt->ops->get_fpu(ctxt);
4771 asm volatile("1: fwait \n\t"
4772 "2: \n\t"
4773 ".pushsection .fixup,\"ax\" \n\t"
4774 "3: \n\t"
4775 "movb $1, %[fault] \n\t"
4776 "jmp 2b \n\t"
4777 ".popsection \n\t"
4778 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004779 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004780 ctxt->ops->put_fpu(ctxt);
4781
4782 if (unlikely(fault))
4783 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4784
4785 return X86EMUL_CONTINUE;
4786}
4787
4788static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4789 struct operand *op)
4790{
4791 if (op->type == OP_MM)
4792 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4793}
4794
Avi Kivitye28bbd42013-01-04 16:18:48 +02004795static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4796{
4797 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivityb9fa4092013-02-09 11:31:48 +02004798 if (!(ctxt->d & ByteOp))
4799 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Avi Kivitye28bbd42013-01-04 16:18:48 +02004800 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004801 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
4802 [fastop]"+S"(fop)
4803 : "c"(ctxt->src2.val));
Avi Kivitye28bbd42013-01-04 16:18:48 +02004804 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004805 if (!fop) /* exception is returned in fop variable */
4806 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02004807 return X86EMUL_CONTINUE;
4808}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004809
Bandan Das14985072014-04-16 12:46:09 -04004810void init_decode_cache(struct x86_emulate_ctxt *ctxt)
4811{
Bandan Das573e80f2014-04-16 12:46:13 -04004812 memset(&ctxt->rip_relative, 0,
4813 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04004814
Bandan Das14985072014-04-16 12:46:09 -04004815 ctxt->io_read.pos = 0;
4816 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04004817 ctxt->mem_read.end = 0;
4818}
4819
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004820int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004821{
Mathias Krause0225fb52012-08-30 01:30:16 +02004822 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004823 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004824 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004825
Avi Kivity9dac77f2011-06-01 15:34:25 +03004826 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004827
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004828 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004829 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004830 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004831 goto done;
4832 }
4833
Avi Kivity9dac77f2011-06-01 15:34:25 +03004834 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004835 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004836 goto done;
4837 }
4838
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004839 if (unlikely(ctxt->d &
4840 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
4841 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4842 (ctxt->d & Undefined)) {
4843 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004844 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004845 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004846
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004847 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4848 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
4849 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004850 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004851 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004852
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004853 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
4854 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004855 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004856 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004857
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004858 if (ctxt->d & Mmx) {
4859 rc = flush_pending_x87_faults(ctxt);
4860 if (rc != X86EMUL_CONTINUE)
4861 goto done;
4862 /*
4863 * Now that we know the fpu is exception safe, we can fetch
4864 * operands from it.
4865 */
4866 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4867 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4868 if (!(ctxt->d & Mov))
4869 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4870 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004871
Bandan Das685bbf42014-04-16 12:46:10 -04004872 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004873 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4874 X86_ICPT_PRE_EXCEPT);
4875 if (rc != X86EMUL_CONTINUE)
4876 goto done;
4877 }
4878
Nadav Amit64a38292014-12-10 11:19:04 +02004879 /* Instruction can only be executed in protected mode */
4880 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
4881 rc = emulate_ud(ctxt);
4882 goto done;
4883 }
4884
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004885 /* Privileged instruction can be executed only in CPL=0 */
4886 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Nadav Amit68efa762014-06-18 17:19:35 +03004887 if (ctxt->d & PrivUD)
4888 rc = emulate_ud(ctxt);
4889 else
4890 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004891 goto done;
4892 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004893
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004894 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04004895 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004896 rc = ctxt->check_perm(ctxt);
4897 if (rc != X86EMUL_CONTINUE)
4898 goto done;
4899 }
4900
Bandan Das685bbf42014-04-16 12:46:10 -04004901 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004902 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4903 X86_ICPT_POST_EXCEPT);
4904 if (rc != X86EMUL_CONTINUE)
4905 goto done;
4906 }
4907
4908 if (ctxt->rep_prefix && (ctxt->d & String)) {
4909 /* All REP prefixes have the same first termination condition */
4910 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
4911 ctxt->eip = ctxt->_eip;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004912 ctxt->eflags &= ~EFLG_RF;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004913 goto done;
4914 }
4915 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004916 }
4917
Avi Kivity9dac77f2011-06-01 15:34:25 +03004918 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4919 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4920 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004921 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004922 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004923 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004924 }
4925
Avi Kivity9dac77f2011-06-01 15:34:25 +03004926 if (ctxt->src2.type == OP_MEM) {
4927 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4928 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004929 if (rc != X86EMUL_CONTINUE)
4930 goto done;
4931 }
4932
Avi Kivity9dac77f2011-06-01 15:34:25 +03004933 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004934 goto special_insn;
4935
4936
Avi Kivity9dac77f2011-06-01 15:34:25 +03004937 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004938 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004939 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4940 &ctxt->dst.val, ctxt->dst.bytes);
Nadav Amitc205fb72014-12-25 02:52:16 +02004941 if (rc != X86EMUL_CONTINUE) {
Paolo Bonzinid44e1212015-02-09 10:02:05 +01004942 if (!(ctxt->d & NoWrite) &&
4943 rc == X86EMUL_PROPAGATE_FAULT &&
Nadav Amitc205fb72014-12-25 02:52:16 +02004944 ctxt->exception.vector == PF_VECTOR)
4945 ctxt->exception.error_code |= PFERR_WRITE_MASK;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004946 goto done;
Nadav Amitc205fb72014-12-25 02:52:16 +02004947 }
Avi Kivity038e51d2007-01-22 20:40:40 -08004948 }
Paolo Bonzini4ff6f8e2015-02-12 17:04:47 +01004949 /* Copy full 64-bit value for CMPXCHG8B. */
4950 ctxt->dst.orig_val64 = ctxt->dst.val64;
Avi Kivity038e51d2007-01-22 20:40:40 -08004951
Avi Kivity018a98d2007-11-27 19:30:56 +02004952special_insn:
4953
Bandan Das685bbf42014-04-16 12:46:10 -04004954 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004955 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004956 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004957 if (rc != X86EMUL_CONTINUE)
4958 goto done;
4959 }
4960
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004961 if (ctxt->rep_prefix && (ctxt->d & String))
4962 ctxt->eflags |= EFLG_RF;
4963 else
4964 ctxt->eflags &= ~EFLG_RF;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004965
Avi Kivity9dac77f2011-06-01 15:34:25 +03004966 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004967 if (ctxt->d & Fastop) {
4968 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4969 rc = fastop(ctxt, fop);
4970 if (rc != X86EMUL_CONTINUE)
4971 goto done;
4972 goto writeback;
4973 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004974 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004975 if (rc != X86EMUL_CONTINUE)
4976 goto done;
4977 goto writeback;
4978 }
4979
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004980 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004981 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004982 else if (ctxt->opcode_len == 3)
4983 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004984
Avi Kivity9dac77f2011-06-01 15:34:25 +03004985 switch (ctxt->b) {
Gleb Natapovb2833e32009-04-12 13:36:30 +03004986 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004987 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03004988 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004989 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004990 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004991 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004992 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004993 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004994 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03004995 ctxt->dst.type = OP_NONE;
4996 else
4997 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004998 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004999 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005000 switch (ctxt->op_bytes) {
5001 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
5002 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
5003 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005004 }
5005 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005006 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005007 rc = emulate_int(ctxt, 3);
5008 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005009 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005010 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005011 break;
5012 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005013 if (ctxt->eflags & EFLG_OF)
5014 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005015 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005016 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09005017 case 0xeb: /* jmp rel short */
Nadav Amit234f3ce2014-09-18 22:39:38 +03005018 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity9dac77f2011-06-01 15:34:25 +03005019 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005020 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005021 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03005022 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03005023 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005024 case 0xf5: /* cmc */
5025 /* complement carry flag from eflags reg */
5026 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005027 break;
5028 case 0xf8: /* clc */
5029 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005030 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03005031 case 0xf9: /* stc */
5032 ctxt->eflags |= EFLG_CF;
5033 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005034 case 0xfc: /* cld */
5035 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005036 break;
5037 case 0xfd: /* std */
5038 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005039 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005040 default:
5041 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005042 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005043
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005044 if (rc != X86EMUL_CONTINUE)
5045 goto done;
5046
Avi Kivity018a98d2007-11-27 19:30:56 +02005047writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02005048 if (ctxt->d & SrcWrite) {
5049 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
5050 rc = writeback(ctxt, &ctxt->src);
5051 if (rc != X86EMUL_CONTINUE)
5052 goto done;
5053 }
Nadav Amitee212292014-06-15 16:12:58 +03005054 if (!(ctxt->d & NoWrite)) {
5055 rc = writeback(ctxt, &ctxt->dst);
5056 if (rc != X86EMUL_CONTINUE)
5057 goto done;
5058 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005059
Gleb Natapov5cd21912010-03-18 15:20:26 +02005060 /*
5061 * restore dst type in case the decoding will be reused
5062 * (happens for string instruction )
5063 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005064 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005065
Avi Kivity9dac77f2011-06-01 15:34:25 +03005066 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005067 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02005068
Avi Kivity9dac77f2011-06-01 15:34:25 +03005069 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005070 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02005071
Avi Kivity9dac77f2011-06-01 15:34:25 +03005072 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005073 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005074 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005075 if ((ctxt->d & SrcMask) == SrcSI)
5076 count = ctxt->src.count;
5077 else
5078 count = ctxt->dst.count;
Paolo Bonzini01485a22014-11-19 18:25:08 +01005079 register_address_increment(ctxt, VCPU_REGS_RCX, -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005080
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005081 if (!string_insn_completed(ctxt)) {
5082 /*
5083 * Re-enter guest when pio read ahead buffer is empty
5084 * or, if it is not used, after each 1024 iteration.
5085 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005086 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005087 (r->end == 0 || r->end != r->pos)) {
5088 /*
5089 * Reset read cache. Usually happens before
5090 * decode, but since instruction is restarted
5091 * we have to do it here.
5092 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005093 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005094 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005095 return EMULATION_RESTART;
5096 }
5097 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03005098 }
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03005099 ctxt->eflags &= ~EFLG_RF;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005100 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005101
Avi Kivity9dac77f2011-06-01 15:34:25 +03005102 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02005103
5104done:
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005105 if (rc == X86EMUL_PROPAGATE_FAULT) {
5106 WARN_ON(ctxt->exception.vector > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +02005107 ctxt->have_exception = true;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005108 }
Joerg Roedel775fde82011-04-04 12:39:24 +02005109 if (rc == X86EMUL_INTERCEPTED)
5110 return EMULATION_INTERCEPTED;
5111
Avi Kivitydd856ef2012-08-27 23:46:17 +03005112 if (rc == X86EMUL_CONTINUE)
5113 writeback_registers(ctxt);
5114
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005115 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005116
5117twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005118 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02005119 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02005120 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005121 break;
5122 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02005123 case 0x0d: /* GrpP (prefetch) */
5124 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02005125 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02005126 break;
5127 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005128 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02005129 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005130 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005131 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005132 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005133 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03005134 if (test_cc(ctxt->b, ctxt->eflags))
5135 ctxt->dst.val = ctxt->src.val;
Nadav Amitb91aa142015-03-30 15:39:19 +03005136 else if (ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005137 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08005138 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03005139 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03005140 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005141 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005142 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08005143 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005144 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08005145 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005146 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005147 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005148 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03005149 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005150 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005151 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005152 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005153 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03005154 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005155 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005156 default:
5157 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005158 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005159
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005160threebyte_insn:
5161
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005162 if (rc != X86EMUL_CONTINUE)
5163 goto done;
5164
Avi Kivity6aa8b732006-12-10 02:21:36 -08005165 goto writeback;
5166
5167cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02005168 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005169}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005170
5171void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
5172{
5173 invalidate_registers(ctxt);
5174}
5175
5176void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
5177{
5178 writeback_registers(ctxt);
5179}