blob: 3b5d4dd6750a139eea05347298069e3b42cfcf94 [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 */
Avi Kivitya9945542011-09-13 10:45:41 +030063
Avi Kivity0fe59122011-09-13 10:45:47 +030064#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030065#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya9945542011-09-13 10:45:41 +030066
67/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080068 * Opcode effective-address decode tables.
69 * Note that we only emulate instructions that have at least one memory
70 * operand (excluding implicit stack references). We assume that stack
71 * references and instruction fetches will never occur in special memory
72 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
73 * not be handled.
74 */
75
76/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030077#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080078/* Destination operand type. */
Avi Kivitya9945542011-09-13 10:45:41 +030079#define DstShift 1
80#define ImplicitOps (OpImplicit << DstShift)
81#define DstReg (OpReg << DstShift)
82#define DstMem (OpMem << DstShift)
83#define DstAcc (OpAcc << DstShift)
84#define DstDI (OpDI << DstShift)
85#define DstMem64 (OpMem64 << DstShift)
86#define DstImmUByte (OpImmUByte << DstShift)
87#define DstDX (OpDX << DstShift)
88#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080089/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030090#define SrcShift 6
91#define SrcNone (OpNone << SrcShift)
92#define SrcReg (OpReg << SrcShift)
93#define SrcMem (OpMem << SrcShift)
94#define SrcMem16 (OpMem16 << SrcShift)
95#define SrcMem32 (OpMem32 << SrcShift)
96#define SrcImm (OpImm << SrcShift)
97#define SrcImmByte (OpImmByte << SrcShift)
98#define SrcOne (OpOne << SrcShift)
99#define SrcImmUByte (OpImmUByte << SrcShift)
100#define SrcImmU (OpImmU << SrcShift)
101#define SrcSI (OpSI << SrcShift)
102#define SrcImmFAddr (OpImmFAddr << SrcShift)
103#define SrcMemFAddr (OpMemFAddr << SrcShift)
104#define SrcAcc (OpAcc << SrcShift)
105#define SrcImmU16 (OpImmU16 << SrcShift)
Nadav Amit5e2c6882012-12-06 21:55:10 -0200106#define SrcImm64 (OpImm64 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300107#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200108#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300109#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300110#define BitOp (1<<11)
111#define MemAbs (1<<12) /* Memory operand is absolute displacement */
112#define String (1<<13) /* String instruction (rep capable) */
113#define Stack (1<<14) /* Stack instruction (push/pop) */
114#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
115#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
116#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
117#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
118#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
Gleb Natapov045a2822012-12-20 16:57:43 +0200119#define Escape (5<<15) /* Escape to coprocessor instruction */
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300120#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300121/* Generic ModRM decode. */
122#define ModRM (1<<19)
123/* Destination is only written; never read. */
124#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300125/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200126#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Avi Kivityd8671622011-02-01 16:32:03 +0200127#define VendorSpecific (1<<22) /* Vendor specific instruction */
Avi Kivity5a506b12010-08-01 15:10:29 +0300128#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300129#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300130#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200131#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200132#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300133#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800134#define PageTable (1 << 29) /* instruction used to write page table */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100135/* Source 2 operand type */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800136#define Src2Shift (30)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300137#define Src2None (OpNone << Src2Shift)
138#define Src2CL (OpCL << Src2Shift)
139#define Src2ImmByte (OpImmByte << Src2Shift)
140#define Src2One (OpOne << Src2Shift)
141#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300142#define Src2ES (OpES << Src2Shift)
143#define Src2CS (OpCS << Src2Shift)
144#define Src2SS (OpSS << Src2Shift)
145#define Src2DS (OpDS << Src2Shift)
146#define Src2FS (OpFS << Src2Shift)
147#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300148#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300149#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Avi Kivity1c11b372012-04-09 18:39:59 +0300150#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
151#define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */
152#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
Avi Kivitye28bbd42013-01-04 16:18:48 +0200153#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
Avi Kivityb6744dc2013-01-04 16:18:50 +0200154#define NoWrite ((u64)1 << 45) /* No writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800155
Avi Kivityd0e53322010-07-29 15:11:54 +0300156#define X2(x...) x, x
157#define X3(x...) X2(x), x
158#define X4(x...) X2(x), X2(x)
159#define X5(x...) X4(x), x
160#define X6(x...) X4(x), X2(x)
161#define X7(x...) X4(x), X3(x)
162#define X8(x...) X4(x), X4(x)
163#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300164
Avi Kivitye28bbd42013-01-04 16:18:48 +0200165#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
166#define FASTOP_SIZE 8
167
168/*
169 * fastop functions have a special calling convention:
170 *
171 * dst: [rdx]:rax (in/out)
172 * src: rbx (in/out)
173 * src2: rcx (in)
174 * flags: rflags (in/out)
175 *
176 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
177 * different operand sizes can be reached by calculation, rather than a jump
178 * table (which would be bigger than the code).
179 *
180 * fastop functions are declared as taking a never-defined fastop parameter,
181 * so they can't be called from C directly.
182 */
183
184struct fastop;
185
Avi Kivityd65b1de2010-07-29 15:11:35 +0300186struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300187 u64 flags : 56;
188 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300189 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300190 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200191 const struct opcode *group;
192 const struct group_dual *gdual;
193 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200194 const struct escape *esc;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200195 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300196 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200197 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300198};
199
200struct group_dual {
201 struct opcode mod012[8];
202 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300203};
204
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200205struct gprefix {
206 struct opcode pfx_no;
207 struct opcode pfx_66;
208 struct opcode pfx_f2;
209 struct opcode pfx_f3;
210};
211
Gleb Natapov045a2822012-12-20 16:57:43 +0200212struct escape {
213 struct opcode op[8];
214 struct opcode high[64];
215};
216
Avi Kivity6aa8b732006-12-10 02:21:36 -0800217/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200218#define EFLG_ID (1<<21)
219#define EFLG_VIP (1<<20)
220#define EFLG_VIF (1<<19)
221#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200222#define EFLG_VM (1<<17)
223#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200224#define EFLG_IOPL (3<<12)
225#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800226#define EFLG_OF (1<<11)
227#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200228#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200229#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800230#define EFLG_SF (1<<7)
231#define EFLG_ZF (1<<6)
232#define EFLG_AF (1<<4)
233#define EFLG_PF (1<<2)
234#define EFLG_CF (1<<0)
235
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300236#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
237#define EFLG_RESERVED_ONE_MASK 2
238
Avi Kivitydd856ef2012-08-27 23:46:17 +0300239static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
240{
241 if (!(ctxt->regs_valid & (1 << nr))) {
242 ctxt->regs_valid |= 1 << nr;
243 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
244 }
245 return ctxt->_regs[nr];
246}
247
248static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
249{
250 ctxt->regs_valid |= 1 << nr;
251 ctxt->regs_dirty |= 1 << nr;
252 return &ctxt->_regs[nr];
253}
254
255static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
256{
257 reg_read(ctxt, nr);
258 return reg_write(ctxt, nr);
259}
260
261static void writeback_registers(struct x86_emulate_ctxt *ctxt)
262{
263 unsigned reg;
264
265 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
266 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
267}
268
269static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
270{
271 ctxt->regs_dirty = 0;
272 ctxt->regs_valid = 0;
273}
274
Avi Kivity6aa8b732006-12-10 02:21:36 -0800275/*
276 * Instruction emulation:
277 * Most instructions are emulated directly via a fragment of inline assembly
278 * code. This allows us to save/restore EFLAGS and thus very easily pick up
279 * any modified flags.
280 */
281
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800282#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800283#define _LO32 "k" /* force 32-bit operand */
284#define _STK "%%rsp" /* stack pointer */
285#elif defined(__i386__)
286#define _LO32 "" /* force 32-bit operand */
287#define _STK "%%esp" /* stack pointer */
288#endif
289
290/*
291 * These EFLAGS bits are restored from saved value during emulation, and
292 * any changes are written back to the saved value after emulation.
293 */
294#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
295
296/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200297#define _PRE_EFLAGS(_sav, _msk, _tmp) \
298 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
299 "movl %"_sav",%"_LO32 _tmp"; " \
300 "push %"_tmp"; " \
301 "push %"_tmp"; " \
302 "movl %"_msk",%"_LO32 _tmp"; " \
303 "andl %"_LO32 _tmp",("_STK"); " \
304 "pushf; " \
305 "notl %"_LO32 _tmp"; " \
306 "andl %"_LO32 _tmp",("_STK"); " \
307 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
308 "pop %"_tmp"; " \
309 "orl %"_LO32 _tmp",("_STK"); " \
310 "popf; " \
311 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800312
313/* After executing instruction: write-back necessary bits in EFLAGS. */
314#define _POST_EFLAGS(_sav, _msk, _tmp) \
315 /* _sav |= EFLAGS & _msk; */ \
316 "pushf; " \
317 "pop %"_tmp"; " \
318 "andl %"_msk",%"_LO32 _tmp"; " \
319 "orl %"_LO32 _tmp",%"_sav"; "
320
Avi Kivitydda96d82008-11-26 15:14:10 +0200321#ifdef CONFIG_X86_64
322#define ON64(x) x
323#else
324#define ON64(x)
325#endif
326
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300327#define ____emulate_2op(ctxt, _op, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200328 do { \
329 __asm__ __volatile__ ( \
330 _PRE_EFLAGS("0", "4", "2") \
331 _op _suffix " %"_x"3,%1; " \
332 _POST_EFLAGS("0", "4", "2") \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300333 : "=m" ((ctxt)->eflags), \
334 "+q" (*(_dsttype*)&(ctxt)->dst.val), \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200335 "=&r" (_tmp) \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300336 : _y ((ctxt)->src.val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200337 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200338
339
Avi Kivity6aa8b732006-12-10 02:21:36 -0800340/* Raw emulation: instruction has two explicit operands. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300341#define __emulate_2op_nobyte(ctxt,_op,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200342 do { \
343 unsigned long _tmp; \
344 \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300345 switch ((ctxt)->dst.bytes) { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200346 case 2: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300347 ____emulate_2op(ctxt,_op,_wx,_wy,"w",u16); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200348 break; \
349 case 4: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300350 ____emulate_2op(ctxt,_op,_lx,_ly,"l",u32); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200351 break; \
352 case 8: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300353 ON64(____emulate_2op(ctxt,_op,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200354 break; \
355 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800356 } while (0)
357
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300358#define __emulate_2op(ctxt,_op,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800359 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200360 unsigned long _tmp; \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300361 switch ((ctxt)->dst.bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800362 case 1: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300363 ____emulate_2op(ctxt,_op,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800364 break; \
365 default: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300366 __emulate_2op_nobyte(ctxt, _op, \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800367 _wx, _wy, _lx, _ly, _qx, _qy); \
368 break; \
369 } \
370 } while (0)
371
372/* Source operand is byte-sized and may be restricted to just %cl. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300373#define emulate_2op_SrcB(ctxt, _op) \
374 __emulate_2op(ctxt, _op, "b", "c", "b", "c", "b", "c", "b", "c")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800375
376/* Source operand is byte, word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300377#define emulate_2op_SrcV(ctxt, _op) \
378 __emulate_2op(ctxt, _op, "b", "q", "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800379
380/* Source operand is word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300381#define emulate_2op_SrcV_nobyte(ctxt, _op) \
382 __emulate_2op_nobyte(ctxt, _op, "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800383
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100384/* Instruction has three operands and one operand is stored in ECX register */
Avi Kivity29053a62011-09-07 16:41:37 +0300385#define __emulate_2op_cl(ctxt, _op, _suffix, _type) \
Avi Kivity72952612011-04-20 13:12:27 +0300386 do { \
387 unsigned long _tmp; \
Avi Kivity761441b2011-09-07 16:41:36 +0300388 _type _clv = (ctxt)->src2.val; \
389 _type _srcv = (ctxt)->src.val; \
390 _type _dstv = (ctxt)->dst.val; \
Avi Kivity72952612011-04-20 13:12:27 +0300391 \
392 __asm__ __volatile__ ( \
393 _PRE_EFLAGS("0", "5", "2") \
394 _op _suffix " %4,%1 \n" \
395 _POST_EFLAGS("0", "5", "2") \
Avi Kivity761441b2011-09-07 16:41:36 +0300396 : "=m" ((ctxt)->eflags), "+r" (_dstv), "=&r" (_tmp) \
Avi Kivity72952612011-04-20 13:12:27 +0300397 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
398 ); \
399 \
Avi Kivity761441b2011-09-07 16:41:36 +0300400 (ctxt)->src2.val = (unsigned long) _clv; \
401 (ctxt)->src2.val = (unsigned long) _srcv; \
402 (ctxt)->dst.val = (unsigned long) _dstv; \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100403 } while (0)
404
Avi Kivity761441b2011-09-07 16:41:36 +0300405#define emulate_2op_cl(ctxt, _op) \
Avi Kivity72952612011-04-20 13:12:27 +0300406 do { \
Avi Kivity761441b2011-09-07 16:41:36 +0300407 switch ((ctxt)->dst.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300408 case 2: \
Avi Kivity29053a62011-09-07 16:41:37 +0300409 __emulate_2op_cl(ctxt, _op, "w", u16); \
Avi Kivity72952612011-04-20 13:12:27 +0300410 break; \
411 case 4: \
Avi Kivity29053a62011-09-07 16:41:37 +0300412 __emulate_2op_cl(ctxt, _op, "l", u32); \
Avi Kivity72952612011-04-20 13:12:27 +0300413 break; \
414 case 8: \
Avi Kivity29053a62011-09-07 16:41:37 +0300415 ON64(__emulate_2op_cl(ctxt, _op, "q", ulong)); \
Avi Kivity72952612011-04-20 13:12:27 +0300416 break; \
417 } \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100418 } while (0)
419
Avi Kivityd1eef452011-09-07 16:41:38 +0300420#define __emulate_1op(ctxt, _op, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800421 do { \
422 unsigned long _tmp; \
423 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200424 __asm__ __volatile__ ( \
425 _PRE_EFLAGS("0", "3", "2") \
426 _op _suffix " %1; " \
427 _POST_EFLAGS("0", "3", "2") \
Avi Kivityd1eef452011-09-07 16:41:38 +0300428 : "=m" ((ctxt)->eflags), "+m" ((ctxt)->dst.val), \
Avi Kivitydda96d82008-11-26 15:14:10 +0200429 "=&r" (_tmp) \
430 : "i" (EFLAGS_MASK)); \
431 } while (0)
432
433/* Instruction has only one explicit operand (no source operand). */
Avi Kivityd1eef452011-09-07 16:41:38 +0300434#define emulate_1op(ctxt, _op) \
Avi Kivitydda96d82008-11-26 15:14:10 +0200435 do { \
Avi Kivityd1eef452011-09-07 16:41:38 +0300436 switch ((ctxt)->dst.bytes) { \
437 case 1: __emulate_1op(ctxt, _op, "b"); break; \
438 case 2: __emulate_1op(ctxt, _op, "w"); break; \
439 case 4: __emulate_1op(ctxt, _op, "l"); break; \
440 case 8: ON64(__emulate_1op(ctxt, _op, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441 } \
442 } while (0)
443
Avi Kivityb7d491e2013-01-04 16:18:49 +0200444#define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t"
445#define FOP_RET "ret \n\t"
446
447#define FOP_START(op) \
448 extern void em_##op(struct fastop *fake); \
449 asm(".pushsection .text, \"ax\" \n\t" \
450 ".global em_" #op " \n\t" \
451 FOP_ALIGN \
452 "em_" #op ": \n\t"
453
454#define FOP_END \
455 ".popsection")
456
457#define FOP1E(op, dst) \
458 FOP_ALIGN #op " %" #dst " \n\t" FOP_RET
459
460#define FASTOP1(op) \
461 FOP_START(op) \
462 FOP1E(op##b, al) \
463 FOP1E(op##w, ax) \
464 FOP1E(op##l, eax) \
465 ON64(FOP1E(op##q, rax)) \
466 FOP_END
467
Avi Kivityf7857f32013-01-04 16:18:53 +0200468#define FOP2E(op, dst, src) \
469 FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET
470
471#define FASTOP2(op) \
472 FOP_START(op) \
473 FOP2E(op##b, al, bl) \
474 FOP2E(op##w, ax, bx) \
475 FOP2E(op##l, eax, ebx) \
476 ON64(FOP2E(op##q, rax, rbx)) \
477 FOP_END
478
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300479#define __emulate_1op_rax_rdx(ctxt, _op, _suffix, _ex) \
Avi Kivityf6b35972010-08-26 11:59:00 +0300480 do { \
481 unsigned long _tmp; \
Avi Kivitydd856ef2012-08-27 23:46:17 +0300482 ulong *rax = reg_rmw((ctxt), VCPU_REGS_RAX); \
483 ulong *rdx = reg_rmw((ctxt), VCPU_REGS_RDX); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300484 \
485 __asm__ __volatile__ ( \
486 _PRE_EFLAGS("0", "5", "1") \
487 "1: \n\t" \
488 _op _suffix " %6; " \
489 "2: \n\t" \
490 _POST_EFLAGS("0", "5", "1") \
491 ".pushsection .fixup,\"ax\" \n\t" \
492 "3: movb $1, %4 \n\t" \
493 "jmp 2b \n\t" \
494 ".popsection \n\t" \
495 _ASM_EXTABLE(1b, 3b) \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300496 : "=m" ((ctxt)->eflags), "=&r" (_tmp), \
497 "+a" (*rax), "+d" (*rdx), "+qm"(_ex) \
H. Peter Anvincb7cb282012-11-21 14:41:21 -0800498 : "i" (EFLAGS_MASK), "m" ((ctxt)->src.val)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300499 } while (0)
500
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300501/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300502#define emulate_1op_rax_rdx(ctxt, _op, _ex) \
Avi Kivity72952612011-04-20 13:12:27 +0300503 do { \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300504 switch((ctxt)->src.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300505 case 1: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300506 __emulate_1op_rax_rdx(ctxt, _op, "b", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300507 break; \
508 case 2: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300509 __emulate_1op_rax_rdx(ctxt, _op, "w", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300510 break; \
511 case 4: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300512 __emulate_1op_rax_rdx(ctxt, _op, "l", _ex); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300513 break; \
514 case 8: ON64( \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300515 __emulate_1op_rax_rdx(ctxt, _op, "q", _ex)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300516 break; \
517 } \
518 } while (0)
519
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200520static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
521 enum x86_intercept intercept,
522 enum x86_intercept_stage stage)
523{
524 struct x86_instruction_info info = {
525 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300526 .rep_prefix = ctxt->rep_prefix,
527 .modrm_mod = ctxt->modrm_mod,
528 .modrm_reg = ctxt->modrm_reg,
529 .modrm_rm = ctxt->modrm_rm,
530 .src_val = ctxt->src.val64,
531 .src_bytes = ctxt->src.bytes,
532 .dst_bytes = ctxt->dst.bytes,
533 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200534 .next_rip = ctxt->eip,
535 };
536
Avi Kivity29535382011-04-20 13:37:53 +0300537 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200538}
539
Avi Kivityf47cfa32012-06-07 17:49:24 +0300540static void assign_masked(ulong *dest, ulong src, ulong mask)
541{
542 *dest = (*dest & ~mask) | (src & mask);
543}
544
Avi Kivity9dac77f2011-06-01 15:34:25 +0300545static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800546{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300547 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800548}
549
Avi Kivityf47cfa32012-06-07 17:49:24 +0300550static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
551{
552 u16 sel;
553 struct desc_struct ss;
554
555 if (ctxt->mode == X86EMUL_MODE_PROT64)
556 return ~0UL;
557 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
558 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
559}
560
Avi Kivity612e89f2012-06-12 20:03:23 +0300561static int stack_size(struct x86_emulate_ctxt *ctxt)
562{
563 return (__fls(stack_mask(ctxt)) + 1) >> 3;
564}
565
Avi Kivity6aa8b732006-12-10 02:21:36 -0800566/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800567static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300568address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800569{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300570 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800571 return reg;
572 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300573 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800574}
575
576static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300577register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800578{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300579 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800580}
581
Avi Kivity5ad105e2012-08-19 14:34:31 +0300582static void masked_increment(ulong *reg, ulong mask, int inc)
583{
584 assign_masked(reg, *reg + inc, mask);
585}
586
Harvey Harrison7a9572752008-02-19 07:40:41 -0800587static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300588register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800589{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300590 ulong mask;
591
Avi Kivity9dac77f2011-06-01 15:34:25 +0300592 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300593 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800594 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300595 mask = ad_mask(ctxt);
596 masked_increment(reg, mask, inc);
597}
598
599static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
600{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300601 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800602}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800603
Avi Kivity9dac77f2011-06-01 15:34:25 +0300604static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800605{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300606 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800607}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300608
Avi Kivity56697682011-04-03 14:08:51 +0300609static u32 desc_limit_scaled(struct desc_struct *desc)
610{
611 u32 limit = get_desc_limit(desc);
612
613 return desc->g ? (limit << 12) | 0xfff : limit;
614}
615
Avi Kivity9dac77f2011-06-01 15:34:25 +0300616static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300617{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300618 ctxt->has_seg_override = true;
619 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300620}
621
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900622static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300623{
624 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
625 return 0;
626
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900627 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300628}
629
Avi Kivity9dac77f2011-06-01 15:34:25 +0300630static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300631{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300632 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300633 return 0;
634
Avi Kivity9dac77f2011-06-01 15:34:25 +0300635 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300636}
637
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200638static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
639 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300640{
Avi Kivityda9cb572010-11-22 17:53:21 +0200641 ctxt->exception.vector = vec;
642 ctxt->exception.error_code = error;
643 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200644 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300645}
646
Joerg Roedel3b88e412011-04-04 12:39:29 +0200647static int emulate_db(struct x86_emulate_ctxt *ctxt)
648{
649 return emulate_exception(ctxt, DB_VECTOR, 0, false);
650}
651
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200652static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300653{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200654 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300655}
656
Avi Kivity618ff152011-04-03 12:32:09 +0300657static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
658{
659 return emulate_exception(ctxt, SS_VECTOR, err, true);
660}
661
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200662static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300663{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200664 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300665}
666
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200667static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300668{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200669 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300670}
671
Avi Kivity34d1f492010-08-26 11:59:01 +0300672static int emulate_de(struct x86_emulate_ctxt *ctxt)
673{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200674 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300675}
676
Avi Kivity12537912011-03-29 11:41:27 +0200677static int emulate_nm(struct x86_emulate_ctxt *ctxt)
678{
679 return emulate_exception(ctxt, NM_VECTOR, 0, false);
680}
681
Avi Kivity1aa36612011-04-27 13:20:30 +0300682static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
683{
684 u16 selector;
685 struct desc_struct desc;
686
687 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
688 return selector;
689}
690
691static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
692 unsigned seg)
693{
694 u16 dummy;
695 u32 base3;
696 struct desc_struct desc;
697
698 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
699 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
700}
701
Avi Kivity1c11b372012-04-09 18:39:59 +0300702/*
703 * x86 defines three classes of vector instructions: explicitly
704 * aligned, explicitly unaligned, and the rest, which change behaviour
705 * depending on whether they're AVX encoded or not.
706 *
707 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
708 * subject to the same check.
709 */
710static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
711{
712 if (likely(size < 16))
713 return false;
714
715 if (ctxt->d & Aligned)
716 return true;
717 else if (ctxt->d & Unaligned)
718 return false;
719 else if (ctxt->d & Avx)
720 return false;
721 else
722 return true;
723}
724
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400725static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300726 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400727 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300728 ulong *linear)
729{
Avi Kivity618ff152011-04-03 12:32:09 +0300730 struct desc_struct desc;
731 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300732 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300733 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300734 u16 sel;
Gleb Natapov3a78a4f2012-12-20 16:57:42 +0200735 unsigned cpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300736
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900737 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300738 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300739 case X86EMUL_MODE_PROT64:
740 if (((signed long)la << 16) >> 16 != la)
741 return emulate_gp(ctxt, 0);
742 break;
743 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300744 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
745 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300746 if (!usable)
747 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200748 /* code segment in protected mode or read-only data segment */
749 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
750 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300751 goto bad;
752 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400753 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300754 goto bad;
755 lim = desc_limit_scaled(&desc);
756 if ((desc.type & 8) || !(desc.type & 4)) {
757 /* expand-up segment */
758 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
759 goto bad;
760 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800761 /* expand-down segment */
Avi Kivity618ff152011-04-03 12:32:09 +0300762 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
763 goto bad;
764 lim = desc.d ? 0xffffffff : 0xffff;
765 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
766 goto bad;
767 }
Avi Kivity717746e2011-04-20 13:37:53 +0300768 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity618ff152011-04-03 12:32:09 +0300769 if (!(desc.type & 8)) {
770 /* data segment */
771 if (cpl > desc.dpl)
772 goto bad;
773 } else if ((desc.type & 8) && !(desc.type & 4)) {
774 /* nonconforming code segment */
775 if (cpl != desc.dpl)
776 goto bad;
777 } else if ((desc.type & 8) && (desc.type & 4)) {
778 /* conforming code segment */
779 if (cpl < desc.dpl)
780 goto bad;
781 }
782 break;
783 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300784 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300785 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300786 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
787 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300788 *linear = la;
789 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300790bad:
791 if (addr.seg == VCPU_SREG_SS)
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300792 return emulate_ss(ctxt, sel);
Avi Kivity618ff152011-04-03 12:32:09 +0300793 else
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300794 return emulate_gp(ctxt, sel);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300795}
796
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400797static int linearize(struct x86_emulate_ctxt *ctxt,
798 struct segmented_address addr,
799 unsigned size, bool write,
800 ulong *linear)
801{
802 return __linearize(ctxt, addr, size, write, false, linear);
803}
804
805
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200806static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
807 struct segmented_address addr,
808 void *data,
809 unsigned size)
810{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200811 int rc;
812 ulong linear;
813
Avi Kivity83b87952011-04-03 11:31:19 +0300814 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200815 if (rc != X86EMUL_CONTINUE)
816 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300817 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200818}
819
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900820/*
821 * Fetch the next byte of the instruction being emulated which is pointed to
822 * by ctxt->_eip, then increment ctxt->_eip.
823 *
824 * Also prefetch the remaining bytes of the instruction without crossing page
825 * boundary if they are not in fetch_cache yet.
826 */
827static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200828{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300829 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200830 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300831 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200832
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900833 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400834 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900835 struct segmented_address addr = { .seg = VCPU_SREG_CS,
836 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300837 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900838 size = min(15UL - cur_size,
839 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400840 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900841 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400842 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900843 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
844 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900845 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200846 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300847 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200848 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900849 *dest = fc->data[ctxt->_eip - fc->start];
850 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900851 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200852}
853
854static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900855 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200856{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900857 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200858
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200859 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900860 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200861 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200862 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900863 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900864 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200865 return rc;
866 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900867 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200868}
869
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900870/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900871#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900872({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900873 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900874 if (rc != X86EMUL_CONTINUE) \
875 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900876 (_type)_x; \
877})
878
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900879#define insn_fetch_arr(_arr, _size, _ctxt) \
880({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900881 if (rc != X86EMUL_CONTINUE) \
882 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900883})
884
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000885/*
886 * Given the 'reg' portion of a ModRM byte, and a register block, return a
887 * pointer into the block that addresses the relevant register.
888 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
889 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300890static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000891 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892{
893 void *p;
894
Avi Kivity6aa8b732006-12-10 02:21:36 -0800895 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300896 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
897 else
898 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899 return p;
900}
901
902static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200903 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800904 u16 *size, unsigned long *address, int op_bytes)
905{
906 int rc;
907
908 if (op_bytes == 2)
909 op_bytes = 3;
910 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200911 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900912 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200914 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200915 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800916 return rc;
917}
918
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300919static int test_cc(unsigned int condition, unsigned int flags)
920{
921 int rc = 0;
922
923 switch ((condition & 15) >> 1) {
924 case 0: /* o */
925 rc |= (flags & EFLG_OF);
926 break;
927 case 1: /* b/c/nae */
928 rc |= (flags & EFLG_CF);
929 break;
930 case 2: /* z/e */
931 rc |= (flags & EFLG_ZF);
932 break;
933 case 3: /* be/na */
934 rc |= (flags & (EFLG_CF|EFLG_ZF));
935 break;
936 case 4: /* s */
937 rc |= (flags & EFLG_SF);
938 break;
939 case 5: /* p/pe */
940 rc |= (flags & EFLG_PF);
941 break;
942 case 7: /* le/ng */
943 rc |= (flags & EFLG_ZF);
944 /* fall through */
945 case 6: /* l/nge */
946 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
947 break;
948 }
949
950 /* Odd condition identifiers (lsb == 1) have inverted sense. */
951 return (!!rc ^ (condition & 1));
952}
953
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300954static void fetch_register_operand(struct operand *op)
955{
956 switch (op->bytes) {
957 case 1:
958 op->val = *(u8 *)op->addr.reg;
959 break;
960 case 2:
961 op->val = *(u16 *)op->addr.reg;
962 break;
963 case 4:
964 op->val = *(u32 *)op->addr.reg;
965 break;
966 case 8:
967 op->val = *(u64 *)op->addr.reg;
968 break;
969 }
970}
971
Avi Kivity12537912011-03-29 11:41:27 +0200972static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
973{
974 ctxt->ops->get_fpu(ctxt);
975 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200976 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
977 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
978 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
979 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
980 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
981 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
982 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
983 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200984#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200985 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
986 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
987 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
988 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
989 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
990 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
991 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
992 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200993#endif
994 default: BUG();
995 }
996 ctxt->ops->put_fpu(ctxt);
997}
998
999static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
1000 int reg)
1001{
1002 ctxt->ops->get_fpu(ctxt);
1003 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001004 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
1005 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
1006 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
1007 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
1008 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
1009 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
1010 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
1011 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001012#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001013 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
1014 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
1015 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
1016 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
1017 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
1018 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
1019 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
1020 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001021#endif
1022 default: BUG();
1023 }
1024 ctxt->ops->put_fpu(ctxt);
1025}
1026
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001027static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1028{
1029 ctxt->ops->get_fpu(ctxt);
1030 switch (reg) {
1031 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1032 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1033 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1034 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1035 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1036 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1037 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1038 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1039 default: BUG();
1040 }
1041 ctxt->ops->put_fpu(ctxt);
1042}
1043
1044static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1045{
1046 ctxt->ops->get_fpu(ctxt);
1047 switch (reg) {
1048 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1049 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1050 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1051 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1052 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1053 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1054 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1055 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1056 default: BUG();
1057 }
1058 ctxt->ops->put_fpu(ctxt);
1059}
1060
Gleb Natapov045a2822012-12-20 16:57:43 +02001061static int em_fninit(struct x86_emulate_ctxt *ctxt)
1062{
1063 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1064 return emulate_nm(ctxt);
1065
1066 ctxt->ops->get_fpu(ctxt);
1067 asm volatile("fninit");
1068 ctxt->ops->put_fpu(ctxt);
1069 return X86EMUL_CONTINUE;
1070}
1071
1072static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1073{
1074 u16 fcw;
1075
1076 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1077 return emulate_nm(ctxt);
1078
1079 ctxt->ops->get_fpu(ctxt);
1080 asm volatile("fnstcw %0": "+m"(fcw));
1081 ctxt->ops->put_fpu(ctxt);
1082
1083 /* force 2 byte destination */
1084 ctxt->dst.bytes = 2;
1085 ctxt->dst.val = fcw;
1086
1087 return X86EMUL_CONTINUE;
1088}
1089
1090static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1091{
1092 u16 fsw;
1093
1094 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1095 return emulate_nm(ctxt);
1096
1097 ctxt->ops->get_fpu(ctxt);
1098 asm volatile("fnstsw %0": "+m"(fsw));
1099 ctxt->ops->put_fpu(ctxt);
1100
1101 /* force 2 byte destination */
1102 ctxt->dst.bytes = 2;
1103 ctxt->dst.val = fsw;
1104
1105 return X86EMUL_CONTINUE;
1106}
1107
Avi Kivity12537912011-03-29 11:41:27 +02001108static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001109 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001110{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001111 unsigned reg = ctxt->modrm_reg;
1112 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +02001113
Avi Kivity9dac77f2011-06-01 15:34:25 +03001114 if (!(ctxt->d & ModRM))
1115 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +02001116
Avi Kivity9dac77f2011-06-01 15:34:25 +03001117 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001118 op->type = OP_XMM;
1119 op->bytes = 16;
1120 op->addr.xmm = reg;
1121 read_sse_reg(ctxt, &op->vec_val, reg);
1122 return;
1123 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001124 if (ctxt->d & Mmx) {
1125 reg &= 7;
1126 op->type = OP_MM;
1127 op->bytes = 8;
1128 op->addr.mm = reg;
1129 return;
1130 }
Avi Kivity12537912011-03-29 11:41:27 +02001131
Avi Kivity3c118e22007-10-31 10:27:04 +02001132 op->type = OP_REG;
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001133 if (ctxt->d & ByteOp) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001134 op->addr.reg = decode_register(ctxt, reg, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +02001135 op->bytes = 1;
1136 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001137 op->addr.reg = decode_register(ctxt, reg, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001138 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +02001139 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001140 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001141 op->orig_val = op->val;
1142}
1143
Avi Kivitya6e34072012-06-10 17:15:39 +03001144static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1145{
1146 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1147 ctxt->modrm_seg = VCPU_SREG_SS;
1148}
1149
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001150static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001151 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001152{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001153 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001154 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001155 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001156 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001157
Avi Kivity9dac77f2011-06-01 15:34:25 +03001158 if (ctxt->rex_prefix) {
1159 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
1160 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
1161 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001162 }
1163
Avi Kivity9dac77f2011-06-01 15:34:25 +03001164 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
1165 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
1166 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
1167 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001168
Avi Kivity9dac77f2011-06-01 15:34:25 +03001169 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001170 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001171 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001172 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm, ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001173 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001174 op->type = OP_XMM;
1175 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001176 op->addr.xmm = ctxt->modrm_rm;
1177 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001178 return rc;
1179 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001180 if (ctxt->d & Mmx) {
1181 op->type = OP_MM;
1182 op->bytes = 8;
1183 op->addr.xmm = ctxt->modrm_rm & 7;
1184 return rc;
1185 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001186 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001187 return rc;
1188 }
1189
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001190 op->type = OP_MEM;
1191
Avi Kivity9dac77f2011-06-01 15:34:25 +03001192 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001193 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1194 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1195 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1196 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001197
1198 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001199 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001200 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001201 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001202 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001203 break;
1204 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001205 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001206 break;
1207 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001208 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001209 break;
1210 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001211 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001212 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001213 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001214 break;
1215 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001216 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001217 break;
1218 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001219 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001220 break;
1221 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001222 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001223 break;
1224 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001225 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001226 break;
1227 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001228 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001229 break;
1230 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001231 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001232 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001233 break;
1234 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001235 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001236 break;
1237 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001238 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1239 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1240 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001241 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001242 } else {
1243 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001244 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001245 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001246 index_reg |= (sib >> 3) & 7;
1247 base_reg |= sib & 7;
1248 scale = sib >> 6;
1249
Avi Kivity9dac77f2011-06-01 15:34:25 +03001250 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001251 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001252 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001253 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001254 adjust_modrm_seg(ctxt, base_reg);
1255 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001256 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001257 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001258 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001259 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001260 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001261 } else {
1262 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001263 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001264 adjust_modrm_seg(ctxt, base_reg);
1265 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001266 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001267 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001268 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001269 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001270 break;
1271 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001272 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001273 break;
1274 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001275 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001276 break;
1277 }
1278 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001279 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001280done:
1281 return rc;
1282}
1283
1284static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001285 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001286{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001287 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001288
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001289 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001290 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001291 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001292 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001293 break;
1294 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001295 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001296 break;
1297 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001298 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001299 break;
1300 }
1301done:
1302 return rc;
1303}
1304
Avi Kivity9dac77f2011-06-01 15:34:25 +03001305static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001306{
Sheng Yang7129eec2010-09-28 16:33:32 +08001307 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001308
Avi Kivity9dac77f2011-06-01 15:34:25 +03001309 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1310 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001311
Avi Kivity9dac77f2011-06-01 15:34:25 +03001312 if (ctxt->src.bytes == 2)
1313 sv = (s16)ctxt->src.val & (s16)mask;
1314 else if (ctxt->src.bytes == 4)
1315 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001316
Avi Kivity9dac77f2011-06-01 15:34:25 +03001317 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001318 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001319
1320 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001321 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001322}
1323
Gleb Natapov9de41572010-04-28 19:15:22 +03001324static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001325 unsigned long addr, void *dest, unsigned size)
1326{
1327 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001328 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001329
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001330 if (mc->pos < mc->end)
1331 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001332
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001333 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001334
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001335 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1336 &ctxt->exception);
1337 if (rc != X86EMUL_CONTINUE)
1338 return rc;
1339
1340 mc->end += size;
1341
1342read_cached:
1343 memcpy(dest, mc->data + mc->pos, size);
1344 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001345 return X86EMUL_CONTINUE;
1346}
1347
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001348static int segmented_read(struct x86_emulate_ctxt *ctxt,
1349 struct segmented_address addr,
1350 void *data,
1351 unsigned size)
1352{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001353 int rc;
1354 ulong linear;
1355
Avi Kivity83b87952011-04-03 11:31:19 +03001356 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001357 if (rc != X86EMUL_CONTINUE)
1358 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001359 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001360}
1361
1362static int segmented_write(struct x86_emulate_ctxt *ctxt,
1363 struct segmented_address addr,
1364 const void *data,
1365 unsigned size)
1366{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001367 int rc;
1368 ulong linear;
1369
Avi Kivity83b87952011-04-03 11:31:19 +03001370 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001371 if (rc != X86EMUL_CONTINUE)
1372 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001373 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1374 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001375}
1376
1377static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1378 struct segmented_address addr,
1379 const void *orig_data, const void *data,
1380 unsigned size)
1381{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001382 int rc;
1383 ulong linear;
1384
Avi Kivity83b87952011-04-03 11:31:19 +03001385 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001386 if (rc != X86EMUL_CONTINUE)
1387 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001388 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1389 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001390}
1391
Gleb Natapov7b262e92010-03-18 15:20:27 +02001392static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001393 unsigned int size, unsigned short port,
1394 void *dest)
1395{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001396 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001397
1398 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001399 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001400 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001401 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001402 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001403 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1404 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Gleb Natapov7b262e92010-03-18 15:20:27 +02001405 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1406 count);
1407 if (n == 0)
1408 n = 1;
1409 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001410 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001411 return 0;
1412 rc->end = n * size;
1413 }
1414
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001415 if (ctxt->rep_prefix && !(ctxt->eflags & EFLG_DF)) {
1416 ctxt->dst.data = rc->data + rc->pos;
1417 ctxt->dst.type = OP_MEM_STR;
1418 ctxt->dst.count = (rc->end - rc->pos) / size;
1419 rc->pos = rc->end;
1420 } else {
1421 memcpy(dest, rc->data + rc->pos, size);
1422 rc->pos += size;
1423 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001424 return 1;
1425}
1426
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001427static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1428 u16 index, struct desc_struct *desc)
1429{
1430 struct desc_ptr dt;
1431 ulong addr;
1432
1433 ctxt->ops->get_idt(ctxt, &dt);
1434
1435 if (dt.size < index * 8 + 7)
1436 return emulate_gp(ctxt, index << 3 | 0x2);
1437
1438 addr = dt.address + index * 8;
1439 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1440 &ctxt->exception);
1441}
1442
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001443static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001444 u16 selector, struct desc_ptr *dt)
1445{
Mathias Krause0225fb52012-08-30 01:30:16 +02001446 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001447
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001448 if (selector & 1 << 2) {
1449 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001450 u16 sel;
1451
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001452 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001453 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001454 return;
1455
1456 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1457 dt->address = get_desc_base(&desc);
1458 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001459 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001460}
1461
1462/* allowed just for 8 bytes segments */
1463static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001464 u16 selector, struct desc_struct *desc,
1465 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001466{
1467 struct desc_ptr dt;
1468 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001469 ulong addr;
1470
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001471 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001472
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001473 if (dt.size < index * 8 + 7)
1474 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001475
Avi Kivitye9194642012-06-13 16:29:39 +03001476 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001477 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1478 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001479}
1480
1481/* allowed just for 8 bytes segments */
1482static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001483 u16 selector, struct desc_struct *desc)
1484{
1485 struct desc_ptr dt;
1486 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001487 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001488
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001489 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001490
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001491 if (dt.size < index * 8 + 7)
1492 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001493
1494 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001495 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1496 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001497}
1498
Gleb Natapov5601d052011-03-07 14:55:06 +02001499/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001500static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001501 u16 selector, int seg)
1502{
Avi Kivity869be992012-06-13 16:30:53 +03001503 struct desc_struct seg_desc, old_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001504 u8 dpl, rpl, cpl;
1505 unsigned err_vec = GP_VECTOR;
1506 u32 err_code = 0;
1507 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001508 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001509 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001510 u16 dummy;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001511
1512 memset(&seg_desc, 0, sizeof seg_desc);
1513
1514 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1515 || ctxt->mode == X86EMUL_MODE_REAL) {
1516 /* set real mode segment descriptor */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001517 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001518 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001519 goto load;
1520 }
1521
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001522 rpl = selector & 3;
1523 cpl = ctxt->ops->cpl(ctxt);
1524
1525 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1526 if ((seg == VCPU_SREG_CS
1527 || (seg == VCPU_SREG_SS
1528 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1529 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001530 && null_selector)
1531 goto exception;
1532
1533 /* TR should be in GDT only */
1534 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1535 goto exception;
1536
1537 if (null_selector) /* for NULL selector skip all following checks */
1538 goto load;
1539
Avi Kivitye9194642012-06-13 16:29:39 +03001540 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001541 if (ret != X86EMUL_CONTINUE)
1542 return ret;
1543
1544 err_code = selector & 0xfffc;
1545 err_vec = GP_VECTOR;
1546
Guo Chaofc058682012-06-28 15:19:51 +08001547 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001548 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1549 goto exception;
1550
1551 if (!seg_desc.p) {
1552 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1553 goto exception;
1554 }
1555
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001556 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001557
1558 switch (seg) {
1559 case VCPU_SREG_SS:
1560 /*
1561 * segment is not a writable data segment or segment
1562 * selector's RPL != CPL or segment selector's RPL != CPL
1563 */
1564 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1565 goto exception;
1566 break;
1567 case VCPU_SREG_CS:
1568 if (!(seg_desc.type & 8))
1569 goto exception;
1570
1571 if (seg_desc.type & 4) {
1572 /* conforming */
1573 if (dpl > cpl)
1574 goto exception;
1575 } else {
1576 /* nonconforming */
1577 if (rpl > cpl || dpl != cpl)
1578 goto exception;
1579 }
1580 /* CS(RPL) <- CPL */
1581 selector = (selector & 0xfffc) | cpl;
1582 break;
1583 case VCPU_SREG_TR:
1584 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1585 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001586 old_desc = seg_desc;
1587 seg_desc.type |= 2; /* busy */
1588 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1589 sizeof(seg_desc), &ctxt->exception);
1590 if (ret != X86EMUL_CONTINUE)
1591 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001592 break;
1593 case VCPU_SREG_LDTR:
1594 if (seg_desc.s || seg_desc.type != 2)
1595 goto exception;
1596 break;
1597 default: /* DS, ES, FS, or GS */
1598 /*
1599 * segment is not a data or readable code segment or
1600 * ((segment is a data or nonconforming code segment)
1601 * and (both RPL and CPL > DPL))
1602 */
1603 if ((seg_desc.type & 0xa) == 0x8 ||
1604 (((seg_desc.type & 0xc) != 0xc) &&
1605 (rpl > dpl && cpl > dpl)))
1606 goto exception;
1607 break;
1608 }
1609
1610 if (seg_desc.s) {
1611 /* mark segment as accessed */
1612 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001613 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001614 if (ret != X86EMUL_CONTINUE)
1615 return ret;
1616 }
1617load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001618 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001619 return X86EMUL_CONTINUE;
1620exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001621 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001622 return X86EMUL_PROPAGATE_FAULT;
1623}
1624
Wei Yongjun31be40b2010-08-17 09:17:30 +08001625static void write_register_operand(struct operand *op)
1626{
1627 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1628 switch (op->bytes) {
1629 case 1:
1630 *(u8 *)op->addr.reg = (u8)op->val;
1631 break;
1632 case 2:
1633 *(u16 *)op->addr.reg = (u16)op->val;
1634 break;
1635 case 4:
1636 *op->addr.reg = (u32)op->val;
1637 break; /* 64b: zero-extend */
1638 case 8:
1639 *op->addr.reg = op->val;
1640 break;
1641 }
1642}
1643
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001644static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001645{
1646 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001647
Avi Kivityb6744dc2013-01-04 16:18:50 +02001648 if (ctxt->d & NoWrite)
1649 return X86EMUL_CONTINUE;
1650
Avi Kivity9dac77f2011-06-01 15:34:25 +03001651 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001652 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001653 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001654 break;
1655 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001656 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001657 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001658 ctxt->dst.addr.mem,
1659 &ctxt->dst.orig_val,
1660 &ctxt->dst.val,
1661 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001662 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001663 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001664 ctxt->dst.addr.mem,
1665 &ctxt->dst.val,
1666 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001667 if (rc != X86EMUL_CONTINUE)
1668 return rc;
1669 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001670 case OP_MEM_STR:
1671 rc = segmented_write(ctxt,
1672 ctxt->dst.addr.mem,
1673 ctxt->dst.data,
1674 ctxt->dst.bytes * ctxt->dst.count);
1675 if (rc != X86EMUL_CONTINUE)
1676 return rc;
1677 break;
Avi Kivity12537912011-03-29 11:41:27 +02001678 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001679 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001680 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001681 case OP_MM:
1682 write_mmx_reg(ctxt, &ctxt->dst.mm_val, ctxt->dst.addr.mm);
1683 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001684 case OP_NONE:
1685 /* no writeback */
1686 break;
1687 default:
1688 break;
1689 }
1690 return X86EMUL_CONTINUE;
1691}
1692
Avi Kivity51ddff52012-06-12 20:19:40 +03001693static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001694{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001695 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001696
Avi Kivity5ad105e2012-08-19 14:34:31 +03001697 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001698 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001699 addr.seg = VCPU_SREG_SS;
1700
Avi Kivity51ddff52012-06-12 20:19:40 +03001701 return segmented_write(ctxt, addr, data, bytes);
1702}
1703
1704static int em_push(struct x86_emulate_ctxt *ctxt)
1705{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001706 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001707 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001708 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001709}
1710
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001711static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001712 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001713{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001714 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001715 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001716
Avi Kivitydd856ef2012-08-27 23:46:17 +03001717 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001718 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001719 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001720 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001721 return rc;
1722
Avi Kivity5ad105e2012-08-19 14:34:31 +03001723 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001724 return rc;
1725}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001726
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001727static int em_pop(struct x86_emulate_ctxt *ctxt)
1728{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001729 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001730}
1731
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001732static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001733 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001734{
1735 int rc;
1736 unsigned long val, change_mask;
1737 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001738 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001739
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001740 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001741 if (rc != X86EMUL_CONTINUE)
1742 return rc;
1743
1744 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1745 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1746
1747 switch(ctxt->mode) {
1748 case X86EMUL_MODE_PROT64:
1749 case X86EMUL_MODE_PROT32:
1750 case X86EMUL_MODE_PROT16:
1751 if (cpl == 0)
1752 change_mask |= EFLG_IOPL;
1753 if (cpl <= iopl)
1754 change_mask |= EFLG_IF;
1755 break;
1756 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001757 if (iopl < 3)
1758 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001759 change_mask |= EFLG_IF;
1760 break;
1761 default: /* real mode */
1762 change_mask |= (EFLG_IOPL | EFLG_IF);
1763 break;
1764 }
1765
1766 *(unsigned long *)dest =
1767 (ctxt->eflags & ~change_mask) | (val & change_mask);
1768
1769 return rc;
1770}
1771
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001772static int em_popf(struct x86_emulate_ctxt *ctxt)
1773{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001774 ctxt->dst.type = OP_REG;
1775 ctxt->dst.addr.reg = &ctxt->eflags;
1776 ctxt->dst.bytes = ctxt->op_bytes;
1777 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001778}
1779
Avi Kivity612e89f2012-06-12 20:03:23 +03001780static int em_enter(struct x86_emulate_ctxt *ctxt)
1781{
1782 int rc;
1783 unsigned frame_size = ctxt->src.val;
1784 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001785 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001786
1787 if (nesting_level)
1788 return X86EMUL_UNHANDLEABLE;
1789
Avi Kivitydd856ef2012-08-27 23:46:17 +03001790 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1791 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001792 if (rc != X86EMUL_CONTINUE)
1793 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001794 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001795 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001796 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1797 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001798 stack_mask(ctxt));
1799 return X86EMUL_CONTINUE;
1800}
1801
Avi Kivityf47cfa32012-06-07 17:49:24 +03001802static int em_leave(struct x86_emulate_ctxt *ctxt)
1803{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001804 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001805 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001806 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001807}
1808
Avi Kivity1cd196e2011-09-13 10:45:51 +03001809static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001810{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001811 int seg = ctxt->src2.val;
1812
Avi Kivity9dac77f2011-06-01 15:34:25 +03001813 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001814
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001815 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001816}
1817
Avi Kivity1cd196e2011-09-13 10:45:51 +03001818static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001819{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001820 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001821 unsigned long selector;
1822 int rc;
1823
Avi Kivity9dac77f2011-06-01 15:34:25 +03001824 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001825 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001826 return rc;
1827
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001828 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001829 return rc;
1830}
1831
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001832static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001833{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001834 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001835 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001836 int reg = VCPU_REGS_RAX;
1837
1838 while (reg <= VCPU_REGS_RDI) {
1839 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001840 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001841
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001842 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001843 if (rc != X86EMUL_CONTINUE)
1844 return rc;
1845
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001846 ++reg;
1847 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001848
Wei Yongjunc37eda12010-06-15 09:03:33 +08001849 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001850}
1851
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001852static int em_pushf(struct x86_emulate_ctxt *ctxt)
1853{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001854 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001855 return em_push(ctxt);
1856}
1857
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001858static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001859{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001860 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001861 int reg = VCPU_REGS_RDI;
1862
1863 while (reg >= VCPU_REGS_RAX) {
1864 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001865 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001866 --reg;
1867 }
1868
Avi Kivitydd856ef2012-08-27 23:46:17 +03001869 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001870 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001871 break;
1872 --reg;
1873 }
1874 return rc;
1875}
1876
Avi Kivitydd856ef2012-08-27 23:46:17 +03001877static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001878{
Mathias Krause0225fb52012-08-30 01:30:16 +02001879 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001880 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001881 struct desc_ptr dt;
1882 gva_t cs_addr;
1883 gva_t eip_addr;
1884 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001885
1886 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001887 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001888 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001889 if (rc != X86EMUL_CONTINUE)
1890 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001891
1892 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1893
Avi Kivity9dac77f2011-06-01 15:34:25 +03001894 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001895 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001896 if (rc != X86EMUL_CONTINUE)
1897 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001898
Avi Kivity9dac77f2011-06-01 15:34:25 +03001899 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001900 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001901 if (rc != X86EMUL_CONTINUE)
1902 return rc;
1903
Avi Kivity4bff1e862011-04-20 13:37:53 +03001904 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001905
1906 eip_addr = dt.address + (irq << 2);
1907 cs_addr = dt.address + (irq << 2) + 2;
1908
Avi Kivity0f65dd72011-04-20 13:37:53 +03001909 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001910 if (rc != X86EMUL_CONTINUE)
1911 return rc;
1912
Avi Kivity0f65dd72011-04-20 13:37:53 +03001913 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001914 if (rc != X86EMUL_CONTINUE)
1915 return rc;
1916
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001917 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001918 if (rc != X86EMUL_CONTINUE)
1919 return rc;
1920
Avi Kivity9dac77f2011-06-01 15:34:25 +03001921 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001922
1923 return rc;
1924}
1925
Avi Kivitydd856ef2012-08-27 23:46:17 +03001926int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1927{
1928 int rc;
1929
1930 invalidate_registers(ctxt);
1931 rc = __emulate_int_real(ctxt, irq);
1932 if (rc == X86EMUL_CONTINUE)
1933 writeback_registers(ctxt);
1934 return rc;
1935}
1936
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001937static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001938{
1939 switch(ctxt->mode) {
1940 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001941 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001942 case X86EMUL_MODE_VM86:
1943 case X86EMUL_MODE_PROT16:
1944 case X86EMUL_MODE_PROT32:
1945 case X86EMUL_MODE_PROT64:
1946 default:
1947 /* Protected mode interrupts unimplemented yet */
1948 return X86EMUL_UNHANDLEABLE;
1949 }
1950}
1951
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001952static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001953{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001954 int rc = X86EMUL_CONTINUE;
1955 unsigned long temp_eip = 0;
1956 unsigned long temp_eflags = 0;
1957 unsigned long cs = 0;
1958 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1959 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1960 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1961 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1962
1963 /* TODO: Add stack limit check */
1964
Avi Kivity9dac77f2011-06-01 15:34:25 +03001965 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001966
1967 if (rc != X86EMUL_CONTINUE)
1968 return rc;
1969
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001970 if (temp_eip & ~0xffff)
1971 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001972
Avi Kivity9dac77f2011-06-01 15:34:25 +03001973 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001974
1975 if (rc != X86EMUL_CONTINUE)
1976 return rc;
1977
Avi Kivity9dac77f2011-06-01 15:34:25 +03001978 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001979
1980 if (rc != X86EMUL_CONTINUE)
1981 return rc;
1982
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001983 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001984
1985 if (rc != X86EMUL_CONTINUE)
1986 return rc;
1987
Avi Kivity9dac77f2011-06-01 15:34:25 +03001988 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001989
1990
Avi Kivity9dac77f2011-06-01 15:34:25 +03001991 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001992 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001993 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001994 ctxt->eflags &= ~0xffff;
1995 ctxt->eflags |= temp_eflags;
1996 }
1997
1998 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1999 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
2000
2001 return rc;
2002}
2003
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002004static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002005{
2006 switch(ctxt->mode) {
2007 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002008 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002009 case X86EMUL_MODE_VM86:
2010 case X86EMUL_MODE_PROT16:
2011 case X86EMUL_MODE_PROT32:
2012 case X86EMUL_MODE_PROT64:
2013 default:
2014 /* iret from protected mode unimplemented yet */
2015 return X86EMUL_UNHANDLEABLE;
2016 }
2017}
2018
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002019static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2020{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002021 int rc;
2022 unsigned short sel;
2023
Avi Kivity9dac77f2011-06-01 15:34:25 +03002024 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002025
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002026 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002027 if (rc != X86EMUL_CONTINUE)
2028 return rc;
2029
Avi Kivity9dac77f2011-06-01 15:34:25 +03002030 ctxt->_eip = 0;
2031 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002032 return X86EMUL_CONTINUE;
2033}
2034
Takuya Yoshikawa51187682011-05-02 02:29:17 +09002035static int em_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002036{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002037 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002038 case 0: /* rol */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002039 emulate_2op_SrcB(ctxt, "rol");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002040 break;
2041 case 1: /* ror */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002042 emulate_2op_SrcB(ctxt, "ror");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002043 break;
2044 case 2: /* rcl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002045 emulate_2op_SrcB(ctxt, "rcl");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002046 break;
2047 case 3: /* rcr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002048 emulate_2op_SrcB(ctxt, "rcr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002049 break;
2050 case 4: /* sal/shl */
2051 case 6: /* sal/shl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002052 emulate_2op_SrcB(ctxt, "sal");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002053 break;
2054 case 5: /* shr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002055 emulate_2op_SrcB(ctxt, "shr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002056 break;
2057 case 7: /* sar */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002058 emulate_2op_SrcB(ctxt, "sar");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002059 break;
2060 }
Takuya Yoshikawa51187682011-05-02 02:29:17 +09002061 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002062}
2063
Avi Kivity45a14672013-01-04 16:18:52 +02002064FASTOP1(not);
2065FASTOP1(neg);
Avi Kivity3329ece2011-09-13 10:45:39 +03002066
2067static int em_mul_ex(struct x86_emulate_ctxt *ctxt)
2068{
2069 u8 ex = 0;
2070
2071 emulate_1op_rax_rdx(ctxt, "mul", ex);
2072 return X86EMUL_CONTINUE;
2073}
2074
2075static int em_imul_ex(struct x86_emulate_ctxt *ctxt)
2076{
2077 u8 ex = 0;
2078
2079 emulate_1op_rax_rdx(ctxt, "imul", ex);
2080 return X86EMUL_CONTINUE;
2081}
2082
2083static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002084{
Avi Kivity34d1f492010-08-26 11:59:01 +03002085 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002086
Avi Kivity3329ece2011-09-13 10:45:39 +03002087 emulate_1op_rax_rdx(ctxt, "div", de);
2088 if (de)
2089 return emulate_de(ctxt);
2090 return X86EMUL_CONTINUE;
2091}
2092
2093static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
2094{
2095 u8 de = 0;
2096
2097 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03002098 if (de)
2099 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03002100 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002101}
2102
Takuya Yoshikawa51187682011-05-02 02:29:17 +09002103static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002104{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002105 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002106
Avi Kivity9dac77f2011-06-01 15:34:25 +03002107 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002108 case 0: /* inc */
Avi Kivityd1eef452011-09-07 16:41:38 +03002109 emulate_1op(ctxt, "inc");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002110 break;
2111 case 1: /* dec */
Avi Kivityd1eef452011-09-07 16:41:38 +03002112 emulate_1op(ctxt, "dec");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002113 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03002114 case 2: /* call near abs */ {
2115 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002116 old_eip = ctxt->_eip;
2117 ctxt->_eip = ctxt->src.val;
2118 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002119 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03002120 break;
2121 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002122 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002123 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002124 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002125 case 5: /* jmp far */
2126 rc = em_jmp_far(ctxt);
2127 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002128 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002129 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002130 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002131 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002132 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002133}
2134
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002135static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002136{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002137 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002138
Avi Kivitydd856ef2012-08-27 23:46:17 +03002139 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2140 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2141 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2142 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002143 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002144 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002145 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2146 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002147
Laurent Vivier05f086f2007-09-24 11:10:55 +02002148 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002149 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002150 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002151}
2152
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002153static int em_ret(struct x86_emulate_ctxt *ctxt)
2154{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002155 ctxt->dst.type = OP_REG;
2156 ctxt->dst.addr.reg = &ctxt->_eip;
2157 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002158 return em_pop(ctxt);
2159}
2160
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002161static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002162{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002163 int rc;
2164 unsigned long cs;
2165
Avi Kivity9dac77f2011-06-01 15:34:25 +03002166 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002167 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002168 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002169 if (ctxt->op_bytes == 4)
2170 ctxt->_eip = (u32)ctxt->_eip;
2171 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002172 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002173 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002174 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002175 return rc;
2176}
2177
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002178static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2179{
2180 /* Save real source value, then compare EAX against destination. */
2181 ctxt->src.orig_val = ctxt->src.val;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002182 ctxt->src.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002183 emulate_2op_SrcV(ctxt, "cmp");
2184
2185 if (ctxt->eflags & EFLG_ZF) {
2186 /* Success: write back to memory. */
2187 ctxt->dst.val = ctxt->src.orig_val;
2188 } else {
2189 /* Failure: write the value we saw to EAX. */
2190 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002191 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002192 }
2193 return X86EMUL_CONTINUE;
2194}
2195
Avi Kivityd4b43252011-09-13 10:45:50 +03002196static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002197{
Avi Kivityd4b43252011-09-13 10:45:50 +03002198 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002199 unsigned short sel;
2200 int rc;
2201
Avi Kivity9dac77f2011-06-01 15:34:25 +03002202 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002203
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002204 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002205 if (rc != X86EMUL_CONTINUE)
2206 return rc;
2207
Avi Kivity9dac77f2011-06-01 15:34:25 +03002208 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002209 return rc;
2210}
2211
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002212static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002213setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002214 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002215{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002216 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002217 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002218 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002219 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002220 cs->type = 0x0b; /* Read, Execute, Accessed */
2221 cs->s = 1;
2222 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002223 cs->p = 1;
2224 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002225 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002226
Gleb Natapov79168fd2010-04-28 19:15:30 +03002227 set_desc_base(ss, 0); /* flat segment */
2228 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002229 ss->g = 1; /* 4kb granularity */
2230 ss->s = 1;
2231 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002232 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002233 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002234 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002235 ss->l = 0;
2236 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002237}
2238
Avi Kivity1a18a692012-02-01 12:23:21 +02002239static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2240{
2241 u32 eax, ebx, ecx, edx;
2242
2243 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002244 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2245 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002246 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2247 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2248}
2249
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002250static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2251{
Mathias Krause0225fb52012-08-30 01:30:16 +02002252 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002253 u32 eax, ebx, ecx, edx;
2254
2255 /*
2256 * syscall should always be enabled in longmode - so only become
2257 * vendor specific (cpuid) if other modes are active...
2258 */
2259 if (ctxt->mode == X86EMUL_MODE_PROT64)
2260 return true;
2261
2262 eax = 0x00000000;
2263 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002264 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2265 /*
2266 * Intel ("GenuineIntel")
2267 * remark: Intel CPUs only support "syscall" in 64bit
2268 * longmode. Also an 64bit guest with a
2269 * 32bit compat-app running will #UD !! While this
2270 * behaviour can be fixed (by emulating) into AMD
2271 * response - CPUs of AMD can't behave like Intel.
2272 */
2273 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2274 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2275 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2276 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002277
Avi Kivity0017f932012-06-07 14:10:16 +03002278 /* AMD ("AuthenticAMD") */
2279 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2280 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2281 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2282 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002283
Avi Kivity0017f932012-06-07 14:10:16 +03002284 /* AMD ("AMDisbetter!") */
2285 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2286 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2287 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2288 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002289
2290 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2291 return false;
2292}
2293
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002294static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002295{
Mathias Krause0225fb52012-08-30 01:30:16 +02002296 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002297 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002298 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002299 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002300 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002301
2302 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002303 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002304 ctxt->mode == X86EMUL_MODE_VM86)
2305 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002306
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002307 if (!(em_syscall_is_enabled(ctxt)))
2308 return emulate_ud(ctxt);
2309
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002310 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002311 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002312
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002313 if (!(efer & EFER_SCE))
2314 return emulate_ud(ctxt);
2315
Avi Kivity717746e2011-04-20 13:37:53 +03002316 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002317 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002318 cs_sel = (u16)(msr_data & 0xfffc);
2319 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002320
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002321 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002322 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002323 cs.l = 1;
2324 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002325 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2326 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002327
Avi Kivitydd856ef2012-08-27 23:46:17 +03002328 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002329 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002330#ifdef CONFIG_X86_64
Avi Kivitydd856ef2012-08-27 23:46:17 +03002331 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002332
Avi Kivity717746e2011-04-20 13:37:53 +03002333 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002334 ctxt->mode == X86EMUL_MODE_PROT64 ?
2335 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002336 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002337
Avi Kivity717746e2011-04-20 13:37:53 +03002338 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002339 ctxt->eflags &= ~(msr_data | EFLG_RF);
2340#endif
2341 } else {
2342 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002343 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002344 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002345
2346 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2347 }
2348
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002349 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002350}
2351
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002352static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002353{
Mathias Krause0225fb52012-08-30 01:30:16 +02002354 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002355 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002356 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002357 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002358 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002359
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002360 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002361 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002362 if (ctxt->mode == X86EMUL_MODE_REAL)
2363 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002364
Avi Kivity1a18a692012-02-01 12:23:21 +02002365 /*
2366 * Not recognized on AMD in compat mode (but is recognized in legacy
2367 * mode).
2368 */
2369 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2370 && !vendor_intel(ctxt))
2371 return emulate_ud(ctxt);
2372
Andre Przywara8c604352009-06-18 12:56:01 +02002373 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2374 * Therefore, we inject an #UD.
2375 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002376 if (ctxt->mode == X86EMUL_MODE_PROT64)
2377 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002378
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002379 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002380
Avi Kivity717746e2011-04-20 13:37:53 +03002381 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002382 switch (ctxt->mode) {
2383 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002384 if ((msr_data & 0xfffc) == 0x0)
2385 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002386 break;
2387 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002388 if (msr_data == 0x0)
2389 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002390 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002391 default:
2392 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002393 }
2394
2395 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002396 cs_sel = (u16)msr_data;
2397 cs_sel &= ~SELECTOR_RPL_MASK;
2398 ss_sel = cs_sel + 8;
2399 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002400 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002401 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002402 cs.l = 1;
2403 }
2404
Avi Kivity1aa36612011-04-27 13:20:30 +03002405 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2406 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002407
Avi Kivity717746e2011-04-20 13:37:53 +03002408 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002409 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002410
Avi Kivity717746e2011-04-20 13:37:53 +03002411 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002412 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002413
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002414 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002415}
2416
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002417static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002418{
Mathias Krause0225fb52012-08-30 01:30:16 +02002419 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002420 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002421 u64 msr_data;
2422 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002423 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002424
Gleb Natapova0044752010-02-10 14:21:31 +02002425 /* inject #GP if in real mode or Virtual 8086 mode */
2426 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002427 ctxt->mode == X86EMUL_MODE_VM86)
2428 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002429
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002430 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002431
Avi Kivity9dac77f2011-06-01 15:34:25 +03002432 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002433 usermode = X86EMUL_MODE_PROT64;
2434 else
2435 usermode = X86EMUL_MODE_PROT32;
2436
2437 cs.dpl = 3;
2438 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002439 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002440 switch (usermode) {
2441 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002442 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002443 if ((msr_data & 0xfffc) == 0x0)
2444 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002445 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002446 break;
2447 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002448 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002449 if (msr_data == 0x0)
2450 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002451 ss_sel = cs_sel + 8;
2452 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002453 cs.l = 1;
2454 break;
2455 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002456 cs_sel |= SELECTOR_RPL_MASK;
2457 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002458
Avi Kivity1aa36612011-04-27 13:20:30 +03002459 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2460 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002461
Avi Kivitydd856ef2012-08-27 23:46:17 +03002462 ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX);
2463 *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX);
Andre Przywara4668f052009-06-18 12:56:02 +02002464
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002465 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002466}
2467
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002468static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002469{
2470 int iopl;
2471 if (ctxt->mode == X86EMUL_MODE_REAL)
2472 return false;
2473 if (ctxt->mode == X86EMUL_MODE_VM86)
2474 return true;
2475 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002476 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002477}
2478
2479static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002480 u16 port, u16 len)
2481{
Mathias Krause0225fb52012-08-30 01:30:16 +02002482 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002483 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002484 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002485 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002486 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002487 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002488 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002489
Avi Kivity1aa36612011-04-27 13:20:30 +03002490 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002491 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002492 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002493 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002494 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002495 base = get_desc_base(&tr_seg);
2496#ifdef CONFIG_X86_64
2497 base |= ((u64)base3) << 32;
2498#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002499 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002500 if (r != X86EMUL_CONTINUE)
2501 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002502 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002503 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002504 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002505 if (r != X86EMUL_CONTINUE)
2506 return false;
2507 if ((perm >> bit_idx) & mask)
2508 return false;
2509 return true;
2510}
2511
2512static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002513 u16 port, u16 len)
2514{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002515 if (ctxt->perm_ok)
2516 return true;
2517
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002518 if (emulator_bad_iopl(ctxt))
2519 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002520 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002521
2522 ctxt->perm_ok = true;
2523
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002524 return true;
2525}
2526
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002527static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002528 struct tss_segment_16 *tss)
2529{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002530 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002531 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002532 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2533 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2534 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2535 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2536 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2537 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2538 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2539 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002540
Avi Kivity1aa36612011-04-27 13:20:30 +03002541 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2542 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2543 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2544 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2545 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002546}
2547
2548static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002549 struct tss_segment_16 *tss)
2550{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002551 int ret;
2552
Avi Kivity9dac77f2011-06-01 15:34:25 +03002553 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002554 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002555 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2556 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2557 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2558 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2559 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2560 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2561 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2562 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002563
2564 /*
2565 * SDM says that segment selectors are loaded before segment
2566 * descriptors
2567 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002568 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2569 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2570 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2571 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2572 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002573
2574 /*
Guo Chaofc058682012-06-28 15:19:51 +08002575 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002576 * it is handled in a context of new task
2577 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002578 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002579 if (ret != X86EMUL_CONTINUE)
2580 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002581 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002582 if (ret != X86EMUL_CONTINUE)
2583 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002584 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002585 if (ret != X86EMUL_CONTINUE)
2586 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002587 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002588 if (ret != X86EMUL_CONTINUE)
2589 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002590 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002591 if (ret != X86EMUL_CONTINUE)
2592 return ret;
2593
2594 return X86EMUL_CONTINUE;
2595}
2596
2597static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002598 u16 tss_selector, u16 old_tss_sel,
2599 ulong old_tss_base, struct desc_struct *new_desc)
2600{
Mathias Krause0225fb52012-08-30 01:30:16 +02002601 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002602 struct tss_segment_16 tss_seg;
2603 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002604 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002605
Avi Kivity0f65dd72011-04-20 13:37:53 +03002606 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002607 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002608 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002609 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002610 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002611
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002612 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002613
Avi Kivity0f65dd72011-04-20 13:37:53 +03002614 ret = ops->write_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
Avi Kivity0f65dd72011-04-20 13:37:53 +03002620 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002621 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002622 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002623 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002624 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002625
2626 if (old_tss_sel != 0xffff) {
2627 tss_seg.prev_task_link = old_tss_sel;
2628
Avi Kivity0f65dd72011-04-20 13:37:53 +03002629 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002630 &tss_seg.prev_task_link,
2631 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002632 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002633 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002634 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002635 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002636 }
2637
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002638 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002639}
2640
2641static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002642 struct tss_segment_32 *tss)
2643{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002644 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002645 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002646 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002647 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2648 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2649 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2650 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2651 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2652 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2653 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2654 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002655
Avi Kivity1aa36612011-04-27 13:20:30 +03002656 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2657 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2658 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2659 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2660 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2661 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2662 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002663}
2664
2665static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002666 struct tss_segment_32 *tss)
2667{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002668 int ret;
2669
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002670 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002671 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002672 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002673 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002674
2675 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002676 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2677 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2678 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2679 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2680 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2681 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2682 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2683 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002684
2685 /*
2686 * SDM says that segment selectors are loaded before segment
2687 * descriptors
2688 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002689 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2690 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2691 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2692 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2693 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2694 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2695 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002696
2697 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002698 * If we're switching between Protected Mode and VM86, we need to make
2699 * sure to update the mode before loading the segment descriptors so
2700 * that the selectors are interpreted correctly.
2701 *
2702 * Need to get rflags to the vcpu struct immediately because it
2703 * influences the CPL which is checked at least when loading the segment
2704 * descriptors and when pushing an error code to the new kernel stack.
2705 *
2706 * TODO Introduce a separate ctxt->ops->set_cpl callback
2707 */
2708 if (ctxt->eflags & X86_EFLAGS_VM)
2709 ctxt->mode = X86EMUL_MODE_VM86;
2710 else
2711 ctxt->mode = X86EMUL_MODE_PROT32;
2712
2713 ctxt->ops->set_rflags(ctxt, ctxt->eflags);
2714
2715 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002716 * Now load segment descriptors. If fault happenes at this stage
2717 * it is handled in a context of new task
2718 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002719 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002720 if (ret != X86EMUL_CONTINUE)
2721 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002722 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002723 if (ret != X86EMUL_CONTINUE)
2724 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002725 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002726 if (ret != X86EMUL_CONTINUE)
2727 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002728 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002729 if (ret != X86EMUL_CONTINUE)
2730 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002731 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002732 if (ret != X86EMUL_CONTINUE)
2733 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002734 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002735 if (ret != X86EMUL_CONTINUE)
2736 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002737 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002738 if (ret != X86EMUL_CONTINUE)
2739 return ret;
2740
2741 return X86EMUL_CONTINUE;
2742}
2743
2744static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002745 u16 tss_selector, u16 old_tss_sel,
2746 ulong old_tss_base, struct desc_struct *new_desc)
2747{
Mathias Krause0225fb52012-08-30 01:30:16 +02002748 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002749 struct tss_segment_32 tss_seg;
2750 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002751 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002752
Avi Kivity0f65dd72011-04-20 13:37:53 +03002753 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002754 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002755 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002756 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002757 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002758
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002759 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002760
Avi Kivity0f65dd72011-04-20 13:37:53 +03002761 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002762 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002763 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002764 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002765 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002766
Avi Kivity0f65dd72011-04-20 13:37:53 +03002767 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002768 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002769 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002770 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002771 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002772
2773 if (old_tss_sel != 0xffff) {
2774 tss_seg.prev_task_link = old_tss_sel;
2775
Avi Kivity0f65dd72011-04-20 13:37:53 +03002776 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002777 &tss_seg.prev_task_link,
2778 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002779 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002780 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002781 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002782 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002783 }
2784
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002785 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002786}
2787
2788static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002789 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002790 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002791{
Mathias Krause0225fb52012-08-30 01:30:16 +02002792 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002793 struct desc_struct curr_tss_desc, next_tss_desc;
2794 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002795 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002796 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002797 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002798 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002799 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002800
2801 /* FIXME: old_tss_base == ~0 ? */
2802
Avi Kivitye9194642012-06-13 16:29:39 +03002803 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002804 if (ret != X86EMUL_CONTINUE)
2805 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002806 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002807 if (ret != X86EMUL_CONTINUE)
2808 return ret;
2809
2810 /* FIXME: check that next_tss_desc is tss */
2811
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002812 /*
2813 * Check privileges. The three cases are task switch caused by...
2814 *
2815 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2816 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002817 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002818 */
2819 if (reason == TASK_SWITCH_GATE) {
2820 if (idt_index != -1) {
2821 /* Software interrupts */
2822 struct desc_struct task_gate_desc;
2823 int dpl;
2824
2825 ret = read_interrupt_descriptor(ctxt, idt_index,
2826 &task_gate_desc);
2827 if (ret != X86EMUL_CONTINUE)
2828 return ret;
2829
2830 dpl = task_gate_desc.dpl;
2831 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2832 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2833 }
2834 } else if (reason != TASK_SWITCH_IRET) {
2835 int dpl = next_tss_desc.dpl;
2836 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2837 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002838 }
2839
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002840
Gleb Natapovceffb452010-03-18 15:20:19 +02002841 desc_limit = desc_limit_scaled(&next_tss_desc);
2842 if (!next_tss_desc.p ||
2843 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2844 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002845 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002846 return X86EMUL_PROPAGATE_FAULT;
2847 }
2848
2849 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2850 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002851 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002852 }
2853
2854 if (reason == TASK_SWITCH_IRET)
2855 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2856
2857 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002858 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002859 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2860 old_tss_sel = 0xffff;
2861
2862 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002863 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002864 old_tss_base, &next_tss_desc);
2865 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002866 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002867 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002868 if (ret != X86EMUL_CONTINUE)
2869 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002870
2871 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2872 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2873
2874 if (reason != TASK_SWITCH_IRET) {
2875 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002876 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002877 }
2878
Avi Kivity717746e2011-04-20 13:37:53 +03002879 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002880 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002881
Jan Kiszkae269fb22010-04-14 15:51:09 +02002882 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002883 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2884 ctxt->lock_prefix = 0;
2885 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002886 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002887 }
2888
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002889 return ret;
2890}
2891
2892int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002893 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002894 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002895{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002896 int rc;
2897
Avi Kivitydd856ef2012-08-27 23:46:17 +03002898 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002899 ctxt->_eip = ctxt->eip;
2900 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002901
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002902 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002903 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002904
Avi Kivitydd856ef2012-08-27 23:46:17 +03002905 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002906 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002907 writeback_registers(ctxt);
2908 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002909
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002910 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002911}
2912
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002913static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2914 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002915{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002916 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002917
Avi Kivitydd856ef2012-08-27 23:46:17 +03002918 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2919 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002920}
2921
Avi Kivity7af04fc2010-08-18 14:16:35 +03002922static int em_das(struct x86_emulate_ctxt *ctxt)
2923{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002924 u8 al, old_al;
2925 bool af, cf, old_cf;
2926
2927 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002928 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002929
2930 old_al = al;
2931 old_cf = cf;
2932 cf = false;
2933 af = ctxt->eflags & X86_EFLAGS_AF;
2934 if ((al & 0x0f) > 9 || af) {
2935 al -= 6;
2936 cf = old_cf | (al >= 250);
2937 af = true;
2938 } else {
2939 af = false;
2940 }
2941 if (old_al > 0x99 || old_cf) {
2942 al -= 0x60;
2943 cf = true;
2944 }
2945
Avi Kivity9dac77f2011-06-01 15:34:25 +03002946 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002947 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002948 ctxt->src.type = OP_IMM;
2949 ctxt->src.val = 0;
2950 ctxt->src.bytes = 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002951 emulate_2op_SrcV(ctxt, "or");
Avi Kivity7af04fc2010-08-18 14:16:35 +03002952 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2953 if (cf)
2954 ctxt->eflags |= X86_EFLAGS_CF;
2955 if (af)
2956 ctxt->eflags |= X86_EFLAGS_AF;
2957 return X86EMUL_CONTINUE;
2958}
2959
Gleb Natapov7f662272012-12-10 11:42:30 +02002960static int em_aad(struct x86_emulate_ctxt *ctxt)
2961{
2962 u8 al = ctxt->dst.val & 0xff;
2963 u8 ah = (ctxt->dst.val >> 8) & 0xff;
2964
2965 al = (al + (ah * ctxt->src.val)) & 0xff;
2966
2967 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
2968
2969 ctxt->eflags &= ~(X86_EFLAGS_PF | X86_EFLAGS_SF | X86_EFLAGS_ZF);
2970
2971 if (!al)
2972 ctxt->eflags |= X86_EFLAGS_ZF;
2973 if (!(al & 1))
2974 ctxt->eflags |= X86_EFLAGS_PF;
2975 if (al & 0x80)
2976 ctxt->eflags |= X86_EFLAGS_SF;
2977
2978 return X86EMUL_CONTINUE;
2979}
2980
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002981static int em_call(struct x86_emulate_ctxt *ctxt)
2982{
2983 long rel = ctxt->src.val;
2984
2985 ctxt->src.val = (unsigned long)ctxt->_eip;
2986 jmp_rel(ctxt, rel);
2987 return em_push(ctxt);
2988}
2989
Avi Kivity0ef753b2010-08-18 14:51:45 +03002990static int em_call_far(struct x86_emulate_ctxt *ctxt)
2991{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002992 u16 sel, old_cs;
2993 ulong old_eip;
2994 int rc;
2995
Avi Kivity1aa36612011-04-27 13:20:30 +03002996 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002997 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002998
Avi Kivity9dac77f2011-06-01 15:34:25 +03002999 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003000 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03003001 return X86EMUL_CONTINUE;
3002
Avi Kivity9dac77f2011-06-01 15:34:25 +03003003 ctxt->_eip = 0;
3004 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003005
Avi Kivity9dac77f2011-06-01 15:34:25 +03003006 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003007 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003008 if (rc != X86EMUL_CONTINUE)
3009 return rc;
3010
Avi Kivity9dac77f2011-06-01 15:34:25 +03003011 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003012 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003013}
3014
Avi Kivity40ece7c2010-08-18 15:12:09 +03003015static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3016{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003017 int rc;
3018
Avi Kivity9dac77f2011-06-01 15:34:25 +03003019 ctxt->dst.type = OP_REG;
3020 ctxt->dst.addr.reg = &ctxt->_eip;
3021 ctxt->dst.bytes = ctxt->op_bytes;
3022 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003023 if (rc != X86EMUL_CONTINUE)
3024 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003025 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003026 return X86EMUL_CONTINUE;
3027}
3028
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003029static int em_add(struct x86_emulate_ctxt *ctxt)
3030{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003031 emulate_2op_SrcV(ctxt, "add");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003032 return X86EMUL_CONTINUE;
3033}
3034
3035static int em_or(struct x86_emulate_ctxt *ctxt)
3036{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003037 emulate_2op_SrcV(ctxt, "or");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003038 return X86EMUL_CONTINUE;
3039}
3040
3041static int em_adc(struct x86_emulate_ctxt *ctxt)
3042{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003043 emulate_2op_SrcV(ctxt, "adc");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003044 return X86EMUL_CONTINUE;
3045}
3046
3047static int em_sbb(struct x86_emulate_ctxt *ctxt)
3048{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003049 emulate_2op_SrcV(ctxt, "sbb");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003050 return X86EMUL_CONTINUE;
3051}
3052
3053static int em_and(struct x86_emulate_ctxt *ctxt)
3054{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003055 emulate_2op_SrcV(ctxt, "and");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003056 return X86EMUL_CONTINUE;
3057}
3058
3059static int em_sub(struct x86_emulate_ctxt *ctxt)
3060{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003061 emulate_2op_SrcV(ctxt, "sub");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003062 return X86EMUL_CONTINUE;
3063}
3064
3065static int em_xor(struct x86_emulate_ctxt *ctxt)
3066{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003067 emulate_2op_SrcV(ctxt, "xor");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003068 return X86EMUL_CONTINUE;
3069}
3070
3071static int em_cmp(struct x86_emulate_ctxt *ctxt)
3072{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003073 emulate_2op_SrcV(ctxt, "cmp");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003074 return X86EMUL_CONTINUE;
3075}
3076
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003077static int em_test(struct x86_emulate_ctxt *ctxt)
3078{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003079 emulate_2op_SrcV(ctxt, "test");
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003080 return X86EMUL_CONTINUE;
3081}
3082
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003083static int em_xchg(struct x86_emulate_ctxt *ctxt)
3084{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003085 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003086 ctxt->src.val = ctxt->dst.val;
3087 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003088
3089 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003090 ctxt->dst.val = ctxt->src.orig_val;
3091 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003092 return X86EMUL_CONTINUE;
3093}
3094
Avi Kivity5c82aa22010-08-18 18:31:43 +03003095static int em_imul(struct x86_emulate_ctxt *ctxt)
3096{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003097 emulate_2op_SrcV_nobyte(ctxt, "imul");
Avi Kivity5c82aa22010-08-18 18:31:43 +03003098 return X86EMUL_CONTINUE;
3099}
3100
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003101static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3102{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003103 ctxt->dst.val = ctxt->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03003104 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003105}
3106
Avi Kivity61429142010-08-19 15:13:00 +03003107static int em_cwd(struct x86_emulate_ctxt *ctxt)
3108{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003109 ctxt->dst.type = OP_REG;
3110 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003111 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003112 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003113
3114 return X86EMUL_CONTINUE;
3115}
3116
Avi Kivity48bb5d32010-08-18 18:54:34 +03003117static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3118{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003119 u64 tsc = 0;
3120
Avi Kivity717746e2011-04-20 13:37:53 +03003121 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003122 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3123 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003124 return X86EMUL_CONTINUE;
3125}
3126
Avi Kivity222d21a2011-11-10 14:57:30 +02003127static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3128{
3129 u64 pmc;
3130
Avi Kivitydd856ef2012-08-27 23:46:17 +03003131 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003132 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003133 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3134 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003135 return X86EMUL_CONTINUE;
3136}
3137
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003138static int em_mov(struct x86_emulate_ctxt *ctxt)
3139{
Stefan Hajnoczi49597d82012-04-09 18:40:00 +03003140 memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003141 return X86EMUL_CONTINUE;
3142}
3143
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003144static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3145{
3146 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3147 return emulate_gp(ctxt, 0);
3148
3149 /* Disable writeback. */
3150 ctxt->dst.type = OP_NONE;
3151 return X86EMUL_CONTINUE;
3152}
3153
3154static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3155{
3156 unsigned long val;
3157
3158 if (ctxt->mode == X86EMUL_MODE_PROT64)
3159 val = ctxt->src.val & ~0ULL;
3160 else
3161 val = ctxt->src.val & ~0U;
3162
3163 /* #UD condition is already handled. */
3164 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3165 return emulate_gp(ctxt, 0);
3166
3167 /* Disable writeback. */
3168 ctxt->dst.type = OP_NONE;
3169 return X86EMUL_CONTINUE;
3170}
3171
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003172static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3173{
3174 u64 msr_data;
3175
Avi Kivitydd856ef2012-08-27 23:46:17 +03003176 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3177 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3178 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003179 return emulate_gp(ctxt, 0);
3180
3181 return X86EMUL_CONTINUE;
3182}
3183
3184static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3185{
3186 u64 msr_data;
3187
Avi Kivitydd856ef2012-08-27 23:46:17 +03003188 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003189 return emulate_gp(ctxt, 0);
3190
Avi Kivitydd856ef2012-08-27 23:46:17 +03003191 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3192 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003193 return X86EMUL_CONTINUE;
3194}
3195
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003196static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3197{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003198 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003199 return emulate_ud(ctxt);
3200
Avi Kivity9dac77f2011-06-01 15:34:25 +03003201 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003202 return X86EMUL_CONTINUE;
3203}
3204
3205static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3206{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003207 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003208
Avi Kivity9dac77f2011-06-01 15:34:25 +03003209 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003210 return emulate_ud(ctxt);
3211
Avi Kivity9dac77f2011-06-01 15:34:25 +03003212 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003213 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3214
3215 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003216 ctxt->dst.type = OP_NONE;
3217 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003218}
3219
Avi Kivitya14e5792012-06-13 12:28:33 +03003220static int em_lldt(struct x86_emulate_ctxt *ctxt)
3221{
3222 u16 sel = ctxt->src.val;
3223
3224 /* Disable writeback. */
3225 ctxt->dst.type = OP_NONE;
3226 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3227}
3228
Avi Kivity80890002012-06-13 16:33:29 +03003229static int em_ltr(struct x86_emulate_ctxt *ctxt)
3230{
3231 u16 sel = ctxt->src.val;
3232
3233 /* Disable writeback. */
3234 ctxt->dst.type = OP_NONE;
3235 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3236}
3237
Avi Kivity38503912011-03-31 18:48:09 +02003238static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3239{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003240 int rc;
3241 ulong linear;
3242
Avi Kivity9dac77f2011-06-01 15:34:25 +03003243 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003244 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003245 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003246 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003247 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003248 return X86EMUL_CONTINUE;
3249}
3250
Avi Kivity2d04a052011-04-20 15:32:49 +03003251static int em_clts(struct x86_emulate_ctxt *ctxt)
3252{
3253 ulong cr0;
3254
3255 cr0 = ctxt->ops->get_cr(ctxt, 0);
3256 cr0 &= ~X86_CR0_TS;
3257 ctxt->ops->set_cr(ctxt, 0, cr0);
3258 return X86EMUL_CONTINUE;
3259}
3260
Avi Kivity26d05cc2011-04-21 12:07:59 +03003261static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3262{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003263 int rc;
3264
Avi Kivity9dac77f2011-06-01 15:34:25 +03003265 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003266 return X86EMUL_UNHANDLEABLE;
3267
3268 rc = ctxt->ops->fix_hypercall(ctxt);
3269 if (rc != X86EMUL_CONTINUE)
3270 return rc;
3271
3272 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003273 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003274 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003275 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003276 return X86EMUL_CONTINUE;
3277}
3278
Avi Kivity96051572012-06-10 17:21:18 +03003279static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3280 void (*get)(struct x86_emulate_ctxt *ctxt,
3281 struct desc_ptr *ptr))
3282{
3283 struct desc_ptr desc_ptr;
3284
3285 if (ctxt->mode == X86EMUL_MODE_PROT64)
3286 ctxt->op_bytes = 8;
3287 get(ctxt, &desc_ptr);
3288 if (ctxt->op_bytes == 2) {
3289 ctxt->op_bytes = 4;
3290 desc_ptr.address &= 0x00ffffff;
3291 }
3292 /* Disable writeback. */
3293 ctxt->dst.type = OP_NONE;
3294 return segmented_write(ctxt, ctxt->dst.addr.mem,
3295 &desc_ptr, 2 + ctxt->op_bytes);
3296}
3297
3298static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3299{
3300 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3301}
3302
3303static int em_sidt(struct x86_emulate_ctxt *ctxt)
3304{
3305 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3306}
3307
Avi Kivity26d05cc2011-04-21 12:07:59 +03003308static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3309{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003310 struct desc_ptr desc_ptr;
3311 int rc;
3312
Avi Kivity510425f2012-06-07 17:04:36 +03003313 if (ctxt->mode == X86EMUL_MODE_PROT64)
3314 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003315 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003316 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003317 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003318 if (rc != X86EMUL_CONTINUE)
3319 return rc;
3320 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3321 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003322 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003323 return X86EMUL_CONTINUE;
3324}
3325
Avi Kivity5ef39c72011-04-21 12:21:50 +03003326static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003327{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003328 int rc;
3329
Avi Kivity5ef39c72011-04-21 12:21:50 +03003330 rc = ctxt->ops->fix_hypercall(ctxt);
3331
Avi Kivity26d05cc2011-04-21 12:07:59 +03003332 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003333 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003334 return rc;
3335}
3336
3337static int em_lidt(struct x86_emulate_ctxt *ctxt)
3338{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003339 struct desc_ptr desc_ptr;
3340 int rc;
3341
Avi Kivity510425f2012-06-07 17:04:36 +03003342 if (ctxt->mode == X86EMUL_MODE_PROT64)
3343 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003344 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003345 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003346 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003347 if (rc != X86EMUL_CONTINUE)
3348 return rc;
3349 ctxt->ops->set_idt(ctxt, &desc_ptr);
3350 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003351 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003352 return X86EMUL_CONTINUE;
3353}
3354
3355static int em_smsw(struct x86_emulate_ctxt *ctxt)
3356{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003357 ctxt->dst.bytes = 2;
3358 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003359 return X86EMUL_CONTINUE;
3360}
3361
3362static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3363{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003364 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003365 | (ctxt->src.val & 0x0f));
3366 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003367 return X86EMUL_CONTINUE;
3368}
3369
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003370static int em_loop(struct x86_emulate_ctxt *ctxt)
3371{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003372 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3373 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003374 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3375 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003376
3377 return X86EMUL_CONTINUE;
3378}
3379
3380static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3381{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003382 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003383 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003384
3385 return X86EMUL_CONTINUE;
3386}
3387
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003388static int em_in(struct x86_emulate_ctxt *ctxt)
3389{
3390 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3391 &ctxt->dst.val))
3392 return X86EMUL_IO_NEEDED;
3393
3394 return X86EMUL_CONTINUE;
3395}
3396
3397static int em_out(struct x86_emulate_ctxt *ctxt)
3398{
3399 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3400 &ctxt->src.val, 1);
3401 /* Disable writeback. */
3402 ctxt->dst.type = OP_NONE;
3403 return X86EMUL_CONTINUE;
3404}
3405
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003406static int em_cli(struct x86_emulate_ctxt *ctxt)
3407{
3408 if (emulator_bad_iopl(ctxt))
3409 return emulate_gp(ctxt, 0);
3410
3411 ctxt->eflags &= ~X86_EFLAGS_IF;
3412 return X86EMUL_CONTINUE;
3413}
3414
3415static int em_sti(struct x86_emulate_ctxt *ctxt)
3416{
3417 if (emulator_bad_iopl(ctxt))
3418 return emulate_gp(ctxt, 0);
3419
3420 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3421 ctxt->eflags |= X86_EFLAGS_IF;
3422 return X86EMUL_CONTINUE;
3423}
3424
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003425static int em_bt(struct x86_emulate_ctxt *ctxt)
3426{
3427 /* Disable writeback. */
3428 ctxt->dst.type = OP_NONE;
3429 /* only subword offset */
3430 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
3431
3432 emulate_2op_SrcV_nobyte(ctxt, "bt");
3433 return X86EMUL_CONTINUE;
3434}
3435
3436static int em_bts(struct x86_emulate_ctxt *ctxt)
3437{
3438 emulate_2op_SrcV_nobyte(ctxt, "bts");
3439 return X86EMUL_CONTINUE;
3440}
3441
3442static int em_btr(struct x86_emulate_ctxt *ctxt)
3443{
3444 emulate_2op_SrcV_nobyte(ctxt, "btr");
3445 return X86EMUL_CONTINUE;
3446}
3447
3448static int em_btc(struct x86_emulate_ctxt *ctxt)
3449{
3450 emulate_2op_SrcV_nobyte(ctxt, "btc");
3451 return X86EMUL_CONTINUE;
3452}
3453
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003454static int em_bsf(struct x86_emulate_ctxt *ctxt)
3455{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003456 emulate_2op_SrcV_nobyte(ctxt, "bsf");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003457 return X86EMUL_CONTINUE;
3458}
3459
3460static int em_bsr(struct x86_emulate_ctxt *ctxt)
3461{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003462 emulate_2op_SrcV_nobyte(ctxt, "bsr");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003463 return X86EMUL_CONTINUE;
3464}
3465
Avi Kivity6d6eede2012-06-07 14:11:36 +03003466static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3467{
3468 u32 eax, ebx, ecx, edx;
3469
Avi Kivitydd856ef2012-08-27 23:46:17 +03003470 eax = reg_read(ctxt, VCPU_REGS_RAX);
3471 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003472 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003473 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3474 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3475 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3476 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003477 return X86EMUL_CONTINUE;
3478}
3479
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003480static int em_lahf(struct x86_emulate_ctxt *ctxt)
3481{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003482 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3483 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003484 return X86EMUL_CONTINUE;
3485}
3486
Avi Kivity92998362012-06-13 12:25:06 +03003487static int em_bswap(struct x86_emulate_ctxt *ctxt)
3488{
3489 switch (ctxt->op_bytes) {
3490#ifdef CONFIG_X86_64
3491 case 8:
3492 asm("bswap %0" : "+r"(ctxt->dst.val));
3493 break;
3494#endif
3495 default:
3496 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3497 break;
3498 }
3499 return X86EMUL_CONTINUE;
3500}
3501
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003502static bool valid_cr(int nr)
3503{
3504 switch (nr) {
3505 case 0:
3506 case 2 ... 4:
3507 case 8:
3508 return true;
3509 default:
3510 return false;
3511 }
3512}
3513
3514static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3515{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003516 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003517 return emulate_ud(ctxt);
3518
3519 return X86EMUL_CONTINUE;
3520}
3521
3522static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3523{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003524 u64 new_val = ctxt->src.val64;
3525 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003526 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003527
3528 static u64 cr_reserved_bits[] = {
3529 0xffffffff00000000ULL,
3530 0, 0, 0, /* CR3 checked later */
3531 CR4_RESERVED_BITS,
3532 0, 0, 0,
3533 CR8_RESERVED_BITS,
3534 };
3535
3536 if (!valid_cr(cr))
3537 return emulate_ud(ctxt);
3538
3539 if (new_val & cr_reserved_bits[cr])
3540 return emulate_gp(ctxt, 0);
3541
3542 switch (cr) {
3543 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003544 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003545 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3546 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3547 return emulate_gp(ctxt, 0);
3548
Avi Kivity717746e2011-04-20 13:37:53 +03003549 cr4 = ctxt->ops->get_cr(ctxt, 4);
3550 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003551
3552 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3553 !(cr4 & X86_CR4_PAE))
3554 return emulate_gp(ctxt, 0);
3555
3556 break;
3557 }
3558 case 3: {
3559 u64 rsvd = 0;
3560
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003561 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3562 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003563 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003564 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003565 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003566 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003567 rsvd = CR3_NONPAE_RESERVED_BITS;
3568
3569 if (new_val & rsvd)
3570 return emulate_gp(ctxt, 0);
3571
3572 break;
3573 }
3574 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003575 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003576
3577 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3578 return emulate_gp(ctxt, 0);
3579
3580 break;
3581 }
3582 }
3583
3584 return X86EMUL_CONTINUE;
3585}
3586
Joerg Roedel3b88e412011-04-04 12:39:29 +02003587static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3588{
3589 unsigned long dr7;
3590
Avi Kivity717746e2011-04-20 13:37:53 +03003591 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003592
3593 /* Check if DR7.Global_Enable is set */
3594 return dr7 & (1 << 13);
3595}
3596
3597static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3598{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003599 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003600 u64 cr4;
3601
3602 if (dr > 7)
3603 return emulate_ud(ctxt);
3604
Avi Kivity717746e2011-04-20 13:37:53 +03003605 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003606 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3607 return emulate_ud(ctxt);
3608
3609 if (check_dr7_gd(ctxt))
3610 return emulate_db(ctxt);
3611
3612 return X86EMUL_CONTINUE;
3613}
3614
3615static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3616{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003617 u64 new_val = ctxt->src.val64;
3618 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003619
3620 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3621 return emulate_gp(ctxt, 0);
3622
3623 return check_dr_read(ctxt);
3624}
3625
Joerg Roedel01de8b02011-04-04 12:39:31 +02003626static int check_svme(struct x86_emulate_ctxt *ctxt)
3627{
3628 u64 efer;
3629
Avi Kivity717746e2011-04-20 13:37:53 +03003630 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003631
3632 if (!(efer & EFER_SVME))
3633 return emulate_ud(ctxt);
3634
3635 return X86EMUL_CONTINUE;
3636}
3637
3638static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3639{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003640 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003641
3642 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003643 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003644 return emulate_gp(ctxt, 0);
3645
3646 return check_svme(ctxt);
3647}
3648
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003649static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3650{
Avi Kivity717746e2011-04-20 13:37:53 +03003651 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003652
Avi Kivity717746e2011-04-20 13:37:53 +03003653 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003654 return emulate_ud(ctxt);
3655
3656 return X86EMUL_CONTINUE;
3657}
3658
Joerg Roedel80612522011-04-04 12:39:33 +02003659static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3660{
Avi Kivity717746e2011-04-20 13:37:53 +03003661 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003662 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003663
Avi Kivity717746e2011-04-20 13:37:53 +03003664 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02003665 (rcx > 3))
3666 return emulate_gp(ctxt, 0);
3667
3668 return X86EMUL_CONTINUE;
3669}
3670
Joerg Roedelf6511932011-04-04 12:39:35 +02003671static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3672{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003673 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3674 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003675 return emulate_gp(ctxt, 0);
3676
3677 return X86EMUL_CONTINUE;
3678}
3679
3680static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3681{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003682 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3683 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003684 return emulate_gp(ctxt, 0);
3685
3686 return X86EMUL_CONTINUE;
3687}
3688
Avi Kivity73fba5f2010-07-29 15:11:53 +03003689#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003690#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003691#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3692 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003693#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003694#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003695#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3696#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003697#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003698#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003699#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003700#define II(_f, _e, _i) \
3701 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003702#define IIP(_f, _e, _i, _p) \
3703 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3704 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003705#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003706
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003707#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003708#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003709#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003710#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003711#define I2bvIP(_f, _e, _i, _p) \
3712 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003713
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003714#define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3715 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3716 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003717
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003718static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003719 DI(SrcNone | Priv, monitor),
3720 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003721 N, N, N, N, N, N,
3722};
3723
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003724static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003725 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
3726 II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall),
3727 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3728 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3729 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3730 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3731 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3732 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003733};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003734
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003735static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003736 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003737 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003738 N, N, N, N, N, N,
3739};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003740
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003741static const struct opcode group1[] = {
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003742 I(Lock, em_add),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003743 I(Lock | PageTable, em_or),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003744 I(Lock, em_adc),
3745 I(Lock, em_sbb),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003746 I(Lock | PageTable, em_and),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003747 I(Lock, em_sub),
3748 I(Lock, em_xor),
Avi Kivity75f72842013-01-04 16:18:51 +02003749 I(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003750};
3751
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003752static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003753 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003754};
3755
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003756static const struct opcode group3[] = {
Avi Kivity75f72842013-01-04 16:18:51 +02003757 I(DstMem | SrcImm | NoWrite, em_test),
3758 I(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003759 F(DstMem | SrcNone | Lock, em_not),
3760 F(DstMem | SrcNone | Lock, em_neg),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003761 I(SrcMem, em_mul_ex),
3762 I(SrcMem, em_imul_ex),
3763 I(SrcMem, em_div_ex),
3764 I(SrcMem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003765};
3766
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003767static const struct opcode group4[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003768 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
3769 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003770 N, N, N, N, N, N,
3771};
3772
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003773static const struct opcode group5[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003774 I(DstMem | SrcNone | Lock, em_grp45),
3775 I(DstMem | SrcNone | Lock, em_grp45),
3776 I(SrcMem | Stack, em_grp45),
3777 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3778 I(SrcMem | Stack, em_grp45),
3779 I(SrcMemFAddr | ImplicitOps, em_grp45),
3780 I(SrcMem | Stack, em_grp45), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003781};
3782
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003783static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003784 DI(Prot, sldt),
3785 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003786 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003787 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003788 N, N, N, N,
3789};
3790
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003791static const struct group_dual group7 = { {
Avi Kivity96051572012-06-10 17:21:18 +03003792 II(Mov | DstMem | Priv, em_sgdt, sgdt),
3793 II(Mov | DstMem | Priv, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003794 II(SrcMem | Priv, em_lgdt, lgdt),
3795 II(SrcMem | Priv, em_lidt, lidt),
3796 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3797 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3798 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003799}, {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003800 I(SrcNone | Priv | VendorSpecific, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003801 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003802 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003803 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3804 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3805 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003806} };
3807
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003808static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003809 N, N, N, N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003810 I(DstMem | SrcImmByte, em_bt),
3811 I(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3812 I(DstMem | SrcImmByte | Lock, em_btr),
3813 I(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003814};
3815
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003816static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003817 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003818}, {
3819 N, N, N, N, N, N, N, N,
3820} };
3821
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003822static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003823 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003824 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003825};
3826
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003827static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003828 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003829};
3830
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003831static const struct gprefix pfx_vmovntpx = {
Avi Kivity3e114eb2012-04-09 18:40:01 +03003832 I(0, em_mov), N, N, N,
3833};
3834
Gleb Natapov045a2822012-12-20 16:57:43 +02003835static const struct escape escape_d9 = { {
3836 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3837}, {
3838 /* 0xC0 - 0xC7 */
3839 N, N, N, N, N, N, N, N,
3840 /* 0xC8 - 0xCF */
3841 N, N, N, N, N, N, N, N,
3842 /* 0xD0 - 0xC7 */
3843 N, N, N, N, N, N, N, N,
3844 /* 0xD8 - 0xDF */
3845 N, N, N, N, N, N, N, N,
3846 /* 0xE0 - 0xE7 */
3847 N, N, N, N, N, N, N, N,
3848 /* 0xE8 - 0xEF */
3849 N, N, N, N, N, N, N, N,
3850 /* 0xF0 - 0xF7 */
3851 N, N, N, N, N, N, N, N,
3852 /* 0xF8 - 0xFF */
3853 N, N, N, N, N, N, N, N,
3854} };
3855
3856static const struct escape escape_db = { {
3857 N, N, N, N, N, N, N, N,
3858}, {
3859 /* 0xC0 - 0xC7 */
3860 N, N, N, N, N, N, N, N,
3861 /* 0xC8 - 0xCF */
3862 N, N, N, N, N, N, N, N,
3863 /* 0xD0 - 0xC7 */
3864 N, N, N, N, N, N, N, N,
3865 /* 0xD8 - 0xDF */
3866 N, N, N, N, N, N, N, N,
3867 /* 0xE0 - 0xE7 */
3868 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3869 /* 0xE8 - 0xEF */
3870 N, N, N, N, N, N, N, N,
3871 /* 0xF0 - 0xF7 */
3872 N, N, N, N, N, N, N, N,
3873 /* 0xF8 - 0xFF */
3874 N, N, N, N, N, N, N, N,
3875} };
3876
3877static const struct escape escape_dd = { {
3878 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3879}, {
3880 /* 0xC0 - 0xC7 */
3881 N, N, N, N, N, N, N, N,
3882 /* 0xC8 - 0xCF */
3883 N, N, N, N, N, N, N, N,
3884 /* 0xD0 - 0xC7 */
3885 N, N, N, N, N, N, N, N,
3886 /* 0xD8 - 0xDF */
3887 N, N, N, N, N, N, N, N,
3888 /* 0xE0 - 0xE7 */
3889 N, N, N, N, N, N, N, N,
3890 /* 0xE8 - 0xEF */
3891 N, N, N, N, N, N, N, N,
3892 /* 0xF0 - 0xF7 */
3893 N, N, N, N, N, N, N, N,
3894 /* 0xF8 - 0xFF */
3895 N, N, N, N, N, N, N, N,
3896} };
3897
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003898static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003899 /* 0x00 - 0x07 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003900 I6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003901 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3902 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003903 /* 0x08 - 0x0F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003904 I6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003905 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3906 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003907 /* 0x10 - 0x17 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003908 I6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003909 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3910 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003911 /* 0x18 - 0x1F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003912 I6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003913 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3914 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003915 /* 0x20 - 0x27 */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003916 I6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003917 /* 0x28 - 0x2F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003918 I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003919 /* 0x30 - 0x37 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003920 I6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003921 /* 0x38 - 0x3F */
Avi Kivity75f72842013-01-04 16:18:51 +02003922 I6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003923 /* 0x40 - 0x4F */
3924 X16(D(DstReg)),
3925 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003926 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003927 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003928 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003929 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003930 I(ImplicitOps | Stack | No64, em_pusha),
3931 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003932 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3933 N, N, N, N,
3934 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003935 I(SrcImm | Mov | Stack, em_push),
3936 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003937 I(SrcImmByte | Mov | Stack, em_push),
3938 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003939 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003940 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003941 /* 0x70 - 0x7F */
3942 X16(D(SrcImmByte)),
3943 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003944 G(ByteOp | DstMem | SrcImm, group1),
3945 G(DstMem | SrcImm, group1),
3946 G(ByteOp | DstMem | SrcImm | No64, group1),
3947 G(DstMem | SrcImmByte, group1),
Avi Kivity75f72842013-01-04 16:18:51 +02003948 I2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003949 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003950 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003951 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003952 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003953 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003954 D(ModRM | SrcMem | NoAccess | DstReg),
3955 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3956 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003957 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003958 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003959 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003960 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003961 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003962 II(ImplicitOps | Stack, em_pushf, pushf),
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003963 II(ImplicitOps | Stack, em_popf, popf), N, I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003964 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003965 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003966 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003967 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Avi Kivity75f72842013-01-04 16:18:51 +02003968 I2bv(SrcSI | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003969 /* 0xA8 - 0xAF */
Avi Kivity75f72842013-01-04 16:18:51 +02003970 I2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003971 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3972 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivity75f72842013-01-04 16:18:51 +02003973 I2bv(SrcAcc | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003974 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003975 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003976 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02003977 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003978 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003979 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003980 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003981 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003982 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3983 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003984 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003985 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003986 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
3987 N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003988 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003989 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003990 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003991 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Gleb Natapov7f662272012-12-10 11:42:30 +02003992 N, I(DstAcc | SrcImmByte | No64, em_aad), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003993 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02003994 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003995 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003996 X3(I(SrcImmByte, em_loop)),
3997 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003998 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3999 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004000 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09004001 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004002 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004003 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
4004 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004005 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004006 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004007 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4008 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004009 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09004010 D(ImplicitOps), D(ImplicitOps),
4011 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004012 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4013};
4014
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004015static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004016 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004017 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004018 N, I(ImplicitOps | VendorSpecific, em_syscall),
4019 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004020 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004021 N, D(ImplicitOps | ModRM), N, N,
4022 /* 0x10 - 0x1F */
4023 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
4024 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004025 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02004026 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09004027 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
4028 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004029 N, N, N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03004030 N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
4031 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004032 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004033 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004034 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004035 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004036 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004037 I(ImplicitOps | VendorSpecific, em_sysenter),
4038 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004039 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004040 N, N, N, N, N, N, N, N,
4041 /* 0x40 - 0x4F */
4042 X16(D(DstReg | SrcMem | ModRM | Mov)),
4043 /* 0x50 - 0x5F */
4044 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4045 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004046 N, N, N, N,
4047 N, N, N, N,
4048 N, N, N, N,
4049 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004050 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004051 N, N, N, N,
4052 N, N, N, N,
4053 N, N, N, N,
4054 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004055 /* 0x80 - 0x8F */
4056 X16(D(SrcImm)),
4057 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004058 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004059 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004060 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity6d6eede2012-06-07 14:11:36 +03004061 II(ImplicitOps, em_cpuid, cpuid), I(DstMem | SrcReg | ModRM | BitOp, em_bt),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004062 D(DstMem | SrcReg | Src2ImmByte | ModRM),
4063 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
4064 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004065 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004066 DI(ImplicitOps, rsm),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004067 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004068 D(DstMem | SrcReg | Src2ImmByte | ModRM),
4069 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03004070 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004071 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09004072 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004073 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004074 I(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004075 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4076 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004077 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004078 /* 0xB8 - 0xBF */
4079 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004080 G(BitOp, group8),
4081 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Takuya Yoshikawaff227392011-11-22 15:21:33 +09004082 I(DstReg | SrcMem | ModRM, em_bsf), I(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004083 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004084 /* 0xC0 - 0xC7 */
Avi Kivity739ae402010-08-26 11:56:13 +03004085 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08004086 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004087 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004088 /* 0xC8 - 0xCF */
4089 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004090 /* 0xD0 - 0xDF */
4091 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4092 /* 0xE0 - 0xEF */
4093 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4094 /* 0xF0 - 0xFF */
4095 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4096};
4097
4098#undef D
4099#undef N
4100#undef G
4101#undef GD
4102#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004103#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004104#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004105
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004106#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004107#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004108#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004109#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004110#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004111
Avi Kivity9dac77f2011-06-01 15:34:25 +03004112static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004113{
4114 unsigned size;
4115
Avi Kivity9dac77f2011-06-01 15:34:25 +03004116 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004117 if (size == 8)
4118 size = 4;
4119 return size;
4120}
4121
4122static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4123 unsigned size, bool sign_extension)
4124{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004125 int rc = X86EMUL_CONTINUE;
4126
4127 op->type = OP_IMM;
4128 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004129 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004130 /* NB. Immediates are sign-extended as necessary. */
4131 switch (op->bytes) {
4132 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004133 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004134 break;
4135 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004136 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004137 break;
4138 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004139 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004140 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004141 case 8:
4142 op->val = insn_fetch(s64, ctxt);
4143 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004144 }
4145 if (!sign_extension) {
4146 switch (op->bytes) {
4147 case 1:
4148 op->val &= 0xff;
4149 break;
4150 case 2:
4151 op->val &= 0xffff;
4152 break;
4153 case 4:
4154 op->val &= 0xffffffff;
4155 break;
4156 }
4157 }
4158done:
4159 return rc;
4160}
4161
Avi Kivitya9945542011-09-13 10:45:41 +03004162static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4163 unsigned d)
4164{
4165 int rc = X86EMUL_CONTINUE;
4166
4167 switch (d) {
4168 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004169 decode_register_operand(ctxt, op);
Avi Kivitya9945542011-09-13 10:45:41 +03004170 break;
4171 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004172 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03004173 break;
4174 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004175 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004176 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03004177 *op = ctxt->memop;
4178 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03004179 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya9945542011-09-13 10:45:41 +03004180 fetch_bit_operand(ctxt);
4181 op->orig_val = op->val;
4182 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004183 case OpMem64:
4184 ctxt->memop.bytes = 8;
4185 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03004186 case OpAcc:
4187 op->type = OP_REG;
4188 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004189 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya9945542011-09-13 10:45:41 +03004190 fetch_register_operand(op);
4191 op->orig_val = op->val;
4192 break;
4193 case OpDI:
4194 op->type = OP_MEM;
4195 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4196 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004197 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya9945542011-09-13 10:45:41 +03004198 op->addr.mem.seg = VCPU_SREG_ES;
4199 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004200 op->count = 1;
Avi Kivitya9945542011-09-13 10:45:41 +03004201 break;
4202 case OpDX:
4203 op->type = OP_REG;
4204 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004205 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya9945542011-09-13 10:45:41 +03004206 fetch_register_operand(op);
4207 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004208 case OpCL:
4209 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004210 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004211 break;
4212 case OpImmByte:
4213 rc = decode_imm(ctxt, op, 1, true);
4214 break;
4215 case OpOne:
4216 op->bytes = 1;
4217 op->val = 1;
4218 break;
4219 case OpImm:
4220 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4221 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004222 case OpImm64:
4223 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4224 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004225 case OpMem8:
4226 ctxt->memop.bytes = 1;
4227 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004228 case OpMem16:
4229 ctxt->memop.bytes = 2;
4230 goto mem_common;
4231 case OpMem32:
4232 ctxt->memop.bytes = 4;
4233 goto mem_common;
4234 case OpImmU16:
4235 rc = decode_imm(ctxt, op, 2, false);
4236 break;
4237 case OpImmU:
4238 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4239 break;
4240 case OpSI:
4241 op->type = OP_MEM;
4242 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4243 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004244 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Avi Kivity0fe59122011-09-13 10:45:47 +03004245 op->addr.mem.seg = seg_override(ctxt);
4246 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004247 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004248 break;
4249 case OpImmFAddr:
4250 op->type = OP_IMM;
4251 op->addr.mem.ea = ctxt->_eip;
4252 op->bytes = ctxt->op_bytes + 2;
4253 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4254 break;
4255 case OpMemFAddr:
4256 ctxt->memop.bytes = ctxt->op_bytes + 2;
4257 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004258 case OpES:
4259 op->val = VCPU_SREG_ES;
4260 break;
4261 case OpCS:
4262 op->val = VCPU_SREG_CS;
4263 break;
4264 case OpSS:
4265 op->val = VCPU_SREG_SS;
4266 break;
4267 case OpDS:
4268 op->val = VCPU_SREG_DS;
4269 break;
4270 case OpFS:
4271 op->val = VCPU_SREG_FS;
4272 break;
4273 case OpGS:
4274 op->val = VCPU_SREG_GS;
4275 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004276 case OpImplicit:
4277 /* Special instructions do their own operand decoding. */
4278 default:
4279 op->type = OP_NONE; /* Disable writeback. */
4280 break;
4281 }
4282
4283done:
4284 return rc;
4285}
4286
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004287int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004288{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004289 int rc = X86EMUL_CONTINUE;
4290 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004291 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004292 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03004293 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004294
Avi Kivityf09ed832011-09-13 10:45:40 +03004295 ctxt->memop.type = OP_NONE;
4296 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004297 ctxt->_eip = ctxt->eip;
4298 ctxt->fetch.start = ctxt->_eip;
4299 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01004300 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004301 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004302
4303 switch (mode) {
4304 case X86EMUL_MODE_REAL:
4305 case X86EMUL_MODE_VM86:
4306 case X86EMUL_MODE_PROT16:
4307 def_op_bytes = def_ad_bytes = 2;
4308 break;
4309 case X86EMUL_MODE_PROT32:
4310 def_op_bytes = def_ad_bytes = 4;
4311 break;
4312#ifdef CONFIG_X86_64
4313 case X86EMUL_MODE_PROT64:
4314 def_op_bytes = 4;
4315 def_ad_bytes = 8;
4316 break;
4317#endif
4318 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004319 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004320 }
4321
Avi Kivity9dac77f2011-06-01 15:34:25 +03004322 ctxt->op_bytes = def_op_bytes;
4323 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004324
4325 /* Legacy prefixes. */
4326 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004327 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004328 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004329 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004330 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004331 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004332 break;
4333 case 0x67: /* address-size override */
4334 if (mode == X86EMUL_MODE_PROT64)
4335 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004336 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004337 else
4338 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004339 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004340 break;
4341 case 0x26: /* ES override */
4342 case 0x2e: /* CS override */
4343 case 0x36: /* SS override */
4344 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004345 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004346 break;
4347 case 0x64: /* FS override */
4348 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004349 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004350 break;
4351 case 0x40 ... 0x4f: /* REX */
4352 if (mode != X86EMUL_MODE_PROT64)
4353 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004354 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004355 continue;
4356 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004357 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004358 break;
4359 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004360 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004361 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004362 break;
4363 default:
4364 goto done_prefixes;
4365 }
4366
4367 /* Any legacy prefix after a REX prefix nullifies its effect. */
4368
Avi Kivity9dac77f2011-06-01 15:34:25 +03004369 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004370 }
4371
4372done_prefixes:
4373
4374 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004375 if (ctxt->rex_prefix & 8)
4376 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004377
4378 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004379 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004380 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004381 if (ctxt->b == 0x0f) {
4382 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004383 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004384 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004385 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004386 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004387
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004388 if (ctxt->d & ModRM)
4389 ctxt->modrm = insn_fetch(u8, ctxt);
4390
Avi Kivity9dac77f2011-06-01 15:34:25 +03004391 while (ctxt->d & GroupMask) {
4392 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004393 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004394 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004395 opcode = opcode.u.group[goffset];
4396 break;
4397 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004398 goffset = (ctxt->modrm >> 3) & 7;
4399 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004400 opcode = opcode.u.gdual->mod3[goffset];
4401 else
4402 opcode = opcode.u.gdual->mod012[goffset];
4403 break;
4404 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004405 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004406 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004407 break;
4408 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004409 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004410 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004411 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004412 switch (simd_prefix) {
4413 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4414 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4415 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4416 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4417 }
4418 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004419 case Escape:
4420 if (ctxt->modrm > 0xbf)
4421 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4422 else
4423 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4424 break;
Avi Kivity46561642011-04-24 14:09:59 +03004425 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004426 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004427 }
Avi Kivity46561642011-04-24 14:09:59 +03004428
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004429 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004430 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004431 }
4432
Avi Kivity9dac77f2011-06-01 15:34:25 +03004433 ctxt->execute = opcode.u.execute;
4434 ctxt->check_perm = opcode.check_perm;
4435 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004436
4437 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004438 if (ctxt->d == 0 || (ctxt->d & Undefined))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004439 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004440
Avi Kivity9dac77f2011-06-01 15:34:25 +03004441 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004442 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004443
Avi Kivity9dac77f2011-06-01 15:34:25 +03004444 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
4445 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004446
Avi Kivity9dac77f2011-06-01 15:34:25 +03004447 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004448 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004449 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004450 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03004451 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004452 }
4453
Avi Kivity9dac77f2011-06-01 15:34:25 +03004454 if (ctxt->d & Sse)
4455 ctxt->op_bytes = 16;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004456 else if (ctxt->d & Mmx)
4457 ctxt->op_bytes = 8;
Avi Kivity12537912011-03-29 11:41:27 +02004458
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004459 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004460 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004461 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004462 if (!ctxt->has_seg_override)
4463 set_seg_override(ctxt, ctxt->modrm_seg);
4464 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004465 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004466 if (rc != X86EMUL_CONTINUE)
4467 goto done;
4468
Avi Kivity9dac77f2011-06-01 15:34:25 +03004469 if (!ctxt->has_seg_override)
4470 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004471
Avi Kivityf09ed832011-09-13 10:45:40 +03004472 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004473
Avi Kivityf09ed832011-09-13 10:45:40 +03004474 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
4475 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004476
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004477 /*
4478 * Decode and fetch the source operand: register, memory
4479 * or immediate.
4480 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004481 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004482 if (rc != X86EMUL_CONTINUE)
4483 goto done;
4484
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004485 /*
4486 * Decode and fetch the second source operand: register, memory
4487 * or immediate.
4488 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004489 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004490 if (rc != X86EMUL_CONTINUE)
4491 goto done;
4492
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004493 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03004494 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004495
4496done:
Avi Kivityf09ed832011-09-13 10:45:40 +03004497 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
4498 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004499
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004500 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004501}
4502
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004503bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4504{
4505 return ctxt->d & PageTable;
4506}
4507
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004508static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4509{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004510 /* The second termination condition only applies for REPE
4511 * and REPNE. Test if the repeat string operation prefix is
4512 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4513 * corresponding termination condition according to:
4514 * - if REPE/REPZ and ZF = 0 then done
4515 * - if REPNE/REPNZ and ZF = 1 then done
4516 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004517 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4518 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4519 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004520 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004521 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004522 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4523 return true;
4524
4525 return false;
4526}
4527
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004528static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4529{
4530 bool fault = false;
4531
4532 ctxt->ops->get_fpu(ctxt);
4533 asm volatile("1: fwait \n\t"
4534 "2: \n\t"
4535 ".pushsection .fixup,\"ax\" \n\t"
4536 "3: \n\t"
4537 "movb $1, %[fault] \n\t"
4538 "jmp 2b \n\t"
4539 ".popsection \n\t"
4540 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004541 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004542 ctxt->ops->put_fpu(ctxt);
4543
4544 if (unlikely(fault))
4545 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4546
4547 return X86EMUL_CONTINUE;
4548}
4549
4550static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4551 struct operand *op)
4552{
4553 if (op->type == OP_MM)
4554 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4555}
4556
Avi Kivitye28bbd42013-01-04 16:18:48 +02004557static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4558{
4559 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
4560 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
4561 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
4562 : "+a"(ctxt->dst.val), "+b"(ctxt->src.val), [flags]"+D"(flags)
4563 : "c"(ctxt->src2.val), [fastop]"S"(fop));
4564 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
4565 return X86EMUL_CONTINUE;
4566}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004567
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004568int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004569{
Mathias Krause0225fb52012-08-30 01:30:16 +02004570 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004571 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004572 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004573
Avi Kivity9dac77f2011-06-01 15:34:25 +03004574 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004575
Avi Kivity9dac77f2011-06-01 15:34:25 +03004576 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004577 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02004578 goto done;
4579 }
4580
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004581 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004582 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004583 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004584 goto done;
4585 }
4586
Avi Kivity9dac77f2011-06-01 15:34:25 +03004587 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004588 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004589 goto done;
4590 }
4591
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004592 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4593 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02004594 rc = emulate_ud(ctxt);
4595 goto done;
4596 }
4597
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004598 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02004599 rc = emulate_nm(ctxt);
4600 goto done;
4601 }
4602
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004603 if (ctxt->d & Mmx) {
4604 rc = flush_pending_x87_faults(ctxt);
4605 if (rc != X86EMUL_CONTINUE)
4606 goto done;
4607 /*
4608 * Now that we know the fpu is exception safe, we can fetch
4609 * operands from it.
4610 */
4611 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4612 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4613 if (!(ctxt->d & Mov))
4614 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4615 }
4616
Avi Kivity9dac77f2011-06-01 15:34:25 +03004617 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4618 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004619 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004620 if (rc != X86EMUL_CONTINUE)
4621 goto done;
4622 }
4623
Gleb Natapove92805a2010-02-10 14:21:35 +02004624 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004625 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004626 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02004627 goto done;
4628 }
4629
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004630 /* Instruction can only be executed in protected mode */
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03004631 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004632 rc = emulate_ud(ctxt);
4633 goto done;
4634 }
4635
Joerg Roedeld09beab2011-04-04 12:39:25 +02004636 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004637 if (ctxt->check_perm) {
4638 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004639 if (rc != X86EMUL_CONTINUE)
4640 goto done;
4641 }
4642
Avi Kivity9dac77f2011-06-01 15:34:25 +03004643 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4644 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004645 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004646 if (rc != X86EMUL_CONTINUE)
4647 goto done;
4648 }
4649
Avi Kivity9dac77f2011-06-01 15:34:25 +03004650 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004651 /* All REP prefixes have the same first termination condition */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004652 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004653 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004654 goto done;
4655 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004656 }
4657
Avi Kivity9dac77f2011-06-01 15:34:25 +03004658 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4659 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4660 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004661 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004662 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004663 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004664 }
4665
Avi Kivity9dac77f2011-06-01 15:34:25 +03004666 if (ctxt->src2.type == OP_MEM) {
4667 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4668 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004669 if (rc != X86EMUL_CONTINUE)
4670 goto done;
4671 }
4672
Avi Kivity9dac77f2011-06-01 15:34:25 +03004673 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004674 goto special_insn;
4675
4676
Avi Kivity9dac77f2011-06-01 15:34:25 +03004677 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004678 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004679 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4680 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004681 if (rc != X86EMUL_CONTINUE)
4682 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004683 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004684 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004685
Avi Kivity018a98d2007-11-27 19:30:56 +02004686special_insn:
4687
Avi Kivity9dac77f2011-06-01 15:34:25 +03004688 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4689 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004690 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004691 if (rc != X86EMUL_CONTINUE)
4692 goto done;
4693 }
4694
Avi Kivity9dac77f2011-06-01 15:34:25 +03004695 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004696 if (ctxt->d & Fastop) {
4697 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4698 rc = fastop(ctxt, fop);
4699 if (rc != X86EMUL_CONTINUE)
4700 goto done;
4701 goto writeback;
4702 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004703 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004704 if (rc != X86EMUL_CONTINUE)
4705 goto done;
4706 goto writeback;
4707 }
4708
Avi Kivity9dac77f2011-06-01 15:34:25 +03004709 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004710 goto twobyte_insn;
4711
Avi Kivity9dac77f2011-06-01 15:34:25 +03004712 switch (ctxt->b) {
Avi Kivity33615aa2007-10-31 11:15:56 +02004713 case 0x40 ... 0x47: /* inc r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004714 emulate_1op(ctxt, "inc");
Avi Kivity33615aa2007-10-31 11:15:56 +02004715 break;
4716 case 0x48 ... 0x4f: /* dec r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004717 emulate_1op(ctxt, "dec");
Avi Kivity33615aa2007-10-31 11:15:56 +02004718 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004719 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004720 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004721 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004722 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004723 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004724 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004725 if (test_cc(ctxt->b, ctxt->eflags))
4726 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004727 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004728 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004729 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004730 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004731 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004732 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Mohammed Gamal34698d82010-08-04 14:41:04 +03004733 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004734 rc = em_xchg(ctxt);
4735 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004736 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004737 switch (ctxt->op_bytes) {
4738 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4739 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4740 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004741 }
4742 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004743 case 0xc0 ... 0xc1:
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004744 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004745 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004746 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004747 rc = emulate_int(ctxt, 3);
4748 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004749 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004750 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004751 break;
4752 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004753 if (ctxt->eflags & EFLG_OF)
4754 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004755 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004756 case 0xd0 ... 0xd1: /* Grp2 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004757 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004758 break;
4759 case 0xd2 ... 0xd3: /* Grp2 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004760 ctxt->src.val = reg_read(ctxt, VCPU_REGS_RCX);
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004761 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004762 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004763 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004764 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004765 jmp_rel(ctxt, ctxt->src.val);
4766 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004767 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004768 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004769 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004770 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004771 case 0xf5: /* cmc */
4772 /* complement carry flag from eflags reg */
4773 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004774 break;
4775 case 0xf8: /* clc */
4776 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004777 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004778 case 0xf9: /* stc */
4779 ctxt->eflags |= EFLG_CF;
4780 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004781 case 0xfc: /* cld */
4782 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004783 break;
4784 case 0xfd: /* std */
4785 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004786 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004787 default:
4788 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004789 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004790
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004791 if (rc != X86EMUL_CONTINUE)
4792 goto done;
4793
Avi Kivity018a98d2007-11-27 19:30:56 +02004794writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09004795 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004796 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02004797 goto done;
4798
Gleb Natapov5cd21912010-03-18 15:20:26 +02004799 /*
4800 * restore dst type in case the decoding will be reused
4801 * (happens for string instruction )
4802 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004803 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004804
Avi Kivity9dac77f2011-06-01 15:34:25 +03004805 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004806 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004807
Avi Kivity9dac77f2011-06-01 15:34:25 +03004808 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004809 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004810
Avi Kivity9dac77f2011-06-01 15:34:25 +03004811 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004812 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004813 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004814 if ((ctxt->d & SrcMask) == SrcSI)
4815 count = ctxt->src.count;
4816 else
4817 count = ctxt->dst.count;
4818 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4819 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004820
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004821 if (!string_insn_completed(ctxt)) {
4822 /*
4823 * Re-enter guest when pio read ahead buffer is empty
4824 * or, if it is not used, after each 1024 iteration.
4825 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004826 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004827 (r->end == 0 || r->end != r->pos)) {
4828 /*
4829 * Reset read cache. Usually happens before
4830 * decode, but since instruction is restarted
4831 * we have to do it here.
4832 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004833 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004834 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004835 return EMULATION_RESTART;
4836 }
4837 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004838 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004839 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004840
Avi Kivity9dac77f2011-06-01 15:34:25 +03004841 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004842
4843done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004844 if (rc == X86EMUL_PROPAGATE_FAULT)
4845 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004846 if (rc == X86EMUL_INTERCEPTED)
4847 return EMULATION_INTERCEPTED;
4848
Avi Kivitydd856ef2012-08-27 23:46:17 +03004849 if (rc == X86EMUL_CONTINUE)
4850 writeback_registers(ctxt);
4851
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004852 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004853
4854twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004855 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004856 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004857 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004858 break;
4859 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004860 case 0x0d: /* GrpP (prefetch) */
4861 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004862 break;
4863 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004864 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004865 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004866 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004867 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004868 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004869 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004870 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4871 if (!test_cc(ctxt->b, ctxt->eflags))
4872 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004873 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004874 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004875 if (test_cc(ctxt->b, ctxt->eflags))
4876 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004877 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004878 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004879 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004880 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004881 case 0xa4: /* shld imm8, r, r/m */
4882 case 0xa5: /* shld cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004883 emulate_2op_cl(ctxt, "shld");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004884 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004885 case 0xac: /* shrd imm8, r, r/m */
4886 case 0xad: /* shrd cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004887 emulate_2op_cl(ctxt, "shrd");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004888 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004889 case 0xae: /* clflush */
4890 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004891 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004892 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004893 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004894 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004895 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004896 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004897 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004898 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004899 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004900 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004901 case 0xc0 ... 0xc1: /* xadd */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004902 emulate_2op_SrcV(ctxt, "add");
Wei Yongjun92f738a2010-08-17 09:19:34 +08004903 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004904 ctxt->src.val = ctxt->dst.orig_val;
4905 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004906 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004907 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004908 ctxt->dst.bytes = ctxt->op_bytes;
4909 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4910 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004911 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004912 default:
4913 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004914 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004915
4916 if (rc != X86EMUL_CONTINUE)
4917 goto done;
4918
Avi Kivity6aa8b732006-12-10 02:21:36 -08004919 goto writeback;
4920
4921cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004922 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004923}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004924
4925void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
4926{
4927 invalidate_registers(ctxt);
4928}
4929
4930void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
4931{
4932 writeback_registers(ctxt);
4933}