blob: 8e2a07bd8eacacc8446a1c3082b44b2c58b38d19 [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,
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000788 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789{
790 void *p;
791
Avi Kivity6aa8b732006-12-10 02:21:36 -0800792 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300793 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
794 else
795 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800796 return p;
797}
798
799static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200800 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800801 u16 *size, unsigned long *address, int op_bytes)
802{
803 int rc;
804
805 if (op_bytes == 2)
806 op_bytes = 3;
807 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200808 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900809 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800810 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200811 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200812 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800813 return rc;
814}
815
Avi Kivity34b77652013-01-19 19:51:56 +0200816FASTOP2(add);
817FASTOP2(or);
818FASTOP2(adc);
819FASTOP2(sbb);
820FASTOP2(and);
821FASTOP2(sub);
822FASTOP2(xor);
823FASTOP2(cmp);
824FASTOP2(test);
825
Avi Kivityb9fa4092013-02-09 11:31:48 +0200826FASTOP1SRC2(mul, mul_ex);
827FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200828FASTOP1SRC2EX(div, div_ex);
829FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +0200830
Avi Kivity34b77652013-01-19 19:51:56 +0200831FASTOP3WCL(shld);
832FASTOP3WCL(shrd);
833
834FASTOP2W(imul);
835
836FASTOP1(not);
837FASTOP1(neg);
838FASTOP1(inc);
839FASTOP1(dec);
840
841FASTOP2CL(rol);
842FASTOP2CL(ror);
843FASTOP2CL(rcl);
844FASTOP2CL(rcr);
845FASTOP2CL(shl);
846FASTOP2CL(shr);
847FASTOP2CL(sar);
848
849FASTOP2W(bsf);
850FASTOP2W(bsr);
851FASTOP2W(bt);
852FASTOP2W(bts);
853FASTOP2W(btr);
854FASTOP2W(btc);
855
Avi Kivitye47a5f52013-02-09 11:31:51 +0200856FASTOP2(xadd);
857
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200858static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300859{
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200860 u8 rc;
861 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300862
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200863 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +0200864 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200865 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
866 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300867}
868
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300869static void fetch_register_operand(struct operand *op)
870{
871 switch (op->bytes) {
872 case 1:
873 op->val = *(u8 *)op->addr.reg;
874 break;
875 case 2:
876 op->val = *(u16 *)op->addr.reg;
877 break;
878 case 4:
879 op->val = *(u32 *)op->addr.reg;
880 break;
881 case 8:
882 op->val = *(u64 *)op->addr.reg;
883 break;
884 }
885}
886
Avi Kivity12537912011-03-29 11:41:27 +0200887static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
888{
889 ctxt->ops->get_fpu(ctxt);
890 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200891 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
892 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
893 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
894 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
895 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
896 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
897 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
898 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200899#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200900 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
901 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
902 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
903 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
904 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
905 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
906 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
907 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200908#endif
909 default: BUG();
910 }
911 ctxt->ops->put_fpu(ctxt);
912}
913
914static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
915 int reg)
916{
917 ctxt->ops->get_fpu(ctxt);
918 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200919 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
920 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
921 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
922 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
923 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
924 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
925 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
926 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200927#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200928 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
929 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
930 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
931 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
932 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
933 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
934 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
935 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200936#endif
937 default: BUG();
938 }
939 ctxt->ops->put_fpu(ctxt);
940}
941
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300942static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
943{
944 ctxt->ops->get_fpu(ctxt);
945 switch (reg) {
946 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
947 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
948 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
949 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
950 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
951 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
952 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
953 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
954 default: BUG();
955 }
956 ctxt->ops->put_fpu(ctxt);
957}
958
959static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
960{
961 ctxt->ops->get_fpu(ctxt);
962 switch (reg) {
963 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
964 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
965 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
966 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
967 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
968 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
969 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
970 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
971 default: BUG();
972 }
973 ctxt->ops->put_fpu(ctxt);
974}
975
Gleb Natapov045a2822012-12-20 16:57:43 +0200976static int em_fninit(struct x86_emulate_ctxt *ctxt)
977{
978 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
979 return emulate_nm(ctxt);
980
981 ctxt->ops->get_fpu(ctxt);
982 asm volatile("fninit");
983 ctxt->ops->put_fpu(ctxt);
984 return X86EMUL_CONTINUE;
985}
986
987static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
988{
989 u16 fcw;
990
991 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
992 return emulate_nm(ctxt);
993
994 ctxt->ops->get_fpu(ctxt);
995 asm volatile("fnstcw %0": "+m"(fcw));
996 ctxt->ops->put_fpu(ctxt);
997
998 /* force 2 byte destination */
999 ctxt->dst.bytes = 2;
1000 ctxt->dst.val = fcw;
1001
1002 return X86EMUL_CONTINUE;
1003}
1004
1005static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1006{
1007 u16 fsw;
1008
1009 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1010 return emulate_nm(ctxt);
1011
1012 ctxt->ops->get_fpu(ctxt);
1013 asm volatile("fnstsw %0": "+m"(fsw));
1014 ctxt->ops->put_fpu(ctxt);
1015
1016 /* force 2 byte destination */
1017 ctxt->dst.bytes = 2;
1018 ctxt->dst.val = fsw;
1019
1020 return X86EMUL_CONTINUE;
1021}
1022
Avi Kivity12537912011-03-29 11:41:27 +02001023static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001024 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001025{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001026 unsigned reg = ctxt->modrm_reg;
1027 int highbyte_regs = ctxt->rex_prefix == 0;
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;
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001048 if (ctxt->d & ByteOp) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001049 op->addr.reg = decode_register(ctxt, reg, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +02001050 op->bytes = 1;
1051 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001052 op->addr.reg = decode_register(ctxt, reg, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001053 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +02001054 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001055 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001056 op->orig_val = op->val;
1057}
1058
Avi Kivitya6e34072012-06-10 17:15:39 +03001059static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1060{
1061 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1062 ctxt->modrm_seg = VCPU_SREG_SS;
1063}
1064
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001065static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001066 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001067{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001068 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001069 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001070 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001071 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001072
Avi Kivity9dac77f2011-06-01 15:34:25 +03001073 if (ctxt->rex_prefix) {
1074 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
1075 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
1076 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001077 }
1078
Avi Kivity9dac77f2011-06-01 15:34:25 +03001079 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
1080 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
1081 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
1082 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001083
Avi Kivity9dac77f2011-06-01 15:34:25 +03001084 if (ctxt->modrm_mod == 3) {
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001085 int highbyte_regs = ctxt->rex_prefix == 0;
1086
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001087 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001088 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001089 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
1090 highbyte_regs && (ctxt->d & ByteOp));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001091 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001092 op->type = OP_XMM;
1093 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001094 op->addr.xmm = ctxt->modrm_rm;
1095 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001096 return rc;
1097 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001098 if (ctxt->d & Mmx) {
1099 op->type = OP_MM;
1100 op->bytes = 8;
1101 op->addr.xmm = ctxt->modrm_rm & 7;
1102 return rc;
1103 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001104 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001105 return rc;
1106 }
1107
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001108 op->type = OP_MEM;
1109
Avi Kivity9dac77f2011-06-01 15:34:25 +03001110 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001111 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1112 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1113 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1114 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001115
1116 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001117 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001118 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001119 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001120 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001121 break;
1122 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001123 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001124 break;
1125 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001126 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001127 break;
1128 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001129 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001130 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001131 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001132 break;
1133 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001134 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001135 break;
1136 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001137 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001138 break;
1139 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001140 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001141 break;
1142 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001143 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001144 break;
1145 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001146 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001147 break;
1148 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001149 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001150 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001151 break;
1152 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001153 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001154 break;
1155 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001156 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1157 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1158 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001159 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001160 } else {
1161 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001162 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001163 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001164 index_reg |= (sib >> 3) & 7;
1165 base_reg |= sib & 7;
1166 scale = sib >> 6;
1167
Avi Kivity9dac77f2011-06-01 15:34:25 +03001168 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001169 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001170 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001171 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001172 adjust_modrm_seg(ctxt, base_reg);
1173 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001174 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001175 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001176 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001177 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001178 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001179 } else {
1180 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001181 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001182 adjust_modrm_seg(ctxt, base_reg);
1183 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001184 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001185 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001186 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001187 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001188 break;
1189 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001190 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001191 break;
1192 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001193 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001194 break;
1195 }
1196 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001197 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001198done:
1199 return rc;
1200}
1201
1202static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001203 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001204{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001205 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001206
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001207 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001208 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001209 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001210 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001211 break;
1212 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001213 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001214 break;
1215 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001216 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001217 break;
1218 }
1219done:
1220 return rc;
1221}
1222
Avi Kivity9dac77f2011-06-01 15:34:25 +03001223static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001224{
Sheng Yang7129eec2010-09-28 16:33:32 +08001225 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001226
Avi Kivity9dac77f2011-06-01 15:34:25 +03001227 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1228 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001229
Avi Kivity9dac77f2011-06-01 15:34:25 +03001230 if (ctxt->src.bytes == 2)
1231 sv = (s16)ctxt->src.val & (s16)mask;
1232 else if (ctxt->src.bytes == 4)
1233 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001234
Avi Kivity9dac77f2011-06-01 15:34:25 +03001235 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001236 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001237
1238 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001239 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001240}
1241
Gleb Natapov9de41572010-04-28 19:15:22 +03001242static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001243 unsigned long addr, void *dest, unsigned size)
1244{
1245 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001246 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001247
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001248 if (mc->pos < mc->end)
1249 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001250
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001251 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001252
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001253 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1254 &ctxt->exception);
1255 if (rc != X86EMUL_CONTINUE)
1256 return rc;
1257
1258 mc->end += size;
1259
1260read_cached:
1261 memcpy(dest, mc->data + mc->pos, size);
1262 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001263 return X86EMUL_CONTINUE;
1264}
1265
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001266static int segmented_read(struct x86_emulate_ctxt *ctxt,
1267 struct segmented_address addr,
1268 void *data,
1269 unsigned size)
1270{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001271 int rc;
1272 ulong linear;
1273
Avi Kivity83b87952011-04-03 11:31:19 +03001274 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001275 if (rc != X86EMUL_CONTINUE)
1276 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001277 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001278}
1279
1280static int segmented_write(struct x86_emulate_ctxt *ctxt,
1281 struct segmented_address addr,
1282 const void *data,
1283 unsigned size)
1284{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001285 int rc;
1286 ulong linear;
1287
Avi Kivity83b87952011-04-03 11:31:19 +03001288 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001289 if (rc != X86EMUL_CONTINUE)
1290 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001291 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1292 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001293}
1294
1295static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1296 struct segmented_address addr,
1297 const void *orig_data, const void *data,
1298 unsigned size)
1299{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001300 int rc;
1301 ulong linear;
1302
Avi Kivity83b87952011-04-03 11:31:19 +03001303 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001304 if (rc != X86EMUL_CONTINUE)
1305 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001306 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1307 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001308}
1309
Gleb Natapov7b262e92010-03-18 15:20:27 +02001310static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001311 unsigned int size, unsigned short port,
1312 void *dest)
1313{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001314 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001315
1316 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001317 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001318 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001319 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001320 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001321 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1322 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Gleb Natapov7b262e92010-03-18 15:20:27 +02001323 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1324 count);
1325 if (n == 0)
1326 n = 1;
1327 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001328 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001329 return 0;
1330 rc->end = n * size;
1331 }
1332
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001333 if (ctxt->rep_prefix && !(ctxt->eflags & EFLG_DF)) {
1334 ctxt->dst.data = rc->data + rc->pos;
1335 ctxt->dst.type = OP_MEM_STR;
1336 ctxt->dst.count = (rc->end - rc->pos) / size;
1337 rc->pos = rc->end;
1338 } else {
1339 memcpy(dest, rc->data + rc->pos, size);
1340 rc->pos += size;
1341 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001342 return 1;
1343}
1344
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001345static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1346 u16 index, struct desc_struct *desc)
1347{
1348 struct desc_ptr dt;
1349 ulong addr;
1350
1351 ctxt->ops->get_idt(ctxt, &dt);
1352
1353 if (dt.size < index * 8 + 7)
1354 return emulate_gp(ctxt, index << 3 | 0x2);
1355
1356 addr = dt.address + index * 8;
1357 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1358 &ctxt->exception);
1359}
1360
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001361static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001362 u16 selector, struct desc_ptr *dt)
1363{
Mathias Krause0225fb52012-08-30 01:30:16 +02001364 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001365
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001366 if (selector & 1 << 2) {
1367 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001368 u16 sel;
1369
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001370 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001371 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001372 return;
1373
1374 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1375 dt->address = get_desc_base(&desc);
1376 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001377 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001378}
1379
1380/* allowed just for 8 bytes segments */
1381static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001382 u16 selector, struct desc_struct *desc,
1383 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001384{
1385 struct desc_ptr dt;
1386 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001387 ulong addr;
1388
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001389 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001390
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001391 if (dt.size < index * 8 + 7)
1392 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001393
Avi Kivitye9194642012-06-13 16:29:39 +03001394 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001395 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1396 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001397}
1398
1399/* allowed just for 8 bytes segments */
1400static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001401 u16 selector, struct desc_struct *desc)
1402{
1403 struct desc_ptr dt;
1404 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001405 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001406
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001407 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001408
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001409 if (dt.size < index * 8 + 7)
1410 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001411
1412 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001413 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1414 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001415}
1416
Gleb Natapov5601d052011-03-07 14:55:06 +02001417/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001418static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001419 u16 selector, int seg)
1420{
Avi Kivity869be992012-06-13 16:30:53 +03001421 struct desc_struct seg_desc, old_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001422 u8 dpl, rpl, cpl;
1423 unsigned err_vec = GP_VECTOR;
1424 u32 err_code = 0;
1425 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001426 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001427 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001428 u16 dummy;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001429
1430 memset(&seg_desc, 0, sizeof seg_desc);
1431
Kevin Wolff8da94e2013-04-11 14:06:03 +02001432 if (ctxt->mode == X86EMUL_MODE_REAL) {
1433 /* set real mode segment descriptor (keep limit etc. for
1434 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001435 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001436 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001437 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001438 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1439 /* VM86 needs a clean new segment descriptor */
1440 set_desc_base(&seg_desc, selector << 4);
1441 set_desc_limit(&seg_desc, 0xffff);
1442 seg_desc.type = 3;
1443 seg_desc.p = 1;
1444 seg_desc.s = 1;
1445 seg_desc.dpl = 3;
1446 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001447 }
1448
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001449 rpl = selector & 3;
1450 cpl = ctxt->ops->cpl(ctxt);
1451
1452 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1453 if ((seg == VCPU_SREG_CS
1454 || (seg == VCPU_SREG_SS
1455 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1456 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001457 && null_selector)
1458 goto exception;
1459
1460 /* TR should be in GDT only */
1461 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1462 goto exception;
1463
1464 if (null_selector) /* for NULL selector skip all following checks */
1465 goto load;
1466
Avi Kivitye9194642012-06-13 16:29:39 +03001467 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001468 if (ret != X86EMUL_CONTINUE)
1469 return ret;
1470
1471 err_code = selector & 0xfffc;
1472 err_vec = GP_VECTOR;
1473
Guo Chaofc058682012-06-28 15:19:51 +08001474 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001475 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1476 goto exception;
1477
1478 if (!seg_desc.p) {
1479 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1480 goto exception;
1481 }
1482
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001483 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001484
1485 switch (seg) {
1486 case VCPU_SREG_SS:
1487 /*
1488 * segment is not a writable data segment or segment
1489 * selector's RPL != CPL or segment selector's RPL != CPL
1490 */
1491 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1492 goto exception;
1493 break;
1494 case VCPU_SREG_CS:
1495 if (!(seg_desc.type & 8))
1496 goto exception;
1497
1498 if (seg_desc.type & 4) {
1499 /* conforming */
1500 if (dpl > cpl)
1501 goto exception;
1502 } else {
1503 /* nonconforming */
1504 if (rpl > cpl || dpl != cpl)
1505 goto exception;
1506 }
1507 /* CS(RPL) <- CPL */
1508 selector = (selector & 0xfffc) | cpl;
1509 break;
1510 case VCPU_SREG_TR:
1511 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1512 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001513 old_desc = seg_desc;
1514 seg_desc.type |= 2; /* busy */
1515 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1516 sizeof(seg_desc), &ctxt->exception);
1517 if (ret != X86EMUL_CONTINUE)
1518 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001519 break;
1520 case VCPU_SREG_LDTR:
1521 if (seg_desc.s || seg_desc.type != 2)
1522 goto exception;
1523 break;
1524 default: /* DS, ES, FS, or GS */
1525 /*
1526 * segment is not a data or readable code segment or
1527 * ((segment is a data or nonconforming code segment)
1528 * and (both RPL and CPL > DPL))
1529 */
1530 if ((seg_desc.type & 0xa) == 0x8 ||
1531 (((seg_desc.type & 0xc) != 0xc) &&
1532 (rpl > dpl && cpl > dpl)))
1533 goto exception;
1534 break;
1535 }
1536
1537 if (seg_desc.s) {
1538 /* mark segment as accessed */
1539 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001540 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001541 if (ret != X86EMUL_CONTINUE)
1542 return ret;
1543 }
1544load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001545 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001546 return X86EMUL_CONTINUE;
1547exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001548 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001549 return X86EMUL_PROPAGATE_FAULT;
1550}
1551
Wei Yongjun31be40b2010-08-17 09:17:30 +08001552static void write_register_operand(struct operand *op)
1553{
1554 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1555 switch (op->bytes) {
1556 case 1:
1557 *(u8 *)op->addr.reg = (u8)op->val;
1558 break;
1559 case 2:
1560 *(u16 *)op->addr.reg = (u16)op->val;
1561 break;
1562 case 4:
1563 *op->addr.reg = (u32)op->val;
1564 break; /* 64b: zero-extend */
1565 case 8:
1566 *op->addr.reg = op->val;
1567 break;
1568 }
1569}
1570
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001571static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001572{
1573 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001574
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001575 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001576 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001577 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001578 break;
1579 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001580 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001581 rc = segmented_cmpxchg(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001582 op->addr.mem,
1583 &op->orig_val,
1584 &op->val,
1585 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001586 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001587 rc = segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001588 op->addr.mem,
1589 &op->val,
1590 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001591 if (rc != X86EMUL_CONTINUE)
1592 return rc;
1593 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001594 case OP_MEM_STR:
1595 rc = segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001596 op->addr.mem,
1597 op->data,
1598 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001599 if (rc != X86EMUL_CONTINUE)
1600 return rc;
1601 break;
Avi Kivity12537912011-03-29 11:41:27 +02001602 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001603 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001604 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001605 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001606 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001607 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001608 case OP_NONE:
1609 /* no writeback */
1610 break;
1611 default:
1612 break;
1613 }
1614 return X86EMUL_CONTINUE;
1615}
1616
Avi Kivity51ddff52012-06-12 20:19:40 +03001617static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001618{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001619 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001620
Avi Kivity5ad105e2012-08-19 14:34:31 +03001621 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001622 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001623 addr.seg = VCPU_SREG_SS;
1624
Avi Kivity51ddff52012-06-12 20:19:40 +03001625 return segmented_write(ctxt, addr, data, bytes);
1626}
1627
1628static int em_push(struct x86_emulate_ctxt *ctxt)
1629{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001630 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001631 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001632 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001633}
1634
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001635static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001636 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001637{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001638 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001639 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001640
Avi Kivitydd856ef2012-08-27 23:46:17 +03001641 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001642 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001643 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001644 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001645 return rc;
1646
Avi Kivity5ad105e2012-08-19 14:34:31 +03001647 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001648 return rc;
1649}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001650
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001651static int em_pop(struct x86_emulate_ctxt *ctxt)
1652{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001653 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001654}
1655
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001656static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001657 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001658{
1659 int rc;
1660 unsigned long val, change_mask;
1661 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001662 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001663
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001664 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001665 if (rc != X86EMUL_CONTINUE)
1666 return rc;
1667
1668 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1669 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1670
1671 switch(ctxt->mode) {
1672 case X86EMUL_MODE_PROT64:
1673 case X86EMUL_MODE_PROT32:
1674 case X86EMUL_MODE_PROT16:
1675 if (cpl == 0)
1676 change_mask |= EFLG_IOPL;
1677 if (cpl <= iopl)
1678 change_mask |= EFLG_IF;
1679 break;
1680 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001681 if (iopl < 3)
1682 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001683 change_mask |= EFLG_IF;
1684 break;
1685 default: /* real mode */
1686 change_mask |= (EFLG_IOPL | EFLG_IF);
1687 break;
1688 }
1689
1690 *(unsigned long *)dest =
1691 (ctxt->eflags & ~change_mask) | (val & change_mask);
1692
1693 return rc;
1694}
1695
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001696static int em_popf(struct x86_emulate_ctxt *ctxt)
1697{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001698 ctxt->dst.type = OP_REG;
1699 ctxt->dst.addr.reg = &ctxt->eflags;
1700 ctxt->dst.bytes = ctxt->op_bytes;
1701 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001702}
1703
Avi Kivity612e89f2012-06-12 20:03:23 +03001704static int em_enter(struct x86_emulate_ctxt *ctxt)
1705{
1706 int rc;
1707 unsigned frame_size = ctxt->src.val;
1708 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001709 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001710
1711 if (nesting_level)
1712 return X86EMUL_UNHANDLEABLE;
1713
Avi Kivitydd856ef2012-08-27 23:46:17 +03001714 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1715 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001716 if (rc != X86EMUL_CONTINUE)
1717 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001718 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001719 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001720 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1721 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001722 stack_mask(ctxt));
1723 return X86EMUL_CONTINUE;
1724}
1725
Avi Kivityf47cfa32012-06-07 17:49:24 +03001726static int em_leave(struct x86_emulate_ctxt *ctxt)
1727{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001728 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001729 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001730 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001731}
1732
Avi Kivity1cd196e2011-09-13 10:45:51 +03001733static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001734{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001735 int seg = ctxt->src2.val;
1736
Avi Kivity9dac77f2011-06-01 15:34:25 +03001737 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001738
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001739 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001740}
1741
Avi Kivity1cd196e2011-09-13 10:45:51 +03001742static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001743{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001744 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001745 unsigned long selector;
1746 int rc;
1747
Avi Kivity9dac77f2011-06-01 15:34:25 +03001748 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001749 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001750 return rc;
1751
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001752 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001753 return rc;
1754}
1755
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001756static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001757{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001758 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001759 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001760 int reg = VCPU_REGS_RAX;
1761
1762 while (reg <= VCPU_REGS_RDI) {
1763 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001764 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001765
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001766 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001767 if (rc != X86EMUL_CONTINUE)
1768 return rc;
1769
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001770 ++reg;
1771 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001772
Wei Yongjunc37eda12010-06-15 09:03:33 +08001773 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001774}
1775
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001776static int em_pushf(struct x86_emulate_ctxt *ctxt)
1777{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001778 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001779 return em_push(ctxt);
1780}
1781
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001782static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001783{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001784 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001785 int reg = VCPU_REGS_RDI;
1786
1787 while (reg >= VCPU_REGS_RAX) {
1788 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001789 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001790 --reg;
1791 }
1792
Avi Kivitydd856ef2012-08-27 23:46:17 +03001793 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001794 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001795 break;
1796 --reg;
1797 }
1798 return rc;
1799}
1800
Avi Kivitydd856ef2012-08-27 23:46:17 +03001801static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001802{
Mathias Krause0225fb52012-08-30 01:30:16 +02001803 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001804 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001805 struct desc_ptr dt;
1806 gva_t cs_addr;
1807 gva_t eip_addr;
1808 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001809
1810 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001811 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001812 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001813 if (rc != X86EMUL_CONTINUE)
1814 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001815
1816 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1817
Avi Kivity9dac77f2011-06-01 15:34:25 +03001818 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
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;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001822
Avi Kivity9dac77f2011-06-01 15:34:25 +03001823 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001824 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001825 if (rc != X86EMUL_CONTINUE)
1826 return rc;
1827
Avi Kivity4bff1e862011-04-20 13:37:53 +03001828 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001829
1830 eip_addr = dt.address + (irq << 2);
1831 cs_addr = dt.address + (irq << 2) + 2;
1832
Avi Kivity0f65dd72011-04-20 13:37:53 +03001833 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001834 if (rc != X86EMUL_CONTINUE)
1835 return rc;
1836
Avi Kivity0f65dd72011-04-20 13:37:53 +03001837 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001838 if (rc != X86EMUL_CONTINUE)
1839 return rc;
1840
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001841 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001842 if (rc != X86EMUL_CONTINUE)
1843 return rc;
1844
Avi Kivity9dac77f2011-06-01 15:34:25 +03001845 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001846
1847 return rc;
1848}
1849
Avi Kivitydd856ef2012-08-27 23:46:17 +03001850int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1851{
1852 int rc;
1853
1854 invalidate_registers(ctxt);
1855 rc = __emulate_int_real(ctxt, irq);
1856 if (rc == X86EMUL_CONTINUE)
1857 writeback_registers(ctxt);
1858 return rc;
1859}
1860
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001861static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001862{
1863 switch(ctxt->mode) {
1864 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001865 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001866 case X86EMUL_MODE_VM86:
1867 case X86EMUL_MODE_PROT16:
1868 case X86EMUL_MODE_PROT32:
1869 case X86EMUL_MODE_PROT64:
1870 default:
1871 /* Protected mode interrupts unimplemented yet */
1872 return X86EMUL_UNHANDLEABLE;
1873 }
1874}
1875
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001876static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001877{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001878 int rc = X86EMUL_CONTINUE;
1879 unsigned long temp_eip = 0;
1880 unsigned long temp_eflags = 0;
1881 unsigned long cs = 0;
1882 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1883 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1884 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1885 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1886
1887 /* TODO: Add stack limit check */
1888
Avi Kivity9dac77f2011-06-01 15:34:25 +03001889 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001890
1891 if (rc != X86EMUL_CONTINUE)
1892 return rc;
1893
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001894 if (temp_eip & ~0xffff)
1895 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001896
Avi Kivity9dac77f2011-06-01 15:34:25 +03001897 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001898
1899 if (rc != X86EMUL_CONTINUE)
1900 return rc;
1901
Avi Kivity9dac77f2011-06-01 15:34:25 +03001902 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001903
1904 if (rc != X86EMUL_CONTINUE)
1905 return rc;
1906
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001907 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001908
1909 if (rc != X86EMUL_CONTINUE)
1910 return rc;
1911
Avi Kivity9dac77f2011-06-01 15:34:25 +03001912 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001913
1914
Avi Kivity9dac77f2011-06-01 15:34:25 +03001915 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001916 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001917 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001918 ctxt->eflags &= ~0xffff;
1919 ctxt->eflags |= temp_eflags;
1920 }
1921
1922 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1923 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1924
1925 return rc;
1926}
1927
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001928static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001929{
1930 switch(ctxt->mode) {
1931 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001932 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001933 case X86EMUL_MODE_VM86:
1934 case X86EMUL_MODE_PROT16:
1935 case X86EMUL_MODE_PROT32:
1936 case X86EMUL_MODE_PROT64:
1937 default:
1938 /* iret from protected mode unimplemented yet */
1939 return X86EMUL_UNHANDLEABLE;
1940 }
1941}
1942
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001943static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1944{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001945 int rc;
1946 unsigned short sel;
1947
Avi Kivity9dac77f2011-06-01 15:34:25 +03001948 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001949
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001950 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001951 if (rc != X86EMUL_CONTINUE)
1952 return rc;
1953
Avi Kivity9dac77f2011-06-01 15:34:25 +03001954 ctxt->_eip = 0;
1955 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001956 return X86EMUL_CONTINUE;
1957}
1958
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001959static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001960{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001961 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001962
Avi Kivity9dac77f2011-06-01 15:34:25 +03001963 switch (ctxt->modrm_reg) {
Mohammed Gamald19292e2008-09-08 21:47:19 +03001964 case 2: /* call near abs */ {
1965 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001966 old_eip = ctxt->_eip;
1967 ctxt->_eip = ctxt->src.val;
1968 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001969 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001970 break;
1971 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001972 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001973 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001974 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001975 case 5: /* jmp far */
1976 rc = em_jmp_far(ctxt);
1977 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001978 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001979 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001980 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001981 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001982 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001983}
1984
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09001985static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001986{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001987 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001988
Avi Kivitydd856ef2012-08-27 23:46:17 +03001989 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
1990 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
1991 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
1992 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001993 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001994 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001995 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
1996 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001997
Laurent Vivier05f086f2007-09-24 11:10:55 +02001998 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001999 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002000 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002001}
2002
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002003static int em_ret(struct x86_emulate_ctxt *ctxt)
2004{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002005 ctxt->dst.type = OP_REG;
2006 ctxt->dst.addr.reg = &ctxt->_eip;
2007 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002008 return em_pop(ctxt);
2009}
2010
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002011static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002012{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002013 int rc;
2014 unsigned long cs;
2015
Avi Kivity9dac77f2011-06-01 15:34:25 +03002016 rc = emulate_pop(ctxt, &ctxt->_eip, 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;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002019 if (ctxt->op_bytes == 4)
2020 ctxt->_eip = (u32)ctxt->_eip;
2021 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002022 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002023 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002024 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002025 return rc;
2026}
2027
Bruce Rogers32611072013-09-09 09:40:20 -06002028static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2029{
2030 int rc;
2031
2032 rc = em_ret_far(ctxt);
2033 if (rc != X86EMUL_CONTINUE)
2034 return rc;
2035 rsp_increment(ctxt, ctxt->src.val);
2036 return X86EMUL_CONTINUE;
2037}
2038
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002039static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2040{
2041 /* Save real source value, then compare EAX against destination. */
2042 ctxt->src.orig_val = ctxt->src.val;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002043 ctxt->src.val = reg_read(ctxt, VCPU_REGS_RAX);
Avi Kivity158de572013-01-19 19:51:57 +02002044 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002045
2046 if (ctxt->eflags & EFLG_ZF) {
2047 /* Success: write back to memory. */
2048 ctxt->dst.val = ctxt->src.orig_val;
2049 } else {
2050 /* Failure: write the value we saw to EAX. */
2051 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002052 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002053 }
2054 return X86EMUL_CONTINUE;
2055}
2056
Avi Kivityd4b43252011-09-13 10:45:50 +03002057static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002058{
Avi Kivityd4b43252011-09-13 10:45:50 +03002059 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002060 unsigned short sel;
2061 int rc;
2062
Avi Kivity9dac77f2011-06-01 15:34:25 +03002063 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002064
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002065 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002066 if (rc != X86EMUL_CONTINUE)
2067 return rc;
2068
Avi Kivity9dac77f2011-06-01 15:34:25 +03002069 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002070 return rc;
2071}
2072
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002073static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002074setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002075 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002076{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002077 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002078 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002079 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002080 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002081 cs->type = 0x0b; /* Read, Execute, Accessed */
2082 cs->s = 1;
2083 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002084 cs->p = 1;
2085 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002086 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002087
Gleb Natapov79168fd2010-04-28 19:15:30 +03002088 set_desc_base(ss, 0); /* flat segment */
2089 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002090 ss->g = 1; /* 4kb granularity */
2091 ss->s = 1;
2092 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002093 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002094 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002095 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002096 ss->l = 0;
2097 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002098}
2099
Avi Kivity1a18a692012-02-01 12:23:21 +02002100static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2101{
2102 u32 eax, ebx, ecx, edx;
2103
2104 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002105 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2106 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002107 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2108 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2109}
2110
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002111static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2112{
Mathias Krause0225fb52012-08-30 01:30:16 +02002113 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002114 u32 eax, ebx, ecx, edx;
2115
2116 /*
2117 * syscall should always be enabled in longmode - so only become
2118 * vendor specific (cpuid) if other modes are active...
2119 */
2120 if (ctxt->mode == X86EMUL_MODE_PROT64)
2121 return true;
2122
2123 eax = 0x00000000;
2124 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002125 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2126 /*
2127 * Intel ("GenuineIntel")
2128 * remark: Intel CPUs only support "syscall" in 64bit
2129 * longmode. Also an 64bit guest with a
2130 * 32bit compat-app running will #UD !! While this
2131 * behaviour can be fixed (by emulating) into AMD
2132 * response - CPUs of AMD can't behave like Intel.
2133 */
2134 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2135 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2136 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2137 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002138
Avi Kivity0017f932012-06-07 14:10:16 +03002139 /* AMD ("AuthenticAMD") */
2140 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2141 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2142 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2143 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002144
Avi Kivity0017f932012-06-07 14:10:16 +03002145 /* AMD ("AMDisbetter!") */
2146 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2147 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2148 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2149 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002150
2151 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2152 return false;
2153}
2154
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002155static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002156{
Mathias Krause0225fb52012-08-30 01:30:16 +02002157 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002158 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002159 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002160 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002161 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002162
2163 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002164 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002165 ctxt->mode == X86EMUL_MODE_VM86)
2166 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002167
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002168 if (!(em_syscall_is_enabled(ctxt)))
2169 return emulate_ud(ctxt);
2170
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002171 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002172 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002173
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002174 if (!(efer & EFER_SCE))
2175 return emulate_ud(ctxt);
2176
Avi Kivity717746e2011-04-20 13:37:53 +03002177 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002178 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002179 cs_sel = (u16)(msr_data & 0xfffc);
2180 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002181
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002182 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002183 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002184 cs.l = 1;
2185 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002186 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2187 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002188
Avi Kivitydd856ef2012-08-27 23:46:17 +03002189 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002190 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002191#ifdef CONFIG_X86_64
Avi Kivitydd856ef2012-08-27 23:46:17 +03002192 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002193
Avi Kivity717746e2011-04-20 13:37:53 +03002194 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002195 ctxt->mode == X86EMUL_MODE_PROT64 ?
2196 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002197 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002198
Avi Kivity717746e2011-04-20 13:37:53 +03002199 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002200 ctxt->eflags &= ~(msr_data | EFLG_RF);
2201#endif
2202 } else {
2203 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002204 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002205 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002206
2207 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2208 }
2209
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002210 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002211}
2212
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002213static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002214{
Mathias Krause0225fb52012-08-30 01:30:16 +02002215 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002216 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002217 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002218 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002219 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002220
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002221 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002222 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002223 if (ctxt->mode == X86EMUL_MODE_REAL)
2224 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002225
Avi Kivity1a18a692012-02-01 12:23:21 +02002226 /*
2227 * Not recognized on AMD in compat mode (but is recognized in legacy
2228 * mode).
2229 */
2230 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2231 && !vendor_intel(ctxt))
2232 return emulate_ud(ctxt);
2233
Andre Przywara8c604352009-06-18 12:56:01 +02002234 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2235 * Therefore, we inject an #UD.
2236 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002237 if (ctxt->mode == X86EMUL_MODE_PROT64)
2238 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002239
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002240 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002241
Avi Kivity717746e2011-04-20 13:37:53 +03002242 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002243 switch (ctxt->mode) {
2244 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002245 if ((msr_data & 0xfffc) == 0x0)
2246 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002247 break;
2248 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002249 if (msr_data == 0x0)
2250 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002251 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002252 default:
2253 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002254 }
2255
2256 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002257 cs_sel = (u16)msr_data;
2258 cs_sel &= ~SELECTOR_RPL_MASK;
2259 ss_sel = cs_sel + 8;
2260 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002261 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002262 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002263 cs.l = 1;
2264 }
2265
Avi Kivity1aa36612011-04-27 13:20:30 +03002266 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2267 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002268
Avi Kivity717746e2011-04-20 13:37:53 +03002269 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002270 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002271
Avi Kivity717746e2011-04-20 13:37:53 +03002272 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002273 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002274
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002275 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002276}
2277
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002278static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002279{
Mathias Krause0225fb52012-08-30 01:30:16 +02002280 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002281 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002282 u64 msr_data;
2283 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002284 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002285
Gleb Natapova0044752010-02-10 14:21:31 +02002286 /* inject #GP if in real mode or Virtual 8086 mode */
2287 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002288 ctxt->mode == X86EMUL_MODE_VM86)
2289 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002290
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002291 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002292
Avi Kivity9dac77f2011-06-01 15:34:25 +03002293 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002294 usermode = X86EMUL_MODE_PROT64;
2295 else
2296 usermode = X86EMUL_MODE_PROT32;
2297
2298 cs.dpl = 3;
2299 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002300 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002301 switch (usermode) {
2302 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002303 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002304 if ((msr_data & 0xfffc) == 0x0)
2305 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002306 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002307 break;
2308 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002309 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002310 if (msr_data == 0x0)
2311 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002312 ss_sel = cs_sel + 8;
2313 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002314 cs.l = 1;
2315 break;
2316 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002317 cs_sel |= SELECTOR_RPL_MASK;
2318 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002319
Avi Kivity1aa36612011-04-27 13:20:30 +03002320 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2321 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002322
Avi Kivitydd856ef2012-08-27 23:46:17 +03002323 ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX);
2324 *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX);
Andre Przywara4668f052009-06-18 12:56:02 +02002325
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002326 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002327}
2328
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002329static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002330{
2331 int iopl;
2332 if (ctxt->mode == X86EMUL_MODE_REAL)
2333 return false;
2334 if (ctxt->mode == X86EMUL_MODE_VM86)
2335 return true;
2336 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002337 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002338}
2339
2340static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002341 u16 port, u16 len)
2342{
Mathias Krause0225fb52012-08-30 01:30:16 +02002343 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002344 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002345 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002346 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002347 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002348 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002349 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002350
Avi Kivity1aa36612011-04-27 13:20:30 +03002351 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002352 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002353 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002354 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002355 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002356 base = get_desc_base(&tr_seg);
2357#ifdef CONFIG_X86_64
2358 base |= ((u64)base3) << 32;
2359#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002360 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002361 if (r != X86EMUL_CONTINUE)
2362 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002363 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002364 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002365 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002366 if (r != X86EMUL_CONTINUE)
2367 return false;
2368 if ((perm >> bit_idx) & mask)
2369 return false;
2370 return true;
2371}
2372
2373static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002374 u16 port, u16 len)
2375{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002376 if (ctxt->perm_ok)
2377 return true;
2378
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002379 if (emulator_bad_iopl(ctxt))
2380 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002381 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002382
2383 ctxt->perm_ok = true;
2384
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002385 return true;
2386}
2387
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002388static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002389 struct tss_segment_16 *tss)
2390{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002391 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002392 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002393 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2394 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2395 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2396 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2397 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2398 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2399 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2400 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002401
Avi Kivity1aa36612011-04-27 13:20:30 +03002402 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2403 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2404 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2405 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2406 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002407}
2408
2409static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002410 struct tss_segment_16 *tss)
2411{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002412 int ret;
2413
Avi Kivity9dac77f2011-06-01 15:34:25 +03002414 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002415 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002416 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2417 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2418 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2419 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2420 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2421 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2422 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2423 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002424
2425 /*
2426 * SDM says that segment selectors are loaded before segment
2427 * descriptors
2428 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002429 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2430 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2431 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2432 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2433 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002434
2435 /*
Guo Chaofc058682012-06-28 15:19:51 +08002436 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002437 * it is handled in a context of new task
2438 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002439 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002440 if (ret != X86EMUL_CONTINUE)
2441 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002442 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002443 if (ret != X86EMUL_CONTINUE)
2444 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002445 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002446 if (ret != X86EMUL_CONTINUE)
2447 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002448 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002449 if (ret != X86EMUL_CONTINUE)
2450 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002451 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002452 if (ret != X86EMUL_CONTINUE)
2453 return ret;
2454
2455 return X86EMUL_CONTINUE;
2456}
2457
2458static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002459 u16 tss_selector, u16 old_tss_sel,
2460 ulong old_tss_base, struct desc_struct *new_desc)
2461{
Mathias Krause0225fb52012-08-30 01:30:16 +02002462 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002463 struct tss_segment_16 tss_seg;
2464 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002465 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002466
Avi Kivity0f65dd72011-04-20 13:37:53 +03002467 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002468 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002469 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002470 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002471 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002472
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002473 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002474
Avi Kivity0f65dd72011-04-20 13:37:53 +03002475 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002476 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002477 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002478 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002479 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002480
Avi Kivity0f65dd72011-04-20 13:37:53 +03002481 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002482 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002483 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002484 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002485 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002486
2487 if (old_tss_sel != 0xffff) {
2488 tss_seg.prev_task_link = old_tss_sel;
2489
Avi Kivity0f65dd72011-04-20 13:37:53 +03002490 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002491 &tss_seg.prev_task_link,
2492 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002493 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002494 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002495 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002496 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002497 }
2498
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002499 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002500}
2501
2502static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002503 struct tss_segment_32 *tss)
2504{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002505 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002506 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002507 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002508 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2509 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2510 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2511 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2512 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2513 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2514 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2515 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002516
Avi Kivity1aa36612011-04-27 13:20:30 +03002517 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2518 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2519 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2520 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2521 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2522 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2523 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002524}
2525
2526static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002527 struct tss_segment_32 *tss)
2528{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002529 int ret;
2530
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002531 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002532 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002533 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002534 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002535
2536 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002537 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2538 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2539 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2540 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2541 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2542 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2543 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2544 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002545
2546 /*
2547 * SDM says that segment selectors are loaded before segment
2548 * descriptors
2549 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002550 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2551 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2552 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2553 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2554 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2555 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2556 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002557
2558 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002559 * If we're switching between Protected Mode and VM86, we need to make
2560 * sure to update the mode before loading the segment descriptors so
2561 * that the selectors are interpreted correctly.
2562 *
2563 * Need to get rflags to the vcpu struct immediately because it
2564 * influences the CPL which is checked at least when loading the segment
2565 * descriptors and when pushing an error code to the new kernel stack.
2566 *
2567 * TODO Introduce a separate ctxt->ops->set_cpl callback
2568 */
2569 if (ctxt->eflags & X86_EFLAGS_VM)
2570 ctxt->mode = X86EMUL_MODE_VM86;
2571 else
2572 ctxt->mode = X86EMUL_MODE_PROT32;
2573
2574 ctxt->ops->set_rflags(ctxt, ctxt->eflags);
2575
2576 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002577 * Now load segment descriptors. If fault happenes at this stage
2578 * it is handled in a context of new task
2579 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002580 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
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->es, VCPU_SREG_ES);
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->cs, VCPU_SREG_CS);
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->ss, VCPU_SREG_SS);
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->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002593 if (ret != X86EMUL_CONTINUE)
2594 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002595 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002596 if (ret != X86EMUL_CONTINUE)
2597 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002598 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002599 if (ret != X86EMUL_CONTINUE)
2600 return ret;
2601
2602 return X86EMUL_CONTINUE;
2603}
2604
2605static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002606 u16 tss_selector, u16 old_tss_sel,
2607 ulong old_tss_base, struct desc_struct *new_desc)
2608{
Mathias Krause0225fb52012-08-30 01:30:16 +02002609 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002610 struct tss_segment_32 tss_seg;
2611 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002612 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002613
Avi Kivity0f65dd72011-04-20 13:37:53 +03002614 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002615 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002616 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002617 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002618 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002619
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002620 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002621
Avi Kivity0f65dd72011-04-20 13:37:53 +03002622 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002623 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002624 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002625 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002626 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002627
Avi Kivity0f65dd72011-04-20 13:37:53 +03002628 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002629 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002630 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002631 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002632 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002633
2634 if (old_tss_sel != 0xffff) {
2635 tss_seg.prev_task_link = old_tss_sel;
2636
Avi Kivity0f65dd72011-04-20 13:37:53 +03002637 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002638 &tss_seg.prev_task_link,
2639 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002640 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002641 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002642 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002643 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002644 }
2645
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002646 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002647}
2648
2649static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002650 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002651 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002652{
Mathias Krause0225fb52012-08-30 01:30:16 +02002653 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002654 struct desc_struct curr_tss_desc, next_tss_desc;
2655 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002656 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002657 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002658 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002659 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002660 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002661
2662 /* FIXME: old_tss_base == ~0 ? */
2663
Avi Kivitye9194642012-06-13 16:29:39 +03002664 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002665 if (ret != X86EMUL_CONTINUE)
2666 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002667 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002668 if (ret != X86EMUL_CONTINUE)
2669 return ret;
2670
2671 /* FIXME: check that next_tss_desc is tss */
2672
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002673 /*
2674 * Check privileges. The three cases are task switch caused by...
2675 *
2676 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2677 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002678 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002679 */
2680 if (reason == TASK_SWITCH_GATE) {
2681 if (idt_index != -1) {
2682 /* Software interrupts */
2683 struct desc_struct task_gate_desc;
2684 int dpl;
2685
2686 ret = read_interrupt_descriptor(ctxt, idt_index,
2687 &task_gate_desc);
2688 if (ret != X86EMUL_CONTINUE)
2689 return ret;
2690
2691 dpl = task_gate_desc.dpl;
2692 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2693 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2694 }
2695 } else if (reason != TASK_SWITCH_IRET) {
2696 int dpl = next_tss_desc.dpl;
2697 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2698 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002699 }
2700
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002701
Gleb Natapovceffb452010-03-18 15:20:19 +02002702 desc_limit = desc_limit_scaled(&next_tss_desc);
2703 if (!next_tss_desc.p ||
2704 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2705 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002706 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002707 return X86EMUL_PROPAGATE_FAULT;
2708 }
2709
2710 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2711 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002712 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002713 }
2714
2715 if (reason == TASK_SWITCH_IRET)
2716 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2717
2718 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002719 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002720 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2721 old_tss_sel = 0xffff;
2722
2723 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002724 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002725 old_tss_base, &next_tss_desc);
2726 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002727 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002728 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002729 if (ret != X86EMUL_CONTINUE)
2730 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002731
2732 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2733 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2734
2735 if (reason != TASK_SWITCH_IRET) {
2736 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002737 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002738 }
2739
Avi Kivity717746e2011-04-20 13:37:53 +03002740 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002741 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002742
Jan Kiszkae269fb22010-04-14 15:51:09 +02002743 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002744 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2745 ctxt->lock_prefix = 0;
2746 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002747 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002748 }
2749
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002750 return ret;
2751}
2752
2753int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002754 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002755 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002756{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002757 int rc;
2758
Avi Kivitydd856ef2012-08-27 23:46:17 +03002759 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002760 ctxt->_eip = ctxt->eip;
2761 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002762
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002763 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002764 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002765
Avi Kivitydd856ef2012-08-27 23:46:17 +03002766 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002767 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002768 writeback_registers(ctxt);
2769 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002770
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002771 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002772}
2773
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002774static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2775 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002776{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002777 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002778
Avi Kivitydd856ef2012-08-27 23:46:17 +03002779 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2780 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002781}
2782
Avi Kivity7af04fc2010-08-18 14:16:35 +03002783static int em_das(struct x86_emulate_ctxt *ctxt)
2784{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002785 u8 al, old_al;
2786 bool af, cf, old_cf;
2787
2788 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002789 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002790
2791 old_al = al;
2792 old_cf = cf;
2793 cf = false;
2794 af = ctxt->eflags & X86_EFLAGS_AF;
2795 if ((al & 0x0f) > 9 || af) {
2796 al -= 6;
2797 cf = old_cf | (al >= 250);
2798 af = true;
2799 } else {
2800 af = false;
2801 }
2802 if (old_al > 0x99 || old_cf) {
2803 al -= 0x60;
2804 cf = true;
2805 }
2806
Avi Kivity9dac77f2011-06-01 15:34:25 +03002807 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002808 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002809 ctxt->src.type = OP_IMM;
2810 ctxt->src.val = 0;
2811 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02002812 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03002813 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2814 if (cf)
2815 ctxt->eflags |= X86_EFLAGS_CF;
2816 if (af)
2817 ctxt->eflags |= X86_EFLAGS_AF;
2818 return X86EMUL_CONTINUE;
2819}
2820
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02002821static int em_aam(struct x86_emulate_ctxt *ctxt)
2822{
2823 u8 al, ah;
2824
2825 if (ctxt->src.val == 0)
2826 return emulate_de(ctxt);
2827
2828 al = ctxt->dst.val & 0xff;
2829 ah = al / ctxt->src.val;
2830 al %= ctxt->src.val;
2831
2832 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
2833
2834 /* Set PF, ZF, SF */
2835 ctxt->src.type = OP_IMM;
2836 ctxt->src.val = 0;
2837 ctxt->src.bytes = 1;
2838 fastop(ctxt, em_or);
2839
2840 return X86EMUL_CONTINUE;
2841}
2842
Gleb Natapov7f662272012-12-10 11:42:30 +02002843static int em_aad(struct x86_emulate_ctxt *ctxt)
2844{
2845 u8 al = ctxt->dst.val & 0xff;
2846 u8 ah = (ctxt->dst.val >> 8) & 0xff;
2847
2848 al = (al + (ah * ctxt->src.val)) & 0xff;
2849
2850 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
2851
Gleb Natapovf583c292013-02-13 17:50:39 +02002852 /* Set PF, ZF, SF */
2853 ctxt->src.type = OP_IMM;
2854 ctxt->src.val = 0;
2855 ctxt->src.bytes = 1;
2856 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02002857
2858 return X86EMUL_CONTINUE;
2859}
2860
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002861static int em_call(struct x86_emulate_ctxt *ctxt)
2862{
2863 long rel = ctxt->src.val;
2864
2865 ctxt->src.val = (unsigned long)ctxt->_eip;
2866 jmp_rel(ctxt, rel);
2867 return em_push(ctxt);
2868}
2869
Avi Kivity0ef753b2010-08-18 14:51:45 +03002870static int em_call_far(struct x86_emulate_ctxt *ctxt)
2871{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002872 u16 sel, old_cs;
2873 ulong old_eip;
2874 int rc;
2875
Avi Kivity1aa36612011-04-27 13:20:30 +03002876 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002877 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002878
Avi Kivity9dac77f2011-06-01 15:34:25 +03002879 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002880 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002881 return X86EMUL_CONTINUE;
2882
Avi Kivity9dac77f2011-06-01 15:34:25 +03002883 ctxt->_eip = 0;
2884 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002885
Avi Kivity9dac77f2011-06-01 15:34:25 +03002886 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002887 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002888 if (rc != X86EMUL_CONTINUE)
2889 return rc;
2890
Avi Kivity9dac77f2011-06-01 15:34:25 +03002891 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002892 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002893}
2894
Avi Kivity40ece7c2010-08-18 15:12:09 +03002895static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2896{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002897 int rc;
2898
Avi Kivity9dac77f2011-06-01 15:34:25 +03002899 ctxt->dst.type = OP_REG;
2900 ctxt->dst.addr.reg = &ctxt->_eip;
2901 ctxt->dst.bytes = ctxt->op_bytes;
2902 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002903 if (rc != X86EMUL_CONTINUE)
2904 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03002905 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002906 return X86EMUL_CONTINUE;
2907}
2908
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002909static int em_xchg(struct x86_emulate_ctxt *ctxt)
2910{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002911 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002912 ctxt->src.val = ctxt->dst.val;
2913 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002914
2915 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002916 ctxt->dst.val = ctxt->src.orig_val;
2917 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002918 return X86EMUL_CONTINUE;
2919}
2920
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002921static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2922{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002923 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02002924 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002925}
2926
Avi Kivity61429142010-08-19 15:13:00 +03002927static int em_cwd(struct x86_emulate_ctxt *ctxt)
2928{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002929 ctxt->dst.type = OP_REG;
2930 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002931 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002932 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002933
2934 return X86EMUL_CONTINUE;
2935}
2936
Avi Kivity48bb5d32010-08-18 18:54:34 +03002937static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2938{
Avi Kivity48bb5d32010-08-18 18:54:34 +03002939 u64 tsc = 0;
2940
Avi Kivity717746e2011-04-20 13:37:53 +03002941 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002942 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
2943 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03002944 return X86EMUL_CONTINUE;
2945}
2946
Avi Kivity222d21a2011-11-10 14:57:30 +02002947static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
2948{
2949 u64 pmc;
2950
Avi Kivitydd856ef2012-08-27 23:46:17 +03002951 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02002952 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002953 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
2954 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02002955 return X86EMUL_CONTINUE;
2956}
2957
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002958static int em_mov(struct x86_emulate_ctxt *ctxt)
2959{
Stefan Hajnoczi49597d82012-04-09 18:40:00 +03002960 memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002961 return X86EMUL_CONTINUE;
2962}
2963
Borislav Petkov84cffe42013-10-29 12:54:56 +01002964#define FFL(x) bit(X86_FEATURE_##x)
2965
2966static int em_movbe(struct x86_emulate_ctxt *ctxt)
2967{
2968 u32 ebx, ecx, edx, eax = 1;
2969 u16 tmp;
2970
2971 /*
2972 * Check MOVBE is set in the guest-visible CPUID leaf.
2973 */
2974 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2975 if (!(ecx & FFL(MOVBE)))
2976 return emulate_ud(ctxt);
2977
2978 switch (ctxt->op_bytes) {
2979 case 2:
2980 /*
2981 * From MOVBE definition: "...When the operand size is 16 bits,
2982 * the upper word of the destination register remains unchanged
2983 * ..."
2984 *
2985 * Both casting ->valptr and ->val to u16 breaks strict aliasing
2986 * rules so we have to do the operation almost per hand.
2987 */
2988 tmp = (u16)ctxt->src.val;
2989 ctxt->dst.val &= ~0xffffUL;
2990 ctxt->dst.val |= (unsigned long)swab16(tmp);
2991 break;
2992 case 4:
2993 ctxt->dst.val = swab32((u32)ctxt->src.val);
2994 break;
2995 case 8:
2996 ctxt->dst.val = swab64(ctxt->src.val);
2997 break;
2998 default:
2999 return X86EMUL_PROPAGATE_FAULT;
3000 }
3001 return X86EMUL_CONTINUE;
3002}
3003
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003004static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3005{
3006 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3007 return emulate_gp(ctxt, 0);
3008
3009 /* Disable writeback. */
3010 ctxt->dst.type = OP_NONE;
3011 return X86EMUL_CONTINUE;
3012}
3013
3014static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3015{
3016 unsigned long val;
3017
3018 if (ctxt->mode == X86EMUL_MODE_PROT64)
3019 val = ctxt->src.val & ~0ULL;
3020 else
3021 val = ctxt->src.val & ~0U;
3022
3023 /* #UD condition is already handled. */
3024 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3025 return emulate_gp(ctxt, 0);
3026
3027 /* Disable writeback. */
3028 ctxt->dst.type = OP_NONE;
3029 return X86EMUL_CONTINUE;
3030}
3031
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003032static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3033{
3034 u64 msr_data;
3035
Avi Kivitydd856ef2012-08-27 23:46:17 +03003036 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3037 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3038 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003039 return emulate_gp(ctxt, 0);
3040
3041 return X86EMUL_CONTINUE;
3042}
3043
3044static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3045{
3046 u64 msr_data;
3047
Avi Kivitydd856ef2012-08-27 23:46:17 +03003048 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003049 return emulate_gp(ctxt, 0);
3050
Avi Kivitydd856ef2012-08-27 23:46:17 +03003051 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3052 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003053 return X86EMUL_CONTINUE;
3054}
3055
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003056static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3057{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003058 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003059 return emulate_ud(ctxt);
3060
Avi Kivity9dac77f2011-06-01 15:34:25 +03003061 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003062 return X86EMUL_CONTINUE;
3063}
3064
3065static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3066{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003067 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003068
Avi Kivity9dac77f2011-06-01 15:34:25 +03003069 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003070 return emulate_ud(ctxt);
3071
Avi Kivity9dac77f2011-06-01 15:34:25 +03003072 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003073 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3074
3075 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003076 ctxt->dst.type = OP_NONE;
3077 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003078}
3079
Avi Kivitya14e5792012-06-13 12:28:33 +03003080static int em_lldt(struct x86_emulate_ctxt *ctxt)
3081{
3082 u16 sel = ctxt->src.val;
3083
3084 /* Disable writeback. */
3085 ctxt->dst.type = OP_NONE;
3086 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3087}
3088
Avi Kivity80890002012-06-13 16:33:29 +03003089static int em_ltr(struct x86_emulate_ctxt *ctxt)
3090{
3091 u16 sel = ctxt->src.val;
3092
3093 /* Disable writeback. */
3094 ctxt->dst.type = OP_NONE;
3095 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3096}
3097
Avi Kivity38503912011-03-31 18:48:09 +02003098static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3099{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003100 int rc;
3101 ulong linear;
3102
Avi Kivity9dac77f2011-06-01 15:34:25 +03003103 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003104 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003105 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003106 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003107 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003108 return X86EMUL_CONTINUE;
3109}
3110
Avi Kivity2d04a052011-04-20 15:32:49 +03003111static int em_clts(struct x86_emulate_ctxt *ctxt)
3112{
3113 ulong cr0;
3114
3115 cr0 = ctxt->ops->get_cr(ctxt, 0);
3116 cr0 &= ~X86_CR0_TS;
3117 ctxt->ops->set_cr(ctxt, 0, cr0);
3118 return X86EMUL_CONTINUE;
3119}
3120
Avi Kivity26d05cc2011-04-21 12:07:59 +03003121static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3122{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003123 int rc;
3124
Avi Kivity9dac77f2011-06-01 15:34:25 +03003125 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003126 return X86EMUL_UNHANDLEABLE;
3127
3128 rc = ctxt->ops->fix_hypercall(ctxt);
3129 if (rc != X86EMUL_CONTINUE)
3130 return rc;
3131
3132 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003133 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003134 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003135 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003136 return X86EMUL_CONTINUE;
3137}
3138
Avi Kivity96051572012-06-10 17:21:18 +03003139static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3140 void (*get)(struct x86_emulate_ctxt *ctxt,
3141 struct desc_ptr *ptr))
3142{
3143 struct desc_ptr desc_ptr;
3144
3145 if (ctxt->mode == X86EMUL_MODE_PROT64)
3146 ctxt->op_bytes = 8;
3147 get(ctxt, &desc_ptr);
3148 if (ctxt->op_bytes == 2) {
3149 ctxt->op_bytes = 4;
3150 desc_ptr.address &= 0x00ffffff;
3151 }
3152 /* Disable writeback. */
3153 ctxt->dst.type = OP_NONE;
3154 return segmented_write(ctxt, ctxt->dst.addr.mem,
3155 &desc_ptr, 2 + ctxt->op_bytes);
3156}
3157
3158static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3159{
3160 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3161}
3162
3163static int em_sidt(struct x86_emulate_ctxt *ctxt)
3164{
3165 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3166}
3167
Avi Kivity26d05cc2011-04-21 12:07:59 +03003168static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3169{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003170 struct desc_ptr desc_ptr;
3171 int rc;
3172
Avi Kivity510425f2012-06-07 17:04:36 +03003173 if (ctxt->mode == X86EMUL_MODE_PROT64)
3174 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003175 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003176 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003177 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003178 if (rc != X86EMUL_CONTINUE)
3179 return rc;
3180 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3181 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003182 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003183 return X86EMUL_CONTINUE;
3184}
3185
Avi Kivity5ef39c72011-04-21 12:21:50 +03003186static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003187{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003188 int rc;
3189
Avi Kivity5ef39c72011-04-21 12:21:50 +03003190 rc = ctxt->ops->fix_hypercall(ctxt);
3191
Avi Kivity26d05cc2011-04-21 12:07:59 +03003192 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003193 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003194 return rc;
3195}
3196
3197static int em_lidt(struct x86_emulate_ctxt *ctxt)
3198{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003199 struct desc_ptr desc_ptr;
3200 int rc;
3201
Avi Kivity510425f2012-06-07 17:04:36 +03003202 if (ctxt->mode == X86EMUL_MODE_PROT64)
3203 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003204 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003205 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003206 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003207 if (rc != X86EMUL_CONTINUE)
3208 return rc;
3209 ctxt->ops->set_idt(ctxt, &desc_ptr);
3210 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003211 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003212 return X86EMUL_CONTINUE;
3213}
3214
3215static int em_smsw(struct x86_emulate_ctxt *ctxt)
3216{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003217 ctxt->dst.bytes = 2;
3218 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003219 return X86EMUL_CONTINUE;
3220}
3221
3222static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3223{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003224 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003225 | (ctxt->src.val & 0x0f));
3226 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003227 return X86EMUL_CONTINUE;
3228}
3229
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003230static int em_loop(struct x86_emulate_ctxt *ctxt)
3231{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003232 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3233 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003234 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3235 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003236
3237 return X86EMUL_CONTINUE;
3238}
3239
3240static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3241{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003242 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003243 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003244
3245 return X86EMUL_CONTINUE;
3246}
3247
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003248static int em_in(struct x86_emulate_ctxt *ctxt)
3249{
3250 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3251 &ctxt->dst.val))
3252 return X86EMUL_IO_NEEDED;
3253
3254 return X86EMUL_CONTINUE;
3255}
3256
3257static int em_out(struct x86_emulate_ctxt *ctxt)
3258{
3259 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3260 &ctxt->src.val, 1);
3261 /* Disable writeback. */
3262 ctxt->dst.type = OP_NONE;
3263 return X86EMUL_CONTINUE;
3264}
3265
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003266static int em_cli(struct x86_emulate_ctxt *ctxt)
3267{
3268 if (emulator_bad_iopl(ctxt))
3269 return emulate_gp(ctxt, 0);
3270
3271 ctxt->eflags &= ~X86_EFLAGS_IF;
3272 return X86EMUL_CONTINUE;
3273}
3274
3275static int em_sti(struct x86_emulate_ctxt *ctxt)
3276{
3277 if (emulator_bad_iopl(ctxt))
3278 return emulate_gp(ctxt, 0);
3279
3280 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3281 ctxt->eflags |= X86_EFLAGS_IF;
3282 return X86EMUL_CONTINUE;
3283}
3284
Avi Kivity6d6eede2012-06-07 14:11:36 +03003285static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3286{
3287 u32 eax, ebx, ecx, edx;
3288
Avi Kivitydd856ef2012-08-27 23:46:17 +03003289 eax = reg_read(ctxt, VCPU_REGS_RAX);
3290 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003291 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003292 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3293 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3294 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3295 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003296 return X86EMUL_CONTINUE;
3297}
3298
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003299static int em_lahf(struct x86_emulate_ctxt *ctxt)
3300{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003301 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3302 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003303 return X86EMUL_CONTINUE;
3304}
3305
Avi Kivity92998362012-06-13 12:25:06 +03003306static int em_bswap(struct x86_emulate_ctxt *ctxt)
3307{
3308 switch (ctxt->op_bytes) {
3309#ifdef CONFIG_X86_64
3310 case 8:
3311 asm("bswap %0" : "+r"(ctxt->dst.val));
3312 break;
3313#endif
3314 default:
3315 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3316 break;
3317 }
3318 return X86EMUL_CONTINUE;
3319}
3320
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003321static bool valid_cr(int nr)
3322{
3323 switch (nr) {
3324 case 0:
3325 case 2 ... 4:
3326 case 8:
3327 return true;
3328 default:
3329 return false;
3330 }
3331}
3332
3333static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3334{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003335 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003336 return emulate_ud(ctxt);
3337
3338 return X86EMUL_CONTINUE;
3339}
3340
3341static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3342{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003343 u64 new_val = ctxt->src.val64;
3344 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003345 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003346
3347 static u64 cr_reserved_bits[] = {
3348 0xffffffff00000000ULL,
3349 0, 0, 0, /* CR3 checked later */
3350 CR4_RESERVED_BITS,
3351 0, 0, 0,
3352 CR8_RESERVED_BITS,
3353 };
3354
3355 if (!valid_cr(cr))
3356 return emulate_ud(ctxt);
3357
3358 if (new_val & cr_reserved_bits[cr])
3359 return emulate_gp(ctxt, 0);
3360
3361 switch (cr) {
3362 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003363 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003364 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3365 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3366 return emulate_gp(ctxt, 0);
3367
Avi Kivity717746e2011-04-20 13:37:53 +03003368 cr4 = ctxt->ops->get_cr(ctxt, 4);
3369 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003370
3371 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3372 !(cr4 & X86_CR4_PAE))
3373 return emulate_gp(ctxt, 0);
3374
3375 break;
3376 }
3377 case 3: {
3378 u64 rsvd = 0;
3379
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003380 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3381 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003382 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003383 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003384 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003385 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003386 rsvd = CR3_NONPAE_RESERVED_BITS;
3387
3388 if (new_val & rsvd)
3389 return emulate_gp(ctxt, 0);
3390
3391 break;
3392 }
3393 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003394 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003395
3396 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3397 return emulate_gp(ctxt, 0);
3398
3399 break;
3400 }
3401 }
3402
3403 return X86EMUL_CONTINUE;
3404}
3405
Joerg Roedel3b88e412011-04-04 12:39:29 +02003406static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3407{
3408 unsigned long dr7;
3409
Avi Kivity717746e2011-04-20 13:37:53 +03003410 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003411
3412 /* Check if DR7.Global_Enable is set */
3413 return dr7 & (1 << 13);
3414}
3415
3416static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3417{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003418 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003419 u64 cr4;
3420
3421 if (dr > 7)
3422 return emulate_ud(ctxt);
3423
Avi Kivity717746e2011-04-20 13:37:53 +03003424 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003425 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3426 return emulate_ud(ctxt);
3427
3428 if (check_dr7_gd(ctxt))
3429 return emulate_db(ctxt);
3430
3431 return X86EMUL_CONTINUE;
3432}
3433
3434static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3435{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003436 u64 new_val = ctxt->src.val64;
3437 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003438
3439 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3440 return emulate_gp(ctxt, 0);
3441
3442 return check_dr_read(ctxt);
3443}
3444
Joerg Roedel01de8b02011-04-04 12:39:31 +02003445static int check_svme(struct x86_emulate_ctxt *ctxt)
3446{
3447 u64 efer;
3448
Avi Kivity717746e2011-04-20 13:37:53 +03003449 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003450
3451 if (!(efer & EFER_SVME))
3452 return emulate_ud(ctxt);
3453
3454 return X86EMUL_CONTINUE;
3455}
3456
3457static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3458{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003459 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003460
3461 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003462 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003463 return emulate_gp(ctxt, 0);
3464
3465 return check_svme(ctxt);
3466}
3467
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003468static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3469{
Avi Kivity717746e2011-04-20 13:37:53 +03003470 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003471
Avi Kivity717746e2011-04-20 13:37:53 +03003472 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003473 return emulate_ud(ctxt);
3474
3475 return X86EMUL_CONTINUE;
3476}
3477
Joerg Roedel80612522011-04-04 12:39:33 +02003478static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3479{
Avi Kivity717746e2011-04-20 13:37:53 +03003480 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003481 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003482
Avi Kivity717746e2011-04-20 13:37:53 +03003483 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02003484 (rcx > 3))
3485 return emulate_gp(ctxt, 0);
3486
3487 return X86EMUL_CONTINUE;
3488}
3489
Joerg Roedelf6511932011-04-04 12:39:35 +02003490static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3491{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003492 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3493 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003494 return emulate_gp(ctxt, 0);
3495
3496 return X86EMUL_CONTINUE;
3497}
3498
3499static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3500{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003501 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3502 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003503 return emulate_gp(ctxt, 0);
3504
3505 return X86EMUL_CONTINUE;
3506}
3507
Avi Kivity73fba5f2010-07-29 15:11:53 +03003508#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003509#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003510#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3511 .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003512#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003513#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003514#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3515#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003516#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003517#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003518#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003519#define II(_f, _e, _i) \
3520 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003521#define IIP(_f, _e, _i, _p) \
3522 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3523 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003524#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003525
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003526#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003527#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003528#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003529#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003530#define I2bvIP(_f, _e, _i, _p) \
3531 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003532
Avi Kivityfb864fb2013-01-04 16:18:54 +02003533#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3534 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3535 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003536
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003537static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003538 DI(SrcNone | Priv, monitor),
3539 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003540 N, N, N, N, N, N,
3541};
3542
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003543static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003544 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003545 II(SrcNone | Prot | EmulateOnUD, em_vmmcall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003546 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3547 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3548 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3549 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3550 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3551 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003552};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003553
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003554static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003555 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003556 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003557 N, N, N, N, N, N,
3558};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003559
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003560static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003561 F(Lock, em_add),
3562 F(Lock | PageTable, em_or),
3563 F(Lock, em_adc),
3564 F(Lock, em_sbb),
3565 F(Lock | PageTable, em_and),
3566 F(Lock, em_sub),
3567 F(Lock, em_xor),
3568 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003569};
3570
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003571static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003572 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003573};
3574
Avi Kivity007a3b52013-01-19 19:51:51 +02003575static const struct opcode group2[] = {
3576 F(DstMem | ModRM, em_rol),
3577 F(DstMem | ModRM, em_ror),
3578 F(DstMem | ModRM, em_rcl),
3579 F(DstMem | ModRM, em_rcr),
3580 F(DstMem | ModRM, em_shl),
3581 F(DstMem | ModRM, em_shr),
3582 F(DstMem | ModRM, em_shl),
3583 F(DstMem | ModRM, em_sar),
3584};
3585
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003586static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003587 F(DstMem | SrcImm | NoWrite, em_test),
3588 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003589 F(DstMem | SrcNone | Lock, em_not),
3590 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02003591 F(DstXacc | Src2Mem, em_mul_ex),
3592 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02003593 F(DstXacc | Src2Mem, em_div_ex),
3594 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003595};
3596
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003597static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003598 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3599 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003600 N, N, N, N, N, N,
3601};
3602
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003603static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003604 F(DstMem | SrcNone | Lock, em_inc),
3605 F(DstMem | SrcNone | Lock, em_dec),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003606 I(SrcMem | Stack, em_grp45),
3607 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3608 I(SrcMem | Stack, em_grp45),
3609 I(SrcMemFAddr | ImplicitOps, em_grp45),
Gleb Natapov188424b2013-04-11 12:32:14 +03003610 I(SrcMem | Stack, em_grp45), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003611};
3612
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003613static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003614 DI(Prot, sldt),
3615 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003616 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003617 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003618 N, N, N, N,
3619};
3620
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003621static const struct group_dual group7 = { {
Avi Kivity96051572012-06-10 17:21:18 +03003622 II(Mov | DstMem | Priv, em_sgdt, sgdt),
3623 II(Mov | DstMem | Priv, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003624 II(SrcMem | Priv, em_lgdt, lgdt),
3625 II(SrcMem | Priv, em_lidt, lidt),
3626 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3627 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3628 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003629}, {
Borislav Petkovb51e9742013-09-22 16:44:52 +02003630 I(SrcNone | Priv | EmulateOnUD, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003631 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003632 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003633 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3634 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3635 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003636} };
3637
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003638static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003639 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003640 F(DstMem | SrcImmByte | NoWrite, em_bt),
3641 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3642 F(DstMem | SrcImmByte | Lock, em_btr),
3643 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003644};
3645
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003646static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003647 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003648}, {
3649 N, N, N, N, N, N, N, N,
3650} };
3651
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003652static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003653 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003654 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003655};
3656
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003657static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003658 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003659};
3660
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003661static const struct gprefix pfx_vmovntpx = {
Avi Kivity3e114eb2012-04-09 18:40:01 +03003662 I(0, em_mov), N, N, N,
3663};
3664
Gleb Natapov045a2822012-12-20 16:57:43 +02003665static const struct escape escape_d9 = { {
3666 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3667}, {
3668 /* 0xC0 - 0xC7 */
3669 N, N, N, N, N, N, N, N,
3670 /* 0xC8 - 0xCF */
3671 N, N, N, N, N, N, N, N,
3672 /* 0xD0 - 0xC7 */
3673 N, N, N, N, N, N, N, N,
3674 /* 0xD8 - 0xDF */
3675 N, N, N, N, N, N, N, N,
3676 /* 0xE0 - 0xE7 */
3677 N, N, N, N, N, N, N, N,
3678 /* 0xE8 - 0xEF */
3679 N, N, N, N, N, N, N, N,
3680 /* 0xF0 - 0xF7 */
3681 N, N, N, N, N, N, N, N,
3682 /* 0xF8 - 0xFF */
3683 N, N, N, N, N, N, N, N,
3684} };
3685
3686static const struct escape escape_db = { {
3687 N, N, N, N, N, N, N, N,
3688}, {
3689 /* 0xC0 - 0xC7 */
3690 N, N, N, N, N, N, N, N,
3691 /* 0xC8 - 0xCF */
3692 N, N, N, N, N, N, N, N,
3693 /* 0xD0 - 0xC7 */
3694 N, N, N, N, N, N, N, N,
3695 /* 0xD8 - 0xDF */
3696 N, N, N, N, N, N, N, N,
3697 /* 0xE0 - 0xE7 */
3698 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3699 /* 0xE8 - 0xEF */
3700 N, N, N, N, N, N, N, N,
3701 /* 0xF0 - 0xF7 */
3702 N, N, N, N, N, N, N, N,
3703 /* 0xF8 - 0xFF */
3704 N, N, N, N, N, N, N, N,
3705} };
3706
3707static const struct escape escape_dd = { {
3708 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3709}, {
3710 /* 0xC0 - 0xC7 */
3711 N, N, N, N, N, N, N, N,
3712 /* 0xC8 - 0xCF */
3713 N, N, N, N, N, N, N, N,
3714 /* 0xD0 - 0xC7 */
3715 N, N, N, N, N, N, N, N,
3716 /* 0xD8 - 0xDF */
3717 N, N, N, N, N, N, N, N,
3718 /* 0xE0 - 0xE7 */
3719 N, N, N, N, N, N, N, N,
3720 /* 0xE8 - 0xEF */
3721 N, N, N, N, N, N, N, N,
3722 /* 0xF0 - 0xF7 */
3723 N, N, N, N, N, N, N, N,
3724 /* 0xF8 - 0xFF */
3725 N, N, N, N, N, N, N, N,
3726} };
3727
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003728static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003729 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003730 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003731 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3732 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003733 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003734 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003735 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3736 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003737 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003738 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003739 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3740 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003741 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003742 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003743 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3744 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003745 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003746 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003747 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003748 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003749 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003750 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003751 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003752 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003753 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02003754 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003755 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003756 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003757 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003758 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003759 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003760 I(ImplicitOps | Stack | No64, em_pusha),
3761 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003762 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3763 N, N, N, N,
3764 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003765 I(SrcImm | Mov | Stack, em_push),
3766 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003767 I(SrcImmByte | Mov | Stack, em_push),
3768 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003769 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003770 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003771 /* 0x70 - 0x7F */
3772 X16(D(SrcImmByte)),
3773 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003774 G(ByteOp | DstMem | SrcImm, group1),
3775 G(DstMem | SrcImm, group1),
3776 G(ByteOp | DstMem | SrcImm | No64, group1),
3777 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003778 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003779 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003780 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003781 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003782 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003783 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003784 D(ModRM | SrcMem | NoAccess | DstReg),
3785 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3786 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003787 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003788 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003789 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003790 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003791 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003792 II(ImplicitOps | Stack, em_pushf, pushf),
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003793 II(ImplicitOps | Stack, em_popf, popf), N, I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003794 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003795 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003796 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003797 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003798 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003799 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003800 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003801 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3802 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003803 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003804 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003805 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003806 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02003807 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003808 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003809 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003810 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003811 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003812 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3813 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003814 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003815 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003816 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Bruce Rogers32611072013-09-09 09:40:20 -06003817 I(ImplicitOps | Stack | SrcImmU16, em_ret_far_imm),
3818 I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003819 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003820 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003821 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003822 G(Src2One | ByteOp, group2), G(Src2One, group2),
3823 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003824 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02003825 I(DstAcc | SrcImmUByte | No64, em_aad),
3826 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02003827 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003828 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02003829 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003830 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003831 X3(I(SrcImmByte, em_loop)),
3832 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003833 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3834 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003835 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003836 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003837 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003838 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3839 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003840 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003841 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003842 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3843 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003844 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003845 D(ImplicitOps), D(ImplicitOps),
3846 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003847 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3848};
3849
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003850static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003851 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003852 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02003853 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003854 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003855 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003856 N, D(ImplicitOps | ModRM), N, N,
3857 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02003858 N, N, N, N, N, N, N, N,
3859 D(ImplicitOps | ModRM), N, N, N, N, N, N, D(ImplicitOps | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003860 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003861 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003862 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003863 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
3864 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003865 N, N, N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003866 N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
3867 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003868 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003869 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02003870 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003871 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02003872 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003873 I(ImplicitOps | EmulateOnUD, em_sysenter),
3874 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003875 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003876 N, N, N, N, N, N, N, N,
3877 /* 0x40 - 0x4F */
3878 X16(D(DstReg | SrcMem | ModRM | Mov)),
3879 /* 0x50 - 0x5F */
3880 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3881 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003882 N, N, N, N,
3883 N, N, N, N,
3884 N, N, N, N,
3885 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003886 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003887 N, N, N, N,
3888 N, N, N, N,
3889 N, N, N, N,
3890 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003891 /* 0x80 - 0x8F */
3892 X16(D(SrcImm)),
3893 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003894 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003895 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003896 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02003897 II(ImplicitOps, em_cpuid, cpuid),
3898 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02003899 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
3900 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003901 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003902 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003903 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02003904 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02003905 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
3906 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Avi Kivity4d758342013-01-19 19:51:55 +02003907 D(ModRM), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003908 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09003909 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03003910 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02003911 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03003912 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
3913 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003914 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003915 /* 0xB8 - 0xBF */
3916 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003917 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02003918 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
3919 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003920 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03003921 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02003922 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003923 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003924 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03003925 /* 0xC8 - 0xCF */
3926 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003927 /* 0xD0 - 0xDF */
3928 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3929 /* 0xE0 - 0xEF */
3930 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3931 /* 0xF0 - 0xFF */
3932 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3933};
3934
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003935static const struct gprefix three_byte_0f_38_f0 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01003936 I(DstReg | SrcMem | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003937};
3938
3939static const struct gprefix three_byte_0f_38_f1 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01003940 I(DstMem | SrcReg | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003941};
3942
3943/*
3944 * Insns below are selected by the prefix which indexed by the third opcode
3945 * byte.
3946 */
3947static const struct opcode opcode_map_0f_38[256] = {
3948 /* 0x00 - 0x7f */
3949 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01003950 /* 0x80 - 0xef */
3951 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
3952 /* 0xf0 - 0xf1 */
3953 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f0),
3954 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f1),
3955 /* 0xf2 - 0xff */
3956 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003957};
3958
Avi Kivity73fba5f2010-07-29 15:11:53 +03003959#undef D
3960#undef N
3961#undef G
3962#undef GD
3963#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003964#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003965#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03003966
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003967#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02003968#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003969#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003970#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003971#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003972
Avi Kivity9dac77f2011-06-01 15:34:25 +03003973static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03003974{
3975 unsigned size;
3976
Avi Kivity9dac77f2011-06-01 15:34:25 +03003977 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003978 if (size == 8)
3979 size = 4;
3980 return size;
3981}
3982
3983static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3984 unsigned size, bool sign_extension)
3985{
Avi Kivity39f21ee2010-08-18 19:20:21 +03003986 int rc = X86EMUL_CONTINUE;
3987
3988 op->type = OP_IMM;
3989 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003990 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003991 /* NB. Immediates are sign-extended as necessary. */
3992 switch (op->bytes) {
3993 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003994 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003995 break;
3996 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003997 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003998 break;
3999 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004000 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004001 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004002 case 8:
4003 op->val = insn_fetch(s64, ctxt);
4004 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004005 }
4006 if (!sign_extension) {
4007 switch (op->bytes) {
4008 case 1:
4009 op->val &= 0xff;
4010 break;
4011 case 2:
4012 op->val &= 0xffff;
4013 break;
4014 case 4:
4015 op->val &= 0xffffffff;
4016 break;
4017 }
4018 }
4019done:
4020 return rc;
4021}
4022
Avi Kivitya9945542011-09-13 10:45:41 +03004023static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4024 unsigned d)
4025{
4026 int rc = X86EMUL_CONTINUE;
4027
4028 switch (d) {
4029 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004030 decode_register_operand(ctxt, op);
Avi Kivitya9945542011-09-13 10:45:41 +03004031 break;
4032 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004033 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03004034 break;
4035 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004036 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004037 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03004038 *op = ctxt->memop;
4039 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03004040 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya9945542011-09-13 10:45:41 +03004041 fetch_bit_operand(ctxt);
4042 op->orig_val = op->val;
4043 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004044 case OpMem64:
4045 ctxt->memop.bytes = 8;
4046 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03004047 case OpAcc:
4048 op->type = OP_REG;
4049 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004050 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya9945542011-09-13 10:45:41 +03004051 fetch_register_operand(op);
4052 op->orig_val = op->val;
4053 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004054 case OpAccLo:
4055 op->type = OP_REG;
4056 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4057 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4058 fetch_register_operand(op);
4059 op->orig_val = op->val;
4060 break;
4061 case OpAccHi:
4062 if (ctxt->d & ByteOp) {
4063 op->type = OP_NONE;
4064 break;
4065 }
4066 op->type = OP_REG;
4067 op->bytes = ctxt->op_bytes;
4068 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4069 fetch_register_operand(op);
4070 op->orig_val = op->val;
4071 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004072 case OpDI:
4073 op->type = OP_MEM;
4074 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4075 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004076 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya9945542011-09-13 10:45:41 +03004077 op->addr.mem.seg = VCPU_SREG_ES;
4078 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004079 op->count = 1;
Avi Kivitya9945542011-09-13 10:45:41 +03004080 break;
4081 case OpDX:
4082 op->type = OP_REG;
4083 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004084 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya9945542011-09-13 10:45:41 +03004085 fetch_register_operand(op);
4086 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004087 case OpCL:
4088 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004089 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004090 break;
4091 case OpImmByte:
4092 rc = decode_imm(ctxt, op, 1, true);
4093 break;
4094 case OpOne:
4095 op->bytes = 1;
4096 op->val = 1;
4097 break;
4098 case OpImm:
4099 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4100 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004101 case OpImm64:
4102 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4103 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004104 case OpMem8:
4105 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004106 if (ctxt->memop.type == OP_REG) {
4107 ctxt->memop.addr.reg = decode_register(ctxt, ctxt->modrm_rm, 1);
4108 fetch_register_operand(&ctxt->memop);
4109 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004110 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004111 case OpMem16:
4112 ctxt->memop.bytes = 2;
4113 goto mem_common;
4114 case OpMem32:
4115 ctxt->memop.bytes = 4;
4116 goto mem_common;
4117 case OpImmU16:
4118 rc = decode_imm(ctxt, op, 2, false);
4119 break;
4120 case OpImmU:
4121 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4122 break;
4123 case OpSI:
4124 op->type = OP_MEM;
4125 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4126 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004127 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Avi Kivity0fe59122011-09-13 10:45:47 +03004128 op->addr.mem.seg = seg_override(ctxt);
4129 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004130 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004131 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004132 case OpXLat:
4133 op->type = OP_MEM;
4134 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4135 op->addr.mem.ea =
4136 register_address(ctxt,
4137 reg_read(ctxt, VCPU_REGS_RBX) +
4138 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
4139 op->addr.mem.seg = seg_override(ctxt);
4140 op->val = 0;
4141 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004142 case OpImmFAddr:
4143 op->type = OP_IMM;
4144 op->addr.mem.ea = ctxt->_eip;
4145 op->bytes = ctxt->op_bytes + 2;
4146 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4147 break;
4148 case OpMemFAddr:
4149 ctxt->memop.bytes = ctxt->op_bytes + 2;
4150 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004151 case OpES:
4152 op->val = VCPU_SREG_ES;
4153 break;
4154 case OpCS:
4155 op->val = VCPU_SREG_CS;
4156 break;
4157 case OpSS:
4158 op->val = VCPU_SREG_SS;
4159 break;
4160 case OpDS:
4161 op->val = VCPU_SREG_DS;
4162 break;
4163 case OpFS:
4164 op->val = VCPU_SREG_FS;
4165 break;
4166 case OpGS:
4167 op->val = VCPU_SREG_GS;
4168 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004169 case OpImplicit:
4170 /* Special instructions do their own operand decoding. */
4171 default:
4172 op->type = OP_NONE; /* Disable writeback. */
4173 break;
4174 }
4175
4176done:
4177 return rc;
4178}
4179
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004180int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004181{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004182 int rc = X86EMUL_CONTINUE;
4183 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004184 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004185 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03004186 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004187
Avi Kivityf09ed832011-09-13 10:45:40 +03004188 ctxt->memop.type = OP_NONE;
4189 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004190 ctxt->_eip = ctxt->eip;
4191 ctxt->fetch.start = ctxt->_eip;
4192 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004193 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01004194 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004195 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004196
4197 switch (mode) {
4198 case X86EMUL_MODE_REAL:
4199 case X86EMUL_MODE_VM86:
4200 case X86EMUL_MODE_PROT16:
4201 def_op_bytes = def_ad_bytes = 2;
4202 break;
4203 case X86EMUL_MODE_PROT32:
4204 def_op_bytes = def_ad_bytes = 4;
4205 break;
4206#ifdef CONFIG_X86_64
4207 case X86EMUL_MODE_PROT64:
4208 def_op_bytes = 4;
4209 def_ad_bytes = 8;
4210 break;
4211#endif
4212 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004213 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004214 }
4215
Avi Kivity9dac77f2011-06-01 15:34:25 +03004216 ctxt->op_bytes = def_op_bytes;
4217 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004218
4219 /* Legacy prefixes. */
4220 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004221 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004222 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004223 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004224 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004225 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004226 break;
4227 case 0x67: /* address-size override */
4228 if (mode == X86EMUL_MODE_PROT64)
4229 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004230 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004231 else
4232 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004233 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004234 break;
4235 case 0x26: /* ES override */
4236 case 0x2e: /* CS override */
4237 case 0x36: /* SS override */
4238 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004239 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004240 break;
4241 case 0x64: /* FS override */
4242 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004243 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004244 break;
4245 case 0x40 ... 0x4f: /* REX */
4246 if (mode != X86EMUL_MODE_PROT64)
4247 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004248 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004249 continue;
4250 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004251 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004252 break;
4253 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004254 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004255 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004256 break;
4257 default:
4258 goto done_prefixes;
4259 }
4260
4261 /* Any legacy prefix after a REX prefix nullifies its effect. */
4262
Avi Kivity9dac77f2011-06-01 15:34:25 +03004263 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004264 }
4265
4266done_prefixes:
4267
4268 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004269 if (ctxt->rex_prefix & 8)
4270 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004271
4272 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004273 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004274 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004275 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004276 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004277 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004278 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004279
4280 /* 0F_38 opcode map */
4281 if (ctxt->b == 0x38) {
4282 ctxt->opcode_len = 3;
4283 ctxt->b = insn_fetch(u8, ctxt);
4284 opcode = opcode_map_0f_38[ctxt->b];
4285 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004286 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004287 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004288
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004289 if (ctxt->d & ModRM)
4290 ctxt->modrm = insn_fetch(u8, ctxt);
4291
Avi Kivity9dac77f2011-06-01 15:34:25 +03004292 while (ctxt->d & GroupMask) {
4293 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004294 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004295 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004296 opcode = opcode.u.group[goffset];
4297 break;
4298 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004299 goffset = (ctxt->modrm >> 3) & 7;
4300 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004301 opcode = opcode.u.gdual->mod3[goffset];
4302 else
4303 opcode = opcode.u.gdual->mod012[goffset];
4304 break;
4305 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004306 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004307 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004308 break;
4309 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004310 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004311 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004312 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004313 switch (simd_prefix) {
4314 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4315 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4316 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4317 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4318 }
4319 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004320 case Escape:
4321 if (ctxt->modrm > 0xbf)
4322 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4323 else
4324 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4325 break;
Avi Kivity46561642011-04-24 14:09:59 +03004326 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004327 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004328 }
Avi Kivity46561642011-04-24 14:09:59 +03004329
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004330 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004331 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004332 }
4333
Avi Kivity9dac77f2011-06-01 15:34:25 +03004334 ctxt->execute = opcode.u.execute;
4335 ctxt->check_perm = opcode.check_perm;
4336 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004337
4338 /* Unrecognised? */
Gleb Natapov1146a782013-04-11 12:30:01 +03004339 if (ctxt->d == 0 || (ctxt->d & NotImpl))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004340 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004341
Borislav Petkovb51e9742013-09-22 16:44:52 +02004342 if (!(ctxt->d & EmulateOnUD) && ctxt->ud)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004343 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004344
Avi Kivity9dac77f2011-06-01 15:34:25 +03004345 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
4346 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004347
Avi Kivity9dac77f2011-06-01 15:34:25 +03004348 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004349 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004350 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004351 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03004352 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004353 }
4354
Avi Kivity9dac77f2011-06-01 15:34:25 +03004355 if (ctxt->d & Sse)
4356 ctxt->op_bytes = 16;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004357 else if (ctxt->d & Mmx)
4358 ctxt->op_bytes = 8;
Avi Kivity12537912011-03-29 11:41:27 +02004359
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004360 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004361 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004362 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004363 if (!ctxt->has_seg_override)
4364 set_seg_override(ctxt, ctxt->modrm_seg);
4365 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004366 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004367 if (rc != X86EMUL_CONTINUE)
4368 goto done;
4369
Avi Kivity9dac77f2011-06-01 15:34:25 +03004370 if (!ctxt->has_seg_override)
4371 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004372
Avi Kivityf09ed832011-09-13 10:45:40 +03004373 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004374
Avi Kivityf09ed832011-09-13 10:45:40 +03004375 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
4376 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004377
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004378 /*
4379 * Decode and fetch the source operand: register, memory
4380 * or immediate.
4381 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004382 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004383 if (rc != X86EMUL_CONTINUE)
4384 goto done;
4385
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004386 /*
4387 * Decode and fetch the second source operand: register, memory
4388 * or immediate.
4389 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004390 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004391 if (rc != X86EMUL_CONTINUE)
4392 goto done;
4393
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004394 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03004395 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004396
4397done:
Avi Kivityf09ed832011-09-13 10:45:40 +03004398 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
4399 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004400
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004401 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004402}
4403
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004404bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4405{
4406 return ctxt->d & PageTable;
4407}
4408
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004409static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4410{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004411 /* The second termination condition only applies for REPE
4412 * and REPNE. Test if the repeat string operation prefix is
4413 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4414 * corresponding termination condition according to:
4415 * - if REPE/REPZ and ZF = 0 then done
4416 * - if REPNE/REPNZ and ZF = 1 then done
4417 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004418 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4419 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4420 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004421 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004422 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004423 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4424 return true;
4425
4426 return false;
4427}
4428
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004429static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4430{
4431 bool fault = false;
4432
4433 ctxt->ops->get_fpu(ctxt);
4434 asm volatile("1: fwait \n\t"
4435 "2: \n\t"
4436 ".pushsection .fixup,\"ax\" \n\t"
4437 "3: \n\t"
4438 "movb $1, %[fault] \n\t"
4439 "jmp 2b \n\t"
4440 ".popsection \n\t"
4441 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004442 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004443 ctxt->ops->put_fpu(ctxt);
4444
4445 if (unlikely(fault))
4446 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4447
4448 return X86EMUL_CONTINUE;
4449}
4450
4451static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4452 struct operand *op)
4453{
4454 if (op->type == OP_MM)
4455 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4456}
4457
Avi Kivitye28bbd42013-01-04 16:18:48 +02004458static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4459{
4460 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivityb9fa4092013-02-09 11:31:48 +02004461 if (!(ctxt->d & ByteOp))
4462 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Avi Kivitye28bbd42013-01-04 16:18:48 +02004463 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004464 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
4465 [fastop]"+S"(fop)
4466 : "c"(ctxt->src2.val));
Avi Kivitye28bbd42013-01-04 16:18:48 +02004467 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004468 if (!fop) /* exception is returned in fop variable */
4469 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02004470 return X86EMUL_CONTINUE;
4471}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004472
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004473int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004474{
Mathias Krause0225fb52012-08-30 01:30:16 +02004475 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004476 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004477 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004478
Avi Kivity9dac77f2011-06-01 15:34:25 +03004479 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004480
Gleb Natapov1146a782013-04-11 12:30:01 +03004481 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4482 (ctxt->d & Undefined)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004483 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02004484 goto done;
4485 }
4486
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004487 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004488 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004489 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004490 goto done;
4491 }
4492
Avi Kivity9dac77f2011-06-01 15:34:25 +03004493 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004494 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004495 goto done;
4496 }
4497
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004498 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4499 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02004500 rc = emulate_ud(ctxt);
4501 goto done;
4502 }
4503
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004504 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02004505 rc = emulate_nm(ctxt);
4506 goto done;
4507 }
4508
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004509 if (ctxt->d & Mmx) {
4510 rc = flush_pending_x87_faults(ctxt);
4511 if (rc != X86EMUL_CONTINUE)
4512 goto done;
4513 /*
4514 * Now that we know the fpu is exception safe, we can fetch
4515 * operands from it.
4516 */
4517 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4518 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4519 if (!(ctxt->d & Mov))
4520 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4521 }
4522
Avi Kivity9dac77f2011-06-01 15:34:25 +03004523 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4524 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004525 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004526 if (rc != X86EMUL_CONTINUE)
4527 goto done;
4528 }
4529
Gleb Natapove92805a2010-02-10 14:21:35 +02004530 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004531 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004532 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02004533 goto done;
4534 }
4535
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004536 /* Instruction can only be executed in protected mode */
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03004537 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004538 rc = emulate_ud(ctxt);
4539 goto done;
4540 }
4541
Joerg Roedeld09beab2011-04-04 12:39:25 +02004542 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004543 if (ctxt->check_perm) {
4544 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004545 if (rc != X86EMUL_CONTINUE)
4546 goto done;
4547 }
4548
Avi Kivity9dac77f2011-06-01 15:34:25 +03004549 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4550 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004551 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004552 if (rc != X86EMUL_CONTINUE)
4553 goto done;
4554 }
4555
Avi Kivity9dac77f2011-06-01 15:34:25 +03004556 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004557 /* All REP prefixes have the same first termination condition */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004558 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004559 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004560 goto done;
4561 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004562 }
4563
Avi Kivity9dac77f2011-06-01 15:34:25 +03004564 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4565 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4566 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004567 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004568 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004569 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004570 }
4571
Avi Kivity9dac77f2011-06-01 15:34:25 +03004572 if (ctxt->src2.type == OP_MEM) {
4573 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4574 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004575 if (rc != X86EMUL_CONTINUE)
4576 goto done;
4577 }
4578
Avi Kivity9dac77f2011-06-01 15:34:25 +03004579 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004580 goto special_insn;
4581
4582
Avi Kivity9dac77f2011-06-01 15:34:25 +03004583 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004584 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004585 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4586 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004587 if (rc != X86EMUL_CONTINUE)
4588 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004589 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004590 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004591
Avi Kivity018a98d2007-11-27 19:30:56 +02004592special_insn:
4593
Avi Kivity9dac77f2011-06-01 15:34:25 +03004594 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4595 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004596 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004597 if (rc != X86EMUL_CONTINUE)
4598 goto done;
4599 }
4600
Avi Kivity9dac77f2011-06-01 15:34:25 +03004601 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004602 if (ctxt->d & Fastop) {
4603 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4604 rc = fastop(ctxt, fop);
4605 if (rc != X86EMUL_CONTINUE)
4606 goto done;
4607 goto writeback;
4608 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004609 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004610 if (rc != X86EMUL_CONTINUE)
4611 goto done;
4612 goto writeback;
4613 }
4614
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004615 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004616 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004617 else if (ctxt->opcode_len == 3)
4618 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004619
Avi Kivity9dac77f2011-06-01 15:34:25 +03004620 switch (ctxt->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004621 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004622 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004623 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004624 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004625 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004626 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004627 if (test_cc(ctxt->b, ctxt->eflags))
4628 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004629 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004630 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004631 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004632 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004633 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004634 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Mohammed Gamal34698d82010-08-04 14:41:04 +03004635 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004636 rc = em_xchg(ctxt);
4637 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004638 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004639 switch (ctxt->op_bytes) {
4640 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4641 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4642 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004643 }
4644 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004645 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004646 rc = emulate_int(ctxt, 3);
4647 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004648 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004649 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004650 break;
4651 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004652 if (ctxt->eflags & EFLG_OF)
4653 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004654 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004655 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004656 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004657 jmp_rel(ctxt, ctxt->src.val);
4658 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004659 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004660 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004661 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004662 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004663 case 0xf5: /* cmc */
4664 /* complement carry flag from eflags reg */
4665 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004666 break;
4667 case 0xf8: /* clc */
4668 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004669 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004670 case 0xf9: /* stc */
4671 ctxt->eflags |= EFLG_CF;
4672 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004673 case 0xfc: /* cld */
4674 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004675 break;
4676 case 0xfd: /* std */
4677 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004678 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004679 default:
4680 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004681 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004682
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004683 if (rc != X86EMUL_CONTINUE)
4684 goto done;
4685
Avi Kivity018a98d2007-11-27 19:30:56 +02004686writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02004687 if (!(ctxt->d & NoWrite)) {
4688 rc = writeback(ctxt, &ctxt->dst);
4689 if (rc != X86EMUL_CONTINUE)
4690 goto done;
4691 }
4692 if (ctxt->d & SrcWrite) {
4693 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
4694 rc = writeback(ctxt, &ctxt->src);
4695 if (rc != X86EMUL_CONTINUE)
4696 goto done;
4697 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004698
Gleb Natapov5cd21912010-03-18 15:20:26 +02004699 /*
4700 * restore dst type in case the decoding will be reused
4701 * (happens for string instruction )
4702 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004703 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004704
Avi Kivity9dac77f2011-06-01 15:34:25 +03004705 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004706 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004707
Avi Kivity9dac77f2011-06-01 15:34:25 +03004708 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004709 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004710
Avi Kivity9dac77f2011-06-01 15:34:25 +03004711 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004712 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004713 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004714 if ((ctxt->d & SrcMask) == SrcSI)
4715 count = ctxt->src.count;
4716 else
4717 count = ctxt->dst.count;
4718 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4719 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004720
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004721 if (!string_insn_completed(ctxt)) {
4722 /*
4723 * Re-enter guest when pio read ahead buffer is empty
4724 * or, if it is not used, after each 1024 iteration.
4725 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004726 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004727 (r->end == 0 || r->end != r->pos)) {
4728 /*
4729 * Reset read cache. Usually happens before
4730 * decode, but since instruction is restarted
4731 * we have to do it here.
4732 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004733 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004734 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004735 return EMULATION_RESTART;
4736 }
4737 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004738 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004739 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004740
Avi Kivity9dac77f2011-06-01 15:34:25 +03004741 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004742
4743done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004744 if (rc == X86EMUL_PROPAGATE_FAULT)
4745 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004746 if (rc == X86EMUL_INTERCEPTED)
4747 return EMULATION_INTERCEPTED;
4748
Avi Kivitydd856ef2012-08-27 23:46:17 +03004749 if (rc == X86EMUL_CONTINUE)
4750 writeback_registers(ctxt);
4751
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004752 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004753
4754twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004755 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004756 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004757 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004758 break;
4759 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004760 case 0x0d: /* GrpP (prefetch) */
4761 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004762 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02004763 break;
4764 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004765 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004766 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004767 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004768 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004769 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004770 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004771 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4772 if (!test_cc(ctxt->b, ctxt->eflags))
4773 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004774 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004775 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004776 if (test_cc(ctxt->b, ctxt->eflags))
4777 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004778 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004779 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004780 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004781 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004782 case 0xae: /* clflush */
4783 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004784 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004785 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004786 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004787 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004788 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004789 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004790 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004791 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004792 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004793 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004794 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004795 ctxt->dst.bytes = ctxt->op_bytes;
4796 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4797 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004798 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004799 default:
4800 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004801 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004802
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004803threebyte_insn:
4804
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004805 if (rc != X86EMUL_CONTINUE)
4806 goto done;
4807
Avi Kivity6aa8b732006-12-10 02:21:36 -08004808 goto writeback;
4809
4810cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004811 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004812}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004813
4814void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
4815{
4816 invalidate_registers(ctxt);
4817}
4818
4819void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
4820{
4821 writeback_registers(ctxt);
4822}