blob: 9f8a2faf50407b6212f71a5de81aea8680855f87 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivityb7d491e2013-01-04 16:18:49 +020027#include <linux/stringify.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028
Avi Kivity3eeb3282010-01-21 15:31:48 +020029#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020030#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020031
Avi Kivity6aa8b732006-12-10 02:21:36 -080032/*
Avi Kivitya9945542011-09-13 10:45:41 +030033 * Operand types
34 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030035#define OpNone 0ull
36#define OpImplicit 1ull /* No generic decode */
37#define OpReg 2ull /* Register */
38#define OpMem 3ull /* Memory */
39#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
40#define OpDI 5ull /* ES:DI/EDI/RDI */
41#define OpMem64 6ull /* Memory, 64-bit */
42#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
43#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030044#define OpCL 9ull /* CL register (for shifts) */
45#define OpImmByte 10ull /* 8-bit sign extended immediate */
46#define OpOne 11ull /* Implied 1 */
Nadav Amit5e2c6882012-12-06 21:55:10 -020047#define OpImm 12ull /* Sign extended up to 32-bit immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030048#define OpMem16 13ull /* Memory operand (16-bit). */
49#define OpMem32 14ull /* Memory operand (32-bit). */
50#define OpImmU 15ull /* Immediate operand, zero extended */
51#define OpSI 16ull /* SI/ESI/RSI */
52#define OpImmFAddr 17ull /* Immediate far address */
53#define OpMemFAddr 18ull /* Far address in memory */
54#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030055#define OpES 20ull /* ES */
56#define OpCS 21ull /* CS */
57#define OpSS 22ull /* SS */
58#define OpDS 23ull /* DS */
59#define OpFS 24ull /* FS */
60#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020061#define OpMem8 26ull /* 8-bit zero extended memory operand */
Nadav Amit5e2c6882012-12-06 21:55:10 -020062#define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */
Paolo Bonzini7fa57952013-05-09 11:32:50 +020063#define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */
Avi Kivity820207c2013-02-09 11:31:45 +020064#define OpAccLo 29ull /* Low part of extended acc (AX/AX/EAX/RAX) */
65#define OpAccHi 30ull /* High part of extended acc (-/DX/EDX/RDX) */
Avi Kivitya9945542011-09-13 10:45:41 +030066
Avi Kivity0fe59122011-09-13 10:45:47 +030067#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030068#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya9945542011-09-13 10:45:41 +030069
70/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080071 * Opcode effective-address decode tables.
72 * Note that we only emulate instructions that have at least one memory
73 * operand (excluding implicit stack references). We assume that stack
74 * references and instruction fetches will never occur in special memory
75 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
76 * not be handled.
77 */
78
79/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030080#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080081/* Destination operand type. */
Avi Kivitya9945542011-09-13 10:45:41 +030082#define DstShift 1
83#define ImplicitOps (OpImplicit << DstShift)
84#define DstReg (OpReg << DstShift)
85#define DstMem (OpMem << DstShift)
86#define DstAcc (OpAcc << DstShift)
87#define DstDI (OpDI << DstShift)
88#define DstMem64 (OpMem64 << DstShift)
89#define DstImmUByte (OpImmUByte << DstShift)
90#define DstDX (OpDX << DstShift)
Avi Kivity820207c2013-02-09 11:31:45 +020091#define DstAccLo (OpAccLo << DstShift)
Avi Kivitya9945542011-09-13 10:45:41 +030092#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080093/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030094#define SrcShift 6
95#define SrcNone (OpNone << SrcShift)
96#define SrcReg (OpReg << SrcShift)
97#define SrcMem (OpMem << SrcShift)
98#define SrcMem16 (OpMem16 << SrcShift)
99#define SrcMem32 (OpMem32 << SrcShift)
100#define SrcImm (OpImm << SrcShift)
101#define SrcImmByte (OpImmByte << SrcShift)
102#define SrcOne (OpOne << SrcShift)
103#define SrcImmUByte (OpImmUByte << SrcShift)
104#define SrcImmU (OpImmU << SrcShift)
105#define SrcSI (OpSI << SrcShift)
Paolo Bonzini7fa57952013-05-09 11:32:50 +0200106#define SrcXLat (OpXLat << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300107#define SrcImmFAddr (OpImmFAddr << SrcShift)
108#define SrcMemFAddr (OpMemFAddr << SrcShift)
109#define SrcAcc (OpAcc << SrcShift)
110#define SrcImmU16 (OpImmU16 << SrcShift)
Nadav Amit5e2c6882012-12-06 21:55:10 -0200111#define SrcImm64 (OpImm64 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300112#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200113#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity820207c2013-02-09 11:31:45 +0200114#define SrcAccHi (OpAccHi << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300115#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300116#define BitOp (1<<11)
117#define MemAbs (1<<12) /* Memory operand is absolute displacement */
118#define String (1<<13) /* String instruction (rep capable) */
119#define Stack (1<<14) /* Stack instruction (push/pop) */
120#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
121#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
122#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
123#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
124#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
Gleb Natapov045a2822012-12-20 16:57:43 +0200125#define Escape (5<<15) /* Escape to coprocessor instruction */
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300126#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300127/* Generic ModRM decode. */
128#define ModRM (1<<19)
129/* Destination is only written; never read. */
130#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300131/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200132#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Borislav Petkovb51e9742013-09-22 16:44:52 +0200133#define EmulateOnUD (1<<22) /* Emulate if unsupported by the host */
Avi Kivity5a506b12010-08-01 15:10:29 +0300134#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300135#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300136#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200137#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200138#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300139#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800140#define PageTable (1 << 29) /* instruction used to write page table */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300141#define NotImpl (1 << 30) /* instruction is not implemented */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100142/* Source 2 operand type */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300143#define Src2Shift (31)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300144#define Src2None (OpNone << Src2Shift)
Avi Kivityab2c5ce2013-02-09 11:31:46 +0200145#define Src2Mem (OpMem << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300146#define Src2CL (OpCL << Src2Shift)
147#define Src2ImmByte (OpImmByte << Src2Shift)
148#define Src2One (OpOne << Src2Shift)
149#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300150#define Src2ES (OpES << Src2Shift)
151#define Src2CS (OpCS << Src2Shift)
152#define Src2SS (OpSS << Src2Shift)
153#define Src2DS (OpDS << Src2Shift)
154#define Src2FS (OpFS << Src2Shift)
155#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300156#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300157#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Avi Kivity1c11b372012-04-09 18:39:59 +0300158#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
159#define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */
160#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
Avi Kivitye28bbd42013-01-04 16:18:48 +0200161#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
Avi Kivityb6744dc2013-01-04 16:18:50 +0200162#define NoWrite ((u64)1 << 45) /* No writeback */
Avi Kivityfb32b1e2013-02-09 11:31:44 +0200163#define SrcWrite ((u64)1 << 46) /* Write back src operand */
Nadav Amit9b88ae92014-05-25 23:05:21 +0300164#define NoMod ((u64)1 << 47) /* Mod field is ignored */
Paolo Bonzinid40a6892014-03-27 11:58:02 +0100165#define Intercept ((u64)1 << 48) /* Has valid intercept field */
166#define CheckPerm ((u64)1 << 49) /* Has valid check_perm field */
Nadav Amit10e38fc2014-06-18 17:19:34 +0300167#define NoBigReal ((u64)1 << 50) /* No big real mode */
Nadav Amit68efa762014-06-18 17:19:35 +0300168#define PrivUD ((u64)1 << 51) /* #UD instead of #GP on CPL > 0 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800169
Avi Kivity820207c2013-02-09 11:31:45 +0200170#define DstXacc (DstAccLo | SrcAccHi | SrcWrite)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800171
Avi Kivityd0e53322010-07-29 15:11:54 +0300172#define X2(x...) x, x
173#define X3(x...) X2(x), x
174#define X4(x...) X2(x), X2(x)
175#define X5(x...) X4(x), x
176#define X6(x...) X4(x), X2(x)
177#define X7(x...) X4(x), X3(x)
178#define X8(x...) X4(x), X4(x)
179#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300180
Avi Kivitye28bbd42013-01-04 16:18:48 +0200181#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
182#define FASTOP_SIZE 8
183
184/*
185 * fastop functions have a special calling convention:
186 *
Avi Kivity017da7b2013-02-09 11:31:47 +0200187 * dst: rax (in/out)
188 * src: rdx (in/out)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200189 * src2: rcx (in)
190 * flags: rflags (in/out)
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200191 * ex: rsi (in:fastop pointer, out:zero if exception)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200192 *
193 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
194 * different operand sizes can be reached by calculation, rather than a jump
195 * table (which would be bigger than the code).
196 *
197 * fastop functions are declared as taking a never-defined fastop parameter,
198 * so they can't be called from C directly.
199 */
200
201struct fastop;
202
Avi Kivityd65b1de2010-07-29 15:11:35 +0300203struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300204 u64 flags : 56;
205 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300206 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300207 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200208 const struct opcode *group;
209 const struct group_dual *gdual;
210 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200211 const struct escape *esc;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200212 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300213 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200214 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300215};
216
217struct group_dual {
218 struct opcode mod012[8];
219 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300220};
221
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200222struct gprefix {
223 struct opcode pfx_no;
224 struct opcode pfx_66;
225 struct opcode pfx_f2;
226 struct opcode pfx_f3;
227};
228
Gleb Natapov045a2822012-12-20 16:57:43 +0200229struct escape {
230 struct opcode op[8];
231 struct opcode high[64];
232};
233
Avi Kivity6aa8b732006-12-10 02:21:36 -0800234/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200235#define EFLG_ID (1<<21)
236#define EFLG_VIP (1<<20)
237#define EFLG_VIF (1<<19)
238#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200239#define EFLG_VM (1<<17)
240#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200241#define EFLG_IOPL (3<<12)
242#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800243#define EFLG_OF (1<<11)
244#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200245#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200246#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800247#define EFLG_SF (1<<7)
248#define EFLG_ZF (1<<6)
249#define EFLG_AF (1<<4)
250#define EFLG_PF (1<<2)
251#define EFLG_CF (1<<0)
252
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300253#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
254#define EFLG_RESERVED_ONE_MASK 2
255
Avi Kivitydd856ef2012-08-27 23:46:17 +0300256static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
257{
258 if (!(ctxt->regs_valid & (1 << nr))) {
259 ctxt->regs_valid |= 1 << nr;
260 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
261 }
262 return ctxt->_regs[nr];
263}
264
265static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
266{
267 ctxt->regs_valid |= 1 << nr;
268 ctxt->regs_dirty |= 1 << nr;
269 return &ctxt->_regs[nr];
270}
271
272static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
273{
274 reg_read(ctxt, nr);
275 return reg_write(ctxt, nr);
276}
277
278static void writeback_registers(struct x86_emulate_ctxt *ctxt)
279{
280 unsigned reg;
281
282 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
283 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
284}
285
286static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
287{
288 ctxt->regs_dirty = 0;
289 ctxt->regs_valid = 0;
290}
291
Avi Kivity6aa8b732006-12-10 02:21:36 -0800292/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800293 * These EFLAGS bits are restored from saved value during emulation, and
294 * any changes are written back to the saved value after emulation.
295 */
296#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
297
Avi Kivitydda96d82008-11-26 15:14:10 +0200298#ifdef CONFIG_X86_64
299#define ON64(x) x
300#else
301#define ON64(x)
302#endif
303
Avi Kivity4d758342013-01-19 19:51:55 +0200304static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *));
305
Avi Kivityb7d491e2013-01-04 16:18:49 +0200306#define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t"
307#define FOP_RET "ret \n\t"
308
309#define FOP_START(op) \
310 extern void em_##op(struct fastop *fake); \
311 asm(".pushsection .text, \"ax\" \n\t" \
312 ".global em_" #op " \n\t" \
313 FOP_ALIGN \
314 "em_" #op ": \n\t"
315
316#define FOP_END \
317 ".popsection")
318
Avi Kivity0bdea062013-01-19 19:51:50 +0200319#define FOPNOP() FOP_ALIGN FOP_RET
320
Avi Kivityb7d491e2013-01-04 16:18:49 +0200321#define FOP1E(op, dst) \
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200322 FOP_ALIGN "10: " #op " %" #dst " \n\t" FOP_RET
323
324#define FOP1EEX(op, dst) \
325 FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200326
327#define FASTOP1(op) \
328 FOP_START(op) \
329 FOP1E(op##b, al) \
330 FOP1E(op##w, ax) \
331 FOP1E(op##l, eax) \
332 ON64(FOP1E(op##q, rax)) \
333 FOP_END
334
Avi Kivityb9fa4092013-02-09 11:31:48 +0200335/* 1-operand, using src2 (for MUL/DIV r/m) */
336#define FASTOP1SRC2(op, name) \
337 FOP_START(name) \
338 FOP1E(op, cl) \
339 FOP1E(op, cx) \
340 FOP1E(op, ecx) \
341 ON64(FOP1E(op, rcx)) \
342 FOP_END
343
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200344/* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */
345#define FASTOP1SRC2EX(op, name) \
346 FOP_START(name) \
347 FOP1EEX(op, cl) \
348 FOP1EEX(op, cx) \
349 FOP1EEX(op, ecx) \
350 ON64(FOP1EEX(op, rcx)) \
351 FOP_END
352
Avi Kivityf7857f32013-01-04 16:18:53 +0200353#define FOP2E(op, dst, src) \
354 FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET
355
356#define FASTOP2(op) \
357 FOP_START(op) \
Avi Kivity017da7b2013-02-09 11:31:47 +0200358 FOP2E(op##b, al, dl) \
359 FOP2E(op##w, ax, dx) \
360 FOP2E(op##l, eax, edx) \
361 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivityf7857f32013-01-04 16:18:53 +0200362 FOP_END
363
Avi Kivity11c363b2013-01-19 19:51:54 +0200364/* 2 operand, word only */
365#define FASTOP2W(op) \
366 FOP_START(op) \
367 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200368 FOP2E(op##w, ax, dx) \
369 FOP2E(op##l, eax, edx) \
370 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivity11c363b2013-01-19 19:51:54 +0200371 FOP_END
372
Avi Kivity007a3b52013-01-19 19:51:51 +0200373/* 2 operand, src is CL */
374#define FASTOP2CL(op) \
375 FOP_START(op) \
376 FOP2E(op##b, al, cl) \
377 FOP2E(op##w, ax, cl) \
378 FOP2E(op##l, eax, cl) \
379 ON64(FOP2E(op##q, rax, cl)) \
380 FOP_END
381
Avi Kivity0bdea062013-01-19 19:51:50 +0200382#define FOP3E(op, dst, src, src2) \
383 FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
384
385/* 3-operand, word-only, src2=cl */
386#define FASTOP3WCL(op) \
387 FOP_START(op) \
388 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200389 FOP3E(op##w, ax, dx, cl) \
390 FOP3E(op##l, eax, edx, cl) \
391 ON64(FOP3E(op##q, rax, rdx, cl)) \
Avi Kivity0bdea062013-01-19 19:51:50 +0200392 FOP_END
393
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200394/* Special case for SETcc - 1 instruction per cc */
395#define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t"
396
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200397asm(".global kvm_fastop_exception \n"
398 "kvm_fastop_exception: xor %esi, %esi; ret");
399
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200400FOP_START(setcc)
401FOP_SETCC(seto)
402FOP_SETCC(setno)
403FOP_SETCC(setc)
404FOP_SETCC(setnc)
405FOP_SETCC(setz)
406FOP_SETCC(setnz)
407FOP_SETCC(setbe)
408FOP_SETCC(setnbe)
409FOP_SETCC(sets)
410FOP_SETCC(setns)
411FOP_SETCC(setp)
412FOP_SETCC(setnp)
413FOP_SETCC(setl)
414FOP_SETCC(setnl)
415FOP_SETCC(setle)
416FOP_SETCC(setnle)
417FOP_END;
418
Paolo Bonzini326f5782013-05-09 11:32:51 +0200419FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET
420FOP_END;
421
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200422static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
423 enum x86_intercept intercept,
424 enum x86_intercept_stage stage)
425{
426 struct x86_instruction_info info = {
427 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300428 .rep_prefix = ctxt->rep_prefix,
429 .modrm_mod = ctxt->modrm_mod,
430 .modrm_reg = ctxt->modrm_reg,
431 .modrm_rm = ctxt->modrm_rm,
432 .src_val = ctxt->src.val64,
Jan Kiszka6cbc5f52014-06-30 12:52:55 +0200433 .dst_val = ctxt->dst.val64,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300434 .src_bytes = ctxt->src.bytes,
435 .dst_bytes = ctxt->dst.bytes,
436 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200437 .next_rip = ctxt->eip,
438 };
439
Avi Kivity29535382011-04-20 13:37:53 +0300440 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200441}
442
Avi Kivityf47cfa32012-06-07 17:49:24 +0300443static void assign_masked(ulong *dest, ulong src, ulong mask)
444{
445 *dest = (*dest & ~mask) | (src & mask);
446}
447
Avi Kivity9dac77f2011-06-01 15:34:25 +0300448static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800449{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300450 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800451}
452
Avi Kivityf47cfa32012-06-07 17:49:24 +0300453static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
454{
455 u16 sel;
456 struct desc_struct ss;
457
458 if (ctxt->mode == X86EMUL_MODE_PROT64)
459 return ~0UL;
460 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
461 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
462}
463
Avi Kivity612e89f2012-06-12 20:03:23 +0300464static int stack_size(struct x86_emulate_ctxt *ctxt)
465{
466 return (__fls(stack_mask(ctxt)) + 1) >> 3;
467}
468
Avi Kivity6aa8b732006-12-10 02:21:36 -0800469/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800470static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300471address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800472{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300473 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800474 return reg;
475 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300476 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800477}
478
479static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300480register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800481{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300482 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800483}
484
Avi Kivity5ad105e2012-08-19 14:34:31 +0300485static void masked_increment(ulong *reg, ulong mask, int inc)
486{
487 assign_masked(reg, *reg + inc, mask);
488}
489
Harvey Harrison7a9572752008-02-19 07:40:41 -0800490static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300491register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800492{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300493 ulong mask;
494
Avi Kivity9dac77f2011-06-01 15:34:25 +0300495 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300496 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800497 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300498 mask = ad_mask(ctxt);
499 masked_increment(reg, mask, inc);
500}
501
502static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
503{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300504 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800505}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800506
Avi Kivity56697682011-04-03 14:08:51 +0300507static u32 desc_limit_scaled(struct desc_struct *desc)
508{
509 u32 limit = get_desc_limit(desc);
510
511 return desc->g ? (limit << 12) | 0xfff : limit;
512}
513
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900514static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300515{
516 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
517 return 0;
518
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900519 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300520}
521
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200522static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
523 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300524{
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +0200525 WARN_ON(vec > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +0200526 ctxt->exception.vector = vec;
527 ctxt->exception.error_code = error;
528 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200529 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300530}
531
Joerg Roedel3b88e412011-04-04 12:39:29 +0200532static int emulate_db(struct x86_emulate_ctxt *ctxt)
533{
534 return emulate_exception(ctxt, DB_VECTOR, 0, false);
535}
536
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200537static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300538{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200539 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300540}
541
Avi Kivity618ff152011-04-03 12:32:09 +0300542static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
543{
544 return emulate_exception(ctxt, SS_VECTOR, err, true);
545}
546
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200547static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300548{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200549 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300550}
551
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200552static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300553{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200554 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300555}
556
Avi Kivity34d1f492010-08-26 11:59:01 +0300557static int emulate_de(struct x86_emulate_ctxt *ctxt)
558{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200559 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300560}
561
Avi Kivity12537912011-03-29 11:41:27 +0200562static int emulate_nm(struct x86_emulate_ctxt *ctxt)
563{
564 return emulate_exception(ctxt, NM_VECTOR, 0, false);
565}
566
Nadav Amit234f3ce2014-09-18 22:39:38 +0300567static inline int assign_eip_far(struct x86_emulate_ctxt *ctxt, ulong dst,
568 int cs_l)
Nadav Amit05c83ec2014-09-18 22:39:37 +0300569{
570 switch (ctxt->op_bytes) {
571 case 2:
572 ctxt->_eip = (u16)dst;
573 break;
574 case 4:
575 ctxt->_eip = (u32)dst;
576 break;
Nadav Amit7e46ddd2014-10-28 00:03:43 +0200577#ifdef CONFIG_X86_64
Nadav Amit05c83ec2014-09-18 22:39:37 +0300578 case 8:
Nadav Amit234f3ce2014-09-18 22:39:38 +0300579 if ((cs_l && is_noncanonical_address(dst)) ||
Nadav Amit7e46ddd2014-10-28 00:03:43 +0200580 (!cs_l && (dst >> 32) != 0))
Nadav Amit234f3ce2014-09-18 22:39:38 +0300581 return emulate_gp(ctxt, 0);
Nadav Amit05c83ec2014-09-18 22:39:37 +0300582 ctxt->_eip = dst;
583 break;
Nadav Amit7e46ddd2014-10-28 00:03:43 +0200584#endif
Nadav Amit05c83ec2014-09-18 22:39:37 +0300585 default:
586 WARN(1, "unsupported eip assignment size\n");
587 }
Nadav Amit234f3ce2014-09-18 22:39:38 +0300588 return X86EMUL_CONTINUE;
Nadav Amit05c83ec2014-09-18 22:39:37 +0300589}
590
Nadav Amit234f3ce2014-09-18 22:39:38 +0300591static inline int assign_eip_near(struct x86_emulate_ctxt *ctxt, ulong dst)
Nadav Amit05c83ec2014-09-18 22:39:37 +0300592{
Nadav Amit234f3ce2014-09-18 22:39:38 +0300593 return assign_eip_far(ctxt, dst, ctxt->mode == X86EMUL_MODE_PROT64);
594}
595
596static inline int jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
597{
598 return assign_eip_near(ctxt, ctxt->_eip + rel);
Nadav Amit05c83ec2014-09-18 22:39:37 +0300599}
600
Avi Kivity1aa36612011-04-27 13:20:30 +0300601static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
602{
603 u16 selector;
604 struct desc_struct desc;
605
606 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
607 return selector;
608}
609
610static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
611 unsigned seg)
612{
613 u16 dummy;
614 u32 base3;
615 struct desc_struct desc;
616
617 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
618 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
619}
620
Avi Kivity1c11b372012-04-09 18:39:59 +0300621/*
622 * x86 defines three classes of vector instructions: explicitly
623 * aligned, explicitly unaligned, and the rest, which change behaviour
624 * depending on whether they're AVX encoded or not.
625 *
626 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
627 * subject to the same check.
628 */
629static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
630{
631 if (likely(size < 16))
632 return false;
633
634 if (ctxt->d & Aligned)
635 return true;
636 else if (ctxt->d & Unaligned)
637 return false;
638 else if (ctxt->d & Avx)
639 return false;
640 else
641 return true;
642}
643
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400644static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300645 struct segmented_address addr,
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100646 unsigned *max_size, unsigned size,
647 bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300648 ulong *linear)
649{
Avi Kivity618ff152011-04-03 12:32:09 +0300650 struct desc_struct desc;
651 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300652 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300653 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300654 u16 sel;
Gleb Natapov3a78a4f2012-12-20 16:57:42 +0200655 unsigned cpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300656
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900657 la = seg_base(ctxt, addr.seg) + addr.ea;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100658 *max_size = 0;
Avi Kivity618ff152011-04-03 12:32:09 +0300659 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300660 case X86EMUL_MODE_PROT64:
661 if (((signed long)la << 16) >> 16 != la)
662 return emulate_gp(ctxt, 0);
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100663
664 *max_size = min_t(u64, ~0u, (1ull << 48) - la);
665 if (size > *max_size)
666 goto bad;
Avi Kivity618ff152011-04-03 12:32:09 +0300667 break;
668 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300669 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
670 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300671 if (!usable)
672 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200673 /* code segment in protected mode or read-only data segment */
674 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
675 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300676 goto bad;
677 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400678 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300679 goto bad;
680 lim = desc_limit_scaled(&desc);
Nadav Amit10e38fc2014-06-18 17:19:34 +0300681 if ((ctxt->mode == X86EMUL_MODE_REAL) && !fetch &&
682 (ctxt->d & NoBigReal)) {
683 /* la is between zero and 0xffff */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100684 if (la > 0xffff)
Nadav Amit10e38fc2014-06-18 17:19:34 +0300685 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100686 *max_size = 0x10000 - la;
Nadav Amit10e38fc2014-06-18 17:19:34 +0300687 } else if ((desc.type & 8) || !(desc.type & 4)) {
Avi Kivity618ff152011-04-03 12:32:09 +0300688 /* expand-up segment */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100689 if (addr.ea > lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300690 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100691 *max_size = min_t(u64, ~0u, (u64)lim + 1 - addr.ea);
Avi Kivity618ff152011-04-03 12:32:09 +0300692 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800693 /* expand-down segment */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100694 if (addr.ea <= lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300695 goto bad;
696 lim = desc.d ? 0xffffffff : 0xffff;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100697 if (addr.ea > lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300698 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100699 *max_size = min_t(u64, ~0u, (u64)lim + 1 - addr.ea);
Avi Kivity618ff152011-04-03 12:32:09 +0300700 }
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100701 if (size > *max_size)
702 goto bad;
Avi Kivity717746e2011-04-20 13:37:53 +0300703 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity618ff152011-04-03 12:32:09 +0300704 if (!(desc.type & 8)) {
705 /* data segment */
706 if (cpl > desc.dpl)
707 goto bad;
708 } else if ((desc.type & 8) && !(desc.type & 4)) {
709 /* nonconforming code segment */
710 if (cpl != desc.dpl)
711 goto bad;
712 } else if ((desc.type & 8) && (desc.type & 4)) {
713 /* conforming code segment */
714 if (cpl < desc.dpl)
715 goto bad;
716 }
717 break;
718 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300719 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300720 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300721 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
722 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300723 *linear = la;
724 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300725bad:
726 if (addr.seg == VCPU_SREG_SS)
Paolo Bonzini36061892014-10-27 14:40:49 +0100727 return emulate_ss(ctxt, 0);
Avi Kivity618ff152011-04-03 12:32:09 +0300728 else
Paolo Bonzini36061892014-10-27 14:40:49 +0100729 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300730}
731
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400732static int linearize(struct x86_emulate_ctxt *ctxt,
733 struct segmented_address addr,
734 unsigned size, bool write,
735 ulong *linear)
736{
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100737 unsigned max_size;
738 return __linearize(ctxt, addr, &max_size, size, write, false, linear);
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400739}
740
741
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200742static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
743 struct segmented_address addr,
744 void *data,
745 unsigned size)
746{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200747 int rc;
748 ulong linear;
749
Avi Kivity83b87952011-04-03 11:31:19 +0300750 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200751 if (rc != X86EMUL_CONTINUE)
752 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300753 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200754}
755
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900756/*
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200757 * Prefetch the remaining bytes of the instruction without crossing page
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900758 * boundary if they are not in fetch_cache yet.
759 */
Paolo Bonzini9506d572014-05-06 13:05:25 +0200760static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
Avi Kivity62266862007-11-20 13:15:52 +0200761{
Avi Kivity62266862007-11-20 13:15:52 +0200762 int rc;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100763 unsigned size, max_size;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200764 unsigned long linear;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200765 int cur_size = ctxt->fetch.end - ctxt->fetch.data;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200766 struct segmented_address addr = { .seg = VCPU_SREG_CS,
Paolo Bonzini17052f12014-05-06 16:33:01 +0200767 .ea = ctxt->eip + cur_size };
768
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100769 /*
770 * We do not know exactly how many bytes will be needed, and
771 * __linearize is expensive, so fetch as much as possible. We
772 * just have to avoid going beyond the 15 byte limit, the end
773 * of the segment, or the end of the page.
774 *
775 * __linearize is called with size 0 so that it does not do any
776 * boundary check itself. Instead, we use max_size to check
777 * against op_size.
778 */
779 rc = __linearize(ctxt, addr, &max_size, 0, false, true, &linear);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200780 if (unlikely(rc != X86EMUL_CONTINUE))
781 return rc;
782
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100783 size = min_t(unsigned, 15UL ^ cur_size, max_size);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200784 size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear));
Paolo Bonzini5cfc7e02014-05-06 13:05:25 +0200785
786 /*
787 * One instruction can only straddle two pages,
788 * and one has been loaded at the beginning of
789 * x86_decode_insn. So, if not enough bytes
790 * still, we must have hit the 15-byte boundary.
791 */
792 if (unlikely(size < op_size))
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100793 return emulate_gp(ctxt, 0);
794
Paolo Bonzini17052f12014-05-06 16:33:01 +0200795 rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end,
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200796 size, &ctxt->exception);
797 if (unlikely(rc != X86EMUL_CONTINUE))
798 return rc;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200799 ctxt->fetch.end += size;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900800 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200801}
802
Paolo Bonzini9506d572014-05-06 13:05:25 +0200803static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt,
804 unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200805{
Nadav Amit08da44a2014-10-03 01:10:04 +0300806 unsigned done_size = ctxt->fetch.end - ctxt->fetch.ptr;
807
808 if (unlikely(done_size < size))
809 return __do_insn_fetch_bytes(ctxt, size - done_size);
Paolo Bonzini9506d572014-05-06 13:05:25 +0200810 else
811 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200812}
813
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900814/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900815#define insn_fetch(_type, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200816({ _type _x; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200817 \
818 rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900819 if (rc != X86EMUL_CONTINUE) \
820 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200821 ctxt->_eip += sizeof(_type); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200822 _x = *(_type __aligned(1) *) ctxt->fetch.ptr; \
823 ctxt->fetch.ptr += sizeof(_type); \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200824 _x; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900825})
826
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900827#define insn_fetch_arr(_arr, _size, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200828({ \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200829 rc = do_insn_fetch_bytes(_ctxt, _size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900830 if (rc != X86EMUL_CONTINUE) \
831 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200832 ctxt->_eip += (_size); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200833 memcpy(_arr, ctxt->fetch.ptr, _size); \
834 ctxt->fetch.ptr += (_size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900835})
836
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000837/*
838 * Given the 'reg' portion of a ModRM byte, and a register block, return a
839 * pointer into the block that addresses the relevant register.
840 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
841 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300842static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200843 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800844{
845 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200846 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300849 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
850 else
851 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800852 return p;
853}
854
855static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200856 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800857 u16 *size, unsigned long *address, int op_bytes)
858{
859 int rc;
860
861 if (op_bytes == 2)
862 op_bytes = 3;
863 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200864 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900865 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800866 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200867 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200868 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800869 return rc;
870}
871
Avi Kivity34b77652013-01-19 19:51:56 +0200872FASTOP2(add);
873FASTOP2(or);
874FASTOP2(adc);
875FASTOP2(sbb);
876FASTOP2(and);
877FASTOP2(sub);
878FASTOP2(xor);
879FASTOP2(cmp);
880FASTOP2(test);
881
Avi Kivityb9fa4092013-02-09 11:31:48 +0200882FASTOP1SRC2(mul, mul_ex);
883FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200884FASTOP1SRC2EX(div, div_ex);
885FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +0200886
Avi Kivity34b77652013-01-19 19:51:56 +0200887FASTOP3WCL(shld);
888FASTOP3WCL(shrd);
889
890FASTOP2W(imul);
891
892FASTOP1(not);
893FASTOP1(neg);
894FASTOP1(inc);
895FASTOP1(dec);
896
897FASTOP2CL(rol);
898FASTOP2CL(ror);
899FASTOP2CL(rcl);
900FASTOP2CL(rcr);
901FASTOP2CL(shl);
902FASTOP2CL(shr);
903FASTOP2CL(sar);
904
905FASTOP2W(bsf);
906FASTOP2W(bsr);
907FASTOP2W(bt);
908FASTOP2W(bts);
909FASTOP2W(btr);
910FASTOP2W(btc);
911
Avi Kivitye47a5f52013-02-09 11:31:51 +0200912FASTOP2(xadd);
913
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200914static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300915{
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200916 u8 rc;
917 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300918
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200919 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +0200920 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200921 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
922 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300923}
924
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300925static void fetch_register_operand(struct operand *op)
926{
927 switch (op->bytes) {
928 case 1:
929 op->val = *(u8 *)op->addr.reg;
930 break;
931 case 2:
932 op->val = *(u16 *)op->addr.reg;
933 break;
934 case 4:
935 op->val = *(u32 *)op->addr.reg;
936 break;
937 case 8:
938 op->val = *(u64 *)op->addr.reg;
939 break;
940 }
941}
942
Avi Kivity12537912011-03-29 11:41:27 +0200943static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
944{
945 ctxt->ops->get_fpu(ctxt);
946 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200947 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
948 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
949 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
950 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
951 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
952 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
953 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
954 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200955#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200956 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
957 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
958 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
959 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
960 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
961 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
962 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
963 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200964#endif
965 default: BUG();
966 }
967 ctxt->ops->put_fpu(ctxt);
968}
969
970static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
971 int reg)
972{
973 ctxt->ops->get_fpu(ctxt);
974 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200975 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
976 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
977 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
978 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
979 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
980 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
981 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
982 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200983#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200984 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
985 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
986 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
987 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
988 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
989 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
990 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
991 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200992#endif
993 default: BUG();
994 }
995 ctxt->ops->put_fpu(ctxt);
996}
997
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300998static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
999{
1000 ctxt->ops->get_fpu(ctxt);
1001 switch (reg) {
1002 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1003 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1004 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1005 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1006 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1007 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1008 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1009 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1010 default: BUG();
1011 }
1012 ctxt->ops->put_fpu(ctxt);
1013}
1014
1015static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1016{
1017 ctxt->ops->get_fpu(ctxt);
1018 switch (reg) {
1019 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1020 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1021 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1022 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1023 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1024 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1025 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1026 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1027 default: BUG();
1028 }
1029 ctxt->ops->put_fpu(ctxt);
1030}
1031
Gleb Natapov045a2822012-12-20 16:57:43 +02001032static int em_fninit(struct x86_emulate_ctxt *ctxt)
1033{
1034 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1035 return emulate_nm(ctxt);
1036
1037 ctxt->ops->get_fpu(ctxt);
1038 asm volatile("fninit");
1039 ctxt->ops->put_fpu(ctxt);
1040 return X86EMUL_CONTINUE;
1041}
1042
1043static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1044{
1045 u16 fcw;
1046
1047 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1048 return emulate_nm(ctxt);
1049
1050 ctxt->ops->get_fpu(ctxt);
1051 asm volatile("fnstcw %0": "+m"(fcw));
1052 ctxt->ops->put_fpu(ctxt);
1053
1054 /* force 2 byte destination */
1055 ctxt->dst.bytes = 2;
1056 ctxt->dst.val = fcw;
1057
1058 return X86EMUL_CONTINUE;
1059}
1060
1061static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1062{
1063 u16 fsw;
1064
1065 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1066 return emulate_nm(ctxt);
1067
1068 ctxt->ops->get_fpu(ctxt);
1069 asm volatile("fnstsw %0": "+m"(fsw));
1070 ctxt->ops->put_fpu(ctxt);
1071
1072 /* force 2 byte destination */
1073 ctxt->dst.bytes = 2;
1074 ctxt->dst.val = fsw;
1075
1076 return X86EMUL_CONTINUE;
1077}
1078
Avi Kivity12537912011-03-29 11:41:27 +02001079static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001080 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001081{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001082 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001083
Avi Kivity9dac77f2011-06-01 15:34:25 +03001084 if (!(ctxt->d & ModRM))
1085 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +02001086
Avi Kivity9dac77f2011-06-01 15:34:25 +03001087 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001088 op->type = OP_XMM;
1089 op->bytes = 16;
1090 op->addr.xmm = reg;
1091 read_sse_reg(ctxt, &op->vec_val, reg);
1092 return;
1093 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001094 if (ctxt->d & Mmx) {
1095 reg &= 7;
1096 op->type = OP_MM;
1097 op->bytes = 8;
1098 op->addr.mm = reg;
1099 return;
1100 }
Avi Kivity12537912011-03-29 11:41:27 +02001101
Avi Kivity3c118e22007-10-31 10:27:04 +02001102 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001103 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1104 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1105
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001106 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001107 op->orig_val = op->val;
1108}
1109
Avi Kivitya6e34072012-06-10 17:15:39 +03001110static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1111{
1112 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1113 ctxt->modrm_seg = VCPU_SREG_SS;
1114}
1115
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001116static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001117 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001118{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001119 u8 sib;
Bandan Das02357bd2014-04-16 12:46:11 -04001120 int index_reg, base_reg, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001121 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001122 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001123
Bandan Das02357bd2014-04-16 12:46:11 -04001124 ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */
1125 index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */
1126 base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001127
Bandan Das02357bd2014-04-16 12:46:11 -04001128 ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001129 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
Bandan Das02357bd2014-04-16 12:46:11 -04001130 ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001131 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001132
Nadav Amit9b88ae92014-05-25 23:05:21 +03001133 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001134 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001135 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001136 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001137 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001138 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001139 op->type = OP_XMM;
1140 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001141 op->addr.xmm = ctxt->modrm_rm;
1142 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001143 return rc;
1144 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001145 if (ctxt->d & Mmx) {
1146 op->type = OP_MM;
1147 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001148 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001149 return rc;
1150 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001151 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001152 return rc;
1153 }
1154
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001155 op->type = OP_MEM;
1156
Avi Kivity9dac77f2011-06-01 15:34:25 +03001157 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001158 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1159 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1160 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1161 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001162
1163 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001164 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001165 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001166 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001167 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001168 break;
1169 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001170 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001171 break;
1172 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001173 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001174 break;
1175 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001176 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001177 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001178 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001179 break;
1180 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001181 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001182 break;
1183 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001184 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001185 break;
1186 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001187 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001188 break;
1189 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001190 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001191 break;
1192 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001193 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001194 break;
1195 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001196 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001197 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001198 break;
1199 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001200 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001201 break;
1202 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001203 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1204 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1205 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001206 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001207 } else {
1208 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001209 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001210 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001211 index_reg |= (sib >> 3) & 7;
1212 base_reg |= sib & 7;
1213 scale = sib >> 6;
1214
Avi Kivity9dac77f2011-06-01 15:34:25 +03001215 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001216 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001217 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001218 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001219 adjust_modrm_seg(ctxt, base_reg);
1220 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001221 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001222 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001223 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001224 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001225 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001226 } else {
1227 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001228 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001229 adjust_modrm_seg(ctxt, base_reg);
1230 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001231 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001232 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001233 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001234 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001235 break;
1236 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001237 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001238 break;
1239 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001240 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001241 break;
1242 }
1243 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001244 op->addr.mem.ea = modrm_ea;
Bandan Das41061cd2014-04-16 12:46:14 -04001245 if (ctxt->ad_bytes != 8)
1246 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
1247
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001248done:
1249 return rc;
1250}
1251
1252static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001253 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001254{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001255 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001256
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001257 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001258 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001259 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001260 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001261 break;
1262 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001263 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001264 break;
1265 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001266 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001267 break;
1268 }
1269done:
1270 return rc;
1271}
1272
Avi Kivity9dac77f2011-06-01 15:34:25 +03001273static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001274{
Sheng Yang7129eec2010-09-28 16:33:32 +08001275 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001276
Avi Kivity9dac77f2011-06-01 15:34:25 +03001277 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001278 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001279
Avi Kivity9dac77f2011-06-01 15:34:25 +03001280 if (ctxt->src.bytes == 2)
1281 sv = (s16)ctxt->src.val & (s16)mask;
1282 else if (ctxt->src.bytes == 4)
1283 sv = (s32)ctxt->src.val & (s32)mask;
Nadav Amit7dec5602014-06-15 16:12:57 +03001284 else
1285 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001286
Avi Kivity9dac77f2011-06-01 15:34:25 +03001287 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001288 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001289
1290 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001291 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001292}
1293
Gleb Natapov9de41572010-04-28 19:15:22 +03001294static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001295 unsigned long addr, void *dest, unsigned size)
1296{
1297 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001298 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001299
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001300 if (mc->pos < mc->end)
1301 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001302
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001303 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001304
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001305 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1306 &ctxt->exception);
1307 if (rc != X86EMUL_CONTINUE)
1308 return rc;
1309
1310 mc->end += size;
1311
1312read_cached:
1313 memcpy(dest, mc->data + mc->pos, size);
1314 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001315 return X86EMUL_CONTINUE;
1316}
1317
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001318static int segmented_read(struct x86_emulate_ctxt *ctxt,
1319 struct segmented_address addr,
1320 void *data,
1321 unsigned size)
1322{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001323 int rc;
1324 ulong linear;
1325
Avi Kivity83b87952011-04-03 11:31:19 +03001326 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001327 if (rc != X86EMUL_CONTINUE)
1328 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001329 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001330}
1331
1332static int segmented_write(struct x86_emulate_ctxt *ctxt,
1333 struct segmented_address addr,
1334 const void *data,
1335 unsigned size)
1336{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001337 int rc;
1338 ulong linear;
1339
Avi Kivity83b87952011-04-03 11:31:19 +03001340 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001341 if (rc != X86EMUL_CONTINUE)
1342 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001343 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1344 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001345}
1346
1347static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1348 struct segmented_address addr,
1349 const void *orig_data, const void *data,
1350 unsigned size)
1351{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001352 int rc;
1353 ulong linear;
1354
Avi Kivity83b87952011-04-03 11:31:19 +03001355 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001356 if (rc != X86EMUL_CONTINUE)
1357 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001358 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1359 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001360}
1361
Gleb Natapov7b262e92010-03-18 15:20:27 +02001362static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001363 unsigned int size, unsigned short port,
1364 void *dest)
1365{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001366 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001367
1368 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001369 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001370 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001371 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001372 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001373 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1374 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Mark Rustadb55a8142014-07-25 06:27:05 -07001375 n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001376 if (n == 0)
1377 n = 1;
1378 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001379 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001380 return 0;
1381 rc->end = n * size;
1382 }
1383
Nadav Amite6e39f02014-04-18 03:35:10 +03001384 if (ctxt->rep_prefix && (ctxt->d & String) &&
1385 !(ctxt->eflags & EFLG_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001386 ctxt->dst.data = rc->data + rc->pos;
1387 ctxt->dst.type = OP_MEM_STR;
1388 ctxt->dst.count = (rc->end - rc->pos) / size;
1389 rc->pos = rc->end;
1390 } else {
1391 memcpy(dest, rc->data + rc->pos, size);
1392 rc->pos += size;
1393 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001394 return 1;
1395}
1396
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001397static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1398 u16 index, struct desc_struct *desc)
1399{
1400 struct desc_ptr dt;
1401 ulong addr;
1402
1403 ctxt->ops->get_idt(ctxt, &dt);
1404
1405 if (dt.size < index * 8 + 7)
1406 return emulate_gp(ctxt, index << 3 | 0x2);
1407
1408 addr = dt.address + index * 8;
1409 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1410 &ctxt->exception);
1411}
1412
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001413static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001414 u16 selector, struct desc_ptr *dt)
1415{
Mathias Krause0225fb52012-08-30 01:30:16 +02001416 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001417 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001418
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001419 if (selector & 1 << 2) {
1420 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001421 u16 sel;
1422
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001423 memset (dt, 0, sizeof *dt);
Nadav Amit2eedcac2014-06-02 18:34:05 +03001424 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1425 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001426 return;
1427
1428 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001429 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001430 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001431 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001432}
1433
1434/* allowed just for 8 bytes segments */
1435static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001436 u16 selector, struct desc_struct *desc,
1437 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001438{
1439 struct desc_ptr dt;
1440 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001441 ulong addr;
1442
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001443 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001444
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001445 if (dt.size < index * 8 + 7)
1446 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001447
Avi Kivitye9194642012-06-13 16:29:39 +03001448 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001449 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1450 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001451}
1452
1453/* allowed just for 8 bytes segments */
1454static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001455 u16 selector, struct desc_struct *desc)
1456{
1457 struct desc_ptr dt;
1458 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001459 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001460
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001461 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001462
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001463 if (dt.size < index * 8 + 7)
1464 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001465
1466 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001467 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1468 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001469}
1470
Gleb Natapov5601d052011-03-07 14:55:06 +02001471/* Does not support long mode */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001472static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Nadav Amitd1442d82014-09-18 22:39:39 +03001473 u16 selector, int seg, u8 cpl,
1474 bool in_task_switch,
1475 struct desc_struct *desc)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001476{
Avi Kivity869be992012-06-13 16:30:53 +03001477 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001478 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001479 unsigned err_vec = GP_VECTOR;
1480 u32 err_code = 0;
1481 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001482 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001483 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001484 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001485 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001486
1487 memset(&seg_desc, 0, sizeof seg_desc);
1488
Kevin Wolff8da94e2013-04-11 14:06:03 +02001489 if (ctxt->mode == X86EMUL_MODE_REAL) {
1490 /* set real mode segment descriptor (keep limit etc. for
1491 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001492 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001493 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001494 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001495 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1496 /* VM86 needs a clean new segment descriptor */
1497 set_desc_base(&seg_desc, selector << 4);
1498 set_desc_limit(&seg_desc, 0xffff);
1499 seg_desc.type = 3;
1500 seg_desc.p = 1;
1501 seg_desc.s = 1;
1502 seg_desc.dpl = 3;
1503 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001504 }
1505
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001506 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001507
1508 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1509 if ((seg == VCPU_SREG_CS
1510 || (seg == VCPU_SREG_SS
1511 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1512 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001513 && null_selector)
1514 goto exception;
1515
1516 /* TR should be in GDT only */
1517 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1518 goto exception;
1519
1520 if (null_selector) /* for NULL selector skip all following checks */
1521 goto load;
1522
Avi Kivitye9194642012-06-13 16:29:39 +03001523 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001524 if (ret != X86EMUL_CONTINUE)
1525 return ret;
1526
1527 err_code = selector & 0xfffc;
Paolo Bonzini15fc0752014-08-18 13:17:00 +02001528 err_vec = in_task_switch ? TS_VECTOR : GP_VECTOR;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001529
Guo Chaofc058682012-06-28 15:19:51 +08001530 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001531 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1532 goto exception;
1533
1534 if (!seg_desc.p) {
1535 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1536 goto exception;
1537 }
1538
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001539 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001540
1541 switch (seg) {
1542 case VCPU_SREG_SS:
1543 /*
1544 * segment is not a writable data segment or segment
1545 * selector's RPL != CPL or segment selector's RPL != CPL
1546 */
1547 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1548 goto exception;
1549 break;
1550 case VCPU_SREG_CS:
1551 if (!(seg_desc.type & 8))
1552 goto exception;
1553
1554 if (seg_desc.type & 4) {
1555 /* conforming */
1556 if (dpl > cpl)
1557 goto exception;
1558 } else {
1559 /* nonconforming */
1560 if (rpl > cpl || dpl != cpl)
1561 goto exception;
1562 }
Nadav Amit040c8dc2014-09-18 22:39:43 +03001563 /* in long-mode d/b must be clear if l is set */
1564 if (seg_desc.d && seg_desc.l) {
1565 u64 efer = 0;
1566
1567 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1568 if (efer & EFER_LMA)
1569 goto exception;
1570 }
1571
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001572 /* CS(RPL) <- CPL */
1573 selector = (selector & 0xfffc) | cpl;
1574 break;
1575 case VCPU_SREG_TR:
1576 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1577 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001578 old_desc = seg_desc;
1579 seg_desc.type |= 2; /* busy */
1580 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1581 sizeof(seg_desc), &ctxt->exception);
1582 if (ret != X86EMUL_CONTINUE)
1583 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001584 break;
1585 case VCPU_SREG_LDTR:
1586 if (seg_desc.s || seg_desc.type != 2)
1587 goto exception;
1588 break;
1589 default: /* DS, ES, FS, or GS */
1590 /*
1591 * segment is not a data or readable code segment or
1592 * ((segment is a data or nonconforming code segment)
1593 * and (both RPL and CPL > DPL))
1594 */
1595 if ((seg_desc.type & 0xa) == 0x8 ||
1596 (((seg_desc.type & 0xc) != 0xc) &&
1597 (rpl > dpl && cpl > dpl)))
1598 goto exception;
1599 break;
1600 }
1601
1602 if (seg_desc.s) {
1603 /* mark segment as accessed */
1604 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001605 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001606 if (ret != X86EMUL_CONTINUE)
1607 return ret;
Nadav Amite37a75a2014-06-02 18:34:04 +03001608 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
1609 ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3,
1610 sizeof(base3), &ctxt->exception);
1611 if (ret != X86EMUL_CONTINUE)
1612 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001613 }
1614load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001615 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Nadav Amitd1442d82014-09-18 22:39:39 +03001616 if (desc)
1617 *desc = seg_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001618 return X86EMUL_CONTINUE;
1619exception:
Paolo Bonzini592f0852014-08-20 10:05:08 +02001620 return emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001621}
1622
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001623static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1624 u16 selector, int seg)
1625{
1626 u8 cpl = ctxt->ops->cpl(ctxt);
Nadav Amitd1442d82014-09-18 22:39:39 +03001627 return __load_segment_descriptor(ctxt, selector, seg, cpl, false, NULL);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001628}
1629
Wei Yongjun31be40b2010-08-17 09:17:30 +08001630static void write_register_operand(struct operand *op)
1631{
1632 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1633 switch (op->bytes) {
1634 case 1:
1635 *(u8 *)op->addr.reg = (u8)op->val;
1636 break;
1637 case 2:
1638 *(u16 *)op->addr.reg = (u16)op->val;
1639 break;
1640 case 4:
1641 *op->addr.reg = (u32)op->val;
1642 break; /* 64b: zero-extend */
1643 case 8:
1644 *op->addr.reg = op->val;
1645 break;
1646 }
1647}
1648
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001649static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001650{
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001651 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001652 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001653 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001654 break;
1655 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001656 if (ctxt->lock_prefix)
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001657 return segmented_cmpxchg(ctxt,
1658 op->addr.mem,
1659 &op->orig_val,
1660 &op->val,
1661 op->bytes);
1662 else
1663 return segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001664 op->addr.mem,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001665 &op->val,
1666 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001667 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001668 case OP_MEM_STR:
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001669 return segmented_write(ctxt,
1670 op->addr.mem,
1671 op->data,
1672 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001673 break;
Avi Kivity12537912011-03-29 11:41:27 +02001674 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001675 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001676 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001677 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001678 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001679 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001680 case OP_NONE:
1681 /* no writeback */
1682 break;
1683 default:
1684 break;
1685 }
1686 return X86EMUL_CONTINUE;
1687}
1688
Avi Kivity51ddff52012-06-12 20:19:40 +03001689static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001690{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001691 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001692
Avi Kivity5ad105e2012-08-19 14:34:31 +03001693 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001694 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001695 addr.seg = VCPU_SREG_SS;
1696
Avi Kivity51ddff52012-06-12 20:19:40 +03001697 return segmented_write(ctxt, addr, data, bytes);
1698}
1699
1700static int em_push(struct x86_emulate_ctxt *ctxt)
1701{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001702 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001703 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001704 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001705}
1706
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001707static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001708 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001709{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001710 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001711 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001712
Avi Kivitydd856ef2012-08-27 23:46:17 +03001713 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001714 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001715 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001716 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001717 return rc;
1718
Avi Kivity5ad105e2012-08-19 14:34:31 +03001719 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001720 return rc;
1721}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001722
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001723static int em_pop(struct x86_emulate_ctxt *ctxt)
1724{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001725 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001726}
1727
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001728static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001729 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001730{
1731 int rc;
1732 unsigned long val, change_mask;
1733 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001734 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001735
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001736 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001737 if (rc != X86EMUL_CONTINUE)
1738 return rc;
1739
1740 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
Nadav Amit163b1352014-07-21 14:37:28 +03001741 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_AC | EFLG_ID;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001742
1743 switch(ctxt->mode) {
1744 case X86EMUL_MODE_PROT64:
1745 case X86EMUL_MODE_PROT32:
1746 case X86EMUL_MODE_PROT16:
1747 if (cpl == 0)
1748 change_mask |= EFLG_IOPL;
1749 if (cpl <= iopl)
1750 change_mask |= EFLG_IF;
1751 break;
1752 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001753 if (iopl < 3)
1754 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001755 change_mask |= EFLG_IF;
1756 break;
1757 default: /* real mode */
1758 change_mask |= (EFLG_IOPL | EFLG_IF);
1759 break;
1760 }
1761
1762 *(unsigned long *)dest =
1763 (ctxt->eflags & ~change_mask) | (val & change_mask);
1764
1765 return rc;
1766}
1767
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001768static int em_popf(struct x86_emulate_ctxt *ctxt)
1769{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001770 ctxt->dst.type = OP_REG;
1771 ctxt->dst.addr.reg = &ctxt->eflags;
1772 ctxt->dst.bytes = ctxt->op_bytes;
1773 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001774}
1775
Avi Kivity612e89f2012-06-12 20:03:23 +03001776static int em_enter(struct x86_emulate_ctxt *ctxt)
1777{
1778 int rc;
1779 unsigned frame_size = ctxt->src.val;
1780 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001781 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001782
1783 if (nesting_level)
1784 return X86EMUL_UNHANDLEABLE;
1785
Avi Kivitydd856ef2012-08-27 23:46:17 +03001786 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1787 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001788 if (rc != X86EMUL_CONTINUE)
1789 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001790 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001791 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001792 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1793 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001794 stack_mask(ctxt));
1795 return X86EMUL_CONTINUE;
1796}
1797
Avi Kivityf47cfa32012-06-07 17:49:24 +03001798static int em_leave(struct x86_emulate_ctxt *ctxt)
1799{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001800 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001801 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001802 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001803}
1804
Avi Kivity1cd196e2011-09-13 10:45:51 +03001805static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001806{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001807 int seg = ctxt->src2.val;
1808
Avi Kivity9dac77f2011-06-01 15:34:25 +03001809 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001810
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001811 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001812}
1813
Avi Kivity1cd196e2011-09-13 10:45:51 +03001814static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001815{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001816 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001817 unsigned long selector;
1818 int rc;
1819
Avi Kivity9dac77f2011-06-01 15:34:25 +03001820 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001821 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001822 return rc;
1823
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001824 if (ctxt->modrm_reg == VCPU_SREG_SS)
1825 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
1826
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001827 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001828 return rc;
1829}
1830
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001831static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001832{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001833 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001834 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001835 int reg = VCPU_REGS_RAX;
1836
1837 while (reg <= VCPU_REGS_RDI) {
1838 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001839 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001840
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001841 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001842 if (rc != X86EMUL_CONTINUE)
1843 return rc;
1844
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001845 ++reg;
1846 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001847
Wei Yongjunc37eda12010-06-15 09:03:33 +08001848 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001849}
1850
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001851static int em_pushf(struct x86_emulate_ctxt *ctxt)
1852{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001853 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001854 return em_push(ctxt);
1855}
1856
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001857static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001858{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001859 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001860 int reg = VCPU_REGS_RDI;
1861
1862 while (reg >= VCPU_REGS_RAX) {
1863 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001864 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001865 --reg;
1866 }
1867
Avi Kivitydd856ef2012-08-27 23:46:17 +03001868 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001869 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001870 break;
1871 --reg;
1872 }
1873 return rc;
1874}
1875
Avi Kivitydd856ef2012-08-27 23:46:17 +03001876static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001877{
Mathias Krause0225fb52012-08-30 01:30:16 +02001878 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001879 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001880 struct desc_ptr dt;
1881 gva_t cs_addr;
1882 gva_t eip_addr;
1883 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001884
1885 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001886 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001887 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001888 if (rc != X86EMUL_CONTINUE)
1889 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001890
1891 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1892
Avi Kivity9dac77f2011-06-01 15:34:25 +03001893 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001894 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001895 if (rc != X86EMUL_CONTINUE)
1896 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001897
Avi Kivity9dac77f2011-06-01 15:34:25 +03001898 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001899 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001900 if (rc != X86EMUL_CONTINUE)
1901 return rc;
1902
Avi Kivity4bff1e862011-04-20 13:37:53 +03001903 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001904
1905 eip_addr = dt.address + (irq << 2);
1906 cs_addr = dt.address + (irq << 2) + 2;
1907
Avi Kivity0f65dd72011-04-20 13:37:53 +03001908 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001909 if (rc != X86EMUL_CONTINUE)
1910 return rc;
1911
Avi Kivity0f65dd72011-04-20 13:37:53 +03001912 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001913 if (rc != X86EMUL_CONTINUE)
1914 return rc;
1915
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001916 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001917 if (rc != X86EMUL_CONTINUE)
1918 return rc;
1919
Avi Kivity9dac77f2011-06-01 15:34:25 +03001920 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001921
1922 return rc;
1923}
1924
Avi Kivitydd856ef2012-08-27 23:46:17 +03001925int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1926{
1927 int rc;
1928
1929 invalidate_registers(ctxt);
1930 rc = __emulate_int_real(ctxt, irq);
1931 if (rc == X86EMUL_CONTINUE)
1932 writeback_registers(ctxt);
1933 return rc;
1934}
1935
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001936static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001937{
1938 switch(ctxt->mode) {
1939 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001940 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001941 case X86EMUL_MODE_VM86:
1942 case X86EMUL_MODE_PROT16:
1943 case X86EMUL_MODE_PROT32:
1944 case X86EMUL_MODE_PROT64:
1945 default:
1946 /* Protected mode interrupts unimplemented yet */
1947 return X86EMUL_UNHANDLEABLE;
1948 }
1949}
1950
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001951static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001952{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001953 int rc = X86EMUL_CONTINUE;
1954 unsigned long temp_eip = 0;
1955 unsigned long temp_eflags = 0;
1956 unsigned long cs = 0;
1957 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1958 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1959 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1960 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1961
1962 /* TODO: Add stack limit check */
1963
Avi Kivity9dac77f2011-06-01 15:34:25 +03001964 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001965
1966 if (rc != X86EMUL_CONTINUE)
1967 return rc;
1968
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001969 if (temp_eip & ~0xffff)
1970 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001971
Avi Kivity9dac77f2011-06-01 15:34:25 +03001972 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001973
1974 if (rc != X86EMUL_CONTINUE)
1975 return rc;
1976
Avi Kivity9dac77f2011-06-01 15:34:25 +03001977 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001978
1979 if (rc != X86EMUL_CONTINUE)
1980 return rc;
1981
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001982 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001983
1984 if (rc != X86EMUL_CONTINUE)
1985 return rc;
1986
Avi Kivity9dac77f2011-06-01 15:34:25 +03001987 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001988
1989
Avi Kivity9dac77f2011-06-01 15:34:25 +03001990 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001991 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001992 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001993 ctxt->eflags &= ~0xffff;
1994 ctxt->eflags |= temp_eflags;
1995 }
1996
1997 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1998 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1999
2000 return rc;
2001}
2002
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002003static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002004{
2005 switch(ctxt->mode) {
2006 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002007 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002008 case X86EMUL_MODE_VM86:
2009 case X86EMUL_MODE_PROT16:
2010 case X86EMUL_MODE_PROT32:
2011 case X86EMUL_MODE_PROT64:
2012 default:
2013 /* iret from protected mode unimplemented yet */
2014 return X86EMUL_UNHANDLEABLE;
2015 }
2016}
2017
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002018static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2019{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002020 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002021 unsigned short sel, old_sel;
2022 struct desc_struct old_desc, new_desc;
2023 const struct x86_emulate_ops *ops = ctxt->ops;
2024 u8 cpl = ctxt->ops->cpl(ctxt);
2025
2026 /* Assignment of RIP may only fail in 64-bit mode */
2027 if (ctxt->mode == X86EMUL_MODE_PROT64)
2028 ops->get_segment(ctxt, &old_sel, &old_desc, NULL,
2029 VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002030
Avi Kivity9dac77f2011-06-01 15:34:25 +03002031 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002032
Nadav Amitd1442d82014-09-18 22:39:39 +03002033 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl, false,
2034 &new_desc);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002035 if (rc != X86EMUL_CONTINUE)
2036 return rc;
2037
Nadav Amitd1442d82014-09-18 22:39:39 +03002038 rc = assign_eip_far(ctxt, ctxt->src.val, new_desc.l);
2039 if (rc != X86EMUL_CONTINUE) {
Nadav Amit7e46ddd2014-10-28 00:03:43 +02002040 WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64);
Nadav Amitd1442d82014-09-18 22:39:39 +03002041 /* assigning eip failed; restore the old cs */
2042 ops->set_segment(ctxt, old_sel, &old_desc, 0, VCPU_SREG_CS);
2043 return rc;
2044 }
2045 return rc;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002046}
2047
Takuya Yoshikawa51187682011-05-02 02:29:17 +09002048static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002049{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002050 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002051
Avi Kivity9dac77f2011-06-01 15:34:25 +03002052 switch (ctxt->modrm_reg) {
Mohammed Gamald19292e2008-09-08 21:47:19 +03002053 case 2: /* call near abs */ {
2054 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002055 old_eip = ctxt->_eip;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002056 rc = assign_eip_near(ctxt, ctxt->src.val);
2057 if (rc != X86EMUL_CONTINUE)
2058 break;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002059 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002060 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03002061 break;
2062 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002063 case 4: /* jmp abs */
Nadav Amit234f3ce2014-09-18 22:39:38 +03002064 rc = assign_eip_near(ctxt, ctxt->src.val);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002065 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002066 case 5: /* jmp far */
2067 rc = em_jmp_far(ctxt);
2068 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002069 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002070 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002071 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002072 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002073 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002074}
2075
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002076static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002077{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002078 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002079
Nadav Amitaaa05f22014-06-02 18:34:10 +03002080 if (ctxt->dst.bytes == 16)
2081 return X86EMUL_UNHANDLEABLE;
2082
Avi Kivitydd856ef2012-08-27 23:46:17 +03002083 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2084 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2085 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2086 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002087 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002088 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002089 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2090 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002091
Laurent Vivier05f086f2007-09-24 11:10:55 +02002092 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002093 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002094 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002095}
2096
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002097static int em_ret(struct x86_emulate_ctxt *ctxt)
2098{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002099 int rc;
2100 unsigned long eip;
2101
2102 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
2103 if (rc != X86EMUL_CONTINUE)
2104 return rc;
2105
2106 return assign_eip_near(ctxt, eip);
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002107}
2108
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002109static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002110{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002111 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002112 unsigned long eip, cs;
2113 u16 old_cs;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002114 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amitd1442d82014-09-18 22:39:39 +03002115 struct desc_struct old_desc, new_desc;
2116 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002117
Nadav Amitd1442d82014-09-18 22:39:39 +03002118 if (ctxt->mode == X86EMUL_MODE_PROT64)
2119 ops->get_segment(ctxt, &old_cs, &old_desc, NULL,
2120 VCPU_SREG_CS);
2121
2122 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002123 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002124 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002125 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002126 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002127 return rc;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002128 /* Outer-privilege level return is not implemented */
2129 if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2130 return X86EMUL_UNHANDLEABLE;
Nadav Amitd1442d82014-09-18 22:39:39 +03002131 rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, 0, false,
2132 &new_desc);
2133 if (rc != X86EMUL_CONTINUE)
2134 return rc;
2135 rc = assign_eip_far(ctxt, eip, new_desc.l);
2136 if (rc != X86EMUL_CONTINUE) {
Nadav Amit7e46ddd2014-10-28 00:03:43 +02002137 WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64);
Nadav Amitd1442d82014-09-18 22:39:39 +03002138 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
2139 }
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002140 return rc;
2141}
2142
Bruce Rogers32611072013-09-09 09:40:20 -06002143static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2144{
2145 int rc;
2146
2147 rc = em_ret_far(ctxt);
2148 if (rc != X86EMUL_CONTINUE)
2149 return rc;
2150 rsp_increment(ctxt, ctxt->src.val);
2151 return X86EMUL_CONTINUE;
2152}
2153
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002154static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2155{
2156 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002157 ctxt->dst.orig_val = ctxt->dst.val;
2158 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002159 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002160 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002161 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002162
2163 if (ctxt->eflags & EFLG_ZF) {
2164 /* Success: write back to memory. */
2165 ctxt->dst.val = ctxt->src.orig_val;
2166 } else {
2167 /* Failure: write the value we saw to EAX. */
2168 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002169 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Nadav Amit37c564f2014-06-02 18:34:07 +03002170 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002171 }
2172 return X86EMUL_CONTINUE;
2173}
2174
Avi Kivityd4b43252011-09-13 10:45:50 +03002175static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002176{
Avi Kivityd4b43252011-09-13 10:45:50 +03002177 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002178 unsigned short sel;
2179 int rc;
2180
Avi Kivity9dac77f2011-06-01 15:34:25 +03002181 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002182
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002183 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002184 if (rc != X86EMUL_CONTINUE)
2185 return rc;
2186
Avi Kivity9dac77f2011-06-01 15:34:25 +03002187 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002188 return rc;
2189}
2190
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002191static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002192setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002193 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002194{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002195 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002196 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002197 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002198 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002199 cs->type = 0x0b; /* Read, Execute, Accessed */
2200 cs->s = 1;
2201 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002202 cs->p = 1;
2203 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002204 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002205
Gleb Natapov79168fd2010-04-28 19:15:30 +03002206 set_desc_base(ss, 0); /* flat segment */
2207 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002208 ss->g = 1; /* 4kb granularity */
2209 ss->s = 1;
2210 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002211 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002212 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002213 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002214 ss->l = 0;
2215 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002216}
2217
Avi Kivity1a18a692012-02-01 12:23:21 +02002218static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2219{
2220 u32 eax, ebx, ecx, edx;
2221
2222 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002223 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2224 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002225 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2226 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2227}
2228
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002229static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2230{
Mathias Krause0225fb52012-08-30 01:30:16 +02002231 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002232 u32 eax, ebx, ecx, edx;
2233
2234 /*
2235 * syscall should always be enabled in longmode - so only become
2236 * vendor specific (cpuid) if other modes are active...
2237 */
2238 if (ctxt->mode == X86EMUL_MODE_PROT64)
2239 return true;
2240
2241 eax = 0x00000000;
2242 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002243 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2244 /*
2245 * Intel ("GenuineIntel")
2246 * remark: Intel CPUs only support "syscall" in 64bit
2247 * longmode. Also an 64bit guest with a
2248 * 32bit compat-app running will #UD !! While this
2249 * behaviour can be fixed (by emulating) into AMD
2250 * response - CPUs of AMD can't behave like Intel.
2251 */
2252 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2253 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2254 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2255 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002256
Avi Kivity0017f932012-06-07 14:10:16 +03002257 /* AMD ("AuthenticAMD") */
2258 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2259 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2260 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2261 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002262
Avi Kivity0017f932012-06-07 14:10:16 +03002263 /* AMD ("AMDisbetter!") */
2264 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2265 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2266 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2267 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002268
2269 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2270 return false;
2271}
2272
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002273static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002274{
Mathias Krause0225fb52012-08-30 01:30:16 +02002275 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002276 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002277 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002278 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002279 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002280
2281 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002282 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002283 ctxt->mode == X86EMUL_MODE_VM86)
2284 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002285
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002286 if (!(em_syscall_is_enabled(ctxt)))
2287 return emulate_ud(ctxt);
2288
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002289 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002290 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002291
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002292 if (!(efer & EFER_SCE))
2293 return emulate_ud(ctxt);
2294
Avi Kivity717746e2011-04-20 13:37:53 +03002295 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002296 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002297 cs_sel = (u16)(msr_data & 0xfffc);
2298 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002299
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002300 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002301 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002302 cs.l = 1;
2303 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002304 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2305 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002306
Avi Kivitydd856ef2012-08-27 23:46:17 +03002307 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002308 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002309#ifdef CONFIG_X86_64
Nadav Amit6c6cb692014-07-21 14:37:30 +03002310 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002311
Avi Kivity717746e2011-04-20 13:37:53 +03002312 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002313 ctxt->mode == X86EMUL_MODE_PROT64 ?
2314 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002315 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002316
Avi Kivity717746e2011-04-20 13:37:53 +03002317 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Nadav Amit6c6cb692014-07-21 14:37:30 +03002318 ctxt->eflags &= ~msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002319#endif
2320 } else {
2321 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002322 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002323 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002324
Nadav Amit6c6cb692014-07-21 14:37:30 +03002325 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002326 }
2327
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002328 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002329}
2330
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002331static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002332{
Mathias Krause0225fb52012-08-30 01:30:16 +02002333 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002334 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002335 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002336 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002337 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002338
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002339 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002340 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002341 if (ctxt->mode == X86EMUL_MODE_REAL)
2342 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002343
Avi Kivity1a18a692012-02-01 12:23:21 +02002344 /*
2345 * Not recognized on AMD in compat mode (but is recognized in legacy
2346 * mode).
2347 */
2348 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2349 && !vendor_intel(ctxt))
2350 return emulate_ud(ctxt);
2351
Andre Przywara8c604352009-06-18 12:56:01 +02002352 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2353 * Therefore, we inject an #UD.
2354 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002355 if (ctxt->mode == X86EMUL_MODE_PROT64)
2356 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002357
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002358 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002359
Avi Kivity717746e2011-04-20 13:37:53 +03002360 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002361 switch (ctxt->mode) {
2362 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002363 if ((msr_data & 0xfffc) == 0x0)
2364 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002365 break;
2366 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002367 if (msr_data == 0x0)
2368 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002369 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002370 default:
2371 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002372 }
2373
Nadav Amit6c6cb692014-07-21 14:37:30 +03002374 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002375 cs_sel = (u16)msr_data;
2376 cs_sel &= ~SELECTOR_RPL_MASK;
2377 ss_sel = cs_sel + 8;
2378 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002379 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002380 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002381 cs.l = 1;
2382 }
2383
Avi Kivity1aa36612011-04-27 13:20:30 +03002384 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2385 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002386
Avi Kivity717746e2011-04-20 13:37:53 +03002387 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002388 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002389
Avi Kivity717746e2011-04-20 13:37:53 +03002390 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002391 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002392
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002393 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002394}
2395
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002396static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002397{
Mathias Krause0225fb52012-08-30 01:30:16 +02002398 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002399 struct desc_struct cs, ss;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002400 u64 msr_data, rcx, rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002401 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002402 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002403
Gleb Natapova0044752010-02-10 14:21:31 +02002404 /* inject #GP if in real mode or Virtual 8086 mode */
2405 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002406 ctxt->mode == X86EMUL_MODE_VM86)
2407 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002408
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002409 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002410
Avi Kivity9dac77f2011-06-01 15:34:25 +03002411 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002412 usermode = X86EMUL_MODE_PROT64;
2413 else
2414 usermode = X86EMUL_MODE_PROT32;
2415
Nadav Amit234f3ce2014-09-18 22:39:38 +03002416 rcx = reg_read(ctxt, VCPU_REGS_RCX);
2417 rdx = reg_read(ctxt, VCPU_REGS_RDX);
2418
Andre Przywara4668f052009-06-18 12:56:02 +02002419 cs.dpl = 3;
2420 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002421 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002422 switch (usermode) {
2423 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002424 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002425 if ((msr_data & 0xfffc) == 0x0)
2426 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002427 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002428 break;
2429 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002430 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002431 if (msr_data == 0x0)
2432 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002433 ss_sel = cs_sel + 8;
2434 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002435 cs.l = 1;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002436 if (is_noncanonical_address(rcx) ||
2437 is_noncanonical_address(rdx))
2438 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002439 break;
2440 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002441 cs_sel |= SELECTOR_RPL_MASK;
2442 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002443
Avi Kivity1aa36612011-04-27 13:20:30 +03002444 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2445 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002446
Nadav Amit234f3ce2014-09-18 22:39:38 +03002447 ctxt->_eip = rdx;
2448 *reg_write(ctxt, VCPU_REGS_RSP) = rcx;
Andre Przywara4668f052009-06-18 12:56:02 +02002449
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002450 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002451}
2452
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002453static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002454{
2455 int iopl;
2456 if (ctxt->mode == X86EMUL_MODE_REAL)
2457 return false;
2458 if (ctxt->mode == X86EMUL_MODE_VM86)
2459 return true;
2460 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002461 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002462}
2463
2464static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002465 u16 port, u16 len)
2466{
Mathias Krause0225fb52012-08-30 01:30:16 +02002467 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002468 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002469 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002470 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002471 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002472 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002473 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002474
Avi Kivity1aa36612011-04-27 13:20:30 +03002475 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002476 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002477 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002478 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002479 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002480 base = get_desc_base(&tr_seg);
2481#ifdef CONFIG_X86_64
2482 base |= ((u64)base3) << 32;
2483#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002484 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002485 if (r != X86EMUL_CONTINUE)
2486 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002487 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002488 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002489 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002490 if (r != X86EMUL_CONTINUE)
2491 return false;
2492 if ((perm >> bit_idx) & mask)
2493 return false;
2494 return true;
2495}
2496
2497static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002498 u16 port, u16 len)
2499{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002500 if (ctxt->perm_ok)
2501 return true;
2502
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002503 if (emulator_bad_iopl(ctxt))
2504 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002505 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002506
2507 ctxt->perm_ok = true;
2508
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002509 return true;
2510}
2511
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002512static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002513 struct tss_segment_16 *tss)
2514{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002515 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002516 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002517 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2518 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2519 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2520 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2521 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2522 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2523 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2524 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002525
Avi Kivity1aa36612011-04-27 13:20:30 +03002526 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2527 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2528 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2529 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2530 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002531}
2532
2533static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002534 struct tss_segment_16 *tss)
2535{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002536 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002537 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002538
Avi Kivity9dac77f2011-06-01 15:34:25 +03002539 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002540 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002541 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2542 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2543 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2544 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2545 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2546 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2547 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2548 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002549
2550 /*
2551 * SDM says that segment selectors are loaded before segment
2552 * descriptors
2553 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002554 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2555 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2556 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2557 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2558 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002559
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002560 cpl = tss->cs & 3;
2561
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002562 /*
Guo Chaofc058682012-06-28 15:19:51 +08002563 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002564 * it is handled in a context of new task
2565 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002566 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl,
2567 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002568 if (ret != X86EMUL_CONTINUE)
2569 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002570 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
2571 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002572 if (ret != X86EMUL_CONTINUE)
2573 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002574 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
2575 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002576 if (ret != X86EMUL_CONTINUE)
2577 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002578 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
2579 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002580 if (ret != X86EMUL_CONTINUE)
2581 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002582 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
2583 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002584 if (ret != X86EMUL_CONTINUE)
2585 return ret;
2586
2587 return X86EMUL_CONTINUE;
2588}
2589
2590static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002591 u16 tss_selector, u16 old_tss_sel,
2592 ulong old_tss_base, struct desc_struct *new_desc)
2593{
Mathias Krause0225fb52012-08-30 01:30:16 +02002594 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002595 struct tss_segment_16 tss_seg;
2596 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002597 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002598
Avi Kivity0f65dd72011-04-20 13:37:53 +03002599 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002600 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002601 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002602 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002603 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002604
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002605 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002606
Avi Kivity0f65dd72011-04-20 13:37:53 +03002607 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002608 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002609 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002610 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002611 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002612
Avi Kivity0f65dd72011-04-20 13:37:53 +03002613 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002614 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002615 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002616 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002617 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002618
2619 if (old_tss_sel != 0xffff) {
2620 tss_seg.prev_task_link = old_tss_sel;
2621
Avi Kivity0f65dd72011-04-20 13:37:53 +03002622 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002623 &tss_seg.prev_task_link,
2624 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002625 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002626 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002627 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002628 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002629 }
2630
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002631 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002632}
2633
2634static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002635 struct tss_segment_32 *tss)
2636{
Nadav Amit5c7411e2014-04-07 18:37:47 +03002637 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002638 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002639 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002640 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2641 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2642 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2643 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2644 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2645 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2646 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2647 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002648
Avi Kivity1aa36612011-04-27 13:20:30 +03002649 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2650 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2651 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2652 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2653 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2654 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002655}
2656
2657static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002658 struct tss_segment_32 *tss)
2659{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002660 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002661 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002662
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002663 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002664 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002665 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002666 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002667
2668 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002669 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2670 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2671 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2672 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2673 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2674 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2675 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2676 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002677
2678 /*
2679 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002680 * descriptors. This is important because CPL checks will
2681 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002682 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002683 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2684 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2685 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2686 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2687 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2688 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2689 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002690
2691 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002692 * If we're switching between Protected Mode and VM86, we need to make
2693 * sure to update the mode before loading the segment descriptors so
2694 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01002695 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002696 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002697 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002698 cpl = 3;
2699 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002700 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002701 cpl = tss->cs & 3;
2702 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01002703
2704 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002705 * Now load segment descriptors. If fault happenes at this stage
2706 * it is handled in a context of new task
2707 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002708 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR,
2709 cpl, true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002710 if (ret != X86EMUL_CONTINUE)
2711 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002712 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
2713 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002714 if (ret != X86EMUL_CONTINUE)
2715 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002716 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
2717 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002718 if (ret != X86EMUL_CONTINUE)
2719 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002720 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
2721 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002722 if (ret != X86EMUL_CONTINUE)
2723 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002724 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
2725 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002726 if (ret != X86EMUL_CONTINUE)
2727 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002728 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl,
2729 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002730 if (ret != X86EMUL_CONTINUE)
2731 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002732 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl,
2733 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002734 if (ret != X86EMUL_CONTINUE)
2735 return ret;
2736
2737 return X86EMUL_CONTINUE;
2738}
2739
2740static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002741 u16 tss_selector, u16 old_tss_sel,
2742 ulong old_tss_base, struct desc_struct *new_desc)
2743{
Mathias Krause0225fb52012-08-30 01:30:16 +02002744 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002745 struct tss_segment_32 tss_seg;
2746 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002747 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03002748 u32 eip_offset = offsetof(struct tss_segment_32, eip);
2749 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002750
Avi Kivity0f65dd72011-04-20 13:37:53 +03002751 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002752 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002753 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002754 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002755 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002756
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002757 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002758
Nadav Amit5c7411e2014-04-07 18:37:47 +03002759 /* Only GP registers and segment selectors are saved */
2760 ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
2761 ldt_sel_offset - eip_offset, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002762 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002763 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002764 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002765
Avi Kivity0f65dd72011-04-20 13:37:53 +03002766 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002767 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002768 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002769 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002770 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002771
2772 if (old_tss_sel != 0xffff) {
2773 tss_seg.prev_task_link = old_tss_sel;
2774
Avi Kivity0f65dd72011-04-20 13:37:53 +03002775 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002776 &tss_seg.prev_task_link,
2777 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002778 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002779 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002780 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002781 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002782 }
2783
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002784 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002785}
2786
2787static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002788 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002789 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002790{
Mathias Krause0225fb52012-08-30 01:30:16 +02002791 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002792 struct desc_struct curr_tss_desc, next_tss_desc;
2793 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002794 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002795 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002796 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002797 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002798 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002799
2800 /* FIXME: old_tss_base == ~0 ? */
2801
Avi Kivitye9194642012-06-13 16:29:39 +03002802 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002803 if (ret != X86EMUL_CONTINUE)
2804 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002805 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002806 if (ret != X86EMUL_CONTINUE)
2807 return ret;
2808
2809 /* FIXME: check that next_tss_desc is tss */
2810
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002811 /*
2812 * Check privileges. The three cases are task switch caused by...
2813 *
2814 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2815 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002816 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002817 */
2818 if (reason == TASK_SWITCH_GATE) {
2819 if (idt_index != -1) {
2820 /* Software interrupts */
2821 struct desc_struct task_gate_desc;
2822 int dpl;
2823
2824 ret = read_interrupt_descriptor(ctxt, idt_index,
2825 &task_gate_desc);
2826 if (ret != X86EMUL_CONTINUE)
2827 return ret;
2828
2829 dpl = task_gate_desc.dpl;
2830 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2831 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2832 }
2833 } else if (reason != TASK_SWITCH_IRET) {
2834 int dpl = next_tss_desc.dpl;
2835 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2836 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002837 }
2838
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002839
Gleb Natapovceffb452010-03-18 15:20:19 +02002840 desc_limit = desc_limit_scaled(&next_tss_desc);
2841 if (!next_tss_desc.p ||
2842 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2843 desc_limit < 0x2b)) {
Paolo Bonzini592f0852014-08-20 10:05:08 +02002844 return emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002845 }
2846
2847 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2848 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002849 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002850 }
2851
2852 if (reason == TASK_SWITCH_IRET)
2853 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2854
2855 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002856 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002857 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2858 old_tss_sel = 0xffff;
2859
2860 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002861 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002862 old_tss_base, &next_tss_desc);
2863 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002864 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002865 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002866 if (ret != X86EMUL_CONTINUE)
2867 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002868
2869 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2870 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2871
2872 if (reason != TASK_SWITCH_IRET) {
2873 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002874 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002875 }
2876
Avi Kivity717746e2011-04-20 13:37:53 +03002877 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002878 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002879
Jan Kiszkae269fb22010-04-14 15:51:09 +02002880 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002881 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2882 ctxt->lock_prefix = 0;
2883 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002884 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002885 }
2886
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002887 return ret;
2888}
2889
2890int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002891 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002892 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002893{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002894 int rc;
2895
Avi Kivitydd856ef2012-08-27 23:46:17 +03002896 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002897 ctxt->_eip = ctxt->eip;
2898 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002899
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002900 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002901 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002902
Avi Kivitydd856ef2012-08-27 23:46:17 +03002903 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002904 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002905 writeback_registers(ctxt);
2906 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002907
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002908 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002909}
2910
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002911static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2912 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002913{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002914 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002915
Avi Kivitydd856ef2012-08-27 23:46:17 +03002916 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2917 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002918}
2919
Avi Kivity7af04fc2010-08-18 14:16:35 +03002920static int em_das(struct x86_emulate_ctxt *ctxt)
2921{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002922 u8 al, old_al;
2923 bool af, cf, old_cf;
2924
2925 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002926 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002927
2928 old_al = al;
2929 old_cf = cf;
2930 cf = false;
2931 af = ctxt->eflags & X86_EFLAGS_AF;
2932 if ((al & 0x0f) > 9 || af) {
2933 al -= 6;
2934 cf = old_cf | (al >= 250);
2935 af = true;
2936 } else {
2937 af = false;
2938 }
2939 if (old_al > 0x99 || old_cf) {
2940 al -= 0x60;
2941 cf = true;
2942 }
2943
Avi Kivity9dac77f2011-06-01 15:34:25 +03002944 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002945 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002946 ctxt->src.type = OP_IMM;
2947 ctxt->src.val = 0;
2948 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02002949 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03002950 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2951 if (cf)
2952 ctxt->eflags |= X86_EFLAGS_CF;
2953 if (af)
2954 ctxt->eflags |= X86_EFLAGS_AF;
2955 return X86EMUL_CONTINUE;
2956}
2957
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02002958static int em_aam(struct x86_emulate_ctxt *ctxt)
2959{
2960 u8 al, ah;
2961
2962 if (ctxt->src.val == 0)
2963 return emulate_de(ctxt);
2964
2965 al = ctxt->dst.val & 0xff;
2966 ah = al / ctxt->src.val;
2967 al %= ctxt->src.val;
2968
2969 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
2970
2971 /* Set PF, ZF, SF */
2972 ctxt->src.type = OP_IMM;
2973 ctxt->src.val = 0;
2974 ctxt->src.bytes = 1;
2975 fastop(ctxt, em_or);
2976
2977 return X86EMUL_CONTINUE;
2978}
2979
Gleb Natapov7f662272012-12-10 11:42:30 +02002980static int em_aad(struct x86_emulate_ctxt *ctxt)
2981{
2982 u8 al = ctxt->dst.val & 0xff;
2983 u8 ah = (ctxt->dst.val >> 8) & 0xff;
2984
2985 al = (al + (ah * ctxt->src.val)) & 0xff;
2986
2987 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
2988
Gleb Natapovf583c292013-02-13 17:50:39 +02002989 /* Set PF, ZF, SF */
2990 ctxt->src.type = OP_IMM;
2991 ctxt->src.val = 0;
2992 ctxt->src.bytes = 1;
2993 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02002994
2995 return X86EMUL_CONTINUE;
2996}
2997
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002998static int em_call(struct x86_emulate_ctxt *ctxt)
2999{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003000 int rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003001 long rel = ctxt->src.val;
3002
3003 ctxt->src.val = (unsigned long)ctxt->_eip;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003004 rc = jmp_rel(ctxt, rel);
3005 if (rc != X86EMUL_CONTINUE)
3006 return rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003007 return em_push(ctxt);
3008}
3009
Avi Kivity0ef753b2010-08-18 14:51:45 +03003010static int em_call_far(struct x86_emulate_ctxt *ctxt)
3011{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003012 u16 sel, old_cs;
3013 ulong old_eip;
3014 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03003015 struct desc_struct old_desc, new_desc;
3016 const struct x86_emulate_ops *ops = ctxt->ops;
3017 int cpl = ctxt->ops->cpl(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003018
Avi Kivity9dac77f2011-06-01 15:34:25 +03003019 old_eip = ctxt->_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003020 ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003021
Avi Kivity9dac77f2011-06-01 15:34:25 +03003022 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Nadav Amitd1442d82014-09-18 22:39:39 +03003023 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl, false,
3024 &new_desc);
3025 if (rc != X86EMUL_CONTINUE)
Avi Kivity0ef753b2010-08-18 14:51:45 +03003026 return X86EMUL_CONTINUE;
3027
Nadav Amitd1442d82014-09-18 22:39:39 +03003028 rc = assign_eip_far(ctxt, ctxt->src.val, new_desc.l);
3029 if (rc != X86EMUL_CONTINUE)
3030 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003031
Avi Kivity9dac77f2011-06-01 15:34:25 +03003032 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003033 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003034 if (rc != X86EMUL_CONTINUE)
Nadav Amitd1442d82014-09-18 22:39:39 +03003035 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003036
Avi Kivity9dac77f2011-06-01 15:34:25 +03003037 ctxt->src.val = old_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003038 rc = em_push(ctxt);
3039 /* If we failed, we tainted the memory, but the very least we should
3040 restore cs */
3041 if (rc != X86EMUL_CONTINUE)
3042 goto fail;
3043 return rc;
3044fail:
3045 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
3046 return rc;
3047
Avi Kivity0ef753b2010-08-18 14:51:45 +03003048}
3049
Avi Kivity40ece7c2010-08-18 15:12:09 +03003050static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3051{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003052 int rc;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003053 unsigned long eip;
Avi Kivity40ece7c2010-08-18 15:12:09 +03003054
Nadav Amit234f3ce2014-09-18 22:39:38 +03003055 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
3056 if (rc != X86EMUL_CONTINUE)
3057 return rc;
3058 rc = assign_eip_near(ctxt, eip);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003059 if (rc != X86EMUL_CONTINUE)
3060 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003061 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003062 return X86EMUL_CONTINUE;
3063}
3064
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003065static int em_xchg(struct x86_emulate_ctxt *ctxt)
3066{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003067 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003068 ctxt->src.val = ctxt->dst.val;
3069 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003070
3071 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003072 ctxt->dst.val = ctxt->src.orig_val;
3073 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003074 return X86EMUL_CONTINUE;
3075}
3076
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003077static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3078{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003079 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003080 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003081}
3082
Avi Kivity61429142010-08-19 15:13:00 +03003083static int em_cwd(struct x86_emulate_ctxt *ctxt)
3084{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003085 ctxt->dst.type = OP_REG;
3086 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003087 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003088 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003089
3090 return X86EMUL_CONTINUE;
3091}
3092
Avi Kivity48bb5d32010-08-18 18:54:34 +03003093static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3094{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003095 u64 tsc = 0;
3096
Avi Kivity717746e2011-04-20 13:37:53 +03003097 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003098 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3099 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003100 return X86EMUL_CONTINUE;
3101}
3102
Avi Kivity222d21a2011-11-10 14:57:30 +02003103static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3104{
3105 u64 pmc;
3106
Avi Kivitydd856ef2012-08-27 23:46:17 +03003107 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003108 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003109 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3110 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003111 return X86EMUL_CONTINUE;
3112}
3113
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003114static int em_mov(struct x86_emulate_ctxt *ctxt)
3115{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01003116 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003117 return X86EMUL_CONTINUE;
3118}
3119
Borislav Petkov84cffe42013-10-29 12:54:56 +01003120#define FFL(x) bit(X86_FEATURE_##x)
3121
3122static int em_movbe(struct x86_emulate_ctxt *ctxt)
3123{
3124 u32 ebx, ecx, edx, eax = 1;
3125 u16 tmp;
3126
3127 /*
3128 * Check MOVBE is set in the guest-visible CPUID leaf.
3129 */
3130 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
3131 if (!(ecx & FFL(MOVBE)))
3132 return emulate_ud(ctxt);
3133
3134 switch (ctxt->op_bytes) {
3135 case 2:
3136 /*
3137 * From MOVBE definition: "...When the operand size is 16 bits,
3138 * the upper word of the destination register remains unchanged
3139 * ..."
3140 *
3141 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3142 * rules so we have to do the operation almost per hand.
3143 */
3144 tmp = (u16)ctxt->src.val;
3145 ctxt->dst.val &= ~0xffffUL;
3146 ctxt->dst.val |= (unsigned long)swab16(tmp);
3147 break;
3148 case 4:
3149 ctxt->dst.val = swab32((u32)ctxt->src.val);
3150 break;
3151 case 8:
3152 ctxt->dst.val = swab64(ctxt->src.val);
3153 break;
3154 default:
Paolo Bonzini592f0852014-08-20 10:05:08 +02003155 BUG();
Borislav Petkov84cffe42013-10-29 12:54:56 +01003156 }
3157 return X86EMUL_CONTINUE;
3158}
3159
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003160static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3161{
3162 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3163 return emulate_gp(ctxt, 0);
3164
3165 /* Disable writeback. */
3166 ctxt->dst.type = OP_NONE;
3167 return X86EMUL_CONTINUE;
3168}
3169
3170static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3171{
3172 unsigned long val;
3173
3174 if (ctxt->mode == X86EMUL_MODE_PROT64)
3175 val = ctxt->src.val & ~0ULL;
3176 else
3177 val = ctxt->src.val & ~0U;
3178
3179 /* #UD condition is already handled. */
3180 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3181 return emulate_gp(ctxt, 0);
3182
3183 /* Disable writeback. */
3184 ctxt->dst.type = OP_NONE;
3185 return X86EMUL_CONTINUE;
3186}
3187
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003188static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3189{
3190 u64 msr_data;
3191
Avi Kivitydd856ef2012-08-27 23:46:17 +03003192 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3193 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3194 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003195 return emulate_gp(ctxt, 0);
3196
3197 return X86EMUL_CONTINUE;
3198}
3199
3200static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3201{
3202 u64 msr_data;
3203
Avi Kivitydd856ef2012-08-27 23:46:17 +03003204 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003205 return emulate_gp(ctxt, 0);
3206
Avi Kivitydd856ef2012-08-27 23:46:17 +03003207 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3208 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003209 return X86EMUL_CONTINUE;
3210}
3211
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003212static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3213{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003214 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003215 return emulate_ud(ctxt);
3216
Avi Kivity9dac77f2011-06-01 15:34:25 +03003217 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003218 return X86EMUL_CONTINUE;
3219}
3220
3221static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3222{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003223 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003224
Avi Kivity9dac77f2011-06-01 15:34:25 +03003225 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003226 return emulate_ud(ctxt);
3227
Avi Kivity9dac77f2011-06-01 15:34:25 +03003228 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003229 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3230
3231 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003232 ctxt->dst.type = OP_NONE;
3233 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003234}
3235
Avi Kivitya14e5792012-06-13 12:28:33 +03003236static int em_lldt(struct x86_emulate_ctxt *ctxt)
3237{
3238 u16 sel = ctxt->src.val;
3239
3240 /* Disable writeback. */
3241 ctxt->dst.type = OP_NONE;
3242 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3243}
3244
Avi Kivity80890002012-06-13 16:33:29 +03003245static int em_ltr(struct x86_emulate_ctxt *ctxt)
3246{
3247 u16 sel = ctxt->src.val;
3248
3249 /* Disable writeback. */
3250 ctxt->dst.type = OP_NONE;
3251 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3252}
3253
Avi Kivity38503912011-03-31 18:48:09 +02003254static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3255{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003256 int rc;
3257 ulong linear;
3258
Avi Kivity9dac77f2011-06-01 15:34:25 +03003259 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003260 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003261 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003262 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003263 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003264 return X86EMUL_CONTINUE;
3265}
3266
Avi Kivity2d04a052011-04-20 15:32:49 +03003267static int em_clts(struct x86_emulate_ctxt *ctxt)
3268{
3269 ulong cr0;
3270
3271 cr0 = ctxt->ops->get_cr(ctxt, 0);
3272 cr0 &= ~X86_CR0_TS;
3273 ctxt->ops->set_cr(ctxt, 0, cr0);
3274 return X86EMUL_CONTINUE;
3275}
3276
Avi Kivity26d05cc2011-04-21 12:07:59 +03003277static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3278{
Nadav Amit0f54a322014-08-29 11:26:55 +03003279 int rc = ctxt->ops->fix_hypercall(ctxt);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003280
Avi Kivity26d05cc2011-04-21 12:07:59 +03003281 if (rc != X86EMUL_CONTINUE)
3282 return rc;
3283
3284 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003285 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003286 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003287 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003288 return X86EMUL_CONTINUE;
3289}
3290
Avi Kivity96051572012-06-10 17:21:18 +03003291static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3292 void (*get)(struct x86_emulate_ctxt *ctxt,
3293 struct desc_ptr *ptr))
3294{
3295 struct desc_ptr desc_ptr;
3296
3297 if (ctxt->mode == X86EMUL_MODE_PROT64)
3298 ctxt->op_bytes = 8;
3299 get(ctxt, &desc_ptr);
3300 if (ctxt->op_bytes == 2) {
3301 ctxt->op_bytes = 4;
3302 desc_ptr.address &= 0x00ffffff;
3303 }
3304 /* Disable writeback. */
3305 ctxt->dst.type = OP_NONE;
3306 return segmented_write(ctxt, ctxt->dst.addr.mem,
3307 &desc_ptr, 2 + ctxt->op_bytes);
3308}
3309
3310static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3311{
3312 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3313}
3314
3315static int em_sidt(struct x86_emulate_ctxt *ctxt)
3316{
3317 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3318}
3319
Avi Kivity26d05cc2011-04-21 12:07:59 +03003320static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3321{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003322 struct desc_ptr desc_ptr;
3323 int rc;
3324
Avi Kivity510425f2012-06-07 17:04:36 +03003325 if (ctxt->mode == X86EMUL_MODE_PROT64)
3326 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003327 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003328 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003329 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003330 if (rc != X86EMUL_CONTINUE)
3331 return rc;
3332 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3333 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003334 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003335 return X86EMUL_CONTINUE;
3336}
3337
Avi Kivity5ef39c72011-04-21 12:21:50 +03003338static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003339{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003340 int rc;
3341
Avi Kivity5ef39c72011-04-21 12:21:50 +03003342 rc = ctxt->ops->fix_hypercall(ctxt);
3343
Avi Kivity26d05cc2011-04-21 12:07:59 +03003344 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003345 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003346 return rc;
3347}
3348
3349static int em_lidt(struct x86_emulate_ctxt *ctxt)
3350{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003351 struct desc_ptr desc_ptr;
3352 int rc;
3353
Avi Kivity510425f2012-06-07 17:04:36 +03003354 if (ctxt->mode == X86EMUL_MODE_PROT64)
3355 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003356 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003357 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003358 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003359 if (rc != X86EMUL_CONTINUE)
3360 return rc;
3361 ctxt->ops->set_idt(ctxt, &desc_ptr);
3362 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003363 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003364 return X86EMUL_CONTINUE;
3365}
3366
3367static int em_smsw(struct x86_emulate_ctxt *ctxt)
3368{
Nadav Amit32e94d02014-06-02 18:34:11 +03003369 if (ctxt->dst.type == OP_MEM)
3370 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003371 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003372 return X86EMUL_CONTINUE;
3373}
3374
3375static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3376{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003377 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003378 | (ctxt->src.val & 0x0f));
3379 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003380 return X86EMUL_CONTINUE;
3381}
3382
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003383static int em_loop(struct x86_emulate_ctxt *ctxt)
3384{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003385 int rc = X86EMUL_CONTINUE;
3386
Avi Kivitydd856ef2012-08-27 23:46:17 +03003387 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3388 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003389 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
Nadav Amit234f3ce2014-09-18 22:39:38 +03003390 rc = jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003391
Nadav Amit234f3ce2014-09-18 22:39:38 +03003392 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003393}
3394
3395static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3396{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003397 int rc = X86EMUL_CONTINUE;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003398
Nadav Amit234f3ce2014-09-18 22:39:38 +03003399 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
3400 rc = jmp_rel(ctxt, ctxt->src.val);
3401
3402 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003403}
3404
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003405static int em_in(struct x86_emulate_ctxt *ctxt)
3406{
3407 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3408 &ctxt->dst.val))
3409 return X86EMUL_IO_NEEDED;
3410
3411 return X86EMUL_CONTINUE;
3412}
3413
3414static int em_out(struct x86_emulate_ctxt *ctxt)
3415{
3416 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3417 &ctxt->src.val, 1);
3418 /* Disable writeback. */
3419 ctxt->dst.type = OP_NONE;
3420 return X86EMUL_CONTINUE;
3421}
3422
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003423static int em_cli(struct x86_emulate_ctxt *ctxt)
3424{
3425 if (emulator_bad_iopl(ctxt))
3426 return emulate_gp(ctxt, 0);
3427
3428 ctxt->eflags &= ~X86_EFLAGS_IF;
3429 return X86EMUL_CONTINUE;
3430}
3431
3432static int em_sti(struct x86_emulate_ctxt *ctxt)
3433{
3434 if (emulator_bad_iopl(ctxt))
3435 return emulate_gp(ctxt, 0);
3436
3437 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3438 ctxt->eflags |= X86_EFLAGS_IF;
3439 return X86EMUL_CONTINUE;
3440}
3441
Avi Kivity6d6eede2012-06-07 14:11:36 +03003442static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3443{
3444 u32 eax, ebx, ecx, edx;
3445
Avi Kivitydd856ef2012-08-27 23:46:17 +03003446 eax = reg_read(ctxt, VCPU_REGS_RAX);
3447 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003448 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003449 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3450 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3451 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3452 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003453 return X86EMUL_CONTINUE;
3454}
3455
Paolo Bonzini98f73632013-10-31 11:19:42 +01003456static int em_sahf(struct x86_emulate_ctxt *ctxt)
3457{
3458 u32 flags;
3459
3460 flags = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF;
3461 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3462
3463 ctxt->eflags &= ~0xffUL;
3464 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3465 return X86EMUL_CONTINUE;
3466}
3467
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003468static int em_lahf(struct x86_emulate_ctxt *ctxt)
3469{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003470 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3471 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003472 return X86EMUL_CONTINUE;
3473}
3474
Avi Kivity92998362012-06-13 12:25:06 +03003475static int em_bswap(struct x86_emulate_ctxt *ctxt)
3476{
3477 switch (ctxt->op_bytes) {
3478#ifdef CONFIG_X86_64
3479 case 8:
3480 asm("bswap %0" : "+r"(ctxt->dst.val));
3481 break;
3482#endif
3483 default:
3484 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3485 break;
3486 }
3487 return X86EMUL_CONTINUE;
3488}
3489
Nadav Amit13e457e2014-10-13 13:04:13 +03003490static int em_clflush(struct x86_emulate_ctxt *ctxt)
3491{
3492 /* emulating clflush regardless of cpuid */
3493 return X86EMUL_CONTINUE;
3494}
3495
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003496static bool valid_cr(int nr)
3497{
3498 switch (nr) {
3499 case 0:
3500 case 2 ... 4:
3501 case 8:
3502 return true;
3503 default:
3504 return false;
3505 }
3506}
3507
3508static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3509{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003510 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003511 return emulate_ud(ctxt);
3512
3513 return X86EMUL_CONTINUE;
3514}
3515
3516static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3517{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003518 u64 new_val = ctxt->src.val64;
3519 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003520 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003521
3522 static u64 cr_reserved_bits[] = {
3523 0xffffffff00000000ULL,
3524 0, 0, 0, /* CR3 checked later */
3525 CR4_RESERVED_BITS,
3526 0, 0, 0,
3527 CR8_RESERVED_BITS,
3528 };
3529
3530 if (!valid_cr(cr))
3531 return emulate_ud(ctxt);
3532
3533 if (new_val & cr_reserved_bits[cr])
3534 return emulate_gp(ctxt, 0);
3535
3536 switch (cr) {
3537 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003538 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003539 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3540 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3541 return emulate_gp(ctxt, 0);
3542
Avi Kivity717746e2011-04-20 13:37:53 +03003543 cr4 = ctxt->ops->get_cr(ctxt, 4);
3544 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003545
3546 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3547 !(cr4 & X86_CR4_PAE))
3548 return emulate_gp(ctxt, 0);
3549
3550 break;
3551 }
3552 case 3: {
3553 u64 rsvd = 0;
3554
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003555 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3556 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003557 rsvd = CR3_L_MODE_RESERVED_BITS;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003558
3559 if (new_val & rsvd)
3560 return emulate_gp(ctxt, 0);
3561
3562 break;
3563 }
3564 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003565 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003566
3567 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3568 return emulate_gp(ctxt, 0);
3569
3570 break;
3571 }
3572 }
3573
3574 return X86EMUL_CONTINUE;
3575}
3576
Joerg Roedel3b88e412011-04-04 12:39:29 +02003577static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3578{
3579 unsigned long dr7;
3580
Avi Kivity717746e2011-04-20 13:37:53 +03003581 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003582
3583 /* Check if DR7.Global_Enable is set */
3584 return dr7 & (1 << 13);
3585}
3586
3587static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3588{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003589 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003590 u64 cr4;
3591
3592 if (dr > 7)
3593 return emulate_ud(ctxt);
3594
Avi Kivity717746e2011-04-20 13:37:53 +03003595 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003596 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3597 return emulate_ud(ctxt);
3598
3599 if (check_dr7_gd(ctxt))
3600 return emulate_db(ctxt);
3601
3602 return X86EMUL_CONTINUE;
3603}
3604
3605static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3606{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003607 u64 new_val = ctxt->src.val64;
3608 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003609
3610 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3611 return emulate_gp(ctxt, 0);
3612
3613 return check_dr_read(ctxt);
3614}
3615
Joerg Roedel01de8b02011-04-04 12:39:31 +02003616static int check_svme(struct x86_emulate_ctxt *ctxt)
3617{
3618 u64 efer;
3619
Avi Kivity717746e2011-04-20 13:37:53 +03003620 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003621
3622 if (!(efer & EFER_SVME))
3623 return emulate_ud(ctxt);
3624
3625 return X86EMUL_CONTINUE;
3626}
3627
3628static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3629{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003630 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003631
3632 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003633 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003634 return emulate_gp(ctxt, 0);
3635
3636 return check_svme(ctxt);
3637}
3638
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003639static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3640{
Avi Kivity717746e2011-04-20 13:37:53 +03003641 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003642
Avi Kivity717746e2011-04-20 13:37:53 +03003643 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003644 return emulate_ud(ctxt);
3645
3646 return X86EMUL_CONTINUE;
3647}
3648
Joerg Roedel80612522011-04-04 12:39:33 +02003649static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3650{
Avi Kivity717746e2011-04-20 13:37:53 +03003651 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003652 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003653
Avi Kivity717746e2011-04-20 13:37:53 +03003654 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03003655 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02003656 return emulate_gp(ctxt, 0);
3657
3658 return X86EMUL_CONTINUE;
3659}
3660
Joerg Roedelf6511932011-04-04 12:39:35 +02003661static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3662{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003663 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3664 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003665 return emulate_gp(ctxt, 0);
3666
3667 return X86EMUL_CONTINUE;
3668}
3669
3670static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3671{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003672 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3673 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003674 return emulate_gp(ctxt, 0);
3675
3676 return X86EMUL_CONTINUE;
3677}
3678
Avi Kivity73fba5f2010-07-29 15:11:53 +03003679#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003680#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
3681#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
3682 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003683#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003684#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003685#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3686#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003687#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003688#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003689#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003690#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003691 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003692#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003693 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
3694 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003695#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003696
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003697#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003698#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003699#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003700#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003701#define I2bvIP(_f, _e, _i, _p) \
3702 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003703
Avi Kivityfb864fb2013-01-04 16:18:54 +02003704#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3705 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3706 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003707
Nadav Amit0f54a322014-08-29 11:26:55 +03003708static const struct opcode group7_rm0[] = {
3709 N,
3710 I(SrcNone | Priv | EmulateOnUD, em_vmcall),
3711 N, N, N, N, N, N,
3712};
3713
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003714static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003715 DI(SrcNone | Priv, monitor),
3716 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003717 N, N, N, N, N, N,
3718};
3719
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003720static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003721 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003722 II(SrcNone | Prot | EmulateOnUD, em_vmmcall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003723 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3724 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3725 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3726 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3727 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3728 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003729};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003730
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003731static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003732 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003733 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003734 N, N, N, N, N, N,
3735};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003736
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003737static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003738 F(Lock, em_add),
3739 F(Lock | PageTable, em_or),
3740 F(Lock, em_adc),
3741 F(Lock, em_sbb),
3742 F(Lock | PageTable, em_and),
3743 F(Lock, em_sub),
3744 F(Lock, em_xor),
3745 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003746};
3747
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003748static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003749 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003750};
3751
Avi Kivity007a3b52013-01-19 19:51:51 +02003752static const struct opcode group2[] = {
3753 F(DstMem | ModRM, em_rol),
3754 F(DstMem | ModRM, em_ror),
3755 F(DstMem | ModRM, em_rcl),
3756 F(DstMem | ModRM, em_rcr),
3757 F(DstMem | ModRM, em_shl),
3758 F(DstMem | ModRM, em_shr),
3759 F(DstMem | ModRM, em_shl),
3760 F(DstMem | ModRM, em_sar),
3761};
3762
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003763static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003764 F(DstMem | SrcImm | NoWrite, em_test),
3765 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003766 F(DstMem | SrcNone | Lock, em_not),
3767 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02003768 F(DstXacc | Src2Mem, em_mul_ex),
3769 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02003770 F(DstXacc | Src2Mem, em_div_ex),
3771 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003772};
3773
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003774static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003775 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3776 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003777 N, N, N, N, N, N,
3778};
3779
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003780static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003781 F(DstMem | SrcNone | Lock, em_inc),
3782 F(DstMem | SrcNone | Lock, em_dec),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003783 I(SrcMem | Stack, em_grp45),
3784 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3785 I(SrcMem | Stack, em_grp45),
3786 I(SrcMemFAddr | ImplicitOps, em_grp45),
Gleb Natapov188424b2013-04-11 12:32:14 +03003787 I(SrcMem | Stack, em_grp45), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003788};
3789
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003790static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003791 DI(Prot, sldt),
3792 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003793 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003794 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003795 N, N, N, N,
3796};
3797
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003798static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03003799 II(Mov | DstMem, em_sgdt, sgdt),
3800 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003801 II(SrcMem | Priv, em_lgdt, lgdt),
3802 II(SrcMem | Priv, em_lidt, lidt),
3803 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3804 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3805 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003806}, {
Nadav Amit0f54a322014-08-29 11:26:55 +03003807 EXT(0, group7_rm0),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003808 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003809 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003810 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3811 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3812 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003813} };
3814
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003815static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003816 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003817 F(DstMem | SrcImmByte | NoWrite, em_bt),
3818 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3819 F(DstMem | SrcImmByte | Lock, em_btr),
3820 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003821};
3822
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003823static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003824 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003825}, {
3826 N, N, N, N, N, N, N, N,
3827} };
3828
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003829static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003830 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003831 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003832};
3833
Nadav Amit13e457e2014-10-13 13:04:13 +03003834static const struct gprefix pfx_0f_ae_7 = {
Nadav Amit3f6f1482014-10-13 13:04:14 +03003835 I(SrcMem | ByteOp, em_clflush), N, N, N,
Nadav Amit13e457e2014-10-13 13:04:13 +03003836};
3837
3838static const struct group_dual group15 = { {
3839 N, N, N, N, N, N, N, GP(0, &pfx_0f_ae_7),
3840}, {
3841 N, N, N, N, N, N, N, N,
3842} };
3843
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003844static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003845 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003846};
3847
Paolo Bonzinid5b77062014-07-14 12:54:48 +02003848static const struct gprefix pfx_0f_2b = {
3849 I(0, em_mov), I(0, em_mov), N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003850};
3851
Igor Mammedov27ce8252014-03-15 21:01:59 +01003852static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01003853 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003854};
3855
Alex Williamson0a370272014-07-11 11:56:31 -06003856static const struct gprefix pfx_0f_e7 = {
3857 N, I(Sse, em_mov), N, N,
3858};
3859
Gleb Natapov045a2822012-12-20 16:57:43 +02003860static const struct escape escape_d9 = { {
3861 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3862}, {
3863 /* 0xC0 - 0xC7 */
3864 N, N, N, N, N, N, N, N,
3865 /* 0xC8 - 0xCF */
3866 N, N, N, N, N, N, N, N,
3867 /* 0xD0 - 0xC7 */
3868 N, N, N, N, N, N, N, N,
3869 /* 0xD8 - 0xDF */
3870 N, N, N, N, N, N, N, N,
3871 /* 0xE0 - 0xE7 */
3872 N, N, N, N, N, N, N, N,
3873 /* 0xE8 - 0xEF */
3874 N, N, N, N, N, N, N, N,
3875 /* 0xF0 - 0xF7 */
3876 N, N, N, N, N, N, N, N,
3877 /* 0xF8 - 0xFF */
3878 N, N, N, N, N, N, N, N,
3879} };
3880
3881static const struct escape escape_db = { {
3882 N, N, N, N, N, N, N, N,
3883}, {
3884 /* 0xC0 - 0xC7 */
3885 N, N, N, N, N, N, N, N,
3886 /* 0xC8 - 0xCF */
3887 N, N, N, N, N, N, N, N,
3888 /* 0xD0 - 0xC7 */
3889 N, N, N, N, N, N, N, N,
3890 /* 0xD8 - 0xDF */
3891 N, N, N, N, N, N, N, N,
3892 /* 0xE0 - 0xE7 */
3893 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3894 /* 0xE8 - 0xEF */
3895 N, N, N, N, N, N, N, N,
3896 /* 0xF0 - 0xF7 */
3897 N, N, N, N, N, N, N, N,
3898 /* 0xF8 - 0xFF */
3899 N, N, N, N, N, N, N, N,
3900} };
3901
3902static const struct escape escape_dd = { {
3903 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3904}, {
3905 /* 0xC0 - 0xC7 */
3906 N, N, N, N, N, N, N, N,
3907 /* 0xC8 - 0xCF */
3908 N, N, N, N, N, N, N, N,
3909 /* 0xD0 - 0xC7 */
3910 N, N, N, N, N, N, N, N,
3911 /* 0xD8 - 0xDF */
3912 N, N, N, N, N, N, N, N,
3913 /* 0xE0 - 0xE7 */
3914 N, N, N, N, N, N, N, N,
3915 /* 0xE8 - 0xEF */
3916 N, N, N, N, N, N, N, N,
3917 /* 0xF0 - 0xF7 */
3918 N, N, N, N, N, N, N, N,
3919 /* 0xF8 - 0xFF */
3920 N, N, N, N, N, N, N, N,
3921} };
3922
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003923static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003924 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003925 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003926 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3927 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003928 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003929 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003930 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3931 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003932 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003933 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003934 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3935 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003936 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003937 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003938 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3939 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003940 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003941 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003942 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003943 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003944 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003945 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003946 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003947 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003948 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02003949 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003950 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003951 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003952 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003953 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003954 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003955 I(ImplicitOps | Stack | No64, em_pusha),
3956 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003957 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3958 N, N, N, N,
3959 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003960 I(SrcImm | Mov | Stack, em_push),
3961 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003962 I(SrcImmByte | Mov | Stack, em_push),
3963 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003964 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003965 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003966 /* 0x70 - 0x7F */
3967 X16(D(SrcImmByte)),
3968 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003969 G(ByteOp | DstMem | SrcImm, group1),
3970 G(DstMem | SrcImm, group1),
3971 G(ByteOp | DstMem | SrcImm | No64, group1),
3972 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003973 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003974 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003975 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003976 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003977 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003978 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003979 D(ModRM | SrcMem | NoAccess | DstReg),
3980 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3981 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003982 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003983 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003984 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003985 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003986 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003987 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01003988 II(ImplicitOps | Stack, em_popf, popf),
3989 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003990 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003991 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003992 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003993 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003994 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003995 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003996 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003997 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3998 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003999 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004000 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004001 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004002 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02004003 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004004 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004005 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Avi Kivity40ece7c2010-08-18 15:12:09 +03004006 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09004007 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03004008 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
4009 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004010 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004011 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03004012 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Bruce Rogers32611072013-09-09 09:40:20 -06004013 I(ImplicitOps | Stack | SrcImmU16, em_ret_far_imm),
4014 I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02004015 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004016 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004017 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004018 G(Src2One | ByteOp, group2), G(Src2One, group2),
4019 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02004020 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02004021 I(DstAcc | SrcImmUByte | No64, em_aad),
4022 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004023 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004024 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02004025 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004026 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09004027 X3(I(SrcImmByte, em_loop)),
4028 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004029 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
4030 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004031 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09004032 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004033 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004034 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
4035 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004036 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004037 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004038 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4039 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004040 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09004041 D(ImplicitOps), D(ImplicitOps),
4042 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004043 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4044};
4045
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004046static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004047 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004048 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02004049 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004050 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004051 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004052 N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004053 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004054 N, N, N, N, N, N, N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004055 D(ImplicitOps | ModRM | SrcMem | NoAccess),
4056 N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004057 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03004058 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
4059 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
4060 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
4061 check_cr_write),
4062 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
4063 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004064 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004065 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
4066 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004067 N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b),
Avi Kivity3e114eb2012-04-09 18:40:01 +03004068 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004069 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004070 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004071 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004072 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004073 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02004074 I(ImplicitOps | EmulateOnUD, em_sysenter),
4075 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004076 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004077 N, N, N, N, N, N, N, N,
4078 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03004079 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004080 /* 0x50 - 0x5F */
4081 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4082 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004083 N, N, N, N,
4084 N, N, N, N,
4085 N, N, N, N,
4086 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004087 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004088 N, N, N, N,
4089 N, N, N, N,
4090 N, N, N, N,
4091 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004092 /* 0x80 - 0x8F */
4093 X16(D(SrcImm)),
4094 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004095 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004096 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004097 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004098 II(ImplicitOps, em_cpuid, cpuid),
4099 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004100 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4101 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004102 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004103 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004104 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004105 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004106 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4107 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Nadav Amit13e457e2014-10-13 13:04:13 +03004108 GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004109 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09004110 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004111 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004112 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004113 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4114 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004115 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004116 /* 0xB8 - 0xBF */
4117 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004118 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004119 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
4120 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004121 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004122 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02004123 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Wei Yongjun92f738a2010-08-17 09:19:34 +08004124 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004125 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004126 /* 0xC8 - 0xCF */
4127 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004128 /* 0xD0 - 0xDF */
4129 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4130 /* 0xE0 - 0xEF */
Alex Williamson0a370272014-07-11 11:56:31 -06004131 N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
4132 N, N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004133 /* 0xF0 - 0xFF */
4134 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4135};
4136
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004137static const struct gprefix three_byte_0f_38_f0 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01004138 I(DstReg | SrcMem | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004139};
4140
4141static const struct gprefix three_byte_0f_38_f1 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01004142 I(DstMem | SrcReg | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004143};
4144
4145/*
4146 * Insns below are selected by the prefix which indexed by the third opcode
4147 * byte.
4148 */
4149static const struct opcode opcode_map_0f_38[256] = {
4150 /* 0x00 - 0x7f */
4151 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004152 /* 0x80 - 0xef */
4153 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4154 /* 0xf0 - 0xf1 */
4155 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f0),
4156 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f1),
4157 /* 0xf2 - 0xff */
4158 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004159};
4160
Avi Kivity73fba5f2010-07-29 15:11:53 +03004161#undef D
4162#undef N
4163#undef G
4164#undef GD
4165#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004166#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004167#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004168
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004169#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004170#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004171#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004172#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004173#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004174
Avi Kivity9dac77f2011-06-01 15:34:25 +03004175static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004176{
4177 unsigned size;
4178
Avi Kivity9dac77f2011-06-01 15:34:25 +03004179 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004180 if (size == 8)
4181 size = 4;
4182 return size;
4183}
4184
4185static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4186 unsigned size, bool sign_extension)
4187{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004188 int rc = X86EMUL_CONTINUE;
4189
4190 op->type = OP_IMM;
4191 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004192 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004193 /* NB. Immediates are sign-extended as necessary. */
4194 switch (op->bytes) {
4195 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004196 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004197 break;
4198 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004199 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004200 break;
4201 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004202 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004203 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004204 case 8:
4205 op->val = insn_fetch(s64, ctxt);
4206 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004207 }
4208 if (!sign_extension) {
4209 switch (op->bytes) {
4210 case 1:
4211 op->val &= 0xff;
4212 break;
4213 case 2:
4214 op->val &= 0xffff;
4215 break;
4216 case 4:
4217 op->val &= 0xffffffff;
4218 break;
4219 }
4220 }
4221done:
4222 return rc;
4223}
4224
Avi Kivitya9945542011-09-13 10:45:41 +03004225static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4226 unsigned d)
4227{
4228 int rc = X86EMUL_CONTINUE;
4229
4230 switch (d) {
4231 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004232 decode_register_operand(ctxt, op);
Avi Kivitya9945542011-09-13 10:45:41 +03004233 break;
4234 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004235 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03004236 break;
4237 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004238 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004239 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03004240 *op = ctxt->memop;
4241 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004242 if (ctxt->d & BitOp)
Avi Kivitya9945542011-09-13 10:45:41 +03004243 fetch_bit_operand(ctxt);
4244 op->orig_val = op->val;
4245 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004246 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004247 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004248 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03004249 case OpAcc:
4250 op->type = OP_REG;
4251 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004252 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya9945542011-09-13 10:45:41 +03004253 fetch_register_operand(op);
4254 op->orig_val = op->val;
4255 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004256 case OpAccLo:
4257 op->type = OP_REG;
4258 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4259 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4260 fetch_register_operand(op);
4261 op->orig_val = op->val;
4262 break;
4263 case OpAccHi:
4264 if (ctxt->d & ByteOp) {
4265 op->type = OP_NONE;
4266 break;
4267 }
4268 op->type = OP_REG;
4269 op->bytes = ctxt->op_bytes;
4270 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4271 fetch_register_operand(op);
4272 op->orig_val = op->val;
4273 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004274 case OpDI:
4275 op->type = OP_MEM;
4276 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4277 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004278 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya9945542011-09-13 10:45:41 +03004279 op->addr.mem.seg = VCPU_SREG_ES;
4280 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004281 op->count = 1;
Avi Kivitya9945542011-09-13 10:45:41 +03004282 break;
4283 case OpDX:
4284 op->type = OP_REG;
4285 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004286 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya9945542011-09-13 10:45:41 +03004287 fetch_register_operand(op);
4288 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004289 case OpCL:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004290 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004291 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004292 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004293 break;
4294 case OpImmByte:
4295 rc = decode_imm(ctxt, op, 1, true);
4296 break;
4297 case OpOne:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004298 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004299 op->bytes = 1;
4300 op->val = 1;
4301 break;
4302 case OpImm:
4303 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4304 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004305 case OpImm64:
4306 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4307 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004308 case OpMem8:
4309 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004310 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004311 ctxt->memop.addr.reg = decode_register(ctxt,
4312 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004313 fetch_register_operand(&ctxt->memop);
4314 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004315 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004316 case OpMem16:
4317 ctxt->memop.bytes = 2;
4318 goto mem_common;
4319 case OpMem32:
4320 ctxt->memop.bytes = 4;
4321 goto mem_common;
4322 case OpImmU16:
4323 rc = decode_imm(ctxt, op, 2, false);
4324 break;
4325 case OpImmU:
4326 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4327 break;
4328 case OpSI:
4329 op->type = OP_MEM;
4330 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4331 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004332 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Bandan Das573e80f2014-04-16 12:46:13 -04004333 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03004334 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004335 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004336 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004337 case OpXLat:
4338 op->type = OP_MEM;
4339 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4340 op->addr.mem.ea =
4341 register_address(ctxt,
4342 reg_read(ctxt, VCPU_REGS_RBX) +
4343 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04004344 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004345 op->val = 0;
4346 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004347 case OpImmFAddr:
4348 op->type = OP_IMM;
4349 op->addr.mem.ea = ctxt->_eip;
4350 op->bytes = ctxt->op_bytes + 2;
4351 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4352 break;
4353 case OpMemFAddr:
4354 ctxt->memop.bytes = ctxt->op_bytes + 2;
4355 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004356 case OpES:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004357 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004358 op->val = VCPU_SREG_ES;
4359 break;
4360 case OpCS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004361 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004362 op->val = VCPU_SREG_CS;
4363 break;
4364 case OpSS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004365 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004366 op->val = VCPU_SREG_SS;
4367 break;
4368 case OpDS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004369 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004370 op->val = VCPU_SREG_DS;
4371 break;
4372 case OpFS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004373 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004374 op->val = VCPU_SREG_FS;
4375 break;
4376 case OpGS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004377 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004378 op->val = VCPU_SREG_GS;
4379 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004380 case OpImplicit:
4381 /* Special instructions do their own operand decoding. */
4382 default:
4383 op->type = OP_NONE; /* Disable writeback. */
4384 break;
4385 }
4386
4387done:
4388 return rc;
4389}
4390
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004391int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004392{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004393 int rc = X86EMUL_CONTINUE;
4394 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004395 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004396 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04004397 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03004398 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004399
Avi Kivityf09ed832011-09-13 10:45:40 +03004400 ctxt->memop.type = OP_NONE;
4401 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004402 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02004403 ctxt->fetch.ptr = ctxt->fetch.data;
4404 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004405 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01004406 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004407 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004408 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02004409 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004410 if (rc != X86EMUL_CONTINUE)
4411 return rc;
4412 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004413
4414 switch (mode) {
4415 case X86EMUL_MODE_REAL:
4416 case X86EMUL_MODE_VM86:
4417 case X86EMUL_MODE_PROT16:
4418 def_op_bytes = def_ad_bytes = 2;
4419 break;
4420 case X86EMUL_MODE_PROT32:
4421 def_op_bytes = def_ad_bytes = 4;
4422 break;
4423#ifdef CONFIG_X86_64
4424 case X86EMUL_MODE_PROT64:
4425 def_op_bytes = 4;
4426 def_ad_bytes = 8;
4427 break;
4428#endif
4429 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004430 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004431 }
4432
Avi Kivity9dac77f2011-06-01 15:34:25 +03004433 ctxt->op_bytes = def_op_bytes;
4434 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004435
4436 /* Legacy prefixes. */
4437 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004438 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004439 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004440 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004441 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004442 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004443 break;
4444 case 0x67: /* address-size override */
4445 if (mode == X86EMUL_MODE_PROT64)
4446 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004447 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004448 else
4449 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004450 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004451 break;
4452 case 0x26: /* ES override */
4453 case 0x2e: /* CS override */
4454 case 0x36: /* SS override */
4455 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004456 has_seg_override = true;
4457 ctxt->seg_override = (ctxt->b >> 3) & 3;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004458 break;
4459 case 0x64: /* FS override */
4460 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004461 has_seg_override = true;
4462 ctxt->seg_override = ctxt->b & 7;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004463 break;
4464 case 0x40 ... 0x4f: /* REX */
4465 if (mode != X86EMUL_MODE_PROT64)
4466 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004467 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004468 continue;
4469 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004470 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004471 break;
4472 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004473 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004474 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004475 break;
4476 default:
4477 goto done_prefixes;
4478 }
4479
4480 /* Any legacy prefix after a REX prefix nullifies its effect. */
4481
Avi Kivity9dac77f2011-06-01 15:34:25 +03004482 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004483 }
4484
4485done_prefixes:
4486
4487 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004488 if (ctxt->rex_prefix & 8)
4489 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004490
4491 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004492 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004493 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004494 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004495 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004496 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004497 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004498
4499 /* 0F_38 opcode map */
4500 if (ctxt->b == 0x38) {
4501 ctxt->opcode_len = 3;
4502 ctxt->b = insn_fetch(u8, ctxt);
4503 opcode = opcode_map_0f_38[ctxt->b];
4504 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004505 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004506 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004507
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004508 if (ctxt->d & ModRM)
4509 ctxt->modrm = insn_fetch(u8, ctxt);
4510
Nadav Amit7fe864d2014-06-02 18:34:03 +03004511 /* vex-prefix instructions are not implemented */
4512 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
4513 (mode == X86EMUL_MODE_PROT64 ||
4514 (mode >= X86EMUL_MODE_PROT16 && (ctxt->modrm & 0x80)))) {
4515 ctxt->d = NotImpl;
4516 }
4517
Avi Kivity9dac77f2011-06-01 15:34:25 +03004518 while (ctxt->d & GroupMask) {
4519 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004520 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004521 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004522 opcode = opcode.u.group[goffset];
4523 break;
4524 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004525 goffset = (ctxt->modrm >> 3) & 7;
4526 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004527 opcode = opcode.u.gdual->mod3[goffset];
4528 else
4529 opcode = opcode.u.gdual->mod012[goffset];
4530 break;
4531 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004532 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004533 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004534 break;
4535 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004536 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004537 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004538 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004539 switch (simd_prefix) {
4540 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4541 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4542 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4543 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4544 }
4545 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004546 case Escape:
4547 if (ctxt->modrm > 0xbf)
4548 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4549 else
4550 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4551 break;
Avi Kivity46561642011-04-24 14:09:59 +03004552 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004553 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004554 }
Avi Kivity46561642011-04-24 14:09:59 +03004555
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004556 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004557 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004558 }
4559
Paolo Bonzinie24186e2014-03-27 12:00:57 +01004560 /* Unrecognised? */
4561 if (ctxt->d == 0)
4562 return EMULATION_FAILED;
4563
Avi Kivity9dac77f2011-06-01 15:34:25 +03004564 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004565
Nadav Amit3a6095a2014-08-13 16:50:13 +03004566 if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD)))
4567 return EMULATION_FAILED;
4568
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004569 if (unlikely(ctxt->d &
Nadav Amit3a6095a2014-08-13 16:50:13 +03004570 (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm))) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004571 /*
4572 * These are copied unconditionally here, and checked unconditionally
4573 * in x86_emulate_insn.
4574 */
4575 ctxt->check_perm = opcode.check_perm;
4576 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004577
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004578 if (ctxt->d & NotImpl)
4579 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004580
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004581 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004582 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004583
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004584 if (ctxt->d & Op3264) {
4585 if (mode == X86EMUL_MODE_PROT64)
4586 ctxt->op_bytes = 8;
4587 else
4588 ctxt->op_bytes = 4;
4589 }
4590
4591 if (ctxt->d & Sse)
4592 ctxt->op_bytes = 16;
4593 else if (ctxt->d & Mmx)
4594 ctxt->op_bytes = 8;
4595 }
Avi Kivity12537912011-03-29 11:41:27 +02004596
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004597 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004598 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004599 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04004600 if (!has_seg_override) {
4601 has_seg_override = true;
4602 ctxt->seg_override = ctxt->modrm_seg;
4603 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004604 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004605 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004606 if (rc != X86EMUL_CONTINUE)
4607 goto done;
4608
Bandan Das573e80f2014-04-16 12:46:13 -04004609 if (!has_seg_override)
4610 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004611
Bandan Das573e80f2014-04-16 12:46:13 -04004612 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004613
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004614 /*
4615 * Decode and fetch the source operand: register, memory
4616 * or immediate.
4617 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004618 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004619 if (rc != X86EMUL_CONTINUE)
4620 goto done;
4621
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004622 /*
4623 * Decode and fetch the second source operand: register, memory
4624 * or immediate.
4625 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004626 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004627 if (rc != X86EMUL_CONTINUE)
4628 goto done;
4629
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004630 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03004631 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004632
Bandan Das41061cd2014-04-16 12:46:14 -04004633 if (ctxt->rip_relative)
Avi Kivityf09ed832011-09-13 10:45:40 +03004634 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004635
Paolo Bonzinia430c912014-10-23 14:54:14 +02004636done:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004637 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004638}
4639
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004640bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4641{
4642 return ctxt->d & PageTable;
4643}
4644
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004645static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4646{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004647 /* The second termination condition only applies for REPE
4648 * and REPNE. Test if the repeat string operation prefix is
4649 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4650 * corresponding termination condition according to:
4651 * - if REPE/REPZ and ZF = 0 then done
4652 * - if REPNE/REPNZ and ZF = 1 then done
4653 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004654 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4655 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4656 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004657 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004658 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004659 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4660 return true;
4661
4662 return false;
4663}
4664
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004665static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4666{
4667 bool fault = false;
4668
4669 ctxt->ops->get_fpu(ctxt);
4670 asm volatile("1: fwait \n\t"
4671 "2: \n\t"
4672 ".pushsection .fixup,\"ax\" \n\t"
4673 "3: \n\t"
4674 "movb $1, %[fault] \n\t"
4675 "jmp 2b \n\t"
4676 ".popsection \n\t"
4677 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004678 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004679 ctxt->ops->put_fpu(ctxt);
4680
4681 if (unlikely(fault))
4682 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4683
4684 return X86EMUL_CONTINUE;
4685}
4686
4687static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4688 struct operand *op)
4689{
4690 if (op->type == OP_MM)
4691 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4692}
4693
Avi Kivitye28bbd42013-01-04 16:18:48 +02004694static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4695{
4696 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivityb9fa4092013-02-09 11:31:48 +02004697 if (!(ctxt->d & ByteOp))
4698 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Avi Kivitye28bbd42013-01-04 16:18:48 +02004699 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004700 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
4701 [fastop]"+S"(fop)
4702 : "c"(ctxt->src2.val));
Avi Kivitye28bbd42013-01-04 16:18:48 +02004703 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004704 if (!fop) /* exception is returned in fop variable */
4705 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02004706 return X86EMUL_CONTINUE;
4707}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004708
Bandan Das14985072014-04-16 12:46:09 -04004709void init_decode_cache(struct x86_emulate_ctxt *ctxt)
4710{
Bandan Das573e80f2014-04-16 12:46:13 -04004711 memset(&ctxt->rip_relative, 0,
4712 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04004713
Bandan Das14985072014-04-16 12:46:09 -04004714 ctxt->io_read.pos = 0;
4715 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04004716 ctxt->mem_read.end = 0;
4717}
4718
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004719int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004720{
Mathias Krause0225fb52012-08-30 01:30:16 +02004721 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004722 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004723 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004724
Avi Kivity9dac77f2011-06-01 15:34:25 +03004725 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004726
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004727 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004728 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004729 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004730 goto done;
4731 }
4732
Avi Kivity9dac77f2011-06-01 15:34:25 +03004733 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004734 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004735 goto done;
4736 }
4737
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004738 if (unlikely(ctxt->d &
4739 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
4740 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4741 (ctxt->d & Undefined)) {
4742 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004743 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004744 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004745
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004746 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4747 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
4748 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004749 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004750 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004751
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004752 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
4753 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004754 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004755 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004756
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004757 if (ctxt->d & Mmx) {
4758 rc = flush_pending_x87_faults(ctxt);
4759 if (rc != X86EMUL_CONTINUE)
4760 goto done;
4761 /*
4762 * Now that we know the fpu is exception safe, we can fetch
4763 * operands from it.
4764 */
4765 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4766 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4767 if (!(ctxt->d & Mov))
4768 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4769 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004770
Bandan Das685bbf42014-04-16 12:46:10 -04004771 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004772 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4773 X86_ICPT_PRE_EXCEPT);
4774 if (rc != X86EMUL_CONTINUE)
4775 goto done;
4776 }
4777
4778 /* Privileged instruction can be executed only in CPL=0 */
4779 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Nadav Amit68efa762014-06-18 17:19:35 +03004780 if (ctxt->d & PrivUD)
4781 rc = emulate_ud(ctxt);
4782 else
4783 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004784 goto done;
4785 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004786
4787 /* Instruction can only be executed in protected mode */
4788 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
4789 rc = emulate_ud(ctxt);
4790 goto done;
4791 }
4792
4793 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04004794 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004795 rc = ctxt->check_perm(ctxt);
4796 if (rc != X86EMUL_CONTINUE)
4797 goto done;
4798 }
4799
Bandan Das685bbf42014-04-16 12:46:10 -04004800 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004801 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4802 X86_ICPT_POST_EXCEPT);
4803 if (rc != X86EMUL_CONTINUE)
4804 goto done;
4805 }
4806
4807 if (ctxt->rep_prefix && (ctxt->d & String)) {
4808 /* All REP prefixes have the same first termination condition */
4809 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
4810 ctxt->eip = ctxt->_eip;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004811 ctxt->eflags &= ~EFLG_RF;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004812 goto done;
4813 }
4814 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004815 }
4816
Avi Kivity9dac77f2011-06-01 15:34:25 +03004817 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4818 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4819 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004820 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004821 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004822 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004823 }
4824
Avi Kivity9dac77f2011-06-01 15:34:25 +03004825 if (ctxt->src2.type == OP_MEM) {
4826 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4827 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004828 if (rc != X86EMUL_CONTINUE)
4829 goto done;
4830 }
4831
Avi Kivity9dac77f2011-06-01 15:34:25 +03004832 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004833 goto special_insn;
4834
4835
Avi Kivity9dac77f2011-06-01 15:34:25 +03004836 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004837 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004838 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4839 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004840 if (rc != X86EMUL_CONTINUE)
4841 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004842 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004843 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004844
Avi Kivity018a98d2007-11-27 19:30:56 +02004845special_insn:
4846
Bandan Das685bbf42014-04-16 12:46:10 -04004847 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004848 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004849 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004850 if (rc != X86EMUL_CONTINUE)
4851 goto done;
4852 }
4853
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004854 if (ctxt->rep_prefix && (ctxt->d & String))
4855 ctxt->eflags |= EFLG_RF;
4856 else
4857 ctxt->eflags &= ~EFLG_RF;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004858
Avi Kivity9dac77f2011-06-01 15:34:25 +03004859 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004860 if (ctxt->d & Fastop) {
4861 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4862 rc = fastop(ctxt, fop);
4863 if (rc != X86EMUL_CONTINUE)
4864 goto done;
4865 goto writeback;
4866 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004867 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004868 if (rc != X86EMUL_CONTINUE)
4869 goto done;
4870 goto writeback;
4871 }
4872
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004873 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004874 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004875 else if (ctxt->opcode_len == 3)
4876 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004877
Avi Kivity9dac77f2011-06-01 15:34:25 +03004878 switch (ctxt->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004879 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004880 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004881 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004882 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004883 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004884 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004885 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03004886 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004887 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004888 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004889 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004890 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004891 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004892 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03004893 ctxt->dst.type = OP_NONE;
4894 else
4895 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004896 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004897 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004898 switch (ctxt->op_bytes) {
4899 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4900 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4901 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004902 }
4903 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004904 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004905 rc = emulate_int(ctxt, 3);
4906 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004907 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004908 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004909 break;
4910 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004911 if (ctxt->eflags & EFLG_OF)
4912 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004913 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004914 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004915 case 0xeb: /* jmp rel short */
Nadav Amit234f3ce2014-09-18 22:39:38 +03004916 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004917 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004918 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004919 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004920 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004921 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004922 case 0xf5: /* cmc */
4923 /* complement carry flag from eflags reg */
4924 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004925 break;
4926 case 0xf8: /* clc */
4927 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004928 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004929 case 0xf9: /* stc */
4930 ctxt->eflags |= EFLG_CF;
4931 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004932 case 0xfc: /* cld */
4933 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004934 break;
4935 case 0xfd: /* std */
4936 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004937 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004938 default:
4939 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004940 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004941
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004942 if (rc != X86EMUL_CONTINUE)
4943 goto done;
4944
Avi Kivity018a98d2007-11-27 19:30:56 +02004945writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02004946 if (ctxt->d & SrcWrite) {
4947 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
4948 rc = writeback(ctxt, &ctxt->src);
4949 if (rc != X86EMUL_CONTINUE)
4950 goto done;
4951 }
Nadav Amitee212292014-06-15 16:12:58 +03004952 if (!(ctxt->d & NoWrite)) {
4953 rc = writeback(ctxt, &ctxt->dst);
4954 if (rc != X86EMUL_CONTINUE)
4955 goto done;
4956 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004957
Gleb Natapov5cd21912010-03-18 15:20:26 +02004958 /*
4959 * restore dst type in case the decoding will be reused
4960 * (happens for string instruction )
4961 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004962 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004963
Avi Kivity9dac77f2011-06-01 15:34:25 +03004964 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004965 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004966
Avi Kivity9dac77f2011-06-01 15:34:25 +03004967 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004968 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004969
Avi Kivity9dac77f2011-06-01 15:34:25 +03004970 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004971 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004972 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004973 if ((ctxt->d & SrcMask) == SrcSI)
4974 count = ctxt->src.count;
4975 else
4976 count = ctxt->dst.count;
4977 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4978 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004979
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004980 if (!string_insn_completed(ctxt)) {
4981 /*
4982 * Re-enter guest when pio read ahead buffer is empty
4983 * or, if it is not used, after each 1024 iteration.
4984 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004985 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004986 (r->end == 0 || r->end != r->pos)) {
4987 /*
4988 * Reset read cache. Usually happens before
4989 * decode, but since instruction is restarted
4990 * we have to do it here.
4991 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004992 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004993 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004994 return EMULATION_RESTART;
4995 }
4996 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004997 }
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004998 ctxt->eflags &= ~EFLG_RF;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004999 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005000
Avi Kivity9dac77f2011-06-01 15:34:25 +03005001 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02005002
5003done:
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005004 if (rc == X86EMUL_PROPAGATE_FAULT) {
5005 WARN_ON(ctxt->exception.vector > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +02005006 ctxt->have_exception = true;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005007 }
Joerg Roedel775fde82011-04-04 12:39:24 +02005008 if (rc == X86EMUL_INTERCEPTED)
5009 return EMULATION_INTERCEPTED;
5010
Avi Kivitydd856ef2012-08-27 23:46:17 +03005011 if (rc == X86EMUL_CONTINUE)
5012 writeback_registers(ctxt);
5013
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005014 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005015
5016twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005017 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02005018 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02005019 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005020 break;
5021 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02005022 case 0x0d: /* GrpP (prefetch) */
5023 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02005024 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02005025 break;
5026 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005027 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02005028 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005029 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005030 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005031 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005032 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03005033 if (test_cc(ctxt->b, ctxt->eflags))
5034 ctxt->dst.val = ctxt->src.val;
5035 else if (ctxt->mode != X86EMUL_MODE_PROT64 ||
5036 ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005037 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08005038 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03005039 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03005040 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005041 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005042 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08005043 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005044 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08005045 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005046 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005047 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005048 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03005049 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005050 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005051 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005052 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005053 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03005054 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005055 break;
Sheng Yanga012e652007-10-15 14:24:20 +08005056 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005057 ctxt->dst.bytes = ctxt->op_bytes;
Nadav Amit3b320042014-06-02 18:34:08 +03005058 ctxt->dst.val = (ctxt->op_bytes == 8) ? (u64) ctxt->src.val :
5059 (u32) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08005060 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005061 default:
5062 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005063 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005064
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005065threebyte_insn:
5066
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005067 if (rc != X86EMUL_CONTINUE)
5068 goto done;
5069
Avi Kivity6aa8b732006-12-10 02:21:36 -08005070 goto writeback;
5071
5072cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02005073 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005074}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005075
5076void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
5077{
5078 invalidate_registers(ctxt);
5079}
5080
5081void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
5082{
5083 writeback_registers(ctxt);
5084}