blob: ca82ec9c5ffe6693663003c9773baaf969823d1a [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 Kivitya9945542011-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 Kivitya9945542011-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 Kivitya9945542011-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 Kivitya9945542011-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 Kivitya9945542011-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 Kivity12537912011-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 Bonzini5cfc7e02014-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 Kivity9dac77f2011-06-01 15:34:25 +0300713 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200714 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300715 int size, cur_size;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200716 unsigned long linear;
Avi Kivity62266862007-11-20 13:15:52 +0200717
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200718 struct segmented_address addr = { .seg = VCPU_SREG_CS,
719 .ea = fc->end };
720 cur_size = fc->end - fc->start;
721 size = min(15UL - cur_size,
722 PAGE_SIZE - offset_in_page(fc->end));
Paolo Bonzini5cfc7e02014-05-06 13:05:25 +0200723
724 /*
725 * One instruction can only straddle two pages,
726 * and one has been loaded at the beginning of
727 * x86_decode_insn. So, if not enough bytes
728 * still, we must have hit the 15-byte boundary.
729 */
730 if (unlikely(size < op_size))
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200731 return X86EMUL_UNHANDLEABLE;
732 rc = __linearize(ctxt, addr, size, false, true, &linear);
733 if (unlikely(rc != X86EMUL_CONTINUE))
734 return rc;
735 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
736 size, &ctxt->exception);
737 if (unlikely(rc != X86EMUL_CONTINUE))
738 return rc;
739 fc->end += size;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900740 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200741}
742
743static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200744 void *__dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200745{
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200746 struct fetch_cache *fc = &ctxt->fetch;
747 u8 *dest = __dest;
748 u8 *src = &fc->data[ctxt->_eip - fc->start];
Avi Kivity62266862007-11-20 13:15:52 +0200749
Paolo Bonzini5cfc7e02014-05-06 13:05:25 +0200750 /* We have to be careful about overflow! */
751 if (unlikely(ctxt->_eip > fc->end - size)) {
752 int rc = do_insn_fetch_bytes(ctxt, size);
753 if (rc != X86EMUL_CONTINUE)
754 return rc;
755 }
756
Avi Kivity62266862007-11-20 13:15:52 +0200757 while (size--) {
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200758 *dest++ = *src++;
759 ctxt->_eip++;
760 continue;
Avi Kivity62266862007-11-20 13:15:52 +0200761 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900762 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200763}
764
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900765/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900766#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900767({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900768 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900769 if (rc != X86EMUL_CONTINUE) \
770 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900771 (_type)_x; \
772})
773
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900774#define insn_fetch_arr(_arr, _size, _ctxt) \
775({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900776 if (rc != X86EMUL_CONTINUE) \
777 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900778})
779
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000780/*
781 * Given the 'reg' portion of a ModRM byte, and a register block, return a
782 * pointer into the block that addresses the relevant register.
783 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
784 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300785static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200786 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800787{
788 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200789 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790
Avi Kivity6aa8b732006-12-10 02:21:36 -0800791 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300792 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
793 else
794 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800795 return p;
796}
797
798static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200799 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800800 u16 *size, unsigned long *address, int op_bytes)
801{
802 int rc;
803
804 if (op_bytes == 2)
805 op_bytes = 3;
806 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200807 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900808 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800809 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200810 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200811 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800812 return rc;
813}
814
Avi Kivity34b77652013-01-19 19:51:56 +0200815FASTOP2(add);
816FASTOP2(or);
817FASTOP2(adc);
818FASTOP2(sbb);
819FASTOP2(and);
820FASTOP2(sub);
821FASTOP2(xor);
822FASTOP2(cmp);
823FASTOP2(test);
824
Avi Kivityb9fa4092013-02-09 11:31:48 +0200825FASTOP1SRC2(mul, mul_ex);
826FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200827FASTOP1SRC2EX(div, div_ex);
828FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +0200829
Avi Kivity34b77652013-01-19 19:51:56 +0200830FASTOP3WCL(shld);
831FASTOP3WCL(shrd);
832
833FASTOP2W(imul);
834
835FASTOP1(not);
836FASTOP1(neg);
837FASTOP1(inc);
838FASTOP1(dec);
839
840FASTOP2CL(rol);
841FASTOP2CL(ror);
842FASTOP2CL(rcl);
843FASTOP2CL(rcr);
844FASTOP2CL(shl);
845FASTOP2CL(shr);
846FASTOP2CL(sar);
847
848FASTOP2W(bsf);
849FASTOP2W(bsr);
850FASTOP2W(bt);
851FASTOP2W(bts);
852FASTOP2W(btr);
853FASTOP2W(btc);
854
Avi Kivitye47a5f52013-02-09 11:31:51 +0200855FASTOP2(xadd);
856
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200857static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300858{
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200859 u8 rc;
860 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300861
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200862 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +0200863 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200864 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
865 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300866}
867
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300868static void fetch_register_operand(struct operand *op)
869{
870 switch (op->bytes) {
871 case 1:
872 op->val = *(u8 *)op->addr.reg;
873 break;
874 case 2:
875 op->val = *(u16 *)op->addr.reg;
876 break;
877 case 4:
878 op->val = *(u32 *)op->addr.reg;
879 break;
880 case 8:
881 op->val = *(u64 *)op->addr.reg;
882 break;
883 }
884}
885
Avi Kivity12537912011-03-29 11:41:27 +0200886static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
887{
888 ctxt->ops->get_fpu(ctxt);
889 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200890 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
891 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
892 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
893 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
894 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
895 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
896 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
897 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200898#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200899 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
900 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
901 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
902 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
903 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
904 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
905 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
906 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200907#endif
908 default: BUG();
909 }
910 ctxt->ops->put_fpu(ctxt);
911}
912
913static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
914 int reg)
915{
916 ctxt->ops->get_fpu(ctxt);
917 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200918 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
919 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
920 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
921 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
922 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
923 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
924 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
925 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200926#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200927 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
928 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
929 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
930 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
931 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
932 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
933 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
934 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200935#endif
936 default: BUG();
937 }
938 ctxt->ops->put_fpu(ctxt);
939}
940
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300941static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
942{
943 ctxt->ops->get_fpu(ctxt);
944 switch (reg) {
945 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
946 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
947 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
948 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
949 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
950 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
951 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
952 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
953 default: BUG();
954 }
955 ctxt->ops->put_fpu(ctxt);
956}
957
958static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
959{
960 ctxt->ops->get_fpu(ctxt);
961 switch (reg) {
962 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
963 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
964 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
965 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
966 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
967 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
968 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
969 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
970 default: BUG();
971 }
972 ctxt->ops->put_fpu(ctxt);
973}
974
Gleb Natapov045a2822012-12-20 16:57:43 +0200975static int em_fninit(struct x86_emulate_ctxt *ctxt)
976{
977 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
978 return emulate_nm(ctxt);
979
980 ctxt->ops->get_fpu(ctxt);
981 asm volatile("fninit");
982 ctxt->ops->put_fpu(ctxt);
983 return X86EMUL_CONTINUE;
984}
985
986static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
987{
988 u16 fcw;
989
990 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
991 return emulate_nm(ctxt);
992
993 ctxt->ops->get_fpu(ctxt);
994 asm volatile("fnstcw %0": "+m"(fcw));
995 ctxt->ops->put_fpu(ctxt);
996
997 /* force 2 byte destination */
998 ctxt->dst.bytes = 2;
999 ctxt->dst.val = fcw;
1000
1001 return X86EMUL_CONTINUE;
1002}
1003
1004static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1005{
1006 u16 fsw;
1007
1008 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1009 return emulate_nm(ctxt);
1010
1011 ctxt->ops->get_fpu(ctxt);
1012 asm volatile("fnstsw %0": "+m"(fsw));
1013 ctxt->ops->put_fpu(ctxt);
1014
1015 /* force 2 byte destination */
1016 ctxt->dst.bytes = 2;
1017 ctxt->dst.val = fsw;
1018
1019 return X86EMUL_CONTINUE;
1020}
1021
Avi Kivity12537912011-03-29 11:41:27 +02001022static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001023 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001024{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001025 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001026
Avi Kivity9dac77f2011-06-01 15:34:25 +03001027 if (!(ctxt->d & ModRM))
1028 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +02001029
Avi Kivity9dac77f2011-06-01 15:34:25 +03001030 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001031 op->type = OP_XMM;
1032 op->bytes = 16;
1033 op->addr.xmm = reg;
1034 read_sse_reg(ctxt, &op->vec_val, reg);
1035 return;
1036 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001037 if (ctxt->d & Mmx) {
1038 reg &= 7;
1039 op->type = OP_MM;
1040 op->bytes = 8;
1041 op->addr.mm = reg;
1042 return;
1043 }
Avi Kivity12537912011-03-29 11:41:27 +02001044
Avi Kivity3c118e22007-10-31 10:27:04 +02001045 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001046 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1047 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1048
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001049 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001050 op->orig_val = op->val;
1051}
1052
Avi Kivitya6e34072012-06-10 17:15:39 +03001053static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1054{
1055 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1056 ctxt->modrm_seg = VCPU_SREG_SS;
1057}
1058
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001059static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001060 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001061{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001062 u8 sib;
Bandan Das02357bd2014-04-16 12:46:11 -04001063 int index_reg, base_reg, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001064 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001065 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001066
Bandan Das02357bd2014-04-16 12:46:11 -04001067 ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */
1068 index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */
1069 base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001070
Bandan Das02357bd2014-04-16 12:46:11 -04001071 ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001072 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
Bandan Das02357bd2014-04-16 12:46:11 -04001073 ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001074 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001075
Nadav Amit9b88ae92014-05-25 23:05:21 +03001076 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001077 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001078 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001079 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001080 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001081 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001082 op->type = OP_XMM;
1083 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001084 op->addr.xmm = ctxt->modrm_rm;
1085 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001086 return rc;
1087 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001088 if (ctxt->d & Mmx) {
1089 op->type = OP_MM;
1090 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001091 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001092 return rc;
1093 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001094 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001095 return rc;
1096 }
1097
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001098 op->type = OP_MEM;
1099
Avi Kivity9dac77f2011-06-01 15:34:25 +03001100 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001101 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1102 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1103 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1104 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001105
1106 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001107 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001108 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001109 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001110 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001111 break;
1112 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001113 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001114 break;
1115 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001116 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001117 break;
1118 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001119 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001120 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001121 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001122 break;
1123 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001124 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001125 break;
1126 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001127 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001128 break;
1129 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001130 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001131 break;
1132 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001133 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001134 break;
1135 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001136 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001137 break;
1138 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001139 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001140 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001141 break;
1142 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001143 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001144 break;
1145 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001146 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1147 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1148 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001149 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001150 } else {
1151 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001152 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001153 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001154 index_reg |= (sib >> 3) & 7;
1155 base_reg |= sib & 7;
1156 scale = sib >> 6;
1157
Avi Kivity9dac77f2011-06-01 15:34:25 +03001158 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001159 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001160 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001161 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001162 adjust_modrm_seg(ctxt, base_reg);
1163 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001164 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001165 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001166 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001167 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001168 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001169 } else {
1170 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001171 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001172 adjust_modrm_seg(ctxt, base_reg);
1173 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001174 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001175 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001176 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001177 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001178 break;
1179 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001180 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001181 break;
1182 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001183 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001184 break;
1185 }
1186 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001187 op->addr.mem.ea = modrm_ea;
Bandan Das41061cd2014-04-16 12:46:14 -04001188 if (ctxt->ad_bytes != 8)
1189 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
1190
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001191done:
1192 return rc;
1193}
1194
1195static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001196 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001197{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001198 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001199
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001200 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001201 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001202 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001203 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001204 break;
1205 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001206 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001207 break;
1208 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001209 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001210 break;
1211 }
1212done:
1213 return rc;
1214}
1215
Avi Kivity9dac77f2011-06-01 15:34:25 +03001216static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001217{
Sheng Yang7129eec2010-09-28 16:33:32 +08001218 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001219
Avi Kivity9dac77f2011-06-01 15:34:25 +03001220 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001221 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001222
Avi Kivity9dac77f2011-06-01 15:34:25 +03001223 if (ctxt->src.bytes == 2)
1224 sv = (s16)ctxt->src.val & (s16)mask;
1225 else if (ctxt->src.bytes == 4)
1226 sv = (s32)ctxt->src.val & (s32)mask;
Nadav Amit7dec5602014-06-15 16:12:57 +03001227 else
1228 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001229
Avi Kivity9dac77f2011-06-01 15:34:25 +03001230 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001231 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001232
1233 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001234 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001235}
1236
Gleb Natapov9de41572010-04-28 19:15:22 +03001237static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001238 unsigned long addr, void *dest, unsigned size)
1239{
1240 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001241 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001242
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001243 if (mc->pos < mc->end)
1244 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001245
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001246 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001247
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001248 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1249 &ctxt->exception);
1250 if (rc != X86EMUL_CONTINUE)
1251 return rc;
1252
1253 mc->end += size;
1254
1255read_cached:
1256 memcpy(dest, mc->data + mc->pos, size);
1257 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001258 return X86EMUL_CONTINUE;
1259}
1260
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001261static int segmented_read(struct x86_emulate_ctxt *ctxt,
1262 struct segmented_address addr,
1263 void *data,
1264 unsigned size)
1265{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001266 int rc;
1267 ulong linear;
1268
Avi Kivity83b87952011-04-03 11:31:19 +03001269 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001270 if (rc != X86EMUL_CONTINUE)
1271 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001272 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001273}
1274
1275static int segmented_write(struct x86_emulate_ctxt *ctxt,
1276 struct segmented_address addr,
1277 const void *data,
1278 unsigned size)
1279{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001280 int rc;
1281 ulong linear;
1282
Avi Kivity83b87952011-04-03 11:31:19 +03001283 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001284 if (rc != X86EMUL_CONTINUE)
1285 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001286 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1287 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001288}
1289
1290static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1291 struct segmented_address addr,
1292 const void *orig_data, const void *data,
1293 unsigned size)
1294{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001295 int rc;
1296 ulong linear;
1297
Avi Kivity83b87952011-04-03 11:31:19 +03001298 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001299 if (rc != X86EMUL_CONTINUE)
1300 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001301 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1302 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001303}
1304
Gleb Natapov7b262e92010-03-18 15:20:27 +02001305static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001306 unsigned int size, unsigned short port,
1307 void *dest)
1308{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001309 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001310
1311 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001312 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001313 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001314 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001315 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001316 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1317 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Gleb Natapov7b262e92010-03-18 15:20:27 +02001318 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1319 count);
1320 if (n == 0)
1321 n = 1;
1322 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001323 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001324 return 0;
1325 rc->end = n * size;
1326 }
1327
Nadav Amite6e39f02014-04-18 03:35:10 +03001328 if (ctxt->rep_prefix && (ctxt->d & String) &&
1329 !(ctxt->eflags & EFLG_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001330 ctxt->dst.data = rc->data + rc->pos;
1331 ctxt->dst.type = OP_MEM_STR;
1332 ctxt->dst.count = (rc->end - rc->pos) / size;
1333 rc->pos = rc->end;
1334 } else {
1335 memcpy(dest, rc->data + rc->pos, size);
1336 rc->pos += size;
1337 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001338 return 1;
1339}
1340
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001341static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1342 u16 index, struct desc_struct *desc)
1343{
1344 struct desc_ptr dt;
1345 ulong addr;
1346
1347 ctxt->ops->get_idt(ctxt, &dt);
1348
1349 if (dt.size < index * 8 + 7)
1350 return emulate_gp(ctxt, index << 3 | 0x2);
1351
1352 addr = dt.address + index * 8;
1353 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1354 &ctxt->exception);
1355}
1356
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001357static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001358 u16 selector, struct desc_ptr *dt)
1359{
Mathias Krause0225fb52012-08-30 01:30:16 +02001360 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001361 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001362
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001363 if (selector & 1 << 2) {
1364 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001365 u16 sel;
1366
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001367 memset (dt, 0, sizeof *dt);
Nadav Amit2eedcac2014-06-02 18:34:05 +03001368 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1369 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001370 return;
1371
1372 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001373 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001374 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001375 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001376}
1377
1378/* allowed just for 8 bytes segments */
1379static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001380 u16 selector, struct desc_struct *desc,
1381 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001382{
1383 struct desc_ptr dt;
1384 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001385 ulong addr;
1386
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001387 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001388
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001389 if (dt.size < index * 8 + 7)
1390 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001391
Avi Kivitye9194642012-06-13 16:29:39 +03001392 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001393 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1394 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001395}
1396
1397/* allowed just for 8 bytes segments */
1398static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001399 u16 selector, struct desc_struct *desc)
1400{
1401 struct desc_ptr dt;
1402 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001403 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001404
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001405 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001406
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001407 if (dt.size < index * 8 + 7)
1408 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001409
1410 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001411 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1412 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001413}
1414
Gleb Natapov5601d052011-03-07 14:55:06 +02001415/* Does not support long mode */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001416static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Paolo Bonzini5045b462014-05-15 18:09:29 +02001417 u16 selector, int seg, u8 cpl, bool in_task_switch)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001418{
Avi Kivity869be992012-06-13 16:30:53 +03001419 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001420 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001421 unsigned err_vec = GP_VECTOR;
1422 u32 err_code = 0;
1423 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001424 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001425 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001426 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001427 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001428
1429 memset(&seg_desc, 0, sizeof seg_desc);
1430
Kevin Wolff8da94e2013-04-11 14:06:03 +02001431 if (ctxt->mode == X86EMUL_MODE_REAL) {
1432 /* set real mode segment descriptor (keep limit etc. for
1433 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001434 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001435 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001436 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001437 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1438 /* VM86 needs a clean new segment descriptor */
1439 set_desc_base(&seg_desc, selector << 4);
1440 set_desc_limit(&seg_desc, 0xffff);
1441 seg_desc.type = 3;
1442 seg_desc.p = 1;
1443 seg_desc.s = 1;
1444 seg_desc.dpl = 3;
1445 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001446 }
1447
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001448 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001449
1450 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1451 if ((seg == VCPU_SREG_CS
1452 || (seg == VCPU_SREG_SS
1453 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1454 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001455 && null_selector)
1456 goto exception;
1457
1458 /* TR should be in GDT only */
1459 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1460 goto exception;
1461
1462 if (null_selector) /* for NULL selector skip all following checks */
1463 goto load;
1464
Avi Kivitye9194642012-06-13 16:29:39 +03001465 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001466 if (ret != X86EMUL_CONTINUE)
1467 return ret;
1468
1469 err_code = selector & 0xfffc;
1470 err_vec = GP_VECTOR;
1471
Guo Chaofc058682012-06-28 15:19:51 +08001472 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001473 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1474 goto exception;
1475
1476 if (!seg_desc.p) {
1477 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1478 goto exception;
1479 }
1480
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001481 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001482
1483 switch (seg) {
1484 case VCPU_SREG_SS:
1485 /*
1486 * segment is not a writable data segment or segment
1487 * selector's RPL != CPL or segment selector's RPL != CPL
1488 */
1489 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1490 goto exception;
1491 break;
1492 case VCPU_SREG_CS:
Paolo Bonzini5045b462014-05-15 18:09:29 +02001493 if (in_task_switch && rpl != dpl)
1494 goto exception;
1495
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001496 if (!(seg_desc.type & 8))
1497 goto exception;
1498
1499 if (seg_desc.type & 4) {
1500 /* conforming */
1501 if (dpl > cpl)
1502 goto exception;
1503 } else {
1504 /* nonconforming */
1505 if (rpl > cpl || dpl != cpl)
1506 goto exception;
1507 }
1508 /* CS(RPL) <- CPL */
1509 selector = (selector & 0xfffc) | cpl;
1510 break;
1511 case VCPU_SREG_TR:
1512 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1513 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001514 old_desc = seg_desc;
1515 seg_desc.type |= 2; /* busy */
1516 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1517 sizeof(seg_desc), &ctxt->exception);
1518 if (ret != X86EMUL_CONTINUE)
1519 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001520 break;
1521 case VCPU_SREG_LDTR:
1522 if (seg_desc.s || seg_desc.type != 2)
1523 goto exception;
1524 break;
1525 default: /* DS, ES, FS, or GS */
1526 /*
1527 * segment is not a data or readable code segment or
1528 * ((segment is a data or nonconforming code segment)
1529 * and (both RPL and CPL > DPL))
1530 */
1531 if ((seg_desc.type & 0xa) == 0x8 ||
1532 (((seg_desc.type & 0xc) != 0xc) &&
1533 (rpl > dpl && cpl > dpl)))
1534 goto exception;
1535 break;
1536 }
1537
1538 if (seg_desc.s) {
1539 /* mark segment as accessed */
1540 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001541 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001542 if (ret != X86EMUL_CONTINUE)
1543 return ret;
Nadav Amite37a75a2014-06-02 18:34:04 +03001544 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
1545 ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3,
1546 sizeof(base3), &ctxt->exception);
1547 if (ret != X86EMUL_CONTINUE)
1548 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001549 }
1550load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001551 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001552 return X86EMUL_CONTINUE;
1553exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001554 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001555 return X86EMUL_PROPAGATE_FAULT;
1556}
1557
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001558static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1559 u16 selector, int seg)
1560{
1561 u8 cpl = ctxt->ops->cpl(ctxt);
Paolo Bonzini5045b462014-05-15 18:09:29 +02001562 return __load_segment_descriptor(ctxt, selector, seg, cpl, false);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001563}
1564
Wei Yongjun31be40b2010-08-17 09:17:30 +08001565static void write_register_operand(struct operand *op)
1566{
1567 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1568 switch (op->bytes) {
1569 case 1:
1570 *(u8 *)op->addr.reg = (u8)op->val;
1571 break;
1572 case 2:
1573 *(u16 *)op->addr.reg = (u16)op->val;
1574 break;
1575 case 4:
1576 *op->addr.reg = (u32)op->val;
1577 break; /* 64b: zero-extend */
1578 case 8:
1579 *op->addr.reg = op->val;
1580 break;
1581 }
1582}
1583
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001584static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001585{
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001586 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001587 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001588 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001589 break;
1590 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001591 if (ctxt->lock_prefix)
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001592 return segmented_cmpxchg(ctxt,
1593 op->addr.mem,
1594 &op->orig_val,
1595 &op->val,
1596 op->bytes);
1597 else
1598 return segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001599 op->addr.mem,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001600 &op->val,
1601 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001602 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001603 case OP_MEM_STR:
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001604 return segmented_write(ctxt,
1605 op->addr.mem,
1606 op->data,
1607 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001608 break;
Avi Kivity12537912011-03-29 11:41:27 +02001609 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001610 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001611 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001612 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001613 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001614 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001615 case OP_NONE:
1616 /* no writeback */
1617 break;
1618 default:
1619 break;
1620 }
1621 return X86EMUL_CONTINUE;
1622}
1623
Avi Kivity51ddff52012-06-12 20:19:40 +03001624static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001625{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001626 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001627
Avi Kivity5ad105e2012-08-19 14:34:31 +03001628 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001629 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001630 addr.seg = VCPU_SREG_SS;
1631
Avi Kivity51ddff52012-06-12 20:19:40 +03001632 return segmented_write(ctxt, addr, data, bytes);
1633}
1634
1635static int em_push(struct x86_emulate_ctxt *ctxt)
1636{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001637 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001638 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001639 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001640}
1641
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001642static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001643 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001644{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001645 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001646 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001647
Avi Kivitydd856ef2012-08-27 23:46:17 +03001648 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001649 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001650 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001651 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001652 return rc;
1653
Avi Kivity5ad105e2012-08-19 14:34:31 +03001654 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001655 return rc;
1656}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001657
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001658static int em_pop(struct x86_emulate_ctxt *ctxt)
1659{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001660 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001661}
1662
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001663static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001664 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001665{
1666 int rc;
1667 unsigned long val, change_mask;
1668 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001669 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001670
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001671 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001672 if (rc != X86EMUL_CONTINUE)
1673 return rc;
1674
1675 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1676 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1677
1678 switch(ctxt->mode) {
1679 case X86EMUL_MODE_PROT64:
1680 case X86EMUL_MODE_PROT32:
1681 case X86EMUL_MODE_PROT16:
1682 if (cpl == 0)
1683 change_mask |= EFLG_IOPL;
1684 if (cpl <= iopl)
1685 change_mask |= EFLG_IF;
1686 break;
1687 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001688 if (iopl < 3)
1689 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001690 change_mask |= EFLG_IF;
1691 break;
1692 default: /* real mode */
1693 change_mask |= (EFLG_IOPL | EFLG_IF);
1694 break;
1695 }
1696
1697 *(unsigned long *)dest =
1698 (ctxt->eflags & ~change_mask) | (val & change_mask);
1699
1700 return rc;
1701}
1702
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001703static int em_popf(struct x86_emulate_ctxt *ctxt)
1704{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001705 ctxt->dst.type = OP_REG;
1706 ctxt->dst.addr.reg = &ctxt->eflags;
1707 ctxt->dst.bytes = ctxt->op_bytes;
1708 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001709}
1710
Avi Kivity612e89f2012-06-12 20:03:23 +03001711static int em_enter(struct x86_emulate_ctxt *ctxt)
1712{
1713 int rc;
1714 unsigned frame_size = ctxt->src.val;
1715 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001716 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001717
1718 if (nesting_level)
1719 return X86EMUL_UNHANDLEABLE;
1720
Avi Kivitydd856ef2012-08-27 23:46:17 +03001721 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1722 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001723 if (rc != X86EMUL_CONTINUE)
1724 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001725 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001726 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001727 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1728 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001729 stack_mask(ctxt));
1730 return X86EMUL_CONTINUE;
1731}
1732
Avi Kivityf47cfa32012-06-07 17:49:24 +03001733static int em_leave(struct x86_emulate_ctxt *ctxt)
1734{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001735 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001736 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001737 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001738}
1739
Avi Kivity1cd196e2011-09-13 10:45:51 +03001740static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001741{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001742 int seg = ctxt->src2.val;
1743
Avi Kivity9dac77f2011-06-01 15:34:25 +03001744 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001745
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001746 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001747}
1748
Avi Kivity1cd196e2011-09-13 10:45:51 +03001749static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001750{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001751 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001752 unsigned long selector;
1753 int rc;
1754
Avi Kivity9dac77f2011-06-01 15:34:25 +03001755 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001756 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001757 return rc;
1758
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001759 if (ctxt->modrm_reg == VCPU_SREG_SS)
1760 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
1761
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001762 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001763 return rc;
1764}
1765
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001766static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001767{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001768 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001769 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001770 int reg = VCPU_REGS_RAX;
1771
1772 while (reg <= VCPU_REGS_RDI) {
1773 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001774 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001775
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001776 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001777 if (rc != X86EMUL_CONTINUE)
1778 return rc;
1779
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001780 ++reg;
1781 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001782
Wei Yongjunc37eda12010-06-15 09:03:33 +08001783 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001784}
1785
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001786static int em_pushf(struct x86_emulate_ctxt *ctxt)
1787{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001788 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001789 return em_push(ctxt);
1790}
1791
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001792static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001793{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001794 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001795 int reg = VCPU_REGS_RDI;
1796
1797 while (reg >= VCPU_REGS_RAX) {
1798 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001799 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001800 --reg;
1801 }
1802
Avi Kivitydd856ef2012-08-27 23:46:17 +03001803 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001804 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001805 break;
1806 --reg;
1807 }
1808 return rc;
1809}
1810
Avi Kivitydd856ef2012-08-27 23:46:17 +03001811static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001812{
Mathias Krause0225fb52012-08-30 01:30:16 +02001813 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001814 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001815 struct desc_ptr dt;
1816 gva_t cs_addr;
1817 gva_t eip_addr;
1818 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001819
1820 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001821 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001822 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001823 if (rc != X86EMUL_CONTINUE)
1824 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001825
1826 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1827
Avi Kivity9dac77f2011-06-01 15:34:25 +03001828 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001829 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001830 if (rc != X86EMUL_CONTINUE)
1831 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001832
Avi Kivity9dac77f2011-06-01 15:34:25 +03001833 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001834 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001835 if (rc != X86EMUL_CONTINUE)
1836 return rc;
1837
Avi Kivity4bff1e862011-04-20 13:37:53 +03001838 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001839
1840 eip_addr = dt.address + (irq << 2);
1841 cs_addr = dt.address + (irq << 2) + 2;
1842
Avi Kivity0f65dd72011-04-20 13:37:53 +03001843 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001844 if (rc != X86EMUL_CONTINUE)
1845 return rc;
1846
Avi Kivity0f65dd72011-04-20 13:37:53 +03001847 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001848 if (rc != X86EMUL_CONTINUE)
1849 return rc;
1850
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001851 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001852 if (rc != X86EMUL_CONTINUE)
1853 return rc;
1854
Avi Kivity9dac77f2011-06-01 15:34:25 +03001855 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001856
1857 return rc;
1858}
1859
Avi Kivitydd856ef2012-08-27 23:46:17 +03001860int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1861{
1862 int rc;
1863
1864 invalidate_registers(ctxt);
1865 rc = __emulate_int_real(ctxt, irq);
1866 if (rc == X86EMUL_CONTINUE)
1867 writeback_registers(ctxt);
1868 return rc;
1869}
1870
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001871static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001872{
1873 switch(ctxt->mode) {
1874 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001875 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001876 case X86EMUL_MODE_VM86:
1877 case X86EMUL_MODE_PROT16:
1878 case X86EMUL_MODE_PROT32:
1879 case X86EMUL_MODE_PROT64:
1880 default:
1881 /* Protected mode interrupts unimplemented yet */
1882 return X86EMUL_UNHANDLEABLE;
1883 }
1884}
1885
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001886static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001887{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001888 int rc = X86EMUL_CONTINUE;
1889 unsigned long temp_eip = 0;
1890 unsigned long temp_eflags = 0;
1891 unsigned long cs = 0;
1892 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1893 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1894 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1895 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1896
1897 /* TODO: Add stack limit check */
1898
Avi Kivity9dac77f2011-06-01 15:34:25 +03001899 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001900
1901 if (rc != X86EMUL_CONTINUE)
1902 return rc;
1903
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001904 if (temp_eip & ~0xffff)
1905 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001906
Avi Kivity9dac77f2011-06-01 15:34:25 +03001907 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001908
1909 if (rc != X86EMUL_CONTINUE)
1910 return rc;
1911
Avi Kivity9dac77f2011-06-01 15:34:25 +03001912 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001913
1914 if (rc != X86EMUL_CONTINUE)
1915 return rc;
1916
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001917 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001918
1919 if (rc != X86EMUL_CONTINUE)
1920 return rc;
1921
Avi Kivity9dac77f2011-06-01 15:34:25 +03001922 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001923
1924
Avi Kivity9dac77f2011-06-01 15:34:25 +03001925 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001926 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001927 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001928 ctxt->eflags &= ~0xffff;
1929 ctxt->eflags |= temp_eflags;
1930 }
1931
1932 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1933 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1934
1935 return rc;
1936}
1937
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001938static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001939{
1940 switch(ctxt->mode) {
1941 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001942 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001943 case X86EMUL_MODE_VM86:
1944 case X86EMUL_MODE_PROT16:
1945 case X86EMUL_MODE_PROT32:
1946 case X86EMUL_MODE_PROT64:
1947 default:
1948 /* iret from protected mode unimplemented yet */
1949 return X86EMUL_UNHANDLEABLE;
1950 }
1951}
1952
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001953static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1954{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001955 int rc;
1956 unsigned short sel;
1957
Avi Kivity9dac77f2011-06-01 15:34:25 +03001958 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001959
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001960 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001961 if (rc != X86EMUL_CONTINUE)
1962 return rc;
1963
Avi Kivity9dac77f2011-06-01 15:34:25 +03001964 ctxt->_eip = 0;
1965 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001966 return X86EMUL_CONTINUE;
1967}
1968
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001969static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001970{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001971 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001972
Avi Kivity9dac77f2011-06-01 15:34:25 +03001973 switch (ctxt->modrm_reg) {
Mohammed Gamald19292e2008-09-08 21:47:19 +03001974 case 2: /* call near abs */ {
1975 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001976 old_eip = ctxt->_eip;
1977 ctxt->_eip = ctxt->src.val;
1978 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001979 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001980 break;
1981 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001982 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001983 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001984 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001985 case 5: /* jmp far */
1986 rc = em_jmp_far(ctxt);
1987 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001988 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001989 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001990 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001991 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001992 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001993}
1994
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09001995static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001996{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001997 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001998
Nadav Amitaaa05f22014-06-02 18:34:10 +03001999 if (ctxt->dst.bytes == 16)
2000 return X86EMUL_UNHANDLEABLE;
2001
Avi Kivitydd856ef2012-08-27 23:46:17 +03002002 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2003 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2004 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2005 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002006 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002007 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002008 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2009 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002010
Laurent Vivier05f086f2007-09-24 11:10:55 +02002011 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002012 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002013 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002014}
2015
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002016static int em_ret(struct x86_emulate_ctxt *ctxt)
2017{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002018 ctxt->dst.type = OP_REG;
2019 ctxt->dst.addr.reg = &ctxt->_eip;
2020 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002021 return em_pop(ctxt);
2022}
2023
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002024static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002025{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002026 int rc;
2027 unsigned long cs;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002028 int cpl = ctxt->ops->cpl(ctxt);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002029
Avi Kivity9dac77f2011-06-01 15:34:25 +03002030 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002031 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002032 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002033 if (ctxt->op_bytes == 4)
2034 ctxt->_eip = (u32)ctxt->_eip;
2035 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002036 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002037 return rc;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002038 /* Outer-privilege level return is not implemented */
2039 if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2040 return X86EMUL_UNHANDLEABLE;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002041 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002042 return rc;
2043}
2044
Bruce Rogers32611072013-09-09 09:40:20 -06002045static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2046{
2047 int rc;
2048
2049 rc = em_ret_far(ctxt);
2050 if (rc != X86EMUL_CONTINUE)
2051 return rc;
2052 rsp_increment(ctxt, ctxt->src.val);
2053 return X86EMUL_CONTINUE;
2054}
2055
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002056static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2057{
2058 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002059 ctxt->dst.orig_val = ctxt->dst.val;
2060 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002061 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002062 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002063 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002064
2065 if (ctxt->eflags & EFLG_ZF) {
2066 /* Success: write back to memory. */
2067 ctxt->dst.val = ctxt->src.orig_val;
2068 } else {
2069 /* Failure: write the value we saw to EAX. */
2070 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002071 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Nadav Amit37c564f2014-06-02 18:34:07 +03002072 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002073 }
2074 return X86EMUL_CONTINUE;
2075}
2076
Avi Kivityd4b43252011-09-13 10:45:50 +03002077static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002078{
Avi Kivityd4b43252011-09-13 10:45:50 +03002079 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002080 unsigned short sel;
2081 int rc;
2082
Avi Kivity9dac77f2011-06-01 15:34:25 +03002083 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002084
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002085 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002086 if (rc != X86EMUL_CONTINUE)
2087 return rc;
2088
Avi Kivity9dac77f2011-06-01 15:34:25 +03002089 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002090 return rc;
2091}
2092
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002093static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002094setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002095 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002096{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002097 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002098 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002099 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002100 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002101 cs->type = 0x0b; /* Read, Execute, Accessed */
2102 cs->s = 1;
2103 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002104 cs->p = 1;
2105 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002106 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002107
Gleb Natapov79168fd2010-04-28 19:15:30 +03002108 set_desc_base(ss, 0); /* flat segment */
2109 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002110 ss->g = 1; /* 4kb granularity */
2111 ss->s = 1;
2112 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002113 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002114 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002115 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002116 ss->l = 0;
2117 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002118}
2119
Avi Kivity1a18a692012-02-01 12:23:21 +02002120static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2121{
2122 u32 eax, ebx, ecx, edx;
2123
2124 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002125 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2126 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002127 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2128 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2129}
2130
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002131static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2132{
Mathias Krause0225fb52012-08-30 01:30:16 +02002133 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002134 u32 eax, ebx, ecx, edx;
2135
2136 /*
2137 * syscall should always be enabled in longmode - so only become
2138 * vendor specific (cpuid) if other modes are active...
2139 */
2140 if (ctxt->mode == X86EMUL_MODE_PROT64)
2141 return true;
2142
2143 eax = 0x00000000;
2144 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002145 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2146 /*
2147 * Intel ("GenuineIntel")
2148 * remark: Intel CPUs only support "syscall" in 64bit
2149 * longmode. Also an 64bit guest with a
2150 * 32bit compat-app running will #UD !! While this
2151 * behaviour can be fixed (by emulating) into AMD
2152 * response - CPUs of AMD can't behave like Intel.
2153 */
2154 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2155 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2156 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2157 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002158
Avi Kivity0017f932012-06-07 14:10:16 +03002159 /* AMD ("AuthenticAMD") */
2160 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2161 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2162 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2163 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002164
Avi Kivity0017f932012-06-07 14:10:16 +03002165 /* AMD ("AMDisbetter!") */
2166 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2167 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2168 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2169 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002170
2171 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2172 return false;
2173}
2174
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002175static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002176{
Mathias Krause0225fb52012-08-30 01:30:16 +02002177 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002178 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002179 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002180 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002181 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002182
2183 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002184 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002185 ctxt->mode == X86EMUL_MODE_VM86)
2186 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002187
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002188 if (!(em_syscall_is_enabled(ctxt)))
2189 return emulate_ud(ctxt);
2190
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002191 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002192 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002193
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002194 if (!(efer & EFER_SCE))
2195 return emulate_ud(ctxt);
2196
Avi Kivity717746e2011-04-20 13:37:53 +03002197 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002198 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002199 cs_sel = (u16)(msr_data & 0xfffc);
2200 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002201
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002202 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002203 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002204 cs.l = 1;
2205 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002206 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2207 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002208
Avi Kivitydd856ef2012-08-27 23:46:17 +03002209 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002210 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002211#ifdef CONFIG_X86_64
Avi Kivitydd856ef2012-08-27 23:46:17 +03002212 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002213
Avi Kivity717746e2011-04-20 13:37:53 +03002214 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002215 ctxt->mode == X86EMUL_MODE_PROT64 ?
2216 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002217 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002218
Avi Kivity717746e2011-04-20 13:37:53 +03002219 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002220 ctxt->eflags &= ~(msr_data | EFLG_RF);
2221#endif
2222 } else {
2223 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002224 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002225 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002226
2227 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2228 }
2229
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002230 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002231}
2232
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002233static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002234{
Mathias Krause0225fb52012-08-30 01:30:16 +02002235 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002236 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002237 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002238 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002239 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002240
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002241 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002242 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002243 if (ctxt->mode == X86EMUL_MODE_REAL)
2244 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002245
Avi Kivity1a18a692012-02-01 12:23:21 +02002246 /*
2247 * Not recognized on AMD in compat mode (but is recognized in legacy
2248 * mode).
2249 */
2250 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2251 && !vendor_intel(ctxt))
2252 return emulate_ud(ctxt);
2253
Andre Przywara8c604352009-06-18 12:56:01 +02002254 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2255 * Therefore, we inject an #UD.
2256 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002257 if (ctxt->mode == X86EMUL_MODE_PROT64)
2258 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002259
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002260 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002261
Avi Kivity717746e2011-04-20 13:37:53 +03002262 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002263 switch (ctxt->mode) {
2264 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002265 if ((msr_data & 0xfffc) == 0x0)
2266 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002267 break;
2268 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002269 if (msr_data == 0x0)
2270 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002271 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002272 default:
2273 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002274 }
2275
2276 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002277 cs_sel = (u16)msr_data;
2278 cs_sel &= ~SELECTOR_RPL_MASK;
2279 ss_sel = cs_sel + 8;
2280 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002281 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002282 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002283 cs.l = 1;
2284 }
2285
Avi Kivity1aa36612011-04-27 13:20:30 +03002286 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2287 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002288
Avi Kivity717746e2011-04-20 13:37:53 +03002289 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002290 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002291
Avi Kivity717746e2011-04-20 13:37:53 +03002292 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002293 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002294
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002295 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002296}
2297
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002298static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002299{
Mathias Krause0225fb52012-08-30 01:30:16 +02002300 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002301 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002302 u64 msr_data;
2303 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002304 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002305
Gleb Natapova0044752010-02-10 14:21:31 +02002306 /* inject #GP if in real mode or Virtual 8086 mode */
2307 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002308 ctxt->mode == X86EMUL_MODE_VM86)
2309 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002310
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002311 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002312
Avi Kivity9dac77f2011-06-01 15:34:25 +03002313 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002314 usermode = X86EMUL_MODE_PROT64;
2315 else
2316 usermode = X86EMUL_MODE_PROT32;
2317
2318 cs.dpl = 3;
2319 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002320 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002321 switch (usermode) {
2322 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002323 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002324 if ((msr_data & 0xfffc) == 0x0)
2325 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002326 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002327 break;
2328 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002329 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002330 if (msr_data == 0x0)
2331 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002332 ss_sel = cs_sel + 8;
2333 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002334 cs.l = 1;
2335 break;
2336 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002337 cs_sel |= SELECTOR_RPL_MASK;
2338 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002339
Avi Kivity1aa36612011-04-27 13:20:30 +03002340 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2341 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002342
Avi Kivitydd856ef2012-08-27 23:46:17 +03002343 ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX);
2344 *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX);
Andre Przywara4668f052009-06-18 12:56:02 +02002345
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002346 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002347}
2348
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002349static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002350{
2351 int iopl;
2352 if (ctxt->mode == X86EMUL_MODE_REAL)
2353 return false;
2354 if (ctxt->mode == X86EMUL_MODE_VM86)
2355 return true;
2356 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002357 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002358}
2359
2360static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002361 u16 port, u16 len)
2362{
Mathias Krause0225fb52012-08-30 01:30:16 +02002363 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002364 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002365 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002366 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002367 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002368 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002369 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002370
Avi Kivity1aa36612011-04-27 13:20:30 +03002371 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002372 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002373 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002374 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002375 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002376 base = get_desc_base(&tr_seg);
2377#ifdef CONFIG_X86_64
2378 base |= ((u64)base3) << 32;
2379#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002380 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002381 if (r != X86EMUL_CONTINUE)
2382 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002383 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002384 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002385 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002386 if (r != X86EMUL_CONTINUE)
2387 return false;
2388 if ((perm >> bit_idx) & mask)
2389 return false;
2390 return true;
2391}
2392
2393static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002394 u16 port, u16 len)
2395{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002396 if (ctxt->perm_ok)
2397 return true;
2398
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002399 if (emulator_bad_iopl(ctxt))
2400 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002401 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002402
2403 ctxt->perm_ok = true;
2404
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002405 return true;
2406}
2407
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002408static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002409 struct tss_segment_16 *tss)
2410{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002411 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002412 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002413 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2414 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2415 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2416 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2417 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2418 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2419 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2420 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002421
Avi Kivity1aa36612011-04-27 13:20:30 +03002422 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2423 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2424 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2425 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2426 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002427}
2428
2429static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002430 struct tss_segment_16 *tss)
2431{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002432 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002433 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002434
Avi Kivity9dac77f2011-06-01 15:34:25 +03002435 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002436 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002437 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2438 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2439 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2440 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2441 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2442 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2443 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2444 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002445
2446 /*
2447 * SDM says that segment selectors are loaded before segment
2448 * descriptors
2449 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002450 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2451 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2452 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2453 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2454 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002455
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002456 cpl = tss->cs & 3;
2457
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002458 /*
Guo Chaofc058682012-06-28 15:19:51 +08002459 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002460 * it is handled in a context of new task
2461 */
Paolo Bonzini5045b462014-05-15 18:09:29 +02002462 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, 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->es, VCPU_SREG_ES, 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->cs, VCPU_SREG_CS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002469 if (ret != X86EMUL_CONTINUE)
2470 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002471 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002472 if (ret != X86EMUL_CONTINUE)
2473 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002474 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002475 if (ret != X86EMUL_CONTINUE)
2476 return ret;
2477
2478 return X86EMUL_CONTINUE;
2479}
2480
2481static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002482 u16 tss_selector, u16 old_tss_sel,
2483 ulong old_tss_base, struct desc_struct *new_desc)
2484{
Mathias Krause0225fb52012-08-30 01:30:16 +02002485 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002486 struct tss_segment_16 tss_seg;
2487 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002488 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002489
Avi Kivity0f65dd72011-04-20 13:37:53 +03002490 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002491 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002492 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002493 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002494 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002495
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002496 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002497
Avi Kivity0f65dd72011-04-20 13:37:53 +03002498 ret = ops->write_std(ctxt, old_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
Avi Kivity0f65dd72011-04-20 13:37:53 +03002504 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002505 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002506 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002507 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002508 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002509
2510 if (old_tss_sel != 0xffff) {
2511 tss_seg.prev_task_link = old_tss_sel;
2512
Avi Kivity0f65dd72011-04-20 13:37:53 +03002513 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002514 &tss_seg.prev_task_link,
2515 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002516 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002517 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002518 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002519 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002520 }
2521
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002522 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002523}
2524
2525static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002526 struct tss_segment_32 *tss)
2527{
Nadav Amit5c7411e2014-04-07 18:37:47 +03002528 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002529 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002530 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002531 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2532 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2533 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2534 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2535 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2536 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2537 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2538 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002539
Avi Kivity1aa36612011-04-27 13:20:30 +03002540 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2541 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2542 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2543 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2544 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2545 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002546}
2547
2548static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002549 struct tss_segment_32 *tss)
2550{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002551 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002552 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002553
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002554 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002555 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002556 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002557 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002558
2559 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002560 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2561 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2562 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2563 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2564 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2565 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2566 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2567 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002568
2569 /*
2570 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002571 * descriptors. This is important because CPL checks will
2572 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002573 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002574 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2575 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2576 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2577 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2578 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2579 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2580 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002581
2582 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002583 * If we're switching between Protected Mode and VM86, we need to make
2584 * sure to update the mode before loading the segment descriptors so
2585 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01002586 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002587 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002588 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002589 cpl = 3;
2590 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002591 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002592 cpl = tss->cs & 3;
2593 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01002594
2595 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002596 * Now load segment descriptors. If fault happenes at this stage
2597 * it is handled in a context of new task
2598 */
Paolo Bonzini5045b462014-05-15 18:09:29 +02002599 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR, 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->es, VCPU_SREG_ES, 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->cs, VCPU_SREG_CS, 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->ss, VCPU_SREG_SS, 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->ds, VCPU_SREG_DS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002612 if (ret != X86EMUL_CONTINUE)
2613 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002614 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002615 if (ret != X86EMUL_CONTINUE)
2616 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002617 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002618 if (ret != X86EMUL_CONTINUE)
2619 return ret;
2620
2621 return X86EMUL_CONTINUE;
2622}
2623
2624static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002625 u16 tss_selector, u16 old_tss_sel,
2626 ulong old_tss_base, struct desc_struct *new_desc)
2627{
Mathias Krause0225fb52012-08-30 01:30:16 +02002628 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002629 struct tss_segment_32 tss_seg;
2630 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002631 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03002632 u32 eip_offset = offsetof(struct tss_segment_32, eip);
2633 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002634
Avi Kivity0f65dd72011-04-20 13:37:53 +03002635 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002636 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002637 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002638 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002639 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002640
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002641 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002642
Nadav Amit5c7411e2014-04-07 18:37:47 +03002643 /* Only GP registers and segment selectors are saved */
2644 ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
2645 ldt_sel_offset - eip_offset, &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
Avi Kivity0f65dd72011-04-20 13:37:53 +03002650 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002651 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002652 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002653 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002654 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002655
2656 if (old_tss_sel != 0xffff) {
2657 tss_seg.prev_task_link = old_tss_sel;
2658
Avi Kivity0f65dd72011-04-20 13:37:53 +03002659 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002660 &tss_seg.prev_task_link,
2661 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002662 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002663 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002664 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002665 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002666 }
2667
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002668 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002669}
2670
2671static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002672 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002673 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002674{
Mathias Krause0225fb52012-08-30 01:30:16 +02002675 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002676 struct desc_struct curr_tss_desc, next_tss_desc;
2677 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002678 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002679 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002680 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002681 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002682 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002683
2684 /* FIXME: old_tss_base == ~0 ? */
2685
Avi Kivitye9194642012-06-13 16:29:39 +03002686 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002687 if (ret != X86EMUL_CONTINUE)
2688 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002689 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002690 if (ret != X86EMUL_CONTINUE)
2691 return ret;
2692
2693 /* FIXME: check that next_tss_desc is tss */
2694
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002695 /*
2696 * Check privileges. The three cases are task switch caused by...
2697 *
2698 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2699 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002700 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002701 */
2702 if (reason == TASK_SWITCH_GATE) {
2703 if (idt_index != -1) {
2704 /* Software interrupts */
2705 struct desc_struct task_gate_desc;
2706 int dpl;
2707
2708 ret = read_interrupt_descriptor(ctxt, idt_index,
2709 &task_gate_desc);
2710 if (ret != X86EMUL_CONTINUE)
2711 return ret;
2712
2713 dpl = task_gate_desc.dpl;
2714 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2715 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2716 }
2717 } else if (reason != TASK_SWITCH_IRET) {
2718 int dpl = next_tss_desc.dpl;
2719 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2720 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002721 }
2722
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002723
Gleb Natapovceffb452010-03-18 15:20:19 +02002724 desc_limit = desc_limit_scaled(&next_tss_desc);
2725 if (!next_tss_desc.p ||
2726 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2727 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002728 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002729 return X86EMUL_PROPAGATE_FAULT;
2730 }
2731
2732 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2733 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002734 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002735 }
2736
2737 if (reason == TASK_SWITCH_IRET)
2738 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2739
2740 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002741 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002742 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2743 old_tss_sel = 0xffff;
2744
2745 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002746 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002747 old_tss_base, &next_tss_desc);
2748 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002749 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002750 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002751 if (ret != X86EMUL_CONTINUE)
2752 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002753
2754 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2755 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2756
2757 if (reason != TASK_SWITCH_IRET) {
2758 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002759 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002760 }
2761
Avi Kivity717746e2011-04-20 13:37:53 +03002762 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002763 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002764
Jan Kiszkae269fb22010-04-14 15:51:09 +02002765 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002766 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2767 ctxt->lock_prefix = 0;
2768 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002769 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002770 }
2771
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002772 return ret;
2773}
2774
2775int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002776 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002777 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002778{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002779 int rc;
2780
Avi Kivitydd856ef2012-08-27 23:46:17 +03002781 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002782 ctxt->_eip = ctxt->eip;
2783 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002784
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002785 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002786 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002787
Avi Kivitydd856ef2012-08-27 23:46:17 +03002788 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002789 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002790 writeback_registers(ctxt);
2791 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002792
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002793 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002794}
2795
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002796static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2797 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002798{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002799 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002800
Avi Kivitydd856ef2012-08-27 23:46:17 +03002801 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2802 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002803}
2804
Avi Kivity7af04fc2010-08-18 14:16:35 +03002805static int em_das(struct x86_emulate_ctxt *ctxt)
2806{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002807 u8 al, old_al;
2808 bool af, cf, old_cf;
2809
2810 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002811 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002812
2813 old_al = al;
2814 old_cf = cf;
2815 cf = false;
2816 af = ctxt->eflags & X86_EFLAGS_AF;
2817 if ((al & 0x0f) > 9 || af) {
2818 al -= 6;
2819 cf = old_cf | (al >= 250);
2820 af = true;
2821 } else {
2822 af = false;
2823 }
2824 if (old_al > 0x99 || old_cf) {
2825 al -= 0x60;
2826 cf = true;
2827 }
2828
Avi Kivity9dac77f2011-06-01 15:34:25 +03002829 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002830 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002831 ctxt->src.type = OP_IMM;
2832 ctxt->src.val = 0;
2833 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02002834 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03002835 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2836 if (cf)
2837 ctxt->eflags |= X86_EFLAGS_CF;
2838 if (af)
2839 ctxt->eflags |= X86_EFLAGS_AF;
2840 return X86EMUL_CONTINUE;
2841}
2842
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02002843static int em_aam(struct x86_emulate_ctxt *ctxt)
2844{
2845 u8 al, ah;
2846
2847 if (ctxt->src.val == 0)
2848 return emulate_de(ctxt);
2849
2850 al = ctxt->dst.val & 0xff;
2851 ah = al / ctxt->src.val;
2852 al %= ctxt->src.val;
2853
2854 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
2855
2856 /* Set PF, ZF, SF */
2857 ctxt->src.type = OP_IMM;
2858 ctxt->src.val = 0;
2859 ctxt->src.bytes = 1;
2860 fastop(ctxt, em_or);
2861
2862 return X86EMUL_CONTINUE;
2863}
2864
Gleb Natapov7f662272012-12-10 11:42:30 +02002865static int em_aad(struct x86_emulate_ctxt *ctxt)
2866{
2867 u8 al = ctxt->dst.val & 0xff;
2868 u8 ah = (ctxt->dst.val >> 8) & 0xff;
2869
2870 al = (al + (ah * ctxt->src.val)) & 0xff;
2871
2872 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
2873
Gleb Natapovf583c292013-02-13 17:50:39 +02002874 /* Set PF, ZF, SF */
2875 ctxt->src.type = OP_IMM;
2876 ctxt->src.val = 0;
2877 ctxt->src.bytes = 1;
2878 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02002879
2880 return X86EMUL_CONTINUE;
2881}
2882
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002883static int em_call(struct x86_emulate_ctxt *ctxt)
2884{
2885 long rel = ctxt->src.val;
2886
2887 ctxt->src.val = (unsigned long)ctxt->_eip;
2888 jmp_rel(ctxt, rel);
2889 return em_push(ctxt);
2890}
2891
Avi Kivity0ef753b2010-08-18 14:51:45 +03002892static int em_call_far(struct x86_emulate_ctxt *ctxt)
2893{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002894 u16 sel, old_cs;
2895 ulong old_eip;
2896 int rc;
2897
Avi Kivity1aa36612011-04-27 13:20:30 +03002898 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002899 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002900
Avi Kivity9dac77f2011-06-01 15:34:25 +03002901 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002902 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002903 return X86EMUL_CONTINUE;
2904
Avi Kivity9dac77f2011-06-01 15:34:25 +03002905 ctxt->_eip = 0;
2906 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002907
Avi Kivity9dac77f2011-06-01 15:34:25 +03002908 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002909 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002910 if (rc != X86EMUL_CONTINUE)
2911 return rc;
2912
Avi Kivity9dac77f2011-06-01 15:34:25 +03002913 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002914 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002915}
2916
Avi Kivity40ece7c2010-08-18 15:12:09 +03002917static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2918{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002919 int rc;
2920
Avi Kivity9dac77f2011-06-01 15:34:25 +03002921 ctxt->dst.type = OP_REG;
2922 ctxt->dst.addr.reg = &ctxt->_eip;
2923 ctxt->dst.bytes = ctxt->op_bytes;
2924 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002925 if (rc != X86EMUL_CONTINUE)
2926 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03002927 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002928 return X86EMUL_CONTINUE;
2929}
2930
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002931static int em_xchg(struct x86_emulate_ctxt *ctxt)
2932{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002933 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002934 ctxt->src.val = ctxt->dst.val;
2935 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002936
2937 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002938 ctxt->dst.val = ctxt->src.orig_val;
2939 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002940 return X86EMUL_CONTINUE;
2941}
2942
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002943static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2944{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002945 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02002946 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002947}
2948
Avi Kivity61429142010-08-19 15:13:00 +03002949static int em_cwd(struct x86_emulate_ctxt *ctxt)
2950{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002951 ctxt->dst.type = OP_REG;
2952 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002953 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002954 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002955
2956 return X86EMUL_CONTINUE;
2957}
2958
Avi Kivity48bb5d32010-08-18 18:54:34 +03002959static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2960{
Avi Kivity48bb5d32010-08-18 18:54:34 +03002961 u64 tsc = 0;
2962
Avi Kivity717746e2011-04-20 13:37:53 +03002963 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002964 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
2965 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03002966 return X86EMUL_CONTINUE;
2967}
2968
Avi Kivity222d21a2011-11-10 14:57:30 +02002969static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
2970{
2971 u64 pmc;
2972
Avi Kivitydd856ef2012-08-27 23:46:17 +03002973 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02002974 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002975 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
2976 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02002977 return X86EMUL_CONTINUE;
2978}
2979
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002980static int em_mov(struct x86_emulate_ctxt *ctxt)
2981{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01002982 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002983 return X86EMUL_CONTINUE;
2984}
2985
Borislav Petkov84cffe42013-10-29 12:54:56 +01002986#define FFL(x) bit(X86_FEATURE_##x)
2987
2988static int em_movbe(struct x86_emulate_ctxt *ctxt)
2989{
2990 u32 ebx, ecx, edx, eax = 1;
2991 u16 tmp;
2992
2993 /*
2994 * Check MOVBE is set in the guest-visible CPUID leaf.
2995 */
2996 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2997 if (!(ecx & FFL(MOVBE)))
2998 return emulate_ud(ctxt);
2999
3000 switch (ctxt->op_bytes) {
3001 case 2:
3002 /*
3003 * From MOVBE definition: "...When the operand size is 16 bits,
3004 * the upper word of the destination register remains unchanged
3005 * ..."
3006 *
3007 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3008 * rules so we have to do the operation almost per hand.
3009 */
3010 tmp = (u16)ctxt->src.val;
3011 ctxt->dst.val &= ~0xffffUL;
3012 ctxt->dst.val |= (unsigned long)swab16(tmp);
3013 break;
3014 case 4:
3015 ctxt->dst.val = swab32((u32)ctxt->src.val);
3016 break;
3017 case 8:
3018 ctxt->dst.val = swab64(ctxt->src.val);
3019 break;
3020 default:
3021 return X86EMUL_PROPAGATE_FAULT;
3022 }
3023 return X86EMUL_CONTINUE;
3024}
3025
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003026static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3027{
3028 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3029 return emulate_gp(ctxt, 0);
3030
3031 /* Disable writeback. */
3032 ctxt->dst.type = OP_NONE;
3033 return X86EMUL_CONTINUE;
3034}
3035
3036static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3037{
3038 unsigned long val;
3039
3040 if (ctxt->mode == X86EMUL_MODE_PROT64)
3041 val = ctxt->src.val & ~0ULL;
3042 else
3043 val = ctxt->src.val & ~0U;
3044
3045 /* #UD condition is already handled. */
3046 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3047 return emulate_gp(ctxt, 0);
3048
3049 /* Disable writeback. */
3050 ctxt->dst.type = OP_NONE;
3051 return X86EMUL_CONTINUE;
3052}
3053
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003054static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3055{
3056 u64 msr_data;
3057
Avi Kivitydd856ef2012-08-27 23:46:17 +03003058 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3059 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3060 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003061 return emulate_gp(ctxt, 0);
3062
3063 return X86EMUL_CONTINUE;
3064}
3065
3066static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3067{
3068 u64 msr_data;
3069
Avi Kivitydd856ef2012-08-27 23:46:17 +03003070 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003071 return emulate_gp(ctxt, 0);
3072
Avi Kivitydd856ef2012-08-27 23:46:17 +03003073 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3074 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003075 return X86EMUL_CONTINUE;
3076}
3077
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003078static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3079{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003080 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003081 return emulate_ud(ctxt);
3082
Avi Kivity9dac77f2011-06-01 15:34:25 +03003083 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003084 return X86EMUL_CONTINUE;
3085}
3086
3087static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3088{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003089 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003090
Avi Kivity9dac77f2011-06-01 15:34:25 +03003091 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003092 return emulate_ud(ctxt);
3093
Avi Kivity9dac77f2011-06-01 15:34:25 +03003094 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003095 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3096
3097 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003098 ctxt->dst.type = OP_NONE;
3099 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003100}
3101
Avi Kivitya14e5792012-06-13 12:28:33 +03003102static int em_lldt(struct x86_emulate_ctxt *ctxt)
3103{
3104 u16 sel = ctxt->src.val;
3105
3106 /* Disable writeback. */
3107 ctxt->dst.type = OP_NONE;
3108 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3109}
3110
Avi Kivity80890002012-06-13 16:33:29 +03003111static int em_ltr(struct x86_emulate_ctxt *ctxt)
3112{
3113 u16 sel = ctxt->src.val;
3114
3115 /* Disable writeback. */
3116 ctxt->dst.type = OP_NONE;
3117 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3118}
3119
Avi Kivity38503912011-03-31 18:48:09 +02003120static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3121{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003122 int rc;
3123 ulong linear;
3124
Avi Kivity9dac77f2011-06-01 15:34:25 +03003125 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003126 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003127 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003128 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003129 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003130 return X86EMUL_CONTINUE;
3131}
3132
Avi Kivity2d04a052011-04-20 15:32:49 +03003133static int em_clts(struct x86_emulate_ctxt *ctxt)
3134{
3135 ulong cr0;
3136
3137 cr0 = ctxt->ops->get_cr(ctxt, 0);
3138 cr0 &= ~X86_CR0_TS;
3139 ctxt->ops->set_cr(ctxt, 0, cr0);
3140 return X86EMUL_CONTINUE;
3141}
3142
Avi Kivity26d05cc2011-04-21 12:07:59 +03003143static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3144{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003145 int rc;
3146
Avi Kivity9dac77f2011-06-01 15:34:25 +03003147 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003148 return X86EMUL_UNHANDLEABLE;
3149
3150 rc = ctxt->ops->fix_hypercall(ctxt);
3151 if (rc != X86EMUL_CONTINUE)
3152 return rc;
3153
3154 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003155 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003156 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003157 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003158 return X86EMUL_CONTINUE;
3159}
3160
Avi Kivity96051572012-06-10 17:21:18 +03003161static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3162 void (*get)(struct x86_emulate_ctxt *ctxt,
3163 struct desc_ptr *ptr))
3164{
3165 struct desc_ptr desc_ptr;
3166
3167 if (ctxt->mode == X86EMUL_MODE_PROT64)
3168 ctxt->op_bytes = 8;
3169 get(ctxt, &desc_ptr);
3170 if (ctxt->op_bytes == 2) {
3171 ctxt->op_bytes = 4;
3172 desc_ptr.address &= 0x00ffffff;
3173 }
3174 /* Disable writeback. */
3175 ctxt->dst.type = OP_NONE;
3176 return segmented_write(ctxt, ctxt->dst.addr.mem,
3177 &desc_ptr, 2 + ctxt->op_bytes);
3178}
3179
3180static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3181{
3182 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3183}
3184
3185static int em_sidt(struct x86_emulate_ctxt *ctxt)
3186{
3187 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3188}
3189
Avi Kivity26d05cc2011-04-21 12:07:59 +03003190static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3191{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003192 struct desc_ptr desc_ptr;
3193 int rc;
3194
Avi Kivity510425f2012-06-07 17:04:36 +03003195 if (ctxt->mode == X86EMUL_MODE_PROT64)
3196 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003197 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003198 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003199 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003200 if (rc != X86EMUL_CONTINUE)
3201 return rc;
3202 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3203 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003204 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003205 return X86EMUL_CONTINUE;
3206}
3207
Avi Kivity5ef39c72011-04-21 12:21:50 +03003208static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003209{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003210 int rc;
3211
Avi Kivity5ef39c72011-04-21 12:21:50 +03003212 rc = ctxt->ops->fix_hypercall(ctxt);
3213
Avi Kivity26d05cc2011-04-21 12:07:59 +03003214 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003215 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003216 return rc;
3217}
3218
3219static int em_lidt(struct x86_emulate_ctxt *ctxt)
3220{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003221 struct desc_ptr desc_ptr;
3222 int rc;
3223
Avi Kivity510425f2012-06-07 17:04:36 +03003224 if (ctxt->mode == X86EMUL_MODE_PROT64)
3225 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003226 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003227 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003228 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003229 if (rc != X86EMUL_CONTINUE)
3230 return rc;
3231 ctxt->ops->set_idt(ctxt, &desc_ptr);
3232 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003233 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003234 return X86EMUL_CONTINUE;
3235}
3236
3237static int em_smsw(struct x86_emulate_ctxt *ctxt)
3238{
Nadav Amit32e94d02014-06-02 18:34:11 +03003239 if (ctxt->dst.type == OP_MEM)
3240 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003241 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003242 return X86EMUL_CONTINUE;
3243}
3244
3245static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3246{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003247 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003248 | (ctxt->src.val & 0x0f));
3249 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003250 return X86EMUL_CONTINUE;
3251}
3252
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003253static int em_loop(struct x86_emulate_ctxt *ctxt)
3254{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003255 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3256 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003257 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3258 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003259
3260 return X86EMUL_CONTINUE;
3261}
3262
3263static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3264{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003265 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003266 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003267
3268 return X86EMUL_CONTINUE;
3269}
3270
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003271static int em_in(struct x86_emulate_ctxt *ctxt)
3272{
3273 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3274 &ctxt->dst.val))
3275 return X86EMUL_IO_NEEDED;
3276
3277 return X86EMUL_CONTINUE;
3278}
3279
3280static int em_out(struct x86_emulate_ctxt *ctxt)
3281{
3282 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3283 &ctxt->src.val, 1);
3284 /* Disable writeback. */
3285 ctxt->dst.type = OP_NONE;
3286 return X86EMUL_CONTINUE;
3287}
3288
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003289static int em_cli(struct x86_emulate_ctxt *ctxt)
3290{
3291 if (emulator_bad_iopl(ctxt))
3292 return emulate_gp(ctxt, 0);
3293
3294 ctxt->eflags &= ~X86_EFLAGS_IF;
3295 return X86EMUL_CONTINUE;
3296}
3297
3298static int em_sti(struct x86_emulate_ctxt *ctxt)
3299{
3300 if (emulator_bad_iopl(ctxt))
3301 return emulate_gp(ctxt, 0);
3302
3303 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3304 ctxt->eflags |= X86_EFLAGS_IF;
3305 return X86EMUL_CONTINUE;
3306}
3307
Avi Kivity6d6eede2012-06-07 14:11:36 +03003308static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3309{
3310 u32 eax, ebx, ecx, edx;
3311
Avi Kivitydd856ef2012-08-27 23:46:17 +03003312 eax = reg_read(ctxt, VCPU_REGS_RAX);
3313 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003314 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003315 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3316 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3317 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3318 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003319 return X86EMUL_CONTINUE;
3320}
3321
Paolo Bonzini98f73632013-10-31 11:19:42 +01003322static int em_sahf(struct x86_emulate_ctxt *ctxt)
3323{
3324 u32 flags;
3325
3326 flags = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF;
3327 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3328
3329 ctxt->eflags &= ~0xffUL;
3330 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3331 return X86EMUL_CONTINUE;
3332}
3333
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003334static int em_lahf(struct x86_emulate_ctxt *ctxt)
3335{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003336 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3337 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003338 return X86EMUL_CONTINUE;
3339}
3340
Avi Kivity92998362012-06-13 12:25:06 +03003341static int em_bswap(struct x86_emulate_ctxt *ctxt)
3342{
3343 switch (ctxt->op_bytes) {
3344#ifdef CONFIG_X86_64
3345 case 8:
3346 asm("bswap %0" : "+r"(ctxt->dst.val));
3347 break;
3348#endif
3349 default:
3350 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3351 break;
3352 }
3353 return X86EMUL_CONTINUE;
3354}
3355
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003356static bool valid_cr(int nr)
3357{
3358 switch (nr) {
3359 case 0:
3360 case 2 ... 4:
3361 case 8:
3362 return true;
3363 default:
3364 return false;
3365 }
3366}
3367
3368static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3369{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003370 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003371 return emulate_ud(ctxt);
3372
3373 return X86EMUL_CONTINUE;
3374}
3375
3376static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3377{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003378 u64 new_val = ctxt->src.val64;
3379 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003380 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003381
3382 static u64 cr_reserved_bits[] = {
3383 0xffffffff00000000ULL,
3384 0, 0, 0, /* CR3 checked later */
3385 CR4_RESERVED_BITS,
3386 0, 0, 0,
3387 CR8_RESERVED_BITS,
3388 };
3389
3390 if (!valid_cr(cr))
3391 return emulate_ud(ctxt);
3392
3393 if (new_val & cr_reserved_bits[cr])
3394 return emulate_gp(ctxt, 0);
3395
3396 switch (cr) {
3397 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003398 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003399 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3400 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3401 return emulate_gp(ctxt, 0);
3402
Avi Kivity717746e2011-04-20 13:37:53 +03003403 cr4 = ctxt->ops->get_cr(ctxt, 4);
3404 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003405
3406 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3407 !(cr4 & X86_CR4_PAE))
3408 return emulate_gp(ctxt, 0);
3409
3410 break;
3411 }
3412 case 3: {
3413 u64 rsvd = 0;
3414
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003415 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3416 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003417 rsvd = CR3_L_MODE_RESERVED_BITS;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003418
3419 if (new_val & rsvd)
3420 return emulate_gp(ctxt, 0);
3421
3422 break;
3423 }
3424 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003425 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003426
3427 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3428 return emulate_gp(ctxt, 0);
3429
3430 break;
3431 }
3432 }
3433
3434 return X86EMUL_CONTINUE;
3435}
3436
Joerg Roedel3b88e412011-04-04 12:39:29 +02003437static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3438{
3439 unsigned long dr7;
3440
Avi Kivity717746e2011-04-20 13:37:53 +03003441 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003442
3443 /* Check if DR7.Global_Enable is set */
3444 return dr7 & (1 << 13);
3445}
3446
3447static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3448{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003449 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003450 u64 cr4;
3451
3452 if (dr > 7)
3453 return emulate_ud(ctxt);
3454
Avi Kivity717746e2011-04-20 13:37:53 +03003455 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003456 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3457 return emulate_ud(ctxt);
3458
3459 if (check_dr7_gd(ctxt))
3460 return emulate_db(ctxt);
3461
3462 return X86EMUL_CONTINUE;
3463}
3464
3465static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3466{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003467 u64 new_val = ctxt->src.val64;
3468 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003469
3470 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3471 return emulate_gp(ctxt, 0);
3472
3473 return check_dr_read(ctxt);
3474}
3475
Joerg Roedel01de8b02011-04-04 12:39:31 +02003476static int check_svme(struct x86_emulate_ctxt *ctxt)
3477{
3478 u64 efer;
3479
Avi Kivity717746e2011-04-20 13:37:53 +03003480 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003481
3482 if (!(efer & EFER_SVME))
3483 return emulate_ud(ctxt);
3484
3485 return X86EMUL_CONTINUE;
3486}
3487
3488static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3489{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003490 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003491
3492 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003493 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003494 return emulate_gp(ctxt, 0);
3495
3496 return check_svme(ctxt);
3497}
3498
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003499static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3500{
Avi Kivity717746e2011-04-20 13:37:53 +03003501 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003502
Avi Kivity717746e2011-04-20 13:37:53 +03003503 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003504 return emulate_ud(ctxt);
3505
3506 return X86EMUL_CONTINUE;
3507}
3508
Joerg Roedel80612522011-04-04 12:39:33 +02003509static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3510{
Avi Kivity717746e2011-04-20 13:37:53 +03003511 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003512 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003513
Avi Kivity717746e2011-04-20 13:37:53 +03003514 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03003515 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02003516 return emulate_gp(ctxt, 0);
3517
3518 return X86EMUL_CONTINUE;
3519}
3520
Joerg Roedelf6511932011-04-04 12:39:35 +02003521static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3522{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003523 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3524 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003525 return emulate_gp(ctxt, 0);
3526
3527 return X86EMUL_CONTINUE;
3528}
3529
3530static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3531{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003532 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3533 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003534 return emulate_gp(ctxt, 0);
3535
3536 return X86EMUL_CONTINUE;
3537}
3538
Avi Kivity73fba5f2010-07-29 15:11:53 +03003539#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003540#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
3541#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
3542 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003543#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003544#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003545#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3546#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003547#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003548#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003549#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003550#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003551 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003552#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003553 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
3554 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003555#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003556
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003557#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003558#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003559#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003560#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003561#define I2bvIP(_f, _e, _i, _p) \
3562 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003563
Avi Kivityfb864fb2013-01-04 16:18:54 +02003564#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3565 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3566 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003567
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003568static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003569 DI(SrcNone | Priv, monitor),
3570 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003571 N, N, N, N, N, N,
3572};
3573
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003574static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003575 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003576 II(SrcNone | Prot | EmulateOnUD, em_vmmcall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003577 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3578 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3579 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3580 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3581 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3582 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003583};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003584
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003585static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003586 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003587 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003588 N, N, N, N, N, N,
3589};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003590
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003591static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003592 F(Lock, em_add),
3593 F(Lock | PageTable, em_or),
3594 F(Lock, em_adc),
3595 F(Lock, em_sbb),
3596 F(Lock | PageTable, em_and),
3597 F(Lock, em_sub),
3598 F(Lock, em_xor),
3599 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003600};
3601
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003602static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003603 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003604};
3605
Avi Kivity007a3b52013-01-19 19:51:51 +02003606static const struct opcode group2[] = {
3607 F(DstMem | ModRM, em_rol),
3608 F(DstMem | ModRM, em_ror),
3609 F(DstMem | ModRM, em_rcl),
3610 F(DstMem | ModRM, em_rcr),
3611 F(DstMem | ModRM, em_shl),
3612 F(DstMem | ModRM, em_shr),
3613 F(DstMem | ModRM, em_shl),
3614 F(DstMem | ModRM, em_sar),
3615};
3616
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003617static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003618 F(DstMem | SrcImm | NoWrite, em_test),
3619 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003620 F(DstMem | SrcNone | Lock, em_not),
3621 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02003622 F(DstXacc | Src2Mem, em_mul_ex),
3623 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02003624 F(DstXacc | Src2Mem, em_div_ex),
3625 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003626};
3627
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003628static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003629 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3630 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003631 N, N, N, N, N, N,
3632};
3633
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003634static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003635 F(DstMem | SrcNone | Lock, em_inc),
3636 F(DstMem | SrcNone | Lock, em_dec),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003637 I(SrcMem | Stack, em_grp45),
3638 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3639 I(SrcMem | Stack, em_grp45),
3640 I(SrcMemFAddr | ImplicitOps, em_grp45),
Gleb Natapov188424b2013-04-11 12:32:14 +03003641 I(SrcMem | Stack, em_grp45), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003642};
3643
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003644static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003645 DI(Prot, sldt),
3646 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003647 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003648 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003649 N, N, N, N,
3650};
3651
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003652static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03003653 II(Mov | DstMem, em_sgdt, sgdt),
3654 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003655 II(SrcMem | Priv, em_lgdt, lgdt),
3656 II(SrcMem | Priv, em_lidt, lidt),
3657 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3658 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3659 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003660}, {
Borislav Petkovb51e9742013-09-22 16:44:52 +02003661 I(SrcNone | Priv | EmulateOnUD, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003662 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003663 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003664 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3665 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3666 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003667} };
3668
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003669static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003670 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003671 F(DstMem | SrcImmByte | NoWrite, em_bt),
3672 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3673 F(DstMem | SrcImmByte | Lock, em_btr),
3674 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003675};
3676
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003677static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003678 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003679}, {
3680 N, N, N, N, N, N, N, N,
3681} };
3682
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003683static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003684 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003685 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003686};
3687
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003688static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003689 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003690};
3691
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003692static const struct gprefix pfx_vmovntpx = {
Avi Kivity3e114eb2012-04-09 18:40:01 +03003693 I(0, em_mov), N, N, N,
3694};
3695
Igor Mammedov27ce8252014-03-15 21:01:59 +01003696static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01003697 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003698};
3699
Gleb Natapov045a2822012-12-20 16:57:43 +02003700static const struct escape escape_d9 = { {
3701 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3702}, {
3703 /* 0xC0 - 0xC7 */
3704 N, N, N, N, N, N, N, N,
3705 /* 0xC8 - 0xCF */
3706 N, N, N, N, N, N, N, N,
3707 /* 0xD0 - 0xC7 */
3708 N, N, N, N, N, N, N, N,
3709 /* 0xD8 - 0xDF */
3710 N, N, N, N, N, N, N, N,
3711 /* 0xE0 - 0xE7 */
3712 N, N, N, N, N, N, N, N,
3713 /* 0xE8 - 0xEF */
3714 N, N, N, N, N, N, N, N,
3715 /* 0xF0 - 0xF7 */
3716 N, N, N, N, N, N, N, N,
3717 /* 0xF8 - 0xFF */
3718 N, N, N, N, N, N, N, N,
3719} };
3720
3721static const struct escape escape_db = { {
3722 N, N, N, N, N, N, N, N,
3723}, {
3724 /* 0xC0 - 0xC7 */
3725 N, N, N, N, N, N, N, N,
3726 /* 0xC8 - 0xCF */
3727 N, N, N, N, N, N, N, N,
3728 /* 0xD0 - 0xC7 */
3729 N, N, N, N, N, N, N, N,
3730 /* 0xD8 - 0xDF */
3731 N, N, N, N, N, N, N, N,
3732 /* 0xE0 - 0xE7 */
3733 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3734 /* 0xE8 - 0xEF */
3735 N, N, N, N, N, N, N, N,
3736 /* 0xF0 - 0xF7 */
3737 N, N, N, N, N, N, N, N,
3738 /* 0xF8 - 0xFF */
3739 N, N, N, N, N, N, N, N,
3740} };
3741
3742static const struct escape escape_dd = { {
3743 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3744}, {
3745 /* 0xC0 - 0xC7 */
3746 N, N, N, N, N, N, N, N,
3747 /* 0xC8 - 0xCF */
3748 N, N, N, N, N, N, N, N,
3749 /* 0xD0 - 0xC7 */
3750 N, N, N, N, N, N, N, N,
3751 /* 0xD8 - 0xDF */
3752 N, N, N, N, N, N, N, N,
3753 /* 0xE0 - 0xE7 */
3754 N, N, N, N, N, N, N, N,
3755 /* 0xE8 - 0xEF */
3756 N, N, N, N, N, N, N, N,
3757 /* 0xF0 - 0xF7 */
3758 N, N, N, N, N, N, N, N,
3759 /* 0xF8 - 0xFF */
3760 N, N, N, N, N, N, N, N,
3761} };
3762
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003763static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003764 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003765 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003766 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3767 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003768 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003769 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003770 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3771 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003772 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003773 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003774 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3775 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003776 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003777 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003778 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3779 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003780 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003781 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003782 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003783 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003784 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003785 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003786 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003787 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003788 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02003789 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003790 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003791 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003792 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003793 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003794 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003795 I(ImplicitOps | Stack | No64, em_pusha),
3796 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003797 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3798 N, N, N, N,
3799 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003800 I(SrcImm | Mov | Stack, em_push),
3801 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003802 I(SrcImmByte | Mov | Stack, em_push),
3803 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003804 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003805 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003806 /* 0x70 - 0x7F */
3807 X16(D(SrcImmByte)),
3808 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003809 G(ByteOp | DstMem | SrcImm, group1),
3810 G(DstMem | SrcImm, group1),
3811 G(ByteOp | DstMem | SrcImm | No64, group1),
3812 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003813 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003814 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003815 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003816 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003817 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003818 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003819 D(ModRM | SrcMem | NoAccess | DstReg),
3820 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3821 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003822 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003823 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003824 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003825 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003826 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003827 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01003828 II(ImplicitOps | Stack, em_popf, popf),
3829 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003830 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003831 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003832 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003833 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003834 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003835 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003836 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003837 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3838 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003839 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003840 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003841 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003842 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02003843 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003844 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003845 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003846 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003847 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003848 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3849 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003850 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003851 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003852 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Bruce Rogers32611072013-09-09 09:40:20 -06003853 I(ImplicitOps | Stack | SrcImmU16, em_ret_far_imm),
3854 I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003855 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003856 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003857 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003858 G(Src2One | ByteOp, group2), G(Src2One, group2),
3859 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003860 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02003861 I(DstAcc | SrcImmUByte | No64, em_aad),
3862 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02003863 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003864 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02003865 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003866 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003867 X3(I(SrcImmByte, em_loop)),
3868 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003869 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3870 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003871 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003872 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003873 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003874 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3875 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003876 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003877 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003878 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3879 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003880 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003881 D(ImplicitOps), D(ImplicitOps),
3882 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003883 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3884};
3885
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003886static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003887 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003888 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02003889 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003890 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003891 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003892 N, D(ImplicitOps | ModRM), N, N,
3893 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02003894 N, N, N, N, N, N, N, N,
3895 D(ImplicitOps | ModRM), N, N, N, N, N, N, D(ImplicitOps | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003896 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03003897 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
3898 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
3899 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
3900 check_cr_write),
3901 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
3902 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003903 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003904 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
3905 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
3906 N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
Avi Kivity3e114eb2012-04-09 18:40:01 +03003907 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003908 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003909 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02003910 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003911 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02003912 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003913 I(ImplicitOps | EmulateOnUD, em_sysenter),
3914 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003915 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003916 N, N, N, N, N, N, N, N,
3917 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03003918 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003919 /* 0x50 - 0x5F */
3920 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3921 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003922 N, N, N, N,
3923 N, N, N, N,
3924 N, N, N, N,
3925 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003926 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003927 N, N, N, N,
3928 N, N, N, N,
3929 N, N, N, N,
3930 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003931 /* 0x80 - 0x8F */
3932 X16(D(SrcImm)),
3933 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003934 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003935 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003936 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02003937 II(ImplicitOps, em_cpuid, cpuid),
3938 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02003939 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
3940 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003941 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003942 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003943 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02003944 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02003945 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
3946 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Avi Kivity4d758342013-01-19 19:51:55 +02003947 D(ModRM), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003948 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09003949 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03003950 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02003951 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03003952 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
3953 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003954 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003955 /* 0xB8 - 0xBF */
3956 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003957 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02003958 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
3959 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003960 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03003961 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02003962 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003963 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003964 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03003965 /* 0xC8 - 0xCF */
3966 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003967 /* 0xD0 - 0xDF */
3968 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3969 /* 0xE0 - 0xEF */
3970 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3971 /* 0xF0 - 0xFF */
3972 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3973};
3974
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003975static const struct gprefix three_byte_0f_38_f0 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01003976 I(DstReg | SrcMem | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003977};
3978
3979static const struct gprefix three_byte_0f_38_f1 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01003980 I(DstMem | SrcReg | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003981};
3982
3983/*
3984 * Insns below are selected by the prefix which indexed by the third opcode
3985 * byte.
3986 */
3987static const struct opcode opcode_map_0f_38[256] = {
3988 /* 0x00 - 0x7f */
3989 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01003990 /* 0x80 - 0xef */
3991 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
3992 /* 0xf0 - 0xf1 */
3993 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f0),
3994 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f1),
3995 /* 0xf2 - 0xff */
3996 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003997};
3998
Avi Kivity73fba5f2010-07-29 15:11:53 +03003999#undef D
4000#undef N
4001#undef G
4002#undef GD
4003#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004004#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004005#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004006
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004007#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004008#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004009#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004010#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004011#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004012
Avi Kivity9dac77f2011-06-01 15:34:25 +03004013static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004014{
4015 unsigned size;
4016
Avi Kivity9dac77f2011-06-01 15:34:25 +03004017 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004018 if (size == 8)
4019 size = 4;
4020 return size;
4021}
4022
4023static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4024 unsigned size, bool sign_extension)
4025{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004026 int rc = X86EMUL_CONTINUE;
4027
4028 op->type = OP_IMM;
4029 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004030 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004031 /* NB. Immediates are sign-extended as necessary. */
4032 switch (op->bytes) {
4033 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004034 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004035 break;
4036 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004037 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004038 break;
4039 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004040 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004041 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004042 case 8:
4043 op->val = insn_fetch(s64, ctxt);
4044 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004045 }
4046 if (!sign_extension) {
4047 switch (op->bytes) {
4048 case 1:
4049 op->val &= 0xff;
4050 break;
4051 case 2:
4052 op->val &= 0xffff;
4053 break;
4054 case 4:
4055 op->val &= 0xffffffff;
4056 break;
4057 }
4058 }
4059done:
4060 return rc;
4061}
4062
Avi Kivitya9945542011-09-13 10:45:41 +03004063static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4064 unsigned d)
4065{
4066 int rc = X86EMUL_CONTINUE;
4067
4068 switch (d) {
4069 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004070 decode_register_operand(ctxt, op);
Avi Kivitya9945542011-09-13 10:45:41 +03004071 break;
4072 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004073 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03004074 break;
4075 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004076 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004077 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03004078 *op = ctxt->memop;
4079 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004080 if (ctxt->d & BitOp)
Avi Kivitya9945542011-09-13 10:45:41 +03004081 fetch_bit_operand(ctxt);
4082 op->orig_val = op->val;
4083 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004084 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004085 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004086 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03004087 case OpAcc:
4088 op->type = OP_REG;
4089 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004090 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya9945542011-09-13 10:45:41 +03004091 fetch_register_operand(op);
4092 op->orig_val = op->val;
4093 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004094 case OpAccLo:
4095 op->type = OP_REG;
4096 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4097 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4098 fetch_register_operand(op);
4099 op->orig_val = op->val;
4100 break;
4101 case OpAccHi:
4102 if (ctxt->d & ByteOp) {
4103 op->type = OP_NONE;
4104 break;
4105 }
4106 op->type = OP_REG;
4107 op->bytes = ctxt->op_bytes;
4108 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4109 fetch_register_operand(op);
4110 op->orig_val = op->val;
4111 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004112 case OpDI:
4113 op->type = OP_MEM;
4114 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4115 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004116 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya9945542011-09-13 10:45:41 +03004117 op->addr.mem.seg = VCPU_SREG_ES;
4118 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004119 op->count = 1;
Avi Kivitya9945542011-09-13 10:45:41 +03004120 break;
4121 case OpDX:
4122 op->type = OP_REG;
4123 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004124 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya9945542011-09-13 10:45:41 +03004125 fetch_register_operand(op);
4126 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004127 case OpCL:
4128 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004129 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004130 break;
4131 case OpImmByte:
4132 rc = decode_imm(ctxt, op, 1, true);
4133 break;
4134 case OpOne:
4135 op->bytes = 1;
4136 op->val = 1;
4137 break;
4138 case OpImm:
4139 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4140 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004141 case OpImm64:
4142 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4143 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004144 case OpMem8:
4145 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004146 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004147 ctxt->memop.addr.reg = decode_register(ctxt,
4148 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004149 fetch_register_operand(&ctxt->memop);
4150 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004151 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004152 case OpMem16:
4153 ctxt->memop.bytes = 2;
4154 goto mem_common;
4155 case OpMem32:
4156 ctxt->memop.bytes = 4;
4157 goto mem_common;
4158 case OpImmU16:
4159 rc = decode_imm(ctxt, op, 2, false);
4160 break;
4161 case OpImmU:
4162 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4163 break;
4164 case OpSI:
4165 op->type = OP_MEM;
4166 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4167 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004168 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Bandan Das573e80f2014-04-16 12:46:13 -04004169 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03004170 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004171 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004172 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004173 case OpXLat:
4174 op->type = OP_MEM;
4175 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4176 op->addr.mem.ea =
4177 register_address(ctxt,
4178 reg_read(ctxt, VCPU_REGS_RBX) +
4179 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04004180 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004181 op->val = 0;
4182 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004183 case OpImmFAddr:
4184 op->type = OP_IMM;
4185 op->addr.mem.ea = ctxt->_eip;
4186 op->bytes = ctxt->op_bytes + 2;
4187 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4188 break;
4189 case OpMemFAddr:
4190 ctxt->memop.bytes = ctxt->op_bytes + 2;
4191 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004192 case OpES:
4193 op->val = VCPU_SREG_ES;
4194 break;
4195 case OpCS:
4196 op->val = VCPU_SREG_CS;
4197 break;
4198 case OpSS:
4199 op->val = VCPU_SREG_SS;
4200 break;
4201 case OpDS:
4202 op->val = VCPU_SREG_DS;
4203 break;
4204 case OpFS:
4205 op->val = VCPU_SREG_FS;
4206 break;
4207 case OpGS:
4208 op->val = VCPU_SREG_GS;
4209 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004210 case OpImplicit:
4211 /* Special instructions do their own operand decoding. */
4212 default:
4213 op->type = OP_NONE; /* Disable writeback. */
4214 break;
4215 }
4216
4217done:
4218 return rc;
4219}
4220
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004221int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004222{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004223 int rc = X86EMUL_CONTINUE;
4224 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004225 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004226 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04004227 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03004228 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004229
Avi Kivityf09ed832011-09-13 10:45:40 +03004230 ctxt->memop.type = OP_NONE;
4231 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004232 ctxt->_eip = ctxt->eip;
4233 ctxt->fetch.start = ctxt->_eip;
4234 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004235 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01004236 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004237 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004238 else {
Paolo Bonzini5cfc7e02014-05-06 13:05:25 +02004239 rc = do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004240 if (rc != X86EMUL_CONTINUE)
4241 return rc;
4242 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004243
4244 switch (mode) {
4245 case X86EMUL_MODE_REAL:
4246 case X86EMUL_MODE_VM86:
4247 case X86EMUL_MODE_PROT16:
4248 def_op_bytes = def_ad_bytes = 2;
4249 break;
4250 case X86EMUL_MODE_PROT32:
4251 def_op_bytes = def_ad_bytes = 4;
4252 break;
4253#ifdef CONFIG_X86_64
4254 case X86EMUL_MODE_PROT64:
4255 def_op_bytes = 4;
4256 def_ad_bytes = 8;
4257 break;
4258#endif
4259 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004260 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004261 }
4262
Avi Kivity9dac77f2011-06-01 15:34:25 +03004263 ctxt->op_bytes = def_op_bytes;
4264 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004265
4266 /* Legacy prefixes. */
4267 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004268 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004269 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004270 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004271 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004272 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004273 break;
4274 case 0x67: /* address-size override */
4275 if (mode == X86EMUL_MODE_PROT64)
4276 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004277 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004278 else
4279 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004280 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004281 break;
4282 case 0x26: /* ES override */
4283 case 0x2e: /* CS override */
4284 case 0x36: /* SS override */
4285 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004286 has_seg_override = true;
4287 ctxt->seg_override = (ctxt->b >> 3) & 3;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004288 break;
4289 case 0x64: /* FS override */
4290 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004291 has_seg_override = true;
4292 ctxt->seg_override = ctxt->b & 7;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004293 break;
4294 case 0x40 ... 0x4f: /* REX */
4295 if (mode != X86EMUL_MODE_PROT64)
4296 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004297 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004298 continue;
4299 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004300 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004301 break;
4302 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004303 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004304 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004305 break;
4306 default:
4307 goto done_prefixes;
4308 }
4309
4310 /* Any legacy prefix after a REX prefix nullifies its effect. */
4311
Avi Kivity9dac77f2011-06-01 15:34:25 +03004312 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004313 }
4314
4315done_prefixes:
4316
4317 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004318 if (ctxt->rex_prefix & 8)
4319 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004320
4321 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004322 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004323 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004324 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004325 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004326 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004327 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004328
4329 /* 0F_38 opcode map */
4330 if (ctxt->b == 0x38) {
4331 ctxt->opcode_len = 3;
4332 ctxt->b = insn_fetch(u8, ctxt);
4333 opcode = opcode_map_0f_38[ctxt->b];
4334 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004335 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004336 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004337
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004338 if (ctxt->d & ModRM)
4339 ctxt->modrm = insn_fetch(u8, ctxt);
4340
Nadav Amit7fe864d2014-06-02 18:34:03 +03004341 /* vex-prefix instructions are not implemented */
4342 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
4343 (mode == X86EMUL_MODE_PROT64 ||
4344 (mode >= X86EMUL_MODE_PROT16 && (ctxt->modrm & 0x80)))) {
4345 ctxt->d = NotImpl;
4346 }
4347
Avi Kivity9dac77f2011-06-01 15:34:25 +03004348 while (ctxt->d & GroupMask) {
4349 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004350 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004351 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004352 opcode = opcode.u.group[goffset];
4353 break;
4354 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004355 goffset = (ctxt->modrm >> 3) & 7;
4356 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004357 opcode = opcode.u.gdual->mod3[goffset];
4358 else
4359 opcode = opcode.u.gdual->mod012[goffset];
4360 break;
4361 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004362 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004363 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004364 break;
4365 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004366 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004367 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004368 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004369 switch (simd_prefix) {
4370 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4371 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4372 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4373 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4374 }
4375 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004376 case Escape:
4377 if (ctxt->modrm > 0xbf)
4378 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4379 else
4380 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4381 break;
Avi Kivity46561642011-04-24 14:09:59 +03004382 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004383 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004384 }
Avi Kivity46561642011-04-24 14:09:59 +03004385
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004386 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004387 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004388 }
4389
Paolo Bonzinie24186e2014-03-27 12:00:57 +01004390 /* Unrecognised? */
4391 if (ctxt->d == 0)
4392 return EMULATION_FAILED;
4393
Avi Kivity9dac77f2011-06-01 15:34:25 +03004394 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004395
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004396 if (unlikely(ctxt->d &
4397 (NotImpl|EmulateOnUD|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm))) {
4398 /*
4399 * These are copied unconditionally here, and checked unconditionally
4400 * in x86_emulate_insn.
4401 */
4402 ctxt->check_perm = opcode.check_perm;
4403 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004404
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004405 if (ctxt->d & NotImpl)
4406 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004407
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004408 if (!(ctxt->d & EmulateOnUD) && ctxt->ud)
4409 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004410
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004411 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004412 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004413
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004414 if (ctxt->d & Op3264) {
4415 if (mode == X86EMUL_MODE_PROT64)
4416 ctxt->op_bytes = 8;
4417 else
4418 ctxt->op_bytes = 4;
4419 }
4420
4421 if (ctxt->d & Sse)
4422 ctxt->op_bytes = 16;
4423 else if (ctxt->d & Mmx)
4424 ctxt->op_bytes = 8;
4425 }
Avi Kivity12537912011-03-29 11:41:27 +02004426
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004427 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004428 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004429 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04004430 if (!has_seg_override) {
4431 has_seg_override = true;
4432 ctxt->seg_override = ctxt->modrm_seg;
4433 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004434 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004435 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004436 if (rc != X86EMUL_CONTINUE)
4437 goto done;
4438
Bandan Das573e80f2014-04-16 12:46:13 -04004439 if (!has_seg_override)
4440 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004441
Bandan Das573e80f2014-04-16 12:46:13 -04004442 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004443
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004444 /*
4445 * Decode and fetch the source operand: register, memory
4446 * or immediate.
4447 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004448 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004449 if (rc != X86EMUL_CONTINUE)
4450 goto done;
4451
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004452 /*
4453 * Decode and fetch the second source operand: register, memory
4454 * or immediate.
4455 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004456 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004457 if (rc != X86EMUL_CONTINUE)
4458 goto done;
4459
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004460 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03004461 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004462
4463done:
Bandan Das41061cd2014-04-16 12:46:14 -04004464 if (ctxt->rip_relative)
Avi Kivityf09ed832011-09-13 10:45:40 +03004465 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004466
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004467 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004468}
4469
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004470bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4471{
4472 return ctxt->d & PageTable;
4473}
4474
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004475static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4476{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004477 /* The second termination condition only applies for REPE
4478 * and REPNE. Test if the repeat string operation prefix is
4479 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4480 * corresponding termination condition according to:
4481 * - if REPE/REPZ and ZF = 0 then done
4482 * - if REPNE/REPNZ and ZF = 1 then done
4483 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004484 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4485 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4486 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004487 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004488 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004489 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4490 return true;
4491
4492 return false;
4493}
4494
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004495static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4496{
4497 bool fault = false;
4498
4499 ctxt->ops->get_fpu(ctxt);
4500 asm volatile("1: fwait \n\t"
4501 "2: \n\t"
4502 ".pushsection .fixup,\"ax\" \n\t"
4503 "3: \n\t"
4504 "movb $1, %[fault] \n\t"
4505 "jmp 2b \n\t"
4506 ".popsection \n\t"
4507 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004508 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004509 ctxt->ops->put_fpu(ctxt);
4510
4511 if (unlikely(fault))
4512 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4513
4514 return X86EMUL_CONTINUE;
4515}
4516
4517static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4518 struct operand *op)
4519{
4520 if (op->type == OP_MM)
4521 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4522}
4523
Avi Kivitye28bbd42013-01-04 16:18:48 +02004524static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4525{
4526 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivityb9fa4092013-02-09 11:31:48 +02004527 if (!(ctxt->d & ByteOp))
4528 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Avi Kivitye28bbd42013-01-04 16:18:48 +02004529 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004530 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
4531 [fastop]"+S"(fop)
4532 : "c"(ctxt->src2.val));
Avi Kivitye28bbd42013-01-04 16:18:48 +02004533 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004534 if (!fop) /* exception is returned in fop variable */
4535 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02004536 return X86EMUL_CONTINUE;
4537}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004538
Bandan Das14985072014-04-16 12:46:09 -04004539void init_decode_cache(struct x86_emulate_ctxt *ctxt)
4540{
Bandan Das573e80f2014-04-16 12:46:13 -04004541 memset(&ctxt->rip_relative, 0,
4542 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04004543
Bandan Das14985072014-04-16 12:46:09 -04004544 ctxt->io_read.pos = 0;
4545 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04004546 ctxt->mem_read.end = 0;
4547}
4548
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004549int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004550{
Mathias Krause0225fb52012-08-30 01:30:16 +02004551 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004552 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004553 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004554
Avi Kivity9dac77f2011-06-01 15:34:25 +03004555 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004556
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004557 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004558 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004559 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004560 goto done;
4561 }
4562
Avi Kivity9dac77f2011-06-01 15:34:25 +03004563 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004564 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004565 goto done;
4566 }
4567
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004568 if (unlikely(ctxt->d &
4569 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
4570 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4571 (ctxt->d & Undefined)) {
4572 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004573 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004574 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004575
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004576 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4577 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
4578 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004579 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004580 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004581
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004582 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
4583 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004584 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004585 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004586
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004587 if (ctxt->d & Mmx) {
4588 rc = flush_pending_x87_faults(ctxt);
4589 if (rc != X86EMUL_CONTINUE)
4590 goto done;
4591 /*
4592 * Now that we know the fpu is exception safe, we can fetch
4593 * operands from it.
4594 */
4595 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4596 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4597 if (!(ctxt->d & Mov))
4598 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4599 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004600
Bandan Das685bbf42014-04-16 12:46:10 -04004601 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004602 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4603 X86_ICPT_PRE_EXCEPT);
4604 if (rc != X86EMUL_CONTINUE)
4605 goto done;
4606 }
4607
4608 /* Privileged instruction can be executed only in CPL=0 */
4609 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
4610 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004611 goto done;
4612 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004613
4614 /* Instruction can only be executed in protected mode */
4615 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
4616 rc = emulate_ud(ctxt);
4617 goto done;
4618 }
4619
4620 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04004621 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004622 rc = ctxt->check_perm(ctxt);
4623 if (rc != X86EMUL_CONTINUE)
4624 goto done;
4625 }
4626
Bandan Das685bbf42014-04-16 12:46:10 -04004627 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004628 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4629 X86_ICPT_POST_EXCEPT);
4630 if (rc != X86EMUL_CONTINUE)
4631 goto done;
4632 }
4633
4634 if (ctxt->rep_prefix && (ctxt->d & String)) {
4635 /* All REP prefixes have the same first termination condition */
4636 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
4637 ctxt->eip = ctxt->_eip;
4638 goto done;
4639 }
4640 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004641 }
4642
Avi Kivity9dac77f2011-06-01 15:34:25 +03004643 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4644 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4645 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004646 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004647 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004648 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004649 }
4650
Avi Kivity9dac77f2011-06-01 15:34:25 +03004651 if (ctxt->src2.type == OP_MEM) {
4652 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4653 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004654 if (rc != X86EMUL_CONTINUE)
4655 goto done;
4656 }
4657
Avi Kivity9dac77f2011-06-01 15:34:25 +03004658 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004659 goto special_insn;
4660
4661
Avi Kivity9dac77f2011-06-01 15:34:25 +03004662 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004663 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004664 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4665 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004666 if (rc != X86EMUL_CONTINUE)
4667 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004668 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004669 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004670
Avi Kivity018a98d2007-11-27 19:30:56 +02004671special_insn:
4672
Bandan Das685bbf42014-04-16 12:46:10 -04004673 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004674 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004675 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004676 if (rc != X86EMUL_CONTINUE)
4677 goto done;
4678 }
4679
Avi Kivity9dac77f2011-06-01 15:34:25 +03004680 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004681 if (ctxt->d & Fastop) {
4682 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4683 rc = fastop(ctxt, fop);
4684 if (rc != X86EMUL_CONTINUE)
4685 goto done;
4686 goto writeback;
4687 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004688 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004689 if (rc != X86EMUL_CONTINUE)
4690 goto done;
4691 goto writeback;
4692 }
4693
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004694 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004695 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004696 else if (ctxt->opcode_len == 3)
4697 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004698
Avi Kivity9dac77f2011-06-01 15:34:25 +03004699 switch (ctxt->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004700 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004701 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004702 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004703 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004704 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004705 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004706 if (test_cc(ctxt->b, ctxt->eflags))
4707 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004708 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004709 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004710 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004711 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004712 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004713 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03004714 ctxt->dst.type = OP_NONE;
4715 else
4716 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004717 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004718 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004719 switch (ctxt->op_bytes) {
4720 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4721 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4722 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004723 }
4724 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004725 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004726 rc = emulate_int(ctxt, 3);
4727 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004728 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004729 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004730 break;
4731 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004732 if (ctxt->eflags & EFLG_OF)
4733 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004734 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004735 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004736 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004737 jmp_rel(ctxt, ctxt->src.val);
4738 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004739 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004740 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004741 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004742 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004743 case 0xf5: /* cmc */
4744 /* complement carry flag from eflags reg */
4745 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004746 break;
4747 case 0xf8: /* clc */
4748 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004749 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004750 case 0xf9: /* stc */
4751 ctxt->eflags |= EFLG_CF;
4752 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004753 case 0xfc: /* cld */
4754 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004755 break;
4756 case 0xfd: /* std */
4757 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004758 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004759 default:
4760 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004761 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004762
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004763 if (rc != X86EMUL_CONTINUE)
4764 goto done;
4765
Avi Kivity018a98d2007-11-27 19:30:56 +02004766writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02004767 if (ctxt->d & SrcWrite) {
4768 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
4769 rc = writeback(ctxt, &ctxt->src);
4770 if (rc != X86EMUL_CONTINUE)
4771 goto done;
4772 }
Nadav Amitee212292014-06-15 16:12:58 +03004773 if (!(ctxt->d & NoWrite)) {
4774 rc = writeback(ctxt, &ctxt->dst);
4775 if (rc != X86EMUL_CONTINUE)
4776 goto done;
4777 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004778
Gleb Natapov5cd21912010-03-18 15:20:26 +02004779 /*
4780 * restore dst type in case the decoding will be reused
4781 * (happens for string instruction )
4782 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004783 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004784
Avi Kivity9dac77f2011-06-01 15:34:25 +03004785 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004786 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004787
Avi Kivity9dac77f2011-06-01 15:34:25 +03004788 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004789 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004790
Avi Kivity9dac77f2011-06-01 15:34:25 +03004791 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004792 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004793 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004794 if ((ctxt->d & SrcMask) == SrcSI)
4795 count = ctxt->src.count;
4796 else
4797 count = ctxt->dst.count;
4798 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4799 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004800
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004801 if (!string_insn_completed(ctxt)) {
4802 /*
4803 * Re-enter guest when pio read ahead buffer is empty
4804 * or, if it is not used, after each 1024 iteration.
4805 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004806 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004807 (r->end == 0 || r->end != r->pos)) {
4808 /*
4809 * Reset read cache. Usually happens before
4810 * decode, but since instruction is restarted
4811 * we have to do it here.
4812 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004813 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004814 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004815 return EMULATION_RESTART;
4816 }
4817 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004818 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004819 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004820
Avi Kivity9dac77f2011-06-01 15:34:25 +03004821 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004822
4823done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004824 if (rc == X86EMUL_PROPAGATE_FAULT)
4825 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004826 if (rc == X86EMUL_INTERCEPTED)
4827 return EMULATION_INTERCEPTED;
4828
Avi Kivitydd856ef2012-08-27 23:46:17 +03004829 if (rc == X86EMUL_CONTINUE)
4830 writeback_registers(ctxt);
4831
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004832 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004833
4834twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004835 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004836 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004837 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004838 break;
4839 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004840 case 0x0d: /* GrpP (prefetch) */
4841 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004842 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02004843 break;
4844 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004845 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004846 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004847 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004848 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004849 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004850 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03004851 if (test_cc(ctxt->b, ctxt->eflags))
4852 ctxt->dst.val = ctxt->src.val;
4853 else if (ctxt->mode != X86EMUL_MODE_PROT64 ||
4854 ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004855 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004856 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004857 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004858 if (test_cc(ctxt->b, ctxt->eflags))
4859 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004860 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004861 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004862 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004863 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004864 case 0xae: /* clflush */
4865 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004866 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004867 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004868 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004869 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004870 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004871 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004872 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004873 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004874 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004875 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004876 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004877 ctxt->dst.bytes = ctxt->op_bytes;
Nadav Amit3b320042014-06-02 18:34:08 +03004878 ctxt->dst.val = (ctxt->op_bytes == 8) ? (u64) ctxt->src.val :
4879 (u32) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004880 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004881 default:
4882 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004883 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004884
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004885threebyte_insn:
4886
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004887 if (rc != X86EMUL_CONTINUE)
4888 goto done;
4889
Avi Kivity6aa8b732006-12-10 02:21:36 -08004890 goto writeback;
4891
4892cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004893 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004894}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004895
4896void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
4897{
4898 invalidate_registers(ctxt);
4899}
4900
4901void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
4902{
4903 writeback_registers(ctxt);
4904}