blob: c16314807756172e48c2d4926589356a132f7a04 [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)
89#define DstImmUByte (OpImmUByte << DstShift)
90#define DstDX (OpDX << DstShift)
Avi Kivity820207c2013-02-09 11:31:45 +020091#define DstAccLo (OpAccLo << DstShift)
Avi Kivitya99455492011-09-13 10:45:41 +030092#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080093/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030094#define SrcShift 6
95#define SrcNone (OpNone << SrcShift)
96#define SrcReg (OpReg << SrcShift)
97#define SrcMem (OpMem << SrcShift)
98#define SrcMem16 (OpMem16 << SrcShift)
99#define SrcMem32 (OpMem32 << SrcShift)
100#define SrcImm (OpImm << SrcShift)
101#define SrcImmByte (OpImmByte << SrcShift)
102#define SrcOne (OpOne << SrcShift)
103#define SrcImmUByte (OpImmUByte << SrcShift)
104#define SrcImmU (OpImmU << SrcShift)
105#define SrcSI (OpSI << SrcShift)
Paolo Bonzini7fa57952013-05-09 11:32:50 +0200106#define SrcXLat (OpXLat << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300107#define SrcImmFAddr (OpImmFAddr << SrcShift)
108#define SrcMemFAddr (OpMemFAddr << SrcShift)
109#define SrcAcc (OpAcc << SrcShift)
110#define SrcImmU16 (OpImmU16 << SrcShift)
Nadav Amit5e2c6882012-12-06 21:55:10 -0200111#define SrcImm64 (OpImm64 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300112#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200113#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity820207c2013-02-09 11:31:45 +0200114#define SrcAccHi (OpAccHi << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300115#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300116#define BitOp (1<<11)
117#define MemAbs (1<<12) /* Memory operand is absolute displacement */
118#define String (1<<13) /* String instruction (rep capable) */
119#define Stack (1<<14) /* Stack instruction (push/pop) */
120#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
121#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
122#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
123#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
124#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
Gleb Natapov045a2822012-12-20 16:57:43 +0200125#define Escape (5<<15) /* Escape to coprocessor instruction */
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300126#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300127/* Generic ModRM decode. */
128#define ModRM (1<<19)
129/* Destination is only written; never read. */
130#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300131/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200132#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Borislav Petkovb51e9742013-09-22 16:44:52 +0200133#define EmulateOnUD (1<<22) /* Emulate if unsupported by the host */
Avi Kivity5a506b12010-08-01 15:10:29 +0300134#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300135#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300136#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200137#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200138#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300139#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800140#define PageTable (1 << 29) /* instruction used to write page table */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300141#define NotImpl (1 << 30) /* instruction is not implemented */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100142/* Source 2 operand type */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300143#define Src2Shift (31)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300144#define Src2None (OpNone << Src2Shift)
Avi Kivityab2c5ce2013-02-09 11:31:46 +0200145#define Src2Mem (OpMem << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300146#define Src2CL (OpCL << Src2Shift)
147#define Src2ImmByte (OpImmByte << Src2Shift)
148#define Src2One (OpOne << Src2Shift)
149#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300150#define Src2ES (OpES << Src2Shift)
151#define Src2CS (OpCS << Src2Shift)
152#define Src2SS (OpSS << Src2Shift)
153#define Src2DS (OpDS << Src2Shift)
154#define Src2FS (OpFS << Src2Shift)
155#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300156#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300157#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Avi Kivity1c11b372012-04-09 18:39:59 +0300158#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
159#define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */
160#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
Avi Kivitye28bbd42013-01-04 16:18:48 +0200161#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
Avi Kivityb6744dc2013-01-04 16:18:50 +0200162#define NoWrite ((u64)1 << 45) /* No writeback */
Avi Kivityfb32b1e2013-02-09 11:31:44 +0200163#define SrcWrite ((u64)1 << 46) /* Write back src operand */
Nadav Amit9b88ae92014-05-25 23:05:21 +0300164#define NoMod ((u64)1 << 47) /* Mod field is ignored */
Paolo Bonzinid40a6892014-03-27 11:58:02 +0100165#define Intercept ((u64)1 << 48) /* Has valid intercept field */
166#define CheckPerm ((u64)1 << 49) /* Has valid check_perm field */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800167
Avi Kivity820207c2013-02-09 11:31:45 +0200168#define DstXacc (DstAccLo | SrcAccHi | SrcWrite)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800169
Avi Kivityd0e53322010-07-29 15:11:54 +0300170#define X2(x...) x, x
171#define X3(x...) X2(x), x
172#define X4(x...) X2(x), X2(x)
173#define X5(x...) X4(x), x
174#define X6(x...) X4(x), X2(x)
175#define X7(x...) X4(x), X3(x)
176#define X8(x...) X4(x), X4(x)
177#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300178
Avi Kivitye28bbd42013-01-04 16:18:48 +0200179#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
180#define FASTOP_SIZE 8
181
182/*
183 * fastop functions have a special calling convention:
184 *
Avi Kivity017da7b2013-02-09 11:31:47 +0200185 * dst: rax (in/out)
186 * src: rdx (in/out)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200187 * src2: rcx (in)
188 * flags: rflags (in/out)
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200189 * ex: rsi (in:fastop pointer, out:zero if exception)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200190 *
191 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
192 * different operand sizes can be reached by calculation, rather than a jump
193 * table (which would be bigger than the code).
194 *
195 * fastop functions are declared as taking a never-defined fastop parameter,
196 * so they can't be called from C directly.
197 */
198
199struct fastop;
200
Avi Kivityd65b1de2010-07-29 15:11:35 +0300201struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300202 u64 flags : 56;
203 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300204 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300205 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200206 const struct opcode *group;
207 const struct group_dual *gdual;
208 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200209 const struct escape *esc;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200210 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300211 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200212 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300213};
214
215struct group_dual {
216 struct opcode mod012[8];
217 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300218};
219
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200220struct gprefix {
221 struct opcode pfx_no;
222 struct opcode pfx_66;
223 struct opcode pfx_f2;
224 struct opcode pfx_f3;
225};
226
Gleb Natapov045a2822012-12-20 16:57:43 +0200227struct escape {
228 struct opcode op[8];
229 struct opcode high[64];
230};
231
Avi Kivity6aa8b732006-12-10 02:21:36 -0800232/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200233#define EFLG_ID (1<<21)
234#define EFLG_VIP (1<<20)
235#define EFLG_VIF (1<<19)
236#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200237#define EFLG_VM (1<<17)
238#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200239#define EFLG_IOPL (3<<12)
240#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800241#define EFLG_OF (1<<11)
242#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200243#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200244#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800245#define EFLG_SF (1<<7)
246#define EFLG_ZF (1<<6)
247#define EFLG_AF (1<<4)
248#define EFLG_PF (1<<2)
249#define EFLG_CF (1<<0)
250
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300251#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
252#define EFLG_RESERVED_ONE_MASK 2
253
Avi Kivitydd856ef2012-08-27 23:46:17 +0300254static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
255{
256 if (!(ctxt->regs_valid & (1 << nr))) {
257 ctxt->regs_valid |= 1 << nr;
258 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
259 }
260 return ctxt->_regs[nr];
261}
262
263static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
264{
265 ctxt->regs_valid |= 1 << nr;
266 ctxt->regs_dirty |= 1 << nr;
267 return &ctxt->_regs[nr];
268}
269
270static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
271{
272 reg_read(ctxt, nr);
273 return reg_write(ctxt, nr);
274}
275
276static void writeback_registers(struct x86_emulate_ctxt *ctxt)
277{
278 unsigned reg;
279
280 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
281 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
282}
283
284static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
285{
286 ctxt->regs_dirty = 0;
287 ctxt->regs_valid = 0;
288}
289
Avi Kivity6aa8b732006-12-10 02:21:36 -0800290/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800291 * These EFLAGS bits are restored from saved value during emulation, and
292 * any changes are written back to the saved value after emulation.
293 */
294#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
295
Avi Kivitydda96d82008-11-26 15:14:10 +0200296#ifdef CONFIG_X86_64
297#define ON64(x) x
298#else
299#define ON64(x)
300#endif
301
Avi Kivity4d758342013-01-19 19:51:55 +0200302static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *));
303
Avi Kivityb7d491e2013-01-04 16:18:49 +0200304#define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t"
305#define FOP_RET "ret \n\t"
306
307#define FOP_START(op) \
308 extern void em_##op(struct fastop *fake); \
309 asm(".pushsection .text, \"ax\" \n\t" \
310 ".global em_" #op " \n\t" \
311 FOP_ALIGN \
312 "em_" #op ": \n\t"
313
314#define FOP_END \
315 ".popsection")
316
Avi Kivity0bdea062013-01-19 19:51:50 +0200317#define FOPNOP() FOP_ALIGN FOP_RET
318
Avi Kivityb7d491e2013-01-04 16:18:49 +0200319#define FOP1E(op, dst) \
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200320 FOP_ALIGN "10: " #op " %" #dst " \n\t" FOP_RET
321
322#define FOP1EEX(op, dst) \
323 FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200324
325#define FASTOP1(op) \
326 FOP_START(op) \
327 FOP1E(op##b, al) \
328 FOP1E(op##w, ax) \
329 FOP1E(op##l, eax) \
330 ON64(FOP1E(op##q, rax)) \
331 FOP_END
332
Avi Kivityb9fa4092013-02-09 11:31:48 +0200333/* 1-operand, using src2 (for MUL/DIV r/m) */
334#define FASTOP1SRC2(op, name) \
335 FOP_START(name) \
336 FOP1E(op, cl) \
337 FOP1E(op, cx) \
338 FOP1E(op, ecx) \
339 ON64(FOP1E(op, rcx)) \
340 FOP_END
341
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200342/* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */
343#define FASTOP1SRC2EX(op, name) \
344 FOP_START(name) \
345 FOP1EEX(op, cl) \
346 FOP1EEX(op, cx) \
347 FOP1EEX(op, ecx) \
348 ON64(FOP1EEX(op, rcx)) \
349 FOP_END
350
Avi Kivityf7857f32013-01-04 16:18:53 +0200351#define FOP2E(op, dst, src) \
352 FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET
353
354#define FASTOP2(op) \
355 FOP_START(op) \
Avi Kivity017da7b2013-02-09 11:31:47 +0200356 FOP2E(op##b, al, dl) \
357 FOP2E(op##w, ax, dx) \
358 FOP2E(op##l, eax, edx) \
359 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivityf7857f32013-01-04 16:18:53 +0200360 FOP_END
361
Avi Kivity11c363b2013-01-19 19:51:54 +0200362/* 2 operand, word only */
363#define FASTOP2W(op) \
364 FOP_START(op) \
365 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200366 FOP2E(op##w, ax, dx) \
367 FOP2E(op##l, eax, edx) \
368 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivity11c363b2013-01-19 19:51:54 +0200369 FOP_END
370
Avi Kivity007a3b52013-01-19 19:51:51 +0200371/* 2 operand, src is CL */
372#define FASTOP2CL(op) \
373 FOP_START(op) \
374 FOP2E(op##b, al, cl) \
375 FOP2E(op##w, ax, cl) \
376 FOP2E(op##l, eax, cl) \
377 ON64(FOP2E(op##q, rax, cl)) \
378 FOP_END
379
Avi Kivity0bdea062013-01-19 19:51:50 +0200380#define FOP3E(op, dst, src, src2) \
381 FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
382
383/* 3-operand, word-only, src2=cl */
384#define FASTOP3WCL(op) \
385 FOP_START(op) \
386 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200387 FOP3E(op##w, ax, dx, cl) \
388 FOP3E(op##l, eax, edx, cl) \
389 ON64(FOP3E(op##q, rax, rdx, cl)) \
Avi Kivity0bdea062013-01-19 19:51:50 +0200390 FOP_END
391
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200392/* Special case for SETcc - 1 instruction per cc */
393#define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t"
394
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200395asm(".global kvm_fastop_exception \n"
396 "kvm_fastop_exception: xor %esi, %esi; ret");
397
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200398FOP_START(setcc)
399FOP_SETCC(seto)
400FOP_SETCC(setno)
401FOP_SETCC(setc)
402FOP_SETCC(setnc)
403FOP_SETCC(setz)
404FOP_SETCC(setnz)
405FOP_SETCC(setbe)
406FOP_SETCC(setnbe)
407FOP_SETCC(sets)
408FOP_SETCC(setns)
409FOP_SETCC(setp)
410FOP_SETCC(setnp)
411FOP_SETCC(setl)
412FOP_SETCC(setnl)
413FOP_SETCC(setle)
414FOP_SETCC(setnle)
415FOP_END;
416
Paolo Bonzini326f5782013-05-09 11:32:51 +0200417FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET
418FOP_END;
419
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200420static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
421 enum x86_intercept intercept,
422 enum x86_intercept_stage stage)
423{
424 struct x86_instruction_info info = {
425 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300426 .rep_prefix = ctxt->rep_prefix,
427 .modrm_mod = ctxt->modrm_mod,
428 .modrm_reg = ctxt->modrm_reg,
429 .modrm_rm = ctxt->modrm_rm,
430 .src_val = ctxt->src.val64,
Jan Kiszka6cbc5f52014-06-30 12:52:55 +0200431 .dst_val = ctxt->dst.val64,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300432 .src_bytes = ctxt->src.bytes,
433 .dst_bytes = ctxt->dst.bytes,
434 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200435 .next_rip = ctxt->eip,
436 };
437
Avi Kivity29535382011-04-20 13:37:53 +0300438 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200439}
440
Avi Kivityf47cfa32012-06-07 17:49:24 +0300441static void assign_masked(ulong *dest, ulong src, ulong mask)
442{
443 *dest = (*dest & ~mask) | (src & mask);
444}
445
Avi Kivity9dac77f2011-06-01 15:34:25 +0300446static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800447{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300448 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800449}
450
Avi Kivityf47cfa32012-06-07 17:49:24 +0300451static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
452{
453 u16 sel;
454 struct desc_struct ss;
455
456 if (ctxt->mode == X86EMUL_MODE_PROT64)
457 return ~0UL;
458 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
459 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
460}
461
Avi Kivity612e89f2012-06-12 20:03:23 +0300462static int stack_size(struct x86_emulate_ctxt *ctxt)
463{
464 return (__fls(stack_mask(ctxt)) + 1) >> 3;
465}
466
Avi Kivity6aa8b732006-12-10 02:21:36 -0800467/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800468static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300469address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800470{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300471 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800472 return reg;
473 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300474 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800475}
476
477static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300478register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800479{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300480 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800481}
482
Avi Kivity5ad105e2012-08-19 14:34:31 +0300483static void masked_increment(ulong *reg, ulong mask, int inc)
484{
485 assign_masked(reg, *reg + inc, mask);
486}
487
Harvey Harrison7a9572752008-02-19 07:40:41 -0800488static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300489register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800490{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300491 ulong mask;
492
Avi Kivity9dac77f2011-06-01 15:34:25 +0300493 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300494 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800495 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300496 mask = ad_mask(ctxt);
497 masked_increment(reg, mask, inc);
498}
499
500static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
501{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300502 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800503}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800504
Avi Kivity9dac77f2011-06-01 15:34:25 +0300505static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800506{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300507 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800508}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300509
Avi Kivity56697682011-04-03 14:08:51 +0300510static u32 desc_limit_scaled(struct desc_struct *desc)
511{
512 u32 limit = get_desc_limit(desc);
513
514 return desc->g ? (limit << 12) | 0xfff : limit;
515}
516
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900517static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300518{
519 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
520 return 0;
521
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900522 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300523}
524
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200525static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
526 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300527{
Avi Kivityda9cb572010-11-22 17:53:21 +0200528 ctxt->exception.vector = vec;
529 ctxt->exception.error_code = error;
530 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200531 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300532}
533
Joerg Roedel3b88e412011-04-04 12:39:29 +0200534static int emulate_db(struct x86_emulate_ctxt *ctxt)
535{
536 return emulate_exception(ctxt, DB_VECTOR, 0, false);
537}
538
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200539static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300540{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200541 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300542}
543
Avi Kivity618ff152011-04-03 12:32:09 +0300544static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
545{
546 return emulate_exception(ctxt, SS_VECTOR, err, true);
547}
548
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200549static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300550{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200551 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300552}
553
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200554static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300555{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200556 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300557}
558
Avi Kivity34d1f492010-08-26 11:59:01 +0300559static int emulate_de(struct x86_emulate_ctxt *ctxt)
560{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200561 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300562}
563
Avi Kivity1253791d2011-03-29 11:41:27 +0200564static int emulate_nm(struct x86_emulate_ctxt *ctxt)
565{
566 return emulate_exception(ctxt, NM_VECTOR, 0, false);
567}
568
Avi Kivity1aa36612011-04-27 13:20:30 +0300569static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
570{
571 u16 selector;
572 struct desc_struct desc;
573
574 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
575 return selector;
576}
577
578static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
579 unsigned seg)
580{
581 u16 dummy;
582 u32 base3;
583 struct desc_struct desc;
584
585 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
586 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
587}
588
Avi Kivity1c11b372012-04-09 18:39:59 +0300589/*
590 * x86 defines three classes of vector instructions: explicitly
591 * aligned, explicitly unaligned, and the rest, which change behaviour
592 * depending on whether they're AVX encoded or not.
593 *
594 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
595 * subject to the same check.
596 */
597static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
598{
599 if (likely(size < 16))
600 return false;
601
602 if (ctxt->d & Aligned)
603 return true;
604 else if (ctxt->d & Unaligned)
605 return false;
606 else if (ctxt->d & Avx)
607 return false;
608 else
609 return true;
610}
611
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400612static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300613 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400614 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300615 ulong *linear)
616{
Avi Kivity618ff152011-04-03 12:32:09 +0300617 struct desc_struct desc;
618 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300619 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300620 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300621 u16 sel;
Gleb Natapov3a78a4f2012-12-20 16:57:42 +0200622 unsigned cpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300623
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900624 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300625 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300626 case X86EMUL_MODE_PROT64:
627 if (((signed long)la << 16) >> 16 != la)
628 return emulate_gp(ctxt, 0);
629 break;
630 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300631 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
632 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300633 if (!usable)
634 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200635 /* code segment in protected mode or read-only data segment */
636 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
637 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300638 goto bad;
639 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400640 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300641 goto bad;
642 lim = desc_limit_scaled(&desc);
643 if ((desc.type & 8) || !(desc.type & 4)) {
644 /* expand-up segment */
645 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
646 goto bad;
647 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800648 /* expand-down segment */
Avi Kivity618ff152011-04-03 12:32:09 +0300649 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
650 goto bad;
651 lim = desc.d ? 0xffffffff : 0xffff;
652 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
653 goto bad;
654 }
Avi Kivity717746e2011-04-20 13:37:53 +0300655 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity618ff152011-04-03 12:32:09 +0300656 if (!(desc.type & 8)) {
657 /* data segment */
658 if (cpl > desc.dpl)
659 goto bad;
660 } else if ((desc.type & 8) && !(desc.type & 4)) {
661 /* nonconforming code segment */
662 if (cpl != desc.dpl)
663 goto bad;
664 } else if ((desc.type & 8) && (desc.type & 4)) {
665 /* conforming code segment */
666 if (cpl < desc.dpl)
667 goto bad;
668 }
669 break;
670 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300671 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300672 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300673 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
674 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300675 *linear = la;
676 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300677bad:
678 if (addr.seg == VCPU_SREG_SS)
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300679 return emulate_ss(ctxt, sel);
Avi Kivity618ff152011-04-03 12:32:09 +0300680 else
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300681 return emulate_gp(ctxt, sel);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300682}
683
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400684static int linearize(struct x86_emulate_ctxt *ctxt,
685 struct segmented_address addr,
686 unsigned size, bool write,
687 ulong *linear)
688{
689 return __linearize(ctxt, addr, size, write, false, linear);
690}
691
692
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200693static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
694 struct segmented_address addr,
695 void *data,
696 unsigned size)
697{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200698 int rc;
699 ulong linear;
700
Avi Kivity83b87952011-04-03 11:31:19 +0300701 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200702 if (rc != X86EMUL_CONTINUE)
703 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300704 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200705}
706
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900707/*
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200708 * Prefetch the remaining bytes of the instruction without crossing page
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900709 * boundary if they are not in fetch_cache yet.
710 */
Paolo Bonzini9506d572014-05-06 13:05:25 +0200711static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
Avi Kivity62266862007-11-20 13:15:52 +0200712{
Avi Kivity62266862007-11-20 13:15:52 +0200713 int rc;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200714 int size;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200715 unsigned long linear;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200716 int cur_size = ctxt->fetch.end - ctxt->fetch.data;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200717 struct segmented_address addr = { .seg = VCPU_SREG_CS,
Paolo Bonzini17052f12014-05-06 16:33:01 +0200718 .ea = ctxt->eip + cur_size };
719
720 size = min(15UL ^ cur_size,
721 PAGE_SIZE - offset_in_page(addr.ea));
Paolo Bonzini5cfc7e02014-05-06 13:05:25 +0200722
723 /*
724 * One instruction can only straddle two pages,
725 * and one has been loaded at the beginning of
726 * x86_decode_insn. So, if not enough bytes
727 * still, we must have hit the 15-byte boundary.
728 */
729 if (unlikely(size < op_size))
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200730 return X86EMUL_UNHANDLEABLE;
731 rc = __linearize(ctxt, addr, size, false, true, &linear);
732 if (unlikely(rc != X86EMUL_CONTINUE))
733 return rc;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200734 rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end,
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200735 size, &ctxt->exception);
736 if (unlikely(rc != X86EMUL_CONTINUE))
737 return rc;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200738 ctxt->fetch.end += size;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900739 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200740}
741
Paolo Bonzini9506d572014-05-06 13:05:25 +0200742static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt,
743 unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200744{
Paolo Bonzini17052f12014-05-06 16:33:01 +0200745 if (unlikely(ctxt->fetch.end - ctxt->fetch.ptr < size))
Paolo Bonzini9506d572014-05-06 13:05:25 +0200746 return __do_insn_fetch_bytes(ctxt, size);
747 else
748 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200749}
750
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900751/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900752#define insn_fetch(_type, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200753({ _type _x; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200754 \
755 rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900756 if (rc != X86EMUL_CONTINUE) \
757 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200758 ctxt->_eip += sizeof(_type); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200759 _x = *(_type __aligned(1) *) ctxt->fetch.ptr; \
760 ctxt->fetch.ptr += sizeof(_type); \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200761 _x; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900762})
763
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900764#define insn_fetch_arr(_arr, _size, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200765({ \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200766 rc = do_insn_fetch_bytes(_ctxt, _size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900767 if (rc != X86EMUL_CONTINUE) \
768 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200769 ctxt->_eip += (_size); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200770 memcpy(_arr, ctxt->fetch.ptr, _size); \
771 ctxt->fetch.ptr += (_size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900772})
773
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000774/*
775 * Given the 'reg' portion of a ModRM byte, and a register block, return a
776 * pointer into the block that addresses the relevant register.
777 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
778 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300779static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200780 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800781{
782 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200783 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800784
Avi Kivity6aa8b732006-12-10 02:21:36 -0800785 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300786 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
787 else
788 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789 return p;
790}
791
792static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200793 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800794 u16 *size, unsigned long *address, int op_bytes)
795{
796 int rc;
797
798 if (op_bytes == 2)
799 op_bytes = 3;
800 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200801 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900802 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800803 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200804 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200805 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800806 return rc;
807}
808
Avi Kivity34b77652013-01-19 19:51:56 +0200809FASTOP2(add);
810FASTOP2(or);
811FASTOP2(adc);
812FASTOP2(sbb);
813FASTOP2(and);
814FASTOP2(sub);
815FASTOP2(xor);
816FASTOP2(cmp);
817FASTOP2(test);
818
Avi Kivityb9fa4092013-02-09 11:31:48 +0200819FASTOP1SRC2(mul, mul_ex);
820FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200821FASTOP1SRC2EX(div, div_ex);
822FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +0200823
Avi Kivity34b77652013-01-19 19:51:56 +0200824FASTOP3WCL(shld);
825FASTOP3WCL(shrd);
826
827FASTOP2W(imul);
828
829FASTOP1(not);
830FASTOP1(neg);
831FASTOP1(inc);
832FASTOP1(dec);
833
834FASTOP2CL(rol);
835FASTOP2CL(ror);
836FASTOP2CL(rcl);
837FASTOP2CL(rcr);
838FASTOP2CL(shl);
839FASTOP2CL(shr);
840FASTOP2CL(sar);
841
842FASTOP2W(bsf);
843FASTOP2W(bsr);
844FASTOP2W(bt);
845FASTOP2W(bts);
846FASTOP2W(btr);
847FASTOP2W(btc);
848
Avi Kivitye47a5f52013-02-09 11:31:51 +0200849FASTOP2(xadd);
850
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200851static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300852{
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200853 u8 rc;
854 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300855
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200856 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +0200857 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200858 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
859 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300860}
861
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300862static void fetch_register_operand(struct operand *op)
863{
864 switch (op->bytes) {
865 case 1:
866 op->val = *(u8 *)op->addr.reg;
867 break;
868 case 2:
869 op->val = *(u16 *)op->addr.reg;
870 break;
871 case 4:
872 op->val = *(u32 *)op->addr.reg;
873 break;
874 case 8:
875 op->val = *(u64 *)op->addr.reg;
876 break;
877 }
878}
879
Avi Kivity1253791d2011-03-29 11:41:27 +0200880static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
881{
882 ctxt->ops->get_fpu(ctxt);
883 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200884 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
885 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
886 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
887 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
888 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
889 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
890 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
891 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200892#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200893 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
894 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
895 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
896 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
897 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
898 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
899 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
900 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200901#endif
902 default: BUG();
903 }
904 ctxt->ops->put_fpu(ctxt);
905}
906
907static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
908 int reg)
909{
910 ctxt->ops->get_fpu(ctxt);
911 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200912 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
913 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
914 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
915 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
916 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
917 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
918 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
919 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200920#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200921 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
922 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
923 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
924 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
925 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
926 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
927 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
928 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200929#endif
930 default: BUG();
931 }
932 ctxt->ops->put_fpu(ctxt);
933}
934
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300935static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
936{
937 ctxt->ops->get_fpu(ctxt);
938 switch (reg) {
939 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
940 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
941 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
942 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
943 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
944 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
945 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
946 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
947 default: BUG();
948 }
949 ctxt->ops->put_fpu(ctxt);
950}
951
952static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
953{
954 ctxt->ops->get_fpu(ctxt);
955 switch (reg) {
956 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
957 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
958 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
959 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
960 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
961 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
962 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
963 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
964 default: BUG();
965 }
966 ctxt->ops->put_fpu(ctxt);
967}
968
Gleb Natapov045a2822012-12-20 16:57:43 +0200969static int em_fninit(struct x86_emulate_ctxt *ctxt)
970{
971 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
972 return emulate_nm(ctxt);
973
974 ctxt->ops->get_fpu(ctxt);
975 asm volatile("fninit");
976 ctxt->ops->put_fpu(ctxt);
977 return X86EMUL_CONTINUE;
978}
979
980static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
981{
982 u16 fcw;
983
984 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
985 return emulate_nm(ctxt);
986
987 ctxt->ops->get_fpu(ctxt);
988 asm volatile("fnstcw %0": "+m"(fcw));
989 ctxt->ops->put_fpu(ctxt);
990
991 /* force 2 byte destination */
992 ctxt->dst.bytes = 2;
993 ctxt->dst.val = fcw;
994
995 return X86EMUL_CONTINUE;
996}
997
998static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
999{
1000 u16 fsw;
1001
1002 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1003 return emulate_nm(ctxt);
1004
1005 ctxt->ops->get_fpu(ctxt);
1006 asm volatile("fnstsw %0": "+m"(fsw));
1007 ctxt->ops->put_fpu(ctxt);
1008
1009 /* force 2 byte destination */
1010 ctxt->dst.bytes = 2;
1011 ctxt->dst.val = fsw;
1012
1013 return X86EMUL_CONTINUE;
1014}
1015
Avi Kivity1253791d2011-03-29 11:41:27 +02001016static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001017 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001018{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001019 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001020
Avi Kivity9dac77f2011-06-01 15:34:25 +03001021 if (!(ctxt->d & ModRM))
1022 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity1253791d2011-03-29 11:41:27 +02001023
Avi Kivity9dac77f2011-06-01 15:34:25 +03001024 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001025 op->type = OP_XMM;
1026 op->bytes = 16;
1027 op->addr.xmm = reg;
1028 read_sse_reg(ctxt, &op->vec_val, reg);
1029 return;
1030 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001031 if (ctxt->d & Mmx) {
1032 reg &= 7;
1033 op->type = OP_MM;
1034 op->bytes = 8;
1035 op->addr.mm = reg;
1036 return;
1037 }
Avi Kivity1253791d2011-03-29 11:41:27 +02001038
Avi Kivity3c118e22007-10-31 10:27:04 +02001039 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001040 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1041 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1042
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001043 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001044 op->orig_val = op->val;
1045}
1046
Avi Kivitya6e34072012-06-10 17:15:39 +03001047static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1048{
1049 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1050 ctxt->modrm_seg = VCPU_SREG_SS;
1051}
1052
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001053static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001054 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001055{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001056 u8 sib;
Bandan Das02357bd2014-04-16 12:46:11 -04001057 int index_reg, base_reg, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001058 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001059 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001060
Bandan Das02357bd2014-04-16 12:46:11 -04001061 ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */
1062 index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */
1063 base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001064
Bandan Das02357bd2014-04-16 12:46:11 -04001065 ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001066 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
Bandan Das02357bd2014-04-16 12:46:11 -04001067 ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001068 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001069
Nadav Amit9b88ae92014-05-25 23:05:21 +03001070 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001071 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001072 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001073 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001074 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001075 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001076 op->type = OP_XMM;
1077 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001078 op->addr.xmm = ctxt->modrm_rm;
1079 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001080 return rc;
1081 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001082 if (ctxt->d & Mmx) {
1083 op->type = OP_MM;
1084 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001085 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001086 return rc;
1087 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001088 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001089 return rc;
1090 }
1091
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001092 op->type = OP_MEM;
1093
Avi Kivity9dac77f2011-06-01 15:34:25 +03001094 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001095 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1096 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1097 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1098 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001099
1100 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001101 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001102 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001103 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001104 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001105 break;
1106 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001107 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001108 break;
1109 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001110 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001111 break;
1112 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001113 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001114 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001115 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001116 break;
1117 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001118 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001119 break;
1120 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001121 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001122 break;
1123 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001124 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001125 break;
1126 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001127 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001128 break;
1129 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001130 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001131 break;
1132 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001133 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001134 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001135 break;
1136 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001137 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001138 break;
1139 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001140 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1141 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1142 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001143 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001144 } else {
1145 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001146 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001147 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001148 index_reg |= (sib >> 3) & 7;
1149 base_reg |= sib & 7;
1150 scale = sib >> 6;
1151
Avi Kivity9dac77f2011-06-01 15:34:25 +03001152 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001153 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001154 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001155 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001156 adjust_modrm_seg(ctxt, base_reg);
1157 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001158 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001159 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001160 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001161 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001162 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001163 } else {
1164 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001165 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001166 adjust_modrm_seg(ctxt, base_reg);
1167 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001168 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001169 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001170 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001171 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001172 break;
1173 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001174 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001175 break;
1176 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001177 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001178 break;
1179 }
1180 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001181 op->addr.mem.ea = modrm_ea;
Bandan Das41061cd2014-04-16 12:46:14 -04001182 if (ctxt->ad_bytes != 8)
1183 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
1184
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001185done:
1186 return rc;
1187}
1188
1189static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001190 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001191{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001192 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001193
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001194 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001195 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001196 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001197 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001198 break;
1199 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001200 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001201 break;
1202 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001203 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001204 break;
1205 }
1206done:
1207 return rc;
1208}
1209
Avi Kivity9dac77f2011-06-01 15:34:25 +03001210static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001211{
Sheng Yang7129eec2010-09-28 16:33:32 +08001212 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001213
Avi Kivity9dac77f2011-06-01 15:34:25 +03001214 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001215 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001216
Avi Kivity9dac77f2011-06-01 15:34:25 +03001217 if (ctxt->src.bytes == 2)
1218 sv = (s16)ctxt->src.val & (s16)mask;
1219 else if (ctxt->src.bytes == 4)
1220 sv = (s32)ctxt->src.val & (s32)mask;
Nadav Amit7dec5602014-06-15 16:12:57 +03001221 else
1222 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001223
Avi Kivity9dac77f2011-06-01 15:34:25 +03001224 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001225 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001226
1227 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001228 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001229}
1230
Gleb Natapov9de41572010-04-28 19:15:22 +03001231static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001232 unsigned long addr, void *dest, unsigned size)
1233{
1234 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001235 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001236
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001237 if (mc->pos < mc->end)
1238 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001239
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001240 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001241
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001242 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1243 &ctxt->exception);
1244 if (rc != X86EMUL_CONTINUE)
1245 return rc;
1246
1247 mc->end += size;
1248
1249read_cached:
1250 memcpy(dest, mc->data + mc->pos, size);
1251 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001252 return X86EMUL_CONTINUE;
1253}
1254
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001255static int segmented_read(struct x86_emulate_ctxt *ctxt,
1256 struct segmented_address addr,
1257 void *data,
1258 unsigned size)
1259{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001260 int rc;
1261 ulong linear;
1262
Avi Kivity83b87952011-04-03 11:31:19 +03001263 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001264 if (rc != X86EMUL_CONTINUE)
1265 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001266 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001267}
1268
1269static int segmented_write(struct x86_emulate_ctxt *ctxt,
1270 struct segmented_address addr,
1271 const void *data,
1272 unsigned size)
1273{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001274 int rc;
1275 ulong linear;
1276
Avi Kivity83b87952011-04-03 11:31:19 +03001277 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001278 if (rc != X86EMUL_CONTINUE)
1279 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001280 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1281 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001282}
1283
1284static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1285 struct segmented_address addr,
1286 const void *orig_data, const void *data,
1287 unsigned size)
1288{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001289 int rc;
1290 ulong linear;
1291
Avi Kivity83b87952011-04-03 11:31:19 +03001292 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001293 if (rc != X86EMUL_CONTINUE)
1294 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001295 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1296 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001297}
1298
Gleb Natapov7b262e92010-03-18 15:20:27 +02001299static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001300 unsigned int size, unsigned short port,
1301 void *dest)
1302{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001303 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001304
1305 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001306 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001307 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001308 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001309 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001310 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1311 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Gleb Natapov7b262e92010-03-18 15:20:27 +02001312 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1313 count);
1314 if (n == 0)
1315 n = 1;
1316 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001317 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001318 return 0;
1319 rc->end = n * size;
1320 }
1321
Nadav Amite6e39f02014-04-18 03:35:10 +03001322 if (ctxt->rep_prefix && (ctxt->d & String) &&
1323 !(ctxt->eflags & EFLG_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001324 ctxt->dst.data = rc->data + rc->pos;
1325 ctxt->dst.type = OP_MEM_STR;
1326 ctxt->dst.count = (rc->end - rc->pos) / size;
1327 rc->pos = rc->end;
1328 } else {
1329 memcpy(dest, rc->data + rc->pos, size);
1330 rc->pos += size;
1331 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001332 return 1;
1333}
1334
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001335static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1336 u16 index, struct desc_struct *desc)
1337{
1338 struct desc_ptr dt;
1339 ulong addr;
1340
1341 ctxt->ops->get_idt(ctxt, &dt);
1342
1343 if (dt.size < index * 8 + 7)
1344 return emulate_gp(ctxt, index << 3 | 0x2);
1345
1346 addr = dt.address + index * 8;
1347 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1348 &ctxt->exception);
1349}
1350
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001351static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001352 u16 selector, struct desc_ptr *dt)
1353{
Mathias Krause0225fb52012-08-30 01:30:16 +02001354 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001355 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001356
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001357 if (selector & 1 << 2) {
1358 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001359 u16 sel;
1360
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001361 memset (dt, 0, sizeof *dt);
Nadav Amit2eedcac2014-06-02 18:34:05 +03001362 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1363 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001364 return;
1365
1366 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001367 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001368 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001369 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001370}
1371
1372/* allowed just for 8 bytes segments */
1373static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001374 u16 selector, struct desc_struct *desc,
1375 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001376{
1377 struct desc_ptr dt;
1378 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001379 ulong addr;
1380
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001381 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001382
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001383 if (dt.size < index * 8 + 7)
1384 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001385
Avi Kivitye9194642012-06-13 16:29:39 +03001386 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001387 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1388 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001389}
1390
1391/* allowed just for 8 bytes segments */
1392static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001393 u16 selector, struct desc_struct *desc)
1394{
1395 struct desc_ptr dt;
1396 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001397 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001398
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001399 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001400
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001401 if (dt.size < index * 8 + 7)
1402 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001403
1404 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001405 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1406 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001407}
1408
Gleb Natapov5601d052011-03-07 14:55:06 +02001409/* Does not support long mode */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001410static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Paolo Bonzini5045b462014-05-15 18:09:29 +02001411 u16 selector, int seg, u8 cpl, bool in_task_switch)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001412{
Avi Kivity869be992012-06-13 16:30:53 +03001413 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001414 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001415 unsigned err_vec = GP_VECTOR;
1416 u32 err_code = 0;
1417 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001418 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001419 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001420 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001421 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001422
1423 memset(&seg_desc, 0, sizeof seg_desc);
1424
Kevin Wolff8da94e2013-04-11 14:06:03 +02001425 if (ctxt->mode == X86EMUL_MODE_REAL) {
1426 /* set real mode segment descriptor (keep limit etc. for
1427 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001428 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001429 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001430 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001431 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1432 /* VM86 needs a clean new segment descriptor */
1433 set_desc_base(&seg_desc, selector << 4);
1434 set_desc_limit(&seg_desc, 0xffff);
1435 seg_desc.type = 3;
1436 seg_desc.p = 1;
1437 seg_desc.s = 1;
1438 seg_desc.dpl = 3;
1439 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001440 }
1441
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001442 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001443
1444 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1445 if ((seg == VCPU_SREG_CS
1446 || (seg == VCPU_SREG_SS
1447 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1448 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001449 && null_selector)
1450 goto exception;
1451
1452 /* TR should be in GDT only */
1453 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1454 goto exception;
1455
1456 if (null_selector) /* for NULL selector skip all following checks */
1457 goto load;
1458
Avi Kivitye9194642012-06-13 16:29:39 +03001459 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001460 if (ret != X86EMUL_CONTINUE)
1461 return ret;
1462
1463 err_code = selector & 0xfffc;
1464 err_vec = GP_VECTOR;
1465
Guo Chaofc058682012-06-28 15:19:51 +08001466 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001467 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1468 goto exception;
1469
1470 if (!seg_desc.p) {
1471 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1472 goto exception;
1473 }
1474
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001475 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001476
1477 switch (seg) {
1478 case VCPU_SREG_SS:
1479 /*
1480 * segment is not a writable data segment or segment
1481 * selector's RPL != CPL or segment selector's RPL != CPL
1482 */
1483 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1484 goto exception;
1485 break;
1486 case VCPU_SREG_CS:
Paolo Bonzini5045b462014-05-15 18:09:29 +02001487 if (in_task_switch && rpl != dpl)
1488 goto exception;
1489
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001490 if (!(seg_desc.type & 8))
1491 goto exception;
1492
1493 if (seg_desc.type & 4) {
1494 /* conforming */
1495 if (dpl > cpl)
1496 goto exception;
1497 } else {
1498 /* nonconforming */
1499 if (rpl > cpl || dpl != cpl)
1500 goto exception;
1501 }
1502 /* CS(RPL) <- CPL */
1503 selector = (selector & 0xfffc) | cpl;
1504 break;
1505 case VCPU_SREG_TR:
1506 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1507 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001508 old_desc = seg_desc;
1509 seg_desc.type |= 2; /* busy */
1510 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1511 sizeof(seg_desc), &ctxt->exception);
1512 if (ret != X86EMUL_CONTINUE)
1513 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001514 break;
1515 case VCPU_SREG_LDTR:
1516 if (seg_desc.s || seg_desc.type != 2)
1517 goto exception;
1518 break;
1519 default: /* DS, ES, FS, or GS */
1520 /*
1521 * segment is not a data or readable code segment or
1522 * ((segment is a data or nonconforming code segment)
1523 * and (both RPL and CPL > DPL))
1524 */
1525 if ((seg_desc.type & 0xa) == 0x8 ||
1526 (((seg_desc.type & 0xc) != 0xc) &&
1527 (rpl > dpl && cpl > dpl)))
1528 goto exception;
1529 break;
1530 }
1531
1532 if (seg_desc.s) {
1533 /* mark segment as accessed */
1534 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001535 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001536 if (ret != X86EMUL_CONTINUE)
1537 return ret;
Nadav Amite37a75a2014-06-02 18:34:04 +03001538 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
1539 ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3,
1540 sizeof(base3), &ctxt->exception);
1541 if (ret != X86EMUL_CONTINUE)
1542 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001543 }
1544load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001545 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001546 return X86EMUL_CONTINUE;
1547exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001548 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001549 return X86EMUL_PROPAGATE_FAULT;
1550}
1551
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001552static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1553 u16 selector, int seg)
1554{
1555 u8 cpl = ctxt->ops->cpl(ctxt);
Paolo Bonzini5045b462014-05-15 18:09:29 +02001556 return __load_segment_descriptor(ctxt, selector, seg, cpl, false);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001557}
1558
Wei Yongjun31be40b2010-08-17 09:17:30 +08001559static void write_register_operand(struct operand *op)
1560{
1561 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1562 switch (op->bytes) {
1563 case 1:
1564 *(u8 *)op->addr.reg = (u8)op->val;
1565 break;
1566 case 2:
1567 *(u16 *)op->addr.reg = (u16)op->val;
1568 break;
1569 case 4:
1570 *op->addr.reg = (u32)op->val;
1571 break; /* 64b: zero-extend */
1572 case 8:
1573 *op->addr.reg = op->val;
1574 break;
1575 }
1576}
1577
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001578static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001579{
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001580 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001581 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001582 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001583 break;
1584 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001585 if (ctxt->lock_prefix)
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001586 return segmented_cmpxchg(ctxt,
1587 op->addr.mem,
1588 &op->orig_val,
1589 &op->val,
1590 op->bytes);
1591 else
1592 return segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001593 op->addr.mem,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001594 &op->val,
1595 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001596 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001597 case OP_MEM_STR:
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001598 return segmented_write(ctxt,
1599 op->addr.mem,
1600 op->data,
1601 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001602 break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001603 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001604 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001605 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001606 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001607 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001608 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001609 case OP_NONE:
1610 /* no writeback */
1611 break;
1612 default:
1613 break;
1614 }
1615 return X86EMUL_CONTINUE;
1616}
1617
Avi Kivity51ddff52012-06-12 20:19:40 +03001618static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001619{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001620 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001621
Avi Kivity5ad105e2012-08-19 14:34:31 +03001622 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001623 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001624 addr.seg = VCPU_SREG_SS;
1625
Avi Kivity51ddff52012-06-12 20:19:40 +03001626 return segmented_write(ctxt, addr, data, bytes);
1627}
1628
1629static int em_push(struct x86_emulate_ctxt *ctxt)
1630{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001631 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001632 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001633 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001634}
1635
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001636static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001637 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001638{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001639 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001640 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001641
Avi Kivitydd856ef2012-08-27 23:46:17 +03001642 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001643 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001644 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001645 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001646 return rc;
1647
Avi Kivity5ad105e2012-08-19 14:34:31 +03001648 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001649 return rc;
1650}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001651
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001652static int em_pop(struct x86_emulate_ctxt *ctxt)
1653{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001654 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001655}
1656
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001657static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001658 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001659{
1660 int rc;
1661 unsigned long val, change_mask;
1662 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001663 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001664
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001665 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001666 if (rc != X86EMUL_CONTINUE)
1667 return rc;
1668
1669 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1670 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1671
1672 switch(ctxt->mode) {
1673 case X86EMUL_MODE_PROT64:
1674 case X86EMUL_MODE_PROT32:
1675 case X86EMUL_MODE_PROT16:
1676 if (cpl == 0)
1677 change_mask |= EFLG_IOPL;
1678 if (cpl <= iopl)
1679 change_mask |= EFLG_IF;
1680 break;
1681 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001682 if (iopl < 3)
1683 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001684 change_mask |= EFLG_IF;
1685 break;
1686 default: /* real mode */
1687 change_mask |= (EFLG_IOPL | EFLG_IF);
1688 break;
1689 }
1690
1691 *(unsigned long *)dest =
1692 (ctxt->eflags & ~change_mask) | (val & change_mask);
1693
1694 return rc;
1695}
1696
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001697static int em_popf(struct x86_emulate_ctxt *ctxt)
1698{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001699 ctxt->dst.type = OP_REG;
1700 ctxt->dst.addr.reg = &ctxt->eflags;
1701 ctxt->dst.bytes = ctxt->op_bytes;
1702 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001703}
1704
Avi Kivity612e89f2012-06-12 20:03:23 +03001705static int em_enter(struct x86_emulate_ctxt *ctxt)
1706{
1707 int rc;
1708 unsigned frame_size = ctxt->src.val;
1709 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001710 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001711
1712 if (nesting_level)
1713 return X86EMUL_UNHANDLEABLE;
1714
Avi Kivitydd856ef2012-08-27 23:46:17 +03001715 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1716 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001717 if (rc != X86EMUL_CONTINUE)
1718 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001719 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001720 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001721 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1722 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001723 stack_mask(ctxt));
1724 return X86EMUL_CONTINUE;
1725}
1726
Avi Kivityf47cfa32012-06-07 17:49:24 +03001727static int em_leave(struct x86_emulate_ctxt *ctxt)
1728{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001729 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001730 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001731 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001732}
1733
Avi Kivity1cd196e2011-09-13 10:45:51 +03001734static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001735{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001736 int seg = ctxt->src2.val;
1737
Avi Kivity9dac77f2011-06-01 15:34:25 +03001738 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001739
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001740 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001741}
1742
Avi Kivity1cd196e2011-09-13 10:45:51 +03001743static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001744{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001745 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001746 unsigned long selector;
1747 int rc;
1748
Avi Kivity9dac77f2011-06-01 15:34:25 +03001749 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001750 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001751 return rc;
1752
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001753 if (ctxt->modrm_reg == VCPU_SREG_SS)
1754 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
1755
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001756 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001757 return rc;
1758}
1759
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001760static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001761{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001762 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001763 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001764 int reg = VCPU_REGS_RAX;
1765
1766 while (reg <= VCPU_REGS_RDI) {
1767 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001768 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001769
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001770 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001771 if (rc != X86EMUL_CONTINUE)
1772 return rc;
1773
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001774 ++reg;
1775 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001776
Wei Yongjunc37eda12010-06-15 09:03:33 +08001777 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001778}
1779
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001780static int em_pushf(struct x86_emulate_ctxt *ctxt)
1781{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001782 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001783 return em_push(ctxt);
1784}
1785
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001786static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001787{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001788 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001789 int reg = VCPU_REGS_RDI;
1790
1791 while (reg >= VCPU_REGS_RAX) {
1792 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001793 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001794 --reg;
1795 }
1796
Avi Kivitydd856ef2012-08-27 23:46:17 +03001797 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001798 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001799 break;
1800 --reg;
1801 }
1802 return rc;
1803}
1804
Avi Kivitydd856ef2012-08-27 23:46:17 +03001805static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001806{
Mathias Krause0225fb52012-08-30 01:30:16 +02001807 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001808 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001809 struct desc_ptr dt;
1810 gva_t cs_addr;
1811 gva_t eip_addr;
1812 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001813
1814 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001815 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001816 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001817 if (rc != X86EMUL_CONTINUE)
1818 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001819
1820 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1821
Avi Kivity9dac77f2011-06-01 15:34:25 +03001822 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001823 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001824 if (rc != X86EMUL_CONTINUE)
1825 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001826
Avi Kivity9dac77f2011-06-01 15:34:25 +03001827 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001828 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001829 if (rc != X86EMUL_CONTINUE)
1830 return rc;
1831
Avi Kivity4bff1e862011-04-20 13:37:53 +03001832 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001833
1834 eip_addr = dt.address + (irq << 2);
1835 cs_addr = dt.address + (irq << 2) + 2;
1836
Avi Kivity0f65dd72011-04-20 13:37:53 +03001837 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001838 if (rc != X86EMUL_CONTINUE)
1839 return rc;
1840
Avi Kivity0f65dd72011-04-20 13:37:53 +03001841 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001842 if (rc != X86EMUL_CONTINUE)
1843 return rc;
1844
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001845 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001846 if (rc != X86EMUL_CONTINUE)
1847 return rc;
1848
Avi Kivity9dac77f2011-06-01 15:34:25 +03001849 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001850
1851 return rc;
1852}
1853
Avi Kivitydd856ef2012-08-27 23:46:17 +03001854int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1855{
1856 int rc;
1857
1858 invalidate_registers(ctxt);
1859 rc = __emulate_int_real(ctxt, irq);
1860 if (rc == X86EMUL_CONTINUE)
1861 writeback_registers(ctxt);
1862 return rc;
1863}
1864
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001865static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001866{
1867 switch(ctxt->mode) {
1868 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001869 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001870 case X86EMUL_MODE_VM86:
1871 case X86EMUL_MODE_PROT16:
1872 case X86EMUL_MODE_PROT32:
1873 case X86EMUL_MODE_PROT64:
1874 default:
1875 /* Protected mode interrupts unimplemented yet */
1876 return X86EMUL_UNHANDLEABLE;
1877 }
1878}
1879
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001880static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001881{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001882 int rc = X86EMUL_CONTINUE;
1883 unsigned long temp_eip = 0;
1884 unsigned long temp_eflags = 0;
1885 unsigned long cs = 0;
1886 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1887 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1888 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1889 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1890
1891 /* TODO: Add stack limit check */
1892
Avi Kivity9dac77f2011-06-01 15:34:25 +03001893 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001894
1895 if (rc != X86EMUL_CONTINUE)
1896 return rc;
1897
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001898 if (temp_eip & ~0xffff)
1899 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001900
Avi Kivity9dac77f2011-06-01 15:34:25 +03001901 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001902
1903 if (rc != X86EMUL_CONTINUE)
1904 return rc;
1905
Avi Kivity9dac77f2011-06-01 15:34:25 +03001906 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001907
1908 if (rc != X86EMUL_CONTINUE)
1909 return rc;
1910
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001911 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001912
1913 if (rc != X86EMUL_CONTINUE)
1914 return rc;
1915
Avi Kivity9dac77f2011-06-01 15:34:25 +03001916 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001917
1918
Avi Kivity9dac77f2011-06-01 15:34:25 +03001919 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001920 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001921 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001922 ctxt->eflags &= ~0xffff;
1923 ctxt->eflags |= temp_eflags;
1924 }
1925
1926 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1927 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1928
1929 return rc;
1930}
1931
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001932static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001933{
1934 switch(ctxt->mode) {
1935 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001936 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001937 case X86EMUL_MODE_VM86:
1938 case X86EMUL_MODE_PROT16:
1939 case X86EMUL_MODE_PROT32:
1940 case X86EMUL_MODE_PROT64:
1941 default:
1942 /* iret from protected mode unimplemented yet */
1943 return X86EMUL_UNHANDLEABLE;
1944 }
1945}
1946
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001947static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1948{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001949 int rc;
1950 unsigned short sel;
1951
Avi Kivity9dac77f2011-06-01 15:34:25 +03001952 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001953
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001954 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001955 if (rc != X86EMUL_CONTINUE)
1956 return rc;
1957
Avi Kivity9dac77f2011-06-01 15:34:25 +03001958 ctxt->_eip = 0;
1959 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001960 return X86EMUL_CONTINUE;
1961}
1962
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001963static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001964{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001965 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001966
Avi Kivity9dac77f2011-06-01 15:34:25 +03001967 switch (ctxt->modrm_reg) {
Mohammed Gamald19292e2008-09-08 21:47:19 +03001968 case 2: /* call near abs */ {
1969 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001970 old_eip = ctxt->_eip;
1971 ctxt->_eip = ctxt->src.val;
1972 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001973 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001974 break;
1975 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001976 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001977 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001978 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001979 case 5: /* jmp far */
1980 rc = em_jmp_far(ctxt);
1981 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001982 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001983 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001984 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001985 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001986 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001987}
1988
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09001989static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001990{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001991 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001992
Nadav Amitaaa05f22014-06-02 18:34:10 +03001993 if (ctxt->dst.bytes == 16)
1994 return X86EMUL_UNHANDLEABLE;
1995
Avi Kivitydd856ef2012-08-27 23:46:17 +03001996 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
1997 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
1998 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
1999 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002000 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002001 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002002 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2003 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002004
Laurent Vivier05f086f2007-09-24 11:10:55 +02002005 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002006 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002007 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002008}
2009
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002010static int em_ret(struct x86_emulate_ctxt *ctxt)
2011{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002012 ctxt->dst.type = OP_REG;
2013 ctxt->dst.addr.reg = &ctxt->_eip;
2014 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002015 return em_pop(ctxt);
2016}
2017
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002018static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002019{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002020 int rc;
2021 unsigned long cs;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002022 int cpl = ctxt->ops->cpl(ctxt);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002023
Avi Kivity9dac77f2011-06-01 15:34:25 +03002024 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002025 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002026 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002027 if (ctxt->op_bytes == 4)
2028 ctxt->_eip = (u32)ctxt->_eip;
2029 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002030 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002031 return rc;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002032 /* Outer-privilege level return is not implemented */
2033 if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2034 return X86EMUL_UNHANDLEABLE;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002035 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002036 return rc;
2037}
2038
Bruce Rogers32611072013-09-09 09:40:20 -06002039static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2040{
2041 int rc;
2042
2043 rc = em_ret_far(ctxt);
2044 if (rc != X86EMUL_CONTINUE)
2045 return rc;
2046 rsp_increment(ctxt, ctxt->src.val);
2047 return X86EMUL_CONTINUE;
2048}
2049
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002050static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2051{
2052 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002053 ctxt->dst.orig_val = ctxt->dst.val;
2054 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002055 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002056 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002057 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002058
2059 if (ctxt->eflags & EFLG_ZF) {
2060 /* Success: write back to memory. */
2061 ctxt->dst.val = ctxt->src.orig_val;
2062 } else {
2063 /* Failure: write the value we saw to EAX. */
2064 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002065 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Nadav Amit37c564f2014-06-02 18:34:07 +03002066 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002067 }
2068 return X86EMUL_CONTINUE;
2069}
2070
Avi Kivityd4b43252011-09-13 10:45:50 +03002071static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002072{
Avi Kivityd4b43252011-09-13 10:45:50 +03002073 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002074 unsigned short sel;
2075 int rc;
2076
Avi Kivity9dac77f2011-06-01 15:34:25 +03002077 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002078
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002079 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002080 if (rc != X86EMUL_CONTINUE)
2081 return rc;
2082
Avi Kivity9dac77f2011-06-01 15:34:25 +03002083 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002084 return rc;
2085}
2086
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002087static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002088setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002089 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002090{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002091 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002092 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002093 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002094 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002095 cs->type = 0x0b; /* Read, Execute, Accessed */
2096 cs->s = 1;
2097 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002098 cs->p = 1;
2099 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002100 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002101
Gleb Natapov79168fd2010-04-28 19:15:30 +03002102 set_desc_base(ss, 0); /* flat segment */
2103 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002104 ss->g = 1; /* 4kb granularity */
2105 ss->s = 1;
2106 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002107 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002108 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002109 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002110 ss->l = 0;
2111 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002112}
2113
Avi Kivity1a18a692012-02-01 12:23:21 +02002114static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2115{
2116 u32 eax, ebx, ecx, edx;
2117
2118 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002119 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2120 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002121 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2122 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2123}
2124
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002125static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2126{
Mathias Krause0225fb52012-08-30 01:30:16 +02002127 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002128 u32 eax, ebx, ecx, edx;
2129
2130 /*
2131 * syscall should always be enabled in longmode - so only become
2132 * vendor specific (cpuid) if other modes are active...
2133 */
2134 if (ctxt->mode == X86EMUL_MODE_PROT64)
2135 return true;
2136
2137 eax = 0x00000000;
2138 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002139 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2140 /*
2141 * Intel ("GenuineIntel")
2142 * remark: Intel CPUs only support "syscall" in 64bit
2143 * longmode. Also an 64bit guest with a
2144 * 32bit compat-app running will #UD !! While this
2145 * behaviour can be fixed (by emulating) into AMD
2146 * response - CPUs of AMD can't behave like Intel.
2147 */
2148 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2149 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2150 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2151 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002152
Avi Kivity0017f932012-06-07 14:10:16 +03002153 /* AMD ("AuthenticAMD") */
2154 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2155 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2156 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2157 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002158
Avi Kivity0017f932012-06-07 14:10:16 +03002159 /* AMD ("AMDisbetter!") */
2160 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2161 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2162 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2163 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002164
2165 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2166 return false;
2167}
2168
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002169static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002170{
Mathias Krause0225fb52012-08-30 01:30:16 +02002171 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002172 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002173 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002174 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002175 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002176
2177 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002178 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002179 ctxt->mode == X86EMUL_MODE_VM86)
2180 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002181
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002182 if (!(em_syscall_is_enabled(ctxt)))
2183 return emulate_ud(ctxt);
2184
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002185 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002186 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002187
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002188 if (!(efer & EFER_SCE))
2189 return emulate_ud(ctxt);
2190
Avi Kivity717746e2011-04-20 13:37:53 +03002191 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002192 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002193 cs_sel = (u16)(msr_data & 0xfffc);
2194 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002195
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002196 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002197 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002198 cs.l = 1;
2199 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002200 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2201 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002202
Avi Kivitydd856ef2012-08-27 23:46:17 +03002203 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002204 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002205#ifdef CONFIG_X86_64
Avi Kivitydd856ef2012-08-27 23:46:17 +03002206 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002207
Avi Kivity717746e2011-04-20 13:37:53 +03002208 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002209 ctxt->mode == X86EMUL_MODE_PROT64 ?
2210 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002211 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002212
Avi Kivity717746e2011-04-20 13:37:53 +03002213 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002214 ctxt->eflags &= ~(msr_data | EFLG_RF);
2215#endif
2216 } else {
2217 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002218 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002219 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002220
2221 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2222 }
2223
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002224 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002225}
2226
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002227static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002228{
Mathias Krause0225fb52012-08-30 01:30:16 +02002229 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002230 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002231 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002232 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002233 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002234
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002235 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002236 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002237 if (ctxt->mode == X86EMUL_MODE_REAL)
2238 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002239
Avi Kivity1a18a692012-02-01 12:23:21 +02002240 /*
2241 * Not recognized on AMD in compat mode (but is recognized in legacy
2242 * mode).
2243 */
2244 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2245 && !vendor_intel(ctxt))
2246 return emulate_ud(ctxt);
2247
Andre Przywara8c604352009-06-18 12:56:01 +02002248 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2249 * Therefore, we inject an #UD.
2250 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002251 if (ctxt->mode == X86EMUL_MODE_PROT64)
2252 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002253
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002254 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002255
Avi Kivity717746e2011-04-20 13:37:53 +03002256 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002257 switch (ctxt->mode) {
2258 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002259 if ((msr_data & 0xfffc) == 0x0)
2260 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002261 break;
2262 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002263 if (msr_data == 0x0)
2264 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002265 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002266 default:
2267 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002268 }
2269
2270 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002271 cs_sel = (u16)msr_data;
2272 cs_sel &= ~SELECTOR_RPL_MASK;
2273 ss_sel = cs_sel + 8;
2274 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002275 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002276 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002277 cs.l = 1;
2278 }
2279
Avi Kivity1aa36612011-04-27 13:20:30 +03002280 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2281 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002282
Avi Kivity717746e2011-04-20 13:37:53 +03002283 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002284 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002285
Avi Kivity717746e2011-04-20 13:37:53 +03002286 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002287 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002288
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002289 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002290}
2291
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002292static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002293{
Mathias Krause0225fb52012-08-30 01:30:16 +02002294 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002295 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002296 u64 msr_data;
2297 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002298 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002299
Gleb Natapova0044752010-02-10 14:21:31 +02002300 /* inject #GP if in real mode or Virtual 8086 mode */
2301 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002302 ctxt->mode == X86EMUL_MODE_VM86)
2303 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002304
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002305 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002306
Avi Kivity9dac77f2011-06-01 15:34:25 +03002307 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002308 usermode = X86EMUL_MODE_PROT64;
2309 else
2310 usermode = X86EMUL_MODE_PROT32;
2311
2312 cs.dpl = 3;
2313 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002314 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002315 switch (usermode) {
2316 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002317 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002318 if ((msr_data & 0xfffc) == 0x0)
2319 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002320 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002321 break;
2322 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002323 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002324 if (msr_data == 0x0)
2325 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002326 ss_sel = cs_sel + 8;
2327 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002328 cs.l = 1;
2329 break;
2330 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002331 cs_sel |= SELECTOR_RPL_MASK;
2332 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002333
Avi Kivity1aa36612011-04-27 13:20:30 +03002334 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2335 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002336
Avi Kivitydd856ef2012-08-27 23:46:17 +03002337 ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX);
2338 *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX);
Andre Przywara4668f052009-06-18 12:56:02 +02002339
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002340 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002341}
2342
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002343static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002344{
2345 int iopl;
2346 if (ctxt->mode == X86EMUL_MODE_REAL)
2347 return false;
2348 if (ctxt->mode == X86EMUL_MODE_VM86)
2349 return true;
2350 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002351 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002352}
2353
2354static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002355 u16 port, u16 len)
2356{
Mathias Krause0225fb52012-08-30 01:30:16 +02002357 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002358 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002359 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002360 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002361 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002362 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002363 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002364
Avi Kivity1aa36612011-04-27 13:20:30 +03002365 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002366 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002367 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002368 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002369 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002370 base = get_desc_base(&tr_seg);
2371#ifdef CONFIG_X86_64
2372 base |= ((u64)base3) << 32;
2373#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002374 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002375 if (r != X86EMUL_CONTINUE)
2376 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002377 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002378 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002379 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002380 if (r != X86EMUL_CONTINUE)
2381 return false;
2382 if ((perm >> bit_idx) & mask)
2383 return false;
2384 return true;
2385}
2386
2387static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002388 u16 port, u16 len)
2389{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002390 if (ctxt->perm_ok)
2391 return true;
2392
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002393 if (emulator_bad_iopl(ctxt))
2394 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002395 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002396
2397 ctxt->perm_ok = true;
2398
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002399 return true;
2400}
2401
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002402static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002403 struct tss_segment_16 *tss)
2404{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002405 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002406 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002407 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2408 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2409 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2410 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2411 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2412 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2413 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2414 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002415
Avi Kivity1aa36612011-04-27 13:20:30 +03002416 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2417 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2418 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2419 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2420 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002421}
2422
2423static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002424 struct tss_segment_16 *tss)
2425{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002426 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002427 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002428
Avi Kivity9dac77f2011-06-01 15:34:25 +03002429 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002430 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002431 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2432 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2433 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2434 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2435 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2436 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2437 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2438 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002439
2440 /*
2441 * SDM says that segment selectors are loaded before segment
2442 * descriptors
2443 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002444 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2445 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2446 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2447 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2448 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002449
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002450 cpl = tss->cs & 3;
2451
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002452 /*
Guo Chaofc058682012-06-28 15:19:51 +08002453 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002454 * it is handled in a context of new task
2455 */
Paolo Bonzini5045b462014-05-15 18:09:29 +02002456 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002457 if (ret != X86EMUL_CONTINUE)
2458 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002459 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002460 if (ret != X86EMUL_CONTINUE)
2461 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002462 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002463 if (ret != X86EMUL_CONTINUE)
2464 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002465 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002466 if (ret != X86EMUL_CONTINUE)
2467 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002468 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002469 if (ret != X86EMUL_CONTINUE)
2470 return ret;
2471
2472 return X86EMUL_CONTINUE;
2473}
2474
2475static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002476 u16 tss_selector, u16 old_tss_sel,
2477 ulong old_tss_base, struct desc_struct *new_desc)
2478{
Mathias Krause0225fb52012-08-30 01:30:16 +02002479 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002480 struct tss_segment_16 tss_seg;
2481 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002482 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002483
Avi Kivity0f65dd72011-04-20 13:37:53 +03002484 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002485 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002486 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002487 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002488 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002489
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002490 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002491
Avi Kivity0f65dd72011-04-20 13:37:53 +03002492 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002493 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002494 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002495 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002496 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002497
Avi Kivity0f65dd72011-04-20 13:37:53 +03002498 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002499 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002500 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002501 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002502 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002503
2504 if (old_tss_sel != 0xffff) {
2505 tss_seg.prev_task_link = old_tss_sel;
2506
Avi Kivity0f65dd72011-04-20 13:37:53 +03002507 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002508 &tss_seg.prev_task_link,
2509 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002510 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002511 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002512 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002513 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002514 }
2515
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002516 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002517}
2518
2519static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002520 struct tss_segment_32 *tss)
2521{
Nadav Amit5c7411e2014-04-07 18:37:47 +03002522 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002523 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002524 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002525 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2526 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2527 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2528 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2529 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2530 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2531 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2532 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002533
Avi Kivity1aa36612011-04-27 13:20:30 +03002534 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2535 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2536 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2537 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2538 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2539 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002540}
2541
2542static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002543 struct tss_segment_32 *tss)
2544{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002545 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002546 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002547
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002548 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002549 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002550 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002551 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002552
2553 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002554 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2555 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2556 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2557 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2558 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2559 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2560 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2561 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002562
2563 /*
2564 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002565 * descriptors. This is important because CPL checks will
2566 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002567 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002568 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2569 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2570 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2571 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2572 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2573 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2574 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002575
2576 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002577 * If we're switching between Protected Mode and VM86, we need to make
2578 * sure to update the mode before loading the segment descriptors so
2579 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01002580 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002581 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002582 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002583 cpl = 3;
2584 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002585 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002586 cpl = tss->cs & 3;
2587 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01002588
2589 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002590 * Now load segment descriptors. If fault happenes at this stage
2591 * it is handled in a context of new task
2592 */
Paolo Bonzini5045b462014-05-15 18:09:29 +02002593 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002594 if (ret != X86EMUL_CONTINUE)
2595 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002596 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002597 if (ret != X86EMUL_CONTINUE)
2598 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002599 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002600 if (ret != X86EMUL_CONTINUE)
2601 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002602 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002603 if (ret != X86EMUL_CONTINUE)
2604 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002605 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002606 if (ret != X86EMUL_CONTINUE)
2607 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002608 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002609 if (ret != X86EMUL_CONTINUE)
2610 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002611 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002612 if (ret != X86EMUL_CONTINUE)
2613 return ret;
2614
2615 return X86EMUL_CONTINUE;
2616}
2617
2618static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002619 u16 tss_selector, u16 old_tss_sel,
2620 ulong old_tss_base, struct desc_struct *new_desc)
2621{
Mathias Krause0225fb52012-08-30 01:30:16 +02002622 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002623 struct tss_segment_32 tss_seg;
2624 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002625 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03002626 u32 eip_offset = offsetof(struct tss_segment_32, eip);
2627 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002628
Avi Kivity0f65dd72011-04-20 13:37:53 +03002629 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002630 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002631 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002632 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002633 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002634
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002635 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002636
Nadav Amit5c7411e2014-04-07 18:37:47 +03002637 /* Only GP registers and segment selectors are saved */
2638 ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
2639 ldt_sel_offset - eip_offset, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002640 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002641 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002642 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002643
Avi Kivity0f65dd72011-04-20 13:37:53 +03002644 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002645 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002646 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002647 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002648 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002649
2650 if (old_tss_sel != 0xffff) {
2651 tss_seg.prev_task_link = old_tss_sel;
2652
Avi Kivity0f65dd72011-04-20 13:37:53 +03002653 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002654 &tss_seg.prev_task_link,
2655 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002656 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002657 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002658 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002659 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002660 }
2661
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002662 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002663}
2664
2665static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002666 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002667 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002668{
Mathias Krause0225fb52012-08-30 01:30:16 +02002669 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002670 struct desc_struct curr_tss_desc, next_tss_desc;
2671 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002672 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002673 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002674 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002675 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002676 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002677
2678 /* FIXME: old_tss_base == ~0 ? */
2679
Avi Kivitye9194642012-06-13 16:29:39 +03002680 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002681 if (ret != X86EMUL_CONTINUE)
2682 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002683 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002684 if (ret != X86EMUL_CONTINUE)
2685 return ret;
2686
2687 /* FIXME: check that next_tss_desc is tss */
2688
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002689 /*
2690 * Check privileges. The three cases are task switch caused by...
2691 *
2692 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2693 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002694 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002695 */
2696 if (reason == TASK_SWITCH_GATE) {
2697 if (idt_index != -1) {
2698 /* Software interrupts */
2699 struct desc_struct task_gate_desc;
2700 int dpl;
2701
2702 ret = read_interrupt_descriptor(ctxt, idt_index,
2703 &task_gate_desc);
2704 if (ret != X86EMUL_CONTINUE)
2705 return ret;
2706
2707 dpl = task_gate_desc.dpl;
2708 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2709 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2710 }
2711 } else if (reason != TASK_SWITCH_IRET) {
2712 int dpl = next_tss_desc.dpl;
2713 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2714 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002715 }
2716
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002717
Gleb Natapovceffb452010-03-18 15:20:19 +02002718 desc_limit = desc_limit_scaled(&next_tss_desc);
2719 if (!next_tss_desc.p ||
2720 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2721 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002722 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002723 return X86EMUL_PROPAGATE_FAULT;
2724 }
2725
2726 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2727 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002728 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002729 }
2730
2731 if (reason == TASK_SWITCH_IRET)
2732 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2733
2734 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002735 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002736 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2737 old_tss_sel = 0xffff;
2738
2739 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002740 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002741 old_tss_base, &next_tss_desc);
2742 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002743 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002744 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002745 if (ret != X86EMUL_CONTINUE)
2746 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002747
2748 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2749 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2750
2751 if (reason != TASK_SWITCH_IRET) {
2752 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002753 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002754 }
2755
Avi Kivity717746e2011-04-20 13:37:53 +03002756 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002757 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002758
Jan Kiszkae269fb22010-04-14 15:51:09 +02002759 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002760 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2761 ctxt->lock_prefix = 0;
2762 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002763 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002764 }
2765
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002766 return ret;
2767}
2768
2769int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002770 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002771 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002772{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002773 int rc;
2774
Avi Kivitydd856ef2012-08-27 23:46:17 +03002775 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002776 ctxt->_eip = ctxt->eip;
2777 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002778
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002779 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002780 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002781
Avi Kivitydd856ef2012-08-27 23:46:17 +03002782 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002783 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002784 writeback_registers(ctxt);
2785 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002786
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002787 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002788}
2789
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002790static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2791 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002792{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002793 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002794
Avi Kivitydd856ef2012-08-27 23:46:17 +03002795 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2796 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002797}
2798
Avi Kivity7af04fc2010-08-18 14:16:35 +03002799static int em_das(struct x86_emulate_ctxt *ctxt)
2800{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002801 u8 al, old_al;
2802 bool af, cf, old_cf;
2803
2804 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002805 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002806
2807 old_al = al;
2808 old_cf = cf;
2809 cf = false;
2810 af = ctxt->eflags & X86_EFLAGS_AF;
2811 if ((al & 0x0f) > 9 || af) {
2812 al -= 6;
2813 cf = old_cf | (al >= 250);
2814 af = true;
2815 } else {
2816 af = false;
2817 }
2818 if (old_al > 0x99 || old_cf) {
2819 al -= 0x60;
2820 cf = true;
2821 }
2822
Avi Kivity9dac77f2011-06-01 15:34:25 +03002823 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002824 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002825 ctxt->src.type = OP_IMM;
2826 ctxt->src.val = 0;
2827 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02002828 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03002829 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2830 if (cf)
2831 ctxt->eflags |= X86_EFLAGS_CF;
2832 if (af)
2833 ctxt->eflags |= X86_EFLAGS_AF;
2834 return X86EMUL_CONTINUE;
2835}
2836
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02002837static int em_aam(struct x86_emulate_ctxt *ctxt)
2838{
2839 u8 al, ah;
2840
2841 if (ctxt->src.val == 0)
2842 return emulate_de(ctxt);
2843
2844 al = ctxt->dst.val & 0xff;
2845 ah = al / ctxt->src.val;
2846 al %= ctxt->src.val;
2847
2848 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
2849
2850 /* Set PF, ZF, SF */
2851 ctxt->src.type = OP_IMM;
2852 ctxt->src.val = 0;
2853 ctxt->src.bytes = 1;
2854 fastop(ctxt, em_or);
2855
2856 return X86EMUL_CONTINUE;
2857}
2858
Gleb Natapov7f662272012-12-10 11:42:30 +02002859static int em_aad(struct x86_emulate_ctxt *ctxt)
2860{
2861 u8 al = ctxt->dst.val & 0xff;
2862 u8 ah = (ctxt->dst.val >> 8) & 0xff;
2863
2864 al = (al + (ah * ctxt->src.val)) & 0xff;
2865
2866 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
2867
Gleb Natapovf583c292013-02-13 17:50:39 +02002868 /* Set PF, ZF, SF */
2869 ctxt->src.type = OP_IMM;
2870 ctxt->src.val = 0;
2871 ctxt->src.bytes = 1;
2872 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02002873
2874 return X86EMUL_CONTINUE;
2875}
2876
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002877static int em_call(struct x86_emulate_ctxt *ctxt)
2878{
2879 long rel = ctxt->src.val;
2880
2881 ctxt->src.val = (unsigned long)ctxt->_eip;
2882 jmp_rel(ctxt, rel);
2883 return em_push(ctxt);
2884}
2885
Avi Kivity0ef753b2010-08-18 14:51:45 +03002886static int em_call_far(struct x86_emulate_ctxt *ctxt)
2887{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002888 u16 sel, old_cs;
2889 ulong old_eip;
2890 int rc;
2891
Avi Kivity1aa36612011-04-27 13:20:30 +03002892 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002893 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002894
Avi Kivity9dac77f2011-06-01 15:34:25 +03002895 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002896 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002897 return X86EMUL_CONTINUE;
2898
Avi Kivity9dac77f2011-06-01 15:34:25 +03002899 ctxt->_eip = 0;
2900 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002901
Avi Kivity9dac77f2011-06-01 15:34:25 +03002902 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002903 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002904 if (rc != X86EMUL_CONTINUE)
2905 return rc;
2906
Avi Kivity9dac77f2011-06-01 15:34:25 +03002907 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002908 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002909}
2910
Avi Kivity40ece7c2010-08-18 15:12:09 +03002911static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2912{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002913 int rc;
2914
Avi Kivity9dac77f2011-06-01 15:34:25 +03002915 ctxt->dst.type = OP_REG;
2916 ctxt->dst.addr.reg = &ctxt->_eip;
2917 ctxt->dst.bytes = ctxt->op_bytes;
2918 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002919 if (rc != X86EMUL_CONTINUE)
2920 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03002921 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002922 return X86EMUL_CONTINUE;
2923}
2924
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002925static int em_xchg(struct x86_emulate_ctxt *ctxt)
2926{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002927 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002928 ctxt->src.val = ctxt->dst.val;
2929 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002930
2931 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002932 ctxt->dst.val = ctxt->src.orig_val;
2933 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002934 return X86EMUL_CONTINUE;
2935}
2936
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002937static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2938{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002939 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02002940 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002941}
2942
Avi Kivity61429142010-08-19 15:13:00 +03002943static int em_cwd(struct x86_emulate_ctxt *ctxt)
2944{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002945 ctxt->dst.type = OP_REG;
2946 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002947 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002948 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002949
2950 return X86EMUL_CONTINUE;
2951}
2952
Avi Kivity48bb5d32010-08-18 18:54:34 +03002953static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2954{
Avi Kivity48bb5d32010-08-18 18:54:34 +03002955 u64 tsc = 0;
2956
Avi Kivity717746e2011-04-20 13:37:53 +03002957 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002958 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
2959 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03002960 return X86EMUL_CONTINUE;
2961}
2962
Avi Kivity222d21a2011-11-10 14:57:30 +02002963static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
2964{
2965 u64 pmc;
2966
Avi Kivitydd856ef2012-08-27 23:46:17 +03002967 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02002968 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002969 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
2970 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02002971 return X86EMUL_CONTINUE;
2972}
2973
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002974static int em_mov(struct x86_emulate_ctxt *ctxt)
2975{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01002976 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002977 return X86EMUL_CONTINUE;
2978}
2979
Borislav Petkov84cffe42013-10-29 12:54:56 +01002980#define FFL(x) bit(X86_FEATURE_##x)
2981
2982static int em_movbe(struct x86_emulate_ctxt *ctxt)
2983{
2984 u32 ebx, ecx, edx, eax = 1;
2985 u16 tmp;
2986
2987 /*
2988 * Check MOVBE is set in the guest-visible CPUID leaf.
2989 */
2990 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2991 if (!(ecx & FFL(MOVBE)))
2992 return emulate_ud(ctxt);
2993
2994 switch (ctxt->op_bytes) {
2995 case 2:
2996 /*
2997 * From MOVBE definition: "...When the operand size is 16 bits,
2998 * the upper word of the destination register remains unchanged
2999 * ..."
3000 *
3001 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3002 * rules so we have to do the operation almost per hand.
3003 */
3004 tmp = (u16)ctxt->src.val;
3005 ctxt->dst.val &= ~0xffffUL;
3006 ctxt->dst.val |= (unsigned long)swab16(tmp);
3007 break;
3008 case 4:
3009 ctxt->dst.val = swab32((u32)ctxt->src.val);
3010 break;
3011 case 8:
3012 ctxt->dst.val = swab64(ctxt->src.val);
3013 break;
3014 default:
3015 return X86EMUL_PROPAGATE_FAULT;
3016 }
3017 return X86EMUL_CONTINUE;
3018}
3019
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003020static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3021{
3022 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3023 return emulate_gp(ctxt, 0);
3024
3025 /* Disable writeback. */
3026 ctxt->dst.type = OP_NONE;
3027 return X86EMUL_CONTINUE;
3028}
3029
3030static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3031{
3032 unsigned long val;
3033
3034 if (ctxt->mode == X86EMUL_MODE_PROT64)
3035 val = ctxt->src.val & ~0ULL;
3036 else
3037 val = ctxt->src.val & ~0U;
3038
3039 /* #UD condition is already handled. */
3040 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3041 return emulate_gp(ctxt, 0);
3042
3043 /* Disable writeback. */
3044 ctxt->dst.type = OP_NONE;
3045 return X86EMUL_CONTINUE;
3046}
3047
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003048static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3049{
3050 u64 msr_data;
3051
Avi Kivitydd856ef2012-08-27 23:46:17 +03003052 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3053 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3054 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003055 return emulate_gp(ctxt, 0);
3056
3057 return X86EMUL_CONTINUE;
3058}
3059
3060static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3061{
3062 u64 msr_data;
3063
Avi Kivitydd856ef2012-08-27 23:46:17 +03003064 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003065 return emulate_gp(ctxt, 0);
3066
Avi Kivitydd856ef2012-08-27 23:46:17 +03003067 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3068 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003069 return X86EMUL_CONTINUE;
3070}
3071
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003072static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3073{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003074 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003075 return emulate_ud(ctxt);
3076
Avi Kivity9dac77f2011-06-01 15:34:25 +03003077 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003078 return X86EMUL_CONTINUE;
3079}
3080
3081static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3082{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003083 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003084
Avi Kivity9dac77f2011-06-01 15:34:25 +03003085 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003086 return emulate_ud(ctxt);
3087
Avi Kivity9dac77f2011-06-01 15:34:25 +03003088 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003089 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3090
3091 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003092 ctxt->dst.type = OP_NONE;
3093 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003094}
3095
Avi Kivitya14e5792012-06-13 12:28:33 +03003096static int em_lldt(struct x86_emulate_ctxt *ctxt)
3097{
3098 u16 sel = ctxt->src.val;
3099
3100 /* Disable writeback. */
3101 ctxt->dst.type = OP_NONE;
3102 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3103}
3104
Avi Kivity80890002012-06-13 16:33:29 +03003105static int em_ltr(struct x86_emulate_ctxt *ctxt)
3106{
3107 u16 sel = ctxt->src.val;
3108
3109 /* Disable writeback. */
3110 ctxt->dst.type = OP_NONE;
3111 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3112}
3113
Avi Kivity38503912011-03-31 18:48:09 +02003114static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3115{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003116 int rc;
3117 ulong linear;
3118
Avi Kivity9dac77f2011-06-01 15:34:25 +03003119 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003120 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003121 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003122 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003123 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003124 return X86EMUL_CONTINUE;
3125}
3126
Avi Kivity2d04a052011-04-20 15:32:49 +03003127static int em_clts(struct x86_emulate_ctxt *ctxt)
3128{
3129 ulong cr0;
3130
3131 cr0 = ctxt->ops->get_cr(ctxt, 0);
3132 cr0 &= ~X86_CR0_TS;
3133 ctxt->ops->set_cr(ctxt, 0, cr0);
3134 return X86EMUL_CONTINUE;
3135}
3136
Avi Kivity26d05cc2011-04-21 12:07:59 +03003137static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3138{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003139 int rc;
3140
Avi Kivity9dac77f2011-06-01 15:34:25 +03003141 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003142 return X86EMUL_UNHANDLEABLE;
3143
3144 rc = ctxt->ops->fix_hypercall(ctxt);
3145 if (rc != X86EMUL_CONTINUE)
3146 return rc;
3147
3148 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003149 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003150 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003151 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003152 return X86EMUL_CONTINUE;
3153}
3154
Avi Kivity96051572012-06-10 17:21:18 +03003155static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3156 void (*get)(struct x86_emulate_ctxt *ctxt,
3157 struct desc_ptr *ptr))
3158{
3159 struct desc_ptr desc_ptr;
3160
3161 if (ctxt->mode == X86EMUL_MODE_PROT64)
3162 ctxt->op_bytes = 8;
3163 get(ctxt, &desc_ptr);
3164 if (ctxt->op_bytes == 2) {
3165 ctxt->op_bytes = 4;
3166 desc_ptr.address &= 0x00ffffff;
3167 }
3168 /* Disable writeback. */
3169 ctxt->dst.type = OP_NONE;
3170 return segmented_write(ctxt, ctxt->dst.addr.mem,
3171 &desc_ptr, 2 + ctxt->op_bytes);
3172}
3173
3174static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3175{
3176 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3177}
3178
3179static int em_sidt(struct x86_emulate_ctxt *ctxt)
3180{
3181 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3182}
3183
Avi Kivity26d05cc2011-04-21 12:07:59 +03003184static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3185{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003186 struct desc_ptr desc_ptr;
3187 int rc;
3188
Avi Kivity510425f2012-06-07 17:04:36 +03003189 if (ctxt->mode == X86EMUL_MODE_PROT64)
3190 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003191 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003192 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003193 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003194 if (rc != X86EMUL_CONTINUE)
3195 return rc;
3196 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3197 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003198 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003199 return X86EMUL_CONTINUE;
3200}
3201
Avi Kivity5ef39c72011-04-21 12:21:50 +03003202static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003203{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003204 int rc;
3205
Avi Kivity5ef39c72011-04-21 12:21:50 +03003206 rc = ctxt->ops->fix_hypercall(ctxt);
3207
Avi Kivity26d05cc2011-04-21 12:07:59 +03003208 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003209 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003210 return rc;
3211}
3212
3213static int em_lidt(struct x86_emulate_ctxt *ctxt)
3214{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003215 struct desc_ptr desc_ptr;
3216 int rc;
3217
Avi Kivity510425f2012-06-07 17:04:36 +03003218 if (ctxt->mode == X86EMUL_MODE_PROT64)
3219 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003220 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003221 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003222 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003223 if (rc != X86EMUL_CONTINUE)
3224 return rc;
3225 ctxt->ops->set_idt(ctxt, &desc_ptr);
3226 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003227 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003228 return X86EMUL_CONTINUE;
3229}
3230
3231static int em_smsw(struct x86_emulate_ctxt *ctxt)
3232{
Nadav Amit32e94d02014-06-02 18:34:11 +03003233 if (ctxt->dst.type == OP_MEM)
3234 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003235 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003236 return X86EMUL_CONTINUE;
3237}
3238
3239static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3240{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003241 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003242 | (ctxt->src.val & 0x0f));
3243 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003244 return X86EMUL_CONTINUE;
3245}
3246
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003247static int em_loop(struct x86_emulate_ctxt *ctxt)
3248{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003249 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3250 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003251 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3252 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003253
3254 return X86EMUL_CONTINUE;
3255}
3256
3257static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3258{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003259 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003260 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003261
3262 return X86EMUL_CONTINUE;
3263}
3264
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003265static int em_in(struct x86_emulate_ctxt *ctxt)
3266{
3267 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3268 &ctxt->dst.val))
3269 return X86EMUL_IO_NEEDED;
3270
3271 return X86EMUL_CONTINUE;
3272}
3273
3274static int em_out(struct x86_emulate_ctxt *ctxt)
3275{
3276 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3277 &ctxt->src.val, 1);
3278 /* Disable writeback. */
3279 ctxt->dst.type = OP_NONE;
3280 return X86EMUL_CONTINUE;
3281}
3282
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003283static int em_cli(struct x86_emulate_ctxt *ctxt)
3284{
3285 if (emulator_bad_iopl(ctxt))
3286 return emulate_gp(ctxt, 0);
3287
3288 ctxt->eflags &= ~X86_EFLAGS_IF;
3289 return X86EMUL_CONTINUE;
3290}
3291
3292static int em_sti(struct x86_emulate_ctxt *ctxt)
3293{
3294 if (emulator_bad_iopl(ctxt))
3295 return emulate_gp(ctxt, 0);
3296
3297 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3298 ctxt->eflags |= X86_EFLAGS_IF;
3299 return X86EMUL_CONTINUE;
3300}
3301
Avi Kivity6d6eede2012-06-07 14:11:36 +03003302static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3303{
3304 u32 eax, ebx, ecx, edx;
3305
Avi Kivitydd856ef2012-08-27 23:46:17 +03003306 eax = reg_read(ctxt, VCPU_REGS_RAX);
3307 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003308 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003309 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3310 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3311 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3312 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003313 return X86EMUL_CONTINUE;
3314}
3315
Paolo Bonzini98f73632013-10-31 11:19:42 +01003316static int em_sahf(struct x86_emulate_ctxt *ctxt)
3317{
3318 u32 flags;
3319
3320 flags = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF;
3321 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3322
3323 ctxt->eflags &= ~0xffUL;
3324 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3325 return X86EMUL_CONTINUE;
3326}
3327
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003328static int em_lahf(struct x86_emulate_ctxt *ctxt)
3329{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003330 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3331 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003332 return X86EMUL_CONTINUE;
3333}
3334
Avi Kivity92998362012-06-13 12:25:06 +03003335static int em_bswap(struct x86_emulate_ctxt *ctxt)
3336{
3337 switch (ctxt->op_bytes) {
3338#ifdef CONFIG_X86_64
3339 case 8:
3340 asm("bswap %0" : "+r"(ctxt->dst.val));
3341 break;
3342#endif
3343 default:
3344 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3345 break;
3346 }
3347 return X86EMUL_CONTINUE;
3348}
3349
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003350static bool valid_cr(int nr)
3351{
3352 switch (nr) {
3353 case 0:
3354 case 2 ... 4:
3355 case 8:
3356 return true;
3357 default:
3358 return false;
3359 }
3360}
3361
3362static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3363{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003364 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003365 return emulate_ud(ctxt);
3366
3367 return X86EMUL_CONTINUE;
3368}
3369
3370static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3371{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003372 u64 new_val = ctxt->src.val64;
3373 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003374 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003375
3376 static u64 cr_reserved_bits[] = {
3377 0xffffffff00000000ULL,
3378 0, 0, 0, /* CR3 checked later */
3379 CR4_RESERVED_BITS,
3380 0, 0, 0,
3381 CR8_RESERVED_BITS,
3382 };
3383
3384 if (!valid_cr(cr))
3385 return emulate_ud(ctxt);
3386
3387 if (new_val & cr_reserved_bits[cr])
3388 return emulate_gp(ctxt, 0);
3389
3390 switch (cr) {
3391 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003392 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003393 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3394 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3395 return emulate_gp(ctxt, 0);
3396
Avi Kivity717746e2011-04-20 13:37:53 +03003397 cr4 = ctxt->ops->get_cr(ctxt, 4);
3398 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003399
3400 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3401 !(cr4 & X86_CR4_PAE))
3402 return emulate_gp(ctxt, 0);
3403
3404 break;
3405 }
3406 case 3: {
3407 u64 rsvd = 0;
3408
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003409 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3410 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003411 rsvd = CR3_L_MODE_RESERVED_BITS;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003412
3413 if (new_val & rsvd)
3414 return emulate_gp(ctxt, 0);
3415
3416 break;
3417 }
3418 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003419 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003420
3421 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3422 return emulate_gp(ctxt, 0);
3423
3424 break;
3425 }
3426 }
3427
3428 return X86EMUL_CONTINUE;
3429}
3430
Joerg Roedel3b88e412011-04-04 12:39:29 +02003431static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3432{
3433 unsigned long dr7;
3434
Avi Kivity717746e2011-04-20 13:37:53 +03003435 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003436
3437 /* Check if DR7.Global_Enable is set */
3438 return dr7 & (1 << 13);
3439}
3440
3441static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3442{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003443 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003444 u64 cr4;
3445
3446 if (dr > 7)
3447 return emulate_ud(ctxt);
3448
Avi Kivity717746e2011-04-20 13:37:53 +03003449 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003450 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3451 return emulate_ud(ctxt);
3452
3453 if (check_dr7_gd(ctxt))
3454 return emulate_db(ctxt);
3455
3456 return X86EMUL_CONTINUE;
3457}
3458
3459static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3460{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003461 u64 new_val = ctxt->src.val64;
3462 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003463
3464 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3465 return emulate_gp(ctxt, 0);
3466
3467 return check_dr_read(ctxt);
3468}
3469
Joerg Roedel01de8b02011-04-04 12:39:31 +02003470static int check_svme(struct x86_emulate_ctxt *ctxt)
3471{
3472 u64 efer;
3473
Avi Kivity717746e2011-04-20 13:37:53 +03003474 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003475
3476 if (!(efer & EFER_SVME))
3477 return emulate_ud(ctxt);
3478
3479 return X86EMUL_CONTINUE;
3480}
3481
3482static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3483{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003484 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003485
3486 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003487 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003488 return emulate_gp(ctxt, 0);
3489
3490 return check_svme(ctxt);
3491}
3492
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003493static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3494{
Avi Kivity717746e2011-04-20 13:37:53 +03003495 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003496
Avi Kivity717746e2011-04-20 13:37:53 +03003497 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003498 return emulate_ud(ctxt);
3499
3500 return X86EMUL_CONTINUE;
3501}
3502
Joerg Roedel80612522011-04-04 12:39:33 +02003503static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3504{
Avi Kivity717746e2011-04-20 13:37:53 +03003505 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003506 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003507
Avi Kivity717746e2011-04-20 13:37:53 +03003508 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03003509 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02003510 return emulate_gp(ctxt, 0);
3511
3512 return X86EMUL_CONTINUE;
3513}
3514
Joerg Roedelf6511932011-04-04 12:39:35 +02003515static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3516{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003517 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3518 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003519 return emulate_gp(ctxt, 0);
3520
3521 return X86EMUL_CONTINUE;
3522}
3523
3524static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3525{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003526 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3527 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003528 return emulate_gp(ctxt, 0);
3529
3530 return X86EMUL_CONTINUE;
3531}
3532
Avi Kivity73fba5f2010-07-29 15:11:53 +03003533#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003534#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
3535#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
3536 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003537#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003538#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003539#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3540#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003541#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003542#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003543#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003544#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003545 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003546#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003547 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
3548 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003549#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003550
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003551#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003552#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003553#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003554#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003555#define I2bvIP(_f, _e, _i, _p) \
3556 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003557
Avi Kivityfb864fb2013-01-04 16:18:54 +02003558#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3559 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3560 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003561
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003562static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003563 DI(SrcNone | Priv, monitor),
3564 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003565 N, N, N, N, N, N,
3566};
3567
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003568static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003569 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003570 II(SrcNone | Prot | EmulateOnUD, em_vmmcall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003571 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3572 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3573 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3574 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3575 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3576 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003577};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003578
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003579static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003580 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003581 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003582 N, N, N, N, N, N,
3583};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003584
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003585static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003586 F(Lock, em_add),
3587 F(Lock | PageTable, em_or),
3588 F(Lock, em_adc),
3589 F(Lock, em_sbb),
3590 F(Lock | PageTable, em_and),
3591 F(Lock, em_sub),
3592 F(Lock, em_xor),
3593 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003594};
3595
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003596static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003597 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003598};
3599
Avi Kivity007a3b52013-01-19 19:51:51 +02003600static const struct opcode group2[] = {
3601 F(DstMem | ModRM, em_rol),
3602 F(DstMem | ModRM, em_ror),
3603 F(DstMem | ModRM, em_rcl),
3604 F(DstMem | ModRM, em_rcr),
3605 F(DstMem | ModRM, em_shl),
3606 F(DstMem | ModRM, em_shr),
3607 F(DstMem | ModRM, em_shl),
3608 F(DstMem | ModRM, em_sar),
3609};
3610
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003611static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003612 F(DstMem | SrcImm | NoWrite, em_test),
3613 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003614 F(DstMem | SrcNone | Lock, em_not),
3615 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02003616 F(DstXacc | Src2Mem, em_mul_ex),
3617 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02003618 F(DstXacc | Src2Mem, em_div_ex),
3619 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003620};
3621
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003622static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003623 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3624 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003625 N, N, N, N, N, N,
3626};
3627
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003628static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003629 F(DstMem | SrcNone | Lock, em_inc),
3630 F(DstMem | SrcNone | Lock, em_dec),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003631 I(SrcMem | Stack, em_grp45),
3632 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3633 I(SrcMem | Stack, em_grp45),
3634 I(SrcMemFAddr | ImplicitOps, em_grp45),
Gleb Natapov188424b2013-04-11 12:32:14 +03003635 I(SrcMem | Stack, em_grp45), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003636};
3637
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003638static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003639 DI(Prot, sldt),
3640 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003641 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003642 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003643 N, N, N, N,
3644};
3645
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003646static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03003647 II(Mov | DstMem, em_sgdt, sgdt),
3648 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003649 II(SrcMem | Priv, em_lgdt, lgdt),
3650 II(SrcMem | Priv, em_lidt, lidt),
3651 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3652 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3653 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003654}, {
Borislav Petkovb51e9742013-09-22 16:44:52 +02003655 I(SrcNone | Priv | EmulateOnUD, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003656 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003657 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003658 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3659 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3660 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003661} };
3662
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003663static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003664 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003665 F(DstMem | SrcImmByte | NoWrite, em_bt),
3666 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3667 F(DstMem | SrcImmByte | Lock, em_btr),
3668 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003669};
3670
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003671static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003672 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003673}, {
3674 N, N, N, N, N, N, N, N,
3675} };
3676
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003677static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003678 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003679 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003680};
3681
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003682static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003683 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003684};
3685
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003686static const struct gprefix pfx_vmovntpx = {
Avi Kivity3e114eb2012-04-09 18:40:01 +03003687 I(0, em_mov), N, N, N,
3688};
3689
Igor Mammedov27ce8252014-03-15 21:01:59 +01003690static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01003691 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003692};
3693
Gleb Natapov045a2822012-12-20 16:57:43 +02003694static const struct escape escape_d9 = { {
3695 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3696}, {
3697 /* 0xC0 - 0xC7 */
3698 N, N, N, N, N, N, N, N,
3699 /* 0xC8 - 0xCF */
3700 N, N, N, N, N, N, N, N,
3701 /* 0xD0 - 0xC7 */
3702 N, N, N, N, N, N, N, N,
3703 /* 0xD8 - 0xDF */
3704 N, N, N, N, N, N, N, N,
3705 /* 0xE0 - 0xE7 */
3706 N, N, N, N, N, N, N, N,
3707 /* 0xE8 - 0xEF */
3708 N, N, N, N, N, N, N, N,
3709 /* 0xF0 - 0xF7 */
3710 N, N, N, N, N, N, N, N,
3711 /* 0xF8 - 0xFF */
3712 N, N, N, N, N, N, N, N,
3713} };
3714
3715static const struct escape escape_db = { {
3716 N, N, N, N, N, N, N, N,
3717}, {
3718 /* 0xC0 - 0xC7 */
3719 N, N, N, N, N, N, N, N,
3720 /* 0xC8 - 0xCF */
3721 N, N, N, N, N, N, N, N,
3722 /* 0xD0 - 0xC7 */
3723 N, N, N, N, N, N, N, N,
3724 /* 0xD8 - 0xDF */
3725 N, N, N, N, N, N, N, N,
3726 /* 0xE0 - 0xE7 */
3727 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3728 /* 0xE8 - 0xEF */
3729 N, N, N, N, N, N, N, N,
3730 /* 0xF0 - 0xF7 */
3731 N, N, N, N, N, N, N, N,
3732 /* 0xF8 - 0xFF */
3733 N, N, N, N, N, N, N, N,
3734} };
3735
3736static const struct escape escape_dd = { {
3737 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3738}, {
3739 /* 0xC0 - 0xC7 */
3740 N, N, N, N, N, N, N, N,
3741 /* 0xC8 - 0xCF */
3742 N, N, N, N, N, N, N, N,
3743 /* 0xD0 - 0xC7 */
3744 N, N, N, N, N, N, N, N,
3745 /* 0xD8 - 0xDF */
3746 N, N, N, N, N, N, N, N,
3747 /* 0xE0 - 0xE7 */
3748 N, N, N, N, N, N, N, N,
3749 /* 0xE8 - 0xEF */
3750 N, N, N, N, N, N, N, N,
3751 /* 0xF0 - 0xF7 */
3752 N, N, N, N, N, N, N, N,
3753 /* 0xF8 - 0xFF */
3754 N, N, N, N, N, N, N, N,
3755} };
3756
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003757static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003758 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003759 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003760 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3761 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003762 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003763 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003764 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3765 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003766 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003767 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003768 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3769 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003770 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003771 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003772 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3773 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003774 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003775 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003776 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003777 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003778 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003779 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003780 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003781 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003782 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02003783 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003784 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003785 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003786 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003787 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003788 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003789 I(ImplicitOps | Stack | No64, em_pusha),
3790 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003791 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3792 N, N, N, N,
3793 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003794 I(SrcImm | Mov | Stack, em_push),
3795 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003796 I(SrcImmByte | Mov | Stack, em_push),
3797 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003798 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003799 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003800 /* 0x70 - 0x7F */
3801 X16(D(SrcImmByte)),
3802 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003803 G(ByteOp | DstMem | SrcImm, group1),
3804 G(DstMem | SrcImm, group1),
3805 G(ByteOp | DstMem | SrcImm | No64, group1),
3806 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003807 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003808 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003809 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003810 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003811 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003812 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003813 D(ModRM | SrcMem | NoAccess | DstReg),
3814 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3815 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003816 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003817 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003818 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003819 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003820 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003821 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01003822 II(ImplicitOps | Stack, em_popf, popf),
3823 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003824 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003825 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003826 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003827 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003828 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003829 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003830 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003831 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3832 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003833 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003834 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003835 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003836 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02003837 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003838 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003839 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003840 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003841 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003842 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3843 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003844 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003845 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003846 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Bruce Rogers32611072013-09-09 09:40:20 -06003847 I(ImplicitOps | Stack | SrcImmU16, em_ret_far_imm),
3848 I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003849 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003850 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003851 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003852 G(Src2One | ByteOp, group2), G(Src2One, group2),
3853 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003854 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02003855 I(DstAcc | SrcImmUByte | No64, em_aad),
3856 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02003857 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003858 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02003859 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003860 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003861 X3(I(SrcImmByte, em_loop)),
3862 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003863 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3864 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003865 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003866 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003867 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003868 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3869 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003870 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003871 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003872 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3873 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003874 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003875 D(ImplicitOps), D(ImplicitOps),
3876 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003877 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3878};
3879
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003880static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003881 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003882 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02003883 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003884 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003885 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003886 N, D(ImplicitOps | ModRM), N, N,
3887 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02003888 N, N, N, N, N, N, N, N,
3889 D(ImplicitOps | ModRM), N, N, N, N, N, N, D(ImplicitOps | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003890 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03003891 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
3892 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
3893 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
3894 check_cr_write),
3895 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
3896 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003897 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003898 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
3899 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
3900 N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
Avi Kivity3e114eb2012-04-09 18:40:01 +03003901 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003902 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003903 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02003904 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003905 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02003906 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003907 I(ImplicitOps | EmulateOnUD, em_sysenter),
3908 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003909 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003910 N, N, N, N, N, N, N, N,
3911 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03003912 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003913 /* 0x50 - 0x5F */
3914 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3915 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003916 N, N, N, N,
3917 N, N, N, N,
3918 N, N, N, N,
3919 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003920 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003921 N, N, N, N,
3922 N, N, N, N,
3923 N, N, N, N,
3924 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003925 /* 0x80 - 0x8F */
3926 X16(D(SrcImm)),
3927 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003928 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003929 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003930 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02003931 II(ImplicitOps, em_cpuid, cpuid),
3932 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02003933 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
3934 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003935 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003936 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003937 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02003938 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02003939 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
3940 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Avi Kivity4d758342013-01-19 19:51:55 +02003941 D(ModRM), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003942 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09003943 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03003944 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02003945 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03003946 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
3947 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003948 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003949 /* 0xB8 - 0xBF */
3950 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003951 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02003952 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
3953 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003954 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03003955 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02003956 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Wei Yongjun92f738a52010-08-17 09:19:34 +08003957 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003958 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03003959 /* 0xC8 - 0xCF */
3960 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003961 /* 0xD0 - 0xDF */
3962 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3963 /* 0xE0 - 0xEF */
3964 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3965 /* 0xF0 - 0xFF */
3966 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3967};
3968
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003969static const struct gprefix three_byte_0f_38_f0 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01003970 I(DstReg | SrcMem | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003971};
3972
3973static const struct gprefix three_byte_0f_38_f1 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01003974 I(DstMem | SrcReg | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003975};
3976
3977/*
3978 * Insns below are selected by the prefix which indexed by the third opcode
3979 * byte.
3980 */
3981static const struct opcode opcode_map_0f_38[256] = {
3982 /* 0x00 - 0x7f */
3983 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01003984 /* 0x80 - 0xef */
3985 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
3986 /* 0xf0 - 0xf1 */
3987 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f0),
3988 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f1),
3989 /* 0xf2 - 0xff */
3990 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003991};
3992
Avi Kivity73fba5f2010-07-29 15:11:53 +03003993#undef D
3994#undef N
3995#undef G
3996#undef GD
3997#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003998#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003999#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004000
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004001#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004002#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004003#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004004#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004005#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004006
Avi Kivity9dac77f2011-06-01 15:34:25 +03004007static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004008{
4009 unsigned size;
4010
Avi Kivity9dac77f2011-06-01 15:34:25 +03004011 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004012 if (size == 8)
4013 size = 4;
4014 return size;
4015}
4016
4017static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4018 unsigned size, bool sign_extension)
4019{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004020 int rc = X86EMUL_CONTINUE;
4021
4022 op->type = OP_IMM;
4023 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004024 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004025 /* NB. Immediates are sign-extended as necessary. */
4026 switch (op->bytes) {
4027 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004028 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004029 break;
4030 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004031 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004032 break;
4033 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004034 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004035 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004036 case 8:
4037 op->val = insn_fetch(s64, ctxt);
4038 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004039 }
4040 if (!sign_extension) {
4041 switch (op->bytes) {
4042 case 1:
4043 op->val &= 0xff;
4044 break;
4045 case 2:
4046 op->val &= 0xffff;
4047 break;
4048 case 4:
4049 op->val &= 0xffffffff;
4050 break;
4051 }
4052 }
4053done:
4054 return rc;
4055}
4056
Avi Kivitya99455492011-09-13 10:45:41 +03004057static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4058 unsigned d)
4059{
4060 int rc = X86EMUL_CONTINUE;
4061
4062 switch (d) {
4063 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004064 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03004065 break;
4066 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004067 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03004068 break;
4069 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004070 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004071 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03004072 *op = ctxt->memop;
4073 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004074 if (ctxt->d & BitOp)
Avi Kivitya99455492011-09-13 10:45:41 +03004075 fetch_bit_operand(ctxt);
4076 op->orig_val = op->val;
4077 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004078 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004079 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004080 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03004081 case OpAcc:
4082 op->type = OP_REG;
4083 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004084 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03004085 fetch_register_operand(op);
4086 op->orig_val = op->val;
4087 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004088 case OpAccLo:
4089 op->type = OP_REG;
4090 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4091 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4092 fetch_register_operand(op);
4093 op->orig_val = op->val;
4094 break;
4095 case OpAccHi:
4096 if (ctxt->d & ByteOp) {
4097 op->type = OP_NONE;
4098 break;
4099 }
4100 op->type = OP_REG;
4101 op->bytes = ctxt->op_bytes;
4102 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4103 fetch_register_operand(op);
4104 op->orig_val = op->val;
4105 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004106 case OpDI:
4107 op->type = OP_MEM;
4108 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4109 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004110 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya99455492011-09-13 10:45:41 +03004111 op->addr.mem.seg = VCPU_SREG_ES;
4112 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004113 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03004114 break;
4115 case OpDX:
4116 op->type = OP_REG;
4117 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004118 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03004119 fetch_register_operand(op);
4120 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004121 case OpCL:
4122 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004123 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004124 break;
4125 case OpImmByte:
4126 rc = decode_imm(ctxt, op, 1, true);
4127 break;
4128 case OpOne:
4129 op->bytes = 1;
4130 op->val = 1;
4131 break;
4132 case OpImm:
4133 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4134 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004135 case OpImm64:
4136 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4137 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004138 case OpMem8:
4139 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004140 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004141 ctxt->memop.addr.reg = decode_register(ctxt,
4142 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004143 fetch_register_operand(&ctxt->memop);
4144 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004145 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004146 case OpMem16:
4147 ctxt->memop.bytes = 2;
4148 goto mem_common;
4149 case OpMem32:
4150 ctxt->memop.bytes = 4;
4151 goto mem_common;
4152 case OpImmU16:
4153 rc = decode_imm(ctxt, op, 2, false);
4154 break;
4155 case OpImmU:
4156 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4157 break;
4158 case OpSI:
4159 op->type = OP_MEM;
4160 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4161 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004162 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Bandan Das573e80f2014-04-16 12:46:13 -04004163 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03004164 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004165 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004166 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004167 case OpXLat:
4168 op->type = OP_MEM;
4169 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4170 op->addr.mem.ea =
4171 register_address(ctxt,
4172 reg_read(ctxt, VCPU_REGS_RBX) +
4173 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04004174 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004175 op->val = 0;
4176 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004177 case OpImmFAddr:
4178 op->type = OP_IMM;
4179 op->addr.mem.ea = ctxt->_eip;
4180 op->bytes = ctxt->op_bytes + 2;
4181 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4182 break;
4183 case OpMemFAddr:
4184 ctxt->memop.bytes = ctxt->op_bytes + 2;
4185 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004186 case OpES:
4187 op->val = VCPU_SREG_ES;
4188 break;
4189 case OpCS:
4190 op->val = VCPU_SREG_CS;
4191 break;
4192 case OpSS:
4193 op->val = VCPU_SREG_SS;
4194 break;
4195 case OpDS:
4196 op->val = VCPU_SREG_DS;
4197 break;
4198 case OpFS:
4199 op->val = VCPU_SREG_FS;
4200 break;
4201 case OpGS:
4202 op->val = VCPU_SREG_GS;
4203 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004204 case OpImplicit:
4205 /* Special instructions do their own operand decoding. */
4206 default:
4207 op->type = OP_NONE; /* Disable writeback. */
4208 break;
4209 }
4210
4211done:
4212 return rc;
4213}
4214
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004215int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004216{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004217 int rc = X86EMUL_CONTINUE;
4218 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004219 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004220 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04004221 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03004222 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004223
Avi Kivityf09ed832011-09-13 10:45:40 +03004224 ctxt->memop.type = OP_NONE;
4225 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004226 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02004227 ctxt->fetch.ptr = ctxt->fetch.data;
4228 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004229 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01004230 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004231 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004232 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02004233 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004234 if (rc != X86EMUL_CONTINUE)
4235 return rc;
4236 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004237
4238 switch (mode) {
4239 case X86EMUL_MODE_REAL:
4240 case X86EMUL_MODE_VM86:
4241 case X86EMUL_MODE_PROT16:
4242 def_op_bytes = def_ad_bytes = 2;
4243 break;
4244 case X86EMUL_MODE_PROT32:
4245 def_op_bytes = def_ad_bytes = 4;
4246 break;
4247#ifdef CONFIG_X86_64
4248 case X86EMUL_MODE_PROT64:
4249 def_op_bytes = 4;
4250 def_ad_bytes = 8;
4251 break;
4252#endif
4253 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004254 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004255 }
4256
Avi Kivity9dac77f2011-06-01 15:34:25 +03004257 ctxt->op_bytes = def_op_bytes;
4258 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004259
4260 /* Legacy prefixes. */
4261 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004262 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004263 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004264 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004265 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004266 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004267 break;
4268 case 0x67: /* address-size override */
4269 if (mode == X86EMUL_MODE_PROT64)
4270 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004271 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004272 else
4273 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004274 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004275 break;
4276 case 0x26: /* ES override */
4277 case 0x2e: /* CS override */
4278 case 0x36: /* SS override */
4279 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004280 has_seg_override = true;
4281 ctxt->seg_override = (ctxt->b >> 3) & 3;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004282 break;
4283 case 0x64: /* FS override */
4284 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004285 has_seg_override = true;
4286 ctxt->seg_override = ctxt->b & 7;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004287 break;
4288 case 0x40 ... 0x4f: /* REX */
4289 if (mode != X86EMUL_MODE_PROT64)
4290 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004291 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004292 continue;
4293 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004294 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004295 break;
4296 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004297 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004298 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004299 break;
4300 default:
4301 goto done_prefixes;
4302 }
4303
4304 /* Any legacy prefix after a REX prefix nullifies its effect. */
4305
Avi Kivity9dac77f2011-06-01 15:34:25 +03004306 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004307 }
4308
4309done_prefixes:
4310
4311 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004312 if (ctxt->rex_prefix & 8)
4313 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004314
4315 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004316 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004317 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004318 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004319 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004320 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004321 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004322
4323 /* 0F_38 opcode map */
4324 if (ctxt->b == 0x38) {
4325 ctxt->opcode_len = 3;
4326 ctxt->b = insn_fetch(u8, ctxt);
4327 opcode = opcode_map_0f_38[ctxt->b];
4328 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004329 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004330 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004331
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004332 if (ctxt->d & ModRM)
4333 ctxt->modrm = insn_fetch(u8, ctxt);
4334
Nadav Amit7fe864d2014-06-02 18:34:03 +03004335 /* vex-prefix instructions are not implemented */
4336 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
4337 (mode == X86EMUL_MODE_PROT64 ||
4338 (mode >= X86EMUL_MODE_PROT16 && (ctxt->modrm & 0x80)))) {
4339 ctxt->d = NotImpl;
4340 }
4341
Avi Kivity9dac77f2011-06-01 15:34:25 +03004342 while (ctxt->d & GroupMask) {
4343 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004344 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004345 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004346 opcode = opcode.u.group[goffset];
4347 break;
4348 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004349 goffset = (ctxt->modrm >> 3) & 7;
4350 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004351 opcode = opcode.u.gdual->mod3[goffset];
4352 else
4353 opcode = opcode.u.gdual->mod012[goffset];
4354 break;
4355 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004356 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004357 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004358 break;
4359 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004360 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004361 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004362 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004363 switch (simd_prefix) {
4364 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4365 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4366 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4367 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4368 }
4369 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004370 case Escape:
4371 if (ctxt->modrm > 0xbf)
4372 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4373 else
4374 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4375 break;
Avi Kivity46561642011-04-24 14:09:59 +03004376 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004377 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004378 }
Avi Kivity46561642011-04-24 14:09:59 +03004379
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004380 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004381 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004382 }
4383
Paolo Bonzinie24186e2014-03-27 12:00:57 +01004384 /* Unrecognised? */
4385 if (ctxt->d == 0)
4386 return EMULATION_FAILED;
4387
Avi Kivity9dac77f2011-06-01 15:34:25 +03004388 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004389
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004390 if (unlikely(ctxt->d &
4391 (NotImpl|EmulateOnUD|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm))) {
4392 /*
4393 * These are copied unconditionally here, and checked unconditionally
4394 * in x86_emulate_insn.
4395 */
4396 ctxt->check_perm = opcode.check_perm;
4397 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004398
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004399 if (ctxt->d & NotImpl)
4400 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004401
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004402 if (!(ctxt->d & EmulateOnUD) && ctxt->ud)
4403 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004404
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004405 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004406 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004407
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004408 if (ctxt->d & Op3264) {
4409 if (mode == X86EMUL_MODE_PROT64)
4410 ctxt->op_bytes = 8;
4411 else
4412 ctxt->op_bytes = 4;
4413 }
4414
4415 if (ctxt->d & Sse)
4416 ctxt->op_bytes = 16;
4417 else if (ctxt->d & Mmx)
4418 ctxt->op_bytes = 8;
4419 }
Avi Kivity1253791d2011-03-29 11:41:27 +02004420
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004421 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004422 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004423 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04004424 if (!has_seg_override) {
4425 has_seg_override = true;
4426 ctxt->seg_override = ctxt->modrm_seg;
4427 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004428 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004429 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004430 if (rc != X86EMUL_CONTINUE)
4431 goto done;
4432
Bandan Das573e80f2014-04-16 12:46:13 -04004433 if (!has_seg_override)
4434 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004435
Bandan Das573e80f2014-04-16 12:46:13 -04004436 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004437
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004438 /*
4439 * Decode and fetch the source operand: register, memory
4440 * or immediate.
4441 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004442 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004443 if (rc != X86EMUL_CONTINUE)
4444 goto done;
4445
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004446 /*
4447 * Decode and fetch the second source operand: register, memory
4448 * or immediate.
4449 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004450 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004451 if (rc != X86EMUL_CONTINUE)
4452 goto done;
4453
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004454 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03004455 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004456
4457done:
Bandan Das41061cd2014-04-16 12:46:14 -04004458 if (ctxt->rip_relative)
Avi Kivityf09ed832011-09-13 10:45:40 +03004459 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004460
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004461 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004462}
4463
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004464bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4465{
4466 return ctxt->d & PageTable;
4467}
4468
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004469static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4470{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004471 /* The second termination condition only applies for REPE
4472 * and REPNE. Test if the repeat string operation prefix is
4473 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4474 * corresponding termination condition according to:
4475 * - if REPE/REPZ and ZF = 0 then done
4476 * - if REPNE/REPNZ and ZF = 1 then done
4477 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004478 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4479 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4480 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004481 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004482 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004483 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4484 return true;
4485
4486 return false;
4487}
4488
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004489static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4490{
4491 bool fault = false;
4492
4493 ctxt->ops->get_fpu(ctxt);
4494 asm volatile("1: fwait \n\t"
4495 "2: \n\t"
4496 ".pushsection .fixup,\"ax\" \n\t"
4497 "3: \n\t"
4498 "movb $1, %[fault] \n\t"
4499 "jmp 2b \n\t"
4500 ".popsection \n\t"
4501 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004502 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004503 ctxt->ops->put_fpu(ctxt);
4504
4505 if (unlikely(fault))
4506 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4507
4508 return X86EMUL_CONTINUE;
4509}
4510
4511static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4512 struct operand *op)
4513{
4514 if (op->type == OP_MM)
4515 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4516}
4517
Avi Kivitye28bbd42013-01-04 16:18:48 +02004518static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4519{
4520 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivityb9fa4092013-02-09 11:31:48 +02004521 if (!(ctxt->d & ByteOp))
4522 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Avi Kivitye28bbd42013-01-04 16:18:48 +02004523 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004524 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
4525 [fastop]"+S"(fop)
4526 : "c"(ctxt->src2.val));
Avi Kivitye28bbd42013-01-04 16:18:48 +02004527 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004528 if (!fop) /* exception is returned in fop variable */
4529 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02004530 return X86EMUL_CONTINUE;
4531}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004532
Bandan Das14985072014-04-16 12:46:09 -04004533void init_decode_cache(struct x86_emulate_ctxt *ctxt)
4534{
Bandan Das573e80f2014-04-16 12:46:13 -04004535 memset(&ctxt->rip_relative, 0,
4536 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04004537
Bandan Das14985072014-04-16 12:46:09 -04004538 ctxt->io_read.pos = 0;
4539 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04004540 ctxt->mem_read.end = 0;
4541}
4542
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004543int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004544{
Mathias Krause0225fb52012-08-30 01:30:16 +02004545 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004546 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004547 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004548
Avi Kivity9dac77f2011-06-01 15:34:25 +03004549 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004550
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004551 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004552 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004553 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004554 goto done;
4555 }
4556
Avi Kivity9dac77f2011-06-01 15:34:25 +03004557 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004558 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004559 goto done;
4560 }
4561
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004562 if (unlikely(ctxt->d &
4563 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
4564 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4565 (ctxt->d & Undefined)) {
4566 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004567 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004568 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004569
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004570 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4571 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
4572 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004573 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004574 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004575
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004576 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
4577 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004578 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004579 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004580
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004581 if (ctxt->d & Mmx) {
4582 rc = flush_pending_x87_faults(ctxt);
4583 if (rc != X86EMUL_CONTINUE)
4584 goto done;
4585 /*
4586 * Now that we know the fpu is exception safe, we can fetch
4587 * operands from it.
4588 */
4589 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4590 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4591 if (!(ctxt->d & Mov))
4592 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4593 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004594
Bandan Das685bbf42014-04-16 12:46:10 -04004595 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004596 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4597 X86_ICPT_PRE_EXCEPT);
4598 if (rc != X86EMUL_CONTINUE)
4599 goto done;
4600 }
4601
4602 /* Privileged instruction can be executed only in CPL=0 */
4603 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
4604 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004605 goto done;
4606 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004607
4608 /* Instruction can only be executed in protected mode */
4609 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
4610 rc = emulate_ud(ctxt);
4611 goto done;
4612 }
4613
4614 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04004615 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004616 rc = ctxt->check_perm(ctxt);
4617 if (rc != X86EMUL_CONTINUE)
4618 goto done;
4619 }
4620
Bandan Das685bbf42014-04-16 12:46:10 -04004621 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004622 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4623 X86_ICPT_POST_EXCEPT);
4624 if (rc != X86EMUL_CONTINUE)
4625 goto done;
4626 }
4627
4628 if (ctxt->rep_prefix && (ctxt->d & String)) {
4629 /* All REP prefixes have the same first termination condition */
4630 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
4631 ctxt->eip = ctxt->_eip;
4632 goto done;
4633 }
4634 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004635 }
4636
Avi Kivity9dac77f2011-06-01 15:34:25 +03004637 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4638 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4639 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004640 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004641 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004642 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004643 }
4644
Avi Kivity9dac77f2011-06-01 15:34:25 +03004645 if (ctxt->src2.type == OP_MEM) {
4646 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4647 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004648 if (rc != X86EMUL_CONTINUE)
4649 goto done;
4650 }
4651
Avi Kivity9dac77f2011-06-01 15:34:25 +03004652 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004653 goto special_insn;
4654
4655
Avi Kivity9dac77f2011-06-01 15:34:25 +03004656 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004657 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004658 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4659 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004660 if (rc != X86EMUL_CONTINUE)
4661 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004662 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004663 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004664
Avi Kivity018a98d2007-11-27 19:30:56 +02004665special_insn:
4666
Bandan Das685bbf42014-04-16 12:46:10 -04004667 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004668 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004669 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004670 if (rc != X86EMUL_CONTINUE)
4671 goto done;
4672 }
4673
Avi Kivity9dac77f2011-06-01 15:34:25 +03004674 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004675 if (ctxt->d & Fastop) {
4676 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4677 rc = fastop(ctxt, fop);
4678 if (rc != X86EMUL_CONTINUE)
4679 goto done;
4680 goto writeback;
4681 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004682 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004683 if (rc != X86EMUL_CONTINUE)
4684 goto done;
4685 goto writeback;
4686 }
4687
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004688 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004689 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004690 else if (ctxt->opcode_len == 3)
4691 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004692
Avi Kivity9dac77f2011-06-01 15:34:25 +03004693 switch (ctxt->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004694 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004695 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004696 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004697 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004698 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004699 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004700 if (test_cc(ctxt->b, ctxt->eflags))
4701 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004702 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004703 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004704 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004705 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004706 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004707 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03004708 ctxt->dst.type = OP_NONE;
4709 else
4710 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004711 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004712 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004713 switch (ctxt->op_bytes) {
4714 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4715 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4716 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004717 }
4718 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004719 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004720 rc = emulate_int(ctxt, 3);
4721 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004722 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004723 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004724 break;
4725 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004726 if (ctxt->eflags & EFLG_OF)
4727 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004728 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004729 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004730 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004731 jmp_rel(ctxt, ctxt->src.val);
4732 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004733 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004734 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004735 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004736 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004737 case 0xf5: /* cmc */
4738 /* complement carry flag from eflags reg */
4739 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004740 break;
4741 case 0xf8: /* clc */
4742 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004743 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004744 case 0xf9: /* stc */
4745 ctxt->eflags |= EFLG_CF;
4746 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004747 case 0xfc: /* cld */
4748 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004749 break;
4750 case 0xfd: /* std */
4751 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004752 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004753 default:
4754 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004755 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004756
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004757 if (rc != X86EMUL_CONTINUE)
4758 goto done;
4759
Avi Kivity018a98d2007-11-27 19:30:56 +02004760writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02004761 if (ctxt->d & SrcWrite) {
4762 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
4763 rc = writeback(ctxt, &ctxt->src);
4764 if (rc != X86EMUL_CONTINUE)
4765 goto done;
4766 }
Nadav Amitee212292014-06-15 16:12:58 +03004767 if (!(ctxt->d & NoWrite)) {
4768 rc = writeback(ctxt, &ctxt->dst);
4769 if (rc != X86EMUL_CONTINUE)
4770 goto done;
4771 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004772
Gleb Natapov5cd21912010-03-18 15:20:26 +02004773 /*
4774 * restore dst type in case the decoding will be reused
4775 * (happens for string instruction )
4776 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004777 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004778
Avi Kivity9dac77f2011-06-01 15:34:25 +03004779 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004780 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004781
Avi Kivity9dac77f2011-06-01 15:34:25 +03004782 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004783 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004784
Avi Kivity9dac77f2011-06-01 15:34:25 +03004785 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004786 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004787 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004788 if ((ctxt->d & SrcMask) == SrcSI)
4789 count = ctxt->src.count;
4790 else
4791 count = ctxt->dst.count;
4792 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4793 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004794
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004795 if (!string_insn_completed(ctxt)) {
4796 /*
4797 * Re-enter guest when pio read ahead buffer is empty
4798 * or, if it is not used, after each 1024 iteration.
4799 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004800 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004801 (r->end == 0 || r->end != r->pos)) {
4802 /*
4803 * Reset read cache. Usually happens before
4804 * decode, but since instruction is restarted
4805 * we have to do it here.
4806 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004807 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004808 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004809 return EMULATION_RESTART;
4810 }
4811 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004812 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004813 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004814
Avi Kivity9dac77f2011-06-01 15:34:25 +03004815 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004816
4817done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004818 if (rc == X86EMUL_PROPAGATE_FAULT)
4819 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004820 if (rc == X86EMUL_INTERCEPTED)
4821 return EMULATION_INTERCEPTED;
4822
Avi Kivitydd856ef2012-08-27 23:46:17 +03004823 if (rc == X86EMUL_CONTINUE)
4824 writeback_registers(ctxt);
4825
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004826 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004827
4828twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004829 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004830 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004831 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004832 break;
4833 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004834 case 0x0d: /* GrpP (prefetch) */
4835 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004836 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02004837 break;
4838 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004839 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004840 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004841 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004842 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004843 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004844 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03004845 if (test_cc(ctxt->b, ctxt->eflags))
4846 ctxt->dst.val = ctxt->src.val;
4847 else if (ctxt->mode != X86EMUL_MODE_PROT64 ||
4848 ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004849 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004850 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004851 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004852 if (test_cc(ctxt->b, ctxt->eflags))
4853 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004854 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004855 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004856 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004857 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004858 case 0xae: /* clflush */
4859 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004860 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004861 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004862 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004863 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004864 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004865 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004866 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004867 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004868 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004869 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004870 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004871 ctxt->dst.bytes = ctxt->op_bytes;
Nadav Amit3b320042014-06-02 18:34:08 +03004872 ctxt->dst.val = (ctxt->op_bytes == 8) ? (u64) ctxt->src.val :
4873 (u32) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004874 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004875 default:
4876 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004877 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004878
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004879threebyte_insn:
4880
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004881 if (rc != X86EMUL_CONTINUE)
4882 goto done;
4883
Avi Kivity6aa8b732006-12-10 02:21:36 -08004884 goto writeback;
4885
4886cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004887 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004888}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004889
4890void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
4891{
4892 invalidate_registers(ctxt);
4893}
4894
4895void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
4896{
4897 writeback_registers(ctxt);
4898}