blob: e8a58409b5ac0e2788a96a2fb97be7b77dea82bd [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 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800164
Avi Kivity820207c2013-02-09 11:31:45 +0200165#define DstXacc (DstAccLo | SrcAccHi | SrcWrite)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800166
Avi Kivityd0e53322010-07-29 15:11:54 +0300167#define X2(x...) x, x
168#define X3(x...) X2(x), x
169#define X4(x...) X2(x), X2(x)
170#define X5(x...) X4(x), x
171#define X6(x...) X4(x), X2(x)
172#define X7(x...) X4(x), X3(x)
173#define X8(x...) X4(x), X4(x)
174#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300175
Avi Kivitye28bbd42013-01-04 16:18:48 +0200176#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
177#define FASTOP_SIZE 8
178
179/*
180 * fastop functions have a special calling convention:
181 *
Avi Kivity017da7b2013-02-09 11:31:47 +0200182 * dst: rax (in/out)
183 * src: rdx (in/out)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200184 * src2: rcx (in)
185 * flags: rflags (in/out)
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200186 * ex: rsi (in:fastop pointer, out:zero if exception)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200187 *
188 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
189 * different operand sizes can be reached by calculation, rather than a jump
190 * table (which would be bigger than the code).
191 *
192 * fastop functions are declared as taking a never-defined fastop parameter,
193 * so they can't be called from C directly.
194 */
195
196struct fastop;
197
Avi Kivityd65b1de2010-07-29 15:11:35 +0300198struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300199 u64 flags : 56;
200 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300201 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300202 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200203 const struct opcode *group;
204 const struct group_dual *gdual;
205 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200206 const struct escape *esc;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200207 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300208 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200209 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300210};
211
212struct group_dual {
213 struct opcode mod012[8];
214 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300215};
216
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200217struct gprefix {
218 struct opcode pfx_no;
219 struct opcode pfx_66;
220 struct opcode pfx_f2;
221 struct opcode pfx_f3;
222};
223
Gleb Natapov045a2822012-12-20 16:57:43 +0200224struct escape {
225 struct opcode op[8];
226 struct opcode high[64];
227};
228
Avi Kivity6aa8b732006-12-10 02:21:36 -0800229/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200230#define EFLG_ID (1<<21)
231#define EFLG_VIP (1<<20)
232#define EFLG_VIF (1<<19)
233#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200234#define EFLG_VM (1<<17)
235#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200236#define EFLG_IOPL (3<<12)
237#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800238#define EFLG_OF (1<<11)
239#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200240#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200241#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800242#define EFLG_SF (1<<7)
243#define EFLG_ZF (1<<6)
244#define EFLG_AF (1<<4)
245#define EFLG_PF (1<<2)
246#define EFLG_CF (1<<0)
247
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300248#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
249#define EFLG_RESERVED_ONE_MASK 2
250
Avi Kivitydd856ef2012-08-27 23:46:17 +0300251static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
252{
253 if (!(ctxt->regs_valid & (1 << nr))) {
254 ctxt->regs_valid |= 1 << nr;
255 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
256 }
257 return ctxt->_regs[nr];
258}
259
260static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
261{
262 ctxt->regs_valid |= 1 << nr;
263 ctxt->regs_dirty |= 1 << nr;
264 return &ctxt->_regs[nr];
265}
266
267static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
268{
269 reg_read(ctxt, nr);
270 return reg_write(ctxt, nr);
271}
272
273static void writeback_registers(struct x86_emulate_ctxt *ctxt)
274{
275 unsigned reg;
276
277 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
278 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
279}
280
281static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
282{
283 ctxt->regs_dirty = 0;
284 ctxt->regs_valid = 0;
285}
286
Avi Kivity6aa8b732006-12-10 02:21:36 -0800287/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800288 * These EFLAGS bits are restored from saved value during emulation, and
289 * any changes are written back to the saved value after emulation.
290 */
291#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
292
Avi Kivitydda96d82008-11-26 15:14:10 +0200293#ifdef CONFIG_X86_64
294#define ON64(x) x
295#else
296#define ON64(x)
297#endif
298
Avi Kivity4d758342013-01-19 19:51:55 +0200299static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *));
300
Avi Kivityb7d491e2013-01-04 16:18:49 +0200301#define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t"
302#define FOP_RET "ret \n\t"
303
304#define FOP_START(op) \
305 extern void em_##op(struct fastop *fake); \
306 asm(".pushsection .text, \"ax\" \n\t" \
307 ".global em_" #op " \n\t" \
308 FOP_ALIGN \
309 "em_" #op ": \n\t"
310
311#define FOP_END \
312 ".popsection")
313
Avi Kivity0bdea062013-01-19 19:51:50 +0200314#define FOPNOP() FOP_ALIGN FOP_RET
315
Avi Kivityb7d491e2013-01-04 16:18:49 +0200316#define FOP1E(op, dst) \
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200317 FOP_ALIGN "10: " #op " %" #dst " \n\t" FOP_RET
318
319#define FOP1EEX(op, dst) \
320 FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200321
322#define FASTOP1(op) \
323 FOP_START(op) \
324 FOP1E(op##b, al) \
325 FOP1E(op##w, ax) \
326 FOP1E(op##l, eax) \
327 ON64(FOP1E(op##q, rax)) \
328 FOP_END
329
Avi Kivityb9fa4092013-02-09 11:31:48 +0200330/* 1-operand, using src2 (for MUL/DIV r/m) */
331#define FASTOP1SRC2(op, name) \
332 FOP_START(name) \
333 FOP1E(op, cl) \
334 FOP1E(op, cx) \
335 FOP1E(op, ecx) \
336 ON64(FOP1E(op, rcx)) \
337 FOP_END
338
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200339/* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */
340#define FASTOP1SRC2EX(op, name) \
341 FOP_START(name) \
342 FOP1EEX(op, cl) \
343 FOP1EEX(op, cx) \
344 FOP1EEX(op, ecx) \
345 ON64(FOP1EEX(op, rcx)) \
346 FOP_END
347
Avi Kivityf7857f32013-01-04 16:18:53 +0200348#define FOP2E(op, dst, src) \
349 FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET
350
351#define FASTOP2(op) \
352 FOP_START(op) \
Avi Kivity017da7b2013-02-09 11:31:47 +0200353 FOP2E(op##b, al, dl) \
354 FOP2E(op##w, ax, dx) \
355 FOP2E(op##l, eax, edx) \
356 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivityf7857f32013-01-04 16:18:53 +0200357 FOP_END
358
Avi Kivity11c363b2013-01-19 19:51:54 +0200359/* 2 operand, word only */
360#define FASTOP2W(op) \
361 FOP_START(op) \
362 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200363 FOP2E(op##w, ax, dx) \
364 FOP2E(op##l, eax, edx) \
365 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivity11c363b2013-01-19 19:51:54 +0200366 FOP_END
367
Avi Kivity007a3b52013-01-19 19:51:51 +0200368/* 2 operand, src is CL */
369#define FASTOP2CL(op) \
370 FOP_START(op) \
371 FOP2E(op##b, al, cl) \
372 FOP2E(op##w, ax, cl) \
373 FOP2E(op##l, eax, cl) \
374 ON64(FOP2E(op##q, rax, cl)) \
375 FOP_END
376
Avi Kivity0bdea062013-01-19 19:51:50 +0200377#define FOP3E(op, dst, src, src2) \
378 FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
379
380/* 3-operand, word-only, src2=cl */
381#define FASTOP3WCL(op) \
382 FOP_START(op) \
383 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200384 FOP3E(op##w, ax, dx, cl) \
385 FOP3E(op##l, eax, edx, cl) \
386 ON64(FOP3E(op##q, rax, rdx, cl)) \
Avi Kivity0bdea062013-01-19 19:51:50 +0200387 FOP_END
388
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200389/* Special case for SETcc - 1 instruction per cc */
390#define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t"
391
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200392asm(".global kvm_fastop_exception \n"
393 "kvm_fastop_exception: xor %esi, %esi; ret");
394
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200395FOP_START(setcc)
396FOP_SETCC(seto)
397FOP_SETCC(setno)
398FOP_SETCC(setc)
399FOP_SETCC(setnc)
400FOP_SETCC(setz)
401FOP_SETCC(setnz)
402FOP_SETCC(setbe)
403FOP_SETCC(setnbe)
404FOP_SETCC(sets)
405FOP_SETCC(setns)
406FOP_SETCC(setp)
407FOP_SETCC(setnp)
408FOP_SETCC(setl)
409FOP_SETCC(setnl)
410FOP_SETCC(setle)
411FOP_SETCC(setnle)
412FOP_END;
413
Paolo Bonzini326f5782013-05-09 11:32:51 +0200414FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET
415FOP_END;
416
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200417static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
418 enum x86_intercept intercept,
419 enum x86_intercept_stage stage)
420{
421 struct x86_instruction_info info = {
422 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300423 .rep_prefix = ctxt->rep_prefix,
424 .modrm_mod = ctxt->modrm_mod,
425 .modrm_reg = ctxt->modrm_reg,
426 .modrm_rm = ctxt->modrm_rm,
427 .src_val = ctxt->src.val64,
428 .src_bytes = ctxt->src.bytes,
429 .dst_bytes = ctxt->dst.bytes,
430 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200431 .next_rip = ctxt->eip,
432 };
433
Avi Kivity29535382011-04-20 13:37:53 +0300434 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200435}
436
Avi Kivityf47cfa32012-06-07 17:49:24 +0300437static void assign_masked(ulong *dest, ulong src, ulong mask)
438{
439 *dest = (*dest & ~mask) | (src & mask);
440}
441
Avi Kivity9dac77f2011-06-01 15:34:25 +0300442static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800443{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300444 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800445}
446
Avi Kivityf47cfa32012-06-07 17:49:24 +0300447static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
448{
449 u16 sel;
450 struct desc_struct ss;
451
452 if (ctxt->mode == X86EMUL_MODE_PROT64)
453 return ~0UL;
454 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
455 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
456}
457
Avi Kivity612e89f2012-06-12 20:03:23 +0300458static int stack_size(struct x86_emulate_ctxt *ctxt)
459{
460 return (__fls(stack_mask(ctxt)) + 1) >> 3;
461}
462
Avi Kivity6aa8b732006-12-10 02:21:36 -0800463/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800464static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300465address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800466{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300467 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800468 return reg;
469 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300470 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800471}
472
473static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300474register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800475{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300476 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800477}
478
Avi Kivity5ad105e2012-08-19 14:34:31 +0300479static void masked_increment(ulong *reg, ulong mask, int inc)
480{
481 assign_masked(reg, *reg + inc, mask);
482}
483
Harvey Harrison7a9572752008-02-19 07:40:41 -0800484static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300485register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800486{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300487 ulong mask;
488
Avi Kivity9dac77f2011-06-01 15:34:25 +0300489 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300490 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800491 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300492 mask = ad_mask(ctxt);
493 masked_increment(reg, mask, inc);
494}
495
496static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
497{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300498 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800499}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800500
Avi Kivity9dac77f2011-06-01 15:34:25 +0300501static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800502{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300503 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800504}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300505
Avi Kivity56697682011-04-03 14:08:51 +0300506static u32 desc_limit_scaled(struct desc_struct *desc)
507{
508 u32 limit = get_desc_limit(desc);
509
510 return desc->g ? (limit << 12) | 0xfff : limit;
511}
512
Avi Kivity9dac77f2011-06-01 15:34:25 +0300513static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300514{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300515 ctxt->has_seg_override = true;
516 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300517}
518
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900519static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300520{
521 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
522 return 0;
523
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900524 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300525}
526
Avi Kivity9dac77f2011-06-01 15:34:25 +0300527static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300528{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300529 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300530 return 0;
531
Avi Kivity9dac77f2011-06-01 15:34:25 +0300532 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300533}
534
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200535static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
536 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300537{
Avi Kivityda9cb572010-11-22 17:53:21 +0200538 ctxt->exception.vector = vec;
539 ctxt->exception.error_code = error;
540 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200541 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300542}
543
Joerg Roedel3b88e412011-04-04 12:39:29 +0200544static int emulate_db(struct x86_emulate_ctxt *ctxt)
545{
546 return emulate_exception(ctxt, DB_VECTOR, 0, false);
547}
548
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200549static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300550{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200551 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300552}
553
Avi Kivity618ff152011-04-03 12:32:09 +0300554static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
555{
556 return emulate_exception(ctxt, SS_VECTOR, err, true);
557}
558
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200559static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300560{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200561 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300562}
563
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200564static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300565{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200566 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300567}
568
Avi Kivity34d1f492010-08-26 11:59:01 +0300569static int emulate_de(struct x86_emulate_ctxt *ctxt)
570{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200571 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300572}
573
Avi Kivity12537912011-03-29 11:41:27 +0200574static int emulate_nm(struct x86_emulate_ctxt *ctxt)
575{
576 return emulate_exception(ctxt, NM_VECTOR, 0, false);
577}
578
Avi Kivity1aa36612011-04-27 13:20:30 +0300579static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
580{
581 u16 selector;
582 struct desc_struct desc;
583
584 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
585 return selector;
586}
587
588static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
589 unsigned seg)
590{
591 u16 dummy;
592 u32 base3;
593 struct desc_struct desc;
594
595 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
596 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
597}
598
Avi Kivity1c11b372012-04-09 18:39:59 +0300599/*
600 * x86 defines three classes of vector instructions: explicitly
601 * aligned, explicitly unaligned, and the rest, which change behaviour
602 * depending on whether they're AVX encoded or not.
603 *
604 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
605 * subject to the same check.
606 */
607static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
608{
609 if (likely(size < 16))
610 return false;
611
612 if (ctxt->d & Aligned)
613 return true;
614 else if (ctxt->d & Unaligned)
615 return false;
616 else if (ctxt->d & Avx)
617 return false;
618 else
619 return true;
620}
621
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400622static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300623 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400624 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300625 ulong *linear)
626{
Avi Kivity618ff152011-04-03 12:32:09 +0300627 struct desc_struct desc;
628 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300629 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300630 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300631 u16 sel;
Gleb Natapov3a78a4f2012-12-20 16:57:42 +0200632 unsigned cpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300633
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900634 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300635 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300636 case X86EMUL_MODE_PROT64:
637 if (((signed long)la << 16) >> 16 != la)
638 return emulate_gp(ctxt, 0);
639 break;
640 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300641 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
642 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300643 if (!usable)
644 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200645 /* code segment in protected mode or read-only data segment */
646 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
647 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300648 goto bad;
649 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400650 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300651 goto bad;
652 lim = desc_limit_scaled(&desc);
653 if ((desc.type & 8) || !(desc.type & 4)) {
654 /* expand-up segment */
655 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
656 goto bad;
657 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800658 /* expand-down segment */
Avi Kivity618ff152011-04-03 12:32:09 +0300659 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
660 goto bad;
661 lim = desc.d ? 0xffffffff : 0xffff;
662 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
663 goto bad;
664 }
Avi Kivity717746e2011-04-20 13:37:53 +0300665 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity618ff152011-04-03 12:32:09 +0300666 if (!(desc.type & 8)) {
667 /* data segment */
668 if (cpl > desc.dpl)
669 goto bad;
670 } else if ((desc.type & 8) && !(desc.type & 4)) {
671 /* nonconforming code segment */
672 if (cpl != desc.dpl)
673 goto bad;
674 } else if ((desc.type & 8) && (desc.type & 4)) {
675 /* conforming code segment */
676 if (cpl < desc.dpl)
677 goto bad;
678 }
679 break;
680 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300681 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300682 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300683 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
684 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300685 *linear = la;
686 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300687bad:
688 if (addr.seg == VCPU_SREG_SS)
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300689 return emulate_ss(ctxt, sel);
Avi Kivity618ff152011-04-03 12:32:09 +0300690 else
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300691 return emulate_gp(ctxt, sel);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300692}
693
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400694static int linearize(struct x86_emulate_ctxt *ctxt,
695 struct segmented_address addr,
696 unsigned size, bool write,
697 ulong *linear)
698{
699 return __linearize(ctxt, addr, size, write, false, linear);
700}
701
702
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200703static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
704 struct segmented_address addr,
705 void *data,
706 unsigned size)
707{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200708 int rc;
709 ulong linear;
710
Avi Kivity83b87952011-04-03 11:31:19 +0300711 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200712 if (rc != X86EMUL_CONTINUE)
713 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300714 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200715}
716
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900717/*
718 * Fetch the next byte of the instruction being emulated which is pointed to
719 * by ctxt->_eip, then increment ctxt->_eip.
720 *
721 * Also prefetch the remaining bytes of the instruction without crossing page
722 * boundary if they are not in fetch_cache yet.
723 */
724static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200725{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300726 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200727 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300728 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200729
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900730 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400731 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900732 struct segmented_address addr = { .seg = VCPU_SREG_CS,
733 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300734 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900735 size = min(15UL - cur_size,
736 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400737 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900738 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400739 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900740 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
741 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900742 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200743 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300744 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200745 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900746 *dest = fc->data[ctxt->_eip - fc->start];
747 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900748 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200749}
750
751static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900752 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200753{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900754 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200755
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200756 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900757 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200758 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200759 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900760 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900761 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200762 return rc;
763 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900764 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200765}
766
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900767/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900768#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900769({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900770 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900771 if (rc != X86EMUL_CONTINUE) \
772 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900773 (_type)_x; \
774})
775
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900776#define insn_fetch_arr(_arr, _size, _ctxt) \
777({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900778 if (rc != X86EMUL_CONTINUE) \
779 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900780})
781
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000782/*
783 * Given the 'reg' portion of a ModRM byte, and a register block, return a
784 * pointer into the block that addresses the relevant register.
785 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
786 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300787static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200788 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789{
790 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200791 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800792
Avi Kivity6aa8b732006-12-10 02:21:36 -0800793 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300794 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
795 else
796 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800797 return p;
798}
799
800static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200801 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800802 u16 *size, unsigned long *address, int op_bytes)
803{
804 int rc;
805
806 if (op_bytes == 2)
807 op_bytes = 3;
808 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200809 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900810 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200812 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200813 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800814 return rc;
815}
816
Avi Kivity34b77652013-01-19 19:51:56 +0200817FASTOP2(add);
818FASTOP2(or);
819FASTOP2(adc);
820FASTOP2(sbb);
821FASTOP2(and);
822FASTOP2(sub);
823FASTOP2(xor);
824FASTOP2(cmp);
825FASTOP2(test);
826
Avi Kivityb9fa4092013-02-09 11:31:48 +0200827FASTOP1SRC2(mul, mul_ex);
828FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200829FASTOP1SRC2EX(div, div_ex);
830FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +0200831
Avi Kivity34b77652013-01-19 19:51:56 +0200832FASTOP3WCL(shld);
833FASTOP3WCL(shrd);
834
835FASTOP2W(imul);
836
837FASTOP1(not);
838FASTOP1(neg);
839FASTOP1(inc);
840FASTOP1(dec);
841
842FASTOP2CL(rol);
843FASTOP2CL(ror);
844FASTOP2CL(rcl);
845FASTOP2CL(rcr);
846FASTOP2CL(shl);
847FASTOP2CL(shr);
848FASTOP2CL(sar);
849
850FASTOP2W(bsf);
851FASTOP2W(bsr);
852FASTOP2W(bt);
853FASTOP2W(bts);
854FASTOP2W(btr);
855FASTOP2W(btc);
856
Avi Kivitye47a5f52013-02-09 11:31:51 +0200857FASTOP2(xadd);
858
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200859static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300860{
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200861 u8 rc;
862 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300863
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200864 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +0200865 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200866 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
867 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300868}
869
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300870static void fetch_register_operand(struct operand *op)
871{
872 switch (op->bytes) {
873 case 1:
874 op->val = *(u8 *)op->addr.reg;
875 break;
876 case 2:
877 op->val = *(u16 *)op->addr.reg;
878 break;
879 case 4:
880 op->val = *(u32 *)op->addr.reg;
881 break;
882 case 8:
883 op->val = *(u64 *)op->addr.reg;
884 break;
885 }
886}
887
Avi Kivity12537912011-03-29 11:41:27 +0200888static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
889{
890 ctxt->ops->get_fpu(ctxt);
891 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200892 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
893 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
894 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
895 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
896 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
897 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
898 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
899 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200900#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200901 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
902 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
903 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
904 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
905 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
906 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
907 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
908 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200909#endif
910 default: BUG();
911 }
912 ctxt->ops->put_fpu(ctxt);
913}
914
915static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
916 int reg)
917{
918 ctxt->ops->get_fpu(ctxt);
919 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200920 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
921 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
922 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
923 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
924 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
925 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
926 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
927 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200928#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200929 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
930 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
931 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
932 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
933 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
934 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
935 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
936 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200937#endif
938 default: BUG();
939 }
940 ctxt->ops->put_fpu(ctxt);
941}
942
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300943static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
944{
945 ctxt->ops->get_fpu(ctxt);
946 switch (reg) {
947 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
948 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
949 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
950 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
951 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
952 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
953 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
954 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
955 default: BUG();
956 }
957 ctxt->ops->put_fpu(ctxt);
958}
959
960static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
961{
962 ctxt->ops->get_fpu(ctxt);
963 switch (reg) {
964 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
965 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
966 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
967 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
968 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
969 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
970 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
971 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
972 default: BUG();
973 }
974 ctxt->ops->put_fpu(ctxt);
975}
976
Gleb Natapov045a2822012-12-20 16:57:43 +0200977static int em_fninit(struct x86_emulate_ctxt *ctxt)
978{
979 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
980 return emulate_nm(ctxt);
981
982 ctxt->ops->get_fpu(ctxt);
983 asm volatile("fninit");
984 ctxt->ops->put_fpu(ctxt);
985 return X86EMUL_CONTINUE;
986}
987
988static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
989{
990 u16 fcw;
991
992 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
993 return emulate_nm(ctxt);
994
995 ctxt->ops->get_fpu(ctxt);
996 asm volatile("fnstcw %0": "+m"(fcw));
997 ctxt->ops->put_fpu(ctxt);
998
999 /* force 2 byte destination */
1000 ctxt->dst.bytes = 2;
1001 ctxt->dst.val = fcw;
1002
1003 return X86EMUL_CONTINUE;
1004}
1005
1006static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1007{
1008 u16 fsw;
1009
1010 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1011 return emulate_nm(ctxt);
1012
1013 ctxt->ops->get_fpu(ctxt);
1014 asm volatile("fnstsw %0": "+m"(fsw));
1015 ctxt->ops->put_fpu(ctxt);
1016
1017 /* force 2 byte destination */
1018 ctxt->dst.bytes = 2;
1019 ctxt->dst.val = fsw;
1020
1021 return X86EMUL_CONTINUE;
1022}
1023
Avi Kivity12537912011-03-29 11:41:27 +02001024static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001025 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001026{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001027 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001028
Avi Kivity9dac77f2011-06-01 15:34:25 +03001029 if (!(ctxt->d & ModRM))
1030 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +02001031
Avi Kivity9dac77f2011-06-01 15:34:25 +03001032 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001033 op->type = OP_XMM;
1034 op->bytes = 16;
1035 op->addr.xmm = reg;
1036 read_sse_reg(ctxt, &op->vec_val, reg);
1037 return;
1038 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001039 if (ctxt->d & Mmx) {
1040 reg &= 7;
1041 op->type = OP_MM;
1042 op->bytes = 8;
1043 op->addr.mm = reg;
1044 return;
1045 }
Avi Kivity12537912011-03-29 11:41:27 +02001046
Avi Kivity3c118e22007-10-31 10:27:04 +02001047 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001048 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1049 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1050
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001051 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001052 op->orig_val = op->val;
1053}
1054
Avi Kivitya6e34072012-06-10 17:15:39 +03001055static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1056{
1057 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1058 ctxt->modrm_seg = VCPU_SREG_SS;
1059}
1060
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001061static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001062 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001063{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001064 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001065 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001066 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001067 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001068
Avi Kivity9dac77f2011-06-01 15:34:25 +03001069 if (ctxt->rex_prefix) {
1070 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
1071 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
1072 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001073 }
1074
Avi Kivity9dac77f2011-06-01 15:34:25 +03001075 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
1076 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
1077 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
1078 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001079
Avi Kivity9dac77f2011-06-01 15:34:25 +03001080 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001081 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001082 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001083 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001084 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001085 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001086 op->type = OP_XMM;
1087 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001088 op->addr.xmm = ctxt->modrm_rm;
1089 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001090 return rc;
1091 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001092 if (ctxt->d & Mmx) {
1093 op->type = OP_MM;
1094 op->bytes = 8;
1095 op->addr.xmm = ctxt->modrm_rm & 7;
1096 return rc;
1097 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001098 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001099 return rc;
1100 }
1101
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001102 op->type = OP_MEM;
1103
Avi Kivity9dac77f2011-06-01 15:34:25 +03001104 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001105 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1106 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1107 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1108 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001109
1110 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001111 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001112 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001113 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001114 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001115 break;
1116 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001117 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001118 break;
1119 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001120 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001121 break;
1122 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001123 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001124 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001125 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001126 break;
1127 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001128 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001129 break;
1130 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001131 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001132 break;
1133 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001134 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001135 break;
1136 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001137 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001138 break;
1139 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001140 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001141 break;
1142 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001143 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001144 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001145 break;
1146 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001147 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001148 break;
1149 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001150 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1151 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1152 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001153 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001154 } else {
1155 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001156 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001157 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001158 index_reg |= (sib >> 3) & 7;
1159 base_reg |= sib & 7;
1160 scale = sib >> 6;
1161
Avi Kivity9dac77f2011-06-01 15:34:25 +03001162 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001163 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001164 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001165 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001166 adjust_modrm_seg(ctxt, base_reg);
1167 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001168 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001169 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001170 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001171 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001172 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001173 } else {
1174 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001175 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001176 adjust_modrm_seg(ctxt, base_reg);
1177 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001178 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001179 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001180 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001181 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001182 break;
1183 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001184 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001185 break;
1186 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001187 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001188 break;
1189 }
1190 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001191 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001192done:
1193 return rc;
1194}
1195
1196static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001197 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001198{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001199 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001200
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001201 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001202 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001203 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001204 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001205 break;
1206 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001207 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001208 break;
1209 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001210 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001211 break;
1212 }
1213done:
1214 return rc;
1215}
1216
Avi Kivity9dac77f2011-06-01 15:34:25 +03001217static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001218{
Sheng Yang7129eec2010-09-28 16:33:32 +08001219 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001220
Avi Kivity9dac77f2011-06-01 15:34:25 +03001221 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1222 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001223
Avi Kivity9dac77f2011-06-01 15:34:25 +03001224 if (ctxt->src.bytes == 2)
1225 sv = (s16)ctxt->src.val & (s16)mask;
1226 else if (ctxt->src.bytes == 4)
1227 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001228
Avi Kivity9dac77f2011-06-01 15:34:25 +03001229 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001230 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001231
1232 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001233 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001234}
1235
Gleb Natapov9de41572010-04-28 19:15:22 +03001236static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001237 unsigned long addr, void *dest, unsigned size)
1238{
1239 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001240 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001241
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001242 if (mc->pos < mc->end)
1243 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001244
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001245 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001246
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001247 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1248 &ctxt->exception);
1249 if (rc != X86EMUL_CONTINUE)
1250 return rc;
1251
1252 mc->end += size;
1253
1254read_cached:
1255 memcpy(dest, mc->data + mc->pos, size);
1256 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001257 return X86EMUL_CONTINUE;
1258}
1259
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001260static int segmented_read(struct x86_emulate_ctxt *ctxt,
1261 struct segmented_address addr,
1262 void *data,
1263 unsigned size)
1264{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001265 int rc;
1266 ulong linear;
1267
Avi Kivity83b87952011-04-03 11:31:19 +03001268 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001269 if (rc != X86EMUL_CONTINUE)
1270 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001271 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001272}
1273
1274static int segmented_write(struct x86_emulate_ctxt *ctxt,
1275 struct segmented_address addr,
1276 const void *data,
1277 unsigned size)
1278{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001279 int rc;
1280 ulong linear;
1281
Avi Kivity83b87952011-04-03 11:31:19 +03001282 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001283 if (rc != X86EMUL_CONTINUE)
1284 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001285 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1286 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001287}
1288
1289static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1290 struct segmented_address addr,
1291 const void *orig_data, const void *data,
1292 unsigned size)
1293{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001294 int rc;
1295 ulong linear;
1296
Avi Kivity83b87952011-04-03 11:31:19 +03001297 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001298 if (rc != X86EMUL_CONTINUE)
1299 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001300 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1301 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001302}
1303
Gleb Natapov7b262e92010-03-18 15:20:27 +02001304static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001305 unsigned int size, unsigned short port,
1306 void *dest)
1307{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001308 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001309
1310 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001311 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001312 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001313 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001314 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001315 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1316 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Gleb Natapov7b262e92010-03-18 15:20:27 +02001317 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1318 count);
1319 if (n == 0)
1320 n = 1;
1321 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001322 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001323 return 0;
1324 rc->end = n * size;
1325 }
1326
Nadav Amite6e39f02014-04-18 03:35:10 +03001327 if (ctxt->rep_prefix && (ctxt->d & String) &&
1328 !(ctxt->eflags & EFLG_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001329 ctxt->dst.data = rc->data + rc->pos;
1330 ctxt->dst.type = OP_MEM_STR;
1331 ctxt->dst.count = (rc->end - rc->pos) / size;
1332 rc->pos = rc->end;
1333 } else {
1334 memcpy(dest, rc->data + rc->pos, size);
1335 rc->pos += size;
1336 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001337 return 1;
1338}
1339
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001340static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1341 u16 index, struct desc_struct *desc)
1342{
1343 struct desc_ptr dt;
1344 ulong addr;
1345
1346 ctxt->ops->get_idt(ctxt, &dt);
1347
1348 if (dt.size < index * 8 + 7)
1349 return emulate_gp(ctxt, index << 3 | 0x2);
1350
1351 addr = dt.address + index * 8;
1352 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1353 &ctxt->exception);
1354}
1355
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001356static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001357 u16 selector, struct desc_ptr *dt)
1358{
Mathias Krause0225fb52012-08-30 01:30:16 +02001359 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001360
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001361 if (selector & 1 << 2) {
1362 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001363 u16 sel;
1364
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001365 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001366 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001367 return;
1368
1369 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1370 dt->address = get_desc_base(&desc);
1371 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001372 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001373}
1374
1375/* allowed just for 8 bytes segments */
1376static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001377 u16 selector, struct desc_struct *desc,
1378 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001379{
1380 struct desc_ptr dt;
1381 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001382 ulong addr;
1383
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001384 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001385
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001386 if (dt.size < index * 8 + 7)
1387 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001388
Avi Kivitye9194642012-06-13 16:29:39 +03001389 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001390 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1391 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001392}
1393
1394/* allowed just for 8 bytes segments */
1395static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001396 u16 selector, struct desc_struct *desc)
1397{
1398 struct desc_ptr dt;
1399 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001400 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001401
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001402 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001403
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001404 if (dt.size < index * 8 + 7)
1405 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001406
1407 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001408 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1409 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001410}
1411
Gleb Natapov5601d052011-03-07 14:55:06 +02001412/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001413static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001414 u16 selector, int seg)
1415{
Avi Kivity869be992012-06-13 16:30:53 +03001416 struct desc_struct seg_desc, old_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001417 u8 dpl, rpl, cpl;
1418 unsigned err_vec = GP_VECTOR;
1419 u32 err_code = 0;
1420 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001421 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001422 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001423 u16 dummy;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001424
1425 memset(&seg_desc, 0, sizeof seg_desc);
1426
Kevin Wolff8da94e2013-04-11 14:06:03 +02001427 if (ctxt->mode == X86EMUL_MODE_REAL) {
1428 /* set real mode segment descriptor (keep limit etc. for
1429 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001430 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001431 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001432 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001433 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1434 /* VM86 needs a clean new segment descriptor */
1435 set_desc_base(&seg_desc, selector << 4);
1436 set_desc_limit(&seg_desc, 0xffff);
1437 seg_desc.type = 3;
1438 seg_desc.p = 1;
1439 seg_desc.s = 1;
1440 seg_desc.dpl = 3;
1441 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001442 }
1443
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001444 rpl = selector & 3;
1445 cpl = ctxt->ops->cpl(ctxt);
1446
1447 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1448 if ((seg == VCPU_SREG_CS
1449 || (seg == VCPU_SREG_SS
1450 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1451 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001452 && null_selector)
1453 goto exception;
1454
1455 /* TR should be in GDT only */
1456 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1457 goto exception;
1458
1459 if (null_selector) /* for NULL selector skip all following checks */
1460 goto load;
1461
Avi Kivitye9194642012-06-13 16:29:39 +03001462 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001463 if (ret != X86EMUL_CONTINUE)
1464 return ret;
1465
1466 err_code = selector & 0xfffc;
1467 err_vec = GP_VECTOR;
1468
Guo Chaofc058682012-06-28 15:19:51 +08001469 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001470 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1471 goto exception;
1472
1473 if (!seg_desc.p) {
1474 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1475 goto exception;
1476 }
1477
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001478 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001479
1480 switch (seg) {
1481 case VCPU_SREG_SS:
1482 /*
1483 * segment is not a writable data segment or segment
1484 * selector's RPL != CPL or segment selector's RPL != CPL
1485 */
1486 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1487 goto exception;
1488 break;
1489 case VCPU_SREG_CS:
1490 if (!(seg_desc.type & 8))
1491 goto exception;
1492
1493 if (seg_desc.type & 4) {
1494 /* conforming */
1495 if (dpl > cpl)
1496 goto exception;
1497 } else {
1498 /* nonconforming */
1499 if (rpl > cpl || dpl != cpl)
1500 goto exception;
1501 }
1502 /* CS(RPL) <- CPL */
1503 selector = (selector & 0xfffc) | cpl;
1504 break;
1505 case VCPU_SREG_TR:
1506 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1507 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001508 old_desc = seg_desc;
1509 seg_desc.type |= 2; /* busy */
1510 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1511 sizeof(seg_desc), &ctxt->exception);
1512 if (ret != X86EMUL_CONTINUE)
1513 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001514 break;
1515 case VCPU_SREG_LDTR:
1516 if (seg_desc.s || seg_desc.type != 2)
1517 goto exception;
1518 break;
1519 default: /* DS, ES, FS, or GS */
1520 /*
1521 * segment is not a data or readable code segment or
1522 * ((segment is a data or nonconforming code segment)
1523 * and (both RPL and CPL > DPL))
1524 */
1525 if ((seg_desc.type & 0xa) == 0x8 ||
1526 (((seg_desc.type & 0xc) != 0xc) &&
1527 (rpl > dpl && cpl > dpl)))
1528 goto exception;
1529 break;
1530 }
1531
1532 if (seg_desc.s) {
1533 /* mark segment as accessed */
1534 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001535 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001536 if (ret != X86EMUL_CONTINUE)
1537 return ret;
1538 }
1539load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001540 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001541 return X86EMUL_CONTINUE;
1542exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001543 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001544 return X86EMUL_PROPAGATE_FAULT;
1545}
1546
Wei Yongjun31be40b2010-08-17 09:17:30 +08001547static void write_register_operand(struct operand *op)
1548{
1549 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1550 switch (op->bytes) {
1551 case 1:
1552 *(u8 *)op->addr.reg = (u8)op->val;
1553 break;
1554 case 2:
1555 *(u16 *)op->addr.reg = (u16)op->val;
1556 break;
1557 case 4:
1558 *op->addr.reg = (u32)op->val;
1559 break; /* 64b: zero-extend */
1560 case 8:
1561 *op->addr.reg = op->val;
1562 break;
1563 }
1564}
1565
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001566static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001567{
1568 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001569
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001570 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001571 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001572 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001573 break;
1574 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001575 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001576 rc = segmented_cmpxchg(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001577 op->addr.mem,
1578 &op->orig_val,
1579 &op->val,
1580 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001581 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001582 rc = segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001583 op->addr.mem,
1584 &op->val,
1585 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001586 if (rc != X86EMUL_CONTINUE)
1587 return rc;
1588 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001589 case OP_MEM_STR:
1590 rc = segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001591 op->addr.mem,
1592 op->data,
1593 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001594 if (rc != X86EMUL_CONTINUE)
1595 return rc;
1596 break;
Avi Kivity12537912011-03-29 11:41:27 +02001597 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001598 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001599 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001600 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001601 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001602 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001603 case OP_NONE:
1604 /* no writeback */
1605 break;
1606 default:
1607 break;
1608 }
1609 return X86EMUL_CONTINUE;
1610}
1611
Avi Kivity51ddff52012-06-12 20:19:40 +03001612static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001613{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001614 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001615
Avi Kivity5ad105e2012-08-19 14:34:31 +03001616 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001617 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001618 addr.seg = VCPU_SREG_SS;
1619
Avi Kivity51ddff52012-06-12 20:19:40 +03001620 return segmented_write(ctxt, addr, data, bytes);
1621}
1622
1623static int em_push(struct x86_emulate_ctxt *ctxt)
1624{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001625 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001626 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001627 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001628}
1629
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001630static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001631 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001632{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001633 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001634 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001635
Avi Kivitydd856ef2012-08-27 23:46:17 +03001636 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001637 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001638 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001639 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001640 return rc;
1641
Avi Kivity5ad105e2012-08-19 14:34:31 +03001642 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001643 return rc;
1644}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001645
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001646static int em_pop(struct x86_emulate_ctxt *ctxt)
1647{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001648 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001649}
1650
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001651static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001652 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001653{
1654 int rc;
1655 unsigned long val, change_mask;
1656 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001657 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001658
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001659 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001660 if (rc != X86EMUL_CONTINUE)
1661 return rc;
1662
1663 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1664 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1665
1666 switch(ctxt->mode) {
1667 case X86EMUL_MODE_PROT64:
1668 case X86EMUL_MODE_PROT32:
1669 case X86EMUL_MODE_PROT16:
1670 if (cpl == 0)
1671 change_mask |= EFLG_IOPL;
1672 if (cpl <= iopl)
1673 change_mask |= EFLG_IF;
1674 break;
1675 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001676 if (iopl < 3)
1677 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001678 change_mask |= EFLG_IF;
1679 break;
1680 default: /* real mode */
1681 change_mask |= (EFLG_IOPL | EFLG_IF);
1682 break;
1683 }
1684
1685 *(unsigned long *)dest =
1686 (ctxt->eflags & ~change_mask) | (val & change_mask);
1687
1688 return rc;
1689}
1690
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001691static int em_popf(struct x86_emulate_ctxt *ctxt)
1692{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001693 ctxt->dst.type = OP_REG;
1694 ctxt->dst.addr.reg = &ctxt->eflags;
1695 ctxt->dst.bytes = ctxt->op_bytes;
1696 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001697}
1698
Avi Kivity612e89f2012-06-12 20:03:23 +03001699static int em_enter(struct x86_emulate_ctxt *ctxt)
1700{
1701 int rc;
1702 unsigned frame_size = ctxt->src.val;
1703 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001704 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001705
1706 if (nesting_level)
1707 return X86EMUL_UNHANDLEABLE;
1708
Avi Kivitydd856ef2012-08-27 23:46:17 +03001709 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1710 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001711 if (rc != X86EMUL_CONTINUE)
1712 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001713 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001714 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001715 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1716 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001717 stack_mask(ctxt));
1718 return X86EMUL_CONTINUE;
1719}
1720
Avi Kivityf47cfa32012-06-07 17:49:24 +03001721static int em_leave(struct x86_emulate_ctxt *ctxt)
1722{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001723 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001724 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001725 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001726}
1727
Avi Kivity1cd196e2011-09-13 10:45:51 +03001728static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001729{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001730 int seg = ctxt->src2.val;
1731
Avi Kivity9dac77f2011-06-01 15:34:25 +03001732 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001733
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001734 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001735}
1736
Avi Kivity1cd196e2011-09-13 10:45:51 +03001737static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001738{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001739 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001740 unsigned long selector;
1741 int rc;
1742
Avi Kivity9dac77f2011-06-01 15:34:25 +03001743 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001744 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001745 return rc;
1746
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001747 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001748 return rc;
1749}
1750
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001751static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001752{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001753 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001754 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001755 int reg = VCPU_REGS_RAX;
1756
1757 while (reg <= VCPU_REGS_RDI) {
1758 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001759 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001760
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001761 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001762 if (rc != X86EMUL_CONTINUE)
1763 return rc;
1764
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001765 ++reg;
1766 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001767
Wei Yongjunc37eda12010-06-15 09:03:33 +08001768 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001769}
1770
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001771static int em_pushf(struct x86_emulate_ctxt *ctxt)
1772{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001773 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001774 return em_push(ctxt);
1775}
1776
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001777static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001778{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001779 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001780 int reg = VCPU_REGS_RDI;
1781
1782 while (reg >= VCPU_REGS_RAX) {
1783 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001784 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001785 --reg;
1786 }
1787
Avi Kivitydd856ef2012-08-27 23:46:17 +03001788 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001789 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001790 break;
1791 --reg;
1792 }
1793 return rc;
1794}
1795
Avi Kivitydd856ef2012-08-27 23:46:17 +03001796static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001797{
Mathias Krause0225fb52012-08-30 01:30:16 +02001798 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001799 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001800 struct desc_ptr dt;
1801 gva_t cs_addr;
1802 gva_t eip_addr;
1803 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001804
1805 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001806 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001807 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001808 if (rc != X86EMUL_CONTINUE)
1809 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001810
1811 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1812
Avi Kivity9dac77f2011-06-01 15:34:25 +03001813 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001814 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001815 if (rc != X86EMUL_CONTINUE)
1816 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001817
Avi Kivity9dac77f2011-06-01 15:34:25 +03001818 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001819 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001820 if (rc != X86EMUL_CONTINUE)
1821 return rc;
1822
Avi Kivity4bff1e862011-04-20 13:37:53 +03001823 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001824
1825 eip_addr = dt.address + (irq << 2);
1826 cs_addr = dt.address + (irq << 2) + 2;
1827
Avi Kivity0f65dd72011-04-20 13:37:53 +03001828 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001829 if (rc != X86EMUL_CONTINUE)
1830 return rc;
1831
Avi Kivity0f65dd72011-04-20 13:37:53 +03001832 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001833 if (rc != X86EMUL_CONTINUE)
1834 return rc;
1835
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001836 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001837 if (rc != X86EMUL_CONTINUE)
1838 return rc;
1839
Avi Kivity9dac77f2011-06-01 15:34:25 +03001840 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001841
1842 return rc;
1843}
1844
Avi Kivitydd856ef2012-08-27 23:46:17 +03001845int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1846{
1847 int rc;
1848
1849 invalidate_registers(ctxt);
1850 rc = __emulate_int_real(ctxt, irq);
1851 if (rc == X86EMUL_CONTINUE)
1852 writeback_registers(ctxt);
1853 return rc;
1854}
1855
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001856static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001857{
1858 switch(ctxt->mode) {
1859 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001860 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001861 case X86EMUL_MODE_VM86:
1862 case X86EMUL_MODE_PROT16:
1863 case X86EMUL_MODE_PROT32:
1864 case X86EMUL_MODE_PROT64:
1865 default:
1866 /* Protected mode interrupts unimplemented yet */
1867 return X86EMUL_UNHANDLEABLE;
1868 }
1869}
1870
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001871static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001872{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001873 int rc = X86EMUL_CONTINUE;
1874 unsigned long temp_eip = 0;
1875 unsigned long temp_eflags = 0;
1876 unsigned long cs = 0;
1877 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1878 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1879 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1880 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1881
1882 /* TODO: Add stack limit check */
1883
Avi Kivity9dac77f2011-06-01 15:34:25 +03001884 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001885
1886 if (rc != X86EMUL_CONTINUE)
1887 return rc;
1888
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001889 if (temp_eip & ~0xffff)
1890 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001891
Avi Kivity9dac77f2011-06-01 15:34:25 +03001892 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001893
1894 if (rc != X86EMUL_CONTINUE)
1895 return rc;
1896
Avi Kivity9dac77f2011-06-01 15:34:25 +03001897 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001898
1899 if (rc != X86EMUL_CONTINUE)
1900 return rc;
1901
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001902 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001903
1904 if (rc != X86EMUL_CONTINUE)
1905 return rc;
1906
Avi Kivity9dac77f2011-06-01 15:34:25 +03001907 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001908
1909
Avi Kivity9dac77f2011-06-01 15:34:25 +03001910 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001911 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001912 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001913 ctxt->eflags &= ~0xffff;
1914 ctxt->eflags |= temp_eflags;
1915 }
1916
1917 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1918 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1919
1920 return rc;
1921}
1922
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001923static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001924{
1925 switch(ctxt->mode) {
1926 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001927 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001928 case X86EMUL_MODE_VM86:
1929 case X86EMUL_MODE_PROT16:
1930 case X86EMUL_MODE_PROT32:
1931 case X86EMUL_MODE_PROT64:
1932 default:
1933 /* iret from protected mode unimplemented yet */
1934 return X86EMUL_UNHANDLEABLE;
1935 }
1936}
1937
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001938static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1939{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001940 int rc;
1941 unsigned short sel;
1942
Avi Kivity9dac77f2011-06-01 15:34:25 +03001943 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001944
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001945 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001946 if (rc != X86EMUL_CONTINUE)
1947 return rc;
1948
Avi Kivity9dac77f2011-06-01 15:34:25 +03001949 ctxt->_eip = 0;
1950 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001951 return X86EMUL_CONTINUE;
1952}
1953
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001954static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001955{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001956 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001957
Avi Kivity9dac77f2011-06-01 15:34:25 +03001958 switch (ctxt->modrm_reg) {
Mohammed Gamald19292e2008-09-08 21:47:19 +03001959 case 2: /* call near abs */ {
1960 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001961 old_eip = ctxt->_eip;
1962 ctxt->_eip = ctxt->src.val;
1963 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001964 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001965 break;
1966 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001967 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001968 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001969 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001970 case 5: /* jmp far */
1971 rc = em_jmp_far(ctxt);
1972 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001973 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001974 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001975 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001976 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001977 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001978}
1979
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09001980static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001981{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001982 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001983
Avi Kivitydd856ef2012-08-27 23:46:17 +03001984 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
1985 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
1986 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
1987 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001988 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001989 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001990 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
1991 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001992
Laurent Vivier05f086f2007-09-24 11:10:55 +02001993 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001994 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001995 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001996}
1997
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001998static int em_ret(struct x86_emulate_ctxt *ctxt)
1999{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002000 ctxt->dst.type = OP_REG;
2001 ctxt->dst.addr.reg = &ctxt->_eip;
2002 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002003 return em_pop(ctxt);
2004}
2005
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002006static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002007{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002008 int rc;
2009 unsigned long cs;
2010
Avi Kivity9dac77f2011-06-01 15:34:25 +03002011 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002012 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002013 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002014 if (ctxt->op_bytes == 4)
2015 ctxt->_eip = (u32)ctxt->_eip;
2016 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002017 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002018 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002019 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002020 return rc;
2021}
2022
Bruce Rogers32611072013-09-09 09:40:20 -06002023static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2024{
2025 int rc;
2026
2027 rc = em_ret_far(ctxt);
2028 if (rc != X86EMUL_CONTINUE)
2029 return rc;
2030 rsp_increment(ctxt, ctxt->src.val);
2031 return X86EMUL_CONTINUE;
2032}
2033
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002034static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2035{
2036 /* Save real source value, then compare EAX against destination. */
2037 ctxt->src.orig_val = ctxt->src.val;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002038 ctxt->src.val = reg_read(ctxt, VCPU_REGS_RAX);
Avi Kivity158de572013-01-19 19:51:57 +02002039 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002040
2041 if (ctxt->eflags & EFLG_ZF) {
2042 /* Success: write back to memory. */
2043 ctxt->dst.val = ctxt->src.orig_val;
2044 } else {
2045 /* Failure: write the value we saw to EAX. */
2046 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002047 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002048 }
2049 return X86EMUL_CONTINUE;
2050}
2051
Avi Kivityd4b43252011-09-13 10:45:50 +03002052static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002053{
Avi Kivityd4b43252011-09-13 10:45:50 +03002054 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002055 unsigned short sel;
2056 int rc;
2057
Avi Kivity9dac77f2011-06-01 15:34:25 +03002058 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002059
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002060 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002061 if (rc != X86EMUL_CONTINUE)
2062 return rc;
2063
Avi Kivity9dac77f2011-06-01 15:34:25 +03002064 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002065 return rc;
2066}
2067
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002068static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002069setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002070 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002071{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002072 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002073 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002074 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002075 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002076 cs->type = 0x0b; /* Read, Execute, Accessed */
2077 cs->s = 1;
2078 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002079 cs->p = 1;
2080 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002081 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002082
Gleb Natapov79168fd2010-04-28 19:15:30 +03002083 set_desc_base(ss, 0); /* flat segment */
2084 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002085 ss->g = 1; /* 4kb granularity */
2086 ss->s = 1;
2087 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002088 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002089 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002090 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002091 ss->l = 0;
2092 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002093}
2094
Avi Kivity1a18a692012-02-01 12:23:21 +02002095static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2096{
2097 u32 eax, ebx, ecx, edx;
2098
2099 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002100 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2101 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002102 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2103 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2104}
2105
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002106static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2107{
Mathias Krause0225fb52012-08-30 01:30:16 +02002108 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002109 u32 eax, ebx, ecx, edx;
2110
2111 /*
2112 * syscall should always be enabled in longmode - so only become
2113 * vendor specific (cpuid) if other modes are active...
2114 */
2115 if (ctxt->mode == X86EMUL_MODE_PROT64)
2116 return true;
2117
2118 eax = 0x00000000;
2119 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002120 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2121 /*
2122 * Intel ("GenuineIntel")
2123 * remark: Intel CPUs only support "syscall" in 64bit
2124 * longmode. Also an 64bit guest with a
2125 * 32bit compat-app running will #UD !! While this
2126 * behaviour can be fixed (by emulating) into AMD
2127 * response - CPUs of AMD can't behave like Intel.
2128 */
2129 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2130 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2131 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2132 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002133
Avi Kivity0017f932012-06-07 14:10:16 +03002134 /* AMD ("AuthenticAMD") */
2135 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2136 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2137 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2138 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002139
Avi Kivity0017f932012-06-07 14:10:16 +03002140 /* AMD ("AMDisbetter!") */
2141 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2142 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2143 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2144 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002145
2146 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2147 return false;
2148}
2149
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002150static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002151{
Mathias Krause0225fb52012-08-30 01:30:16 +02002152 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002153 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002154 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002155 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002156 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002157
2158 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002159 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002160 ctxt->mode == X86EMUL_MODE_VM86)
2161 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002162
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002163 if (!(em_syscall_is_enabled(ctxt)))
2164 return emulate_ud(ctxt);
2165
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002166 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002167 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002168
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002169 if (!(efer & EFER_SCE))
2170 return emulate_ud(ctxt);
2171
Avi Kivity717746e2011-04-20 13:37:53 +03002172 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002173 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002174 cs_sel = (u16)(msr_data & 0xfffc);
2175 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002176
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002177 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002178 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002179 cs.l = 1;
2180 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002181 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2182 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002183
Avi Kivitydd856ef2012-08-27 23:46:17 +03002184 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002185 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002186#ifdef CONFIG_X86_64
Avi Kivitydd856ef2012-08-27 23:46:17 +03002187 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002188
Avi Kivity717746e2011-04-20 13:37:53 +03002189 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002190 ctxt->mode == X86EMUL_MODE_PROT64 ?
2191 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002192 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002193
Avi Kivity717746e2011-04-20 13:37:53 +03002194 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002195 ctxt->eflags &= ~(msr_data | EFLG_RF);
2196#endif
2197 } else {
2198 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002199 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002200 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002201
2202 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2203 }
2204
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002205 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002206}
2207
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002208static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002209{
Mathias Krause0225fb52012-08-30 01:30:16 +02002210 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002211 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002212 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002213 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002214 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002215
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002216 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002217 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002218 if (ctxt->mode == X86EMUL_MODE_REAL)
2219 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002220
Avi Kivity1a18a692012-02-01 12:23:21 +02002221 /*
2222 * Not recognized on AMD in compat mode (but is recognized in legacy
2223 * mode).
2224 */
2225 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2226 && !vendor_intel(ctxt))
2227 return emulate_ud(ctxt);
2228
Andre Przywara8c604352009-06-18 12:56:01 +02002229 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2230 * Therefore, we inject an #UD.
2231 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002232 if (ctxt->mode == X86EMUL_MODE_PROT64)
2233 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002234
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002235 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002236
Avi Kivity717746e2011-04-20 13:37:53 +03002237 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002238 switch (ctxt->mode) {
2239 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002240 if ((msr_data & 0xfffc) == 0x0)
2241 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002242 break;
2243 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002244 if (msr_data == 0x0)
2245 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002246 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002247 default:
2248 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002249 }
2250
2251 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002252 cs_sel = (u16)msr_data;
2253 cs_sel &= ~SELECTOR_RPL_MASK;
2254 ss_sel = cs_sel + 8;
2255 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002256 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002257 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002258 cs.l = 1;
2259 }
2260
Avi Kivity1aa36612011-04-27 13:20:30 +03002261 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2262 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002263
Avi Kivity717746e2011-04-20 13:37:53 +03002264 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002265 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002266
Avi Kivity717746e2011-04-20 13:37:53 +03002267 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002268 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002269
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002270 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002271}
2272
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002273static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +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 Przywara4668f052009-06-18 12:56:02 +02002277 u64 msr_data;
2278 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002279 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002280
Gleb Natapova0044752010-02-10 14:21:31 +02002281 /* inject #GP if in real mode or Virtual 8086 mode */
2282 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002283 ctxt->mode == X86EMUL_MODE_VM86)
2284 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002285
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002286 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002287
Avi Kivity9dac77f2011-06-01 15:34:25 +03002288 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002289 usermode = X86EMUL_MODE_PROT64;
2290 else
2291 usermode = X86EMUL_MODE_PROT32;
2292
2293 cs.dpl = 3;
2294 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002295 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002296 switch (usermode) {
2297 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002298 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002299 if ((msr_data & 0xfffc) == 0x0)
2300 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002301 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002302 break;
2303 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002304 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002305 if (msr_data == 0x0)
2306 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002307 ss_sel = cs_sel + 8;
2308 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002309 cs.l = 1;
2310 break;
2311 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002312 cs_sel |= SELECTOR_RPL_MASK;
2313 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002314
Avi Kivity1aa36612011-04-27 13:20:30 +03002315 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2316 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002317
Avi Kivitydd856ef2012-08-27 23:46:17 +03002318 ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX);
2319 *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX);
Andre Przywara4668f052009-06-18 12:56:02 +02002320
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002321 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002322}
2323
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002324static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002325{
2326 int iopl;
2327 if (ctxt->mode == X86EMUL_MODE_REAL)
2328 return false;
2329 if (ctxt->mode == X86EMUL_MODE_VM86)
2330 return true;
2331 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002332 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002333}
2334
2335static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002336 u16 port, u16 len)
2337{
Mathias Krause0225fb52012-08-30 01:30:16 +02002338 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002339 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002340 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002341 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002342 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002343 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002344 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002345
Avi Kivity1aa36612011-04-27 13:20:30 +03002346 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002347 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002348 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002349 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002350 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002351 base = get_desc_base(&tr_seg);
2352#ifdef CONFIG_X86_64
2353 base |= ((u64)base3) << 32;
2354#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002355 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002356 if (r != X86EMUL_CONTINUE)
2357 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002358 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002359 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002360 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002361 if (r != X86EMUL_CONTINUE)
2362 return false;
2363 if ((perm >> bit_idx) & mask)
2364 return false;
2365 return true;
2366}
2367
2368static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002369 u16 port, u16 len)
2370{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002371 if (ctxt->perm_ok)
2372 return true;
2373
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002374 if (emulator_bad_iopl(ctxt))
2375 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002376 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002377
2378 ctxt->perm_ok = true;
2379
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002380 return true;
2381}
2382
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002383static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002384 struct tss_segment_16 *tss)
2385{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002386 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002387 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002388 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2389 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2390 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2391 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2392 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2393 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2394 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2395 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002396
Avi Kivity1aa36612011-04-27 13:20:30 +03002397 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2398 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2399 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2400 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2401 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002402}
2403
2404static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002405 struct tss_segment_16 *tss)
2406{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002407 int ret;
2408
Avi Kivity9dac77f2011-06-01 15:34:25 +03002409 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002410 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002411 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2412 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2413 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2414 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2415 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2416 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2417 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2418 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002419
2420 /*
2421 * SDM says that segment selectors are loaded before segment
2422 * descriptors
2423 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002424 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2425 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2426 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2427 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2428 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002429
2430 /*
Guo Chaofc058682012-06-28 15:19:51 +08002431 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002432 * it is handled in a context of new task
2433 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002434 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002435 if (ret != X86EMUL_CONTINUE)
2436 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002437 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002438 if (ret != X86EMUL_CONTINUE)
2439 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002440 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002441 if (ret != X86EMUL_CONTINUE)
2442 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002443 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002444 if (ret != X86EMUL_CONTINUE)
2445 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002446 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002447 if (ret != X86EMUL_CONTINUE)
2448 return ret;
2449
2450 return X86EMUL_CONTINUE;
2451}
2452
2453static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002454 u16 tss_selector, u16 old_tss_sel,
2455 ulong old_tss_base, struct desc_struct *new_desc)
2456{
Mathias Krause0225fb52012-08-30 01:30:16 +02002457 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002458 struct tss_segment_16 tss_seg;
2459 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002460 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002461
Avi Kivity0f65dd72011-04-20 13:37:53 +03002462 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002463 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002464 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002465 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002466 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002467
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002468 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002469
Avi Kivity0f65dd72011-04-20 13:37:53 +03002470 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002471 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002472 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002473 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002474 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002475
Avi Kivity0f65dd72011-04-20 13:37:53 +03002476 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002477 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002478 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002479 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002480 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002481
2482 if (old_tss_sel != 0xffff) {
2483 tss_seg.prev_task_link = old_tss_sel;
2484
Avi Kivity0f65dd72011-04-20 13:37:53 +03002485 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002486 &tss_seg.prev_task_link,
2487 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002488 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002489 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002490 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002491 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002492 }
2493
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002494 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002495}
2496
2497static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002498 struct tss_segment_32 *tss)
2499{
Nadav Amit5c7411e2014-04-07 18:37:47 +03002500 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002501 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002502 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002503 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2504 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2505 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2506 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2507 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2508 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2509 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2510 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002511
Avi Kivity1aa36612011-04-27 13:20:30 +03002512 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2513 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2514 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2515 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2516 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2517 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002518}
2519
2520static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002521 struct tss_segment_32 *tss)
2522{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002523 int ret;
2524
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002525 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002526 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002527 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002528 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002529
2530 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002531 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2532 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2533 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2534 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2535 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2536 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2537 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2538 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002539
2540 /*
2541 * SDM says that segment selectors are loaded before segment
2542 * descriptors
2543 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002544 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2545 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2546 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2547 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2548 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2549 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2550 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002551
2552 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002553 * If we're switching between Protected Mode and VM86, we need to make
2554 * sure to update the mode before loading the segment descriptors so
2555 * that the selectors are interpreted correctly.
2556 *
2557 * Need to get rflags to the vcpu struct immediately because it
2558 * influences the CPL which is checked at least when loading the segment
2559 * descriptors and when pushing an error code to the new kernel stack.
2560 *
2561 * TODO Introduce a separate ctxt->ops->set_cpl callback
2562 */
2563 if (ctxt->eflags & X86_EFLAGS_VM)
2564 ctxt->mode = X86EMUL_MODE_VM86;
2565 else
2566 ctxt->mode = X86EMUL_MODE_PROT32;
2567
2568 ctxt->ops->set_rflags(ctxt, ctxt->eflags);
2569
2570 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002571 * Now load segment descriptors. If fault happenes at this stage
2572 * it is handled in a context of new task
2573 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002574 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002575 if (ret != X86EMUL_CONTINUE)
2576 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002577 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002578 if (ret != X86EMUL_CONTINUE)
2579 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002580 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002581 if (ret != X86EMUL_CONTINUE)
2582 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002583 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002584 if (ret != X86EMUL_CONTINUE)
2585 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002586 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002587 if (ret != X86EMUL_CONTINUE)
2588 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002589 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002590 if (ret != X86EMUL_CONTINUE)
2591 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002592 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002593 if (ret != X86EMUL_CONTINUE)
2594 return ret;
2595
2596 return X86EMUL_CONTINUE;
2597}
2598
2599static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002600 u16 tss_selector, u16 old_tss_sel,
2601 ulong old_tss_base, struct desc_struct *new_desc)
2602{
Mathias Krause0225fb52012-08-30 01:30:16 +02002603 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002604 struct tss_segment_32 tss_seg;
2605 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002606 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03002607 u32 eip_offset = offsetof(struct tss_segment_32, eip);
2608 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002609
Avi Kivity0f65dd72011-04-20 13:37:53 +03002610 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002611 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002612 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002613 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002614 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002615
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002616 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002617
Nadav Amit5c7411e2014-04-07 18:37:47 +03002618 /* Only GP registers and segment selectors are saved */
2619 ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
2620 ldt_sel_offset - eip_offset, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002621 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002622 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002623 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002624
Avi Kivity0f65dd72011-04-20 13:37:53 +03002625 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002626 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002627 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002628 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002629 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002630
2631 if (old_tss_sel != 0xffff) {
2632 tss_seg.prev_task_link = old_tss_sel;
2633
Avi Kivity0f65dd72011-04-20 13:37:53 +03002634 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002635 &tss_seg.prev_task_link,
2636 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002637 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002638 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002639 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002640 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002641 }
2642
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002643 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002644}
2645
2646static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002647 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002648 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002649{
Mathias Krause0225fb52012-08-30 01:30:16 +02002650 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002651 struct desc_struct curr_tss_desc, next_tss_desc;
2652 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002653 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002654 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002655 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002656 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002657 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002658
2659 /* FIXME: old_tss_base == ~0 ? */
2660
Avi Kivitye9194642012-06-13 16:29:39 +03002661 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002662 if (ret != X86EMUL_CONTINUE)
2663 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002664 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002665 if (ret != X86EMUL_CONTINUE)
2666 return ret;
2667
2668 /* FIXME: check that next_tss_desc is tss */
2669
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002670 /*
2671 * Check privileges. The three cases are task switch caused by...
2672 *
2673 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2674 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002675 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002676 */
2677 if (reason == TASK_SWITCH_GATE) {
2678 if (idt_index != -1) {
2679 /* Software interrupts */
2680 struct desc_struct task_gate_desc;
2681 int dpl;
2682
2683 ret = read_interrupt_descriptor(ctxt, idt_index,
2684 &task_gate_desc);
2685 if (ret != X86EMUL_CONTINUE)
2686 return ret;
2687
2688 dpl = task_gate_desc.dpl;
2689 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2690 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2691 }
2692 } else if (reason != TASK_SWITCH_IRET) {
2693 int dpl = next_tss_desc.dpl;
2694 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2695 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002696 }
2697
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002698
Gleb Natapovceffb452010-03-18 15:20:19 +02002699 desc_limit = desc_limit_scaled(&next_tss_desc);
2700 if (!next_tss_desc.p ||
2701 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2702 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002703 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002704 return X86EMUL_PROPAGATE_FAULT;
2705 }
2706
2707 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2708 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002709 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002710 }
2711
2712 if (reason == TASK_SWITCH_IRET)
2713 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2714
2715 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002716 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002717 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2718 old_tss_sel = 0xffff;
2719
2720 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002721 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002722 old_tss_base, &next_tss_desc);
2723 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002724 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002725 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002726 if (ret != X86EMUL_CONTINUE)
2727 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002728
2729 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2730 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2731
2732 if (reason != TASK_SWITCH_IRET) {
2733 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002734 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002735 }
2736
Avi Kivity717746e2011-04-20 13:37:53 +03002737 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002738 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002739
Jan Kiszkae269fb22010-04-14 15:51:09 +02002740 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002741 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2742 ctxt->lock_prefix = 0;
2743 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002744 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002745 }
2746
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002747 return ret;
2748}
2749
2750int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002751 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002752 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002753{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002754 int rc;
2755
Avi Kivitydd856ef2012-08-27 23:46:17 +03002756 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002757 ctxt->_eip = ctxt->eip;
2758 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002759
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002760 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002761 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002762
Avi Kivitydd856ef2012-08-27 23:46:17 +03002763 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002764 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002765 writeback_registers(ctxt);
2766 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002767
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002768 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002769}
2770
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002771static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2772 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002773{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002774 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002775
Avi Kivitydd856ef2012-08-27 23:46:17 +03002776 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2777 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002778}
2779
Avi Kivity7af04fc2010-08-18 14:16:35 +03002780static int em_das(struct x86_emulate_ctxt *ctxt)
2781{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002782 u8 al, old_al;
2783 bool af, cf, old_cf;
2784
2785 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002786 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002787
2788 old_al = al;
2789 old_cf = cf;
2790 cf = false;
2791 af = ctxt->eflags & X86_EFLAGS_AF;
2792 if ((al & 0x0f) > 9 || af) {
2793 al -= 6;
2794 cf = old_cf | (al >= 250);
2795 af = true;
2796 } else {
2797 af = false;
2798 }
2799 if (old_al > 0x99 || old_cf) {
2800 al -= 0x60;
2801 cf = true;
2802 }
2803
Avi Kivity9dac77f2011-06-01 15:34:25 +03002804 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002805 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002806 ctxt->src.type = OP_IMM;
2807 ctxt->src.val = 0;
2808 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02002809 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03002810 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2811 if (cf)
2812 ctxt->eflags |= X86_EFLAGS_CF;
2813 if (af)
2814 ctxt->eflags |= X86_EFLAGS_AF;
2815 return X86EMUL_CONTINUE;
2816}
2817
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02002818static int em_aam(struct x86_emulate_ctxt *ctxt)
2819{
2820 u8 al, ah;
2821
2822 if (ctxt->src.val == 0)
2823 return emulate_de(ctxt);
2824
2825 al = ctxt->dst.val & 0xff;
2826 ah = al / ctxt->src.val;
2827 al %= ctxt->src.val;
2828
2829 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
2830
2831 /* Set PF, ZF, SF */
2832 ctxt->src.type = OP_IMM;
2833 ctxt->src.val = 0;
2834 ctxt->src.bytes = 1;
2835 fastop(ctxt, em_or);
2836
2837 return X86EMUL_CONTINUE;
2838}
2839
Gleb Natapov7f662272012-12-10 11:42:30 +02002840static int em_aad(struct x86_emulate_ctxt *ctxt)
2841{
2842 u8 al = ctxt->dst.val & 0xff;
2843 u8 ah = (ctxt->dst.val >> 8) & 0xff;
2844
2845 al = (al + (ah * ctxt->src.val)) & 0xff;
2846
2847 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
2848
Gleb Natapovf583c292013-02-13 17:50:39 +02002849 /* Set PF, ZF, SF */
2850 ctxt->src.type = OP_IMM;
2851 ctxt->src.val = 0;
2852 ctxt->src.bytes = 1;
2853 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02002854
2855 return X86EMUL_CONTINUE;
2856}
2857
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002858static int em_call(struct x86_emulate_ctxt *ctxt)
2859{
2860 long rel = ctxt->src.val;
2861
2862 ctxt->src.val = (unsigned long)ctxt->_eip;
2863 jmp_rel(ctxt, rel);
2864 return em_push(ctxt);
2865}
2866
Avi Kivity0ef753b2010-08-18 14:51:45 +03002867static int em_call_far(struct x86_emulate_ctxt *ctxt)
2868{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002869 u16 sel, old_cs;
2870 ulong old_eip;
2871 int rc;
2872
Avi Kivity1aa36612011-04-27 13:20:30 +03002873 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002874 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002875
Avi Kivity9dac77f2011-06-01 15:34:25 +03002876 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002877 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002878 return X86EMUL_CONTINUE;
2879
Avi Kivity9dac77f2011-06-01 15:34:25 +03002880 ctxt->_eip = 0;
2881 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002882
Avi Kivity9dac77f2011-06-01 15:34:25 +03002883 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002884 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002885 if (rc != X86EMUL_CONTINUE)
2886 return rc;
2887
Avi Kivity9dac77f2011-06-01 15:34:25 +03002888 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002889 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002890}
2891
Avi Kivity40ece7c2010-08-18 15:12:09 +03002892static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2893{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002894 int rc;
2895
Avi Kivity9dac77f2011-06-01 15:34:25 +03002896 ctxt->dst.type = OP_REG;
2897 ctxt->dst.addr.reg = &ctxt->_eip;
2898 ctxt->dst.bytes = ctxt->op_bytes;
2899 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002900 if (rc != X86EMUL_CONTINUE)
2901 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03002902 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002903 return X86EMUL_CONTINUE;
2904}
2905
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002906static int em_xchg(struct x86_emulate_ctxt *ctxt)
2907{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002908 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002909 ctxt->src.val = ctxt->dst.val;
2910 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002911
2912 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002913 ctxt->dst.val = ctxt->src.orig_val;
2914 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002915 return X86EMUL_CONTINUE;
2916}
2917
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002918static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2919{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002920 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02002921 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002922}
2923
Avi Kivity61429142010-08-19 15:13:00 +03002924static int em_cwd(struct x86_emulate_ctxt *ctxt)
2925{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002926 ctxt->dst.type = OP_REG;
2927 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002928 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002929 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002930
2931 return X86EMUL_CONTINUE;
2932}
2933
Avi Kivity48bb5d32010-08-18 18:54:34 +03002934static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2935{
Avi Kivity48bb5d32010-08-18 18:54:34 +03002936 u64 tsc = 0;
2937
Avi Kivity717746e2011-04-20 13:37:53 +03002938 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002939 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
2940 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03002941 return X86EMUL_CONTINUE;
2942}
2943
Avi Kivity222d21a2011-11-10 14:57:30 +02002944static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
2945{
2946 u64 pmc;
2947
Avi Kivitydd856ef2012-08-27 23:46:17 +03002948 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02002949 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002950 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
2951 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02002952 return X86EMUL_CONTINUE;
2953}
2954
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002955static int em_mov(struct x86_emulate_ctxt *ctxt)
2956{
Stefan Hajnoczi49597d82012-04-09 18:40:00 +03002957 memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002958 return X86EMUL_CONTINUE;
2959}
2960
Borislav Petkov84cffe42013-10-29 12:54:56 +01002961#define FFL(x) bit(X86_FEATURE_##x)
2962
2963static int em_movbe(struct x86_emulate_ctxt *ctxt)
2964{
2965 u32 ebx, ecx, edx, eax = 1;
2966 u16 tmp;
2967
2968 /*
2969 * Check MOVBE is set in the guest-visible CPUID leaf.
2970 */
2971 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2972 if (!(ecx & FFL(MOVBE)))
2973 return emulate_ud(ctxt);
2974
2975 switch (ctxt->op_bytes) {
2976 case 2:
2977 /*
2978 * From MOVBE definition: "...When the operand size is 16 bits,
2979 * the upper word of the destination register remains unchanged
2980 * ..."
2981 *
2982 * Both casting ->valptr and ->val to u16 breaks strict aliasing
2983 * rules so we have to do the operation almost per hand.
2984 */
2985 tmp = (u16)ctxt->src.val;
2986 ctxt->dst.val &= ~0xffffUL;
2987 ctxt->dst.val |= (unsigned long)swab16(tmp);
2988 break;
2989 case 4:
2990 ctxt->dst.val = swab32((u32)ctxt->src.val);
2991 break;
2992 case 8:
2993 ctxt->dst.val = swab64(ctxt->src.val);
2994 break;
2995 default:
2996 return X86EMUL_PROPAGATE_FAULT;
2997 }
2998 return X86EMUL_CONTINUE;
2999}
3000
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003001static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3002{
3003 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3004 return emulate_gp(ctxt, 0);
3005
3006 /* Disable writeback. */
3007 ctxt->dst.type = OP_NONE;
3008 return X86EMUL_CONTINUE;
3009}
3010
3011static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3012{
3013 unsigned long val;
3014
3015 if (ctxt->mode == X86EMUL_MODE_PROT64)
3016 val = ctxt->src.val & ~0ULL;
3017 else
3018 val = ctxt->src.val & ~0U;
3019
3020 /* #UD condition is already handled. */
3021 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3022 return emulate_gp(ctxt, 0);
3023
3024 /* Disable writeback. */
3025 ctxt->dst.type = OP_NONE;
3026 return X86EMUL_CONTINUE;
3027}
3028
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003029static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3030{
3031 u64 msr_data;
3032
Avi Kivitydd856ef2012-08-27 23:46:17 +03003033 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3034 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3035 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003036 return emulate_gp(ctxt, 0);
3037
3038 return X86EMUL_CONTINUE;
3039}
3040
3041static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3042{
3043 u64 msr_data;
3044
Avi Kivitydd856ef2012-08-27 23:46:17 +03003045 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003046 return emulate_gp(ctxt, 0);
3047
Avi Kivitydd856ef2012-08-27 23:46:17 +03003048 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3049 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003050 return X86EMUL_CONTINUE;
3051}
3052
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003053static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3054{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003055 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003056 return emulate_ud(ctxt);
3057
Avi Kivity9dac77f2011-06-01 15:34:25 +03003058 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003059 return X86EMUL_CONTINUE;
3060}
3061
3062static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3063{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003064 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003065
Avi Kivity9dac77f2011-06-01 15:34:25 +03003066 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003067 return emulate_ud(ctxt);
3068
Avi Kivity9dac77f2011-06-01 15:34:25 +03003069 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003070 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3071
3072 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003073 ctxt->dst.type = OP_NONE;
3074 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003075}
3076
Avi Kivitya14e5792012-06-13 12:28:33 +03003077static int em_lldt(struct x86_emulate_ctxt *ctxt)
3078{
3079 u16 sel = ctxt->src.val;
3080
3081 /* Disable writeback. */
3082 ctxt->dst.type = OP_NONE;
3083 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3084}
3085
Avi Kivity80890002012-06-13 16:33:29 +03003086static int em_ltr(struct x86_emulate_ctxt *ctxt)
3087{
3088 u16 sel = ctxt->src.val;
3089
3090 /* Disable writeback. */
3091 ctxt->dst.type = OP_NONE;
3092 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3093}
3094
Avi Kivity38503912011-03-31 18:48:09 +02003095static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3096{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003097 int rc;
3098 ulong linear;
3099
Avi Kivity9dac77f2011-06-01 15:34:25 +03003100 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003101 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003102 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003103 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003104 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003105 return X86EMUL_CONTINUE;
3106}
3107
Avi Kivity2d04a052011-04-20 15:32:49 +03003108static int em_clts(struct x86_emulate_ctxt *ctxt)
3109{
3110 ulong cr0;
3111
3112 cr0 = ctxt->ops->get_cr(ctxt, 0);
3113 cr0 &= ~X86_CR0_TS;
3114 ctxt->ops->set_cr(ctxt, 0, cr0);
3115 return X86EMUL_CONTINUE;
3116}
3117
Avi Kivity26d05cc2011-04-21 12:07:59 +03003118static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3119{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003120 int rc;
3121
Avi Kivity9dac77f2011-06-01 15:34:25 +03003122 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003123 return X86EMUL_UNHANDLEABLE;
3124
3125 rc = ctxt->ops->fix_hypercall(ctxt);
3126 if (rc != X86EMUL_CONTINUE)
3127 return rc;
3128
3129 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003130 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003131 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003132 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003133 return X86EMUL_CONTINUE;
3134}
3135
Avi Kivity96051572012-06-10 17:21:18 +03003136static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3137 void (*get)(struct x86_emulate_ctxt *ctxt,
3138 struct desc_ptr *ptr))
3139{
3140 struct desc_ptr desc_ptr;
3141
3142 if (ctxt->mode == X86EMUL_MODE_PROT64)
3143 ctxt->op_bytes = 8;
3144 get(ctxt, &desc_ptr);
3145 if (ctxt->op_bytes == 2) {
3146 ctxt->op_bytes = 4;
3147 desc_ptr.address &= 0x00ffffff;
3148 }
3149 /* Disable writeback. */
3150 ctxt->dst.type = OP_NONE;
3151 return segmented_write(ctxt, ctxt->dst.addr.mem,
3152 &desc_ptr, 2 + ctxt->op_bytes);
3153}
3154
3155static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3156{
3157 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3158}
3159
3160static int em_sidt(struct x86_emulate_ctxt *ctxt)
3161{
3162 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3163}
3164
Avi Kivity26d05cc2011-04-21 12:07:59 +03003165static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3166{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003167 struct desc_ptr desc_ptr;
3168 int rc;
3169
Avi Kivity510425f2012-06-07 17:04:36 +03003170 if (ctxt->mode == X86EMUL_MODE_PROT64)
3171 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003172 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003173 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003174 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003175 if (rc != X86EMUL_CONTINUE)
3176 return rc;
3177 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3178 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003179 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003180 return X86EMUL_CONTINUE;
3181}
3182
Avi Kivity5ef39c72011-04-21 12:21:50 +03003183static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003184{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003185 int rc;
3186
Avi Kivity5ef39c72011-04-21 12:21:50 +03003187 rc = ctxt->ops->fix_hypercall(ctxt);
3188
Avi Kivity26d05cc2011-04-21 12:07:59 +03003189 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003190 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003191 return rc;
3192}
3193
3194static int em_lidt(struct x86_emulate_ctxt *ctxt)
3195{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003196 struct desc_ptr desc_ptr;
3197 int rc;
3198
Avi Kivity510425f2012-06-07 17:04:36 +03003199 if (ctxt->mode == X86EMUL_MODE_PROT64)
3200 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003201 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003202 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003203 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003204 if (rc != X86EMUL_CONTINUE)
3205 return rc;
3206 ctxt->ops->set_idt(ctxt, &desc_ptr);
3207 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003208 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003209 return X86EMUL_CONTINUE;
3210}
3211
3212static int em_smsw(struct x86_emulate_ctxt *ctxt)
3213{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003214 ctxt->dst.bytes = 2;
3215 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003216 return X86EMUL_CONTINUE;
3217}
3218
3219static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3220{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003221 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003222 | (ctxt->src.val & 0x0f));
3223 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003224 return X86EMUL_CONTINUE;
3225}
3226
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003227static int em_loop(struct x86_emulate_ctxt *ctxt)
3228{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003229 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3230 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003231 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3232 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003233
3234 return X86EMUL_CONTINUE;
3235}
3236
3237static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3238{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003239 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003240 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003241
3242 return X86EMUL_CONTINUE;
3243}
3244
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003245static int em_in(struct x86_emulate_ctxt *ctxt)
3246{
3247 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3248 &ctxt->dst.val))
3249 return X86EMUL_IO_NEEDED;
3250
3251 return X86EMUL_CONTINUE;
3252}
3253
3254static int em_out(struct x86_emulate_ctxt *ctxt)
3255{
3256 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3257 &ctxt->src.val, 1);
3258 /* Disable writeback. */
3259 ctxt->dst.type = OP_NONE;
3260 return X86EMUL_CONTINUE;
3261}
3262
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003263static int em_cli(struct x86_emulate_ctxt *ctxt)
3264{
3265 if (emulator_bad_iopl(ctxt))
3266 return emulate_gp(ctxt, 0);
3267
3268 ctxt->eflags &= ~X86_EFLAGS_IF;
3269 return X86EMUL_CONTINUE;
3270}
3271
3272static int em_sti(struct x86_emulate_ctxt *ctxt)
3273{
3274 if (emulator_bad_iopl(ctxt))
3275 return emulate_gp(ctxt, 0);
3276
3277 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3278 ctxt->eflags |= X86_EFLAGS_IF;
3279 return X86EMUL_CONTINUE;
3280}
3281
Avi Kivity6d6eede2012-06-07 14:11:36 +03003282static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3283{
3284 u32 eax, ebx, ecx, edx;
3285
Avi Kivitydd856ef2012-08-27 23:46:17 +03003286 eax = reg_read(ctxt, VCPU_REGS_RAX);
3287 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003288 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003289 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3290 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3291 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3292 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003293 return X86EMUL_CONTINUE;
3294}
3295
Paolo Bonzini98f73632013-10-31 11:19:42 +01003296static int em_sahf(struct x86_emulate_ctxt *ctxt)
3297{
3298 u32 flags;
3299
3300 flags = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF;
3301 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3302
3303 ctxt->eflags &= ~0xffUL;
3304 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3305 return X86EMUL_CONTINUE;
3306}
3307
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003308static int em_lahf(struct x86_emulate_ctxt *ctxt)
3309{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003310 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3311 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003312 return X86EMUL_CONTINUE;
3313}
3314
Avi Kivity92998362012-06-13 12:25:06 +03003315static int em_bswap(struct x86_emulate_ctxt *ctxt)
3316{
3317 switch (ctxt->op_bytes) {
3318#ifdef CONFIG_X86_64
3319 case 8:
3320 asm("bswap %0" : "+r"(ctxt->dst.val));
3321 break;
3322#endif
3323 default:
3324 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3325 break;
3326 }
3327 return X86EMUL_CONTINUE;
3328}
3329
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003330static bool valid_cr(int nr)
3331{
3332 switch (nr) {
3333 case 0:
3334 case 2 ... 4:
3335 case 8:
3336 return true;
3337 default:
3338 return false;
3339 }
3340}
3341
3342static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3343{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003344 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003345 return emulate_ud(ctxt);
3346
3347 return X86EMUL_CONTINUE;
3348}
3349
3350static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3351{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003352 u64 new_val = ctxt->src.val64;
3353 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003354 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003355
3356 static u64 cr_reserved_bits[] = {
3357 0xffffffff00000000ULL,
3358 0, 0, 0, /* CR3 checked later */
3359 CR4_RESERVED_BITS,
3360 0, 0, 0,
3361 CR8_RESERVED_BITS,
3362 };
3363
3364 if (!valid_cr(cr))
3365 return emulate_ud(ctxt);
3366
3367 if (new_val & cr_reserved_bits[cr])
3368 return emulate_gp(ctxt, 0);
3369
3370 switch (cr) {
3371 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003372 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003373 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3374 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3375 return emulate_gp(ctxt, 0);
3376
Avi Kivity717746e2011-04-20 13:37:53 +03003377 cr4 = ctxt->ops->get_cr(ctxt, 4);
3378 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003379
3380 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3381 !(cr4 & X86_CR4_PAE))
3382 return emulate_gp(ctxt, 0);
3383
3384 break;
3385 }
3386 case 3: {
3387 u64 rsvd = 0;
3388
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003389 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3390 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003391 rsvd = CR3_L_MODE_RESERVED_BITS;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003392
3393 if (new_val & rsvd)
3394 return emulate_gp(ctxt, 0);
3395
3396 break;
3397 }
3398 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003399 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003400
3401 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3402 return emulate_gp(ctxt, 0);
3403
3404 break;
3405 }
3406 }
3407
3408 return X86EMUL_CONTINUE;
3409}
3410
Joerg Roedel3b88e412011-04-04 12:39:29 +02003411static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3412{
3413 unsigned long dr7;
3414
Avi Kivity717746e2011-04-20 13:37:53 +03003415 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003416
3417 /* Check if DR7.Global_Enable is set */
3418 return dr7 & (1 << 13);
3419}
3420
3421static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3422{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003423 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003424 u64 cr4;
3425
3426 if (dr > 7)
3427 return emulate_ud(ctxt);
3428
Avi Kivity717746e2011-04-20 13:37:53 +03003429 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003430 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3431 return emulate_ud(ctxt);
3432
3433 if (check_dr7_gd(ctxt))
3434 return emulate_db(ctxt);
3435
3436 return X86EMUL_CONTINUE;
3437}
3438
3439static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3440{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003441 u64 new_val = ctxt->src.val64;
3442 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003443
3444 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3445 return emulate_gp(ctxt, 0);
3446
3447 return check_dr_read(ctxt);
3448}
3449
Joerg Roedel01de8b02011-04-04 12:39:31 +02003450static int check_svme(struct x86_emulate_ctxt *ctxt)
3451{
3452 u64 efer;
3453
Avi Kivity717746e2011-04-20 13:37:53 +03003454 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003455
3456 if (!(efer & EFER_SVME))
3457 return emulate_ud(ctxt);
3458
3459 return X86EMUL_CONTINUE;
3460}
3461
3462static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3463{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003464 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003465
3466 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003467 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003468 return emulate_gp(ctxt, 0);
3469
3470 return check_svme(ctxt);
3471}
3472
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003473static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3474{
Avi Kivity717746e2011-04-20 13:37:53 +03003475 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003476
Avi Kivity717746e2011-04-20 13:37:53 +03003477 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003478 return emulate_ud(ctxt);
3479
3480 return X86EMUL_CONTINUE;
3481}
3482
Joerg Roedel80612522011-04-04 12:39:33 +02003483static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3484{
Avi Kivity717746e2011-04-20 13:37:53 +03003485 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003486 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003487
Avi Kivity717746e2011-04-20 13:37:53 +03003488 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02003489 (rcx > 3))
3490 return emulate_gp(ctxt, 0);
3491
3492 return X86EMUL_CONTINUE;
3493}
3494
Joerg Roedelf6511932011-04-04 12:39:35 +02003495static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3496{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003497 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3498 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003499 return emulate_gp(ctxt, 0);
3500
3501 return X86EMUL_CONTINUE;
3502}
3503
3504static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3505{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003506 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3507 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003508 return emulate_gp(ctxt, 0);
3509
3510 return X86EMUL_CONTINUE;
3511}
3512
Avi Kivity73fba5f2010-07-29 15:11:53 +03003513#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003514#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003515#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3516 .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003517#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003518#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003519#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3520#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003521#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003522#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003523#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003524#define II(_f, _e, _i) \
3525 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003526#define IIP(_f, _e, _i, _p) \
3527 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3528 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003529#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003530
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003531#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003532#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003533#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003534#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003535#define I2bvIP(_f, _e, _i, _p) \
3536 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003537
Avi Kivityfb864fb2013-01-04 16:18:54 +02003538#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3539 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3540 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003541
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003542static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003543 DI(SrcNone | Priv, monitor),
3544 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003545 N, N, N, N, N, N,
3546};
3547
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003548static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003549 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003550 II(SrcNone | Prot | EmulateOnUD, em_vmmcall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003551 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3552 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3553 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3554 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3555 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3556 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003557};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003558
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003559static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003560 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003561 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003562 N, N, N, N, N, N,
3563};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003564
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003565static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003566 F(Lock, em_add),
3567 F(Lock | PageTable, em_or),
3568 F(Lock, em_adc),
3569 F(Lock, em_sbb),
3570 F(Lock | PageTable, em_and),
3571 F(Lock, em_sub),
3572 F(Lock, em_xor),
3573 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003574};
3575
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003576static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003577 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003578};
3579
Avi Kivity007a3b52013-01-19 19:51:51 +02003580static const struct opcode group2[] = {
3581 F(DstMem | ModRM, em_rol),
3582 F(DstMem | ModRM, em_ror),
3583 F(DstMem | ModRM, em_rcl),
3584 F(DstMem | ModRM, em_rcr),
3585 F(DstMem | ModRM, em_shl),
3586 F(DstMem | ModRM, em_shr),
3587 F(DstMem | ModRM, em_shl),
3588 F(DstMem | ModRM, em_sar),
3589};
3590
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003591static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003592 F(DstMem | SrcImm | NoWrite, em_test),
3593 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003594 F(DstMem | SrcNone | Lock, em_not),
3595 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02003596 F(DstXacc | Src2Mem, em_mul_ex),
3597 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02003598 F(DstXacc | Src2Mem, em_div_ex),
3599 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003600};
3601
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003602static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003603 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3604 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003605 N, N, N, N, N, N,
3606};
3607
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003608static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003609 F(DstMem | SrcNone | Lock, em_inc),
3610 F(DstMem | SrcNone | Lock, em_dec),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003611 I(SrcMem | Stack, em_grp45),
3612 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3613 I(SrcMem | Stack, em_grp45),
3614 I(SrcMemFAddr | ImplicitOps, em_grp45),
Gleb Natapov188424b2013-04-11 12:32:14 +03003615 I(SrcMem | Stack, em_grp45), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003616};
3617
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003618static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003619 DI(Prot, sldt),
3620 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003621 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003622 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003623 N, N, N, N,
3624};
3625
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003626static const struct group_dual group7 = { {
Avi Kivity96051572012-06-10 17:21:18 +03003627 II(Mov | DstMem | Priv, em_sgdt, sgdt),
3628 II(Mov | DstMem | Priv, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003629 II(SrcMem | Priv, em_lgdt, lgdt),
3630 II(SrcMem | Priv, em_lidt, lidt),
3631 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3632 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3633 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003634}, {
Borislav Petkovb51e9742013-09-22 16:44:52 +02003635 I(SrcNone | Priv | EmulateOnUD, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003636 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003637 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003638 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3639 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3640 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003641} };
3642
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003643static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003644 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003645 F(DstMem | SrcImmByte | NoWrite, em_bt),
3646 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3647 F(DstMem | SrcImmByte | Lock, em_btr),
3648 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003649};
3650
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003651static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003652 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003653}, {
3654 N, N, N, N, N, N, N, N,
3655} };
3656
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003657static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003658 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003659 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003660};
3661
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003662static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003663 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003664};
3665
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003666static const struct gprefix pfx_vmovntpx = {
Avi Kivity3e114eb2012-04-09 18:40:01 +03003667 I(0, em_mov), N, N, N,
3668};
3669
Igor Mammedov27ce8252014-03-15 21:01:59 +01003670static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01003671 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003672};
3673
Gleb Natapov045a2822012-12-20 16:57:43 +02003674static const struct escape escape_d9 = { {
3675 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3676}, {
3677 /* 0xC0 - 0xC7 */
3678 N, N, N, N, N, N, N, N,
3679 /* 0xC8 - 0xCF */
3680 N, N, N, N, N, N, N, N,
3681 /* 0xD0 - 0xC7 */
3682 N, N, N, N, N, N, N, N,
3683 /* 0xD8 - 0xDF */
3684 N, N, N, N, N, N, N, N,
3685 /* 0xE0 - 0xE7 */
3686 N, N, N, N, N, N, N, N,
3687 /* 0xE8 - 0xEF */
3688 N, N, N, N, N, N, N, N,
3689 /* 0xF0 - 0xF7 */
3690 N, N, N, N, N, N, N, N,
3691 /* 0xF8 - 0xFF */
3692 N, N, N, N, N, N, N, N,
3693} };
3694
3695static const struct escape escape_db = { {
3696 N, N, N, N, N, N, N, N,
3697}, {
3698 /* 0xC0 - 0xC7 */
3699 N, N, N, N, N, N, N, N,
3700 /* 0xC8 - 0xCF */
3701 N, N, N, N, N, N, N, N,
3702 /* 0xD0 - 0xC7 */
3703 N, N, N, N, N, N, N, N,
3704 /* 0xD8 - 0xDF */
3705 N, N, N, N, N, N, N, N,
3706 /* 0xE0 - 0xE7 */
3707 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3708 /* 0xE8 - 0xEF */
3709 N, N, N, N, N, N, N, N,
3710 /* 0xF0 - 0xF7 */
3711 N, N, N, N, N, N, N, N,
3712 /* 0xF8 - 0xFF */
3713 N, N, N, N, N, N, N, N,
3714} };
3715
3716static const struct escape escape_dd = { {
3717 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3718}, {
3719 /* 0xC0 - 0xC7 */
3720 N, N, N, N, N, N, N, N,
3721 /* 0xC8 - 0xCF */
3722 N, N, N, N, N, N, N, N,
3723 /* 0xD0 - 0xC7 */
3724 N, N, N, N, N, N, N, N,
3725 /* 0xD8 - 0xDF */
3726 N, N, N, N, N, N, N, N,
3727 /* 0xE0 - 0xE7 */
3728 N, N, N, N, N, N, N, N,
3729 /* 0xE8 - 0xEF */
3730 N, N, N, N, N, N, N, N,
3731 /* 0xF0 - 0xF7 */
3732 N, N, N, N, N, N, N, N,
3733 /* 0xF8 - 0xFF */
3734 N, N, N, N, N, N, N, N,
3735} };
3736
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003737static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003738 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003739 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003740 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3741 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003742 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003743 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003744 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3745 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003746 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003747 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003748 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3749 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003750 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003751 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003752 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3753 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003754 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003755 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003756 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003757 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003758 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003759 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003760 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003761 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003762 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02003763 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003764 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003765 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003766 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003767 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003768 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003769 I(ImplicitOps | Stack | No64, em_pusha),
3770 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003771 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3772 N, N, N, N,
3773 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003774 I(SrcImm | Mov | Stack, em_push),
3775 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003776 I(SrcImmByte | Mov | Stack, em_push),
3777 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003778 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003779 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003780 /* 0x70 - 0x7F */
3781 X16(D(SrcImmByte)),
3782 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003783 G(ByteOp | DstMem | SrcImm, group1),
3784 G(DstMem | SrcImm, group1),
3785 G(ByteOp | DstMem | SrcImm | No64, group1),
3786 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003787 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003788 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003789 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003790 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003791 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003792 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003793 D(ModRM | SrcMem | NoAccess | DstReg),
3794 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3795 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003796 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003797 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003798 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003799 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003800 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003801 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01003802 II(ImplicitOps | Stack, em_popf, popf),
3803 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003804 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003805 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003806 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003807 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003808 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003809 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003810 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003811 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3812 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003813 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003814 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003815 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003816 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02003817 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003818 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003819 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003820 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003821 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003822 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3823 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003824 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003825 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003826 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Bruce Rogers32611072013-09-09 09:40:20 -06003827 I(ImplicitOps | Stack | SrcImmU16, em_ret_far_imm),
3828 I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003829 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003830 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003831 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003832 G(Src2One | ByteOp, group2), G(Src2One, group2),
3833 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003834 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02003835 I(DstAcc | SrcImmUByte | No64, em_aad),
3836 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02003837 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003838 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02003839 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003840 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003841 X3(I(SrcImmByte, em_loop)),
3842 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003843 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3844 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003845 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003846 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003847 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003848 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3849 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003850 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003851 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003852 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3853 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003854 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003855 D(ImplicitOps), D(ImplicitOps),
3856 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003857 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3858};
3859
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003860static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003861 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003862 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02003863 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003864 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003865 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003866 N, D(ImplicitOps | ModRM), N, N,
3867 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02003868 N, N, N, N, N, N, N, N,
3869 D(ImplicitOps | ModRM), N, N, N, N, N, N, D(ImplicitOps | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003870 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003871 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003872 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003873 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
3874 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003875 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003876 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
3877 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
3878 N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
Avi Kivity3e114eb2012-04-09 18:40:01 +03003879 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003880 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003881 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02003882 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003883 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02003884 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003885 I(ImplicitOps | EmulateOnUD, em_sysenter),
3886 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003887 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003888 N, N, N, N, N, N, N, N,
3889 /* 0x40 - 0x4F */
3890 X16(D(DstReg | SrcMem | ModRM | Mov)),
3891 /* 0x50 - 0x5F */
3892 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3893 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003894 N, N, N, N,
3895 N, N, N, N,
3896 N, N, N, N,
3897 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003898 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003899 N, N, N, N,
3900 N, N, N, N,
3901 N, N, N, N,
3902 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003903 /* 0x80 - 0x8F */
3904 X16(D(SrcImm)),
3905 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003906 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003907 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003908 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02003909 II(ImplicitOps, em_cpuid, cpuid),
3910 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02003911 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
3912 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003913 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003914 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003915 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02003916 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02003917 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
3918 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Avi Kivity4d758342013-01-19 19:51:55 +02003919 D(ModRM), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003920 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09003921 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03003922 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02003923 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03003924 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
3925 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003926 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003927 /* 0xB8 - 0xBF */
3928 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003929 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02003930 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
3931 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003932 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03003933 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02003934 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003935 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003936 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03003937 /* 0xC8 - 0xCF */
3938 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003939 /* 0xD0 - 0xDF */
3940 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3941 /* 0xE0 - 0xEF */
3942 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3943 /* 0xF0 - 0xFF */
3944 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3945};
3946
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003947static const struct gprefix three_byte_0f_38_f0 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01003948 I(DstReg | SrcMem | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003949};
3950
3951static const struct gprefix three_byte_0f_38_f1 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01003952 I(DstMem | SrcReg | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003953};
3954
3955/*
3956 * Insns below are selected by the prefix which indexed by the third opcode
3957 * byte.
3958 */
3959static const struct opcode opcode_map_0f_38[256] = {
3960 /* 0x00 - 0x7f */
3961 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01003962 /* 0x80 - 0xef */
3963 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
3964 /* 0xf0 - 0xf1 */
3965 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f0),
3966 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f1),
3967 /* 0xf2 - 0xff */
3968 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003969};
3970
Avi Kivity73fba5f2010-07-29 15:11:53 +03003971#undef D
3972#undef N
3973#undef G
3974#undef GD
3975#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003976#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003977#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03003978
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003979#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02003980#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003981#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003982#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003983#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003984
Avi Kivity9dac77f2011-06-01 15:34:25 +03003985static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03003986{
3987 unsigned size;
3988
Avi Kivity9dac77f2011-06-01 15:34:25 +03003989 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003990 if (size == 8)
3991 size = 4;
3992 return size;
3993}
3994
3995static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3996 unsigned size, bool sign_extension)
3997{
Avi Kivity39f21ee2010-08-18 19:20:21 +03003998 int rc = X86EMUL_CONTINUE;
3999
4000 op->type = OP_IMM;
4001 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004002 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004003 /* NB. Immediates are sign-extended as necessary. */
4004 switch (op->bytes) {
4005 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004006 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004007 break;
4008 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004009 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004010 break;
4011 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004012 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004013 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004014 case 8:
4015 op->val = insn_fetch(s64, ctxt);
4016 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004017 }
4018 if (!sign_extension) {
4019 switch (op->bytes) {
4020 case 1:
4021 op->val &= 0xff;
4022 break;
4023 case 2:
4024 op->val &= 0xffff;
4025 break;
4026 case 4:
4027 op->val &= 0xffffffff;
4028 break;
4029 }
4030 }
4031done:
4032 return rc;
4033}
4034
Avi Kivitya9945542011-09-13 10:45:41 +03004035static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4036 unsigned d)
4037{
4038 int rc = X86EMUL_CONTINUE;
4039
4040 switch (d) {
4041 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004042 decode_register_operand(ctxt, op);
Avi Kivitya9945542011-09-13 10:45:41 +03004043 break;
4044 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004045 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03004046 break;
4047 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004048 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004049 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03004050 *op = ctxt->memop;
4051 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03004052 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya9945542011-09-13 10:45:41 +03004053 fetch_bit_operand(ctxt);
4054 op->orig_val = op->val;
4055 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004056 case OpMem64:
4057 ctxt->memop.bytes = 8;
4058 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03004059 case OpAcc:
4060 op->type = OP_REG;
4061 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004062 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya9945542011-09-13 10:45:41 +03004063 fetch_register_operand(op);
4064 op->orig_val = op->val;
4065 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004066 case OpAccLo:
4067 op->type = OP_REG;
4068 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4069 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4070 fetch_register_operand(op);
4071 op->orig_val = op->val;
4072 break;
4073 case OpAccHi:
4074 if (ctxt->d & ByteOp) {
4075 op->type = OP_NONE;
4076 break;
4077 }
4078 op->type = OP_REG;
4079 op->bytes = ctxt->op_bytes;
4080 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4081 fetch_register_operand(op);
4082 op->orig_val = op->val;
4083 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004084 case OpDI:
4085 op->type = OP_MEM;
4086 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4087 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004088 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya9945542011-09-13 10:45:41 +03004089 op->addr.mem.seg = VCPU_SREG_ES;
4090 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004091 op->count = 1;
Avi Kivitya9945542011-09-13 10:45:41 +03004092 break;
4093 case OpDX:
4094 op->type = OP_REG;
4095 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004096 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya9945542011-09-13 10:45:41 +03004097 fetch_register_operand(op);
4098 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004099 case OpCL:
4100 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004101 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004102 break;
4103 case OpImmByte:
4104 rc = decode_imm(ctxt, op, 1, true);
4105 break;
4106 case OpOne:
4107 op->bytes = 1;
4108 op->val = 1;
4109 break;
4110 case OpImm:
4111 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4112 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004113 case OpImm64:
4114 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4115 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004116 case OpMem8:
4117 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004118 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004119 ctxt->memop.addr.reg = decode_register(ctxt,
4120 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004121 fetch_register_operand(&ctxt->memop);
4122 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004123 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004124 case OpMem16:
4125 ctxt->memop.bytes = 2;
4126 goto mem_common;
4127 case OpMem32:
4128 ctxt->memop.bytes = 4;
4129 goto mem_common;
4130 case OpImmU16:
4131 rc = decode_imm(ctxt, op, 2, false);
4132 break;
4133 case OpImmU:
4134 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4135 break;
4136 case OpSI:
4137 op->type = OP_MEM;
4138 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4139 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004140 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Avi Kivity0fe59122011-09-13 10:45:47 +03004141 op->addr.mem.seg = seg_override(ctxt);
4142 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004143 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004144 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004145 case OpXLat:
4146 op->type = OP_MEM;
4147 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4148 op->addr.mem.ea =
4149 register_address(ctxt,
4150 reg_read(ctxt, VCPU_REGS_RBX) +
4151 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
4152 op->addr.mem.seg = seg_override(ctxt);
4153 op->val = 0;
4154 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004155 case OpImmFAddr:
4156 op->type = OP_IMM;
4157 op->addr.mem.ea = ctxt->_eip;
4158 op->bytes = ctxt->op_bytes + 2;
4159 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4160 break;
4161 case OpMemFAddr:
4162 ctxt->memop.bytes = ctxt->op_bytes + 2;
4163 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004164 case OpES:
4165 op->val = VCPU_SREG_ES;
4166 break;
4167 case OpCS:
4168 op->val = VCPU_SREG_CS;
4169 break;
4170 case OpSS:
4171 op->val = VCPU_SREG_SS;
4172 break;
4173 case OpDS:
4174 op->val = VCPU_SREG_DS;
4175 break;
4176 case OpFS:
4177 op->val = VCPU_SREG_FS;
4178 break;
4179 case OpGS:
4180 op->val = VCPU_SREG_GS;
4181 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004182 case OpImplicit:
4183 /* Special instructions do their own operand decoding. */
4184 default:
4185 op->type = OP_NONE; /* Disable writeback. */
4186 break;
4187 }
4188
4189done:
4190 return rc;
4191}
4192
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004193int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004194{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004195 int rc = X86EMUL_CONTINUE;
4196 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004197 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004198 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03004199 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004200
Avi Kivityf09ed832011-09-13 10:45:40 +03004201 ctxt->memop.type = OP_NONE;
4202 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004203 ctxt->_eip = ctxt->eip;
4204 ctxt->fetch.start = ctxt->_eip;
4205 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004206 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01004207 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004208 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004209
4210 switch (mode) {
4211 case X86EMUL_MODE_REAL:
4212 case X86EMUL_MODE_VM86:
4213 case X86EMUL_MODE_PROT16:
4214 def_op_bytes = def_ad_bytes = 2;
4215 break;
4216 case X86EMUL_MODE_PROT32:
4217 def_op_bytes = def_ad_bytes = 4;
4218 break;
4219#ifdef CONFIG_X86_64
4220 case X86EMUL_MODE_PROT64:
4221 def_op_bytes = 4;
4222 def_ad_bytes = 8;
4223 break;
4224#endif
4225 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004226 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004227 }
4228
Avi Kivity9dac77f2011-06-01 15:34:25 +03004229 ctxt->op_bytes = def_op_bytes;
4230 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004231
4232 /* Legacy prefixes. */
4233 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004234 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004235 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004236 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004237 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004238 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004239 break;
4240 case 0x67: /* address-size override */
4241 if (mode == X86EMUL_MODE_PROT64)
4242 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004243 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004244 else
4245 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004246 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004247 break;
4248 case 0x26: /* ES override */
4249 case 0x2e: /* CS override */
4250 case 0x36: /* SS override */
4251 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004252 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004253 break;
4254 case 0x64: /* FS override */
4255 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004256 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004257 break;
4258 case 0x40 ... 0x4f: /* REX */
4259 if (mode != X86EMUL_MODE_PROT64)
4260 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004261 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004262 continue;
4263 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004264 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004265 break;
4266 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004267 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004268 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004269 break;
4270 default:
4271 goto done_prefixes;
4272 }
4273
4274 /* Any legacy prefix after a REX prefix nullifies its effect. */
4275
Avi Kivity9dac77f2011-06-01 15:34:25 +03004276 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004277 }
4278
4279done_prefixes:
4280
4281 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004282 if (ctxt->rex_prefix & 8)
4283 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004284
4285 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004286 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004287 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004288 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004289 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004290 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004291 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004292
4293 /* 0F_38 opcode map */
4294 if (ctxt->b == 0x38) {
4295 ctxt->opcode_len = 3;
4296 ctxt->b = insn_fetch(u8, ctxt);
4297 opcode = opcode_map_0f_38[ctxt->b];
4298 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004299 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004300 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004301
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004302 if (ctxt->d & ModRM)
4303 ctxt->modrm = insn_fetch(u8, ctxt);
4304
Avi Kivity9dac77f2011-06-01 15:34:25 +03004305 while (ctxt->d & GroupMask) {
4306 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004307 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004308 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004309 opcode = opcode.u.group[goffset];
4310 break;
4311 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004312 goffset = (ctxt->modrm >> 3) & 7;
4313 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004314 opcode = opcode.u.gdual->mod3[goffset];
4315 else
4316 opcode = opcode.u.gdual->mod012[goffset];
4317 break;
4318 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004319 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004320 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004321 break;
4322 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004323 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004324 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004325 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004326 switch (simd_prefix) {
4327 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4328 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4329 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4330 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4331 }
4332 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004333 case Escape:
4334 if (ctxt->modrm > 0xbf)
4335 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4336 else
4337 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4338 break;
Avi Kivity46561642011-04-24 14:09:59 +03004339 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004340 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004341 }
Avi Kivity46561642011-04-24 14:09:59 +03004342
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004343 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004344 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004345 }
4346
Avi Kivity9dac77f2011-06-01 15:34:25 +03004347 ctxt->execute = opcode.u.execute;
4348 ctxt->check_perm = opcode.check_perm;
4349 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004350
4351 /* Unrecognised? */
Gleb Natapov1146a782013-04-11 12:30:01 +03004352 if (ctxt->d == 0 || (ctxt->d & NotImpl))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004353 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004354
Borislav Petkovb51e9742013-09-22 16:44:52 +02004355 if (!(ctxt->d & EmulateOnUD) && ctxt->ud)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004356 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004357
Avi Kivity9dac77f2011-06-01 15:34:25 +03004358 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
4359 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004360
Avi Kivity9dac77f2011-06-01 15:34:25 +03004361 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004362 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004363 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004364 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03004365 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004366 }
4367
Avi Kivity9dac77f2011-06-01 15:34:25 +03004368 if (ctxt->d & Sse)
4369 ctxt->op_bytes = 16;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004370 else if (ctxt->d & Mmx)
4371 ctxt->op_bytes = 8;
Avi Kivity12537912011-03-29 11:41:27 +02004372
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004373 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004374 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004375 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004376 if (!ctxt->has_seg_override)
4377 set_seg_override(ctxt, ctxt->modrm_seg);
4378 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004379 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004380 if (rc != X86EMUL_CONTINUE)
4381 goto done;
4382
Avi Kivity9dac77f2011-06-01 15:34:25 +03004383 if (!ctxt->has_seg_override)
4384 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004385
Avi Kivityf09ed832011-09-13 10:45:40 +03004386 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004387
Avi Kivityf09ed832011-09-13 10:45:40 +03004388 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
4389 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004390
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004391 /*
4392 * Decode and fetch the source operand: register, memory
4393 * or immediate.
4394 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004395 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004396 if (rc != X86EMUL_CONTINUE)
4397 goto done;
4398
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004399 /*
4400 * Decode and fetch the second source operand: register, memory
4401 * or immediate.
4402 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004403 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004404 if (rc != X86EMUL_CONTINUE)
4405 goto done;
4406
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004407 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03004408 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004409
4410done:
Avi Kivityf09ed832011-09-13 10:45:40 +03004411 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
4412 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004413
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004414 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004415}
4416
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004417bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4418{
4419 return ctxt->d & PageTable;
4420}
4421
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004422static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4423{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004424 /* The second termination condition only applies for REPE
4425 * and REPNE. Test if the repeat string operation prefix is
4426 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4427 * corresponding termination condition according to:
4428 * - if REPE/REPZ and ZF = 0 then done
4429 * - if REPNE/REPNZ and ZF = 1 then done
4430 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004431 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4432 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4433 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004434 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004435 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004436 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4437 return true;
4438
4439 return false;
4440}
4441
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004442static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4443{
4444 bool fault = false;
4445
4446 ctxt->ops->get_fpu(ctxt);
4447 asm volatile("1: fwait \n\t"
4448 "2: \n\t"
4449 ".pushsection .fixup,\"ax\" \n\t"
4450 "3: \n\t"
4451 "movb $1, %[fault] \n\t"
4452 "jmp 2b \n\t"
4453 ".popsection \n\t"
4454 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004455 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004456 ctxt->ops->put_fpu(ctxt);
4457
4458 if (unlikely(fault))
4459 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4460
4461 return X86EMUL_CONTINUE;
4462}
4463
4464static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4465 struct operand *op)
4466{
4467 if (op->type == OP_MM)
4468 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4469}
4470
Avi Kivitye28bbd42013-01-04 16:18:48 +02004471static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4472{
4473 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivityb9fa4092013-02-09 11:31:48 +02004474 if (!(ctxt->d & ByteOp))
4475 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Avi Kivitye28bbd42013-01-04 16:18:48 +02004476 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004477 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
4478 [fastop]"+S"(fop)
4479 : "c"(ctxt->src2.val));
Avi Kivitye28bbd42013-01-04 16:18:48 +02004480 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004481 if (!fop) /* exception is returned in fop variable */
4482 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02004483 return X86EMUL_CONTINUE;
4484}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004485
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004486int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004487{
Mathias Krause0225fb52012-08-30 01:30:16 +02004488 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004489 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004490 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004491
Avi Kivity9dac77f2011-06-01 15:34:25 +03004492 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004493
Gleb Natapov1146a782013-04-11 12:30:01 +03004494 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4495 (ctxt->d & Undefined)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004496 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02004497 goto done;
4498 }
4499
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004500 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004501 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004502 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004503 goto done;
4504 }
4505
Avi Kivity9dac77f2011-06-01 15:34:25 +03004506 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004507 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004508 goto done;
4509 }
4510
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004511 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4512 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02004513 rc = emulate_ud(ctxt);
4514 goto done;
4515 }
4516
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004517 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02004518 rc = emulate_nm(ctxt);
4519 goto done;
4520 }
4521
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004522 if (ctxt->d & Mmx) {
4523 rc = flush_pending_x87_faults(ctxt);
4524 if (rc != X86EMUL_CONTINUE)
4525 goto done;
4526 /*
4527 * Now that we know the fpu is exception safe, we can fetch
4528 * operands from it.
4529 */
4530 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4531 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4532 if (!(ctxt->d & Mov))
4533 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4534 }
4535
Avi Kivity9dac77f2011-06-01 15:34:25 +03004536 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4537 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004538 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004539 if (rc != X86EMUL_CONTINUE)
4540 goto done;
4541 }
4542
Gleb Natapove92805a2010-02-10 14:21:35 +02004543 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004544 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004545 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02004546 goto done;
4547 }
4548
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004549 /* Instruction can only be executed in protected mode */
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03004550 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004551 rc = emulate_ud(ctxt);
4552 goto done;
4553 }
4554
Joerg Roedeld09beab2011-04-04 12:39:25 +02004555 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004556 if (ctxt->check_perm) {
4557 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004558 if (rc != X86EMUL_CONTINUE)
4559 goto done;
4560 }
4561
Avi Kivity9dac77f2011-06-01 15:34:25 +03004562 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4563 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004564 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004565 if (rc != X86EMUL_CONTINUE)
4566 goto done;
4567 }
4568
Avi Kivity9dac77f2011-06-01 15:34:25 +03004569 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004570 /* All REP prefixes have the same first termination condition */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004571 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004572 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004573 goto done;
4574 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004575 }
4576
Avi Kivity9dac77f2011-06-01 15:34:25 +03004577 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4578 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4579 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004580 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004581 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004582 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004583 }
4584
Avi Kivity9dac77f2011-06-01 15:34:25 +03004585 if (ctxt->src2.type == OP_MEM) {
4586 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4587 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004588 if (rc != X86EMUL_CONTINUE)
4589 goto done;
4590 }
4591
Avi Kivity9dac77f2011-06-01 15:34:25 +03004592 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004593 goto special_insn;
4594
4595
Avi Kivity9dac77f2011-06-01 15:34:25 +03004596 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004597 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004598 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4599 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004600 if (rc != X86EMUL_CONTINUE)
4601 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004602 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004603 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004604
Avi Kivity018a98d2007-11-27 19:30:56 +02004605special_insn:
4606
Avi Kivity9dac77f2011-06-01 15:34:25 +03004607 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4608 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004609 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004610 if (rc != X86EMUL_CONTINUE)
4611 goto done;
4612 }
4613
Avi Kivity9dac77f2011-06-01 15:34:25 +03004614 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004615 if (ctxt->d & Fastop) {
4616 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4617 rc = fastop(ctxt, fop);
4618 if (rc != X86EMUL_CONTINUE)
4619 goto done;
4620 goto writeback;
4621 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004622 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004623 if (rc != X86EMUL_CONTINUE)
4624 goto done;
4625 goto writeback;
4626 }
4627
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004628 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004629 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004630 else if (ctxt->opcode_len == 3)
4631 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004632
Avi Kivity9dac77f2011-06-01 15:34:25 +03004633 switch (ctxt->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004634 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004635 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004636 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004637 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004638 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004639 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004640 if (test_cc(ctxt->b, ctxt->eflags))
4641 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004642 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004643 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004644 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004645 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004646 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004647 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Mohammed Gamal34698d82010-08-04 14:41:04 +03004648 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004649 rc = em_xchg(ctxt);
4650 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004651 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004652 switch (ctxt->op_bytes) {
4653 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4654 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4655 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004656 }
4657 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004658 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004659 rc = emulate_int(ctxt, 3);
4660 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004661 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004662 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004663 break;
4664 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004665 if (ctxt->eflags & EFLG_OF)
4666 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004667 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004668 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004669 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004670 jmp_rel(ctxt, ctxt->src.val);
4671 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004672 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004673 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004674 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004675 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004676 case 0xf5: /* cmc */
4677 /* complement carry flag from eflags reg */
4678 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004679 break;
4680 case 0xf8: /* clc */
4681 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004682 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004683 case 0xf9: /* stc */
4684 ctxt->eflags |= EFLG_CF;
4685 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004686 case 0xfc: /* cld */
4687 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004688 break;
4689 case 0xfd: /* std */
4690 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004691 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004692 default:
4693 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004694 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004695
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004696 if (rc != X86EMUL_CONTINUE)
4697 goto done;
4698
Avi Kivity018a98d2007-11-27 19:30:56 +02004699writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02004700 if (!(ctxt->d & NoWrite)) {
4701 rc = writeback(ctxt, &ctxt->dst);
4702 if (rc != X86EMUL_CONTINUE)
4703 goto done;
4704 }
4705 if (ctxt->d & SrcWrite) {
4706 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
4707 rc = writeback(ctxt, &ctxt->src);
4708 if (rc != X86EMUL_CONTINUE)
4709 goto done;
4710 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004711
Gleb Natapov5cd21912010-03-18 15:20:26 +02004712 /*
4713 * restore dst type in case the decoding will be reused
4714 * (happens for string instruction )
4715 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004716 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004717
Avi Kivity9dac77f2011-06-01 15:34:25 +03004718 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004719 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004720
Avi Kivity9dac77f2011-06-01 15:34:25 +03004721 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004722 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004723
Avi Kivity9dac77f2011-06-01 15:34:25 +03004724 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004725 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004726 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004727 if ((ctxt->d & SrcMask) == SrcSI)
4728 count = ctxt->src.count;
4729 else
4730 count = ctxt->dst.count;
4731 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4732 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004733
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004734 if (!string_insn_completed(ctxt)) {
4735 /*
4736 * Re-enter guest when pio read ahead buffer is empty
4737 * or, if it is not used, after each 1024 iteration.
4738 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004739 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004740 (r->end == 0 || r->end != r->pos)) {
4741 /*
4742 * Reset read cache. Usually happens before
4743 * decode, but since instruction is restarted
4744 * we have to do it here.
4745 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004746 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004747 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004748 return EMULATION_RESTART;
4749 }
4750 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004751 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004752 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004753
Avi Kivity9dac77f2011-06-01 15:34:25 +03004754 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004755
4756done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004757 if (rc == X86EMUL_PROPAGATE_FAULT)
4758 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004759 if (rc == X86EMUL_INTERCEPTED)
4760 return EMULATION_INTERCEPTED;
4761
Avi Kivitydd856ef2012-08-27 23:46:17 +03004762 if (rc == X86EMUL_CONTINUE)
4763 writeback_registers(ctxt);
4764
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004765 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004766
4767twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004768 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004769 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004770 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004771 break;
4772 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004773 case 0x0d: /* GrpP (prefetch) */
4774 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004775 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02004776 break;
4777 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004778 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004779 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004780 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004781 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004782 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004783 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004784 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4785 if (!test_cc(ctxt->b, ctxt->eflags))
4786 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004787 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004788 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004789 if (test_cc(ctxt->b, ctxt->eflags))
4790 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004791 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004792 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004793 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004794 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004795 case 0xae: /* clflush */
4796 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004797 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004798 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004799 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004800 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004801 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004802 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004803 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004804 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004805 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004806 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004807 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004808 ctxt->dst.bytes = ctxt->op_bytes;
4809 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4810 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004811 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004812 default:
4813 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004814 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004815
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004816threebyte_insn:
4817
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004818 if (rc != X86EMUL_CONTINUE)
4819 goto done;
4820
Avi Kivity6aa8b732006-12-10 02:21:36 -08004821 goto writeback;
4822
4823cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004824 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004825}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004826
4827void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
4828{
4829 invalidate_registers(ctxt);
4830}
4831
4832void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
4833{
4834 writeback_registers(ctxt);
4835}