blob: c0deaff8d9f04d6bdc89f50ddb79343bf5ca56e1 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivityb7d491e2013-01-04 16:18:49 +020027#include <linux/stringify.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028
Avi Kivity3eeb3282010-01-21 15:31:48 +020029#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020030#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020031
Avi Kivity6aa8b732006-12-10 02:21:36 -080032/*
Avi Kivitya99455492011-09-13 10:45:41 +030033 * Operand types
34 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030035#define OpNone 0ull
36#define OpImplicit 1ull /* No generic decode */
37#define OpReg 2ull /* Register */
38#define OpMem 3ull /* Memory */
39#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
40#define OpDI 5ull /* ES:DI/EDI/RDI */
41#define OpMem64 6ull /* Memory, 64-bit */
42#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
43#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030044#define OpCL 9ull /* CL register (for shifts) */
45#define OpImmByte 10ull /* 8-bit sign extended immediate */
46#define OpOne 11ull /* Implied 1 */
Nadav Amit5e2c6882012-12-06 21:55:10 -020047#define OpImm 12ull /* Sign extended up to 32-bit immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030048#define OpMem16 13ull /* Memory operand (16-bit). */
49#define OpMem32 14ull /* Memory operand (32-bit). */
50#define OpImmU 15ull /* Immediate operand, zero extended */
51#define OpSI 16ull /* SI/ESI/RSI */
52#define OpImmFAddr 17ull /* Immediate far address */
53#define OpMemFAddr 18ull /* Far address in memory */
54#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030055#define OpES 20ull /* ES */
56#define OpCS 21ull /* CS */
57#define OpSS 22ull /* SS */
58#define OpDS 23ull /* DS */
59#define OpFS 24ull /* FS */
60#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020061#define OpMem8 26ull /* 8-bit zero extended memory operand */
Nadav Amit5e2c6882012-12-06 21:55:10 -020062#define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */
Paolo Bonzini7fa57952013-05-09 11:32:50 +020063#define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */
Avi Kivity820207c2013-02-09 11:31:45 +020064#define OpAccLo 29ull /* Low part of extended acc (AX/AX/EAX/RAX) */
65#define OpAccHi 30ull /* High part of extended acc (-/DX/EDX/RDX) */
Avi Kivitya99455492011-09-13 10:45:41 +030066
Avi Kivity0fe59122011-09-13 10:45:47 +030067#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030068#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya99455492011-09-13 10:45:41 +030069
70/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080071 * Opcode effective-address decode tables.
72 * Note that we only emulate instructions that have at least one memory
73 * operand (excluding implicit stack references). We assume that stack
74 * references and instruction fetches will never occur in special memory
75 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
76 * not be handled.
77 */
78
79/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030080#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080081/* Destination operand type. */
Avi Kivitya99455492011-09-13 10:45:41 +030082#define DstShift 1
83#define ImplicitOps (OpImplicit << DstShift)
84#define DstReg (OpReg << DstShift)
85#define DstMem (OpMem << DstShift)
86#define DstAcc (OpAcc << DstShift)
87#define DstDI (OpDI << DstShift)
88#define DstMem64 (OpMem64 << DstShift)
89#define DstImmUByte (OpImmUByte << DstShift)
90#define DstDX (OpDX << DstShift)
Avi Kivity820207c2013-02-09 11:31:45 +020091#define DstAccLo (OpAccLo << DstShift)
Avi Kivitya99455492011-09-13 10:45:41 +030092#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080093/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030094#define SrcShift 6
95#define SrcNone (OpNone << SrcShift)
96#define SrcReg (OpReg << SrcShift)
97#define SrcMem (OpMem << SrcShift)
98#define SrcMem16 (OpMem16 << SrcShift)
99#define SrcMem32 (OpMem32 << SrcShift)
100#define SrcImm (OpImm << SrcShift)
101#define SrcImmByte (OpImmByte << SrcShift)
102#define SrcOne (OpOne << SrcShift)
103#define SrcImmUByte (OpImmUByte << SrcShift)
104#define SrcImmU (OpImmU << SrcShift)
105#define SrcSI (OpSI << SrcShift)
Paolo Bonzini7fa57952013-05-09 11:32:50 +0200106#define SrcXLat (OpXLat << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300107#define SrcImmFAddr (OpImmFAddr << SrcShift)
108#define SrcMemFAddr (OpMemFAddr << SrcShift)
109#define SrcAcc (OpAcc << SrcShift)
110#define SrcImmU16 (OpImmU16 << SrcShift)
Nadav Amit5e2c6882012-12-06 21:55:10 -0200111#define SrcImm64 (OpImm64 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300112#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200113#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity820207c2013-02-09 11:31:45 +0200114#define SrcAccHi (OpAccHi << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300115#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300116#define BitOp (1<<11)
117#define MemAbs (1<<12) /* Memory operand is absolute displacement */
118#define String (1<<13) /* String instruction (rep capable) */
119#define Stack (1<<14) /* Stack instruction (push/pop) */
120#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
121#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
122#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
123#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
124#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
Gleb Natapov045a2822012-12-20 16:57:43 +0200125#define Escape (5<<15) /* Escape to coprocessor instruction */
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300126#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300127/* Generic ModRM decode. */
128#define ModRM (1<<19)
129/* Destination is only written; never read. */
130#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300131/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200132#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Borislav Petkovb51e9742013-09-22 16:44:52 +0200133#define EmulateOnUD (1<<22) /* Emulate if unsupported by the host */
Avi Kivity5a506b12010-08-01 15:10:29 +0300134#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300135#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300136#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200137#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200138#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300139#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800140#define PageTable (1 << 29) /* instruction used to write page table */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300141#define NotImpl (1 << 30) /* instruction is not implemented */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100142/* Source 2 operand type */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300143#define Src2Shift (31)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300144#define Src2None (OpNone << Src2Shift)
Avi Kivityab2c5ce2013-02-09 11:31:46 +0200145#define Src2Mem (OpMem << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300146#define Src2CL (OpCL << Src2Shift)
147#define Src2ImmByte (OpImmByte << Src2Shift)
148#define Src2One (OpOne << Src2Shift)
149#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300150#define Src2ES (OpES << Src2Shift)
151#define Src2CS (OpCS << Src2Shift)
152#define Src2SS (OpSS << Src2Shift)
153#define Src2DS (OpDS << Src2Shift)
154#define Src2FS (OpFS << Src2Shift)
155#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300156#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300157#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Avi Kivity1c11b372012-04-09 18:39:59 +0300158#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
159#define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */
160#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
Avi Kivitye28bbd42013-01-04 16:18:48 +0200161#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
Avi Kivityb6744dc2013-01-04 16:18:50 +0200162#define NoWrite ((u64)1 << 45) /* No writeback */
Avi Kivityfb32b1e2013-02-09 11:31:44 +0200163#define SrcWrite ((u64)1 << 46) /* Write back src operand */
Nadav Amit9b88ae92014-05-25 23:05:21 +0300164#define NoMod ((u64)1 << 47) /* Mod field is ignored */
Paolo Bonzinid40a6892014-03-27 11:58:02 +0100165#define Intercept ((u64)1 << 48) /* Has valid intercept field */
166#define CheckPerm ((u64)1 << 49) /* Has valid check_perm field */
Nadav Amit10e38fc2014-06-18 17:19:34 +0300167#define NoBigReal ((u64)1 << 50) /* No big real mode */
Nadav Amit68efa762014-06-18 17:19:35 +0300168#define PrivUD ((u64)1 << 51) /* #UD instead of #GP on CPL > 0 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800169
Avi Kivity820207c2013-02-09 11:31:45 +0200170#define DstXacc (DstAccLo | SrcAccHi | SrcWrite)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800171
Avi Kivityd0e53322010-07-29 15:11:54 +0300172#define X2(x...) x, x
173#define X3(x...) X2(x), x
174#define X4(x...) X2(x), X2(x)
175#define X5(x...) X4(x), x
176#define X6(x...) X4(x), X2(x)
177#define X7(x...) X4(x), X3(x)
178#define X8(x...) X4(x), X4(x)
179#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300180
Avi Kivitye28bbd42013-01-04 16:18:48 +0200181#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
182#define FASTOP_SIZE 8
183
184/*
185 * fastop functions have a special calling convention:
186 *
Avi Kivity017da7b2013-02-09 11:31:47 +0200187 * dst: rax (in/out)
188 * src: rdx (in/out)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200189 * src2: rcx (in)
190 * flags: rflags (in/out)
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200191 * ex: rsi (in:fastop pointer, out:zero if exception)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200192 *
193 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
194 * different operand sizes can be reached by calculation, rather than a jump
195 * table (which would be bigger than the code).
196 *
197 * fastop functions are declared as taking a never-defined fastop parameter,
198 * so they can't be called from C directly.
199 */
200
201struct fastop;
202
Avi Kivityd65b1de2010-07-29 15:11:35 +0300203struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300204 u64 flags : 56;
205 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300206 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300207 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200208 const struct opcode *group;
209 const struct group_dual *gdual;
210 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200211 const struct escape *esc;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200212 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300213 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200214 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300215};
216
217struct group_dual {
218 struct opcode mod012[8];
219 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300220};
221
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200222struct gprefix {
223 struct opcode pfx_no;
224 struct opcode pfx_66;
225 struct opcode pfx_f2;
226 struct opcode pfx_f3;
227};
228
Gleb Natapov045a2822012-12-20 16:57:43 +0200229struct escape {
230 struct opcode op[8];
231 struct opcode high[64];
232};
233
Avi Kivity6aa8b732006-12-10 02:21:36 -0800234/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200235#define EFLG_ID (1<<21)
236#define EFLG_VIP (1<<20)
237#define EFLG_VIF (1<<19)
238#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200239#define EFLG_VM (1<<17)
240#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200241#define EFLG_IOPL (3<<12)
242#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800243#define EFLG_OF (1<<11)
244#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200245#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200246#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800247#define EFLG_SF (1<<7)
248#define EFLG_ZF (1<<6)
249#define EFLG_AF (1<<4)
250#define EFLG_PF (1<<2)
251#define EFLG_CF (1<<0)
252
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300253#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
254#define EFLG_RESERVED_ONE_MASK 2
255
Avi Kivitydd856ef2012-08-27 23:46:17 +0300256static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
257{
258 if (!(ctxt->regs_valid & (1 << nr))) {
259 ctxt->regs_valid |= 1 << nr;
260 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
261 }
262 return ctxt->_regs[nr];
263}
264
265static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
266{
267 ctxt->regs_valid |= 1 << nr;
268 ctxt->regs_dirty |= 1 << nr;
269 return &ctxt->_regs[nr];
270}
271
272static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
273{
274 reg_read(ctxt, nr);
275 return reg_write(ctxt, nr);
276}
277
278static void writeback_registers(struct x86_emulate_ctxt *ctxt)
279{
280 unsigned reg;
281
282 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
283 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
284}
285
286static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
287{
288 ctxt->regs_dirty = 0;
289 ctxt->regs_valid = 0;
290}
291
Avi Kivity6aa8b732006-12-10 02:21:36 -0800292/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800293 * These EFLAGS bits are restored from saved value during emulation, and
294 * any changes are written back to the saved value after emulation.
295 */
296#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
297
Avi Kivitydda96d82008-11-26 15:14:10 +0200298#ifdef CONFIG_X86_64
299#define ON64(x) x
300#else
301#define ON64(x)
302#endif
303
Avi Kivity4d758342013-01-19 19:51:55 +0200304static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *));
305
Avi Kivityb7d491e2013-01-04 16:18:49 +0200306#define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t"
307#define FOP_RET "ret \n\t"
308
309#define FOP_START(op) \
310 extern void em_##op(struct fastop *fake); \
311 asm(".pushsection .text, \"ax\" \n\t" \
312 ".global em_" #op " \n\t" \
313 FOP_ALIGN \
314 "em_" #op ": \n\t"
315
316#define FOP_END \
317 ".popsection")
318
Avi Kivity0bdea062013-01-19 19:51:50 +0200319#define FOPNOP() FOP_ALIGN FOP_RET
320
Avi Kivityb7d491e2013-01-04 16:18:49 +0200321#define FOP1E(op, dst) \
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200322 FOP_ALIGN "10: " #op " %" #dst " \n\t" FOP_RET
323
324#define FOP1EEX(op, dst) \
325 FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200326
327#define FASTOP1(op) \
328 FOP_START(op) \
329 FOP1E(op##b, al) \
330 FOP1E(op##w, ax) \
331 FOP1E(op##l, eax) \
332 ON64(FOP1E(op##q, rax)) \
333 FOP_END
334
Avi Kivityb9fa4092013-02-09 11:31:48 +0200335/* 1-operand, using src2 (for MUL/DIV r/m) */
336#define FASTOP1SRC2(op, name) \
337 FOP_START(name) \
338 FOP1E(op, cl) \
339 FOP1E(op, cx) \
340 FOP1E(op, ecx) \
341 ON64(FOP1E(op, rcx)) \
342 FOP_END
343
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200344/* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */
345#define FASTOP1SRC2EX(op, name) \
346 FOP_START(name) \
347 FOP1EEX(op, cl) \
348 FOP1EEX(op, cx) \
349 FOP1EEX(op, ecx) \
350 ON64(FOP1EEX(op, rcx)) \
351 FOP_END
352
Avi Kivityf7857f32013-01-04 16:18:53 +0200353#define FOP2E(op, dst, src) \
354 FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET
355
356#define FASTOP2(op) \
357 FOP_START(op) \
Avi Kivity017da7b2013-02-09 11:31:47 +0200358 FOP2E(op##b, al, dl) \
359 FOP2E(op##w, ax, dx) \
360 FOP2E(op##l, eax, edx) \
361 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivityf7857f32013-01-04 16:18:53 +0200362 FOP_END
363
Avi Kivity11c363b2013-01-19 19:51:54 +0200364/* 2 operand, word only */
365#define FASTOP2W(op) \
366 FOP_START(op) \
367 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200368 FOP2E(op##w, ax, dx) \
369 FOP2E(op##l, eax, edx) \
370 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivity11c363b2013-01-19 19:51:54 +0200371 FOP_END
372
Avi Kivity007a3b52013-01-19 19:51:51 +0200373/* 2 operand, src is CL */
374#define FASTOP2CL(op) \
375 FOP_START(op) \
376 FOP2E(op##b, al, cl) \
377 FOP2E(op##w, ax, cl) \
378 FOP2E(op##l, eax, cl) \
379 ON64(FOP2E(op##q, rax, cl)) \
380 FOP_END
381
Avi Kivity0bdea062013-01-19 19:51:50 +0200382#define FOP3E(op, dst, src, src2) \
383 FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
384
385/* 3-operand, word-only, src2=cl */
386#define FASTOP3WCL(op) \
387 FOP_START(op) \
388 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200389 FOP3E(op##w, ax, dx, cl) \
390 FOP3E(op##l, eax, edx, cl) \
391 ON64(FOP3E(op##q, rax, rdx, cl)) \
Avi Kivity0bdea062013-01-19 19:51:50 +0200392 FOP_END
393
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200394/* Special case for SETcc - 1 instruction per cc */
395#define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t"
396
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200397asm(".global kvm_fastop_exception \n"
398 "kvm_fastop_exception: xor %esi, %esi; ret");
399
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200400FOP_START(setcc)
401FOP_SETCC(seto)
402FOP_SETCC(setno)
403FOP_SETCC(setc)
404FOP_SETCC(setnc)
405FOP_SETCC(setz)
406FOP_SETCC(setnz)
407FOP_SETCC(setbe)
408FOP_SETCC(setnbe)
409FOP_SETCC(sets)
410FOP_SETCC(setns)
411FOP_SETCC(setp)
412FOP_SETCC(setnp)
413FOP_SETCC(setl)
414FOP_SETCC(setnl)
415FOP_SETCC(setle)
416FOP_SETCC(setnle)
417FOP_END;
418
Paolo Bonzini326f5782013-05-09 11:32:51 +0200419FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET
420FOP_END;
421
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200422static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
423 enum x86_intercept intercept,
424 enum x86_intercept_stage stage)
425{
426 struct x86_instruction_info info = {
427 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300428 .rep_prefix = ctxt->rep_prefix,
429 .modrm_mod = ctxt->modrm_mod,
430 .modrm_reg = ctxt->modrm_reg,
431 .modrm_rm = ctxt->modrm_rm,
432 .src_val = ctxt->src.val64,
Jan Kiszka6cbc5f52014-06-30 12:52:55 +0200433 .dst_val = ctxt->dst.val64,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300434 .src_bytes = ctxt->src.bytes,
435 .dst_bytes = ctxt->dst.bytes,
436 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200437 .next_rip = ctxt->eip,
438 };
439
Avi Kivity29535382011-04-20 13:37:53 +0300440 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200441}
442
Avi Kivityf47cfa32012-06-07 17:49:24 +0300443static void assign_masked(ulong *dest, ulong src, ulong mask)
444{
445 *dest = (*dest & ~mask) | (src & mask);
446}
447
Avi Kivity9dac77f2011-06-01 15:34:25 +0300448static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800449{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300450 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800451}
452
Avi Kivityf47cfa32012-06-07 17:49:24 +0300453static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
454{
455 u16 sel;
456 struct desc_struct ss;
457
458 if (ctxt->mode == X86EMUL_MODE_PROT64)
459 return ~0UL;
460 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
461 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
462}
463
Avi Kivity612e89f2012-06-12 20:03:23 +0300464static int stack_size(struct x86_emulate_ctxt *ctxt)
465{
466 return (__fls(stack_mask(ctxt)) + 1) >> 3;
467}
468
Avi Kivity6aa8b732006-12-10 02:21:36 -0800469/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800470static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300471address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800472{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300473 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800474 return reg;
475 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300476 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800477}
478
479static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300480register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800481{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300482 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800483}
484
Avi Kivity5ad105e2012-08-19 14:34:31 +0300485static void masked_increment(ulong *reg, ulong mask, int inc)
486{
487 assign_masked(reg, *reg + inc, mask);
488}
489
Harvey Harrison7a9572752008-02-19 07:40:41 -0800490static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300491register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800492{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300493 ulong mask;
494
Avi Kivity9dac77f2011-06-01 15:34:25 +0300495 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300496 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800497 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300498 mask = ad_mask(ctxt);
499 masked_increment(reg, mask, inc);
500}
501
502static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
503{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300504 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800505}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800506
Avi Kivity56697682011-04-03 14:08:51 +0300507static u32 desc_limit_scaled(struct desc_struct *desc)
508{
509 u32 limit = get_desc_limit(desc);
510
511 return desc->g ? (limit << 12) | 0xfff : limit;
512}
513
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900514static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300515{
516 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
517 return 0;
518
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900519 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300520}
521
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200522static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
523 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300524{
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +0200525 WARN_ON(vec > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +0200526 ctxt->exception.vector = vec;
527 ctxt->exception.error_code = error;
528 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200529 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300530}
531
Joerg Roedel3b88e412011-04-04 12:39:29 +0200532static int emulate_db(struct x86_emulate_ctxt *ctxt)
533{
534 return emulate_exception(ctxt, DB_VECTOR, 0, false);
535}
536
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200537static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300538{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200539 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300540}
541
Avi Kivity618ff152011-04-03 12:32:09 +0300542static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
543{
544 return emulate_exception(ctxt, SS_VECTOR, err, true);
545}
546
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200547static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300548{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200549 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300550}
551
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200552static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300553{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200554 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300555}
556
Avi Kivity34d1f492010-08-26 11:59:01 +0300557static int emulate_de(struct x86_emulate_ctxt *ctxt)
558{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200559 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300560}
561
Avi Kivity1253791d2011-03-29 11:41:27 +0200562static int emulate_nm(struct x86_emulate_ctxt *ctxt)
563{
564 return emulate_exception(ctxt, NM_VECTOR, 0, false);
565}
566
Nadav Amit234f3ce2014-09-18 22:39:38 +0300567static inline int assign_eip_far(struct x86_emulate_ctxt *ctxt, ulong dst,
568 int cs_l)
Nadav Amit05c83ec2014-09-18 22:39:37 +0300569{
570 switch (ctxt->op_bytes) {
571 case 2:
572 ctxt->_eip = (u16)dst;
573 break;
574 case 4:
575 ctxt->_eip = (u32)dst;
576 break;
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{
Paolo Bonzini17052f12014-05-06 16:33:01 +0200781 if (unlikely(ctxt->fetch.end - ctxt->fetch.ptr < size))
Paolo Bonzini9506d572014-05-06 13:05:25 +0200782 return __do_insn_fetch_bytes(ctxt, size);
783 else
784 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200785}
786
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900787/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900788#define insn_fetch(_type, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200789({ _type _x; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200790 \
791 rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900792 if (rc != X86EMUL_CONTINUE) \
793 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200794 ctxt->_eip += sizeof(_type); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200795 _x = *(_type __aligned(1) *) ctxt->fetch.ptr; \
796 ctxt->fetch.ptr += sizeof(_type); \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200797 _x; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900798})
799
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900800#define insn_fetch_arr(_arr, _size, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200801({ \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200802 rc = do_insn_fetch_bytes(_ctxt, _size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900803 if (rc != X86EMUL_CONTINUE) \
804 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200805 ctxt->_eip += (_size); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200806 memcpy(_arr, ctxt->fetch.ptr, _size); \
807 ctxt->fetch.ptr += (_size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900808})
809
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000810/*
811 * Given the 'reg' portion of a ModRM byte, and a register block, return a
812 * pointer into the block that addresses the relevant register.
813 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
814 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300815static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200816 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800817{
818 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200819 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800820
Avi Kivity6aa8b732006-12-10 02:21:36 -0800821 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300822 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
823 else
824 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825 return p;
826}
827
828static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200829 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800830 u16 *size, unsigned long *address, int op_bytes)
831{
832 int rc;
833
834 if (op_bytes == 2)
835 op_bytes = 3;
836 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200837 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900838 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200840 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200841 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800842 return rc;
843}
844
Avi Kivity34b77652013-01-19 19:51:56 +0200845FASTOP2(add);
846FASTOP2(or);
847FASTOP2(adc);
848FASTOP2(sbb);
849FASTOP2(and);
850FASTOP2(sub);
851FASTOP2(xor);
852FASTOP2(cmp);
853FASTOP2(test);
854
Avi Kivityb9fa4092013-02-09 11:31:48 +0200855FASTOP1SRC2(mul, mul_ex);
856FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200857FASTOP1SRC2EX(div, div_ex);
858FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +0200859
Avi Kivity34b77652013-01-19 19:51:56 +0200860FASTOP3WCL(shld);
861FASTOP3WCL(shrd);
862
863FASTOP2W(imul);
864
865FASTOP1(not);
866FASTOP1(neg);
867FASTOP1(inc);
868FASTOP1(dec);
869
870FASTOP2CL(rol);
871FASTOP2CL(ror);
872FASTOP2CL(rcl);
873FASTOP2CL(rcr);
874FASTOP2CL(shl);
875FASTOP2CL(shr);
876FASTOP2CL(sar);
877
878FASTOP2W(bsf);
879FASTOP2W(bsr);
880FASTOP2W(bt);
881FASTOP2W(bts);
882FASTOP2W(btr);
883FASTOP2W(btc);
884
Avi Kivitye47a5f52013-02-09 11:31:51 +0200885FASTOP2(xadd);
886
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200887static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300888{
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200889 u8 rc;
890 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300891
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200892 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +0200893 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200894 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
895 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300896}
897
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300898static void fetch_register_operand(struct operand *op)
899{
900 switch (op->bytes) {
901 case 1:
902 op->val = *(u8 *)op->addr.reg;
903 break;
904 case 2:
905 op->val = *(u16 *)op->addr.reg;
906 break;
907 case 4:
908 op->val = *(u32 *)op->addr.reg;
909 break;
910 case 8:
911 op->val = *(u64 *)op->addr.reg;
912 break;
913 }
914}
915
Avi Kivity1253791d2011-03-29 11:41:27 +0200916static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
917{
918 ctxt->ops->get_fpu(ctxt);
919 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200920 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
921 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
922 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
923 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
924 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
925 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
926 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
927 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200928#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200929 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
930 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
931 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
932 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
933 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
934 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
935 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
936 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200937#endif
938 default: BUG();
939 }
940 ctxt->ops->put_fpu(ctxt);
941}
942
943static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
944 int reg)
945{
946 ctxt->ops->get_fpu(ctxt);
947 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200948 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
949 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
950 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
951 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
952 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
953 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
954 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
955 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200956#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200957 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
958 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
959 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
960 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
961 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
962 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
963 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
964 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200965#endif
966 default: BUG();
967 }
968 ctxt->ops->put_fpu(ctxt);
969}
970
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300971static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
972{
973 ctxt->ops->get_fpu(ctxt);
974 switch (reg) {
975 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
976 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
977 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
978 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
979 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
980 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
981 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
982 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
983 default: BUG();
984 }
985 ctxt->ops->put_fpu(ctxt);
986}
987
988static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
989{
990 ctxt->ops->get_fpu(ctxt);
991 switch (reg) {
992 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
993 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
994 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
995 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
996 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
997 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
998 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
999 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1000 default: BUG();
1001 }
1002 ctxt->ops->put_fpu(ctxt);
1003}
1004
Gleb Natapov045a2822012-12-20 16:57:43 +02001005static int em_fninit(struct x86_emulate_ctxt *ctxt)
1006{
1007 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1008 return emulate_nm(ctxt);
1009
1010 ctxt->ops->get_fpu(ctxt);
1011 asm volatile("fninit");
1012 ctxt->ops->put_fpu(ctxt);
1013 return X86EMUL_CONTINUE;
1014}
1015
1016static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1017{
1018 u16 fcw;
1019
1020 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1021 return emulate_nm(ctxt);
1022
1023 ctxt->ops->get_fpu(ctxt);
1024 asm volatile("fnstcw %0": "+m"(fcw));
1025 ctxt->ops->put_fpu(ctxt);
1026
1027 /* force 2 byte destination */
1028 ctxt->dst.bytes = 2;
1029 ctxt->dst.val = fcw;
1030
1031 return X86EMUL_CONTINUE;
1032}
1033
1034static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1035{
1036 u16 fsw;
1037
1038 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1039 return emulate_nm(ctxt);
1040
1041 ctxt->ops->get_fpu(ctxt);
1042 asm volatile("fnstsw %0": "+m"(fsw));
1043 ctxt->ops->put_fpu(ctxt);
1044
1045 /* force 2 byte destination */
1046 ctxt->dst.bytes = 2;
1047 ctxt->dst.val = fsw;
1048
1049 return X86EMUL_CONTINUE;
1050}
1051
Avi Kivity1253791d2011-03-29 11:41:27 +02001052static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001053 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001054{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001055 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001056
Avi Kivity9dac77f2011-06-01 15:34:25 +03001057 if (!(ctxt->d & ModRM))
1058 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity1253791d2011-03-29 11:41:27 +02001059
Avi Kivity9dac77f2011-06-01 15:34:25 +03001060 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001061 op->type = OP_XMM;
1062 op->bytes = 16;
1063 op->addr.xmm = reg;
1064 read_sse_reg(ctxt, &op->vec_val, reg);
1065 return;
1066 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001067 if (ctxt->d & Mmx) {
1068 reg &= 7;
1069 op->type = OP_MM;
1070 op->bytes = 8;
1071 op->addr.mm = reg;
1072 return;
1073 }
Avi Kivity1253791d2011-03-29 11:41:27 +02001074
Avi Kivity3c118e22007-10-31 10:27:04 +02001075 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001076 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1077 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1078
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001079 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001080 op->orig_val = op->val;
1081}
1082
Avi Kivitya6e34072012-06-10 17:15:39 +03001083static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1084{
1085 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1086 ctxt->modrm_seg = VCPU_SREG_SS;
1087}
1088
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001089static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001090 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001091{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001092 u8 sib;
Bandan Das02357bd2014-04-16 12:46:11 -04001093 int index_reg, base_reg, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001094 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001095 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001096
Bandan Das02357bd2014-04-16 12:46:11 -04001097 ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */
1098 index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */
1099 base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001100
Bandan Das02357bd2014-04-16 12:46:11 -04001101 ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001102 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
Bandan Das02357bd2014-04-16 12:46:11 -04001103 ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001104 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001105
Nadav Amit9b88ae92014-05-25 23:05:21 +03001106 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001107 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001108 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001109 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001110 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001111 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001112 op->type = OP_XMM;
1113 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001114 op->addr.xmm = ctxt->modrm_rm;
1115 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001116 return rc;
1117 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001118 if (ctxt->d & Mmx) {
1119 op->type = OP_MM;
1120 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001121 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001122 return rc;
1123 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001124 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001125 return rc;
1126 }
1127
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001128 op->type = OP_MEM;
1129
Avi Kivity9dac77f2011-06-01 15:34:25 +03001130 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001131 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1132 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1133 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1134 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001135
1136 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001137 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001138 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001139 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001140 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001141 break;
1142 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001143 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001144 break;
1145 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001146 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001147 break;
1148 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001149 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001150 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001151 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001152 break;
1153 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001154 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001155 break;
1156 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001157 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001158 break;
1159 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001160 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001161 break;
1162 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001163 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001164 break;
1165 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001166 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001167 break;
1168 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001169 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001170 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001171 break;
1172 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001173 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001174 break;
1175 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001176 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1177 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1178 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001179 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001180 } else {
1181 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001182 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001183 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001184 index_reg |= (sib >> 3) & 7;
1185 base_reg |= sib & 7;
1186 scale = sib >> 6;
1187
Avi Kivity9dac77f2011-06-01 15:34:25 +03001188 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001189 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001190 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001191 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001192 adjust_modrm_seg(ctxt, base_reg);
1193 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001194 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001195 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001196 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001197 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001198 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001199 } else {
1200 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001201 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001202 adjust_modrm_seg(ctxt, base_reg);
1203 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001204 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001205 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001206 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001207 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001208 break;
1209 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001210 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001211 break;
1212 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001213 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001214 break;
1215 }
1216 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001217 op->addr.mem.ea = modrm_ea;
Bandan Das41061cd2014-04-16 12:46:14 -04001218 if (ctxt->ad_bytes != 8)
1219 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
1220
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001221done:
1222 return rc;
1223}
1224
1225static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001226 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001227{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001228 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001229
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001230 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001231 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001232 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001233 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001234 break;
1235 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001236 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001237 break;
1238 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001239 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001240 break;
1241 }
1242done:
1243 return rc;
1244}
1245
Avi Kivity9dac77f2011-06-01 15:34:25 +03001246static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001247{
Sheng Yang7129eec2010-09-28 16:33:32 +08001248 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001249
Avi Kivity9dac77f2011-06-01 15:34:25 +03001250 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001251 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001252
Avi Kivity9dac77f2011-06-01 15:34:25 +03001253 if (ctxt->src.bytes == 2)
1254 sv = (s16)ctxt->src.val & (s16)mask;
1255 else if (ctxt->src.bytes == 4)
1256 sv = (s32)ctxt->src.val & (s32)mask;
Nadav Amit7dec5602014-06-15 16:12:57 +03001257 else
1258 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001259
Avi Kivity9dac77f2011-06-01 15:34:25 +03001260 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001261 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001262
1263 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001264 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001265}
1266
Gleb Natapov9de41572010-04-28 19:15:22 +03001267static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001268 unsigned long addr, void *dest, unsigned size)
1269{
1270 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001271 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001272
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001273 if (mc->pos < mc->end)
1274 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001275
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001276 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001277
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001278 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1279 &ctxt->exception);
1280 if (rc != X86EMUL_CONTINUE)
1281 return rc;
1282
1283 mc->end += size;
1284
1285read_cached:
1286 memcpy(dest, mc->data + mc->pos, size);
1287 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001288 return X86EMUL_CONTINUE;
1289}
1290
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001291static int segmented_read(struct x86_emulate_ctxt *ctxt,
1292 struct segmented_address addr,
1293 void *data,
1294 unsigned size)
1295{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001296 int rc;
1297 ulong linear;
1298
Avi Kivity83b87952011-04-03 11:31:19 +03001299 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001300 if (rc != X86EMUL_CONTINUE)
1301 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001302 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001303}
1304
1305static int segmented_write(struct x86_emulate_ctxt *ctxt,
1306 struct segmented_address addr,
1307 const void *data,
1308 unsigned size)
1309{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001310 int rc;
1311 ulong linear;
1312
Avi Kivity83b87952011-04-03 11:31:19 +03001313 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001314 if (rc != X86EMUL_CONTINUE)
1315 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001316 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1317 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001318}
1319
1320static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1321 struct segmented_address addr,
1322 const void *orig_data, const void *data,
1323 unsigned size)
1324{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001325 int rc;
1326 ulong linear;
1327
Avi Kivity83b87952011-04-03 11:31:19 +03001328 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001329 if (rc != X86EMUL_CONTINUE)
1330 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001331 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1332 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001333}
1334
Gleb Natapov7b262e92010-03-18 15:20:27 +02001335static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001336 unsigned int size, unsigned short port,
1337 void *dest)
1338{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001339 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001340
1341 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001342 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001343 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001344 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001345 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001346 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1347 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Mark Rustadb55a8142014-07-25 06:27:05 -07001348 n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001349 if (n == 0)
1350 n = 1;
1351 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001352 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001353 return 0;
1354 rc->end = n * size;
1355 }
1356
Nadav Amite6e39f02014-04-18 03:35:10 +03001357 if (ctxt->rep_prefix && (ctxt->d & String) &&
1358 !(ctxt->eflags & EFLG_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001359 ctxt->dst.data = rc->data + rc->pos;
1360 ctxt->dst.type = OP_MEM_STR;
1361 ctxt->dst.count = (rc->end - rc->pos) / size;
1362 rc->pos = rc->end;
1363 } else {
1364 memcpy(dest, rc->data + rc->pos, size);
1365 rc->pos += size;
1366 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001367 return 1;
1368}
1369
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001370static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1371 u16 index, struct desc_struct *desc)
1372{
1373 struct desc_ptr dt;
1374 ulong addr;
1375
1376 ctxt->ops->get_idt(ctxt, &dt);
1377
1378 if (dt.size < index * 8 + 7)
1379 return emulate_gp(ctxt, index << 3 | 0x2);
1380
1381 addr = dt.address + index * 8;
1382 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1383 &ctxt->exception);
1384}
1385
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001386static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001387 u16 selector, struct desc_ptr *dt)
1388{
Mathias Krause0225fb52012-08-30 01:30:16 +02001389 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001390 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001391
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001392 if (selector & 1 << 2) {
1393 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001394 u16 sel;
1395
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001396 memset (dt, 0, sizeof *dt);
Nadav Amit2eedcac2014-06-02 18:34:05 +03001397 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1398 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001399 return;
1400
1401 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001402 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001403 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001404 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001405}
1406
1407/* allowed just for 8 bytes segments */
1408static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001409 u16 selector, struct desc_struct *desc,
1410 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001411{
1412 struct desc_ptr dt;
1413 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001414 ulong addr;
1415
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001416 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001417
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001418 if (dt.size < index * 8 + 7)
1419 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001420
Avi Kivitye9194642012-06-13 16:29:39 +03001421 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001422 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1423 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001424}
1425
1426/* allowed just for 8 bytes segments */
1427static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001428 u16 selector, struct desc_struct *desc)
1429{
1430 struct desc_ptr dt;
1431 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001432 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001433
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001434 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001435
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001436 if (dt.size < index * 8 + 7)
1437 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001438
1439 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001440 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1441 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001442}
1443
Gleb Natapov5601d052011-03-07 14:55:06 +02001444/* Does not support long mode */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001445static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Nadav Amitd1442d82014-09-18 22:39:39 +03001446 u16 selector, int seg, u8 cpl,
1447 bool in_task_switch,
1448 struct desc_struct *desc)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001449{
Avi Kivity869be992012-06-13 16:30:53 +03001450 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001451 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001452 unsigned err_vec = GP_VECTOR;
1453 u32 err_code = 0;
1454 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001455 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001456 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001457 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001458 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001459
1460 memset(&seg_desc, 0, sizeof seg_desc);
1461
Kevin Wolff8da94e2013-04-11 14:06:03 +02001462 if (ctxt->mode == X86EMUL_MODE_REAL) {
1463 /* set real mode segment descriptor (keep limit etc. for
1464 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001465 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001466 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001467 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001468 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1469 /* VM86 needs a clean new segment descriptor */
1470 set_desc_base(&seg_desc, selector << 4);
1471 set_desc_limit(&seg_desc, 0xffff);
1472 seg_desc.type = 3;
1473 seg_desc.p = 1;
1474 seg_desc.s = 1;
1475 seg_desc.dpl = 3;
1476 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001477 }
1478
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001479 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001480
1481 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1482 if ((seg == VCPU_SREG_CS
1483 || (seg == VCPU_SREG_SS
1484 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1485 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001486 && null_selector)
1487 goto exception;
1488
1489 /* TR should be in GDT only */
1490 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1491 goto exception;
1492
1493 if (null_selector) /* for NULL selector skip all following checks */
1494 goto load;
1495
Avi Kivitye9194642012-06-13 16:29:39 +03001496 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001497 if (ret != X86EMUL_CONTINUE)
1498 return ret;
1499
1500 err_code = selector & 0xfffc;
Paolo Bonzini15fc0752014-08-18 13:17:00 +02001501 err_vec = in_task_switch ? TS_VECTOR : GP_VECTOR;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001502
Guo Chaofc058682012-06-28 15:19:51 +08001503 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001504 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1505 goto exception;
1506
1507 if (!seg_desc.p) {
1508 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1509 goto exception;
1510 }
1511
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001512 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001513
1514 switch (seg) {
1515 case VCPU_SREG_SS:
1516 /*
1517 * segment is not a writable data segment or segment
1518 * selector's RPL != CPL or segment selector's RPL != CPL
1519 */
1520 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1521 goto exception;
1522 break;
1523 case VCPU_SREG_CS:
1524 if (!(seg_desc.type & 8))
1525 goto exception;
1526
1527 if (seg_desc.type & 4) {
1528 /* conforming */
1529 if (dpl > cpl)
1530 goto exception;
1531 } else {
1532 /* nonconforming */
1533 if (rpl > cpl || dpl != cpl)
1534 goto exception;
1535 }
Nadav Amit040c8dc2014-09-18 22:39:43 +03001536 /* in long-mode d/b must be clear if l is set */
1537 if (seg_desc.d && seg_desc.l) {
1538 u64 efer = 0;
1539
1540 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1541 if (efer & EFER_LMA)
1542 goto exception;
1543 }
1544
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001545 /* CS(RPL) <- CPL */
1546 selector = (selector & 0xfffc) | cpl;
1547 break;
1548 case VCPU_SREG_TR:
1549 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1550 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001551 old_desc = seg_desc;
1552 seg_desc.type |= 2; /* busy */
1553 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1554 sizeof(seg_desc), &ctxt->exception);
1555 if (ret != X86EMUL_CONTINUE)
1556 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001557 break;
1558 case VCPU_SREG_LDTR:
1559 if (seg_desc.s || seg_desc.type != 2)
1560 goto exception;
1561 break;
1562 default: /* DS, ES, FS, or GS */
1563 /*
1564 * segment is not a data or readable code segment or
1565 * ((segment is a data or nonconforming code segment)
1566 * and (both RPL and CPL > DPL))
1567 */
1568 if ((seg_desc.type & 0xa) == 0x8 ||
1569 (((seg_desc.type & 0xc) != 0xc) &&
1570 (rpl > dpl && cpl > dpl)))
1571 goto exception;
1572 break;
1573 }
1574
1575 if (seg_desc.s) {
1576 /* mark segment as accessed */
1577 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001578 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001579 if (ret != X86EMUL_CONTINUE)
1580 return ret;
Nadav Amite37a75a2014-06-02 18:34:04 +03001581 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
1582 ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3,
1583 sizeof(base3), &ctxt->exception);
1584 if (ret != X86EMUL_CONTINUE)
1585 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001586 }
1587load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001588 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Nadav Amitd1442d82014-09-18 22:39:39 +03001589 if (desc)
1590 *desc = seg_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001591 return X86EMUL_CONTINUE;
1592exception:
Paolo Bonzini592f0852014-08-20 10:05:08 +02001593 return emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001594}
1595
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001596static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1597 u16 selector, int seg)
1598{
1599 u8 cpl = ctxt->ops->cpl(ctxt);
Nadav Amitd1442d82014-09-18 22:39:39 +03001600 return __load_segment_descriptor(ctxt, selector, seg, cpl, false, NULL);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001601}
1602
Wei Yongjun31be40b2010-08-17 09:17:30 +08001603static void write_register_operand(struct operand *op)
1604{
1605 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1606 switch (op->bytes) {
1607 case 1:
1608 *(u8 *)op->addr.reg = (u8)op->val;
1609 break;
1610 case 2:
1611 *(u16 *)op->addr.reg = (u16)op->val;
1612 break;
1613 case 4:
1614 *op->addr.reg = (u32)op->val;
1615 break; /* 64b: zero-extend */
1616 case 8:
1617 *op->addr.reg = op->val;
1618 break;
1619 }
1620}
1621
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001622static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001623{
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001624 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001625 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001626 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001627 break;
1628 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001629 if (ctxt->lock_prefix)
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001630 return segmented_cmpxchg(ctxt,
1631 op->addr.mem,
1632 &op->orig_val,
1633 &op->val,
1634 op->bytes);
1635 else
1636 return segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001637 op->addr.mem,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001638 &op->val,
1639 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001640 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001641 case OP_MEM_STR:
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001642 return segmented_write(ctxt,
1643 op->addr.mem,
1644 op->data,
1645 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001646 break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001647 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001648 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001649 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001650 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001651 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001652 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001653 case OP_NONE:
1654 /* no writeback */
1655 break;
1656 default:
1657 break;
1658 }
1659 return X86EMUL_CONTINUE;
1660}
1661
Avi Kivity51ddff52012-06-12 20:19:40 +03001662static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001663{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001664 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001665
Avi Kivity5ad105e2012-08-19 14:34:31 +03001666 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001667 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001668 addr.seg = VCPU_SREG_SS;
1669
Avi Kivity51ddff52012-06-12 20:19:40 +03001670 return segmented_write(ctxt, addr, data, bytes);
1671}
1672
1673static int em_push(struct x86_emulate_ctxt *ctxt)
1674{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001675 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001676 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001677 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001678}
1679
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001680static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001681 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001682{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001683 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001684 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001685
Avi Kivitydd856ef2012-08-27 23:46:17 +03001686 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001687 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001688 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001689 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001690 return rc;
1691
Avi Kivity5ad105e2012-08-19 14:34:31 +03001692 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001693 return rc;
1694}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001695
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001696static int em_pop(struct x86_emulate_ctxt *ctxt)
1697{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001698 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001699}
1700
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001701static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001702 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001703{
1704 int rc;
1705 unsigned long val, change_mask;
1706 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001707 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001708
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001709 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001710 if (rc != X86EMUL_CONTINUE)
1711 return rc;
1712
1713 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
Nadav Amit163b1352014-07-21 14:37:28 +03001714 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_AC | EFLG_ID;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001715
1716 switch(ctxt->mode) {
1717 case X86EMUL_MODE_PROT64:
1718 case X86EMUL_MODE_PROT32:
1719 case X86EMUL_MODE_PROT16:
1720 if (cpl == 0)
1721 change_mask |= EFLG_IOPL;
1722 if (cpl <= iopl)
1723 change_mask |= EFLG_IF;
1724 break;
1725 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001726 if (iopl < 3)
1727 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001728 change_mask |= EFLG_IF;
1729 break;
1730 default: /* real mode */
1731 change_mask |= (EFLG_IOPL | EFLG_IF);
1732 break;
1733 }
1734
1735 *(unsigned long *)dest =
1736 (ctxt->eflags & ~change_mask) | (val & change_mask);
1737
1738 return rc;
1739}
1740
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001741static int em_popf(struct x86_emulate_ctxt *ctxt)
1742{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001743 ctxt->dst.type = OP_REG;
1744 ctxt->dst.addr.reg = &ctxt->eflags;
1745 ctxt->dst.bytes = ctxt->op_bytes;
1746 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001747}
1748
Avi Kivity612e89f2012-06-12 20:03:23 +03001749static int em_enter(struct x86_emulate_ctxt *ctxt)
1750{
1751 int rc;
1752 unsigned frame_size = ctxt->src.val;
1753 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001754 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001755
1756 if (nesting_level)
1757 return X86EMUL_UNHANDLEABLE;
1758
Avi Kivitydd856ef2012-08-27 23:46:17 +03001759 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1760 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001761 if (rc != X86EMUL_CONTINUE)
1762 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001763 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001764 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001765 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1766 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001767 stack_mask(ctxt));
1768 return X86EMUL_CONTINUE;
1769}
1770
Avi Kivityf47cfa32012-06-07 17:49:24 +03001771static int em_leave(struct x86_emulate_ctxt *ctxt)
1772{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001773 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001774 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001775 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001776}
1777
Avi Kivity1cd196e2011-09-13 10:45:51 +03001778static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001779{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001780 int seg = ctxt->src2.val;
1781
Avi Kivity9dac77f2011-06-01 15:34:25 +03001782 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001783
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001784 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001785}
1786
Avi Kivity1cd196e2011-09-13 10:45:51 +03001787static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001788{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001789 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001790 unsigned long selector;
1791 int rc;
1792
Avi Kivity9dac77f2011-06-01 15:34:25 +03001793 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001794 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001795 return rc;
1796
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001797 if (ctxt->modrm_reg == VCPU_SREG_SS)
1798 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
1799
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001800 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001801 return rc;
1802}
1803
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001804static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001805{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001806 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001807 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001808 int reg = VCPU_REGS_RAX;
1809
1810 while (reg <= VCPU_REGS_RDI) {
1811 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001812 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001813
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001814 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001815 if (rc != X86EMUL_CONTINUE)
1816 return rc;
1817
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001818 ++reg;
1819 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001820
Wei Yongjunc37eda12010-06-15 09:03:33 +08001821 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001822}
1823
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001824static int em_pushf(struct x86_emulate_ctxt *ctxt)
1825{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001826 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001827 return em_push(ctxt);
1828}
1829
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001830static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001831{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001832 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001833 int reg = VCPU_REGS_RDI;
1834
1835 while (reg >= VCPU_REGS_RAX) {
1836 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001837 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001838 --reg;
1839 }
1840
Avi Kivitydd856ef2012-08-27 23:46:17 +03001841 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001842 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001843 break;
1844 --reg;
1845 }
1846 return rc;
1847}
1848
Avi Kivitydd856ef2012-08-27 23:46:17 +03001849static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001850{
Mathias Krause0225fb52012-08-30 01:30:16 +02001851 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001852 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001853 struct desc_ptr dt;
1854 gva_t cs_addr;
1855 gva_t eip_addr;
1856 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001857
1858 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001859 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001860 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001861 if (rc != X86EMUL_CONTINUE)
1862 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001863
1864 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1865
Avi Kivity9dac77f2011-06-01 15:34:25 +03001866 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001867 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001868 if (rc != X86EMUL_CONTINUE)
1869 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001870
Avi Kivity9dac77f2011-06-01 15:34:25 +03001871 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001872 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001873 if (rc != X86EMUL_CONTINUE)
1874 return rc;
1875
Avi Kivity4bff1e862011-04-20 13:37:53 +03001876 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001877
1878 eip_addr = dt.address + (irq << 2);
1879 cs_addr = dt.address + (irq << 2) + 2;
1880
Avi Kivity0f65dd72011-04-20 13:37:53 +03001881 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001882 if (rc != X86EMUL_CONTINUE)
1883 return rc;
1884
Avi Kivity0f65dd72011-04-20 13:37:53 +03001885 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001886 if (rc != X86EMUL_CONTINUE)
1887 return rc;
1888
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001889 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001890 if (rc != X86EMUL_CONTINUE)
1891 return rc;
1892
Avi Kivity9dac77f2011-06-01 15:34:25 +03001893 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001894
1895 return rc;
1896}
1897
Avi Kivitydd856ef2012-08-27 23:46:17 +03001898int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1899{
1900 int rc;
1901
1902 invalidate_registers(ctxt);
1903 rc = __emulate_int_real(ctxt, irq);
1904 if (rc == X86EMUL_CONTINUE)
1905 writeback_registers(ctxt);
1906 return rc;
1907}
1908
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001909static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001910{
1911 switch(ctxt->mode) {
1912 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001913 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001914 case X86EMUL_MODE_VM86:
1915 case X86EMUL_MODE_PROT16:
1916 case X86EMUL_MODE_PROT32:
1917 case X86EMUL_MODE_PROT64:
1918 default:
1919 /* Protected mode interrupts unimplemented yet */
1920 return X86EMUL_UNHANDLEABLE;
1921 }
1922}
1923
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001924static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001925{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001926 int rc = X86EMUL_CONTINUE;
1927 unsigned long temp_eip = 0;
1928 unsigned long temp_eflags = 0;
1929 unsigned long cs = 0;
1930 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1931 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1932 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1933 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1934
1935 /* TODO: Add stack limit check */
1936
Avi Kivity9dac77f2011-06-01 15:34:25 +03001937 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001938
1939 if (rc != X86EMUL_CONTINUE)
1940 return rc;
1941
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001942 if (temp_eip & ~0xffff)
1943 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001944
Avi Kivity9dac77f2011-06-01 15:34:25 +03001945 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001946
1947 if (rc != X86EMUL_CONTINUE)
1948 return rc;
1949
Avi Kivity9dac77f2011-06-01 15:34:25 +03001950 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001951
1952 if (rc != X86EMUL_CONTINUE)
1953 return rc;
1954
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001955 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001956
1957 if (rc != X86EMUL_CONTINUE)
1958 return rc;
1959
Avi Kivity9dac77f2011-06-01 15:34:25 +03001960 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001961
1962
Avi Kivity9dac77f2011-06-01 15:34:25 +03001963 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001964 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001965 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001966 ctxt->eflags &= ~0xffff;
1967 ctxt->eflags |= temp_eflags;
1968 }
1969
1970 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1971 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1972
1973 return rc;
1974}
1975
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001976static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001977{
1978 switch(ctxt->mode) {
1979 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001980 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001981 case X86EMUL_MODE_VM86:
1982 case X86EMUL_MODE_PROT16:
1983 case X86EMUL_MODE_PROT32:
1984 case X86EMUL_MODE_PROT64:
1985 default:
1986 /* iret from protected mode unimplemented yet */
1987 return X86EMUL_UNHANDLEABLE;
1988 }
1989}
1990
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001991static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1992{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001993 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03001994 unsigned short sel, old_sel;
1995 struct desc_struct old_desc, new_desc;
1996 const struct x86_emulate_ops *ops = ctxt->ops;
1997 u8 cpl = ctxt->ops->cpl(ctxt);
1998
1999 /* Assignment of RIP may only fail in 64-bit mode */
2000 if (ctxt->mode == X86EMUL_MODE_PROT64)
2001 ops->get_segment(ctxt, &old_sel, &old_desc, NULL,
2002 VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002003
Avi Kivity9dac77f2011-06-01 15:34:25 +03002004 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002005
Nadav Amitd1442d82014-09-18 22:39:39 +03002006 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl, false,
2007 &new_desc);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002008 if (rc != X86EMUL_CONTINUE)
2009 return rc;
2010
Nadav Amitd1442d82014-09-18 22:39:39 +03002011 rc = assign_eip_far(ctxt, ctxt->src.val, new_desc.l);
2012 if (rc != X86EMUL_CONTINUE) {
2013 WARN_ON(!ctxt->mode != X86EMUL_MODE_PROT64);
2014 /* assigning eip failed; restore the old cs */
2015 ops->set_segment(ctxt, old_sel, &old_desc, 0, VCPU_SREG_CS);
2016 return rc;
2017 }
2018 return rc;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002019}
2020
Takuya Yoshikawa51187682011-05-02 02:29:17 +09002021static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002022{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002023 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002024
Avi Kivity9dac77f2011-06-01 15:34:25 +03002025 switch (ctxt->modrm_reg) {
Mohammed Gamald19292e2008-09-08 21:47:19 +03002026 case 2: /* call near abs */ {
2027 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002028 old_eip = ctxt->_eip;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002029 rc = assign_eip_near(ctxt, ctxt->src.val);
2030 if (rc != X86EMUL_CONTINUE)
2031 break;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002032 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002033 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03002034 break;
2035 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002036 case 4: /* jmp abs */
Nadav Amit234f3ce2014-09-18 22:39:38 +03002037 rc = assign_eip_near(ctxt, ctxt->src.val);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002038 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002039 case 5: /* jmp far */
2040 rc = em_jmp_far(ctxt);
2041 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002042 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002043 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002044 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002045 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002046 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002047}
2048
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002049static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002050{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002051 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002052
Nadav Amitaaa05f22014-06-02 18:34:10 +03002053 if (ctxt->dst.bytes == 16)
2054 return X86EMUL_UNHANDLEABLE;
2055
Avi Kivitydd856ef2012-08-27 23:46:17 +03002056 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2057 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2058 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2059 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002060 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002061 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002062 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2063 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002064
Laurent Vivier05f086f2007-09-24 11:10:55 +02002065 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002066 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002067 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002068}
2069
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002070static int em_ret(struct x86_emulate_ctxt *ctxt)
2071{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002072 int rc;
2073 unsigned long eip;
2074
2075 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
2076 if (rc != X86EMUL_CONTINUE)
2077 return rc;
2078
2079 return assign_eip_near(ctxt, eip);
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002080}
2081
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002082static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002083{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002084 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002085 unsigned long eip, cs;
2086 u16 old_cs;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002087 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amitd1442d82014-09-18 22:39:39 +03002088 struct desc_struct old_desc, new_desc;
2089 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002090
Nadav Amitd1442d82014-09-18 22:39:39 +03002091 if (ctxt->mode == X86EMUL_MODE_PROT64)
2092 ops->get_segment(ctxt, &old_cs, &old_desc, NULL,
2093 VCPU_SREG_CS);
2094
2095 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002096 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002097 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002098 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002099 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002100 return rc;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002101 /* Outer-privilege level return is not implemented */
2102 if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2103 return X86EMUL_UNHANDLEABLE;
Nadav Amitd1442d82014-09-18 22:39:39 +03002104 rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, 0, false,
2105 &new_desc);
2106 if (rc != X86EMUL_CONTINUE)
2107 return rc;
2108 rc = assign_eip_far(ctxt, eip, new_desc.l);
2109 if (rc != X86EMUL_CONTINUE) {
2110 WARN_ON(!ctxt->mode != X86EMUL_MODE_PROT64);
2111 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
2112 }
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002113 return rc;
2114}
2115
Bruce Rogers32611072013-09-09 09:40:20 -06002116static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2117{
2118 int rc;
2119
2120 rc = em_ret_far(ctxt);
2121 if (rc != X86EMUL_CONTINUE)
2122 return rc;
2123 rsp_increment(ctxt, ctxt->src.val);
2124 return X86EMUL_CONTINUE;
2125}
2126
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002127static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2128{
2129 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002130 ctxt->dst.orig_val = ctxt->dst.val;
2131 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002132 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002133 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002134 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002135
2136 if (ctxt->eflags & EFLG_ZF) {
2137 /* Success: write back to memory. */
2138 ctxt->dst.val = ctxt->src.orig_val;
2139 } else {
2140 /* Failure: write the value we saw to EAX. */
2141 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002142 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Nadav Amit37c564f2014-06-02 18:34:07 +03002143 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002144 }
2145 return X86EMUL_CONTINUE;
2146}
2147
Avi Kivityd4b43252011-09-13 10:45:50 +03002148static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002149{
Avi Kivityd4b43252011-09-13 10:45:50 +03002150 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002151 unsigned short sel;
2152 int rc;
2153
Avi Kivity9dac77f2011-06-01 15:34:25 +03002154 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002155
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002156 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002157 if (rc != X86EMUL_CONTINUE)
2158 return rc;
2159
Avi Kivity9dac77f2011-06-01 15:34:25 +03002160 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002161 return rc;
2162}
2163
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002164static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002165setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002166 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002167{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002168 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002169 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002170 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002171 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002172 cs->type = 0x0b; /* Read, Execute, Accessed */
2173 cs->s = 1;
2174 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002175 cs->p = 1;
2176 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002177 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002178
Gleb Natapov79168fd2010-04-28 19:15:30 +03002179 set_desc_base(ss, 0); /* flat segment */
2180 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002181 ss->g = 1; /* 4kb granularity */
2182 ss->s = 1;
2183 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002184 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002185 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002186 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002187 ss->l = 0;
2188 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002189}
2190
Avi Kivity1a18a692012-02-01 12:23:21 +02002191static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2192{
2193 u32 eax, ebx, ecx, edx;
2194
2195 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002196 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2197 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002198 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2199 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2200}
2201
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002202static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2203{
Mathias Krause0225fb52012-08-30 01:30:16 +02002204 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002205 u32 eax, ebx, ecx, edx;
2206
2207 /*
2208 * syscall should always be enabled in longmode - so only become
2209 * vendor specific (cpuid) if other modes are active...
2210 */
2211 if (ctxt->mode == X86EMUL_MODE_PROT64)
2212 return true;
2213
2214 eax = 0x00000000;
2215 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002216 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2217 /*
2218 * Intel ("GenuineIntel")
2219 * remark: Intel CPUs only support "syscall" in 64bit
2220 * longmode. Also an 64bit guest with a
2221 * 32bit compat-app running will #UD !! While this
2222 * behaviour can be fixed (by emulating) into AMD
2223 * response - CPUs of AMD can't behave like Intel.
2224 */
2225 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2226 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2227 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2228 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002229
Avi Kivity0017f932012-06-07 14:10:16 +03002230 /* AMD ("AuthenticAMD") */
2231 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2232 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2233 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2234 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002235
Avi Kivity0017f932012-06-07 14:10:16 +03002236 /* AMD ("AMDisbetter!") */
2237 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2238 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2239 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2240 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002241
2242 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2243 return false;
2244}
2245
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002246static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002247{
Mathias Krause0225fb52012-08-30 01:30:16 +02002248 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002249 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002250 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002251 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002252 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002253
2254 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002255 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002256 ctxt->mode == X86EMUL_MODE_VM86)
2257 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002258
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002259 if (!(em_syscall_is_enabled(ctxt)))
2260 return emulate_ud(ctxt);
2261
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002262 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002263 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002264
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002265 if (!(efer & EFER_SCE))
2266 return emulate_ud(ctxt);
2267
Avi Kivity717746e2011-04-20 13:37:53 +03002268 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002269 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002270 cs_sel = (u16)(msr_data & 0xfffc);
2271 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002272
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002273 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002274 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002275 cs.l = 1;
2276 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002277 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2278 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002279
Avi Kivitydd856ef2012-08-27 23:46:17 +03002280 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002281 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002282#ifdef CONFIG_X86_64
Nadav Amit6c6cb692014-07-21 14:37:30 +03002283 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002284
Avi Kivity717746e2011-04-20 13:37:53 +03002285 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002286 ctxt->mode == X86EMUL_MODE_PROT64 ?
2287 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002288 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002289
Avi Kivity717746e2011-04-20 13:37:53 +03002290 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Nadav Amit6c6cb692014-07-21 14:37:30 +03002291 ctxt->eflags &= ~msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002292#endif
2293 } else {
2294 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002295 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002296 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002297
Nadav Amit6c6cb692014-07-21 14:37:30 +03002298 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002299 }
2300
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002301 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002302}
2303
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002304static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002305{
Mathias Krause0225fb52012-08-30 01:30:16 +02002306 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002307 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002308 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002309 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002310 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002311
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002312 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002313 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002314 if (ctxt->mode == X86EMUL_MODE_REAL)
2315 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002316
Avi Kivity1a18a692012-02-01 12:23:21 +02002317 /*
2318 * Not recognized on AMD in compat mode (but is recognized in legacy
2319 * mode).
2320 */
2321 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2322 && !vendor_intel(ctxt))
2323 return emulate_ud(ctxt);
2324
Andre Przywara8c604352009-06-18 12:56:01 +02002325 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2326 * Therefore, we inject an #UD.
2327 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002328 if (ctxt->mode == X86EMUL_MODE_PROT64)
2329 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002330
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002331 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002332
Avi Kivity717746e2011-04-20 13:37:53 +03002333 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002334 switch (ctxt->mode) {
2335 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002336 if ((msr_data & 0xfffc) == 0x0)
2337 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002338 break;
2339 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002340 if (msr_data == 0x0)
2341 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002342 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002343 default:
2344 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002345 }
2346
Nadav Amit6c6cb692014-07-21 14:37:30 +03002347 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002348 cs_sel = (u16)msr_data;
2349 cs_sel &= ~SELECTOR_RPL_MASK;
2350 ss_sel = cs_sel + 8;
2351 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002352 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002353 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002354 cs.l = 1;
2355 }
2356
Avi Kivity1aa36612011-04-27 13:20:30 +03002357 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2358 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002359
Avi Kivity717746e2011-04-20 13:37:53 +03002360 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002361 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002362
Avi Kivity717746e2011-04-20 13:37:53 +03002363 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002364 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002365
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002366 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002367}
2368
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002369static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002370{
Mathias Krause0225fb52012-08-30 01:30:16 +02002371 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002372 struct desc_struct cs, ss;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002373 u64 msr_data, rcx, rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002374 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002375 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002376
Gleb Natapova0044752010-02-10 14:21:31 +02002377 /* inject #GP if in real mode or Virtual 8086 mode */
2378 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002379 ctxt->mode == X86EMUL_MODE_VM86)
2380 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002381
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002382 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002383
Avi Kivity9dac77f2011-06-01 15:34:25 +03002384 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002385 usermode = X86EMUL_MODE_PROT64;
2386 else
2387 usermode = X86EMUL_MODE_PROT32;
2388
Nadav Amit234f3ce2014-09-18 22:39:38 +03002389 rcx = reg_read(ctxt, VCPU_REGS_RCX);
2390 rdx = reg_read(ctxt, VCPU_REGS_RDX);
2391
Andre Przywara4668f052009-06-18 12:56:02 +02002392 cs.dpl = 3;
2393 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002394 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002395 switch (usermode) {
2396 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002397 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002398 if ((msr_data & 0xfffc) == 0x0)
2399 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002400 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002401 break;
2402 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002403 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002404 if (msr_data == 0x0)
2405 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002406 ss_sel = cs_sel + 8;
2407 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002408 cs.l = 1;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002409 if (is_noncanonical_address(rcx) ||
2410 is_noncanonical_address(rdx))
2411 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002412 break;
2413 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002414 cs_sel |= SELECTOR_RPL_MASK;
2415 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002416
Avi Kivity1aa36612011-04-27 13:20:30 +03002417 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2418 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002419
Nadav Amit234f3ce2014-09-18 22:39:38 +03002420 ctxt->_eip = rdx;
2421 *reg_write(ctxt, VCPU_REGS_RSP) = rcx;
Andre Przywara4668f052009-06-18 12:56:02 +02002422
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002423 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002424}
2425
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002426static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002427{
2428 int iopl;
2429 if (ctxt->mode == X86EMUL_MODE_REAL)
2430 return false;
2431 if (ctxt->mode == X86EMUL_MODE_VM86)
2432 return true;
2433 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002434 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002435}
2436
2437static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002438 u16 port, u16 len)
2439{
Mathias Krause0225fb52012-08-30 01:30:16 +02002440 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002441 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002442 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002443 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002444 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002445 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002446 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002447
Avi Kivity1aa36612011-04-27 13:20:30 +03002448 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002449 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002450 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002451 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002452 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002453 base = get_desc_base(&tr_seg);
2454#ifdef CONFIG_X86_64
2455 base |= ((u64)base3) << 32;
2456#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002457 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002458 if (r != X86EMUL_CONTINUE)
2459 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002460 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002461 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002462 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002463 if (r != X86EMUL_CONTINUE)
2464 return false;
2465 if ((perm >> bit_idx) & mask)
2466 return false;
2467 return true;
2468}
2469
2470static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002471 u16 port, u16 len)
2472{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002473 if (ctxt->perm_ok)
2474 return true;
2475
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002476 if (emulator_bad_iopl(ctxt))
2477 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002478 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002479
2480 ctxt->perm_ok = true;
2481
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002482 return true;
2483}
2484
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002485static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002486 struct tss_segment_16 *tss)
2487{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002488 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002489 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002490 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2491 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2492 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2493 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2494 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2495 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2496 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2497 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002498
Avi Kivity1aa36612011-04-27 13:20:30 +03002499 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2500 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2501 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2502 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2503 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002504}
2505
2506static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002507 struct tss_segment_16 *tss)
2508{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002509 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002510 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002511
Avi Kivity9dac77f2011-06-01 15:34:25 +03002512 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002513 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002514 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2515 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2516 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2517 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2518 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2519 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2520 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2521 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002522
2523 /*
2524 * SDM says that segment selectors are loaded before segment
2525 * descriptors
2526 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002527 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2528 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2529 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2530 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2531 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002532
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002533 cpl = tss->cs & 3;
2534
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002535 /*
Guo Chaofc058682012-06-28 15:19:51 +08002536 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002537 * it is handled in a context of new task
2538 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002539 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl,
2540 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002541 if (ret != X86EMUL_CONTINUE)
2542 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002543 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
2544 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002545 if (ret != X86EMUL_CONTINUE)
2546 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002547 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
2548 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002549 if (ret != X86EMUL_CONTINUE)
2550 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002551 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
2552 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002553 if (ret != X86EMUL_CONTINUE)
2554 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002555 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
2556 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002557 if (ret != X86EMUL_CONTINUE)
2558 return ret;
2559
2560 return X86EMUL_CONTINUE;
2561}
2562
2563static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002564 u16 tss_selector, u16 old_tss_sel,
2565 ulong old_tss_base, struct desc_struct *new_desc)
2566{
Mathias Krause0225fb52012-08-30 01:30:16 +02002567 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002568 struct tss_segment_16 tss_seg;
2569 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002570 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002571
Avi Kivity0f65dd72011-04-20 13:37:53 +03002572 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002573 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002574 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002575 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002576 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002577
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002578 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002579
Avi Kivity0f65dd72011-04-20 13:37:53 +03002580 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002581 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002582 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002583 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002584 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002585
Avi Kivity0f65dd72011-04-20 13:37:53 +03002586 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002587 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002588 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002589 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002590 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002591
2592 if (old_tss_sel != 0xffff) {
2593 tss_seg.prev_task_link = old_tss_sel;
2594
Avi Kivity0f65dd72011-04-20 13:37:53 +03002595 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002596 &tss_seg.prev_task_link,
2597 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002598 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002599 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002600 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002601 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002602 }
2603
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002604 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002605}
2606
2607static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002608 struct tss_segment_32 *tss)
2609{
Nadav Amit5c7411e2014-04-07 18:37:47 +03002610 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002611 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002612 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002613 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2614 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2615 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2616 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2617 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2618 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2619 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2620 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002621
Avi Kivity1aa36612011-04-27 13:20:30 +03002622 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2623 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2624 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2625 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2626 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2627 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002628}
2629
2630static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002631 struct tss_segment_32 *tss)
2632{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002633 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002634 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002635
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002636 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002637 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002638 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002639 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002640
2641 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002642 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2643 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2644 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2645 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2646 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2647 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2648 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2649 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002650
2651 /*
2652 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002653 * descriptors. This is important because CPL checks will
2654 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002655 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002656 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2657 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2658 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2659 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2660 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2661 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2662 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002663
2664 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002665 * If we're switching between Protected Mode and VM86, we need to make
2666 * sure to update the mode before loading the segment descriptors so
2667 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01002668 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002669 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002670 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002671 cpl = 3;
2672 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002673 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002674 cpl = tss->cs & 3;
2675 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01002676
2677 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002678 * Now load segment descriptors. If fault happenes at this stage
2679 * it is handled in a context of new task
2680 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002681 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR,
2682 cpl, true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002683 if (ret != X86EMUL_CONTINUE)
2684 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002685 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
2686 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002687 if (ret != X86EMUL_CONTINUE)
2688 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002689 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
2690 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002691 if (ret != X86EMUL_CONTINUE)
2692 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002693 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
2694 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002695 if (ret != X86EMUL_CONTINUE)
2696 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002697 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
2698 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002699 if (ret != X86EMUL_CONTINUE)
2700 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002701 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl,
2702 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002703 if (ret != X86EMUL_CONTINUE)
2704 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002705 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl,
2706 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002707 if (ret != X86EMUL_CONTINUE)
2708 return ret;
2709
2710 return X86EMUL_CONTINUE;
2711}
2712
2713static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002714 u16 tss_selector, u16 old_tss_sel,
2715 ulong old_tss_base, struct desc_struct *new_desc)
2716{
Mathias Krause0225fb52012-08-30 01:30:16 +02002717 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002718 struct tss_segment_32 tss_seg;
2719 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002720 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03002721 u32 eip_offset = offsetof(struct tss_segment_32, eip);
2722 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002723
Avi Kivity0f65dd72011-04-20 13:37:53 +03002724 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002725 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002726 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002727 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002728 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002729
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002730 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002731
Nadav Amit5c7411e2014-04-07 18:37:47 +03002732 /* Only GP registers and segment selectors are saved */
2733 ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
2734 ldt_sel_offset - eip_offset, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002735 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002736 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002737 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002738
Avi Kivity0f65dd72011-04-20 13:37:53 +03002739 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002740 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002741 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002742 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002743 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002744
2745 if (old_tss_sel != 0xffff) {
2746 tss_seg.prev_task_link = old_tss_sel;
2747
Avi Kivity0f65dd72011-04-20 13:37:53 +03002748 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002749 &tss_seg.prev_task_link,
2750 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002751 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002752 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002753 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002754 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002755 }
2756
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002757 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002758}
2759
2760static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002761 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002762 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002763{
Mathias Krause0225fb52012-08-30 01:30:16 +02002764 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002765 struct desc_struct curr_tss_desc, next_tss_desc;
2766 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002767 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002768 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002769 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002770 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002771 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002772
2773 /* FIXME: old_tss_base == ~0 ? */
2774
Avi Kivitye9194642012-06-13 16:29:39 +03002775 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002776 if (ret != X86EMUL_CONTINUE)
2777 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002778 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002779 if (ret != X86EMUL_CONTINUE)
2780 return ret;
2781
2782 /* FIXME: check that next_tss_desc is tss */
2783
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002784 /*
2785 * Check privileges. The three cases are task switch caused by...
2786 *
2787 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2788 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002789 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002790 */
2791 if (reason == TASK_SWITCH_GATE) {
2792 if (idt_index != -1) {
2793 /* Software interrupts */
2794 struct desc_struct task_gate_desc;
2795 int dpl;
2796
2797 ret = read_interrupt_descriptor(ctxt, idt_index,
2798 &task_gate_desc);
2799 if (ret != X86EMUL_CONTINUE)
2800 return ret;
2801
2802 dpl = task_gate_desc.dpl;
2803 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2804 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2805 }
2806 } else if (reason != TASK_SWITCH_IRET) {
2807 int dpl = next_tss_desc.dpl;
2808 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2809 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002810 }
2811
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002812
Gleb Natapovceffb452010-03-18 15:20:19 +02002813 desc_limit = desc_limit_scaled(&next_tss_desc);
2814 if (!next_tss_desc.p ||
2815 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2816 desc_limit < 0x2b)) {
Paolo Bonzini592f0852014-08-20 10:05:08 +02002817 return emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002818 }
2819
2820 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2821 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002822 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002823 }
2824
2825 if (reason == TASK_SWITCH_IRET)
2826 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2827
2828 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002829 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002830 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2831 old_tss_sel = 0xffff;
2832
2833 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002834 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002835 old_tss_base, &next_tss_desc);
2836 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002837 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002838 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002839 if (ret != X86EMUL_CONTINUE)
2840 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002841
2842 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2843 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2844
2845 if (reason != TASK_SWITCH_IRET) {
2846 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002847 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002848 }
2849
Avi Kivity717746e2011-04-20 13:37:53 +03002850 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002851 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002852
Jan Kiszkae269fb22010-04-14 15:51:09 +02002853 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002854 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2855 ctxt->lock_prefix = 0;
2856 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002857 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002858 }
2859
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002860 return ret;
2861}
2862
2863int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002864 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002865 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002866{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002867 int rc;
2868
Avi Kivitydd856ef2012-08-27 23:46:17 +03002869 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002870 ctxt->_eip = ctxt->eip;
2871 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002872
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002873 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002874 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002875
Avi Kivitydd856ef2012-08-27 23:46:17 +03002876 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002877 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002878 writeback_registers(ctxt);
2879 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002880
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002881 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002882}
2883
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002884static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2885 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002886{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002887 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002888
Avi Kivitydd856ef2012-08-27 23:46:17 +03002889 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2890 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002891}
2892
Avi Kivity7af04fc2010-08-18 14:16:35 +03002893static int em_das(struct x86_emulate_ctxt *ctxt)
2894{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002895 u8 al, old_al;
2896 bool af, cf, old_cf;
2897
2898 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002899 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002900
2901 old_al = al;
2902 old_cf = cf;
2903 cf = false;
2904 af = ctxt->eflags & X86_EFLAGS_AF;
2905 if ((al & 0x0f) > 9 || af) {
2906 al -= 6;
2907 cf = old_cf | (al >= 250);
2908 af = true;
2909 } else {
2910 af = false;
2911 }
2912 if (old_al > 0x99 || old_cf) {
2913 al -= 0x60;
2914 cf = true;
2915 }
2916
Avi Kivity9dac77f2011-06-01 15:34:25 +03002917 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002918 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002919 ctxt->src.type = OP_IMM;
2920 ctxt->src.val = 0;
2921 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02002922 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03002923 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2924 if (cf)
2925 ctxt->eflags |= X86_EFLAGS_CF;
2926 if (af)
2927 ctxt->eflags |= X86_EFLAGS_AF;
2928 return X86EMUL_CONTINUE;
2929}
2930
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02002931static int em_aam(struct x86_emulate_ctxt *ctxt)
2932{
2933 u8 al, ah;
2934
2935 if (ctxt->src.val == 0)
2936 return emulate_de(ctxt);
2937
2938 al = ctxt->dst.val & 0xff;
2939 ah = al / ctxt->src.val;
2940 al %= ctxt->src.val;
2941
2942 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
2943
2944 /* Set PF, ZF, SF */
2945 ctxt->src.type = OP_IMM;
2946 ctxt->src.val = 0;
2947 ctxt->src.bytes = 1;
2948 fastop(ctxt, em_or);
2949
2950 return X86EMUL_CONTINUE;
2951}
2952
Gleb Natapov7f662272012-12-10 11:42:30 +02002953static int em_aad(struct x86_emulate_ctxt *ctxt)
2954{
2955 u8 al = ctxt->dst.val & 0xff;
2956 u8 ah = (ctxt->dst.val >> 8) & 0xff;
2957
2958 al = (al + (ah * ctxt->src.val)) & 0xff;
2959
2960 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
2961
Gleb Natapovf583c292013-02-13 17:50:39 +02002962 /* Set PF, ZF, SF */
2963 ctxt->src.type = OP_IMM;
2964 ctxt->src.val = 0;
2965 ctxt->src.bytes = 1;
2966 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02002967
2968 return X86EMUL_CONTINUE;
2969}
2970
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002971static int em_call(struct x86_emulate_ctxt *ctxt)
2972{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002973 int rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002974 long rel = ctxt->src.val;
2975
2976 ctxt->src.val = (unsigned long)ctxt->_eip;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002977 rc = jmp_rel(ctxt, rel);
2978 if (rc != X86EMUL_CONTINUE)
2979 return rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002980 return em_push(ctxt);
2981}
2982
Avi Kivity0ef753b2010-08-18 14:51:45 +03002983static int em_call_far(struct x86_emulate_ctxt *ctxt)
2984{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002985 u16 sel, old_cs;
2986 ulong old_eip;
2987 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002988 struct desc_struct old_desc, new_desc;
2989 const struct x86_emulate_ops *ops = ctxt->ops;
2990 int cpl = ctxt->ops->cpl(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002991
Avi Kivity9dac77f2011-06-01 15:34:25 +03002992 old_eip = ctxt->_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03002993 ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002994
Avi Kivity9dac77f2011-06-01 15:34:25 +03002995 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Nadav Amitd1442d82014-09-18 22:39:39 +03002996 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl, false,
2997 &new_desc);
2998 if (rc != X86EMUL_CONTINUE)
Avi Kivity0ef753b2010-08-18 14:51:45 +03002999 return X86EMUL_CONTINUE;
3000
Nadav Amitd1442d82014-09-18 22:39:39 +03003001 rc = assign_eip_far(ctxt, ctxt->src.val, new_desc.l);
3002 if (rc != X86EMUL_CONTINUE)
3003 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003004
Avi Kivity9dac77f2011-06-01 15:34:25 +03003005 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003006 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003007 if (rc != X86EMUL_CONTINUE)
Nadav Amitd1442d82014-09-18 22:39:39 +03003008 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003009
Avi Kivity9dac77f2011-06-01 15:34:25 +03003010 ctxt->src.val = old_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003011 rc = em_push(ctxt);
3012 /* If we failed, we tainted the memory, but the very least we should
3013 restore cs */
3014 if (rc != X86EMUL_CONTINUE)
3015 goto fail;
3016 return rc;
3017fail:
3018 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
3019 return rc;
3020
Avi Kivity0ef753b2010-08-18 14:51:45 +03003021}
3022
Avi Kivity40ece7c2010-08-18 15:12:09 +03003023static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3024{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003025 int rc;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003026 unsigned long eip;
Avi Kivity40ece7c2010-08-18 15:12:09 +03003027
Nadav Amit234f3ce2014-09-18 22:39:38 +03003028 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
3029 if (rc != X86EMUL_CONTINUE)
3030 return rc;
3031 rc = assign_eip_near(ctxt, eip);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003032 if (rc != X86EMUL_CONTINUE)
3033 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003034 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003035 return X86EMUL_CONTINUE;
3036}
3037
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003038static int em_xchg(struct x86_emulate_ctxt *ctxt)
3039{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003040 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003041 ctxt->src.val = ctxt->dst.val;
3042 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003043
3044 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003045 ctxt->dst.val = ctxt->src.orig_val;
3046 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003047 return X86EMUL_CONTINUE;
3048}
3049
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003050static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3051{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003052 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003053 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003054}
3055
Avi Kivity61429142010-08-19 15:13:00 +03003056static int em_cwd(struct x86_emulate_ctxt *ctxt)
3057{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003058 ctxt->dst.type = OP_REG;
3059 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003060 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003061 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003062
3063 return X86EMUL_CONTINUE;
3064}
3065
Avi Kivity48bb5d32010-08-18 18:54:34 +03003066static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3067{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003068 u64 tsc = 0;
3069
Avi Kivity717746e2011-04-20 13:37:53 +03003070 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003071 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3072 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003073 return X86EMUL_CONTINUE;
3074}
3075
Avi Kivity222d21a2011-11-10 14:57:30 +02003076static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3077{
3078 u64 pmc;
3079
Avi Kivitydd856ef2012-08-27 23:46:17 +03003080 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003081 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003082 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3083 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003084 return X86EMUL_CONTINUE;
3085}
3086
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003087static int em_mov(struct x86_emulate_ctxt *ctxt)
3088{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01003089 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003090 return X86EMUL_CONTINUE;
3091}
3092
Borislav Petkov84cffe42013-10-29 12:54:56 +01003093#define FFL(x) bit(X86_FEATURE_##x)
3094
3095static int em_movbe(struct x86_emulate_ctxt *ctxt)
3096{
3097 u32 ebx, ecx, edx, eax = 1;
3098 u16 tmp;
3099
3100 /*
3101 * Check MOVBE is set in the guest-visible CPUID leaf.
3102 */
3103 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
3104 if (!(ecx & FFL(MOVBE)))
3105 return emulate_ud(ctxt);
3106
3107 switch (ctxt->op_bytes) {
3108 case 2:
3109 /*
3110 * From MOVBE definition: "...When the operand size is 16 bits,
3111 * the upper word of the destination register remains unchanged
3112 * ..."
3113 *
3114 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3115 * rules so we have to do the operation almost per hand.
3116 */
3117 tmp = (u16)ctxt->src.val;
3118 ctxt->dst.val &= ~0xffffUL;
3119 ctxt->dst.val |= (unsigned long)swab16(tmp);
3120 break;
3121 case 4:
3122 ctxt->dst.val = swab32((u32)ctxt->src.val);
3123 break;
3124 case 8:
3125 ctxt->dst.val = swab64(ctxt->src.val);
3126 break;
3127 default:
Paolo Bonzini592f0852014-08-20 10:05:08 +02003128 BUG();
Borislav Petkov84cffe42013-10-29 12:54:56 +01003129 }
3130 return X86EMUL_CONTINUE;
3131}
3132
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003133static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3134{
3135 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3136 return emulate_gp(ctxt, 0);
3137
3138 /* Disable writeback. */
3139 ctxt->dst.type = OP_NONE;
3140 return X86EMUL_CONTINUE;
3141}
3142
3143static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3144{
3145 unsigned long val;
3146
3147 if (ctxt->mode == X86EMUL_MODE_PROT64)
3148 val = ctxt->src.val & ~0ULL;
3149 else
3150 val = ctxt->src.val & ~0U;
3151
3152 /* #UD condition is already handled. */
3153 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3154 return emulate_gp(ctxt, 0);
3155
3156 /* Disable writeback. */
3157 ctxt->dst.type = OP_NONE;
3158 return X86EMUL_CONTINUE;
3159}
3160
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003161static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3162{
3163 u64 msr_data;
3164
Avi Kivitydd856ef2012-08-27 23:46:17 +03003165 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3166 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3167 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003168 return emulate_gp(ctxt, 0);
3169
3170 return X86EMUL_CONTINUE;
3171}
3172
3173static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3174{
3175 u64 msr_data;
3176
Avi Kivitydd856ef2012-08-27 23:46:17 +03003177 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003178 return emulate_gp(ctxt, 0);
3179
Avi Kivitydd856ef2012-08-27 23:46:17 +03003180 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3181 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003182 return X86EMUL_CONTINUE;
3183}
3184
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003185static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3186{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003187 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003188 return emulate_ud(ctxt);
3189
Avi Kivity9dac77f2011-06-01 15:34:25 +03003190 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003191 return X86EMUL_CONTINUE;
3192}
3193
3194static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3195{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003196 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003197
Avi Kivity9dac77f2011-06-01 15:34:25 +03003198 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003199 return emulate_ud(ctxt);
3200
Avi Kivity9dac77f2011-06-01 15:34:25 +03003201 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003202 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3203
3204 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003205 ctxt->dst.type = OP_NONE;
3206 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003207}
3208
Avi Kivitya14e5792012-06-13 12:28:33 +03003209static int em_lldt(struct x86_emulate_ctxt *ctxt)
3210{
3211 u16 sel = ctxt->src.val;
3212
3213 /* Disable writeback. */
3214 ctxt->dst.type = OP_NONE;
3215 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3216}
3217
Avi Kivity80890002012-06-13 16:33:29 +03003218static int em_ltr(struct x86_emulate_ctxt *ctxt)
3219{
3220 u16 sel = ctxt->src.val;
3221
3222 /* Disable writeback. */
3223 ctxt->dst.type = OP_NONE;
3224 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3225}
3226
Avi Kivity38503912011-03-31 18:48:09 +02003227static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3228{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003229 int rc;
3230 ulong linear;
3231
Avi Kivity9dac77f2011-06-01 15:34:25 +03003232 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003233 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003234 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003235 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003236 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003237 return X86EMUL_CONTINUE;
3238}
3239
Avi Kivity2d04a052011-04-20 15:32:49 +03003240static int em_clts(struct x86_emulate_ctxt *ctxt)
3241{
3242 ulong cr0;
3243
3244 cr0 = ctxt->ops->get_cr(ctxt, 0);
3245 cr0 &= ~X86_CR0_TS;
3246 ctxt->ops->set_cr(ctxt, 0, cr0);
3247 return X86EMUL_CONTINUE;
3248}
3249
Avi Kivity26d05cc2011-04-21 12:07:59 +03003250static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3251{
Nadav Amit0f54a322014-08-29 11:26:55 +03003252 int rc = ctxt->ops->fix_hypercall(ctxt);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003253
Avi Kivity26d05cc2011-04-21 12:07:59 +03003254 if (rc != X86EMUL_CONTINUE)
3255 return rc;
3256
3257 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003258 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003259 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003260 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003261 return X86EMUL_CONTINUE;
3262}
3263
Avi Kivity96051572012-06-10 17:21:18 +03003264static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3265 void (*get)(struct x86_emulate_ctxt *ctxt,
3266 struct desc_ptr *ptr))
3267{
3268 struct desc_ptr desc_ptr;
3269
3270 if (ctxt->mode == X86EMUL_MODE_PROT64)
3271 ctxt->op_bytes = 8;
3272 get(ctxt, &desc_ptr);
3273 if (ctxt->op_bytes == 2) {
3274 ctxt->op_bytes = 4;
3275 desc_ptr.address &= 0x00ffffff;
3276 }
3277 /* Disable writeback. */
3278 ctxt->dst.type = OP_NONE;
3279 return segmented_write(ctxt, ctxt->dst.addr.mem,
3280 &desc_ptr, 2 + ctxt->op_bytes);
3281}
3282
3283static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3284{
3285 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3286}
3287
3288static int em_sidt(struct x86_emulate_ctxt *ctxt)
3289{
3290 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3291}
3292
Avi Kivity26d05cc2011-04-21 12:07:59 +03003293static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3294{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003295 struct desc_ptr desc_ptr;
3296 int rc;
3297
Avi Kivity510425f2012-06-07 17:04:36 +03003298 if (ctxt->mode == X86EMUL_MODE_PROT64)
3299 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003300 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003301 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003302 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003303 if (rc != X86EMUL_CONTINUE)
3304 return rc;
3305 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3306 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003307 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003308 return X86EMUL_CONTINUE;
3309}
3310
Avi Kivity5ef39c72011-04-21 12:21:50 +03003311static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003312{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003313 int rc;
3314
Avi Kivity5ef39c72011-04-21 12:21:50 +03003315 rc = ctxt->ops->fix_hypercall(ctxt);
3316
Avi Kivity26d05cc2011-04-21 12:07:59 +03003317 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003318 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003319 return rc;
3320}
3321
3322static int em_lidt(struct x86_emulate_ctxt *ctxt)
3323{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003324 struct desc_ptr desc_ptr;
3325 int rc;
3326
Avi Kivity510425f2012-06-07 17:04:36 +03003327 if (ctxt->mode == X86EMUL_MODE_PROT64)
3328 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003329 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003330 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003331 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003332 if (rc != X86EMUL_CONTINUE)
3333 return rc;
3334 ctxt->ops->set_idt(ctxt, &desc_ptr);
3335 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003336 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003337 return X86EMUL_CONTINUE;
3338}
3339
3340static int em_smsw(struct x86_emulate_ctxt *ctxt)
3341{
Nadav Amit32e94d02014-06-02 18:34:11 +03003342 if (ctxt->dst.type == OP_MEM)
3343 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003344 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003345 return X86EMUL_CONTINUE;
3346}
3347
3348static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3349{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003350 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003351 | (ctxt->src.val & 0x0f));
3352 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003353 return X86EMUL_CONTINUE;
3354}
3355
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003356static int em_loop(struct x86_emulate_ctxt *ctxt)
3357{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003358 int rc = X86EMUL_CONTINUE;
3359
Avi Kivitydd856ef2012-08-27 23:46:17 +03003360 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3361 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003362 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
Nadav Amit234f3ce2014-09-18 22:39:38 +03003363 rc = jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003364
Nadav Amit234f3ce2014-09-18 22:39:38 +03003365 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003366}
3367
3368static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3369{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003370 int rc = X86EMUL_CONTINUE;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003371
Nadav Amit234f3ce2014-09-18 22:39:38 +03003372 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
3373 rc = jmp_rel(ctxt, ctxt->src.val);
3374
3375 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003376}
3377
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003378static int em_in(struct x86_emulate_ctxt *ctxt)
3379{
3380 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3381 &ctxt->dst.val))
3382 return X86EMUL_IO_NEEDED;
3383
3384 return X86EMUL_CONTINUE;
3385}
3386
3387static int em_out(struct x86_emulate_ctxt *ctxt)
3388{
3389 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3390 &ctxt->src.val, 1);
3391 /* Disable writeback. */
3392 ctxt->dst.type = OP_NONE;
3393 return X86EMUL_CONTINUE;
3394}
3395
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003396static int em_cli(struct x86_emulate_ctxt *ctxt)
3397{
3398 if (emulator_bad_iopl(ctxt))
3399 return emulate_gp(ctxt, 0);
3400
3401 ctxt->eflags &= ~X86_EFLAGS_IF;
3402 return X86EMUL_CONTINUE;
3403}
3404
3405static int em_sti(struct x86_emulate_ctxt *ctxt)
3406{
3407 if (emulator_bad_iopl(ctxt))
3408 return emulate_gp(ctxt, 0);
3409
3410 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3411 ctxt->eflags |= X86_EFLAGS_IF;
3412 return X86EMUL_CONTINUE;
3413}
3414
Avi Kivity6d6eede2012-06-07 14:11:36 +03003415static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3416{
3417 u32 eax, ebx, ecx, edx;
3418
Avi Kivitydd856ef2012-08-27 23:46:17 +03003419 eax = reg_read(ctxt, VCPU_REGS_RAX);
3420 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003421 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003422 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3423 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3424 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3425 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003426 return X86EMUL_CONTINUE;
3427}
3428
Paolo Bonzini98f73632013-10-31 11:19:42 +01003429static int em_sahf(struct x86_emulate_ctxt *ctxt)
3430{
3431 u32 flags;
3432
3433 flags = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF;
3434 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3435
3436 ctxt->eflags &= ~0xffUL;
3437 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3438 return X86EMUL_CONTINUE;
3439}
3440
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003441static int em_lahf(struct x86_emulate_ctxt *ctxt)
3442{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003443 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3444 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003445 return X86EMUL_CONTINUE;
3446}
3447
Avi Kivity92998362012-06-13 12:25:06 +03003448static int em_bswap(struct x86_emulate_ctxt *ctxt)
3449{
3450 switch (ctxt->op_bytes) {
3451#ifdef CONFIG_X86_64
3452 case 8:
3453 asm("bswap %0" : "+r"(ctxt->dst.val));
3454 break;
3455#endif
3456 default:
3457 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3458 break;
3459 }
3460 return X86EMUL_CONTINUE;
3461}
3462
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003463static bool valid_cr(int nr)
3464{
3465 switch (nr) {
3466 case 0:
3467 case 2 ... 4:
3468 case 8:
3469 return true;
3470 default:
3471 return false;
3472 }
3473}
3474
3475static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3476{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003477 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003478 return emulate_ud(ctxt);
3479
3480 return X86EMUL_CONTINUE;
3481}
3482
3483static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3484{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003485 u64 new_val = ctxt->src.val64;
3486 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003487 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003488
3489 static u64 cr_reserved_bits[] = {
3490 0xffffffff00000000ULL,
3491 0, 0, 0, /* CR3 checked later */
3492 CR4_RESERVED_BITS,
3493 0, 0, 0,
3494 CR8_RESERVED_BITS,
3495 };
3496
3497 if (!valid_cr(cr))
3498 return emulate_ud(ctxt);
3499
3500 if (new_val & cr_reserved_bits[cr])
3501 return emulate_gp(ctxt, 0);
3502
3503 switch (cr) {
3504 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003505 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003506 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3507 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3508 return emulate_gp(ctxt, 0);
3509
Avi Kivity717746e2011-04-20 13:37:53 +03003510 cr4 = ctxt->ops->get_cr(ctxt, 4);
3511 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003512
3513 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3514 !(cr4 & X86_CR4_PAE))
3515 return emulate_gp(ctxt, 0);
3516
3517 break;
3518 }
3519 case 3: {
3520 u64 rsvd = 0;
3521
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003522 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3523 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003524 rsvd = CR3_L_MODE_RESERVED_BITS;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003525
3526 if (new_val & rsvd)
3527 return emulate_gp(ctxt, 0);
3528
3529 break;
3530 }
3531 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003532 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003533
3534 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3535 return emulate_gp(ctxt, 0);
3536
3537 break;
3538 }
3539 }
3540
3541 return X86EMUL_CONTINUE;
3542}
3543
Joerg Roedel3b88e412011-04-04 12:39:29 +02003544static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3545{
3546 unsigned long dr7;
3547
Avi Kivity717746e2011-04-20 13:37:53 +03003548 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003549
3550 /* Check if DR7.Global_Enable is set */
3551 return dr7 & (1 << 13);
3552}
3553
3554static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3555{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003556 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003557 u64 cr4;
3558
3559 if (dr > 7)
3560 return emulate_ud(ctxt);
3561
Avi Kivity717746e2011-04-20 13:37:53 +03003562 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003563 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3564 return emulate_ud(ctxt);
3565
3566 if (check_dr7_gd(ctxt))
3567 return emulate_db(ctxt);
3568
3569 return X86EMUL_CONTINUE;
3570}
3571
3572static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3573{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003574 u64 new_val = ctxt->src.val64;
3575 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003576
3577 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3578 return emulate_gp(ctxt, 0);
3579
3580 return check_dr_read(ctxt);
3581}
3582
Joerg Roedel01de8b02011-04-04 12:39:31 +02003583static int check_svme(struct x86_emulate_ctxt *ctxt)
3584{
3585 u64 efer;
3586
Avi Kivity717746e2011-04-20 13:37:53 +03003587 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003588
3589 if (!(efer & EFER_SVME))
3590 return emulate_ud(ctxt);
3591
3592 return X86EMUL_CONTINUE;
3593}
3594
3595static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3596{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003597 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003598
3599 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003600 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003601 return emulate_gp(ctxt, 0);
3602
3603 return check_svme(ctxt);
3604}
3605
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003606static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3607{
Avi Kivity717746e2011-04-20 13:37:53 +03003608 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003609
Avi Kivity717746e2011-04-20 13:37:53 +03003610 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003611 return emulate_ud(ctxt);
3612
3613 return X86EMUL_CONTINUE;
3614}
3615
Joerg Roedel80612522011-04-04 12:39:33 +02003616static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3617{
Avi Kivity717746e2011-04-20 13:37:53 +03003618 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003619 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003620
Avi Kivity717746e2011-04-20 13:37:53 +03003621 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03003622 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02003623 return emulate_gp(ctxt, 0);
3624
3625 return X86EMUL_CONTINUE;
3626}
3627
Joerg Roedelf6511932011-04-04 12:39:35 +02003628static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3629{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003630 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3631 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003632 return emulate_gp(ctxt, 0);
3633
3634 return X86EMUL_CONTINUE;
3635}
3636
3637static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3638{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003639 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3640 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003641 return emulate_gp(ctxt, 0);
3642
3643 return X86EMUL_CONTINUE;
3644}
3645
Avi Kivity73fba5f2010-07-29 15:11:53 +03003646#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003647#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
3648#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
3649 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003650#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003651#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003652#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3653#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003654#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003655#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003656#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003657#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003658 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003659#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003660 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
3661 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003662#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003663
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003664#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003665#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003666#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003667#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003668#define I2bvIP(_f, _e, _i, _p) \
3669 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003670
Avi Kivityfb864fb2013-01-04 16:18:54 +02003671#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3672 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3673 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003674
Nadav Amit0f54a322014-08-29 11:26:55 +03003675static const struct opcode group7_rm0[] = {
3676 N,
3677 I(SrcNone | Priv | EmulateOnUD, em_vmcall),
3678 N, N, N, N, N, N,
3679};
3680
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003681static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003682 DI(SrcNone | Priv, monitor),
3683 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003684 N, N, N, N, N, N,
3685};
3686
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003687static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003688 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003689 II(SrcNone | Prot | EmulateOnUD, em_vmmcall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003690 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3691 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3692 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3693 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3694 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3695 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003696};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003697
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003698static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003699 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003700 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003701 N, N, N, N, N, N,
3702};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003703
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003704static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003705 F(Lock, em_add),
3706 F(Lock | PageTable, em_or),
3707 F(Lock, em_adc),
3708 F(Lock, em_sbb),
3709 F(Lock | PageTable, em_and),
3710 F(Lock, em_sub),
3711 F(Lock, em_xor),
3712 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003713};
3714
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003715static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003716 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003717};
3718
Avi Kivity007a3b52013-01-19 19:51:51 +02003719static const struct opcode group2[] = {
3720 F(DstMem | ModRM, em_rol),
3721 F(DstMem | ModRM, em_ror),
3722 F(DstMem | ModRM, em_rcl),
3723 F(DstMem | ModRM, em_rcr),
3724 F(DstMem | ModRM, em_shl),
3725 F(DstMem | ModRM, em_shr),
3726 F(DstMem | ModRM, em_shl),
3727 F(DstMem | ModRM, em_sar),
3728};
3729
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003730static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003731 F(DstMem | SrcImm | NoWrite, em_test),
3732 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003733 F(DstMem | SrcNone | Lock, em_not),
3734 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02003735 F(DstXacc | Src2Mem, em_mul_ex),
3736 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02003737 F(DstXacc | Src2Mem, em_div_ex),
3738 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003739};
3740
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003741static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003742 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3743 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003744 N, N, N, N, N, N,
3745};
3746
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003747static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003748 F(DstMem | SrcNone | Lock, em_inc),
3749 F(DstMem | SrcNone | Lock, em_dec),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003750 I(SrcMem | Stack, em_grp45),
3751 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3752 I(SrcMem | Stack, em_grp45),
3753 I(SrcMemFAddr | ImplicitOps, em_grp45),
Gleb Natapov188424b2013-04-11 12:32:14 +03003754 I(SrcMem | Stack, em_grp45), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003755};
3756
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003757static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003758 DI(Prot, sldt),
3759 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003760 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003761 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003762 N, N, N, N,
3763};
3764
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003765static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03003766 II(Mov | DstMem, em_sgdt, sgdt),
3767 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003768 II(SrcMem | Priv, em_lgdt, lgdt),
3769 II(SrcMem | Priv, em_lidt, lidt),
3770 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3771 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3772 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003773}, {
Nadav Amit0f54a322014-08-29 11:26:55 +03003774 EXT(0, group7_rm0),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003775 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003776 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003777 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3778 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3779 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003780} };
3781
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003782static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003783 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003784 F(DstMem | SrcImmByte | NoWrite, em_bt),
3785 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3786 F(DstMem | SrcImmByte | Lock, em_btr),
3787 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003788};
3789
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003790static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003791 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003792}, {
3793 N, N, N, N, N, N, N, N,
3794} };
3795
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003796static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003797 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003798 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003799};
3800
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003801static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003802 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003803};
3804
Paolo Bonzinid5b77062014-07-14 12:54:48 +02003805static const struct gprefix pfx_0f_2b = {
3806 I(0, em_mov), I(0, em_mov), N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003807};
3808
Igor Mammedov27ce8252014-03-15 21:01:59 +01003809static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01003810 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003811};
3812
Alex Williamson0a370272014-07-11 11:56:31 -06003813static const struct gprefix pfx_0f_e7 = {
3814 N, I(Sse, em_mov), N, N,
3815};
3816
Gleb Natapov045a2822012-12-20 16:57:43 +02003817static const struct escape escape_d9 = { {
3818 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3819}, {
3820 /* 0xC0 - 0xC7 */
3821 N, N, N, N, N, N, N, N,
3822 /* 0xC8 - 0xCF */
3823 N, N, N, N, N, N, N, N,
3824 /* 0xD0 - 0xC7 */
3825 N, N, N, N, N, N, N, N,
3826 /* 0xD8 - 0xDF */
3827 N, N, N, N, N, N, N, N,
3828 /* 0xE0 - 0xE7 */
3829 N, N, N, N, N, N, N, N,
3830 /* 0xE8 - 0xEF */
3831 N, N, N, N, N, N, N, N,
3832 /* 0xF0 - 0xF7 */
3833 N, N, N, N, N, N, N, N,
3834 /* 0xF8 - 0xFF */
3835 N, N, N, N, N, N, N, N,
3836} };
3837
3838static const struct escape escape_db = { {
3839 N, N, N, N, N, N, N, N,
3840}, {
3841 /* 0xC0 - 0xC7 */
3842 N, N, N, N, N, N, N, N,
3843 /* 0xC8 - 0xCF */
3844 N, N, N, N, N, N, N, N,
3845 /* 0xD0 - 0xC7 */
3846 N, N, N, N, N, N, N, N,
3847 /* 0xD8 - 0xDF */
3848 N, N, N, N, N, N, N, N,
3849 /* 0xE0 - 0xE7 */
3850 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3851 /* 0xE8 - 0xEF */
3852 N, N, N, N, N, N, N, N,
3853 /* 0xF0 - 0xF7 */
3854 N, N, N, N, N, N, N, N,
3855 /* 0xF8 - 0xFF */
3856 N, N, N, N, N, N, N, N,
3857} };
3858
3859static const struct escape escape_dd = { {
3860 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3861}, {
3862 /* 0xC0 - 0xC7 */
3863 N, N, N, N, N, N, N, N,
3864 /* 0xC8 - 0xCF */
3865 N, N, N, N, N, N, N, N,
3866 /* 0xD0 - 0xC7 */
3867 N, N, N, N, N, N, N, N,
3868 /* 0xD8 - 0xDF */
3869 N, N, N, N, N, N, N, N,
3870 /* 0xE0 - 0xE7 */
3871 N, N, N, N, N, N, N, N,
3872 /* 0xE8 - 0xEF */
3873 N, N, N, N, N, N, N, N,
3874 /* 0xF0 - 0xF7 */
3875 N, N, N, N, N, N, N, N,
3876 /* 0xF8 - 0xFF */
3877 N, N, N, N, N, N, N, N,
3878} };
3879
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003880static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003881 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003882 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003883 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3884 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003885 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003886 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003887 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3888 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003889 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003890 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003891 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3892 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003893 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003894 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003895 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3896 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003897 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003898 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003899 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003900 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003901 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003902 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003903 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003904 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003905 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02003906 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003907 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003908 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003909 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003910 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003911 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003912 I(ImplicitOps | Stack | No64, em_pusha),
3913 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003914 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3915 N, N, N, N,
3916 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003917 I(SrcImm | Mov | Stack, em_push),
3918 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003919 I(SrcImmByte | Mov | Stack, em_push),
3920 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003921 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003922 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003923 /* 0x70 - 0x7F */
3924 X16(D(SrcImmByte)),
3925 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003926 G(ByteOp | DstMem | SrcImm, group1),
3927 G(DstMem | SrcImm, group1),
3928 G(ByteOp | DstMem | SrcImm | No64, group1),
3929 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003930 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003931 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003932 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003933 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003934 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003935 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003936 D(ModRM | SrcMem | NoAccess | DstReg),
3937 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3938 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003939 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003940 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003941 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003942 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003943 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003944 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01003945 II(ImplicitOps | Stack, em_popf, popf),
3946 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003947 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003948 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003949 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003950 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003951 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003952 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003953 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003954 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3955 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003956 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003957 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003958 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003959 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02003960 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003961 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003962 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003963 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003964 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003965 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3966 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003967 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003968 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003969 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Bruce Rogers32611072013-09-09 09:40:20 -06003970 I(ImplicitOps | Stack | SrcImmU16, em_ret_far_imm),
3971 I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003972 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003973 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003974 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003975 G(Src2One | ByteOp, group2), G(Src2One, group2),
3976 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003977 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02003978 I(DstAcc | SrcImmUByte | No64, em_aad),
3979 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02003980 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003981 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02003982 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003983 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003984 X3(I(SrcImmByte, em_loop)),
3985 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003986 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3987 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003988 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003989 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003990 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003991 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3992 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003993 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003994 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003995 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3996 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003997 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003998 D(ImplicitOps), D(ImplicitOps),
3999 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004000 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4001};
4002
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004003static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004004 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004005 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02004006 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004007 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004008 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004009 N, D(ImplicitOps | ModRM), N, N,
4010 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004011 N, N, N, N, N, N, N, N,
4012 D(ImplicitOps | ModRM), N, N, N, N, N, N, D(ImplicitOps | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004013 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03004014 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
4015 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
4016 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
4017 check_cr_write),
4018 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
4019 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004020 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004021 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
4022 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004023 N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b),
Avi Kivity3e114eb2012-04-09 18:40:01 +03004024 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004025 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004026 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004027 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004028 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004029 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02004030 I(ImplicitOps | EmulateOnUD, em_sysenter),
4031 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004032 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004033 N, N, N, N, N, N, N, N,
4034 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03004035 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004036 /* 0x50 - 0x5F */
4037 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4038 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004039 N, N, N, N,
4040 N, N, N, N,
4041 N, N, N, N,
4042 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004043 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004044 N, N, N, N,
4045 N, N, N, N,
4046 N, N, N, N,
4047 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004048 /* 0x80 - 0x8F */
4049 X16(D(SrcImm)),
4050 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004051 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004052 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004053 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004054 II(ImplicitOps, em_cpuid, cpuid),
4055 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004056 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4057 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004058 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004059 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004060 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004061 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004062 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4063 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Avi Kivity4d758342013-01-19 19:51:55 +02004064 D(ModRM), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004065 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09004066 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004067 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004068 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004069 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4070 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004071 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004072 /* 0xB8 - 0xBF */
4073 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004074 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004075 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
4076 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004077 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004078 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02004079 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Wei Yongjun92f738a52010-08-17 09:19:34 +08004080 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004081 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004082 /* 0xC8 - 0xCF */
4083 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004084 /* 0xD0 - 0xDF */
4085 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4086 /* 0xE0 - 0xEF */
Alex Williamson0a370272014-07-11 11:56:31 -06004087 N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
4088 N, N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004089 /* 0xF0 - 0xFF */
4090 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4091};
4092
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004093static const struct gprefix three_byte_0f_38_f0 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01004094 I(DstReg | SrcMem | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004095};
4096
4097static const struct gprefix three_byte_0f_38_f1 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01004098 I(DstMem | SrcReg | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004099};
4100
4101/*
4102 * Insns below are selected by the prefix which indexed by the third opcode
4103 * byte.
4104 */
4105static const struct opcode opcode_map_0f_38[256] = {
4106 /* 0x00 - 0x7f */
4107 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004108 /* 0x80 - 0xef */
4109 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4110 /* 0xf0 - 0xf1 */
4111 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f0),
4112 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f1),
4113 /* 0xf2 - 0xff */
4114 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004115};
4116
Avi Kivity73fba5f2010-07-29 15:11:53 +03004117#undef D
4118#undef N
4119#undef G
4120#undef GD
4121#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004122#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004123#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004124
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004125#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004126#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004127#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004128#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004129#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004130
Avi Kivity9dac77f2011-06-01 15:34:25 +03004131static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004132{
4133 unsigned size;
4134
Avi Kivity9dac77f2011-06-01 15:34:25 +03004135 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004136 if (size == 8)
4137 size = 4;
4138 return size;
4139}
4140
4141static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4142 unsigned size, bool sign_extension)
4143{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004144 int rc = X86EMUL_CONTINUE;
4145
4146 op->type = OP_IMM;
4147 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004148 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004149 /* NB. Immediates are sign-extended as necessary. */
4150 switch (op->bytes) {
4151 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004152 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004153 break;
4154 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004155 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004156 break;
4157 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004158 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004159 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004160 case 8:
4161 op->val = insn_fetch(s64, ctxt);
4162 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004163 }
4164 if (!sign_extension) {
4165 switch (op->bytes) {
4166 case 1:
4167 op->val &= 0xff;
4168 break;
4169 case 2:
4170 op->val &= 0xffff;
4171 break;
4172 case 4:
4173 op->val &= 0xffffffff;
4174 break;
4175 }
4176 }
4177done:
4178 return rc;
4179}
4180
Avi Kivitya99455492011-09-13 10:45:41 +03004181static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4182 unsigned d)
4183{
4184 int rc = X86EMUL_CONTINUE;
4185
4186 switch (d) {
4187 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004188 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03004189 break;
4190 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004191 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03004192 break;
4193 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004194 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004195 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03004196 *op = ctxt->memop;
4197 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004198 if (ctxt->d & BitOp)
Avi Kivitya99455492011-09-13 10:45:41 +03004199 fetch_bit_operand(ctxt);
4200 op->orig_val = op->val;
4201 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004202 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004203 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004204 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03004205 case OpAcc:
4206 op->type = OP_REG;
4207 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004208 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03004209 fetch_register_operand(op);
4210 op->orig_val = op->val;
4211 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004212 case OpAccLo:
4213 op->type = OP_REG;
4214 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4215 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4216 fetch_register_operand(op);
4217 op->orig_val = op->val;
4218 break;
4219 case OpAccHi:
4220 if (ctxt->d & ByteOp) {
4221 op->type = OP_NONE;
4222 break;
4223 }
4224 op->type = OP_REG;
4225 op->bytes = ctxt->op_bytes;
4226 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4227 fetch_register_operand(op);
4228 op->orig_val = op->val;
4229 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004230 case OpDI:
4231 op->type = OP_MEM;
4232 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4233 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004234 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya99455492011-09-13 10:45:41 +03004235 op->addr.mem.seg = VCPU_SREG_ES;
4236 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004237 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03004238 break;
4239 case OpDX:
4240 op->type = OP_REG;
4241 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004242 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03004243 fetch_register_operand(op);
4244 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004245 case OpCL:
4246 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004247 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004248 break;
4249 case OpImmByte:
4250 rc = decode_imm(ctxt, op, 1, true);
4251 break;
4252 case OpOne:
4253 op->bytes = 1;
4254 op->val = 1;
4255 break;
4256 case OpImm:
4257 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4258 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004259 case OpImm64:
4260 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4261 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004262 case OpMem8:
4263 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004264 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004265 ctxt->memop.addr.reg = decode_register(ctxt,
4266 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004267 fetch_register_operand(&ctxt->memop);
4268 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004269 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004270 case OpMem16:
4271 ctxt->memop.bytes = 2;
4272 goto mem_common;
4273 case OpMem32:
4274 ctxt->memop.bytes = 4;
4275 goto mem_common;
4276 case OpImmU16:
4277 rc = decode_imm(ctxt, op, 2, false);
4278 break;
4279 case OpImmU:
4280 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4281 break;
4282 case OpSI:
4283 op->type = OP_MEM;
4284 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4285 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004286 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Bandan Das573e80f2014-04-16 12:46:13 -04004287 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03004288 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004289 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004290 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004291 case OpXLat:
4292 op->type = OP_MEM;
4293 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4294 op->addr.mem.ea =
4295 register_address(ctxt,
4296 reg_read(ctxt, VCPU_REGS_RBX) +
4297 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04004298 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004299 op->val = 0;
4300 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004301 case OpImmFAddr:
4302 op->type = OP_IMM;
4303 op->addr.mem.ea = ctxt->_eip;
4304 op->bytes = ctxt->op_bytes + 2;
4305 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4306 break;
4307 case OpMemFAddr:
4308 ctxt->memop.bytes = ctxt->op_bytes + 2;
4309 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004310 case OpES:
4311 op->val = VCPU_SREG_ES;
4312 break;
4313 case OpCS:
4314 op->val = VCPU_SREG_CS;
4315 break;
4316 case OpSS:
4317 op->val = VCPU_SREG_SS;
4318 break;
4319 case OpDS:
4320 op->val = VCPU_SREG_DS;
4321 break;
4322 case OpFS:
4323 op->val = VCPU_SREG_FS;
4324 break;
4325 case OpGS:
4326 op->val = VCPU_SREG_GS;
4327 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004328 case OpImplicit:
4329 /* Special instructions do their own operand decoding. */
4330 default:
4331 op->type = OP_NONE; /* Disable writeback. */
4332 break;
4333 }
4334
4335done:
4336 return rc;
4337}
4338
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004339int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004340{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004341 int rc = X86EMUL_CONTINUE;
4342 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004343 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004344 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04004345 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03004346 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004347
Avi Kivityf09ed832011-09-13 10:45:40 +03004348 ctxt->memop.type = OP_NONE;
4349 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004350 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02004351 ctxt->fetch.ptr = ctxt->fetch.data;
4352 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004353 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01004354 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004355 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004356 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02004357 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004358 if (rc != X86EMUL_CONTINUE)
4359 return rc;
4360 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004361
4362 switch (mode) {
4363 case X86EMUL_MODE_REAL:
4364 case X86EMUL_MODE_VM86:
4365 case X86EMUL_MODE_PROT16:
4366 def_op_bytes = def_ad_bytes = 2;
4367 break;
4368 case X86EMUL_MODE_PROT32:
4369 def_op_bytes = def_ad_bytes = 4;
4370 break;
4371#ifdef CONFIG_X86_64
4372 case X86EMUL_MODE_PROT64:
4373 def_op_bytes = 4;
4374 def_ad_bytes = 8;
4375 break;
4376#endif
4377 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004378 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004379 }
4380
Avi Kivity9dac77f2011-06-01 15:34:25 +03004381 ctxt->op_bytes = def_op_bytes;
4382 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004383
4384 /* Legacy prefixes. */
4385 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004386 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004387 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004388 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004389 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004390 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004391 break;
4392 case 0x67: /* address-size override */
4393 if (mode == X86EMUL_MODE_PROT64)
4394 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004395 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004396 else
4397 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004398 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004399 break;
4400 case 0x26: /* ES override */
4401 case 0x2e: /* CS override */
4402 case 0x36: /* SS override */
4403 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004404 has_seg_override = true;
4405 ctxt->seg_override = (ctxt->b >> 3) & 3;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004406 break;
4407 case 0x64: /* FS override */
4408 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004409 has_seg_override = true;
4410 ctxt->seg_override = ctxt->b & 7;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004411 break;
4412 case 0x40 ... 0x4f: /* REX */
4413 if (mode != X86EMUL_MODE_PROT64)
4414 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004415 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004416 continue;
4417 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004418 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004419 break;
4420 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004421 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004422 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004423 break;
4424 default:
4425 goto done_prefixes;
4426 }
4427
4428 /* Any legacy prefix after a REX prefix nullifies its effect. */
4429
Avi Kivity9dac77f2011-06-01 15:34:25 +03004430 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004431 }
4432
4433done_prefixes:
4434
4435 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004436 if (ctxt->rex_prefix & 8)
4437 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004438
4439 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004440 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004441 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004442 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004443 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004444 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004445 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004446
4447 /* 0F_38 opcode map */
4448 if (ctxt->b == 0x38) {
4449 ctxt->opcode_len = 3;
4450 ctxt->b = insn_fetch(u8, ctxt);
4451 opcode = opcode_map_0f_38[ctxt->b];
4452 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004453 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004454 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004455
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004456 if (ctxt->d & ModRM)
4457 ctxt->modrm = insn_fetch(u8, ctxt);
4458
Nadav Amit7fe864d2014-06-02 18:34:03 +03004459 /* vex-prefix instructions are not implemented */
4460 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
4461 (mode == X86EMUL_MODE_PROT64 ||
4462 (mode >= X86EMUL_MODE_PROT16 && (ctxt->modrm & 0x80)))) {
4463 ctxt->d = NotImpl;
4464 }
4465
Avi Kivity9dac77f2011-06-01 15:34:25 +03004466 while (ctxt->d & GroupMask) {
4467 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004468 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004469 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004470 opcode = opcode.u.group[goffset];
4471 break;
4472 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004473 goffset = (ctxt->modrm >> 3) & 7;
4474 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004475 opcode = opcode.u.gdual->mod3[goffset];
4476 else
4477 opcode = opcode.u.gdual->mod012[goffset];
4478 break;
4479 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004480 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004481 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004482 break;
4483 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004484 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004485 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004486 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004487 switch (simd_prefix) {
4488 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4489 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4490 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4491 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4492 }
4493 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004494 case Escape:
4495 if (ctxt->modrm > 0xbf)
4496 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4497 else
4498 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4499 break;
Avi Kivity46561642011-04-24 14:09:59 +03004500 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004501 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004502 }
Avi Kivity46561642011-04-24 14:09:59 +03004503
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004504 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004505 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004506 }
4507
Paolo Bonzinie24186e2014-03-27 12:00:57 +01004508 /* Unrecognised? */
4509 if (ctxt->d == 0)
4510 return EMULATION_FAILED;
4511
Avi Kivity9dac77f2011-06-01 15:34:25 +03004512 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004513
Nadav Amit3a6095a2014-08-13 16:50:13 +03004514 if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD)))
4515 return EMULATION_FAILED;
4516
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004517 if (unlikely(ctxt->d &
Nadav Amit3a6095a2014-08-13 16:50:13 +03004518 (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm))) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004519 /*
4520 * These are copied unconditionally here, and checked unconditionally
4521 * in x86_emulate_insn.
4522 */
4523 ctxt->check_perm = opcode.check_perm;
4524 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004525
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004526 if (ctxt->d & NotImpl)
4527 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004528
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004529 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004530 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004531
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004532 if (ctxt->d & Op3264) {
4533 if (mode == X86EMUL_MODE_PROT64)
4534 ctxt->op_bytes = 8;
4535 else
4536 ctxt->op_bytes = 4;
4537 }
4538
4539 if (ctxt->d & Sse)
4540 ctxt->op_bytes = 16;
4541 else if (ctxt->d & Mmx)
4542 ctxt->op_bytes = 8;
4543 }
Avi Kivity1253791d2011-03-29 11:41:27 +02004544
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004545 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004546 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004547 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04004548 if (!has_seg_override) {
4549 has_seg_override = true;
4550 ctxt->seg_override = ctxt->modrm_seg;
4551 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004552 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004553 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004554 if (rc != X86EMUL_CONTINUE)
4555 goto done;
4556
Bandan Das573e80f2014-04-16 12:46:13 -04004557 if (!has_seg_override)
4558 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004559
Bandan Das573e80f2014-04-16 12:46:13 -04004560 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004561
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004562 /*
4563 * Decode and fetch the source operand: register, memory
4564 * or immediate.
4565 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004566 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004567 if (rc != X86EMUL_CONTINUE)
4568 goto done;
4569
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004570 /*
4571 * Decode and fetch the second source operand: register, memory
4572 * or immediate.
4573 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004574 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004575 if (rc != X86EMUL_CONTINUE)
4576 goto done;
4577
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004578 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03004579 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004580
4581done:
Bandan Das41061cd2014-04-16 12:46:14 -04004582 if (ctxt->rip_relative)
Avi Kivityf09ed832011-09-13 10:45:40 +03004583 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004584
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004585 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004586}
4587
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004588bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4589{
4590 return ctxt->d & PageTable;
4591}
4592
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004593static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4594{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004595 /* The second termination condition only applies for REPE
4596 * and REPNE. Test if the repeat string operation prefix is
4597 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4598 * corresponding termination condition according to:
4599 * - if REPE/REPZ and ZF = 0 then done
4600 * - if REPNE/REPNZ and ZF = 1 then done
4601 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004602 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4603 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4604 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004605 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004606 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004607 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4608 return true;
4609
4610 return false;
4611}
4612
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004613static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4614{
4615 bool fault = false;
4616
4617 ctxt->ops->get_fpu(ctxt);
4618 asm volatile("1: fwait \n\t"
4619 "2: \n\t"
4620 ".pushsection .fixup,\"ax\" \n\t"
4621 "3: \n\t"
4622 "movb $1, %[fault] \n\t"
4623 "jmp 2b \n\t"
4624 ".popsection \n\t"
4625 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004626 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004627 ctxt->ops->put_fpu(ctxt);
4628
4629 if (unlikely(fault))
4630 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4631
4632 return X86EMUL_CONTINUE;
4633}
4634
4635static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4636 struct operand *op)
4637{
4638 if (op->type == OP_MM)
4639 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4640}
4641
Avi Kivitye28bbd42013-01-04 16:18:48 +02004642static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4643{
4644 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivityb9fa4092013-02-09 11:31:48 +02004645 if (!(ctxt->d & ByteOp))
4646 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Avi Kivitye28bbd42013-01-04 16:18:48 +02004647 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004648 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
4649 [fastop]"+S"(fop)
4650 : "c"(ctxt->src2.val));
Avi Kivitye28bbd42013-01-04 16:18:48 +02004651 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004652 if (!fop) /* exception is returned in fop variable */
4653 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02004654 return X86EMUL_CONTINUE;
4655}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004656
Bandan Das14985072014-04-16 12:46:09 -04004657void init_decode_cache(struct x86_emulate_ctxt *ctxt)
4658{
Bandan Das573e80f2014-04-16 12:46:13 -04004659 memset(&ctxt->rip_relative, 0,
4660 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04004661
Bandan Das14985072014-04-16 12:46:09 -04004662 ctxt->io_read.pos = 0;
4663 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04004664 ctxt->mem_read.end = 0;
4665}
4666
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004667int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004668{
Mathias Krause0225fb52012-08-30 01:30:16 +02004669 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004670 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004671 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004672
Avi Kivity9dac77f2011-06-01 15:34:25 +03004673 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004674
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004675 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004676 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004677 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004678 goto done;
4679 }
4680
Avi Kivity9dac77f2011-06-01 15:34:25 +03004681 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004682 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004683 goto done;
4684 }
4685
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004686 if (unlikely(ctxt->d &
4687 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
4688 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4689 (ctxt->d & Undefined)) {
4690 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004691 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004692 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004693
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004694 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4695 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
4696 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004697 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004698 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004699
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004700 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
4701 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004702 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004703 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004704
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004705 if (ctxt->d & Mmx) {
4706 rc = flush_pending_x87_faults(ctxt);
4707 if (rc != X86EMUL_CONTINUE)
4708 goto done;
4709 /*
4710 * Now that we know the fpu is exception safe, we can fetch
4711 * operands from it.
4712 */
4713 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4714 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4715 if (!(ctxt->d & Mov))
4716 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4717 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004718
Bandan Das685bbf42014-04-16 12:46:10 -04004719 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004720 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4721 X86_ICPT_PRE_EXCEPT);
4722 if (rc != X86EMUL_CONTINUE)
4723 goto done;
4724 }
4725
4726 /* Privileged instruction can be executed only in CPL=0 */
4727 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Nadav Amit68efa762014-06-18 17:19:35 +03004728 if (ctxt->d & PrivUD)
4729 rc = emulate_ud(ctxt);
4730 else
4731 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004732 goto done;
4733 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004734
4735 /* Instruction can only be executed in protected mode */
4736 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
4737 rc = emulate_ud(ctxt);
4738 goto done;
4739 }
4740
4741 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04004742 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004743 rc = ctxt->check_perm(ctxt);
4744 if (rc != X86EMUL_CONTINUE)
4745 goto done;
4746 }
4747
Bandan Das685bbf42014-04-16 12:46:10 -04004748 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004749 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4750 X86_ICPT_POST_EXCEPT);
4751 if (rc != X86EMUL_CONTINUE)
4752 goto done;
4753 }
4754
4755 if (ctxt->rep_prefix && (ctxt->d & String)) {
4756 /* All REP prefixes have the same first termination condition */
4757 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
4758 ctxt->eip = ctxt->_eip;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004759 ctxt->eflags &= ~EFLG_RF;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004760 goto done;
4761 }
4762 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004763 }
4764
Avi Kivity9dac77f2011-06-01 15:34:25 +03004765 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4766 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4767 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004768 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004769 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004770 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004771 }
4772
Avi Kivity9dac77f2011-06-01 15:34:25 +03004773 if (ctxt->src2.type == OP_MEM) {
4774 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4775 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004776 if (rc != X86EMUL_CONTINUE)
4777 goto done;
4778 }
4779
Avi Kivity9dac77f2011-06-01 15:34:25 +03004780 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004781 goto special_insn;
4782
4783
Avi Kivity9dac77f2011-06-01 15:34:25 +03004784 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004785 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004786 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4787 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004788 if (rc != X86EMUL_CONTINUE)
4789 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004790 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004791 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004792
Avi Kivity018a98d2007-11-27 19:30:56 +02004793special_insn:
4794
Bandan Das685bbf42014-04-16 12:46:10 -04004795 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004796 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004797 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004798 if (rc != X86EMUL_CONTINUE)
4799 goto done;
4800 }
4801
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004802 if (ctxt->rep_prefix && (ctxt->d & String))
4803 ctxt->eflags |= EFLG_RF;
4804 else
4805 ctxt->eflags &= ~EFLG_RF;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004806
Avi Kivity9dac77f2011-06-01 15:34:25 +03004807 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004808 if (ctxt->d & Fastop) {
4809 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4810 rc = fastop(ctxt, fop);
4811 if (rc != X86EMUL_CONTINUE)
4812 goto done;
4813 goto writeback;
4814 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004815 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004816 if (rc != X86EMUL_CONTINUE)
4817 goto done;
4818 goto writeback;
4819 }
4820
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004821 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004822 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004823 else if (ctxt->opcode_len == 3)
4824 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004825
Avi Kivity9dac77f2011-06-01 15:34:25 +03004826 switch (ctxt->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004827 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004828 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004829 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004830 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004831 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004832 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004833 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03004834 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004835 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004836 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004837 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004838 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004839 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004840 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03004841 ctxt->dst.type = OP_NONE;
4842 else
4843 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004844 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004845 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004846 switch (ctxt->op_bytes) {
4847 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4848 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4849 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004850 }
4851 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004852 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004853 rc = emulate_int(ctxt, 3);
4854 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004855 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004856 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004857 break;
4858 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004859 if (ctxt->eflags & EFLG_OF)
4860 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004861 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004862 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004863 case 0xeb: /* jmp rel short */
Nadav Amit234f3ce2014-09-18 22:39:38 +03004864 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004865 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004866 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004867 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004868 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004869 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004870 case 0xf5: /* cmc */
4871 /* complement carry flag from eflags reg */
4872 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004873 break;
4874 case 0xf8: /* clc */
4875 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004876 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004877 case 0xf9: /* stc */
4878 ctxt->eflags |= EFLG_CF;
4879 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004880 case 0xfc: /* cld */
4881 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004882 break;
4883 case 0xfd: /* std */
4884 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004885 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004886 default:
4887 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004888 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004889
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004890 if (rc != X86EMUL_CONTINUE)
4891 goto done;
4892
Avi Kivity018a98d2007-11-27 19:30:56 +02004893writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02004894 if (ctxt->d & SrcWrite) {
4895 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
4896 rc = writeback(ctxt, &ctxt->src);
4897 if (rc != X86EMUL_CONTINUE)
4898 goto done;
4899 }
Nadav Amitee212292014-06-15 16:12:58 +03004900 if (!(ctxt->d & NoWrite)) {
4901 rc = writeback(ctxt, &ctxt->dst);
4902 if (rc != X86EMUL_CONTINUE)
4903 goto done;
4904 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004905
Gleb Natapov5cd21912010-03-18 15:20:26 +02004906 /*
4907 * restore dst type in case the decoding will be reused
4908 * (happens for string instruction )
4909 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004910 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004911
Avi Kivity9dac77f2011-06-01 15:34:25 +03004912 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004913 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004914
Avi Kivity9dac77f2011-06-01 15:34:25 +03004915 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004916 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004917
Avi Kivity9dac77f2011-06-01 15:34:25 +03004918 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004919 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004920 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004921 if ((ctxt->d & SrcMask) == SrcSI)
4922 count = ctxt->src.count;
4923 else
4924 count = ctxt->dst.count;
4925 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4926 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004927
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004928 if (!string_insn_completed(ctxt)) {
4929 /*
4930 * Re-enter guest when pio read ahead buffer is empty
4931 * or, if it is not used, after each 1024 iteration.
4932 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004933 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004934 (r->end == 0 || r->end != r->pos)) {
4935 /*
4936 * Reset read cache. Usually happens before
4937 * decode, but since instruction is restarted
4938 * we have to do it here.
4939 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004940 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004941 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004942 return EMULATION_RESTART;
4943 }
4944 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004945 }
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004946 ctxt->eflags &= ~EFLG_RF;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004947 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004948
Avi Kivity9dac77f2011-06-01 15:34:25 +03004949 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004950
4951done:
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02004952 if (rc == X86EMUL_PROPAGATE_FAULT) {
4953 WARN_ON(ctxt->exception.vector > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +02004954 ctxt->have_exception = true;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02004955 }
Joerg Roedel775fde82011-04-04 12:39:24 +02004956 if (rc == X86EMUL_INTERCEPTED)
4957 return EMULATION_INTERCEPTED;
4958
Avi Kivitydd856ef2012-08-27 23:46:17 +03004959 if (rc == X86EMUL_CONTINUE)
4960 writeback_registers(ctxt);
4961
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004962 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004963
4964twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004965 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004966 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004967 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004968 break;
4969 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004970 case 0x0d: /* GrpP (prefetch) */
4971 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004972 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02004973 break;
4974 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004975 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004976 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004977 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004978 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004979 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004980 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03004981 if (test_cc(ctxt->b, ctxt->eflags))
4982 ctxt->dst.val = ctxt->src.val;
4983 else if (ctxt->mode != X86EMUL_MODE_PROT64 ||
4984 ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004985 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004986 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004987 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004988 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03004989 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004990 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004991 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004992 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004993 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004994 case 0xae: /* clflush */
4995 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004996 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004997 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004998 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004999 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005000 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005001 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005002 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005003 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03005004 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005005 break;
Sheng Yanga012e652007-10-15 14:24:20 +08005006 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005007 ctxt->dst.bytes = ctxt->op_bytes;
Nadav Amit3b320042014-06-02 18:34:08 +03005008 ctxt->dst.val = (ctxt->op_bytes == 8) ? (u64) ctxt->src.val :
5009 (u32) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08005010 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005011 default:
5012 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005013 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005014
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005015threebyte_insn:
5016
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005017 if (rc != X86EMUL_CONTINUE)
5018 goto done;
5019
Avi Kivity6aa8b732006-12-10 02:21:36 -08005020 goto writeback;
5021
5022cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02005023 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005024}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005025
5026void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
5027{
5028 invalidate_registers(ctxt);
5029}
5030
5031void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
5032{
5033 writeback_registers(ctxt);
5034}