blob: 6a1d60956d63dea658e14ef98e1c6a1fb55387ec [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivityb7d491e2013-01-04 16:18:49 +020027#include <linux/stringify.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028
Avi Kivity3eeb3282010-01-21 15:31:48 +020029#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020030#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020031
Avi Kivity6aa8b732006-12-10 02:21:36 -080032/*
Avi Kivitya9945542011-09-13 10:45:41 +030033 * Operand types
34 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030035#define OpNone 0ull
36#define OpImplicit 1ull /* No generic decode */
37#define OpReg 2ull /* Register */
38#define OpMem 3ull /* Memory */
39#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
40#define OpDI 5ull /* ES:DI/EDI/RDI */
41#define OpMem64 6ull /* Memory, 64-bit */
42#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
43#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030044#define OpCL 9ull /* CL register (for shifts) */
45#define OpImmByte 10ull /* 8-bit sign extended immediate */
46#define OpOne 11ull /* Implied 1 */
Nadav Amit5e2c6882012-12-06 21:55:10 -020047#define OpImm 12ull /* Sign extended up to 32-bit immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030048#define OpMem16 13ull /* Memory operand (16-bit). */
49#define OpMem32 14ull /* Memory operand (32-bit). */
50#define OpImmU 15ull /* Immediate operand, zero extended */
51#define OpSI 16ull /* SI/ESI/RSI */
52#define OpImmFAddr 17ull /* Immediate far address */
53#define OpMemFAddr 18ull /* Far address in memory */
54#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030055#define OpES 20ull /* ES */
56#define OpCS 21ull /* CS */
57#define OpSS 22ull /* SS */
58#define OpDS 23ull /* DS */
59#define OpFS 24ull /* FS */
60#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020061#define OpMem8 26ull /* 8-bit zero extended memory operand */
Nadav Amit5e2c6882012-12-06 21:55:10 -020062#define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */
Paolo Bonzini7fa57952013-05-09 11:32:50 +020063#define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */
Avi Kivity820207c2013-02-09 11:31:45 +020064#define OpAccLo 29ull /* Low part of extended acc (AX/AX/EAX/RAX) */
65#define OpAccHi 30ull /* High part of extended acc (-/DX/EDX/RDX) */
Avi Kivitya9945542011-09-13 10:45:41 +030066
Avi Kivity0fe59122011-09-13 10:45:47 +030067#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030068#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya9945542011-09-13 10:45:41 +030069
70/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080071 * Opcode effective-address decode tables.
72 * Note that we only emulate instructions that have at least one memory
73 * operand (excluding implicit stack references). We assume that stack
74 * references and instruction fetches will never occur in special memory
75 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
76 * not be handled.
77 */
78
79/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030080#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080081/* Destination operand type. */
Avi Kivitya9945542011-09-13 10:45:41 +030082#define DstShift 1
83#define ImplicitOps (OpImplicit << DstShift)
84#define DstReg (OpReg << DstShift)
85#define DstMem (OpMem << DstShift)
86#define DstAcc (OpAcc << DstShift)
87#define DstDI (OpDI << DstShift)
88#define DstMem64 (OpMem64 << DstShift)
89#define DstImmUByte (OpImmUByte << DstShift)
90#define DstDX (OpDX << DstShift)
Avi Kivity820207c2013-02-09 11:31:45 +020091#define DstAccLo (OpAccLo << DstShift)
Avi Kivitya9945542011-09-13 10:45:41 +030092#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080093/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030094#define SrcShift 6
95#define SrcNone (OpNone << SrcShift)
96#define SrcReg (OpReg << SrcShift)
97#define SrcMem (OpMem << SrcShift)
98#define SrcMem16 (OpMem16 << SrcShift)
99#define SrcMem32 (OpMem32 << SrcShift)
100#define SrcImm (OpImm << SrcShift)
101#define SrcImmByte (OpImmByte << SrcShift)
102#define SrcOne (OpOne << SrcShift)
103#define SrcImmUByte (OpImmUByte << SrcShift)
104#define SrcImmU (OpImmU << SrcShift)
105#define SrcSI (OpSI << SrcShift)
Paolo Bonzini7fa57952013-05-09 11:32:50 +0200106#define SrcXLat (OpXLat << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300107#define SrcImmFAddr (OpImmFAddr << SrcShift)
108#define SrcMemFAddr (OpMemFAddr << SrcShift)
109#define SrcAcc (OpAcc << SrcShift)
110#define SrcImmU16 (OpImmU16 << SrcShift)
Nadav Amit5e2c6882012-12-06 21:55:10 -0200111#define SrcImm64 (OpImm64 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300112#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200113#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity820207c2013-02-09 11:31:45 +0200114#define SrcAccHi (OpAccHi << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300115#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300116#define BitOp (1<<11)
117#define MemAbs (1<<12) /* Memory operand is absolute displacement */
118#define String (1<<13) /* String instruction (rep capable) */
119#define Stack (1<<14) /* Stack instruction (push/pop) */
120#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
121#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
122#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
123#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
124#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
Gleb Natapov045a2822012-12-20 16:57:43 +0200125#define Escape (5<<15) /* Escape to coprocessor instruction */
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300126#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300127/* Generic ModRM decode. */
128#define ModRM (1<<19)
129/* Destination is only written; never read. */
130#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300131/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200132#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Borislav Petkovb51e9742013-09-22 16:44:52 +0200133#define EmulateOnUD (1<<22) /* Emulate if unsupported by the host */
Avi Kivity5a506b12010-08-01 15:10:29 +0300134#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300135#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300136#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200137#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200138#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300139#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800140#define PageTable (1 << 29) /* instruction used to write page table */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300141#define NotImpl (1 << 30) /* instruction is not implemented */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100142/* Source 2 operand type */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300143#define Src2Shift (31)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300144#define Src2None (OpNone << Src2Shift)
Avi Kivityab2c5ce2013-02-09 11:31:46 +0200145#define Src2Mem (OpMem << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300146#define Src2CL (OpCL << Src2Shift)
147#define Src2ImmByte (OpImmByte << Src2Shift)
148#define Src2One (OpOne << Src2Shift)
149#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300150#define Src2ES (OpES << Src2Shift)
151#define Src2CS (OpCS << Src2Shift)
152#define Src2SS (OpSS << Src2Shift)
153#define Src2DS (OpDS << Src2Shift)
154#define Src2FS (OpFS << Src2Shift)
155#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300156#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300157#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Avi Kivity1c11b372012-04-09 18:39:59 +0300158#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
159#define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */
160#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
Avi Kivitye28bbd42013-01-04 16:18:48 +0200161#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
Avi Kivityb6744dc2013-01-04 16:18:50 +0200162#define NoWrite ((u64)1 << 45) /* No writeback */
Avi Kivityfb32b1e2013-02-09 11:31:44 +0200163#define SrcWrite ((u64)1 << 46) /* Write back src operand */
Nadav Amit9b88ae92014-05-25 23:05:21 +0300164#define NoMod ((u64)1 << 47) /* Mod field is ignored */
Paolo Bonzinid40a6892014-03-27 11:58:02 +0100165#define Intercept ((u64)1 << 48) /* Has valid intercept field */
166#define CheckPerm ((u64)1 << 49) /* Has valid check_perm field */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800167
Avi Kivity820207c2013-02-09 11:31:45 +0200168#define DstXacc (DstAccLo | SrcAccHi | SrcWrite)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800169
Avi Kivityd0e53322010-07-29 15:11:54 +0300170#define X2(x...) x, x
171#define X3(x...) X2(x), x
172#define X4(x...) X2(x), X2(x)
173#define X5(x...) X4(x), x
174#define X6(x...) X4(x), X2(x)
175#define X7(x...) X4(x), X3(x)
176#define X8(x...) X4(x), X4(x)
177#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300178
Avi Kivitye28bbd42013-01-04 16:18:48 +0200179#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
180#define FASTOP_SIZE 8
181
182/*
183 * fastop functions have a special calling convention:
184 *
Avi Kivity017da7b2013-02-09 11:31:47 +0200185 * dst: rax (in/out)
186 * src: rdx (in/out)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200187 * src2: rcx (in)
188 * flags: rflags (in/out)
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200189 * ex: rsi (in:fastop pointer, out:zero if exception)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200190 *
191 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
192 * different operand sizes can be reached by calculation, rather than a jump
193 * table (which would be bigger than the code).
194 *
195 * fastop functions are declared as taking a never-defined fastop parameter,
196 * so they can't be called from C directly.
197 */
198
199struct fastop;
200
Avi Kivityd65b1de2010-07-29 15:11:35 +0300201struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300202 u64 flags : 56;
203 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300204 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300205 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200206 const struct opcode *group;
207 const struct group_dual *gdual;
208 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200209 const struct escape *esc;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200210 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300211 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200212 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300213};
214
215struct group_dual {
216 struct opcode mod012[8];
217 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300218};
219
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200220struct gprefix {
221 struct opcode pfx_no;
222 struct opcode pfx_66;
223 struct opcode pfx_f2;
224 struct opcode pfx_f3;
225};
226
Gleb Natapov045a2822012-12-20 16:57:43 +0200227struct escape {
228 struct opcode op[8];
229 struct opcode high[64];
230};
231
Avi Kivity6aa8b732006-12-10 02:21:36 -0800232/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200233#define EFLG_ID (1<<21)
234#define EFLG_VIP (1<<20)
235#define EFLG_VIF (1<<19)
236#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200237#define EFLG_VM (1<<17)
238#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200239#define EFLG_IOPL (3<<12)
240#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800241#define EFLG_OF (1<<11)
242#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200243#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200244#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800245#define EFLG_SF (1<<7)
246#define EFLG_ZF (1<<6)
247#define EFLG_AF (1<<4)
248#define EFLG_PF (1<<2)
249#define EFLG_CF (1<<0)
250
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300251#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
252#define EFLG_RESERVED_ONE_MASK 2
253
Avi Kivitydd856ef2012-08-27 23:46:17 +0300254static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
255{
256 if (!(ctxt->regs_valid & (1 << nr))) {
257 ctxt->regs_valid |= 1 << nr;
258 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
259 }
260 return ctxt->_regs[nr];
261}
262
263static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
264{
265 ctxt->regs_valid |= 1 << nr;
266 ctxt->regs_dirty |= 1 << nr;
267 return &ctxt->_regs[nr];
268}
269
270static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
271{
272 reg_read(ctxt, nr);
273 return reg_write(ctxt, nr);
274}
275
276static void writeback_registers(struct x86_emulate_ctxt *ctxt)
277{
278 unsigned reg;
279
280 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
281 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
282}
283
284static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
285{
286 ctxt->regs_dirty = 0;
287 ctxt->regs_valid = 0;
288}
289
Avi Kivity6aa8b732006-12-10 02:21:36 -0800290/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800291 * These EFLAGS bits are restored from saved value during emulation, and
292 * any changes are written back to the saved value after emulation.
293 */
294#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
295
Avi Kivitydda96d82008-11-26 15:14:10 +0200296#ifdef CONFIG_X86_64
297#define ON64(x) x
298#else
299#define ON64(x)
300#endif
301
Avi Kivity4d758342013-01-19 19:51:55 +0200302static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *));
303
Avi Kivityb7d491e2013-01-04 16:18:49 +0200304#define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t"
305#define FOP_RET "ret \n\t"
306
307#define FOP_START(op) \
308 extern void em_##op(struct fastop *fake); \
309 asm(".pushsection .text, \"ax\" \n\t" \
310 ".global em_" #op " \n\t" \
311 FOP_ALIGN \
312 "em_" #op ": \n\t"
313
314#define FOP_END \
315 ".popsection")
316
Avi Kivity0bdea062013-01-19 19:51:50 +0200317#define FOPNOP() FOP_ALIGN FOP_RET
318
Avi Kivityb7d491e2013-01-04 16:18:49 +0200319#define FOP1E(op, dst) \
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200320 FOP_ALIGN "10: " #op " %" #dst " \n\t" FOP_RET
321
322#define FOP1EEX(op, dst) \
323 FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200324
325#define FASTOP1(op) \
326 FOP_START(op) \
327 FOP1E(op##b, al) \
328 FOP1E(op##w, ax) \
329 FOP1E(op##l, eax) \
330 ON64(FOP1E(op##q, rax)) \
331 FOP_END
332
Avi Kivityb9fa4092013-02-09 11:31:48 +0200333/* 1-operand, using src2 (for MUL/DIV r/m) */
334#define FASTOP1SRC2(op, name) \
335 FOP_START(name) \
336 FOP1E(op, cl) \
337 FOP1E(op, cx) \
338 FOP1E(op, ecx) \
339 ON64(FOP1E(op, rcx)) \
340 FOP_END
341
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200342/* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */
343#define FASTOP1SRC2EX(op, name) \
344 FOP_START(name) \
345 FOP1EEX(op, cl) \
346 FOP1EEX(op, cx) \
347 FOP1EEX(op, ecx) \
348 ON64(FOP1EEX(op, rcx)) \
349 FOP_END
350
Avi Kivityf7857f32013-01-04 16:18:53 +0200351#define FOP2E(op, dst, src) \
352 FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET
353
354#define FASTOP2(op) \
355 FOP_START(op) \
Avi Kivity017da7b2013-02-09 11:31:47 +0200356 FOP2E(op##b, al, dl) \
357 FOP2E(op##w, ax, dx) \
358 FOP2E(op##l, eax, edx) \
359 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivityf7857f32013-01-04 16:18:53 +0200360 FOP_END
361
Avi Kivity11c363b2013-01-19 19:51:54 +0200362/* 2 operand, word only */
363#define FASTOP2W(op) \
364 FOP_START(op) \
365 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200366 FOP2E(op##w, ax, dx) \
367 FOP2E(op##l, eax, edx) \
368 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivity11c363b2013-01-19 19:51:54 +0200369 FOP_END
370
Avi Kivity007a3b52013-01-19 19:51:51 +0200371/* 2 operand, src is CL */
372#define FASTOP2CL(op) \
373 FOP_START(op) \
374 FOP2E(op##b, al, cl) \
375 FOP2E(op##w, ax, cl) \
376 FOP2E(op##l, eax, cl) \
377 ON64(FOP2E(op##q, rax, cl)) \
378 FOP_END
379
Avi Kivity0bdea062013-01-19 19:51:50 +0200380#define FOP3E(op, dst, src, src2) \
381 FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
382
383/* 3-operand, word-only, src2=cl */
384#define FASTOP3WCL(op) \
385 FOP_START(op) \
386 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200387 FOP3E(op##w, ax, dx, cl) \
388 FOP3E(op##l, eax, edx, cl) \
389 ON64(FOP3E(op##q, rax, rdx, cl)) \
Avi Kivity0bdea062013-01-19 19:51:50 +0200390 FOP_END
391
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200392/* Special case for SETcc - 1 instruction per cc */
393#define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t"
394
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200395asm(".global kvm_fastop_exception \n"
396 "kvm_fastop_exception: xor %esi, %esi; ret");
397
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200398FOP_START(setcc)
399FOP_SETCC(seto)
400FOP_SETCC(setno)
401FOP_SETCC(setc)
402FOP_SETCC(setnc)
403FOP_SETCC(setz)
404FOP_SETCC(setnz)
405FOP_SETCC(setbe)
406FOP_SETCC(setnbe)
407FOP_SETCC(sets)
408FOP_SETCC(setns)
409FOP_SETCC(setp)
410FOP_SETCC(setnp)
411FOP_SETCC(setl)
412FOP_SETCC(setnl)
413FOP_SETCC(setle)
414FOP_SETCC(setnle)
415FOP_END;
416
Paolo Bonzini326f5782013-05-09 11:32:51 +0200417FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET
418FOP_END;
419
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200420static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
421 enum x86_intercept intercept,
422 enum x86_intercept_stage stage)
423{
424 struct x86_instruction_info info = {
425 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300426 .rep_prefix = ctxt->rep_prefix,
427 .modrm_mod = ctxt->modrm_mod,
428 .modrm_reg = ctxt->modrm_reg,
429 .modrm_rm = ctxt->modrm_rm,
430 .src_val = ctxt->src.val64,
Jan Kiszka6cbc5f52014-06-30 12:52:55 +0200431 .dst_val = ctxt->dst.val64,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300432 .src_bytes = ctxt->src.bytes,
433 .dst_bytes = ctxt->dst.bytes,
434 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200435 .next_rip = ctxt->eip,
436 };
437
Avi Kivity29535382011-04-20 13:37:53 +0300438 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200439}
440
Avi Kivityf47cfa32012-06-07 17:49:24 +0300441static void assign_masked(ulong *dest, ulong src, ulong mask)
442{
443 *dest = (*dest & ~mask) | (src & mask);
444}
445
Avi Kivity9dac77f2011-06-01 15:34:25 +0300446static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800447{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300448 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800449}
450
Avi Kivityf47cfa32012-06-07 17:49:24 +0300451static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
452{
453 u16 sel;
454 struct desc_struct ss;
455
456 if (ctxt->mode == X86EMUL_MODE_PROT64)
457 return ~0UL;
458 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
459 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
460}
461
Avi Kivity612e89f2012-06-12 20:03:23 +0300462static int stack_size(struct x86_emulate_ctxt *ctxt)
463{
464 return (__fls(stack_mask(ctxt)) + 1) >> 3;
465}
466
Avi Kivity6aa8b732006-12-10 02:21:36 -0800467/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800468static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300469address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800470{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300471 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800472 return reg;
473 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300474 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800475}
476
477static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300478register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800479{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300480 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800481}
482
Avi Kivity5ad105e2012-08-19 14:34:31 +0300483static void masked_increment(ulong *reg, ulong mask, int inc)
484{
485 assign_masked(reg, *reg + inc, mask);
486}
487
Harvey Harrison7a9572752008-02-19 07:40:41 -0800488static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300489register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800490{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300491 ulong mask;
492
Avi Kivity9dac77f2011-06-01 15:34:25 +0300493 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300494 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800495 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300496 mask = ad_mask(ctxt);
497 masked_increment(reg, mask, inc);
498}
499
500static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
501{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300502 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800503}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800504
Avi Kivity9dac77f2011-06-01 15:34:25 +0300505static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800506{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300507 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800508}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300509
Avi Kivity56697682011-04-03 14:08:51 +0300510static u32 desc_limit_scaled(struct desc_struct *desc)
511{
512 u32 limit = get_desc_limit(desc);
513
514 return desc->g ? (limit << 12) | 0xfff : limit;
515}
516
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900517static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300518{
519 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
520 return 0;
521
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900522 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300523}
524
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200525static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
526 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300527{
Avi Kivityda9cb572010-11-22 17:53:21 +0200528 ctxt->exception.vector = vec;
529 ctxt->exception.error_code = error;
530 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200531 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300532}
533
Joerg Roedel3b88e412011-04-04 12:39:29 +0200534static int emulate_db(struct x86_emulate_ctxt *ctxt)
535{
536 return emulate_exception(ctxt, DB_VECTOR, 0, false);
537}
538
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200539static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300540{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200541 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300542}
543
Avi Kivity618ff152011-04-03 12:32:09 +0300544static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
545{
546 return emulate_exception(ctxt, SS_VECTOR, err, true);
547}
548
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200549static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300550{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200551 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300552}
553
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200554static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300555{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200556 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300557}
558
Avi Kivity34d1f492010-08-26 11:59:01 +0300559static int emulate_de(struct x86_emulate_ctxt *ctxt)
560{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200561 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300562}
563
Avi Kivity12537912011-03-29 11:41:27 +0200564static int emulate_nm(struct x86_emulate_ctxt *ctxt)
565{
566 return emulate_exception(ctxt, NM_VECTOR, 0, false);
567}
568
Avi Kivity1aa36612011-04-27 13:20:30 +0300569static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
570{
571 u16 selector;
572 struct desc_struct desc;
573
574 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
575 return selector;
576}
577
578static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
579 unsigned seg)
580{
581 u16 dummy;
582 u32 base3;
583 struct desc_struct desc;
584
585 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
586 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
587}
588
Avi Kivity1c11b372012-04-09 18:39:59 +0300589/*
590 * x86 defines three classes of vector instructions: explicitly
591 * aligned, explicitly unaligned, and the rest, which change behaviour
592 * depending on whether they're AVX encoded or not.
593 *
594 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
595 * subject to the same check.
596 */
597static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
598{
599 if (likely(size < 16))
600 return false;
601
602 if (ctxt->d & Aligned)
603 return true;
604 else if (ctxt->d & Unaligned)
605 return false;
606 else if (ctxt->d & Avx)
607 return false;
608 else
609 return true;
610}
611
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400612static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300613 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400614 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300615 ulong *linear)
616{
Avi Kivity618ff152011-04-03 12:32:09 +0300617 struct desc_struct desc;
618 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300619 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300620 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300621 u16 sel;
Gleb Natapov3a78a4f2012-12-20 16:57:42 +0200622 unsigned cpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300623
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900624 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300625 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300626 case X86EMUL_MODE_PROT64:
627 if (((signed long)la << 16) >> 16 != la)
628 return emulate_gp(ctxt, 0);
629 break;
630 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300631 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
632 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300633 if (!usable)
634 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200635 /* code segment in protected mode or read-only data segment */
636 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
637 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300638 goto bad;
639 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400640 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300641 goto bad;
642 lim = desc_limit_scaled(&desc);
643 if ((desc.type & 8) || !(desc.type & 4)) {
644 /* expand-up segment */
645 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
646 goto bad;
647 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800648 /* expand-down segment */
Avi Kivity618ff152011-04-03 12:32:09 +0300649 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
650 goto bad;
651 lim = desc.d ? 0xffffffff : 0xffff;
652 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
653 goto bad;
654 }
Avi Kivity717746e2011-04-20 13:37:53 +0300655 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity618ff152011-04-03 12:32:09 +0300656 if (!(desc.type & 8)) {
657 /* data segment */
658 if (cpl > desc.dpl)
659 goto bad;
660 } else if ((desc.type & 8) && !(desc.type & 4)) {
661 /* nonconforming code segment */
662 if (cpl != desc.dpl)
663 goto bad;
664 } else if ((desc.type & 8) && (desc.type & 4)) {
665 /* conforming code segment */
666 if (cpl < desc.dpl)
667 goto bad;
668 }
669 break;
670 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300671 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300672 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300673 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
674 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300675 *linear = la;
676 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300677bad:
678 if (addr.seg == VCPU_SREG_SS)
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300679 return emulate_ss(ctxt, sel);
Avi Kivity618ff152011-04-03 12:32:09 +0300680 else
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300681 return emulate_gp(ctxt, sel);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300682}
683
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400684static int linearize(struct x86_emulate_ctxt *ctxt,
685 struct segmented_address addr,
686 unsigned size, bool write,
687 ulong *linear)
688{
689 return __linearize(ctxt, addr, size, write, false, linear);
690}
691
692
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200693static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
694 struct segmented_address addr,
695 void *data,
696 unsigned size)
697{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200698 int rc;
699 ulong linear;
700
Avi Kivity83b87952011-04-03 11:31:19 +0300701 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200702 if (rc != X86EMUL_CONTINUE)
703 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300704 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200705}
706
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900707/*
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200708 * Prefetch the remaining bytes of the instruction without crossing page
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900709 * boundary if they are not in fetch_cache yet.
710 */
Paolo Bonzini9506d572014-05-06 13:05:25 +0200711static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
Avi Kivity62266862007-11-20 13:15:52 +0200712{
Avi Kivity62266862007-11-20 13:15:52 +0200713 int rc;
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200714 unsigned size;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200715 unsigned long linear;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200716 int cur_size = ctxt->fetch.end - ctxt->fetch.data;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200717 struct segmented_address addr = { .seg = VCPU_SREG_CS,
Paolo Bonzini17052f12014-05-06 16:33:01 +0200718 .ea = ctxt->eip + cur_size };
719
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200720 size = 15UL ^ cur_size;
721 rc = __linearize(ctxt, addr, size, false, true, &linear);
722 if (unlikely(rc != X86EMUL_CONTINUE))
723 return rc;
724
725 size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear));
Paolo Bonzini5cfc7e02014-05-06 13:05:25 +0200726
727 /*
728 * One instruction can only straddle two pages,
729 * and one has been loaded at the beginning of
730 * x86_decode_insn. So, if not enough bytes
731 * still, we must have hit the 15-byte boundary.
732 */
733 if (unlikely(size < op_size))
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200734 return X86EMUL_UNHANDLEABLE;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200735 rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end,
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200736 size, &ctxt->exception);
737 if (unlikely(rc != X86EMUL_CONTINUE))
738 return rc;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200739 ctxt->fetch.end += size;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900740 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200741}
742
Paolo Bonzini9506d572014-05-06 13:05:25 +0200743static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt,
744 unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200745{
Paolo Bonzini17052f12014-05-06 16:33:01 +0200746 if (unlikely(ctxt->fetch.end - ctxt->fetch.ptr < size))
Paolo Bonzini9506d572014-05-06 13:05:25 +0200747 return __do_insn_fetch_bytes(ctxt, size);
748 else
749 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200750}
751
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900752/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900753#define insn_fetch(_type, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200754({ _type _x; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200755 \
756 rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900757 if (rc != X86EMUL_CONTINUE) \
758 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200759 ctxt->_eip += sizeof(_type); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200760 _x = *(_type __aligned(1) *) ctxt->fetch.ptr; \
761 ctxt->fetch.ptr += sizeof(_type); \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200762 _x; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900763})
764
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900765#define insn_fetch_arr(_arr, _size, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200766({ \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200767 rc = do_insn_fetch_bytes(_ctxt, _size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900768 if (rc != X86EMUL_CONTINUE) \
769 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200770 ctxt->_eip += (_size); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200771 memcpy(_arr, ctxt->fetch.ptr, _size); \
772 ctxt->fetch.ptr += (_size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900773})
774
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000775/*
776 * Given the 'reg' portion of a ModRM byte, and a register block, return a
777 * pointer into the block that addresses the relevant register.
778 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
779 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300780static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200781 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800782{
783 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200784 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800785
Avi Kivity6aa8b732006-12-10 02:21:36 -0800786 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300787 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
788 else
789 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790 return p;
791}
792
793static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200794 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800795 u16 *size, unsigned long *address, int op_bytes)
796{
797 int rc;
798
799 if (op_bytes == 2)
800 op_bytes = 3;
801 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200802 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900803 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800804 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200805 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200806 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800807 return rc;
808}
809
Avi Kivity34b77652013-01-19 19:51:56 +0200810FASTOP2(add);
811FASTOP2(or);
812FASTOP2(adc);
813FASTOP2(sbb);
814FASTOP2(and);
815FASTOP2(sub);
816FASTOP2(xor);
817FASTOP2(cmp);
818FASTOP2(test);
819
Avi Kivityb9fa4092013-02-09 11:31:48 +0200820FASTOP1SRC2(mul, mul_ex);
821FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200822FASTOP1SRC2EX(div, div_ex);
823FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +0200824
Avi Kivity34b77652013-01-19 19:51:56 +0200825FASTOP3WCL(shld);
826FASTOP3WCL(shrd);
827
828FASTOP2W(imul);
829
830FASTOP1(not);
831FASTOP1(neg);
832FASTOP1(inc);
833FASTOP1(dec);
834
835FASTOP2CL(rol);
836FASTOP2CL(ror);
837FASTOP2CL(rcl);
838FASTOP2CL(rcr);
839FASTOP2CL(shl);
840FASTOP2CL(shr);
841FASTOP2CL(sar);
842
843FASTOP2W(bsf);
844FASTOP2W(bsr);
845FASTOP2W(bt);
846FASTOP2W(bts);
847FASTOP2W(btr);
848FASTOP2W(btc);
849
Avi Kivitye47a5f52013-02-09 11:31:51 +0200850FASTOP2(xadd);
851
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200852static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300853{
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200854 u8 rc;
855 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300856
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200857 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +0200858 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200859 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
860 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300861}
862
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300863static void fetch_register_operand(struct operand *op)
864{
865 switch (op->bytes) {
866 case 1:
867 op->val = *(u8 *)op->addr.reg;
868 break;
869 case 2:
870 op->val = *(u16 *)op->addr.reg;
871 break;
872 case 4:
873 op->val = *(u32 *)op->addr.reg;
874 break;
875 case 8:
876 op->val = *(u64 *)op->addr.reg;
877 break;
878 }
879}
880
Avi Kivity12537912011-03-29 11:41:27 +0200881static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
882{
883 ctxt->ops->get_fpu(ctxt);
884 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200885 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
886 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
887 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
888 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
889 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
890 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
891 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
892 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200893#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200894 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
895 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
896 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
897 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
898 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
899 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
900 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
901 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200902#endif
903 default: BUG();
904 }
905 ctxt->ops->put_fpu(ctxt);
906}
907
908static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
909 int reg)
910{
911 ctxt->ops->get_fpu(ctxt);
912 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200913 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
914 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
915 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
916 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
917 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
918 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
919 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
920 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200921#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200922 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
923 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
924 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
925 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
926 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
927 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
928 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
929 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200930#endif
931 default: BUG();
932 }
933 ctxt->ops->put_fpu(ctxt);
934}
935
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300936static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
937{
938 ctxt->ops->get_fpu(ctxt);
939 switch (reg) {
940 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
941 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
942 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
943 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
944 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
945 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
946 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
947 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
948 default: BUG();
949 }
950 ctxt->ops->put_fpu(ctxt);
951}
952
953static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
954{
955 ctxt->ops->get_fpu(ctxt);
956 switch (reg) {
957 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
958 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
959 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
960 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
961 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
962 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
963 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
964 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
965 default: BUG();
966 }
967 ctxt->ops->put_fpu(ctxt);
968}
969
Gleb Natapov045a2822012-12-20 16:57:43 +0200970static int em_fninit(struct x86_emulate_ctxt *ctxt)
971{
972 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
973 return emulate_nm(ctxt);
974
975 ctxt->ops->get_fpu(ctxt);
976 asm volatile("fninit");
977 ctxt->ops->put_fpu(ctxt);
978 return X86EMUL_CONTINUE;
979}
980
981static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
982{
983 u16 fcw;
984
985 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
986 return emulate_nm(ctxt);
987
988 ctxt->ops->get_fpu(ctxt);
989 asm volatile("fnstcw %0": "+m"(fcw));
990 ctxt->ops->put_fpu(ctxt);
991
992 /* force 2 byte destination */
993 ctxt->dst.bytes = 2;
994 ctxt->dst.val = fcw;
995
996 return X86EMUL_CONTINUE;
997}
998
999static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1000{
1001 u16 fsw;
1002
1003 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1004 return emulate_nm(ctxt);
1005
1006 ctxt->ops->get_fpu(ctxt);
1007 asm volatile("fnstsw %0": "+m"(fsw));
1008 ctxt->ops->put_fpu(ctxt);
1009
1010 /* force 2 byte destination */
1011 ctxt->dst.bytes = 2;
1012 ctxt->dst.val = fsw;
1013
1014 return X86EMUL_CONTINUE;
1015}
1016
Avi Kivity12537912011-03-29 11:41:27 +02001017static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001018 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001019{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001020 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001021
Avi Kivity9dac77f2011-06-01 15:34:25 +03001022 if (!(ctxt->d & ModRM))
1023 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +02001024
Avi Kivity9dac77f2011-06-01 15:34:25 +03001025 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001026 op->type = OP_XMM;
1027 op->bytes = 16;
1028 op->addr.xmm = reg;
1029 read_sse_reg(ctxt, &op->vec_val, reg);
1030 return;
1031 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001032 if (ctxt->d & Mmx) {
1033 reg &= 7;
1034 op->type = OP_MM;
1035 op->bytes = 8;
1036 op->addr.mm = reg;
1037 return;
1038 }
Avi Kivity12537912011-03-29 11:41:27 +02001039
Avi Kivity3c118e22007-10-31 10:27:04 +02001040 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001041 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1042 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1043
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001044 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001045 op->orig_val = op->val;
1046}
1047
Avi Kivitya6e34072012-06-10 17:15:39 +03001048static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1049{
1050 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1051 ctxt->modrm_seg = VCPU_SREG_SS;
1052}
1053
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001054static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001055 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001056{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001057 u8 sib;
Bandan Das02357bd2014-04-16 12:46:11 -04001058 int index_reg, base_reg, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001059 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001060 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001061
Bandan Das02357bd2014-04-16 12:46:11 -04001062 ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */
1063 index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */
1064 base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001065
Bandan Das02357bd2014-04-16 12:46:11 -04001066 ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001067 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
Bandan Das02357bd2014-04-16 12:46:11 -04001068 ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001069 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001070
Nadav Amit9b88ae92014-05-25 23:05:21 +03001071 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001072 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001073 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001074 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001075 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001076 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001077 op->type = OP_XMM;
1078 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001079 op->addr.xmm = ctxt->modrm_rm;
1080 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001081 return rc;
1082 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001083 if (ctxt->d & Mmx) {
1084 op->type = OP_MM;
1085 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001086 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001087 return rc;
1088 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001089 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001090 return rc;
1091 }
1092
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001093 op->type = OP_MEM;
1094
Avi Kivity9dac77f2011-06-01 15:34:25 +03001095 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001096 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1097 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1098 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1099 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001100
1101 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001102 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001103 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001104 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001105 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001106 break;
1107 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001108 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001109 break;
1110 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001111 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001112 break;
1113 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001114 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001115 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001116 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001117 break;
1118 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001119 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001120 break;
1121 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001122 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001123 break;
1124 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001125 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001126 break;
1127 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001128 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001129 break;
1130 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001131 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001132 break;
1133 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001134 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001135 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001136 break;
1137 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001138 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001139 break;
1140 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001141 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1142 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1143 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001144 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001145 } else {
1146 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001147 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001148 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001149 index_reg |= (sib >> 3) & 7;
1150 base_reg |= sib & 7;
1151 scale = sib >> 6;
1152
Avi Kivity9dac77f2011-06-01 15:34:25 +03001153 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001154 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001155 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001156 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001157 adjust_modrm_seg(ctxt, base_reg);
1158 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001159 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001160 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001161 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001162 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001163 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001164 } else {
1165 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001166 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001167 adjust_modrm_seg(ctxt, base_reg);
1168 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001169 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001170 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001171 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001172 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001173 break;
1174 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001175 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001176 break;
1177 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001178 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001179 break;
1180 }
1181 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001182 op->addr.mem.ea = modrm_ea;
Bandan Das41061cd2014-04-16 12:46:14 -04001183 if (ctxt->ad_bytes != 8)
1184 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
1185
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001186done:
1187 return rc;
1188}
1189
1190static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001191 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001192{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001193 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001194
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001195 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001196 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001197 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001198 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001199 break;
1200 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001201 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001202 break;
1203 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001204 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001205 break;
1206 }
1207done:
1208 return rc;
1209}
1210
Avi Kivity9dac77f2011-06-01 15:34:25 +03001211static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001212{
Sheng Yang7129eec2010-09-28 16:33:32 +08001213 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001214
Avi Kivity9dac77f2011-06-01 15:34:25 +03001215 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001216 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001217
Avi Kivity9dac77f2011-06-01 15:34:25 +03001218 if (ctxt->src.bytes == 2)
1219 sv = (s16)ctxt->src.val & (s16)mask;
1220 else if (ctxt->src.bytes == 4)
1221 sv = (s32)ctxt->src.val & (s32)mask;
Nadav Amit7dec5602014-06-15 16:12:57 +03001222 else
1223 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001224
Avi Kivity9dac77f2011-06-01 15:34:25 +03001225 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001226 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001227
1228 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001229 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001230}
1231
Gleb Natapov9de41572010-04-28 19:15:22 +03001232static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001233 unsigned long addr, void *dest, unsigned size)
1234{
1235 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001236 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001237
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001238 if (mc->pos < mc->end)
1239 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001240
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001241 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001242
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001243 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1244 &ctxt->exception);
1245 if (rc != X86EMUL_CONTINUE)
1246 return rc;
1247
1248 mc->end += size;
1249
1250read_cached:
1251 memcpy(dest, mc->data + mc->pos, size);
1252 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001253 return X86EMUL_CONTINUE;
1254}
1255
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001256static int segmented_read(struct x86_emulate_ctxt *ctxt,
1257 struct segmented_address addr,
1258 void *data,
1259 unsigned size)
1260{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001261 int rc;
1262 ulong linear;
1263
Avi Kivity83b87952011-04-03 11:31:19 +03001264 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001265 if (rc != X86EMUL_CONTINUE)
1266 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001267 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001268}
1269
1270static int segmented_write(struct x86_emulate_ctxt *ctxt,
1271 struct segmented_address addr,
1272 const void *data,
1273 unsigned size)
1274{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001275 int rc;
1276 ulong linear;
1277
Avi Kivity83b87952011-04-03 11:31:19 +03001278 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001279 if (rc != X86EMUL_CONTINUE)
1280 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001281 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1282 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001283}
1284
1285static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1286 struct segmented_address addr,
1287 const void *orig_data, const void *data,
1288 unsigned size)
1289{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001290 int rc;
1291 ulong linear;
1292
Avi Kivity83b87952011-04-03 11:31:19 +03001293 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001294 if (rc != X86EMUL_CONTINUE)
1295 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001296 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1297 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001298}
1299
Gleb Natapov7b262e92010-03-18 15:20:27 +02001300static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001301 unsigned int size, unsigned short port,
1302 void *dest)
1303{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001304 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001305
1306 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001307 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001308 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001309 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001310 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001311 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1312 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Gleb Natapov7b262e92010-03-18 15:20:27 +02001313 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1314 count);
1315 if (n == 0)
1316 n = 1;
1317 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001318 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001319 return 0;
1320 rc->end = n * size;
1321 }
1322
Nadav Amite6e39f02014-04-18 03:35:10 +03001323 if (ctxt->rep_prefix && (ctxt->d & String) &&
1324 !(ctxt->eflags & EFLG_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001325 ctxt->dst.data = rc->data + rc->pos;
1326 ctxt->dst.type = OP_MEM_STR;
1327 ctxt->dst.count = (rc->end - rc->pos) / size;
1328 rc->pos = rc->end;
1329 } else {
1330 memcpy(dest, rc->data + rc->pos, size);
1331 rc->pos += size;
1332 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001333 return 1;
1334}
1335
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001336static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1337 u16 index, struct desc_struct *desc)
1338{
1339 struct desc_ptr dt;
1340 ulong addr;
1341
1342 ctxt->ops->get_idt(ctxt, &dt);
1343
1344 if (dt.size < index * 8 + 7)
1345 return emulate_gp(ctxt, index << 3 | 0x2);
1346
1347 addr = dt.address + index * 8;
1348 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1349 &ctxt->exception);
1350}
1351
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001352static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001353 u16 selector, struct desc_ptr *dt)
1354{
Mathias Krause0225fb52012-08-30 01:30:16 +02001355 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001356 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001357
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001358 if (selector & 1 << 2) {
1359 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001360 u16 sel;
1361
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001362 memset (dt, 0, sizeof *dt);
Nadav Amit2eedcac2014-06-02 18:34:05 +03001363 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1364 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001365 return;
1366
1367 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001368 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001369 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001370 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001371}
1372
1373/* allowed just for 8 bytes segments */
1374static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001375 u16 selector, struct desc_struct *desc,
1376 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001377{
1378 struct desc_ptr dt;
1379 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001380 ulong addr;
1381
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001382 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001383
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001384 if (dt.size < index * 8 + 7)
1385 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001386
Avi Kivitye9194642012-06-13 16:29:39 +03001387 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001388 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1389 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001390}
1391
1392/* allowed just for 8 bytes segments */
1393static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001394 u16 selector, struct desc_struct *desc)
1395{
1396 struct desc_ptr dt;
1397 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001398 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001399
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001400 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001401
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001402 if (dt.size < index * 8 + 7)
1403 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001404
1405 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001406 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1407 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001408}
1409
Gleb Natapov5601d052011-03-07 14:55:06 +02001410/* Does not support long mode */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001411static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Paolo Bonzini5045b462014-05-15 18:09:29 +02001412 u16 selector, int seg, u8 cpl, bool in_task_switch)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001413{
Avi Kivity869be992012-06-13 16:30:53 +03001414 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001415 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001416 unsigned err_vec = GP_VECTOR;
1417 u32 err_code = 0;
1418 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001419 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001420 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001421 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001422 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001423
1424 memset(&seg_desc, 0, sizeof seg_desc);
1425
Kevin Wolff8da94e2013-04-11 14:06:03 +02001426 if (ctxt->mode == X86EMUL_MODE_REAL) {
1427 /* set real mode segment descriptor (keep limit etc. for
1428 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001429 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001430 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001431 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001432 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1433 /* VM86 needs a clean new segment descriptor */
1434 set_desc_base(&seg_desc, selector << 4);
1435 set_desc_limit(&seg_desc, 0xffff);
1436 seg_desc.type = 3;
1437 seg_desc.p = 1;
1438 seg_desc.s = 1;
1439 seg_desc.dpl = 3;
1440 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001441 }
1442
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001443 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001444
1445 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1446 if ((seg == VCPU_SREG_CS
1447 || (seg == VCPU_SREG_SS
1448 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1449 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001450 && null_selector)
1451 goto exception;
1452
1453 /* TR should be in GDT only */
1454 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1455 goto exception;
1456
1457 if (null_selector) /* for NULL selector skip all following checks */
1458 goto load;
1459
Avi Kivitye9194642012-06-13 16:29:39 +03001460 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001461 if (ret != X86EMUL_CONTINUE)
1462 return ret;
1463
1464 err_code = selector & 0xfffc;
1465 err_vec = GP_VECTOR;
1466
Guo Chaofc058682012-06-28 15:19:51 +08001467 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001468 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1469 goto exception;
1470
1471 if (!seg_desc.p) {
1472 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1473 goto exception;
1474 }
1475
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001476 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001477
1478 switch (seg) {
1479 case VCPU_SREG_SS:
1480 /*
1481 * segment is not a writable data segment or segment
1482 * selector's RPL != CPL or segment selector's RPL != CPL
1483 */
1484 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1485 goto exception;
1486 break;
1487 case VCPU_SREG_CS:
Paolo Bonzini5045b462014-05-15 18:09:29 +02001488 if (in_task_switch && rpl != dpl)
1489 goto exception;
1490
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001491 if (!(seg_desc.type & 8))
1492 goto exception;
1493
1494 if (seg_desc.type & 4) {
1495 /* conforming */
1496 if (dpl > cpl)
1497 goto exception;
1498 } else {
1499 /* nonconforming */
1500 if (rpl > cpl || dpl != cpl)
1501 goto exception;
1502 }
1503 /* CS(RPL) <- CPL */
1504 selector = (selector & 0xfffc) | cpl;
1505 break;
1506 case VCPU_SREG_TR:
1507 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1508 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001509 old_desc = seg_desc;
1510 seg_desc.type |= 2; /* busy */
1511 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1512 sizeof(seg_desc), &ctxt->exception);
1513 if (ret != X86EMUL_CONTINUE)
1514 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001515 break;
1516 case VCPU_SREG_LDTR:
1517 if (seg_desc.s || seg_desc.type != 2)
1518 goto exception;
1519 break;
1520 default: /* DS, ES, FS, or GS */
1521 /*
1522 * segment is not a data or readable code segment or
1523 * ((segment is a data or nonconforming code segment)
1524 * and (both RPL and CPL > DPL))
1525 */
1526 if ((seg_desc.type & 0xa) == 0x8 ||
1527 (((seg_desc.type & 0xc) != 0xc) &&
1528 (rpl > dpl && cpl > dpl)))
1529 goto exception;
1530 break;
1531 }
1532
1533 if (seg_desc.s) {
1534 /* mark segment as accessed */
1535 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001536 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001537 if (ret != X86EMUL_CONTINUE)
1538 return ret;
Nadav Amite37a75a2014-06-02 18:34:04 +03001539 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
1540 ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3,
1541 sizeof(base3), &ctxt->exception);
1542 if (ret != X86EMUL_CONTINUE)
1543 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001544 }
1545load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001546 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001547 return X86EMUL_CONTINUE;
1548exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001549 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001550 return X86EMUL_PROPAGATE_FAULT;
1551}
1552
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001553static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1554 u16 selector, int seg)
1555{
1556 u8 cpl = ctxt->ops->cpl(ctxt);
Paolo Bonzini5045b462014-05-15 18:09:29 +02001557 return __load_segment_descriptor(ctxt, selector, seg, cpl, false);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001558}
1559
Wei Yongjun31be40b2010-08-17 09:17:30 +08001560static void write_register_operand(struct operand *op)
1561{
1562 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1563 switch (op->bytes) {
1564 case 1:
1565 *(u8 *)op->addr.reg = (u8)op->val;
1566 break;
1567 case 2:
1568 *(u16 *)op->addr.reg = (u16)op->val;
1569 break;
1570 case 4:
1571 *op->addr.reg = (u32)op->val;
1572 break; /* 64b: zero-extend */
1573 case 8:
1574 *op->addr.reg = op->val;
1575 break;
1576 }
1577}
1578
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001579static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001580{
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001581 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001582 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001583 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001584 break;
1585 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001586 if (ctxt->lock_prefix)
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001587 return segmented_cmpxchg(ctxt,
1588 op->addr.mem,
1589 &op->orig_val,
1590 &op->val,
1591 op->bytes);
1592 else
1593 return segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001594 op->addr.mem,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001595 &op->val,
1596 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001597 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001598 case OP_MEM_STR:
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001599 return segmented_write(ctxt,
1600 op->addr.mem,
1601 op->data,
1602 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001603 break;
Avi Kivity12537912011-03-29 11:41:27 +02001604 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001605 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001606 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001607 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001608 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001609 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001610 case OP_NONE:
1611 /* no writeback */
1612 break;
1613 default:
1614 break;
1615 }
1616 return X86EMUL_CONTINUE;
1617}
1618
Avi Kivity51ddff52012-06-12 20:19:40 +03001619static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001620{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001621 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001622
Avi Kivity5ad105e2012-08-19 14:34:31 +03001623 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001624 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001625 addr.seg = VCPU_SREG_SS;
1626
Avi Kivity51ddff52012-06-12 20:19:40 +03001627 return segmented_write(ctxt, addr, data, bytes);
1628}
1629
1630static int em_push(struct x86_emulate_ctxt *ctxt)
1631{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001632 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001633 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001634 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001635}
1636
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001637static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001638 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001639{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001640 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001641 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001642
Avi Kivitydd856ef2012-08-27 23:46:17 +03001643 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001644 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001645 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001646 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001647 return rc;
1648
Avi Kivity5ad105e2012-08-19 14:34:31 +03001649 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001650 return rc;
1651}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001652
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001653static int em_pop(struct x86_emulate_ctxt *ctxt)
1654{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001655 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001656}
1657
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001658static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001659 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001660{
1661 int rc;
1662 unsigned long val, change_mask;
1663 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001664 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001665
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001666 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001667 if (rc != X86EMUL_CONTINUE)
1668 return rc;
1669
1670 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1671 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1672
1673 switch(ctxt->mode) {
1674 case X86EMUL_MODE_PROT64:
1675 case X86EMUL_MODE_PROT32:
1676 case X86EMUL_MODE_PROT16:
1677 if (cpl == 0)
1678 change_mask |= EFLG_IOPL;
1679 if (cpl <= iopl)
1680 change_mask |= EFLG_IF;
1681 break;
1682 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001683 if (iopl < 3)
1684 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001685 change_mask |= EFLG_IF;
1686 break;
1687 default: /* real mode */
1688 change_mask |= (EFLG_IOPL | EFLG_IF);
1689 break;
1690 }
1691
1692 *(unsigned long *)dest =
1693 (ctxt->eflags & ~change_mask) | (val & change_mask);
1694
1695 return rc;
1696}
1697
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001698static int em_popf(struct x86_emulate_ctxt *ctxt)
1699{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001700 ctxt->dst.type = OP_REG;
1701 ctxt->dst.addr.reg = &ctxt->eflags;
1702 ctxt->dst.bytes = ctxt->op_bytes;
1703 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001704}
1705
Avi Kivity612e89f2012-06-12 20:03:23 +03001706static int em_enter(struct x86_emulate_ctxt *ctxt)
1707{
1708 int rc;
1709 unsigned frame_size = ctxt->src.val;
1710 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001711 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001712
1713 if (nesting_level)
1714 return X86EMUL_UNHANDLEABLE;
1715
Avi Kivitydd856ef2012-08-27 23:46:17 +03001716 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1717 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001718 if (rc != X86EMUL_CONTINUE)
1719 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001720 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001721 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001722 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1723 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001724 stack_mask(ctxt));
1725 return X86EMUL_CONTINUE;
1726}
1727
Avi Kivityf47cfa32012-06-07 17:49:24 +03001728static int em_leave(struct x86_emulate_ctxt *ctxt)
1729{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001730 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001731 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001732 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001733}
1734
Avi Kivity1cd196e2011-09-13 10:45:51 +03001735static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001736{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001737 int seg = ctxt->src2.val;
1738
Avi Kivity9dac77f2011-06-01 15:34:25 +03001739 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001740
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001741 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001742}
1743
Avi Kivity1cd196e2011-09-13 10:45:51 +03001744static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001745{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001746 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001747 unsigned long selector;
1748 int rc;
1749
Avi Kivity9dac77f2011-06-01 15:34:25 +03001750 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001751 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001752 return rc;
1753
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001754 if (ctxt->modrm_reg == VCPU_SREG_SS)
1755 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
1756
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001757 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001758 return rc;
1759}
1760
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001761static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001762{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001763 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001764 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001765 int reg = VCPU_REGS_RAX;
1766
1767 while (reg <= VCPU_REGS_RDI) {
1768 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001769 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001770
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001771 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001772 if (rc != X86EMUL_CONTINUE)
1773 return rc;
1774
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001775 ++reg;
1776 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001777
Wei Yongjunc37eda12010-06-15 09:03:33 +08001778 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001779}
1780
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001781static int em_pushf(struct x86_emulate_ctxt *ctxt)
1782{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001783 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001784 return em_push(ctxt);
1785}
1786
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001787static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001788{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001789 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001790 int reg = VCPU_REGS_RDI;
1791
1792 while (reg >= VCPU_REGS_RAX) {
1793 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001794 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001795 --reg;
1796 }
1797
Avi Kivitydd856ef2012-08-27 23:46:17 +03001798 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001799 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001800 break;
1801 --reg;
1802 }
1803 return rc;
1804}
1805
Avi Kivitydd856ef2012-08-27 23:46:17 +03001806static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001807{
Mathias Krause0225fb52012-08-30 01:30:16 +02001808 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001809 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001810 struct desc_ptr dt;
1811 gva_t cs_addr;
1812 gva_t eip_addr;
1813 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001814
1815 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001816 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001817 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001818 if (rc != X86EMUL_CONTINUE)
1819 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001820
1821 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1822
Avi Kivity9dac77f2011-06-01 15:34:25 +03001823 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001824 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001825 if (rc != X86EMUL_CONTINUE)
1826 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001827
Avi Kivity9dac77f2011-06-01 15:34:25 +03001828 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001829 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001830 if (rc != X86EMUL_CONTINUE)
1831 return rc;
1832
Avi Kivity4bff1e862011-04-20 13:37:53 +03001833 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001834
1835 eip_addr = dt.address + (irq << 2);
1836 cs_addr = dt.address + (irq << 2) + 2;
1837
Avi Kivity0f65dd72011-04-20 13:37:53 +03001838 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001839 if (rc != X86EMUL_CONTINUE)
1840 return rc;
1841
Avi Kivity0f65dd72011-04-20 13:37:53 +03001842 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001843 if (rc != X86EMUL_CONTINUE)
1844 return rc;
1845
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001846 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001847 if (rc != X86EMUL_CONTINUE)
1848 return rc;
1849
Avi Kivity9dac77f2011-06-01 15:34:25 +03001850 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001851
1852 return rc;
1853}
1854
Avi Kivitydd856ef2012-08-27 23:46:17 +03001855int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1856{
1857 int rc;
1858
1859 invalidate_registers(ctxt);
1860 rc = __emulate_int_real(ctxt, irq);
1861 if (rc == X86EMUL_CONTINUE)
1862 writeback_registers(ctxt);
1863 return rc;
1864}
1865
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001866static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001867{
1868 switch(ctxt->mode) {
1869 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001870 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001871 case X86EMUL_MODE_VM86:
1872 case X86EMUL_MODE_PROT16:
1873 case X86EMUL_MODE_PROT32:
1874 case X86EMUL_MODE_PROT64:
1875 default:
1876 /* Protected mode interrupts unimplemented yet */
1877 return X86EMUL_UNHANDLEABLE;
1878 }
1879}
1880
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001881static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001882{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001883 int rc = X86EMUL_CONTINUE;
1884 unsigned long temp_eip = 0;
1885 unsigned long temp_eflags = 0;
1886 unsigned long cs = 0;
1887 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1888 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1889 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1890 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1891
1892 /* TODO: Add stack limit check */
1893
Avi Kivity9dac77f2011-06-01 15:34:25 +03001894 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001895
1896 if (rc != X86EMUL_CONTINUE)
1897 return rc;
1898
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001899 if (temp_eip & ~0xffff)
1900 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001901
Avi Kivity9dac77f2011-06-01 15:34:25 +03001902 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001903
1904 if (rc != X86EMUL_CONTINUE)
1905 return rc;
1906
Avi Kivity9dac77f2011-06-01 15:34:25 +03001907 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001908
1909 if (rc != X86EMUL_CONTINUE)
1910 return rc;
1911
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001912 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001913
1914 if (rc != X86EMUL_CONTINUE)
1915 return rc;
1916
Avi Kivity9dac77f2011-06-01 15:34:25 +03001917 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001918
1919
Avi Kivity9dac77f2011-06-01 15:34:25 +03001920 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001921 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001922 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001923 ctxt->eflags &= ~0xffff;
1924 ctxt->eflags |= temp_eflags;
1925 }
1926
1927 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1928 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1929
1930 return rc;
1931}
1932
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001933static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001934{
1935 switch(ctxt->mode) {
1936 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001937 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001938 case X86EMUL_MODE_VM86:
1939 case X86EMUL_MODE_PROT16:
1940 case X86EMUL_MODE_PROT32:
1941 case X86EMUL_MODE_PROT64:
1942 default:
1943 /* iret from protected mode unimplemented yet */
1944 return X86EMUL_UNHANDLEABLE;
1945 }
1946}
1947
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001948static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1949{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001950 int rc;
1951 unsigned short sel;
1952
Avi Kivity9dac77f2011-06-01 15:34:25 +03001953 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001954
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001955 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001956 if (rc != X86EMUL_CONTINUE)
1957 return rc;
1958
Avi Kivity9dac77f2011-06-01 15:34:25 +03001959 ctxt->_eip = 0;
1960 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001961 return X86EMUL_CONTINUE;
1962}
1963
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001964static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001965{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001966 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001967
Avi Kivity9dac77f2011-06-01 15:34:25 +03001968 switch (ctxt->modrm_reg) {
Mohammed Gamald19292e2008-09-08 21:47:19 +03001969 case 2: /* call near abs */ {
1970 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001971 old_eip = ctxt->_eip;
1972 ctxt->_eip = ctxt->src.val;
1973 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001974 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001975 break;
1976 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001977 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001978 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001979 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001980 case 5: /* jmp far */
1981 rc = em_jmp_far(ctxt);
1982 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001983 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001984 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001985 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001986 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001987 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001988}
1989
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09001990static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001991{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001992 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001993
Nadav Amitaaa05f22014-06-02 18:34:10 +03001994 if (ctxt->dst.bytes == 16)
1995 return X86EMUL_UNHANDLEABLE;
1996
Avi Kivitydd856ef2012-08-27 23:46:17 +03001997 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
1998 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
1999 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2000 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002001 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002002 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002003 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2004 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002005
Laurent Vivier05f086f2007-09-24 11:10:55 +02002006 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002007 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002008 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002009}
2010
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002011static int em_ret(struct x86_emulate_ctxt *ctxt)
2012{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002013 ctxt->dst.type = OP_REG;
2014 ctxt->dst.addr.reg = &ctxt->_eip;
2015 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002016 return em_pop(ctxt);
2017}
2018
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002019static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002020{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002021 int rc;
2022 unsigned long cs;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002023 int cpl = ctxt->ops->cpl(ctxt);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002024
Avi Kivity9dac77f2011-06-01 15:34:25 +03002025 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002026 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002027 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002028 if (ctxt->op_bytes == 4)
2029 ctxt->_eip = (u32)ctxt->_eip;
2030 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002031 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002032 return rc;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002033 /* Outer-privilege level return is not implemented */
2034 if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2035 return X86EMUL_UNHANDLEABLE;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002036 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002037 return rc;
2038}
2039
Bruce Rogers32611072013-09-09 09:40:20 -06002040static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2041{
2042 int rc;
2043
2044 rc = em_ret_far(ctxt);
2045 if (rc != X86EMUL_CONTINUE)
2046 return rc;
2047 rsp_increment(ctxt, ctxt->src.val);
2048 return X86EMUL_CONTINUE;
2049}
2050
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002051static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2052{
2053 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002054 ctxt->dst.orig_val = ctxt->dst.val;
2055 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002056 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002057 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002058 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002059
2060 if (ctxt->eflags & EFLG_ZF) {
2061 /* Success: write back to memory. */
2062 ctxt->dst.val = ctxt->src.orig_val;
2063 } else {
2064 /* Failure: write the value we saw to EAX. */
2065 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002066 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Nadav Amit37c564f2014-06-02 18:34:07 +03002067 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002068 }
2069 return X86EMUL_CONTINUE;
2070}
2071
Avi Kivityd4b43252011-09-13 10:45:50 +03002072static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002073{
Avi Kivityd4b43252011-09-13 10:45:50 +03002074 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002075 unsigned short sel;
2076 int rc;
2077
Avi Kivity9dac77f2011-06-01 15:34:25 +03002078 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002079
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002080 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002081 if (rc != X86EMUL_CONTINUE)
2082 return rc;
2083
Avi Kivity9dac77f2011-06-01 15:34:25 +03002084 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002085 return rc;
2086}
2087
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002088static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002089setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002090 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002091{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002092 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002093 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002094 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002095 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002096 cs->type = 0x0b; /* Read, Execute, Accessed */
2097 cs->s = 1;
2098 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002099 cs->p = 1;
2100 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002101 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002102
Gleb Natapov79168fd2010-04-28 19:15:30 +03002103 set_desc_base(ss, 0); /* flat segment */
2104 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002105 ss->g = 1; /* 4kb granularity */
2106 ss->s = 1;
2107 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002108 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002109 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002110 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002111 ss->l = 0;
2112 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002113}
2114
Avi Kivity1a18a692012-02-01 12:23:21 +02002115static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2116{
2117 u32 eax, ebx, ecx, edx;
2118
2119 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002120 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2121 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002122 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2123 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2124}
2125
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002126static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2127{
Mathias Krause0225fb52012-08-30 01:30:16 +02002128 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002129 u32 eax, ebx, ecx, edx;
2130
2131 /*
2132 * syscall should always be enabled in longmode - so only become
2133 * vendor specific (cpuid) if other modes are active...
2134 */
2135 if (ctxt->mode == X86EMUL_MODE_PROT64)
2136 return true;
2137
2138 eax = 0x00000000;
2139 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002140 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2141 /*
2142 * Intel ("GenuineIntel")
2143 * remark: Intel CPUs only support "syscall" in 64bit
2144 * longmode. Also an 64bit guest with a
2145 * 32bit compat-app running will #UD !! While this
2146 * behaviour can be fixed (by emulating) into AMD
2147 * response - CPUs of AMD can't behave like Intel.
2148 */
2149 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2150 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2151 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2152 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002153
Avi Kivity0017f932012-06-07 14:10:16 +03002154 /* AMD ("AuthenticAMD") */
2155 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2156 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2157 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2158 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002159
Avi Kivity0017f932012-06-07 14:10:16 +03002160 /* AMD ("AMDisbetter!") */
2161 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2162 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2163 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2164 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002165
2166 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2167 return false;
2168}
2169
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002170static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002171{
Mathias Krause0225fb52012-08-30 01:30:16 +02002172 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002173 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002174 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002175 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002176 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002177
2178 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002179 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002180 ctxt->mode == X86EMUL_MODE_VM86)
2181 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002182
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002183 if (!(em_syscall_is_enabled(ctxt)))
2184 return emulate_ud(ctxt);
2185
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002186 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002187 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002188
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002189 if (!(efer & EFER_SCE))
2190 return emulate_ud(ctxt);
2191
Avi Kivity717746e2011-04-20 13:37:53 +03002192 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002193 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002194 cs_sel = (u16)(msr_data & 0xfffc);
2195 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002196
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002197 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002198 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002199 cs.l = 1;
2200 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002201 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2202 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002203
Avi Kivitydd856ef2012-08-27 23:46:17 +03002204 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002205 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002206#ifdef CONFIG_X86_64
Avi Kivitydd856ef2012-08-27 23:46:17 +03002207 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002208
Avi Kivity717746e2011-04-20 13:37:53 +03002209 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002210 ctxt->mode == X86EMUL_MODE_PROT64 ?
2211 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002212 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002213
Avi Kivity717746e2011-04-20 13:37:53 +03002214 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002215 ctxt->eflags &= ~(msr_data | EFLG_RF);
2216#endif
2217 } else {
2218 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002219 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002220 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002221
2222 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2223 }
2224
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002225 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002226}
2227
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002228static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002229{
Mathias Krause0225fb52012-08-30 01:30:16 +02002230 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002231 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002232 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002233 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002234 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002235
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002236 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002237 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002238 if (ctxt->mode == X86EMUL_MODE_REAL)
2239 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002240
Avi Kivity1a18a692012-02-01 12:23:21 +02002241 /*
2242 * Not recognized on AMD in compat mode (but is recognized in legacy
2243 * mode).
2244 */
2245 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2246 && !vendor_intel(ctxt))
2247 return emulate_ud(ctxt);
2248
Andre Przywara8c604352009-06-18 12:56:01 +02002249 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2250 * Therefore, we inject an #UD.
2251 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002252 if (ctxt->mode == X86EMUL_MODE_PROT64)
2253 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002254
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002255 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002256
Avi Kivity717746e2011-04-20 13:37:53 +03002257 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002258 switch (ctxt->mode) {
2259 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002260 if ((msr_data & 0xfffc) == 0x0)
2261 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002262 break;
2263 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002264 if (msr_data == 0x0)
2265 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002266 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002267 default:
2268 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002269 }
2270
2271 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002272 cs_sel = (u16)msr_data;
2273 cs_sel &= ~SELECTOR_RPL_MASK;
2274 ss_sel = cs_sel + 8;
2275 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002276 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002277 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002278 cs.l = 1;
2279 }
2280
Avi Kivity1aa36612011-04-27 13:20:30 +03002281 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2282 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002283
Avi Kivity717746e2011-04-20 13:37:53 +03002284 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002285 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002286
Avi Kivity717746e2011-04-20 13:37:53 +03002287 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002288 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002289
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002290 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002291}
2292
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002293static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002294{
Mathias Krause0225fb52012-08-30 01:30:16 +02002295 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002296 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002297 u64 msr_data;
2298 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002299 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002300
Gleb Natapova0044752010-02-10 14:21:31 +02002301 /* inject #GP if in real mode or Virtual 8086 mode */
2302 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002303 ctxt->mode == X86EMUL_MODE_VM86)
2304 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002305
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002306 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002307
Avi Kivity9dac77f2011-06-01 15:34:25 +03002308 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002309 usermode = X86EMUL_MODE_PROT64;
2310 else
2311 usermode = X86EMUL_MODE_PROT32;
2312
2313 cs.dpl = 3;
2314 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002315 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002316 switch (usermode) {
2317 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002318 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002319 if ((msr_data & 0xfffc) == 0x0)
2320 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002321 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002322 break;
2323 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002324 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002325 if (msr_data == 0x0)
2326 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002327 ss_sel = cs_sel + 8;
2328 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002329 cs.l = 1;
2330 break;
2331 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002332 cs_sel |= SELECTOR_RPL_MASK;
2333 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002334
Avi Kivity1aa36612011-04-27 13:20:30 +03002335 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2336 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002337
Avi Kivitydd856ef2012-08-27 23:46:17 +03002338 ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX);
2339 *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX);
Andre Przywara4668f052009-06-18 12:56:02 +02002340
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002341 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002342}
2343
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002344static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002345{
2346 int iopl;
2347 if (ctxt->mode == X86EMUL_MODE_REAL)
2348 return false;
2349 if (ctxt->mode == X86EMUL_MODE_VM86)
2350 return true;
2351 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002352 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002353}
2354
2355static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002356 u16 port, u16 len)
2357{
Mathias Krause0225fb52012-08-30 01:30:16 +02002358 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002359 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002360 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002361 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002362 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002363 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002364 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002365
Avi Kivity1aa36612011-04-27 13:20:30 +03002366 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002367 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002368 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002369 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002370 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002371 base = get_desc_base(&tr_seg);
2372#ifdef CONFIG_X86_64
2373 base |= ((u64)base3) << 32;
2374#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002375 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002376 if (r != X86EMUL_CONTINUE)
2377 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002378 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002379 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002380 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002381 if (r != X86EMUL_CONTINUE)
2382 return false;
2383 if ((perm >> bit_idx) & mask)
2384 return false;
2385 return true;
2386}
2387
2388static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002389 u16 port, u16 len)
2390{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002391 if (ctxt->perm_ok)
2392 return true;
2393
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002394 if (emulator_bad_iopl(ctxt))
2395 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002396 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002397
2398 ctxt->perm_ok = true;
2399
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002400 return true;
2401}
2402
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002403static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002404 struct tss_segment_16 *tss)
2405{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002406 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002407 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002408 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2409 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2410 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2411 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2412 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2413 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2414 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2415 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002416
Avi Kivity1aa36612011-04-27 13:20:30 +03002417 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2418 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2419 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2420 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2421 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002422}
2423
2424static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002425 struct tss_segment_16 *tss)
2426{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002427 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002428 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002429
Avi Kivity9dac77f2011-06-01 15:34:25 +03002430 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002431 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002432 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2433 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2434 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2435 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2436 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2437 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2438 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2439 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002440
2441 /*
2442 * SDM says that segment selectors are loaded before segment
2443 * descriptors
2444 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002445 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2446 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2447 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2448 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2449 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002450
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002451 cpl = tss->cs & 3;
2452
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002453 /*
Guo Chaofc058682012-06-28 15:19:51 +08002454 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002455 * it is handled in a context of new task
2456 */
Paolo Bonzini5045b462014-05-15 18:09:29 +02002457 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002458 if (ret != X86EMUL_CONTINUE)
2459 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002460 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002461 if (ret != X86EMUL_CONTINUE)
2462 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002463 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002464 if (ret != X86EMUL_CONTINUE)
2465 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002466 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002467 if (ret != X86EMUL_CONTINUE)
2468 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002469 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002470 if (ret != X86EMUL_CONTINUE)
2471 return ret;
2472
2473 return X86EMUL_CONTINUE;
2474}
2475
2476static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002477 u16 tss_selector, u16 old_tss_sel,
2478 ulong old_tss_base, struct desc_struct *new_desc)
2479{
Mathias Krause0225fb52012-08-30 01:30:16 +02002480 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002481 struct tss_segment_16 tss_seg;
2482 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002483 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002484
Avi Kivity0f65dd72011-04-20 13:37:53 +03002485 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002486 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002487 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002488 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002489 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002490
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002491 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002492
Avi Kivity0f65dd72011-04-20 13:37:53 +03002493 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002494 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002495 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002496 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002497 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002498
Avi Kivity0f65dd72011-04-20 13:37:53 +03002499 ret = ops->read_std(ctxt, new_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
2505 if (old_tss_sel != 0xffff) {
2506 tss_seg.prev_task_link = old_tss_sel;
2507
Avi Kivity0f65dd72011-04-20 13:37:53 +03002508 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002509 &tss_seg.prev_task_link,
2510 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002511 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002512 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002513 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002514 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002515 }
2516
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002517 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002518}
2519
2520static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002521 struct tss_segment_32 *tss)
2522{
Nadav Amit5c7411e2014-04-07 18:37:47 +03002523 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002524 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002525 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002526 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2527 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2528 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2529 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2530 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2531 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2532 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2533 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002534
Avi Kivity1aa36612011-04-27 13:20:30 +03002535 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2536 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2537 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2538 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2539 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2540 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002541}
2542
2543static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002544 struct tss_segment_32 *tss)
2545{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002546 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002547 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002548
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002549 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002550 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002551 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002552 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002553
2554 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002555 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2556 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2557 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2558 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2559 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2560 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2561 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2562 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002563
2564 /*
2565 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002566 * descriptors. This is important because CPL checks will
2567 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002568 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002569 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2570 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2571 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2572 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2573 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2574 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2575 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002576
2577 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002578 * If we're switching between Protected Mode and VM86, we need to make
2579 * sure to update the mode before loading the segment descriptors so
2580 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01002581 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002582 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002583 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002584 cpl = 3;
2585 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002586 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002587 cpl = tss->cs & 3;
2588 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01002589
2590 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002591 * Now load segment descriptors. If fault happenes at this stage
2592 * it is handled in a context of new task
2593 */
Paolo Bonzini5045b462014-05-15 18:09:29 +02002594 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002595 if (ret != X86EMUL_CONTINUE)
2596 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002597 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002598 if (ret != X86EMUL_CONTINUE)
2599 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002600 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002601 if (ret != X86EMUL_CONTINUE)
2602 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002603 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002604 if (ret != X86EMUL_CONTINUE)
2605 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002606 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002607 if (ret != X86EMUL_CONTINUE)
2608 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002609 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002610 if (ret != X86EMUL_CONTINUE)
2611 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002612 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002613 if (ret != X86EMUL_CONTINUE)
2614 return ret;
2615
2616 return X86EMUL_CONTINUE;
2617}
2618
2619static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002620 u16 tss_selector, u16 old_tss_sel,
2621 ulong old_tss_base, struct desc_struct *new_desc)
2622{
Mathias Krause0225fb52012-08-30 01:30:16 +02002623 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002624 struct tss_segment_32 tss_seg;
2625 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002626 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03002627 u32 eip_offset = offsetof(struct tss_segment_32, eip);
2628 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002629
Avi Kivity0f65dd72011-04-20 13:37:53 +03002630 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002631 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002632 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002633 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002634 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002635
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002636 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002637
Nadav Amit5c7411e2014-04-07 18:37:47 +03002638 /* Only GP registers and segment selectors are saved */
2639 ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
2640 ldt_sel_offset - eip_offset, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002641 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002642 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002643 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002644
Avi Kivity0f65dd72011-04-20 13:37:53 +03002645 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002646 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002647 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002648 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002649 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002650
2651 if (old_tss_sel != 0xffff) {
2652 tss_seg.prev_task_link = old_tss_sel;
2653
Avi Kivity0f65dd72011-04-20 13:37:53 +03002654 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002655 &tss_seg.prev_task_link,
2656 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002657 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002658 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002659 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002660 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002661 }
2662
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002663 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002664}
2665
2666static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002667 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002668 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002669{
Mathias Krause0225fb52012-08-30 01:30:16 +02002670 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002671 struct desc_struct curr_tss_desc, next_tss_desc;
2672 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002673 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002674 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002675 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002676 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002677 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002678
2679 /* FIXME: old_tss_base == ~0 ? */
2680
Avi Kivitye9194642012-06-13 16:29:39 +03002681 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002682 if (ret != X86EMUL_CONTINUE)
2683 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002684 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002685 if (ret != X86EMUL_CONTINUE)
2686 return ret;
2687
2688 /* FIXME: check that next_tss_desc is tss */
2689
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002690 /*
2691 * Check privileges. The three cases are task switch caused by...
2692 *
2693 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2694 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002695 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002696 */
2697 if (reason == TASK_SWITCH_GATE) {
2698 if (idt_index != -1) {
2699 /* Software interrupts */
2700 struct desc_struct task_gate_desc;
2701 int dpl;
2702
2703 ret = read_interrupt_descriptor(ctxt, idt_index,
2704 &task_gate_desc);
2705 if (ret != X86EMUL_CONTINUE)
2706 return ret;
2707
2708 dpl = task_gate_desc.dpl;
2709 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2710 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2711 }
2712 } else if (reason != TASK_SWITCH_IRET) {
2713 int dpl = next_tss_desc.dpl;
2714 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2715 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002716 }
2717
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002718
Gleb Natapovceffb452010-03-18 15:20:19 +02002719 desc_limit = desc_limit_scaled(&next_tss_desc);
2720 if (!next_tss_desc.p ||
2721 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2722 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002723 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002724 return X86EMUL_PROPAGATE_FAULT;
2725 }
2726
2727 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2728 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002729 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002730 }
2731
2732 if (reason == TASK_SWITCH_IRET)
2733 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2734
2735 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002736 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002737 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2738 old_tss_sel = 0xffff;
2739
2740 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002741 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002742 old_tss_base, &next_tss_desc);
2743 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002744 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002745 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002746 if (ret != X86EMUL_CONTINUE)
2747 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002748
2749 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2750 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2751
2752 if (reason != TASK_SWITCH_IRET) {
2753 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002754 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002755 }
2756
Avi Kivity717746e2011-04-20 13:37:53 +03002757 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002758 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002759
Jan Kiszkae269fb22010-04-14 15:51:09 +02002760 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002761 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2762 ctxt->lock_prefix = 0;
2763 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002764 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002765 }
2766
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002767 return ret;
2768}
2769
2770int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002771 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002772 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002773{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002774 int rc;
2775
Avi Kivitydd856ef2012-08-27 23:46:17 +03002776 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002777 ctxt->_eip = ctxt->eip;
2778 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002779
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002780 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002781 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002782
Avi Kivitydd856ef2012-08-27 23:46:17 +03002783 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002784 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002785 writeback_registers(ctxt);
2786 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002787
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002788 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002789}
2790
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002791static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2792 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002793{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002794 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002795
Avi Kivitydd856ef2012-08-27 23:46:17 +03002796 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2797 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002798}
2799
Avi Kivity7af04fc2010-08-18 14:16:35 +03002800static int em_das(struct x86_emulate_ctxt *ctxt)
2801{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002802 u8 al, old_al;
2803 bool af, cf, old_cf;
2804
2805 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002806 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002807
2808 old_al = al;
2809 old_cf = cf;
2810 cf = false;
2811 af = ctxt->eflags & X86_EFLAGS_AF;
2812 if ((al & 0x0f) > 9 || af) {
2813 al -= 6;
2814 cf = old_cf | (al >= 250);
2815 af = true;
2816 } else {
2817 af = false;
2818 }
2819 if (old_al > 0x99 || old_cf) {
2820 al -= 0x60;
2821 cf = true;
2822 }
2823
Avi Kivity9dac77f2011-06-01 15:34:25 +03002824 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002825 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002826 ctxt->src.type = OP_IMM;
2827 ctxt->src.val = 0;
2828 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02002829 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03002830 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2831 if (cf)
2832 ctxt->eflags |= X86_EFLAGS_CF;
2833 if (af)
2834 ctxt->eflags |= X86_EFLAGS_AF;
2835 return X86EMUL_CONTINUE;
2836}
2837
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02002838static int em_aam(struct x86_emulate_ctxt *ctxt)
2839{
2840 u8 al, ah;
2841
2842 if (ctxt->src.val == 0)
2843 return emulate_de(ctxt);
2844
2845 al = ctxt->dst.val & 0xff;
2846 ah = al / ctxt->src.val;
2847 al %= ctxt->src.val;
2848
2849 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
2850
2851 /* Set PF, ZF, SF */
2852 ctxt->src.type = OP_IMM;
2853 ctxt->src.val = 0;
2854 ctxt->src.bytes = 1;
2855 fastop(ctxt, em_or);
2856
2857 return X86EMUL_CONTINUE;
2858}
2859
Gleb Natapov7f662272012-12-10 11:42:30 +02002860static int em_aad(struct x86_emulate_ctxt *ctxt)
2861{
2862 u8 al = ctxt->dst.val & 0xff;
2863 u8 ah = (ctxt->dst.val >> 8) & 0xff;
2864
2865 al = (al + (ah * ctxt->src.val)) & 0xff;
2866
2867 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
2868
Gleb Natapovf583c292013-02-13 17:50:39 +02002869 /* Set PF, ZF, SF */
2870 ctxt->src.type = OP_IMM;
2871 ctxt->src.val = 0;
2872 ctxt->src.bytes = 1;
2873 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02002874
2875 return X86EMUL_CONTINUE;
2876}
2877
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002878static int em_call(struct x86_emulate_ctxt *ctxt)
2879{
2880 long rel = ctxt->src.val;
2881
2882 ctxt->src.val = (unsigned long)ctxt->_eip;
2883 jmp_rel(ctxt, rel);
2884 return em_push(ctxt);
2885}
2886
Avi Kivity0ef753b2010-08-18 14:51:45 +03002887static int em_call_far(struct x86_emulate_ctxt *ctxt)
2888{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002889 u16 sel, old_cs;
2890 ulong old_eip;
2891 int rc;
2892
Avi Kivity1aa36612011-04-27 13:20:30 +03002893 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002894 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002895
Avi Kivity9dac77f2011-06-01 15:34:25 +03002896 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002897 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002898 return X86EMUL_CONTINUE;
2899
Avi Kivity9dac77f2011-06-01 15:34:25 +03002900 ctxt->_eip = 0;
2901 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002902
Avi Kivity9dac77f2011-06-01 15:34:25 +03002903 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002904 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002905 if (rc != X86EMUL_CONTINUE)
2906 return rc;
2907
Avi Kivity9dac77f2011-06-01 15:34:25 +03002908 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002909 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002910}
2911
Avi Kivity40ece7c2010-08-18 15:12:09 +03002912static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2913{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002914 int rc;
2915
Avi Kivity9dac77f2011-06-01 15:34:25 +03002916 ctxt->dst.type = OP_REG;
2917 ctxt->dst.addr.reg = &ctxt->_eip;
2918 ctxt->dst.bytes = ctxt->op_bytes;
2919 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002920 if (rc != X86EMUL_CONTINUE)
2921 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03002922 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002923 return X86EMUL_CONTINUE;
2924}
2925
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002926static int em_xchg(struct x86_emulate_ctxt *ctxt)
2927{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002928 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002929 ctxt->src.val = ctxt->dst.val;
2930 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002931
2932 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002933 ctxt->dst.val = ctxt->src.orig_val;
2934 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002935 return X86EMUL_CONTINUE;
2936}
2937
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002938static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2939{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002940 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02002941 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002942}
2943
Avi Kivity61429142010-08-19 15:13:00 +03002944static int em_cwd(struct x86_emulate_ctxt *ctxt)
2945{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002946 ctxt->dst.type = OP_REG;
2947 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002948 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002949 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002950
2951 return X86EMUL_CONTINUE;
2952}
2953
Avi Kivity48bb5d32010-08-18 18:54:34 +03002954static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2955{
Avi Kivity48bb5d32010-08-18 18:54:34 +03002956 u64 tsc = 0;
2957
Avi Kivity717746e2011-04-20 13:37:53 +03002958 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002959 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
2960 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03002961 return X86EMUL_CONTINUE;
2962}
2963
Avi Kivity222d21a2011-11-10 14:57:30 +02002964static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
2965{
2966 u64 pmc;
2967
Avi Kivitydd856ef2012-08-27 23:46:17 +03002968 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02002969 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002970 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
2971 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02002972 return X86EMUL_CONTINUE;
2973}
2974
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002975static int em_mov(struct x86_emulate_ctxt *ctxt)
2976{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01002977 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002978 return X86EMUL_CONTINUE;
2979}
2980
Borislav Petkov84cffe42013-10-29 12:54:56 +01002981#define FFL(x) bit(X86_FEATURE_##x)
2982
2983static int em_movbe(struct x86_emulate_ctxt *ctxt)
2984{
2985 u32 ebx, ecx, edx, eax = 1;
2986 u16 tmp;
2987
2988 /*
2989 * Check MOVBE is set in the guest-visible CPUID leaf.
2990 */
2991 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2992 if (!(ecx & FFL(MOVBE)))
2993 return emulate_ud(ctxt);
2994
2995 switch (ctxt->op_bytes) {
2996 case 2:
2997 /*
2998 * From MOVBE definition: "...When the operand size is 16 bits,
2999 * the upper word of the destination register remains unchanged
3000 * ..."
3001 *
3002 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3003 * rules so we have to do the operation almost per hand.
3004 */
3005 tmp = (u16)ctxt->src.val;
3006 ctxt->dst.val &= ~0xffffUL;
3007 ctxt->dst.val |= (unsigned long)swab16(tmp);
3008 break;
3009 case 4:
3010 ctxt->dst.val = swab32((u32)ctxt->src.val);
3011 break;
3012 case 8:
3013 ctxt->dst.val = swab64(ctxt->src.val);
3014 break;
3015 default:
3016 return X86EMUL_PROPAGATE_FAULT;
3017 }
3018 return X86EMUL_CONTINUE;
3019}
3020
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003021static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3022{
3023 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3024 return emulate_gp(ctxt, 0);
3025
3026 /* Disable writeback. */
3027 ctxt->dst.type = OP_NONE;
3028 return X86EMUL_CONTINUE;
3029}
3030
3031static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3032{
3033 unsigned long val;
3034
3035 if (ctxt->mode == X86EMUL_MODE_PROT64)
3036 val = ctxt->src.val & ~0ULL;
3037 else
3038 val = ctxt->src.val & ~0U;
3039
3040 /* #UD condition is already handled. */
3041 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3042 return emulate_gp(ctxt, 0);
3043
3044 /* Disable writeback. */
3045 ctxt->dst.type = OP_NONE;
3046 return X86EMUL_CONTINUE;
3047}
3048
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003049static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3050{
3051 u64 msr_data;
3052
Avi Kivitydd856ef2012-08-27 23:46:17 +03003053 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3054 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3055 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003056 return emulate_gp(ctxt, 0);
3057
3058 return X86EMUL_CONTINUE;
3059}
3060
3061static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3062{
3063 u64 msr_data;
3064
Avi Kivitydd856ef2012-08-27 23:46:17 +03003065 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003066 return emulate_gp(ctxt, 0);
3067
Avi Kivitydd856ef2012-08-27 23:46:17 +03003068 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3069 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003070 return X86EMUL_CONTINUE;
3071}
3072
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003073static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3074{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003075 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003076 return emulate_ud(ctxt);
3077
Avi Kivity9dac77f2011-06-01 15:34:25 +03003078 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003079 return X86EMUL_CONTINUE;
3080}
3081
3082static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3083{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003084 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003085
Avi Kivity9dac77f2011-06-01 15:34:25 +03003086 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003087 return emulate_ud(ctxt);
3088
Avi Kivity9dac77f2011-06-01 15:34:25 +03003089 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003090 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3091
3092 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003093 ctxt->dst.type = OP_NONE;
3094 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003095}
3096
Avi Kivitya14e5792012-06-13 12:28:33 +03003097static int em_lldt(struct x86_emulate_ctxt *ctxt)
3098{
3099 u16 sel = ctxt->src.val;
3100
3101 /* Disable writeback. */
3102 ctxt->dst.type = OP_NONE;
3103 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3104}
3105
Avi Kivity80890002012-06-13 16:33:29 +03003106static int em_ltr(struct x86_emulate_ctxt *ctxt)
3107{
3108 u16 sel = ctxt->src.val;
3109
3110 /* Disable writeback. */
3111 ctxt->dst.type = OP_NONE;
3112 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3113}
3114
Avi Kivity38503912011-03-31 18:48:09 +02003115static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3116{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003117 int rc;
3118 ulong linear;
3119
Avi Kivity9dac77f2011-06-01 15:34:25 +03003120 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003121 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003122 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003123 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003124 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003125 return X86EMUL_CONTINUE;
3126}
3127
Avi Kivity2d04a052011-04-20 15:32:49 +03003128static int em_clts(struct x86_emulate_ctxt *ctxt)
3129{
3130 ulong cr0;
3131
3132 cr0 = ctxt->ops->get_cr(ctxt, 0);
3133 cr0 &= ~X86_CR0_TS;
3134 ctxt->ops->set_cr(ctxt, 0, cr0);
3135 return X86EMUL_CONTINUE;
3136}
3137
Avi Kivity26d05cc2011-04-21 12:07:59 +03003138static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3139{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003140 int rc;
3141
Avi Kivity9dac77f2011-06-01 15:34:25 +03003142 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003143 return X86EMUL_UNHANDLEABLE;
3144
3145 rc = ctxt->ops->fix_hypercall(ctxt);
3146 if (rc != X86EMUL_CONTINUE)
3147 return rc;
3148
3149 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003150 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003151 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003152 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003153 return X86EMUL_CONTINUE;
3154}
3155
Avi Kivity96051572012-06-10 17:21:18 +03003156static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3157 void (*get)(struct x86_emulate_ctxt *ctxt,
3158 struct desc_ptr *ptr))
3159{
3160 struct desc_ptr desc_ptr;
3161
3162 if (ctxt->mode == X86EMUL_MODE_PROT64)
3163 ctxt->op_bytes = 8;
3164 get(ctxt, &desc_ptr);
3165 if (ctxt->op_bytes == 2) {
3166 ctxt->op_bytes = 4;
3167 desc_ptr.address &= 0x00ffffff;
3168 }
3169 /* Disable writeback. */
3170 ctxt->dst.type = OP_NONE;
3171 return segmented_write(ctxt, ctxt->dst.addr.mem,
3172 &desc_ptr, 2 + ctxt->op_bytes);
3173}
3174
3175static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3176{
3177 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3178}
3179
3180static int em_sidt(struct x86_emulate_ctxt *ctxt)
3181{
3182 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3183}
3184
Avi Kivity26d05cc2011-04-21 12:07:59 +03003185static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3186{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003187 struct desc_ptr desc_ptr;
3188 int rc;
3189
Avi Kivity510425f2012-06-07 17:04:36 +03003190 if (ctxt->mode == X86EMUL_MODE_PROT64)
3191 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003192 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003193 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003194 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003195 if (rc != X86EMUL_CONTINUE)
3196 return rc;
3197 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3198 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003199 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003200 return X86EMUL_CONTINUE;
3201}
3202
Avi Kivity5ef39c72011-04-21 12:21:50 +03003203static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003204{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003205 int rc;
3206
Avi Kivity5ef39c72011-04-21 12:21:50 +03003207 rc = ctxt->ops->fix_hypercall(ctxt);
3208
Avi Kivity26d05cc2011-04-21 12:07:59 +03003209 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003210 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003211 return rc;
3212}
3213
3214static int em_lidt(struct x86_emulate_ctxt *ctxt)
3215{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003216 struct desc_ptr desc_ptr;
3217 int rc;
3218
Avi Kivity510425f2012-06-07 17:04:36 +03003219 if (ctxt->mode == X86EMUL_MODE_PROT64)
3220 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003221 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003222 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003223 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003224 if (rc != X86EMUL_CONTINUE)
3225 return rc;
3226 ctxt->ops->set_idt(ctxt, &desc_ptr);
3227 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003228 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003229 return X86EMUL_CONTINUE;
3230}
3231
3232static int em_smsw(struct x86_emulate_ctxt *ctxt)
3233{
Nadav Amit32e94d02014-06-02 18:34:11 +03003234 if (ctxt->dst.type == OP_MEM)
3235 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003236 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003237 return X86EMUL_CONTINUE;
3238}
3239
3240static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3241{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003242 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003243 | (ctxt->src.val & 0x0f));
3244 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003245 return X86EMUL_CONTINUE;
3246}
3247
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003248static int em_loop(struct x86_emulate_ctxt *ctxt)
3249{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003250 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3251 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003252 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3253 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003254
3255 return X86EMUL_CONTINUE;
3256}
3257
3258static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3259{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003260 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003261 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003262
3263 return X86EMUL_CONTINUE;
3264}
3265
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003266static int em_in(struct x86_emulate_ctxt *ctxt)
3267{
3268 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3269 &ctxt->dst.val))
3270 return X86EMUL_IO_NEEDED;
3271
3272 return X86EMUL_CONTINUE;
3273}
3274
3275static int em_out(struct x86_emulate_ctxt *ctxt)
3276{
3277 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3278 &ctxt->src.val, 1);
3279 /* Disable writeback. */
3280 ctxt->dst.type = OP_NONE;
3281 return X86EMUL_CONTINUE;
3282}
3283
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003284static int em_cli(struct x86_emulate_ctxt *ctxt)
3285{
3286 if (emulator_bad_iopl(ctxt))
3287 return emulate_gp(ctxt, 0);
3288
3289 ctxt->eflags &= ~X86_EFLAGS_IF;
3290 return X86EMUL_CONTINUE;
3291}
3292
3293static int em_sti(struct x86_emulate_ctxt *ctxt)
3294{
3295 if (emulator_bad_iopl(ctxt))
3296 return emulate_gp(ctxt, 0);
3297
3298 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3299 ctxt->eflags |= X86_EFLAGS_IF;
3300 return X86EMUL_CONTINUE;
3301}
3302
Avi Kivity6d6eede2012-06-07 14:11:36 +03003303static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3304{
3305 u32 eax, ebx, ecx, edx;
3306
Avi Kivitydd856ef2012-08-27 23:46:17 +03003307 eax = reg_read(ctxt, VCPU_REGS_RAX);
3308 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003309 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003310 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3311 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3312 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3313 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003314 return X86EMUL_CONTINUE;
3315}
3316
Paolo Bonzini98f73632013-10-31 11:19:42 +01003317static int em_sahf(struct x86_emulate_ctxt *ctxt)
3318{
3319 u32 flags;
3320
3321 flags = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF;
3322 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3323
3324 ctxt->eflags &= ~0xffUL;
3325 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3326 return X86EMUL_CONTINUE;
3327}
3328
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003329static int em_lahf(struct x86_emulate_ctxt *ctxt)
3330{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003331 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3332 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003333 return X86EMUL_CONTINUE;
3334}
3335
Avi Kivity92998362012-06-13 12:25:06 +03003336static int em_bswap(struct x86_emulate_ctxt *ctxt)
3337{
3338 switch (ctxt->op_bytes) {
3339#ifdef CONFIG_X86_64
3340 case 8:
3341 asm("bswap %0" : "+r"(ctxt->dst.val));
3342 break;
3343#endif
3344 default:
3345 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3346 break;
3347 }
3348 return X86EMUL_CONTINUE;
3349}
3350
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003351static bool valid_cr(int nr)
3352{
3353 switch (nr) {
3354 case 0:
3355 case 2 ... 4:
3356 case 8:
3357 return true;
3358 default:
3359 return false;
3360 }
3361}
3362
3363static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3364{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003365 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003366 return emulate_ud(ctxt);
3367
3368 return X86EMUL_CONTINUE;
3369}
3370
3371static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3372{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003373 u64 new_val = ctxt->src.val64;
3374 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003375 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003376
3377 static u64 cr_reserved_bits[] = {
3378 0xffffffff00000000ULL,
3379 0, 0, 0, /* CR3 checked later */
3380 CR4_RESERVED_BITS,
3381 0, 0, 0,
3382 CR8_RESERVED_BITS,
3383 };
3384
3385 if (!valid_cr(cr))
3386 return emulate_ud(ctxt);
3387
3388 if (new_val & cr_reserved_bits[cr])
3389 return emulate_gp(ctxt, 0);
3390
3391 switch (cr) {
3392 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003393 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003394 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3395 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3396 return emulate_gp(ctxt, 0);
3397
Avi Kivity717746e2011-04-20 13:37:53 +03003398 cr4 = ctxt->ops->get_cr(ctxt, 4);
3399 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003400
3401 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3402 !(cr4 & X86_CR4_PAE))
3403 return emulate_gp(ctxt, 0);
3404
3405 break;
3406 }
3407 case 3: {
3408 u64 rsvd = 0;
3409
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003410 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3411 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003412 rsvd = CR3_L_MODE_RESERVED_BITS;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003413
3414 if (new_val & rsvd)
3415 return emulate_gp(ctxt, 0);
3416
3417 break;
3418 }
3419 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003420 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003421
3422 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3423 return emulate_gp(ctxt, 0);
3424
3425 break;
3426 }
3427 }
3428
3429 return X86EMUL_CONTINUE;
3430}
3431
Joerg Roedel3b88e412011-04-04 12:39:29 +02003432static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3433{
3434 unsigned long dr7;
3435
Avi Kivity717746e2011-04-20 13:37:53 +03003436 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003437
3438 /* Check if DR7.Global_Enable is set */
3439 return dr7 & (1 << 13);
3440}
3441
3442static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3443{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003444 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003445 u64 cr4;
3446
3447 if (dr > 7)
3448 return emulate_ud(ctxt);
3449
Avi Kivity717746e2011-04-20 13:37:53 +03003450 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003451 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3452 return emulate_ud(ctxt);
3453
3454 if (check_dr7_gd(ctxt))
3455 return emulate_db(ctxt);
3456
3457 return X86EMUL_CONTINUE;
3458}
3459
3460static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3461{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003462 u64 new_val = ctxt->src.val64;
3463 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003464
3465 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3466 return emulate_gp(ctxt, 0);
3467
3468 return check_dr_read(ctxt);
3469}
3470
Joerg Roedel01de8b02011-04-04 12:39:31 +02003471static int check_svme(struct x86_emulate_ctxt *ctxt)
3472{
3473 u64 efer;
3474
Avi Kivity717746e2011-04-20 13:37:53 +03003475 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003476
3477 if (!(efer & EFER_SVME))
3478 return emulate_ud(ctxt);
3479
3480 return X86EMUL_CONTINUE;
3481}
3482
3483static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3484{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003485 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003486
3487 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003488 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003489 return emulate_gp(ctxt, 0);
3490
3491 return check_svme(ctxt);
3492}
3493
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003494static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3495{
Avi Kivity717746e2011-04-20 13:37:53 +03003496 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003497
Avi Kivity717746e2011-04-20 13:37:53 +03003498 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003499 return emulate_ud(ctxt);
3500
3501 return X86EMUL_CONTINUE;
3502}
3503
Joerg Roedel80612522011-04-04 12:39:33 +02003504static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3505{
Avi Kivity717746e2011-04-20 13:37:53 +03003506 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003507 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003508
Avi Kivity717746e2011-04-20 13:37:53 +03003509 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03003510 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02003511 return emulate_gp(ctxt, 0);
3512
3513 return X86EMUL_CONTINUE;
3514}
3515
Joerg Roedelf6511932011-04-04 12:39:35 +02003516static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3517{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003518 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3519 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003520 return emulate_gp(ctxt, 0);
3521
3522 return X86EMUL_CONTINUE;
3523}
3524
3525static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3526{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003527 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3528 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003529 return emulate_gp(ctxt, 0);
3530
3531 return X86EMUL_CONTINUE;
3532}
3533
Avi Kivity73fba5f2010-07-29 15:11:53 +03003534#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003535#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
3536#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
3537 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003538#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003539#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003540#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3541#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003542#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003543#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003544#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003545#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003546 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003547#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003548 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
3549 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003550#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003551
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003552#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003553#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003554#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003555#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003556#define I2bvIP(_f, _e, _i, _p) \
3557 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003558
Avi Kivityfb864fb2013-01-04 16:18:54 +02003559#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3560 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3561 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003562
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003563static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003564 DI(SrcNone | Priv, monitor),
3565 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003566 N, N, N, N, N, N,
3567};
3568
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003569static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003570 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003571 II(SrcNone | Prot | EmulateOnUD, em_vmmcall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003572 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3573 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3574 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3575 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3576 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3577 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003578};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003579
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003580static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003581 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003582 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003583 N, N, N, N, N, N,
3584};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003585
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003586static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003587 F(Lock, em_add),
3588 F(Lock | PageTable, em_or),
3589 F(Lock, em_adc),
3590 F(Lock, em_sbb),
3591 F(Lock | PageTable, em_and),
3592 F(Lock, em_sub),
3593 F(Lock, em_xor),
3594 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003595};
3596
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003597static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003598 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003599};
3600
Avi Kivity007a3b52013-01-19 19:51:51 +02003601static const struct opcode group2[] = {
3602 F(DstMem | ModRM, em_rol),
3603 F(DstMem | ModRM, em_ror),
3604 F(DstMem | ModRM, em_rcl),
3605 F(DstMem | ModRM, em_rcr),
3606 F(DstMem | ModRM, em_shl),
3607 F(DstMem | ModRM, em_shr),
3608 F(DstMem | ModRM, em_shl),
3609 F(DstMem | ModRM, em_sar),
3610};
3611
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003612static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003613 F(DstMem | SrcImm | NoWrite, em_test),
3614 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003615 F(DstMem | SrcNone | Lock, em_not),
3616 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02003617 F(DstXacc | Src2Mem, em_mul_ex),
3618 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02003619 F(DstXacc | Src2Mem, em_div_ex),
3620 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003621};
3622
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003623static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003624 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3625 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003626 N, N, N, N, N, N,
3627};
3628
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003629static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003630 F(DstMem | SrcNone | Lock, em_inc),
3631 F(DstMem | SrcNone | Lock, em_dec),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003632 I(SrcMem | Stack, em_grp45),
3633 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3634 I(SrcMem | Stack, em_grp45),
3635 I(SrcMemFAddr | ImplicitOps, em_grp45),
Gleb Natapov188424b2013-04-11 12:32:14 +03003636 I(SrcMem | Stack, em_grp45), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003637};
3638
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003639static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003640 DI(Prot, sldt),
3641 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003642 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003643 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003644 N, N, N, N,
3645};
3646
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003647static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03003648 II(Mov | DstMem, em_sgdt, sgdt),
3649 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003650 II(SrcMem | Priv, em_lgdt, lgdt),
3651 II(SrcMem | Priv, em_lidt, lidt),
3652 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3653 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3654 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003655}, {
Borislav Petkovb51e9742013-09-22 16:44:52 +02003656 I(SrcNone | Priv | EmulateOnUD, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003657 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003658 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003659 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3660 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3661 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003662} };
3663
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003664static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003665 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003666 F(DstMem | SrcImmByte | NoWrite, em_bt),
3667 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3668 F(DstMem | SrcImmByte | Lock, em_btr),
3669 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003670};
3671
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003672static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003673 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003674}, {
3675 N, N, N, N, N, N, N, N,
3676} };
3677
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003678static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003679 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003680 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003681};
3682
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003683static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003684 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003685};
3686
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003687static const struct gprefix pfx_vmovntpx = {
Avi Kivity3e114eb2012-04-09 18:40:01 +03003688 I(0, em_mov), N, N, N,
3689};
3690
Igor Mammedov27ce8252014-03-15 21:01:59 +01003691static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01003692 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003693};
3694
Gleb Natapov045a2822012-12-20 16:57:43 +02003695static const struct escape escape_d9 = { {
3696 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3697}, {
3698 /* 0xC0 - 0xC7 */
3699 N, N, N, N, N, N, N, N,
3700 /* 0xC8 - 0xCF */
3701 N, N, N, N, N, N, N, N,
3702 /* 0xD0 - 0xC7 */
3703 N, N, N, N, N, N, N, N,
3704 /* 0xD8 - 0xDF */
3705 N, N, N, N, N, N, N, N,
3706 /* 0xE0 - 0xE7 */
3707 N, N, N, N, N, N, N, N,
3708 /* 0xE8 - 0xEF */
3709 N, N, N, N, N, N, N, N,
3710 /* 0xF0 - 0xF7 */
3711 N, N, N, N, N, N, N, N,
3712 /* 0xF8 - 0xFF */
3713 N, N, N, N, N, N, N, N,
3714} };
3715
3716static const struct escape escape_db = { {
3717 N, N, N, N, N, N, N, N,
3718}, {
3719 /* 0xC0 - 0xC7 */
3720 N, N, N, N, N, N, N, N,
3721 /* 0xC8 - 0xCF */
3722 N, N, N, N, N, N, N, N,
3723 /* 0xD0 - 0xC7 */
3724 N, N, N, N, N, N, N, N,
3725 /* 0xD8 - 0xDF */
3726 N, N, N, N, N, N, N, N,
3727 /* 0xE0 - 0xE7 */
3728 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3729 /* 0xE8 - 0xEF */
3730 N, N, N, N, N, N, N, N,
3731 /* 0xF0 - 0xF7 */
3732 N, N, N, N, N, N, N, N,
3733 /* 0xF8 - 0xFF */
3734 N, N, N, N, N, N, N, N,
3735} };
3736
3737static const struct escape escape_dd = { {
3738 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3739}, {
3740 /* 0xC0 - 0xC7 */
3741 N, N, N, N, N, N, N, N,
3742 /* 0xC8 - 0xCF */
3743 N, N, N, N, N, N, N, N,
3744 /* 0xD0 - 0xC7 */
3745 N, N, N, N, N, N, N, N,
3746 /* 0xD8 - 0xDF */
3747 N, N, N, N, N, N, N, N,
3748 /* 0xE0 - 0xE7 */
3749 N, N, N, N, N, N, N, N,
3750 /* 0xE8 - 0xEF */
3751 N, N, N, N, N, N, N, N,
3752 /* 0xF0 - 0xF7 */
3753 N, N, N, N, N, N, N, N,
3754 /* 0xF8 - 0xFF */
3755 N, N, N, N, N, N, N, N,
3756} };
3757
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003758static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003759 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003760 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003761 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3762 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003763 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003764 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003765 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3766 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003767 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003768 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003769 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3770 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003771 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003772 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003773 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3774 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003775 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003776 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003777 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003778 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003779 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003780 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003781 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003782 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003783 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02003784 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003785 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003786 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003787 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003788 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003789 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003790 I(ImplicitOps | Stack | No64, em_pusha),
3791 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003792 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3793 N, N, N, N,
3794 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003795 I(SrcImm | Mov | Stack, em_push),
3796 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003797 I(SrcImmByte | Mov | Stack, em_push),
3798 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003799 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003800 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003801 /* 0x70 - 0x7F */
3802 X16(D(SrcImmByte)),
3803 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003804 G(ByteOp | DstMem | SrcImm, group1),
3805 G(DstMem | SrcImm, group1),
3806 G(ByteOp | DstMem | SrcImm | No64, group1),
3807 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003808 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003809 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003810 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003811 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003812 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003813 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003814 D(ModRM | SrcMem | NoAccess | DstReg),
3815 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3816 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003817 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003818 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003819 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003820 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003821 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003822 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01003823 II(ImplicitOps | Stack, em_popf, popf),
3824 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003825 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003826 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003827 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003828 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003829 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003830 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003831 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003832 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3833 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003834 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003835 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003836 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003837 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02003838 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003839 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003840 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003841 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003842 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003843 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3844 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003845 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003846 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003847 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Bruce Rogers32611072013-09-09 09:40:20 -06003848 I(ImplicitOps | Stack | SrcImmU16, em_ret_far_imm),
3849 I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003850 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003851 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003852 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003853 G(Src2One | ByteOp, group2), G(Src2One, group2),
3854 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003855 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02003856 I(DstAcc | SrcImmUByte | No64, em_aad),
3857 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02003858 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003859 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02003860 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003861 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003862 X3(I(SrcImmByte, em_loop)),
3863 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003864 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3865 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003866 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003867 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003868 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003869 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3870 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003871 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003872 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003873 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3874 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003875 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003876 D(ImplicitOps), D(ImplicitOps),
3877 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003878 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3879};
3880
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003881static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003882 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003883 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02003884 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003885 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003886 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003887 N, D(ImplicitOps | ModRM), N, N,
3888 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02003889 N, N, N, N, N, N, N, N,
3890 D(ImplicitOps | ModRM), N, N, N, N, N, N, D(ImplicitOps | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003891 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03003892 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
3893 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
3894 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
3895 check_cr_write),
3896 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
3897 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003898 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003899 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
3900 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
3901 N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
Avi Kivity3e114eb2012-04-09 18:40:01 +03003902 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003903 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003904 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02003905 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003906 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02003907 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003908 I(ImplicitOps | EmulateOnUD, em_sysenter),
3909 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003910 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003911 N, N, N, N, N, N, N, N,
3912 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03003913 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003914 /* 0x50 - 0x5F */
3915 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3916 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003917 N, N, N, N,
3918 N, N, N, N,
3919 N, N, N, N,
3920 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003921 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003922 N, N, N, N,
3923 N, N, N, N,
3924 N, N, N, N,
3925 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003926 /* 0x80 - 0x8F */
3927 X16(D(SrcImm)),
3928 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003929 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003930 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003931 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02003932 II(ImplicitOps, em_cpuid, cpuid),
3933 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02003934 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
3935 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003936 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003937 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003938 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02003939 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02003940 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
3941 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Avi Kivity4d758342013-01-19 19:51:55 +02003942 D(ModRM), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003943 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09003944 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03003945 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02003946 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03003947 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
3948 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003949 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003950 /* 0xB8 - 0xBF */
3951 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003952 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02003953 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
3954 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003955 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03003956 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02003957 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003958 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003959 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03003960 /* 0xC8 - 0xCF */
3961 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003962 /* 0xD0 - 0xDF */
3963 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3964 /* 0xE0 - 0xEF */
3965 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3966 /* 0xF0 - 0xFF */
3967 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3968};
3969
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003970static const struct gprefix three_byte_0f_38_f0 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01003971 I(DstReg | SrcMem | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003972};
3973
3974static const struct gprefix three_byte_0f_38_f1 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01003975 I(DstMem | SrcReg | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003976};
3977
3978/*
3979 * Insns below are selected by the prefix which indexed by the third opcode
3980 * byte.
3981 */
3982static const struct opcode opcode_map_0f_38[256] = {
3983 /* 0x00 - 0x7f */
3984 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01003985 /* 0x80 - 0xef */
3986 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
3987 /* 0xf0 - 0xf1 */
3988 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f0),
3989 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f1),
3990 /* 0xf2 - 0xff */
3991 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003992};
3993
Avi Kivity73fba5f2010-07-29 15:11:53 +03003994#undef D
3995#undef N
3996#undef G
3997#undef GD
3998#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003999#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004000#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004001
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004002#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004003#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004004#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004005#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004006#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004007
Avi Kivity9dac77f2011-06-01 15:34:25 +03004008static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004009{
4010 unsigned size;
4011
Avi Kivity9dac77f2011-06-01 15:34:25 +03004012 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004013 if (size == 8)
4014 size = 4;
4015 return size;
4016}
4017
4018static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4019 unsigned size, bool sign_extension)
4020{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004021 int rc = X86EMUL_CONTINUE;
4022
4023 op->type = OP_IMM;
4024 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004025 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004026 /* NB. Immediates are sign-extended as necessary. */
4027 switch (op->bytes) {
4028 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004029 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004030 break;
4031 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004032 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004033 break;
4034 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004035 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004036 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004037 case 8:
4038 op->val = insn_fetch(s64, ctxt);
4039 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004040 }
4041 if (!sign_extension) {
4042 switch (op->bytes) {
4043 case 1:
4044 op->val &= 0xff;
4045 break;
4046 case 2:
4047 op->val &= 0xffff;
4048 break;
4049 case 4:
4050 op->val &= 0xffffffff;
4051 break;
4052 }
4053 }
4054done:
4055 return rc;
4056}
4057
Avi Kivitya9945542011-09-13 10:45:41 +03004058static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4059 unsigned d)
4060{
4061 int rc = X86EMUL_CONTINUE;
4062
4063 switch (d) {
4064 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004065 decode_register_operand(ctxt, op);
Avi Kivitya9945542011-09-13 10:45:41 +03004066 break;
4067 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004068 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03004069 break;
4070 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004071 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004072 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03004073 *op = ctxt->memop;
4074 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004075 if (ctxt->d & BitOp)
Avi Kivitya9945542011-09-13 10:45:41 +03004076 fetch_bit_operand(ctxt);
4077 op->orig_val = op->val;
4078 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004079 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004080 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004081 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03004082 case OpAcc:
4083 op->type = OP_REG;
4084 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004085 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya9945542011-09-13 10:45:41 +03004086 fetch_register_operand(op);
4087 op->orig_val = op->val;
4088 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004089 case OpAccLo:
4090 op->type = OP_REG;
4091 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4092 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4093 fetch_register_operand(op);
4094 op->orig_val = op->val;
4095 break;
4096 case OpAccHi:
4097 if (ctxt->d & ByteOp) {
4098 op->type = OP_NONE;
4099 break;
4100 }
4101 op->type = OP_REG;
4102 op->bytes = ctxt->op_bytes;
4103 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4104 fetch_register_operand(op);
4105 op->orig_val = op->val;
4106 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004107 case OpDI:
4108 op->type = OP_MEM;
4109 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4110 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004111 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya9945542011-09-13 10:45:41 +03004112 op->addr.mem.seg = VCPU_SREG_ES;
4113 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004114 op->count = 1;
Avi Kivitya9945542011-09-13 10:45:41 +03004115 break;
4116 case OpDX:
4117 op->type = OP_REG;
4118 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004119 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya9945542011-09-13 10:45:41 +03004120 fetch_register_operand(op);
4121 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004122 case OpCL:
4123 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004124 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004125 break;
4126 case OpImmByte:
4127 rc = decode_imm(ctxt, op, 1, true);
4128 break;
4129 case OpOne:
4130 op->bytes = 1;
4131 op->val = 1;
4132 break;
4133 case OpImm:
4134 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4135 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004136 case OpImm64:
4137 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4138 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004139 case OpMem8:
4140 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004141 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004142 ctxt->memop.addr.reg = decode_register(ctxt,
4143 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004144 fetch_register_operand(&ctxt->memop);
4145 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004146 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004147 case OpMem16:
4148 ctxt->memop.bytes = 2;
4149 goto mem_common;
4150 case OpMem32:
4151 ctxt->memop.bytes = 4;
4152 goto mem_common;
4153 case OpImmU16:
4154 rc = decode_imm(ctxt, op, 2, false);
4155 break;
4156 case OpImmU:
4157 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4158 break;
4159 case OpSI:
4160 op->type = OP_MEM;
4161 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4162 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004163 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Bandan Das573e80f2014-04-16 12:46:13 -04004164 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03004165 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004166 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004167 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004168 case OpXLat:
4169 op->type = OP_MEM;
4170 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4171 op->addr.mem.ea =
4172 register_address(ctxt,
4173 reg_read(ctxt, VCPU_REGS_RBX) +
4174 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04004175 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004176 op->val = 0;
4177 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004178 case OpImmFAddr:
4179 op->type = OP_IMM;
4180 op->addr.mem.ea = ctxt->_eip;
4181 op->bytes = ctxt->op_bytes + 2;
4182 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4183 break;
4184 case OpMemFAddr:
4185 ctxt->memop.bytes = ctxt->op_bytes + 2;
4186 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004187 case OpES:
4188 op->val = VCPU_SREG_ES;
4189 break;
4190 case OpCS:
4191 op->val = VCPU_SREG_CS;
4192 break;
4193 case OpSS:
4194 op->val = VCPU_SREG_SS;
4195 break;
4196 case OpDS:
4197 op->val = VCPU_SREG_DS;
4198 break;
4199 case OpFS:
4200 op->val = VCPU_SREG_FS;
4201 break;
4202 case OpGS:
4203 op->val = VCPU_SREG_GS;
4204 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004205 case OpImplicit:
4206 /* Special instructions do their own operand decoding. */
4207 default:
4208 op->type = OP_NONE; /* Disable writeback. */
4209 break;
4210 }
4211
4212done:
4213 return rc;
4214}
4215
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004216int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004217{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004218 int rc = X86EMUL_CONTINUE;
4219 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004220 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004221 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04004222 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03004223 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004224
Avi Kivityf09ed832011-09-13 10:45:40 +03004225 ctxt->memop.type = OP_NONE;
4226 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004227 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02004228 ctxt->fetch.ptr = ctxt->fetch.data;
4229 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004230 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01004231 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004232 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004233 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02004234 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004235 if (rc != X86EMUL_CONTINUE)
4236 return rc;
4237 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004238
4239 switch (mode) {
4240 case X86EMUL_MODE_REAL:
4241 case X86EMUL_MODE_VM86:
4242 case X86EMUL_MODE_PROT16:
4243 def_op_bytes = def_ad_bytes = 2;
4244 break;
4245 case X86EMUL_MODE_PROT32:
4246 def_op_bytes = def_ad_bytes = 4;
4247 break;
4248#ifdef CONFIG_X86_64
4249 case X86EMUL_MODE_PROT64:
4250 def_op_bytes = 4;
4251 def_ad_bytes = 8;
4252 break;
4253#endif
4254 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004255 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004256 }
4257
Avi Kivity9dac77f2011-06-01 15:34:25 +03004258 ctxt->op_bytes = def_op_bytes;
4259 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004260
4261 /* Legacy prefixes. */
4262 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004263 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004264 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004265 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004266 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004267 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004268 break;
4269 case 0x67: /* address-size override */
4270 if (mode == X86EMUL_MODE_PROT64)
4271 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004272 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004273 else
4274 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004275 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004276 break;
4277 case 0x26: /* ES override */
4278 case 0x2e: /* CS override */
4279 case 0x36: /* SS override */
4280 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004281 has_seg_override = true;
4282 ctxt->seg_override = (ctxt->b >> 3) & 3;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004283 break;
4284 case 0x64: /* FS override */
4285 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004286 has_seg_override = true;
4287 ctxt->seg_override = ctxt->b & 7;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004288 break;
4289 case 0x40 ... 0x4f: /* REX */
4290 if (mode != X86EMUL_MODE_PROT64)
4291 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004292 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004293 continue;
4294 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004295 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004296 break;
4297 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004298 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004299 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004300 break;
4301 default:
4302 goto done_prefixes;
4303 }
4304
4305 /* Any legacy prefix after a REX prefix nullifies its effect. */
4306
Avi Kivity9dac77f2011-06-01 15:34:25 +03004307 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004308 }
4309
4310done_prefixes:
4311
4312 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004313 if (ctxt->rex_prefix & 8)
4314 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004315
4316 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004317 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004318 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004319 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004320 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004321 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004322 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004323
4324 /* 0F_38 opcode map */
4325 if (ctxt->b == 0x38) {
4326 ctxt->opcode_len = 3;
4327 ctxt->b = insn_fetch(u8, ctxt);
4328 opcode = opcode_map_0f_38[ctxt->b];
4329 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004330 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004331 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004332
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004333 if (ctxt->d & ModRM)
4334 ctxt->modrm = insn_fetch(u8, ctxt);
4335
Nadav Amit7fe864d2014-06-02 18:34:03 +03004336 /* vex-prefix instructions are not implemented */
4337 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
4338 (mode == X86EMUL_MODE_PROT64 ||
4339 (mode >= X86EMUL_MODE_PROT16 && (ctxt->modrm & 0x80)))) {
4340 ctxt->d = NotImpl;
4341 }
4342
Avi Kivity9dac77f2011-06-01 15:34:25 +03004343 while (ctxt->d & GroupMask) {
4344 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004345 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004346 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004347 opcode = opcode.u.group[goffset];
4348 break;
4349 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004350 goffset = (ctxt->modrm >> 3) & 7;
4351 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004352 opcode = opcode.u.gdual->mod3[goffset];
4353 else
4354 opcode = opcode.u.gdual->mod012[goffset];
4355 break;
4356 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004357 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004358 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004359 break;
4360 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004361 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004362 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004363 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004364 switch (simd_prefix) {
4365 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4366 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4367 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4368 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4369 }
4370 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004371 case Escape:
4372 if (ctxt->modrm > 0xbf)
4373 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4374 else
4375 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4376 break;
Avi Kivity46561642011-04-24 14:09:59 +03004377 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004378 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004379 }
Avi Kivity46561642011-04-24 14:09:59 +03004380
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004381 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004382 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004383 }
4384
Paolo Bonzinie24186e2014-03-27 12:00:57 +01004385 /* Unrecognised? */
4386 if (ctxt->d == 0)
4387 return EMULATION_FAILED;
4388
Avi Kivity9dac77f2011-06-01 15:34:25 +03004389 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004390
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004391 if (unlikely(ctxt->d &
4392 (NotImpl|EmulateOnUD|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm))) {
4393 /*
4394 * These are copied unconditionally here, and checked unconditionally
4395 * in x86_emulate_insn.
4396 */
4397 ctxt->check_perm = opcode.check_perm;
4398 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004399
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004400 if (ctxt->d & NotImpl)
4401 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004402
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004403 if (!(ctxt->d & EmulateOnUD) && ctxt->ud)
4404 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004405
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004406 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004407 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004408
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004409 if (ctxt->d & Op3264) {
4410 if (mode == X86EMUL_MODE_PROT64)
4411 ctxt->op_bytes = 8;
4412 else
4413 ctxt->op_bytes = 4;
4414 }
4415
4416 if (ctxt->d & Sse)
4417 ctxt->op_bytes = 16;
4418 else if (ctxt->d & Mmx)
4419 ctxt->op_bytes = 8;
4420 }
Avi Kivity12537912011-03-29 11:41:27 +02004421
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004422 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004423 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004424 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04004425 if (!has_seg_override) {
4426 has_seg_override = true;
4427 ctxt->seg_override = ctxt->modrm_seg;
4428 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004429 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004430 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004431 if (rc != X86EMUL_CONTINUE)
4432 goto done;
4433
Bandan Das573e80f2014-04-16 12:46:13 -04004434 if (!has_seg_override)
4435 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004436
Bandan Das573e80f2014-04-16 12:46:13 -04004437 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004438
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004439 /*
4440 * Decode and fetch the source operand: register, memory
4441 * or immediate.
4442 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004443 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004444 if (rc != X86EMUL_CONTINUE)
4445 goto done;
4446
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004447 /*
4448 * Decode and fetch the second source operand: register, memory
4449 * or immediate.
4450 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004451 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004452 if (rc != X86EMUL_CONTINUE)
4453 goto done;
4454
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004455 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03004456 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004457
4458done:
Bandan Das41061cd2014-04-16 12:46:14 -04004459 if (ctxt->rip_relative)
Avi Kivityf09ed832011-09-13 10:45:40 +03004460 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004461
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004462 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004463}
4464
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004465bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4466{
4467 return ctxt->d & PageTable;
4468}
4469
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004470static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4471{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004472 /* The second termination condition only applies for REPE
4473 * and REPNE. Test if the repeat string operation prefix is
4474 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4475 * corresponding termination condition according to:
4476 * - if REPE/REPZ and ZF = 0 then done
4477 * - if REPNE/REPNZ and ZF = 1 then done
4478 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004479 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4480 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4481 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004482 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004483 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004484 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4485 return true;
4486
4487 return false;
4488}
4489
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004490static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4491{
4492 bool fault = false;
4493
4494 ctxt->ops->get_fpu(ctxt);
4495 asm volatile("1: fwait \n\t"
4496 "2: \n\t"
4497 ".pushsection .fixup,\"ax\" \n\t"
4498 "3: \n\t"
4499 "movb $1, %[fault] \n\t"
4500 "jmp 2b \n\t"
4501 ".popsection \n\t"
4502 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004503 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004504 ctxt->ops->put_fpu(ctxt);
4505
4506 if (unlikely(fault))
4507 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4508
4509 return X86EMUL_CONTINUE;
4510}
4511
4512static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4513 struct operand *op)
4514{
4515 if (op->type == OP_MM)
4516 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4517}
4518
Avi Kivitye28bbd42013-01-04 16:18:48 +02004519static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4520{
4521 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivityb9fa4092013-02-09 11:31:48 +02004522 if (!(ctxt->d & ByteOp))
4523 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Avi Kivitye28bbd42013-01-04 16:18:48 +02004524 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004525 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
4526 [fastop]"+S"(fop)
4527 : "c"(ctxt->src2.val));
Avi Kivitye28bbd42013-01-04 16:18:48 +02004528 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004529 if (!fop) /* exception is returned in fop variable */
4530 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02004531 return X86EMUL_CONTINUE;
4532}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004533
Bandan Das14985072014-04-16 12:46:09 -04004534void init_decode_cache(struct x86_emulate_ctxt *ctxt)
4535{
Bandan Das573e80f2014-04-16 12:46:13 -04004536 memset(&ctxt->rip_relative, 0,
4537 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04004538
Bandan Das14985072014-04-16 12:46:09 -04004539 ctxt->io_read.pos = 0;
4540 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04004541 ctxt->mem_read.end = 0;
4542}
4543
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004544int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004545{
Mathias Krause0225fb52012-08-30 01:30:16 +02004546 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004547 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004548 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004549
Avi Kivity9dac77f2011-06-01 15:34:25 +03004550 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004551
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004552 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004553 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004554 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004555 goto done;
4556 }
4557
Avi Kivity9dac77f2011-06-01 15:34:25 +03004558 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004559 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004560 goto done;
4561 }
4562
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004563 if (unlikely(ctxt->d &
4564 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
4565 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4566 (ctxt->d & Undefined)) {
4567 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004568 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004569 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004570
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004571 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4572 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
4573 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004574 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004575 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004576
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004577 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
4578 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004579 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004580 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004581
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004582 if (ctxt->d & Mmx) {
4583 rc = flush_pending_x87_faults(ctxt);
4584 if (rc != X86EMUL_CONTINUE)
4585 goto done;
4586 /*
4587 * Now that we know the fpu is exception safe, we can fetch
4588 * operands from it.
4589 */
4590 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4591 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4592 if (!(ctxt->d & Mov))
4593 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4594 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004595
Bandan Das685bbf42014-04-16 12:46:10 -04004596 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004597 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4598 X86_ICPT_PRE_EXCEPT);
4599 if (rc != X86EMUL_CONTINUE)
4600 goto done;
4601 }
4602
4603 /* Privileged instruction can be executed only in CPL=0 */
4604 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
4605 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004606 goto done;
4607 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004608
4609 /* Instruction can only be executed in protected mode */
4610 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
4611 rc = emulate_ud(ctxt);
4612 goto done;
4613 }
4614
4615 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04004616 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004617 rc = ctxt->check_perm(ctxt);
4618 if (rc != X86EMUL_CONTINUE)
4619 goto done;
4620 }
4621
Bandan Das685bbf42014-04-16 12:46:10 -04004622 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004623 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4624 X86_ICPT_POST_EXCEPT);
4625 if (rc != X86EMUL_CONTINUE)
4626 goto done;
4627 }
4628
4629 if (ctxt->rep_prefix && (ctxt->d & String)) {
4630 /* All REP prefixes have the same first termination condition */
4631 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
4632 ctxt->eip = ctxt->_eip;
4633 goto done;
4634 }
4635 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004636 }
4637
Avi Kivity9dac77f2011-06-01 15:34:25 +03004638 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4639 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4640 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004641 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004642 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004643 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004644 }
4645
Avi Kivity9dac77f2011-06-01 15:34:25 +03004646 if (ctxt->src2.type == OP_MEM) {
4647 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4648 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004649 if (rc != X86EMUL_CONTINUE)
4650 goto done;
4651 }
4652
Avi Kivity9dac77f2011-06-01 15:34:25 +03004653 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004654 goto special_insn;
4655
4656
Avi Kivity9dac77f2011-06-01 15:34:25 +03004657 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004658 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004659 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4660 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004661 if (rc != X86EMUL_CONTINUE)
4662 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004663 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004664 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004665
Avi Kivity018a98d2007-11-27 19:30:56 +02004666special_insn:
4667
Bandan Das685bbf42014-04-16 12:46:10 -04004668 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004669 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004670 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004671 if (rc != X86EMUL_CONTINUE)
4672 goto done;
4673 }
4674
Avi Kivity9dac77f2011-06-01 15:34:25 +03004675 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004676 if (ctxt->d & Fastop) {
4677 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4678 rc = fastop(ctxt, fop);
4679 if (rc != X86EMUL_CONTINUE)
4680 goto done;
4681 goto writeback;
4682 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004683 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004684 if (rc != X86EMUL_CONTINUE)
4685 goto done;
4686 goto writeback;
4687 }
4688
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004689 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004690 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004691 else if (ctxt->opcode_len == 3)
4692 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004693
Avi Kivity9dac77f2011-06-01 15:34:25 +03004694 switch (ctxt->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004695 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004696 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004697 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004698 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004699 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004700 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004701 if (test_cc(ctxt->b, ctxt->eflags))
4702 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004703 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004704 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004705 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004706 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004707 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004708 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03004709 ctxt->dst.type = OP_NONE;
4710 else
4711 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004712 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004713 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004714 switch (ctxt->op_bytes) {
4715 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4716 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4717 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004718 }
4719 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004720 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004721 rc = emulate_int(ctxt, 3);
4722 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004723 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004724 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004725 break;
4726 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004727 if (ctxt->eflags & EFLG_OF)
4728 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004729 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004730 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004731 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004732 jmp_rel(ctxt, ctxt->src.val);
4733 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004734 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004735 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004736 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004737 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004738 case 0xf5: /* cmc */
4739 /* complement carry flag from eflags reg */
4740 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004741 break;
4742 case 0xf8: /* clc */
4743 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004744 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004745 case 0xf9: /* stc */
4746 ctxt->eflags |= EFLG_CF;
4747 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004748 case 0xfc: /* cld */
4749 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004750 break;
4751 case 0xfd: /* std */
4752 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004753 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004754 default:
4755 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004756 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004757
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004758 if (rc != X86EMUL_CONTINUE)
4759 goto done;
4760
Avi Kivity018a98d2007-11-27 19:30:56 +02004761writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02004762 if (ctxt->d & SrcWrite) {
4763 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
4764 rc = writeback(ctxt, &ctxt->src);
4765 if (rc != X86EMUL_CONTINUE)
4766 goto done;
4767 }
Nadav Amitee212292014-06-15 16:12:58 +03004768 if (!(ctxt->d & NoWrite)) {
4769 rc = writeback(ctxt, &ctxt->dst);
4770 if (rc != X86EMUL_CONTINUE)
4771 goto done;
4772 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004773
Gleb Natapov5cd21912010-03-18 15:20:26 +02004774 /*
4775 * restore dst type in case the decoding will be reused
4776 * (happens for string instruction )
4777 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004778 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004779
Avi Kivity9dac77f2011-06-01 15:34:25 +03004780 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004781 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004782
Avi Kivity9dac77f2011-06-01 15:34:25 +03004783 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004784 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004785
Avi Kivity9dac77f2011-06-01 15:34:25 +03004786 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004787 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004788 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004789 if ((ctxt->d & SrcMask) == SrcSI)
4790 count = ctxt->src.count;
4791 else
4792 count = ctxt->dst.count;
4793 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4794 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004795
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004796 if (!string_insn_completed(ctxt)) {
4797 /*
4798 * Re-enter guest when pio read ahead buffer is empty
4799 * or, if it is not used, after each 1024 iteration.
4800 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004801 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004802 (r->end == 0 || r->end != r->pos)) {
4803 /*
4804 * Reset read cache. Usually happens before
4805 * decode, but since instruction is restarted
4806 * we have to do it here.
4807 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004808 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004809 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004810 return EMULATION_RESTART;
4811 }
4812 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004813 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004814 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004815
Avi Kivity9dac77f2011-06-01 15:34:25 +03004816 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004817
4818done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004819 if (rc == X86EMUL_PROPAGATE_FAULT)
4820 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004821 if (rc == X86EMUL_INTERCEPTED)
4822 return EMULATION_INTERCEPTED;
4823
Avi Kivitydd856ef2012-08-27 23:46:17 +03004824 if (rc == X86EMUL_CONTINUE)
4825 writeback_registers(ctxt);
4826
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004827 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004828
4829twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004830 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004831 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004832 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004833 break;
4834 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004835 case 0x0d: /* GrpP (prefetch) */
4836 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004837 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02004838 break;
4839 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004840 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004841 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004842 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004843 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004844 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004845 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03004846 if (test_cc(ctxt->b, ctxt->eflags))
4847 ctxt->dst.val = ctxt->src.val;
4848 else if (ctxt->mode != X86EMUL_MODE_PROT64 ||
4849 ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004850 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004851 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004852 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004853 if (test_cc(ctxt->b, ctxt->eflags))
4854 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004855 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004856 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004857 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004858 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004859 case 0xae: /* clflush */
4860 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004861 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004862 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004863 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004864 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004865 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004866 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004867 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004868 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004869 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004870 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004871 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004872 ctxt->dst.bytes = ctxt->op_bytes;
Nadav Amit3b320042014-06-02 18:34:08 +03004873 ctxt->dst.val = (ctxt->op_bytes == 8) ? (u64) ctxt->src.val :
4874 (u32) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004875 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004876 default:
4877 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004878 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004879
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004880threebyte_insn:
4881
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004882 if (rc != X86EMUL_CONTINUE)
4883 goto done;
4884
Avi Kivity6aa8b732006-12-10 02:21:36 -08004885 goto writeback;
4886
4887cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004888 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004889}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004890
4891void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
4892{
4893 invalidate_registers(ctxt);
4894}
4895
4896void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
4897{
4898 writeback_registers(ctxt);
4899}