blob: 749f9fa3825422adb9a3cc9a5867f52ec256dedd [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivityb7d491e2013-01-04 16:18:49 +020027#include <linux/stringify.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028
Avi Kivity3eeb3282010-01-21 15:31:48 +020029#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020030#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020031
Avi Kivity6aa8b732006-12-10 02:21:36 -080032/*
Avi Kivitya9945542011-09-13 10:45:41 +030033 * Operand types
34 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030035#define OpNone 0ull
36#define OpImplicit 1ull /* No generic decode */
37#define OpReg 2ull /* Register */
38#define OpMem 3ull /* Memory */
39#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
40#define OpDI 5ull /* ES:DI/EDI/RDI */
41#define OpMem64 6ull /* Memory, 64-bit */
42#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
43#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030044#define OpCL 9ull /* CL register (for shifts) */
45#define OpImmByte 10ull /* 8-bit sign extended immediate */
46#define OpOne 11ull /* Implied 1 */
Nadav Amit5e2c6882012-12-06 21:55:10 -020047#define OpImm 12ull /* Sign extended up to 32-bit immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030048#define OpMem16 13ull /* Memory operand (16-bit). */
49#define OpMem32 14ull /* Memory operand (32-bit). */
50#define OpImmU 15ull /* Immediate operand, zero extended */
51#define OpSI 16ull /* SI/ESI/RSI */
52#define OpImmFAddr 17ull /* Immediate far address */
53#define OpMemFAddr 18ull /* Far address in memory */
54#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030055#define OpES 20ull /* ES */
56#define OpCS 21ull /* CS */
57#define OpSS 22ull /* SS */
58#define OpDS 23ull /* DS */
59#define OpFS 24ull /* FS */
60#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020061#define OpMem8 26ull /* 8-bit zero extended memory operand */
Nadav Amit5e2c6882012-12-06 21:55:10 -020062#define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */
Paolo Bonzini7fa57952013-05-09 11:32:50 +020063#define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */
Avi Kivity820207c2013-02-09 11:31:45 +020064#define OpAccLo 29ull /* Low part of extended acc (AX/AX/EAX/RAX) */
65#define OpAccHi 30ull /* High part of extended acc (-/DX/EDX/RDX) */
Avi Kivitya9945542011-09-13 10:45:41 +030066
Avi Kivity0fe59122011-09-13 10:45:47 +030067#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030068#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya9945542011-09-13 10:45:41 +030069
70/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080071 * Opcode effective-address decode tables.
72 * Note that we only emulate instructions that have at least one memory
73 * operand (excluding implicit stack references). We assume that stack
74 * references and instruction fetches will never occur in special memory
75 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
76 * not be handled.
77 */
78
79/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030080#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080081/* Destination operand type. */
Avi Kivitya9945542011-09-13 10:45:41 +030082#define DstShift 1
83#define ImplicitOps (OpImplicit << DstShift)
84#define DstReg (OpReg << DstShift)
85#define DstMem (OpMem << DstShift)
86#define DstAcc (OpAcc << DstShift)
87#define DstDI (OpDI << DstShift)
88#define DstMem64 (OpMem64 << DstShift)
89#define DstImmUByte (OpImmUByte << DstShift)
90#define DstDX (OpDX << DstShift)
Avi Kivity820207c2013-02-09 11:31:45 +020091#define DstAccLo (OpAccLo << DstShift)
Avi Kivitya9945542011-09-13 10:45:41 +030092#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080093/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030094#define SrcShift 6
95#define SrcNone (OpNone << SrcShift)
96#define SrcReg (OpReg << SrcShift)
97#define SrcMem (OpMem << SrcShift)
98#define SrcMem16 (OpMem16 << SrcShift)
99#define SrcMem32 (OpMem32 << SrcShift)
100#define SrcImm (OpImm << SrcShift)
101#define SrcImmByte (OpImmByte << SrcShift)
102#define SrcOne (OpOne << SrcShift)
103#define SrcImmUByte (OpImmUByte << SrcShift)
104#define SrcImmU (OpImmU << SrcShift)
105#define SrcSI (OpSI << SrcShift)
Paolo Bonzini7fa57952013-05-09 11:32:50 +0200106#define SrcXLat (OpXLat << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300107#define SrcImmFAddr (OpImmFAddr << SrcShift)
108#define SrcMemFAddr (OpMemFAddr << SrcShift)
109#define SrcAcc (OpAcc << SrcShift)
110#define SrcImmU16 (OpImmU16 << SrcShift)
Nadav Amit5e2c6882012-12-06 21:55:10 -0200111#define SrcImm64 (OpImm64 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300112#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200113#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity820207c2013-02-09 11:31:45 +0200114#define SrcAccHi (OpAccHi << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300115#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300116#define BitOp (1<<11)
117#define MemAbs (1<<12) /* Memory operand is absolute displacement */
118#define String (1<<13) /* String instruction (rep capable) */
119#define Stack (1<<14) /* Stack instruction (push/pop) */
120#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
121#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
122#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
123#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
124#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
Gleb Natapov045a2822012-12-20 16:57:43 +0200125#define Escape (5<<15) /* Escape to coprocessor instruction */
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300126#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300127/* Generic ModRM decode. */
128#define ModRM (1<<19)
129/* Destination is only written; never read. */
130#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300131/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200132#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Borislav Petkovb51e9742013-09-22 16:44:52 +0200133#define EmulateOnUD (1<<22) /* Emulate if unsupported by the host */
Avi Kivity5a506b12010-08-01 15:10:29 +0300134#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300135#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300136#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200137#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200138#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300139#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800140#define PageTable (1 << 29) /* instruction used to write page table */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300141#define NotImpl (1 << 30) /* instruction is not implemented */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100142/* Source 2 operand type */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300143#define Src2Shift (31)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300144#define Src2None (OpNone << Src2Shift)
Avi Kivityab2c5ce2013-02-09 11:31:46 +0200145#define Src2Mem (OpMem << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300146#define Src2CL (OpCL << Src2Shift)
147#define Src2ImmByte (OpImmByte << Src2Shift)
148#define Src2One (OpOne << Src2Shift)
149#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300150#define Src2ES (OpES << Src2Shift)
151#define Src2CS (OpCS << Src2Shift)
152#define Src2SS (OpSS << Src2Shift)
153#define Src2DS (OpDS << Src2Shift)
154#define Src2FS (OpFS << Src2Shift)
155#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300156#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300157#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Avi Kivity1c11b372012-04-09 18:39:59 +0300158#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
159#define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */
160#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
Avi Kivitye28bbd42013-01-04 16:18:48 +0200161#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
Avi Kivityb6744dc2013-01-04 16:18:50 +0200162#define NoWrite ((u64)1 << 45) /* No writeback */
Avi Kivityfb32b1e2013-02-09 11:31:44 +0200163#define SrcWrite ((u64)1 << 46) /* Write back src operand */
Nadav Amit9b88ae92014-05-25 23:05:21 +0300164#define NoMod ((u64)1 << 47) /* Mod field is ignored */
Paolo Bonzinid40a6892014-03-27 11:58:02 +0100165#define Intercept ((u64)1 << 48) /* Has valid intercept field */
166#define CheckPerm ((u64)1 << 49) /* Has valid check_perm field */
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 Kivity12537912011-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;
577 case 8:
Nadav Amit234f3ce2014-09-18 22:39:38 +0300578 if ((cs_l && is_noncanonical_address(dst)) ||
579 (!cs_l && (dst & ~(u32)-1)))
580 return emulate_gp(ctxt, 0);
Nadav Amit05c83ec2014-09-18 22:39:37 +0300581 ctxt->_eip = dst;
582 break;
583 default:
584 WARN(1, "unsupported eip assignment size\n");
585 }
Nadav Amit234f3ce2014-09-18 22:39:38 +0300586 return X86EMUL_CONTINUE;
Nadav Amit05c83ec2014-09-18 22:39:37 +0300587}
588
Nadav Amit234f3ce2014-09-18 22:39:38 +0300589static inline int assign_eip_near(struct x86_emulate_ctxt *ctxt, ulong dst)
Nadav Amit05c83ec2014-09-18 22:39:37 +0300590{
Nadav Amit234f3ce2014-09-18 22:39:38 +0300591 return assign_eip_far(ctxt, dst, ctxt->mode == X86EMUL_MODE_PROT64);
592}
593
594static inline int jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
595{
596 return assign_eip_near(ctxt, ctxt->_eip + rel);
Nadav Amit05c83ec2014-09-18 22:39:37 +0300597}
598
Avi Kivity1aa36612011-04-27 13:20:30 +0300599static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
600{
601 u16 selector;
602 struct desc_struct desc;
603
604 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
605 return selector;
606}
607
608static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
609 unsigned seg)
610{
611 u16 dummy;
612 u32 base3;
613 struct desc_struct desc;
614
615 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
616 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
617}
618
Avi Kivity1c11b372012-04-09 18:39:59 +0300619/*
620 * x86 defines three classes of vector instructions: explicitly
621 * aligned, explicitly unaligned, and the rest, which change behaviour
622 * depending on whether they're AVX encoded or not.
623 *
624 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
625 * subject to the same check.
626 */
627static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
628{
629 if (likely(size < 16))
630 return false;
631
632 if (ctxt->d & Aligned)
633 return true;
634 else if (ctxt->d & Unaligned)
635 return false;
636 else if (ctxt->d & Avx)
637 return false;
638 else
639 return true;
640}
641
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400642static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300643 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400644 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300645 ulong *linear)
646{
Avi Kivity618ff152011-04-03 12:32:09 +0300647 struct desc_struct desc;
648 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300649 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300650 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300651 u16 sel;
Gleb Natapov3a78a4f2012-12-20 16:57:42 +0200652 unsigned cpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300653
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900654 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300655 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300656 case X86EMUL_MODE_PROT64:
657 if (((signed long)la << 16) >> 16 != la)
658 return emulate_gp(ctxt, 0);
659 break;
660 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300661 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
662 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300663 if (!usable)
664 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200665 /* code segment in protected mode or read-only data segment */
666 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
667 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300668 goto bad;
669 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400670 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300671 goto bad;
672 lim = desc_limit_scaled(&desc);
Nadav Amit10e38fc2014-06-18 17:19:34 +0300673 if ((ctxt->mode == X86EMUL_MODE_REAL) && !fetch &&
674 (ctxt->d & NoBigReal)) {
675 /* la is between zero and 0xffff */
676 if (la > 0xffff || (u32)(la + size - 1) > 0xffff)
677 goto bad;
678 } else if ((desc.type & 8) || !(desc.type & 4)) {
Avi Kivity618ff152011-04-03 12:32:09 +0300679 /* expand-up segment */
680 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
681 goto bad;
682 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800683 /* expand-down segment */
Avi Kivity618ff152011-04-03 12:32:09 +0300684 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
685 goto bad;
686 lim = desc.d ? 0xffffffff : 0xffff;
687 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
688 goto bad;
689 }
Avi Kivity717746e2011-04-20 13:37:53 +0300690 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity618ff152011-04-03 12:32:09 +0300691 if (!(desc.type & 8)) {
692 /* data segment */
693 if (cpl > desc.dpl)
694 goto bad;
695 } else if ((desc.type & 8) && !(desc.type & 4)) {
696 /* nonconforming code segment */
697 if (cpl != desc.dpl)
698 goto bad;
699 } else if ((desc.type & 8) && (desc.type & 4)) {
700 /* conforming code segment */
701 if (cpl < desc.dpl)
702 goto bad;
703 }
704 break;
705 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300706 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300707 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300708 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
709 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300710 *linear = la;
711 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300712bad:
713 if (addr.seg == VCPU_SREG_SS)
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300714 return emulate_ss(ctxt, sel);
Avi Kivity618ff152011-04-03 12:32:09 +0300715 else
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300716 return emulate_gp(ctxt, sel);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300717}
718
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400719static int linearize(struct x86_emulate_ctxt *ctxt,
720 struct segmented_address addr,
721 unsigned size, bool write,
722 ulong *linear)
723{
724 return __linearize(ctxt, addr, size, write, false, linear);
725}
726
727
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200728static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
729 struct segmented_address addr,
730 void *data,
731 unsigned size)
732{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200733 int rc;
734 ulong linear;
735
Avi Kivity83b87952011-04-03 11:31:19 +0300736 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200737 if (rc != X86EMUL_CONTINUE)
738 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300739 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200740}
741
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900742/*
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200743 * Prefetch the remaining bytes of the instruction without crossing page
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900744 * boundary if they are not in fetch_cache yet.
745 */
Paolo Bonzini9506d572014-05-06 13:05:25 +0200746static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
Avi Kivity62266862007-11-20 13:15:52 +0200747{
Avi Kivity62266862007-11-20 13:15:52 +0200748 int rc;
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200749 unsigned size;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200750 unsigned long linear;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200751 int cur_size = ctxt->fetch.end - ctxt->fetch.data;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200752 struct segmented_address addr = { .seg = VCPU_SREG_CS,
Paolo Bonzini17052f12014-05-06 16:33:01 +0200753 .ea = ctxt->eip + cur_size };
754
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200755 size = 15UL ^ cur_size;
756 rc = __linearize(ctxt, addr, size, false, true, &linear);
757 if (unlikely(rc != X86EMUL_CONTINUE))
758 return rc;
759
760 size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear));
Paolo Bonzini5cfc7e02014-05-06 13:05:25 +0200761
762 /*
763 * One instruction can only straddle two pages,
764 * and one has been loaded at the beginning of
765 * x86_decode_insn. So, if not enough bytes
766 * still, we must have hit the 15-byte boundary.
767 */
768 if (unlikely(size < op_size))
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200769 return X86EMUL_UNHANDLEABLE;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200770 rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end,
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200771 size, &ctxt->exception);
772 if (unlikely(rc != X86EMUL_CONTINUE))
773 return rc;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200774 ctxt->fetch.end += size;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900775 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200776}
777
Paolo Bonzini9506d572014-05-06 13:05:25 +0200778static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt,
779 unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200780{
Nadav Amit08da44a2014-10-03 01:10:04 +0300781 unsigned done_size = ctxt->fetch.end - ctxt->fetch.ptr;
782
783 if (unlikely(done_size < size))
784 return __do_insn_fetch_bytes(ctxt, size - done_size);
Paolo Bonzini9506d572014-05-06 13:05:25 +0200785 else
786 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200787}
788
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900789/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900790#define insn_fetch(_type, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200791({ _type _x; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200792 \
793 rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900794 if (rc != X86EMUL_CONTINUE) \
795 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200796 ctxt->_eip += sizeof(_type); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200797 _x = *(_type __aligned(1) *) ctxt->fetch.ptr; \
798 ctxt->fetch.ptr += sizeof(_type); \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200799 _x; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900800})
801
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900802#define insn_fetch_arr(_arr, _size, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200803({ \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200804 rc = do_insn_fetch_bytes(_ctxt, _size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900805 if (rc != X86EMUL_CONTINUE) \
806 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200807 ctxt->_eip += (_size); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200808 memcpy(_arr, ctxt->fetch.ptr, _size); \
809 ctxt->fetch.ptr += (_size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900810})
811
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000812/*
813 * Given the 'reg' portion of a ModRM byte, and a register block, return a
814 * pointer into the block that addresses the relevant register.
815 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
816 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300817static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200818 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800819{
820 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200821 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800822
Avi Kivity6aa8b732006-12-10 02:21:36 -0800823 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300824 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
825 else
826 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800827 return p;
828}
829
830static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200831 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800832 u16 *size, unsigned long *address, int op_bytes)
833{
834 int rc;
835
836 if (op_bytes == 2)
837 op_bytes = 3;
838 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200839 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900840 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800841 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200842 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200843 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800844 return rc;
845}
846
Avi Kivity34b77652013-01-19 19:51:56 +0200847FASTOP2(add);
848FASTOP2(or);
849FASTOP2(adc);
850FASTOP2(sbb);
851FASTOP2(and);
852FASTOP2(sub);
853FASTOP2(xor);
854FASTOP2(cmp);
855FASTOP2(test);
856
Avi Kivityb9fa4092013-02-09 11:31:48 +0200857FASTOP1SRC2(mul, mul_ex);
858FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200859FASTOP1SRC2EX(div, div_ex);
860FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +0200861
Avi Kivity34b77652013-01-19 19:51:56 +0200862FASTOP3WCL(shld);
863FASTOP3WCL(shrd);
864
865FASTOP2W(imul);
866
867FASTOP1(not);
868FASTOP1(neg);
869FASTOP1(inc);
870FASTOP1(dec);
871
872FASTOP2CL(rol);
873FASTOP2CL(ror);
874FASTOP2CL(rcl);
875FASTOP2CL(rcr);
876FASTOP2CL(shl);
877FASTOP2CL(shr);
878FASTOP2CL(sar);
879
880FASTOP2W(bsf);
881FASTOP2W(bsr);
882FASTOP2W(bt);
883FASTOP2W(bts);
884FASTOP2W(btr);
885FASTOP2W(btc);
886
Avi Kivitye47a5f52013-02-09 11:31:51 +0200887FASTOP2(xadd);
888
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200889static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300890{
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200891 u8 rc;
892 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300893
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200894 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +0200895 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200896 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
897 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300898}
899
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300900static void fetch_register_operand(struct operand *op)
901{
902 switch (op->bytes) {
903 case 1:
904 op->val = *(u8 *)op->addr.reg;
905 break;
906 case 2:
907 op->val = *(u16 *)op->addr.reg;
908 break;
909 case 4:
910 op->val = *(u32 *)op->addr.reg;
911 break;
912 case 8:
913 op->val = *(u64 *)op->addr.reg;
914 break;
915 }
916}
917
Avi Kivity12537912011-03-29 11:41:27 +0200918static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
919{
920 ctxt->ops->get_fpu(ctxt);
921 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200922 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
923 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
924 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
925 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
926 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
927 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
928 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
929 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200930#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200931 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
932 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
933 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
934 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
935 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
936 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
937 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
938 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200939#endif
940 default: BUG();
941 }
942 ctxt->ops->put_fpu(ctxt);
943}
944
945static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
946 int reg)
947{
948 ctxt->ops->get_fpu(ctxt);
949 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200950 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
951 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
952 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
953 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
954 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
955 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
956 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
957 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200958#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200959 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
960 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
961 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
962 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
963 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
964 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
965 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
966 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200967#endif
968 default: BUG();
969 }
970 ctxt->ops->put_fpu(ctxt);
971}
972
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300973static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
974{
975 ctxt->ops->get_fpu(ctxt);
976 switch (reg) {
977 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
978 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
979 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
980 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
981 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
982 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
983 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
984 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
985 default: BUG();
986 }
987 ctxt->ops->put_fpu(ctxt);
988}
989
990static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
991{
992 ctxt->ops->get_fpu(ctxt);
993 switch (reg) {
994 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
995 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
996 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
997 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
998 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
999 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1000 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1001 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1002 default: BUG();
1003 }
1004 ctxt->ops->put_fpu(ctxt);
1005}
1006
Gleb Natapov045a2822012-12-20 16:57:43 +02001007static int em_fninit(struct x86_emulate_ctxt *ctxt)
1008{
1009 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1010 return emulate_nm(ctxt);
1011
1012 ctxt->ops->get_fpu(ctxt);
1013 asm volatile("fninit");
1014 ctxt->ops->put_fpu(ctxt);
1015 return X86EMUL_CONTINUE;
1016}
1017
1018static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1019{
1020 u16 fcw;
1021
1022 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1023 return emulate_nm(ctxt);
1024
1025 ctxt->ops->get_fpu(ctxt);
1026 asm volatile("fnstcw %0": "+m"(fcw));
1027 ctxt->ops->put_fpu(ctxt);
1028
1029 /* force 2 byte destination */
1030 ctxt->dst.bytes = 2;
1031 ctxt->dst.val = fcw;
1032
1033 return X86EMUL_CONTINUE;
1034}
1035
1036static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1037{
1038 u16 fsw;
1039
1040 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1041 return emulate_nm(ctxt);
1042
1043 ctxt->ops->get_fpu(ctxt);
1044 asm volatile("fnstsw %0": "+m"(fsw));
1045 ctxt->ops->put_fpu(ctxt);
1046
1047 /* force 2 byte destination */
1048 ctxt->dst.bytes = 2;
1049 ctxt->dst.val = fsw;
1050
1051 return X86EMUL_CONTINUE;
1052}
1053
Avi Kivity12537912011-03-29 11:41:27 +02001054static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001055 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001056{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001057 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001058
Avi Kivity9dac77f2011-06-01 15:34:25 +03001059 if (!(ctxt->d & ModRM))
1060 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +02001061
Avi Kivity9dac77f2011-06-01 15:34:25 +03001062 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001063 op->type = OP_XMM;
1064 op->bytes = 16;
1065 op->addr.xmm = reg;
1066 read_sse_reg(ctxt, &op->vec_val, reg);
1067 return;
1068 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001069 if (ctxt->d & Mmx) {
1070 reg &= 7;
1071 op->type = OP_MM;
1072 op->bytes = 8;
1073 op->addr.mm = reg;
1074 return;
1075 }
Avi Kivity12537912011-03-29 11:41:27 +02001076
Avi Kivity3c118e22007-10-31 10:27:04 +02001077 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001078 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1079 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1080
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001081 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001082 op->orig_val = op->val;
1083}
1084
Avi Kivitya6e34072012-06-10 17:15:39 +03001085static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1086{
1087 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1088 ctxt->modrm_seg = VCPU_SREG_SS;
1089}
1090
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001091static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001092 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001093{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001094 u8 sib;
Bandan Das02357bd2014-04-16 12:46:11 -04001095 int index_reg, base_reg, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001096 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001097 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001098
Bandan Das02357bd2014-04-16 12:46:11 -04001099 ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */
1100 index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */
1101 base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001102
Bandan Das02357bd2014-04-16 12:46:11 -04001103 ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001104 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
Bandan Das02357bd2014-04-16 12:46:11 -04001105 ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001106 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001107
Nadav Amit9b88ae92014-05-25 23:05:21 +03001108 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001109 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001110 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001111 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001112 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001113 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001114 op->type = OP_XMM;
1115 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001116 op->addr.xmm = ctxt->modrm_rm;
1117 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001118 return rc;
1119 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001120 if (ctxt->d & Mmx) {
1121 op->type = OP_MM;
1122 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001123 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001124 return rc;
1125 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001126 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001127 return rc;
1128 }
1129
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001130 op->type = OP_MEM;
1131
Avi Kivity9dac77f2011-06-01 15:34:25 +03001132 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001133 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1134 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1135 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1136 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001137
1138 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001139 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001140 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001141 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001142 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001143 break;
1144 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001145 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001146 break;
1147 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001148 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001149 break;
1150 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001151 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001152 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001153 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001154 break;
1155 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001156 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001157 break;
1158 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001159 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001160 break;
1161 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001162 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001163 break;
1164 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001165 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001166 break;
1167 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001168 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001169 break;
1170 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001171 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001172 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001173 break;
1174 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001175 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001176 break;
1177 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001178 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1179 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1180 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001181 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001182 } else {
1183 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001184 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001185 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001186 index_reg |= (sib >> 3) & 7;
1187 base_reg |= sib & 7;
1188 scale = sib >> 6;
1189
Avi Kivity9dac77f2011-06-01 15:34:25 +03001190 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001191 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001192 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001193 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001194 adjust_modrm_seg(ctxt, base_reg);
1195 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001196 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001197 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001198 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001199 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001200 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001201 } else {
1202 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001203 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001204 adjust_modrm_seg(ctxt, base_reg);
1205 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001206 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001207 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001208 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001209 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001210 break;
1211 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001212 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001213 break;
1214 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001215 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001216 break;
1217 }
1218 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001219 op->addr.mem.ea = modrm_ea;
Bandan Das41061cd2014-04-16 12:46:14 -04001220 if (ctxt->ad_bytes != 8)
1221 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
1222
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001223done:
1224 return rc;
1225}
1226
1227static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001228 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001229{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001230 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001231
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001232 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001233 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001234 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001235 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001236 break;
1237 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001238 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001239 break;
1240 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001241 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001242 break;
1243 }
1244done:
1245 return rc;
1246}
1247
Avi Kivity9dac77f2011-06-01 15:34:25 +03001248static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001249{
Sheng Yang7129eec2010-09-28 16:33:32 +08001250 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001251
Avi Kivity9dac77f2011-06-01 15:34:25 +03001252 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001253 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001254
Avi Kivity9dac77f2011-06-01 15:34:25 +03001255 if (ctxt->src.bytes == 2)
1256 sv = (s16)ctxt->src.val & (s16)mask;
1257 else if (ctxt->src.bytes == 4)
1258 sv = (s32)ctxt->src.val & (s32)mask;
Nadav Amit7dec5602014-06-15 16:12:57 +03001259 else
1260 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001261
Avi Kivity9dac77f2011-06-01 15:34:25 +03001262 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001263 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001264
1265 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001266 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001267}
1268
Gleb Natapov9de41572010-04-28 19:15:22 +03001269static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001270 unsigned long addr, void *dest, unsigned size)
1271{
1272 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001273 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001274
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001275 if (mc->pos < mc->end)
1276 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001277
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001278 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001279
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001280 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1281 &ctxt->exception);
1282 if (rc != X86EMUL_CONTINUE)
1283 return rc;
1284
1285 mc->end += size;
1286
1287read_cached:
1288 memcpy(dest, mc->data + mc->pos, size);
1289 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001290 return X86EMUL_CONTINUE;
1291}
1292
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001293static int segmented_read(struct x86_emulate_ctxt *ctxt,
1294 struct segmented_address addr,
1295 void *data,
1296 unsigned size)
1297{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001298 int rc;
1299 ulong linear;
1300
Avi Kivity83b87952011-04-03 11:31:19 +03001301 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001302 if (rc != X86EMUL_CONTINUE)
1303 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001304 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001305}
1306
1307static int segmented_write(struct x86_emulate_ctxt *ctxt,
1308 struct segmented_address addr,
1309 const void *data,
1310 unsigned size)
1311{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001312 int rc;
1313 ulong linear;
1314
Avi Kivity83b87952011-04-03 11:31:19 +03001315 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001316 if (rc != X86EMUL_CONTINUE)
1317 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001318 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1319 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001320}
1321
1322static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1323 struct segmented_address addr,
1324 const void *orig_data, const void *data,
1325 unsigned size)
1326{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001327 int rc;
1328 ulong linear;
1329
Avi Kivity83b87952011-04-03 11:31:19 +03001330 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001331 if (rc != X86EMUL_CONTINUE)
1332 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001333 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1334 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001335}
1336
Gleb Natapov7b262e92010-03-18 15:20:27 +02001337static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001338 unsigned int size, unsigned short port,
1339 void *dest)
1340{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001341 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001342
1343 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001344 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001345 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001346 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001347 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001348 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1349 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Mark Rustadb55a8142014-07-25 06:27:05 -07001350 n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001351 if (n == 0)
1352 n = 1;
1353 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001354 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001355 return 0;
1356 rc->end = n * size;
1357 }
1358
Nadav Amite6e39f02014-04-18 03:35:10 +03001359 if (ctxt->rep_prefix && (ctxt->d & String) &&
1360 !(ctxt->eflags & EFLG_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001361 ctxt->dst.data = rc->data + rc->pos;
1362 ctxt->dst.type = OP_MEM_STR;
1363 ctxt->dst.count = (rc->end - rc->pos) / size;
1364 rc->pos = rc->end;
1365 } else {
1366 memcpy(dest, rc->data + rc->pos, size);
1367 rc->pos += size;
1368 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001369 return 1;
1370}
1371
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001372static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1373 u16 index, struct desc_struct *desc)
1374{
1375 struct desc_ptr dt;
1376 ulong addr;
1377
1378 ctxt->ops->get_idt(ctxt, &dt);
1379
1380 if (dt.size < index * 8 + 7)
1381 return emulate_gp(ctxt, index << 3 | 0x2);
1382
1383 addr = dt.address + index * 8;
1384 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1385 &ctxt->exception);
1386}
1387
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001388static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001389 u16 selector, struct desc_ptr *dt)
1390{
Mathias Krause0225fb52012-08-30 01:30:16 +02001391 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001392 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001393
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001394 if (selector & 1 << 2) {
1395 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001396 u16 sel;
1397
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001398 memset (dt, 0, sizeof *dt);
Nadav Amit2eedcac2014-06-02 18:34:05 +03001399 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1400 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001401 return;
1402
1403 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001404 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001405 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001406 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001407}
1408
1409/* allowed just for 8 bytes segments */
1410static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001411 u16 selector, struct desc_struct *desc,
1412 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001413{
1414 struct desc_ptr dt;
1415 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001416 ulong addr;
1417
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001418 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001419
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001420 if (dt.size < index * 8 + 7)
1421 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001422
Avi Kivitye9194642012-06-13 16:29:39 +03001423 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001424 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1425 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001426}
1427
1428/* allowed just for 8 bytes segments */
1429static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001430 u16 selector, struct desc_struct *desc)
1431{
1432 struct desc_ptr dt;
1433 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001434 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001435
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001436 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001437
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001438 if (dt.size < index * 8 + 7)
1439 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001440
1441 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001442 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1443 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001444}
1445
Gleb Natapov5601d052011-03-07 14:55:06 +02001446/* Does not support long mode */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001447static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Nadav Amitd1442d82014-09-18 22:39:39 +03001448 u16 selector, int seg, u8 cpl,
1449 bool in_task_switch,
1450 struct desc_struct *desc)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001451{
Avi Kivity869be992012-06-13 16:30:53 +03001452 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001453 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001454 unsigned err_vec = GP_VECTOR;
1455 u32 err_code = 0;
1456 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001457 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001458 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001459 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001460 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001461
1462 memset(&seg_desc, 0, sizeof seg_desc);
1463
Kevin Wolff8da94e2013-04-11 14:06:03 +02001464 if (ctxt->mode == X86EMUL_MODE_REAL) {
1465 /* set real mode segment descriptor (keep limit etc. for
1466 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001467 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001468 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001469 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001470 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1471 /* VM86 needs a clean new segment descriptor */
1472 set_desc_base(&seg_desc, selector << 4);
1473 set_desc_limit(&seg_desc, 0xffff);
1474 seg_desc.type = 3;
1475 seg_desc.p = 1;
1476 seg_desc.s = 1;
1477 seg_desc.dpl = 3;
1478 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001479 }
1480
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001481 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001482
1483 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1484 if ((seg == VCPU_SREG_CS
1485 || (seg == VCPU_SREG_SS
1486 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1487 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001488 && null_selector)
1489 goto exception;
1490
1491 /* TR should be in GDT only */
1492 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1493 goto exception;
1494
1495 if (null_selector) /* for NULL selector skip all following checks */
1496 goto load;
1497
Avi Kivitye9194642012-06-13 16:29:39 +03001498 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001499 if (ret != X86EMUL_CONTINUE)
1500 return ret;
1501
1502 err_code = selector & 0xfffc;
Paolo Bonzini15fc0752014-08-18 13:17:00 +02001503 err_vec = in_task_switch ? TS_VECTOR : GP_VECTOR;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001504
Guo Chaofc058682012-06-28 15:19:51 +08001505 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001506 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1507 goto exception;
1508
1509 if (!seg_desc.p) {
1510 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1511 goto exception;
1512 }
1513
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001514 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001515
1516 switch (seg) {
1517 case VCPU_SREG_SS:
1518 /*
1519 * segment is not a writable data segment or segment
1520 * selector's RPL != CPL or segment selector's RPL != CPL
1521 */
1522 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1523 goto exception;
1524 break;
1525 case VCPU_SREG_CS:
1526 if (!(seg_desc.type & 8))
1527 goto exception;
1528
1529 if (seg_desc.type & 4) {
1530 /* conforming */
1531 if (dpl > cpl)
1532 goto exception;
1533 } else {
1534 /* nonconforming */
1535 if (rpl > cpl || dpl != cpl)
1536 goto exception;
1537 }
Nadav Amit040c8dc2014-09-18 22:39:43 +03001538 /* in long-mode d/b must be clear if l is set */
1539 if (seg_desc.d && seg_desc.l) {
1540 u64 efer = 0;
1541
1542 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1543 if (efer & EFER_LMA)
1544 goto exception;
1545 }
1546
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001547 /* CS(RPL) <- CPL */
1548 selector = (selector & 0xfffc) | cpl;
1549 break;
1550 case VCPU_SREG_TR:
1551 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1552 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001553 old_desc = seg_desc;
1554 seg_desc.type |= 2; /* busy */
1555 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1556 sizeof(seg_desc), &ctxt->exception);
1557 if (ret != X86EMUL_CONTINUE)
1558 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001559 break;
1560 case VCPU_SREG_LDTR:
1561 if (seg_desc.s || seg_desc.type != 2)
1562 goto exception;
1563 break;
1564 default: /* DS, ES, FS, or GS */
1565 /*
1566 * segment is not a data or readable code segment or
1567 * ((segment is a data or nonconforming code segment)
1568 * and (both RPL and CPL > DPL))
1569 */
1570 if ((seg_desc.type & 0xa) == 0x8 ||
1571 (((seg_desc.type & 0xc) != 0xc) &&
1572 (rpl > dpl && cpl > dpl)))
1573 goto exception;
1574 break;
1575 }
1576
1577 if (seg_desc.s) {
1578 /* mark segment as accessed */
1579 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001580 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001581 if (ret != X86EMUL_CONTINUE)
1582 return ret;
Nadav Amite37a75a2014-06-02 18:34:04 +03001583 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
1584 ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3,
1585 sizeof(base3), &ctxt->exception);
1586 if (ret != X86EMUL_CONTINUE)
1587 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001588 }
1589load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001590 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Nadav Amitd1442d82014-09-18 22:39:39 +03001591 if (desc)
1592 *desc = seg_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001593 return X86EMUL_CONTINUE;
1594exception:
Paolo Bonzini592f0852014-08-20 10:05:08 +02001595 return emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001596}
1597
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001598static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1599 u16 selector, int seg)
1600{
1601 u8 cpl = ctxt->ops->cpl(ctxt);
Nadav Amitd1442d82014-09-18 22:39:39 +03001602 return __load_segment_descriptor(ctxt, selector, seg, cpl, false, NULL);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001603}
1604
Wei Yongjun31be40b2010-08-17 09:17:30 +08001605static void write_register_operand(struct operand *op)
1606{
1607 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1608 switch (op->bytes) {
1609 case 1:
1610 *(u8 *)op->addr.reg = (u8)op->val;
1611 break;
1612 case 2:
1613 *(u16 *)op->addr.reg = (u16)op->val;
1614 break;
1615 case 4:
1616 *op->addr.reg = (u32)op->val;
1617 break; /* 64b: zero-extend */
1618 case 8:
1619 *op->addr.reg = op->val;
1620 break;
1621 }
1622}
1623
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001624static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001625{
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001626 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001627 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001628 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001629 break;
1630 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001631 if (ctxt->lock_prefix)
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001632 return segmented_cmpxchg(ctxt,
1633 op->addr.mem,
1634 &op->orig_val,
1635 &op->val,
1636 op->bytes);
1637 else
1638 return segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001639 op->addr.mem,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001640 &op->val,
1641 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001642 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001643 case OP_MEM_STR:
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001644 return segmented_write(ctxt,
1645 op->addr.mem,
1646 op->data,
1647 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001648 break;
Avi Kivity12537912011-03-29 11:41:27 +02001649 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001650 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001651 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001652 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001653 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001654 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001655 case OP_NONE:
1656 /* no writeback */
1657 break;
1658 default:
1659 break;
1660 }
1661 return X86EMUL_CONTINUE;
1662}
1663
Avi Kivity51ddff52012-06-12 20:19:40 +03001664static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001665{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001666 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001667
Avi Kivity5ad105e2012-08-19 14:34:31 +03001668 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001669 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001670 addr.seg = VCPU_SREG_SS;
1671
Avi Kivity51ddff52012-06-12 20:19:40 +03001672 return segmented_write(ctxt, addr, data, bytes);
1673}
1674
1675static int em_push(struct x86_emulate_ctxt *ctxt)
1676{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001677 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001678 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001679 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001680}
1681
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001682static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001683 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001684{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001685 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001686 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001687
Avi Kivitydd856ef2012-08-27 23:46:17 +03001688 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001689 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001690 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001691 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001692 return rc;
1693
Avi Kivity5ad105e2012-08-19 14:34:31 +03001694 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001695 return rc;
1696}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001697
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001698static int em_pop(struct x86_emulate_ctxt *ctxt)
1699{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001700 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001701}
1702
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001703static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001704 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001705{
1706 int rc;
1707 unsigned long val, change_mask;
1708 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001709 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001710
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001711 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001712 if (rc != X86EMUL_CONTINUE)
1713 return rc;
1714
1715 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
Nadav Amit163b1352014-07-21 14:37:28 +03001716 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_AC | EFLG_ID;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001717
1718 switch(ctxt->mode) {
1719 case X86EMUL_MODE_PROT64:
1720 case X86EMUL_MODE_PROT32:
1721 case X86EMUL_MODE_PROT16:
1722 if (cpl == 0)
1723 change_mask |= EFLG_IOPL;
1724 if (cpl <= iopl)
1725 change_mask |= EFLG_IF;
1726 break;
1727 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001728 if (iopl < 3)
1729 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001730 change_mask |= EFLG_IF;
1731 break;
1732 default: /* real mode */
1733 change_mask |= (EFLG_IOPL | EFLG_IF);
1734 break;
1735 }
1736
1737 *(unsigned long *)dest =
1738 (ctxt->eflags & ~change_mask) | (val & change_mask);
1739
1740 return rc;
1741}
1742
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001743static int em_popf(struct x86_emulate_ctxt *ctxt)
1744{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001745 ctxt->dst.type = OP_REG;
1746 ctxt->dst.addr.reg = &ctxt->eflags;
1747 ctxt->dst.bytes = ctxt->op_bytes;
1748 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001749}
1750
Avi Kivity612e89f2012-06-12 20:03:23 +03001751static int em_enter(struct x86_emulate_ctxt *ctxt)
1752{
1753 int rc;
1754 unsigned frame_size = ctxt->src.val;
1755 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001756 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001757
1758 if (nesting_level)
1759 return X86EMUL_UNHANDLEABLE;
1760
Avi Kivitydd856ef2012-08-27 23:46:17 +03001761 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1762 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001763 if (rc != X86EMUL_CONTINUE)
1764 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001765 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001766 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001767 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1768 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001769 stack_mask(ctxt));
1770 return X86EMUL_CONTINUE;
1771}
1772
Avi Kivityf47cfa32012-06-07 17:49:24 +03001773static int em_leave(struct x86_emulate_ctxt *ctxt)
1774{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001775 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001776 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001777 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001778}
1779
Avi Kivity1cd196e2011-09-13 10:45:51 +03001780static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001781{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001782 int seg = ctxt->src2.val;
1783
Avi Kivity9dac77f2011-06-01 15:34:25 +03001784 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001785
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001786 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001787}
1788
Avi Kivity1cd196e2011-09-13 10:45:51 +03001789static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001790{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001791 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001792 unsigned long selector;
1793 int rc;
1794
Avi Kivity9dac77f2011-06-01 15:34:25 +03001795 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001796 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001797 return rc;
1798
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001799 if (ctxt->modrm_reg == VCPU_SREG_SS)
1800 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
1801
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001802 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001803 return rc;
1804}
1805
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001806static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001807{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001808 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001809 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001810 int reg = VCPU_REGS_RAX;
1811
1812 while (reg <= VCPU_REGS_RDI) {
1813 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001814 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001815
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001816 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001817 if (rc != X86EMUL_CONTINUE)
1818 return rc;
1819
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001820 ++reg;
1821 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001822
Wei Yongjunc37eda12010-06-15 09:03:33 +08001823 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001824}
1825
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001826static int em_pushf(struct x86_emulate_ctxt *ctxt)
1827{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001828 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001829 return em_push(ctxt);
1830}
1831
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001832static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001833{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001834 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001835 int reg = VCPU_REGS_RDI;
1836
1837 while (reg >= VCPU_REGS_RAX) {
1838 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001839 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001840 --reg;
1841 }
1842
Avi Kivitydd856ef2012-08-27 23:46:17 +03001843 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001844 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001845 break;
1846 --reg;
1847 }
1848 return rc;
1849}
1850
Avi Kivitydd856ef2012-08-27 23:46:17 +03001851static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001852{
Mathias Krause0225fb52012-08-30 01:30:16 +02001853 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001854 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001855 struct desc_ptr dt;
1856 gva_t cs_addr;
1857 gva_t eip_addr;
1858 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001859
1860 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001861 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001862 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001863 if (rc != X86EMUL_CONTINUE)
1864 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001865
1866 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1867
Avi Kivity9dac77f2011-06-01 15:34:25 +03001868 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001869 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001870 if (rc != X86EMUL_CONTINUE)
1871 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001872
Avi Kivity9dac77f2011-06-01 15:34:25 +03001873 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001874 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001875 if (rc != X86EMUL_CONTINUE)
1876 return rc;
1877
Avi Kivity4bff1e862011-04-20 13:37:53 +03001878 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001879
1880 eip_addr = dt.address + (irq << 2);
1881 cs_addr = dt.address + (irq << 2) + 2;
1882
Avi Kivity0f65dd72011-04-20 13:37:53 +03001883 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001884 if (rc != X86EMUL_CONTINUE)
1885 return rc;
1886
Avi Kivity0f65dd72011-04-20 13:37:53 +03001887 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001888 if (rc != X86EMUL_CONTINUE)
1889 return rc;
1890
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001891 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001892 if (rc != X86EMUL_CONTINUE)
1893 return rc;
1894
Avi Kivity9dac77f2011-06-01 15:34:25 +03001895 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001896
1897 return rc;
1898}
1899
Avi Kivitydd856ef2012-08-27 23:46:17 +03001900int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1901{
1902 int rc;
1903
1904 invalidate_registers(ctxt);
1905 rc = __emulate_int_real(ctxt, irq);
1906 if (rc == X86EMUL_CONTINUE)
1907 writeback_registers(ctxt);
1908 return rc;
1909}
1910
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001911static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001912{
1913 switch(ctxt->mode) {
1914 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001915 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001916 case X86EMUL_MODE_VM86:
1917 case X86EMUL_MODE_PROT16:
1918 case X86EMUL_MODE_PROT32:
1919 case X86EMUL_MODE_PROT64:
1920 default:
1921 /* Protected mode interrupts unimplemented yet */
1922 return X86EMUL_UNHANDLEABLE;
1923 }
1924}
1925
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001926static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001927{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001928 int rc = X86EMUL_CONTINUE;
1929 unsigned long temp_eip = 0;
1930 unsigned long temp_eflags = 0;
1931 unsigned long cs = 0;
1932 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1933 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1934 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1935 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1936
1937 /* TODO: Add stack limit check */
1938
Avi Kivity9dac77f2011-06-01 15:34:25 +03001939 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001940
1941 if (rc != X86EMUL_CONTINUE)
1942 return rc;
1943
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001944 if (temp_eip & ~0xffff)
1945 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001946
Avi Kivity9dac77f2011-06-01 15:34:25 +03001947 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001948
1949 if (rc != X86EMUL_CONTINUE)
1950 return rc;
1951
Avi Kivity9dac77f2011-06-01 15:34:25 +03001952 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001953
1954 if (rc != X86EMUL_CONTINUE)
1955 return rc;
1956
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001957 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001958
1959 if (rc != X86EMUL_CONTINUE)
1960 return rc;
1961
Avi Kivity9dac77f2011-06-01 15:34:25 +03001962 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001963
1964
Avi Kivity9dac77f2011-06-01 15:34:25 +03001965 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001966 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001967 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001968 ctxt->eflags &= ~0xffff;
1969 ctxt->eflags |= temp_eflags;
1970 }
1971
1972 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1973 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1974
1975 return rc;
1976}
1977
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001978static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001979{
1980 switch(ctxt->mode) {
1981 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001982 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001983 case X86EMUL_MODE_VM86:
1984 case X86EMUL_MODE_PROT16:
1985 case X86EMUL_MODE_PROT32:
1986 case X86EMUL_MODE_PROT64:
1987 default:
1988 /* iret from protected mode unimplemented yet */
1989 return X86EMUL_UNHANDLEABLE;
1990 }
1991}
1992
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001993static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1994{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001995 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03001996 unsigned short sel, old_sel;
1997 struct desc_struct old_desc, new_desc;
1998 const struct x86_emulate_ops *ops = ctxt->ops;
1999 u8 cpl = ctxt->ops->cpl(ctxt);
2000
2001 /* Assignment of RIP may only fail in 64-bit mode */
2002 if (ctxt->mode == X86EMUL_MODE_PROT64)
2003 ops->get_segment(ctxt, &old_sel, &old_desc, NULL,
2004 VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002005
Avi Kivity9dac77f2011-06-01 15:34:25 +03002006 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002007
Nadav Amitd1442d82014-09-18 22:39:39 +03002008 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl, false,
2009 &new_desc);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002010 if (rc != X86EMUL_CONTINUE)
2011 return rc;
2012
Nadav Amitd1442d82014-09-18 22:39:39 +03002013 rc = assign_eip_far(ctxt, ctxt->src.val, new_desc.l);
2014 if (rc != X86EMUL_CONTINUE) {
2015 WARN_ON(!ctxt->mode != X86EMUL_MODE_PROT64);
2016 /* assigning eip failed; restore the old cs */
2017 ops->set_segment(ctxt, old_sel, &old_desc, 0, VCPU_SREG_CS);
2018 return rc;
2019 }
2020 return rc;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002021}
2022
Takuya Yoshikawa51187682011-05-02 02:29:17 +09002023static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002024{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002025 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002026
Avi Kivity9dac77f2011-06-01 15:34:25 +03002027 switch (ctxt->modrm_reg) {
Mohammed Gamald19292e2008-09-08 21:47:19 +03002028 case 2: /* call near abs */ {
2029 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002030 old_eip = ctxt->_eip;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002031 rc = assign_eip_near(ctxt, ctxt->src.val);
2032 if (rc != X86EMUL_CONTINUE)
2033 break;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002034 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002035 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03002036 break;
2037 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002038 case 4: /* jmp abs */
Nadav Amit234f3ce2014-09-18 22:39:38 +03002039 rc = assign_eip_near(ctxt, ctxt->src.val);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002040 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002041 case 5: /* jmp far */
2042 rc = em_jmp_far(ctxt);
2043 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002044 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002045 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002046 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002047 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002048 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002049}
2050
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002051static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002052{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002053 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002054
Nadav Amitaaa05f22014-06-02 18:34:10 +03002055 if (ctxt->dst.bytes == 16)
2056 return X86EMUL_UNHANDLEABLE;
2057
Avi Kivitydd856ef2012-08-27 23:46:17 +03002058 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2059 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2060 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2061 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002062 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002063 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002064 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2065 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002066
Laurent Vivier05f086f2007-09-24 11:10:55 +02002067 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002068 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002069 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002070}
2071
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002072static int em_ret(struct x86_emulate_ctxt *ctxt)
2073{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002074 int rc;
2075 unsigned long eip;
2076
2077 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
2078 if (rc != X86EMUL_CONTINUE)
2079 return rc;
2080
2081 return assign_eip_near(ctxt, eip);
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002082}
2083
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002084static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002085{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002086 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002087 unsigned long eip, cs;
2088 u16 old_cs;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002089 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amitd1442d82014-09-18 22:39:39 +03002090 struct desc_struct old_desc, new_desc;
2091 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002092
Nadav Amitd1442d82014-09-18 22:39:39 +03002093 if (ctxt->mode == X86EMUL_MODE_PROT64)
2094 ops->get_segment(ctxt, &old_cs, &old_desc, NULL,
2095 VCPU_SREG_CS);
2096
2097 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002098 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002099 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002100 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002101 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002102 return rc;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002103 /* Outer-privilege level return is not implemented */
2104 if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2105 return X86EMUL_UNHANDLEABLE;
Nadav Amitd1442d82014-09-18 22:39:39 +03002106 rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, 0, false,
2107 &new_desc);
2108 if (rc != X86EMUL_CONTINUE)
2109 return rc;
2110 rc = assign_eip_far(ctxt, eip, new_desc.l);
2111 if (rc != X86EMUL_CONTINUE) {
2112 WARN_ON(!ctxt->mode != X86EMUL_MODE_PROT64);
2113 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
2114 }
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002115 return rc;
2116}
2117
Bruce Rogers32611072013-09-09 09:40:20 -06002118static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2119{
2120 int rc;
2121
2122 rc = em_ret_far(ctxt);
2123 if (rc != X86EMUL_CONTINUE)
2124 return rc;
2125 rsp_increment(ctxt, ctxt->src.val);
2126 return X86EMUL_CONTINUE;
2127}
2128
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002129static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2130{
2131 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002132 ctxt->dst.orig_val = ctxt->dst.val;
2133 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002134 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002135 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002136 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002137
2138 if (ctxt->eflags & EFLG_ZF) {
2139 /* Success: write back to memory. */
2140 ctxt->dst.val = ctxt->src.orig_val;
2141 } else {
2142 /* Failure: write the value we saw to EAX. */
2143 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002144 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Nadav Amit37c564f2014-06-02 18:34:07 +03002145 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002146 }
2147 return X86EMUL_CONTINUE;
2148}
2149
Avi Kivityd4b43252011-09-13 10:45:50 +03002150static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002151{
Avi Kivityd4b43252011-09-13 10:45:50 +03002152 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002153 unsigned short sel;
2154 int rc;
2155
Avi Kivity9dac77f2011-06-01 15:34:25 +03002156 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002157
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002158 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002159 if (rc != X86EMUL_CONTINUE)
2160 return rc;
2161
Avi Kivity9dac77f2011-06-01 15:34:25 +03002162 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002163 return rc;
2164}
2165
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002166static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002167setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002168 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002169{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002170 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002171 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002172 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002173 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002174 cs->type = 0x0b; /* Read, Execute, Accessed */
2175 cs->s = 1;
2176 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002177 cs->p = 1;
2178 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002179 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002180
Gleb Natapov79168fd2010-04-28 19:15:30 +03002181 set_desc_base(ss, 0); /* flat segment */
2182 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002183 ss->g = 1; /* 4kb granularity */
2184 ss->s = 1;
2185 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002186 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002187 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002188 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002189 ss->l = 0;
2190 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002191}
2192
Avi Kivity1a18a692012-02-01 12:23:21 +02002193static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2194{
2195 u32 eax, ebx, ecx, edx;
2196
2197 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002198 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2199 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002200 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2201 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2202}
2203
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002204static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2205{
Mathias Krause0225fb52012-08-30 01:30:16 +02002206 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002207 u32 eax, ebx, ecx, edx;
2208
2209 /*
2210 * syscall should always be enabled in longmode - so only become
2211 * vendor specific (cpuid) if other modes are active...
2212 */
2213 if (ctxt->mode == X86EMUL_MODE_PROT64)
2214 return true;
2215
2216 eax = 0x00000000;
2217 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002218 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2219 /*
2220 * Intel ("GenuineIntel")
2221 * remark: Intel CPUs only support "syscall" in 64bit
2222 * longmode. Also an 64bit guest with a
2223 * 32bit compat-app running will #UD !! While this
2224 * behaviour can be fixed (by emulating) into AMD
2225 * response - CPUs of AMD can't behave like Intel.
2226 */
2227 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2228 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2229 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2230 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002231
Avi Kivity0017f932012-06-07 14:10:16 +03002232 /* AMD ("AuthenticAMD") */
2233 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2234 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2235 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2236 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002237
Avi Kivity0017f932012-06-07 14:10:16 +03002238 /* AMD ("AMDisbetter!") */
2239 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2240 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2241 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2242 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002243
2244 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2245 return false;
2246}
2247
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002248static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002249{
Mathias Krause0225fb52012-08-30 01:30:16 +02002250 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002251 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002252 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002253 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002254 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002255
2256 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002257 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002258 ctxt->mode == X86EMUL_MODE_VM86)
2259 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002260
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002261 if (!(em_syscall_is_enabled(ctxt)))
2262 return emulate_ud(ctxt);
2263
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002264 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002265 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002266
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002267 if (!(efer & EFER_SCE))
2268 return emulate_ud(ctxt);
2269
Avi Kivity717746e2011-04-20 13:37:53 +03002270 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002271 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002272 cs_sel = (u16)(msr_data & 0xfffc);
2273 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002274
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002275 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002276 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002277 cs.l = 1;
2278 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002279 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2280 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002281
Avi Kivitydd856ef2012-08-27 23:46:17 +03002282 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002283 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002284#ifdef CONFIG_X86_64
Nadav Amit6c6cb692014-07-21 14:37:30 +03002285 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002286
Avi Kivity717746e2011-04-20 13:37:53 +03002287 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002288 ctxt->mode == X86EMUL_MODE_PROT64 ?
2289 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002290 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002291
Avi Kivity717746e2011-04-20 13:37:53 +03002292 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Nadav Amit6c6cb692014-07-21 14:37:30 +03002293 ctxt->eflags &= ~msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002294#endif
2295 } else {
2296 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002297 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002298 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002299
Nadav Amit6c6cb692014-07-21 14:37:30 +03002300 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002301 }
2302
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002303 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002304}
2305
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002306static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002307{
Mathias Krause0225fb52012-08-30 01:30:16 +02002308 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002309 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002310 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002311 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002312 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002313
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002314 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002315 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002316 if (ctxt->mode == X86EMUL_MODE_REAL)
2317 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002318
Avi Kivity1a18a692012-02-01 12:23:21 +02002319 /*
2320 * Not recognized on AMD in compat mode (but is recognized in legacy
2321 * mode).
2322 */
2323 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2324 && !vendor_intel(ctxt))
2325 return emulate_ud(ctxt);
2326
Andre Przywara8c604352009-06-18 12:56:01 +02002327 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2328 * Therefore, we inject an #UD.
2329 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002330 if (ctxt->mode == X86EMUL_MODE_PROT64)
2331 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002332
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002333 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002334
Avi Kivity717746e2011-04-20 13:37:53 +03002335 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002336 switch (ctxt->mode) {
2337 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002338 if ((msr_data & 0xfffc) == 0x0)
2339 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002340 break;
2341 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002342 if (msr_data == 0x0)
2343 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002344 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002345 default:
2346 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002347 }
2348
Nadav Amit6c6cb692014-07-21 14:37:30 +03002349 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002350 cs_sel = (u16)msr_data;
2351 cs_sel &= ~SELECTOR_RPL_MASK;
2352 ss_sel = cs_sel + 8;
2353 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002354 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002355 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002356 cs.l = 1;
2357 }
2358
Avi Kivity1aa36612011-04-27 13:20:30 +03002359 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2360 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002361
Avi Kivity717746e2011-04-20 13:37:53 +03002362 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002363 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002364
Avi Kivity717746e2011-04-20 13:37:53 +03002365 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002366 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002367
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002368 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002369}
2370
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002371static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002372{
Mathias Krause0225fb52012-08-30 01:30:16 +02002373 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002374 struct desc_struct cs, ss;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002375 u64 msr_data, rcx, rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002376 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002377 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002378
Gleb Natapova0044752010-02-10 14:21:31 +02002379 /* inject #GP if in real mode or Virtual 8086 mode */
2380 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002381 ctxt->mode == X86EMUL_MODE_VM86)
2382 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002383
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002384 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002385
Avi Kivity9dac77f2011-06-01 15:34:25 +03002386 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002387 usermode = X86EMUL_MODE_PROT64;
2388 else
2389 usermode = X86EMUL_MODE_PROT32;
2390
Nadav Amit234f3ce2014-09-18 22:39:38 +03002391 rcx = reg_read(ctxt, VCPU_REGS_RCX);
2392 rdx = reg_read(ctxt, VCPU_REGS_RDX);
2393
Andre Przywara4668f052009-06-18 12:56:02 +02002394 cs.dpl = 3;
2395 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002396 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002397 switch (usermode) {
2398 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002399 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002400 if ((msr_data & 0xfffc) == 0x0)
2401 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002402 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002403 break;
2404 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002405 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002406 if (msr_data == 0x0)
2407 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002408 ss_sel = cs_sel + 8;
2409 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002410 cs.l = 1;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002411 if (is_noncanonical_address(rcx) ||
2412 is_noncanonical_address(rdx))
2413 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002414 break;
2415 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002416 cs_sel |= SELECTOR_RPL_MASK;
2417 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002418
Avi Kivity1aa36612011-04-27 13:20:30 +03002419 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2420 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002421
Nadav Amit234f3ce2014-09-18 22:39:38 +03002422 ctxt->_eip = rdx;
2423 *reg_write(ctxt, VCPU_REGS_RSP) = rcx;
Andre Przywara4668f052009-06-18 12:56:02 +02002424
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002425 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002426}
2427
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002428static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002429{
2430 int iopl;
2431 if (ctxt->mode == X86EMUL_MODE_REAL)
2432 return false;
2433 if (ctxt->mode == X86EMUL_MODE_VM86)
2434 return true;
2435 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002436 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002437}
2438
2439static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002440 u16 port, u16 len)
2441{
Mathias Krause0225fb52012-08-30 01:30:16 +02002442 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002443 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002444 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002445 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002446 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002447 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002448 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002449
Avi Kivity1aa36612011-04-27 13:20:30 +03002450 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002451 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002452 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002453 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002454 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002455 base = get_desc_base(&tr_seg);
2456#ifdef CONFIG_X86_64
2457 base |= ((u64)base3) << 32;
2458#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002459 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002460 if (r != X86EMUL_CONTINUE)
2461 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002462 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002463 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002464 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002465 if (r != X86EMUL_CONTINUE)
2466 return false;
2467 if ((perm >> bit_idx) & mask)
2468 return false;
2469 return true;
2470}
2471
2472static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002473 u16 port, u16 len)
2474{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002475 if (ctxt->perm_ok)
2476 return true;
2477
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002478 if (emulator_bad_iopl(ctxt))
2479 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002480 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002481
2482 ctxt->perm_ok = true;
2483
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002484 return true;
2485}
2486
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002487static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002488 struct tss_segment_16 *tss)
2489{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002490 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002491 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002492 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2493 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2494 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2495 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2496 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2497 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2498 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2499 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002500
Avi Kivity1aa36612011-04-27 13:20:30 +03002501 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2502 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2503 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2504 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2505 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002506}
2507
2508static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002509 struct tss_segment_16 *tss)
2510{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002511 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002512 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002513
Avi Kivity9dac77f2011-06-01 15:34:25 +03002514 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002515 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002516 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2517 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2518 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2519 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2520 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2521 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2522 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2523 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002524
2525 /*
2526 * SDM says that segment selectors are loaded before segment
2527 * descriptors
2528 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002529 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2530 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2531 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2532 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2533 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002534
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002535 cpl = tss->cs & 3;
2536
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002537 /*
Guo Chaofc058682012-06-28 15:19:51 +08002538 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002539 * it is handled in a context of new task
2540 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002541 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl,
2542 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002543 if (ret != X86EMUL_CONTINUE)
2544 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002545 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
2546 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002547 if (ret != X86EMUL_CONTINUE)
2548 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002549 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
2550 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002551 if (ret != X86EMUL_CONTINUE)
2552 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002553 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
2554 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002555 if (ret != X86EMUL_CONTINUE)
2556 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002557 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
2558 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002559 if (ret != X86EMUL_CONTINUE)
2560 return ret;
2561
2562 return X86EMUL_CONTINUE;
2563}
2564
2565static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002566 u16 tss_selector, u16 old_tss_sel,
2567 ulong old_tss_base, struct desc_struct *new_desc)
2568{
Mathias Krause0225fb52012-08-30 01:30:16 +02002569 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002570 struct tss_segment_16 tss_seg;
2571 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002572 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002573
Avi Kivity0f65dd72011-04-20 13:37:53 +03002574 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002575 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002576 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002577 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002578 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002579
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002580 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002581
Avi Kivity0f65dd72011-04-20 13:37:53 +03002582 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002583 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002584 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002585 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002586 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002587
Avi Kivity0f65dd72011-04-20 13:37:53 +03002588 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002589 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002590 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002591 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002592 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002593
2594 if (old_tss_sel != 0xffff) {
2595 tss_seg.prev_task_link = old_tss_sel;
2596
Avi Kivity0f65dd72011-04-20 13:37:53 +03002597 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002598 &tss_seg.prev_task_link,
2599 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002600 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002601 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002602 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002603 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002604 }
2605
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002606 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002607}
2608
2609static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002610 struct tss_segment_32 *tss)
2611{
Nadav Amit5c7411e2014-04-07 18:37:47 +03002612 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002613 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002614 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002615 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2616 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2617 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2618 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2619 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2620 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2621 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2622 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002623
Avi Kivity1aa36612011-04-27 13:20:30 +03002624 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2625 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2626 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2627 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2628 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2629 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002630}
2631
2632static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002633 struct tss_segment_32 *tss)
2634{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002635 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002636 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002637
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002638 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002639 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002640 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002641 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002642
2643 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002644 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2645 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2646 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2647 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2648 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2649 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2650 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2651 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002652
2653 /*
2654 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002655 * descriptors. This is important because CPL checks will
2656 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002657 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002658 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2659 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2660 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2661 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2662 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2663 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2664 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002665
2666 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002667 * If we're switching between Protected Mode and VM86, we need to make
2668 * sure to update the mode before loading the segment descriptors so
2669 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01002670 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002671 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002672 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002673 cpl = 3;
2674 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002675 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002676 cpl = tss->cs & 3;
2677 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01002678
2679 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002680 * Now load segment descriptors. If fault happenes at this stage
2681 * it is handled in a context of new task
2682 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002683 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR,
2684 cpl, true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002685 if (ret != X86EMUL_CONTINUE)
2686 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002687 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
2688 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002689 if (ret != X86EMUL_CONTINUE)
2690 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002691 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
2692 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002693 if (ret != X86EMUL_CONTINUE)
2694 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002695 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
2696 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002697 if (ret != X86EMUL_CONTINUE)
2698 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002699 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
2700 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->fs, VCPU_SREG_FS, 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->gs, VCPU_SREG_GS, cpl,
2708 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002709 if (ret != X86EMUL_CONTINUE)
2710 return ret;
2711
2712 return X86EMUL_CONTINUE;
2713}
2714
2715static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002716 u16 tss_selector, u16 old_tss_sel,
2717 ulong old_tss_base, struct desc_struct *new_desc)
2718{
Mathias Krause0225fb52012-08-30 01:30:16 +02002719 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002720 struct tss_segment_32 tss_seg;
2721 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002722 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03002723 u32 eip_offset = offsetof(struct tss_segment_32, eip);
2724 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002725
Avi Kivity0f65dd72011-04-20 13:37:53 +03002726 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002727 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002728 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002729 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002730 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002731
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002732 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002733
Nadav Amit5c7411e2014-04-07 18:37:47 +03002734 /* Only GP registers and segment selectors are saved */
2735 ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
2736 ldt_sel_offset - eip_offset, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002737 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002738 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002739 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002740
Avi Kivity0f65dd72011-04-20 13:37:53 +03002741 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002742 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002743 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002744 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002745 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002746
2747 if (old_tss_sel != 0xffff) {
2748 tss_seg.prev_task_link = old_tss_sel;
2749
Avi Kivity0f65dd72011-04-20 13:37:53 +03002750 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002751 &tss_seg.prev_task_link,
2752 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002753 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002754 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002755 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002756 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002757 }
2758
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002759 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002760}
2761
2762static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002763 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002764 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002765{
Mathias Krause0225fb52012-08-30 01:30:16 +02002766 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002767 struct desc_struct curr_tss_desc, next_tss_desc;
2768 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002769 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002770 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002771 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002772 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002773 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002774
2775 /* FIXME: old_tss_base == ~0 ? */
2776
Avi Kivitye9194642012-06-13 16:29:39 +03002777 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002778 if (ret != X86EMUL_CONTINUE)
2779 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002780 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002781 if (ret != X86EMUL_CONTINUE)
2782 return ret;
2783
2784 /* FIXME: check that next_tss_desc is tss */
2785
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002786 /*
2787 * Check privileges. The three cases are task switch caused by...
2788 *
2789 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2790 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002791 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002792 */
2793 if (reason == TASK_SWITCH_GATE) {
2794 if (idt_index != -1) {
2795 /* Software interrupts */
2796 struct desc_struct task_gate_desc;
2797 int dpl;
2798
2799 ret = read_interrupt_descriptor(ctxt, idt_index,
2800 &task_gate_desc);
2801 if (ret != X86EMUL_CONTINUE)
2802 return ret;
2803
2804 dpl = task_gate_desc.dpl;
2805 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2806 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2807 }
2808 } else if (reason != TASK_SWITCH_IRET) {
2809 int dpl = next_tss_desc.dpl;
2810 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2811 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002812 }
2813
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002814
Gleb Natapovceffb452010-03-18 15:20:19 +02002815 desc_limit = desc_limit_scaled(&next_tss_desc);
2816 if (!next_tss_desc.p ||
2817 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2818 desc_limit < 0x2b)) {
Paolo Bonzini592f0852014-08-20 10:05:08 +02002819 return emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002820 }
2821
2822 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2823 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002824 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002825 }
2826
2827 if (reason == TASK_SWITCH_IRET)
2828 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2829
2830 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002831 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002832 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2833 old_tss_sel = 0xffff;
2834
2835 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002836 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002837 old_tss_base, &next_tss_desc);
2838 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002839 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002840 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002841 if (ret != X86EMUL_CONTINUE)
2842 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002843
2844 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2845 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2846
2847 if (reason != TASK_SWITCH_IRET) {
2848 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002849 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002850 }
2851
Avi Kivity717746e2011-04-20 13:37:53 +03002852 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002853 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002854
Jan Kiszkae269fb22010-04-14 15:51:09 +02002855 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002856 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2857 ctxt->lock_prefix = 0;
2858 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002859 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002860 }
2861
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002862 return ret;
2863}
2864
2865int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002866 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002867 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002868{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002869 int rc;
2870
Avi Kivitydd856ef2012-08-27 23:46:17 +03002871 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002872 ctxt->_eip = ctxt->eip;
2873 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002874
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002875 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002876 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002877
Avi Kivitydd856ef2012-08-27 23:46:17 +03002878 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002879 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002880 writeback_registers(ctxt);
2881 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002882
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002883 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002884}
2885
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002886static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2887 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002888{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002889 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002890
Avi Kivitydd856ef2012-08-27 23:46:17 +03002891 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2892 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002893}
2894
Avi Kivity7af04fc2010-08-18 14:16:35 +03002895static int em_das(struct x86_emulate_ctxt *ctxt)
2896{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002897 u8 al, old_al;
2898 bool af, cf, old_cf;
2899
2900 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002901 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002902
2903 old_al = al;
2904 old_cf = cf;
2905 cf = false;
2906 af = ctxt->eflags & X86_EFLAGS_AF;
2907 if ((al & 0x0f) > 9 || af) {
2908 al -= 6;
2909 cf = old_cf | (al >= 250);
2910 af = true;
2911 } else {
2912 af = false;
2913 }
2914 if (old_al > 0x99 || old_cf) {
2915 al -= 0x60;
2916 cf = true;
2917 }
2918
Avi Kivity9dac77f2011-06-01 15:34:25 +03002919 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002920 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002921 ctxt->src.type = OP_IMM;
2922 ctxt->src.val = 0;
2923 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02002924 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03002925 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2926 if (cf)
2927 ctxt->eflags |= X86_EFLAGS_CF;
2928 if (af)
2929 ctxt->eflags |= X86_EFLAGS_AF;
2930 return X86EMUL_CONTINUE;
2931}
2932
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02002933static int em_aam(struct x86_emulate_ctxt *ctxt)
2934{
2935 u8 al, ah;
2936
2937 if (ctxt->src.val == 0)
2938 return emulate_de(ctxt);
2939
2940 al = ctxt->dst.val & 0xff;
2941 ah = al / ctxt->src.val;
2942 al %= ctxt->src.val;
2943
2944 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
2945
2946 /* Set PF, ZF, SF */
2947 ctxt->src.type = OP_IMM;
2948 ctxt->src.val = 0;
2949 ctxt->src.bytes = 1;
2950 fastop(ctxt, em_or);
2951
2952 return X86EMUL_CONTINUE;
2953}
2954
Gleb Natapov7f662272012-12-10 11:42:30 +02002955static int em_aad(struct x86_emulate_ctxt *ctxt)
2956{
2957 u8 al = ctxt->dst.val & 0xff;
2958 u8 ah = (ctxt->dst.val >> 8) & 0xff;
2959
2960 al = (al + (ah * ctxt->src.val)) & 0xff;
2961
2962 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
2963
Gleb Natapovf583c292013-02-13 17:50:39 +02002964 /* Set PF, ZF, SF */
2965 ctxt->src.type = OP_IMM;
2966 ctxt->src.val = 0;
2967 ctxt->src.bytes = 1;
2968 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02002969
2970 return X86EMUL_CONTINUE;
2971}
2972
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002973static int em_call(struct x86_emulate_ctxt *ctxt)
2974{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002975 int rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002976 long rel = ctxt->src.val;
2977
2978 ctxt->src.val = (unsigned long)ctxt->_eip;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002979 rc = jmp_rel(ctxt, rel);
2980 if (rc != X86EMUL_CONTINUE)
2981 return rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002982 return em_push(ctxt);
2983}
2984
Avi Kivity0ef753b2010-08-18 14:51:45 +03002985static int em_call_far(struct x86_emulate_ctxt *ctxt)
2986{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002987 u16 sel, old_cs;
2988 ulong old_eip;
2989 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002990 struct desc_struct old_desc, new_desc;
2991 const struct x86_emulate_ops *ops = ctxt->ops;
2992 int cpl = ctxt->ops->cpl(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002993
Avi Kivity9dac77f2011-06-01 15:34:25 +03002994 old_eip = ctxt->_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03002995 ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002996
Avi Kivity9dac77f2011-06-01 15:34:25 +03002997 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Nadav Amitd1442d82014-09-18 22:39:39 +03002998 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl, false,
2999 &new_desc);
3000 if (rc != X86EMUL_CONTINUE)
Avi Kivity0ef753b2010-08-18 14:51:45 +03003001 return X86EMUL_CONTINUE;
3002
Nadav Amitd1442d82014-09-18 22:39:39 +03003003 rc = assign_eip_far(ctxt, ctxt->src.val, new_desc.l);
3004 if (rc != X86EMUL_CONTINUE)
3005 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003006
Avi Kivity9dac77f2011-06-01 15:34:25 +03003007 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003008 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003009 if (rc != X86EMUL_CONTINUE)
Nadav Amitd1442d82014-09-18 22:39:39 +03003010 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003011
Avi Kivity9dac77f2011-06-01 15:34:25 +03003012 ctxt->src.val = old_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003013 rc = em_push(ctxt);
3014 /* If we failed, we tainted the memory, but the very least we should
3015 restore cs */
3016 if (rc != X86EMUL_CONTINUE)
3017 goto fail;
3018 return rc;
3019fail:
3020 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
3021 return rc;
3022
Avi Kivity0ef753b2010-08-18 14:51:45 +03003023}
3024
Avi Kivity40ece7c2010-08-18 15:12:09 +03003025static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3026{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003027 int rc;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003028 unsigned long eip;
Avi Kivity40ece7c2010-08-18 15:12:09 +03003029
Nadav Amit234f3ce2014-09-18 22:39:38 +03003030 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
3031 if (rc != X86EMUL_CONTINUE)
3032 return rc;
3033 rc = assign_eip_near(ctxt, eip);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003034 if (rc != X86EMUL_CONTINUE)
3035 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003036 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003037 return X86EMUL_CONTINUE;
3038}
3039
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003040static int em_xchg(struct x86_emulate_ctxt *ctxt)
3041{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003042 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003043 ctxt->src.val = ctxt->dst.val;
3044 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003045
3046 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003047 ctxt->dst.val = ctxt->src.orig_val;
3048 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003049 return X86EMUL_CONTINUE;
3050}
3051
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003052static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3053{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003054 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003055 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003056}
3057
Avi Kivity61429142010-08-19 15:13:00 +03003058static int em_cwd(struct x86_emulate_ctxt *ctxt)
3059{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003060 ctxt->dst.type = OP_REG;
3061 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003062 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003063 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003064
3065 return X86EMUL_CONTINUE;
3066}
3067
Avi Kivity48bb5d32010-08-18 18:54:34 +03003068static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3069{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003070 u64 tsc = 0;
3071
Avi Kivity717746e2011-04-20 13:37:53 +03003072 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003073 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3074 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003075 return X86EMUL_CONTINUE;
3076}
3077
Avi Kivity222d21a2011-11-10 14:57:30 +02003078static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3079{
3080 u64 pmc;
3081
Avi Kivitydd856ef2012-08-27 23:46:17 +03003082 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003083 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003084 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3085 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003086 return X86EMUL_CONTINUE;
3087}
3088
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003089static int em_mov(struct x86_emulate_ctxt *ctxt)
3090{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01003091 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003092 return X86EMUL_CONTINUE;
3093}
3094
Borislav Petkov84cffe42013-10-29 12:54:56 +01003095#define FFL(x) bit(X86_FEATURE_##x)
3096
3097static int em_movbe(struct x86_emulate_ctxt *ctxt)
3098{
3099 u32 ebx, ecx, edx, eax = 1;
3100 u16 tmp;
3101
3102 /*
3103 * Check MOVBE is set in the guest-visible CPUID leaf.
3104 */
3105 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
3106 if (!(ecx & FFL(MOVBE)))
3107 return emulate_ud(ctxt);
3108
3109 switch (ctxt->op_bytes) {
3110 case 2:
3111 /*
3112 * From MOVBE definition: "...When the operand size is 16 bits,
3113 * the upper word of the destination register remains unchanged
3114 * ..."
3115 *
3116 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3117 * rules so we have to do the operation almost per hand.
3118 */
3119 tmp = (u16)ctxt->src.val;
3120 ctxt->dst.val &= ~0xffffUL;
3121 ctxt->dst.val |= (unsigned long)swab16(tmp);
3122 break;
3123 case 4:
3124 ctxt->dst.val = swab32((u32)ctxt->src.val);
3125 break;
3126 case 8:
3127 ctxt->dst.val = swab64(ctxt->src.val);
3128 break;
3129 default:
Paolo Bonzini592f0852014-08-20 10:05:08 +02003130 BUG();
Borislav Petkov84cffe42013-10-29 12:54:56 +01003131 }
3132 return X86EMUL_CONTINUE;
3133}
3134
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003135static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3136{
3137 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3138 return emulate_gp(ctxt, 0);
3139
3140 /* Disable writeback. */
3141 ctxt->dst.type = OP_NONE;
3142 return X86EMUL_CONTINUE;
3143}
3144
3145static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3146{
3147 unsigned long val;
3148
3149 if (ctxt->mode == X86EMUL_MODE_PROT64)
3150 val = ctxt->src.val & ~0ULL;
3151 else
3152 val = ctxt->src.val & ~0U;
3153
3154 /* #UD condition is already handled. */
3155 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3156 return emulate_gp(ctxt, 0);
3157
3158 /* Disable writeback. */
3159 ctxt->dst.type = OP_NONE;
3160 return X86EMUL_CONTINUE;
3161}
3162
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003163static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3164{
3165 u64 msr_data;
3166
Avi Kivitydd856ef2012-08-27 23:46:17 +03003167 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3168 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3169 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003170 return emulate_gp(ctxt, 0);
3171
3172 return X86EMUL_CONTINUE;
3173}
3174
3175static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3176{
3177 u64 msr_data;
3178
Avi Kivitydd856ef2012-08-27 23:46:17 +03003179 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003180 return emulate_gp(ctxt, 0);
3181
Avi Kivitydd856ef2012-08-27 23:46:17 +03003182 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3183 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003184 return X86EMUL_CONTINUE;
3185}
3186
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003187static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3188{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003189 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003190 return emulate_ud(ctxt);
3191
Avi Kivity9dac77f2011-06-01 15:34:25 +03003192 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003193 return X86EMUL_CONTINUE;
3194}
3195
3196static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3197{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003198 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003199
Avi Kivity9dac77f2011-06-01 15:34:25 +03003200 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003201 return emulate_ud(ctxt);
3202
Avi Kivity9dac77f2011-06-01 15:34:25 +03003203 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003204 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3205
3206 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003207 ctxt->dst.type = OP_NONE;
3208 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003209}
3210
Avi Kivitya14e5792012-06-13 12:28:33 +03003211static int em_lldt(struct x86_emulate_ctxt *ctxt)
3212{
3213 u16 sel = ctxt->src.val;
3214
3215 /* Disable writeback. */
3216 ctxt->dst.type = OP_NONE;
3217 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3218}
3219
Avi Kivity80890002012-06-13 16:33:29 +03003220static int em_ltr(struct x86_emulate_ctxt *ctxt)
3221{
3222 u16 sel = ctxt->src.val;
3223
3224 /* Disable writeback. */
3225 ctxt->dst.type = OP_NONE;
3226 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3227}
3228
Avi Kivity38503912011-03-31 18:48:09 +02003229static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3230{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003231 int rc;
3232 ulong linear;
3233
Avi Kivity9dac77f2011-06-01 15:34:25 +03003234 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003235 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003236 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003237 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003238 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003239 return X86EMUL_CONTINUE;
3240}
3241
Avi Kivity2d04a052011-04-20 15:32:49 +03003242static int em_clts(struct x86_emulate_ctxt *ctxt)
3243{
3244 ulong cr0;
3245
3246 cr0 = ctxt->ops->get_cr(ctxt, 0);
3247 cr0 &= ~X86_CR0_TS;
3248 ctxt->ops->set_cr(ctxt, 0, cr0);
3249 return X86EMUL_CONTINUE;
3250}
3251
Avi Kivity26d05cc2011-04-21 12:07:59 +03003252static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3253{
Nadav Amit0f54a322014-08-29 11:26:55 +03003254 int rc = ctxt->ops->fix_hypercall(ctxt);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003255
Avi Kivity26d05cc2011-04-21 12:07:59 +03003256 if (rc != X86EMUL_CONTINUE)
3257 return rc;
3258
3259 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003260 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003261 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003262 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003263 return X86EMUL_CONTINUE;
3264}
3265
Avi Kivity96051572012-06-10 17:21:18 +03003266static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3267 void (*get)(struct x86_emulate_ctxt *ctxt,
3268 struct desc_ptr *ptr))
3269{
3270 struct desc_ptr desc_ptr;
3271
3272 if (ctxt->mode == X86EMUL_MODE_PROT64)
3273 ctxt->op_bytes = 8;
3274 get(ctxt, &desc_ptr);
3275 if (ctxt->op_bytes == 2) {
3276 ctxt->op_bytes = 4;
3277 desc_ptr.address &= 0x00ffffff;
3278 }
3279 /* Disable writeback. */
3280 ctxt->dst.type = OP_NONE;
3281 return segmented_write(ctxt, ctxt->dst.addr.mem,
3282 &desc_ptr, 2 + ctxt->op_bytes);
3283}
3284
3285static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3286{
3287 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3288}
3289
3290static int em_sidt(struct x86_emulate_ctxt *ctxt)
3291{
3292 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3293}
3294
Avi Kivity26d05cc2011-04-21 12:07:59 +03003295static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3296{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003297 struct desc_ptr desc_ptr;
3298 int rc;
3299
Avi Kivity510425f2012-06-07 17:04:36 +03003300 if (ctxt->mode == X86EMUL_MODE_PROT64)
3301 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003302 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003303 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003304 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003305 if (rc != X86EMUL_CONTINUE)
3306 return rc;
3307 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3308 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003309 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003310 return X86EMUL_CONTINUE;
3311}
3312
Avi Kivity5ef39c72011-04-21 12:21:50 +03003313static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003314{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003315 int rc;
3316
Avi Kivity5ef39c72011-04-21 12:21:50 +03003317 rc = ctxt->ops->fix_hypercall(ctxt);
3318
Avi Kivity26d05cc2011-04-21 12:07:59 +03003319 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003320 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003321 return rc;
3322}
3323
3324static int em_lidt(struct x86_emulate_ctxt *ctxt)
3325{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003326 struct desc_ptr desc_ptr;
3327 int rc;
3328
Avi Kivity510425f2012-06-07 17:04:36 +03003329 if (ctxt->mode == X86EMUL_MODE_PROT64)
3330 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003331 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003332 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003333 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003334 if (rc != X86EMUL_CONTINUE)
3335 return rc;
3336 ctxt->ops->set_idt(ctxt, &desc_ptr);
3337 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003338 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003339 return X86EMUL_CONTINUE;
3340}
3341
3342static int em_smsw(struct x86_emulate_ctxt *ctxt)
3343{
Nadav Amit32e94d02014-06-02 18:34:11 +03003344 if (ctxt->dst.type == OP_MEM)
3345 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003346 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003347 return X86EMUL_CONTINUE;
3348}
3349
3350static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3351{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003352 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003353 | (ctxt->src.val & 0x0f));
3354 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003355 return X86EMUL_CONTINUE;
3356}
3357
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003358static int em_loop(struct x86_emulate_ctxt *ctxt)
3359{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003360 int rc = X86EMUL_CONTINUE;
3361
Avi Kivitydd856ef2012-08-27 23:46:17 +03003362 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3363 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003364 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
Nadav Amit234f3ce2014-09-18 22:39:38 +03003365 rc = jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003366
Nadav Amit234f3ce2014-09-18 22:39:38 +03003367 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003368}
3369
3370static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3371{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003372 int rc = X86EMUL_CONTINUE;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003373
Nadav Amit234f3ce2014-09-18 22:39:38 +03003374 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
3375 rc = jmp_rel(ctxt, ctxt->src.val);
3376
3377 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003378}
3379
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003380static int em_in(struct x86_emulate_ctxt *ctxt)
3381{
3382 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3383 &ctxt->dst.val))
3384 return X86EMUL_IO_NEEDED;
3385
3386 return X86EMUL_CONTINUE;
3387}
3388
3389static int em_out(struct x86_emulate_ctxt *ctxt)
3390{
3391 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3392 &ctxt->src.val, 1);
3393 /* Disable writeback. */
3394 ctxt->dst.type = OP_NONE;
3395 return X86EMUL_CONTINUE;
3396}
3397
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003398static int em_cli(struct x86_emulate_ctxt *ctxt)
3399{
3400 if (emulator_bad_iopl(ctxt))
3401 return emulate_gp(ctxt, 0);
3402
3403 ctxt->eflags &= ~X86_EFLAGS_IF;
3404 return X86EMUL_CONTINUE;
3405}
3406
3407static int em_sti(struct x86_emulate_ctxt *ctxt)
3408{
3409 if (emulator_bad_iopl(ctxt))
3410 return emulate_gp(ctxt, 0);
3411
3412 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3413 ctxt->eflags |= X86_EFLAGS_IF;
3414 return X86EMUL_CONTINUE;
3415}
3416
Avi Kivity6d6eede2012-06-07 14:11:36 +03003417static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3418{
3419 u32 eax, ebx, ecx, edx;
3420
Avi Kivitydd856ef2012-08-27 23:46:17 +03003421 eax = reg_read(ctxt, VCPU_REGS_RAX);
3422 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003423 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003424 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3425 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3426 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3427 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003428 return X86EMUL_CONTINUE;
3429}
3430
Paolo Bonzini98f73632013-10-31 11:19:42 +01003431static int em_sahf(struct x86_emulate_ctxt *ctxt)
3432{
3433 u32 flags;
3434
3435 flags = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF;
3436 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3437
3438 ctxt->eflags &= ~0xffUL;
3439 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3440 return X86EMUL_CONTINUE;
3441}
3442
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003443static int em_lahf(struct x86_emulate_ctxt *ctxt)
3444{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003445 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3446 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003447 return X86EMUL_CONTINUE;
3448}
3449
Avi Kivity92998362012-06-13 12:25:06 +03003450static int em_bswap(struct x86_emulate_ctxt *ctxt)
3451{
3452 switch (ctxt->op_bytes) {
3453#ifdef CONFIG_X86_64
3454 case 8:
3455 asm("bswap %0" : "+r"(ctxt->dst.val));
3456 break;
3457#endif
3458 default:
3459 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3460 break;
3461 }
3462 return X86EMUL_CONTINUE;
3463}
3464
Nadav Amit13e457e2014-10-13 13:04:13 +03003465static int em_clflush(struct x86_emulate_ctxt *ctxt)
3466{
3467 /* emulating clflush regardless of cpuid */
3468 return X86EMUL_CONTINUE;
3469}
3470
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003471static bool valid_cr(int nr)
3472{
3473 switch (nr) {
3474 case 0:
3475 case 2 ... 4:
3476 case 8:
3477 return true;
3478 default:
3479 return false;
3480 }
3481}
3482
3483static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3484{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003485 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003486 return emulate_ud(ctxt);
3487
3488 return X86EMUL_CONTINUE;
3489}
3490
3491static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3492{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003493 u64 new_val = ctxt->src.val64;
3494 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003495 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003496
3497 static u64 cr_reserved_bits[] = {
3498 0xffffffff00000000ULL,
3499 0, 0, 0, /* CR3 checked later */
3500 CR4_RESERVED_BITS,
3501 0, 0, 0,
3502 CR8_RESERVED_BITS,
3503 };
3504
3505 if (!valid_cr(cr))
3506 return emulate_ud(ctxt);
3507
3508 if (new_val & cr_reserved_bits[cr])
3509 return emulate_gp(ctxt, 0);
3510
3511 switch (cr) {
3512 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003513 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003514 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3515 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3516 return emulate_gp(ctxt, 0);
3517
Avi Kivity717746e2011-04-20 13:37:53 +03003518 cr4 = ctxt->ops->get_cr(ctxt, 4);
3519 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003520
3521 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3522 !(cr4 & X86_CR4_PAE))
3523 return emulate_gp(ctxt, 0);
3524
3525 break;
3526 }
3527 case 3: {
3528 u64 rsvd = 0;
3529
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003530 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3531 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003532 rsvd = CR3_L_MODE_RESERVED_BITS;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003533
3534 if (new_val & rsvd)
3535 return emulate_gp(ctxt, 0);
3536
3537 break;
3538 }
3539 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003540 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003541
3542 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3543 return emulate_gp(ctxt, 0);
3544
3545 break;
3546 }
3547 }
3548
3549 return X86EMUL_CONTINUE;
3550}
3551
Joerg Roedel3b88e412011-04-04 12:39:29 +02003552static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3553{
3554 unsigned long dr7;
3555
Avi Kivity717746e2011-04-20 13:37:53 +03003556 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003557
3558 /* Check if DR7.Global_Enable is set */
3559 return dr7 & (1 << 13);
3560}
3561
3562static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3563{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003564 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003565 u64 cr4;
3566
3567 if (dr > 7)
3568 return emulate_ud(ctxt);
3569
Avi Kivity717746e2011-04-20 13:37:53 +03003570 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003571 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3572 return emulate_ud(ctxt);
3573
3574 if (check_dr7_gd(ctxt))
3575 return emulate_db(ctxt);
3576
3577 return X86EMUL_CONTINUE;
3578}
3579
3580static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3581{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003582 u64 new_val = ctxt->src.val64;
3583 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003584
3585 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3586 return emulate_gp(ctxt, 0);
3587
3588 return check_dr_read(ctxt);
3589}
3590
Joerg Roedel01de8b02011-04-04 12:39:31 +02003591static int check_svme(struct x86_emulate_ctxt *ctxt)
3592{
3593 u64 efer;
3594
Avi Kivity717746e2011-04-20 13:37:53 +03003595 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003596
3597 if (!(efer & EFER_SVME))
3598 return emulate_ud(ctxt);
3599
3600 return X86EMUL_CONTINUE;
3601}
3602
3603static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3604{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003605 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003606
3607 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003608 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003609 return emulate_gp(ctxt, 0);
3610
3611 return check_svme(ctxt);
3612}
3613
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003614static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3615{
Avi Kivity717746e2011-04-20 13:37:53 +03003616 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003617
Avi Kivity717746e2011-04-20 13:37:53 +03003618 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003619 return emulate_ud(ctxt);
3620
3621 return X86EMUL_CONTINUE;
3622}
3623
Joerg Roedel80612522011-04-04 12:39:33 +02003624static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3625{
Avi Kivity717746e2011-04-20 13:37:53 +03003626 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003627 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003628
Avi Kivity717746e2011-04-20 13:37:53 +03003629 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03003630 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02003631 return emulate_gp(ctxt, 0);
3632
3633 return X86EMUL_CONTINUE;
3634}
3635
Joerg Roedelf6511932011-04-04 12:39:35 +02003636static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3637{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003638 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3639 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003640 return emulate_gp(ctxt, 0);
3641
3642 return X86EMUL_CONTINUE;
3643}
3644
3645static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3646{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003647 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3648 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003649 return emulate_gp(ctxt, 0);
3650
3651 return X86EMUL_CONTINUE;
3652}
3653
Avi Kivity73fba5f2010-07-29 15:11:53 +03003654#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003655#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
3656#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
3657 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003658#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003659#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003660#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3661#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003662#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003663#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003664#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003665#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003666 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003667#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003668 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
3669 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003670#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003671
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003672#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003673#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003674#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003675#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003676#define I2bvIP(_f, _e, _i, _p) \
3677 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003678
Avi Kivityfb864fb2013-01-04 16:18:54 +02003679#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3680 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3681 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003682
Nadav Amit0f54a322014-08-29 11:26:55 +03003683static const struct opcode group7_rm0[] = {
3684 N,
3685 I(SrcNone | Priv | EmulateOnUD, em_vmcall),
3686 N, N, N, N, N, N,
3687};
3688
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003689static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003690 DI(SrcNone | Priv, monitor),
3691 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003692 N, N, N, N, N, N,
3693};
3694
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003695static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003696 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003697 II(SrcNone | Prot | EmulateOnUD, em_vmmcall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003698 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3699 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3700 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3701 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3702 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3703 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003704};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003705
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003706static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003707 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003708 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003709 N, N, N, N, N, N,
3710};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003711
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003712static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003713 F(Lock, em_add),
3714 F(Lock | PageTable, em_or),
3715 F(Lock, em_adc),
3716 F(Lock, em_sbb),
3717 F(Lock | PageTable, em_and),
3718 F(Lock, em_sub),
3719 F(Lock, em_xor),
3720 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003721};
3722
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003723static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003724 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003725};
3726
Avi Kivity007a3b52013-01-19 19:51:51 +02003727static const struct opcode group2[] = {
3728 F(DstMem | ModRM, em_rol),
3729 F(DstMem | ModRM, em_ror),
3730 F(DstMem | ModRM, em_rcl),
3731 F(DstMem | ModRM, em_rcr),
3732 F(DstMem | ModRM, em_shl),
3733 F(DstMem | ModRM, em_shr),
3734 F(DstMem | ModRM, em_shl),
3735 F(DstMem | ModRM, em_sar),
3736};
3737
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003738static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003739 F(DstMem | SrcImm | NoWrite, em_test),
3740 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003741 F(DstMem | SrcNone | Lock, em_not),
3742 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02003743 F(DstXacc | Src2Mem, em_mul_ex),
3744 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02003745 F(DstXacc | Src2Mem, em_div_ex),
3746 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003747};
3748
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003749static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003750 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3751 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003752 N, N, N, N, N, N,
3753};
3754
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003755static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003756 F(DstMem | SrcNone | Lock, em_inc),
3757 F(DstMem | SrcNone | Lock, em_dec),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003758 I(SrcMem | Stack, em_grp45),
3759 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3760 I(SrcMem | Stack, em_grp45),
3761 I(SrcMemFAddr | ImplicitOps, em_grp45),
Gleb Natapov188424b2013-04-11 12:32:14 +03003762 I(SrcMem | Stack, em_grp45), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003763};
3764
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003765static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003766 DI(Prot, sldt),
3767 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003768 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003769 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003770 N, N, N, N,
3771};
3772
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003773static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03003774 II(Mov | DstMem, em_sgdt, sgdt),
3775 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003776 II(SrcMem | Priv, em_lgdt, lgdt),
3777 II(SrcMem | Priv, em_lidt, lidt),
3778 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3779 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3780 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003781}, {
Nadav Amit0f54a322014-08-29 11:26:55 +03003782 EXT(0, group7_rm0),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003783 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003784 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003785 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3786 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3787 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003788} };
3789
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003790static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003791 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003792 F(DstMem | SrcImmByte | NoWrite, em_bt),
3793 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3794 F(DstMem | SrcImmByte | Lock, em_btr),
3795 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003796};
3797
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003798static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003799 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003800}, {
3801 N, N, N, N, N, N, N, N,
3802} };
3803
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003804static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003805 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003806 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003807};
3808
Nadav Amit13e457e2014-10-13 13:04:13 +03003809static const struct gprefix pfx_0f_ae_7 = {
Nadav Amit3f6f1482014-10-13 13:04:14 +03003810 I(SrcMem | ByteOp, em_clflush), N, N, N,
Nadav Amit13e457e2014-10-13 13:04:13 +03003811};
3812
3813static const struct group_dual group15 = { {
3814 N, N, N, N, N, N, N, GP(0, &pfx_0f_ae_7),
3815}, {
3816 N, N, N, N, N, N, N, N,
3817} };
3818
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003819static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003820 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003821};
3822
Paolo Bonzinid5b77062014-07-14 12:54:48 +02003823static const struct gprefix pfx_0f_2b = {
3824 I(0, em_mov), I(0, em_mov), N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003825};
3826
Igor Mammedov27ce8252014-03-15 21:01:59 +01003827static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01003828 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003829};
3830
Alex Williamson0a370272014-07-11 11:56:31 -06003831static const struct gprefix pfx_0f_e7 = {
3832 N, I(Sse, em_mov), N, N,
3833};
3834
Gleb Natapov045a2822012-12-20 16:57:43 +02003835static const struct escape escape_d9 = { {
3836 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3837}, {
3838 /* 0xC0 - 0xC7 */
3839 N, N, N, N, N, N, N, N,
3840 /* 0xC8 - 0xCF */
3841 N, N, N, N, N, N, N, N,
3842 /* 0xD0 - 0xC7 */
3843 N, N, N, N, N, N, N, N,
3844 /* 0xD8 - 0xDF */
3845 N, N, N, N, N, N, N, N,
3846 /* 0xE0 - 0xE7 */
3847 N, N, N, N, N, N, N, N,
3848 /* 0xE8 - 0xEF */
3849 N, N, N, N, N, N, N, N,
3850 /* 0xF0 - 0xF7 */
3851 N, N, N, N, N, N, N, N,
3852 /* 0xF8 - 0xFF */
3853 N, N, N, N, N, N, N, N,
3854} };
3855
3856static const struct escape escape_db = { {
3857 N, N, N, N, N, N, N, N,
3858}, {
3859 /* 0xC0 - 0xC7 */
3860 N, N, N, N, N, N, N, N,
3861 /* 0xC8 - 0xCF */
3862 N, N, N, N, N, N, N, N,
3863 /* 0xD0 - 0xC7 */
3864 N, N, N, N, N, N, N, N,
3865 /* 0xD8 - 0xDF */
3866 N, N, N, N, N, N, N, N,
3867 /* 0xE0 - 0xE7 */
3868 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3869 /* 0xE8 - 0xEF */
3870 N, N, N, N, N, N, N, N,
3871 /* 0xF0 - 0xF7 */
3872 N, N, N, N, N, N, N, N,
3873 /* 0xF8 - 0xFF */
3874 N, N, N, N, N, N, N, N,
3875} };
3876
3877static const struct escape escape_dd = { {
3878 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3879}, {
3880 /* 0xC0 - 0xC7 */
3881 N, N, N, N, N, N, N, N,
3882 /* 0xC8 - 0xCF */
3883 N, N, N, N, N, N, N, N,
3884 /* 0xD0 - 0xC7 */
3885 N, N, N, N, N, N, N, N,
3886 /* 0xD8 - 0xDF */
3887 N, N, N, N, N, N, N, N,
3888 /* 0xE0 - 0xE7 */
3889 N, N, N, N, N, N, N, N,
3890 /* 0xE8 - 0xEF */
3891 N, N, N, N, N, N, N, N,
3892 /* 0xF0 - 0xF7 */
3893 N, N, N, N, N, N, N, N,
3894 /* 0xF8 - 0xFF */
3895 N, N, N, N, N, N, N, N,
3896} };
3897
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003898static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003899 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003900 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003901 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3902 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003903 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003904 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003905 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3906 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003907 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003908 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003909 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3910 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003911 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003912 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003913 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3914 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003915 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003916 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003917 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003918 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003919 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003920 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003921 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003922 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003923 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02003924 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003925 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003926 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003927 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003928 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003929 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003930 I(ImplicitOps | Stack | No64, em_pusha),
3931 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003932 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3933 N, N, N, N,
3934 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003935 I(SrcImm | Mov | Stack, em_push),
3936 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003937 I(SrcImmByte | Mov | Stack, em_push),
3938 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003939 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003940 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003941 /* 0x70 - 0x7F */
3942 X16(D(SrcImmByte)),
3943 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003944 G(ByteOp | DstMem | SrcImm, group1),
3945 G(DstMem | SrcImm, group1),
3946 G(ByteOp | DstMem | SrcImm | No64, group1),
3947 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003948 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003949 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003950 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003951 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003952 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003953 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003954 D(ModRM | SrcMem | NoAccess | DstReg),
3955 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3956 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003957 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003958 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003959 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003960 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003961 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003962 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01003963 II(ImplicitOps | Stack, em_popf, popf),
3964 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003965 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003966 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003967 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003968 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003969 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003970 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003971 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003972 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3973 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003974 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003975 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003976 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003977 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02003978 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003979 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003980 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003981 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003982 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003983 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3984 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003985 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003986 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003987 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Bruce Rogers32611072013-09-09 09:40:20 -06003988 I(ImplicitOps | Stack | SrcImmU16, em_ret_far_imm),
3989 I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003990 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003991 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003992 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003993 G(Src2One | ByteOp, group2), G(Src2One, group2),
3994 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003995 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02003996 I(DstAcc | SrcImmUByte | No64, em_aad),
3997 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02003998 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003999 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02004000 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004001 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09004002 X3(I(SrcImmByte, em_loop)),
4003 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004004 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
4005 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004006 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09004007 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004008 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004009 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
4010 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004011 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004012 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004013 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4014 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004015 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09004016 D(ImplicitOps), D(ImplicitOps),
4017 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004018 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4019};
4020
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004021static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004022 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004023 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02004024 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004025 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004026 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004027 N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004028 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004029 N, N, N, N, N, N, N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004030 D(ImplicitOps | ModRM | SrcMem | NoAccess),
4031 N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004032 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03004033 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
4034 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
4035 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
4036 check_cr_write),
4037 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
4038 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004039 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004040 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
4041 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004042 N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b),
Avi Kivity3e114eb2012-04-09 18:40:01 +03004043 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004044 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004045 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004046 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004047 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004048 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02004049 I(ImplicitOps | EmulateOnUD, em_sysenter),
4050 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004051 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004052 N, N, N, N, N, N, N, N,
4053 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03004054 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004055 /* 0x50 - 0x5F */
4056 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4057 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004058 N, N, N, N,
4059 N, N, N, N,
4060 N, N, N, N,
4061 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004062 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004063 N, N, N, N,
4064 N, N, N, N,
4065 N, N, N, N,
4066 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004067 /* 0x80 - 0x8F */
4068 X16(D(SrcImm)),
4069 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004070 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004071 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004072 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004073 II(ImplicitOps, em_cpuid, cpuid),
4074 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004075 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4076 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004077 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004078 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004079 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004080 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004081 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4082 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Nadav Amit13e457e2014-10-13 13:04:13 +03004083 GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004084 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09004085 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004086 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004087 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004088 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4089 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004090 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004091 /* 0xB8 - 0xBF */
4092 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004093 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004094 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
4095 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004096 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004097 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02004098 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Wei Yongjun92f738a2010-08-17 09:19:34 +08004099 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004100 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004101 /* 0xC8 - 0xCF */
4102 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004103 /* 0xD0 - 0xDF */
4104 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4105 /* 0xE0 - 0xEF */
Alex Williamson0a370272014-07-11 11:56:31 -06004106 N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
4107 N, N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004108 /* 0xF0 - 0xFF */
4109 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4110};
4111
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004112static const struct gprefix three_byte_0f_38_f0 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01004113 I(DstReg | SrcMem | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004114};
4115
4116static const struct gprefix three_byte_0f_38_f1 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01004117 I(DstMem | SrcReg | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004118};
4119
4120/*
4121 * Insns below are selected by the prefix which indexed by the third opcode
4122 * byte.
4123 */
4124static const struct opcode opcode_map_0f_38[256] = {
4125 /* 0x00 - 0x7f */
4126 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004127 /* 0x80 - 0xef */
4128 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4129 /* 0xf0 - 0xf1 */
4130 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f0),
4131 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f1),
4132 /* 0xf2 - 0xff */
4133 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004134};
4135
Avi Kivity73fba5f2010-07-29 15:11:53 +03004136#undef D
4137#undef N
4138#undef G
4139#undef GD
4140#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004141#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004142#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004143
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004144#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004145#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004146#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004147#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004148#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004149
Avi Kivity9dac77f2011-06-01 15:34:25 +03004150static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004151{
4152 unsigned size;
4153
Avi Kivity9dac77f2011-06-01 15:34:25 +03004154 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004155 if (size == 8)
4156 size = 4;
4157 return size;
4158}
4159
4160static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4161 unsigned size, bool sign_extension)
4162{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004163 int rc = X86EMUL_CONTINUE;
4164
4165 op->type = OP_IMM;
4166 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004167 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004168 /* NB. Immediates are sign-extended as necessary. */
4169 switch (op->bytes) {
4170 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004171 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004172 break;
4173 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004174 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004175 break;
4176 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004177 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004178 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004179 case 8:
4180 op->val = insn_fetch(s64, ctxt);
4181 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004182 }
4183 if (!sign_extension) {
4184 switch (op->bytes) {
4185 case 1:
4186 op->val &= 0xff;
4187 break;
4188 case 2:
4189 op->val &= 0xffff;
4190 break;
4191 case 4:
4192 op->val &= 0xffffffff;
4193 break;
4194 }
4195 }
4196done:
4197 return rc;
4198}
4199
Avi Kivitya9945542011-09-13 10:45:41 +03004200static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4201 unsigned d)
4202{
4203 int rc = X86EMUL_CONTINUE;
4204
4205 switch (d) {
4206 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004207 decode_register_operand(ctxt, op);
Avi Kivitya9945542011-09-13 10:45:41 +03004208 break;
4209 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004210 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03004211 break;
4212 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004213 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004214 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03004215 *op = ctxt->memop;
4216 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004217 if (ctxt->d & BitOp)
Avi Kivitya9945542011-09-13 10:45:41 +03004218 fetch_bit_operand(ctxt);
4219 op->orig_val = op->val;
4220 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004221 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004222 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004223 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03004224 case OpAcc:
4225 op->type = OP_REG;
4226 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004227 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya9945542011-09-13 10:45:41 +03004228 fetch_register_operand(op);
4229 op->orig_val = op->val;
4230 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004231 case OpAccLo:
4232 op->type = OP_REG;
4233 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4234 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4235 fetch_register_operand(op);
4236 op->orig_val = op->val;
4237 break;
4238 case OpAccHi:
4239 if (ctxt->d & ByteOp) {
4240 op->type = OP_NONE;
4241 break;
4242 }
4243 op->type = OP_REG;
4244 op->bytes = ctxt->op_bytes;
4245 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4246 fetch_register_operand(op);
4247 op->orig_val = op->val;
4248 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004249 case OpDI:
4250 op->type = OP_MEM;
4251 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4252 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004253 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya9945542011-09-13 10:45:41 +03004254 op->addr.mem.seg = VCPU_SREG_ES;
4255 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004256 op->count = 1;
Avi Kivitya9945542011-09-13 10:45:41 +03004257 break;
4258 case OpDX:
4259 op->type = OP_REG;
4260 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004261 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya9945542011-09-13 10:45:41 +03004262 fetch_register_operand(op);
4263 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004264 case OpCL:
4265 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004266 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004267 break;
4268 case OpImmByte:
4269 rc = decode_imm(ctxt, op, 1, true);
4270 break;
4271 case OpOne:
4272 op->bytes = 1;
4273 op->val = 1;
4274 break;
4275 case OpImm:
4276 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4277 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004278 case OpImm64:
4279 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4280 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004281 case OpMem8:
4282 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004283 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004284 ctxt->memop.addr.reg = decode_register(ctxt,
4285 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004286 fetch_register_operand(&ctxt->memop);
4287 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004288 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004289 case OpMem16:
4290 ctxt->memop.bytes = 2;
4291 goto mem_common;
4292 case OpMem32:
4293 ctxt->memop.bytes = 4;
4294 goto mem_common;
4295 case OpImmU16:
4296 rc = decode_imm(ctxt, op, 2, false);
4297 break;
4298 case OpImmU:
4299 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4300 break;
4301 case OpSI:
4302 op->type = OP_MEM;
4303 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4304 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004305 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Bandan Das573e80f2014-04-16 12:46:13 -04004306 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03004307 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004308 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004309 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004310 case OpXLat:
4311 op->type = OP_MEM;
4312 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4313 op->addr.mem.ea =
4314 register_address(ctxt,
4315 reg_read(ctxt, VCPU_REGS_RBX) +
4316 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04004317 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004318 op->val = 0;
4319 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004320 case OpImmFAddr:
4321 op->type = OP_IMM;
4322 op->addr.mem.ea = ctxt->_eip;
4323 op->bytes = ctxt->op_bytes + 2;
4324 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4325 break;
4326 case OpMemFAddr:
4327 ctxt->memop.bytes = ctxt->op_bytes + 2;
4328 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004329 case OpES:
4330 op->val = VCPU_SREG_ES;
4331 break;
4332 case OpCS:
4333 op->val = VCPU_SREG_CS;
4334 break;
4335 case OpSS:
4336 op->val = VCPU_SREG_SS;
4337 break;
4338 case OpDS:
4339 op->val = VCPU_SREG_DS;
4340 break;
4341 case OpFS:
4342 op->val = VCPU_SREG_FS;
4343 break;
4344 case OpGS:
4345 op->val = VCPU_SREG_GS;
4346 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004347 case OpImplicit:
4348 /* Special instructions do their own operand decoding. */
4349 default:
4350 op->type = OP_NONE; /* Disable writeback. */
4351 break;
4352 }
4353
4354done:
4355 return rc;
4356}
4357
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004358int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004359{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004360 int rc = X86EMUL_CONTINUE;
4361 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004362 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004363 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04004364 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03004365 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004366
Avi Kivityf09ed832011-09-13 10:45:40 +03004367 ctxt->memop.type = OP_NONE;
4368 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004369 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02004370 ctxt->fetch.ptr = ctxt->fetch.data;
4371 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004372 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01004373 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004374 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004375 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02004376 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004377 if (rc != X86EMUL_CONTINUE)
4378 return rc;
4379 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004380
4381 switch (mode) {
4382 case X86EMUL_MODE_REAL:
4383 case X86EMUL_MODE_VM86:
4384 case X86EMUL_MODE_PROT16:
4385 def_op_bytes = def_ad_bytes = 2;
4386 break;
4387 case X86EMUL_MODE_PROT32:
4388 def_op_bytes = def_ad_bytes = 4;
4389 break;
4390#ifdef CONFIG_X86_64
4391 case X86EMUL_MODE_PROT64:
4392 def_op_bytes = 4;
4393 def_ad_bytes = 8;
4394 break;
4395#endif
4396 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004397 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004398 }
4399
Avi Kivity9dac77f2011-06-01 15:34:25 +03004400 ctxt->op_bytes = def_op_bytes;
4401 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004402
4403 /* Legacy prefixes. */
4404 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004405 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004406 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004407 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004408 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004409 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004410 break;
4411 case 0x67: /* address-size override */
4412 if (mode == X86EMUL_MODE_PROT64)
4413 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004414 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004415 else
4416 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004417 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004418 break;
4419 case 0x26: /* ES override */
4420 case 0x2e: /* CS override */
4421 case 0x36: /* SS override */
4422 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004423 has_seg_override = true;
4424 ctxt->seg_override = (ctxt->b >> 3) & 3;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004425 break;
4426 case 0x64: /* FS override */
4427 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004428 has_seg_override = true;
4429 ctxt->seg_override = ctxt->b & 7;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004430 break;
4431 case 0x40 ... 0x4f: /* REX */
4432 if (mode != X86EMUL_MODE_PROT64)
4433 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004434 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004435 continue;
4436 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004437 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004438 break;
4439 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004440 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004441 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004442 break;
4443 default:
4444 goto done_prefixes;
4445 }
4446
4447 /* Any legacy prefix after a REX prefix nullifies its effect. */
4448
Avi Kivity9dac77f2011-06-01 15:34:25 +03004449 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004450 }
4451
4452done_prefixes:
4453
4454 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004455 if (ctxt->rex_prefix & 8)
4456 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004457
4458 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004459 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004460 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004461 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004462 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004463 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004464 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004465
4466 /* 0F_38 opcode map */
4467 if (ctxt->b == 0x38) {
4468 ctxt->opcode_len = 3;
4469 ctxt->b = insn_fetch(u8, ctxt);
4470 opcode = opcode_map_0f_38[ctxt->b];
4471 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004472 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004473 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004474
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004475 if (ctxt->d & ModRM)
4476 ctxt->modrm = insn_fetch(u8, ctxt);
4477
Nadav Amit7fe864d2014-06-02 18:34:03 +03004478 /* vex-prefix instructions are not implemented */
4479 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
4480 (mode == X86EMUL_MODE_PROT64 ||
4481 (mode >= X86EMUL_MODE_PROT16 && (ctxt->modrm & 0x80)))) {
4482 ctxt->d = NotImpl;
4483 }
4484
Avi Kivity9dac77f2011-06-01 15:34:25 +03004485 while (ctxt->d & GroupMask) {
4486 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004487 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004488 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004489 opcode = opcode.u.group[goffset];
4490 break;
4491 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004492 goffset = (ctxt->modrm >> 3) & 7;
4493 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004494 opcode = opcode.u.gdual->mod3[goffset];
4495 else
4496 opcode = opcode.u.gdual->mod012[goffset];
4497 break;
4498 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004499 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004500 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004501 break;
4502 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004503 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004504 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004505 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004506 switch (simd_prefix) {
4507 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4508 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4509 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4510 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4511 }
4512 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004513 case Escape:
4514 if (ctxt->modrm > 0xbf)
4515 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4516 else
4517 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4518 break;
Avi Kivity46561642011-04-24 14:09:59 +03004519 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004520 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004521 }
Avi Kivity46561642011-04-24 14:09:59 +03004522
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004523 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004524 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004525 }
4526
Paolo Bonzinie24186e2014-03-27 12:00:57 +01004527 /* Unrecognised? */
4528 if (ctxt->d == 0)
4529 return EMULATION_FAILED;
4530
Avi Kivity9dac77f2011-06-01 15:34:25 +03004531 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004532
Nadav Amit3a6095a2014-08-13 16:50:13 +03004533 if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD)))
4534 return EMULATION_FAILED;
4535
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004536 if (unlikely(ctxt->d &
Nadav Amit3a6095a2014-08-13 16:50:13 +03004537 (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm))) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004538 /*
4539 * These are copied unconditionally here, and checked unconditionally
4540 * in x86_emulate_insn.
4541 */
4542 ctxt->check_perm = opcode.check_perm;
4543 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004544
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004545 if (ctxt->d & NotImpl)
4546 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004547
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004548 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004549 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004550
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004551 if (ctxt->d & Op3264) {
4552 if (mode == X86EMUL_MODE_PROT64)
4553 ctxt->op_bytes = 8;
4554 else
4555 ctxt->op_bytes = 4;
4556 }
4557
4558 if (ctxt->d & Sse)
4559 ctxt->op_bytes = 16;
4560 else if (ctxt->d & Mmx)
4561 ctxt->op_bytes = 8;
4562 }
Avi Kivity12537912011-03-29 11:41:27 +02004563
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004564 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004565 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004566 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04004567 if (!has_seg_override) {
4568 has_seg_override = true;
4569 ctxt->seg_override = ctxt->modrm_seg;
4570 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004571 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004572 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004573 if (rc != X86EMUL_CONTINUE)
4574 goto done;
4575
Bandan Das573e80f2014-04-16 12:46:13 -04004576 if (!has_seg_override)
4577 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004578
Bandan Das573e80f2014-04-16 12:46:13 -04004579 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004580
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004581 /*
4582 * Decode and fetch the source operand: register, memory
4583 * or immediate.
4584 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004585 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004586 if (rc != X86EMUL_CONTINUE)
4587 goto done;
4588
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004589 /*
4590 * Decode and fetch the second source operand: register, memory
4591 * or immediate.
4592 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004593 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004594 if (rc != X86EMUL_CONTINUE)
4595 goto done;
4596
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004597 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03004598 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004599
Bandan Das41061cd2014-04-16 12:46:14 -04004600 if (ctxt->rip_relative)
Avi Kivityf09ed832011-09-13 10:45:40 +03004601 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004602
Paolo Bonzinia430c912014-10-23 14:54:14 +02004603done:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004604 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004605}
4606
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004607bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4608{
4609 return ctxt->d & PageTable;
4610}
4611
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004612static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4613{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004614 /* The second termination condition only applies for REPE
4615 * and REPNE. Test if the repeat string operation prefix is
4616 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4617 * corresponding termination condition according to:
4618 * - if REPE/REPZ and ZF = 0 then done
4619 * - if REPNE/REPNZ and ZF = 1 then done
4620 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004621 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4622 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4623 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004624 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004625 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004626 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4627 return true;
4628
4629 return false;
4630}
4631
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004632static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4633{
4634 bool fault = false;
4635
4636 ctxt->ops->get_fpu(ctxt);
4637 asm volatile("1: fwait \n\t"
4638 "2: \n\t"
4639 ".pushsection .fixup,\"ax\" \n\t"
4640 "3: \n\t"
4641 "movb $1, %[fault] \n\t"
4642 "jmp 2b \n\t"
4643 ".popsection \n\t"
4644 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004645 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004646 ctxt->ops->put_fpu(ctxt);
4647
4648 if (unlikely(fault))
4649 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4650
4651 return X86EMUL_CONTINUE;
4652}
4653
4654static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4655 struct operand *op)
4656{
4657 if (op->type == OP_MM)
4658 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4659}
4660
Avi Kivitye28bbd42013-01-04 16:18:48 +02004661static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4662{
4663 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivityb9fa4092013-02-09 11:31:48 +02004664 if (!(ctxt->d & ByteOp))
4665 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Avi Kivitye28bbd42013-01-04 16:18:48 +02004666 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004667 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
4668 [fastop]"+S"(fop)
4669 : "c"(ctxt->src2.val));
Avi Kivitye28bbd42013-01-04 16:18:48 +02004670 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004671 if (!fop) /* exception is returned in fop variable */
4672 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02004673 return X86EMUL_CONTINUE;
4674}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004675
Bandan Das14985072014-04-16 12:46:09 -04004676void init_decode_cache(struct x86_emulate_ctxt *ctxt)
4677{
Bandan Das573e80f2014-04-16 12:46:13 -04004678 memset(&ctxt->rip_relative, 0,
4679 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04004680
Bandan Das14985072014-04-16 12:46:09 -04004681 ctxt->io_read.pos = 0;
4682 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04004683 ctxt->mem_read.end = 0;
4684}
4685
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004686int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004687{
Mathias Krause0225fb52012-08-30 01:30:16 +02004688 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004689 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004690 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004691
Avi Kivity9dac77f2011-06-01 15:34:25 +03004692 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004693
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004694 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004695 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004696 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004697 goto done;
4698 }
4699
Avi Kivity9dac77f2011-06-01 15:34:25 +03004700 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004701 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004702 goto done;
4703 }
4704
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004705 if (unlikely(ctxt->d &
4706 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
4707 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4708 (ctxt->d & Undefined)) {
4709 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004710 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004711 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004712
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004713 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4714 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
4715 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004716 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004717 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004718
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004719 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
4720 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004721 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004722 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004723
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004724 if (ctxt->d & Mmx) {
4725 rc = flush_pending_x87_faults(ctxt);
4726 if (rc != X86EMUL_CONTINUE)
4727 goto done;
4728 /*
4729 * Now that we know the fpu is exception safe, we can fetch
4730 * operands from it.
4731 */
4732 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4733 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4734 if (!(ctxt->d & Mov))
4735 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4736 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004737
Bandan Das685bbf42014-04-16 12:46:10 -04004738 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004739 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4740 X86_ICPT_PRE_EXCEPT);
4741 if (rc != X86EMUL_CONTINUE)
4742 goto done;
4743 }
4744
4745 /* Privileged instruction can be executed only in CPL=0 */
4746 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Nadav Amit68efa762014-06-18 17:19:35 +03004747 if (ctxt->d & PrivUD)
4748 rc = emulate_ud(ctxt);
4749 else
4750 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004751 goto done;
4752 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004753
4754 /* Instruction can only be executed in protected mode */
4755 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
4756 rc = emulate_ud(ctxt);
4757 goto done;
4758 }
4759
4760 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04004761 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004762 rc = ctxt->check_perm(ctxt);
4763 if (rc != X86EMUL_CONTINUE)
4764 goto done;
4765 }
4766
Bandan Das685bbf42014-04-16 12:46:10 -04004767 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004768 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4769 X86_ICPT_POST_EXCEPT);
4770 if (rc != X86EMUL_CONTINUE)
4771 goto done;
4772 }
4773
4774 if (ctxt->rep_prefix && (ctxt->d & String)) {
4775 /* All REP prefixes have the same first termination condition */
4776 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
4777 ctxt->eip = ctxt->_eip;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004778 ctxt->eflags &= ~EFLG_RF;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004779 goto done;
4780 }
4781 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004782 }
4783
Avi Kivity9dac77f2011-06-01 15:34:25 +03004784 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4785 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4786 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004787 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004788 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004789 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004790 }
4791
Avi Kivity9dac77f2011-06-01 15:34:25 +03004792 if (ctxt->src2.type == OP_MEM) {
4793 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4794 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004795 if (rc != X86EMUL_CONTINUE)
4796 goto done;
4797 }
4798
Avi Kivity9dac77f2011-06-01 15:34:25 +03004799 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004800 goto special_insn;
4801
4802
Avi Kivity9dac77f2011-06-01 15:34:25 +03004803 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004804 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004805 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4806 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004807 if (rc != X86EMUL_CONTINUE)
4808 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004809 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004810 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004811
Avi Kivity018a98d2007-11-27 19:30:56 +02004812special_insn:
4813
Bandan Das685bbf42014-04-16 12:46:10 -04004814 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004815 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004816 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004817 if (rc != X86EMUL_CONTINUE)
4818 goto done;
4819 }
4820
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004821 if (ctxt->rep_prefix && (ctxt->d & String))
4822 ctxt->eflags |= EFLG_RF;
4823 else
4824 ctxt->eflags &= ~EFLG_RF;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004825
Avi Kivity9dac77f2011-06-01 15:34:25 +03004826 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004827 if (ctxt->d & Fastop) {
4828 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4829 rc = fastop(ctxt, fop);
4830 if (rc != X86EMUL_CONTINUE)
4831 goto done;
4832 goto writeback;
4833 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004834 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004835 if (rc != X86EMUL_CONTINUE)
4836 goto done;
4837 goto writeback;
4838 }
4839
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004840 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004841 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004842 else if (ctxt->opcode_len == 3)
4843 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004844
Avi Kivity9dac77f2011-06-01 15:34:25 +03004845 switch (ctxt->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004846 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004847 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004848 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004849 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004850 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004851 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004852 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03004853 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004854 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004855 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004856 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004857 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004858 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004859 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03004860 ctxt->dst.type = OP_NONE;
4861 else
4862 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004863 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004864 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004865 switch (ctxt->op_bytes) {
4866 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4867 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4868 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004869 }
4870 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004871 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004872 rc = emulate_int(ctxt, 3);
4873 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004874 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004875 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004876 break;
4877 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004878 if (ctxt->eflags & EFLG_OF)
4879 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004880 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004881 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004882 case 0xeb: /* jmp rel short */
Nadav Amit234f3ce2014-09-18 22:39:38 +03004883 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004884 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004885 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004886 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004887 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004888 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004889 case 0xf5: /* cmc */
4890 /* complement carry flag from eflags reg */
4891 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004892 break;
4893 case 0xf8: /* clc */
4894 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004895 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004896 case 0xf9: /* stc */
4897 ctxt->eflags |= EFLG_CF;
4898 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004899 case 0xfc: /* cld */
4900 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004901 break;
4902 case 0xfd: /* std */
4903 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004904 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004905 default:
4906 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004907 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004908
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004909 if (rc != X86EMUL_CONTINUE)
4910 goto done;
4911
Avi Kivity018a98d2007-11-27 19:30:56 +02004912writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02004913 if (ctxt->d & SrcWrite) {
4914 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
4915 rc = writeback(ctxt, &ctxt->src);
4916 if (rc != X86EMUL_CONTINUE)
4917 goto done;
4918 }
Nadav Amitee212292014-06-15 16:12:58 +03004919 if (!(ctxt->d & NoWrite)) {
4920 rc = writeback(ctxt, &ctxt->dst);
4921 if (rc != X86EMUL_CONTINUE)
4922 goto done;
4923 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004924
Gleb Natapov5cd21912010-03-18 15:20:26 +02004925 /*
4926 * restore dst type in case the decoding will be reused
4927 * (happens for string instruction )
4928 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004929 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004930
Avi Kivity9dac77f2011-06-01 15:34:25 +03004931 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004932 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004933
Avi Kivity9dac77f2011-06-01 15:34:25 +03004934 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004935 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004936
Avi Kivity9dac77f2011-06-01 15:34:25 +03004937 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004938 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004939 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004940 if ((ctxt->d & SrcMask) == SrcSI)
4941 count = ctxt->src.count;
4942 else
4943 count = ctxt->dst.count;
4944 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4945 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004946
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004947 if (!string_insn_completed(ctxt)) {
4948 /*
4949 * Re-enter guest when pio read ahead buffer is empty
4950 * or, if it is not used, after each 1024 iteration.
4951 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004952 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004953 (r->end == 0 || r->end != r->pos)) {
4954 /*
4955 * Reset read cache. Usually happens before
4956 * decode, but since instruction is restarted
4957 * we have to do it here.
4958 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004959 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004960 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004961 return EMULATION_RESTART;
4962 }
4963 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004964 }
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004965 ctxt->eflags &= ~EFLG_RF;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004966 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004967
Avi Kivity9dac77f2011-06-01 15:34:25 +03004968 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004969
4970done:
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02004971 if (rc == X86EMUL_PROPAGATE_FAULT) {
4972 WARN_ON(ctxt->exception.vector > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +02004973 ctxt->have_exception = true;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02004974 }
Joerg Roedel775fde82011-04-04 12:39:24 +02004975 if (rc == X86EMUL_INTERCEPTED)
4976 return EMULATION_INTERCEPTED;
4977
Avi Kivitydd856ef2012-08-27 23:46:17 +03004978 if (rc == X86EMUL_CONTINUE)
4979 writeback_registers(ctxt);
4980
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004981 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004982
4983twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004984 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004985 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004986 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004987 break;
4988 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004989 case 0x0d: /* GrpP (prefetch) */
4990 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004991 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02004992 break;
4993 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004994 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004995 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004996 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004997 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004998 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004999 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03005000 if (test_cc(ctxt->b, ctxt->eflags))
5001 ctxt->dst.val = ctxt->src.val;
5002 else if (ctxt->mode != X86EMUL_MODE_PROT64 ||
5003 ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005004 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08005005 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03005006 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03005007 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005008 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005009 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08005010 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005011 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08005012 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005013 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005014 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005015 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03005016 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005017 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005018 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005019 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005020 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03005021 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005022 break;
Sheng Yanga012e652007-10-15 14:24:20 +08005023 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005024 ctxt->dst.bytes = ctxt->op_bytes;
Nadav Amit3b320042014-06-02 18:34:08 +03005025 ctxt->dst.val = (ctxt->op_bytes == 8) ? (u64) ctxt->src.val :
5026 (u32) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08005027 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005028 default:
5029 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005030 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005031
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005032threebyte_insn:
5033
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005034 if (rc != X86EMUL_CONTINUE)
5035 goto done;
5036
Avi Kivity6aa8b732006-12-10 02:21:36 -08005037 goto writeback;
5038
5039cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02005040 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005041}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005042
5043void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
5044{
5045 invalidate_registers(ctxt);
5046}
5047
5048void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
5049{
5050 writeback_registers(ctxt);
5051}