blob: 439a3286406a4c641419372412bbd89e60d2284c [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 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800165
Avi Kivity820207c2013-02-09 11:31:45 +0200166#define DstXacc (DstAccLo | SrcAccHi | SrcWrite)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800167
Avi Kivityd0e53322010-07-29 15:11:54 +0300168#define X2(x...) x, x
169#define X3(x...) X2(x), x
170#define X4(x...) X2(x), X2(x)
171#define X5(x...) X4(x), x
172#define X6(x...) X4(x), X2(x)
173#define X7(x...) X4(x), X3(x)
174#define X8(x...) X4(x), X4(x)
175#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300176
Avi Kivitye28bbd42013-01-04 16:18:48 +0200177#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
178#define FASTOP_SIZE 8
179
180/*
181 * fastop functions have a special calling convention:
182 *
Avi Kivity017da7b2013-02-09 11:31:47 +0200183 * dst: rax (in/out)
184 * src: rdx (in/out)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200185 * src2: rcx (in)
186 * flags: rflags (in/out)
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200187 * ex: rsi (in:fastop pointer, out:zero if exception)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200188 *
189 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
190 * different operand sizes can be reached by calculation, rather than a jump
191 * table (which would be bigger than the code).
192 *
193 * fastop functions are declared as taking a never-defined fastop parameter,
194 * so they can't be called from C directly.
195 */
196
197struct fastop;
198
Avi Kivityd65b1de2010-07-29 15:11:35 +0300199struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300200 u64 flags : 56;
201 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300202 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300203 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200204 const struct opcode *group;
205 const struct group_dual *gdual;
206 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200207 const struct escape *esc;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200208 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300209 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200210 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300211};
212
213struct group_dual {
214 struct opcode mod012[8];
215 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300216};
217
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200218struct gprefix {
219 struct opcode pfx_no;
220 struct opcode pfx_66;
221 struct opcode pfx_f2;
222 struct opcode pfx_f3;
223};
224
Gleb Natapov045a2822012-12-20 16:57:43 +0200225struct escape {
226 struct opcode op[8];
227 struct opcode high[64];
228};
229
Avi Kivity6aa8b732006-12-10 02:21:36 -0800230/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200231#define EFLG_ID (1<<21)
232#define EFLG_VIP (1<<20)
233#define EFLG_VIF (1<<19)
234#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200235#define EFLG_VM (1<<17)
236#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200237#define EFLG_IOPL (3<<12)
238#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800239#define EFLG_OF (1<<11)
240#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200241#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200242#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800243#define EFLG_SF (1<<7)
244#define EFLG_ZF (1<<6)
245#define EFLG_AF (1<<4)
246#define EFLG_PF (1<<2)
247#define EFLG_CF (1<<0)
248
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300249#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
250#define EFLG_RESERVED_ONE_MASK 2
251
Avi Kivitydd856ef2012-08-27 23:46:17 +0300252static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
253{
254 if (!(ctxt->regs_valid & (1 << nr))) {
255 ctxt->regs_valid |= 1 << nr;
256 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
257 }
258 return ctxt->_regs[nr];
259}
260
261static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
262{
263 ctxt->regs_valid |= 1 << nr;
264 ctxt->regs_dirty |= 1 << nr;
265 return &ctxt->_regs[nr];
266}
267
268static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
269{
270 reg_read(ctxt, nr);
271 return reg_write(ctxt, nr);
272}
273
274static void writeback_registers(struct x86_emulate_ctxt *ctxt)
275{
276 unsigned reg;
277
278 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
279 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
280}
281
282static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
283{
284 ctxt->regs_dirty = 0;
285 ctxt->regs_valid = 0;
286}
287
Avi Kivity6aa8b732006-12-10 02:21:36 -0800288/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800289 * These EFLAGS bits are restored from saved value during emulation, and
290 * any changes are written back to the saved value after emulation.
291 */
292#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
293
Avi Kivitydda96d82008-11-26 15:14:10 +0200294#ifdef CONFIG_X86_64
295#define ON64(x) x
296#else
297#define ON64(x)
298#endif
299
Avi Kivity4d758342013-01-19 19:51:55 +0200300static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *));
301
Avi Kivityb7d491e2013-01-04 16:18:49 +0200302#define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t"
303#define FOP_RET "ret \n\t"
304
305#define FOP_START(op) \
306 extern void em_##op(struct fastop *fake); \
307 asm(".pushsection .text, \"ax\" \n\t" \
308 ".global em_" #op " \n\t" \
309 FOP_ALIGN \
310 "em_" #op ": \n\t"
311
312#define FOP_END \
313 ".popsection")
314
Avi Kivity0bdea062013-01-19 19:51:50 +0200315#define FOPNOP() FOP_ALIGN FOP_RET
316
Avi Kivityb7d491e2013-01-04 16:18:49 +0200317#define FOP1E(op, dst) \
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200318 FOP_ALIGN "10: " #op " %" #dst " \n\t" FOP_RET
319
320#define FOP1EEX(op, dst) \
321 FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200322
323#define FASTOP1(op) \
324 FOP_START(op) \
325 FOP1E(op##b, al) \
326 FOP1E(op##w, ax) \
327 FOP1E(op##l, eax) \
328 ON64(FOP1E(op##q, rax)) \
329 FOP_END
330
Avi Kivityb9fa4092013-02-09 11:31:48 +0200331/* 1-operand, using src2 (for MUL/DIV r/m) */
332#define FASTOP1SRC2(op, name) \
333 FOP_START(name) \
334 FOP1E(op, cl) \
335 FOP1E(op, cx) \
336 FOP1E(op, ecx) \
337 ON64(FOP1E(op, rcx)) \
338 FOP_END
339
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200340/* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */
341#define FASTOP1SRC2EX(op, name) \
342 FOP_START(name) \
343 FOP1EEX(op, cl) \
344 FOP1EEX(op, cx) \
345 FOP1EEX(op, ecx) \
346 ON64(FOP1EEX(op, rcx)) \
347 FOP_END
348
Avi Kivityf7857f32013-01-04 16:18:53 +0200349#define FOP2E(op, dst, src) \
350 FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET
351
352#define FASTOP2(op) \
353 FOP_START(op) \
Avi Kivity017da7b2013-02-09 11:31:47 +0200354 FOP2E(op##b, al, dl) \
355 FOP2E(op##w, ax, dx) \
356 FOP2E(op##l, eax, edx) \
357 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivityf7857f32013-01-04 16:18:53 +0200358 FOP_END
359
Avi Kivity11c363b2013-01-19 19:51:54 +0200360/* 2 operand, word only */
361#define FASTOP2W(op) \
362 FOP_START(op) \
363 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200364 FOP2E(op##w, ax, dx) \
365 FOP2E(op##l, eax, edx) \
366 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivity11c363b2013-01-19 19:51:54 +0200367 FOP_END
368
Avi Kivity007a3b52013-01-19 19:51:51 +0200369/* 2 operand, src is CL */
370#define FASTOP2CL(op) \
371 FOP_START(op) \
372 FOP2E(op##b, al, cl) \
373 FOP2E(op##w, ax, cl) \
374 FOP2E(op##l, eax, cl) \
375 ON64(FOP2E(op##q, rax, cl)) \
376 FOP_END
377
Avi Kivity0bdea062013-01-19 19:51:50 +0200378#define FOP3E(op, dst, src, src2) \
379 FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
380
381/* 3-operand, word-only, src2=cl */
382#define FASTOP3WCL(op) \
383 FOP_START(op) \
384 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200385 FOP3E(op##w, ax, dx, cl) \
386 FOP3E(op##l, eax, edx, cl) \
387 ON64(FOP3E(op##q, rax, rdx, cl)) \
Avi Kivity0bdea062013-01-19 19:51:50 +0200388 FOP_END
389
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200390/* Special case for SETcc - 1 instruction per cc */
391#define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t"
392
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200393asm(".global kvm_fastop_exception \n"
394 "kvm_fastop_exception: xor %esi, %esi; ret");
395
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200396FOP_START(setcc)
397FOP_SETCC(seto)
398FOP_SETCC(setno)
399FOP_SETCC(setc)
400FOP_SETCC(setnc)
401FOP_SETCC(setz)
402FOP_SETCC(setnz)
403FOP_SETCC(setbe)
404FOP_SETCC(setnbe)
405FOP_SETCC(sets)
406FOP_SETCC(setns)
407FOP_SETCC(setp)
408FOP_SETCC(setnp)
409FOP_SETCC(setl)
410FOP_SETCC(setnl)
411FOP_SETCC(setle)
412FOP_SETCC(setnle)
413FOP_END;
414
Paolo Bonzini326f5782013-05-09 11:32:51 +0200415FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET
416FOP_END;
417
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200418static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
419 enum x86_intercept intercept,
420 enum x86_intercept_stage stage)
421{
422 struct x86_instruction_info info = {
423 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300424 .rep_prefix = ctxt->rep_prefix,
425 .modrm_mod = ctxt->modrm_mod,
426 .modrm_reg = ctxt->modrm_reg,
427 .modrm_rm = ctxt->modrm_rm,
428 .src_val = ctxt->src.val64,
429 .src_bytes = ctxt->src.bytes,
430 .dst_bytes = ctxt->dst.bytes,
431 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200432 .next_rip = ctxt->eip,
433 };
434
Avi Kivity29535382011-04-20 13:37:53 +0300435 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200436}
437
Avi Kivityf47cfa32012-06-07 17:49:24 +0300438static void assign_masked(ulong *dest, ulong src, ulong mask)
439{
440 *dest = (*dest & ~mask) | (src & mask);
441}
442
Avi Kivity9dac77f2011-06-01 15:34:25 +0300443static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800444{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300445 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800446}
447
Avi Kivityf47cfa32012-06-07 17:49:24 +0300448static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
449{
450 u16 sel;
451 struct desc_struct ss;
452
453 if (ctxt->mode == X86EMUL_MODE_PROT64)
454 return ~0UL;
455 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
456 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
457}
458
Avi Kivity612e89f2012-06-12 20:03:23 +0300459static int stack_size(struct x86_emulate_ctxt *ctxt)
460{
461 return (__fls(stack_mask(ctxt)) + 1) >> 3;
462}
463
Avi Kivity6aa8b732006-12-10 02:21:36 -0800464/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800465static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300466address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800467{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300468 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800469 return reg;
470 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300471 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800472}
473
474static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300475register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800476{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300477 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800478}
479
Avi Kivity5ad105e2012-08-19 14:34:31 +0300480static void masked_increment(ulong *reg, ulong mask, int inc)
481{
482 assign_masked(reg, *reg + inc, mask);
483}
484
Harvey Harrison7a9572752008-02-19 07:40:41 -0800485static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300486register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800487{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300488 ulong mask;
489
Avi Kivity9dac77f2011-06-01 15:34:25 +0300490 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300491 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800492 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300493 mask = ad_mask(ctxt);
494 masked_increment(reg, mask, inc);
495}
496
497static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
498{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300499 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800500}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800501
Avi Kivity9dac77f2011-06-01 15:34:25 +0300502static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800503{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300504 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800505}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300506
Avi Kivity56697682011-04-03 14:08:51 +0300507static u32 desc_limit_scaled(struct desc_struct *desc)
508{
509 u32 limit = get_desc_limit(desc);
510
511 return desc->g ? (limit << 12) | 0xfff : limit;
512}
513
Avi Kivity9dac77f2011-06-01 15:34:25 +0300514static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300515{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300516 ctxt->has_seg_override = true;
517 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300518}
519
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900520static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300521{
522 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
523 return 0;
524
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900525 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300526}
527
Avi Kivity9dac77f2011-06-01 15:34:25 +0300528static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300529{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300530 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300531 return 0;
532
Avi Kivity9dac77f2011-06-01 15:34:25 +0300533 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300534}
535
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200536static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
537 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300538{
Avi Kivityda9cb572010-11-22 17:53:21 +0200539 ctxt->exception.vector = vec;
540 ctxt->exception.error_code = error;
541 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200542 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300543}
544
Joerg Roedel3b88e412011-04-04 12:39:29 +0200545static int emulate_db(struct x86_emulate_ctxt *ctxt)
546{
547 return emulate_exception(ctxt, DB_VECTOR, 0, false);
548}
549
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200550static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300551{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200552 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300553}
554
Avi Kivity618ff152011-04-03 12:32:09 +0300555static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
556{
557 return emulate_exception(ctxt, SS_VECTOR, err, true);
558}
559
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200560static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300561{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200562 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300563}
564
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200565static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300566{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200567 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300568}
569
Avi Kivity34d1f492010-08-26 11:59:01 +0300570static int emulate_de(struct x86_emulate_ctxt *ctxt)
571{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200572 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300573}
574
Avi Kivity12537912011-03-29 11:41:27 +0200575static int emulate_nm(struct x86_emulate_ctxt *ctxt)
576{
577 return emulate_exception(ctxt, NM_VECTOR, 0, false);
578}
579
Avi Kivity1aa36612011-04-27 13:20:30 +0300580static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
581{
582 u16 selector;
583 struct desc_struct desc;
584
585 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
586 return selector;
587}
588
589static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
590 unsigned seg)
591{
592 u16 dummy;
593 u32 base3;
594 struct desc_struct desc;
595
596 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
597 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
598}
599
Avi Kivity1c11b372012-04-09 18:39:59 +0300600/*
601 * x86 defines three classes of vector instructions: explicitly
602 * aligned, explicitly unaligned, and the rest, which change behaviour
603 * depending on whether they're AVX encoded or not.
604 *
605 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
606 * subject to the same check.
607 */
608static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
609{
610 if (likely(size < 16))
611 return false;
612
613 if (ctxt->d & Aligned)
614 return true;
615 else if (ctxt->d & Unaligned)
616 return false;
617 else if (ctxt->d & Avx)
618 return false;
619 else
620 return true;
621}
622
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400623static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300624 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400625 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300626 ulong *linear)
627{
Avi Kivity618ff152011-04-03 12:32:09 +0300628 struct desc_struct desc;
629 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300630 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300631 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300632 u16 sel;
Gleb Natapov3a78a4f2012-12-20 16:57:42 +0200633 unsigned cpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300634
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900635 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300636 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300637 case X86EMUL_MODE_PROT64:
638 if (((signed long)la << 16) >> 16 != la)
639 return emulate_gp(ctxt, 0);
640 break;
641 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300642 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
643 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300644 if (!usable)
645 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200646 /* code segment in protected mode or read-only data segment */
647 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
648 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300649 goto bad;
650 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400651 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300652 goto bad;
653 lim = desc_limit_scaled(&desc);
654 if ((desc.type & 8) || !(desc.type & 4)) {
655 /* expand-up segment */
656 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
657 goto bad;
658 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800659 /* expand-down segment */
Avi Kivity618ff152011-04-03 12:32:09 +0300660 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
661 goto bad;
662 lim = desc.d ? 0xffffffff : 0xffff;
663 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
664 goto bad;
665 }
Avi Kivity717746e2011-04-20 13:37:53 +0300666 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity618ff152011-04-03 12:32:09 +0300667 if (!(desc.type & 8)) {
668 /* data segment */
669 if (cpl > desc.dpl)
670 goto bad;
671 } else if ((desc.type & 8) && !(desc.type & 4)) {
672 /* nonconforming code segment */
673 if (cpl != desc.dpl)
674 goto bad;
675 } else if ((desc.type & 8) && (desc.type & 4)) {
676 /* conforming code segment */
677 if (cpl < desc.dpl)
678 goto bad;
679 }
680 break;
681 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300682 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300683 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300684 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
685 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300686 *linear = la;
687 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300688bad:
689 if (addr.seg == VCPU_SREG_SS)
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300690 return emulate_ss(ctxt, sel);
Avi Kivity618ff152011-04-03 12:32:09 +0300691 else
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300692 return emulate_gp(ctxt, sel);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300693}
694
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400695static int linearize(struct x86_emulate_ctxt *ctxt,
696 struct segmented_address addr,
697 unsigned size, bool write,
698 ulong *linear)
699{
700 return __linearize(ctxt, addr, size, write, false, linear);
701}
702
703
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200704static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
705 struct segmented_address addr,
706 void *data,
707 unsigned size)
708{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200709 int rc;
710 ulong linear;
711
Avi Kivity83b87952011-04-03 11:31:19 +0300712 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200713 if (rc != X86EMUL_CONTINUE)
714 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300715 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200716}
717
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900718/*
719 * Fetch the next byte of the instruction being emulated which is pointed to
720 * by ctxt->_eip, then increment ctxt->_eip.
721 *
722 * Also prefetch the remaining bytes of the instruction without crossing page
723 * boundary if they are not in fetch_cache yet.
724 */
725static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200726{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300727 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200728 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300729 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200730
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900731 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400732 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900733 struct segmented_address addr = { .seg = VCPU_SREG_CS,
734 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300735 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900736 size = min(15UL - cur_size,
737 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400738 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900739 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400740 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900741 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
742 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900743 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200744 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300745 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200746 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900747 *dest = fc->data[ctxt->_eip - fc->start];
748 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900749 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200750}
751
752static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900753 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200754{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900755 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200756
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200757 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900758 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200759 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200760 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900761 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900762 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200763 return rc;
764 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900765 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200766}
767
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900768/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900769#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900770({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900771 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900772 if (rc != X86EMUL_CONTINUE) \
773 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900774 (_type)_x; \
775})
776
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900777#define insn_fetch_arr(_arr, _size, _ctxt) \
778({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900779 if (rc != X86EMUL_CONTINUE) \
780 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900781})
782
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000783/*
784 * Given the 'reg' portion of a ModRM byte, and a register block, return a
785 * pointer into the block that addresses the relevant register.
786 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
787 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300788static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200789 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790{
791 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200792 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800793
Avi Kivity6aa8b732006-12-10 02:21:36 -0800794 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300795 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
796 else
797 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800798 return p;
799}
800
801static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200802 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800803 u16 *size, unsigned long *address, int op_bytes)
804{
805 int rc;
806
807 if (op_bytes == 2)
808 op_bytes = 3;
809 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200810 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900811 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800812 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200813 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200814 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800815 return rc;
816}
817
Avi Kivity34b77652013-01-19 19:51:56 +0200818FASTOP2(add);
819FASTOP2(or);
820FASTOP2(adc);
821FASTOP2(sbb);
822FASTOP2(and);
823FASTOP2(sub);
824FASTOP2(xor);
825FASTOP2(cmp);
826FASTOP2(test);
827
Avi Kivityb9fa4092013-02-09 11:31:48 +0200828FASTOP1SRC2(mul, mul_ex);
829FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200830FASTOP1SRC2EX(div, div_ex);
831FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +0200832
Avi Kivity34b77652013-01-19 19:51:56 +0200833FASTOP3WCL(shld);
834FASTOP3WCL(shrd);
835
836FASTOP2W(imul);
837
838FASTOP1(not);
839FASTOP1(neg);
840FASTOP1(inc);
841FASTOP1(dec);
842
843FASTOP2CL(rol);
844FASTOP2CL(ror);
845FASTOP2CL(rcl);
846FASTOP2CL(rcr);
847FASTOP2CL(shl);
848FASTOP2CL(shr);
849FASTOP2CL(sar);
850
851FASTOP2W(bsf);
852FASTOP2W(bsr);
853FASTOP2W(bt);
854FASTOP2W(bts);
855FASTOP2W(btr);
856FASTOP2W(btc);
857
Avi Kivitye47a5f52013-02-09 11:31:51 +0200858FASTOP2(xadd);
859
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200860static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300861{
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200862 u8 rc;
863 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300864
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200865 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +0200866 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200867 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
868 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300869}
870
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300871static void fetch_register_operand(struct operand *op)
872{
873 switch (op->bytes) {
874 case 1:
875 op->val = *(u8 *)op->addr.reg;
876 break;
877 case 2:
878 op->val = *(u16 *)op->addr.reg;
879 break;
880 case 4:
881 op->val = *(u32 *)op->addr.reg;
882 break;
883 case 8:
884 op->val = *(u64 *)op->addr.reg;
885 break;
886 }
887}
888
Avi Kivity12537912011-03-29 11:41:27 +0200889static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
890{
891 ctxt->ops->get_fpu(ctxt);
892 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200893 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
894 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
895 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
896 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
897 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
898 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
899 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
900 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200901#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200902 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
903 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
904 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
905 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
906 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
907 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
908 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
909 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200910#endif
911 default: BUG();
912 }
913 ctxt->ops->put_fpu(ctxt);
914}
915
916static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
917 int reg)
918{
919 ctxt->ops->get_fpu(ctxt);
920 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200921 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
922 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
923 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
924 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
925 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
926 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
927 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
928 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200929#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200930 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
931 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
932 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
933 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
934 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
935 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
936 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
937 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200938#endif
939 default: BUG();
940 }
941 ctxt->ops->put_fpu(ctxt);
942}
943
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300944static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
945{
946 ctxt->ops->get_fpu(ctxt);
947 switch (reg) {
948 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
949 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
950 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
951 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
952 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
953 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
954 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
955 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
956 default: BUG();
957 }
958 ctxt->ops->put_fpu(ctxt);
959}
960
961static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
962{
963 ctxt->ops->get_fpu(ctxt);
964 switch (reg) {
965 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
966 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
967 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
968 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
969 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
970 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
971 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
972 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
973 default: BUG();
974 }
975 ctxt->ops->put_fpu(ctxt);
976}
977
Gleb Natapov045a2822012-12-20 16:57:43 +0200978static int em_fninit(struct x86_emulate_ctxt *ctxt)
979{
980 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
981 return emulate_nm(ctxt);
982
983 ctxt->ops->get_fpu(ctxt);
984 asm volatile("fninit");
985 ctxt->ops->put_fpu(ctxt);
986 return X86EMUL_CONTINUE;
987}
988
989static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
990{
991 u16 fcw;
992
993 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
994 return emulate_nm(ctxt);
995
996 ctxt->ops->get_fpu(ctxt);
997 asm volatile("fnstcw %0": "+m"(fcw));
998 ctxt->ops->put_fpu(ctxt);
999
1000 /* force 2 byte destination */
1001 ctxt->dst.bytes = 2;
1002 ctxt->dst.val = fcw;
1003
1004 return X86EMUL_CONTINUE;
1005}
1006
1007static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1008{
1009 u16 fsw;
1010
1011 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1012 return emulate_nm(ctxt);
1013
1014 ctxt->ops->get_fpu(ctxt);
1015 asm volatile("fnstsw %0": "+m"(fsw));
1016 ctxt->ops->put_fpu(ctxt);
1017
1018 /* force 2 byte destination */
1019 ctxt->dst.bytes = 2;
1020 ctxt->dst.val = fsw;
1021
1022 return X86EMUL_CONTINUE;
1023}
1024
Avi Kivity12537912011-03-29 11:41:27 +02001025static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001026 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001027{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001028 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001029
Avi Kivity9dac77f2011-06-01 15:34:25 +03001030 if (!(ctxt->d & ModRM))
1031 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +02001032
Avi Kivity9dac77f2011-06-01 15:34:25 +03001033 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001034 op->type = OP_XMM;
1035 op->bytes = 16;
1036 op->addr.xmm = reg;
1037 read_sse_reg(ctxt, &op->vec_val, reg);
1038 return;
1039 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001040 if (ctxt->d & Mmx) {
1041 reg &= 7;
1042 op->type = OP_MM;
1043 op->bytes = 8;
1044 op->addr.mm = reg;
1045 return;
1046 }
Avi Kivity12537912011-03-29 11:41:27 +02001047
Avi Kivity3c118e22007-10-31 10:27:04 +02001048 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001049 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1050 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1051
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001052 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001053 op->orig_val = op->val;
1054}
1055
Avi Kivitya6e34072012-06-10 17:15:39 +03001056static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1057{
1058 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1059 ctxt->modrm_seg = VCPU_SREG_SS;
1060}
1061
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001062static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001063 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001064{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001065 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001066 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001067 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001068 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001069
Avi Kivity9dac77f2011-06-01 15:34:25 +03001070 if (ctxt->rex_prefix) {
1071 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
1072 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
1073 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001074 }
1075
Avi Kivity9dac77f2011-06-01 15:34:25 +03001076 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
1077 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
1078 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
1079 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001080
Nadav Amit9b88ae92014-05-25 23:05:21 +03001081 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001082 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001083 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001084 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001085 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001086 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001087 op->type = OP_XMM;
1088 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001089 op->addr.xmm = ctxt->modrm_rm;
1090 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001091 return rc;
1092 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001093 if (ctxt->d & Mmx) {
1094 op->type = OP_MM;
1095 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001096 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001097 return rc;
1098 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001099 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001100 return rc;
1101 }
1102
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001103 op->type = OP_MEM;
1104
Avi Kivity9dac77f2011-06-01 15:34:25 +03001105 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001106 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1107 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1108 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1109 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001110
1111 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001112 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001113 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001114 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001115 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001116 break;
1117 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001118 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001119 break;
1120 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001121 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001122 break;
1123 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001124 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001125 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001126 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001127 break;
1128 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001129 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001130 break;
1131 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001132 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001133 break;
1134 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001135 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001136 break;
1137 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001138 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001139 break;
1140 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001141 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001142 break;
1143 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001144 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001145 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001146 break;
1147 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001148 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001149 break;
1150 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001151 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1152 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1153 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001154 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001155 } else {
1156 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001157 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001158 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001159 index_reg |= (sib >> 3) & 7;
1160 base_reg |= sib & 7;
1161 scale = sib >> 6;
1162
Avi Kivity9dac77f2011-06-01 15:34:25 +03001163 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001164 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001165 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001166 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001167 adjust_modrm_seg(ctxt, base_reg);
1168 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001169 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001170 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001171 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001172 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001173 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001174 } else {
1175 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001176 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001177 adjust_modrm_seg(ctxt, base_reg);
1178 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001179 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001180 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001181 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001182 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001183 break;
1184 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001185 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001186 break;
1187 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001188 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001189 break;
1190 }
1191 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001192 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001193done:
1194 return rc;
1195}
1196
1197static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001198 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001199{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001200 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001201
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001202 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001203 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001204 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001205 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001206 break;
1207 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001208 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001209 break;
1210 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001211 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001212 break;
1213 }
1214done:
1215 return rc;
1216}
1217
Avi Kivity9dac77f2011-06-01 15:34:25 +03001218static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001219{
Sheng Yang7129eec2010-09-28 16:33:32 +08001220 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001221
Avi Kivity9dac77f2011-06-01 15:34:25 +03001222 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001223 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001224
Avi Kivity9dac77f2011-06-01 15:34:25 +03001225 if (ctxt->src.bytes == 2)
1226 sv = (s16)ctxt->src.val & (s16)mask;
1227 else if (ctxt->src.bytes == 4)
1228 sv = (s32)ctxt->src.val & (s32)mask;
Nadav Amit7dec5602014-06-15 16:12:57 +03001229 else
1230 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001231
Avi Kivity9dac77f2011-06-01 15:34:25 +03001232 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001233 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001234
1235 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001236 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001237}
1238
Gleb Natapov9de41572010-04-28 19:15:22 +03001239static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001240 unsigned long addr, void *dest, unsigned size)
1241{
1242 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001243 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001244
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001245 if (mc->pos < mc->end)
1246 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001247
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001248 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001249
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001250 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1251 &ctxt->exception);
1252 if (rc != X86EMUL_CONTINUE)
1253 return rc;
1254
1255 mc->end += size;
1256
1257read_cached:
1258 memcpy(dest, mc->data + mc->pos, size);
1259 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001260 return X86EMUL_CONTINUE;
1261}
1262
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001263static int segmented_read(struct x86_emulate_ctxt *ctxt,
1264 struct segmented_address addr,
1265 void *data,
1266 unsigned size)
1267{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001268 int rc;
1269 ulong linear;
1270
Avi Kivity83b87952011-04-03 11:31:19 +03001271 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001272 if (rc != X86EMUL_CONTINUE)
1273 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001274 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001275}
1276
1277static int segmented_write(struct x86_emulate_ctxt *ctxt,
1278 struct segmented_address addr,
1279 const void *data,
1280 unsigned size)
1281{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001282 int rc;
1283 ulong linear;
1284
Avi Kivity83b87952011-04-03 11:31:19 +03001285 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001286 if (rc != X86EMUL_CONTINUE)
1287 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001288 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1289 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001290}
1291
1292static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1293 struct segmented_address addr,
1294 const void *orig_data, const void *data,
1295 unsigned size)
1296{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001297 int rc;
1298 ulong linear;
1299
Avi Kivity83b87952011-04-03 11:31:19 +03001300 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001301 if (rc != X86EMUL_CONTINUE)
1302 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001303 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1304 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001305}
1306
Gleb Natapov7b262e92010-03-18 15:20:27 +02001307static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001308 unsigned int size, unsigned short port,
1309 void *dest)
1310{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001311 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001312
1313 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001314 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001315 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001316 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001317 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001318 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1319 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Gleb Natapov7b262e92010-03-18 15:20:27 +02001320 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1321 count);
1322 if (n == 0)
1323 n = 1;
1324 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001325 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001326 return 0;
1327 rc->end = n * size;
1328 }
1329
Nadav Amite6e39f02014-04-18 03:35:10 +03001330 if (ctxt->rep_prefix && (ctxt->d & String) &&
1331 !(ctxt->eflags & EFLG_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001332 ctxt->dst.data = rc->data + rc->pos;
1333 ctxt->dst.type = OP_MEM_STR;
1334 ctxt->dst.count = (rc->end - rc->pos) / size;
1335 rc->pos = rc->end;
1336 } else {
1337 memcpy(dest, rc->data + rc->pos, size);
1338 rc->pos += size;
1339 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001340 return 1;
1341}
1342
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001343static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1344 u16 index, struct desc_struct *desc)
1345{
1346 struct desc_ptr dt;
1347 ulong addr;
1348
1349 ctxt->ops->get_idt(ctxt, &dt);
1350
1351 if (dt.size < index * 8 + 7)
1352 return emulate_gp(ctxt, index << 3 | 0x2);
1353
1354 addr = dt.address + index * 8;
1355 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1356 &ctxt->exception);
1357}
1358
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001359static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001360 u16 selector, struct desc_ptr *dt)
1361{
Mathias Krause0225fb52012-08-30 01:30:16 +02001362 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001363 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001364
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001365 if (selector & 1 << 2) {
1366 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001367 u16 sel;
1368
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001369 memset (dt, 0, sizeof *dt);
Nadav Amit2eedcac2014-06-02 18:34:05 +03001370 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1371 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001372 return;
1373
1374 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001375 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001376 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001377 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001378}
1379
1380/* allowed just for 8 bytes segments */
1381static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001382 u16 selector, struct desc_struct *desc,
1383 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001384{
1385 struct desc_ptr dt;
1386 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001387 ulong addr;
1388
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001389 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001390
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001391 if (dt.size < index * 8 + 7)
1392 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001393
Avi Kivitye9194642012-06-13 16:29:39 +03001394 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001395 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1396 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001397}
1398
1399/* allowed just for 8 bytes segments */
1400static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001401 u16 selector, struct desc_struct *desc)
1402{
1403 struct desc_ptr dt;
1404 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001405 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001406
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001407 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001408
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001409 if (dt.size < index * 8 + 7)
1410 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001411
1412 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001413 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1414 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001415}
1416
Gleb Natapov5601d052011-03-07 14:55:06 +02001417/* Does not support long mode */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001418static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Paolo Bonzini5045b462014-05-15 18:09:29 +02001419 u16 selector, int seg, u8 cpl, bool in_task_switch)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001420{
Avi Kivity869be992012-06-13 16:30:53 +03001421 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001422 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001423 unsigned err_vec = GP_VECTOR;
1424 u32 err_code = 0;
1425 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001426 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001427 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001428 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001429 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001430
1431 memset(&seg_desc, 0, sizeof seg_desc);
1432
Kevin Wolff8da94e2013-04-11 14:06:03 +02001433 if (ctxt->mode == X86EMUL_MODE_REAL) {
1434 /* set real mode segment descriptor (keep limit etc. for
1435 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001436 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001437 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001438 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001439 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1440 /* VM86 needs a clean new segment descriptor */
1441 set_desc_base(&seg_desc, selector << 4);
1442 set_desc_limit(&seg_desc, 0xffff);
1443 seg_desc.type = 3;
1444 seg_desc.p = 1;
1445 seg_desc.s = 1;
1446 seg_desc.dpl = 3;
1447 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001448 }
1449
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001450 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001451
1452 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1453 if ((seg == VCPU_SREG_CS
1454 || (seg == VCPU_SREG_SS
1455 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1456 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001457 && null_selector)
1458 goto exception;
1459
1460 /* TR should be in GDT only */
1461 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1462 goto exception;
1463
1464 if (null_selector) /* for NULL selector skip all following checks */
1465 goto load;
1466
Avi Kivitye9194642012-06-13 16:29:39 +03001467 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001468 if (ret != X86EMUL_CONTINUE)
1469 return ret;
1470
1471 err_code = selector & 0xfffc;
1472 err_vec = GP_VECTOR;
1473
Guo Chaofc058682012-06-28 15:19:51 +08001474 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001475 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1476 goto exception;
1477
1478 if (!seg_desc.p) {
1479 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1480 goto exception;
1481 }
1482
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001483 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001484
1485 switch (seg) {
1486 case VCPU_SREG_SS:
1487 /*
1488 * segment is not a writable data segment or segment
1489 * selector's RPL != CPL or segment selector's RPL != CPL
1490 */
1491 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1492 goto exception;
1493 break;
1494 case VCPU_SREG_CS:
Paolo Bonzini5045b462014-05-15 18:09:29 +02001495 if (in_task_switch && rpl != dpl)
1496 goto exception;
1497
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001498 if (!(seg_desc.type & 8))
1499 goto exception;
1500
1501 if (seg_desc.type & 4) {
1502 /* conforming */
1503 if (dpl > cpl)
1504 goto exception;
1505 } else {
1506 /* nonconforming */
1507 if (rpl > cpl || dpl != cpl)
1508 goto exception;
1509 }
1510 /* CS(RPL) <- CPL */
1511 selector = (selector & 0xfffc) | cpl;
1512 break;
1513 case VCPU_SREG_TR:
1514 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1515 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001516 old_desc = seg_desc;
1517 seg_desc.type |= 2; /* busy */
1518 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1519 sizeof(seg_desc), &ctxt->exception);
1520 if (ret != X86EMUL_CONTINUE)
1521 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001522 break;
1523 case VCPU_SREG_LDTR:
1524 if (seg_desc.s || seg_desc.type != 2)
1525 goto exception;
1526 break;
1527 default: /* DS, ES, FS, or GS */
1528 /*
1529 * segment is not a data or readable code segment or
1530 * ((segment is a data or nonconforming code segment)
1531 * and (both RPL and CPL > DPL))
1532 */
1533 if ((seg_desc.type & 0xa) == 0x8 ||
1534 (((seg_desc.type & 0xc) != 0xc) &&
1535 (rpl > dpl && cpl > dpl)))
1536 goto exception;
1537 break;
1538 }
1539
1540 if (seg_desc.s) {
1541 /* mark segment as accessed */
1542 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001543 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001544 if (ret != X86EMUL_CONTINUE)
1545 return ret;
Nadav Amite37a75a2014-06-02 18:34:04 +03001546 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
1547 ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3,
1548 sizeof(base3), &ctxt->exception);
1549 if (ret != X86EMUL_CONTINUE)
1550 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001551 }
1552load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001553 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001554 return X86EMUL_CONTINUE;
1555exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001556 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001557 return X86EMUL_PROPAGATE_FAULT;
1558}
1559
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001560static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1561 u16 selector, int seg)
1562{
1563 u8 cpl = ctxt->ops->cpl(ctxt);
Paolo Bonzini5045b462014-05-15 18:09:29 +02001564 return __load_segment_descriptor(ctxt, selector, seg, cpl, false);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001565}
1566
Wei Yongjun31be40b2010-08-17 09:17:30 +08001567static void write_register_operand(struct operand *op)
1568{
1569 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1570 switch (op->bytes) {
1571 case 1:
1572 *(u8 *)op->addr.reg = (u8)op->val;
1573 break;
1574 case 2:
1575 *(u16 *)op->addr.reg = (u16)op->val;
1576 break;
1577 case 4:
1578 *op->addr.reg = (u32)op->val;
1579 break; /* 64b: zero-extend */
1580 case 8:
1581 *op->addr.reg = op->val;
1582 break;
1583 }
1584}
1585
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001586static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001587{
1588 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001589
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001590 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001591 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001592 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001593 break;
1594 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001595 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001596 rc = segmented_cmpxchg(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001597 op->addr.mem,
1598 &op->orig_val,
1599 &op->val,
1600 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001601 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001602 rc = segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001603 op->addr.mem,
1604 &op->val,
1605 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001606 if (rc != X86EMUL_CONTINUE)
1607 return rc;
1608 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001609 case OP_MEM_STR:
1610 rc = segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001611 op->addr.mem,
1612 op->data,
1613 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001614 if (rc != X86EMUL_CONTINUE)
1615 return rc;
1616 break;
Avi Kivity12537912011-03-29 11:41:27 +02001617 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001618 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001619 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001620 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001621 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001622 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001623 case OP_NONE:
1624 /* no writeback */
1625 break;
1626 default:
1627 break;
1628 }
1629 return X86EMUL_CONTINUE;
1630}
1631
Avi Kivity51ddff52012-06-12 20:19:40 +03001632static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001633{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001634 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001635
Avi Kivity5ad105e2012-08-19 14:34:31 +03001636 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001637 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001638 addr.seg = VCPU_SREG_SS;
1639
Avi Kivity51ddff52012-06-12 20:19:40 +03001640 return segmented_write(ctxt, addr, data, bytes);
1641}
1642
1643static int em_push(struct x86_emulate_ctxt *ctxt)
1644{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001645 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001646 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001647 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001648}
1649
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001650static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001651 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001652{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001653 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001654 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001655
Avi Kivitydd856ef2012-08-27 23:46:17 +03001656 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001657 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001658 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001659 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001660 return rc;
1661
Avi Kivity5ad105e2012-08-19 14:34:31 +03001662 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001663 return rc;
1664}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001665
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001666static int em_pop(struct x86_emulate_ctxt *ctxt)
1667{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001668 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001669}
1670
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001671static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001672 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001673{
1674 int rc;
1675 unsigned long val, change_mask;
1676 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001677 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001678
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001679 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001680 if (rc != X86EMUL_CONTINUE)
1681 return rc;
1682
1683 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1684 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1685
1686 switch(ctxt->mode) {
1687 case X86EMUL_MODE_PROT64:
1688 case X86EMUL_MODE_PROT32:
1689 case X86EMUL_MODE_PROT16:
1690 if (cpl == 0)
1691 change_mask |= EFLG_IOPL;
1692 if (cpl <= iopl)
1693 change_mask |= EFLG_IF;
1694 break;
1695 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001696 if (iopl < 3)
1697 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001698 change_mask |= EFLG_IF;
1699 break;
1700 default: /* real mode */
1701 change_mask |= (EFLG_IOPL | EFLG_IF);
1702 break;
1703 }
1704
1705 *(unsigned long *)dest =
1706 (ctxt->eflags & ~change_mask) | (val & change_mask);
1707
1708 return rc;
1709}
1710
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001711static int em_popf(struct x86_emulate_ctxt *ctxt)
1712{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001713 ctxt->dst.type = OP_REG;
1714 ctxt->dst.addr.reg = &ctxt->eflags;
1715 ctxt->dst.bytes = ctxt->op_bytes;
1716 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001717}
1718
Avi Kivity612e89f2012-06-12 20:03:23 +03001719static int em_enter(struct x86_emulate_ctxt *ctxt)
1720{
1721 int rc;
1722 unsigned frame_size = ctxt->src.val;
1723 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001724 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001725
1726 if (nesting_level)
1727 return X86EMUL_UNHANDLEABLE;
1728
Avi Kivitydd856ef2012-08-27 23:46:17 +03001729 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1730 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001731 if (rc != X86EMUL_CONTINUE)
1732 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001733 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001734 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001735 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1736 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001737 stack_mask(ctxt));
1738 return X86EMUL_CONTINUE;
1739}
1740
Avi Kivityf47cfa32012-06-07 17:49:24 +03001741static int em_leave(struct x86_emulate_ctxt *ctxt)
1742{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001743 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001744 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001745 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001746}
1747
Avi Kivity1cd196e2011-09-13 10:45:51 +03001748static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001749{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001750 int seg = ctxt->src2.val;
1751
Avi Kivity9dac77f2011-06-01 15:34:25 +03001752 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001753
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001754 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001755}
1756
Avi Kivity1cd196e2011-09-13 10:45:51 +03001757static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001758{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001759 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001760 unsigned long selector;
1761 int rc;
1762
Avi Kivity9dac77f2011-06-01 15:34:25 +03001763 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001764 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001765 return rc;
1766
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001767 if (ctxt->modrm_reg == VCPU_SREG_SS)
1768 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
1769
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001770 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001771 return rc;
1772}
1773
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001774static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001775{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001776 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001777 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001778 int reg = VCPU_REGS_RAX;
1779
1780 while (reg <= VCPU_REGS_RDI) {
1781 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001782 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001783
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001784 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001785 if (rc != X86EMUL_CONTINUE)
1786 return rc;
1787
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001788 ++reg;
1789 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001790
Wei Yongjunc37eda12010-06-15 09:03:33 +08001791 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001792}
1793
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001794static int em_pushf(struct x86_emulate_ctxt *ctxt)
1795{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001796 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001797 return em_push(ctxt);
1798}
1799
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001800static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001801{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001802 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001803 int reg = VCPU_REGS_RDI;
1804
1805 while (reg >= VCPU_REGS_RAX) {
1806 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001807 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001808 --reg;
1809 }
1810
Avi Kivitydd856ef2012-08-27 23:46:17 +03001811 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001812 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001813 break;
1814 --reg;
1815 }
1816 return rc;
1817}
1818
Avi Kivitydd856ef2012-08-27 23:46:17 +03001819static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001820{
Mathias Krause0225fb52012-08-30 01:30:16 +02001821 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001822 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001823 struct desc_ptr dt;
1824 gva_t cs_addr;
1825 gva_t eip_addr;
1826 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001827
1828 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001829 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001830 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001831 if (rc != X86EMUL_CONTINUE)
1832 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001833
1834 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1835
Avi Kivity9dac77f2011-06-01 15:34:25 +03001836 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001837 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001838 if (rc != X86EMUL_CONTINUE)
1839 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001840
Avi Kivity9dac77f2011-06-01 15:34:25 +03001841 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001842 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001843 if (rc != X86EMUL_CONTINUE)
1844 return rc;
1845
Avi Kivity4bff1e862011-04-20 13:37:53 +03001846 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001847
1848 eip_addr = dt.address + (irq << 2);
1849 cs_addr = dt.address + (irq << 2) + 2;
1850
Avi Kivity0f65dd72011-04-20 13:37:53 +03001851 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001852 if (rc != X86EMUL_CONTINUE)
1853 return rc;
1854
Avi Kivity0f65dd72011-04-20 13:37:53 +03001855 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001856 if (rc != X86EMUL_CONTINUE)
1857 return rc;
1858
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001859 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001860 if (rc != X86EMUL_CONTINUE)
1861 return rc;
1862
Avi Kivity9dac77f2011-06-01 15:34:25 +03001863 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001864
1865 return rc;
1866}
1867
Avi Kivitydd856ef2012-08-27 23:46:17 +03001868int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1869{
1870 int rc;
1871
1872 invalidate_registers(ctxt);
1873 rc = __emulate_int_real(ctxt, irq);
1874 if (rc == X86EMUL_CONTINUE)
1875 writeback_registers(ctxt);
1876 return rc;
1877}
1878
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001879static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001880{
1881 switch(ctxt->mode) {
1882 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001883 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001884 case X86EMUL_MODE_VM86:
1885 case X86EMUL_MODE_PROT16:
1886 case X86EMUL_MODE_PROT32:
1887 case X86EMUL_MODE_PROT64:
1888 default:
1889 /* Protected mode interrupts unimplemented yet */
1890 return X86EMUL_UNHANDLEABLE;
1891 }
1892}
1893
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001894static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001895{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001896 int rc = X86EMUL_CONTINUE;
1897 unsigned long temp_eip = 0;
1898 unsigned long temp_eflags = 0;
1899 unsigned long cs = 0;
1900 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1901 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1902 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1903 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1904
1905 /* TODO: Add stack limit check */
1906
Avi Kivity9dac77f2011-06-01 15:34:25 +03001907 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001908
1909 if (rc != X86EMUL_CONTINUE)
1910 return rc;
1911
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001912 if (temp_eip & ~0xffff)
1913 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001914
Avi Kivity9dac77f2011-06-01 15:34:25 +03001915 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001916
1917 if (rc != X86EMUL_CONTINUE)
1918 return rc;
1919
Avi Kivity9dac77f2011-06-01 15:34:25 +03001920 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001921
1922 if (rc != X86EMUL_CONTINUE)
1923 return rc;
1924
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001925 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001926
1927 if (rc != X86EMUL_CONTINUE)
1928 return rc;
1929
Avi Kivity9dac77f2011-06-01 15:34:25 +03001930 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001931
1932
Avi Kivity9dac77f2011-06-01 15:34:25 +03001933 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001934 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001935 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001936 ctxt->eflags &= ~0xffff;
1937 ctxt->eflags |= temp_eflags;
1938 }
1939
1940 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1941 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1942
1943 return rc;
1944}
1945
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001946static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001947{
1948 switch(ctxt->mode) {
1949 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001950 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001951 case X86EMUL_MODE_VM86:
1952 case X86EMUL_MODE_PROT16:
1953 case X86EMUL_MODE_PROT32:
1954 case X86EMUL_MODE_PROT64:
1955 default:
1956 /* iret from protected mode unimplemented yet */
1957 return X86EMUL_UNHANDLEABLE;
1958 }
1959}
1960
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001961static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1962{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001963 int rc;
1964 unsigned short sel;
1965
Avi Kivity9dac77f2011-06-01 15:34:25 +03001966 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001967
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001968 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001969 if (rc != X86EMUL_CONTINUE)
1970 return rc;
1971
Avi Kivity9dac77f2011-06-01 15:34:25 +03001972 ctxt->_eip = 0;
1973 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001974 return X86EMUL_CONTINUE;
1975}
1976
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001977static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001978{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001979 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001980
Avi Kivity9dac77f2011-06-01 15:34:25 +03001981 switch (ctxt->modrm_reg) {
Mohammed Gamald19292e2008-09-08 21:47:19 +03001982 case 2: /* call near abs */ {
1983 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001984 old_eip = ctxt->_eip;
1985 ctxt->_eip = ctxt->src.val;
1986 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001987 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001988 break;
1989 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001990 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001991 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001992 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001993 case 5: /* jmp far */
1994 rc = em_jmp_far(ctxt);
1995 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001996 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001997 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001998 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001999 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002000 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002001}
2002
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002003static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002004{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002005 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002006
Nadav Amitaaa05f22014-06-02 18:34:10 +03002007 if (ctxt->dst.bytes == 16)
2008 return X86EMUL_UNHANDLEABLE;
2009
Avi Kivitydd856ef2012-08-27 23:46:17 +03002010 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2011 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2012 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2013 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002014 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002015 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002016 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2017 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002018
Laurent Vivier05f086f2007-09-24 11:10:55 +02002019 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002020 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002021 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002022}
2023
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002024static int em_ret(struct x86_emulate_ctxt *ctxt)
2025{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002026 ctxt->dst.type = OP_REG;
2027 ctxt->dst.addr.reg = &ctxt->_eip;
2028 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002029 return em_pop(ctxt);
2030}
2031
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002032static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002033{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002034 int rc;
2035 unsigned long cs;
2036
Avi Kivity9dac77f2011-06-01 15:34:25 +03002037 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002038 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002039 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002040 if (ctxt->op_bytes == 4)
2041 ctxt->_eip = (u32)ctxt->_eip;
2042 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002043 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002044 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002045 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002046 return rc;
2047}
2048
Bruce Rogers32611072013-09-09 09:40:20 -06002049static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2050{
2051 int rc;
2052
2053 rc = em_ret_far(ctxt);
2054 if (rc != X86EMUL_CONTINUE)
2055 return rc;
2056 rsp_increment(ctxt, ctxt->src.val);
2057 return X86EMUL_CONTINUE;
2058}
2059
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002060static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2061{
2062 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002063 ctxt->dst.orig_val = ctxt->dst.val;
2064 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002065 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002066 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002067 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002068
2069 if (ctxt->eflags & EFLG_ZF) {
2070 /* Success: write back to memory. */
2071 ctxt->dst.val = ctxt->src.orig_val;
2072 } else {
2073 /* Failure: write the value we saw to EAX. */
2074 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002075 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Nadav Amit37c564f2014-06-02 18:34:07 +03002076 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002077 }
2078 return X86EMUL_CONTINUE;
2079}
2080
Avi Kivityd4b43252011-09-13 10:45:50 +03002081static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002082{
Avi Kivityd4b43252011-09-13 10:45:50 +03002083 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002084 unsigned short sel;
2085 int rc;
2086
Avi Kivity9dac77f2011-06-01 15:34:25 +03002087 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002088
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002089 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002090 if (rc != X86EMUL_CONTINUE)
2091 return rc;
2092
Avi Kivity9dac77f2011-06-01 15:34:25 +03002093 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002094 return rc;
2095}
2096
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002097static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002098setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002099 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002100{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002101 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002102 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002103 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002104 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002105 cs->type = 0x0b; /* Read, Execute, Accessed */
2106 cs->s = 1;
2107 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002108 cs->p = 1;
2109 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002110 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002111
Gleb Natapov79168fd2010-04-28 19:15:30 +03002112 set_desc_base(ss, 0); /* flat segment */
2113 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002114 ss->g = 1; /* 4kb granularity */
2115 ss->s = 1;
2116 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002117 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002118 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002119 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002120 ss->l = 0;
2121 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002122}
2123
Avi Kivity1a18a692012-02-01 12:23:21 +02002124static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2125{
2126 u32 eax, ebx, ecx, edx;
2127
2128 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002129 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2130 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002131 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2132 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2133}
2134
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002135static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2136{
Mathias Krause0225fb52012-08-30 01:30:16 +02002137 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002138 u32 eax, ebx, ecx, edx;
2139
2140 /*
2141 * syscall should always be enabled in longmode - so only become
2142 * vendor specific (cpuid) if other modes are active...
2143 */
2144 if (ctxt->mode == X86EMUL_MODE_PROT64)
2145 return true;
2146
2147 eax = 0x00000000;
2148 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002149 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2150 /*
2151 * Intel ("GenuineIntel")
2152 * remark: Intel CPUs only support "syscall" in 64bit
2153 * longmode. Also an 64bit guest with a
2154 * 32bit compat-app running will #UD !! While this
2155 * behaviour can be fixed (by emulating) into AMD
2156 * response - CPUs of AMD can't behave like Intel.
2157 */
2158 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2159 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2160 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2161 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002162
Avi Kivity0017f932012-06-07 14:10:16 +03002163 /* AMD ("AuthenticAMD") */
2164 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2165 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2166 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2167 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002168
Avi Kivity0017f932012-06-07 14:10:16 +03002169 /* AMD ("AMDisbetter!") */
2170 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2171 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2172 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2173 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002174
2175 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2176 return false;
2177}
2178
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002179static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002180{
Mathias Krause0225fb52012-08-30 01:30:16 +02002181 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002182 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002183 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002184 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002185 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002186
2187 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002188 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002189 ctxt->mode == X86EMUL_MODE_VM86)
2190 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002191
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002192 if (!(em_syscall_is_enabled(ctxt)))
2193 return emulate_ud(ctxt);
2194
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002195 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002196 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002197
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002198 if (!(efer & EFER_SCE))
2199 return emulate_ud(ctxt);
2200
Avi Kivity717746e2011-04-20 13:37:53 +03002201 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002202 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002203 cs_sel = (u16)(msr_data & 0xfffc);
2204 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002205
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002206 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002207 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002208 cs.l = 1;
2209 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002210 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2211 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002212
Avi Kivitydd856ef2012-08-27 23:46:17 +03002213 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002214 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002215#ifdef CONFIG_X86_64
Avi Kivitydd856ef2012-08-27 23:46:17 +03002216 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002217
Avi Kivity717746e2011-04-20 13:37:53 +03002218 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002219 ctxt->mode == X86EMUL_MODE_PROT64 ?
2220 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002221 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002222
Avi Kivity717746e2011-04-20 13:37:53 +03002223 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002224 ctxt->eflags &= ~(msr_data | EFLG_RF);
2225#endif
2226 } else {
2227 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002228 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002229 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002230
2231 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2232 }
2233
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002234 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002235}
2236
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002237static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002238{
Mathias Krause0225fb52012-08-30 01:30:16 +02002239 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002240 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002241 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002242 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002243 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002244
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002245 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002246 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002247 if (ctxt->mode == X86EMUL_MODE_REAL)
2248 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002249
Avi Kivity1a18a692012-02-01 12:23:21 +02002250 /*
2251 * Not recognized on AMD in compat mode (but is recognized in legacy
2252 * mode).
2253 */
2254 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2255 && !vendor_intel(ctxt))
2256 return emulate_ud(ctxt);
2257
Andre Przywara8c604352009-06-18 12:56:01 +02002258 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2259 * Therefore, we inject an #UD.
2260 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002261 if (ctxt->mode == X86EMUL_MODE_PROT64)
2262 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002263
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002264 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002265
Avi Kivity717746e2011-04-20 13:37:53 +03002266 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002267 switch (ctxt->mode) {
2268 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002269 if ((msr_data & 0xfffc) == 0x0)
2270 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002271 break;
2272 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002273 if (msr_data == 0x0)
2274 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002275 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002276 default:
2277 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002278 }
2279
2280 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002281 cs_sel = (u16)msr_data;
2282 cs_sel &= ~SELECTOR_RPL_MASK;
2283 ss_sel = cs_sel + 8;
2284 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002285 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002286 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002287 cs.l = 1;
2288 }
2289
Avi Kivity1aa36612011-04-27 13:20:30 +03002290 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2291 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002292
Avi Kivity717746e2011-04-20 13:37:53 +03002293 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002294 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002295
Avi Kivity717746e2011-04-20 13:37:53 +03002296 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002297 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002298
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002299 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002300}
2301
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002302static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002303{
Mathias Krause0225fb52012-08-30 01:30:16 +02002304 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002305 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002306 u64 msr_data;
2307 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002308 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002309
Gleb Natapova0044752010-02-10 14:21:31 +02002310 /* inject #GP if in real mode or Virtual 8086 mode */
2311 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002312 ctxt->mode == X86EMUL_MODE_VM86)
2313 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002314
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002315 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002316
Avi Kivity9dac77f2011-06-01 15:34:25 +03002317 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002318 usermode = X86EMUL_MODE_PROT64;
2319 else
2320 usermode = X86EMUL_MODE_PROT32;
2321
2322 cs.dpl = 3;
2323 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002324 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002325 switch (usermode) {
2326 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002327 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002328 if ((msr_data & 0xfffc) == 0x0)
2329 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002330 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002331 break;
2332 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002333 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002334 if (msr_data == 0x0)
2335 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002336 ss_sel = cs_sel + 8;
2337 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002338 cs.l = 1;
2339 break;
2340 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002341 cs_sel |= SELECTOR_RPL_MASK;
2342 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002343
Avi Kivity1aa36612011-04-27 13:20:30 +03002344 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2345 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002346
Avi Kivitydd856ef2012-08-27 23:46:17 +03002347 ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX);
2348 *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX);
Andre Przywara4668f052009-06-18 12:56:02 +02002349
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002350 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002351}
2352
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002353static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002354{
2355 int iopl;
2356 if (ctxt->mode == X86EMUL_MODE_REAL)
2357 return false;
2358 if (ctxt->mode == X86EMUL_MODE_VM86)
2359 return true;
2360 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002361 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002362}
2363
2364static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002365 u16 port, u16 len)
2366{
Mathias Krause0225fb52012-08-30 01:30:16 +02002367 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002368 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002369 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002370 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002371 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002372 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002373 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002374
Avi Kivity1aa36612011-04-27 13:20:30 +03002375 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002376 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002377 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002378 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002379 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002380 base = get_desc_base(&tr_seg);
2381#ifdef CONFIG_X86_64
2382 base |= ((u64)base3) << 32;
2383#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002384 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002385 if (r != X86EMUL_CONTINUE)
2386 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002387 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002388 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002389 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002390 if (r != X86EMUL_CONTINUE)
2391 return false;
2392 if ((perm >> bit_idx) & mask)
2393 return false;
2394 return true;
2395}
2396
2397static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002398 u16 port, u16 len)
2399{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002400 if (ctxt->perm_ok)
2401 return true;
2402
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002403 if (emulator_bad_iopl(ctxt))
2404 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002405 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002406
2407 ctxt->perm_ok = true;
2408
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002409 return true;
2410}
2411
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002412static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002413 struct tss_segment_16 *tss)
2414{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002415 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002416 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002417 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2418 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2419 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2420 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2421 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2422 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2423 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2424 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002425
Avi Kivity1aa36612011-04-27 13:20:30 +03002426 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2427 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2428 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2429 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2430 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002431}
2432
2433static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002434 struct tss_segment_16 *tss)
2435{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002436 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002437 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002438
Avi Kivity9dac77f2011-06-01 15:34:25 +03002439 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002440 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002441 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2442 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2443 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2444 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2445 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2446 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2447 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2448 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002449
2450 /*
2451 * SDM says that segment selectors are loaded before segment
2452 * descriptors
2453 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002454 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2455 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2456 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2457 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2458 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002459
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002460 cpl = tss->cs & 3;
2461
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002462 /*
Guo Chaofc058682012-06-28 15:19:51 +08002463 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002464 * it is handled in a context of new task
2465 */
Paolo Bonzini5045b462014-05-15 18:09:29 +02002466 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002467 if (ret != X86EMUL_CONTINUE)
2468 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002469 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002470 if (ret != X86EMUL_CONTINUE)
2471 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002472 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002473 if (ret != X86EMUL_CONTINUE)
2474 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002475 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002476 if (ret != X86EMUL_CONTINUE)
2477 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002478 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002479 if (ret != X86EMUL_CONTINUE)
2480 return ret;
2481
2482 return X86EMUL_CONTINUE;
2483}
2484
2485static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002486 u16 tss_selector, u16 old_tss_sel,
2487 ulong old_tss_base, struct desc_struct *new_desc)
2488{
Mathias Krause0225fb52012-08-30 01:30:16 +02002489 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002490 struct tss_segment_16 tss_seg;
2491 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002492 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002493
Avi Kivity0f65dd72011-04-20 13:37:53 +03002494 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002495 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002496 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002497 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002498 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002499
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002500 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002501
Avi Kivity0f65dd72011-04-20 13:37:53 +03002502 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002503 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002504 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002505 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002506 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002507
Avi Kivity0f65dd72011-04-20 13:37:53 +03002508 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002509 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002510 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002511 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002512 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002513
2514 if (old_tss_sel != 0xffff) {
2515 tss_seg.prev_task_link = old_tss_sel;
2516
Avi Kivity0f65dd72011-04-20 13:37:53 +03002517 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002518 &tss_seg.prev_task_link,
2519 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002520 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002521 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002522 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002523 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002524 }
2525
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002526 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002527}
2528
2529static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002530 struct tss_segment_32 *tss)
2531{
Nadav Amit5c7411e2014-04-07 18:37:47 +03002532 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002533 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002534 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002535 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2536 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2537 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2538 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2539 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2540 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2541 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2542 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002543
Avi Kivity1aa36612011-04-27 13:20:30 +03002544 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2545 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2546 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2547 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2548 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2549 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002550}
2551
2552static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002553 struct tss_segment_32 *tss)
2554{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002555 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002556 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002557
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002558 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002559 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002560 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002561 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002562
2563 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002564 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2565 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2566 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2567 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2568 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2569 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2570 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2571 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002572
2573 /*
2574 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002575 * descriptors. This is important because CPL checks will
2576 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002577 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002578 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2579 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2580 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2581 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2582 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2583 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2584 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002585
2586 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002587 * If we're switching between Protected Mode and VM86, we need to make
2588 * sure to update the mode before loading the segment descriptors so
2589 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01002590 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002591 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002592 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002593 cpl = 3;
2594 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002595 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002596 cpl = tss->cs & 3;
2597 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01002598
2599 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002600 * Now load segment descriptors. If fault happenes at this stage
2601 * it is handled in a context of new task
2602 */
Paolo Bonzini5045b462014-05-15 18:09:29 +02002603 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002604 if (ret != X86EMUL_CONTINUE)
2605 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002606 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002607 if (ret != X86EMUL_CONTINUE)
2608 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002609 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002610 if (ret != X86EMUL_CONTINUE)
2611 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002612 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002613 if (ret != X86EMUL_CONTINUE)
2614 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002615 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002616 if (ret != X86EMUL_CONTINUE)
2617 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002618 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002619 if (ret != X86EMUL_CONTINUE)
2620 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002621 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002622 if (ret != X86EMUL_CONTINUE)
2623 return ret;
2624
2625 return X86EMUL_CONTINUE;
2626}
2627
2628static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002629 u16 tss_selector, u16 old_tss_sel,
2630 ulong old_tss_base, struct desc_struct *new_desc)
2631{
Mathias Krause0225fb52012-08-30 01:30:16 +02002632 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002633 struct tss_segment_32 tss_seg;
2634 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002635 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03002636 u32 eip_offset = offsetof(struct tss_segment_32, eip);
2637 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002638
Avi Kivity0f65dd72011-04-20 13:37:53 +03002639 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002640 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002641 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002642 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002643 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002644
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002645 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002646
Nadav Amit5c7411e2014-04-07 18:37:47 +03002647 /* Only GP registers and segment selectors are saved */
2648 ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
2649 ldt_sel_offset - eip_offset, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002650 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002651 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002652 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002653
Avi Kivity0f65dd72011-04-20 13:37:53 +03002654 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002655 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002656 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002657 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002658 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002659
2660 if (old_tss_sel != 0xffff) {
2661 tss_seg.prev_task_link = old_tss_sel;
2662
Avi Kivity0f65dd72011-04-20 13:37:53 +03002663 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002664 &tss_seg.prev_task_link,
2665 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002666 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002667 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002668 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002669 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002670 }
2671
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002672 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002673}
2674
2675static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002676 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002677 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002678{
Mathias Krause0225fb52012-08-30 01:30:16 +02002679 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002680 struct desc_struct curr_tss_desc, next_tss_desc;
2681 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002682 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002683 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002684 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002685 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002686 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002687
2688 /* FIXME: old_tss_base == ~0 ? */
2689
Avi Kivitye9194642012-06-13 16:29:39 +03002690 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002691 if (ret != X86EMUL_CONTINUE)
2692 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002693 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002694 if (ret != X86EMUL_CONTINUE)
2695 return ret;
2696
2697 /* FIXME: check that next_tss_desc is tss */
2698
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002699 /*
2700 * Check privileges. The three cases are task switch caused by...
2701 *
2702 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2703 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002704 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002705 */
2706 if (reason == TASK_SWITCH_GATE) {
2707 if (idt_index != -1) {
2708 /* Software interrupts */
2709 struct desc_struct task_gate_desc;
2710 int dpl;
2711
2712 ret = read_interrupt_descriptor(ctxt, idt_index,
2713 &task_gate_desc);
2714 if (ret != X86EMUL_CONTINUE)
2715 return ret;
2716
2717 dpl = task_gate_desc.dpl;
2718 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2719 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2720 }
2721 } else if (reason != TASK_SWITCH_IRET) {
2722 int dpl = next_tss_desc.dpl;
2723 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2724 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002725 }
2726
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002727
Gleb Natapovceffb452010-03-18 15:20:19 +02002728 desc_limit = desc_limit_scaled(&next_tss_desc);
2729 if (!next_tss_desc.p ||
2730 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2731 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002732 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002733 return X86EMUL_PROPAGATE_FAULT;
2734 }
2735
2736 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2737 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002738 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002739 }
2740
2741 if (reason == TASK_SWITCH_IRET)
2742 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2743
2744 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002745 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002746 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2747 old_tss_sel = 0xffff;
2748
2749 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002750 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002751 old_tss_base, &next_tss_desc);
2752 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002753 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002754 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002755 if (ret != X86EMUL_CONTINUE)
2756 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002757
2758 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2759 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2760
2761 if (reason != TASK_SWITCH_IRET) {
2762 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002763 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002764 }
2765
Avi Kivity717746e2011-04-20 13:37:53 +03002766 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002767 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002768
Jan Kiszkae269fb22010-04-14 15:51:09 +02002769 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002770 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2771 ctxt->lock_prefix = 0;
2772 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002773 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002774 }
2775
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002776 return ret;
2777}
2778
2779int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002780 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002781 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002782{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002783 int rc;
2784
Avi Kivitydd856ef2012-08-27 23:46:17 +03002785 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002786 ctxt->_eip = ctxt->eip;
2787 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002788
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002789 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002790 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002791
Avi Kivitydd856ef2012-08-27 23:46:17 +03002792 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002793 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002794 writeback_registers(ctxt);
2795 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002796
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002797 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002798}
2799
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002800static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2801 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002802{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002803 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002804
Avi Kivitydd856ef2012-08-27 23:46:17 +03002805 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2806 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002807}
2808
Avi Kivity7af04fc2010-08-18 14:16:35 +03002809static int em_das(struct x86_emulate_ctxt *ctxt)
2810{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002811 u8 al, old_al;
2812 bool af, cf, old_cf;
2813
2814 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002815 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002816
2817 old_al = al;
2818 old_cf = cf;
2819 cf = false;
2820 af = ctxt->eflags & X86_EFLAGS_AF;
2821 if ((al & 0x0f) > 9 || af) {
2822 al -= 6;
2823 cf = old_cf | (al >= 250);
2824 af = true;
2825 } else {
2826 af = false;
2827 }
2828 if (old_al > 0x99 || old_cf) {
2829 al -= 0x60;
2830 cf = true;
2831 }
2832
Avi Kivity9dac77f2011-06-01 15:34:25 +03002833 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002834 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002835 ctxt->src.type = OP_IMM;
2836 ctxt->src.val = 0;
2837 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02002838 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03002839 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2840 if (cf)
2841 ctxt->eflags |= X86_EFLAGS_CF;
2842 if (af)
2843 ctxt->eflags |= X86_EFLAGS_AF;
2844 return X86EMUL_CONTINUE;
2845}
2846
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02002847static int em_aam(struct x86_emulate_ctxt *ctxt)
2848{
2849 u8 al, ah;
2850
2851 if (ctxt->src.val == 0)
2852 return emulate_de(ctxt);
2853
2854 al = ctxt->dst.val & 0xff;
2855 ah = al / ctxt->src.val;
2856 al %= ctxt->src.val;
2857
2858 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
2859
2860 /* Set PF, ZF, SF */
2861 ctxt->src.type = OP_IMM;
2862 ctxt->src.val = 0;
2863 ctxt->src.bytes = 1;
2864 fastop(ctxt, em_or);
2865
2866 return X86EMUL_CONTINUE;
2867}
2868
Gleb Natapov7f662272012-12-10 11:42:30 +02002869static int em_aad(struct x86_emulate_ctxt *ctxt)
2870{
2871 u8 al = ctxt->dst.val & 0xff;
2872 u8 ah = (ctxt->dst.val >> 8) & 0xff;
2873
2874 al = (al + (ah * ctxt->src.val)) & 0xff;
2875
2876 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
2877
Gleb Natapovf583c292013-02-13 17:50:39 +02002878 /* Set PF, ZF, SF */
2879 ctxt->src.type = OP_IMM;
2880 ctxt->src.val = 0;
2881 ctxt->src.bytes = 1;
2882 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02002883
2884 return X86EMUL_CONTINUE;
2885}
2886
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002887static int em_call(struct x86_emulate_ctxt *ctxt)
2888{
2889 long rel = ctxt->src.val;
2890
2891 ctxt->src.val = (unsigned long)ctxt->_eip;
2892 jmp_rel(ctxt, rel);
2893 return em_push(ctxt);
2894}
2895
Avi Kivity0ef753b2010-08-18 14:51:45 +03002896static int em_call_far(struct x86_emulate_ctxt *ctxt)
2897{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002898 u16 sel, old_cs;
2899 ulong old_eip;
2900 int rc;
2901
Avi Kivity1aa36612011-04-27 13:20:30 +03002902 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002903 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002904
Avi Kivity9dac77f2011-06-01 15:34:25 +03002905 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002906 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002907 return X86EMUL_CONTINUE;
2908
Avi Kivity9dac77f2011-06-01 15:34:25 +03002909 ctxt->_eip = 0;
2910 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002911
Avi Kivity9dac77f2011-06-01 15:34:25 +03002912 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002913 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002914 if (rc != X86EMUL_CONTINUE)
2915 return rc;
2916
Avi Kivity9dac77f2011-06-01 15:34:25 +03002917 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002918 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002919}
2920
Avi Kivity40ece7c2010-08-18 15:12:09 +03002921static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2922{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002923 int rc;
2924
Avi Kivity9dac77f2011-06-01 15:34:25 +03002925 ctxt->dst.type = OP_REG;
2926 ctxt->dst.addr.reg = &ctxt->_eip;
2927 ctxt->dst.bytes = ctxt->op_bytes;
2928 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002929 if (rc != X86EMUL_CONTINUE)
2930 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03002931 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002932 return X86EMUL_CONTINUE;
2933}
2934
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002935static int em_xchg(struct x86_emulate_ctxt *ctxt)
2936{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002937 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002938 ctxt->src.val = ctxt->dst.val;
2939 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002940
2941 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002942 ctxt->dst.val = ctxt->src.orig_val;
2943 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002944 return X86EMUL_CONTINUE;
2945}
2946
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002947static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2948{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002949 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02002950 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002951}
2952
Avi Kivity61429142010-08-19 15:13:00 +03002953static int em_cwd(struct x86_emulate_ctxt *ctxt)
2954{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002955 ctxt->dst.type = OP_REG;
2956 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002957 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002958 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002959
2960 return X86EMUL_CONTINUE;
2961}
2962
Avi Kivity48bb5d32010-08-18 18:54:34 +03002963static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2964{
Avi Kivity48bb5d32010-08-18 18:54:34 +03002965 u64 tsc = 0;
2966
Avi Kivity717746e2011-04-20 13:37:53 +03002967 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002968 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
2969 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03002970 return X86EMUL_CONTINUE;
2971}
2972
Avi Kivity222d21a2011-11-10 14:57:30 +02002973static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
2974{
2975 u64 pmc;
2976
Avi Kivitydd856ef2012-08-27 23:46:17 +03002977 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02002978 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002979 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
2980 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02002981 return X86EMUL_CONTINUE;
2982}
2983
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002984static int em_mov(struct x86_emulate_ctxt *ctxt)
2985{
Stefan Hajnoczi49597d82012-04-09 18:40:00 +03002986 memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002987 return X86EMUL_CONTINUE;
2988}
2989
Borislav Petkov84cffe42013-10-29 12:54:56 +01002990#define FFL(x) bit(X86_FEATURE_##x)
2991
2992static int em_movbe(struct x86_emulate_ctxt *ctxt)
2993{
2994 u32 ebx, ecx, edx, eax = 1;
2995 u16 tmp;
2996
2997 /*
2998 * Check MOVBE is set in the guest-visible CPUID leaf.
2999 */
3000 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
3001 if (!(ecx & FFL(MOVBE)))
3002 return emulate_ud(ctxt);
3003
3004 switch (ctxt->op_bytes) {
3005 case 2:
3006 /*
3007 * From MOVBE definition: "...When the operand size is 16 bits,
3008 * the upper word of the destination register remains unchanged
3009 * ..."
3010 *
3011 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3012 * rules so we have to do the operation almost per hand.
3013 */
3014 tmp = (u16)ctxt->src.val;
3015 ctxt->dst.val &= ~0xffffUL;
3016 ctxt->dst.val |= (unsigned long)swab16(tmp);
3017 break;
3018 case 4:
3019 ctxt->dst.val = swab32((u32)ctxt->src.val);
3020 break;
3021 case 8:
3022 ctxt->dst.val = swab64(ctxt->src.val);
3023 break;
3024 default:
3025 return X86EMUL_PROPAGATE_FAULT;
3026 }
3027 return X86EMUL_CONTINUE;
3028}
3029
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003030static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3031{
3032 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3033 return emulate_gp(ctxt, 0);
3034
3035 /* Disable writeback. */
3036 ctxt->dst.type = OP_NONE;
3037 return X86EMUL_CONTINUE;
3038}
3039
3040static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3041{
3042 unsigned long val;
3043
3044 if (ctxt->mode == X86EMUL_MODE_PROT64)
3045 val = ctxt->src.val & ~0ULL;
3046 else
3047 val = ctxt->src.val & ~0U;
3048
3049 /* #UD condition is already handled. */
3050 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3051 return emulate_gp(ctxt, 0);
3052
3053 /* Disable writeback. */
3054 ctxt->dst.type = OP_NONE;
3055 return X86EMUL_CONTINUE;
3056}
3057
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003058static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3059{
3060 u64 msr_data;
3061
Avi Kivitydd856ef2012-08-27 23:46:17 +03003062 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3063 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3064 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003065 return emulate_gp(ctxt, 0);
3066
3067 return X86EMUL_CONTINUE;
3068}
3069
3070static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3071{
3072 u64 msr_data;
3073
Avi Kivitydd856ef2012-08-27 23:46:17 +03003074 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003075 return emulate_gp(ctxt, 0);
3076
Avi Kivitydd856ef2012-08-27 23:46:17 +03003077 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3078 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003079 return X86EMUL_CONTINUE;
3080}
3081
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003082static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3083{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003084 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003085 return emulate_ud(ctxt);
3086
Avi Kivity9dac77f2011-06-01 15:34:25 +03003087 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003088 return X86EMUL_CONTINUE;
3089}
3090
3091static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3092{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003093 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003094
Avi Kivity9dac77f2011-06-01 15:34:25 +03003095 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003096 return emulate_ud(ctxt);
3097
Avi Kivity9dac77f2011-06-01 15:34:25 +03003098 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003099 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3100
3101 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003102 ctxt->dst.type = OP_NONE;
3103 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003104}
3105
Avi Kivitya14e5792012-06-13 12:28:33 +03003106static int em_lldt(struct x86_emulate_ctxt *ctxt)
3107{
3108 u16 sel = ctxt->src.val;
3109
3110 /* Disable writeback. */
3111 ctxt->dst.type = OP_NONE;
3112 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3113}
3114
Avi Kivity80890002012-06-13 16:33:29 +03003115static int em_ltr(struct x86_emulate_ctxt *ctxt)
3116{
3117 u16 sel = ctxt->src.val;
3118
3119 /* Disable writeback. */
3120 ctxt->dst.type = OP_NONE;
3121 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3122}
3123
Avi Kivity38503912011-03-31 18:48:09 +02003124static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3125{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003126 int rc;
3127 ulong linear;
3128
Avi Kivity9dac77f2011-06-01 15:34:25 +03003129 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003130 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003131 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003132 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003133 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003134 return X86EMUL_CONTINUE;
3135}
3136
Avi Kivity2d04a052011-04-20 15:32:49 +03003137static int em_clts(struct x86_emulate_ctxt *ctxt)
3138{
3139 ulong cr0;
3140
3141 cr0 = ctxt->ops->get_cr(ctxt, 0);
3142 cr0 &= ~X86_CR0_TS;
3143 ctxt->ops->set_cr(ctxt, 0, cr0);
3144 return X86EMUL_CONTINUE;
3145}
3146
Avi Kivity26d05cc2011-04-21 12:07:59 +03003147static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3148{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003149 int rc;
3150
Avi Kivity9dac77f2011-06-01 15:34:25 +03003151 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003152 return X86EMUL_UNHANDLEABLE;
3153
3154 rc = ctxt->ops->fix_hypercall(ctxt);
3155 if (rc != X86EMUL_CONTINUE)
3156 return rc;
3157
3158 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003159 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003160 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003161 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003162 return X86EMUL_CONTINUE;
3163}
3164
Avi Kivity96051572012-06-10 17:21:18 +03003165static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3166 void (*get)(struct x86_emulate_ctxt *ctxt,
3167 struct desc_ptr *ptr))
3168{
3169 struct desc_ptr desc_ptr;
3170
3171 if (ctxt->mode == X86EMUL_MODE_PROT64)
3172 ctxt->op_bytes = 8;
3173 get(ctxt, &desc_ptr);
3174 if (ctxt->op_bytes == 2) {
3175 ctxt->op_bytes = 4;
3176 desc_ptr.address &= 0x00ffffff;
3177 }
3178 /* Disable writeback. */
3179 ctxt->dst.type = OP_NONE;
3180 return segmented_write(ctxt, ctxt->dst.addr.mem,
3181 &desc_ptr, 2 + ctxt->op_bytes);
3182}
3183
3184static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3185{
3186 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3187}
3188
3189static int em_sidt(struct x86_emulate_ctxt *ctxt)
3190{
3191 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3192}
3193
Avi Kivity26d05cc2011-04-21 12:07:59 +03003194static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3195{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003196 struct desc_ptr desc_ptr;
3197 int rc;
3198
Avi Kivity510425f2012-06-07 17:04:36 +03003199 if (ctxt->mode == X86EMUL_MODE_PROT64)
3200 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003201 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003202 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003203 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003204 if (rc != X86EMUL_CONTINUE)
3205 return rc;
3206 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3207 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003208 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003209 return X86EMUL_CONTINUE;
3210}
3211
Avi Kivity5ef39c72011-04-21 12:21:50 +03003212static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003213{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003214 int rc;
3215
Avi Kivity5ef39c72011-04-21 12:21:50 +03003216 rc = ctxt->ops->fix_hypercall(ctxt);
3217
Avi Kivity26d05cc2011-04-21 12:07:59 +03003218 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003219 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003220 return rc;
3221}
3222
3223static int em_lidt(struct x86_emulate_ctxt *ctxt)
3224{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003225 struct desc_ptr desc_ptr;
3226 int rc;
3227
Avi Kivity510425f2012-06-07 17:04:36 +03003228 if (ctxt->mode == X86EMUL_MODE_PROT64)
3229 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003230 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003231 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003232 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003233 if (rc != X86EMUL_CONTINUE)
3234 return rc;
3235 ctxt->ops->set_idt(ctxt, &desc_ptr);
3236 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003237 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003238 return X86EMUL_CONTINUE;
3239}
3240
3241static int em_smsw(struct x86_emulate_ctxt *ctxt)
3242{
Nadav Amit32e94d02014-06-02 18:34:11 +03003243 if (ctxt->dst.type == OP_MEM)
3244 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003245 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003246 return X86EMUL_CONTINUE;
3247}
3248
3249static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3250{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003251 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003252 | (ctxt->src.val & 0x0f));
3253 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003254 return X86EMUL_CONTINUE;
3255}
3256
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003257static int em_loop(struct x86_emulate_ctxt *ctxt)
3258{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003259 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3260 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003261 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3262 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003263
3264 return X86EMUL_CONTINUE;
3265}
3266
3267static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3268{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003269 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003270 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003271
3272 return X86EMUL_CONTINUE;
3273}
3274
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003275static int em_in(struct x86_emulate_ctxt *ctxt)
3276{
3277 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3278 &ctxt->dst.val))
3279 return X86EMUL_IO_NEEDED;
3280
3281 return X86EMUL_CONTINUE;
3282}
3283
3284static int em_out(struct x86_emulate_ctxt *ctxt)
3285{
3286 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3287 &ctxt->src.val, 1);
3288 /* Disable writeback. */
3289 ctxt->dst.type = OP_NONE;
3290 return X86EMUL_CONTINUE;
3291}
3292
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003293static int em_cli(struct x86_emulate_ctxt *ctxt)
3294{
3295 if (emulator_bad_iopl(ctxt))
3296 return emulate_gp(ctxt, 0);
3297
3298 ctxt->eflags &= ~X86_EFLAGS_IF;
3299 return X86EMUL_CONTINUE;
3300}
3301
3302static int em_sti(struct x86_emulate_ctxt *ctxt)
3303{
3304 if (emulator_bad_iopl(ctxt))
3305 return emulate_gp(ctxt, 0);
3306
3307 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3308 ctxt->eflags |= X86_EFLAGS_IF;
3309 return X86EMUL_CONTINUE;
3310}
3311
Avi Kivity6d6eede2012-06-07 14:11:36 +03003312static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3313{
3314 u32 eax, ebx, ecx, edx;
3315
Avi Kivitydd856ef2012-08-27 23:46:17 +03003316 eax = reg_read(ctxt, VCPU_REGS_RAX);
3317 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003318 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003319 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3320 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3321 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3322 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003323 return X86EMUL_CONTINUE;
3324}
3325
Paolo Bonzini98f73632013-10-31 11:19:42 +01003326static int em_sahf(struct x86_emulate_ctxt *ctxt)
3327{
3328 u32 flags;
3329
3330 flags = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF;
3331 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3332
3333 ctxt->eflags &= ~0xffUL;
3334 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3335 return X86EMUL_CONTINUE;
3336}
3337
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003338static int em_lahf(struct x86_emulate_ctxt *ctxt)
3339{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003340 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3341 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003342 return X86EMUL_CONTINUE;
3343}
3344
Avi Kivity92998362012-06-13 12:25:06 +03003345static int em_bswap(struct x86_emulate_ctxt *ctxt)
3346{
3347 switch (ctxt->op_bytes) {
3348#ifdef CONFIG_X86_64
3349 case 8:
3350 asm("bswap %0" : "+r"(ctxt->dst.val));
3351 break;
3352#endif
3353 default:
3354 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3355 break;
3356 }
3357 return X86EMUL_CONTINUE;
3358}
3359
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003360static bool valid_cr(int nr)
3361{
3362 switch (nr) {
3363 case 0:
3364 case 2 ... 4:
3365 case 8:
3366 return true;
3367 default:
3368 return false;
3369 }
3370}
3371
3372static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3373{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003374 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003375 return emulate_ud(ctxt);
3376
3377 return X86EMUL_CONTINUE;
3378}
3379
3380static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3381{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003382 u64 new_val = ctxt->src.val64;
3383 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003384 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003385
3386 static u64 cr_reserved_bits[] = {
3387 0xffffffff00000000ULL,
3388 0, 0, 0, /* CR3 checked later */
3389 CR4_RESERVED_BITS,
3390 0, 0, 0,
3391 CR8_RESERVED_BITS,
3392 };
3393
3394 if (!valid_cr(cr))
3395 return emulate_ud(ctxt);
3396
3397 if (new_val & cr_reserved_bits[cr])
3398 return emulate_gp(ctxt, 0);
3399
3400 switch (cr) {
3401 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003402 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003403 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3404 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3405 return emulate_gp(ctxt, 0);
3406
Avi Kivity717746e2011-04-20 13:37:53 +03003407 cr4 = ctxt->ops->get_cr(ctxt, 4);
3408 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003409
3410 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3411 !(cr4 & X86_CR4_PAE))
3412 return emulate_gp(ctxt, 0);
3413
3414 break;
3415 }
3416 case 3: {
3417 u64 rsvd = 0;
3418
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003419 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3420 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003421 rsvd = CR3_L_MODE_RESERVED_BITS;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003422
3423 if (new_val & rsvd)
3424 return emulate_gp(ctxt, 0);
3425
3426 break;
3427 }
3428 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003429 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003430
3431 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3432 return emulate_gp(ctxt, 0);
3433
3434 break;
3435 }
3436 }
3437
3438 return X86EMUL_CONTINUE;
3439}
3440
Joerg Roedel3b88e412011-04-04 12:39:29 +02003441static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3442{
3443 unsigned long dr7;
3444
Avi Kivity717746e2011-04-20 13:37:53 +03003445 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003446
3447 /* Check if DR7.Global_Enable is set */
3448 return dr7 & (1 << 13);
3449}
3450
3451static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3452{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003453 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003454 u64 cr4;
3455
3456 if (dr > 7)
3457 return emulate_ud(ctxt);
3458
Avi Kivity717746e2011-04-20 13:37:53 +03003459 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003460 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3461 return emulate_ud(ctxt);
3462
3463 if (check_dr7_gd(ctxt))
3464 return emulate_db(ctxt);
3465
3466 return X86EMUL_CONTINUE;
3467}
3468
3469static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3470{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003471 u64 new_val = ctxt->src.val64;
3472 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003473
3474 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3475 return emulate_gp(ctxt, 0);
3476
3477 return check_dr_read(ctxt);
3478}
3479
Joerg Roedel01de8b02011-04-04 12:39:31 +02003480static int check_svme(struct x86_emulate_ctxt *ctxt)
3481{
3482 u64 efer;
3483
Avi Kivity717746e2011-04-20 13:37:53 +03003484 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003485
3486 if (!(efer & EFER_SVME))
3487 return emulate_ud(ctxt);
3488
3489 return X86EMUL_CONTINUE;
3490}
3491
3492static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3493{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003494 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003495
3496 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003497 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003498 return emulate_gp(ctxt, 0);
3499
3500 return check_svme(ctxt);
3501}
3502
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003503static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3504{
Avi Kivity717746e2011-04-20 13:37:53 +03003505 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003506
Avi Kivity717746e2011-04-20 13:37:53 +03003507 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003508 return emulate_ud(ctxt);
3509
3510 return X86EMUL_CONTINUE;
3511}
3512
Joerg Roedel80612522011-04-04 12:39:33 +02003513static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3514{
Avi Kivity717746e2011-04-20 13:37:53 +03003515 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003516 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003517
Avi Kivity717746e2011-04-20 13:37:53 +03003518 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03003519 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02003520 return emulate_gp(ctxt, 0);
3521
3522 return X86EMUL_CONTINUE;
3523}
3524
Joerg Roedelf6511932011-04-04 12:39:35 +02003525static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3526{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003527 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3528 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003529 return emulate_gp(ctxt, 0);
3530
3531 return X86EMUL_CONTINUE;
3532}
3533
3534static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3535{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003536 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3537 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003538 return emulate_gp(ctxt, 0);
3539
3540 return X86EMUL_CONTINUE;
3541}
3542
Avi Kivity73fba5f2010-07-29 15:11:53 +03003543#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003544#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003545#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3546 .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003547#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003548#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003549#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3550#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003551#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003552#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003553#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003554#define II(_f, _e, _i) \
3555 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003556#define IIP(_f, _e, _i, _p) \
3557 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3558 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003559#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003560
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003561#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003562#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003563#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003564#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003565#define I2bvIP(_f, _e, _i, _p) \
3566 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003567
Avi Kivityfb864fb2013-01-04 16:18:54 +02003568#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3569 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3570 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003571
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003572static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003573 DI(SrcNone | Priv, monitor),
3574 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003575 N, N, N, N, N, N,
3576};
3577
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003578static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003579 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003580 II(SrcNone | Prot | EmulateOnUD, em_vmmcall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003581 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3582 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3583 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3584 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3585 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3586 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003587};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003588
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003589static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003590 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003591 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003592 N, N, N, N, N, N,
3593};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003594
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003595static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003596 F(Lock, em_add),
3597 F(Lock | PageTable, em_or),
3598 F(Lock, em_adc),
3599 F(Lock, em_sbb),
3600 F(Lock | PageTable, em_and),
3601 F(Lock, em_sub),
3602 F(Lock, em_xor),
3603 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003604};
3605
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003606static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003607 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003608};
3609
Avi Kivity007a3b52013-01-19 19:51:51 +02003610static const struct opcode group2[] = {
3611 F(DstMem | ModRM, em_rol),
3612 F(DstMem | ModRM, em_ror),
3613 F(DstMem | ModRM, em_rcl),
3614 F(DstMem | ModRM, em_rcr),
3615 F(DstMem | ModRM, em_shl),
3616 F(DstMem | ModRM, em_shr),
3617 F(DstMem | ModRM, em_shl),
3618 F(DstMem | ModRM, em_sar),
3619};
3620
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003621static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003622 F(DstMem | SrcImm | NoWrite, em_test),
3623 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003624 F(DstMem | SrcNone | Lock, em_not),
3625 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02003626 F(DstXacc | Src2Mem, em_mul_ex),
3627 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02003628 F(DstXacc | Src2Mem, em_div_ex),
3629 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003630};
3631
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003632static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003633 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3634 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003635 N, N, N, N, N, N,
3636};
3637
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003638static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003639 F(DstMem | SrcNone | Lock, em_inc),
3640 F(DstMem | SrcNone | Lock, em_dec),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003641 I(SrcMem | Stack, em_grp45),
3642 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3643 I(SrcMem | Stack, em_grp45),
3644 I(SrcMemFAddr | ImplicitOps, em_grp45),
Gleb Natapov188424b2013-04-11 12:32:14 +03003645 I(SrcMem | Stack, em_grp45), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003646};
3647
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003648static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003649 DI(Prot, sldt),
3650 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003651 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003652 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003653 N, N, N, N,
3654};
3655
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003656static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03003657 II(Mov | DstMem, em_sgdt, sgdt),
3658 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003659 II(SrcMem | Priv, em_lgdt, lgdt),
3660 II(SrcMem | Priv, em_lidt, lidt),
3661 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3662 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3663 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003664}, {
Borislav Petkovb51e9742013-09-22 16:44:52 +02003665 I(SrcNone | Priv | EmulateOnUD, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003666 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003667 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003668 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3669 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3670 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003671} };
3672
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003673static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003674 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003675 F(DstMem | SrcImmByte | NoWrite, em_bt),
3676 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3677 F(DstMem | SrcImmByte | Lock, em_btr),
3678 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003679};
3680
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003681static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003682 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003683}, {
3684 N, N, N, N, N, N, N, N,
3685} };
3686
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003687static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003688 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003689 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003690};
3691
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003692static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003693 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003694};
3695
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003696static const struct gprefix pfx_vmovntpx = {
Avi Kivity3e114eb2012-04-09 18:40:01 +03003697 I(0, em_mov), N, N, N,
3698};
3699
Igor Mammedov27ce8252014-03-15 21:01:59 +01003700static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01003701 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003702};
3703
Gleb Natapov045a2822012-12-20 16:57:43 +02003704static const struct escape escape_d9 = { {
3705 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3706}, {
3707 /* 0xC0 - 0xC7 */
3708 N, N, N, N, N, N, N, N,
3709 /* 0xC8 - 0xCF */
3710 N, N, N, N, N, N, N, N,
3711 /* 0xD0 - 0xC7 */
3712 N, N, N, N, N, N, N, N,
3713 /* 0xD8 - 0xDF */
3714 N, N, N, N, N, N, N, N,
3715 /* 0xE0 - 0xE7 */
3716 N, N, N, N, N, N, N, N,
3717 /* 0xE8 - 0xEF */
3718 N, N, N, N, N, N, N, N,
3719 /* 0xF0 - 0xF7 */
3720 N, N, N, N, N, N, N, N,
3721 /* 0xF8 - 0xFF */
3722 N, N, N, N, N, N, N, N,
3723} };
3724
3725static const struct escape escape_db = { {
3726 N, N, N, N, N, N, N, N,
3727}, {
3728 /* 0xC0 - 0xC7 */
3729 N, N, N, N, N, N, N, N,
3730 /* 0xC8 - 0xCF */
3731 N, N, N, N, N, N, N, N,
3732 /* 0xD0 - 0xC7 */
3733 N, N, N, N, N, N, N, N,
3734 /* 0xD8 - 0xDF */
3735 N, N, N, N, N, N, N, N,
3736 /* 0xE0 - 0xE7 */
3737 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3738 /* 0xE8 - 0xEF */
3739 N, N, N, N, N, N, N, N,
3740 /* 0xF0 - 0xF7 */
3741 N, N, N, N, N, N, N, N,
3742 /* 0xF8 - 0xFF */
3743 N, N, N, N, N, N, N, N,
3744} };
3745
3746static const struct escape escape_dd = { {
3747 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3748}, {
3749 /* 0xC0 - 0xC7 */
3750 N, N, N, N, N, N, N, N,
3751 /* 0xC8 - 0xCF */
3752 N, N, N, N, N, N, N, N,
3753 /* 0xD0 - 0xC7 */
3754 N, N, N, N, N, N, N, N,
3755 /* 0xD8 - 0xDF */
3756 N, N, N, N, N, N, N, N,
3757 /* 0xE0 - 0xE7 */
3758 N, N, N, N, N, N, N, N,
3759 /* 0xE8 - 0xEF */
3760 N, N, N, N, N, N, N, N,
3761 /* 0xF0 - 0xF7 */
3762 N, N, N, N, N, N, N, N,
3763 /* 0xF8 - 0xFF */
3764 N, N, N, N, N, N, N, N,
3765} };
3766
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003767static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003768 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003769 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003770 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3771 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003772 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003773 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003774 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3775 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003776 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003777 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003778 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3779 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003780 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003781 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003782 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3783 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003784 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003785 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003786 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003787 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003788 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003789 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003790 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003791 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003792 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02003793 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003794 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003795 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003796 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003797 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003798 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003799 I(ImplicitOps | Stack | No64, em_pusha),
3800 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003801 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3802 N, N, N, N,
3803 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003804 I(SrcImm | Mov | Stack, em_push),
3805 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003806 I(SrcImmByte | Mov | Stack, em_push),
3807 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003808 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003809 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003810 /* 0x70 - 0x7F */
3811 X16(D(SrcImmByte)),
3812 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003813 G(ByteOp | DstMem | SrcImm, group1),
3814 G(DstMem | SrcImm, group1),
3815 G(ByteOp | DstMem | SrcImm | No64, group1),
3816 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003817 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003818 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003819 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003820 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003821 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003822 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003823 D(ModRM | SrcMem | NoAccess | DstReg),
3824 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3825 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003826 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003827 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003828 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003829 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003830 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003831 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01003832 II(ImplicitOps | Stack, em_popf, popf),
3833 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003834 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003835 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003836 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003837 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003838 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003839 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003840 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003841 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3842 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003843 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003844 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003845 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003846 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02003847 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003848 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003849 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003850 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003851 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003852 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3853 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003854 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003855 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003856 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Bruce Rogers32611072013-09-09 09:40:20 -06003857 I(ImplicitOps | Stack | SrcImmU16, em_ret_far_imm),
3858 I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003859 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003860 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003861 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003862 G(Src2One | ByteOp, group2), G(Src2One, group2),
3863 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003864 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02003865 I(DstAcc | SrcImmUByte | No64, em_aad),
3866 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02003867 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003868 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02003869 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003870 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003871 X3(I(SrcImmByte, em_loop)),
3872 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003873 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3874 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003875 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003876 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003877 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003878 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3879 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003880 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003881 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003882 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3883 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003884 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003885 D(ImplicitOps), D(ImplicitOps),
3886 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003887 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3888};
3889
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003890static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003891 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003892 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02003893 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003894 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003895 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003896 N, D(ImplicitOps | ModRM), N, N,
3897 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02003898 N, N, N, N, N, N, N, N,
3899 D(ImplicitOps | ModRM), N, N, N, N, N, N, D(ImplicitOps | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003900 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03003901 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
3902 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
3903 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
3904 check_cr_write),
3905 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
3906 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003907 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003908 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
3909 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
3910 N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
Avi Kivity3e114eb2012-04-09 18:40:01 +03003911 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003912 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003913 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02003914 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003915 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02003916 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003917 I(ImplicitOps | EmulateOnUD, em_sysenter),
3918 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003919 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003920 N, N, N, N, N, N, N, N,
3921 /* 0x40 - 0x4F */
3922 X16(D(DstReg | SrcMem | ModRM | Mov)),
3923 /* 0x50 - 0x5F */
3924 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3925 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003926 N, N, N, N,
3927 N, N, N, N,
3928 N, N, N, N,
3929 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003930 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003931 N, N, N, N,
3932 N, N, N, N,
3933 N, N, N, N,
3934 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003935 /* 0x80 - 0x8F */
3936 X16(D(SrcImm)),
3937 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003938 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003939 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003940 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02003941 II(ImplicitOps, em_cpuid, cpuid),
3942 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02003943 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
3944 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003945 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003946 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003947 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02003948 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02003949 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
3950 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Avi Kivity4d758342013-01-19 19:51:55 +02003951 D(ModRM), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003952 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09003953 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03003954 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02003955 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03003956 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
3957 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003958 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003959 /* 0xB8 - 0xBF */
3960 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003961 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02003962 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
3963 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003964 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03003965 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02003966 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003967 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003968 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03003969 /* 0xC8 - 0xCF */
3970 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003971 /* 0xD0 - 0xDF */
3972 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3973 /* 0xE0 - 0xEF */
3974 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3975 /* 0xF0 - 0xFF */
3976 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3977};
3978
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003979static const struct gprefix three_byte_0f_38_f0 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01003980 I(DstReg | SrcMem | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003981};
3982
3983static const struct gprefix three_byte_0f_38_f1 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01003984 I(DstMem | SrcReg | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003985};
3986
3987/*
3988 * Insns below are selected by the prefix which indexed by the third opcode
3989 * byte.
3990 */
3991static const struct opcode opcode_map_0f_38[256] = {
3992 /* 0x00 - 0x7f */
3993 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01003994 /* 0x80 - 0xef */
3995 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
3996 /* 0xf0 - 0xf1 */
3997 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f0),
3998 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f1),
3999 /* 0xf2 - 0xff */
4000 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004001};
4002
Avi Kivity73fba5f2010-07-29 15:11:53 +03004003#undef D
4004#undef N
4005#undef G
4006#undef GD
4007#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004008#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004009#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004010
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004011#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004012#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004013#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004014#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004015#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004016
Avi Kivity9dac77f2011-06-01 15:34:25 +03004017static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004018{
4019 unsigned size;
4020
Avi Kivity9dac77f2011-06-01 15:34:25 +03004021 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004022 if (size == 8)
4023 size = 4;
4024 return size;
4025}
4026
4027static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4028 unsigned size, bool sign_extension)
4029{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004030 int rc = X86EMUL_CONTINUE;
4031
4032 op->type = OP_IMM;
4033 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004034 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004035 /* NB. Immediates are sign-extended as necessary. */
4036 switch (op->bytes) {
4037 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004038 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004039 break;
4040 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004041 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004042 break;
4043 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004044 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004045 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004046 case 8:
4047 op->val = insn_fetch(s64, ctxt);
4048 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004049 }
4050 if (!sign_extension) {
4051 switch (op->bytes) {
4052 case 1:
4053 op->val &= 0xff;
4054 break;
4055 case 2:
4056 op->val &= 0xffff;
4057 break;
4058 case 4:
4059 op->val &= 0xffffffff;
4060 break;
4061 }
4062 }
4063done:
4064 return rc;
4065}
4066
Avi Kivitya9945542011-09-13 10:45:41 +03004067static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4068 unsigned d)
4069{
4070 int rc = X86EMUL_CONTINUE;
4071
4072 switch (d) {
4073 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004074 decode_register_operand(ctxt, op);
Avi Kivitya9945542011-09-13 10:45:41 +03004075 break;
4076 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004077 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03004078 break;
4079 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004080 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004081 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03004082 *op = ctxt->memop;
4083 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004084 if (ctxt->d & BitOp)
Avi Kivitya9945542011-09-13 10:45:41 +03004085 fetch_bit_operand(ctxt);
4086 op->orig_val = op->val;
4087 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004088 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004089 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004090 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03004091 case OpAcc:
4092 op->type = OP_REG;
4093 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004094 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya9945542011-09-13 10:45:41 +03004095 fetch_register_operand(op);
4096 op->orig_val = op->val;
4097 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004098 case OpAccLo:
4099 op->type = OP_REG;
4100 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4101 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4102 fetch_register_operand(op);
4103 op->orig_val = op->val;
4104 break;
4105 case OpAccHi:
4106 if (ctxt->d & ByteOp) {
4107 op->type = OP_NONE;
4108 break;
4109 }
4110 op->type = OP_REG;
4111 op->bytes = ctxt->op_bytes;
4112 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4113 fetch_register_operand(op);
4114 op->orig_val = op->val;
4115 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004116 case OpDI:
4117 op->type = OP_MEM;
4118 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4119 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004120 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya9945542011-09-13 10:45:41 +03004121 op->addr.mem.seg = VCPU_SREG_ES;
4122 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004123 op->count = 1;
Avi Kivitya9945542011-09-13 10:45:41 +03004124 break;
4125 case OpDX:
4126 op->type = OP_REG;
4127 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004128 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya9945542011-09-13 10:45:41 +03004129 fetch_register_operand(op);
4130 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004131 case OpCL:
4132 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004133 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004134 break;
4135 case OpImmByte:
4136 rc = decode_imm(ctxt, op, 1, true);
4137 break;
4138 case OpOne:
4139 op->bytes = 1;
4140 op->val = 1;
4141 break;
4142 case OpImm:
4143 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4144 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004145 case OpImm64:
4146 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4147 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004148 case OpMem8:
4149 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004150 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004151 ctxt->memop.addr.reg = decode_register(ctxt,
4152 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004153 fetch_register_operand(&ctxt->memop);
4154 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004155 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004156 case OpMem16:
4157 ctxt->memop.bytes = 2;
4158 goto mem_common;
4159 case OpMem32:
4160 ctxt->memop.bytes = 4;
4161 goto mem_common;
4162 case OpImmU16:
4163 rc = decode_imm(ctxt, op, 2, false);
4164 break;
4165 case OpImmU:
4166 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4167 break;
4168 case OpSI:
4169 op->type = OP_MEM;
4170 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4171 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004172 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Avi Kivity0fe59122011-09-13 10:45:47 +03004173 op->addr.mem.seg = seg_override(ctxt);
4174 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004175 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004176 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004177 case OpXLat:
4178 op->type = OP_MEM;
4179 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4180 op->addr.mem.ea =
4181 register_address(ctxt,
4182 reg_read(ctxt, VCPU_REGS_RBX) +
4183 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
4184 op->addr.mem.seg = seg_override(ctxt);
4185 op->val = 0;
4186 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004187 case OpImmFAddr:
4188 op->type = OP_IMM;
4189 op->addr.mem.ea = ctxt->_eip;
4190 op->bytes = ctxt->op_bytes + 2;
4191 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4192 break;
4193 case OpMemFAddr:
4194 ctxt->memop.bytes = ctxt->op_bytes + 2;
4195 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004196 case OpES:
4197 op->val = VCPU_SREG_ES;
4198 break;
4199 case OpCS:
4200 op->val = VCPU_SREG_CS;
4201 break;
4202 case OpSS:
4203 op->val = VCPU_SREG_SS;
4204 break;
4205 case OpDS:
4206 op->val = VCPU_SREG_DS;
4207 break;
4208 case OpFS:
4209 op->val = VCPU_SREG_FS;
4210 break;
4211 case OpGS:
4212 op->val = VCPU_SREG_GS;
4213 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004214 case OpImplicit:
4215 /* Special instructions do their own operand decoding. */
4216 default:
4217 op->type = OP_NONE; /* Disable writeback. */
4218 break;
4219 }
4220
4221done:
4222 return rc;
4223}
4224
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004225int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004226{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004227 int rc = X86EMUL_CONTINUE;
4228 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004229 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004230 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03004231 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004232
Avi Kivityf09ed832011-09-13 10:45:40 +03004233 ctxt->memop.type = OP_NONE;
4234 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004235 ctxt->_eip = ctxt->eip;
4236 ctxt->fetch.start = ctxt->_eip;
4237 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004238 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01004239 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004240 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004241
4242 switch (mode) {
4243 case X86EMUL_MODE_REAL:
4244 case X86EMUL_MODE_VM86:
4245 case X86EMUL_MODE_PROT16:
4246 def_op_bytes = def_ad_bytes = 2;
4247 break;
4248 case X86EMUL_MODE_PROT32:
4249 def_op_bytes = def_ad_bytes = 4;
4250 break;
4251#ifdef CONFIG_X86_64
4252 case X86EMUL_MODE_PROT64:
4253 def_op_bytes = 4;
4254 def_ad_bytes = 8;
4255 break;
4256#endif
4257 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004258 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004259 }
4260
Avi Kivity9dac77f2011-06-01 15:34:25 +03004261 ctxt->op_bytes = def_op_bytes;
4262 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004263
4264 /* Legacy prefixes. */
4265 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004266 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004267 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004268 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004269 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004270 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004271 break;
4272 case 0x67: /* address-size override */
4273 if (mode == X86EMUL_MODE_PROT64)
4274 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004275 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004276 else
4277 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004278 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004279 break;
4280 case 0x26: /* ES override */
4281 case 0x2e: /* CS override */
4282 case 0x36: /* SS override */
4283 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004284 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004285 break;
4286 case 0x64: /* FS override */
4287 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004288 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004289 break;
4290 case 0x40 ... 0x4f: /* REX */
4291 if (mode != X86EMUL_MODE_PROT64)
4292 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004293 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004294 continue;
4295 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004296 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004297 break;
4298 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004299 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004300 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004301 break;
4302 default:
4303 goto done_prefixes;
4304 }
4305
4306 /* Any legacy prefix after a REX prefix nullifies its effect. */
4307
Avi Kivity9dac77f2011-06-01 15:34:25 +03004308 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004309 }
4310
4311done_prefixes:
4312
4313 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004314 if (ctxt->rex_prefix & 8)
4315 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004316
4317 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004318 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004319 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004320 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004321 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004322 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004323 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004324
4325 /* 0F_38 opcode map */
4326 if (ctxt->b == 0x38) {
4327 ctxt->opcode_len = 3;
4328 ctxt->b = insn_fetch(u8, ctxt);
4329 opcode = opcode_map_0f_38[ctxt->b];
4330 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004331 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004332 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004333
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004334 if (ctxt->d & ModRM)
4335 ctxt->modrm = insn_fetch(u8, ctxt);
4336
Nadav Amit7fe864d2014-06-02 18:34:03 +03004337 /* vex-prefix instructions are not implemented */
4338 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
4339 (mode == X86EMUL_MODE_PROT64 ||
4340 (mode >= X86EMUL_MODE_PROT16 && (ctxt->modrm & 0x80)))) {
4341 ctxt->d = NotImpl;
4342 }
4343
Avi Kivity9dac77f2011-06-01 15:34:25 +03004344 while (ctxt->d & GroupMask) {
4345 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004346 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004347 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004348 opcode = opcode.u.group[goffset];
4349 break;
4350 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004351 goffset = (ctxt->modrm >> 3) & 7;
4352 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004353 opcode = opcode.u.gdual->mod3[goffset];
4354 else
4355 opcode = opcode.u.gdual->mod012[goffset];
4356 break;
4357 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004358 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004359 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004360 break;
4361 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004362 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004363 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004364 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004365 switch (simd_prefix) {
4366 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4367 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4368 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4369 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4370 }
4371 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004372 case Escape:
4373 if (ctxt->modrm > 0xbf)
4374 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4375 else
4376 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4377 break;
Avi Kivity46561642011-04-24 14:09:59 +03004378 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004379 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004380 }
Avi Kivity46561642011-04-24 14:09:59 +03004381
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004382 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004383 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004384 }
4385
Avi Kivity9dac77f2011-06-01 15:34:25 +03004386 ctxt->execute = opcode.u.execute;
4387 ctxt->check_perm = opcode.check_perm;
4388 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004389
4390 /* Unrecognised? */
Gleb Natapov1146a782013-04-11 12:30:01 +03004391 if (ctxt->d == 0 || (ctxt->d & NotImpl))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004392 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004393
Borislav Petkovb51e9742013-09-22 16:44:52 +02004394 if (!(ctxt->d & EmulateOnUD) && ctxt->ud)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004395 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004396
Avi Kivity9dac77f2011-06-01 15:34:25 +03004397 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
4398 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004399
Avi Kivity9dac77f2011-06-01 15:34:25 +03004400 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004401 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004402 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004403 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03004404 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004405 }
4406
Avi Kivity9dac77f2011-06-01 15:34:25 +03004407 if (ctxt->d & Sse)
4408 ctxt->op_bytes = 16;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004409 else if (ctxt->d & Mmx)
4410 ctxt->op_bytes = 8;
Avi Kivity12537912011-03-29 11:41:27 +02004411
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004412 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004413 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004414 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004415 if (!ctxt->has_seg_override)
4416 set_seg_override(ctxt, ctxt->modrm_seg);
4417 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004418 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004419 if (rc != X86EMUL_CONTINUE)
4420 goto done;
4421
Avi Kivity9dac77f2011-06-01 15:34:25 +03004422 if (!ctxt->has_seg_override)
4423 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004424
Avi Kivityf09ed832011-09-13 10:45:40 +03004425 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004426
Avi Kivityf09ed832011-09-13 10:45:40 +03004427 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
4428 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004429
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004430 /*
4431 * Decode and fetch the source operand: register, memory
4432 * or immediate.
4433 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004434 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004435 if (rc != X86EMUL_CONTINUE)
4436 goto done;
4437
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004438 /*
4439 * Decode and fetch the second source operand: register, memory
4440 * or immediate.
4441 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004442 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004443 if (rc != X86EMUL_CONTINUE)
4444 goto done;
4445
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004446 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03004447 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004448
4449done:
Avi Kivityf09ed832011-09-13 10:45:40 +03004450 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
4451 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004452
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004453 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004454}
4455
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004456bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4457{
4458 return ctxt->d & PageTable;
4459}
4460
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004461static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4462{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004463 /* The second termination condition only applies for REPE
4464 * and REPNE. Test if the repeat string operation prefix is
4465 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4466 * corresponding termination condition according to:
4467 * - if REPE/REPZ and ZF = 0 then done
4468 * - if REPNE/REPNZ and ZF = 1 then done
4469 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004470 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4471 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4472 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004473 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004474 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004475 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4476 return true;
4477
4478 return false;
4479}
4480
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004481static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4482{
4483 bool fault = false;
4484
4485 ctxt->ops->get_fpu(ctxt);
4486 asm volatile("1: fwait \n\t"
4487 "2: \n\t"
4488 ".pushsection .fixup,\"ax\" \n\t"
4489 "3: \n\t"
4490 "movb $1, %[fault] \n\t"
4491 "jmp 2b \n\t"
4492 ".popsection \n\t"
4493 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004494 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004495 ctxt->ops->put_fpu(ctxt);
4496
4497 if (unlikely(fault))
4498 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4499
4500 return X86EMUL_CONTINUE;
4501}
4502
4503static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4504 struct operand *op)
4505{
4506 if (op->type == OP_MM)
4507 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4508}
4509
Avi Kivitye28bbd42013-01-04 16:18:48 +02004510static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4511{
4512 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivityb9fa4092013-02-09 11:31:48 +02004513 if (!(ctxt->d & ByteOp))
4514 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Avi Kivitye28bbd42013-01-04 16:18:48 +02004515 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004516 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
4517 [fastop]"+S"(fop)
4518 : "c"(ctxt->src2.val));
Avi Kivitye28bbd42013-01-04 16:18:48 +02004519 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004520 if (!fop) /* exception is returned in fop variable */
4521 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02004522 return X86EMUL_CONTINUE;
4523}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004524
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004525int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004526{
Mathias Krause0225fb52012-08-30 01:30:16 +02004527 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004528 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004529 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004530
Avi Kivity9dac77f2011-06-01 15:34:25 +03004531 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004532
Gleb Natapov1146a782013-04-11 12:30:01 +03004533 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4534 (ctxt->d & Undefined)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004535 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02004536 goto done;
4537 }
4538
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004539 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004540 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004541 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004542 goto done;
4543 }
4544
Avi Kivity9dac77f2011-06-01 15:34:25 +03004545 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004546 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004547 goto done;
4548 }
4549
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004550 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4551 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02004552 rc = emulate_ud(ctxt);
4553 goto done;
4554 }
4555
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004556 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02004557 rc = emulate_nm(ctxt);
4558 goto done;
4559 }
4560
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004561 if (ctxt->d & Mmx) {
4562 rc = flush_pending_x87_faults(ctxt);
4563 if (rc != X86EMUL_CONTINUE)
4564 goto done;
4565 /*
4566 * Now that we know the fpu is exception safe, we can fetch
4567 * operands from it.
4568 */
4569 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4570 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4571 if (!(ctxt->d & Mov))
4572 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4573 }
4574
Avi Kivity9dac77f2011-06-01 15:34:25 +03004575 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4576 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004577 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004578 if (rc != X86EMUL_CONTINUE)
4579 goto done;
4580 }
4581
Gleb Natapove92805a2010-02-10 14:21:35 +02004582 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004583 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004584 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02004585 goto done;
4586 }
4587
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004588 /* Instruction can only be executed in protected mode */
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03004589 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004590 rc = emulate_ud(ctxt);
4591 goto done;
4592 }
4593
Joerg Roedeld09beab2011-04-04 12:39:25 +02004594 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004595 if (ctxt->check_perm) {
4596 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004597 if (rc != X86EMUL_CONTINUE)
4598 goto done;
4599 }
4600
Avi Kivity9dac77f2011-06-01 15:34:25 +03004601 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4602 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004603 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004604 if (rc != X86EMUL_CONTINUE)
4605 goto done;
4606 }
4607
Avi Kivity9dac77f2011-06-01 15:34:25 +03004608 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004609 /* All REP prefixes have the same first termination condition */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004610 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004611 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004612 goto done;
4613 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004614 }
4615
Avi Kivity9dac77f2011-06-01 15:34:25 +03004616 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4617 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4618 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004619 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004620 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004621 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004622 }
4623
Avi Kivity9dac77f2011-06-01 15:34:25 +03004624 if (ctxt->src2.type == OP_MEM) {
4625 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4626 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004627 if (rc != X86EMUL_CONTINUE)
4628 goto done;
4629 }
4630
Avi Kivity9dac77f2011-06-01 15:34:25 +03004631 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004632 goto special_insn;
4633
4634
Avi Kivity9dac77f2011-06-01 15:34:25 +03004635 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004636 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004637 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4638 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004639 if (rc != X86EMUL_CONTINUE)
4640 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004641 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004642 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004643
Avi Kivity018a98d2007-11-27 19:30:56 +02004644special_insn:
4645
Avi Kivity9dac77f2011-06-01 15:34:25 +03004646 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4647 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004648 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004649 if (rc != X86EMUL_CONTINUE)
4650 goto done;
4651 }
4652
Avi Kivity9dac77f2011-06-01 15:34:25 +03004653 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004654 if (ctxt->d & Fastop) {
4655 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4656 rc = fastop(ctxt, fop);
4657 if (rc != X86EMUL_CONTINUE)
4658 goto done;
4659 goto writeback;
4660 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004661 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004662 if (rc != X86EMUL_CONTINUE)
4663 goto done;
4664 goto writeback;
4665 }
4666
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004667 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004668 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004669 else if (ctxt->opcode_len == 3)
4670 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004671
Avi Kivity9dac77f2011-06-01 15:34:25 +03004672 switch (ctxt->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004673 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004674 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004675 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004676 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004677 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004678 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004679 if (test_cc(ctxt->b, ctxt->eflags))
4680 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004681 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004682 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004683 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004684 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004685 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004686 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Mohammed Gamal34698d82010-08-04 14:41:04 +03004687 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004688 rc = em_xchg(ctxt);
4689 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004690 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004691 switch (ctxt->op_bytes) {
4692 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4693 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4694 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004695 }
4696 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004697 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004698 rc = emulate_int(ctxt, 3);
4699 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004700 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004701 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004702 break;
4703 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004704 if (ctxt->eflags & EFLG_OF)
4705 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004706 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004707 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004708 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004709 jmp_rel(ctxt, ctxt->src.val);
4710 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004711 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004712 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004713 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004714 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004715 case 0xf5: /* cmc */
4716 /* complement carry flag from eflags reg */
4717 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004718 break;
4719 case 0xf8: /* clc */
4720 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004721 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004722 case 0xf9: /* stc */
4723 ctxt->eflags |= EFLG_CF;
4724 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004725 case 0xfc: /* cld */
4726 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004727 break;
4728 case 0xfd: /* std */
4729 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004730 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004731 default:
4732 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004733 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004734
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004735 if (rc != X86EMUL_CONTINUE)
4736 goto done;
4737
Avi Kivity018a98d2007-11-27 19:30:56 +02004738writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02004739 if (!(ctxt->d & NoWrite)) {
4740 rc = writeback(ctxt, &ctxt->dst);
4741 if (rc != X86EMUL_CONTINUE)
4742 goto done;
4743 }
4744 if (ctxt->d & SrcWrite) {
4745 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
4746 rc = writeback(ctxt, &ctxt->src);
4747 if (rc != X86EMUL_CONTINUE)
4748 goto done;
4749 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004750
Gleb Natapov5cd21912010-03-18 15:20:26 +02004751 /*
4752 * restore dst type in case the decoding will be reused
4753 * (happens for string instruction )
4754 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004755 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004756
Avi Kivity9dac77f2011-06-01 15:34:25 +03004757 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004758 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004759
Avi Kivity9dac77f2011-06-01 15:34:25 +03004760 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004761 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004762
Avi Kivity9dac77f2011-06-01 15:34:25 +03004763 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004764 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004765 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004766 if ((ctxt->d & SrcMask) == SrcSI)
4767 count = ctxt->src.count;
4768 else
4769 count = ctxt->dst.count;
4770 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4771 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004772
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004773 if (!string_insn_completed(ctxt)) {
4774 /*
4775 * Re-enter guest when pio read ahead buffer is empty
4776 * or, if it is not used, after each 1024 iteration.
4777 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004778 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004779 (r->end == 0 || r->end != r->pos)) {
4780 /*
4781 * Reset read cache. Usually happens before
4782 * decode, but since instruction is restarted
4783 * we have to do it here.
4784 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004785 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004786 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004787 return EMULATION_RESTART;
4788 }
4789 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004790 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004791 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004792
Avi Kivity9dac77f2011-06-01 15:34:25 +03004793 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004794
4795done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004796 if (rc == X86EMUL_PROPAGATE_FAULT)
4797 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004798 if (rc == X86EMUL_INTERCEPTED)
4799 return EMULATION_INTERCEPTED;
4800
Avi Kivitydd856ef2012-08-27 23:46:17 +03004801 if (rc == X86EMUL_CONTINUE)
4802 writeback_registers(ctxt);
4803
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004804 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004805
4806twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004807 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004808 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004809 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004810 break;
4811 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004812 case 0x0d: /* GrpP (prefetch) */
4813 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004814 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02004815 break;
4816 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004817 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004818 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004819 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004820 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004821 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004822 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004823 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4824 if (!test_cc(ctxt->b, ctxt->eflags))
4825 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004826 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004827 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004828 if (test_cc(ctxt->b, ctxt->eflags))
4829 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004830 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004831 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004832 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004833 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004834 case 0xae: /* clflush */
4835 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004836 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004837 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004838 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004839 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004840 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004841 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004842 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004843 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004844 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004845 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004846 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004847 ctxt->dst.bytes = ctxt->op_bytes;
Nadav Amit3b320042014-06-02 18:34:08 +03004848 ctxt->dst.val = (ctxt->op_bytes == 8) ? (u64) ctxt->src.val :
4849 (u32) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004850 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004851 default:
4852 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004853 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004854
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004855threebyte_insn:
4856
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004857 if (rc != X86EMUL_CONTINUE)
4858 goto done;
4859
Avi Kivity6aa8b732006-12-10 02:21:36 -08004860 goto writeback;
4861
4862cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004863 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004864}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004865
4866void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
4867{
4868 invalidate_registers(ctxt);
4869}
4870
4871void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
4872{
4873 writeback_registers(ctxt);
4874}