blob: 147f3fdfb5ced254149a008af3bdeaeff877eb27 [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 Kivity56697682011-04-03 14:08:51 +0300507static u32 desc_limit_scaled(struct desc_struct *desc)
508{
509 u32 limit = get_desc_limit(desc);
510
511 return desc->g ? (limit << 12) | 0xfff : limit;
512}
513
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900514static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300515{
516 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
517 return 0;
518
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900519 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300520}
521
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200522static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
523 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300524{
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +0200525 WARN_ON(vec > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +0200526 ctxt->exception.vector = vec;
527 ctxt->exception.error_code = error;
528 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200529 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300530}
531
Joerg Roedel3b88e412011-04-04 12:39:29 +0200532static int emulate_db(struct x86_emulate_ctxt *ctxt)
533{
534 return emulate_exception(ctxt, DB_VECTOR, 0, false);
535}
536
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200537static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300538{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200539 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300540}
541
Avi Kivity618ff152011-04-03 12:32:09 +0300542static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
543{
544 return emulate_exception(ctxt, SS_VECTOR, err, true);
545}
546
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200547static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300548{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200549 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300550}
551
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200552static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300553{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200554 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300555}
556
Avi Kivity34d1f492010-08-26 11:59:01 +0300557static int emulate_de(struct x86_emulate_ctxt *ctxt)
558{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200559 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300560}
561
Avi Kivity1253791d2011-03-29 11:41:27 +0200562static int emulate_nm(struct x86_emulate_ctxt *ctxt)
563{
564 return emulate_exception(ctxt, NM_VECTOR, 0, false);
565}
566
Nadav Amit234f3ce2014-09-18 22:39:38 +0300567static inline int assign_eip_far(struct x86_emulate_ctxt *ctxt, ulong dst,
568 int cs_l)
Nadav Amit05c83ec2014-09-18 22:39:37 +0300569{
570 switch (ctxt->op_bytes) {
571 case 2:
572 ctxt->_eip = (u16)dst;
573 break;
574 case 4:
575 ctxt->_eip = (u32)dst;
576 break;
Nadav Amitcd9b8e2c2014-10-28 00:03:43 +0200577#ifdef CONFIG_X86_64
Nadav Amit05c83ec2014-09-18 22:39:37 +0300578 case 8:
Nadav Amit234f3ce2014-09-18 22:39:38 +0300579 if ((cs_l && is_noncanonical_address(dst)) ||
Nadav Amitcd9b8e2c2014-10-28 00:03:43 +0200580 (!cs_l && (dst >> 32) != 0))
Nadav Amit234f3ce2014-09-18 22:39:38 +0300581 return emulate_gp(ctxt, 0);
Nadav Amit05c83ec2014-09-18 22:39:37 +0300582 ctxt->_eip = dst;
583 break;
Nadav Amitcd9b8e2c2014-10-28 00:03:43 +0200584#endif
Nadav Amit05c83ec2014-09-18 22:39:37 +0300585 default:
586 WARN(1, "unsupported eip assignment size\n");
587 }
Nadav Amit234f3ce2014-09-18 22:39:38 +0300588 return X86EMUL_CONTINUE;
Nadav Amit05c83ec2014-09-18 22:39:37 +0300589}
590
Nadav Amit234f3ce2014-09-18 22:39:38 +0300591static inline int assign_eip_near(struct x86_emulate_ctxt *ctxt, ulong dst)
Nadav Amit05c83ec2014-09-18 22:39:37 +0300592{
Nadav Amit234f3ce2014-09-18 22:39:38 +0300593 return assign_eip_far(ctxt, dst, ctxt->mode == X86EMUL_MODE_PROT64);
594}
595
596static inline int jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
597{
598 return assign_eip_near(ctxt, ctxt->_eip + rel);
Nadav Amit05c83ec2014-09-18 22:39:37 +0300599}
600
Avi Kivity1aa36612011-04-27 13:20:30 +0300601static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
602{
603 u16 selector;
604 struct desc_struct desc;
605
606 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
607 return selector;
608}
609
610static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
611 unsigned seg)
612{
613 u16 dummy;
614 u32 base3;
615 struct desc_struct desc;
616
617 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
618 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
619}
620
Avi Kivity1c11b372012-04-09 18:39:59 +0300621/*
622 * x86 defines three classes of vector instructions: explicitly
623 * aligned, explicitly unaligned, and the rest, which change behaviour
624 * depending on whether they're AVX encoded or not.
625 *
626 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
627 * subject to the same check.
628 */
629static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
630{
631 if (likely(size < 16))
632 return false;
633
634 if (ctxt->d & Aligned)
635 return true;
636 else if (ctxt->d & Unaligned)
637 return false;
638 else if (ctxt->d & Avx)
639 return false;
640 else
641 return true;
642}
643
Paolo Bonzinid09155d2014-10-27 14:54:44 +0100644static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
645 struct segmented_address addr,
646 unsigned *max_size, unsigned size,
647 bool write, bool fetch,
648 ulong *linear)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300649{
Avi Kivity618ff152011-04-03 12:32:09 +0300650 struct desc_struct desc;
651 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300652 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300653 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300654 u16 sel;
Gleb Natapov3a78a4f2012-12-20 16:57:42 +0200655 unsigned cpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300656
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900657 la = seg_base(ctxt, addr.seg) + addr.ea;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100658 *max_size = 0;
Avi Kivity618ff152011-04-03 12:32:09 +0300659 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300660 case X86EMUL_MODE_PROT64:
Nadav Amit4be4de72014-09-18 22:39:40 +0300661 if (is_noncanonical_address(la))
Avi Kivity618ff152011-04-03 12:32:09 +0300662 return emulate_gp(ctxt, 0);
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100663
664 *max_size = min_t(u64, ~0u, (1ull << 48) - la);
665 if (size > *max_size)
666 goto bad;
Avi Kivity618ff152011-04-03 12:32:09 +0300667 break;
668 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300669 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
670 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300671 if (!usable)
672 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200673 /* code segment in protected mode or read-only data segment */
674 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
675 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300676 goto bad;
677 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400678 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300679 goto bad;
680 lim = desc_limit_scaled(&desc);
Nadav Amit10e38fc2014-06-18 17:19:34 +0300681 if ((ctxt->mode == X86EMUL_MODE_REAL) && !fetch &&
682 (ctxt->d & NoBigReal)) {
683 /* la is between zero and 0xffff */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100684 if (la > 0xffff)
Nadav Amit10e38fc2014-06-18 17:19:34 +0300685 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100686 *max_size = 0x10000 - la;
Nadav Amit10e38fc2014-06-18 17:19:34 +0300687 } else if ((desc.type & 8) || !(desc.type & 4)) {
Avi Kivity618ff152011-04-03 12:32:09 +0300688 /* expand-up segment */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100689 if (addr.ea > lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300690 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100691 *max_size = min_t(u64, ~0u, (u64)lim + 1 - addr.ea);
Avi Kivity618ff152011-04-03 12:32:09 +0300692 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800693 /* expand-down segment */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100694 if (addr.ea <= lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300695 goto bad;
696 lim = desc.d ? 0xffffffff : 0xffff;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100697 if (addr.ea > lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300698 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100699 *max_size = min_t(u64, ~0u, (u64)lim + 1 - addr.ea);
Avi Kivity618ff152011-04-03 12:32:09 +0300700 }
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100701 if (size > *max_size)
702 goto bad;
Avi Kivity717746e2011-04-20 13:37:53 +0300703 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity618ff152011-04-03 12:32:09 +0300704 if (!(desc.type & 8)) {
705 /* data segment */
706 if (cpl > desc.dpl)
707 goto bad;
708 } else if ((desc.type & 8) && !(desc.type & 4)) {
709 /* nonconforming code segment */
710 if (cpl != desc.dpl)
711 goto bad;
712 } else if ((desc.type & 8) && (desc.type & 4)) {
713 /* conforming code segment */
714 if (cpl < desc.dpl)
715 goto bad;
716 }
717 break;
718 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300719 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300720 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300721 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
722 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300723 *linear = la;
724 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300725bad:
726 if (addr.seg == VCPU_SREG_SS)
Paolo Bonzini36061892014-10-27 14:40:49 +0100727 return emulate_ss(ctxt, 0);
Avi Kivity618ff152011-04-03 12:32:09 +0300728 else
Paolo Bonzini36061892014-10-27 14:40:49 +0100729 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300730}
731
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400732static int linearize(struct x86_emulate_ctxt *ctxt,
733 struct segmented_address addr,
734 unsigned size, bool write,
735 ulong *linear)
736{
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100737 unsigned max_size;
738 return __linearize(ctxt, addr, &max_size, size, write, false, linear);
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400739}
740
741
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200742static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
743 struct segmented_address addr,
744 void *data,
745 unsigned size)
746{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200747 int rc;
748 ulong linear;
749
Avi Kivity83b87952011-04-03 11:31:19 +0300750 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200751 if (rc != X86EMUL_CONTINUE)
752 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300753 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200754}
755
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900756/*
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200757 * Prefetch the remaining bytes of the instruction without crossing page
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900758 * boundary if they are not in fetch_cache yet.
759 */
Paolo Bonzini9506d572014-05-06 13:05:25 +0200760static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
Avi Kivity62266862007-11-20 13:15:52 +0200761{
Avi Kivity62266862007-11-20 13:15:52 +0200762 int rc;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100763 unsigned size, max_size;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200764 unsigned long linear;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200765 int cur_size = ctxt->fetch.end - ctxt->fetch.data;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200766 struct segmented_address addr = { .seg = VCPU_SREG_CS,
Paolo Bonzini17052f12014-05-06 16:33:01 +0200767 .ea = ctxt->eip + cur_size };
768
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100769 /*
770 * We do not know exactly how many bytes will be needed, and
771 * __linearize is expensive, so fetch as much as possible. We
772 * just have to avoid going beyond the 15 byte limit, the end
773 * of the segment, or the end of the page.
774 *
775 * __linearize is called with size 0 so that it does not do any
776 * boundary check itself. Instead, we use max_size to check
777 * against op_size.
778 */
779 rc = __linearize(ctxt, addr, &max_size, 0, false, true, &linear);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200780 if (unlikely(rc != X86EMUL_CONTINUE))
781 return rc;
782
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100783 size = min_t(unsigned, 15UL ^ cur_size, max_size);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200784 size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear));
Paolo Bonzini5cfc7e02014-05-06 13:05:25 +0200785
786 /*
787 * One instruction can only straddle two pages,
788 * and one has been loaded at the beginning of
789 * x86_decode_insn. So, if not enough bytes
790 * still, we must have hit the 15-byte boundary.
791 */
792 if (unlikely(size < op_size))
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100793 return emulate_gp(ctxt, 0);
794
Paolo Bonzini17052f12014-05-06 16:33:01 +0200795 rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end,
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200796 size, &ctxt->exception);
797 if (unlikely(rc != X86EMUL_CONTINUE))
798 return rc;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200799 ctxt->fetch.end += size;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900800 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200801}
802
Paolo Bonzini9506d572014-05-06 13:05:25 +0200803static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt,
804 unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200805{
Nadav Amit08da44a2014-10-03 01:10:04 +0300806 unsigned done_size = ctxt->fetch.end - ctxt->fetch.ptr;
807
808 if (unlikely(done_size < size))
809 return __do_insn_fetch_bytes(ctxt, size - done_size);
Paolo Bonzini9506d572014-05-06 13:05:25 +0200810 else
811 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200812}
813
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900814/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900815#define insn_fetch(_type, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200816({ _type _x; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200817 \
818 rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900819 if (rc != X86EMUL_CONTINUE) \
820 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200821 ctxt->_eip += sizeof(_type); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200822 _x = *(_type __aligned(1) *) ctxt->fetch.ptr; \
823 ctxt->fetch.ptr += sizeof(_type); \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200824 _x; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900825})
826
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900827#define insn_fetch_arr(_arr, _size, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200828({ \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200829 rc = do_insn_fetch_bytes(_ctxt, _size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900830 if (rc != X86EMUL_CONTINUE) \
831 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200832 ctxt->_eip += (_size); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200833 memcpy(_arr, ctxt->fetch.ptr, _size); \
834 ctxt->fetch.ptr += (_size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900835})
836
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000837/*
838 * Given the 'reg' portion of a ModRM byte, and a register block, return a
839 * pointer into the block that addresses the relevant register.
840 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
841 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300842static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200843 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800844{
845 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200846 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300849 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
850 else
851 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800852 return p;
853}
854
855static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200856 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800857 u16 *size, unsigned long *address, int op_bytes)
858{
859 int rc;
860
861 if (op_bytes == 2)
862 op_bytes = 3;
863 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200864 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900865 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800866 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200867 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200868 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800869 return rc;
870}
871
Avi Kivity34b77652013-01-19 19:51:56 +0200872FASTOP2(add);
873FASTOP2(or);
874FASTOP2(adc);
875FASTOP2(sbb);
876FASTOP2(and);
877FASTOP2(sub);
878FASTOP2(xor);
879FASTOP2(cmp);
880FASTOP2(test);
881
Avi Kivityb9fa4092013-02-09 11:31:48 +0200882FASTOP1SRC2(mul, mul_ex);
883FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200884FASTOP1SRC2EX(div, div_ex);
885FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +0200886
Avi Kivity34b77652013-01-19 19:51:56 +0200887FASTOP3WCL(shld);
888FASTOP3WCL(shrd);
889
890FASTOP2W(imul);
891
892FASTOP1(not);
893FASTOP1(neg);
894FASTOP1(inc);
895FASTOP1(dec);
896
897FASTOP2CL(rol);
898FASTOP2CL(ror);
899FASTOP2CL(rcl);
900FASTOP2CL(rcr);
901FASTOP2CL(shl);
902FASTOP2CL(shr);
903FASTOP2CL(sar);
904
905FASTOP2W(bsf);
906FASTOP2W(bsr);
907FASTOP2W(bt);
908FASTOP2W(bts);
909FASTOP2W(btr);
910FASTOP2W(btc);
911
Avi Kivitye47a5f52013-02-09 11:31:51 +0200912FASTOP2(xadd);
913
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200914static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300915{
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200916 u8 rc;
917 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300918
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200919 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +0200920 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200921 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
922 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300923}
924
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300925static void fetch_register_operand(struct operand *op)
926{
927 switch (op->bytes) {
928 case 1:
929 op->val = *(u8 *)op->addr.reg;
930 break;
931 case 2:
932 op->val = *(u16 *)op->addr.reg;
933 break;
934 case 4:
935 op->val = *(u32 *)op->addr.reg;
936 break;
937 case 8:
938 op->val = *(u64 *)op->addr.reg;
939 break;
940 }
941}
942
Avi Kivity1253791d2011-03-29 11:41:27 +0200943static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
944{
945 ctxt->ops->get_fpu(ctxt);
946 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200947 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
948 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
949 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
950 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
951 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
952 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
953 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
954 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200955#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200956 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
957 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
958 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
959 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
960 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
961 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
962 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
963 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200964#endif
965 default: BUG();
966 }
967 ctxt->ops->put_fpu(ctxt);
968}
969
970static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
971 int reg)
972{
973 ctxt->ops->get_fpu(ctxt);
974 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200975 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
976 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
977 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
978 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
979 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
980 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
981 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
982 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200983#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200984 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
985 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
986 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
987 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
988 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
989 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
990 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
991 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200992#endif
993 default: BUG();
994 }
995 ctxt->ops->put_fpu(ctxt);
996}
997
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300998static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
999{
1000 ctxt->ops->get_fpu(ctxt);
1001 switch (reg) {
1002 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1003 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1004 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1005 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1006 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1007 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1008 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1009 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1010 default: BUG();
1011 }
1012 ctxt->ops->put_fpu(ctxt);
1013}
1014
1015static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1016{
1017 ctxt->ops->get_fpu(ctxt);
1018 switch (reg) {
1019 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1020 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1021 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1022 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1023 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1024 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1025 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1026 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1027 default: BUG();
1028 }
1029 ctxt->ops->put_fpu(ctxt);
1030}
1031
Gleb Natapov045a2822012-12-20 16:57:43 +02001032static int em_fninit(struct x86_emulate_ctxt *ctxt)
1033{
1034 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1035 return emulate_nm(ctxt);
1036
1037 ctxt->ops->get_fpu(ctxt);
1038 asm volatile("fninit");
1039 ctxt->ops->put_fpu(ctxt);
1040 return X86EMUL_CONTINUE;
1041}
1042
1043static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1044{
1045 u16 fcw;
1046
1047 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1048 return emulate_nm(ctxt);
1049
1050 ctxt->ops->get_fpu(ctxt);
1051 asm volatile("fnstcw %0": "+m"(fcw));
1052 ctxt->ops->put_fpu(ctxt);
1053
1054 /* force 2 byte destination */
1055 ctxt->dst.bytes = 2;
1056 ctxt->dst.val = fcw;
1057
1058 return X86EMUL_CONTINUE;
1059}
1060
1061static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1062{
1063 u16 fsw;
1064
1065 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1066 return emulate_nm(ctxt);
1067
1068 ctxt->ops->get_fpu(ctxt);
1069 asm volatile("fnstsw %0": "+m"(fsw));
1070 ctxt->ops->put_fpu(ctxt);
1071
1072 /* force 2 byte destination */
1073 ctxt->dst.bytes = 2;
1074 ctxt->dst.val = fsw;
1075
1076 return X86EMUL_CONTINUE;
1077}
1078
Avi Kivity1253791d2011-03-29 11:41:27 +02001079static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001080 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001081{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001082 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001083
Avi Kivity9dac77f2011-06-01 15:34:25 +03001084 if (!(ctxt->d & ModRM))
1085 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity1253791d2011-03-29 11:41:27 +02001086
Avi Kivity9dac77f2011-06-01 15:34:25 +03001087 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001088 op->type = OP_XMM;
1089 op->bytes = 16;
1090 op->addr.xmm = reg;
1091 read_sse_reg(ctxt, &op->vec_val, reg);
1092 return;
1093 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001094 if (ctxt->d & Mmx) {
1095 reg &= 7;
1096 op->type = OP_MM;
1097 op->bytes = 8;
1098 op->addr.mm = reg;
1099 return;
1100 }
Avi Kivity1253791d2011-03-29 11:41:27 +02001101
Avi Kivity3c118e22007-10-31 10:27:04 +02001102 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001103 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1104 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1105
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001106 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001107 op->orig_val = op->val;
1108}
1109
Avi Kivitya6e34072012-06-10 17:15:39 +03001110static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1111{
1112 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1113 ctxt->modrm_seg = VCPU_SREG_SS;
1114}
1115
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001116static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001117 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001118{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001119 u8 sib;
Bandan Das02357bd2014-04-16 12:46:11 -04001120 int index_reg, base_reg, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001121 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001122 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001123
Bandan Das02357bd2014-04-16 12:46:11 -04001124 ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */
1125 index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */
1126 base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001127
Bandan Das02357bd2014-04-16 12:46:11 -04001128 ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001129 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
Bandan Das02357bd2014-04-16 12:46:11 -04001130 ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001131 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001132
Nadav Amit9b88ae92014-05-25 23:05:21 +03001133 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001134 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001135 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001136 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001137 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001138 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001139 op->type = OP_XMM;
1140 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001141 op->addr.xmm = ctxt->modrm_rm;
1142 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001143 return rc;
1144 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001145 if (ctxt->d & Mmx) {
1146 op->type = OP_MM;
1147 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001148 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001149 return rc;
1150 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001151 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001152 return rc;
1153 }
1154
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001155 op->type = OP_MEM;
1156
Avi Kivity9dac77f2011-06-01 15:34:25 +03001157 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001158 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1159 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1160 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1161 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001162
1163 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001164 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001165 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001166 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001167 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001168 break;
1169 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001170 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001171 break;
1172 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001173 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001174 break;
1175 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001176 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001177 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001178 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001179 break;
1180 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001181 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001182 break;
1183 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001184 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001185 break;
1186 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001187 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001188 break;
1189 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001190 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001191 break;
1192 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001193 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001194 break;
1195 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001196 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001197 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001198 break;
1199 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001200 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001201 break;
1202 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001203 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1204 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1205 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001206 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001207 } else {
1208 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001209 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001210 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001211 index_reg |= (sib >> 3) & 7;
1212 base_reg |= sib & 7;
1213 scale = sib >> 6;
1214
Avi Kivity9dac77f2011-06-01 15:34:25 +03001215 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001216 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001217 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001218 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001219 adjust_modrm_seg(ctxt, base_reg);
1220 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001221 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001222 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001223 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001224 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001225 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001226 } else {
1227 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001228 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001229 adjust_modrm_seg(ctxt, base_reg);
1230 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001231 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001232 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001233 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001234 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001235 break;
1236 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001237 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001238 break;
1239 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001240 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001241 break;
1242 }
1243 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001244 op->addr.mem.ea = modrm_ea;
Bandan Das41061cd2014-04-16 12:46:14 -04001245 if (ctxt->ad_bytes != 8)
1246 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
1247
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001248done:
1249 return rc;
1250}
1251
1252static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001253 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001254{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001255 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001256
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001257 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001258 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001259 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001260 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001261 break;
1262 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001263 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001264 break;
1265 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001266 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001267 break;
1268 }
1269done:
1270 return rc;
1271}
1272
Avi Kivity9dac77f2011-06-01 15:34:25 +03001273static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001274{
Sheng Yang7129eec2010-09-28 16:33:32 +08001275 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001276
Avi Kivity9dac77f2011-06-01 15:34:25 +03001277 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001278 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001279
Avi Kivity9dac77f2011-06-01 15:34:25 +03001280 if (ctxt->src.bytes == 2)
1281 sv = (s16)ctxt->src.val & (s16)mask;
1282 else if (ctxt->src.bytes == 4)
1283 sv = (s32)ctxt->src.val & (s32)mask;
Nadav Amit7dec5602014-06-15 16:12:57 +03001284 else
1285 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001286
Avi Kivity9dac77f2011-06-01 15:34:25 +03001287 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001288 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001289
1290 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001291 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001292}
1293
Gleb Natapov9de41572010-04-28 19:15:22 +03001294static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001295 unsigned long addr, void *dest, unsigned size)
1296{
1297 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001298 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001299
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001300 if (mc->pos < mc->end)
1301 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001302
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001303 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001304
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001305 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1306 &ctxt->exception);
1307 if (rc != X86EMUL_CONTINUE)
1308 return rc;
1309
1310 mc->end += size;
1311
1312read_cached:
1313 memcpy(dest, mc->data + mc->pos, size);
1314 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001315 return X86EMUL_CONTINUE;
1316}
1317
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001318static int segmented_read(struct x86_emulate_ctxt *ctxt,
1319 struct segmented_address addr,
1320 void *data,
1321 unsigned size)
1322{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001323 int rc;
1324 ulong linear;
1325
Avi Kivity83b87952011-04-03 11:31:19 +03001326 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001327 if (rc != X86EMUL_CONTINUE)
1328 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001329 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001330}
1331
1332static int segmented_write(struct x86_emulate_ctxt *ctxt,
1333 struct segmented_address addr,
1334 const void *data,
1335 unsigned size)
1336{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001337 int rc;
1338 ulong linear;
1339
Avi Kivity83b87952011-04-03 11:31:19 +03001340 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001341 if (rc != X86EMUL_CONTINUE)
1342 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001343 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1344 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001345}
1346
1347static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1348 struct segmented_address addr,
1349 const void *orig_data, const void *data,
1350 unsigned size)
1351{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001352 int rc;
1353 ulong linear;
1354
Avi Kivity83b87952011-04-03 11:31:19 +03001355 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001356 if (rc != X86EMUL_CONTINUE)
1357 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001358 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1359 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001360}
1361
Gleb Natapov7b262e92010-03-18 15:20:27 +02001362static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001363 unsigned int size, unsigned short port,
1364 void *dest)
1365{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001366 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001367
1368 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001369 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001370 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001371 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001372 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001373 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1374 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Mark Rustadb55a8142014-07-25 06:27:05 -07001375 n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001376 if (n == 0)
1377 n = 1;
1378 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001379 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001380 return 0;
1381 rc->end = n * size;
1382 }
1383
Nadav Amite6e39f02014-04-18 03:35:10 +03001384 if (ctxt->rep_prefix && (ctxt->d & String) &&
1385 !(ctxt->eflags & EFLG_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001386 ctxt->dst.data = rc->data + rc->pos;
1387 ctxt->dst.type = OP_MEM_STR;
1388 ctxt->dst.count = (rc->end - rc->pos) / size;
1389 rc->pos = rc->end;
1390 } else {
1391 memcpy(dest, rc->data + rc->pos, size);
1392 rc->pos += size;
1393 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001394 return 1;
1395}
1396
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001397static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1398 u16 index, struct desc_struct *desc)
1399{
1400 struct desc_ptr dt;
1401 ulong addr;
1402
1403 ctxt->ops->get_idt(ctxt, &dt);
1404
1405 if (dt.size < index * 8 + 7)
1406 return emulate_gp(ctxt, index << 3 | 0x2);
1407
1408 addr = dt.address + index * 8;
1409 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1410 &ctxt->exception);
1411}
1412
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001413static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001414 u16 selector, struct desc_ptr *dt)
1415{
Mathias Krause0225fb52012-08-30 01:30:16 +02001416 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001417 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001418
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001419 if (selector & 1 << 2) {
1420 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001421 u16 sel;
1422
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001423 memset (dt, 0, sizeof *dt);
Nadav Amit2eedcac2014-06-02 18:34:05 +03001424 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1425 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001426 return;
1427
1428 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001429 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001430 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001431 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001432}
1433
1434/* allowed just for 8 bytes segments */
1435static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001436 u16 selector, struct desc_struct *desc,
1437 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001438{
1439 struct desc_ptr dt;
1440 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001441 ulong addr;
1442
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001443 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001444
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001445 if (dt.size < index * 8 + 7)
1446 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001447
Avi Kivitye9194642012-06-13 16:29:39 +03001448 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001449 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1450 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001451}
1452
1453/* allowed just for 8 bytes segments */
1454static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001455 u16 selector, struct desc_struct *desc)
1456{
1457 struct desc_ptr dt;
1458 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001459 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001460
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001461 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001462
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001463 if (dt.size < index * 8 + 7)
1464 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001465
1466 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001467 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1468 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001469}
1470
Gleb Natapov5601d052011-03-07 14:55:06 +02001471/* Does not support long mode */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001472static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Nadav Amitd1442d82014-09-18 22:39:39 +03001473 u16 selector, int seg, u8 cpl,
1474 bool in_task_switch,
1475 struct desc_struct *desc)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001476{
Avi Kivity869be992012-06-13 16:30:53 +03001477 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001478 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001479 unsigned err_vec = GP_VECTOR;
1480 u32 err_code = 0;
1481 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001482 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001483 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001484 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001485 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001486
1487 memset(&seg_desc, 0, sizeof seg_desc);
1488
Kevin Wolff8da94e2013-04-11 14:06:03 +02001489 if (ctxt->mode == X86EMUL_MODE_REAL) {
1490 /* set real mode segment descriptor (keep limit etc. for
1491 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001492 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001493 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001494 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001495 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1496 /* VM86 needs a clean new segment descriptor */
1497 set_desc_base(&seg_desc, selector << 4);
1498 set_desc_limit(&seg_desc, 0xffff);
1499 seg_desc.type = 3;
1500 seg_desc.p = 1;
1501 seg_desc.s = 1;
1502 seg_desc.dpl = 3;
1503 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001504 }
1505
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001506 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001507
1508 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1509 if ((seg == VCPU_SREG_CS
1510 || (seg == VCPU_SREG_SS
1511 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1512 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001513 && null_selector)
1514 goto exception;
1515
1516 /* TR should be in GDT only */
1517 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1518 goto exception;
1519
1520 if (null_selector) /* for NULL selector skip all following checks */
1521 goto load;
1522
Avi Kivitye9194642012-06-13 16:29:39 +03001523 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001524 if (ret != X86EMUL_CONTINUE)
1525 return ret;
1526
1527 err_code = selector & 0xfffc;
Paolo Bonzini15fc0752014-08-18 13:17:00 +02001528 err_vec = in_task_switch ? TS_VECTOR : GP_VECTOR;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001529
Guo Chaofc058682012-06-28 15:19:51 +08001530 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001531 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1532 goto exception;
1533
1534 if (!seg_desc.p) {
1535 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1536 goto exception;
1537 }
1538
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001539 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001540
1541 switch (seg) {
1542 case VCPU_SREG_SS:
1543 /*
1544 * segment is not a writable data segment or segment
1545 * selector's RPL != CPL or segment selector's RPL != CPL
1546 */
1547 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1548 goto exception;
1549 break;
1550 case VCPU_SREG_CS:
1551 if (!(seg_desc.type & 8))
1552 goto exception;
1553
1554 if (seg_desc.type & 4) {
1555 /* conforming */
1556 if (dpl > cpl)
1557 goto exception;
1558 } else {
1559 /* nonconforming */
1560 if (rpl > cpl || dpl != cpl)
1561 goto exception;
1562 }
Nadav Amit040c8dc2014-09-18 22:39:43 +03001563 /* in long-mode d/b must be clear if l is set */
1564 if (seg_desc.d && seg_desc.l) {
1565 u64 efer = 0;
1566
1567 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1568 if (efer & EFER_LMA)
1569 goto exception;
1570 }
1571
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001572 /* CS(RPL) <- CPL */
1573 selector = (selector & 0xfffc) | cpl;
1574 break;
1575 case VCPU_SREG_TR:
1576 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1577 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001578 old_desc = seg_desc;
1579 seg_desc.type |= 2; /* busy */
1580 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1581 sizeof(seg_desc), &ctxt->exception);
1582 if (ret != X86EMUL_CONTINUE)
1583 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001584 break;
1585 case VCPU_SREG_LDTR:
1586 if (seg_desc.s || seg_desc.type != 2)
1587 goto exception;
1588 break;
1589 default: /* DS, ES, FS, or GS */
1590 /*
1591 * segment is not a data or readable code segment or
1592 * ((segment is a data or nonconforming code segment)
1593 * and (both RPL and CPL > DPL))
1594 */
1595 if ((seg_desc.type & 0xa) == 0x8 ||
1596 (((seg_desc.type & 0xc) != 0xc) &&
1597 (rpl > dpl && cpl > dpl)))
1598 goto exception;
1599 break;
1600 }
1601
1602 if (seg_desc.s) {
1603 /* mark segment as accessed */
1604 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001605 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001606 if (ret != X86EMUL_CONTINUE)
1607 return ret;
Nadav Amite37a75a2014-06-02 18:34:04 +03001608 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
1609 ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3,
1610 sizeof(base3), &ctxt->exception);
1611 if (ret != X86EMUL_CONTINUE)
1612 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001613 }
1614load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001615 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Nadav Amitd1442d82014-09-18 22:39:39 +03001616 if (desc)
1617 *desc = seg_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001618 return X86EMUL_CONTINUE;
1619exception:
Paolo Bonzini592f0852014-08-20 10:05:08 +02001620 return emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001621}
1622
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001623static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1624 u16 selector, int seg)
1625{
1626 u8 cpl = ctxt->ops->cpl(ctxt);
Nadav Amitd1442d82014-09-18 22:39:39 +03001627 return __load_segment_descriptor(ctxt, selector, seg, cpl, false, NULL);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001628}
1629
Wei Yongjun31be40b2010-08-17 09:17:30 +08001630static void write_register_operand(struct operand *op)
1631{
1632 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1633 switch (op->bytes) {
1634 case 1:
1635 *(u8 *)op->addr.reg = (u8)op->val;
1636 break;
1637 case 2:
1638 *(u16 *)op->addr.reg = (u16)op->val;
1639 break;
1640 case 4:
1641 *op->addr.reg = (u32)op->val;
1642 break; /* 64b: zero-extend */
1643 case 8:
1644 *op->addr.reg = op->val;
1645 break;
1646 }
1647}
1648
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001649static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001650{
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001651 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001652 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001653 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001654 break;
1655 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001656 if (ctxt->lock_prefix)
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001657 return segmented_cmpxchg(ctxt,
1658 op->addr.mem,
1659 &op->orig_val,
1660 &op->val,
1661 op->bytes);
1662 else
1663 return segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001664 op->addr.mem,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001665 &op->val,
1666 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001667 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001668 case OP_MEM_STR:
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001669 return segmented_write(ctxt,
1670 op->addr.mem,
1671 op->data,
1672 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001673 break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001674 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001675 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001676 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001677 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001678 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001679 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001680 case OP_NONE:
1681 /* no writeback */
1682 break;
1683 default:
1684 break;
1685 }
1686 return X86EMUL_CONTINUE;
1687}
1688
Avi Kivity51ddff52012-06-12 20:19:40 +03001689static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001690{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001691 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001692
Avi Kivity5ad105e2012-08-19 14:34:31 +03001693 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001694 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001695 addr.seg = VCPU_SREG_SS;
1696
Avi Kivity51ddff52012-06-12 20:19:40 +03001697 return segmented_write(ctxt, addr, data, bytes);
1698}
1699
1700static int em_push(struct x86_emulate_ctxt *ctxt)
1701{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001702 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001703 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001704 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001705}
1706
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001707static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001708 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001709{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001710 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001711 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001712
Avi Kivitydd856ef2012-08-27 23:46:17 +03001713 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001714 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001715 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001716 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001717 return rc;
1718
Avi Kivity5ad105e2012-08-19 14:34:31 +03001719 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001720 return rc;
1721}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001722
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001723static int em_pop(struct x86_emulate_ctxt *ctxt)
1724{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001725 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001726}
1727
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001728static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001729 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001730{
1731 int rc;
1732 unsigned long val, change_mask;
1733 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001734 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001735
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001736 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001737 if (rc != X86EMUL_CONTINUE)
1738 return rc;
1739
1740 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
Nadav Amit163b1352014-07-21 14:37:28 +03001741 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_AC | EFLG_ID;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001742
1743 switch(ctxt->mode) {
1744 case X86EMUL_MODE_PROT64:
1745 case X86EMUL_MODE_PROT32:
1746 case X86EMUL_MODE_PROT16:
1747 if (cpl == 0)
1748 change_mask |= EFLG_IOPL;
1749 if (cpl <= iopl)
1750 change_mask |= EFLG_IF;
1751 break;
1752 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001753 if (iopl < 3)
1754 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001755 change_mask |= EFLG_IF;
1756 break;
1757 default: /* real mode */
1758 change_mask |= (EFLG_IOPL | EFLG_IF);
1759 break;
1760 }
1761
1762 *(unsigned long *)dest =
1763 (ctxt->eflags & ~change_mask) | (val & change_mask);
1764
1765 return rc;
1766}
1767
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001768static int em_popf(struct x86_emulate_ctxt *ctxt)
1769{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001770 ctxt->dst.type = OP_REG;
1771 ctxt->dst.addr.reg = &ctxt->eflags;
1772 ctxt->dst.bytes = ctxt->op_bytes;
1773 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001774}
1775
Avi Kivity612e89f2012-06-12 20:03:23 +03001776static int em_enter(struct x86_emulate_ctxt *ctxt)
1777{
1778 int rc;
1779 unsigned frame_size = ctxt->src.val;
1780 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001781 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001782
1783 if (nesting_level)
1784 return X86EMUL_UNHANDLEABLE;
1785
Avi Kivitydd856ef2012-08-27 23:46:17 +03001786 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1787 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001788 if (rc != X86EMUL_CONTINUE)
1789 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001790 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001791 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001792 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1793 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001794 stack_mask(ctxt));
1795 return X86EMUL_CONTINUE;
1796}
1797
Avi Kivityf47cfa32012-06-07 17:49:24 +03001798static int em_leave(struct x86_emulate_ctxt *ctxt)
1799{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001800 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001801 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001802 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001803}
1804
Avi Kivity1cd196e2011-09-13 10:45:51 +03001805static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001806{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001807 int seg = ctxt->src2.val;
1808
Avi Kivity9dac77f2011-06-01 15:34:25 +03001809 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001810
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001811 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001812}
1813
Avi Kivity1cd196e2011-09-13 10:45:51 +03001814static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001815{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001816 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001817 unsigned long selector;
1818 int rc;
1819
Avi Kivity9dac77f2011-06-01 15:34:25 +03001820 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001821 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001822 return rc;
1823
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001824 if (ctxt->modrm_reg == VCPU_SREG_SS)
1825 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
1826
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001827 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001828 return rc;
1829}
1830
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001831static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001832{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001833 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001834 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001835 int reg = VCPU_REGS_RAX;
1836
1837 while (reg <= VCPU_REGS_RDI) {
1838 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001839 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001840
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001841 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001842 if (rc != X86EMUL_CONTINUE)
1843 return rc;
1844
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001845 ++reg;
1846 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001847
Wei Yongjunc37eda12010-06-15 09:03:33 +08001848 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001849}
1850
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001851static int em_pushf(struct x86_emulate_ctxt *ctxt)
1852{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001853 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001854 return em_push(ctxt);
1855}
1856
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001857static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001858{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001859 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001860 int reg = VCPU_REGS_RDI;
1861
1862 while (reg >= VCPU_REGS_RAX) {
1863 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001864 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001865 --reg;
1866 }
1867
Avi Kivitydd856ef2012-08-27 23:46:17 +03001868 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001869 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001870 break;
1871 --reg;
1872 }
1873 return rc;
1874}
1875
Avi Kivitydd856ef2012-08-27 23:46:17 +03001876static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001877{
Mathias Krause0225fb52012-08-30 01:30:16 +02001878 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001879 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001880 struct desc_ptr dt;
1881 gva_t cs_addr;
1882 gva_t eip_addr;
1883 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001884
1885 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001886 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001887 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001888 if (rc != X86EMUL_CONTINUE)
1889 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001890
1891 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1892
Avi Kivity9dac77f2011-06-01 15:34:25 +03001893 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001894 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001895 if (rc != X86EMUL_CONTINUE)
1896 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001897
Avi Kivity9dac77f2011-06-01 15:34:25 +03001898 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001899 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001900 if (rc != X86EMUL_CONTINUE)
1901 return rc;
1902
Avi Kivity4bff1e862011-04-20 13:37:53 +03001903 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001904
1905 eip_addr = dt.address + (irq << 2);
1906 cs_addr = dt.address + (irq << 2) + 2;
1907
Avi Kivity0f65dd72011-04-20 13:37:53 +03001908 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001909 if (rc != X86EMUL_CONTINUE)
1910 return rc;
1911
Avi Kivity0f65dd72011-04-20 13:37:53 +03001912 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001913 if (rc != X86EMUL_CONTINUE)
1914 return rc;
1915
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001916 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001917 if (rc != X86EMUL_CONTINUE)
1918 return rc;
1919
Avi Kivity9dac77f2011-06-01 15:34:25 +03001920 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001921
1922 return rc;
1923}
1924
Avi Kivitydd856ef2012-08-27 23:46:17 +03001925int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1926{
1927 int rc;
1928
1929 invalidate_registers(ctxt);
1930 rc = __emulate_int_real(ctxt, irq);
1931 if (rc == X86EMUL_CONTINUE)
1932 writeback_registers(ctxt);
1933 return rc;
1934}
1935
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001936static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001937{
1938 switch(ctxt->mode) {
1939 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001940 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001941 case X86EMUL_MODE_VM86:
1942 case X86EMUL_MODE_PROT16:
1943 case X86EMUL_MODE_PROT32:
1944 case X86EMUL_MODE_PROT64:
1945 default:
1946 /* Protected mode interrupts unimplemented yet */
1947 return X86EMUL_UNHANDLEABLE;
1948 }
1949}
1950
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001951static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001952{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001953 int rc = X86EMUL_CONTINUE;
1954 unsigned long temp_eip = 0;
1955 unsigned long temp_eflags = 0;
1956 unsigned long cs = 0;
1957 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1958 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1959 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1960 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1961
1962 /* TODO: Add stack limit check */
1963
Avi Kivity9dac77f2011-06-01 15:34:25 +03001964 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001965
1966 if (rc != X86EMUL_CONTINUE)
1967 return rc;
1968
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001969 if (temp_eip & ~0xffff)
1970 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001971
Avi Kivity9dac77f2011-06-01 15:34:25 +03001972 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001973
1974 if (rc != X86EMUL_CONTINUE)
1975 return rc;
1976
Avi Kivity9dac77f2011-06-01 15:34:25 +03001977 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001978
1979 if (rc != X86EMUL_CONTINUE)
1980 return rc;
1981
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001982 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001983
1984 if (rc != X86EMUL_CONTINUE)
1985 return rc;
1986
Avi Kivity9dac77f2011-06-01 15:34:25 +03001987 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001988
1989
Avi Kivity9dac77f2011-06-01 15:34:25 +03001990 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001991 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001992 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001993 ctxt->eflags &= ~0xffff;
1994 ctxt->eflags |= temp_eflags;
1995 }
1996
1997 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1998 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1999
2000 return rc;
2001}
2002
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002003static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002004{
2005 switch(ctxt->mode) {
2006 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002007 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002008 case X86EMUL_MODE_VM86:
2009 case X86EMUL_MODE_PROT16:
2010 case X86EMUL_MODE_PROT32:
2011 case X86EMUL_MODE_PROT64:
2012 default:
2013 /* iret from protected mode unimplemented yet */
2014 return X86EMUL_UNHANDLEABLE;
2015 }
2016}
2017
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002018static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2019{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002020 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002021 unsigned short sel, old_sel;
2022 struct desc_struct old_desc, new_desc;
2023 const struct x86_emulate_ops *ops = ctxt->ops;
2024 u8 cpl = ctxt->ops->cpl(ctxt);
2025
2026 /* Assignment of RIP may only fail in 64-bit mode */
2027 if (ctxt->mode == X86EMUL_MODE_PROT64)
2028 ops->get_segment(ctxt, &old_sel, &old_desc, NULL,
2029 VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002030
Avi Kivity9dac77f2011-06-01 15:34:25 +03002031 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002032
Nadav Amitd1442d82014-09-18 22:39:39 +03002033 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl, false,
2034 &new_desc);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002035 if (rc != X86EMUL_CONTINUE)
2036 return rc;
2037
Nadav Amitd1442d82014-09-18 22:39:39 +03002038 rc = assign_eip_far(ctxt, ctxt->src.val, new_desc.l);
2039 if (rc != X86EMUL_CONTINUE) {
Nadav Amitcd9b8e2c2014-10-28 00:03:43 +02002040 WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64);
Nadav Amitd1442d82014-09-18 22:39:39 +03002041 /* assigning eip failed; restore the old cs */
2042 ops->set_segment(ctxt, old_sel, &old_desc, 0, VCPU_SREG_CS);
2043 return rc;
2044 }
2045 return rc;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002046}
2047
Nadav Amitf7784042014-09-18 22:39:41 +03002048static int em_jmp_abs(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002049{
Nadav Amitf7784042014-09-18 22:39:41 +03002050 return assign_eip_near(ctxt, ctxt->src.val);
2051}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002052
Nadav Amitf7784042014-09-18 22:39:41 +03002053static int em_call_near_abs(struct x86_emulate_ctxt *ctxt)
2054{
2055 int rc;
2056 long int old_eip;
2057
2058 old_eip = ctxt->_eip;
2059 rc = assign_eip_near(ctxt, ctxt->src.val);
2060 if (rc != X86EMUL_CONTINUE)
2061 return rc;
2062 ctxt->src.val = old_eip;
2063 rc = em_push(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002064 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002065}
2066
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002067static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002068{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002069 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002070
Nadav Amitaaa05f22014-06-02 18:34:10 +03002071 if (ctxt->dst.bytes == 16)
2072 return X86EMUL_UNHANDLEABLE;
2073
Avi Kivitydd856ef2012-08-27 23:46:17 +03002074 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2075 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2076 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2077 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002078 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002079 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002080 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2081 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002082
Laurent Vivier05f086f2007-09-24 11:10:55 +02002083 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002084 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002085 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002086}
2087
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002088static int em_ret(struct x86_emulate_ctxt *ctxt)
2089{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002090 int rc;
2091 unsigned long eip;
2092
2093 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
2094 if (rc != X86EMUL_CONTINUE)
2095 return rc;
2096
2097 return assign_eip_near(ctxt, eip);
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002098}
2099
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002100static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002101{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002102 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002103 unsigned long eip, cs;
2104 u16 old_cs;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002105 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amitd1442d82014-09-18 22:39:39 +03002106 struct desc_struct old_desc, new_desc;
2107 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002108
Nadav Amitd1442d82014-09-18 22:39:39 +03002109 if (ctxt->mode == X86EMUL_MODE_PROT64)
2110 ops->get_segment(ctxt, &old_cs, &old_desc, NULL,
2111 VCPU_SREG_CS);
2112
2113 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002114 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002115 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002116 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002117 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002118 return rc;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002119 /* Outer-privilege level return is not implemented */
2120 if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2121 return X86EMUL_UNHANDLEABLE;
Nadav Amitd1442d82014-09-18 22:39:39 +03002122 rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, 0, false,
2123 &new_desc);
2124 if (rc != X86EMUL_CONTINUE)
2125 return rc;
2126 rc = assign_eip_far(ctxt, eip, new_desc.l);
2127 if (rc != X86EMUL_CONTINUE) {
Nadav Amitcd9b8e2c2014-10-28 00:03:43 +02002128 WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64);
Nadav Amitd1442d82014-09-18 22:39:39 +03002129 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
2130 }
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002131 return rc;
2132}
2133
Bruce Rogers32611072013-09-09 09:40:20 -06002134static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2135{
2136 int rc;
2137
2138 rc = em_ret_far(ctxt);
2139 if (rc != X86EMUL_CONTINUE)
2140 return rc;
2141 rsp_increment(ctxt, ctxt->src.val);
2142 return X86EMUL_CONTINUE;
2143}
2144
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002145static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2146{
2147 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002148 ctxt->dst.orig_val = ctxt->dst.val;
2149 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002150 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002151 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002152 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002153
2154 if (ctxt->eflags & EFLG_ZF) {
2155 /* Success: write back to memory. */
2156 ctxt->dst.val = ctxt->src.orig_val;
2157 } else {
2158 /* Failure: write the value we saw to EAX. */
2159 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002160 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Nadav Amit37c564f2014-06-02 18:34:07 +03002161 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002162 }
2163 return X86EMUL_CONTINUE;
2164}
2165
Avi Kivityd4b43252011-09-13 10:45:50 +03002166static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002167{
Avi Kivityd4b43252011-09-13 10:45:50 +03002168 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002169 unsigned short sel;
2170 int rc;
2171
Avi Kivity9dac77f2011-06-01 15:34:25 +03002172 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002173
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002174 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002175 if (rc != X86EMUL_CONTINUE)
2176 return rc;
2177
Avi Kivity9dac77f2011-06-01 15:34:25 +03002178 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002179 return rc;
2180}
2181
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002182static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002183setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002184 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002185{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002186 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002187 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002188 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002189 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002190 cs->type = 0x0b; /* Read, Execute, Accessed */
2191 cs->s = 1;
2192 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002193 cs->p = 1;
2194 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002195 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002196
Gleb Natapov79168fd2010-04-28 19:15:30 +03002197 set_desc_base(ss, 0); /* flat segment */
2198 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002199 ss->g = 1; /* 4kb granularity */
2200 ss->s = 1;
2201 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002202 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002203 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002204 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002205 ss->l = 0;
2206 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002207}
2208
Avi Kivity1a18a692012-02-01 12:23:21 +02002209static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2210{
2211 u32 eax, ebx, ecx, edx;
2212
2213 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002214 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2215 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002216 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2217 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2218}
2219
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002220static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2221{
Mathias Krause0225fb52012-08-30 01:30:16 +02002222 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002223 u32 eax, ebx, ecx, edx;
2224
2225 /*
2226 * syscall should always be enabled in longmode - so only become
2227 * vendor specific (cpuid) if other modes are active...
2228 */
2229 if (ctxt->mode == X86EMUL_MODE_PROT64)
2230 return true;
2231
2232 eax = 0x00000000;
2233 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002234 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2235 /*
2236 * Intel ("GenuineIntel")
2237 * remark: Intel CPUs only support "syscall" in 64bit
2238 * longmode. Also an 64bit guest with a
2239 * 32bit compat-app running will #UD !! While this
2240 * behaviour can be fixed (by emulating) into AMD
2241 * response - CPUs of AMD can't behave like Intel.
2242 */
2243 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2244 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2245 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2246 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002247
Avi Kivity0017f932012-06-07 14:10:16 +03002248 /* AMD ("AuthenticAMD") */
2249 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2250 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2251 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2252 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002253
Avi Kivity0017f932012-06-07 14:10:16 +03002254 /* AMD ("AMDisbetter!") */
2255 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2256 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2257 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2258 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002259
2260 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2261 return false;
2262}
2263
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002264static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002265{
Mathias Krause0225fb52012-08-30 01:30:16 +02002266 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002267 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002268 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002269 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002270 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002271
2272 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002273 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002274 ctxt->mode == X86EMUL_MODE_VM86)
2275 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002276
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002277 if (!(em_syscall_is_enabled(ctxt)))
2278 return emulate_ud(ctxt);
2279
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002280 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002281 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002282
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002283 if (!(efer & EFER_SCE))
2284 return emulate_ud(ctxt);
2285
Avi Kivity717746e2011-04-20 13:37:53 +03002286 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002287 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002288 cs_sel = (u16)(msr_data & 0xfffc);
2289 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002290
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002291 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002292 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002293 cs.l = 1;
2294 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002295 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2296 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002297
Avi Kivitydd856ef2012-08-27 23:46:17 +03002298 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002299 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002300#ifdef CONFIG_X86_64
Nadav Amit6c6cb692014-07-21 14:37:30 +03002301 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002302
Avi Kivity717746e2011-04-20 13:37:53 +03002303 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002304 ctxt->mode == X86EMUL_MODE_PROT64 ?
2305 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002306 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002307
Avi Kivity717746e2011-04-20 13:37:53 +03002308 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Nadav Amit6c6cb692014-07-21 14:37:30 +03002309 ctxt->eflags &= ~msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002310#endif
2311 } else {
2312 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002313 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002314 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002315
Nadav Amit6c6cb692014-07-21 14:37:30 +03002316 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002317 }
2318
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002319 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002320}
2321
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002322static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002323{
Mathias Krause0225fb52012-08-30 01:30:16 +02002324 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002325 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002326 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002327 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002328 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002329
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002330 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002331 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002332 if (ctxt->mode == X86EMUL_MODE_REAL)
2333 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002334
Avi Kivity1a18a692012-02-01 12:23:21 +02002335 /*
2336 * Not recognized on AMD in compat mode (but is recognized in legacy
2337 * mode).
2338 */
2339 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2340 && !vendor_intel(ctxt))
2341 return emulate_ud(ctxt);
2342
Andre Przywara8c604352009-06-18 12:56:01 +02002343 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2344 * Therefore, we inject an #UD.
2345 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002346 if (ctxt->mode == X86EMUL_MODE_PROT64)
2347 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002348
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002349 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002350
Avi Kivity717746e2011-04-20 13:37:53 +03002351 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002352 switch (ctxt->mode) {
2353 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002354 if ((msr_data & 0xfffc) == 0x0)
2355 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002356 break;
2357 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002358 if (msr_data == 0x0)
2359 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002360 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002361 default:
2362 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002363 }
2364
Nadav Amit6c6cb692014-07-21 14:37:30 +03002365 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002366 cs_sel = (u16)msr_data;
2367 cs_sel &= ~SELECTOR_RPL_MASK;
2368 ss_sel = cs_sel + 8;
2369 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002370 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002371 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002372 cs.l = 1;
2373 }
2374
Avi Kivity1aa36612011-04-27 13:20:30 +03002375 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2376 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002377
Avi Kivity717746e2011-04-20 13:37:53 +03002378 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002379 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002380
Avi Kivity717746e2011-04-20 13:37:53 +03002381 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002382 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002383
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002384 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002385}
2386
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002387static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002388{
Mathias Krause0225fb52012-08-30 01:30:16 +02002389 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002390 struct desc_struct cs, ss;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002391 u64 msr_data, rcx, rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002392 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002393 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002394
Gleb Natapova0044752010-02-10 14:21:31 +02002395 /* inject #GP if in real mode or Virtual 8086 mode */
2396 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002397 ctxt->mode == X86EMUL_MODE_VM86)
2398 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002399
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002400 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002401
Avi Kivity9dac77f2011-06-01 15:34:25 +03002402 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002403 usermode = X86EMUL_MODE_PROT64;
2404 else
2405 usermode = X86EMUL_MODE_PROT32;
2406
Nadav Amit234f3ce2014-09-18 22:39:38 +03002407 rcx = reg_read(ctxt, VCPU_REGS_RCX);
2408 rdx = reg_read(ctxt, VCPU_REGS_RDX);
2409
Andre Przywara4668f052009-06-18 12:56:02 +02002410 cs.dpl = 3;
2411 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002412 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002413 switch (usermode) {
2414 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002415 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002416 if ((msr_data & 0xfffc) == 0x0)
2417 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002418 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002419 break;
2420 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002421 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002422 if (msr_data == 0x0)
2423 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002424 ss_sel = cs_sel + 8;
2425 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002426 cs.l = 1;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002427 if (is_noncanonical_address(rcx) ||
2428 is_noncanonical_address(rdx))
2429 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002430 break;
2431 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002432 cs_sel |= SELECTOR_RPL_MASK;
2433 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002434
Avi Kivity1aa36612011-04-27 13:20:30 +03002435 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2436 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002437
Nadav Amit234f3ce2014-09-18 22:39:38 +03002438 ctxt->_eip = rdx;
2439 *reg_write(ctxt, VCPU_REGS_RSP) = rcx;
Andre Przywara4668f052009-06-18 12:56:02 +02002440
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002441 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002442}
2443
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002444static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002445{
2446 int iopl;
2447 if (ctxt->mode == X86EMUL_MODE_REAL)
2448 return false;
2449 if (ctxt->mode == X86EMUL_MODE_VM86)
2450 return true;
2451 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002452 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002453}
2454
2455static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002456 u16 port, u16 len)
2457{
Mathias Krause0225fb52012-08-30 01:30:16 +02002458 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002459 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002460 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002461 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002462 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002463 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002464 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002465
Avi Kivity1aa36612011-04-27 13:20:30 +03002466 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002467 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002468 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002469 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002470 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002471 base = get_desc_base(&tr_seg);
2472#ifdef CONFIG_X86_64
2473 base |= ((u64)base3) << 32;
2474#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002475 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002476 if (r != X86EMUL_CONTINUE)
2477 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002478 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002479 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002480 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002481 if (r != X86EMUL_CONTINUE)
2482 return false;
2483 if ((perm >> bit_idx) & mask)
2484 return false;
2485 return true;
2486}
2487
2488static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002489 u16 port, u16 len)
2490{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002491 if (ctxt->perm_ok)
2492 return true;
2493
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002494 if (emulator_bad_iopl(ctxt))
2495 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002496 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002497
2498 ctxt->perm_ok = true;
2499
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002500 return true;
2501}
2502
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002503static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002504 struct tss_segment_16 *tss)
2505{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002506 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002507 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002508 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2509 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2510 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2511 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2512 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2513 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2514 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2515 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002516
Avi Kivity1aa36612011-04-27 13:20:30 +03002517 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2518 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2519 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2520 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2521 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002522}
2523
2524static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002525 struct tss_segment_16 *tss)
2526{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002527 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002528 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002529
Avi Kivity9dac77f2011-06-01 15:34:25 +03002530 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002531 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002532 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2533 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2534 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2535 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2536 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2537 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2538 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2539 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002540
2541 /*
2542 * SDM says that segment selectors are loaded before segment
2543 * descriptors
2544 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002545 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2546 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2547 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2548 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2549 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002550
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002551 cpl = tss->cs & 3;
2552
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002553 /*
Guo Chaofc058682012-06-28 15:19:51 +08002554 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002555 * it is handled in a context of new task
2556 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002557 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl,
2558 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002559 if (ret != X86EMUL_CONTINUE)
2560 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002561 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
2562 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002563 if (ret != X86EMUL_CONTINUE)
2564 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002565 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
2566 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002567 if (ret != X86EMUL_CONTINUE)
2568 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002569 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
2570 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002571 if (ret != X86EMUL_CONTINUE)
2572 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002573 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
2574 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002575 if (ret != X86EMUL_CONTINUE)
2576 return ret;
2577
2578 return X86EMUL_CONTINUE;
2579}
2580
2581static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002582 u16 tss_selector, u16 old_tss_sel,
2583 ulong old_tss_base, struct desc_struct *new_desc)
2584{
Mathias Krause0225fb52012-08-30 01:30:16 +02002585 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002586 struct tss_segment_16 tss_seg;
2587 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002588 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002589
Avi Kivity0f65dd72011-04-20 13:37:53 +03002590 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002591 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002592 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002593 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002594 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002595
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002596 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002597
Avi Kivity0f65dd72011-04-20 13:37:53 +03002598 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002599 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002600 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002601 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002602 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002603
Avi Kivity0f65dd72011-04-20 13:37:53 +03002604 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002605 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002606 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002607 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002608 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002609
2610 if (old_tss_sel != 0xffff) {
2611 tss_seg.prev_task_link = old_tss_sel;
2612
Avi Kivity0f65dd72011-04-20 13:37:53 +03002613 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002614 &tss_seg.prev_task_link,
2615 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002616 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002617 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002618 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002619 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002620 }
2621
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002622 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002623}
2624
2625static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002626 struct tss_segment_32 *tss)
2627{
Nadav Amit5c7411e2014-04-07 18:37:47 +03002628 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002629 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002630 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002631 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2632 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2633 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2634 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2635 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2636 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2637 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2638 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002639
Avi Kivity1aa36612011-04-27 13:20:30 +03002640 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2641 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2642 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2643 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2644 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2645 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002646}
2647
2648static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002649 struct tss_segment_32 *tss)
2650{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002651 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002652 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002653
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002654 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002655 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002656 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002657 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002658
2659 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002660 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2661 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2662 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2663 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2664 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2665 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2666 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2667 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002668
2669 /*
2670 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002671 * descriptors. This is important because CPL checks will
2672 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002673 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002674 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2675 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2676 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2677 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2678 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2679 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2680 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002681
2682 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002683 * If we're switching between Protected Mode and VM86, we need to make
2684 * sure to update the mode before loading the segment descriptors so
2685 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01002686 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002687 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002688 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002689 cpl = 3;
2690 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002691 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002692 cpl = tss->cs & 3;
2693 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01002694
2695 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002696 * Now load segment descriptors. If fault happenes at this stage
2697 * it is handled in a context of new task
2698 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002699 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR,
2700 cpl, true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002701 if (ret != X86EMUL_CONTINUE)
2702 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002703 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
2704 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002705 if (ret != X86EMUL_CONTINUE)
2706 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002707 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
2708 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002709 if (ret != X86EMUL_CONTINUE)
2710 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002711 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
2712 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002713 if (ret != X86EMUL_CONTINUE)
2714 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002715 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
2716 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002717 if (ret != X86EMUL_CONTINUE)
2718 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002719 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl,
2720 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002721 if (ret != X86EMUL_CONTINUE)
2722 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002723 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl,
2724 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002725 if (ret != X86EMUL_CONTINUE)
2726 return ret;
2727
2728 return X86EMUL_CONTINUE;
2729}
2730
2731static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002732 u16 tss_selector, u16 old_tss_sel,
2733 ulong old_tss_base, struct desc_struct *new_desc)
2734{
Mathias Krause0225fb52012-08-30 01:30:16 +02002735 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002736 struct tss_segment_32 tss_seg;
2737 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002738 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03002739 u32 eip_offset = offsetof(struct tss_segment_32, eip);
2740 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002741
Avi Kivity0f65dd72011-04-20 13:37:53 +03002742 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002743 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002744 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002745 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002746 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002747
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002748 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002749
Nadav Amit5c7411e2014-04-07 18:37:47 +03002750 /* Only GP registers and segment selectors are saved */
2751 ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
2752 ldt_sel_offset - eip_offset, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002753 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002754 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002755 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002756
Avi Kivity0f65dd72011-04-20 13:37:53 +03002757 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002758 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002759 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002760 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002761 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002762
2763 if (old_tss_sel != 0xffff) {
2764 tss_seg.prev_task_link = old_tss_sel;
2765
Avi Kivity0f65dd72011-04-20 13:37:53 +03002766 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002767 &tss_seg.prev_task_link,
2768 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002769 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002770 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002771 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002772 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002773 }
2774
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002775 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002776}
2777
2778static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002779 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002780 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002781{
Mathias Krause0225fb52012-08-30 01:30:16 +02002782 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002783 struct desc_struct curr_tss_desc, next_tss_desc;
2784 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002785 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002786 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002787 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002788 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002789 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002790
2791 /* FIXME: old_tss_base == ~0 ? */
2792
Avi Kivitye9194642012-06-13 16:29:39 +03002793 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002794 if (ret != X86EMUL_CONTINUE)
2795 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002796 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002797 if (ret != X86EMUL_CONTINUE)
2798 return ret;
2799
2800 /* FIXME: check that next_tss_desc is tss */
2801
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002802 /*
2803 * Check privileges. The three cases are task switch caused by...
2804 *
2805 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2806 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002807 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002808 */
2809 if (reason == TASK_SWITCH_GATE) {
2810 if (idt_index != -1) {
2811 /* Software interrupts */
2812 struct desc_struct task_gate_desc;
2813 int dpl;
2814
2815 ret = read_interrupt_descriptor(ctxt, idt_index,
2816 &task_gate_desc);
2817 if (ret != X86EMUL_CONTINUE)
2818 return ret;
2819
2820 dpl = task_gate_desc.dpl;
2821 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2822 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2823 }
2824 } else if (reason != TASK_SWITCH_IRET) {
2825 int dpl = next_tss_desc.dpl;
2826 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2827 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002828 }
2829
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002830
Gleb Natapovceffb452010-03-18 15:20:19 +02002831 desc_limit = desc_limit_scaled(&next_tss_desc);
2832 if (!next_tss_desc.p ||
2833 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2834 desc_limit < 0x2b)) {
Paolo Bonzini592f0852014-08-20 10:05:08 +02002835 return emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002836 }
2837
2838 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2839 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002840 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002841 }
2842
2843 if (reason == TASK_SWITCH_IRET)
2844 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2845
2846 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002847 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002848 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2849 old_tss_sel = 0xffff;
2850
2851 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002852 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002853 old_tss_base, &next_tss_desc);
2854 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002855 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002856 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002857 if (ret != X86EMUL_CONTINUE)
2858 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002859
2860 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2861 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2862
2863 if (reason != TASK_SWITCH_IRET) {
2864 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002865 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002866 }
2867
Avi Kivity717746e2011-04-20 13:37:53 +03002868 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002869 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002870
Jan Kiszkae269fb22010-04-14 15:51:09 +02002871 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002872 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2873 ctxt->lock_prefix = 0;
2874 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002875 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002876 }
2877
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002878 return ret;
2879}
2880
2881int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002882 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002883 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002884{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002885 int rc;
2886
Avi Kivitydd856ef2012-08-27 23:46:17 +03002887 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002888 ctxt->_eip = ctxt->eip;
2889 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002890
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002891 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002892 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002893
Avi Kivitydd856ef2012-08-27 23:46:17 +03002894 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002895 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002896 writeback_registers(ctxt);
2897 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002898
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002899 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002900}
2901
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002902static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2903 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002904{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002905 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002906
Avi Kivitydd856ef2012-08-27 23:46:17 +03002907 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2908 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002909}
2910
Avi Kivity7af04fc2010-08-18 14:16:35 +03002911static int em_das(struct x86_emulate_ctxt *ctxt)
2912{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002913 u8 al, old_al;
2914 bool af, cf, old_cf;
2915
2916 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002917 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002918
2919 old_al = al;
2920 old_cf = cf;
2921 cf = false;
2922 af = ctxt->eflags & X86_EFLAGS_AF;
2923 if ((al & 0x0f) > 9 || af) {
2924 al -= 6;
2925 cf = old_cf | (al >= 250);
2926 af = true;
2927 } else {
2928 af = false;
2929 }
2930 if (old_al > 0x99 || old_cf) {
2931 al -= 0x60;
2932 cf = true;
2933 }
2934
Avi Kivity9dac77f2011-06-01 15:34:25 +03002935 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002936 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002937 ctxt->src.type = OP_IMM;
2938 ctxt->src.val = 0;
2939 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02002940 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03002941 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2942 if (cf)
2943 ctxt->eflags |= X86_EFLAGS_CF;
2944 if (af)
2945 ctxt->eflags |= X86_EFLAGS_AF;
2946 return X86EMUL_CONTINUE;
2947}
2948
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02002949static int em_aam(struct x86_emulate_ctxt *ctxt)
2950{
2951 u8 al, ah;
2952
2953 if (ctxt->src.val == 0)
2954 return emulate_de(ctxt);
2955
2956 al = ctxt->dst.val & 0xff;
2957 ah = al / ctxt->src.val;
2958 al %= ctxt->src.val;
2959
2960 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
2961
2962 /* Set PF, ZF, SF */
2963 ctxt->src.type = OP_IMM;
2964 ctxt->src.val = 0;
2965 ctxt->src.bytes = 1;
2966 fastop(ctxt, em_or);
2967
2968 return X86EMUL_CONTINUE;
2969}
2970
Gleb Natapov7f662272012-12-10 11:42:30 +02002971static int em_aad(struct x86_emulate_ctxt *ctxt)
2972{
2973 u8 al = ctxt->dst.val & 0xff;
2974 u8 ah = (ctxt->dst.val >> 8) & 0xff;
2975
2976 al = (al + (ah * ctxt->src.val)) & 0xff;
2977
2978 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
2979
Gleb Natapovf583c292013-02-13 17:50:39 +02002980 /* Set PF, ZF, SF */
2981 ctxt->src.type = OP_IMM;
2982 ctxt->src.val = 0;
2983 ctxt->src.bytes = 1;
2984 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02002985
2986 return X86EMUL_CONTINUE;
2987}
2988
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002989static int em_call(struct x86_emulate_ctxt *ctxt)
2990{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002991 int rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002992 long rel = ctxt->src.val;
2993
2994 ctxt->src.val = (unsigned long)ctxt->_eip;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002995 rc = jmp_rel(ctxt, rel);
2996 if (rc != X86EMUL_CONTINUE)
2997 return rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002998 return em_push(ctxt);
2999}
3000
Avi Kivity0ef753b2010-08-18 14:51:45 +03003001static int em_call_far(struct x86_emulate_ctxt *ctxt)
3002{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003003 u16 sel, old_cs;
3004 ulong old_eip;
3005 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03003006 struct desc_struct old_desc, new_desc;
3007 const struct x86_emulate_ops *ops = ctxt->ops;
3008 int cpl = ctxt->ops->cpl(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003009
Avi Kivity9dac77f2011-06-01 15:34:25 +03003010 old_eip = ctxt->_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003011 ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003012
Avi Kivity9dac77f2011-06-01 15:34:25 +03003013 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Nadav Amitd1442d82014-09-18 22:39:39 +03003014 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl, false,
3015 &new_desc);
3016 if (rc != X86EMUL_CONTINUE)
Avi Kivity0ef753b2010-08-18 14:51:45 +03003017 return X86EMUL_CONTINUE;
3018
Nadav Amitd1442d82014-09-18 22:39:39 +03003019 rc = assign_eip_far(ctxt, ctxt->src.val, new_desc.l);
3020 if (rc != X86EMUL_CONTINUE)
3021 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003022
Avi Kivity9dac77f2011-06-01 15:34:25 +03003023 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003024 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003025 if (rc != X86EMUL_CONTINUE)
Nadav Amitd1442d82014-09-18 22:39:39 +03003026 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003027
Avi Kivity9dac77f2011-06-01 15:34:25 +03003028 ctxt->src.val = old_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003029 rc = em_push(ctxt);
3030 /* If we failed, we tainted the memory, but the very least we should
3031 restore cs */
3032 if (rc != X86EMUL_CONTINUE)
3033 goto fail;
3034 return rc;
3035fail:
3036 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
3037 return rc;
3038
Avi Kivity0ef753b2010-08-18 14:51:45 +03003039}
3040
Avi Kivity40ece7c2010-08-18 15:12:09 +03003041static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3042{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003043 int rc;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003044 unsigned long eip;
Avi Kivity40ece7c2010-08-18 15:12:09 +03003045
Nadav Amit234f3ce2014-09-18 22:39:38 +03003046 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
3047 if (rc != X86EMUL_CONTINUE)
3048 return rc;
3049 rc = assign_eip_near(ctxt, eip);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003050 if (rc != X86EMUL_CONTINUE)
3051 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003052 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003053 return X86EMUL_CONTINUE;
3054}
3055
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003056static int em_xchg(struct x86_emulate_ctxt *ctxt)
3057{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003058 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003059 ctxt->src.val = ctxt->dst.val;
3060 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003061
3062 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003063 ctxt->dst.val = ctxt->src.orig_val;
3064 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003065 return X86EMUL_CONTINUE;
3066}
3067
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003068static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3069{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003070 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003071 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003072}
3073
Avi Kivity61429142010-08-19 15:13:00 +03003074static int em_cwd(struct x86_emulate_ctxt *ctxt)
3075{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003076 ctxt->dst.type = OP_REG;
3077 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003078 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003079 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003080
3081 return X86EMUL_CONTINUE;
3082}
3083
Avi Kivity48bb5d32010-08-18 18:54:34 +03003084static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3085{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003086 u64 tsc = 0;
3087
Avi Kivity717746e2011-04-20 13:37:53 +03003088 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003089 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3090 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003091 return X86EMUL_CONTINUE;
3092}
3093
Avi Kivity222d21a2011-11-10 14:57:30 +02003094static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3095{
3096 u64 pmc;
3097
Avi Kivitydd856ef2012-08-27 23:46:17 +03003098 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003099 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003100 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3101 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003102 return X86EMUL_CONTINUE;
3103}
3104
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003105static int em_mov(struct x86_emulate_ctxt *ctxt)
3106{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01003107 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003108 return X86EMUL_CONTINUE;
3109}
3110
Borislav Petkov84cffe42013-10-29 12:54:56 +01003111#define FFL(x) bit(X86_FEATURE_##x)
3112
3113static int em_movbe(struct x86_emulate_ctxt *ctxt)
3114{
3115 u32 ebx, ecx, edx, eax = 1;
3116 u16 tmp;
3117
3118 /*
3119 * Check MOVBE is set in the guest-visible CPUID leaf.
3120 */
3121 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
3122 if (!(ecx & FFL(MOVBE)))
3123 return emulate_ud(ctxt);
3124
3125 switch (ctxt->op_bytes) {
3126 case 2:
3127 /*
3128 * From MOVBE definition: "...When the operand size is 16 bits,
3129 * the upper word of the destination register remains unchanged
3130 * ..."
3131 *
3132 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3133 * rules so we have to do the operation almost per hand.
3134 */
3135 tmp = (u16)ctxt->src.val;
3136 ctxt->dst.val &= ~0xffffUL;
3137 ctxt->dst.val |= (unsigned long)swab16(tmp);
3138 break;
3139 case 4:
3140 ctxt->dst.val = swab32((u32)ctxt->src.val);
3141 break;
3142 case 8:
3143 ctxt->dst.val = swab64(ctxt->src.val);
3144 break;
3145 default:
Paolo Bonzini592f0852014-08-20 10:05:08 +02003146 BUG();
Borislav Petkov84cffe42013-10-29 12:54:56 +01003147 }
3148 return X86EMUL_CONTINUE;
3149}
3150
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003151static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3152{
3153 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3154 return emulate_gp(ctxt, 0);
3155
3156 /* Disable writeback. */
3157 ctxt->dst.type = OP_NONE;
3158 return X86EMUL_CONTINUE;
3159}
3160
3161static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3162{
3163 unsigned long val;
3164
3165 if (ctxt->mode == X86EMUL_MODE_PROT64)
3166 val = ctxt->src.val & ~0ULL;
3167 else
3168 val = ctxt->src.val & ~0U;
3169
3170 /* #UD condition is already handled. */
3171 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3172 return emulate_gp(ctxt, 0);
3173
3174 /* Disable writeback. */
3175 ctxt->dst.type = OP_NONE;
3176 return X86EMUL_CONTINUE;
3177}
3178
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003179static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3180{
3181 u64 msr_data;
3182
Avi Kivitydd856ef2012-08-27 23:46:17 +03003183 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3184 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3185 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003186 return emulate_gp(ctxt, 0);
3187
3188 return X86EMUL_CONTINUE;
3189}
3190
3191static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3192{
3193 u64 msr_data;
3194
Avi Kivitydd856ef2012-08-27 23:46:17 +03003195 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003196 return emulate_gp(ctxt, 0);
3197
Avi Kivitydd856ef2012-08-27 23:46:17 +03003198 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3199 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003200 return X86EMUL_CONTINUE;
3201}
3202
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003203static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3204{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003205 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003206 return emulate_ud(ctxt);
3207
Avi Kivity9dac77f2011-06-01 15:34:25 +03003208 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003209 return X86EMUL_CONTINUE;
3210}
3211
3212static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3213{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003214 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003215
Avi Kivity9dac77f2011-06-01 15:34:25 +03003216 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003217 return emulate_ud(ctxt);
3218
Avi Kivity9dac77f2011-06-01 15:34:25 +03003219 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003220 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3221
3222 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003223 ctxt->dst.type = OP_NONE;
3224 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003225}
3226
Avi Kivitya14e5792012-06-13 12:28:33 +03003227static int em_lldt(struct x86_emulate_ctxt *ctxt)
3228{
3229 u16 sel = ctxt->src.val;
3230
3231 /* Disable writeback. */
3232 ctxt->dst.type = OP_NONE;
3233 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3234}
3235
Avi Kivity80890002012-06-13 16:33:29 +03003236static int em_ltr(struct x86_emulate_ctxt *ctxt)
3237{
3238 u16 sel = ctxt->src.val;
3239
3240 /* Disable writeback. */
3241 ctxt->dst.type = OP_NONE;
3242 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3243}
3244
Avi Kivity38503912011-03-31 18:48:09 +02003245static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3246{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003247 int rc;
3248 ulong linear;
3249
Avi Kivity9dac77f2011-06-01 15:34:25 +03003250 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003251 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003252 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003253 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003254 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003255 return X86EMUL_CONTINUE;
3256}
3257
Avi Kivity2d04a052011-04-20 15:32:49 +03003258static int em_clts(struct x86_emulate_ctxt *ctxt)
3259{
3260 ulong cr0;
3261
3262 cr0 = ctxt->ops->get_cr(ctxt, 0);
3263 cr0 &= ~X86_CR0_TS;
3264 ctxt->ops->set_cr(ctxt, 0, cr0);
3265 return X86EMUL_CONTINUE;
3266}
3267
Avi Kivity26d05cc2011-04-21 12:07:59 +03003268static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3269{
Nadav Amit0f54a322014-08-29 11:26:55 +03003270 int rc = ctxt->ops->fix_hypercall(ctxt);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003271
Avi Kivity26d05cc2011-04-21 12:07:59 +03003272 if (rc != X86EMUL_CONTINUE)
3273 return rc;
3274
3275 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003276 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003277 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003278 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003279 return X86EMUL_CONTINUE;
3280}
3281
Avi Kivity96051572012-06-10 17:21:18 +03003282static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3283 void (*get)(struct x86_emulate_ctxt *ctxt,
3284 struct desc_ptr *ptr))
3285{
3286 struct desc_ptr desc_ptr;
3287
3288 if (ctxt->mode == X86EMUL_MODE_PROT64)
3289 ctxt->op_bytes = 8;
3290 get(ctxt, &desc_ptr);
3291 if (ctxt->op_bytes == 2) {
3292 ctxt->op_bytes = 4;
3293 desc_ptr.address &= 0x00ffffff;
3294 }
3295 /* Disable writeback. */
3296 ctxt->dst.type = OP_NONE;
3297 return segmented_write(ctxt, ctxt->dst.addr.mem,
3298 &desc_ptr, 2 + ctxt->op_bytes);
3299}
3300
3301static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3302{
3303 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3304}
3305
3306static int em_sidt(struct x86_emulate_ctxt *ctxt)
3307{
3308 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3309}
3310
Avi Kivity26d05cc2011-04-21 12:07:59 +03003311static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3312{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003313 struct desc_ptr desc_ptr;
3314 int rc;
3315
Avi Kivity510425f2012-06-07 17:04:36 +03003316 if (ctxt->mode == X86EMUL_MODE_PROT64)
3317 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003318 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003319 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003320 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003321 if (rc != X86EMUL_CONTINUE)
3322 return rc;
3323 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3324 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003325 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003326 return X86EMUL_CONTINUE;
3327}
3328
Avi Kivity5ef39c72011-04-21 12:21:50 +03003329static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003330{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003331 int rc;
3332
Avi Kivity5ef39c72011-04-21 12:21:50 +03003333 rc = ctxt->ops->fix_hypercall(ctxt);
3334
Avi Kivity26d05cc2011-04-21 12:07:59 +03003335 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003336 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003337 return rc;
3338}
3339
3340static int em_lidt(struct x86_emulate_ctxt *ctxt)
3341{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003342 struct desc_ptr desc_ptr;
3343 int rc;
3344
Avi Kivity510425f2012-06-07 17:04:36 +03003345 if (ctxt->mode == X86EMUL_MODE_PROT64)
3346 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003347 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003348 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003349 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003350 if (rc != X86EMUL_CONTINUE)
3351 return rc;
3352 ctxt->ops->set_idt(ctxt, &desc_ptr);
3353 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003354 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003355 return X86EMUL_CONTINUE;
3356}
3357
3358static int em_smsw(struct x86_emulate_ctxt *ctxt)
3359{
Nadav Amit32e94d02014-06-02 18:34:11 +03003360 if (ctxt->dst.type == OP_MEM)
3361 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003362 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003363 return X86EMUL_CONTINUE;
3364}
3365
3366static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3367{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003368 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003369 | (ctxt->src.val & 0x0f));
3370 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003371 return X86EMUL_CONTINUE;
3372}
3373
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003374static int em_loop(struct x86_emulate_ctxt *ctxt)
3375{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003376 int rc = X86EMUL_CONTINUE;
3377
Avi Kivitydd856ef2012-08-27 23:46:17 +03003378 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3379 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003380 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
Nadav Amit234f3ce2014-09-18 22:39:38 +03003381 rc = jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003382
Nadav Amit234f3ce2014-09-18 22:39:38 +03003383 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003384}
3385
3386static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3387{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003388 int rc = X86EMUL_CONTINUE;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003389
Nadav Amit234f3ce2014-09-18 22:39:38 +03003390 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
3391 rc = jmp_rel(ctxt, ctxt->src.val);
3392
3393 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003394}
3395
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003396static int em_in(struct x86_emulate_ctxt *ctxt)
3397{
3398 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3399 &ctxt->dst.val))
3400 return X86EMUL_IO_NEEDED;
3401
3402 return X86EMUL_CONTINUE;
3403}
3404
3405static int em_out(struct x86_emulate_ctxt *ctxt)
3406{
3407 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3408 &ctxt->src.val, 1);
3409 /* Disable writeback. */
3410 ctxt->dst.type = OP_NONE;
3411 return X86EMUL_CONTINUE;
3412}
3413
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003414static int em_cli(struct x86_emulate_ctxt *ctxt)
3415{
3416 if (emulator_bad_iopl(ctxt))
3417 return emulate_gp(ctxt, 0);
3418
3419 ctxt->eflags &= ~X86_EFLAGS_IF;
3420 return X86EMUL_CONTINUE;
3421}
3422
3423static int em_sti(struct x86_emulate_ctxt *ctxt)
3424{
3425 if (emulator_bad_iopl(ctxt))
3426 return emulate_gp(ctxt, 0);
3427
3428 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3429 ctxt->eflags |= X86_EFLAGS_IF;
3430 return X86EMUL_CONTINUE;
3431}
3432
Avi Kivity6d6eede2012-06-07 14:11:36 +03003433static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3434{
3435 u32 eax, ebx, ecx, edx;
3436
Avi Kivitydd856ef2012-08-27 23:46:17 +03003437 eax = reg_read(ctxt, VCPU_REGS_RAX);
3438 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003439 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003440 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3441 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3442 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3443 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003444 return X86EMUL_CONTINUE;
3445}
3446
Paolo Bonzini98f73632013-10-31 11:19:42 +01003447static int em_sahf(struct x86_emulate_ctxt *ctxt)
3448{
3449 u32 flags;
3450
3451 flags = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF;
3452 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3453
3454 ctxt->eflags &= ~0xffUL;
3455 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3456 return X86EMUL_CONTINUE;
3457}
3458
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003459static int em_lahf(struct x86_emulate_ctxt *ctxt)
3460{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003461 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3462 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003463 return X86EMUL_CONTINUE;
3464}
3465
Avi Kivity92998362012-06-13 12:25:06 +03003466static int em_bswap(struct x86_emulate_ctxt *ctxt)
3467{
3468 switch (ctxt->op_bytes) {
3469#ifdef CONFIG_X86_64
3470 case 8:
3471 asm("bswap %0" : "+r"(ctxt->dst.val));
3472 break;
3473#endif
3474 default:
3475 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3476 break;
3477 }
3478 return X86EMUL_CONTINUE;
3479}
3480
Nadav Amit13e457e2014-10-13 13:04:13 +03003481static int em_clflush(struct x86_emulate_ctxt *ctxt)
3482{
3483 /* emulating clflush regardless of cpuid */
3484 return X86EMUL_CONTINUE;
3485}
3486
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003487static bool valid_cr(int nr)
3488{
3489 switch (nr) {
3490 case 0:
3491 case 2 ... 4:
3492 case 8:
3493 return true;
3494 default:
3495 return false;
3496 }
3497}
3498
3499static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3500{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003501 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003502 return emulate_ud(ctxt);
3503
3504 return X86EMUL_CONTINUE;
3505}
3506
3507static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3508{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003509 u64 new_val = ctxt->src.val64;
3510 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003511 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003512
3513 static u64 cr_reserved_bits[] = {
3514 0xffffffff00000000ULL,
3515 0, 0, 0, /* CR3 checked later */
3516 CR4_RESERVED_BITS,
3517 0, 0, 0,
3518 CR8_RESERVED_BITS,
3519 };
3520
3521 if (!valid_cr(cr))
3522 return emulate_ud(ctxt);
3523
3524 if (new_val & cr_reserved_bits[cr])
3525 return emulate_gp(ctxt, 0);
3526
3527 switch (cr) {
3528 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003529 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003530 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3531 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3532 return emulate_gp(ctxt, 0);
3533
Avi Kivity717746e2011-04-20 13:37:53 +03003534 cr4 = ctxt->ops->get_cr(ctxt, 4);
3535 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003536
3537 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3538 !(cr4 & X86_CR4_PAE))
3539 return emulate_gp(ctxt, 0);
3540
3541 break;
3542 }
3543 case 3: {
3544 u64 rsvd = 0;
3545
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003546 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3547 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003548 rsvd = CR3_L_MODE_RESERVED_BITS;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003549
3550 if (new_val & rsvd)
3551 return emulate_gp(ctxt, 0);
3552
3553 break;
3554 }
3555 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003556 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003557
3558 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3559 return emulate_gp(ctxt, 0);
3560
3561 break;
3562 }
3563 }
3564
3565 return X86EMUL_CONTINUE;
3566}
3567
Joerg Roedel3b88e412011-04-04 12:39:29 +02003568static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3569{
3570 unsigned long dr7;
3571
Avi Kivity717746e2011-04-20 13:37:53 +03003572 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003573
3574 /* Check if DR7.Global_Enable is set */
3575 return dr7 & (1 << 13);
3576}
3577
3578static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3579{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003580 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003581 u64 cr4;
3582
3583 if (dr > 7)
3584 return emulate_ud(ctxt);
3585
Avi Kivity717746e2011-04-20 13:37:53 +03003586 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003587 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3588 return emulate_ud(ctxt);
3589
3590 if (check_dr7_gd(ctxt))
3591 return emulate_db(ctxt);
3592
3593 return X86EMUL_CONTINUE;
3594}
3595
3596static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3597{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003598 u64 new_val = ctxt->src.val64;
3599 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003600
3601 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3602 return emulate_gp(ctxt, 0);
3603
3604 return check_dr_read(ctxt);
3605}
3606
Joerg Roedel01de8b02011-04-04 12:39:31 +02003607static int check_svme(struct x86_emulate_ctxt *ctxt)
3608{
3609 u64 efer;
3610
Avi Kivity717746e2011-04-20 13:37:53 +03003611 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003612
3613 if (!(efer & EFER_SVME))
3614 return emulate_ud(ctxt);
3615
3616 return X86EMUL_CONTINUE;
3617}
3618
3619static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3620{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003621 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003622
3623 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003624 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003625 return emulate_gp(ctxt, 0);
3626
3627 return check_svme(ctxt);
3628}
3629
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003630static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3631{
Avi Kivity717746e2011-04-20 13:37:53 +03003632 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003633
Avi Kivity717746e2011-04-20 13:37:53 +03003634 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003635 return emulate_ud(ctxt);
3636
3637 return X86EMUL_CONTINUE;
3638}
3639
Joerg Roedel80612522011-04-04 12:39:33 +02003640static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3641{
Avi Kivity717746e2011-04-20 13:37:53 +03003642 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003643 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003644
Avi Kivity717746e2011-04-20 13:37:53 +03003645 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03003646 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02003647 return emulate_gp(ctxt, 0);
3648
3649 return X86EMUL_CONTINUE;
3650}
3651
Joerg Roedelf6511932011-04-04 12:39:35 +02003652static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3653{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003654 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3655 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003656 return emulate_gp(ctxt, 0);
3657
3658 return X86EMUL_CONTINUE;
3659}
3660
3661static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3662{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003663 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3664 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003665 return emulate_gp(ctxt, 0);
3666
3667 return X86EMUL_CONTINUE;
3668}
3669
Avi Kivity73fba5f2010-07-29 15:11:53 +03003670#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003671#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
3672#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
3673 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003674#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003675#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003676#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3677#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003678#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003679#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003680#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003681#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003682 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003683#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003684 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
3685 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003686#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003687
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003688#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003689#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003690#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003691#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003692#define I2bvIP(_f, _e, _i, _p) \
3693 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003694
Avi Kivityfb864fb2013-01-04 16:18:54 +02003695#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3696 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3697 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003698
Nadav Amit0f54a322014-08-29 11:26:55 +03003699static const struct opcode group7_rm0[] = {
3700 N,
3701 I(SrcNone | Priv | EmulateOnUD, em_vmcall),
3702 N, N, N, N, N, N,
3703};
3704
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003705static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003706 DI(SrcNone | Priv, monitor),
3707 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003708 N, N, N, N, N, N,
3709};
3710
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003711static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003712 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003713 II(SrcNone | Prot | EmulateOnUD, em_vmmcall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003714 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3715 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3716 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3717 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3718 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3719 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003720};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003721
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003722static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003723 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003724 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003725 N, N, N, N, N, N,
3726};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003727
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003728static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003729 F(Lock, em_add),
3730 F(Lock | PageTable, em_or),
3731 F(Lock, em_adc),
3732 F(Lock, em_sbb),
3733 F(Lock | PageTable, em_and),
3734 F(Lock, em_sub),
3735 F(Lock, em_xor),
3736 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003737};
3738
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003739static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003740 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003741};
3742
Avi Kivity007a3b52013-01-19 19:51:51 +02003743static const struct opcode group2[] = {
3744 F(DstMem | ModRM, em_rol),
3745 F(DstMem | ModRM, em_ror),
3746 F(DstMem | ModRM, em_rcl),
3747 F(DstMem | ModRM, em_rcr),
3748 F(DstMem | ModRM, em_shl),
3749 F(DstMem | ModRM, em_shr),
3750 F(DstMem | ModRM, em_shl),
3751 F(DstMem | ModRM, em_sar),
3752};
3753
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003754static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003755 F(DstMem | SrcImm | NoWrite, em_test),
3756 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003757 F(DstMem | SrcNone | Lock, em_not),
3758 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02003759 F(DstXacc | Src2Mem, em_mul_ex),
3760 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02003761 F(DstXacc | Src2Mem, em_div_ex),
3762 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003763};
3764
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003765static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003766 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3767 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003768 N, N, N, N, N, N,
3769};
3770
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003771static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003772 F(DstMem | SrcNone | Lock, em_inc),
3773 F(DstMem | SrcNone | Lock, em_dec),
Nadav Amitf7784042014-09-18 22:39:41 +03003774 I(SrcMem | Stack, em_call_near_abs),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003775 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
Nadav Amitf7784042014-09-18 22:39:41 +03003776 I(SrcMem | Stack, em_jmp_abs),
3777 I(SrcMemFAddr | ImplicitOps, em_jmp_far),
3778 I(SrcMem | Stack, em_push), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003779};
3780
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003781static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003782 DI(Prot, sldt),
3783 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003784 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003785 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003786 N, N, N, N,
3787};
3788
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003789static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03003790 II(Mov | DstMem, em_sgdt, sgdt),
3791 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003792 II(SrcMem | Priv, em_lgdt, lgdt),
3793 II(SrcMem | Priv, em_lidt, lidt),
3794 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3795 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3796 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003797}, {
Nadav Amit0f54a322014-08-29 11:26:55 +03003798 EXT(0, group7_rm0),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003799 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003800 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003801 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3802 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3803 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003804} };
3805
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003806static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003807 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003808 F(DstMem | SrcImmByte | NoWrite, em_bt),
3809 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3810 F(DstMem | SrcImmByte | Lock, em_btr),
3811 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003812};
3813
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003814static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003815 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003816}, {
3817 N, N, N, N, N, N, N, N,
3818} };
3819
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003820static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003821 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003822 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003823};
3824
Nadav Amit13e457e2014-10-13 13:04:13 +03003825static const struct gprefix pfx_0f_ae_7 = {
Nadav Amit3f6f1482014-10-13 13:04:14 +03003826 I(SrcMem | ByteOp, em_clflush), N, N, N,
Nadav Amit13e457e2014-10-13 13:04:13 +03003827};
3828
3829static const struct group_dual group15 = { {
3830 N, N, N, N, N, N, N, GP(0, &pfx_0f_ae_7),
3831}, {
3832 N, N, N, N, N, N, N, N,
3833} };
3834
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003835static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003836 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003837};
3838
Paolo Bonzinid5b77062014-07-14 12:54:48 +02003839static const struct gprefix pfx_0f_2b = {
3840 I(0, em_mov), I(0, em_mov), N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003841};
3842
Igor Mammedov27ce8252014-03-15 21:01:59 +01003843static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01003844 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003845};
3846
Alex Williamson0a370272014-07-11 11:56:31 -06003847static const struct gprefix pfx_0f_e7 = {
3848 N, I(Sse, em_mov), N, N,
3849};
3850
Gleb Natapov045a2822012-12-20 16:57:43 +02003851static const struct escape escape_d9 = { {
3852 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3853}, {
3854 /* 0xC0 - 0xC7 */
3855 N, N, N, N, N, N, N, N,
3856 /* 0xC8 - 0xCF */
3857 N, N, N, N, N, N, N, N,
3858 /* 0xD0 - 0xC7 */
3859 N, N, N, N, N, N, N, N,
3860 /* 0xD8 - 0xDF */
3861 N, N, N, N, N, N, N, N,
3862 /* 0xE0 - 0xE7 */
3863 N, N, N, N, N, N, N, N,
3864 /* 0xE8 - 0xEF */
3865 N, N, N, N, N, N, N, N,
3866 /* 0xF0 - 0xF7 */
3867 N, N, N, N, N, N, N, N,
3868 /* 0xF8 - 0xFF */
3869 N, N, N, N, N, N, N, N,
3870} };
3871
3872static const struct escape escape_db = { {
3873 N, N, N, N, N, N, N, N,
3874}, {
3875 /* 0xC0 - 0xC7 */
3876 N, N, N, N, N, N, N, N,
3877 /* 0xC8 - 0xCF */
3878 N, N, N, N, N, N, N, N,
3879 /* 0xD0 - 0xC7 */
3880 N, N, N, N, N, N, N, N,
3881 /* 0xD8 - 0xDF */
3882 N, N, N, N, N, N, N, N,
3883 /* 0xE0 - 0xE7 */
3884 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3885 /* 0xE8 - 0xEF */
3886 N, N, N, N, N, N, N, N,
3887 /* 0xF0 - 0xF7 */
3888 N, N, N, N, N, N, N, N,
3889 /* 0xF8 - 0xFF */
3890 N, N, N, N, N, N, N, N,
3891} };
3892
3893static const struct escape escape_dd = { {
3894 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3895}, {
3896 /* 0xC0 - 0xC7 */
3897 N, N, N, N, N, N, N, N,
3898 /* 0xC8 - 0xCF */
3899 N, N, N, N, N, N, N, N,
3900 /* 0xD0 - 0xC7 */
3901 N, N, N, N, N, N, N, N,
3902 /* 0xD8 - 0xDF */
3903 N, N, N, N, N, N, N, N,
3904 /* 0xE0 - 0xE7 */
3905 N, N, N, N, N, N, N, N,
3906 /* 0xE8 - 0xEF */
3907 N, N, N, N, N, N, N, N,
3908 /* 0xF0 - 0xF7 */
3909 N, N, N, N, N, N, N, N,
3910 /* 0xF8 - 0xFF */
3911 N, N, N, N, N, N, N, N,
3912} };
3913
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003914static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003915 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003916 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003917 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3918 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003919 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003920 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003921 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3922 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003923 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003924 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003925 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3926 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003927 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003928 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003929 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3930 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003931 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003932 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003933 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003934 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003935 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003936 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003937 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003938 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003939 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02003940 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003941 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003942 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003943 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003944 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003945 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003946 I(ImplicitOps | Stack | No64, em_pusha),
3947 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003948 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3949 N, N, N, N,
3950 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003951 I(SrcImm | Mov | Stack, em_push),
3952 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003953 I(SrcImmByte | Mov | Stack, em_push),
3954 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003955 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003956 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003957 /* 0x70 - 0x7F */
3958 X16(D(SrcImmByte)),
3959 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003960 G(ByteOp | DstMem | SrcImm, group1),
3961 G(DstMem | SrcImm, group1),
3962 G(ByteOp | DstMem | SrcImm | No64, group1),
3963 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003964 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003965 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003966 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003967 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003968 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003969 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003970 D(ModRM | SrcMem | NoAccess | DstReg),
3971 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3972 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003973 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003974 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003975 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003976 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003977 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003978 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01003979 II(ImplicitOps | Stack, em_popf, popf),
3980 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003981 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003982 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003983 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003984 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003985 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003986 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003987 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003988 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3989 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003990 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003991 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003992 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003993 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02003994 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003995 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003996 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003997 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003998 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003999 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
4000 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004001 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004002 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03004003 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Bruce Rogers32611072013-09-09 09:40:20 -06004004 I(ImplicitOps | Stack | SrcImmU16, em_ret_far_imm),
4005 I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02004006 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004007 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004008 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004009 G(Src2One | ByteOp, group2), G(Src2One, group2),
4010 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02004011 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02004012 I(DstAcc | SrcImmUByte | No64, em_aad),
4013 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004014 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004015 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02004016 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004017 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09004018 X3(I(SrcImmByte, em_loop)),
4019 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004020 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
4021 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004022 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09004023 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004024 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004025 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
4026 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004027 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004028 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004029 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4030 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004031 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09004032 D(ImplicitOps), D(ImplicitOps),
4033 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004034 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4035};
4036
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004037static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004038 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004039 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02004040 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004041 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004042 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004043 N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004044 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004045 N, N, N, N, N, N, N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004046 D(ImplicitOps | ModRM | SrcMem | NoAccess),
4047 N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004048 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03004049 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
4050 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
4051 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
4052 check_cr_write),
4053 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
4054 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004055 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004056 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
4057 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004058 N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b),
Avi Kivity3e114eb2012-04-09 18:40:01 +03004059 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004060 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004061 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004062 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004063 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004064 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02004065 I(ImplicitOps | EmulateOnUD, em_sysenter),
4066 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004067 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004068 N, N, N, N, N, N, N, N,
4069 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03004070 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004071 /* 0x50 - 0x5F */
4072 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4073 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004074 N, N, N, N,
4075 N, N, N, N,
4076 N, N, N, N,
4077 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004078 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004079 N, N, N, N,
4080 N, N, N, N,
4081 N, N, N, N,
4082 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004083 /* 0x80 - 0x8F */
4084 X16(D(SrcImm)),
4085 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004086 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004087 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004088 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004089 II(ImplicitOps, em_cpuid, cpuid),
4090 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004091 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4092 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004093 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004094 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004095 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004096 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004097 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4098 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Nadav Amit13e457e2014-10-13 13:04:13 +03004099 GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004100 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09004101 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004102 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004103 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004104 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4105 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004106 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004107 /* 0xB8 - 0xBF */
4108 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004109 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004110 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
4111 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004112 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004113 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02004114 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Wei Yongjun92f738a52010-08-17 09:19:34 +08004115 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004116 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004117 /* 0xC8 - 0xCF */
4118 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004119 /* 0xD0 - 0xDF */
4120 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4121 /* 0xE0 - 0xEF */
Alex Williamson0a370272014-07-11 11:56:31 -06004122 N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
4123 N, N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004124 /* 0xF0 - 0xFF */
4125 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4126};
4127
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004128static const struct gprefix three_byte_0f_38_f0 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01004129 I(DstReg | SrcMem | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004130};
4131
4132static const struct gprefix three_byte_0f_38_f1 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01004133 I(DstMem | SrcReg | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004134};
4135
4136/*
4137 * Insns below are selected by the prefix which indexed by the third opcode
4138 * byte.
4139 */
4140static const struct opcode opcode_map_0f_38[256] = {
4141 /* 0x00 - 0x7f */
4142 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004143 /* 0x80 - 0xef */
4144 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4145 /* 0xf0 - 0xf1 */
4146 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f0),
4147 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f1),
4148 /* 0xf2 - 0xff */
4149 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004150};
4151
Avi Kivity73fba5f2010-07-29 15:11:53 +03004152#undef D
4153#undef N
4154#undef G
4155#undef GD
4156#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004157#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004158#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004159
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004160#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004161#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004162#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004163#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004164#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004165
Avi Kivity9dac77f2011-06-01 15:34:25 +03004166static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004167{
4168 unsigned size;
4169
Avi Kivity9dac77f2011-06-01 15:34:25 +03004170 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004171 if (size == 8)
4172 size = 4;
4173 return size;
4174}
4175
4176static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4177 unsigned size, bool sign_extension)
4178{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004179 int rc = X86EMUL_CONTINUE;
4180
4181 op->type = OP_IMM;
4182 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004183 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004184 /* NB. Immediates are sign-extended as necessary. */
4185 switch (op->bytes) {
4186 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004187 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004188 break;
4189 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004190 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004191 break;
4192 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004193 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004194 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004195 case 8:
4196 op->val = insn_fetch(s64, ctxt);
4197 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004198 }
4199 if (!sign_extension) {
4200 switch (op->bytes) {
4201 case 1:
4202 op->val &= 0xff;
4203 break;
4204 case 2:
4205 op->val &= 0xffff;
4206 break;
4207 case 4:
4208 op->val &= 0xffffffff;
4209 break;
4210 }
4211 }
4212done:
4213 return rc;
4214}
4215
Avi Kivitya99455492011-09-13 10:45:41 +03004216static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4217 unsigned d)
4218{
4219 int rc = X86EMUL_CONTINUE;
4220
4221 switch (d) {
4222 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004223 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03004224 break;
4225 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004226 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03004227 break;
4228 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004229 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004230 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03004231 *op = ctxt->memop;
4232 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004233 if (ctxt->d & BitOp)
Avi Kivitya99455492011-09-13 10:45:41 +03004234 fetch_bit_operand(ctxt);
4235 op->orig_val = op->val;
4236 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004237 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004238 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004239 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03004240 case OpAcc:
4241 op->type = OP_REG;
4242 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004243 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03004244 fetch_register_operand(op);
4245 op->orig_val = op->val;
4246 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004247 case OpAccLo:
4248 op->type = OP_REG;
4249 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4250 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4251 fetch_register_operand(op);
4252 op->orig_val = op->val;
4253 break;
4254 case OpAccHi:
4255 if (ctxt->d & ByteOp) {
4256 op->type = OP_NONE;
4257 break;
4258 }
4259 op->type = OP_REG;
4260 op->bytes = ctxt->op_bytes;
4261 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4262 fetch_register_operand(op);
4263 op->orig_val = op->val;
4264 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004265 case OpDI:
4266 op->type = OP_MEM;
4267 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4268 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004269 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya99455492011-09-13 10:45:41 +03004270 op->addr.mem.seg = VCPU_SREG_ES;
4271 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004272 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03004273 break;
4274 case OpDX:
4275 op->type = OP_REG;
4276 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004277 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03004278 fetch_register_operand(op);
4279 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004280 case OpCL:
4281 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004282 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004283 break;
4284 case OpImmByte:
4285 rc = decode_imm(ctxt, op, 1, true);
4286 break;
4287 case OpOne:
4288 op->bytes = 1;
4289 op->val = 1;
4290 break;
4291 case OpImm:
4292 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4293 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004294 case OpImm64:
4295 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4296 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004297 case OpMem8:
4298 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004299 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004300 ctxt->memop.addr.reg = decode_register(ctxt,
4301 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004302 fetch_register_operand(&ctxt->memop);
4303 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004304 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004305 case OpMem16:
4306 ctxt->memop.bytes = 2;
4307 goto mem_common;
4308 case OpMem32:
4309 ctxt->memop.bytes = 4;
4310 goto mem_common;
4311 case OpImmU16:
4312 rc = decode_imm(ctxt, op, 2, false);
4313 break;
4314 case OpImmU:
4315 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4316 break;
4317 case OpSI:
4318 op->type = OP_MEM;
4319 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4320 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004321 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Bandan Das573e80f2014-04-16 12:46:13 -04004322 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03004323 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004324 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004325 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004326 case OpXLat:
4327 op->type = OP_MEM;
4328 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4329 op->addr.mem.ea =
4330 register_address(ctxt,
4331 reg_read(ctxt, VCPU_REGS_RBX) +
4332 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04004333 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004334 op->val = 0;
4335 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004336 case OpImmFAddr:
4337 op->type = OP_IMM;
4338 op->addr.mem.ea = ctxt->_eip;
4339 op->bytes = ctxt->op_bytes + 2;
4340 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4341 break;
4342 case OpMemFAddr:
4343 ctxt->memop.bytes = ctxt->op_bytes + 2;
4344 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004345 case OpES:
4346 op->val = VCPU_SREG_ES;
4347 break;
4348 case OpCS:
4349 op->val = VCPU_SREG_CS;
4350 break;
4351 case OpSS:
4352 op->val = VCPU_SREG_SS;
4353 break;
4354 case OpDS:
4355 op->val = VCPU_SREG_DS;
4356 break;
4357 case OpFS:
4358 op->val = VCPU_SREG_FS;
4359 break;
4360 case OpGS:
4361 op->val = VCPU_SREG_GS;
4362 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004363 case OpImplicit:
4364 /* Special instructions do their own operand decoding. */
4365 default:
4366 op->type = OP_NONE; /* Disable writeback. */
4367 break;
4368 }
4369
4370done:
4371 return rc;
4372}
4373
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004374int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004375{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004376 int rc = X86EMUL_CONTINUE;
4377 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004378 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004379 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04004380 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03004381 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004382
Avi Kivityf09ed832011-09-13 10:45:40 +03004383 ctxt->memop.type = OP_NONE;
4384 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004385 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02004386 ctxt->fetch.ptr = ctxt->fetch.data;
4387 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004388 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01004389 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004390 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004391 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02004392 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004393 if (rc != X86EMUL_CONTINUE)
4394 return rc;
4395 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004396
4397 switch (mode) {
4398 case X86EMUL_MODE_REAL:
4399 case X86EMUL_MODE_VM86:
4400 case X86EMUL_MODE_PROT16:
4401 def_op_bytes = def_ad_bytes = 2;
4402 break;
4403 case X86EMUL_MODE_PROT32:
4404 def_op_bytes = def_ad_bytes = 4;
4405 break;
4406#ifdef CONFIG_X86_64
4407 case X86EMUL_MODE_PROT64:
4408 def_op_bytes = 4;
4409 def_ad_bytes = 8;
4410 break;
4411#endif
4412 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004413 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004414 }
4415
Avi Kivity9dac77f2011-06-01 15:34:25 +03004416 ctxt->op_bytes = def_op_bytes;
4417 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004418
4419 /* Legacy prefixes. */
4420 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004421 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004422 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004423 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004424 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004425 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004426 break;
4427 case 0x67: /* address-size override */
4428 if (mode == X86EMUL_MODE_PROT64)
4429 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004430 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004431 else
4432 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004433 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004434 break;
4435 case 0x26: /* ES override */
4436 case 0x2e: /* CS override */
4437 case 0x36: /* SS override */
4438 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004439 has_seg_override = true;
4440 ctxt->seg_override = (ctxt->b >> 3) & 3;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004441 break;
4442 case 0x64: /* FS override */
4443 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004444 has_seg_override = true;
4445 ctxt->seg_override = ctxt->b & 7;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004446 break;
4447 case 0x40 ... 0x4f: /* REX */
4448 if (mode != X86EMUL_MODE_PROT64)
4449 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004450 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004451 continue;
4452 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004453 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004454 break;
4455 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004456 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004457 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004458 break;
4459 default:
4460 goto done_prefixes;
4461 }
4462
4463 /* Any legacy prefix after a REX prefix nullifies its effect. */
4464
Avi Kivity9dac77f2011-06-01 15:34:25 +03004465 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004466 }
4467
4468done_prefixes:
4469
4470 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004471 if (ctxt->rex_prefix & 8)
4472 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004473
4474 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004475 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004476 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004477 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004478 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004479 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004480 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004481
4482 /* 0F_38 opcode map */
4483 if (ctxt->b == 0x38) {
4484 ctxt->opcode_len = 3;
4485 ctxt->b = insn_fetch(u8, ctxt);
4486 opcode = opcode_map_0f_38[ctxt->b];
4487 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004488 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004489 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004490
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004491 if (ctxt->d & ModRM)
4492 ctxt->modrm = insn_fetch(u8, ctxt);
4493
Nadav Amit7fe864d2014-06-02 18:34:03 +03004494 /* vex-prefix instructions are not implemented */
4495 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
4496 (mode == X86EMUL_MODE_PROT64 ||
4497 (mode >= X86EMUL_MODE_PROT16 && (ctxt->modrm & 0x80)))) {
4498 ctxt->d = NotImpl;
4499 }
4500
Avi Kivity9dac77f2011-06-01 15:34:25 +03004501 while (ctxt->d & GroupMask) {
4502 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004503 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004504 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004505 opcode = opcode.u.group[goffset];
4506 break;
4507 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004508 goffset = (ctxt->modrm >> 3) & 7;
4509 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004510 opcode = opcode.u.gdual->mod3[goffset];
4511 else
4512 opcode = opcode.u.gdual->mod012[goffset];
4513 break;
4514 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004515 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004516 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004517 break;
4518 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004519 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004520 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004521 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004522 switch (simd_prefix) {
4523 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4524 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4525 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4526 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4527 }
4528 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004529 case Escape:
4530 if (ctxt->modrm > 0xbf)
4531 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4532 else
4533 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4534 break;
Avi Kivity46561642011-04-24 14:09:59 +03004535 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004536 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004537 }
Avi Kivity46561642011-04-24 14:09:59 +03004538
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004539 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004540 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004541 }
4542
Paolo Bonzinie24186e2014-03-27 12:00:57 +01004543 /* Unrecognised? */
4544 if (ctxt->d == 0)
4545 return EMULATION_FAILED;
4546
Avi Kivity9dac77f2011-06-01 15:34:25 +03004547 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004548
Nadav Amit3a6095a2014-08-13 16:50:13 +03004549 if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD)))
4550 return EMULATION_FAILED;
4551
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004552 if (unlikely(ctxt->d &
Nadav Amit3a6095a2014-08-13 16:50:13 +03004553 (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm))) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004554 /*
4555 * These are copied unconditionally here, and checked unconditionally
4556 * in x86_emulate_insn.
4557 */
4558 ctxt->check_perm = opcode.check_perm;
4559 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004560
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004561 if (ctxt->d & NotImpl)
4562 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004563
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004564 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004565 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004566
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004567 if (ctxt->d & Op3264) {
4568 if (mode == X86EMUL_MODE_PROT64)
4569 ctxt->op_bytes = 8;
4570 else
4571 ctxt->op_bytes = 4;
4572 }
4573
4574 if (ctxt->d & Sse)
4575 ctxt->op_bytes = 16;
4576 else if (ctxt->d & Mmx)
4577 ctxt->op_bytes = 8;
4578 }
Avi Kivity1253791d2011-03-29 11:41:27 +02004579
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004580 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004581 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004582 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04004583 if (!has_seg_override) {
4584 has_seg_override = true;
4585 ctxt->seg_override = ctxt->modrm_seg;
4586 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004587 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004588 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004589 if (rc != X86EMUL_CONTINUE)
4590 goto done;
4591
Bandan Das573e80f2014-04-16 12:46:13 -04004592 if (!has_seg_override)
4593 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004594
Bandan Das573e80f2014-04-16 12:46:13 -04004595 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004596
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004597 /*
4598 * Decode and fetch the source operand: register, memory
4599 * or immediate.
4600 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004601 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004602 if (rc != X86EMUL_CONTINUE)
4603 goto done;
4604
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004605 /*
4606 * Decode and fetch the second source operand: register, memory
4607 * or immediate.
4608 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004609 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004610 if (rc != X86EMUL_CONTINUE)
4611 goto done;
4612
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004613 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03004614 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004615
Bandan Das41061cd2014-04-16 12:46:14 -04004616 if (ctxt->rip_relative)
Avi Kivityf09ed832011-09-13 10:45:40 +03004617 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004618
Paolo Bonzinia430c912014-10-23 14:54:14 +02004619done:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004620 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004621}
4622
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004623bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4624{
4625 return ctxt->d & PageTable;
4626}
4627
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004628static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4629{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004630 /* The second termination condition only applies for REPE
4631 * and REPNE. Test if the repeat string operation prefix is
4632 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4633 * corresponding termination condition according to:
4634 * - if REPE/REPZ and ZF = 0 then done
4635 * - if REPNE/REPNZ and ZF = 1 then done
4636 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004637 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4638 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4639 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004640 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004641 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004642 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4643 return true;
4644
4645 return false;
4646}
4647
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004648static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4649{
4650 bool fault = false;
4651
4652 ctxt->ops->get_fpu(ctxt);
4653 asm volatile("1: fwait \n\t"
4654 "2: \n\t"
4655 ".pushsection .fixup,\"ax\" \n\t"
4656 "3: \n\t"
4657 "movb $1, %[fault] \n\t"
4658 "jmp 2b \n\t"
4659 ".popsection \n\t"
4660 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004661 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004662 ctxt->ops->put_fpu(ctxt);
4663
4664 if (unlikely(fault))
4665 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4666
4667 return X86EMUL_CONTINUE;
4668}
4669
4670static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4671 struct operand *op)
4672{
4673 if (op->type == OP_MM)
4674 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4675}
4676
Avi Kivitye28bbd42013-01-04 16:18:48 +02004677static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4678{
4679 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivityb9fa4092013-02-09 11:31:48 +02004680 if (!(ctxt->d & ByteOp))
4681 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Avi Kivitye28bbd42013-01-04 16:18:48 +02004682 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004683 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
4684 [fastop]"+S"(fop)
4685 : "c"(ctxt->src2.val));
Avi Kivitye28bbd42013-01-04 16:18:48 +02004686 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004687 if (!fop) /* exception is returned in fop variable */
4688 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02004689 return X86EMUL_CONTINUE;
4690}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004691
Bandan Das14985072014-04-16 12:46:09 -04004692void init_decode_cache(struct x86_emulate_ctxt *ctxt)
4693{
Bandan Das573e80f2014-04-16 12:46:13 -04004694 memset(&ctxt->rip_relative, 0,
4695 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04004696
Bandan Das14985072014-04-16 12:46:09 -04004697 ctxt->io_read.pos = 0;
4698 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04004699 ctxt->mem_read.end = 0;
4700}
4701
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004702int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004703{
Mathias Krause0225fb52012-08-30 01:30:16 +02004704 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004705 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004706 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004707
Avi Kivity9dac77f2011-06-01 15:34:25 +03004708 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004709
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004710 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004711 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004712 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004713 goto done;
4714 }
4715
Avi Kivity9dac77f2011-06-01 15:34:25 +03004716 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004717 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004718 goto done;
4719 }
4720
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004721 if (unlikely(ctxt->d &
4722 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
4723 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4724 (ctxt->d & Undefined)) {
4725 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004726 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004727 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004728
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004729 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4730 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
4731 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004732 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004733 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004734
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004735 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
4736 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004737 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004738 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004739
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004740 if (ctxt->d & Mmx) {
4741 rc = flush_pending_x87_faults(ctxt);
4742 if (rc != X86EMUL_CONTINUE)
4743 goto done;
4744 /*
4745 * Now that we know the fpu is exception safe, we can fetch
4746 * operands from it.
4747 */
4748 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4749 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4750 if (!(ctxt->d & Mov))
4751 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4752 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004753
Bandan Das685bbf42014-04-16 12:46:10 -04004754 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004755 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4756 X86_ICPT_PRE_EXCEPT);
4757 if (rc != X86EMUL_CONTINUE)
4758 goto done;
4759 }
4760
4761 /* Privileged instruction can be executed only in CPL=0 */
4762 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Nadav Amit68efa762014-06-18 17:19:35 +03004763 if (ctxt->d & PrivUD)
4764 rc = emulate_ud(ctxt);
4765 else
4766 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004767 goto done;
4768 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004769
4770 /* Instruction can only be executed in protected mode */
4771 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
4772 rc = emulate_ud(ctxt);
4773 goto done;
4774 }
4775
4776 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04004777 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004778 rc = ctxt->check_perm(ctxt);
4779 if (rc != X86EMUL_CONTINUE)
4780 goto done;
4781 }
4782
Bandan Das685bbf42014-04-16 12:46:10 -04004783 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004784 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4785 X86_ICPT_POST_EXCEPT);
4786 if (rc != X86EMUL_CONTINUE)
4787 goto done;
4788 }
4789
4790 if (ctxt->rep_prefix && (ctxt->d & String)) {
4791 /* All REP prefixes have the same first termination condition */
4792 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
4793 ctxt->eip = ctxt->_eip;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004794 ctxt->eflags &= ~EFLG_RF;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004795 goto done;
4796 }
4797 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004798 }
4799
Avi Kivity9dac77f2011-06-01 15:34:25 +03004800 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4801 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4802 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004803 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004804 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004805 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004806 }
4807
Avi Kivity9dac77f2011-06-01 15:34:25 +03004808 if (ctxt->src2.type == OP_MEM) {
4809 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4810 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004811 if (rc != X86EMUL_CONTINUE)
4812 goto done;
4813 }
4814
Avi Kivity9dac77f2011-06-01 15:34:25 +03004815 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004816 goto special_insn;
4817
4818
Avi Kivity9dac77f2011-06-01 15:34:25 +03004819 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004820 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004821 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4822 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004823 if (rc != X86EMUL_CONTINUE)
4824 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004825 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004826 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004827
Avi Kivity018a98d2007-11-27 19:30:56 +02004828special_insn:
4829
Bandan Das685bbf42014-04-16 12:46:10 -04004830 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004831 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004832 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004833 if (rc != X86EMUL_CONTINUE)
4834 goto done;
4835 }
4836
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004837 if (ctxt->rep_prefix && (ctxt->d & String))
4838 ctxt->eflags |= EFLG_RF;
4839 else
4840 ctxt->eflags &= ~EFLG_RF;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004841
Avi Kivity9dac77f2011-06-01 15:34:25 +03004842 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004843 if (ctxt->d & Fastop) {
4844 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4845 rc = fastop(ctxt, fop);
4846 if (rc != X86EMUL_CONTINUE)
4847 goto done;
4848 goto writeback;
4849 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004850 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004851 if (rc != X86EMUL_CONTINUE)
4852 goto done;
4853 goto writeback;
4854 }
4855
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004856 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004857 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004858 else if (ctxt->opcode_len == 3)
4859 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004860
Avi Kivity9dac77f2011-06-01 15:34:25 +03004861 switch (ctxt->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004862 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004863 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004864 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004865 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004866 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004867 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004868 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03004869 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004870 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004871 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004872 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004873 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004874 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004875 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03004876 ctxt->dst.type = OP_NONE;
4877 else
4878 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004879 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004880 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004881 switch (ctxt->op_bytes) {
4882 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4883 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4884 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004885 }
4886 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004887 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004888 rc = emulate_int(ctxt, 3);
4889 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004890 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004891 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004892 break;
4893 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004894 if (ctxt->eflags & EFLG_OF)
4895 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004896 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004897 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004898 case 0xeb: /* jmp rel short */
Nadav Amit234f3ce2014-09-18 22:39:38 +03004899 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004900 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004901 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004902 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004903 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004904 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004905 case 0xf5: /* cmc */
4906 /* complement carry flag from eflags reg */
4907 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004908 break;
4909 case 0xf8: /* clc */
4910 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004911 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004912 case 0xf9: /* stc */
4913 ctxt->eflags |= EFLG_CF;
4914 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004915 case 0xfc: /* cld */
4916 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004917 break;
4918 case 0xfd: /* std */
4919 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004920 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004921 default:
4922 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004923 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004924
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004925 if (rc != X86EMUL_CONTINUE)
4926 goto done;
4927
Avi Kivity018a98d2007-11-27 19:30:56 +02004928writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02004929 if (ctxt->d & SrcWrite) {
4930 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
4931 rc = writeback(ctxt, &ctxt->src);
4932 if (rc != X86EMUL_CONTINUE)
4933 goto done;
4934 }
Nadav Amitee212292014-06-15 16:12:58 +03004935 if (!(ctxt->d & NoWrite)) {
4936 rc = writeback(ctxt, &ctxt->dst);
4937 if (rc != X86EMUL_CONTINUE)
4938 goto done;
4939 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004940
Gleb Natapov5cd21912010-03-18 15:20:26 +02004941 /*
4942 * restore dst type in case the decoding will be reused
4943 * (happens for string instruction )
4944 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004945 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004946
Avi Kivity9dac77f2011-06-01 15:34:25 +03004947 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004948 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004949
Avi Kivity9dac77f2011-06-01 15:34:25 +03004950 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004951 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004952
Avi Kivity9dac77f2011-06-01 15:34:25 +03004953 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004954 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004955 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004956 if ((ctxt->d & SrcMask) == SrcSI)
4957 count = ctxt->src.count;
4958 else
4959 count = ctxt->dst.count;
4960 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4961 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004962
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004963 if (!string_insn_completed(ctxt)) {
4964 /*
4965 * Re-enter guest when pio read ahead buffer is empty
4966 * or, if it is not used, after each 1024 iteration.
4967 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004968 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004969 (r->end == 0 || r->end != r->pos)) {
4970 /*
4971 * Reset read cache. Usually happens before
4972 * decode, but since instruction is restarted
4973 * we have to do it here.
4974 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004975 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004976 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004977 return EMULATION_RESTART;
4978 }
4979 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004980 }
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004981 ctxt->eflags &= ~EFLG_RF;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004982 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004983
Avi Kivity9dac77f2011-06-01 15:34:25 +03004984 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004985
4986done:
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02004987 if (rc == X86EMUL_PROPAGATE_FAULT) {
4988 WARN_ON(ctxt->exception.vector > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +02004989 ctxt->have_exception = true;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02004990 }
Joerg Roedel775fde82011-04-04 12:39:24 +02004991 if (rc == X86EMUL_INTERCEPTED)
4992 return EMULATION_INTERCEPTED;
4993
Avi Kivitydd856ef2012-08-27 23:46:17 +03004994 if (rc == X86EMUL_CONTINUE)
4995 writeback_registers(ctxt);
4996
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004997 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004998
4999twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005000 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02005001 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02005002 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005003 break;
5004 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02005005 case 0x0d: /* GrpP (prefetch) */
5006 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02005007 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02005008 break;
5009 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005010 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02005011 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005012 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005013 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005014 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005015 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03005016 if (test_cc(ctxt->b, ctxt->eflags))
5017 ctxt->dst.val = ctxt->src.val;
5018 else if (ctxt->mode != X86EMUL_MODE_PROT64 ||
5019 ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005020 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08005021 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03005022 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03005023 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005024 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005025 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08005026 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005027 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08005028 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005029 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005030 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005031 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03005032 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005033 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005034 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005035 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005036 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03005037 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005038 break;
Sheng Yanga012e652007-10-15 14:24:20 +08005039 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005040 ctxt->dst.bytes = ctxt->op_bytes;
Nadav Amit3b320042014-06-02 18:34:08 +03005041 ctxt->dst.val = (ctxt->op_bytes == 8) ? (u64) ctxt->src.val :
5042 (u32) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08005043 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005044 default:
5045 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005046 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005047
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005048threebyte_insn:
5049
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005050 if (rc != X86EMUL_CONTINUE)
5051 goto done;
5052
Avi Kivity6aa8b732006-12-10 02:21:36 -08005053 goto writeback;
5054
5055cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02005056 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005057}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005058
5059void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
5060{
5061 invalidate_registers(ctxt);
5062}
5063
5064void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
5065{
5066 writeback_registers(ctxt);
5067}