blob: 56657b0bb3bb14f14b76fdcd99a746598ce5e8b4 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivityb7d491e2013-01-04 16:18:49 +020027#include <linux/stringify.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028
Avi Kivity3eeb3282010-01-21 15:31:48 +020029#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020030#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020031
Avi Kivity6aa8b732006-12-10 02:21:36 -080032/*
Avi Kivitya99455492011-09-13 10:45:41 +030033 * Operand types
34 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030035#define OpNone 0ull
36#define OpImplicit 1ull /* No generic decode */
37#define OpReg 2ull /* Register */
38#define OpMem 3ull /* Memory */
39#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
40#define OpDI 5ull /* ES:DI/EDI/RDI */
41#define OpMem64 6ull /* Memory, 64-bit */
42#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
43#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030044#define OpCL 9ull /* CL register (for shifts) */
45#define OpImmByte 10ull /* 8-bit sign extended immediate */
46#define OpOne 11ull /* Implied 1 */
Nadav Amit5e2c6882012-12-06 21:55:10 -020047#define OpImm 12ull /* Sign extended up to 32-bit immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030048#define OpMem16 13ull /* Memory operand (16-bit). */
49#define OpMem32 14ull /* Memory operand (32-bit). */
50#define OpImmU 15ull /* Immediate operand, zero extended */
51#define OpSI 16ull /* SI/ESI/RSI */
52#define OpImmFAddr 17ull /* Immediate far address */
53#define OpMemFAddr 18ull /* Far address in memory */
54#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030055#define OpES 20ull /* ES */
56#define OpCS 21ull /* CS */
57#define OpSS 22ull /* SS */
58#define OpDS 23ull /* DS */
59#define OpFS 24ull /* FS */
60#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020061#define OpMem8 26ull /* 8-bit zero extended memory operand */
Nadav Amit5e2c6882012-12-06 21:55:10 -020062#define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */
Paolo Bonzini7fa57952013-05-09 11:32:50 +020063#define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */
Avi Kivity820207c2013-02-09 11:31:45 +020064#define OpAccLo 29ull /* Low part of extended acc (AX/AX/EAX/RAX) */
65#define OpAccHi 30ull /* High part of extended acc (-/DX/EDX/RDX) */
Avi Kivitya99455492011-09-13 10:45:41 +030066
Avi Kivity0fe59122011-09-13 10:45:47 +030067#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030068#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya99455492011-09-13 10:45:41 +030069
70/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080071 * Opcode effective-address decode tables.
72 * Note that we only emulate instructions that have at least one memory
73 * operand (excluding implicit stack references). We assume that stack
74 * references and instruction fetches will never occur in special memory
75 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
76 * not be handled.
77 */
78
79/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030080#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080081/* Destination operand type. */
Avi Kivitya99455492011-09-13 10:45:41 +030082#define DstShift 1
83#define ImplicitOps (OpImplicit << DstShift)
84#define DstReg (OpReg << DstShift)
85#define DstMem (OpMem << DstShift)
86#define DstAcc (OpAcc << DstShift)
87#define DstDI (OpDI << DstShift)
88#define DstMem64 (OpMem64 << DstShift)
89#define DstImmUByte (OpImmUByte << DstShift)
90#define DstDX (OpDX << DstShift)
Avi Kivity820207c2013-02-09 11:31:45 +020091#define DstAccLo (OpAccLo << DstShift)
Avi Kivitya99455492011-09-13 10:45:41 +030092#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080093/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030094#define SrcShift 6
95#define SrcNone (OpNone << SrcShift)
96#define SrcReg (OpReg << SrcShift)
97#define SrcMem (OpMem << SrcShift)
98#define SrcMem16 (OpMem16 << SrcShift)
99#define SrcMem32 (OpMem32 << SrcShift)
100#define SrcImm (OpImm << SrcShift)
101#define SrcImmByte (OpImmByte << SrcShift)
102#define SrcOne (OpOne << SrcShift)
103#define SrcImmUByte (OpImmUByte << SrcShift)
104#define SrcImmU (OpImmU << SrcShift)
105#define SrcSI (OpSI << SrcShift)
Paolo Bonzini7fa57952013-05-09 11:32:50 +0200106#define SrcXLat (OpXLat << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300107#define SrcImmFAddr (OpImmFAddr << SrcShift)
108#define SrcMemFAddr (OpMemFAddr << SrcShift)
109#define SrcAcc (OpAcc << SrcShift)
110#define SrcImmU16 (OpImmU16 << SrcShift)
Nadav Amit5e2c6882012-12-06 21:55:10 -0200111#define SrcImm64 (OpImm64 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300112#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200113#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity820207c2013-02-09 11:31:45 +0200114#define SrcAccHi (OpAccHi << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300115#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300116#define BitOp (1<<11)
117#define MemAbs (1<<12) /* Memory operand is absolute displacement */
118#define String (1<<13) /* String instruction (rep capable) */
119#define Stack (1<<14) /* Stack instruction (push/pop) */
120#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
121#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
122#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
123#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
124#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
Gleb Natapov045a2822012-12-20 16:57:43 +0200125#define Escape (5<<15) /* Escape to coprocessor instruction */
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300126#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300127/* Generic ModRM decode. */
128#define ModRM (1<<19)
129/* Destination is only written; never read. */
130#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300131/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200132#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Borislav Petkovb51e9742013-09-22 16:44:52 +0200133#define EmulateOnUD (1<<22) /* Emulate if unsupported by the host */
Avi Kivity5a506b12010-08-01 15:10:29 +0300134#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300135#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300136#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200137#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200138#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300139#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800140#define PageTable (1 << 29) /* instruction used to write page table */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300141#define NotImpl (1 << 30) /* instruction is not implemented */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100142/* Source 2 operand type */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300143#define Src2Shift (31)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300144#define Src2None (OpNone << Src2Shift)
Avi Kivityab2c5ce2013-02-09 11:31:46 +0200145#define Src2Mem (OpMem << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300146#define Src2CL (OpCL << Src2Shift)
147#define Src2ImmByte (OpImmByte << Src2Shift)
148#define Src2One (OpOne << Src2Shift)
149#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300150#define Src2ES (OpES << Src2Shift)
151#define Src2CS (OpCS << Src2Shift)
152#define Src2SS (OpSS << Src2Shift)
153#define Src2DS (OpDS << Src2Shift)
154#define Src2FS (OpFS << Src2Shift)
155#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300156#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300157#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Avi Kivity1c11b372012-04-09 18:39:59 +0300158#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
159#define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */
160#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
Avi Kivitye28bbd42013-01-04 16:18:48 +0200161#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
Avi Kivityb6744dc2013-01-04 16:18:50 +0200162#define NoWrite ((u64)1 << 45) /* No writeback */
Avi Kivityfb32b1e2013-02-09 11:31:44 +0200163#define SrcWrite ((u64)1 << 46) /* Write back src operand */
Nadav Amit9b88ae92014-05-25 23:05:21 +0300164#define NoMod ((u64)1 << 47) /* Mod field is ignored */
Paolo Bonzinid40a6892014-03-27 11:58:02 +0100165#define Intercept ((u64)1 << 48) /* Has valid intercept field */
166#define CheckPerm ((u64)1 << 49) /* Has valid check_perm field */
Nadav Amit10e38fc2014-06-18 17:19:34 +0300167#define NoBigReal ((u64)1 << 50) /* No big real mode */
Nadav Amit68efa762014-06-18 17:19:35 +0300168#define PrivUD ((u64)1 << 51) /* #UD instead of #GP on CPL > 0 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800169
Avi Kivity820207c2013-02-09 11:31:45 +0200170#define DstXacc (DstAccLo | SrcAccHi | SrcWrite)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800171
Avi Kivityd0e53322010-07-29 15:11:54 +0300172#define X2(x...) x, x
173#define X3(x...) X2(x), x
174#define X4(x...) X2(x), X2(x)
175#define X5(x...) X4(x), x
176#define X6(x...) X4(x), X2(x)
177#define X7(x...) X4(x), X3(x)
178#define X8(x...) X4(x), X4(x)
179#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300180
Avi Kivitye28bbd42013-01-04 16:18:48 +0200181#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
182#define FASTOP_SIZE 8
183
184/*
185 * fastop functions have a special calling convention:
186 *
Avi Kivity017da7b2013-02-09 11:31:47 +0200187 * dst: rax (in/out)
188 * src: rdx (in/out)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200189 * src2: rcx (in)
190 * flags: rflags (in/out)
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200191 * ex: rsi (in:fastop pointer, out:zero if exception)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200192 *
193 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
194 * different operand sizes can be reached by calculation, rather than a jump
195 * table (which would be bigger than the code).
196 *
197 * fastop functions are declared as taking a never-defined fastop parameter,
198 * so they can't be called from C directly.
199 */
200
201struct fastop;
202
Avi Kivityd65b1de2010-07-29 15:11:35 +0300203struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300204 u64 flags : 56;
205 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300206 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300207 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200208 const struct opcode *group;
209 const struct group_dual *gdual;
210 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200211 const struct escape *esc;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200212 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300213 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200214 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300215};
216
217struct group_dual {
218 struct opcode mod012[8];
219 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300220};
221
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200222struct gprefix {
223 struct opcode pfx_no;
224 struct opcode pfx_66;
225 struct opcode pfx_f2;
226 struct opcode pfx_f3;
227};
228
Gleb Natapov045a2822012-12-20 16:57:43 +0200229struct escape {
230 struct opcode op[8];
231 struct opcode high[64];
232};
233
Avi Kivity6aa8b732006-12-10 02:21:36 -0800234/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200235#define EFLG_ID (1<<21)
236#define EFLG_VIP (1<<20)
237#define EFLG_VIF (1<<19)
238#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200239#define EFLG_VM (1<<17)
240#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200241#define EFLG_IOPL (3<<12)
242#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800243#define EFLG_OF (1<<11)
244#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200245#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200246#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800247#define EFLG_SF (1<<7)
248#define EFLG_ZF (1<<6)
249#define EFLG_AF (1<<4)
250#define EFLG_PF (1<<2)
251#define EFLG_CF (1<<0)
252
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300253#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
254#define EFLG_RESERVED_ONE_MASK 2
255
Avi Kivitydd856ef2012-08-27 23:46:17 +0300256static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
257{
258 if (!(ctxt->regs_valid & (1 << nr))) {
259 ctxt->regs_valid |= 1 << nr;
260 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
261 }
262 return ctxt->_regs[nr];
263}
264
265static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
266{
267 ctxt->regs_valid |= 1 << nr;
268 ctxt->regs_dirty |= 1 << nr;
269 return &ctxt->_regs[nr];
270}
271
272static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
273{
274 reg_read(ctxt, nr);
275 return reg_write(ctxt, nr);
276}
277
278static void writeback_registers(struct x86_emulate_ctxt *ctxt)
279{
280 unsigned reg;
281
282 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
283 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
284}
285
286static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
287{
288 ctxt->regs_dirty = 0;
289 ctxt->regs_valid = 0;
290}
291
Avi Kivity6aa8b732006-12-10 02:21:36 -0800292/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800293 * These EFLAGS bits are restored from saved value during emulation, and
294 * any changes are written back to the saved value after emulation.
295 */
296#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
297
Avi Kivitydda96d82008-11-26 15:14:10 +0200298#ifdef CONFIG_X86_64
299#define ON64(x) x
300#else
301#define ON64(x)
302#endif
303
Avi Kivity4d758342013-01-19 19:51:55 +0200304static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *));
305
Avi Kivityb7d491e2013-01-04 16:18:49 +0200306#define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t"
307#define FOP_RET "ret \n\t"
308
309#define FOP_START(op) \
310 extern void em_##op(struct fastop *fake); \
311 asm(".pushsection .text, \"ax\" \n\t" \
312 ".global em_" #op " \n\t" \
313 FOP_ALIGN \
314 "em_" #op ": \n\t"
315
316#define FOP_END \
317 ".popsection")
318
Avi Kivity0bdea062013-01-19 19:51:50 +0200319#define FOPNOP() FOP_ALIGN FOP_RET
320
Avi Kivityb7d491e2013-01-04 16:18:49 +0200321#define FOP1E(op, dst) \
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200322 FOP_ALIGN "10: " #op " %" #dst " \n\t" FOP_RET
323
324#define FOP1EEX(op, dst) \
325 FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200326
327#define FASTOP1(op) \
328 FOP_START(op) \
329 FOP1E(op##b, al) \
330 FOP1E(op##w, ax) \
331 FOP1E(op##l, eax) \
332 ON64(FOP1E(op##q, rax)) \
333 FOP_END
334
Avi Kivityb9fa4092013-02-09 11:31:48 +0200335/* 1-operand, using src2 (for MUL/DIV r/m) */
336#define FASTOP1SRC2(op, name) \
337 FOP_START(name) \
338 FOP1E(op, cl) \
339 FOP1E(op, cx) \
340 FOP1E(op, ecx) \
341 ON64(FOP1E(op, rcx)) \
342 FOP_END
343
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200344/* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */
345#define FASTOP1SRC2EX(op, name) \
346 FOP_START(name) \
347 FOP1EEX(op, cl) \
348 FOP1EEX(op, cx) \
349 FOP1EEX(op, ecx) \
350 ON64(FOP1EEX(op, rcx)) \
351 FOP_END
352
Avi Kivityf7857f32013-01-04 16:18:53 +0200353#define FOP2E(op, dst, src) \
354 FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET
355
356#define FASTOP2(op) \
357 FOP_START(op) \
Avi Kivity017da7b2013-02-09 11:31:47 +0200358 FOP2E(op##b, al, dl) \
359 FOP2E(op##w, ax, dx) \
360 FOP2E(op##l, eax, edx) \
361 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivityf7857f32013-01-04 16:18:53 +0200362 FOP_END
363
Avi Kivity11c363b2013-01-19 19:51:54 +0200364/* 2 operand, word only */
365#define FASTOP2W(op) \
366 FOP_START(op) \
367 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200368 FOP2E(op##w, ax, dx) \
369 FOP2E(op##l, eax, edx) \
370 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivity11c363b2013-01-19 19:51:54 +0200371 FOP_END
372
Avi Kivity007a3b52013-01-19 19:51:51 +0200373/* 2 operand, src is CL */
374#define FASTOP2CL(op) \
375 FOP_START(op) \
376 FOP2E(op##b, al, cl) \
377 FOP2E(op##w, ax, cl) \
378 FOP2E(op##l, eax, cl) \
379 ON64(FOP2E(op##q, rax, cl)) \
380 FOP_END
381
Avi Kivity0bdea062013-01-19 19:51:50 +0200382#define FOP3E(op, dst, src, src2) \
383 FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
384
385/* 3-operand, word-only, src2=cl */
386#define FASTOP3WCL(op) \
387 FOP_START(op) \
388 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200389 FOP3E(op##w, ax, dx, cl) \
390 FOP3E(op##l, eax, edx, cl) \
391 ON64(FOP3E(op##q, rax, rdx, cl)) \
Avi Kivity0bdea062013-01-19 19:51:50 +0200392 FOP_END
393
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200394/* Special case for SETcc - 1 instruction per cc */
395#define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t"
396
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200397asm(".global kvm_fastop_exception \n"
398 "kvm_fastop_exception: xor %esi, %esi; ret");
399
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200400FOP_START(setcc)
401FOP_SETCC(seto)
402FOP_SETCC(setno)
403FOP_SETCC(setc)
404FOP_SETCC(setnc)
405FOP_SETCC(setz)
406FOP_SETCC(setnz)
407FOP_SETCC(setbe)
408FOP_SETCC(setnbe)
409FOP_SETCC(sets)
410FOP_SETCC(setns)
411FOP_SETCC(setp)
412FOP_SETCC(setnp)
413FOP_SETCC(setl)
414FOP_SETCC(setnl)
415FOP_SETCC(setle)
416FOP_SETCC(setnle)
417FOP_END;
418
Paolo Bonzini326f5782013-05-09 11:32:51 +0200419FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET
420FOP_END;
421
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200422static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
423 enum x86_intercept intercept,
424 enum x86_intercept_stage stage)
425{
426 struct x86_instruction_info info = {
427 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300428 .rep_prefix = ctxt->rep_prefix,
429 .modrm_mod = ctxt->modrm_mod,
430 .modrm_reg = ctxt->modrm_reg,
431 .modrm_rm = ctxt->modrm_rm,
432 .src_val = ctxt->src.val64,
Jan Kiszka6cbc5f52014-06-30 12:52:55 +0200433 .dst_val = ctxt->dst.val64,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300434 .src_bytes = ctxt->src.bytes,
435 .dst_bytes = ctxt->dst.bytes,
436 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200437 .next_rip = ctxt->eip,
438 };
439
Avi Kivity29535382011-04-20 13:37:53 +0300440 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200441}
442
Avi Kivityf47cfa32012-06-07 17:49:24 +0300443static void assign_masked(ulong *dest, ulong src, ulong mask)
444{
445 *dest = (*dest & ~mask) | (src & mask);
446}
447
Avi Kivity9dac77f2011-06-01 15:34:25 +0300448static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800449{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300450 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800451}
452
Avi Kivityf47cfa32012-06-07 17:49:24 +0300453static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
454{
455 u16 sel;
456 struct desc_struct ss;
457
458 if (ctxt->mode == X86EMUL_MODE_PROT64)
459 return ~0UL;
460 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
461 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
462}
463
Avi Kivity612e89f2012-06-12 20:03:23 +0300464static int stack_size(struct x86_emulate_ctxt *ctxt)
465{
466 return (__fls(stack_mask(ctxt)) + 1) >> 3;
467}
468
Avi Kivity6aa8b732006-12-10 02:21:36 -0800469/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800470static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300471address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800472{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300473 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800474 return reg;
475 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300476 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800477}
478
479static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300480register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800481{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300482 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800483}
484
Avi Kivity5ad105e2012-08-19 14:34:31 +0300485static void masked_increment(ulong *reg, ulong mask, int inc)
486{
487 assign_masked(reg, *reg + inc, mask);
488}
489
Harvey Harrison7a9572752008-02-19 07:40:41 -0800490static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300491register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800492{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300493 ulong mask;
494
Avi Kivity9dac77f2011-06-01 15:34:25 +0300495 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300496 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800497 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300498 mask = ad_mask(ctxt);
499 masked_increment(reg, mask, inc);
500}
501
502static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
503{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300504 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800505}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800506
Avi Kivity9dac77f2011-06-01 15:34:25 +0300507static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800508{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300509 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800510}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300511
Avi Kivity56697682011-04-03 14:08:51 +0300512static u32 desc_limit_scaled(struct desc_struct *desc)
513{
514 u32 limit = get_desc_limit(desc);
515
516 return desc->g ? (limit << 12) | 0xfff : limit;
517}
518
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900519static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300520{
521 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
522 return 0;
523
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900524 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300525}
526
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200527static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
528 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300529{
Avi Kivityda9cb572010-11-22 17:53:21 +0200530 ctxt->exception.vector = vec;
531 ctxt->exception.error_code = error;
532 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200533 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300534}
535
Joerg Roedel3b88e412011-04-04 12:39:29 +0200536static int emulate_db(struct x86_emulate_ctxt *ctxt)
537{
538 return emulate_exception(ctxt, DB_VECTOR, 0, false);
539}
540
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200541static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300542{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200543 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300544}
545
Avi Kivity618ff152011-04-03 12:32:09 +0300546static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
547{
548 return emulate_exception(ctxt, SS_VECTOR, err, true);
549}
550
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200551static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300552{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200553 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300554}
555
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200556static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300557{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200558 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300559}
560
Avi Kivity34d1f492010-08-26 11:59:01 +0300561static int emulate_de(struct x86_emulate_ctxt *ctxt)
562{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200563 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300564}
565
Avi Kivity12537912011-03-29 11:41:27 +0200566static int emulate_nm(struct x86_emulate_ctxt *ctxt)
567{
568 return emulate_exception(ctxt, NM_VECTOR, 0, false);
569}
570
Avi Kivity1aa36612011-04-27 13:20:30 +0300571static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
572{
573 u16 selector;
574 struct desc_struct desc;
575
576 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
577 return selector;
578}
579
580static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
581 unsigned seg)
582{
583 u16 dummy;
584 u32 base3;
585 struct desc_struct desc;
586
587 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
588 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
589}
590
Avi Kivity1c11b372012-04-09 18:39:59 +0300591/*
592 * x86 defines three classes of vector instructions: explicitly
593 * aligned, explicitly unaligned, and the rest, which change behaviour
594 * depending on whether they're AVX encoded or not.
595 *
596 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
597 * subject to the same check.
598 */
599static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
600{
601 if (likely(size < 16))
602 return false;
603
604 if (ctxt->d & Aligned)
605 return true;
606 else if (ctxt->d & Unaligned)
607 return false;
608 else if (ctxt->d & Avx)
609 return false;
610 else
611 return true;
612}
613
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400614static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300615 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400616 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300617 ulong *linear)
618{
Avi Kivity618ff152011-04-03 12:32:09 +0300619 struct desc_struct desc;
620 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300621 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300622 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300623 u16 sel;
Gleb Natapov3a78a4f2012-12-20 16:57:42 +0200624 unsigned cpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300625
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900626 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300627 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300628 case X86EMUL_MODE_PROT64:
629 if (((signed long)la << 16) >> 16 != la)
630 return emulate_gp(ctxt, 0);
631 break;
632 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300633 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
634 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300635 if (!usable)
636 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200637 /* code segment in protected mode or read-only data segment */
638 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
639 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300640 goto bad;
641 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400642 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300643 goto bad;
644 lim = desc_limit_scaled(&desc);
Nadav Amit10e38fc2014-06-18 17:19:34 +0300645 if ((ctxt->mode == X86EMUL_MODE_REAL) && !fetch &&
646 (ctxt->d & NoBigReal)) {
647 /* la is between zero and 0xffff */
648 if (la > 0xffff || (u32)(la + size - 1) > 0xffff)
649 goto bad;
650 } else if ((desc.type & 8) || !(desc.type & 4)) {
Avi Kivity618ff152011-04-03 12:32:09 +0300651 /* expand-up segment */
652 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
653 goto bad;
654 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800655 /* expand-down segment */
Avi Kivity618ff152011-04-03 12:32:09 +0300656 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
657 goto bad;
658 lim = desc.d ? 0xffffffff : 0xffff;
659 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
660 goto bad;
661 }
Avi Kivity717746e2011-04-20 13:37:53 +0300662 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity618ff152011-04-03 12:32:09 +0300663 if (!(desc.type & 8)) {
664 /* data segment */
665 if (cpl > desc.dpl)
666 goto bad;
667 } else if ((desc.type & 8) && !(desc.type & 4)) {
668 /* nonconforming code segment */
669 if (cpl != desc.dpl)
670 goto bad;
671 } else if ((desc.type & 8) && (desc.type & 4)) {
672 /* conforming code segment */
673 if (cpl < desc.dpl)
674 goto bad;
675 }
676 break;
677 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300678 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300679 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300680 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
681 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300682 *linear = la;
683 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300684bad:
685 if (addr.seg == VCPU_SREG_SS)
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300686 return emulate_ss(ctxt, sel);
Avi Kivity618ff152011-04-03 12:32:09 +0300687 else
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300688 return emulate_gp(ctxt, sel);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300689}
690
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400691static int linearize(struct x86_emulate_ctxt *ctxt,
692 struct segmented_address addr,
693 unsigned size, bool write,
694 ulong *linear)
695{
696 return __linearize(ctxt, addr, size, write, false, linear);
697}
698
699
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200700static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
701 struct segmented_address addr,
702 void *data,
703 unsigned size)
704{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200705 int rc;
706 ulong linear;
707
Avi Kivity83b87952011-04-03 11:31:19 +0300708 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200709 if (rc != X86EMUL_CONTINUE)
710 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300711 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200712}
713
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900714/*
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200715 * Prefetch the remaining bytes of the instruction without crossing page
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900716 * boundary if they are not in fetch_cache yet.
717 */
Paolo Bonzini9506d572014-05-06 13:05:25 +0200718static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
Avi Kivity62266862007-11-20 13:15:52 +0200719{
Avi Kivity62266862007-11-20 13:15:52 +0200720 int rc;
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200721 unsigned size;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200722 unsigned long linear;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200723 int cur_size = ctxt->fetch.end - ctxt->fetch.data;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200724 struct segmented_address addr = { .seg = VCPU_SREG_CS,
Paolo Bonzini17052f12014-05-06 16:33:01 +0200725 .ea = ctxt->eip + cur_size };
726
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200727 size = 15UL ^ cur_size;
728 rc = __linearize(ctxt, addr, size, false, true, &linear);
729 if (unlikely(rc != X86EMUL_CONTINUE))
730 return rc;
731
732 size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear));
Paolo Bonzini5cfc7e02014-05-06 13:05:25 +0200733
734 /*
735 * One instruction can only straddle two pages,
736 * and one has been loaded at the beginning of
737 * x86_decode_insn. So, if not enough bytes
738 * still, we must have hit the 15-byte boundary.
739 */
740 if (unlikely(size < op_size))
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200741 return X86EMUL_UNHANDLEABLE;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200742 rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end,
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200743 size, &ctxt->exception);
744 if (unlikely(rc != X86EMUL_CONTINUE))
745 return rc;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200746 ctxt->fetch.end += size;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900747 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200748}
749
Paolo Bonzini9506d572014-05-06 13:05:25 +0200750static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt,
751 unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200752{
Paolo Bonzini17052f12014-05-06 16:33:01 +0200753 if (unlikely(ctxt->fetch.end - ctxt->fetch.ptr < size))
Paolo Bonzini9506d572014-05-06 13:05:25 +0200754 return __do_insn_fetch_bytes(ctxt, size);
755 else
756 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200757}
758
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900759/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900760#define insn_fetch(_type, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200761({ _type _x; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200762 \
763 rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900764 if (rc != X86EMUL_CONTINUE) \
765 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200766 ctxt->_eip += sizeof(_type); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200767 _x = *(_type __aligned(1) *) ctxt->fetch.ptr; \
768 ctxt->fetch.ptr += sizeof(_type); \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200769 _x; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900770})
771
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900772#define insn_fetch_arr(_arr, _size, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200773({ \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200774 rc = do_insn_fetch_bytes(_ctxt, _size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900775 if (rc != X86EMUL_CONTINUE) \
776 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200777 ctxt->_eip += (_size); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200778 memcpy(_arr, ctxt->fetch.ptr, _size); \
779 ctxt->fetch.ptr += (_size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900780})
781
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000782/*
783 * Given the 'reg' portion of a ModRM byte, and a register block, return a
784 * pointer into the block that addresses the relevant register.
785 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
786 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300787static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200788 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789{
790 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200791 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800792
Avi Kivity6aa8b732006-12-10 02:21:36 -0800793 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300794 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
795 else
796 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800797 return p;
798}
799
800static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200801 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800802 u16 *size, unsigned long *address, int op_bytes)
803{
804 int rc;
805
806 if (op_bytes == 2)
807 op_bytes = 3;
808 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200809 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900810 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200812 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200813 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800814 return rc;
815}
816
Avi Kivity34b77652013-01-19 19:51:56 +0200817FASTOP2(add);
818FASTOP2(or);
819FASTOP2(adc);
820FASTOP2(sbb);
821FASTOP2(and);
822FASTOP2(sub);
823FASTOP2(xor);
824FASTOP2(cmp);
825FASTOP2(test);
826
Avi Kivityb9fa4092013-02-09 11:31:48 +0200827FASTOP1SRC2(mul, mul_ex);
828FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200829FASTOP1SRC2EX(div, div_ex);
830FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +0200831
Avi Kivity34b77652013-01-19 19:51:56 +0200832FASTOP3WCL(shld);
833FASTOP3WCL(shrd);
834
835FASTOP2W(imul);
836
837FASTOP1(not);
838FASTOP1(neg);
839FASTOP1(inc);
840FASTOP1(dec);
841
842FASTOP2CL(rol);
843FASTOP2CL(ror);
844FASTOP2CL(rcl);
845FASTOP2CL(rcr);
846FASTOP2CL(shl);
847FASTOP2CL(shr);
848FASTOP2CL(sar);
849
850FASTOP2W(bsf);
851FASTOP2W(bsr);
852FASTOP2W(bt);
853FASTOP2W(bts);
854FASTOP2W(btr);
855FASTOP2W(btc);
856
Avi Kivitye47a5f52013-02-09 11:31:51 +0200857FASTOP2(xadd);
858
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200859static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300860{
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200861 u8 rc;
862 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300863
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200864 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +0200865 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200866 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
867 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300868}
869
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300870static void fetch_register_operand(struct operand *op)
871{
872 switch (op->bytes) {
873 case 1:
874 op->val = *(u8 *)op->addr.reg;
875 break;
876 case 2:
877 op->val = *(u16 *)op->addr.reg;
878 break;
879 case 4:
880 op->val = *(u32 *)op->addr.reg;
881 break;
882 case 8:
883 op->val = *(u64 *)op->addr.reg;
884 break;
885 }
886}
887
Avi Kivity12537912011-03-29 11:41:27 +0200888static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
889{
890 ctxt->ops->get_fpu(ctxt);
891 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200892 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
893 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
894 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
895 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
896 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
897 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
898 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
899 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200900#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200901 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
902 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
903 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
904 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
905 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
906 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
907 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
908 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200909#endif
910 default: BUG();
911 }
912 ctxt->ops->put_fpu(ctxt);
913}
914
915static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
916 int reg)
917{
918 ctxt->ops->get_fpu(ctxt);
919 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200920 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
921 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
922 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
923 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
924 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
925 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
926 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
927 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200928#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200929 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
930 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
931 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
932 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
933 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
934 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
935 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
936 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200937#endif
938 default: BUG();
939 }
940 ctxt->ops->put_fpu(ctxt);
941}
942
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300943static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
944{
945 ctxt->ops->get_fpu(ctxt);
946 switch (reg) {
947 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
948 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
949 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
950 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
951 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
952 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
953 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
954 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
955 default: BUG();
956 }
957 ctxt->ops->put_fpu(ctxt);
958}
959
960static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
961{
962 ctxt->ops->get_fpu(ctxt);
963 switch (reg) {
964 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
965 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
966 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
967 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
968 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
969 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
970 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
971 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
972 default: BUG();
973 }
974 ctxt->ops->put_fpu(ctxt);
975}
976
Gleb Natapov045a2822012-12-20 16:57:43 +0200977static int em_fninit(struct x86_emulate_ctxt *ctxt)
978{
979 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
980 return emulate_nm(ctxt);
981
982 ctxt->ops->get_fpu(ctxt);
983 asm volatile("fninit");
984 ctxt->ops->put_fpu(ctxt);
985 return X86EMUL_CONTINUE;
986}
987
988static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
989{
990 u16 fcw;
991
992 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
993 return emulate_nm(ctxt);
994
995 ctxt->ops->get_fpu(ctxt);
996 asm volatile("fnstcw %0": "+m"(fcw));
997 ctxt->ops->put_fpu(ctxt);
998
999 /* force 2 byte destination */
1000 ctxt->dst.bytes = 2;
1001 ctxt->dst.val = fcw;
1002
1003 return X86EMUL_CONTINUE;
1004}
1005
1006static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1007{
1008 u16 fsw;
1009
1010 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1011 return emulate_nm(ctxt);
1012
1013 ctxt->ops->get_fpu(ctxt);
1014 asm volatile("fnstsw %0": "+m"(fsw));
1015 ctxt->ops->put_fpu(ctxt);
1016
1017 /* force 2 byte destination */
1018 ctxt->dst.bytes = 2;
1019 ctxt->dst.val = fsw;
1020
1021 return X86EMUL_CONTINUE;
1022}
1023
Avi Kivity12537912011-03-29 11:41:27 +02001024static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001025 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001026{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001027 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001028
Avi Kivity9dac77f2011-06-01 15:34:25 +03001029 if (!(ctxt->d & ModRM))
1030 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +02001031
Avi Kivity9dac77f2011-06-01 15:34:25 +03001032 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001033 op->type = OP_XMM;
1034 op->bytes = 16;
1035 op->addr.xmm = reg;
1036 read_sse_reg(ctxt, &op->vec_val, reg);
1037 return;
1038 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001039 if (ctxt->d & Mmx) {
1040 reg &= 7;
1041 op->type = OP_MM;
1042 op->bytes = 8;
1043 op->addr.mm = reg;
1044 return;
1045 }
Avi Kivity12537912011-03-29 11:41:27 +02001046
Avi Kivity3c118e22007-10-31 10:27:04 +02001047 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001048 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1049 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1050
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001051 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001052 op->orig_val = op->val;
1053}
1054
Avi Kivitya6e34072012-06-10 17:15:39 +03001055static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1056{
1057 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1058 ctxt->modrm_seg = VCPU_SREG_SS;
1059}
1060
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001061static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001062 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001063{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001064 u8 sib;
Bandan Das02357bd2014-04-16 12:46:11 -04001065 int index_reg, base_reg, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001066 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001067 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001068
Bandan Das02357bd2014-04-16 12:46:11 -04001069 ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */
1070 index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */
1071 base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001072
Bandan Das02357bd2014-04-16 12:46:11 -04001073 ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001074 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
Bandan Das02357bd2014-04-16 12:46:11 -04001075 ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001076 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001077
Nadav Amit9b88ae92014-05-25 23:05:21 +03001078 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001079 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001080 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001081 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001082 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001083 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001084 op->type = OP_XMM;
1085 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001086 op->addr.xmm = ctxt->modrm_rm;
1087 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001088 return rc;
1089 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001090 if (ctxt->d & Mmx) {
1091 op->type = OP_MM;
1092 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001093 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001094 return rc;
1095 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001096 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001097 return rc;
1098 }
1099
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001100 op->type = OP_MEM;
1101
Avi Kivity9dac77f2011-06-01 15:34:25 +03001102 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001103 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1104 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1105 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1106 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001107
1108 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001109 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001110 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001111 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001112 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001113 break;
1114 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001115 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001116 break;
1117 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001118 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001119 break;
1120 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001121 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001122 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001123 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001124 break;
1125 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001126 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001127 break;
1128 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001129 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001130 break;
1131 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001132 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001133 break;
1134 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001135 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001136 break;
1137 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001138 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001139 break;
1140 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001141 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001142 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001143 break;
1144 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001145 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001146 break;
1147 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001148 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1149 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1150 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001151 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001152 } else {
1153 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001154 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001155 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001156 index_reg |= (sib >> 3) & 7;
1157 base_reg |= sib & 7;
1158 scale = sib >> 6;
1159
Avi Kivity9dac77f2011-06-01 15:34:25 +03001160 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001161 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001162 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001163 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001164 adjust_modrm_seg(ctxt, base_reg);
1165 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001166 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001167 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001168 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001169 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001170 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001171 } else {
1172 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001173 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001174 adjust_modrm_seg(ctxt, base_reg);
1175 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001176 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001177 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001178 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001179 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001180 break;
1181 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001182 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001183 break;
1184 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001185 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001186 break;
1187 }
1188 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001189 op->addr.mem.ea = modrm_ea;
Bandan Das41061cd2014-04-16 12:46:14 -04001190 if (ctxt->ad_bytes != 8)
1191 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
1192
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001193done:
1194 return rc;
1195}
1196
1197static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001198 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001199{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001200 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001201
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001202 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001203 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001204 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001205 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001206 break;
1207 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001208 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001209 break;
1210 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001211 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001212 break;
1213 }
1214done:
1215 return rc;
1216}
1217
Avi Kivity9dac77f2011-06-01 15:34:25 +03001218static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001219{
Sheng Yang7129eec2010-09-28 16:33:32 +08001220 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001221
Avi Kivity9dac77f2011-06-01 15:34:25 +03001222 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001223 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001224
Avi Kivity9dac77f2011-06-01 15:34:25 +03001225 if (ctxt->src.bytes == 2)
1226 sv = (s16)ctxt->src.val & (s16)mask;
1227 else if (ctxt->src.bytes == 4)
1228 sv = (s32)ctxt->src.val & (s32)mask;
Nadav Amit7dec5602014-06-15 16:12:57 +03001229 else
1230 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001231
Avi Kivity9dac77f2011-06-01 15:34:25 +03001232 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001233 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001234
1235 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001236 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001237}
1238
Gleb Natapov9de41572010-04-28 19:15:22 +03001239static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001240 unsigned long addr, void *dest, unsigned size)
1241{
1242 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001243 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001244
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001245 if (mc->pos < mc->end)
1246 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001247
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001248 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001249
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001250 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1251 &ctxt->exception);
1252 if (rc != X86EMUL_CONTINUE)
1253 return rc;
1254
1255 mc->end += size;
1256
1257read_cached:
1258 memcpy(dest, mc->data + mc->pos, size);
1259 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001260 return X86EMUL_CONTINUE;
1261}
1262
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001263static int segmented_read(struct x86_emulate_ctxt *ctxt,
1264 struct segmented_address addr,
1265 void *data,
1266 unsigned size)
1267{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001268 int rc;
1269 ulong linear;
1270
Avi Kivity83b87952011-04-03 11:31:19 +03001271 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001272 if (rc != X86EMUL_CONTINUE)
1273 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001274 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001275}
1276
1277static int segmented_write(struct x86_emulate_ctxt *ctxt,
1278 struct segmented_address addr,
1279 const void *data,
1280 unsigned size)
1281{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001282 int rc;
1283 ulong linear;
1284
Avi Kivity83b87952011-04-03 11:31:19 +03001285 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001286 if (rc != X86EMUL_CONTINUE)
1287 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001288 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1289 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001290}
1291
1292static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1293 struct segmented_address addr,
1294 const void *orig_data, const void *data,
1295 unsigned size)
1296{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001297 int rc;
1298 ulong linear;
1299
Avi Kivity83b87952011-04-03 11:31:19 +03001300 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001301 if (rc != X86EMUL_CONTINUE)
1302 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001303 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1304 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001305}
1306
Gleb Natapov7b262e92010-03-18 15:20:27 +02001307static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001308 unsigned int size, unsigned short port,
1309 void *dest)
1310{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001311 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001312
1313 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001314 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001315 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001316 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001317 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001318 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1319 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Mark Rustadb55a8142014-07-25 06:27:05 -07001320 n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001321 if (n == 0)
1322 n = 1;
1323 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001324 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001325 return 0;
1326 rc->end = n * size;
1327 }
1328
Nadav Amite6e39f02014-04-18 03:35:10 +03001329 if (ctxt->rep_prefix && (ctxt->d & String) &&
1330 !(ctxt->eflags & EFLG_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001331 ctxt->dst.data = rc->data + rc->pos;
1332 ctxt->dst.type = OP_MEM_STR;
1333 ctxt->dst.count = (rc->end - rc->pos) / size;
1334 rc->pos = rc->end;
1335 } else {
1336 memcpy(dest, rc->data + rc->pos, size);
1337 rc->pos += size;
1338 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001339 return 1;
1340}
1341
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001342static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1343 u16 index, struct desc_struct *desc)
1344{
1345 struct desc_ptr dt;
1346 ulong addr;
1347
1348 ctxt->ops->get_idt(ctxt, &dt);
1349
1350 if (dt.size < index * 8 + 7)
1351 return emulate_gp(ctxt, index << 3 | 0x2);
1352
1353 addr = dt.address + index * 8;
1354 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1355 &ctxt->exception);
1356}
1357
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001358static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001359 u16 selector, struct desc_ptr *dt)
1360{
Mathias Krause0225fb52012-08-30 01:30:16 +02001361 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001362 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001363
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001364 if (selector & 1 << 2) {
1365 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001366 u16 sel;
1367
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001368 memset (dt, 0, sizeof *dt);
Nadav Amit2eedcac2014-06-02 18:34:05 +03001369 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1370 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001371 return;
1372
1373 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001374 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001375 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001376 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001377}
1378
1379/* allowed just for 8 bytes segments */
1380static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001381 u16 selector, struct desc_struct *desc,
1382 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001383{
1384 struct desc_ptr dt;
1385 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001386 ulong addr;
1387
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001388 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001389
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001390 if (dt.size < index * 8 + 7)
1391 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001392
Avi Kivitye9194642012-06-13 16:29:39 +03001393 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001394 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1395 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001396}
1397
1398/* allowed just for 8 bytes segments */
1399static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001400 u16 selector, struct desc_struct *desc)
1401{
1402 struct desc_ptr dt;
1403 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001404 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001405
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001406 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001407
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001408 if (dt.size < index * 8 + 7)
1409 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001410
1411 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001412 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1413 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001414}
1415
Gleb Natapov5601d052011-03-07 14:55:06 +02001416/* Does not support long mode */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001417static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Paolo Bonzini5045b462014-05-15 18:09:29 +02001418 u16 selector, int seg, u8 cpl, bool in_task_switch)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001419{
Avi Kivity869be992012-06-13 16:30:53 +03001420 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001421 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001422 unsigned err_vec = GP_VECTOR;
1423 u32 err_code = 0;
1424 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001425 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001426 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001427 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001428 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001429
1430 memset(&seg_desc, 0, sizeof seg_desc);
1431
Kevin Wolff8da94e2013-04-11 14:06:03 +02001432 if (ctxt->mode == X86EMUL_MODE_REAL) {
1433 /* set real mode segment descriptor (keep limit etc. for
1434 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001435 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001436 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001437 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001438 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1439 /* VM86 needs a clean new segment descriptor */
1440 set_desc_base(&seg_desc, selector << 4);
1441 set_desc_limit(&seg_desc, 0xffff);
1442 seg_desc.type = 3;
1443 seg_desc.p = 1;
1444 seg_desc.s = 1;
1445 seg_desc.dpl = 3;
1446 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001447 }
1448
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001449 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001450
1451 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1452 if ((seg == VCPU_SREG_CS
1453 || (seg == VCPU_SREG_SS
1454 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1455 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001456 && null_selector)
1457 goto exception;
1458
1459 /* TR should be in GDT only */
1460 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1461 goto exception;
1462
1463 if (null_selector) /* for NULL selector skip all following checks */
1464 goto load;
1465
Avi Kivitye9194642012-06-13 16:29:39 +03001466 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001467 if (ret != X86EMUL_CONTINUE)
1468 return ret;
1469
1470 err_code = selector & 0xfffc;
1471 err_vec = GP_VECTOR;
1472
Guo Chaofc058682012-06-28 15:19:51 +08001473 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001474 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1475 goto exception;
1476
1477 if (!seg_desc.p) {
1478 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1479 goto exception;
1480 }
1481
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001482 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001483
1484 switch (seg) {
1485 case VCPU_SREG_SS:
1486 /*
1487 * segment is not a writable data segment or segment
1488 * selector's RPL != CPL or segment selector's RPL != CPL
1489 */
1490 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1491 goto exception;
1492 break;
1493 case VCPU_SREG_CS:
Paolo Bonzini5045b462014-05-15 18:09:29 +02001494 if (in_task_switch && rpl != dpl)
1495 goto exception;
1496
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001497 if (!(seg_desc.type & 8))
1498 goto exception;
1499
1500 if (seg_desc.type & 4) {
1501 /* conforming */
1502 if (dpl > cpl)
1503 goto exception;
1504 } else {
1505 /* nonconforming */
1506 if (rpl > cpl || dpl != cpl)
1507 goto exception;
1508 }
1509 /* CS(RPL) <- CPL */
1510 selector = (selector & 0xfffc) | cpl;
1511 break;
1512 case VCPU_SREG_TR:
1513 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1514 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001515 old_desc = seg_desc;
1516 seg_desc.type |= 2; /* busy */
1517 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1518 sizeof(seg_desc), &ctxt->exception);
1519 if (ret != X86EMUL_CONTINUE)
1520 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001521 break;
1522 case VCPU_SREG_LDTR:
1523 if (seg_desc.s || seg_desc.type != 2)
1524 goto exception;
1525 break;
1526 default: /* DS, ES, FS, or GS */
1527 /*
1528 * segment is not a data or readable code segment or
1529 * ((segment is a data or nonconforming code segment)
1530 * and (both RPL and CPL > DPL))
1531 */
1532 if ((seg_desc.type & 0xa) == 0x8 ||
1533 (((seg_desc.type & 0xc) != 0xc) &&
1534 (rpl > dpl && cpl > dpl)))
1535 goto exception;
1536 break;
1537 }
1538
1539 if (seg_desc.s) {
1540 /* mark segment as accessed */
1541 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001542 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001543 if (ret != X86EMUL_CONTINUE)
1544 return ret;
Nadav Amite37a75a2014-06-02 18:34:04 +03001545 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
1546 ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3,
1547 sizeof(base3), &ctxt->exception);
1548 if (ret != X86EMUL_CONTINUE)
1549 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001550 }
1551load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001552 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001553 return X86EMUL_CONTINUE;
1554exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001555 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001556 return X86EMUL_PROPAGATE_FAULT;
1557}
1558
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001559static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1560 u16 selector, int seg)
1561{
1562 u8 cpl = ctxt->ops->cpl(ctxt);
Paolo Bonzini5045b462014-05-15 18:09:29 +02001563 return __load_segment_descriptor(ctxt, selector, seg, cpl, false);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001564}
1565
Wei Yongjun31be40b2010-08-17 09:17:30 +08001566static void write_register_operand(struct operand *op)
1567{
1568 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1569 switch (op->bytes) {
1570 case 1:
1571 *(u8 *)op->addr.reg = (u8)op->val;
1572 break;
1573 case 2:
1574 *(u16 *)op->addr.reg = (u16)op->val;
1575 break;
1576 case 4:
1577 *op->addr.reg = (u32)op->val;
1578 break; /* 64b: zero-extend */
1579 case 8:
1580 *op->addr.reg = op->val;
1581 break;
1582 }
1583}
1584
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001585static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001586{
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001587 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001588 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001589 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001590 break;
1591 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001592 if (ctxt->lock_prefix)
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001593 return segmented_cmpxchg(ctxt,
1594 op->addr.mem,
1595 &op->orig_val,
1596 &op->val,
1597 op->bytes);
1598 else
1599 return segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001600 op->addr.mem,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001601 &op->val,
1602 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001603 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001604 case OP_MEM_STR:
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001605 return segmented_write(ctxt,
1606 op->addr.mem,
1607 op->data,
1608 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001609 break;
Avi Kivity12537912011-03-29 11:41:27 +02001610 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001611 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001612 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001613 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001614 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001615 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001616 case OP_NONE:
1617 /* no writeback */
1618 break;
1619 default:
1620 break;
1621 }
1622 return X86EMUL_CONTINUE;
1623}
1624
Avi Kivity51ddff52012-06-12 20:19:40 +03001625static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001626{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001627 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001628
Avi Kivity5ad105e2012-08-19 14:34:31 +03001629 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001630 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001631 addr.seg = VCPU_SREG_SS;
1632
Avi Kivity51ddff52012-06-12 20:19:40 +03001633 return segmented_write(ctxt, addr, data, bytes);
1634}
1635
1636static int em_push(struct x86_emulate_ctxt *ctxt)
1637{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001638 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001639 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001640 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001641}
1642
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001643static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001644 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001645{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001646 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001647 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001648
Avi Kivitydd856ef2012-08-27 23:46:17 +03001649 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001650 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001651 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001652 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001653 return rc;
1654
Avi Kivity5ad105e2012-08-19 14:34:31 +03001655 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001656 return rc;
1657}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001658
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001659static int em_pop(struct x86_emulate_ctxt *ctxt)
1660{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001661 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001662}
1663
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001664static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001665 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001666{
1667 int rc;
1668 unsigned long val, change_mask;
1669 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001670 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001671
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001672 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001673 if (rc != X86EMUL_CONTINUE)
1674 return rc;
1675
1676 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
Nadav Amit163b1352014-07-21 14:37:28 +03001677 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_AC | EFLG_ID;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001678
1679 switch(ctxt->mode) {
1680 case X86EMUL_MODE_PROT64:
1681 case X86EMUL_MODE_PROT32:
1682 case X86EMUL_MODE_PROT16:
1683 if (cpl == 0)
1684 change_mask |= EFLG_IOPL;
1685 if (cpl <= iopl)
1686 change_mask |= EFLG_IF;
1687 break;
1688 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001689 if (iopl < 3)
1690 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001691 change_mask |= EFLG_IF;
1692 break;
1693 default: /* real mode */
1694 change_mask |= (EFLG_IOPL | EFLG_IF);
1695 break;
1696 }
1697
1698 *(unsigned long *)dest =
1699 (ctxt->eflags & ~change_mask) | (val & change_mask);
1700
1701 return rc;
1702}
1703
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001704static int em_popf(struct x86_emulate_ctxt *ctxt)
1705{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001706 ctxt->dst.type = OP_REG;
1707 ctxt->dst.addr.reg = &ctxt->eflags;
1708 ctxt->dst.bytes = ctxt->op_bytes;
1709 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001710}
1711
Avi Kivity612e89f2012-06-12 20:03:23 +03001712static int em_enter(struct x86_emulate_ctxt *ctxt)
1713{
1714 int rc;
1715 unsigned frame_size = ctxt->src.val;
1716 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001717 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001718
1719 if (nesting_level)
1720 return X86EMUL_UNHANDLEABLE;
1721
Avi Kivitydd856ef2012-08-27 23:46:17 +03001722 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1723 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001724 if (rc != X86EMUL_CONTINUE)
1725 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001726 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001727 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001728 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1729 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001730 stack_mask(ctxt));
1731 return X86EMUL_CONTINUE;
1732}
1733
Avi Kivityf47cfa32012-06-07 17:49:24 +03001734static int em_leave(struct x86_emulate_ctxt *ctxt)
1735{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001736 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001737 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001738 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001739}
1740
Avi Kivity1cd196e2011-09-13 10:45:51 +03001741static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001742{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001743 int seg = ctxt->src2.val;
1744
Avi Kivity9dac77f2011-06-01 15:34:25 +03001745 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001746
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001747 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001748}
1749
Avi Kivity1cd196e2011-09-13 10:45:51 +03001750static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001751{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001752 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001753 unsigned long selector;
1754 int rc;
1755
Avi Kivity9dac77f2011-06-01 15:34:25 +03001756 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001757 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001758 return rc;
1759
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001760 if (ctxt->modrm_reg == VCPU_SREG_SS)
1761 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
1762
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001763 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001764 return rc;
1765}
1766
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001767static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001768{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001769 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001770 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001771 int reg = VCPU_REGS_RAX;
1772
1773 while (reg <= VCPU_REGS_RDI) {
1774 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001775 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001776
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001777 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001778 if (rc != X86EMUL_CONTINUE)
1779 return rc;
1780
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001781 ++reg;
1782 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001783
Wei Yongjunc37eda12010-06-15 09:03:33 +08001784 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001785}
1786
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001787static int em_pushf(struct x86_emulate_ctxt *ctxt)
1788{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001789 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001790 return em_push(ctxt);
1791}
1792
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001793static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001794{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001795 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001796 int reg = VCPU_REGS_RDI;
1797
1798 while (reg >= VCPU_REGS_RAX) {
1799 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001800 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001801 --reg;
1802 }
1803
Avi Kivitydd856ef2012-08-27 23:46:17 +03001804 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001805 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001806 break;
1807 --reg;
1808 }
1809 return rc;
1810}
1811
Avi Kivitydd856ef2012-08-27 23:46:17 +03001812static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001813{
Mathias Krause0225fb52012-08-30 01:30:16 +02001814 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001815 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001816 struct desc_ptr dt;
1817 gva_t cs_addr;
1818 gva_t eip_addr;
1819 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001820
1821 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001822 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001823 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001824 if (rc != X86EMUL_CONTINUE)
1825 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001826
1827 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1828
Avi Kivity9dac77f2011-06-01 15:34:25 +03001829 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001830 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001831 if (rc != X86EMUL_CONTINUE)
1832 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001833
Avi Kivity9dac77f2011-06-01 15:34:25 +03001834 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001835 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001836 if (rc != X86EMUL_CONTINUE)
1837 return rc;
1838
Avi Kivity4bff1e862011-04-20 13:37:53 +03001839 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001840
1841 eip_addr = dt.address + (irq << 2);
1842 cs_addr = dt.address + (irq << 2) + 2;
1843
Avi Kivity0f65dd72011-04-20 13:37:53 +03001844 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001845 if (rc != X86EMUL_CONTINUE)
1846 return rc;
1847
Avi Kivity0f65dd72011-04-20 13:37:53 +03001848 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001849 if (rc != X86EMUL_CONTINUE)
1850 return rc;
1851
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001852 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001853 if (rc != X86EMUL_CONTINUE)
1854 return rc;
1855
Avi Kivity9dac77f2011-06-01 15:34:25 +03001856 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001857
1858 return rc;
1859}
1860
Avi Kivitydd856ef2012-08-27 23:46:17 +03001861int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1862{
1863 int rc;
1864
1865 invalidate_registers(ctxt);
1866 rc = __emulate_int_real(ctxt, irq);
1867 if (rc == X86EMUL_CONTINUE)
1868 writeback_registers(ctxt);
1869 return rc;
1870}
1871
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001872static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001873{
1874 switch(ctxt->mode) {
1875 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001876 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001877 case X86EMUL_MODE_VM86:
1878 case X86EMUL_MODE_PROT16:
1879 case X86EMUL_MODE_PROT32:
1880 case X86EMUL_MODE_PROT64:
1881 default:
1882 /* Protected mode interrupts unimplemented yet */
1883 return X86EMUL_UNHANDLEABLE;
1884 }
1885}
1886
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001887static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001888{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001889 int rc = X86EMUL_CONTINUE;
1890 unsigned long temp_eip = 0;
1891 unsigned long temp_eflags = 0;
1892 unsigned long cs = 0;
1893 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1894 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1895 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1896 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1897
1898 /* TODO: Add stack limit check */
1899
Avi Kivity9dac77f2011-06-01 15:34:25 +03001900 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001901
1902 if (rc != X86EMUL_CONTINUE)
1903 return rc;
1904
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001905 if (temp_eip & ~0xffff)
1906 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001907
Avi Kivity9dac77f2011-06-01 15:34:25 +03001908 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001909
1910 if (rc != X86EMUL_CONTINUE)
1911 return rc;
1912
Avi Kivity9dac77f2011-06-01 15:34:25 +03001913 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001914
1915 if (rc != X86EMUL_CONTINUE)
1916 return rc;
1917
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001918 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001919
1920 if (rc != X86EMUL_CONTINUE)
1921 return rc;
1922
Avi Kivity9dac77f2011-06-01 15:34:25 +03001923 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001924
1925
Avi Kivity9dac77f2011-06-01 15:34:25 +03001926 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001927 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001928 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001929 ctxt->eflags &= ~0xffff;
1930 ctxt->eflags |= temp_eflags;
1931 }
1932
1933 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1934 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1935
1936 return rc;
1937}
1938
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001939static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001940{
1941 switch(ctxt->mode) {
1942 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001943 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001944 case X86EMUL_MODE_VM86:
1945 case X86EMUL_MODE_PROT16:
1946 case X86EMUL_MODE_PROT32:
1947 case X86EMUL_MODE_PROT64:
1948 default:
1949 /* iret from protected mode unimplemented yet */
1950 return X86EMUL_UNHANDLEABLE;
1951 }
1952}
1953
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001954static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1955{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001956 int rc;
1957 unsigned short sel;
1958
Avi Kivity9dac77f2011-06-01 15:34:25 +03001959 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001960
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001961 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001962 if (rc != X86EMUL_CONTINUE)
1963 return rc;
1964
Avi Kivity9dac77f2011-06-01 15:34:25 +03001965 ctxt->_eip = 0;
1966 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001967 return X86EMUL_CONTINUE;
1968}
1969
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001970static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001971{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001972 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001973
Avi Kivity9dac77f2011-06-01 15:34:25 +03001974 switch (ctxt->modrm_reg) {
Mohammed Gamald19292e2008-09-08 21:47:19 +03001975 case 2: /* call near abs */ {
1976 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001977 old_eip = ctxt->_eip;
1978 ctxt->_eip = ctxt->src.val;
1979 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001980 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001981 break;
1982 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001983 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001984 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001985 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001986 case 5: /* jmp far */
1987 rc = em_jmp_far(ctxt);
1988 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001989 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001990 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001991 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001992 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001993 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001994}
1995
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09001996static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001997{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001998 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001999
Nadav Amitaaa05f22014-06-02 18:34:10 +03002000 if (ctxt->dst.bytes == 16)
2001 return X86EMUL_UNHANDLEABLE;
2002
Avi Kivitydd856ef2012-08-27 23:46:17 +03002003 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2004 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2005 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2006 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002007 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002008 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002009 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2010 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002011
Laurent Vivier05f086f2007-09-24 11:10:55 +02002012 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002013 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002014 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002015}
2016
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002017static int em_ret(struct x86_emulate_ctxt *ctxt)
2018{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002019 ctxt->dst.type = OP_REG;
2020 ctxt->dst.addr.reg = &ctxt->_eip;
2021 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002022 return em_pop(ctxt);
2023}
2024
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002025static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002026{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002027 int rc;
2028 unsigned long cs;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002029 int cpl = ctxt->ops->cpl(ctxt);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002030
Avi Kivity9dac77f2011-06-01 15:34:25 +03002031 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002032 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002033 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002034 if (ctxt->op_bytes == 4)
2035 ctxt->_eip = (u32)ctxt->_eip;
2036 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002037 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002038 return rc;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002039 /* Outer-privilege level return is not implemented */
2040 if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2041 return X86EMUL_UNHANDLEABLE;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002042 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002043 return rc;
2044}
2045
Bruce Rogers32611072013-09-09 09:40:20 -06002046static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2047{
2048 int rc;
2049
2050 rc = em_ret_far(ctxt);
2051 if (rc != X86EMUL_CONTINUE)
2052 return rc;
2053 rsp_increment(ctxt, ctxt->src.val);
2054 return X86EMUL_CONTINUE;
2055}
2056
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002057static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2058{
2059 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002060 ctxt->dst.orig_val = ctxt->dst.val;
2061 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002062 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002063 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002064 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002065
2066 if (ctxt->eflags & EFLG_ZF) {
2067 /* Success: write back to memory. */
2068 ctxt->dst.val = ctxt->src.orig_val;
2069 } else {
2070 /* Failure: write the value we saw to EAX. */
2071 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002072 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Nadav Amit37c564f2014-06-02 18:34:07 +03002073 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002074 }
2075 return X86EMUL_CONTINUE;
2076}
2077
Avi Kivityd4b43252011-09-13 10:45:50 +03002078static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002079{
Avi Kivityd4b43252011-09-13 10:45:50 +03002080 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002081 unsigned short sel;
2082 int rc;
2083
Avi Kivity9dac77f2011-06-01 15:34:25 +03002084 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002085
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002086 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002087 if (rc != X86EMUL_CONTINUE)
2088 return rc;
2089
Avi Kivity9dac77f2011-06-01 15:34:25 +03002090 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002091 return rc;
2092}
2093
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002094static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002095setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002096 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002097{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002098 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002099 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002100 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002101 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002102 cs->type = 0x0b; /* Read, Execute, Accessed */
2103 cs->s = 1;
2104 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002105 cs->p = 1;
2106 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002107 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002108
Gleb Natapov79168fd2010-04-28 19:15:30 +03002109 set_desc_base(ss, 0); /* flat segment */
2110 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002111 ss->g = 1; /* 4kb granularity */
2112 ss->s = 1;
2113 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002114 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002115 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002116 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002117 ss->l = 0;
2118 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002119}
2120
Avi Kivity1a18a692012-02-01 12:23:21 +02002121static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2122{
2123 u32 eax, ebx, ecx, edx;
2124
2125 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002126 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2127 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002128 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2129 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2130}
2131
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002132static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2133{
Mathias Krause0225fb52012-08-30 01:30:16 +02002134 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002135 u32 eax, ebx, ecx, edx;
2136
2137 /*
2138 * syscall should always be enabled in longmode - so only become
2139 * vendor specific (cpuid) if other modes are active...
2140 */
2141 if (ctxt->mode == X86EMUL_MODE_PROT64)
2142 return true;
2143
2144 eax = 0x00000000;
2145 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002146 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2147 /*
2148 * Intel ("GenuineIntel")
2149 * remark: Intel CPUs only support "syscall" in 64bit
2150 * longmode. Also an 64bit guest with a
2151 * 32bit compat-app running will #UD !! While this
2152 * behaviour can be fixed (by emulating) into AMD
2153 * response - CPUs of AMD can't behave like Intel.
2154 */
2155 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2156 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2157 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2158 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002159
Avi Kivity0017f932012-06-07 14:10:16 +03002160 /* AMD ("AuthenticAMD") */
2161 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2162 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2163 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2164 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002165
Avi Kivity0017f932012-06-07 14:10:16 +03002166 /* AMD ("AMDisbetter!") */
2167 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2168 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2169 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2170 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002171
2172 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2173 return false;
2174}
2175
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002176static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002177{
Mathias Krause0225fb52012-08-30 01:30:16 +02002178 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002179 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002180 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002181 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002182 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002183
2184 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002185 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002186 ctxt->mode == X86EMUL_MODE_VM86)
2187 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002188
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002189 if (!(em_syscall_is_enabled(ctxt)))
2190 return emulate_ud(ctxt);
2191
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002192 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002193 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002194
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002195 if (!(efer & EFER_SCE))
2196 return emulate_ud(ctxt);
2197
Avi Kivity717746e2011-04-20 13:37:53 +03002198 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002199 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002200 cs_sel = (u16)(msr_data & 0xfffc);
2201 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002202
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002203 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002204 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002205 cs.l = 1;
2206 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002207 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2208 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002209
Avi Kivitydd856ef2012-08-27 23:46:17 +03002210 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002211 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002212#ifdef CONFIG_X86_64
Nadav Amit6c6cb692014-07-21 14:37:30 +03002213 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002214
Avi Kivity717746e2011-04-20 13:37:53 +03002215 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002216 ctxt->mode == X86EMUL_MODE_PROT64 ?
2217 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002218 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002219
Avi Kivity717746e2011-04-20 13:37:53 +03002220 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Nadav Amit6c6cb692014-07-21 14:37:30 +03002221 ctxt->eflags &= ~msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002222#endif
2223 } else {
2224 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002225 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002226 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002227
Nadav Amit6c6cb692014-07-21 14:37:30 +03002228 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002229 }
2230
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002231 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002232}
2233
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002234static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002235{
Mathias Krause0225fb52012-08-30 01:30:16 +02002236 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002237 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002238 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002239 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002240 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002241
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002242 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002243 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002244 if (ctxt->mode == X86EMUL_MODE_REAL)
2245 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002246
Avi Kivity1a18a692012-02-01 12:23:21 +02002247 /*
2248 * Not recognized on AMD in compat mode (but is recognized in legacy
2249 * mode).
2250 */
2251 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2252 && !vendor_intel(ctxt))
2253 return emulate_ud(ctxt);
2254
Andre Przywara8c604352009-06-18 12:56:01 +02002255 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2256 * Therefore, we inject an #UD.
2257 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002258 if (ctxt->mode == X86EMUL_MODE_PROT64)
2259 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002260
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002261 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002262
Avi Kivity717746e2011-04-20 13:37:53 +03002263 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002264 switch (ctxt->mode) {
2265 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002266 if ((msr_data & 0xfffc) == 0x0)
2267 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002268 break;
2269 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002270 if (msr_data == 0x0)
2271 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002272 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002273 default:
2274 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002275 }
2276
Nadav Amit6c6cb692014-07-21 14:37:30 +03002277 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002278 cs_sel = (u16)msr_data;
2279 cs_sel &= ~SELECTOR_RPL_MASK;
2280 ss_sel = cs_sel + 8;
2281 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002282 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002283 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002284 cs.l = 1;
2285 }
2286
Avi Kivity1aa36612011-04-27 13:20:30 +03002287 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2288 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002289
Avi Kivity717746e2011-04-20 13:37:53 +03002290 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002291 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002292
Avi Kivity717746e2011-04-20 13:37:53 +03002293 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002294 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002295
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002296 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002297}
2298
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002299static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002300{
Mathias Krause0225fb52012-08-30 01:30:16 +02002301 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002302 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002303 u64 msr_data;
2304 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002305 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002306
Gleb Natapova0044752010-02-10 14:21:31 +02002307 /* inject #GP if in real mode or Virtual 8086 mode */
2308 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002309 ctxt->mode == X86EMUL_MODE_VM86)
2310 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002311
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002312 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002313
Avi Kivity9dac77f2011-06-01 15:34:25 +03002314 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002315 usermode = X86EMUL_MODE_PROT64;
2316 else
2317 usermode = X86EMUL_MODE_PROT32;
2318
2319 cs.dpl = 3;
2320 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002321 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002322 switch (usermode) {
2323 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002324 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002325 if ((msr_data & 0xfffc) == 0x0)
2326 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002327 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002328 break;
2329 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002330 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002331 if (msr_data == 0x0)
2332 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002333 ss_sel = cs_sel + 8;
2334 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002335 cs.l = 1;
2336 break;
2337 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002338 cs_sel |= SELECTOR_RPL_MASK;
2339 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002340
Avi Kivity1aa36612011-04-27 13:20:30 +03002341 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2342 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002343
Avi Kivitydd856ef2012-08-27 23:46:17 +03002344 ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX);
2345 *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX);
Andre Przywara4668f052009-06-18 12:56:02 +02002346
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002347 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002348}
2349
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002350static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002351{
2352 int iopl;
2353 if (ctxt->mode == X86EMUL_MODE_REAL)
2354 return false;
2355 if (ctxt->mode == X86EMUL_MODE_VM86)
2356 return true;
2357 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002358 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002359}
2360
2361static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002362 u16 port, u16 len)
2363{
Mathias Krause0225fb52012-08-30 01:30:16 +02002364 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002365 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002366 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002367 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002368 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002369 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002370 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002371
Avi Kivity1aa36612011-04-27 13:20:30 +03002372 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002373 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002374 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002375 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002376 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002377 base = get_desc_base(&tr_seg);
2378#ifdef CONFIG_X86_64
2379 base |= ((u64)base3) << 32;
2380#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002381 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002382 if (r != X86EMUL_CONTINUE)
2383 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002384 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002385 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002386 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002387 if (r != X86EMUL_CONTINUE)
2388 return false;
2389 if ((perm >> bit_idx) & mask)
2390 return false;
2391 return true;
2392}
2393
2394static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002395 u16 port, u16 len)
2396{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002397 if (ctxt->perm_ok)
2398 return true;
2399
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002400 if (emulator_bad_iopl(ctxt))
2401 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002402 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002403
2404 ctxt->perm_ok = true;
2405
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002406 return true;
2407}
2408
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002409static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002410 struct tss_segment_16 *tss)
2411{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002412 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002413 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002414 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2415 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2416 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2417 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2418 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2419 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2420 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2421 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002422
Avi Kivity1aa36612011-04-27 13:20:30 +03002423 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2424 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2425 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2426 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2427 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002428}
2429
2430static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002431 struct tss_segment_16 *tss)
2432{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002433 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002434 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002435
Avi Kivity9dac77f2011-06-01 15:34:25 +03002436 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002437 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002438 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2439 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2440 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2441 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2442 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2443 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2444 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2445 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002446
2447 /*
2448 * SDM says that segment selectors are loaded before segment
2449 * descriptors
2450 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002451 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2452 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2453 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2454 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2455 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002456
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002457 cpl = tss->cs & 3;
2458
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002459 /*
Guo Chaofc058682012-06-28 15:19:51 +08002460 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002461 * it is handled in a context of new task
2462 */
Paolo Bonzini5045b462014-05-15 18:09:29 +02002463 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, 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->es, VCPU_SREG_ES, 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->cs, VCPU_SREG_CS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002470 if (ret != X86EMUL_CONTINUE)
2471 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002472 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002473 if (ret != X86EMUL_CONTINUE)
2474 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002475 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002476 if (ret != X86EMUL_CONTINUE)
2477 return ret;
2478
2479 return X86EMUL_CONTINUE;
2480}
2481
2482static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002483 u16 tss_selector, u16 old_tss_sel,
2484 ulong old_tss_base, struct desc_struct *new_desc)
2485{
Mathias Krause0225fb52012-08-30 01:30:16 +02002486 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002487 struct tss_segment_16 tss_seg;
2488 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002489 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002490
Avi Kivity0f65dd72011-04-20 13:37:53 +03002491 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002492 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002493 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002494 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002495 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002496
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002497 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002498
Avi Kivity0f65dd72011-04-20 13:37:53 +03002499 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002500 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002501 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002502 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002503 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002504
Avi Kivity0f65dd72011-04-20 13:37:53 +03002505 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002506 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002507 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002508 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002509 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002510
2511 if (old_tss_sel != 0xffff) {
2512 tss_seg.prev_task_link = old_tss_sel;
2513
Avi Kivity0f65dd72011-04-20 13:37:53 +03002514 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002515 &tss_seg.prev_task_link,
2516 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002517 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002518 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002519 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002520 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002521 }
2522
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002523 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002524}
2525
2526static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002527 struct tss_segment_32 *tss)
2528{
Nadav Amit5c7411e2014-04-07 18:37:47 +03002529 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002530 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002531 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002532 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2533 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2534 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2535 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2536 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2537 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2538 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2539 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002540
Avi Kivity1aa36612011-04-27 13:20:30 +03002541 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2542 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2543 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2544 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2545 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2546 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002547}
2548
2549static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002550 struct tss_segment_32 *tss)
2551{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002552 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002553 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002554
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002555 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002556 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002557 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002558 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002559
2560 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002561 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2562 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2563 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2564 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2565 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2566 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2567 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2568 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002569
2570 /*
2571 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002572 * descriptors. This is important because CPL checks will
2573 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002574 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002575 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2576 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2577 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2578 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2579 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2580 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2581 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002582
2583 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002584 * If we're switching between Protected Mode and VM86, we need to make
2585 * sure to update the mode before loading the segment descriptors so
2586 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01002587 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002588 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002589 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002590 cpl = 3;
2591 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002592 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002593 cpl = tss->cs & 3;
2594 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01002595
2596 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002597 * Now load segment descriptors. If fault happenes at this stage
2598 * it is handled in a context of new task
2599 */
Paolo Bonzini5045b462014-05-15 18:09:29 +02002600 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR, 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->es, VCPU_SREG_ES, 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->cs, VCPU_SREG_CS, 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->ss, VCPU_SREG_SS, 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->ds, VCPU_SREG_DS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002613 if (ret != X86EMUL_CONTINUE)
2614 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002615 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002616 if (ret != X86EMUL_CONTINUE)
2617 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002618 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002619 if (ret != X86EMUL_CONTINUE)
2620 return ret;
2621
2622 return X86EMUL_CONTINUE;
2623}
2624
2625static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002626 u16 tss_selector, u16 old_tss_sel,
2627 ulong old_tss_base, struct desc_struct *new_desc)
2628{
Mathias Krause0225fb52012-08-30 01:30:16 +02002629 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002630 struct tss_segment_32 tss_seg;
2631 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002632 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03002633 u32 eip_offset = offsetof(struct tss_segment_32, eip);
2634 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002635
Avi Kivity0f65dd72011-04-20 13:37:53 +03002636 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002637 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002638 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002639 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002640 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002641
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002642 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002643
Nadav Amit5c7411e2014-04-07 18:37:47 +03002644 /* Only GP registers and segment selectors are saved */
2645 ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
2646 ldt_sel_offset - eip_offset, &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
Avi Kivity0f65dd72011-04-20 13:37:53 +03002651 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002652 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002653 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002654 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002655 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002656
2657 if (old_tss_sel != 0xffff) {
2658 tss_seg.prev_task_link = old_tss_sel;
2659
Avi Kivity0f65dd72011-04-20 13:37:53 +03002660 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002661 &tss_seg.prev_task_link,
2662 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002663 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002664 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002665 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002666 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002667 }
2668
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002669 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002670}
2671
2672static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002673 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002674 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002675{
Mathias Krause0225fb52012-08-30 01:30:16 +02002676 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002677 struct desc_struct curr_tss_desc, next_tss_desc;
2678 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002679 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002680 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002681 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002682 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002683 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002684
2685 /* FIXME: old_tss_base == ~0 ? */
2686
Avi Kivitye9194642012-06-13 16:29:39 +03002687 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002688 if (ret != X86EMUL_CONTINUE)
2689 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002690 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002691 if (ret != X86EMUL_CONTINUE)
2692 return ret;
2693
2694 /* FIXME: check that next_tss_desc is tss */
2695
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002696 /*
2697 * Check privileges. The three cases are task switch caused by...
2698 *
2699 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2700 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002701 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002702 */
2703 if (reason == TASK_SWITCH_GATE) {
2704 if (idt_index != -1) {
2705 /* Software interrupts */
2706 struct desc_struct task_gate_desc;
2707 int dpl;
2708
2709 ret = read_interrupt_descriptor(ctxt, idt_index,
2710 &task_gate_desc);
2711 if (ret != X86EMUL_CONTINUE)
2712 return ret;
2713
2714 dpl = task_gate_desc.dpl;
2715 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2716 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2717 }
2718 } else if (reason != TASK_SWITCH_IRET) {
2719 int dpl = next_tss_desc.dpl;
2720 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2721 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002722 }
2723
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002724
Gleb Natapovceffb452010-03-18 15:20:19 +02002725 desc_limit = desc_limit_scaled(&next_tss_desc);
2726 if (!next_tss_desc.p ||
2727 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2728 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002729 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002730 return X86EMUL_PROPAGATE_FAULT;
2731 }
2732
2733 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2734 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002735 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002736 }
2737
2738 if (reason == TASK_SWITCH_IRET)
2739 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2740
2741 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002742 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002743 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2744 old_tss_sel = 0xffff;
2745
2746 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002747 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002748 old_tss_base, &next_tss_desc);
2749 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002750 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002751 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002752 if (ret != X86EMUL_CONTINUE)
2753 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002754
2755 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2756 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2757
2758 if (reason != TASK_SWITCH_IRET) {
2759 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002760 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002761 }
2762
Avi Kivity717746e2011-04-20 13:37:53 +03002763 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002764 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002765
Jan Kiszkae269fb22010-04-14 15:51:09 +02002766 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002767 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2768 ctxt->lock_prefix = 0;
2769 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002770 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002771 }
2772
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002773 return ret;
2774}
2775
2776int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002777 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002778 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002779{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002780 int rc;
2781
Avi Kivitydd856ef2012-08-27 23:46:17 +03002782 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002783 ctxt->_eip = ctxt->eip;
2784 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002785
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002786 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002787 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002788
Avi Kivitydd856ef2012-08-27 23:46:17 +03002789 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002790 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002791 writeback_registers(ctxt);
2792 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002793
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002794 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002795}
2796
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002797static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2798 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002799{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002800 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002801
Avi Kivitydd856ef2012-08-27 23:46:17 +03002802 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2803 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002804}
2805
Avi Kivity7af04fc2010-08-18 14:16:35 +03002806static int em_das(struct x86_emulate_ctxt *ctxt)
2807{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002808 u8 al, old_al;
2809 bool af, cf, old_cf;
2810
2811 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002812 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002813
2814 old_al = al;
2815 old_cf = cf;
2816 cf = false;
2817 af = ctxt->eflags & X86_EFLAGS_AF;
2818 if ((al & 0x0f) > 9 || af) {
2819 al -= 6;
2820 cf = old_cf | (al >= 250);
2821 af = true;
2822 } else {
2823 af = false;
2824 }
2825 if (old_al > 0x99 || old_cf) {
2826 al -= 0x60;
2827 cf = true;
2828 }
2829
Avi Kivity9dac77f2011-06-01 15:34:25 +03002830 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002831 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002832 ctxt->src.type = OP_IMM;
2833 ctxt->src.val = 0;
2834 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02002835 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03002836 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2837 if (cf)
2838 ctxt->eflags |= X86_EFLAGS_CF;
2839 if (af)
2840 ctxt->eflags |= X86_EFLAGS_AF;
2841 return X86EMUL_CONTINUE;
2842}
2843
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02002844static int em_aam(struct x86_emulate_ctxt *ctxt)
2845{
2846 u8 al, ah;
2847
2848 if (ctxt->src.val == 0)
2849 return emulate_de(ctxt);
2850
2851 al = ctxt->dst.val & 0xff;
2852 ah = al / ctxt->src.val;
2853 al %= ctxt->src.val;
2854
2855 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
2856
2857 /* Set PF, ZF, SF */
2858 ctxt->src.type = OP_IMM;
2859 ctxt->src.val = 0;
2860 ctxt->src.bytes = 1;
2861 fastop(ctxt, em_or);
2862
2863 return X86EMUL_CONTINUE;
2864}
2865
Gleb Natapov7f662272012-12-10 11:42:30 +02002866static int em_aad(struct x86_emulate_ctxt *ctxt)
2867{
2868 u8 al = ctxt->dst.val & 0xff;
2869 u8 ah = (ctxt->dst.val >> 8) & 0xff;
2870
2871 al = (al + (ah * ctxt->src.val)) & 0xff;
2872
2873 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
2874
Gleb Natapovf583c292013-02-13 17:50:39 +02002875 /* Set PF, ZF, SF */
2876 ctxt->src.type = OP_IMM;
2877 ctxt->src.val = 0;
2878 ctxt->src.bytes = 1;
2879 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02002880
2881 return X86EMUL_CONTINUE;
2882}
2883
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002884static int em_call(struct x86_emulate_ctxt *ctxt)
2885{
2886 long rel = ctxt->src.val;
2887
2888 ctxt->src.val = (unsigned long)ctxt->_eip;
2889 jmp_rel(ctxt, rel);
2890 return em_push(ctxt);
2891}
2892
Avi Kivity0ef753b2010-08-18 14:51:45 +03002893static int em_call_far(struct x86_emulate_ctxt *ctxt)
2894{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002895 u16 sel, old_cs;
2896 ulong old_eip;
2897 int rc;
2898
Avi Kivity1aa36612011-04-27 13:20:30 +03002899 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002900 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002901
Avi Kivity9dac77f2011-06-01 15:34:25 +03002902 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002903 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002904 return X86EMUL_CONTINUE;
2905
Avi Kivity9dac77f2011-06-01 15:34:25 +03002906 ctxt->_eip = 0;
2907 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002908
Avi Kivity9dac77f2011-06-01 15:34:25 +03002909 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002910 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002911 if (rc != X86EMUL_CONTINUE)
2912 return rc;
2913
Avi Kivity9dac77f2011-06-01 15:34:25 +03002914 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002915 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002916}
2917
Avi Kivity40ece7c2010-08-18 15:12:09 +03002918static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2919{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002920 int rc;
2921
Avi Kivity9dac77f2011-06-01 15:34:25 +03002922 ctxt->dst.type = OP_REG;
2923 ctxt->dst.addr.reg = &ctxt->_eip;
2924 ctxt->dst.bytes = ctxt->op_bytes;
2925 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002926 if (rc != X86EMUL_CONTINUE)
2927 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03002928 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002929 return X86EMUL_CONTINUE;
2930}
2931
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002932static int em_xchg(struct x86_emulate_ctxt *ctxt)
2933{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002934 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002935 ctxt->src.val = ctxt->dst.val;
2936 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002937
2938 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002939 ctxt->dst.val = ctxt->src.orig_val;
2940 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002941 return X86EMUL_CONTINUE;
2942}
2943
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002944static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2945{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002946 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02002947 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002948}
2949
Avi Kivity61429142010-08-19 15:13:00 +03002950static int em_cwd(struct x86_emulate_ctxt *ctxt)
2951{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002952 ctxt->dst.type = OP_REG;
2953 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002954 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002955 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002956
2957 return X86EMUL_CONTINUE;
2958}
2959
Avi Kivity48bb5d32010-08-18 18:54:34 +03002960static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2961{
Avi Kivity48bb5d32010-08-18 18:54:34 +03002962 u64 tsc = 0;
2963
Avi Kivity717746e2011-04-20 13:37:53 +03002964 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002965 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
2966 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03002967 return X86EMUL_CONTINUE;
2968}
2969
Avi Kivity222d21a2011-11-10 14:57:30 +02002970static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
2971{
2972 u64 pmc;
2973
Avi Kivitydd856ef2012-08-27 23:46:17 +03002974 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02002975 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002976 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
2977 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02002978 return X86EMUL_CONTINUE;
2979}
2980
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002981static int em_mov(struct x86_emulate_ctxt *ctxt)
2982{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01002983 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002984 return X86EMUL_CONTINUE;
2985}
2986
Borislav Petkov84cffe42013-10-29 12:54:56 +01002987#define FFL(x) bit(X86_FEATURE_##x)
2988
2989static int em_movbe(struct x86_emulate_ctxt *ctxt)
2990{
2991 u32 ebx, ecx, edx, eax = 1;
2992 u16 tmp;
2993
2994 /*
2995 * Check MOVBE is set in the guest-visible CPUID leaf.
2996 */
2997 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2998 if (!(ecx & FFL(MOVBE)))
2999 return emulate_ud(ctxt);
3000
3001 switch (ctxt->op_bytes) {
3002 case 2:
3003 /*
3004 * From MOVBE definition: "...When the operand size is 16 bits,
3005 * the upper word of the destination register remains unchanged
3006 * ..."
3007 *
3008 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3009 * rules so we have to do the operation almost per hand.
3010 */
3011 tmp = (u16)ctxt->src.val;
3012 ctxt->dst.val &= ~0xffffUL;
3013 ctxt->dst.val |= (unsigned long)swab16(tmp);
3014 break;
3015 case 4:
3016 ctxt->dst.val = swab32((u32)ctxt->src.val);
3017 break;
3018 case 8:
3019 ctxt->dst.val = swab64(ctxt->src.val);
3020 break;
3021 default:
3022 return X86EMUL_PROPAGATE_FAULT;
3023 }
3024 return X86EMUL_CONTINUE;
3025}
3026
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003027static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3028{
3029 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3030 return emulate_gp(ctxt, 0);
3031
3032 /* Disable writeback. */
3033 ctxt->dst.type = OP_NONE;
3034 return X86EMUL_CONTINUE;
3035}
3036
3037static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3038{
3039 unsigned long val;
3040
3041 if (ctxt->mode == X86EMUL_MODE_PROT64)
3042 val = ctxt->src.val & ~0ULL;
3043 else
3044 val = ctxt->src.val & ~0U;
3045
3046 /* #UD condition is already handled. */
3047 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3048 return emulate_gp(ctxt, 0);
3049
3050 /* Disable writeback. */
3051 ctxt->dst.type = OP_NONE;
3052 return X86EMUL_CONTINUE;
3053}
3054
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003055static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3056{
3057 u64 msr_data;
3058
Avi Kivitydd856ef2012-08-27 23:46:17 +03003059 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3060 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3061 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003062 return emulate_gp(ctxt, 0);
3063
3064 return X86EMUL_CONTINUE;
3065}
3066
3067static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3068{
3069 u64 msr_data;
3070
Avi Kivitydd856ef2012-08-27 23:46:17 +03003071 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003072 return emulate_gp(ctxt, 0);
3073
Avi Kivitydd856ef2012-08-27 23:46:17 +03003074 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3075 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003076 return X86EMUL_CONTINUE;
3077}
3078
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003079static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3080{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003081 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003082 return emulate_ud(ctxt);
3083
Avi Kivity9dac77f2011-06-01 15:34:25 +03003084 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003085 return X86EMUL_CONTINUE;
3086}
3087
3088static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3089{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003090 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003091
Avi Kivity9dac77f2011-06-01 15:34:25 +03003092 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003093 return emulate_ud(ctxt);
3094
Avi Kivity9dac77f2011-06-01 15:34:25 +03003095 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003096 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3097
3098 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003099 ctxt->dst.type = OP_NONE;
3100 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003101}
3102
Avi Kivitya14e5792012-06-13 12:28:33 +03003103static int em_lldt(struct x86_emulate_ctxt *ctxt)
3104{
3105 u16 sel = ctxt->src.val;
3106
3107 /* Disable writeback. */
3108 ctxt->dst.type = OP_NONE;
3109 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3110}
3111
Avi Kivity80890002012-06-13 16:33:29 +03003112static int em_ltr(struct x86_emulate_ctxt *ctxt)
3113{
3114 u16 sel = ctxt->src.val;
3115
3116 /* Disable writeback. */
3117 ctxt->dst.type = OP_NONE;
3118 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3119}
3120
Avi Kivity38503912011-03-31 18:48:09 +02003121static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3122{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003123 int rc;
3124 ulong linear;
3125
Avi Kivity9dac77f2011-06-01 15:34:25 +03003126 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003127 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003128 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003129 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003130 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003131 return X86EMUL_CONTINUE;
3132}
3133
Avi Kivity2d04a052011-04-20 15:32:49 +03003134static int em_clts(struct x86_emulate_ctxt *ctxt)
3135{
3136 ulong cr0;
3137
3138 cr0 = ctxt->ops->get_cr(ctxt, 0);
3139 cr0 &= ~X86_CR0_TS;
3140 ctxt->ops->set_cr(ctxt, 0, cr0);
3141 return X86EMUL_CONTINUE;
3142}
3143
Avi Kivity26d05cc2011-04-21 12:07:59 +03003144static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3145{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003146 int rc;
3147
Avi Kivity9dac77f2011-06-01 15:34:25 +03003148 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003149 return X86EMUL_UNHANDLEABLE;
3150
3151 rc = ctxt->ops->fix_hypercall(ctxt);
3152 if (rc != X86EMUL_CONTINUE)
3153 return rc;
3154
3155 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003156 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003157 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003158 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003159 return X86EMUL_CONTINUE;
3160}
3161
Avi Kivity96051572012-06-10 17:21:18 +03003162static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3163 void (*get)(struct x86_emulate_ctxt *ctxt,
3164 struct desc_ptr *ptr))
3165{
3166 struct desc_ptr desc_ptr;
3167
3168 if (ctxt->mode == X86EMUL_MODE_PROT64)
3169 ctxt->op_bytes = 8;
3170 get(ctxt, &desc_ptr);
3171 if (ctxt->op_bytes == 2) {
3172 ctxt->op_bytes = 4;
3173 desc_ptr.address &= 0x00ffffff;
3174 }
3175 /* Disable writeback. */
3176 ctxt->dst.type = OP_NONE;
3177 return segmented_write(ctxt, ctxt->dst.addr.mem,
3178 &desc_ptr, 2 + ctxt->op_bytes);
3179}
3180
3181static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3182{
3183 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3184}
3185
3186static int em_sidt(struct x86_emulate_ctxt *ctxt)
3187{
3188 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3189}
3190
Avi Kivity26d05cc2011-04-21 12:07:59 +03003191static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3192{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003193 struct desc_ptr desc_ptr;
3194 int rc;
3195
Avi Kivity510425f2012-06-07 17:04:36 +03003196 if (ctxt->mode == X86EMUL_MODE_PROT64)
3197 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003198 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003199 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003200 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003201 if (rc != X86EMUL_CONTINUE)
3202 return rc;
3203 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3204 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003205 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003206 return X86EMUL_CONTINUE;
3207}
3208
Avi Kivity5ef39c72011-04-21 12:21:50 +03003209static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003210{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003211 int rc;
3212
Avi Kivity5ef39c72011-04-21 12:21:50 +03003213 rc = ctxt->ops->fix_hypercall(ctxt);
3214
Avi Kivity26d05cc2011-04-21 12:07:59 +03003215 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003216 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003217 return rc;
3218}
3219
3220static int em_lidt(struct x86_emulate_ctxt *ctxt)
3221{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003222 struct desc_ptr desc_ptr;
3223 int rc;
3224
Avi Kivity510425f2012-06-07 17:04:36 +03003225 if (ctxt->mode == X86EMUL_MODE_PROT64)
3226 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003227 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003228 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003229 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003230 if (rc != X86EMUL_CONTINUE)
3231 return rc;
3232 ctxt->ops->set_idt(ctxt, &desc_ptr);
3233 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003234 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003235 return X86EMUL_CONTINUE;
3236}
3237
3238static int em_smsw(struct x86_emulate_ctxt *ctxt)
3239{
Nadav Amit32e94d02014-06-02 18:34:11 +03003240 if (ctxt->dst.type == OP_MEM)
3241 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003242 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003243 return X86EMUL_CONTINUE;
3244}
3245
3246static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3247{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003248 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003249 | (ctxt->src.val & 0x0f));
3250 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003251 return X86EMUL_CONTINUE;
3252}
3253
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003254static int em_loop(struct x86_emulate_ctxt *ctxt)
3255{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003256 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3257 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003258 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3259 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003260
3261 return X86EMUL_CONTINUE;
3262}
3263
3264static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3265{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003266 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003267 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003268
3269 return X86EMUL_CONTINUE;
3270}
3271
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003272static int em_in(struct x86_emulate_ctxt *ctxt)
3273{
3274 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3275 &ctxt->dst.val))
3276 return X86EMUL_IO_NEEDED;
3277
3278 return X86EMUL_CONTINUE;
3279}
3280
3281static int em_out(struct x86_emulate_ctxt *ctxt)
3282{
3283 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3284 &ctxt->src.val, 1);
3285 /* Disable writeback. */
3286 ctxt->dst.type = OP_NONE;
3287 return X86EMUL_CONTINUE;
3288}
3289
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003290static int em_cli(struct x86_emulate_ctxt *ctxt)
3291{
3292 if (emulator_bad_iopl(ctxt))
3293 return emulate_gp(ctxt, 0);
3294
3295 ctxt->eflags &= ~X86_EFLAGS_IF;
3296 return X86EMUL_CONTINUE;
3297}
3298
3299static int em_sti(struct x86_emulate_ctxt *ctxt)
3300{
3301 if (emulator_bad_iopl(ctxt))
3302 return emulate_gp(ctxt, 0);
3303
3304 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3305 ctxt->eflags |= X86_EFLAGS_IF;
3306 return X86EMUL_CONTINUE;
3307}
3308
Avi Kivity6d6eede2012-06-07 14:11:36 +03003309static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3310{
3311 u32 eax, ebx, ecx, edx;
3312
Avi Kivitydd856ef2012-08-27 23:46:17 +03003313 eax = reg_read(ctxt, VCPU_REGS_RAX);
3314 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003315 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003316 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3317 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3318 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3319 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003320 return X86EMUL_CONTINUE;
3321}
3322
Paolo Bonzini98f73632013-10-31 11:19:42 +01003323static int em_sahf(struct x86_emulate_ctxt *ctxt)
3324{
3325 u32 flags;
3326
3327 flags = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF;
3328 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3329
3330 ctxt->eflags &= ~0xffUL;
3331 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3332 return X86EMUL_CONTINUE;
3333}
3334
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003335static int em_lahf(struct x86_emulate_ctxt *ctxt)
3336{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003337 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3338 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003339 return X86EMUL_CONTINUE;
3340}
3341
Avi Kivity92998362012-06-13 12:25:06 +03003342static int em_bswap(struct x86_emulate_ctxt *ctxt)
3343{
3344 switch (ctxt->op_bytes) {
3345#ifdef CONFIG_X86_64
3346 case 8:
3347 asm("bswap %0" : "+r"(ctxt->dst.val));
3348 break;
3349#endif
3350 default:
3351 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3352 break;
3353 }
3354 return X86EMUL_CONTINUE;
3355}
3356
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003357static bool valid_cr(int nr)
3358{
3359 switch (nr) {
3360 case 0:
3361 case 2 ... 4:
3362 case 8:
3363 return true;
3364 default:
3365 return false;
3366 }
3367}
3368
3369static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3370{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003371 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003372 return emulate_ud(ctxt);
3373
3374 return X86EMUL_CONTINUE;
3375}
3376
3377static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3378{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003379 u64 new_val = ctxt->src.val64;
3380 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003381 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003382
3383 static u64 cr_reserved_bits[] = {
3384 0xffffffff00000000ULL,
3385 0, 0, 0, /* CR3 checked later */
3386 CR4_RESERVED_BITS,
3387 0, 0, 0,
3388 CR8_RESERVED_BITS,
3389 };
3390
3391 if (!valid_cr(cr))
3392 return emulate_ud(ctxt);
3393
3394 if (new_val & cr_reserved_bits[cr])
3395 return emulate_gp(ctxt, 0);
3396
3397 switch (cr) {
3398 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003399 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003400 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3401 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3402 return emulate_gp(ctxt, 0);
3403
Avi Kivity717746e2011-04-20 13:37:53 +03003404 cr4 = ctxt->ops->get_cr(ctxt, 4);
3405 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003406
3407 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3408 !(cr4 & X86_CR4_PAE))
3409 return emulate_gp(ctxt, 0);
3410
3411 break;
3412 }
3413 case 3: {
3414 u64 rsvd = 0;
3415
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003416 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3417 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003418 rsvd = CR3_L_MODE_RESERVED_BITS;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003419
3420 if (new_val & rsvd)
3421 return emulate_gp(ctxt, 0);
3422
3423 break;
3424 }
3425 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003426 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003427
3428 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3429 return emulate_gp(ctxt, 0);
3430
3431 break;
3432 }
3433 }
3434
3435 return X86EMUL_CONTINUE;
3436}
3437
Joerg Roedel3b88e412011-04-04 12:39:29 +02003438static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3439{
3440 unsigned long dr7;
3441
Avi Kivity717746e2011-04-20 13:37:53 +03003442 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003443
3444 /* Check if DR7.Global_Enable is set */
3445 return dr7 & (1 << 13);
3446}
3447
3448static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3449{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003450 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003451 u64 cr4;
3452
3453 if (dr > 7)
3454 return emulate_ud(ctxt);
3455
Avi Kivity717746e2011-04-20 13:37:53 +03003456 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003457 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3458 return emulate_ud(ctxt);
3459
3460 if (check_dr7_gd(ctxt))
3461 return emulate_db(ctxt);
3462
3463 return X86EMUL_CONTINUE;
3464}
3465
3466static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3467{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003468 u64 new_val = ctxt->src.val64;
3469 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003470
3471 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3472 return emulate_gp(ctxt, 0);
3473
3474 return check_dr_read(ctxt);
3475}
3476
Joerg Roedel01de8b02011-04-04 12:39:31 +02003477static int check_svme(struct x86_emulate_ctxt *ctxt)
3478{
3479 u64 efer;
3480
Avi Kivity717746e2011-04-20 13:37:53 +03003481 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003482
3483 if (!(efer & EFER_SVME))
3484 return emulate_ud(ctxt);
3485
3486 return X86EMUL_CONTINUE;
3487}
3488
3489static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3490{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003491 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003492
3493 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003494 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003495 return emulate_gp(ctxt, 0);
3496
3497 return check_svme(ctxt);
3498}
3499
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003500static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3501{
Avi Kivity717746e2011-04-20 13:37:53 +03003502 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003503
Avi Kivity717746e2011-04-20 13:37:53 +03003504 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003505 return emulate_ud(ctxt);
3506
3507 return X86EMUL_CONTINUE;
3508}
3509
Joerg Roedel80612522011-04-04 12:39:33 +02003510static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3511{
Avi Kivity717746e2011-04-20 13:37:53 +03003512 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003513 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003514
Avi Kivity717746e2011-04-20 13:37:53 +03003515 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03003516 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02003517 return emulate_gp(ctxt, 0);
3518
3519 return X86EMUL_CONTINUE;
3520}
3521
Joerg Roedelf6511932011-04-04 12:39:35 +02003522static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3523{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003524 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3525 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003526 return emulate_gp(ctxt, 0);
3527
3528 return X86EMUL_CONTINUE;
3529}
3530
3531static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3532{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003533 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3534 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003535 return emulate_gp(ctxt, 0);
3536
3537 return X86EMUL_CONTINUE;
3538}
3539
Avi Kivity73fba5f2010-07-29 15:11:53 +03003540#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003541#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
3542#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
3543 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003544#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003545#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003546#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3547#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003548#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003549#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003550#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003551#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003552 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003553#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003554 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
3555 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003556#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003557
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003558#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003559#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003560#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003561#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003562#define I2bvIP(_f, _e, _i, _p) \
3563 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003564
Avi Kivityfb864fb2013-01-04 16:18:54 +02003565#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3566 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3567 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003568
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003569static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003570 DI(SrcNone | Priv, monitor),
3571 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003572 N, N, N, N, N, N,
3573};
3574
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003575static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003576 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003577 II(SrcNone | Prot | EmulateOnUD, em_vmmcall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003578 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3579 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3580 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3581 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3582 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3583 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003584};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003585
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003586static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003587 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003588 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003589 N, N, N, N, N, N,
3590};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003591
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003592static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003593 F(Lock, em_add),
3594 F(Lock | PageTable, em_or),
3595 F(Lock, em_adc),
3596 F(Lock, em_sbb),
3597 F(Lock | PageTable, em_and),
3598 F(Lock, em_sub),
3599 F(Lock, em_xor),
3600 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003601};
3602
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003603static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003604 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003605};
3606
Avi Kivity007a3b52013-01-19 19:51:51 +02003607static const struct opcode group2[] = {
3608 F(DstMem | ModRM, em_rol),
3609 F(DstMem | ModRM, em_ror),
3610 F(DstMem | ModRM, em_rcl),
3611 F(DstMem | ModRM, em_rcr),
3612 F(DstMem | ModRM, em_shl),
3613 F(DstMem | ModRM, em_shr),
3614 F(DstMem | ModRM, em_shl),
3615 F(DstMem | ModRM, em_sar),
3616};
3617
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003618static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003619 F(DstMem | SrcImm | NoWrite, em_test),
3620 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003621 F(DstMem | SrcNone | Lock, em_not),
3622 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02003623 F(DstXacc | Src2Mem, em_mul_ex),
3624 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02003625 F(DstXacc | Src2Mem, em_div_ex),
3626 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003627};
3628
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003629static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003630 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3631 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003632 N, N, N, N, N, N,
3633};
3634
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003635static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003636 F(DstMem | SrcNone | Lock, em_inc),
3637 F(DstMem | SrcNone | Lock, em_dec),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003638 I(SrcMem | Stack, em_grp45),
3639 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3640 I(SrcMem | Stack, em_grp45),
3641 I(SrcMemFAddr | ImplicitOps, em_grp45),
Gleb Natapov188424b2013-04-11 12:32:14 +03003642 I(SrcMem | Stack, em_grp45), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003643};
3644
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003645static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003646 DI(Prot, sldt),
3647 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003648 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003649 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003650 N, N, N, N,
3651};
3652
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003653static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03003654 II(Mov | DstMem, em_sgdt, sgdt),
3655 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003656 II(SrcMem | Priv, em_lgdt, lgdt),
3657 II(SrcMem | Priv, em_lidt, lidt),
3658 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3659 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3660 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003661}, {
Borislav Petkovb51e9742013-09-22 16:44:52 +02003662 I(SrcNone | Priv | EmulateOnUD, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003663 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003664 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003665 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3666 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3667 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003668} };
3669
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003670static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003671 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003672 F(DstMem | SrcImmByte | NoWrite, em_bt),
3673 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3674 F(DstMem | SrcImmByte | Lock, em_btr),
3675 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003676};
3677
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003678static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003679 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003680}, {
3681 N, N, N, N, N, N, N, N,
3682} };
3683
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003684static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003685 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003686 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003687};
3688
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003689static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003690 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003691};
3692
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003693static const struct gprefix pfx_vmovntpx = {
Avi Kivity3e114eb2012-04-09 18:40:01 +03003694 I(0, em_mov), N, N, N,
3695};
3696
Igor Mammedov27ce8252014-03-15 21:01:59 +01003697static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01003698 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003699};
3700
Gleb Natapov045a2822012-12-20 16:57:43 +02003701static const struct escape escape_d9 = { {
3702 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3703}, {
3704 /* 0xC0 - 0xC7 */
3705 N, N, N, N, N, N, N, N,
3706 /* 0xC8 - 0xCF */
3707 N, N, N, N, N, N, N, N,
3708 /* 0xD0 - 0xC7 */
3709 N, N, N, N, N, N, N, N,
3710 /* 0xD8 - 0xDF */
3711 N, N, N, N, N, N, N, N,
3712 /* 0xE0 - 0xE7 */
3713 N, N, N, N, N, N, N, N,
3714 /* 0xE8 - 0xEF */
3715 N, N, N, N, N, N, N, N,
3716 /* 0xF0 - 0xF7 */
3717 N, N, N, N, N, N, N, N,
3718 /* 0xF8 - 0xFF */
3719 N, N, N, N, N, N, N, N,
3720} };
3721
3722static const struct escape escape_db = { {
3723 N, N, N, N, N, N, N, N,
3724}, {
3725 /* 0xC0 - 0xC7 */
3726 N, N, N, N, N, N, N, N,
3727 /* 0xC8 - 0xCF */
3728 N, N, N, N, N, N, N, N,
3729 /* 0xD0 - 0xC7 */
3730 N, N, N, N, N, N, N, N,
3731 /* 0xD8 - 0xDF */
3732 N, N, N, N, N, N, N, N,
3733 /* 0xE0 - 0xE7 */
3734 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3735 /* 0xE8 - 0xEF */
3736 N, N, N, N, N, N, N, N,
3737 /* 0xF0 - 0xF7 */
3738 N, N, N, N, N, N, N, N,
3739 /* 0xF8 - 0xFF */
3740 N, N, N, N, N, N, N, N,
3741} };
3742
3743static const struct escape escape_dd = { {
3744 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3745}, {
3746 /* 0xC0 - 0xC7 */
3747 N, N, N, N, N, N, N, N,
3748 /* 0xC8 - 0xCF */
3749 N, N, N, N, N, N, N, N,
3750 /* 0xD0 - 0xC7 */
3751 N, N, N, N, N, N, N, N,
3752 /* 0xD8 - 0xDF */
3753 N, N, N, N, N, N, N, N,
3754 /* 0xE0 - 0xE7 */
3755 N, N, N, N, N, N, N, N,
3756 /* 0xE8 - 0xEF */
3757 N, N, N, N, N, N, N, N,
3758 /* 0xF0 - 0xF7 */
3759 N, N, N, N, N, N, N, N,
3760 /* 0xF8 - 0xFF */
3761 N, N, N, N, N, N, N, N,
3762} };
3763
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003764static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003765 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003766 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003767 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3768 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003769 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003770 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003771 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3772 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003773 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003774 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003775 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3776 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003777 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003778 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003779 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3780 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003781 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003782 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003783 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003784 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003785 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003786 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003787 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003788 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003789 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02003790 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003791 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003792 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003793 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003794 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003795 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003796 I(ImplicitOps | Stack | No64, em_pusha),
3797 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003798 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3799 N, N, N, N,
3800 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003801 I(SrcImm | Mov | Stack, em_push),
3802 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003803 I(SrcImmByte | Mov | Stack, em_push),
3804 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003805 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003806 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003807 /* 0x70 - 0x7F */
3808 X16(D(SrcImmByte)),
3809 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003810 G(ByteOp | DstMem | SrcImm, group1),
3811 G(DstMem | SrcImm, group1),
3812 G(ByteOp | DstMem | SrcImm | No64, group1),
3813 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003814 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003815 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003816 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003817 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003818 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003819 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003820 D(ModRM | SrcMem | NoAccess | DstReg),
3821 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3822 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003823 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003824 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003825 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003826 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003827 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003828 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01003829 II(ImplicitOps | Stack, em_popf, popf),
3830 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003831 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003832 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003833 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003834 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003835 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003836 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003837 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003838 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3839 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003840 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003841 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003842 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003843 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02003844 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003845 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003846 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003847 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003848 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003849 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3850 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003851 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003852 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003853 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Bruce Rogers32611072013-09-09 09:40:20 -06003854 I(ImplicitOps | Stack | SrcImmU16, em_ret_far_imm),
3855 I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003856 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003857 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003858 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003859 G(Src2One | ByteOp, group2), G(Src2One, group2),
3860 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003861 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02003862 I(DstAcc | SrcImmUByte | No64, em_aad),
3863 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02003864 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003865 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02003866 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003867 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003868 X3(I(SrcImmByte, em_loop)),
3869 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003870 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3871 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003872 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003873 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003874 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003875 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3876 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003877 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003878 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003879 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3880 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003881 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003882 D(ImplicitOps), D(ImplicitOps),
3883 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003884 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3885};
3886
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003887static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003888 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003889 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02003890 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003891 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003892 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003893 N, D(ImplicitOps | ModRM), N, N,
3894 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02003895 N, N, N, N, N, N, N, N,
3896 D(ImplicitOps | ModRM), N, N, N, N, N, N, D(ImplicitOps | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003897 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03003898 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
3899 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
3900 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
3901 check_cr_write),
3902 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
3903 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003904 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003905 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
3906 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
3907 N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
Avi Kivity3e114eb2012-04-09 18:40:01 +03003908 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003909 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003910 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02003911 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003912 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02003913 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003914 I(ImplicitOps | EmulateOnUD, em_sysenter),
3915 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003916 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003917 N, N, N, N, N, N, N, N,
3918 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03003919 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003920 /* 0x50 - 0x5F */
3921 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3922 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003923 N, N, N, N,
3924 N, N, N, N,
3925 N, N, N, N,
3926 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003927 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003928 N, N, N, N,
3929 N, N, N, N,
3930 N, N, N, N,
3931 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003932 /* 0x80 - 0x8F */
3933 X16(D(SrcImm)),
3934 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003935 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003936 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003937 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02003938 II(ImplicitOps, em_cpuid, cpuid),
3939 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02003940 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
3941 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003942 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003943 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003944 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02003945 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02003946 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
3947 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Avi Kivity4d758342013-01-19 19:51:55 +02003948 D(ModRM), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003949 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09003950 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03003951 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02003952 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03003953 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
3954 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003955 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003956 /* 0xB8 - 0xBF */
3957 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003958 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02003959 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
3960 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003961 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03003962 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02003963 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003964 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003965 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03003966 /* 0xC8 - 0xCF */
3967 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003968 /* 0xD0 - 0xDF */
3969 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3970 /* 0xE0 - 0xEF */
3971 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3972 /* 0xF0 - 0xFF */
3973 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3974};
3975
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003976static const struct gprefix three_byte_0f_38_f0 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01003977 I(DstReg | SrcMem | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003978};
3979
3980static const struct gprefix three_byte_0f_38_f1 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01003981 I(DstMem | SrcReg | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003982};
3983
3984/*
3985 * Insns below are selected by the prefix which indexed by the third opcode
3986 * byte.
3987 */
3988static const struct opcode opcode_map_0f_38[256] = {
3989 /* 0x00 - 0x7f */
3990 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01003991 /* 0x80 - 0xef */
3992 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
3993 /* 0xf0 - 0xf1 */
3994 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f0),
3995 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f1),
3996 /* 0xf2 - 0xff */
3997 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003998};
3999
Avi Kivity73fba5f2010-07-29 15:11:53 +03004000#undef D
4001#undef N
4002#undef G
4003#undef GD
4004#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004005#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004006#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004007
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004008#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004009#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004010#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004011#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004012#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004013
Avi Kivity9dac77f2011-06-01 15:34:25 +03004014static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004015{
4016 unsigned size;
4017
Avi Kivity9dac77f2011-06-01 15:34:25 +03004018 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004019 if (size == 8)
4020 size = 4;
4021 return size;
4022}
4023
4024static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4025 unsigned size, bool sign_extension)
4026{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004027 int rc = X86EMUL_CONTINUE;
4028
4029 op->type = OP_IMM;
4030 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004031 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004032 /* NB. Immediates are sign-extended as necessary. */
4033 switch (op->bytes) {
4034 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004035 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004036 break;
4037 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004038 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004039 break;
4040 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004041 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004042 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004043 case 8:
4044 op->val = insn_fetch(s64, ctxt);
4045 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004046 }
4047 if (!sign_extension) {
4048 switch (op->bytes) {
4049 case 1:
4050 op->val &= 0xff;
4051 break;
4052 case 2:
4053 op->val &= 0xffff;
4054 break;
4055 case 4:
4056 op->val &= 0xffffffff;
4057 break;
4058 }
4059 }
4060done:
4061 return rc;
4062}
4063
Avi Kivitya99455492011-09-13 10:45:41 +03004064static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4065 unsigned d)
4066{
4067 int rc = X86EMUL_CONTINUE;
4068
4069 switch (d) {
4070 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004071 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03004072 break;
4073 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004074 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03004075 break;
4076 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004077 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004078 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03004079 *op = ctxt->memop;
4080 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004081 if (ctxt->d & BitOp)
Avi Kivitya99455492011-09-13 10:45:41 +03004082 fetch_bit_operand(ctxt);
4083 op->orig_val = op->val;
4084 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004085 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004086 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004087 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03004088 case OpAcc:
4089 op->type = OP_REG;
4090 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004091 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03004092 fetch_register_operand(op);
4093 op->orig_val = op->val;
4094 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004095 case OpAccLo:
4096 op->type = OP_REG;
4097 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4098 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4099 fetch_register_operand(op);
4100 op->orig_val = op->val;
4101 break;
4102 case OpAccHi:
4103 if (ctxt->d & ByteOp) {
4104 op->type = OP_NONE;
4105 break;
4106 }
4107 op->type = OP_REG;
4108 op->bytes = ctxt->op_bytes;
4109 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4110 fetch_register_operand(op);
4111 op->orig_val = op->val;
4112 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004113 case OpDI:
4114 op->type = OP_MEM;
4115 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4116 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004117 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya99455492011-09-13 10:45:41 +03004118 op->addr.mem.seg = VCPU_SREG_ES;
4119 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004120 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03004121 break;
4122 case OpDX:
4123 op->type = OP_REG;
4124 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004125 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03004126 fetch_register_operand(op);
4127 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004128 case OpCL:
4129 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004130 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004131 break;
4132 case OpImmByte:
4133 rc = decode_imm(ctxt, op, 1, true);
4134 break;
4135 case OpOne:
4136 op->bytes = 1;
4137 op->val = 1;
4138 break;
4139 case OpImm:
4140 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4141 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004142 case OpImm64:
4143 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4144 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004145 case OpMem8:
4146 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004147 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004148 ctxt->memop.addr.reg = decode_register(ctxt,
4149 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004150 fetch_register_operand(&ctxt->memop);
4151 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004152 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004153 case OpMem16:
4154 ctxt->memop.bytes = 2;
4155 goto mem_common;
4156 case OpMem32:
4157 ctxt->memop.bytes = 4;
4158 goto mem_common;
4159 case OpImmU16:
4160 rc = decode_imm(ctxt, op, 2, false);
4161 break;
4162 case OpImmU:
4163 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4164 break;
4165 case OpSI:
4166 op->type = OP_MEM;
4167 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4168 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004169 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Bandan Das573e80f2014-04-16 12:46:13 -04004170 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03004171 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004172 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004173 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004174 case OpXLat:
4175 op->type = OP_MEM;
4176 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4177 op->addr.mem.ea =
4178 register_address(ctxt,
4179 reg_read(ctxt, VCPU_REGS_RBX) +
4180 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04004181 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004182 op->val = 0;
4183 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004184 case OpImmFAddr:
4185 op->type = OP_IMM;
4186 op->addr.mem.ea = ctxt->_eip;
4187 op->bytes = ctxt->op_bytes + 2;
4188 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4189 break;
4190 case OpMemFAddr:
4191 ctxt->memop.bytes = ctxt->op_bytes + 2;
4192 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004193 case OpES:
4194 op->val = VCPU_SREG_ES;
4195 break;
4196 case OpCS:
4197 op->val = VCPU_SREG_CS;
4198 break;
4199 case OpSS:
4200 op->val = VCPU_SREG_SS;
4201 break;
4202 case OpDS:
4203 op->val = VCPU_SREG_DS;
4204 break;
4205 case OpFS:
4206 op->val = VCPU_SREG_FS;
4207 break;
4208 case OpGS:
4209 op->val = VCPU_SREG_GS;
4210 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004211 case OpImplicit:
4212 /* Special instructions do their own operand decoding. */
4213 default:
4214 op->type = OP_NONE; /* Disable writeback. */
4215 break;
4216 }
4217
4218done:
4219 return rc;
4220}
4221
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004222int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004223{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004224 int rc = X86EMUL_CONTINUE;
4225 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004226 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004227 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04004228 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03004229 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004230
Avi Kivityf09ed832011-09-13 10:45:40 +03004231 ctxt->memop.type = OP_NONE;
4232 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004233 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02004234 ctxt->fetch.ptr = ctxt->fetch.data;
4235 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004236 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01004237 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004238 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004239 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02004240 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004241 if (rc != X86EMUL_CONTINUE)
4242 return rc;
4243 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004244
4245 switch (mode) {
4246 case X86EMUL_MODE_REAL:
4247 case X86EMUL_MODE_VM86:
4248 case X86EMUL_MODE_PROT16:
4249 def_op_bytes = def_ad_bytes = 2;
4250 break;
4251 case X86EMUL_MODE_PROT32:
4252 def_op_bytes = def_ad_bytes = 4;
4253 break;
4254#ifdef CONFIG_X86_64
4255 case X86EMUL_MODE_PROT64:
4256 def_op_bytes = 4;
4257 def_ad_bytes = 8;
4258 break;
4259#endif
4260 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004261 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004262 }
4263
Avi Kivity9dac77f2011-06-01 15:34:25 +03004264 ctxt->op_bytes = def_op_bytes;
4265 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004266
4267 /* Legacy prefixes. */
4268 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004269 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004270 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004271 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004272 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004273 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004274 break;
4275 case 0x67: /* address-size override */
4276 if (mode == X86EMUL_MODE_PROT64)
4277 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004278 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004279 else
4280 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004281 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004282 break;
4283 case 0x26: /* ES override */
4284 case 0x2e: /* CS override */
4285 case 0x36: /* SS override */
4286 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004287 has_seg_override = true;
4288 ctxt->seg_override = (ctxt->b >> 3) & 3;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004289 break;
4290 case 0x64: /* FS override */
4291 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004292 has_seg_override = true;
4293 ctxt->seg_override = ctxt->b & 7;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004294 break;
4295 case 0x40 ... 0x4f: /* REX */
4296 if (mode != X86EMUL_MODE_PROT64)
4297 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004298 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004299 continue;
4300 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004301 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004302 break;
4303 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004304 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004305 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004306 break;
4307 default:
4308 goto done_prefixes;
4309 }
4310
4311 /* Any legacy prefix after a REX prefix nullifies its effect. */
4312
Avi Kivity9dac77f2011-06-01 15:34:25 +03004313 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004314 }
4315
4316done_prefixes:
4317
4318 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004319 if (ctxt->rex_prefix & 8)
4320 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004321
4322 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004323 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004324 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004325 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004326 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004327 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004328 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004329
4330 /* 0F_38 opcode map */
4331 if (ctxt->b == 0x38) {
4332 ctxt->opcode_len = 3;
4333 ctxt->b = insn_fetch(u8, ctxt);
4334 opcode = opcode_map_0f_38[ctxt->b];
4335 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004336 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004337 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004338
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004339 if (ctxt->d & ModRM)
4340 ctxt->modrm = insn_fetch(u8, ctxt);
4341
Nadav Amit7fe864d2014-06-02 18:34:03 +03004342 /* vex-prefix instructions are not implemented */
4343 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
4344 (mode == X86EMUL_MODE_PROT64 ||
4345 (mode >= X86EMUL_MODE_PROT16 && (ctxt->modrm & 0x80)))) {
4346 ctxt->d = NotImpl;
4347 }
4348
Avi Kivity9dac77f2011-06-01 15:34:25 +03004349 while (ctxt->d & GroupMask) {
4350 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004351 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004352 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004353 opcode = opcode.u.group[goffset];
4354 break;
4355 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004356 goffset = (ctxt->modrm >> 3) & 7;
4357 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004358 opcode = opcode.u.gdual->mod3[goffset];
4359 else
4360 opcode = opcode.u.gdual->mod012[goffset];
4361 break;
4362 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004363 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004364 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004365 break;
4366 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004367 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004368 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004369 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004370 switch (simd_prefix) {
4371 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4372 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4373 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4374 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4375 }
4376 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004377 case Escape:
4378 if (ctxt->modrm > 0xbf)
4379 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4380 else
4381 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4382 break;
Avi Kivity46561642011-04-24 14:09:59 +03004383 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004384 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004385 }
Avi Kivity46561642011-04-24 14:09:59 +03004386
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004387 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004388 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004389 }
4390
Paolo Bonzinie24186e2014-03-27 12:00:57 +01004391 /* Unrecognised? */
4392 if (ctxt->d == 0)
4393 return EMULATION_FAILED;
4394
Avi Kivity9dac77f2011-06-01 15:34:25 +03004395 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004396
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004397 if (unlikely(ctxt->d &
4398 (NotImpl|EmulateOnUD|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm))) {
4399 /*
4400 * These are copied unconditionally here, and checked unconditionally
4401 * in x86_emulate_insn.
4402 */
4403 ctxt->check_perm = opcode.check_perm;
4404 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004405
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004406 if (ctxt->d & NotImpl)
4407 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004408
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004409 if (!(ctxt->d & EmulateOnUD) && ctxt->ud)
4410 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004411
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004412 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004413 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004414
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004415 if (ctxt->d & Op3264) {
4416 if (mode == X86EMUL_MODE_PROT64)
4417 ctxt->op_bytes = 8;
4418 else
4419 ctxt->op_bytes = 4;
4420 }
4421
4422 if (ctxt->d & Sse)
4423 ctxt->op_bytes = 16;
4424 else if (ctxt->d & Mmx)
4425 ctxt->op_bytes = 8;
4426 }
Avi Kivity12537912011-03-29 11:41:27 +02004427
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004428 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004429 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004430 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04004431 if (!has_seg_override) {
4432 has_seg_override = true;
4433 ctxt->seg_override = ctxt->modrm_seg;
4434 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004435 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004436 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004437 if (rc != X86EMUL_CONTINUE)
4438 goto done;
4439
Bandan Das573e80f2014-04-16 12:46:13 -04004440 if (!has_seg_override)
4441 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004442
Bandan Das573e80f2014-04-16 12:46:13 -04004443 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004444
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004445 /*
4446 * Decode and fetch the source operand: register, memory
4447 * or immediate.
4448 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004449 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004450 if (rc != X86EMUL_CONTINUE)
4451 goto done;
4452
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004453 /*
4454 * Decode and fetch the second source operand: register, memory
4455 * or immediate.
4456 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004457 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004458 if (rc != X86EMUL_CONTINUE)
4459 goto done;
4460
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004461 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03004462 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004463
4464done:
Bandan Das41061cd2014-04-16 12:46:14 -04004465 if (ctxt->rip_relative)
Avi Kivityf09ed832011-09-13 10:45:40 +03004466 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004467
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004468 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004469}
4470
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004471bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4472{
4473 return ctxt->d & PageTable;
4474}
4475
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004476static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4477{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004478 /* The second termination condition only applies for REPE
4479 * and REPNE. Test if the repeat string operation prefix is
4480 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4481 * corresponding termination condition according to:
4482 * - if REPE/REPZ and ZF = 0 then done
4483 * - if REPNE/REPNZ and ZF = 1 then done
4484 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004485 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4486 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4487 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004488 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004489 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004490 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4491 return true;
4492
4493 return false;
4494}
4495
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004496static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4497{
4498 bool fault = false;
4499
4500 ctxt->ops->get_fpu(ctxt);
4501 asm volatile("1: fwait \n\t"
4502 "2: \n\t"
4503 ".pushsection .fixup,\"ax\" \n\t"
4504 "3: \n\t"
4505 "movb $1, %[fault] \n\t"
4506 "jmp 2b \n\t"
4507 ".popsection \n\t"
4508 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004509 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004510 ctxt->ops->put_fpu(ctxt);
4511
4512 if (unlikely(fault))
4513 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4514
4515 return X86EMUL_CONTINUE;
4516}
4517
4518static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4519 struct operand *op)
4520{
4521 if (op->type == OP_MM)
4522 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4523}
4524
Avi Kivitye28bbd42013-01-04 16:18:48 +02004525static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4526{
4527 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivityb9fa4092013-02-09 11:31:48 +02004528 if (!(ctxt->d & ByteOp))
4529 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Avi Kivitye28bbd42013-01-04 16:18:48 +02004530 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004531 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
4532 [fastop]"+S"(fop)
4533 : "c"(ctxt->src2.val));
Avi Kivitye28bbd42013-01-04 16:18:48 +02004534 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004535 if (!fop) /* exception is returned in fop variable */
4536 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02004537 return X86EMUL_CONTINUE;
4538}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004539
Bandan Das14985072014-04-16 12:46:09 -04004540void init_decode_cache(struct x86_emulate_ctxt *ctxt)
4541{
Bandan Das573e80f2014-04-16 12:46:13 -04004542 memset(&ctxt->rip_relative, 0,
4543 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04004544
Bandan Das14985072014-04-16 12:46:09 -04004545 ctxt->io_read.pos = 0;
4546 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04004547 ctxt->mem_read.end = 0;
4548}
4549
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004550int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004551{
Mathias Krause0225fb52012-08-30 01:30:16 +02004552 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004553 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004554 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004555
Avi Kivity9dac77f2011-06-01 15:34:25 +03004556 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004557
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004558 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004559 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004560 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004561 goto done;
4562 }
4563
Avi Kivity9dac77f2011-06-01 15:34:25 +03004564 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004565 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004566 goto done;
4567 }
4568
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004569 if (unlikely(ctxt->d &
4570 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
4571 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4572 (ctxt->d & Undefined)) {
4573 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004574 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004575 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004576
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004577 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4578 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
4579 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004580 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004581 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004582
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004583 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
4584 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004585 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004586 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004587
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004588 if (ctxt->d & Mmx) {
4589 rc = flush_pending_x87_faults(ctxt);
4590 if (rc != X86EMUL_CONTINUE)
4591 goto done;
4592 /*
4593 * Now that we know the fpu is exception safe, we can fetch
4594 * operands from it.
4595 */
4596 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4597 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4598 if (!(ctxt->d & Mov))
4599 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4600 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004601
Bandan Das685bbf42014-04-16 12:46:10 -04004602 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004603 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4604 X86_ICPT_PRE_EXCEPT);
4605 if (rc != X86EMUL_CONTINUE)
4606 goto done;
4607 }
4608
4609 /* Privileged instruction can be executed only in CPL=0 */
4610 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Nadav Amit68efa762014-06-18 17:19:35 +03004611 if (ctxt->d & PrivUD)
4612 rc = emulate_ud(ctxt);
4613 else
4614 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004615 goto done;
4616 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004617
4618 /* Instruction can only be executed in protected mode */
4619 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
4620 rc = emulate_ud(ctxt);
4621 goto done;
4622 }
4623
4624 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04004625 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004626 rc = ctxt->check_perm(ctxt);
4627 if (rc != X86EMUL_CONTINUE)
4628 goto done;
4629 }
4630
Bandan Das685bbf42014-04-16 12:46:10 -04004631 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004632 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4633 X86_ICPT_POST_EXCEPT);
4634 if (rc != X86EMUL_CONTINUE)
4635 goto done;
4636 }
4637
4638 if (ctxt->rep_prefix && (ctxt->d & String)) {
4639 /* All REP prefixes have the same first termination condition */
4640 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
4641 ctxt->eip = ctxt->_eip;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004642 ctxt->eflags &= ~EFLG_RF;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004643 goto done;
4644 }
4645 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004646 }
4647
Avi Kivity9dac77f2011-06-01 15:34:25 +03004648 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4649 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4650 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004651 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004652 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004653 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004654 }
4655
Avi Kivity9dac77f2011-06-01 15:34:25 +03004656 if (ctxt->src2.type == OP_MEM) {
4657 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4658 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004659 if (rc != X86EMUL_CONTINUE)
4660 goto done;
4661 }
4662
Avi Kivity9dac77f2011-06-01 15:34:25 +03004663 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004664 goto special_insn;
4665
4666
Avi Kivity9dac77f2011-06-01 15:34:25 +03004667 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004668 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004669 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4670 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004671 if (rc != X86EMUL_CONTINUE)
4672 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004673 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004674 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004675
Avi Kivity018a98d2007-11-27 19:30:56 +02004676special_insn:
4677
Bandan Das685bbf42014-04-16 12:46:10 -04004678 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004679 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004680 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004681 if (rc != X86EMUL_CONTINUE)
4682 goto done;
4683 }
4684
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004685 if (ctxt->rep_prefix && (ctxt->d & String))
4686 ctxt->eflags |= EFLG_RF;
4687 else
4688 ctxt->eflags &= ~EFLG_RF;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004689
Avi Kivity9dac77f2011-06-01 15:34:25 +03004690 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004691 if (ctxt->d & Fastop) {
4692 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4693 rc = fastop(ctxt, fop);
4694 if (rc != X86EMUL_CONTINUE)
4695 goto done;
4696 goto writeback;
4697 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004698 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004699 if (rc != X86EMUL_CONTINUE)
4700 goto done;
4701 goto writeback;
4702 }
4703
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004704 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004705 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004706 else if (ctxt->opcode_len == 3)
4707 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004708
Avi Kivity9dac77f2011-06-01 15:34:25 +03004709 switch (ctxt->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004710 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004711 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004712 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004713 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004714 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004715 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004716 if (test_cc(ctxt->b, ctxt->eflags))
4717 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004718 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004719 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004720 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004721 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004722 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004723 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03004724 ctxt->dst.type = OP_NONE;
4725 else
4726 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004727 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004728 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004729 switch (ctxt->op_bytes) {
4730 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4731 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4732 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004733 }
4734 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004735 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004736 rc = emulate_int(ctxt, 3);
4737 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004738 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004739 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004740 break;
4741 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004742 if (ctxt->eflags & EFLG_OF)
4743 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004744 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004745 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004746 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004747 jmp_rel(ctxt, ctxt->src.val);
4748 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004749 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004750 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004751 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004752 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004753 case 0xf5: /* cmc */
4754 /* complement carry flag from eflags reg */
4755 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004756 break;
4757 case 0xf8: /* clc */
4758 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004759 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004760 case 0xf9: /* stc */
4761 ctxt->eflags |= EFLG_CF;
4762 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004763 case 0xfc: /* cld */
4764 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004765 break;
4766 case 0xfd: /* std */
4767 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004768 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004769 default:
4770 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004771 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004772
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004773 if (rc != X86EMUL_CONTINUE)
4774 goto done;
4775
Avi Kivity018a98d2007-11-27 19:30:56 +02004776writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02004777 if (ctxt->d & SrcWrite) {
4778 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
4779 rc = writeback(ctxt, &ctxt->src);
4780 if (rc != X86EMUL_CONTINUE)
4781 goto done;
4782 }
Nadav Amitee212292014-06-15 16:12:58 +03004783 if (!(ctxt->d & NoWrite)) {
4784 rc = writeback(ctxt, &ctxt->dst);
4785 if (rc != X86EMUL_CONTINUE)
4786 goto done;
4787 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004788
Gleb Natapov5cd21912010-03-18 15:20:26 +02004789 /*
4790 * restore dst type in case the decoding will be reused
4791 * (happens for string instruction )
4792 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004793 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004794
Avi Kivity9dac77f2011-06-01 15:34:25 +03004795 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004796 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004797
Avi Kivity9dac77f2011-06-01 15:34:25 +03004798 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004799 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004800
Avi Kivity9dac77f2011-06-01 15:34:25 +03004801 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004802 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004803 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004804 if ((ctxt->d & SrcMask) == SrcSI)
4805 count = ctxt->src.count;
4806 else
4807 count = ctxt->dst.count;
4808 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4809 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004810
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004811 if (!string_insn_completed(ctxt)) {
4812 /*
4813 * Re-enter guest when pio read ahead buffer is empty
4814 * or, if it is not used, after each 1024 iteration.
4815 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004816 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004817 (r->end == 0 || r->end != r->pos)) {
4818 /*
4819 * Reset read cache. Usually happens before
4820 * decode, but since instruction is restarted
4821 * we have to do it here.
4822 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004823 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004824 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004825 return EMULATION_RESTART;
4826 }
4827 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004828 }
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004829 ctxt->eflags &= ~EFLG_RF;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004830 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004831
Avi Kivity9dac77f2011-06-01 15:34:25 +03004832 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004833
4834done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004835 if (rc == X86EMUL_PROPAGATE_FAULT)
4836 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004837 if (rc == X86EMUL_INTERCEPTED)
4838 return EMULATION_INTERCEPTED;
4839
Avi Kivitydd856ef2012-08-27 23:46:17 +03004840 if (rc == X86EMUL_CONTINUE)
4841 writeback_registers(ctxt);
4842
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004843 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004844
4845twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004846 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004847 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004848 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004849 break;
4850 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004851 case 0x0d: /* GrpP (prefetch) */
4852 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004853 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02004854 break;
4855 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004856 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004857 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004858 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004859 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004860 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004861 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03004862 if (test_cc(ctxt->b, ctxt->eflags))
4863 ctxt->dst.val = ctxt->src.val;
4864 else if (ctxt->mode != X86EMUL_MODE_PROT64 ||
4865 ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004866 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004867 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004868 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004869 if (test_cc(ctxt->b, ctxt->eflags))
4870 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004871 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004872 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004873 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004874 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004875 case 0xae: /* clflush */
4876 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004877 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004878 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004879 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004880 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004881 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004882 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004883 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004884 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004885 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004886 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004887 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004888 ctxt->dst.bytes = ctxt->op_bytes;
Nadav Amit3b320042014-06-02 18:34:08 +03004889 ctxt->dst.val = (ctxt->op_bytes == 8) ? (u64) ctxt->src.val :
4890 (u32) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004891 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004892 default:
4893 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004894 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004895
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004896threebyte_insn:
4897
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004898 if (rc != X86EMUL_CONTINUE)
4899 goto done;
4900
Avi Kivity6aa8b732006-12-10 02:21:36 -08004901 goto writeback;
4902
4903cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004904 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004905}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004906
4907void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
4908{
4909 invalidate_registers(ctxt);
4910}
4911
4912void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
4913{
4914 writeback_registers(ctxt);
4915}