blob: 15453e569f3d612114fc344444eac314c152c2df [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,
Jan Kiszka6cbc5f52014-06-30 12:52:55 +0200429 .dst_val = ctxt->dst.val64,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300430 .src_bytes = ctxt->src.bytes,
431 .dst_bytes = ctxt->dst.bytes,
432 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200433 .next_rip = ctxt->eip,
434 };
435
Avi Kivity29535382011-04-20 13:37:53 +0300436 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200437}
438
Avi Kivityf47cfa32012-06-07 17:49:24 +0300439static void assign_masked(ulong *dest, ulong src, ulong mask)
440{
441 *dest = (*dest & ~mask) | (src & mask);
442}
443
Avi Kivity9dac77f2011-06-01 15:34:25 +0300444static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800445{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300446 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800447}
448
Avi Kivityf47cfa32012-06-07 17:49:24 +0300449static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
450{
451 u16 sel;
452 struct desc_struct ss;
453
454 if (ctxt->mode == X86EMUL_MODE_PROT64)
455 return ~0UL;
456 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
457 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
458}
459
Avi Kivity612e89f2012-06-12 20:03:23 +0300460static int stack_size(struct x86_emulate_ctxt *ctxt)
461{
462 return (__fls(stack_mask(ctxt)) + 1) >> 3;
463}
464
Avi Kivity6aa8b732006-12-10 02:21:36 -0800465/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800466static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300467address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800468{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300469 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800470 return reg;
471 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300472 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800473}
474
475static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300476register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800477{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300478 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800479}
480
Avi Kivity5ad105e2012-08-19 14:34:31 +0300481static void masked_increment(ulong *reg, ulong mask, int inc)
482{
483 assign_masked(reg, *reg + inc, mask);
484}
485
Harvey Harrison7a9572752008-02-19 07:40:41 -0800486static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300487register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800488{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300489 ulong mask;
490
Avi Kivity9dac77f2011-06-01 15:34:25 +0300491 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300492 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800493 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300494 mask = ad_mask(ctxt);
495 masked_increment(reg, mask, inc);
496}
497
498static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
499{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300500 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800501}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800502
Avi Kivity9dac77f2011-06-01 15:34:25 +0300503static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800504{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300505 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800506}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300507
Avi Kivity56697682011-04-03 14:08:51 +0300508static u32 desc_limit_scaled(struct desc_struct *desc)
509{
510 u32 limit = get_desc_limit(desc);
511
512 return desc->g ? (limit << 12) | 0xfff : limit;
513}
514
Avi Kivity9dac77f2011-06-01 15:34:25 +0300515static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300516{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300517 ctxt->has_seg_override = true;
518 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300519}
520
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900521static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300522{
523 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
524 return 0;
525
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900526 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300527}
528
Avi Kivity9dac77f2011-06-01 15:34:25 +0300529static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300530{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300531 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300532 return 0;
533
Avi Kivity9dac77f2011-06-01 15:34:25 +0300534 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300535}
536
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200537static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
538 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300539{
Avi Kivityda9cb572010-11-22 17:53:21 +0200540 ctxt->exception.vector = vec;
541 ctxt->exception.error_code = error;
542 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200543 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300544}
545
Joerg Roedel3b88e412011-04-04 12:39:29 +0200546static int emulate_db(struct x86_emulate_ctxt *ctxt)
547{
548 return emulate_exception(ctxt, DB_VECTOR, 0, false);
549}
550
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200551static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300552{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200553 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300554}
555
Avi Kivity618ff152011-04-03 12:32:09 +0300556static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
557{
558 return emulate_exception(ctxt, SS_VECTOR, err, true);
559}
560
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200561static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300562{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200563 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300564}
565
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200566static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300567{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200568 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300569}
570
Avi Kivity34d1f492010-08-26 11:59:01 +0300571static int emulate_de(struct x86_emulate_ctxt *ctxt)
572{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200573 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300574}
575
Avi Kivity12537912011-03-29 11:41:27 +0200576static int emulate_nm(struct x86_emulate_ctxt *ctxt)
577{
578 return emulate_exception(ctxt, NM_VECTOR, 0, false);
579}
580
Avi Kivity1aa36612011-04-27 13:20:30 +0300581static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
582{
583 u16 selector;
584 struct desc_struct desc;
585
586 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
587 return selector;
588}
589
590static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
591 unsigned seg)
592{
593 u16 dummy;
594 u32 base3;
595 struct desc_struct desc;
596
597 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
598 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
599}
600
Avi Kivity1c11b372012-04-09 18:39:59 +0300601/*
602 * x86 defines three classes of vector instructions: explicitly
603 * aligned, explicitly unaligned, and the rest, which change behaviour
604 * depending on whether they're AVX encoded or not.
605 *
606 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
607 * subject to the same check.
608 */
609static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
610{
611 if (likely(size < 16))
612 return false;
613
614 if (ctxt->d & Aligned)
615 return true;
616 else if (ctxt->d & Unaligned)
617 return false;
618 else if (ctxt->d & Avx)
619 return false;
620 else
621 return true;
622}
623
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400624static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300625 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400626 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300627 ulong *linear)
628{
Avi Kivity618ff152011-04-03 12:32:09 +0300629 struct desc_struct desc;
630 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300631 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300632 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300633 u16 sel;
Gleb Natapov3a78a4f2012-12-20 16:57:42 +0200634 unsigned cpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300635
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900636 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300637 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300638 case X86EMUL_MODE_PROT64:
639 if (((signed long)la << 16) >> 16 != la)
640 return emulate_gp(ctxt, 0);
641 break;
642 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300643 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
644 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300645 if (!usable)
646 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200647 /* code segment in protected mode or read-only data segment */
648 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
649 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300650 goto bad;
651 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400652 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300653 goto bad;
654 lim = desc_limit_scaled(&desc);
655 if ((desc.type & 8) || !(desc.type & 4)) {
656 /* expand-up segment */
657 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
658 goto bad;
659 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800660 /* expand-down segment */
Avi Kivity618ff152011-04-03 12:32:09 +0300661 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
662 goto bad;
663 lim = desc.d ? 0xffffffff : 0xffff;
664 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
665 goto bad;
666 }
Avi Kivity717746e2011-04-20 13:37:53 +0300667 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity618ff152011-04-03 12:32:09 +0300668 if (!(desc.type & 8)) {
669 /* data segment */
670 if (cpl > desc.dpl)
671 goto bad;
672 } else if ((desc.type & 8) && !(desc.type & 4)) {
673 /* nonconforming code segment */
674 if (cpl != desc.dpl)
675 goto bad;
676 } else if ((desc.type & 8) && (desc.type & 4)) {
677 /* conforming code segment */
678 if (cpl < desc.dpl)
679 goto bad;
680 }
681 break;
682 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300683 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300684 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300685 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
686 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300687 *linear = la;
688 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300689bad:
690 if (addr.seg == VCPU_SREG_SS)
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300691 return emulate_ss(ctxt, sel);
Avi Kivity618ff152011-04-03 12:32:09 +0300692 else
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300693 return emulate_gp(ctxt, sel);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300694}
695
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400696static int linearize(struct x86_emulate_ctxt *ctxt,
697 struct segmented_address addr,
698 unsigned size, bool write,
699 ulong *linear)
700{
701 return __linearize(ctxt, addr, size, write, false, linear);
702}
703
704
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200705static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
706 struct segmented_address addr,
707 void *data,
708 unsigned size)
709{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200710 int rc;
711 ulong linear;
712
Avi Kivity83b87952011-04-03 11:31:19 +0300713 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200714 if (rc != X86EMUL_CONTINUE)
715 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300716 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200717}
718
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900719/*
720 * Fetch the next byte of the instruction being emulated which is pointed to
721 * by ctxt->_eip, then increment ctxt->_eip.
722 *
723 * Also prefetch the remaining bytes of the instruction without crossing page
724 * boundary if they are not in fetch_cache yet.
725 */
726static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200727{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300728 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200729 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300730 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200731
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900732 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400733 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900734 struct segmented_address addr = { .seg = VCPU_SREG_CS,
735 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300736 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900737 size = min(15UL - cur_size,
738 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400739 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900740 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400741 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900742 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
743 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900744 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200745 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300746 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200747 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900748 *dest = fc->data[ctxt->_eip - fc->start];
749 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900750 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200751}
752
753static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900754 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200755{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900756 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200757
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200758 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900759 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200760 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200761 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900762 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900763 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200764 return rc;
765 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900766 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200767}
768
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900769/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900770#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900771({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900772 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900773 if (rc != X86EMUL_CONTINUE) \
774 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900775 (_type)_x; \
776})
777
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900778#define insn_fetch_arr(_arr, _size, _ctxt) \
779({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900780 if (rc != X86EMUL_CONTINUE) \
781 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900782})
783
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000784/*
785 * Given the 'reg' portion of a ModRM byte, and a register block, return a
786 * pointer into the block that addresses the relevant register.
787 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
788 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300789static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200790 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800791{
792 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200793 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800794
Avi Kivity6aa8b732006-12-10 02:21:36 -0800795 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300796 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
797 else
798 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800799 return p;
800}
801
802static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200803 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800804 u16 *size, unsigned long *address, int op_bytes)
805{
806 int rc;
807
808 if (op_bytes == 2)
809 op_bytes = 3;
810 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200811 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900812 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800813 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200814 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200815 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800816 return rc;
817}
818
Avi Kivity34b77652013-01-19 19:51:56 +0200819FASTOP2(add);
820FASTOP2(or);
821FASTOP2(adc);
822FASTOP2(sbb);
823FASTOP2(and);
824FASTOP2(sub);
825FASTOP2(xor);
826FASTOP2(cmp);
827FASTOP2(test);
828
Avi Kivityb9fa4092013-02-09 11:31:48 +0200829FASTOP1SRC2(mul, mul_ex);
830FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200831FASTOP1SRC2EX(div, div_ex);
832FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +0200833
Avi Kivity34b77652013-01-19 19:51:56 +0200834FASTOP3WCL(shld);
835FASTOP3WCL(shrd);
836
837FASTOP2W(imul);
838
839FASTOP1(not);
840FASTOP1(neg);
841FASTOP1(inc);
842FASTOP1(dec);
843
844FASTOP2CL(rol);
845FASTOP2CL(ror);
846FASTOP2CL(rcl);
847FASTOP2CL(rcr);
848FASTOP2CL(shl);
849FASTOP2CL(shr);
850FASTOP2CL(sar);
851
852FASTOP2W(bsf);
853FASTOP2W(bsr);
854FASTOP2W(bt);
855FASTOP2W(bts);
856FASTOP2W(btr);
857FASTOP2W(btc);
858
Avi Kivitye47a5f52013-02-09 11:31:51 +0200859FASTOP2(xadd);
860
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200861static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300862{
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200863 u8 rc;
864 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300865
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200866 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +0200867 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200868 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
869 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300870}
871
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300872static void fetch_register_operand(struct operand *op)
873{
874 switch (op->bytes) {
875 case 1:
876 op->val = *(u8 *)op->addr.reg;
877 break;
878 case 2:
879 op->val = *(u16 *)op->addr.reg;
880 break;
881 case 4:
882 op->val = *(u32 *)op->addr.reg;
883 break;
884 case 8:
885 op->val = *(u64 *)op->addr.reg;
886 break;
887 }
888}
889
Avi Kivity12537912011-03-29 11:41:27 +0200890static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
891{
892 ctxt->ops->get_fpu(ctxt);
893 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200894 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
895 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
896 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
897 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
898 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
899 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
900 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
901 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200902#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200903 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
904 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
905 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
906 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
907 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
908 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
909 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
910 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200911#endif
912 default: BUG();
913 }
914 ctxt->ops->put_fpu(ctxt);
915}
916
917static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
918 int reg)
919{
920 ctxt->ops->get_fpu(ctxt);
921 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200922 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
923 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
924 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
925 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
926 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
927 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
928 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
929 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200930#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200931 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
932 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
933 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
934 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
935 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
936 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
937 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
938 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200939#endif
940 default: BUG();
941 }
942 ctxt->ops->put_fpu(ctxt);
943}
944
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300945static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
946{
947 ctxt->ops->get_fpu(ctxt);
948 switch (reg) {
949 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
950 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
951 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
952 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
953 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
954 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
955 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
956 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
957 default: BUG();
958 }
959 ctxt->ops->put_fpu(ctxt);
960}
961
962static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
963{
964 ctxt->ops->get_fpu(ctxt);
965 switch (reg) {
966 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
967 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
968 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
969 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
970 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
971 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
972 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
973 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
974 default: BUG();
975 }
976 ctxt->ops->put_fpu(ctxt);
977}
978
Gleb Natapov045a2822012-12-20 16:57:43 +0200979static int em_fninit(struct x86_emulate_ctxt *ctxt)
980{
981 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
982 return emulate_nm(ctxt);
983
984 ctxt->ops->get_fpu(ctxt);
985 asm volatile("fninit");
986 ctxt->ops->put_fpu(ctxt);
987 return X86EMUL_CONTINUE;
988}
989
990static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
991{
992 u16 fcw;
993
994 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
995 return emulate_nm(ctxt);
996
997 ctxt->ops->get_fpu(ctxt);
998 asm volatile("fnstcw %0": "+m"(fcw));
999 ctxt->ops->put_fpu(ctxt);
1000
1001 /* force 2 byte destination */
1002 ctxt->dst.bytes = 2;
1003 ctxt->dst.val = fcw;
1004
1005 return X86EMUL_CONTINUE;
1006}
1007
1008static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1009{
1010 u16 fsw;
1011
1012 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1013 return emulate_nm(ctxt);
1014
1015 ctxt->ops->get_fpu(ctxt);
1016 asm volatile("fnstsw %0": "+m"(fsw));
1017 ctxt->ops->put_fpu(ctxt);
1018
1019 /* force 2 byte destination */
1020 ctxt->dst.bytes = 2;
1021 ctxt->dst.val = fsw;
1022
1023 return X86EMUL_CONTINUE;
1024}
1025
Avi Kivity12537912011-03-29 11:41:27 +02001026static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001027 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001028{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001029 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001030
Avi Kivity9dac77f2011-06-01 15:34:25 +03001031 if (!(ctxt->d & ModRM))
1032 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +02001033
Avi Kivity9dac77f2011-06-01 15:34:25 +03001034 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001035 op->type = OP_XMM;
1036 op->bytes = 16;
1037 op->addr.xmm = reg;
1038 read_sse_reg(ctxt, &op->vec_val, reg);
1039 return;
1040 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001041 if (ctxt->d & Mmx) {
1042 reg &= 7;
1043 op->type = OP_MM;
1044 op->bytes = 8;
1045 op->addr.mm = reg;
1046 return;
1047 }
Avi Kivity12537912011-03-29 11:41:27 +02001048
Avi Kivity3c118e22007-10-31 10:27:04 +02001049 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001050 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1051 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1052
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001053 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001054 op->orig_val = op->val;
1055}
1056
Avi Kivitya6e34072012-06-10 17:15:39 +03001057static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1058{
1059 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1060 ctxt->modrm_seg = VCPU_SREG_SS;
1061}
1062
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001063static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001064 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001065{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001066 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001067 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001068 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001069 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001070
Avi Kivity9dac77f2011-06-01 15:34:25 +03001071 if (ctxt->rex_prefix) {
1072 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
1073 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
1074 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001075 }
1076
Avi Kivity9dac77f2011-06-01 15:34:25 +03001077 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
1078 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
1079 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
1080 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001081
Nadav Amit9b88ae92014-05-25 23:05:21 +03001082 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001083 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001084 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001085 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001086 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001087 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001088 op->type = OP_XMM;
1089 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001090 op->addr.xmm = ctxt->modrm_rm;
1091 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001092 return rc;
1093 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001094 if (ctxt->d & Mmx) {
1095 op->type = OP_MM;
1096 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001097 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001098 return rc;
1099 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001100 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001101 return rc;
1102 }
1103
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001104 op->type = OP_MEM;
1105
Avi Kivity9dac77f2011-06-01 15:34:25 +03001106 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001107 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1108 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1109 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1110 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001111
1112 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001113 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001114 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001115 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001116 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001117 break;
1118 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001119 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001120 break;
1121 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001122 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001123 break;
1124 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001125 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001126 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001127 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001128 break;
1129 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001130 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001131 break;
1132 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001133 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001134 break;
1135 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001136 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001137 break;
1138 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001139 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001140 break;
1141 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001142 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001143 break;
1144 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001145 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001146 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001147 break;
1148 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001149 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001150 break;
1151 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001152 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1153 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1154 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001155 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001156 } else {
1157 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001158 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001159 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001160 index_reg |= (sib >> 3) & 7;
1161 base_reg |= sib & 7;
1162 scale = sib >> 6;
1163
Avi Kivity9dac77f2011-06-01 15:34:25 +03001164 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001165 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001166 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001167 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001168 adjust_modrm_seg(ctxt, base_reg);
1169 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001170 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001171 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001172 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001173 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001174 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001175 } else {
1176 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001177 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001178 adjust_modrm_seg(ctxt, base_reg);
1179 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001180 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001181 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001182 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001183 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001184 break;
1185 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001186 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001187 break;
1188 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001189 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001190 break;
1191 }
1192 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001193 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001194done:
1195 return rc;
1196}
1197
1198static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001199 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001200{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001201 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001202
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001203 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001204 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001205 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001206 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001207 break;
1208 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001209 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001210 break;
1211 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001212 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001213 break;
1214 }
1215done:
1216 return rc;
1217}
1218
Avi Kivity9dac77f2011-06-01 15:34:25 +03001219static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001220{
Sheng Yang7129eec2010-09-28 16:33:32 +08001221 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001222
Avi Kivity9dac77f2011-06-01 15:34:25 +03001223 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001224 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001225
Avi Kivity9dac77f2011-06-01 15:34:25 +03001226 if (ctxt->src.bytes == 2)
1227 sv = (s16)ctxt->src.val & (s16)mask;
1228 else if (ctxt->src.bytes == 4)
1229 sv = (s32)ctxt->src.val & (s32)mask;
Nadav Amit7dec5602014-06-15 16:12:57 +03001230 else
1231 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001232
Avi Kivity9dac77f2011-06-01 15:34:25 +03001233 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001234 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001235
1236 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001237 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001238}
1239
Gleb Natapov9de41572010-04-28 19:15:22 +03001240static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001241 unsigned long addr, void *dest, unsigned size)
1242{
1243 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001244 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001245
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001246 if (mc->pos < mc->end)
1247 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001248
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001249 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001250
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001251 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1252 &ctxt->exception);
1253 if (rc != X86EMUL_CONTINUE)
1254 return rc;
1255
1256 mc->end += size;
1257
1258read_cached:
1259 memcpy(dest, mc->data + mc->pos, size);
1260 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001261 return X86EMUL_CONTINUE;
1262}
1263
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001264static int segmented_read(struct x86_emulate_ctxt *ctxt,
1265 struct segmented_address addr,
1266 void *data,
1267 unsigned size)
1268{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001269 int rc;
1270 ulong linear;
1271
Avi Kivity83b87952011-04-03 11:31:19 +03001272 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001273 if (rc != X86EMUL_CONTINUE)
1274 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001275 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001276}
1277
1278static int segmented_write(struct x86_emulate_ctxt *ctxt,
1279 struct segmented_address addr,
1280 const void *data,
1281 unsigned size)
1282{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001283 int rc;
1284 ulong linear;
1285
Avi Kivity83b87952011-04-03 11:31:19 +03001286 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001287 if (rc != X86EMUL_CONTINUE)
1288 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001289 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1290 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001291}
1292
1293static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1294 struct segmented_address addr,
1295 const void *orig_data, const void *data,
1296 unsigned size)
1297{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001298 int rc;
1299 ulong linear;
1300
Avi Kivity83b87952011-04-03 11:31:19 +03001301 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001302 if (rc != X86EMUL_CONTINUE)
1303 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001304 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1305 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001306}
1307
Gleb Natapov7b262e92010-03-18 15:20:27 +02001308static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001309 unsigned int size, unsigned short port,
1310 void *dest)
1311{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001312 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001313
1314 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001315 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001316 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001317 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001318 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001319 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1320 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Gleb Natapov7b262e92010-03-18 15:20:27 +02001321 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1322 count);
1323 if (n == 0)
1324 n = 1;
1325 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001326 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001327 return 0;
1328 rc->end = n * size;
1329 }
1330
Nadav Amite6e39f02014-04-18 03:35:10 +03001331 if (ctxt->rep_prefix && (ctxt->d & String) &&
1332 !(ctxt->eflags & EFLG_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001333 ctxt->dst.data = rc->data + rc->pos;
1334 ctxt->dst.type = OP_MEM_STR;
1335 ctxt->dst.count = (rc->end - rc->pos) / size;
1336 rc->pos = rc->end;
1337 } else {
1338 memcpy(dest, rc->data + rc->pos, size);
1339 rc->pos += size;
1340 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001341 return 1;
1342}
1343
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001344static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1345 u16 index, struct desc_struct *desc)
1346{
1347 struct desc_ptr dt;
1348 ulong addr;
1349
1350 ctxt->ops->get_idt(ctxt, &dt);
1351
1352 if (dt.size < index * 8 + 7)
1353 return emulate_gp(ctxt, index << 3 | 0x2);
1354
1355 addr = dt.address + index * 8;
1356 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1357 &ctxt->exception);
1358}
1359
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001360static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001361 u16 selector, struct desc_ptr *dt)
1362{
Mathias Krause0225fb52012-08-30 01:30:16 +02001363 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001364 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001365
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001366 if (selector & 1 << 2) {
1367 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001368 u16 sel;
1369
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001370 memset (dt, 0, sizeof *dt);
Nadav Amit2eedcac2014-06-02 18:34:05 +03001371 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1372 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001373 return;
1374
1375 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001376 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001377 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001378 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001379}
1380
1381/* allowed just for 8 bytes segments */
1382static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001383 u16 selector, struct desc_struct *desc,
1384 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001385{
1386 struct desc_ptr dt;
1387 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001388 ulong addr;
1389
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001390 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001391
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001392 if (dt.size < index * 8 + 7)
1393 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001394
Avi Kivitye9194642012-06-13 16:29:39 +03001395 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001396 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1397 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001398}
1399
1400/* allowed just for 8 bytes segments */
1401static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001402 u16 selector, struct desc_struct *desc)
1403{
1404 struct desc_ptr dt;
1405 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001406 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001407
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001408 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001409
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001410 if (dt.size < index * 8 + 7)
1411 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001412
1413 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001414 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1415 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001416}
1417
Gleb Natapov5601d052011-03-07 14:55:06 +02001418/* Does not support long mode */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001419static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Paolo Bonzini5045b462014-05-15 18:09:29 +02001420 u16 selector, int seg, u8 cpl, bool in_task_switch)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001421{
Avi Kivity869be992012-06-13 16:30:53 +03001422 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001423 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001424 unsigned err_vec = GP_VECTOR;
1425 u32 err_code = 0;
1426 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001427 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001428 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001429 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001430 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001431
1432 memset(&seg_desc, 0, sizeof seg_desc);
1433
Kevin Wolff8da94e2013-04-11 14:06:03 +02001434 if (ctxt->mode == X86EMUL_MODE_REAL) {
1435 /* set real mode segment descriptor (keep limit etc. for
1436 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001437 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001438 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001439 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001440 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1441 /* VM86 needs a clean new segment descriptor */
1442 set_desc_base(&seg_desc, selector << 4);
1443 set_desc_limit(&seg_desc, 0xffff);
1444 seg_desc.type = 3;
1445 seg_desc.p = 1;
1446 seg_desc.s = 1;
1447 seg_desc.dpl = 3;
1448 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001449 }
1450
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001451 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001452
1453 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1454 if ((seg == VCPU_SREG_CS
1455 || (seg == VCPU_SREG_SS
1456 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1457 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001458 && null_selector)
1459 goto exception;
1460
1461 /* TR should be in GDT only */
1462 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1463 goto exception;
1464
1465 if (null_selector) /* for NULL selector skip all following checks */
1466 goto load;
1467
Avi Kivitye9194642012-06-13 16:29:39 +03001468 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001469 if (ret != X86EMUL_CONTINUE)
1470 return ret;
1471
1472 err_code = selector & 0xfffc;
1473 err_vec = GP_VECTOR;
1474
Guo Chaofc058682012-06-28 15:19:51 +08001475 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001476 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1477 goto exception;
1478
1479 if (!seg_desc.p) {
1480 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1481 goto exception;
1482 }
1483
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001484 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001485
1486 switch (seg) {
1487 case VCPU_SREG_SS:
1488 /*
1489 * segment is not a writable data segment or segment
1490 * selector's RPL != CPL or segment selector's RPL != CPL
1491 */
1492 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1493 goto exception;
1494 break;
1495 case VCPU_SREG_CS:
Paolo Bonzini5045b462014-05-15 18:09:29 +02001496 if (in_task_switch && rpl != dpl)
1497 goto exception;
1498
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001499 if (!(seg_desc.type & 8))
1500 goto exception;
1501
1502 if (seg_desc.type & 4) {
1503 /* conforming */
1504 if (dpl > cpl)
1505 goto exception;
1506 } else {
1507 /* nonconforming */
1508 if (rpl > cpl || dpl != cpl)
1509 goto exception;
1510 }
1511 /* CS(RPL) <- CPL */
1512 selector = (selector & 0xfffc) | cpl;
1513 break;
1514 case VCPU_SREG_TR:
1515 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1516 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001517 old_desc = seg_desc;
1518 seg_desc.type |= 2; /* busy */
1519 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1520 sizeof(seg_desc), &ctxt->exception);
1521 if (ret != X86EMUL_CONTINUE)
1522 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001523 break;
1524 case VCPU_SREG_LDTR:
1525 if (seg_desc.s || seg_desc.type != 2)
1526 goto exception;
1527 break;
1528 default: /* DS, ES, FS, or GS */
1529 /*
1530 * segment is not a data or readable code segment or
1531 * ((segment is a data or nonconforming code segment)
1532 * and (both RPL and CPL > DPL))
1533 */
1534 if ((seg_desc.type & 0xa) == 0x8 ||
1535 (((seg_desc.type & 0xc) != 0xc) &&
1536 (rpl > dpl && cpl > dpl)))
1537 goto exception;
1538 break;
1539 }
1540
1541 if (seg_desc.s) {
1542 /* mark segment as accessed */
1543 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001544 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001545 if (ret != X86EMUL_CONTINUE)
1546 return ret;
Nadav Amite37a75a2014-06-02 18:34:04 +03001547 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
1548 ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3,
1549 sizeof(base3), &ctxt->exception);
1550 if (ret != X86EMUL_CONTINUE)
1551 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001552 }
1553load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001554 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001555 return X86EMUL_CONTINUE;
1556exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001557 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001558 return X86EMUL_PROPAGATE_FAULT;
1559}
1560
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001561static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1562 u16 selector, int seg)
1563{
1564 u8 cpl = ctxt->ops->cpl(ctxt);
Paolo Bonzini5045b462014-05-15 18:09:29 +02001565 return __load_segment_descriptor(ctxt, selector, seg, cpl, false);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001566}
1567
Wei Yongjun31be40b2010-08-17 09:17:30 +08001568static void write_register_operand(struct operand *op)
1569{
1570 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1571 switch (op->bytes) {
1572 case 1:
1573 *(u8 *)op->addr.reg = (u8)op->val;
1574 break;
1575 case 2:
1576 *(u16 *)op->addr.reg = (u16)op->val;
1577 break;
1578 case 4:
1579 *op->addr.reg = (u32)op->val;
1580 break; /* 64b: zero-extend */
1581 case 8:
1582 *op->addr.reg = op->val;
1583 break;
1584 }
1585}
1586
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001587static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001588{
1589 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001590
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001591 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001592 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001593 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001594 break;
1595 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001596 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001597 rc = segmented_cmpxchg(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001598 op->addr.mem,
1599 &op->orig_val,
1600 &op->val,
1601 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001602 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001603 rc = segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001604 op->addr.mem,
1605 &op->val,
1606 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001607 if (rc != X86EMUL_CONTINUE)
1608 return rc;
1609 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001610 case OP_MEM_STR:
1611 rc = segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001612 op->addr.mem,
1613 op->data,
1614 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001615 if (rc != X86EMUL_CONTINUE)
1616 return rc;
1617 break;
Avi Kivity12537912011-03-29 11:41:27 +02001618 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001619 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001620 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001621 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001622 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001623 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001624 case OP_NONE:
1625 /* no writeback */
1626 break;
1627 default:
1628 break;
1629 }
1630 return X86EMUL_CONTINUE;
1631}
1632
Avi Kivity51ddff52012-06-12 20:19:40 +03001633static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001634{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001635 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001636
Avi Kivity5ad105e2012-08-19 14:34:31 +03001637 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001638 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001639 addr.seg = VCPU_SREG_SS;
1640
Avi Kivity51ddff52012-06-12 20:19:40 +03001641 return segmented_write(ctxt, addr, data, bytes);
1642}
1643
1644static int em_push(struct x86_emulate_ctxt *ctxt)
1645{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001646 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001647 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001648 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001649}
1650
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001651static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001652 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001653{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001654 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001655 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001656
Avi Kivitydd856ef2012-08-27 23:46:17 +03001657 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001658 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001659 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001660 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001661 return rc;
1662
Avi Kivity5ad105e2012-08-19 14:34:31 +03001663 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001664 return rc;
1665}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001666
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001667static int em_pop(struct x86_emulate_ctxt *ctxt)
1668{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001669 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001670}
1671
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001672static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001673 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001674{
1675 int rc;
1676 unsigned long val, change_mask;
1677 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001678 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001679
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001680 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001681 if (rc != X86EMUL_CONTINUE)
1682 return rc;
1683
1684 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1685 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1686
1687 switch(ctxt->mode) {
1688 case X86EMUL_MODE_PROT64:
1689 case X86EMUL_MODE_PROT32:
1690 case X86EMUL_MODE_PROT16:
1691 if (cpl == 0)
1692 change_mask |= EFLG_IOPL;
1693 if (cpl <= iopl)
1694 change_mask |= EFLG_IF;
1695 break;
1696 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001697 if (iopl < 3)
1698 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001699 change_mask |= EFLG_IF;
1700 break;
1701 default: /* real mode */
1702 change_mask |= (EFLG_IOPL | EFLG_IF);
1703 break;
1704 }
1705
1706 *(unsigned long *)dest =
1707 (ctxt->eflags & ~change_mask) | (val & change_mask);
1708
1709 return rc;
1710}
1711
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001712static int em_popf(struct x86_emulate_ctxt *ctxt)
1713{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001714 ctxt->dst.type = OP_REG;
1715 ctxt->dst.addr.reg = &ctxt->eflags;
1716 ctxt->dst.bytes = ctxt->op_bytes;
1717 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001718}
1719
Avi Kivity612e89f2012-06-12 20:03:23 +03001720static int em_enter(struct x86_emulate_ctxt *ctxt)
1721{
1722 int rc;
1723 unsigned frame_size = ctxt->src.val;
1724 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001725 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001726
1727 if (nesting_level)
1728 return X86EMUL_UNHANDLEABLE;
1729
Avi Kivitydd856ef2012-08-27 23:46:17 +03001730 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1731 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001732 if (rc != X86EMUL_CONTINUE)
1733 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001734 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001735 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001736 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1737 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001738 stack_mask(ctxt));
1739 return X86EMUL_CONTINUE;
1740}
1741
Avi Kivityf47cfa32012-06-07 17:49:24 +03001742static int em_leave(struct x86_emulate_ctxt *ctxt)
1743{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001744 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001745 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001746 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001747}
1748
Avi Kivity1cd196e2011-09-13 10:45:51 +03001749static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001750{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001751 int seg = ctxt->src2.val;
1752
Avi Kivity9dac77f2011-06-01 15:34:25 +03001753 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001754
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001755 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001756}
1757
Avi Kivity1cd196e2011-09-13 10:45:51 +03001758static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001759{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001760 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001761 unsigned long selector;
1762 int rc;
1763
Avi Kivity9dac77f2011-06-01 15:34:25 +03001764 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001765 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001766 return rc;
1767
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001768 if (ctxt->modrm_reg == VCPU_SREG_SS)
1769 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
1770
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001771 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001772 return rc;
1773}
1774
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001775static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001776{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001777 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001778 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001779 int reg = VCPU_REGS_RAX;
1780
1781 while (reg <= VCPU_REGS_RDI) {
1782 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001783 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001784
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001785 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001786 if (rc != X86EMUL_CONTINUE)
1787 return rc;
1788
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001789 ++reg;
1790 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001791
Wei Yongjunc37eda12010-06-15 09:03:33 +08001792 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001793}
1794
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001795static int em_pushf(struct x86_emulate_ctxt *ctxt)
1796{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001797 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001798 return em_push(ctxt);
1799}
1800
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001801static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001802{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001803 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001804 int reg = VCPU_REGS_RDI;
1805
1806 while (reg >= VCPU_REGS_RAX) {
1807 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001808 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001809 --reg;
1810 }
1811
Avi Kivitydd856ef2012-08-27 23:46:17 +03001812 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001813 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001814 break;
1815 --reg;
1816 }
1817 return rc;
1818}
1819
Avi Kivitydd856ef2012-08-27 23:46:17 +03001820static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001821{
Mathias Krause0225fb52012-08-30 01:30:16 +02001822 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001823 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001824 struct desc_ptr dt;
1825 gva_t cs_addr;
1826 gva_t eip_addr;
1827 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001828
1829 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001830 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001831 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001832 if (rc != X86EMUL_CONTINUE)
1833 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001834
1835 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1836
Avi Kivity9dac77f2011-06-01 15:34:25 +03001837 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001838 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001839 if (rc != X86EMUL_CONTINUE)
1840 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001841
Avi Kivity9dac77f2011-06-01 15:34:25 +03001842 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001843 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001844 if (rc != X86EMUL_CONTINUE)
1845 return rc;
1846
Avi Kivity4bff1e862011-04-20 13:37:53 +03001847 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001848
1849 eip_addr = dt.address + (irq << 2);
1850 cs_addr = dt.address + (irq << 2) + 2;
1851
Avi Kivity0f65dd72011-04-20 13:37:53 +03001852 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001853 if (rc != X86EMUL_CONTINUE)
1854 return rc;
1855
Avi Kivity0f65dd72011-04-20 13:37:53 +03001856 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001857 if (rc != X86EMUL_CONTINUE)
1858 return rc;
1859
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001860 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001861 if (rc != X86EMUL_CONTINUE)
1862 return rc;
1863
Avi Kivity9dac77f2011-06-01 15:34:25 +03001864 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001865
1866 return rc;
1867}
1868
Avi Kivitydd856ef2012-08-27 23:46:17 +03001869int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1870{
1871 int rc;
1872
1873 invalidate_registers(ctxt);
1874 rc = __emulate_int_real(ctxt, irq);
1875 if (rc == X86EMUL_CONTINUE)
1876 writeback_registers(ctxt);
1877 return rc;
1878}
1879
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001880static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001881{
1882 switch(ctxt->mode) {
1883 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001884 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001885 case X86EMUL_MODE_VM86:
1886 case X86EMUL_MODE_PROT16:
1887 case X86EMUL_MODE_PROT32:
1888 case X86EMUL_MODE_PROT64:
1889 default:
1890 /* Protected mode interrupts unimplemented yet */
1891 return X86EMUL_UNHANDLEABLE;
1892 }
1893}
1894
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001895static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001896{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001897 int rc = X86EMUL_CONTINUE;
1898 unsigned long temp_eip = 0;
1899 unsigned long temp_eflags = 0;
1900 unsigned long cs = 0;
1901 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1902 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1903 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1904 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1905
1906 /* TODO: Add stack limit check */
1907
Avi Kivity9dac77f2011-06-01 15:34:25 +03001908 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001909
1910 if (rc != X86EMUL_CONTINUE)
1911 return rc;
1912
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001913 if (temp_eip & ~0xffff)
1914 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001915
Avi Kivity9dac77f2011-06-01 15:34:25 +03001916 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001917
1918 if (rc != X86EMUL_CONTINUE)
1919 return rc;
1920
Avi Kivity9dac77f2011-06-01 15:34:25 +03001921 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001922
1923 if (rc != X86EMUL_CONTINUE)
1924 return rc;
1925
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001926 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001927
1928 if (rc != X86EMUL_CONTINUE)
1929 return rc;
1930
Avi Kivity9dac77f2011-06-01 15:34:25 +03001931 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001932
1933
Avi Kivity9dac77f2011-06-01 15:34:25 +03001934 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001935 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001936 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001937 ctxt->eflags &= ~0xffff;
1938 ctxt->eflags |= temp_eflags;
1939 }
1940
1941 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1942 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1943
1944 return rc;
1945}
1946
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001947static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001948{
1949 switch(ctxt->mode) {
1950 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001951 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001952 case X86EMUL_MODE_VM86:
1953 case X86EMUL_MODE_PROT16:
1954 case X86EMUL_MODE_PROT32:
1955 case X86EMUL_MODE_PROT64:
1956 default:
1957 /* iret from protected mode unimplemented yet */
1958 return X86EMUL_UNHANDLEABLE;
1959 }
1960}
1961
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001962static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1963{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001964 int rc;
1965 unsigned short sel;
1966
Avi Kivity9dac77f2011-06-01 15:34:25 +03001967 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001968
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001969 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001970 if (rc != X86EMUL_CONTINUE)
1971 return rc;
1972
Avi Kivity9dac77f2011-06-01 15:34:25 +03001973 ctxt->_eip = 0;
1974 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001975 return X86EMUL_CONTINUE;
1976}
1977
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001978static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001979{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001980 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001981
Avi Kivity9dac77f2011-06-01 15:34:25 +03001982 switch (ctxt->modrm_reg) {
Mohammed Gamald19292e2008-09-08 21:47:19 +03001983 case 2: /* call near abs */ {
1984 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001985 old_eip = ctxt->_eip;
1986 ctxt->_eip = ctxt->src.val;
1987 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001988 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001989 break;
1990 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001991 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001992 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001993 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001994 case 5: /* jmp far */
1995 rc = em_jmp_far(ctxt);
1996 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001997 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001998 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001999 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002000 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002001 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002002}
2003
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002004static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002005{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002006 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002007
Nadav Amitaaa05f22014-06-02 18:34:10 +03002008 if (ctxt->dst.bytes == 16)
2009 return X86EMUL_UNHANDLEABLE;
2010
Avi Kivitydd856ef2012-08-27 23:46:17 +03002011 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2012 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2013 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2014 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002015 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002016 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002017 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2018 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002019
Laurent Vivier05f086f2007-09-24 11:10:55 +02002020 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002021 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002022 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002023}
2024
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002025static int em_ret(struct x86_emulate_ctxt *ctxt)
2026{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002027 ctxt->dst.type = OP_REG;
2028 ctxt->dst.addr.reg = &ctxt->_eip;
2029 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002030 return em_pop(ctxt);
2031}
2032
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002033static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002034{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002035 int rc;
2036 unsigned long cs;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002037 int cpl = ctxt->ops->cpl(ctxt);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002038
Avi Kivity9dac77f2011-06-01 15:34:25 +03002039 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002040 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002041 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002042 if (ctxt->op_bytes == 4)
2043 ctxt->_eip = (u32)ctxt->_eip;
2044 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002045 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002046 return rc;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002047 /* Outer-privilege level return is not implemented */
2048 if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2049 return X86EMUL_UNHANDLEABLE;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002050 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002051 return rc;
2052}
2053
Bruce Rogers32611072013-09-09 09:40:20 -06002054static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2055{
2056 int rc;
2057
2058 rc = em_ret_far(ctxt);
2059 if (rc != X86EMUL_CONTINUE)
2060 return rc;
2061 rsp_increment(ctxt, ctxt->src.val);
2062 return X86EMUL_CONTINUE;
2063}
2064
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002065static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2066{
2067 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002068 ctxt->dst.orig_val = ctxt->dst.val;
2069 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002070 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002071 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002072 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002073
2074 if (ctxt->eflags & EFLG_ZF) {
2075 /* Success: write back to memory. */
2076 ctxt->dst.val = ctxt->src.orig_val;
2077 } else {
2078 /* Failure: write the value we saw to EAX. */
2079 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002080 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Nadav Amit37c564f2014-06-02 18:34:07 +03002081 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002082 }
2083 return X86EMUL_CONTINUE;
2084}
2085
Avi Kivityd4b43252011-09-13 10:45:50 +03002086static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002087{
Avi Kivityd4b43252011-09-13 10:45:50 +03002088 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002089 unsigned short sel;
2090 int rc;
2091
Avi Kivity9dac77f2011-06-01 15:34:25 +03002092 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002093
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002094 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002095 if (rc != X86EMUL_CONTINUE)
2096 return rc;
2097
Avi Kivity9dac77f2011-06-01 15:34:25 +03002098 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002099 return rc;
2100}
2101
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002102static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002103setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002104 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002105{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002106 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002107 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002108 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002109 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002110 cs->type = 0x0b; /* Read, Execute, Accessed */
2111 cs->s = 1;
2112 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002113 cs->p = 1;
2114 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002115 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002116
Gleb Natapov79168fd2010-04-28 19:15:30 +03002117 set_desc_base(ss, 0); /* flat segment */
2118 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002119 ss->g = 1; /* 4kb granularity */
2120 ss->s = 1;
2121 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002122 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002123 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002124 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002125 ss->l = 0;
2126 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002127}
2128
Avi Kivity1a18a692012-02-01 12:23:21 +02002129static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2130{
2131 u32 eax, ebx, ecx, edx;
2132
2133 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002134 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2135 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002136 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2137 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2138}
2139
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002140static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2141{
Mathias Krause0225fb52012-08-30 01:30:16 +02002142 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002143 u32 eax, ebx, ecx, edx;
2144
2145 /*
2146 * syscall should always be enabled in longmode - so only become
2147 * vendor specific (cpuid) if other modes are active...
2148 */
2149 if (ctxt->mode == X86EMUL_MODE_PROT64)
2150 return true;
2151
2152 eax = 0x00000000;
2153 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002154 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2155 /*
2156 * Intel ("GenuineIntel")
2157 * remark: Intel CPUs only support "syscall" in 64bit
2158 * longmode. Also an 64bit guest with a
2159 * 32bit compat-app running will #UD !! While this
2160 * behaviour can be fixed (by emulating) into AMD
2161 * response - CPUs of AMD can't behave like Intel.
2162 */
2163 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2164 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2165 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2166 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002167
Avi Kivity0017f932012-06-07 14:10:16 +03002168 /* AMD ("AuthenticAMD") */
2169 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2170 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2171 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2172 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002173
Avi Kivity0017f932012-06-07 14:10:16 +03002174 /* AMD ("AMDisbetter!") */
2175 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2176 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2177 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2178 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002179
2180 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2181 return false;
2182}
2183
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002184static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002185{
Mathias Krause0225fb52012-08-30 01:30:16 +02002186 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002187 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002188 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002189 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002190 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002191
2192 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002193 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002194 ctxt->mode == X86EMUL_MODE_VM86)
2195 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002196
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002197 if (!(em_syscall_is_enabled(ctxt)))
2198 return emulate_ud(ctxt);
2199
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002200 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002201 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002202
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002203 if (!(efer & EFER_SCE))
2204 return emulate_ud(ctxt);
2205
Avi Kivity717746e2011-04-20 13:37:53 +03002206 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002207 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002208 cs_sel = (u16)(msr_data & 0xfffc);
2209 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002210
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002211 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002212 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002213 cs.l = 1;
2214 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002215 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2216 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002217
Avi Kivitydd856ef2012-08-27 23:46:17 +03002218 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002219 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002220#ifdef CONFIG_X86_64
Avi Kivitydd856ef2012-08-27 23:46:17 +03002221 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002222
Avi Kivity717746e2011-04-20 13:37:53 +03002223 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002224 ctxt->mode == X86EMUL_MODE_PROT64 ?
2225 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002226 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002227
Avi Kivity717746e2011-04-20 13:37:53 +03002228 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002229 ctxt->eflags &= ~(msr_data | EFLG_RF);
2230#endif
2231 } else {
2232 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002233 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002234 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002235
2236 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2237 }
2238
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002239 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002240}
2241
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002242static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002243{
Mathias Krause0225fb52012-08-30 01:30:16 +02002244 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002245 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002246 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002247 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002248 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002249
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002250 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002251 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002252 if (ctxt->mode == X86EMUL_MODE_REAL)
2253 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002254
Avi Kivity1a18a692012-02-01 12:23:21 +02002255 /*
2256 * Not recognized on AMD in compat mode (but is recognized in legacy
2257 * mode).
2258 */
2259 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2260 && !vendor_intel(ctxt))
2261 return emulate_ud(ctxt);
2262
Andre Przywara8c604352009-06-18 12:56:01 +02002263 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2264 * Therefore, we inject an #UD.
2265 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002266 if (ctxt->mode == X86EMUL_MODE_PROT64)
2267 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002268
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002269 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002270
Avi Kivity717746e2011-04-20 13:37:53 +03002271 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002272 switch (ctxt->mode) {
2273 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002274 if ((msr_data & 0xfffc) == 0x0)
2275 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002276 break;
2277 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002278 if (msr_data == 0x0)
2279 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002280 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002281 default:
2282 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002283 }
2284
2285 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002286 cs_sel = (u16)msr_data;
2287 cs_sel &= ~SELECTOR_RPL_MASK;
2288 ss_sel = cs_sel + 8;
2289 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002290 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002291 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002292 cs.l = 1;
2293 }
2294
Avi Kivity1aa36612011-04-27 13:20:30 +03002295 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2296 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002297
Avi Kivity717746e2011-04-20 13:37:53 +03002298 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002299 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002300
Avi Kivity717746e2011-04-20 13:37:53 +03002301 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002302 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002303
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002304 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002305}
2306
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002307static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002308{
Mathias Krause0225fb52012-08-30 01:30:16 +02002309 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002310 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002311 u64 msr_data;
2312 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002313 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002314
Gleb Natapova0044752010-02-10 14:21:31 +02002315 /* inject #GP if in real mode or Virtual 8086 mode */
2316 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002317 ctxt->mode == X86EMUL_MODE_VM86)
2318 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002319
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002320 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002321
Avi Kivity9dac77f2011-06-01 15:34:25 +03002322 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002323 usermode = X86EMUL_MODE_PROT64;
2324 else
2325 usermode = X86EMUL_MODE_PROT32;
2326
2327 cs.dpl = 3;
2328 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002329 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002330 switch (usermode) {
2331 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002332 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002333 if ((msr_data & 0xfffc) == 0x0)
2334 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002335 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002336 break;
2337 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002338 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002339 if (msr_data == 0x0)
2340 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002341 ss_sel = cs_sel + 8;
2342 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002343 cs.l = 1;
2344 break;
2345 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002346 cs_sel |= SELECTOR_RPL_MASK;
2347 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002348
Avi Kivity1aa36612011-04-27 13:20:30 +03002349 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2350 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002351
Avi Kivitydd856ef2012-08-27 23:46:17 +03002352 ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX);
2353 *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX);
Andre Przywara4668f052009-06-18 12:56:02 +02002354
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002355 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002356}
2357
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002358static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002359{
2360 int iopl;
2361 if (ctxt->mode == X86EMUL_MODE_REAL)
2362 return false;
2363 if (ctxt->mode == X86EMUL_MODE_VM86)
2364 return true;
2365 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002366 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002367}
2368
2369static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002370 u16 port, u16 len)
2371{
Mathias Krause0225fb52012-08-30 01:30:16 +02002372 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002373 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002374 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002375 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002376 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002377 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002378 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002379
Avi Kivity1aa36612011-04-27 13:20:30 +03002380 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002381 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002382 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002383 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002384 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002385 base = get_desc_base(&tr_seg);
2386#ifdef CONFIG_X86_64
2387 base |= ((u64)base3) << 32;
2388#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002389 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002390 if (r != X86EMUL_CONTINUE)
2391 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002392 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002393 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002394 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002395 if (r != X86EMUL_CONTINUE)
2396 return false;
2397 if ((perm >> bit_idx) & mask)
2398 return false;
2399 return true;
2400}
2401
2402static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002403 u16 port, u16 len)
2404{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002405 if (ctxt->perm_ok)
2406 return true;
2407
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002408 if (emulator_bad_iopl(ctxt))
2409 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002410 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002411
2412 ctxt->perm_ok = true;
2413
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002414 return true;
2415}
2416
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002417static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002418 struct tss_segment_16 *tss)
2419{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002420 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002421 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002422 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2423 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2424 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2425 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2426 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2427 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2428 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2429 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002430
Avi Kivity1aa36612011-04-27 13:20:30 +03002431 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2432 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2433 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2434 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2435 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002436}
2437
2438static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002439 struct tss_segment_16 *tss)
2440{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002441 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002442 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002443
Avi Kivity9dac77f2011-06-01 15:34:25 +03002444 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002445 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002446 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2447 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2448 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2449 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2450 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2451 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2452 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2453 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002454
2455 /*
2456 * SDM says that segment selectors are loaded before segment
2457 * descriptors
2458 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002459 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2460 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2461 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2462 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2463 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002464
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002465 cpl = tss->cs & 3;
2466
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002467 /*
Guo Chaofc058682012-06-28 15:19:51 +08002468 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002469 * it is handled in a context of new task
2470 */
Paolo Bonzini5045b462014-05-15 18:09:29 +02002471 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002472 if (ret != X86EMUL_CONTINUE)
2473 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002474 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002475 if (ret != X86EMUL_CONTINUE)
2476 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002477 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002478 if (ret != X86EMUL_CONTINUE)
2479 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002480 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002481 if (ret != X86EMUL_CONTINUE)
2482 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002483 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002484 if (ret != X86EMUL_CONTINUE)
2485 return ret;
2486
2487 return X86EMUL_CONTINUE;
2488}
2489
2490static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002491 u16 tss_selector, u16 old_tss_sel,
2492 ulong old_tss_base, struct desc_struct *new_desc)
2493{
Mathias Krause0225fb52012-08-30 01:30:16 +02002494 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002495 struct tss_segment_16 tss_seg;
2496 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002497 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002498
Avi Kivity0f65dd72011-04-20 13:37:53 +03002499 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002500 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002501 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002502 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002503 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002504
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002505 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002506
Avi Kivity0f65dd72011-04-20 13:37:53 +03002507 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002508 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002509 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002510 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002511 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002512
Avi Kivity0f65dd72011-04-20 13:37:53 +03002513 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002514 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002515 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002516 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002517 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002518
2519 if (old_tss_sel != 0xffff) {
2520 tss_seg.prev_task_link = old_tss_sel;
2521
Avi Kivity0f65dd72011-04-20 13:37:53 +03002522 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002523 &tss_seg.prev_task_link,
2524 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002525 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002526 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002527 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002528 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002529 }
2530
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002531 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002532}
2533
2534static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002535 struct tss_segment_32 *tss)
2536{
Nadav Amit5c7411e2014-04-07 18:37:47 +03002537 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002538 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002539 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002540 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2541 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2542 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2543 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2544 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2545 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2546 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2547 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002548
Avi Kivity1aa36612011-04-27 13:20:30 +03002549 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2550 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2551 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2552 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2553 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2554 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002555}
2556
2557static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002558 struct tss_segment_32 *tss)
2559{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002560 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002561 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002562
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002563 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002564 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002565 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002566 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002567
2568 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002569 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2570 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2571 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2572 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2573 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2574 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2575 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2576 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002577
2578 /*
2579 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002580 * descriptors. This is important because CPL checks will
2581 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002582 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002583 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2584 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2585 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2586 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2587 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2588 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2589 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002590
2591 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002592 * If we're switching between Protected Mode and VM86, we need to make
2593 * sure to update the mode before loading the segment descriptors so
2594 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01002595 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002596 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002597 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002598 cpl = 3;
2599 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002600 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002601 cpl = tss->cs & 3;
2602 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01002603
2604 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002605 * Now load segment descriptors. If fault happenes at this stage
2606 * it is handled in a context of new task
2607 */
Paolo Bonzini5045b462014-05-15 18:09:29 +02002608 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002609 if (ret != X86EMUL_CONTINUE)
2610 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002611 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002612 if (ret != X86EMUL_CONTINUE)
2613 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002614 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002615 if (ret != X86EMUL_CONTINUE)
2616 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002617 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002618 if (ret != X86EMUL_CONTINUE)
2619 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002620 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002621 if (ret != X86EMUL_CONTINUE)
2622 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002623 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002624 if (ret != X86EMUL_CONTINUE)
2625 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002626 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002627 if (ret != X86EMUL_CONTINUE)
2628 return ret;
2629
2630 return X86EMUL_CONTINUE;
2631}
2632
2633static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002634 u16 tss_selector, u16 old_tss_sel,
2635 ulong old_tss_base, struct desc_struct *new_desc)
2636{
Mathias Krause0225fb52012-08-30 01:30:16 +02002637 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002638 struct tss_segment_32 tss_seg;
2639 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002640 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03002641 u32 eip_offset = offsetof(struct tss_segment_32, eip);
2642 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002643
Avi Kivity0f65dd72011-04-20 13:37:53 +03002644 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002645 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002646 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002647 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002648 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002649
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002650 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002651
Nadav Amit5c7411e2014-04-07 18:37:47 +03002652 /* Only GP registers and segment selectors are saved */
2653 ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
2654 ldt_sel_offset - eip_offset, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002655 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002656 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002657 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002658
Avi Kivity0f65dd72011-04-20 13:37:53 +03002659 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002660 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002661 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002662 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002663 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002664
2665 if (old_tss_sel != 0xffff) {
2666 tss_seg.prev_task_link = old_tss_sel;
2667
Avi Kivity0f65dd72011-04-20 13:37:53 +03002668 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002669 &tss_seg.prev_task_link,
2670 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002671 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002672 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002673 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002674 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002675 }
2676
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002677 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002678}
2679
2680static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002681 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002682 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002683{
Mathias Krause0225fb52012-08-30 01:30:16 +02002684 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002685 struct desc_struct curr_tss_desc, next_tss_desc;
2686 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002687 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002688 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002689 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002690 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002691 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002692
2693 /* FIXME: old_tss_base == ~0 ? */
2694
Avi Kivitye9194642012-06-13 16:29:39 +03002695 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002696 if (ret != X86EMUL_CONTINUE)
2697 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002698 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002699 if (ret != X86EMUL_CONTINUE)
2700 return ret;
2701
2702 /* FIXME: check that next_tss_desc is tss */
2703
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002704 /*
2705 * Check privileges. The three cases are task switch caused by...
2706 *
2707 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2708 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002709 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002710 */
2711 if (reason == TASK_SWITCH_GATE) {
2712 if (idt_index != -1) {
2713 /* Software interrupts */
2714 struct desc_struct task_gate_desc;
2715 int dpl;
2716
2717 ret = read_interrupt_descriptor(ctxt, idt_index,
2718 &task_gate_desc);
2719 if (ret != X86EMUL_CONTINUE)
2720 return ret;
2721
2722 dpl = task_gate_desc.dpl;
2723 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2724 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2725 }
2726 } else if (reason != TASK_SWITCH_IRET) {
2727 int dpl = next_tss_desc.dpl;
2728 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2729 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002730 }
2731
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002732
Gleb Natapovceffb452010-03-18 15:20:19 +02002733 desc_limit = desc_limit_scaled(&next_tss_desc);
2734 if (!next_tss_desc.p ||
2735 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2736 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002737 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002738 return X86EMUL_PROPAGATE_FAULT;
2739 }
2740
2741 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2742 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002743 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002744 }
2745
2746 if (reason == TASK_SWITCH_IRET)
2747 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2748
2749 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002750 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002751 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2752 old_tss_sel = 0xffff;
2753
2754 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002755 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002756 old_tss_base, &next_tss_desc);
2757 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002758 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002759 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002760 if (ret != X86EMUL_CONTINUE)
2761 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002762
2763 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2764 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2765
2766 if (reason != TASK_SWITCH_IRET) {
2767 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002768 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002769 }
2770
Avi Kivity717746e2011-04-20 13:37:53 +03002771 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002772 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002773
Jan Kiszkae269fb22010-04-14 15:51:09 +02002774 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002775 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2776 ctxt->lock_prefix = 0;
2777 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002778 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002779 }
2780
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002781 return ret;
2782}
2783
2784int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002785 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002786 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002787{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002788 int rc;
2789
Avi Kivitydd856ef2012-08-27 23:46:17 +03002790 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002791 ctxt->_eip = ctxt->eip;
2792 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002793
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002794 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002795 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002796
Avi Kivitydd856ef2012-08-27 23:46:17 +03002797 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002798 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002799 writeback_registers(ctxt);
2800 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002801
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002802 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002803}
2804
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002805static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2806 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002807{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002808 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002809
Avi Kivitydd856ef2012-08-27 23:46:17 +03002810 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2811 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002812}
2813
Avi Kivity7af04fc2010-08-18 14:16:35 +03002814static int em_das(struct x86_emulate_ctxt *ctxt)
2815{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002816 u8 al, old_al;
2817 bool af, cf, old_cf;
2818
2819 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002820 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002821
2822 old_al = al;
2823 old_cf = cf;
2824 cf = false;
2825 af = ctxt->eflags & X86_EFLAGS_AF;
2826 if ((al & 0x0f) > 9 || af) {
2827 al -= 6;
2828 cf = old_cf | (al >= 250);
2829 af = true;
2830 } else {
2831 af = false;
2832 }
2833 if (old_al > 0x99 || old_cf) {
2834 al -= 0x60;
2835 cf = true;
2836 }
2837
Avi Kivity9dac77f2011-06-01 15:34:25 +03002838 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002839 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002840 ctxt->src.type = OP_IMM;
2841 ctxt->src.val = 0;
2842 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02002843 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03002844 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2845 if (cf)
2846 ctxt->eflags |= X86_EFLAGS_CF;
2847 if (af)
2848 ctxt->eflags |= X86_EFLAGS_AF;
2849 return X86EMUL_CONTINUE;
2850}
2851
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02002852static int em_aam(struct x86_emulate_ctxt *ctxt)
2853{
2854 u8 al, ah;
2855
2856 if (ctxt->src.val == 0)
2857 return emulate_de(ctxt);
2858
2859 al = ctxt->dst.val & 0xff;
2860 ah = al / ctxt->src.val;
2861 al %= ctxt->src.val;
2862
2863 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
2864
2865 /* Set PF, ZF, SF */
2866 ctxt->src.type = OP_IMM;
2867 ctxt->src.val = 0;
2868 ctxt->src.bytes = 1;
2869 fastop(ctxt, em_or);
2870
2871 return X86EMUL_CONTINUE;
2872}
2873
Gleb Natapov7f662272012-12-10 11:42:30 +02002874static int em_aad(struct x86_emulate_ctxt *ctxt)
2875{
2876 u8 al = ctxt->dst.val & 0xff;
2877 u8 ah = (ctxt->dst.val >> 8) & 0xff;
2878
2879 al = (al + (ah * ctxt->src.val)) & 0xff;
2880
2881 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
2882
Gleb Natapovf583c292013-02-13 17:50:39 +02002883 /* Set PF, ZF, SF */
2884 ctxt->src.type = OP_IMM;
2885 ctxt->src.val = 0;
2886 ctxt->src.bytes = 1;
2887 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02002888
2889 return X86EMUL_CONTINUE;
2890}
2891
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002892static int em_call(struct x86_emulate_ctxt *ctxt)
2893{
2894 long rel = ctxt->src.val;
2895
2896 ctxt->src.val = (unsigned long)ctxt->_eip;
2897 jmp_rel(ctxt, rel);
2898 return em_push(ctxt);
2899}
2900
Avi Kivity0ef753b2010-08-18 14:51:45 +03002901static int em_call_far(struct x86_emulate_ctxt *ctxt)
2902{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002903 u16 sel, old_cs;
2904 ulong old_eip;
2905 int rc;
2906
Avi Kivity1aa36612011-04-27 13:20:30 +03002907 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002908 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002909
Avi Kivity9dac77f2011-06-01 15:34:25 +03002910 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002911 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002912 return X86EMUL_CONTINUE;
2913
Avi Kivity9dac77f2011-06-01 15:34:25 +03002914 ctxt->_eip = 0;
2915 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002916
Avi Kivity9dac77f2011-06-01 15:34:25 +03002917 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002918 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002919 if (rc != X86EMUL_CONTINUE)
2920 return rc;
2921
Avi Kivity9dac77f2011-06-01 15:34:25 +03002922 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002923 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002924}
2925
Avi Kivity40ece7c2010-08-18 15:12:09 +03002926static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2927{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002928 int rc;
2929
Avi Kivity9dac77f2011-06-01 15:34:25 +03002930 ctxt->dst.type = OP_REG;
2931 ctxt->dst.addr.reg = &ctxt->_eip;
2932 ctxt->dst.bytes = ctxt->op_bytes;
2933 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002934 if (rc != X86EMUL_CONTINUE)
2935 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03002936 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002937 return X86EMUL_CONTINUE;
2938}
2939
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002940static int em_xchg(struct x86_emulate_ctxt *ctxt)
2941{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002942 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002943 ctxt->src.val = ctxt->dst.val;
2944 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002945
2946 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002947 ctxt->dst.val = ctxt->src.orig_val;
2948 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002949 return X86EMUL_CONTINUE;
2950}
2951
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002952static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2953{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002954 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02002955 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002956}
2957
Avi Kivity61429142010-08-19 15:13:00 +03002958static int em_cwd(struct x86_emulate_ctxt *ctxt)
2959{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002960 ctxt->dst.type = OP_REG;
2961 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002962 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002963 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002964
2965 return X86EMUL_CONTINUE;
2966}
2967
Avi Kivity48bb5d32010-08-18 18:54:34 +03002968static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2969{
Avi Kivity48bb5d32010-08-18 18:54:34 +03002970 u64 tsc = 0;
2971
Avi Kivity717746e2011-04-20 13:37:53 +03002972 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002973 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
2974 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03002975 return X86EMUL_CONTINUE;
2976}
2977
Avi Kivity222d21a2011-11-10 14:57:30 +02002978static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
2979{
2980 u64 pmc;
2981
Avi Kivitydd856ef2012-08-27 23:46:17 +03002982 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02002983 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002984 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
2985 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02002986 return X86EMUL_CONTINUE;
2987}
2988
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002989static int em_mov(struct x86_emulate_ctxt *ctxt)
2990{
Stefan Hajnoczi49597d82012-04-09 18:40:00 +03002991 memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002992 return X86EMUL_CONTINUE;
2993}
2994
Borislav Petkov84cffe42013-10-29 12:54:56 +01002995#define FFL(x) bit(X86_FEATURE_##x)
2996
2997static int em_movbe(struct x86_emulate_ctxt *ctxt)
2998{
2999 u32 ebx, ecx, edx, eax = 1;
3000 u16 tmp;
3001
3002 /*
3003 * Check MOVBE is set in the guest-visible CPUID leaf.
3004 */
3005 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
3006 if (!(ecx & FFL(MOVBE)))
3007 return emulate_ud(ctxt);
3008
3009 switch (ctxt->op_bytes) {
3010 case 2:
3011 /*
3012 * From MOVBE definition: "...When the operand size is 16 bits,
3013 * the upper word of the destination register remains unchanged
3014 * ..."
3015 *
3016 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3017 * rules so we have to do the operation almost per hand.
3018 */
3019 tmp = (u16)ctxt->src.val;
3020 ctxt->dst.val &= ~0xffffUL;
3021 ctxt->dst.val |= (unsigned long)swab16(tmp);
3022 break;
3023 case 4:
3024 ctxt->dst.val = swab32((u32)ctxt->src.val);
3025 break;
3026 case 8:
3027 ctxt->dst.val = swab64(ctxt->src.val);
3028 break;
3029 default:
3030 return X86EMUL_PROPAGATE_FAULT;
3031 }
3032 return X86EMUL_CONTINUE;
3033}
3034
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003035static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3036{
3037 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3038 return emulate_gp(ctxt, 0);
3039
3040 /* Disable writeback. */
3041 ctxt->dst.type = OP_NONE;
3042 return X86EMUL_CONTINUE;
3043}
3044
3045static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3046{
3047 unsigned long val;
3048
3049 if (ctxt->mode == X86EMUL_MODE_PROT64)
3050 val = ctxt->src.val & ~0ULL;
3051 else
3052 val = ctxt->src.val & ~0U;
3053
3054 /* #UD condition is already handled. */
3055 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3056 return emulate_gp(ctxt, 0);
3057
3058 /* Disable writeback. */
3059 ctxt->dst.type = OP_NONE;
3060 return X86EMUL_CONTINUE;
3061}
3062
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003063static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3064{
3065 u64 msr_data;
3066
Avi Kivitydd856ef2012-08-27 23:46:17 +03003067 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3068 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3069 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003070 return emulate_gp(ctxt, 0);
3071
3072 return X86EMUL_CONTINUE;
3073}
3074
3075static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3076{
3077 u64 msr_data;
3078
Avi Kivitydd856ef2012-08-27 23:46:17 +03003079 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003080 return emulate_gp(ctxt, 0);
3081
Avi Kivitydd856ef2012-08-27 23:46:17 +03003082 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3083 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003084 return X86EMUL_CONTINUE;
3085}
3086
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003087static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3088{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003089 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003090 return emulate_ud(ctxt);
3091
Avi Kivity9dac77f2011-06-01 15:34:25 +03003092 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003093 return X86EMUL_CONTINUE;
3094}
3095
3096static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3097{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003098 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003099
Avi Kivity9dac77f2011-06-01 15:34:25 +03003100 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003101 return emulate_ud(ctxt);
3102
Avi Kivity9dac77f2011-06-01 15:34:25 +03003103 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003104 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3105
3106 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003107 ctxt->dst.type = OP_NONE;
3108 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003109}
3110
Avi Kivitya14e5792012-06-13 12:28:33 +03003111static int em_lldt(struct x86_emulate_ctxt *ctxt)
3112{
3113 u16 sel = ctxt->src.val;
3114
3115 /* Disable writeback. */
3116 ctxt->dst.type = OP_NONE;
3117 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3118}
3119
Avi Kivity80890002012-06-13 16:33:29 +03003120static int em_ltr(struct x86_emulate_ctxt *ctxt)
3121{
3122 u16 sel = ctxt->src.val;
3123
3124 /* Disable writeback. */
3125 ctxt->dst.type = OP_NONE;
3126 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3127}
3128
Avi Kivity38503912011-03-31 18:48:09 +02003129static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3130{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003131 int rc;
3132 ulong linear;
3133
Avi Kivity9dac77f2011-06-01 15:34:25 +03003134 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003135 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003136 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003137 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003138 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003139 return X86EMUL_CONTINUE;
3140}
3141
Avi Kivity2d04a052011-04-20 15:32:49 +03003142static int em_clts(struct x86_emulate_ctxt *ctxt)
3143{
3144 ulong cr0;
3145
3146 cr0 = ctxt->ops->get_cr(ctxt, 0);
3147 cr0 &= ~X86_CR0_TS;
3148 ctxt->ops->set_cr(ctxt, 0, cr0);
3149 return X86EMUL_CONTINUE;
3150}
3151
Avi Kivity26d05cc2011-04-21 12:07:59 +03003152static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3153{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003154 int rc;
3155
Avi Kivity9dac77f2011-06-01 15:34:25 +03003156 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003157 return X86EMUL_UNHANDLEABLE;
3158
3159 rc = ctxt->ops->fix_hypercall(ctxt);
3160 if (rc != X86EMUL_CONTINUE)
3161 return rc;
3162
3163 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003164 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003165 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003166 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003167 return X86EMUL_CONTINUE;
3168}
3169
Avi Kivity96051572012-06-10 17:21:18 +03003170static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3171 void (*get)(struct x86_emulate_ctxt *ctxt,
3172 struct desc_ptr *ptr))
3173{
3174 struct desc_ptr desc_ptr;
3175
3176 if (ctxt->mode == X86EMUL_MODE_PROT64)
3177 ctxt->op_bytes = 8;
3178 get(ctxt, &desc_ptr);
3179 if (ctxt->op_bytes == 2) {
3180 ctxt->op_bytes = 4;
3181 desc_ptr.address &= 0x00ffffff;
3182 }
3183 /* Disable writeback. */
3184 ctxt->dst.type = OP_NONE;
3185 return segmented_write(ctxt, ctxt->dst.addr.mem,
3186 &desc_ptr, 2 + ctxt->op_bytes);
3187}
3188
3189static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3190{
3191 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3192}
3193
3194static int em_sidt(struct x86_emulate_ctxt *ctxt)
3195{
3196 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3197}
3198
Avi Kivity26d05cc2011-04-21 12:07:59 +03003199static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3200{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003201 struct desc_ptr desc_ptr;
3202 int rc;
3203
Avi Kivity510425f2012-06-07 17:04:36 +03003204 if (ctxt->mode == X86EMUL_MODE_PROT64)
3205 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003206 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003207 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003208 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003209 if (rc != X86EMUL_CONTINUE)
3210 return rc;
3211 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3212 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003213 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003214 return X86EMUL_CONTINUE;
3215}
3216
Avi Kivity5ef39c72011-04-21 12:21:50 +03003217static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003218{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003219 int rc;
3220
Avi Kivity5ef39c72011-04-21 12:21:50 +03003221 rc = ctxt->ops->fix_hypercall(ctxt);
3222
Avi Kivity26d05cc2011-04-21 12:07:59 +03003223 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003224 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003225 return rc;
3226}
3227
3228static int em_lidt(struct x86_emulate_ctxt *ctxt)
3229{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003230 struct desc_ptr desc_ptr;
3231 int rc;
3232
Avi Kivity510425f2012-06-07 17:04:36 +03003233 if (ctxt->mode == X86EMUL_MODE_PROT64)
3234 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003235 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003236 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003237 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003238 if (rc != X86EMUL_CONTINUE)
3239 return rc;
3240 ctxt->ops->set_idt(ctxt, &desc_ptr);
3241 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003242 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003243 return X86EMUL_CONTINUE;
3244}
3245
3246static int em_smsw(struct x86_emulate_ctxt *ctxt)
3247{
Nadav Amit32e94d02014-06-02 18:34:11 +03003248 if (ctxt->dst.type == OP_MEM)
3249 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003250 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003251 return X86EMUL_CONTINUE;
3252}
3253
3254static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3255{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003256 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003257 | (ctxt->src.val & 0x0f));
3258 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003259 return X86EMUL_CONTINUE;
3260}
3261
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003262static int em_loop(struct x86_emulate_ctxt *ctxt)
3263{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003264 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3265 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003266 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3267 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003268
3269 return X86EMUL_CONTINUE;
3270}
3271
3272static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3273{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003274 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003275 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003276
3277 return X86EMUL_CONTINUE;
3278}
3279
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003280static int em_in(struct x86_emulate_ctxt *ctxt)
3281{
3282 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3283 &ctxt->dst.val))
3284 return X86EMUL_IO_NEEDED;
3285
3286 return X86EMUL_CONTINUE;
3287}
3288
3289static int em_out(struct x86_emulate_ctxt *ctxt)
3290{
3291 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3292 &ctxt->src.val, 1);
3293 /* Disable writeback. */
3294 ctxt->dst.type = OP_NONE;
3295 return X86EMUL_CONTINUE;
3296}
3297
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003298static int em_cli(struct x86_emulate_ctxt *ctxt)
3299{
3300 if (emulator_bad_iopl(ctxt))
3301 return emulate_gp(ctxt, 0);
3302
3303 ctxt->eflags &= ~X86_EFLAGS_IF;
3304 return X86EMUL_CONTINUE;
3305}
3306
3307static int em_sti(struct x86_emulate_ctxt *ctxt)
3308{
3309 if (emulator_bad_iopl(ctxt))
3310 return emulate_gp(ctxt, 0);
3311
3312 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3313 ctxt->eflags |= X86_EFLAGS_IF;
3314 return X86EMUL_CONTINUE;
3315}
3316
Avi Kivity6d6eede2012-06-07 14:11:36 +03003317static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3318{
3319 u32 eax, ebx, ecx, edx;
3320
Avi Kivitydd856ef2012-08-27 23:46:17 +03003321 eax = reg_read(ctxt, VCPU_REGS_RAX);
3322 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003323 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003324 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3325 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3326 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3327 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003328 return X86EMUL_CONTINUE;
3329}
3330
Paolo Bonzini98f73632013-10-31 11:19:42 +01003331static int em_sahf(struct x86_emulate_ctxt *ctxt)
3332{
3333 u32 flags;
3334
3335 flags = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF;
3336 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3337
3338 ctxt->eflags &= ~0xffUL;
3339 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3340 return X86EMUL_CONTINUE;
3341}
3342
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003343static int em_lahf(struct x86_emulate_ctxt *ctxt)
3344{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003345 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3346 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003347 return X86EMUL_CONTINUE;
3348}
3349
Avi Kivity92998362012-06-13 12:25:06 +03003350static int em_bswap(struct x86_emulate_ctxt *ctxt)
3351{
3352 switch (ctxt->op_bytes) {
3353#ifdef CONFIG_X86_64
3354 case 8:
3355 asm("bswap %0" : "+r"(ctxt->dst.val));
3356 break;
3357#endif
3358 default:
3359 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3360 break;
3361 }
3362 return X86EMUL_CONTINUE;
3363}
3364
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003365static bool valid_cr(int nr)
3366{
3367 switch (nr) {
3368 case 0:
3369 case 2 ... 4:
3370 case 8:
3371 return true;
3372 default:
3373 return false;
3374 }
3375}
3376
3377static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3378{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003379 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003380 return emulate_ud(ctxt);
3381
3382 return X86EMUL_CONTINUE;
3383}
3384
3385static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3386{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003387 u64 new_val = ctxt->src.val64;
3388 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003389 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003390
3391 static u64 cr_reserved_bits[] = {
3392 0xffffffff00000000ULL,
3393 0, 0, 0, /* CR3 checked later */
3394 CR4_RESERVED_BITS,
3395 0, 0, 0,
3396 CR8_RESERVED_BITS,
3397 };
3398
3399 if (!valid_cr(cr))
3400 return emulate_ud(ctxt);
3401
3402 if (new_val & cr_reserved_bits[cr])
3403 return emulate_gp(ctxt, 0);
3404
3405 switch (cr) {
3406 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003407 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003408 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3409 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3410 return emulate_gp(ctxt, 0);
3411
Avi Kivity717746e2011-04-20 13:37:53 +03003412 cr4 = ctxt->ops->get_cr(ctxt, 4);
3413 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003414
3415 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3416 !(cr4 & X86_CR4_PAE))
3417 return emulate_gp(ctxt, 0);
3418
3419 break;
3420 }
3421 case 3: {
3422 u64 rsvd = 0;
3423
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003424 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3425 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003426 rsvd = CR3_L_MODE_RESERVED_BITS;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003427
3428 if (new_val & rsvd)
3429 return emulate_gp(ctxt, 0);
3430
3431 break;
3432 }
3433 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003434 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003435
3436 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3437 return emulate_gp(ctxt, 0);
3438
3439 break;
3440 }
3441 }
3442
3443 return X86EMUL_CONTINUE;
3444}
3445
Joerg Roedel3b88e412011-04-04 12:39:29 +02003446static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3447{
3448 unsigned long dr7;
3449
Avi Kivity717746e2011-04-20 13:37:53 +03003450 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003451
3452 /* Check if DR7.Global_Enable is set */
3453 return dr7 & (1 << 13);
3454}
3455
3456static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3457{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003458 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003459 u64 cr4;
3460
3461 if (dr > 7)
3462 return emulate_ud(ctxt);
3463
Avi Kivity717746e2011-04-20 13:37:53 +03003464 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003465 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3466 return emulate_ud(ctxt);
3467
3468 if (check_dr7_gd(ctxt))
3469 return emulate_db(ctxt);
3470
3471 return X86EMUL_CONTINUE;
3472}
3473
3474static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3475{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003476 u64 new_val = ctxt->src.val64;
3477 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003478
3479 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3480 return emulate_gp(ctxt, 0);
3481
3482 return check_dr_read(ctxt);
3483}
3484
Joerg Roedel01de8b02011-04-04 12:39:31 +02003485static int check_svme(struct x86_emulate_ctxt *ctxt)
3486{
3487 u64 efer;
3488
Avi Kivity717746e2011-04-20 13:37:53 +03003489 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003490
3491 if (!(efer & EFER_SVME))
3492 return emulate_ud(ctxt);
3493
3494 return X86EMUL_CONTINUE;
3495}
3496
3497static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3498{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003499 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003500
3501 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003502 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003503 return emulate_gp(ctxt, 0);
3504
3505 return check_svme(ctxt);
3506}
3507
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003508static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3509{
Avi Kivity717746e2011-04-20 13:37:53 +03003510 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003511
Avi Kivity717746e2011-04-20 13:37:53 +03003512 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003513 return emulate_ud(ctxt);
3514
3515 return X86EMUL_CONTINUE;
3516}
3517
Joerg Roedel80612522011-04-04 12:39:33 +02003518static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3519{
Avi Kivity717746e2011-04-20 13:37:53 +03003520 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003521 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003522
Avi Kivity717746e2011-04-20 13:37:53 +03003523 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03003524 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02003525 return emulate_gp(ctxt, 0);
3526
3527 return X86EMUL_CONTINUE;
3528}
3529
Joerg Roedelf6511932011-04-04 12:39:35 +02003530static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3531{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003532 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3533 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003534 return emulate_gp(ctxt, 0);
3535
3536 return X86EMUL_CONTINUE;
3537}
3538
3539static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3540{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003541 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3542 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003543 return emulate_gp(ctxt, 0);
3544
3545 return X86EMUL_CONTINUE;
3546}
3547
Avi Kivity73fba5f2010-07-29 15:11:53 +03003548#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003549#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003550#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3551 .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003552#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003553#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003554#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3555#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003556#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003557#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003558#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003559#define II(_f, _e, _i) \
3560 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003561#define IIP(_f, _e, _i, _p) \
3562 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3563 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003564#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003565
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003566#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003567#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003568#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003569#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003570#define I2bvIP(_f, _e, _i, _p) \
3571 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003572
Avi Kivityfb864fb2013-01-04 16:18:54 +02003573#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3574 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3575 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003576
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003577static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003578 DI(SrcNone | Priv, monitor),
3579 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003580 N, N, N, N, N, N,
3581};
3582
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003583static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003584 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003585 II(SrcNone | Prot | EmulateOnUD, em_vmmcall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003586 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3587 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3588 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3589 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3590 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3591 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003592};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003593
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003594static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003595 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003596 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003597 N, N, N, N, N, N,
3598};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003599
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003600static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003601 F(Lock, em_add),
3602 F(Lock | PageTable, em_or),
3603 F(Lock, em_adc),
3604 F(Lock, em_sbb),
3605 F(Lock | PageTable, em_and),
3606 F(Lock, em_sub),
3607 F(Lock, em_xor),
3608 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003609};
3610
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003611static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003612 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003613};
3614
Avi Kivity007a3b52013-01-19 19:51:51 +02003615static const struct opcode group2[] = {
3616 F(DstMem | ModRM, em_rol),
3617 F(DstMem | ModRM, em_ror),
3618 F(DstMem | ModRM, em_rcl),
3619 F(DstMem | ModRM, em_rcr),
3620 F(DstMem | ModRM, em_shl),
3621 F(DstMem | ModRM, em_shr),
3622 F(DstMem | ModRM, em_shl),
3623 F(DstMem | ModRM, em_sar),
3624};
3625
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003626static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003627 F(DstMem | SrcImm | NoWrite, em_test),
3628 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003629 F(DstMem | SrcNone | Lock, em_not),
3630 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02003631 F(DstXacc | Src2Mem, em_mul_ex),
3632 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02003633 F(DstXacc | Src2Mem, em_div_ex),
3634 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003635};
3636
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003637static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003638 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3639 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003640 N, N, N, N, N, N,
3641};
3642
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003643static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003644 F(DstMem | SrcNone | Lock, em_inc),
3645 F(DstMem | SrcNone | Lock, em_dec),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003646 I(SrcMem | Stack, em_grp45),
3647 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3648 I(SrcMem | Stack, em_grp45),
3649 I(SrcMemFAddr | ImplicitOps, em_grp45),
Gleb Natapov188424b2013-04-11 12:32:14 +03003650 I(SrcMem | Stack, em_grp45), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003651};
3652
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003653static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003654 DI(Prot, sldt),
3655 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003656 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003657 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003658 N, N, N, N,
3659};
3660
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003661static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03003662 II(Mov | DstMem, em_sgdt, sgdt),
3663 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003664 II(SrcMem | Priv, em_lgdt, lgdt),
3665 II(SrcMem | Priv, em_lidt, lidt),
3666 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3667 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3668 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003669}, {
Borislav Petkovb51e9742013-09-22 16:44:52 +02003670 I(SrcNone | Priv | EmulateOnUD, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003671 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003672 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003673 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3674 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3675 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003676} };
3677
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003678static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003679 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003680 F(DstMem | SrcImmByte | NoWrite, em_bt),
3681 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3682 F(DstMem | SrcImmByte | Lock, em_btr),
3683 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003684};
3685
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003686static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003687 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003688}, {
3689 N, N, N, N, N, N, N, N,
3690} };
3691
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003692static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003693 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003694 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003695};
3696
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003697static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003698 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003699};
3700
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003701static const struct gprefix pfx_vmovntpx = {
Avi Kivity3e114eb2012-04-09 18:40:01 +03003702 I(0, em_mov), N, N, N,
3703};
3704
Igor Mammedov27ce8252014-03-15 21:01:59 +01003705static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01003706 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003707};
3708
Gleb Natapov045a2822012-12-20 16:57:43 +02003709static const struct escape escape_d9 = { {
3710 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3711}, {
3712 /* 0xC0 - 0xC7 */
3713 N, N, N, N, N, N, N, N,
3714 /* 0xC8 - 0xCF */
3715 N, N, N, N, N, N, N, N,
3716 /* 0xD0 - 0xC7 */
3717 N, N, N, N, N, N, N, N,
3718 /* 0xD8 - 0xDF */
3719 N, N, N, N, N, N, N, N,
3720 /* 0xE0 - 0xE7 */
3721 N, N, N, N, N, N, N, N,
3722 /* 0xE8 - 0xEF */
3723 N, N, N, N, N, N, N, N,
3724 /* 0xF0 - 0xF7 */
3725 N, N, N, N, N, N, N, N,
3726 /* 0xF8 - 0xFF */
3727 N, N, N, N, N, N, N, N,
3728} };
3729
3730static const struct escape escape_db = { {
3731 N, N, N, N, N, N, N, N,
3732}, {
3733 /* 0xC0 - 0xC7 */
3734 N, N, N, N, N, N, N, N,
3735 /* 0xC8 - 0xCF */
3736 N, N, N, N, N, N, N, N,
3737 /* 0xD0 - 0xC7 */
3738 N, N, N, N, N, N, N, N,
3739 /* 0xD8 - 0xDF */
3740 N, N, N, N, N, N, N, N,
3741 /* 0xE0 - 0xE7 */
3742 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3743 /* 0xE8 - 0xEF */
3744 N, N, N, N, N, N, N, N,
3745 /* 0xF0 - 0xF7 */
3746 N, N, N, N, N, N, N, N,
3747 /* 0xF8 - 0xFF */
3748 N, N, N, N, N, N, N, N,
3749} };
3750
3751static const struct escape escape_dd = { {
3752 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3753}, {
3754 /* 0xC0 - 0xC7 */
3755 N, N, N, N, N, N, N, N,
3756 /* 0xC8 - 0xCF */
3757 N, N, N, N, N, N, N, N,
3758 /* 0xD0 - 0xC7 */
3759 N, N, N, N, N, N, N, N,
3760 /* 0xD8 - 0xDF */
3761 N, N, N, N, N, N, N, N,
3762 /* 0xE0 - 0xE7 */
3763 N, N, N, N, N, N, N, N,
3764 /* 0xE8 - 0xEF */
3765 N, N, N, N, N, N, N, N,
3766 /* 0xF0 - 0xF7 */
3767 N, N, N, N, N, N, N, N,
3768 /* 0xF8 - 0xFF */
3769 N, N, N, N, N, N, N, N,
3770} };
3771
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003772static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003773 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003774 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003775 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3776 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003777 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003778 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003779 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3780 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003781 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003782 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003783 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3784 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003785 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003786 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003787 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3788 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003789 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003790 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003791 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003792 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003793 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003794 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003795 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003796 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003797 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02003798 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003799 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003800 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003801 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003802 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003803 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003804 I(ImplicitOps | Stack | No64, em_pusha),
3805 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003806 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3807 N, N, N, N,
3808 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003809 I(SrcImm | Mov | Stack, em_push),
3810 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003811 I(SrcImmByte | Mov | Stack, em_push),
3812 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003813 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003814 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003815 /* 0x70 - 0x7F */
3816 X16(D(SrcImmByte)),
3817 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003818 G(ByteOp | DstMem | SrcImm, group1),
3819 G(DstMem | SrcImm, group1),
3820 G(ByteOp | DstMem | SrcImm | No64, group1),
3821 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003822 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003823 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003824 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003825 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003826 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003827 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003828 D(ModRM | SrcMem | NoAccess | DstReg),
3829 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3830 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003831 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003832 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003833 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003834 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003835 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003836 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01003837 II(ImplicitOps | Stack, em_popf, popf),
3838 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003839 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003840 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003841 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003842 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003843 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003844 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003845 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003846 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3847 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003848 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003849 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003850 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003851 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02003852 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003853 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003854 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003855 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003856 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003857 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3858 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003859 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003860 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003861 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Bruce Rogers32611072013-09-09 09:40:20 -06003862 I(ImplicitOps | Stack | SrcImmU16, em_ret_far_imm),
3863 I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003864 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003865 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003866 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003867 G(Src2One | ByteOp, group2), G(Src2One, group2),
3868 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003869 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02003870 I(DstAcc | SrcImmUByte | No64, em_aad),
3871 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02003872 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003873 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02003874 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003875 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003876 X3(I(SrcImmByte, em_loop)),
3877 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003878 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3879 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003880 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003881 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003882 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003883 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3884 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003885 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003886 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003887 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3888 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003889 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003890 D(ImplicitOps), D(ImplicitOps),
3891 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003892 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3893};
3894
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003895static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003896 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003897 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02003898 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003899 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003900 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003901 N, D(ImplicitOps | ModRM), N, N,
3902 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02003903 N, N, N, N, N, N, N, N,
3904 D(ImplicitOps | ModRM), N, N, N, N, N, N, D(ImplicitOps | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003905 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03003906 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
3907 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
3908 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
3909 check_cr_write),
3910 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
3911 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003912 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003913 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
3914 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
3915 N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
Avi Kivity3e114eb2012-04-09 18:40:01 +03003916 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003917 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003918 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02003919 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003920 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02003921 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003922 I(ImplicitOps | EmulateOnUD, em_sysenter),
3923 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003924 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003925 N, N, N, N, N, N, N, N,
3926 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03003927 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003928 /* 0x50 - 0x5F */
3929 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3930 /* 0x60 - 0x6F */
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(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003935 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003936 N, N, N, N,
3937 N, N, N, N,
3938 N, N, N, N,
3939 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003940 /* 0x80 - 0x8F */
3941 X16(D(SrcImm)),
3942 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003943 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003944 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003945 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02003946 II(ImplicitOps, em_cpuid, cpuid),
3947 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02003948 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
3949 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003950 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003951 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003952 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02003953 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02003954 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
3955 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Avi Kivity4d758342013-01-19 19:51:55 +02003956 D(ModRM), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003957 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09003958 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03003959 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02003960 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03003961 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
3962 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003963 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003964 /* 0xB8 - 0xBF */
3965 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003966 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02003967 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
3968 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003969 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03003970 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02003971 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003972 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003973 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03003974 /* 0xC8 - 0xCF */
3975 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003976 /* 0xD0 - 0xDF */
3977 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3978 /* 0xE0 - 0xEF */
3979 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3980 /* 0xF0 - 0xFF */
3981 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3982};
3983
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003984static const struct gprefix three_byte_0f_38_f0 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01003985 I(DstReg | SrcMem | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003986};
3987
3988static const struct gprefix three_byte_0f_38_f1 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01003989 I(DstMem | SrcReg | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003990};
3991
3992/*
3993 * Insns below are selected by the prefix which indexed by the third opcode
3994 * byte.
3995 */
3996static const struct opcode opcode_map_0f_38[256] = {
3997 /* 0x00 - 0x7f */
3998 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01003999 /* 0x80 - 0xef */
4000 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4001 /* 0xf0 - 0xf1 */
4002 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f0),
4003 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f1),
4004 /* 0xf2 - 0xff */
4005 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004006};
4007
Avi Kivity73fba5f2010-07-29 15:11:53 +03004008#undef D
4009#undef N
4010#undef G
4011#undef GD
4012#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004013#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004014#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004015
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004016#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004017#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004018#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004019#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004020#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004021
Avi Kivity9dac77f2011-06-01 15:34:25 +03004022static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004023{
4024 unsigned size;
4025
Avi Kivity9dac77f2011-06-01 15:34:25 +03004026 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004027 if (size == 8)
4028 size = 4;
4029 return size;
4030}
4031
4032static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4033 unsigned size, bool sign_extension)
4034{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004035 int rc = X86EMUL_CONTINUE;
4036
4037 op->type = OP_IMM;
4038 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004039 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004040 /* NB. Immediates are sign-extended as necessary. */
4041 switch (op->bytes) {
4042 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004043 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004044 break;
4045 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004046 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004047 break;
4048 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004049 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004050 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004051 case 8:
4052 op->val = insn_fetch(s64, ctxt);
4053 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004054 }
4055 if (!sign_extension) {
4056 switch (op->bytes) {
4057 case 1:
4058 op->val &= 0xff;
4059 break;
4060 case 2:
4061 op->val &= 0xffff;
4062 break;
4063 case 4:
4064 op->val &= 0xffffffff;
4065 break;
4066 }
4067 }
4068done:
4069 return rc;
4070}
4071
Avi Kivitya9945542011-09-13 10:45:41 +03004072static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4073 unsigned d)
4074{
4075 int rc = X86EMUL_CONTINUE;
4076
4077 switch (d) {
4078 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004079 decode_register_operand(ctxt, op);
Avi Kivitya9945542011-09-13 10:45:41 +03004080 break;
4081 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004082 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03004083 break;
4084 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004085 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004086 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03004087 *op = ctxt->memop;
4088 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004089 if (ctxt->d & BitOp)
Avi Kivitya9945542011-09-13 10:45:41 +03004090 fetch_bit_operand(ctxt);
4091 op->orig_val = op->val;
4092 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004093 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004094 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004095 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03004096 case OpAcc:
4097 op->type = OP_REG;
4098 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004099 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya9945542011-09-13 10:45:41 +03004100 fetch_register_operand(op);
4101 op->orig_val = op->val;
4102 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004103 case OpAccLo:
4104 op->type = OP_REG;
4105 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4106 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4107 fetch_register_operand(op);
4108 op->orig_val = op->val;
4109 break;
4110 case OpAccHi:
4111 if (ctxt->d & ByteOp) {
4112 op->type = OP_NONE;
4113 break;
4114 }
4115 op->type = OP_REG;
4116 op->bytes = ctxt->op_bytes;
4117 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4118 fetch_register_operand(op);
4119 op->orig_val = op->val;
4120 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004121 case OpDI:
4122 op->type = OP_MEM;
4123 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4124 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004125 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya9945542011-09-13 10:45:41 +03004126 op->addr.mem.seg = VCPU_SREG_ES;
4127 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004128 op->count = 1;
Avi Kivitya9945542011-09-13 10:45:41 +03004129 break;
4130 case OpDX:
4131 op->type = OP_REG;
4132 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004133 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya9945542011-09-13 10:45:41 +03004134 fetch_register_operand(op);
4135 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004136 case OpCL:
4137 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004138 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004139 break;
4140 case OpImmByte:
4141 rc = decode_imm(ctxt, op, 1, true);
4142 break;
4143 case OpOne:
4144 op->bytes = 1;
4145 op->val = 1;
4146 break;
4147 case OpImm:
4148 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4149 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004150 case OpImm64:
4151 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4152 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004153 case OpMem8:
4154 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004155 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004156 ctxt->memop.addr.reg = decode_register(ctxt,
4157 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004158 fetch_register_operand(&ctxt->memop);
4159 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004160 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004161 case OpMem16:
4162 ctxt->memop.bytes = 2;
4163 goto mem_common;
4164 case OpMem32:
4165 ctxt->memop.bytes = 4;
4166 goto mem_common;
4167 case OpImmU16:
4168 rc = decode_imm(ctxt, op, 2, false);
4169 break;
4170 case OpImmU:
4171 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4172 break;
4173 case OpSI:
4174 op->type = OP_MEM;
4175 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4176 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004177 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Avi Kivity0fe59122011-09-13 10:45:47 +03004178 op->addr.mem.seg = seg_override(ctxt);
4179 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004180 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004181 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004182 case OpXLat:
4183 op->type = OP_MEM;
4184 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4185 op->addr.mem.ea =
4186 register_address(ctxt,
4187 reg_read(ctxt, VCPU_REGS_RBX) +
4188 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
4189 op->addr.mem.seg = seg_override(ctxt);
4190 op->val = 0;
4191 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004192 case OpImmFAddr:
4193 op->type = OP_IMM;
4194 op->addr.mem.ea = ctxt->_eip;
4195 op->bytes = ctxt->op_bytes + 2;
4196 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4197 break;
4198 case OpMemFAddr:
4199 ctxt->memop.bytes = ctxt->op_bytes + 2;
4200 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004201 case OpES:
4202 op->val = VCPU_SREG_ES;
4203 break;
4204 case OpCS:
4205 op->val = VCPU_SREG_CS;
4206 break;
4207 case OpSS:
4208 op->val = VCPU_SREG_SS;
4209 break;
4210 case OpDS:
4211 op->val = VCPU_SREG_DS;
4212 break;
4213 case OpFS:
4214 op->val = VCPU_SREG_FS;
4215 break;
4216 case OpGS:
4217 op->val = VCPU_SREG_GS;
4218 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004219 case OpImplicit:
4220 /* Special instructions do their own operand decoding. */
4221 default:
4222 op->type = OP_NONE; /* Disable writeback. */
4223 break;
4224 }
4225
4226done:
4227 return rc;
4228}
4229
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004230int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004231{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004232 int rc = X86EMUL_CONTINUE;
4233 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004234 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004235 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03004236 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004237
Avi Kivityf09ed832011-09-13 10:45:40 +03004238 ctxt->memop.type = OP_NONE;
4239 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004240 ctxt->_eip = ctxt->eip;
4241 ctxt->fetch.start = ctxt->_eip;
4242 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004243 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01004244 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004245 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004246
4247 switch (mode) {
4248 case X86EMUL_MODE_REAL:
4249 case X86EMUL_MODE_VM86:
4250 case X86EMUL_MODE_PROT16:
4251 def_op_bytes = def_ad_bytes = 2;
4252 break;
4253 case X86EMUL_MODE_PROT32:
4254 def_op_bytes = def_ad_bytes = 4;
4255 break;
4256#ifdef CONFIG_X86_64
4257 case X86EMUL_MODE_PROT64:
4258 def_op_bytes = 4;
4259 def_ad_bytes = 8;
4260 break;
4261#endif
4262 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004263 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004264 }
4265
Avi Kivity9dac77f2011-06-01 15:34:25 +03004266 ctxt->op_bytes = def_op_bytes;
4267 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004268
4269 /* Legacy prefixes. */
4270 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004271 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004272 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004273 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004274 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004275 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004276 break;
4277 case 0x67: /* address-size override */
4278 if (mode == X86EMUL_MODE_PROT64)
4279 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004280 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004281 else
4282 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004283 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004284 break;
4285 case 0x26: /* ES override */
4286 case 0x2e: /* CS override */
4287 case 0x36: /* SS override */
4288 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004289 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004290 break;
4291 case 0x64: /* FS override */
4292 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004293 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004294 break;
4295 case 0x40 ... 0x4f: /* REX */
4296 if (mode != X86EMUL_MODE_PROT64)
4297 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004298 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004299 continue;
4300 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004301 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004302 break;
4303 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004304 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004305 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004306 break;
4307 default:
4308 goto done_prefixes;
4309 }
4310
4311 /* Any legacy prefix after a REX prefix nullifies its effect. */
4312
Avi Kivity9dac77f2011-06-01 15:34:25 +03004313 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004314 }
4315
4316done_prefixes:
4317
4318 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004319 if (ctxt->rex_prefix & 8)
4320 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004321
4322 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004323 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004324 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004325 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004326 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004327 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004328 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004329
4330 /* 0F_38 opcode map */
4331 if (ctxt->b == 0x38) {
4332 ctxt->opcode_len = 3;
4333 ctxt->b = insn_fetch(u8, ctxt);
4334 opcode = opcode_map_0f_38[ctxt->b];
4335 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004336 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004337 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004338
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004339 if (ctxt->d & ModRM)
4340 ctxt->modrm = insn_fetch(u8, ctxt);
4341
Nadav Amit7fe864d2014-06-02 18:34:03 +03004342 /* vex-prefix instructions are not implemented */
4343 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
4344 (mode == X86EMUL_MODE_PROT64 ||
4345 (mode >= X86EMUL_MODE_PROT16 && (ctxt->modrm & 0x80)))) {
4346 ctxt->d = NotImpl;
4347 }
4348
Avi Kivity9dac77f2011-06-01 15:34:25 +03004349 while (ctxt->d & GroupMask) {
4350 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004351 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004352 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004353 opcode = opcode.u.group[goffset];
4354 break;
4355 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004356 goffset = (ctxt->modrm >> 3) & 7;
4357 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004358 opcode = opcode.u.gdual->mod3[goffset];
4359 else
4360 opcode = opcode.u.gdual->mod012[goffset];
4361 break;
4362 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004363 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004364 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004365 break;
4366 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004367 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004368 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004369 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004370 switch (simd_prefix) {
4371 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4372 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4373 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4374 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4375 }
4376 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004377 case Escape:
4378 if (ctxt->modrm > 0xbf)
4379 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4380 else
4381 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4382 break;
Avi Kivity46561642011-04-24 14:09:59 +03004383 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004384 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004385 }
Avi Kivity46561642011-04-24 14:09:59 +03004386
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004387 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004388 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004389 }
4390
Avi Kivity9dac77f2011-06-01 15:34:25 +03004391 ctxt->execute = opcode.u.execute;
4392 ctxt->check_perm = opcode.check_perm;
4393 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004394
4395 /* Unrecognised? */
Gleb Natapov1146a782013-04-11 12:30:01 +03004396 if (ctxt->d == 0 || (ctxt->d & NotImpl))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004397 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004398
Borislav Petkovb51e9742013-09-22 16:44:52 +02004399 if (!(ctxt->d & EmulateOnUD) && ctxt->ud)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004400 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004401
Avi Kivity9dac77f2011-06-01 15:34:25 +03004402 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
4403 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004404
Avi Kivity9dac77f2011-06-01 15:34:25 +03004405 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004406 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004407 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004408 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03004409 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004410 }
4411
Avi Kivity9dac77f2011-06-01 15:34:25 +03004412 if (ctxt->d & Sse)
4413 ctxt->op_bytes = 16;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004414 else if (ctxt->d & Mmx)
4415 ctxt->op_bytes = 8;
Avi Kivity12537912011-03-29 11:41:27 +02004416
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004417 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004418 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004419 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004420 if (!ctxt->has_seg_override)
4421 set_seg_override(ctxt, ctxt->modrm_seg);
4422 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004423 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004424 if (rc != X86EMUL_CONTINUE)
4425 goto done;
4426
Avi Kivity9dac77f2011-06-01 15:34:25 +03004427 if (!ctxt->has_seg_override)
4428 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004429
Avi Kivityf09ed832011-09-13 10:45:40 +03004430 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004431
Avi Kivityf09ed832011-09-13 10:45:40 +03004432 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
4433 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004434
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004435 /*
4436 * Decode and fetch the source operand: register, memory
4437 * or immediate.
4438 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004439 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004440 if (rc != X86EMUL_CONTINUE)
4441 goto done;
4442
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004443 /*
4444 * Decode and fetch the second source operand: register, memory
4445 * or immediate.
4446 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004447 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004448 if (rc != X86EMUL_CONTINUE)
4449 goto done;
4450
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004451 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03004452 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004453
4454done:
Avi Kivityf09ed832011-09-13 10:45:40 +03004455 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
4456 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004457
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004458 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004459}
4460
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004461bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4462{
4463 return ctxt->d & PageTable;
4464}
4465
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004466static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4467{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004468 /* The second termination condition only applies for REPE
4469 * and REPNE. Test if the repeat string operation prefix is
4470 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4471 * corresponding termination condition according to:
4472 * - if REPE/REPZ and ZF = 0 then done
4473 * - if REPNE/REPNZ and ZF = 1 then done
4474 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004475 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4476 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4477 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004478 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004479 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004480 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4481 return true;
4482
4483 return false;
4484}
4485
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004486static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4487{
4488 bool fault = false;
4489
4490 ctxt->ops->get_fpu(ctxt);
4491 asm volatile("1: fwait \n\t"
4492 "2: \n\t"
4493 ".pushsection .fixup,\"ax\" \n\t"
4494 "3: \n\t"
4495 "movb $1, %[fault] \n\t"
4496 "jmp 2b \n\t"
4497 ".popsection \n\t"
4498 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004499 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004500 ctxt->ops->put_fpu(ctxt);
4501
4502 if (unlikely(fault))
4503 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4504
4505 return X86EMUL_CONTINUE;
4506}
4507
4508static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4509 struct operand *op)
4510{
4511 if (op->type == OP_MM)
4512 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4513}
4514
Avi Kivitye28bbd42013-01-04 16:18:48 +02004515static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4516{
4517 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivityb9fa4092013-02-09 11:31:48 +02004518 if (!(ctxt->d & ByteOp))
4519 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Avi Kivitye28bbd42013-01-04 16:18:48 +02004520 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004521 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
4522 [fastop]"+S"(fop)
4523 : "c"(ctxt->src2.val));
Avi Kivitye28bbd42013-01-04 16:18:48 +02004524 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004525 if (!fop) /* exception is returned in fop variable */
4526 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02004527 return X86EMUL_CONTINUE;
4528}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004529
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004530int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004531{
Mathias Krause0225fb52012-08-30 01:30:16 +02004532 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004533 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004534 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004535
Avi Kivity9dac77f2011-06-01 15:34:25 +03004536 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004537
Gleb Natapov1146a782013-04-11 12:30:01 +03004538 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4539 (ctxt->d & Undefined)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004540 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02004541 goto done;
4542 }
4543
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004544 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004545 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004546 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004547 goto done;
4548 }
4549
Avi Kivity9dac77f2011-06-01 15:34:25 +03004550 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004551 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004552 goto done;
4553 }
4554
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004555 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4556 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02004557 rc = emulate_ud(ctxt);
4558 goto done;
4559 }
4560
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004561 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02004562 rc = emulate_nm(ctxt);
4563 goto done;
4564 }
4565
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004566 if (ctxt->d & Mmx) {
4567 rc = flush_pending_x87_faults(ctxt);
4568 if (rc != X86EMUL_CONTINUE)
4569 goto done;
4570 /*
4571 * Now that we know the fpu is exception safe, we can fetch
4572 * operands from it.
4573 */
4574 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4575 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4576 if (!(ctxt->d & Mov))
4577 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4578 }
4579
Avi Kivity9dac77f2011-06-01 15:34:25 +03004580 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4581 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004582 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004583 if (rc != X86EMUL_CONTINUE)
4584 goto done;
4585 }
4586
Gleb Natapove92805a2010-02-10 14:21:35 +02004587 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004588 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004589 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02004590 goto done;
4591 }
4592
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004593 /* Instruction can only be executed in protected mode */
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03004594 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004595 rc = emulate_ud(ctxt);
4596 goto done;
4597 }
4598
Joerg Roedeld09beab2011-04-04 12:39:25 +02004599 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004600 if (ctxt->check_perm) {
4601 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004602 if (rc != X86EMUL_CONTINUE)
4603 goto done;
4604 }
4605
Avi Kivity9dac77f2011-06-01 15:34:25 +03004606 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4607 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004608 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004609 if (rc != X86EMUL_CONTINUE)
4610 goto done;
4611 }
4612
Avi Kivity9dac77f2011-06-01 15:34:25 +03004613 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004614 /* All REP prefixes have the same first termination condition */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004615 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004616 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004617 goto done;
4618 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004619 }
4620
Avi Kivity9dac77f2011-06-01 15:34:25 +03004621 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4622 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4623 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004624 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004625 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004626 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004627 }
4628
Avi Kivity9dac77f2011-06-01 15:34:25 +03004629 if (ctxt->src2.type == OP_MEM) {
4630 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4631 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004632 if (rc != X86EMUL_CONTINUE)
4633 goto done;
4634 }
4635
Avi Kivity9dac77f2011-06-01 15:34:25 +03004636 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004637 goto special_insn;
4638
4639
Avi Kivity9dac77f2011-06-01 15:34:25 +03004640 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004641 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004642 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4643 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004644 if (rc != X86EMUL_CONTINUE)
4645 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004646 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004647 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004648
Avi Kivity018a98d2007-11-27 19:30:56 +02004649special_insn:
4650
Avi Kivity9dac77f2011-06-01 15:34:25 +03004651 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4652 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004653 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004654 if (rc != X86EMUL_CONTINUE)
4655 goto done;
4656 }
4657
Avi Kivity9dac77f2011-06-01 15:34:25 +03004658 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004659 if (ctxt->d & Fastop) {
4660 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4661 rc = fastop(ctxt, fop);
4662 if (rc != X86EMUL_CONTINUE)
4663 goto done;
4664 goto writeback;
4665 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004666 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004667 if (rc != X86EMUL_CONTINUE)
4668 goto done;
4669 goto writeback;
4670 }
4671
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004672 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004673 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004674 else if (ctxt->opcode_len == 3)
4675 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004676
Avi Kivity9dac77f2011-06-01 15:34:25 +03004677 switch (ctxt->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004678 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004679 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004680 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004681 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004682 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004683 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004684 if (test_cc(ctxt->b, ctxt->eflags))
4685 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004686 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004687 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004688 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004689 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004690 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004691 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03004692 ctxt->dst.type = OP_NONE;
4693 else
4694 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004695 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004696 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004697 switch (ctxt->op_bytes) {
4698 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4699 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4700 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004701 }
4702 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004703 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004704 rc = emulate_int(ctxt, 3);
4705 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004706 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004707 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004708 break;
4709 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004710 if (ctxt->eflags & EFLG_OF)
4711 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004712 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004713 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004714 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004715 jmp_rel(ctxt, ctxt->src.val);
4716 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004717 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004718 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004719 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004720 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004721 case 0xf5: /* cmc */
4722 /* complement carry flag from eflags reg */
4723 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004724 break;
4725 case 0xf8: /* clc */
4726 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004727 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004728 case 0xf9: /* stc */
4729 ctxt->eflags |= EFLG_CF;
4730 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004731 case 0xfc: /* cld */
4732 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004733 break;
4734 case 0xfd: /* std */
4735 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004736 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004737 default:
4738 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004739 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004740
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004741 if (rc != X86EMUL_CONTINUE)
4742 goto done;
4743
Avi Kivity018a98d2007-11-27 19:30:56 +02004744writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02004745 if (ctxt->d & SrcWrite) {
4746 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
4747 rc = writeback(ctxt, &ctxt->src);
4748 if (rc != X86EMUL_CONTINUE)
4749 goto done;
4750 }
Nadav Amitee212292014-06-15 16:12:58 +03004751 if (!(ctxt->d & NoWrite)) {
4752 rc = writeback(ctxt, &ctxt->dst);
4753 if (rc != X86EMUL_CONTINUE)
4754 goto done;
4755 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004756
Gleb Natapov5cd21912010-03-18 15:20:26 +02004757 /*
4758 * restore dst type in case the decoding will be reused
4759 * (happens for string instruction )
4760 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004761 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004762
Avi Kivity9dac77f2011-06-01 15:34:25 +03004763 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004764 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004765
Avi Kivity9dac77f2011-06-01 15:34:25 +03004766 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004767 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004768
Avi Kivity9dac77f2011-06-01 15:34:25 +03004769 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004770 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004771 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004772 if ((ctxt->d & SrcMask) == SrcSI)
4773 count = ctxt->src.count;
4774 else
4775 count = ctxt->dst.count;
4776 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4777 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004778
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004779 if (!string_insn_completed(ctxt)) {
4780 /*
4781 * Re-enter guest when pio read ahead buffer is empty
4782 * or, if it is not used, after each 1024 iteration.
4783 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004784 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004785 (r->end == 0 || r->end != r->pos)) {
4786 /*
4787 * Reset read cache. Usually happens before
4788 * decode, but since instruction is restarted
4789 * we have to do it here.
4790 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004791 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004792 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004793 return EMULATION_RESTART;
4794 }
4795 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004796 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004797 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004798
Avi Kivity9dac77f2011-06-01 15:34:25 +03004799 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004800
4801done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004802 if (rc == X86EMUL_PROPAGATE_FAULT)
4803 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004804 if (rc == X86EMUL_INTERCEPTED)
4805 return EMULATION_INTERCEPTED;
4806
Avi Kivitydd856ef2012-08-27 23:46:17 +03004807 if (rc == X86EMUL_CONTINUE)
4808 writeback_registers(ctxt);
4809
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004810 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004811
4812twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004813 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004814 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004815 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004816 break;
4817 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004818 case 0x0d: /* GrpP (prefetch) */
4819 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004820 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02004821 break;
4822 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004823 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004824 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004825 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004826 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004827 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004828 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03004829 if (test_cc(ctxt->b, ctxt->eflags))
4830 ctxt->dst.val = ctxt->src.val;
4831 else if (ctxt->mode != X86EMUL_MODE_PROT64 ||
4832 ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004833 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004834 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004835 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004836 if (test_cc(ctxt->b, ctxt->eflags))
4837 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004838 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004839 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004840 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004841 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004842 case 0xae: /* clflush */
4843 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004844 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004845 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004846 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004847 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004848 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004849 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004850 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004851 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004852 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004853 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004854 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004855 ctxt->dst.bytes = ctxt->op_bytes;
Nadav Amit3b320042014-06-02 18:34:08 +03004856 ctxt->dst.val = (ctxt->op_bytes == 8) ? (u64) ctxt->src.val :
4857 (u32) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004858 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004859 default:
4860 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004861 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004862
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004863threebyte_insn:
4864
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004865 if (rc != X86EMUL_CONTINUE)
4866 goto done;
4867
Avi Kivity6aa8b732006-12-10 02:21:36 -08004868 goto writeback;
4869
4870cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004871 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004872}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004873
4874void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
4875{
4876 invalidate_registers(ctxt);
4877}
4878
4879void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
4880{
4881 writeback_registers(ctxt);
4882}