blob: abe95d2e6848b9014dc6e845e213a254d5957e3a [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivityb7d491e2013-01-04 16:18:49 +020027#include <linux/stringify.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028
Avi Kivity3eeb3282010-01-21 15:31:48 +020029#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020030#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020031
Avi Kivity6aa8b732006-12-10 02:21:36 -080032/*
Avi Kivitya99455492011-09-13 10:45:41 +030033 * Operand types
34 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030035#define OpNone 0ull
36#define OpImplicit 1ull /* No generic decode */
37#define OpReg 2ull /* Register */
38#define OpMem 3ull /* Memory */
39#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
40#define OpDI 5ull /* ES:DI/EDI/RDI */
41#define OpMem64 6ull /* Memory, 64-bit */
42#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
43#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030044#define OpCL 9ull /* CL register (for shifts) */
45#define OpImmByte 10ull /* 8-bit sign extended immediate */
46#define OpOne 11ull /* Implied 1 */
Nadav Amit5e2c6882012-12-06 21:55:10 -020047#define OpImm 12ull /* Sign extended up to 32-bit immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030048#define OpMem16 13ull /* Memory operand (16-bit). */
49#define OpMem32 14ull /* Memory operand (32-bit). */
50#define OpImmU 15ull /* Immediate operand, zero extended */
51#define OpSI 16ull /* SI/ESI/RSI */
52#define OpImmFAddr 17ull /* Immediate far address */
53#define OpMemFAddr 18ull /* Far address in memory */
54#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030055#define OpES 20ull /* ES */
56#define OpCS 21ull /* CS */
57#define OpSS 22ull /* SS */
58#define OpDS 23ull /* DS */
59#define OpFS 24ull /* FS */
60#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020061#define OpMem8 26ull /* 8-bit zero extended memory operand */
Nadav Amit5e2c6882012-12-06 21:55:10 -020062#define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */
Paolo Bonzini7fa57952013-05-09 11:32:50 +020063#define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */
Avi Kivity820207c2013-02-09 11:31:45 +020064#define OpAccLo 29ull /* Low part of extended acc (AX/AX/EAX/RAX) */
65#define OpAccHi 30ull /* High part of extended acc (-/DX/EDX/RDX) */
Avi Kivitya99455492011-09-13 10:45:41 +030066
Avi Kivity0fe59122011-09-13 10:45:47 +030067#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030068#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya99455492011-09-13 10:45:41 +030069
70/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080071 * Opcode effective-address decode tables.
72 * Note that we only emulate instructions that have at least one memory
73 * operand (excluding implicit stack references). We assume that stack
74 * references and instruction fetches will never occur in special memory
75 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
76 * not be handled.
77 */
78
79/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030080#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080081/* Destination operand type. */
Avi Kivitya99455492011-09-13 10:45:41 +030082#define DstShift 1
83#define ImplicitOps (OpImplicit << DstShift)
84#define DstReg (OpReg << DstShift)
85#define DstMem (OpMem << DstShift)
86#define DstAcc (OpAcc << DstShift)
87#define DstDI (OpDI << DstShift)
88#define DstMem64 (OpMem64 << DstShift)
89#define DstImmUByte (OpImmUByte << DstShift)
90#define DstDX (OpDX << DstShift)
Avi Kivity820207c2013-02-09 11:31:45 +020091#define DstAccLo (OpAccLo << DstShift)
Avi Kivitya99455492011-09-13 10:45:41 +030092#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080093/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030094#define SrcShift 6
95#define SrcNone (OpNone << SrcShift)
96#define SrcReg (OpReg << SrcShift)
97#define SrcMem (OpMem << SrcShift)
98#define SrcMem16 (OpMem16 << SrcShift)
99#define SrcMem32 (OpMem32 << SrcShift)
100#define SrcImm (OpImm << SrcShift)
101#define SrcImmByte (OpImmByte << SrcShift)
102#define SrcOne (OpOne << SrcShift)
103#define SrcImmUByte (OpImmUByte << SrcShift)
104#define SrcImmU (OpImmU << SrcShift)
105#define SrcSI (OpSI << SrcShift)
Paolo Bonzini7fa57952013-05-09 11:32:50 +0200106#define SrcXLat (OpXLat << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300107#define SrcImmFAddr (OpImmFAddr << SrcShift)
108#define SrcMemFAddr (OpMemFAddr << SrcShift)
109#define SrcAcc (OpAcc << SrcShift)
110#define SrcImmU16 (OpImmU16 << SrcShift)
Nadav Amit5e2c6882012-12-06 21:55:10 -0200111#define SrcImm64 (OpImm64 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300112#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200113#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity820207c2013-02-09 11:31:45 +0200114#define SrcAccHi (OpAccHi << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300115#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300116#define BitOp (1<<11)
117#define MemAbs (1<<12) /* Memory operand is absolute displacement */
118#define String (1<<13) /* String instruction (rep capable) */
119#define Stack (1<<14) /* Stack instruction (push/pop) */
120#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
121#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
122#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
123#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
124#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
Gleb Natapov045a2822012-12-20 16:57:43 +0200125#define Escape (5<<15) /* Escape to coprocessor instruction */
Nadav Amit39f062f2014-11-26 15:47:18 +0200126#define InstrDual (6<<15) /* Alternate instruction decoding of mod == 3 */
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300127#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300128/* Generic ModRM decode. */
129#define ModRM (1<<19)
130/* Destination is only written; never read. */
131#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300132/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200133#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Borislav Petkovb51e9742013-09-22 16:44:52 +0200134#define EmulateOnUD (1<<22) /* Emulate if unsupported by the host */
Avi Kivity5a506b12010-08-01 15:10:29 +0300135#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300136#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300137#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200138#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200139#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300140#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800141#define PageTable (1 << 29) /* instruction used to write page table */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300142#define NotImpl (1 << 30) /* instruction is not implemented */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100143/* Source 2 operand type */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300144#define Src2Shift (31)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300145#define Src2None (OpNone << Src2Shift)
Avi Kivityab2c5ce2013-02-09 11:31:46 +0200146#define Src2Mem (OpMem << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300147#define Src2CL (OpCL << Src2Shift)
148#define Src2ImmByte (OpImmByte << Src2Shift)
149#define Src2One (OpOne << Src2Shift)
150#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300151#define Src2ES (OpES << Src2Shift)
152#define Src2CS (OpCS << Src2Shift)
153#define Src2SS (OpSS << Src2Shift)
154#define Src2DS (OpDS << Src2Shift)
155#define Src2FS (OpFS << Src2Shift)
156#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300157#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300158#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Avi Kivity1c11b372012-04-09 18:39:59 +0300159#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
160#define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */
161#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
Avi Kivitye28bbd42013-01-04 16:18:48 +0200162#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
Avi Kivityb6744dc2013-01-04 16:18:50 +0200163#define NoWrite ((u64)1 << 45) /* No writeback */
Avi Kivityfb32b1e2013-02-09 11:31:44 +0200164#define SrcWrite ((u64)1 << 46) /* Write back src operand */
Nadav Amit9b88ae92014-05-25 23:05:21 +0300165#define NoMod ((u64)1 << 47) /* Mod field is ignored */
Paolo Bonzinid40a6892014-03-27 11:58:02 +0100166#define Intercept ((u64)1 << 48) /* Has valid intercept field */
167#define CheckPerm ((u64)1 << 49) /* Has valid check_perm field */
Nadav Amit10e38fc2014-06-18 17:19:34 +0300168#define NoBigReal ((u64)1 << 50) /* No big real mode */
Nadav Amit68efa762014-06-18 17:19:35 +0300169#define PrivUD ((u64)1 << 51) /* #UD instead of #GP on CPL > 0 */
Nadav Amit58b70752014-10-24 11:35:09 +0300170#define NearBranch ((u64)1 << 52) /* Near branches */
Nadav Amited9aad22014-11-02 11:55:00 +0200171#define No16 ((u64)1 << 53) /* No 16 bit operand */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800172
Avi Kivity820207c2013-02-09 11:31:45 +0200173#define DstXacc (DstAccLo | SrcAccHi | SrcWrite)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800174
Avi Kivityd0e53322010-07-29 15:11:54 +0300175#define X2(x...) x, x
176#define X3(x...) X2(x), x
177#define X4(x...) X2(x), X2(x)
178#define X5(x...) X4(x), x
179#define X6(x...) X4(x), X2(x)
180#define X7(x...) X4(x), X3(x)
181#define X8(x...) X4(x), X4(x)
182#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300183
Avi Kivitye28bbd42013-01-04 16:18:48 +0200184#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
185#define FASTOP_SIZE 8
186
187/*
188 * fastop functions have a special calling convention:
189 *
Avi Kivity017da7b2013-02-09 11:31:47 +0200190 * dst: rax (in/out)
191 * src: rdx (in/out)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200192 * src2: rcx (in)
193 * flags: rflags (in/out)
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200194 * ex: rsi (in:fastop pointer, out:zero if exception)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200195 *
196 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
197 * different operand sizes can be reached by calculation, rather than a jump
198 * table (which would be bigger than the code).
199 *
200 * fastop functions are declared as taking a never-defined fastop parameter,
201 * so they can't be called from C directly.
202 */
203
204struct fastop;
205
Avi Kivityd65b1de2010-07-29 15:11:35 +0300206struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300207 u64 flags : 56;
208 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300209 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300210 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200211 const struct opcode *group;
212 const struct group_dual *gdual;
213 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200214 const struct escape *esc;
Nadav Amit39f062f2014-11-26 15:47:18 +0200215 const struct instr_dual *idual;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200216 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300217 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200218 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300219};
220
221struct group_dual {
222 struct opcode mod012[8];
223 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300224};
225
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200226struct gprefix {
227 struct opcode pfx_no;
228 struct opcode pfx_66;
229 struct opcode pfx_f2;
230 struct opcode pfx_f3;
231};
232
Gleb Natapov045a2822012-12-20 16:57:43 +0200233struct escape {
234 struct opcode op[8];
235 struct opcode high[64];
236};
237
Nadav Amit39f062f2014-11-26 15:47:18 +0200238struct instr_dual {
239 struct opcode mod012;
240 struct opcode mod3;
241};
242
Avi Kivity6aa8b732006-12-10 02:21:36 -0800243/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200244#define EFLG_ID (1<<21)
245#define EFLG_VIP (1<<20)
246#define EFLG_VIF (1<<19)
247#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200248#define EFLG_VM (1<<17)
249#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200250#define EFLG_IOPL (3<<12)
251#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800252#define EFLG_OF (1<<11)
253#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200254#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200255#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800256#define EFLG_SF (1<<7)
257#define EFLG_ZF (1<<6)
258#define EFLG_AF (1<<4)
259#define EFLG_PF (1<<2)
260#define EFLG_CF (1<<0)
261
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300262#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
263#define EFLG_RESERVED_ONE_MASK 2
264
Avi Kivitydd856ef2012-08-27 23:46:17 +0300265static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
266{
267 if (!(ctxt->regs_valid & (1 << nr))) {
268 ctxt->regs_valid |= 1 << nr;
269 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
270 }
271 return ctxt->_regs[nr];
272}
273
274static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
275{
276 ctxt->regs_valid |= 1 << nr;
277 ctxt->regs_dirty |= 1 << nr;
278 return &ctxt->_regs[nr];
279}
280
281static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
282{
283 reg_read(ctxt, nr);
284 return reg_write(ctxt, nr);
285}
286
287static void writeback_registers(struct x86_emulate_ctxt *ctxt)
288{
289 unsigned reg;
290
291 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
292 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
293}
294
295static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
296{
297 ctxt->regs_dirty = 0;
298 ctxt->regs_valid = 0;
299}
300
Avi Kivity6aa8b732006-12-10 02:21:36 -0800301/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800302 * These EFLAGS bits are restored from saved value during emulation, and
303 * any changes are written back to the saved value after emulation.
304 */
305#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
306
Avi Kivitydda96d82008-11-26 15:14:10 +0200307#ifdef CONFIG_X86_64
308#define ON64(x) x
309#else
310#define ON64(x)
311#endif
312
Avi Kivity4d758342013-01-19 19:51:55 +0200313static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *));
314
Avi Kivityb7d491e2013-01-04 16:18:49 +0200315#define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t"
316#define FOP_RET "ret \n\t"
317
318#define FOP_START(op) \
319 extern void em_##op(struct fastop *fake); \
320 asm(".pushsection .text, \"ax\" \n\t" \
321 ".global em_" #op " \n\t" \
322 FOP_ALIGN \
323 "em_" #op ": \n\t"
324
325#define FOP_END \
326 ".popsection")
327
Avi Kivity0bdea062013-01-19 19:51:50 +0200328#define FOPNOP() FOP_ALIGN FOP_RET
329
Avi Kivityb7d491e2013-01-04 16:18:49 +0200330#define FOP1E(op, dst) \
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200331 FOP_ALIGN "10: " #op " %" #dst " \n\t" FOP_RET
332
333#define FOP1EEX(op, dst) \
334 FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200335
336#define FASTOP1(op) \
337 FOP_START(op) \
338 FOP1E(op##b, al) \
339 FOP1E(op##w, ax) \
340 FOP1E(op##l, eax) \
341 ON64(FOP1E(op##q, rax)) \
342 FOP_END
343
Avi Kivityb9fa4092013-02-09 11:31:48 +0200344/* 1-operand, using src2 (for MUL/DIV r/m) */
345#define FASTOP1SRC2(op, name) \
346 FOP_START(name) \
347 FOP1E(op, cl) \
348 FOP1E(op, cx) \
349 FOP1E(op, ecx) \
350 ON64(FOP1E(op, rcx)) \
351 FOP_END
352
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200353/* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */
354#define FASTOP1SRC2EX(op, name) \
355 FOP_START(name) \
356 FOP1EEX(op, cl) \
357 FOP1EEX(op, cx) \
358 FOP1EEX(op, ecx) \
359 ON64(FOP1EEX(op, rcx)) \
360 FOP_END
361
Avi Kivityf7857f32013-01-04 16:18:53 +0200362#define FOP2E(op, dst, src) \
363 FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET
364
365#define FASTOP2(op) \
366 FOP_START(op) \
Avi Kivity017da7b2013-02-09 11:31:47 +0200367 FOP2E(op##b, al, dl) \
368 FOP2E(op##w, ax, dx) \
369 FOP2E(op##l, eax, edx) \
370 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivityf7857f32013-01-04 16:18:53 +0200371 FOP_END
372
Avi Kivity11c363b2013-01-19 19:51:54 +0200373/* 2 operand, word only */
374#define FASTOP2W(op) \
375 FOP_START(op) \
376 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200377 FOP2E(op##w, ax, dx) \
378 FOP2E(op##l, eax, edx) \
379 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivity11c363b2013-01-19 19:51:54 +0200380 FOP_END
381
Avi Kivity007a3b52013-01-19 19:51:51 +0200382/* 2 operand, src is CL */
383#define FASTOP2CL(op) \
384 FOP_START(op) \
385 FOP2E(op##b, al, cl) \
386 FOP2E(op##w, ax, cl) \
387 FOP2E(op##l, eax, cl) \
388 ON64(FOP2E(op##q, rax, cl)) \
389 FOP_END
390
Nadav Amit5aca3722014-11-02 11:54:50 +0200391/* 2 operand, src and dest are reversed */
392#define FASTOP2R(op, name) \
393 FOP_START(name) \
394 FOP2E(op##b, dl, al) \
395 FOP2E(op##w, dx, ax) \
396 FOP2E(op##l, edx, eax) \
397 ON64(FOP2E(op##q, rdx, rax)) \
398 FOP_END
399
Avi Kivity0bdea062013-01-19 19:51:50 +0200400#define FOP3E(op, dst, src, src2) \
401 FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
402
403/* 3-operand, word-only, src2=cl */
404#define FASTOP3WCL(op) \
405 FOP_START(op) \
406 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200407 FOP3E(op##w, ax, dx, cl) \
408 FOP3E(op##l, eax, edx, cl) \
409 ON64(FOP3E(op##q, rax, rdx, cl)) \
Avi Kivity0bdea062013-01-19 19:51:50 +0200410 FOP_END
411
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200412/* Special case for SETcc - 1 instruction per cc */
413#define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t"
414
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200415asm(".global kvm_fastop_exception \n"
416 "kvm_fastop_exception: xor %esi, %esi; ret");
417
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200418FOP_START(setcc)
419FOP_SETCC(seto)
420FOP_SETCC(setno)
421FOP_SETCC(setc)
422FOP_SETCC(setnc)
423FOP_SETCC(setz)
424FOP_SETCC(setnz)
425FOP_SETCC(setbe)
426FOP_SETCC(setnbe)
427FOP_SETCC(sets)
428FOP_SETCC(setns)
429FOP_SETCC(setp)
430FOP_SETCC(setnp)
431FOP_SETCC(setl)
432FOP_SETCC(setnl)
433FOP_SETCC(setle)
434FOP_SETCC(setnle)
435FOP_END;
436
Paolo Bonzini326f5782013-05-09 11:32:51 +0200437FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET
438FOP_END;
439
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200440static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
441 enum x86_intercept intercept,
442 enum x86_intercept_stage stage)
443{
444 struct x86_instruction_info info = {
445 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300446 .rep_prefix = ctxt->rep_prefix,
447 .modrm_mod = ctxt->modrm_mod,
448 .modrm_reg = ctxt->modrm_reg,
449 .modrm_rm = ctxt->modrm_rm,
450 .src_val = ctxt->src.val64,
Jan Kiszka6cbc5f52014-06-30 12:52:55 +0200451 .dst_val = ctxt->dst.val64,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300452 .src_bytes = ctxt->src.bytes,
453 .dst_bytes = ctxt->dst.bytes,
454 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200455 .next_rip = ctxt->eip,
456 };
457
Avi Kivity29535382011-04-20 13:37:53 +0300458 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200459}
460
Avi Kivityf47cfa32012-06-07 17:49:24 +0300461static void assign_masked(ulong *dest, ulong src, ulong mask)
462{
463 *dest = (*dest & ~mask) | (src & mask);
464}
465
Avi Kivity9dac77f2011-06-01 15:34:25 +0300466static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800467{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300468 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800469}
470
Avi Kivityf47cfa32012-06-07 17:49:24 +0300471static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
472{
473 u16 sel;
474 struct desc_struct ss;
475
476 if (ctxt->mode == X86EMUL_MODE_PROT64)
477 return ~0UL;
478 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
479 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
480}
481
Avi Kivity612e89f2012-06-12 20:03:23 +0300482static int stack_size(struct x86_emulate_ctxt *ctxt)
483{
484 return (__fls(stack_mask(ctxt)) + 1) >> 3;
485}
486
Avi Kivity6aa8b732006-12-10 02:21:36 -0800487/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800488static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300489address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800490{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300491 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800492 return reg;
493 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300494 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800495}
496
497static inline unsigned long
Paolo Bonzini01485a22014-11-19 18:25:08 +0100498register_address(struct x86_emulate_ctxt *ctxt, int reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800499{
Paolo Bonzini01485a22014-11-19 18:25:08 +0100500 return address_mask(ctxt, reg_read(ctxt, reg));
Harvey Harrisone4706772008-02-19 07:40:38 -0800501}
502
Avi Kivity5ad105e2012-08-19 14:34:31 +0300503static void masked_increment(ulong *reg, ulong mask, int inc)
504{
505 assign_masked(reg, *reg + inc, mask);
506}
507
Harvey Harrison7a9572752008-02-19 07:40:41 -0800508static inline void
Paolo Bonzini01485a22014-11-19 18:25:08 +0100509register_address_increment(struct x86_emulate_ctxt *ctxt, int reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800510{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300511 ulong mask;
512
Avi Kivity9dac77f2011-06-01 15:34:25 +0300513 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300514 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800515 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300516 mask = ad_mask(ctxt);
Paolo Bonzini01485a22014-11-19 18:25:08 +0100517 masked_increment(reg_rmw(ctxt, reg), mask, inc);
Avi Kivity5ad105e2012-08-19 14:34:31 +0300518}
519
520static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
521{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300522 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800523}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800524
Avi Kivity56697682011-04-03 14:08:51 +0300525static u32 desc_limit_scaled(struct desc_struct *desc)
526{
527 u32 limit = get_desc_limit(desc);
528
529 return desc->g ? (limit << 12) | 0xfff : limit;
530}
531
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900532static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300533{
534 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
535 return 0;
536
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900537 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300538}
539
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200540static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
541 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300542{
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +0200543 WARN_ON(vec > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +0200544 ctxt->exception.vector = vec;
545 ctxt->exception.error_code = error;
546 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200547 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300548}
549
Joerg Roedel3b88e412011-04-04 12:39:29 +0200550static int emulate_db(struct x86_emulate_ctxt *ctxt)
551{
552 return emulate_exception(ctxt, DB_VECTOR, 0, false);
553}
554
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200555static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300556{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200557 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300558}
559
Avi Kivity618ff152011-04-03 12:32:09 +0300560static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
561{
562 return emulate_exception(ctxt, SS_VECTOR, err, true);
563}
564
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200565static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300566{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200567 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300568}
569
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200570static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300571{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200572 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300573}
574
Avi Kivity34d1f492010-08-26 11:59:01 +0300575static int emulate_de(struct x86_emulate_ctxt *ctxt)
576{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200577 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300578}
579
Avi Kivity1253791d2011-03-29 11:41:27 +0200580static int emulate_nm(struct x86_emulate_ctxt *ctxt)
581{
582 return emulate_exception(ctxt, NM_VECTOR, 0, false);
583}
584
Avi Kivity1aa36612011-04-27 13:20:30 +0300585static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
586{
587 u16 selector;
588 struct desc_struct desc;
589
590 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
591 return selector;
592}
593
594static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
595 unsigned seg)
596{
597 u16 dummy;
598 u32 base3;
599 struct desc_struct desc;
600
601 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
602 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
603}
604
Avi Kivity1c11b372012-04-09 18:39:59 +0300605/*
606 * x86 defines three classes of vector instructions: explicitly
607 * aligned, explicitly unaligned, and the rest, which change behaviour
608 * depending on whether they're AVX encoded or not.
609 *
610 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
611 * subject to the same check.
612 */
613static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
614{
615 if (likely(size < 16))
616 return false;
617
618 if (ctxt->d & Aligned)
619 return true;
620 else if (ctxt->d & Unaligned)
621 return false;
622 else if (ctxt->d & Avx)
623 return false;
624 else
625 return true;
626}
627
Paolo Bonzinid09155d2014-10-27 14:54:44 +0100628static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
629 struct segmented_address addr,
630 unsigned *max_size, unsigned size,
631 bool write, bool fetch,
Nadav Amitd50eaa12014-11-19 17:43:11 +0200632 enum x86emul_mode mode, ulong *linear)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300633{
Avi Kivity618ff152011-04-03 12:32:09 +0300634 struct desc_struct desc;
635 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300636 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300637 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300638 u16 sel;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300639
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900640 la = seg_base(ctxt, addr.seg) + addr.ea;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100641 *max_size = 0;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200642 switch (mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300643 case X86EMUL_MODE_PROT64:
Nadav Amit4be4de72014-09-18 22:39:40 +0300644 if (is_noncanonical_address(la))
Nadav Amitabc7d8a2014-11-19 17:43:12 +0200645 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100646
647 *max_size = min_t(u64, ~0u, (1ull << 48) - la);
648 if (size > *max_size)
649 goto bad;
Avi Kivity618ff152011-04-03 12:32:09 +0300650 break;
651 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300652 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
653 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300654 if (!usable)
655 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200656 /* code segment in protected mode or read-only data segment */
657 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
658 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300659 goto bad;
660 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400661 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300662 goto bad;
663 lim = desc_limit_scaled(&desc);
Paolo Bonzini997b0412014-11-19 18:33:38 +0100664 if (!(desc.type & 8) && (desc.type & 4)) {
Guo Chaofc058682012-06-28 15:19:51 +0800665 /* expand-down segment */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100666 if (addr.ea <= lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300667 goto bad;
668 lim = desc.d ? 0xffffffff : 0xffff;
Avi Kivity618ff152011-04-03 12:32:09 +0300669 }
Paolo Bonzini997b0412014-11-19 18:33:38 +0100670 if (addr.ea > lim)
671 goto bad;
672 *max_size = min_t(u64, ~0u, (u64)lim + 1 - addr.ea);
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100673 if (size > *max_size)
674 goto bad;
Nadav Amit31ff6482014-11-19 17:43:13 +0200675 la &= (u32)-1;
Avi Kivity618ff152011-04-03 12:32:09 +0300676 break;
677 }
Avi Kivity1c11b372012-04-09 18:39:59 +0300678 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
679 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300680 *linear = la;
681 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300682bad:
683 if (addr.seg == VCPU_SREG_SS)
Paolo Bonzini36061892014-10-27 14:40:49 +0100684 return emulate_ss(ctxt, 0);
Avi Kivity618ff152011-04-03 12:32:09 +0300685 else
Paolo Bonzini36061892014-10-27 14:40:49 +0100686 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300687}
688
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400689static int linearize(struct x86_emulate_ctxt *ctxt,
690 struct segmented_address addr,
691 unsigned size, bool write,
692 ulong *linear)
693{
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100694 unsigned max_size;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200695 return __linearize(ctxt, addr, &max_size, size, write, false,
696 ctxt->mode, linear);
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400697}
698
Nadav Amitd50eaa12014-11-19 17:43:11 +0200699static inline int assign_eip(struct x86_emulate_ctxt *ctxt, ulong dst,
700 enum x86emul_mode mode)
701{
702 ulong linear;
703 int rc;
704 unsigned max_size;
705 struct segmented_address addr = { .seg = VCPU_SREG_CS,
706 .ea = dst };
707
708 if (ctxt->op_bytes != sizeof(unsigned long))
709 addr.ea = dst & ((1UL << (ctxt->op_bytes << 3)) - 1);
710 rc = __linearize(ctxt, addr, &max_size, 1, false, true, mode, &linear);
711 if (rc == X86EMUL_CONTINUE)
712 ctxt->_eip = addr.ea;
713 return rc;
714}
715
716static inline int assign_eip_near(struct x86_emulate_ctxt *ctxt, ulong dst)
717{
718 return assign_eip(ctxt, dst, ctxt->mode);
719}
720
721static int assign_eip_far(struct x86_emulate_ctxt *ctxt, ulong dst,
722 const struct desc_struct *cs_desc)
723{
724 enum x86emul_mode mode = ctxt->mode;
725
726#ifdef CONFIG_X86_64
727 if (ctxt->mode >= X86EMUL_MODE_PROT32 && cs_desc->l) {
728 u64 efer = 0;
729
730 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
731 if (efer & EFER_LMA)
732 mode = X86EMUL_MODE_PROT64;
733 }
734#endif
735 if (mode == X86EMUL_MODE_PROT16 || mode == X86EMUL_MODE_PROT32)
736 mode = cs_desc->d ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
737 return assign_eip(ctxt, dst, mode);
738}
739
740static inline int jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
741{
742 return assign_eip_near(ctxt, ctxt->_eip + rel);
743}
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400744
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200745static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
746 struct segmented_address addr,
747 void *data,
748 unsigned size)
749{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200750 int rc;
751 ulong linear;
752
Avi Kivity83b87952011-04-03 11:31:19 +0300753 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200754 if (rc != X86EMUL_CONTINUE)
755 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300756 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200757}
758
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900759/*
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200760 * Prefetch the remaining bytes of the instruction without crossing page
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900761 * boundary if they are not in fetch_cache yet.
762 */
Paolo Bonzini9506d572014-05-06 13:05:25 +0200763static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
Avi Kivity62266862007-11-20 13:15:52 +0200764{
Avi Kivity62266862007-11-20 13:15:52 +0200765 int rc;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100766 unsigned size, max_size;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200767 unsigned long linear;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200768 int cur_size = ctxt->fetch.end - ctxt->fetch.data;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200769 struct segmented_address addr = { .seg = VCPU_SREG_CS,
Paolo Bonzini17052f12014-05-06 16:33:01 +0200770 .ea = ctxt->eip + cur_size };
771
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100772 /*
773 * We do not know exactly how many bytes will be needed, and
774 * __linearize is expensive, so fetch as much as possible. We
775 * just have to avoid going beyond the 15 byte limit, the end
776 * of the segment, or the end of the page.
777 *
778 * __linearize is called with size 0 so that it does not do any
779 * boundary check itself. Instead, we use max_size to check
780 * against op_size.
781 */
Nadav Amitd50eaa12014-11-19 17:43:11 +0200782 rc = __linearize(ctxt, addr, &max_size, 0, false, true, ctxt->mode,
783 &linear);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200784 if (unlikely(rc != X86EMUL_CONTINUE))
785 return rc;
786
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100787 size = min_t(unsigned, 15UL ^ cur_size, max_size);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200788 size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear));
Paolo Bonzini5cfc7e02014-05-06 13:05:25 +0200789
790 /*
791 * One instruction can only straddle two pages,
792 * and one has been loaded at the beginning of
793 * x86_decode_insn. So, if not enough bytes
794 * still, we must have hit the 15-byte boundary.
795 */
796 if (unlikely(size < op_size))
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100797 return emulate_gp(ctxt, 0);
798
Paolo Bonzini17052f12014-05-06 16:33:01 +0200799 rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end,
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200800 size, &ctxt->exception);
801 if (unlikely(rc != X86EMUL_CONTINUE))
802 return rc;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200803 ctxt->fetch.end += size;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900804 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200805}
806
Paolo Bonzini9506d572014-05-06 13:05:25 +0200807static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt,
808 unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200809{
Nadav Amit08da44a2014-10-03 01:10:04 +0300810 unsigned done_size = ctxt->fetch.end - ctxt->fetch.ptr;
811
812 if (unlikely(done_size < size))
813 return __do_insn_fetch_bytes(ctxt, size - done_size);
Paolo Bonzini9506d572014-05-06 13:05:25 +0200814 else
815 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200816}
817
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900818/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900819#define insn_fetch(_type, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200820({ _type _x; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200821 \
822 rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900823 if (rc != X86EMUL_CONTINUE) \
824 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200825 ctxt->_eip += sizeof(_type); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200826 _x = *(_type __aligned(1) *) ctxt->fetch.ptr; \
827 ctxt->fetch.ptr += sizeof(_type); \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200828 _x; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900829})
830
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900831#define insn_fetch_arr(_arr, _size, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200832({ \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200833 rc = do_insn_fetch_bytes(_ctxt, _size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900834 if (rc != X86EMUL_CONTINUE) \
835 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200836 ctxt->_eip += (_size); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200837 memcpy(_arr, ctxt->fetch.ptr, _size); \
838 ctxt->fetch.ptr += (_size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900839})
840
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000841/*
842 * Given the 'reg' portion of a ModRM byte, and a register block, return a
843 * pointer into the block that addresses the relevant register.
844 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
845 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300846static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200847 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848{
849 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200850 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800851
Avi Kivity6aa8b732006-12-10 02:21:36 -0800852 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300853 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
854 else
855 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800856 return p;
857}
858
859static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200860 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800861 u16 *size, unsigned long *address, int op_bytes)
862{
863 int rc;
864
865 if (op_bytes == 2)
866 op_bytes = 3;
867 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200868 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900869 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800870 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200871 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200872 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800873 return rc;
874}
875
Avi Kivity34b77652013-01-19 19:51:56 +0200876FASTOP2(add);
877FASTOP2(or);
878FASTOP2(adc);
879FASTOP2(sbb);
880FASTOP2(and);
881FASTOP2(sub);
882FASTOP2(xor);
883FASTOP2(cmp);
884FASTOP2(test);
885
Avi Kivityb9fa4092013-02-09 11:31:48 +0200886FASTOP1SRC2(mul, mul_ex);
887FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200888FASTOP1SRC2EX(div, div_ex);
889FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +0200890
Avi Kivity34b77652013-01-19 19:51:56 +0200891FASTOP3WCL(shld);
892FASTOP3WCL(shrd);
893
894FASTOP2W(imul);
895
896FASTOP1(not);
897FASTOP1(neg);
898FASTOP1(inc);
899FASTOP1(dec);
900
901FASTOP2CL(rol);
902FASTOP2CL(ror);
903FASTOP2CL(rcl);
904FASTOP2CL(rcr);
905FASTOP2CL(shl);
906FASTOP2CL(shr);
907FASTOP2CL(sar);
908
909FASTOP2W(bsf);
910FASTOP2W(bsr);
911FASTOP2W(bt);
912FASTOP2W(bts);
913FASTOP2W(btr);
914FASTOP2W(btc);
915
Avi Kivitye47a5f52013-02-09 11:31:51 +0200916FASTOP2(xadd);
917
Nadav Amit5aca3722014-11-02 11:54:50 +0200918FASTOP2R(cmp, cmp_r);
919
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200920static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300921{
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200922 u8 rc;
923 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300924
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200925 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +0200926 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200927 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
928 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300929}
930
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300931static void fetch_register_operand(struct operand *op)
932{
933 switch (op->bytes) {
934 case 1:
935 op->val = *(u8 *)op->addr.reg;
936 break;
937 case 2:
938 op->val = *(u16 *)op->addr.reg;
939 break;
940 case 4:
941 op->val = *(u32 *)op->addr.reg;
942 break;
943 case 8:
944 op->val = *(u64 *)op->addr.reg;
945 break;
946 }
947}
948
Avi Kivity1253791d2011-03-29 11:41:27 +0200949static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
950{
951 ctxt->ops->get_fpu(ctxt);
952 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200953 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
954 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
955 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
956 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
957 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
958 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
959 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
960 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200961#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200962 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
963 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
964 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
965 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
966 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
967 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
968 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
969 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200970#endif
971 default: BUG();
972 }
973 ctxt->ops->put_fpu(ctxt);
974}
975
976static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
977 int reg)
978{
979 ctxt->ops->get_fpu(ctxt);
980 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200981 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
982 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
983 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
984 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
985 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
986 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
987 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
988 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200989#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200990 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
991 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
992 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
993 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
994 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
995 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
996 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
997 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200998#endif
999 default: BUG();
1000 }
1001 ctxt->ops->put_fpu(ctxt);
1002}
1003
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001004static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1005{
1006 ctxt->ops->get_fpu(ctxt);
1007 switch (reg) {
1008 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1009 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1010 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1011 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1012 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1013 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1014 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1015 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1016 default: BUG();
1017 }
1018 ctxt->ops->put_fpu(ctxt);
1019}
1020
1021static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1022{
1023 ctxt->ops->get_fpu(ctxt);
1024 switch (reg) {
1025 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1026 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1027 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1028 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1029 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1030 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1031 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1032 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1033 default: BUG();
1034 }
1035 ctxt->ops->put_fpu(ctxt);
1036}
1037
Gleb Natapov045a2822012-12-20 16:57:43 +02001038static int em_fninit(struct x86_emulate_ctxt *ctxt)
1039{
1040 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1041 return emulate_nm(ctxt);
1042
1043 ctxt->ops->get_fpu(ctxt);
1044 asm volatile("fninit");
1045 ctxt->ops->put_fpu(ctxt);
1046 return X86EMUL_CONTINUE;
1047}
1048
1049static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1050{
1051 u16 fcw;
1052
1053 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1054 return emulate_nm(ctxt);
1055
1056 ctxt->ops->get_fpu(ctxt);
1057 asm volatile("fnstcw %0": "+m"(fcw));
1058 ctxt->ops->put_fpu(ctxt);
1059
1060 /* force 2 byte destination */
1061 ctxt->dst.bytes = 2;
1062 ctxt->dst.val = fcw;
1063
1064 return X86EMUL_CONTINUE;
1065}
1066
1067static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1068{
1069 u16 fsw;
1070
1071 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1072 return emulate_nm(ctxt);
1073
1074 ctxt->ops->get_fpu(ctxt);
1075 asm volatile("fnstsw %0": "+m"(fsw));
1076 ctxt->ops->put_fpu(ctxt);
1077
1078 /* force 2 byte destination */
1079 ctxt->dst.bytes = 2;
1080 ctxt->dst.val = fsw;
1081
1082 return X86EMUL_CONTINUE;
1083}
1084
Avi Kivity1253791d2011-03-29 11:41:27 +02001085static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001086 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001087{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001088 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001089
Avi Kivity9dac77f2011-06-01 15:34:25 +03001090 if (!(ctxt->d & ModRM))
1091 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity1253791d2011-03-29 11:41:27 +02001092
Avi Kivity9dac77f2011-06-01 15:34:25 +03001093 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001094 op->type = OP_XMM;
1095 op->bytes = 16;
1096 op->addr.xmm = reg;
1097 read_sse_reg(ctxt, &op->vec_val, reg);
1098 return;
1099 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001100 if (ctxt->d & Mmx) {
1101 reg &= 7;
1102 op->type = OP_MM;
1103 op->bytes = 8;
1104 op->addr.mm = reg;
1105 return;
1106 }
Avi Kivity1253791d2011-03-29 11:41:27 +02001107
Avi Kivity3c118e22007-10-31 10:27:04 +02001108 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001109 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1110 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1111
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001112 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001113 op->orig_val = op->val;
1114}
1115
Avi Kivitya6e34072012-06-10 17:15:39 +03001116static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1117{
1118 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1119 ctxt->modrm_seg = VCPU_SREG_SS;
1120}
1121
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001122static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001123 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001124{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001125 u8 sib;
Bandan Das02357bd2014-04-16 12:46:11 -04001126 int index_reg, base_reg, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001127 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001128 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001129
Bandan Das02357bd2014-04-16 12:46:11 -04001130 ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */
1131 index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */
1132 base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001133
Bandan Das02357bd2014-04-16 12:46:11 -04001134 ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001135 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
Bandan Das02357bd2014-04-16 12:46:11 -04001136 ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001137 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001138
Nadav Amit9b88ae92014-05-25 23:05:21 +03001139 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001140 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001141 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001142 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001143 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001144 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001145 op->type = OP_XMM;
1146 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001147 op->addr.xmm = ctxt->modrm_rm;
1148 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001149 return rc;
1150 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001151 if (ctxt->d & Mmx) {
1152 op->type = OP_MM;
1153 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001154 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001155 return rc;
1156 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001157 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001158 return rc;
1159 }
1160
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001161 op->type = OP_MEM;
1162
Avi Kivity9dac77f2011-06-01 15:34:25 +03001163 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001164 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1165 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1166 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1167 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001168
1169 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001170 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001171 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001172 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001173 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001174 break;
1175 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001176 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001177 break;
1178 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001179 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001180 break;
1181 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001182 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001183 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001184 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001185 break;
1186 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001187 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001188 break;
1189 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001190 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001191 break;
1192 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001193 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001194 break;
1195 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001196 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001197 break;
1198 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001199 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001200 break;
1201 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001202 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001203 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001204 break;
1205 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001206 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001207 break;
1208 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001209 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1210 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1211 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001212 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001213 } else {
1214 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001215 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001216 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001217 index_reg |= (sib >> 3) & 7;
1218 base_reg |= sib & 7;
1219 scale = sib >> 6;
1220
Avi Kivity9dac77f2011-06-01 15:34:25 +03001221 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001222 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001223 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001224 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001225 adjust_modrm_seg(ctxt, base_reg);
1226 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001227 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001228 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001229 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Nadav Amit5b38ab82014-11-02 11:54:41 +02001230 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity84411d82008-06-15 21:53:26 -07001231 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001232 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001233 } else {
1234 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001235 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001236 adjust_modrm_seg(ctxt, base_reg);
1237 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001238 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001239 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001240 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001241 break;
1242 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001243 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001244 break;
1245 }
1246 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001247 op->addr.mem.ea = modrm_ea;
Bandan Das41061cd2014-04-16 12:46:14 -04001248 if (ctxt->ad_bytes != 8)
1249 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
1250
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001251done:
1252 return rc;
1253}
1254
1255static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001256 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001257{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001258 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001259
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001260 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001261 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001262 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001263 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001264 break;
1265 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001266 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001267 break;
1268 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001269 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001270 break;
1271 }
1272done:
1273 return rc;
1274}
1275
Avi Kivity9dac77f2011-06-01 15:34:25 +03001276static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001277{
Sheng Yang7129eec2010-09-28 16:33:32 +08001278 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001279
Avi Kivity9dac77f2011-06-01 15:34:25 +03001280 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001281 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001282
Avi Kivity9dac77f2011-06-01 15:34:25 +03001283 if (ctxt->src.bytes == 2)
1284 sv = (s16)ctxt->src.val & (s16)mask;
1285 else if (ctxt->src.bytes == 4)
1286 sv = (s32)ctxt->src.val & (s32)mask;
Nadav Amit7dec5602014-06-15 16:12:57 +03001287 else
1288 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001289
Nadav Amit1c1c35a2014-11-19 17:43:09 +02001290 ctxt->dst.addr.mem.ea = address_mask(ctxt,
1291 ctxt->dst.addr.mem.ea + (sv >> 3));
Wei Yongjun35c843c2010-08-09 11:34:56 +08001292 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001293
1294 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001295 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001296}
1297
Gleb Natapov9de41572010-04-28 19:15:22 +03001298static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001299 unsigned long addr, void *dest, unsigned size)
1300{
1301 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001302 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001303
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001304 if (mc->pos < mc->end)
1305 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001306
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001307 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001308
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001309 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1310 &ctxt->exception);
1311 if (rc != X86EMUL_CONTINUE)
1312 return rc;
1313
1314 mc->end += size;
1315
1316read_cached:
1317 memcpy(dest, mc->data + mc->pos, size);
1318 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001319 return X86EMUL_CONTINUE;
1320}
1321
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001322static int segmented_read(struct x86_emulate_ctxt *ctxt,
1323 struct segmented_address addr,
1324 void *data,
1325 unsigned size)
1326{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001327 int rc;
1328 ulong linear;
1329
Avi Kivity83b87952011-04-03 11:31:19 +03001330 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001331 if (rc != X86EMUL_CONTINUE)
1332 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001333 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001334}
1335
1336static int segmented_write(struct x86_emulate_ctxt *ctxt,
1337 struct segmented_address addr,
1338 const void *data,
1339 unsigned size)
1340{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001341 int rc;
1342 ulong linear;
1343
Avi Kivity83b87952011-04-03 11:31:19 +03001344 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001345 if (rc != X86EMUL_CONTINUE)
1346 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001347 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1348 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001349}
1350
1351static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1352 struct segmented_address addr,
1353 const void *orig_data, const void *data,
1354 unsigned size)
1355{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001356 int rc;
1357 ulong linear;
1358
Avi Kivity83b87952011-04-03 11:31:19 +03001359 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001360 if (rc != X86EMUL_CONTINUE)
1361 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001362 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1363 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001364}
1365
Gleb Natapov7b262e92010-03-18 15:20:27 +02001366static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001367 unsigned int size, unsigned short port,
1368 void *dest)
1369{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001370 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001371
1372 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001373 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001374 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001375 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001376 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001377 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1378 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Mark Rustadb55a8142014-07-25 06:27:05 -07001379 n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001380 if (n == 0)
1381 n = 1;
1382 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001383 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001384 return 0;
1385 rc->end = n * size;
1386 }
1387
Nadav Amite6e39f02014-04-18 03:35:10 +03001388 if (ctxt->rep_prefix && (ctxt->d & String) &&
1389 !(ctxt->eflags & EFLG_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001390 ctxt->dst.data = rc->data + rc->pos;
1391 ctxt->dst.type = OP_MEM_STR;
1392 ctxt->dst.count = (rc->end - rc->pos) / size;
1393 rc->pos = rc->end;
1394 } else {
1395 memcpy(dest, rc->data + rc->pos, size);
1396 rc->pos += size;
1397 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001398 return 1;
1399}
1400
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001401static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1402 u16 index, struct desc_struct *desc)
1403{
1404 struct desc_ptr dt;
1405 ulong addr;
1406
1407 ctxt->ops->get_idt(ctxt, &dt);
1408
1409 if (dt.size < index * 8 + 7)
1410 return emulate_gp(ctxt, index << 3 | 0x2);
1411
1412 addr = dt.address + index * 8;
1413 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1414 &ctxt->exception);
1415}
1416
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001417static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001418 u16 selector, struct desc_ptr *dt)
1419{
Mathias Krause0225fb52012-08-30 01:30:16 +02001420 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001421 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001422
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001423 if (selector & 1 << 2) {
1424 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001425 u16 sel;
1426
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001427 memset (dt, 0, sizeof *dt);
Nadav Amit2eedcac2014-06-02 18:34:05 +03001428 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1429 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001430 return;
1431
1432 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001433 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001434 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001435 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001436}
1437
1438/* allowed just for 8 bytes segments */
1439static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001440 u16 selector, struct desc_struct *desc,
1441 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001442{
1443 struct desc_ptr dt;
1444 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001445 ulong addr;
1446
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001447 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001448
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001449 if (dt.size < index * 8 + 7)
1450 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001451
Avi Kivitye9194642012-06-13 16:29:39 +03001452 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001453 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1454 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001455}
1456
1457/* allowed just for 8 bytes segments */
1458static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001459 u16 selector, struct desc_struct *desc)
1460{
1461 struct desc_ptr dt;
1462 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001463 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001464
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001465 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001466
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001467 if (dt.size < index * 8 + 7)
1468 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001469
1470 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001471 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1472 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001473}
1474
Gleb Natapov5601d052011-03-07 14:55:06 +02001475/* Does not support long mode */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001476static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Nadav Amitd1442d82014-09-18 22:39:39 +03001477 u16 selector, int seg, u8 cpl,
1478 bool in_task_switch,
1479 struct desc_struct *desc)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001480{
Avi Kivity869be992012-06-13 16:30:53 +03001481 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001482 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001483 unsigned err_vec = GP_VECTOR;
1484 u32 err_code = 0;
1485 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001486 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001487 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001488 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001489 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001490
1491 memset(&seg_desc, 0, sizeof seg_desc);
1492
Kevin Wolff8da94e2013-04-11 14:06:03 +02001493 if (ctxt->mode == X86EMUL_MODE_REAL) {
1494 /* set real mode segment descriptor (keep limit etc. for
1495 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001496 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001497 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001498 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001499 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1500 /* VM86 needs a clean new segment descriptor */
1501 set_desc_base(&seg_desc, selector << 4);
1502 set_desc_limit(&seg_desc, 0xffff);
1503 seg_desc.type = 3;
1504 seg_desc.p = 1;
1505 seg_desc.s = 1;
1506 seg_desc.dpl = 3;
1507 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001508 }
1509
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001510 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001511
1512 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1513 if ((seg == VCPU_SREG_CS
1514 || (seg == VCPU_SREG_SS
1515 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1516 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001517 && null_selector)
1518 goto exception;
1519
1520 /* TR should be in GDT only */
1521 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1522 goto exception;
1523
1524 if (null_selector) /* for NULL selector skip all following checks */
1525 goto load;
1526
Avi Kivitye9194642012-06-13 16:29:39 +03001527 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001528 if (ret != X86EMUL_CONTINUE)
1529 return ret;
1530
1531 err_code = selector & 0xfffc;
Paolo Bonzini15fc0752014-08-18 13:17:00 +02001532 err_vec = in_task_switch ? TS_VECTOR : GP_VECTOR;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001533
Guo Chaofc058682012-06-28 15:19:51 +08001534 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001535 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1536 goto exception;
1537
1538 if (!seg_desc.p) {
1539 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1540 goto exception;
1541 }
1542
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001543 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001544
1545 switch (seg) {
1546 case VCPU_SREG_SS:
1547 /*
1548 * segment is not a writable data segment or segment
1549 * selector's RPL != CPL or segment selector's RPL != CPL
1550 */
1551 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1552 goto exception;
1553 break;
1554 case VCPU_SREG_CS:
1555 if (!(seg_desc.type & 8))
1556 goto exception;
1557
1558 if (seg_desc.type & 4) {
1559 /* conforming */
1560 if (dpl > cpl)
1561 goto exception;
1562 } else {
1563 /* nonconforming */
1564 if (rpl > cpl || dpl != cpl)
1565 goto exception;
1566 }
Nadav Amit040c8dc2014-09-18 22:39:43 +03001567 /* in long-mode d/b must be clear if l is set */
1568 if (seg_desc.d && seg_desc.l) {
1569 u64 efer = 0;
1570
1571 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1572 if (efer & EFER_LMA)
1573 goto exception;
1574 }
1575
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001576 /* CS(RPL) <- CPL */
1577 selector = (selector & 0xfffc) | cpl;
1578 break;
1579 case VCPU_SREG_TR:
1580 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1581 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001582 old_desc = seg_desc;
1583 seg_desc.type |= 2; /* busy */
1584 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1585 sizeof(seg_desc), &ctxt->exception);
1586 if (ret != X86EMUL_CONTINUE)
1587 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001588 break;
1589 case VCPU_SREG_LDTR:
1590 if (seg_desc.s || seg_desc.type != 2)
1591 goto exception;
1592 break;
1593 default: /* DS, ES, FS, or GS */
1594 /*
1595 * segment is not a data or readable code segment or
1596 * ((segment is a data or nonconforming code segment)
1597 * and (both RPL and CPL > DPL))
1598 */
1599 if ((seg_desc.type & 0xa) == 0x8 ||
1600 (((seg_desc.type & 0xc) != 0xc) &&
1601 (rpl > dpl && cpl > dpl)))
1602 goto exception;
1603 break;
1604 }
1605
1606 if (seg_desc.s) {
1607 /* mark segment as accessed */
1608 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001609 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001610 if (ret != X86EMUL_CONTINUE)
1611 return ret;
Nadav Amite37a75a2014-06-02 18:34:04 +03001612 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
1613 ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3,
1614 sizeof(base3), &ctxt->exception);
1615 if (ret != X86EMUL_CONTINUE)
1616 return ret;
Nadav Amit9a9abf62014-11-02 11:54:56 +02001617 if (is_noncanonical_address(get_desc_base(&seg_desc) |
1618 ((u64)base3 << 32)))
1619 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001620 }
1621load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001622 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Nadav Amitd1442d82014-09-18 22:39:39 +03001623 if (desc)
1624 *desc = seg_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001625 return X86EMUL_CONTINUE;
1626exception:
Paolo Bonzini592f0852014-08-20 10:05:08 +02001627 return emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001628}
1629
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001630static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1631 u16 selector, int seg)
1632{
1633 u8 cpl = ctxt->ops->cpl(ctxt);
Nadav Amitd1442d82014-09-18 22:39:39 +03001634 return __load_segment_descriptor(ctxt, selector, seg, cpl, false, NULL);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001635}
1636
Wei Yongjun31be40b2010-08-17 09:17:30 +08001637static void write_register_operand(struct operand *op)
1638{
1639 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1640 switch (op->bytes) {
1641 case 1:
1642 *(u8 *)op->addr.reg = (u8)op->val;
1643 break;
1644 case 2:
1645 *(u16 *)op->addr.reg = (u16)op->val;
1646 break;
1647 case 4:
1648 *op->addr.reg = (u32)op->val;
1649 break; /* 64b: zero-extend */
1650 case 8:
1651 *op->addr.reg = op->val;
1652 break;
1653 }
1654}
1655
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001656static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001657{
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001658 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001659 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001660 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001661 break;
1662 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001663 if (ctxt->lock_prefix)
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001664 return segmented_cmpxchg(ctxt,
1665 op->addr.mem,
1666 &op->orig_val,
1667 &op->val,
1668 op->bytes);
1669 else
1670 return segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001671 op->addr.mem,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001672 &op->val,
1673 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001674 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001675 case OP_MEM_STR:
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001676 return segmented_write(ctxt,
1677 op->addr.mem,
1678 op->data,
1679 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001680 break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001681 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001682 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001683 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001684 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001685 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001686 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001687 case OP_NONE:
1688 /* no writeback */
1689 break;
1690 default:
1691 break;
1692 }
1693 return X86EMUL_CONTINUE;
1694}
1695
Avi Kivity51ddff52012-06-12 20:19:40 +03001696static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001697{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001698 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001699
Avi Kivity5ad105e2012-08-19 14:34:31 +03001700 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001701 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001702 addr.seg = VCPU_SREG_SS;
1703
Avi Kivity51ddff52012-06-12 20:19:40 +03001704 return segmented_write(ctxt, addr, data, bytes);
1705}
1706
1707static int em_push(struct x86_emulate_ctxt *ctxt)
1708{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001709 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001710 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001711 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001712}
1713
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001714static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001715 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001716{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001717 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001718 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001719
Avi Kivitydd856ef2012-08-27 23:46:17 +03001720 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001721 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001722 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001723 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001724 return rc;
1725
Avi Kivity5ad105e2012-08-19 14:34:31 +03001726 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001727 return rc;
1728}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001729
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001730static int em_pop(struct x86_emulate_ctxt *ctxt)
1731{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001732 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001733}
1734
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001735static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001736 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001737{
1738 int rc;
1739 unsigned long val, change_mask;
1740 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001741 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001742
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001743 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001744 if (rc != X86EMUL_CONTINUE)
1745 return rc;
1746
1747 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
Nadav Amit163b1352014-07-21 14:37:28 +03001748 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_AC | EFLG_ID;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001749
1750 switch(ctxt->mode) {
1751 case X86EMUL_MODE_PROT64:
1752 case X86EMUL_MODE_PROT32:
1753 case X86EMUL_MODE_PROT16:
1754 if (cpl == 0)
1755 change_mask |= EFLG_IOPL;
1756 if (cpl <= iopl)
1757 change_mask |= EFLG_IF;
1758 break;
1759 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001760 if (iopl < 3)
1761 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001762 change_mask |= EFLG_IF;
1763 break;
1764 default: /* real mode */
1765 change_mask |= (EFLG_IOPL | EFLG_IF);
1766 break;
1767 }
1768
1769 *(unsigned long *)dest =
1770 (ctxt->eflags & ~change_mask) | (val & change_mask);
1771
1772 return rc;
1773}
1774
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001775static int em_popf(struct x86_emulate_ctxt *ctxt)
1776{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001777 ctxt->dst.type = OP_REG;
1778 ctxt->dst.addr.reg = &ctxt->eflags;
1779 ctxt->dst.bytes = ctxt->op_bytes;
1780 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001781}
1782
Avi Kivity612e89f2012-06-12 20:03:23 +03001783static int em_enter(struct x86_emulate_ctxt *ctxt)
1784{
1785 int rc;
1786 unsigned frame_size = ctxt->src.val;
1787 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001788 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001789
1790 if (nesting_level)
1791 return X86EMUL_UNHANDLEABLE;
1792
Avi Kivitydd856ef2012-08-27 23:46:17 +03001793 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1794 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001795 if (rc != X86EMUL_CONTINUE)
1796 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001797 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001798 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001799 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1800 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001801 stack_mask(ctxt));
1802 return X86EMUL_CONTINUE;
1803}
1804
Avi Kivityf47cfa32012-06-07 17:49:24 +03001805static int em_leave(struct x86_emulate_ctxt *ctxt)
1806{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001807 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001808 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001809 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001810}
1811
Avi Kivity1cd196e2011-09-13 10:45:51 +03001812static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001813{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001814 int seg = ctxt->src2.val;
1815
Avi Kivity9dac77f2011-06-01 15:34:25 +03001816 ctxt->src.val = get_segment_selector(ctxt, seg);
Nadav Amit0fcc2072014-11-02 11:54:51 +02001817 if (ctxt->op_bytes == 4) {
1818 rsp_increment(ctxt, -2);
1819 ctxt->op_bytes = 2;
1820 }
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001821
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001822 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001823}
1824
Avi Kivity1cd196e2011-09-13 10:45:51 +03001825static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001826{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001827 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001828 unsigned long selector;
1829 int rc;
1830
Nadav Amit3313bc42014-12-25 02:52:17 +02001831 rc = emulate_pop(ctxt, &selector, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001832 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001833 return rc;
1834
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001835 if (ctxt->modrm_reg == VCPU_SREG_SS)
1836 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Nadav Amit3313bc42014-12-25 02:52:17 +02001837 if (ctxt->op_bytes > 2)
1838 rsp_increment(ctxt, ctxt->op_bytes - 2);
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001839
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001840 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001841 return rc;
1842}
1843
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001844static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001845{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001846 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001847 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001848 int reg = VCPU_REGS_RAX;
1849
1850 while (reg <= VCPU_REGS_RDI) {
1851 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001852 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001853
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001854 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001855 if (rc != X86EMUL_CONTINUE)
1856 return rc;
1857
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001858 ++reg;
1859 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001860
Wei Yongjunc37eda12010-06-15 09:03:33 +08001861 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001862}
1863
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001864static int em_pushf(struct x86_emulate_ctxt *ctxt)
1865{
Nadav Amitbc397a62014-12-10 11:19:03 +02001866 ctxt->src.val = (unsigned long)ctxt->eflags & ~EFLG_VM;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001867 return em_push(ctxt);
1868}
1869
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001870static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001871{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001872 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001873 int reg = VCPU_REGS_RDI;
1874
1875 while (reg >= VCPU_REGS_RAX) {
1876 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001877 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001878 --reg;
1879 }
1880
Avi Kivitydd856ef2012-08-27 23:46:17 +03001881 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001882 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001883 break;
1884 --reg;
1885 }
1886 return rc;
1887}
1888
Avi Kivitydd856ef2012-08-27 23:46:17 +03001889static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001890{
Mathias Krause0225fb52012-08-30 01:30:16 +02001891 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001892 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001893 struct desc_ptr dt;
1894 gva_t cs_addr;
1895 gva_t eip_addr;
1896 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001897
1898 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001899 ctxt->src.val = ctxt->eflags;
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;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001903
1904 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1905
Avi Kivity9dac77f2011-06-01 15:34:25 +03001906 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001907 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001908 if (rc != X86EMUL_CONTINUE)
1909 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001910
Avi Kivity9dac77f2011-06-01 15:34:25 +03001911 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001912 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001913 if (rc != X86EMUL_CONTINUE)
1914 return rc;
1915
Avi Kivity4bff1e862011-04-20 13:37:53 +03001916 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001917
1918 eip_addr = dt.address + (irq << 2);
1919 cs_addr = dt.address + (irq << 2) + 2;
1920
Avi Kivity0f65dd72011-04-20 13:37:53 +03001921 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001922 if (rc != X86EMUL_CONTINUE)
1923 return rc;
1924
Avi Kivity0f65dd72011-04-20 13:37:53 +03001925 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001926 if (rc != X86EMUL_CONTINUE)
1927 return rc;
1928
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001929 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001930 if (rc != X86EMUL_CONTINUE)
1931 return rc;
1932
Avi Kivity9dac77f2011-06-01 15:34:25 +03001933 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001934
1935 return rc;
1936}
1937
Avi Kivitydd856ef2012-08-27 23:46:17 +03001938int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1939{
1940 int rc;
1941
1942 invalidate_registers(ctxt);
1943 rc = __emulate_int_real(ctxt, irq);
1944 if (rc == X86EMUL_CONTINUE)
1945 writeback_registers(ctxt);
1946 return rc;
1947}
1948
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001949static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001950{
1951 switch(ctxt->mode) {
1952 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001953 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001954 case X86EMUL_MODE_VM86:
1955 case X86EMUL_MODE_PROT16:
1956 case X86EMUL_MODE_PROT32:
1957 case X86EMUL_MODE_PROT64:
1958 default:
1959 /* Protected mode interrupts unimplemented yet */
1960 return X86EMUL_UNHANDLEABLE;
1961 }
1962}
1963
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001964static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001965{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001966 int rc = X86EMUL_CONTINUE;
1967 unsigned long temp_eip = 0;
1968 unsigned long temp_eflags = 0;
1969 unsigned long cs = 0;
1970 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1971 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1972 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1973 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1974
1975 /* TODO: Add stack limit check */
1976
Avi Kivity9dac77f2011-06-01 15:34:25 +03001977 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001978
1979 if (rc != X86EMUL_CONTINUE)
1980 return rc;
1981
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001982 if (temp_eip & ~0xffff)
1983 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001984
Avi Kivity9dac77f2011-06-01 15:34:25 +03001985 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001986
1987 if (rc != X86EMUL_CONTINUE)
1988 return rc;
1989
Avi Kivity9dac77f2011-06-01 15:34:25 +03001990 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001991
1992 if (rc != X86EMUL_CONTINUE)
1993 return rc;
1994
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001995 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001996
1997 if (rc != X86EMUL_CONTINUE)
1998 return rc;
1999
Avi Kivity9dac77f2011-06-01 15:34:25 +03002000 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002001
2002
Avi Kivity9dac77f2011-06-01 15:34:25 +03002003 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002004 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03002005 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002006 ctxt->eflags &= ~0xffff;
2007 ctxt->eflags |= temp_eflags;
2008 }
2009
2010 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
2011 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
2012
2013 return rc;
2014}
2015
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002016static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002017{
2018 switch(ctxt->mode) {
2019 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002020 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002021 case X86EMUL_MODE_VM86:
2022 case X86EMUL_MODE_PROT16:
2023 case X86EMUL_MODE_PROT32:
2024 case X86EMUL_MODE_PROT64:
2025 default:
2026 /* iret from protected mode unimplemented yet */
2027 return X86EMUL_UNHANDLEABLE;
2028 }
2029}
2030
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002031static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2032{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002033 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002034 unsigned short sel, old_sel;
2035 struct desc_struct old_desc, new_desc;
2036 const struct x86_emulate_ops *ops = ctxt->ops;
2037 u8 cpl = ctxt->ops->cpl(ctxt);
2038
2039 /* Assignment of RIP may only fail in 64-bit mode */
2040 if (ctxt->mode == X86EMUL_MODE_PROT64)
2041 ops->get_segment(ctxt, &old_sel, &old_desc, NULL,
2042 VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002043
Avi Kivity9dac77f2011-06-01 15:34:25 +03002044 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002045
Nadav Amitd1442d82014-09-18 22:39:39 +03002046 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl, false,
2047 &new_desc);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002048 if (rc != X86EMUL_CONTINUE)
2049 return rc;
2050
Nadav Amitd50eaa12014-11-19 17:43:11 +02002051 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03002052 if (rc != X86EMUL_CONTINUE) {
Nadav Amit7e46ddd2014-10-28 00:03:43 +02002053 WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64);
Nadav Amitd1442d82014-09-18 22:39:39 +03002054 /* assigning eip failed; restore the old cs */
2055 ops->set_segment(ctxt, old_sel, &old_desc, 0, VCPU_SREG_CS);
2056 return rc;
2057 }
2058 return rc;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002059}
2060
Nadav Amitf7784042014-09-18 22:39:41 +03002061static int em_jmp_abs(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002062{
Nadav Amitf7784042014-09-18 22:39:41 +03002063 return assign_eip_near(ctxt, ctxt->src.val);
2064}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002065
Nadav Amitf7784042014-09-18 22:39:41 +03002066static int em_call_near_abs(struct x86_emulate_ctxt *ctxt)
2067{
2068 int rc;
2069 long int old_eip;
2070
2071 old_eip = ctxt->_eip;
2072 rc = assign_eip_near(ctxt, ctxt->src.val);
2073 if (rc != X86EMUL_CONTINUE)
2074 return rc;
2075 ctxt->src.val = old_eip;
2076 rc = em_push(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002077 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002078}
2079
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002080static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002081{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002082 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002083
Nadav Amitaaa05f22014-06-02 18:34:10 +03002084 if (ctxt->dst.bytes == 16)
2085 return X86EMUL_UNHANDLEABLE;
2086
Avi Kivitydd856ef2012-08-27 23:46:17 +03002087 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2088 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2089 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2090 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002091 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002092 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002093 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2094 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002095
Laurent Vivier05f086f2007-09-24 11:10:55 +02002096 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002097 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002098 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002099}
2100
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002101static int em_ret(struct x86_emulate_ctxt *ctxt)
2102{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002103 int rc;
2104 unsigned long eip;
2105
2106 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
2107 if (rc != X86EMUL_CONTINUE)
2108 return rc;
2109
2110 return assign_eip_near(ctxt, eip);
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002111}
2112
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002113static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002114{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002115 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002116 unsigned long eip, cs;
2117 u16 old_cs;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002118 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amitd1442d82014-09-18 22:39:39 +03002119 struct desc_struct old_desc, new_desc;
2120 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002121
Nadav Amitd1442d82014-09-18 22:39:39 +03002122 if (ctxt->mode == X86EMUL_MODE_PROT64)
2123 ops->get_segment(ctxt, &old_cs, &old_desc, NULL,
2124 VCPU_SREG_CS);
2125
2126 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002127 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002128 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002129 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002130 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002131 return rc;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002132 /* Outer-privilege level return is not implemented */
2133 if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2134 return X86EMUL_UNHANDLEABLE;
Nadav Amitab646f52014-12-11 12:27:14 +01002135 rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, cpl, false,
Nadav Amitd1442d82014-09-18 22:39:39 +03002136 &new_desc);
2137 if (rc != X86EMUL_CONTINUE)
2138 return rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +02002139 rc = assign_eip_far(ctxt, eip, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03002140 if (rc != X86EMUL_CONTINUE) {
Nadav Amit7e46ddd2014-10-28 00:03:43 +02002141 WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64);
Nadav Amitd1442d82014-09-18 22:39:39 +03002142 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
2143 }
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002144 return rc;
2145}
2146
Bruce Rogers32611072013-09-09 09:40:20 -06002147static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2148{
2149 int rc;
2150
2151 rc = em_ret_far(ctxt);
2152 if (rc != X86EMUL_CONTINUE)
2153 return rc;
2154 rsp_increment(ctxt, ctxt->src.val);
2155 return X86EMUL_CONTINUE;
2156}
2157
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002158static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2159{
2160 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002161 ctxt->dst.orig_val = ctxt->dst.val;
2162 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002163 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002164 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002165 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002166
2167 if (ctxt->eflags & EFLG_ZF) {
2168 /* Success: write back to memory. */
2169 ctxt->dst.val = ctxt->src.orig_val;
2170 } else {
2171 /* Failure: write the value we saw to EAX. */
2172 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002173 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Nadav Amit37c564f2014-06-02 18:34:07 +03002174 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002175 }
2176 return X86EMUL_CONTINUE;
2177}
2178
Avi Kivityd4b43252011-09-13 10:45:50 +03002179static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002180{
Avi Kivityd4b43252011-09-13 10:45:50 +03002181 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002182 unsigned short sel;
2183 int rc;
2184
Avi Kivity9dac77f2011-06-01 15:34:25 +03002185 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002186
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002187 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002188 if (rc != X86EMUL_CONTINUE)
2189 return rc;
2190
Avi Kivity9dac77f2011-06-01 15:34:25 +03002191 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002192 return rc;
2193}
2194
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002195static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002196setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002197 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002198{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002199 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002200 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002201 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002202 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002203 cs->type = 0x0b; /* Read, Execute, Accessed */
2204 cs->s = 1;
2205 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002206 cs->p = 1;
2207 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002208 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002209
Gleb Natapov79168fd2010-04-28 19:15:30 +03002210 set_desc_base(ss, 0); /* flat segment */
2211 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002212 ss->g = 1; /* 4kb granularity */
2213 ss->s = 1;
2214 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002215 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002216 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002217 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002218 ss->l = 0;
2219 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002220}
2221
Avi Kivity1a18a692012-02-01 12:23:21 +02002222static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2223{
2224 u32 eax, ebx, ecx, edx;
2225
2226 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002227 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2228 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002229 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2230 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2231}
2232
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002233static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2234{
Mathias Krause0225fb52012-08-30 01:30:16 +02002235 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002236 u32 eax, ebx, ecx, edx;
2237
2238 /*
2239 * syscall should always be enabled in longmode - so only become
2240 * vendor specific (cpuid) if other modes are active...
2241 */
2242 if (ctxt->mode == X86EMUL_MODE_PROT64)
2243 return true;
2244
2245 eax = 0x00000000;
2246 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002247 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2248 /*
2249 * Intel ("GenuineIntel")
2250 * remark: Intel CPUs only support "syscall" in 64bit
2251 * longmode. Also an 64bit guest with a
2252 * 32bit compat-app running will #UD !! While this
2253 * behaviour can be fixed (by emulating) into AMD
2254 * response - CPUs of AMD can't behave like Intel.
2255 */
2256 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2257 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2258 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2259 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002260
Avi Kivity0017f932012-06-07 14:10:16 +03002261 /* AMD ("AuthenticAMD") */
2262 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2263 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2264 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2265 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002266
Avi Kivity0017f932012-06-07 14:10:16 +03002267 /* AMD ("AMDisbetter!") */
2268 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2269 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2270 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2271 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002272
2273 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2274 return false;
2275}
2276
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002277static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002278{
Mathias Krause0225fb52012-08-30 01:30:16 +02002279 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002280 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002281 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002282 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002283 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002284
2285 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002286 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002287 ctxt->mode == X86EMUL_MODE_VM86)
2288 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002289
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002290 if (!(em_syscall_is_enabled(ctxt)))
2291 return emulate_ud(ctxt);
2292
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002293 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002294 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002295
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002296 if (!(efer & EFER_SCE))
2297 return emulate_ud(ctxt);
2298
Avi Kivity717746e2011-04-20 13:37:53 +03002299 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002300 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002301 cs_sel = (u16)(msr_data & 0xfffc);
2302 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002303
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002304 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002305 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002306 cs.l = 1;
2307 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002308 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2309 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002310
Avi Kivitydd856ef2012-08-27 23:46:17 +03002311 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002312 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002313#ifdef CONFIG_X86_64
Nadav Amit6c6cb692014-07-21 14:37:30 +03002314 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002315
Avi Kivity717746e2011-04-20 13:37:53 +03002316 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002317 ctxt->mode == X86EMUL_MODE_PROT64 ?
2318 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002319 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002320
Avi Kivity717746e2011-04-20 13:37:53 +03002321 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Nadav Amit6c6cb692014-07-21 14:37:30 +03002322 ctxt->eflags &= ~msr_data;
Nadav Amit807c1422014-11-02 11:54:49 +02002323 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002324#endif
2325 } else {
2326 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002327 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002328 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002329
Nadav Amit6c6cb692014-07-21 14:37:30 +03002330 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002331 }
2332
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002333 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002334}
2335
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002336static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002337{
Mathias Krause0225fb52012-08-30 01:30:16 +02002338 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002339 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002340 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002341 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002342 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002343
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002344 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002345 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002346 if (ctxt->mode == X86EMUL_MODE_REAL)
2347 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002348
Avi Kivity1a18a692012-02-01 12:23:21 +02002349 /*
2350 * Not recognized on AMD in compat mode (but is recognized in legacy
2351 * mode).
2352 */
2353 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2354 && !vendor_intel(ctxt))
2355 return emulate_ud(ctxt);
2356
Nadav Amitb2c9d432014-11-02 11:55:01 +02002357 /* sysenter/sysexit have not been tested in 64bit mode. */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002358 if (ctxt->mode == X86EMUL_MODE_PROT64)
Nadav Amitb2c9d432014-11-02 11:55:01 +02002359 return X86EMUL_UNHANDLEABLE;
Andre Przywara8c604352009-06-18 12:56:01 +02002360
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002361 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002362
Avi Kivity717746e2011-04-20 13:37:53 +03002363 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002364 switch (ctxt->mode) {
2365 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002366 if ((msr_data & 0xfffc) == 0x0)
2367 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002368 break;
2369 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002370 if (msr_data == 0x0)
2371 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002372 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002373 default:
2374 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002375 }
2376
Nadav Amit6c6cb692014-07-21 14:37:30 +03002377 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002378 cs_sel = (u16)msr_data;
2379 cs_sel &= ~SELECTOR_RPL_MASK;
2380 ss_sel = cs_sel + 8;
2381 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002382 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002383 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002384 cs.l = 1;
2385 }
2386
Avi Kivity1aa36612011-04-27 13:20:30 +03002387 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2388 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002389
Avi Kivity717746e2011-04-20 13:37:53 +03002390 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002391 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002392
Avi Kivity717746e2011-04-20 13:37:53 +03002393 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002394 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002395
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002396 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002397}
2398
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002399static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002400{
Mathias Krause0225fb52012-08-30 01:30:16 +02002401 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002402 struct desc_struct cs, ss;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002403 u64 msr_data, rcx, rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002404 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002405 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002406
Gleb Natapova0044752010-02-10 14:21:31 +02002407 /* inject #GP if in real mode or Virtual 8086 mode */
2408 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002409 ctxt->mode == X86EMUL_MODE_VM86)
2410 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002411
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002412 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002413
Avi Kivity9dac77f2011-06-01 15:34:25 +03002414 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002415 usermode = X86EMUL_MODE_PROT64;
2416 else
2417 usermode = X86EMUL_MODE_PROT32;
2418
Nadav Amit234f3ce2014-09-18 22:39:38 +03002419 rcx = reg_read(ctxt, VCPU_REGS_RCX);
2420 rdx = reg_read(ctxt, VCPU_REGS_RDX);
2421
Andre Przywara4668f052009-06-18 12:56:02 +02002422 cs.dpl = 3;
2423 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002424 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002425 switch (usermode) {
2426 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002427 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002428 if ((msr_data & 0xfffc) == 0x0)
2429 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002430 ss_sel = (u16)(msr_data + 24);
Nadav Amitbf0b6822014-09-18 22:39:45 +03002431 rcx = (u32)rcx;
2432 rdx = (u32)rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002433 break;
2434 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002435 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002436 if (msr_data == 0x0)
2437 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002438 ss_sel = cs_sel + 8;
2439 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002440 cs.l = 1;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002441 if (is_noncanonical_address(rcx) ||
2442 is_noncanonical_address(rdx))
2443 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002444 break;
2445 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002446 cs_sel |= SELECTOR_RPL_MASK;
2447 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002448
Avi Kivity1aa36612011-04-27 13:20:30 +03002449 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2450 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002451
Nadav Amit234f3ce2014-09-18 22:39:38 +03002452 ctxt->_eip = rdx;
2453 *reg_write(ctxt, VCPU_REGS_RSP) = rcx;
Andre Przywara4668f052009-06-18 12:56:02 +02002454
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002455 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002456}
2457
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002458static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002459{
2460 int iopl;
2461 if (ctxt->mode == X86EMUL_MODE_REAL)
2462 return false;
2463 if (ctxt->mode == X86EMUL_MODE_VM86)
2464 return true;
2465 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002466 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002467}
2468
2469static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002470 u16 port, u16 len)
2471{
Mathias Krause0225fb52012-08-30 01:30:16 +02002472 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002473 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002474 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002475 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002476 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002477 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002478 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002479
Avi Kivity1aa36612011-04-27 13:20:30 +03002480 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002481 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002482 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002483 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002484 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002485 base = get_desc_base(&tr_seg);
2486#ifdef CONFIG_X86_64
2487 base |= ((u64)base3) << 32;
2488#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002489 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002490 if (r != X86EMUL_CONTINUE)
2491 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002492 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002493 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002494 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002495 if (r != X86EMUL_CONTINUE)
2496 return false;
2497 if ((perm >> bit_idx) & mask)
2498 return false;
2499 return true;
2500}
2501
2502static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002503 u16 port, u16 len)
2504{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002505 if (ctxt->perm_ok)
2506 return true;
2507
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002508 if (emulator_bad_iopl(ctxt))
2509 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002510 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002511
2512 ctxt->perm_ok = true;
2513
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002514 return true;
2515}
2516
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002517static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002518 struct tss_segment_16 *tss)
2519{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002520 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002521 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002522 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2523 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2524 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2525 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2526 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2527 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2528 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2529 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002530
Avi Kivity1aa36612011-04-27 13:20:30 +03002531 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2532 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2533 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2534 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2535 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002536}
2537
2538static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002539 struct tss_segment_16 *tss)
2540{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002541 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002542 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002543
Avi Kivity9dac77f2011-06-01 15:34:25 +03002544 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002545 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002546 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2547 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2548 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2549 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2550 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2551 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2552 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2553 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002554
2555 /*
2556 * SDM says that segment selectors are loaded before segment
2557 * descriptors
2558 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002559 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2560 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2561 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2562 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2563 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002564
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002565 cpl = tss->cs & 3;
2566
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002567 /*
Guo Chaofc058682012-06-28 15:19:51 +08002568 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002569 * it is handled in a context of new task
2570 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002571 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl,
2572 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002573 if (ret != X86EMUL_CONTINUE)
2574 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002575 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
2576 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002577 if (ret != X86EMUL_CONTINUE)
2578 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002579 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
2580 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002581 if (ret != X86EMUL_CONTINUE)
2582 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002583 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
2584 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002585 if (ret != X86EMUL_CONTINUE)
2586 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002587 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
2588 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002589 if (ret != X86EMUL_CONTINUE)
2590 return ret;
2591
2592 return X86EMUL_CONTINUE;
2593}
2594
2595static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002596 u16 tss_selector, u16 old_tss_sel,
2597 ulong old_tss_base, struct desc_struct *new_desc)
2598{
Mathias Krause0225fb52012-08-30 01:30:16 +02002599 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002600 struct tss_segment_16 tss_seg;
2601 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002602 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002603
Avi Kivity0f65dd72011-04-20 13:37:53 +03002604 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002605 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002606 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002607 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002608
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002609 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002610
Avi Kivity0f65dd72011-04-20 13:37:53 +03002611 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002612 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002613 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002614 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002615
Avi Kivity0f65dd72011-04-20 13:37:53 +03002616 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002617 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002618 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002619 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002620
2621 if (old_tss_sel != 0xffff) {
2622 tss_seg.prev_task_link = old_tss_sel;
2623
Avi Kivity0f65dd72011-04-20 13:37:53 +03002624 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002625 &tss_seg.prev_task_link,
2626 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002627 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002628 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002629 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002630 }
2631
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002632 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002633}
2634
2635static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002636 struct tss_segment_32 *tss)
2637{
Nadav Amit5c7411e2014-04-07 18:37:47 +03002638 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002639 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002640 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002641 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2642 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2643 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2644 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2645 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2646 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2647 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2648 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002649
Avi Kivity1aa36612011-04-27 13:20:30 +03002650 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2651 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2652 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2653 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2654 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2655 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002656}
2657
2658static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002659 struct tss_segment_32 *tss)
2660{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002661 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002662 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002663
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002664 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002665 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002666 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002667 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002668
2669 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002670 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2671 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2672 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2673 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2674 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2675 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2676 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2677 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002678
2679 /*
2680 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002681 * descriptors. This is important because CPL checks will
2682 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002683 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002684 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2685 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2686 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2687 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2688 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2689 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2690 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002691
2692 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002693 * If we're switching between Protected Mode and VM86, we need to make
2694 * sure to update the mode before loading the segment descriptors so
2695 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01002696 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002697 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002698 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002699 cpl = 3;
2700 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002701 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002702 cpl = tss->cs & 3;
2703 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01002704
2705 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002706 * Now load segment descriptors. If fault happenes at this stage
2707 * it is handled in a context of new task
2708 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002709 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR,
2710 cpl, true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002711 if (ret != X86EMUL_CONTINUE)
2712 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002713 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
2714 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002715 if (ret != X86EMUL_CONTINUE)
2716 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002717 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
2718 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002719 if (ret != X86EMUL_CONTINUE)
2720 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002721 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
2722 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002723 if (ret != X86EMUL_CONTINUE)
2724 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002725 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
2726 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002727 if (ret != X86EMUL_CONTINUE)
2728 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002729 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl,
2730 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002731 if (ret != X86EMUL_CONTINUE)
2732 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002733 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl,
2734 true, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002735 if (ret != X86EMUL_CONTINUE)
2736 return ret;
2737
2738 return X86EMUL_CONTINUE;
2739}
2740
2741static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002742 u16 tss_selector, u16 old_tss_sel,
2743 ulong old_tss_base, struct desc_struct *new_desc)
2744{
Mathias Krause0225fb52012-08-30 01:30:16 +02002745 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002746 struct tss_segment_32 tss_seg;
2747 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002748 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03002749 u32 eip_offset = offsetof(struct tss_segment_32, eip);
2750 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002751
Avi Kivity0f65dd72011-04-20 13:37:53 +03002752 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002753 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002754 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002755 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002756
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002757 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002758
Nadav Amit5c7411e2014-04-07 18:37:47 +03002759 /* Only GP registers and segment selectors are saved */
2760 ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
2761 ldt_sel_offset - eip_offset, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002762 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002763 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002764
Avi Kivity0f65dd72011-04-20 13:37:53 +03002765 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002766 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002767 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002768 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002769
2770 if (old_tss_sel != 0xffff) {
2771 tss_seg.prev_task_link = old_tss_sel;
2772
Avi Kivity0f65dd72011-04-20 13:37:53 +03002773 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002774 &tss_seg.prev_task_link,
2775 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002776 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002777 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002778 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002779 }
2780
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002781 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002782}
2783
2784static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002785 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002786 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002787{
Mathias Krause0225fb52012-08-30 01:30:16 +02002788 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002789 struct desc_struct curr_tss_desc, next_tss_desc;
2790 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002791 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002792 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002793 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002794 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002795 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002796
2797 /* FIXME: old_tss_base == ~0 ? */
2798
Avi Kivitye9194642012-06-13 16:29:39 +03002799 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002800 if (ret != X86EMUL_CONTINUE)
2801 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002802 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002803 if (ret != X86EMUL_CONTINUE)
2804 return ret;
2805
2806 /* FIXME: check that next_tss_desc is tss */
2807
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002808 /*
2809 * Check privileges. The three cases are task switch caused by...
2810 *
2811 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2812 * 2. Exception/IRQ/iret: No check is performed
Nadav Amit2c2ca2d2014-11-02 11:54:57 +02002813 * 3. jmp/call to TSS/task-gate: No check is performed since the
2814 * hardware checks it before exiting.
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002815 */
2816 if (reason == TASK_SWITCH_GATE) {
2817 if (idt_index != -1) {
2818 /* Software interrupts */
2819 struct desc_struct task_gate_desc;
2820 int dpl;
2821
2822 ret = read_interrupt_descriptor(ctxt, idt_index,
2823 &task_gate_desc);
2824 if (ret != X86EMUL_CONTINUE)
2825 return ret;
2826
2827 dpl = task_gate_desc.dpl;
2828 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2829 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2830 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002831 }
2832
Gleb Natapovceffb452010-03-18 15:20:19 +02002833 desc_limit = desc_limit_scaled(&next_tss_desc);
2834 if (!next_tss_desc.p ||
2835 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2836 desc_limit < 0x2b)) {
Paolo Bonzini592f0852014-08-20 10:05:08 +02002837 return emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002838 }
2839
2840 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2841 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002842 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002843 }
2844
2845 if (reason == TASK_SWITCH_IRET)
2846 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2847
2848 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002849 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002850 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2851 old_tss_sel = 0xffff;
2852
2853 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002854 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002855 old_tss_base, &next_tss_desc);
2856 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002857 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002858 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002859 if (ret != X86EMUL_CONTINUE)
2860 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002861
2862 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2863 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2864
2865 if (reason != TASK_SWITCH_IRET) {
2866 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002867 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002868 }
2869
Avi Kivity717746e2011-04-20 13:37:53 +03002870 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002871 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002872
Jan Kiszkae269fb22010-04-14 15:51:09 +02002873 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002874 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2875 ctxt->lock_prefix = 0;
2876 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002877 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002878 }
2879
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002880 return ret;
2881}
2882
2883int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002884 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002885 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002886{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002887 int rc;
2888
Avi Kivitydd856ef2012-08-27 23:46:17 +03002889 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002890 ctxt->_eip = ctxt->eip;
2891 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002892
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002893 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002894 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002895
Avi Kivitydd856ef2012-08-27 23:46:17 +03002896 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002897 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002898 writeback_registers(ctxt);
2899 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002900
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002901 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002902}
2903
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002904static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2905 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002906{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002907 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002908
Paolo Bonzini01485a22014-11-19 18:25:08 +01002909 register_address_increment(ctxt, reg, df * op->bytes);
2910 op->addr.mem.ea = register_address(ctxt, reg);
Gleb Natapova682e352010-03-18 15:20:21 +02002911}
2912
Avi Kivity7af04fc2010-08-18 14:16:35 +03002913static int em_das(struct x86_emulate_ctxt *ctxt)
2914{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002915 u8 al, old_al;
2916 bool af, cf, old_cf;
2917
2918 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002919 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002920
2921 old_al = al;
2922 old_cf = cf;
2923 cf = false;
2924 af = ctxt->eflags & X86_EFLAGS_AF;
2925 if ((al & 0x0f) > 9 || af) {
2926 al -= 6;
2927 cf = old_cf | (al >= 250);
2928 af = true;
2929 } else {
2930 af = false;
2931 }
2932 if (old_al > 0x99 || old_cf) {
2933 al -= 0x60;
2934 cf = true;
2935 }
2936
Avi Kivity9dac77f2011-06-01 15:34:25 +03002937 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002938 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002939 ctxt->src.type = OP_IMM;
2940 ctxt->src.val = 0;
2941 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02002942 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03002943 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2944 if (cf)
2945 ctxt->eflags |= X86_EFLAGS_CF;
2946 if (af)
2947 ctxt->eflags |= X86_EFLAGS_AF;
2948 return X86EMUL_CONTINUE;
2949}
2950
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02002951static int em_aam(struct x86_emulate_ctxt *ctxt)
2952{
2953 u8 al, ah;
2954
2955 if (ctxt->src.val == 0)
2956 return emulate_de(ctxt);
2957
2958 al = ctxt->dst.val & 0xff;
2959 ah = al / ctxt->src.val;
2960 al %= ctxt->src.val;
2961
2962 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
2963
2964 /* Set PF, ZF, SF */
2965 ctxt->src.type = OP_IMM;
2966 ctxt->src.val = 0;
2967 ctxt->src.bytes = 1;
2968 fastop(ctxt, em_or);
2969
2970 return X86EMUL_CONTINUE;
2971}
2972
Gleb Natapov7f662272012-12-10 11:42:30 +02002973static int em_aad(struct x86_emulate_ctxt *ctxt)
2974{
2975 u8 al = ctxt->dst.val & 0xff;
2976 u8 ah = (ctxt->dst.val >> 8) & 0xff;
2977
2978 al = (al + (ah * ctxt->src.val)) & 0xff;
2979
2980 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
2981
Gleb Natapovf583c292013-02-13 17:50:39 +02002982 /* Set PF, ZF, SF */
2983 ctxt->src.type = OP_IMM;
2984 ctxt->src.val = 0;
2985 ctxt->src.bytes = 1;
2986 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02002987
2988 return X86EMUL_CONTINUE;
2989}
2990
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002991static int em_call(struct x86_emulate_ctxt *ctxt)
2992{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002993 int rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002994 long rel = ctxt->src.val;
2995
2996 ctxt->src.val = (unsigned long)ctxt->_eip;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002997 rc = jmp_rel(ctxt, rel);
2998 if (rc != X86EMUL_CONTINUE)
2999 return rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003000 return em_push(ctxt);
3001}
3002
Avi Kivity0ef753b2010-08-18 14:51:45 +03003003static int em_call_far(struct x86_emulate_ctxt *ctxt)
3004{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003005 u16 sel, old_cs;
3006 ulong old_eip;
3007 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03003008 struct desc_struct old_desc, new_desc;
3009 const struct x86_emulate_ops *ops = ctxt->ops;
3010 int cpl = ctxt->ops->cpl(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003011
Avi Kivity9dac77f2011-06-01 15:34:25 +03003012 old_eip = ctxt->_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003013 ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003014
Avi Kivity9dac77f2011-06-01 15:34:25 +03003015 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Nadav Amitd1442d82014-09-18 22:39:39 +03003016 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl, false,
3017 &new_desc);
3018 if (rc != X86EMUL_CONTINUE)
Avi Kivity0ef753b2010-08-18 14:51:45 +03003019 return X86EMUL_CONTINUE;
3020
Nadav Amitd50eaa12014-11-19 17:43:11 +02003021 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003022 if (rc != X86EMUL_CONTINUE)
3023 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003024
Avi Kivity9dac77f2011-06-01 15:34:25 +03003025 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003026 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003027 if (rc != X86EMUL_CONTINUE)
Nadav Amitd1442d82014-09-18 22:39:39 +03003028 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003029
Avi Kivity9dac77f2011-06-01 15:34:25 +03003030 ctxt->src.val = old_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003031 rc = em_push(ctxt);
3032 /* If we failed, we tainted the memory, but the very least we should
3033 restore cs */
3034 if (rc != X86EMUL_CONTINUE)
3035 goto fail;
3036 return rc;
3037fail:
3038 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
3039 return rc;
3040
Avi Kivity0ef753b2010-08-18 14:51:45 +03003041}
3042
Avi Kivity40ece7c2010-08-18 15:12:09 +03003043static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3044{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003045 int rc;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003046 unsigned long eip;
Avi Kivity40ece7c2010-08-18 15:12:09 +03003047
Nadav Amit234f3ce2014-09-18 22:39:38 +03003048 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
3049 if (rc != X86EMUL_CONTINUE)
3050 return rc;
3051 rc = assign_eip_near(ctxt, eip);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003052 if (rc != X86EMUL_CONTINUE)
3053 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003054 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003055 return X86EMUL_CONTINUE;
3056}
3057
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003058static int em_xchg(struct x86_emulate_ctxt *ctxt)
3059{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003060 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003061 ctxt->src.val = ctxt->dst.val;
3062 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003063
3064 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003065 ctxt->dst.val = ctxt->src.orig_val;
3066 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003067 return X86EMUL_CONTINUE;
3068}
3069
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003070static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3071{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003072 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003073 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003074}
3075
Avi Kivity61429142010-08-19 15:13:00 +03003076static int em_cwd(struct x86_emulate_ctxt *ctxt)
3077{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003078 ctxt->dst.type = OP_REG;
3079 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003080 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003081 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003082
3083 return X86EMUL_CONTINUE;
3084}
3085
Avi Kivity48bb5d32010-08-18 18:54:34 +03003086static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3087{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003088 u64 tsc = 0;
3089
Avi Kivity717746e2011-04-20 13:37:53 +03003090 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003091 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3092 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003093 return X86EMUL_CONTINUE;
3094}
3095
Avi Kivity222d21a2011-11-10 14:57:30 +02003096static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3097{
3098 u64 pmc;
3099
Avi Kivitydd856ef2012-08-27 23:46:17 +03003100 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003101 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003102 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3103 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003104 return X86EMUL_CONTINUE;
3105}
3106
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003107static int em_mov(struct x86_emulate_ctxt *ctxt)
3108{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01003109 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003110 return X86EMUL_CONTINUE;
3111}
3112
Borislav Petkov84cffe42013-10-29 12:54:56 +01003113#define FFL(x) bit(X86_FEATURE_##x)
3114
3115static int em_movbe(struct x86_emulate_ctxt *ctxt)
3116{
3117 u32 ebx, ecx, edx, eax = 1;
3118 u16 tmp;
3119
3120 /*
3121 * Check MOVBE is set in the guest-visible CPUID leaf.
3122 */
3123 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
3124 if (!(ecx & FFL(MOVBE)))
3125 return emulate_ud(ctxt);
3126
3127 switch (ctxt->op_bytes) {
3128 case 2:
3129 /*
3130 * From MOVBE definition: "...When the operand size is 16 bits,
3131 * the upper word of the destination register remains unchanged
3132 * ..."
3133 *
3134 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3135 * rules so we have to do the operation almost per hand.
3136 */
3137 tmp = (u16)ctxt->src.val;
3138 ctxt->dst.val &= ~0xffffUL;
3139 ctxt->dst.val |= (unsigned long)swab16(tmp);
3140 break;
3141 case 4:
3142 ctxt->dst.val = swab32((u32)ctxt->src.val);
3143 break;
3144 case 8:
3145 ctxt->dst.val = swab64(ctxt->src.val);
3146 break;
3147 default:
Paolo Bonzini592f0852014-08-20 10:05:08 +02003148 BUG();
Borislav Petkov84cffe42013-10-29 12:54:56 +01003149 }
3150 return X86EMUL_CONTINUE;
3151}
3152
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003153static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3154{
3155 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3156 return emulate_gp(ctxt, 0);
3157
3158 /* Disable writeback. */
3159 ctxt->dst.type = OP_NONE;
3160 return X86EMUL_CONTINUE;
3161}
3162
3163static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3164{
3165 unsigned long val;
3166
3167 if (ctxt->mode == X86EMUL_MODE_PROT64)
3168 val = ctxt->src.val & ~0ULL;
3169 else
3170 val = ctxt->src.val & ~0U;
3171
3172 /* #UD condition is already handled. */
3173 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3174 return emulate_gp(ctxt, 0);
3175
3176 /* Disable writeback. */
3177 ctxt->dst.type = OP_NONE;
3178 return X86EMUL_CONTINUE;
3179}
3180
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003181static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3182{
3183 u64 msr_data;
3184
Avi Kivitydd856ef2012-08-27 23:46:17 +03003185 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3186 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3187 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003188 return emulate_gp(ctxt, 0);
3189
3190 return X86EMUL_CONTINUE;
3191}
3192
3193static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3194{
3195 u64 msr_data;
3196
Avi Kivitydd856ef2012-08-27 23:46:17 +03003197 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003198 return emulate_gp(ctxt, 0);
3199
Avi Kivitydd856ef2012-08-27 23:46:17 +03003200 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3201 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003202 return X86EMUL_CONTINUE;
3203}
3204
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003205static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3206{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003207 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003208 return emulate_ud(ctxt);
3209
Avi Kivity9dac77f2011-06-01 15:34:25 +03003210 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Nadav Amitb5bbf102014-11-02 11:54:46 +02003211 if (ctxt->dst.bytes == 4 && ctxt->dst.type == OP_MEM)
3212 ctxt->dst.bytes = 2;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003213 return X86EMUL_CONTINUE;
3214}
3215
3216static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3217{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003218 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003219
Avi Kivity9dac77f2011-06-01 15:34:25 +03003220 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003221 return emulate_ud(ctxt);
3222
Avi Kivity9dac77f2011-06-01 15:34:25 +03003223 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003224 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3225
3226 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003227 ctxt->dst.type = OP_NONE;
3228 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003229}
3230
Avi Kivitya14e5792012-06-13 12:28:33 +03003231static int em_lldt(struct x86_emulate_ctxt *ctxt)
3232{
3233 u16 sel = ctxt->src.val;
3234
3235 /* Disable writeback. */
3236 ctxt->dst.type = OP_NONE;
3237 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3238}
3239
Avi Kivity80890002012-06-13 16:33:29 +03003240static int em_ltr(struct x86_emulate_ctxt *ctxt)
3241{
3242 u16 sel = ctxt->src.val;
3243
3244 /* Disable writeback. */
3245 ctxt->dst.type = OP_NONE;
3246 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3247}
3248
Avi Kivity38503912011-03-31 18:48:09 +02003249static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3250{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003251 int rc;
3252 ulong linear;
3253
Avi Kivity9dac77f2011-06-01 15:34:25 +03003254 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003255 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003256 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003257 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003258 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003259 return X86EMUL_CONTINUE;
3260}
3261
Avi Kivity2d04a052011-04-20 15:32:49 +03003262static int em_clts(struct x86_emulate_ctxt *ctxt)
3263{
3264 ulong cr0;
3265
3266 cr0 = ctxt->ops->get_cr(ctxt, 0);
3267 cr0 &= ~X86_CR0_TS;
3268 ctxt->ops->set_cr(ctxt, 0, cr0);
3269 return X86EMUL_CONTINUE;
3270}
3271
Avi Kivity26d05cc2011-04-21 12:07:59 +03003272static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3273{
Nadav Amit0f54a322014-08-29 11:26:55 +03003274 int rc = ctxt->ops->fix_hypercall(ctxt);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003275
Avi Kivity26d05cc2011-04-21 12:07:59 +03003276 if (rc != X86EMUL_CONTINUE)
3277 return rc;
3278
3279 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003280 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003281 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003282 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003283 return X86EMUL_CONTINUE;
3284}
3285
Avi Kivity96051572012-06-10 17:21:18 +03003286static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3287 void (*get)(struct x86_emulate_ctxt *ctxt,
3288 struct desc_ptr *ptr))
3289{
3290 struct desc_ptr desc_ptr;
3291
3292 if (ctxt->mode == X86EMUL_MODE_PROT64)
3293 ctxt->op_bytes = 8;
3294 get(ctxt, &desc_ptr);
3295 if (ctxt->op_bytes == 2) {
3296 ctxt->op_bytes = 4;
3297 desc_ptr.address &= 0x00ffffff;
3298 }
3299 /* Disable writeback. */
3300 ctxt->dst.type = OP_NONE;
3301 return segmented_write(ctxt, ctxt->dst.addr.mem,
3302 &desc_ptr, 2 + ctxt->op_bytes);
3303}
3304
3305static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3306{
3307 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3308}
3309
3310static int em_sidt(struct x86_emulate_ctxt *ctxt)
3311{
3312 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3313}
3314
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003315static int em_lgdt_lidt(struct x86_emulate_ctxt *ctxt, bool lgdt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003316{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003317 struct desc_ptr desc_ptr;
3318 int rc;
3319
Avi Kivity510425f2012-06-07 17:04:36 +03003320 if (ctxt->mode == X86EMUL_MODE_PROT64)
3321 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003322 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003323 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003324 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003325 if (rc != X86EMUL_CONTINUE)
3326 return rc;
Nadav Amit9a9abf62014-11-02 11:54:56 +02003327 if (ctxt->mode == X86EMUL_MODE_PROT64 &&
3328 is_noncanonical_address(desc_ptr.address))
3329 return emulate_gp(ctxt, 0);
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003330 if (lgdt)
3331 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3332 else
3333 ctxt->ops->set_idt(ctxt, &desc_ptr);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003334 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003335 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003336 return X86EMUL_CONTINUE;
3337}
3338
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003339static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3340{
3341 return em_lgdt_lidt(ctxt, true);
3342}
3343
Avi Kivity5ef39c72011-04-21 12:21:50 +03003344static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003345{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003346 int rc;
3347
Avi Kivity5ef39c72011-04-21 12:21:50 +03003348 rc = ctxt->ops->fix_hypercall(ctxt);
3349
Avi Kivity26d05cc2011-04-21 12:07:59 +03003350 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003351 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003352 return rc;
3353}
3354
3355static int em_lidt(struct x86_emulate_ctxt *ctxt)
3356{
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003357 return em_lgdt_lidt(ctxt, false);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003358}
3359
3360static int em_smsw(struct x86_emulate_ctxt *ctxt)
3361{
Nadav Amit32e94d02014-06-02 18:34:11 +03003362 if (ctxt->dst.type == OP_MEM)
3363 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003364 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003365 return X86EMUL_CONTINUE;
3366}
3367
3368static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3369{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003370 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003371 | (ctxt->src.val & 0x0f));
3372 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003373 return X86EMUL_CONTINUE;
3374}
3375
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003376static int em_loop(struct x86_emulate_ctxt *ctxt)
3377{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003378 int rc = X86EMUL_CONTINUE;
3379
Paolo Bonzini01485a22014-11-19 18:25:08 +01003380 register_address_increment(ctxt, VCPU_REGS_RCX, -1);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003381 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003382 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
Nadav Amit234f3ce2014-09-18 22:39:38 +03003383 rc = jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003384
Nadav Amit234f3ce2014-09-18 22:39:38 +03003385 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003386}
3387
3388static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3389{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003390 int rc = X86EMUL_CONTINUE;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003391
Nadav Amit234f3ce2014-09-18 22:39:38 +03003392 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
3393 rc = jmp_rel(ctxt, ctxt->src.val);
3394
3395 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003396}
3397
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003398static int em_in(struct x86_emulate_ctxt *ctxt)
3399{
3400 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3401 &ctxt->dst.val))
3402 return X86EMUL_IO_NEEDED;
3403
3404 return X86EMUL_CONTINUE;
3405}
3406
3407static int em_out(struct x86_emulate_ctxt *ctxt)
3408{
3409 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3410 &ctxt->src.val, 1);
3411 /* Disable writeback. */
3412 ctxt->dst.type = OP_NONE;
3413 return X86EMUL_CONTINUE;
3414}
3415
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003416static int em_cli(struct x86_emulate_ctxt *ctxt)
3417{
3418 if (emulator_bad_iopl(ctxt))
3419 return emulate_gp(ctxt, 0);
3420
3421 ctxt->eflags &= ~X86_EFLAGS_IF;
3422 return X86EMUL_CONTINUE;
3423}
3424
3425static int em_sti(struct x86_emulate_ctxt *ctxt)
3426{
3427 if (emulator_bad_iopl(ctxt))
3428 return emulate_gp(ctxt, 0);
3429
3430 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3431 ctxt->eflags |= X86_EFLAGS_IF;
3432 return X86EMUL_CONTINUE;
3433}
3434
Avi Kivity6d6eede2012-06-07 14:11:36 +03003435static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3436{
3437 u32 eax, ebx, ecx, edx;
3438
Avi Kivitydd856ef2012-08-27 23:46:17 +03003439 eax = reg_read(ctxt, VCPU_REGS_RAX);
3440 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003441 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003442 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3443 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3444 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3445 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003446 return X86EMUL_CONTINUE;
3447}
3448
Paolo Bonzini98f73632013-10-31 11:19:42 +01003449static int em_sahf(struct x86_emulate_ctxt *ctxt)
3450{
3451 u32 flags;
3452
3453 flags = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF;
3454 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3455
3456 ctxt->eflags &= ~0xffUL;
3457 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3458 return X86EMUL_CONTINUE;
3459}
3460
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003461static int em_lahf(struct x86_emulate_ctxt *ctxt)
3462{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003463 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3464 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003465 return X86EMUL_CONTINUE;
3466}
3467
Avi Kivity92998362012-06-13 12:25:06 +03003468static int em_bswap(struct x86_emulate_ctxt *ctxt)
3469{
3470 switch (ctxt->op_bytes) {
3471#ifdef CONFIG_X86_64
3472 case 8:
3473 asm("bswap %0" : "+r"(ctxt->dst.val));
3474 break;
3475#endif
3476 default:
3477 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3478 break;
3479 }
3480 return X86EMUL_CONTINUE;
3481}
3482
Nadav Amit13e457e2014-10-13 13:04:13 +03003483static int em_clflush(struct x86_emulate_ctxt *ctxt)
3484{
3485 /* emulating clflush regardless of cpuid */
3486 return X86EMUL_CONTINUE;
3487}
3488
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003489static bool valid_cr(int nr)
3490{
3491 switch (nr) {
3492 case 0:
3493 case 2 ... 4:
3494 case 8:
3495 return true;
3496 default:
3497 return false;
3498 }
3499}
3500
3501static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3502{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003503 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003504 return emulate_ud(ctxt);
3505
3506 return X86EMUL_CONTINUE;
3507}
3508
3509static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3510{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003511 u64 new_val = ctxt->src.val64;
3512 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003513 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003514
3515 static u64 cr_reserved_bits[] = {
3516 0xffffffff00000000ULL,
3517 0, 0, 0, /* CR3 checked later */
3518 CR4_RESERVED_BITS,
3519 0, 0, 0,
3520 CR8_RESERVED_BITS,
3521 };
3522
3523 if (!valid_cr(cr))
3524 return emulate_ud(ctxt);
3525
3526 if (new_val & cr_reserved_bits[cr])
3527 return emulate_gp(ctxt, 0);
3528
3529 switch (cr) {
3530 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003531 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003532 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3533 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3534 return emulate_gp(ctxt, 0);
3535
Avi Kivity717746e2011-04-20 13:37:53 +03003536 cr4 = ctxt->ops->get_cr(ctxt, 4);
3537 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003538
3539 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3540 !(cr4 & X86_CR4_PAE))
3541 return emulate_gp(ctxt, 0);
3542
3543 break;
3544 }
3545 case 3: {
3546 u64 rsvd = 0;
3547
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003548 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3549 if (efer & EFER_LMA)
Nadav Amit9d88fca2014-11-02 11:54:52 +02003550 rsvd = CR3_L_MODE_RESERVED_BITS & ~CR3_PCID_INVD;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003551
3552 if (new_val & rsvd)
3553 return emulate_gp(ctxt, 0);
3554
3555 break;
3556 }
3557 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003558 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003559
3560 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3561 return emulate_gp(ctxt, 0);
3562
3563 break;
3564 }
3565 }
3566
3567 return X86EMUL_CONTINUE;
3568}
3569
Joerg Roedel3b88e412011-04-04 12:39:29 +02003570static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3571{
3572 unsigned long dr7;
3573
Avi Kivity717746e2011-04-20 13:37:53 +03003574 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003575
3576 /* Check if DR7.Global_Enable is set */
3577 return dr7 & (1 << 13);
3578}
3579
3580static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3581{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003582 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003583 u64 cr4;
3584
3585 if (dr > 7)
3586 return emulate_ud(ctxt);
3587
Avi Kivity717746e2011-04-20 13:37:53 +03003588 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003589 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3590 return emulate_ud(ctxt);
3591
Nadav Amit6d2a0522014-11-02 11:54:43 +02003592 if (check_dr7_gd(ctxt)) {
3593 ulong dr6;
3594
3595 ctxt->ops->get_dr(ctxt, 6, &dr6);
3596 dr6 &= ~15;
3597 dr6 |= DR6_BD | DR6_RTM;
3598 ctxt->ops->set_dr(ctxt, 6, dr6);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003599 return emulate_db(ctxt);
Nadav Amit6d2a0522014-11-02 11:54:43 +02003600 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02003601
3602 return X86EMUL_CONTINUE;
3603}
3604
3605static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3606{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003607 u64 new_val = ctxt->src.val64;
3608 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003609
3610 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3611 return emulate_gp(ctxt, 0);
3612
3613 return check_dr_read(ctxt);
3614}
3615
Joerg Roedel01de8b02011-04-04 12:39:31 +02003616static int check_svme(struct x86_emulate_ctxt *ctxt)
3617{
3618 u64 efer;
3619
Avi Kivity717746e2011-04-20 13:37:53 +03003620 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003621
3622 if (!(efer & EFER_SVME))
3623 return emulate_ud(ctxt);
3624
3625 return X86EMUL_CONTINUE;
3626}
3627
3628static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3629{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003630 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003631
3632 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003633 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003634 return emulate_gp(ctxt, 0);
3635
3636 return check_svme(ctxt);
3637}
3638
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003639static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3640{
Avi Kivity717746e2011-04-20 13:37:53 +03003641 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003642
Avi Kivity717746e2011-04-20 13:37:53 +03003643 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003644 return emulate_ud(ctxt);
3645
3646 return X86EMUL_CONTINUE;
3647}
3648
Joerg Roedel80612522011-04-04 12:39:33 +02003649static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3650{
Avi Kivity717746e2011-04-20 13:37:53 +03003651 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003652 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003653
Avi Kivity717746e2011-04-20 13:37:53 +03003654 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03003655 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02003656 return emulate_gp(ctxt, 0);
3657
3658 return X86EMUL_CONTINUE;
3659}
3660
Joerg Roedelf6511932011-04-04 12:39:35 +02003661static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3662{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003663 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3664 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003665 return emulate_gp(ctxt, 0);
3666
3667 return X86EMUL_CONTINUE;
3668}
3669
3670static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3671{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003672 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3673 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003674 return emulate_gp(ctxt, 0);
3675
3676 return X86EMUL_CONTINUE;
3677}
3678
Avi Kivity73fba5f2010-07-29 15:11:53 +03003679#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003680#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
3681#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
3682 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003683#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003684#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003685#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3686#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Nadav Amit39f062f2014-11-26 15:47:18 +02003687#define ID(_f, _i) { .flags = ((_f) | InstrDual | ModRM), .u.idual = (_i) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003688#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003689#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003690#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003691#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003692 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003693#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003694 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
3695 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003696#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003697
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003698#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003699#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003700#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003701#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003702#define I2bvIP(_f, _e, _i, _p) \
3703 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003704
Avi Kivityfb864fb2013-01-04 16:18:54 +02003705#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3706 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3707 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003708
Nadav Amit0f54a322014-08-29 11:26:55 +03003709static const struct opcode group7_rm0[] = {
3710 N,
3711 I(SrcNone | Priv | EmulateOnUD, em_vmcall),
3712 N, N, N, N, N, N,
3713};
3714
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003715static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003716 DI(SrcNone | Priv, monitor),
3717 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003718 N, N, N, N, N, N,
3719};
3720
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003721static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003722 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003723 II(SrcNone | Prot | EmulateOnUD, em_vmmcall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003724 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3725 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3726 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3727 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3728 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3729 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003730};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003731
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003732static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003733 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003734 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003735 N, N, N, N, N, N,
3736};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003737
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003738static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003739 F(Lock, em_add),
3740 F(Lock | PageTable, em_or),
3741 F(Lock, em_adc),
3742 F(Lock, em_sbb),
3743 F(Lock | PageTable, em_and),
3744 F(Lock, em_sub),
3745 F(Lock, em_xor),
3746 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003747};
3748
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003749static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003750 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003751};
3752
Avi Kivity007a3b52013-01-19 19:51:51 +02003753static const struct opcode group2[] = {
3754 F(DstMem | ModRM, em_rol),
3755 F(DstMem | ModRM, em_ror),
3756 F(DstMem | ModRM, em_rcl),
3757 F(DstMem | ModRM, em_rcr),
3758 F(DstMem | ModRM, em_shl),
3759 F(DstMem | ModRM, em_shr),
3760 F(DstMem | ModRM, em_shl),
3761 F(DstMem | ModRM, em_sar),
3762};
3763
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003764static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003765 F(DstMem | SrcImm | NoWrite, em_test),
3766 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003767 F(DstMem | SrcNone | Lock, em_not),
3768 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02003769 F(DstXacc | Src2Mem, em_mul_ex),
3770 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02003771 F(DstXacc | Src2Mem, em_div_ex),
3772 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003773};
3774
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003775static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003776 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3777 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003778 N, N, N, N, N, N,
3779};
3780
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003781static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003782 F(DstMem | SrcNone | Lock, em_inc),
3783 F(DstMem | SrcNone | Lock, em_dec),
Nadav Amit58b70752014-10-24 11:35:09 +03003784 I(SrcMem | NearBranch, em_call_near_abs),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003785 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
Nadav Amit58b70752014-10-24 11:35:09 +03003786 I(SrcMem | NearBranch, em_jmp_abs),
Nadav Amitf7784042014-09-18 22:39:41 +03003787 I(SrcMemFAddr | ImplicitOps, em_jmp_far),
3788 I(SrcMem | Stack, em_push), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003789};
3790
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003791static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003792 DI(Prot, sldt),
3793 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003794 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003795 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003796 N, N, N, N,
3797};
3798
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003799static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03003800 II(Mov | DstMem, em_sgdt, sgdt),
3801 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003802 II(SrcMem | Priv, em_lgdt, lgdt),
3803 II(SrcMem | Priv, em_lidt, lidt),
3804 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3805 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3806 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003807}, {
Nadav Amit0f54a322014-08-29 11:26:55 +03003808 EXT(0, group7_rm0),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003809 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003810 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003811 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3812 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3813 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003814} };
3815
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003816static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003817 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003818 F(DstMem | SrcImmByte | NoWrite, em_bt),
3819 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3820 F(DstMem | SrcImmByte | Lock, em_btr),
3821 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003822};
3823
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003824static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003825 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003826}, {
3827 N, N, N, N, N, N, N, N,
3828} };
3829
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003830static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003831 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003832 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003833};
3834
Nadav Amit13e457e2014-10-13 13:04:13 +03003835static const struct gprefix pfx_0f_ae_7 = {
Nadav Amit3f6f1482014-10-13 13:04:14 +03003836 I(SrcMem | ByteOp, em_clflush), N, N, N,
Nadav Amit13e457e2014-10-13 13:04:13 +03003837};
3838
3839static const struct group_dual group15 = { {
3840 N, N, N, N, N, N, N, GP(0, &pfx_0f_ae_7),
3841}, {
3842 N, N, N, N, N, N, N, N,
3843} };
3844
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003845static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003846 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003847};
3848
Nadav Amit39f062f2014-11-26 15:47:18 +02003849static const struct instr_dual instr_dual_0f_2b = {
3850 I(0, em_mov), N
3851};
3852
Paolo Bonzinid5b77062014-07-14 12:54:48 +02003853static const struct gprefix pfx_0f_2b = {
Nadav Amit39f062f2014-11-26 15:47:18 +02003854 ID(0, &instr_dual_0f_2b), ID(0, &instr_dual_0f_2b), N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003855};
3856
Igor Mammedov27ce8252014-03-15 21:01:59 +01003857static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01003858 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003859};
3860
Alex Williamson0a370272014-07-11 11:56:31 -06003861static const struct gprefix pfx_0f_e7 = {
3862 N, I(Sse, em_mov), N, N,
3863};
3864
Gleb Natapov045a2822012-12-20 16:57:43 +02003865static const struct escape escape_d9 = { {
3866 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3867}, {
3868 /* 0xC0 - 0xC7 */
3869 N, N, N, N, N, N, N, N,
3870 /* 0xC8 - 0xCF */
3871 N, N, N, N, N, N, N, N,
3872 /* 0xD0 - 0xC7 */
3873 N, N, N, N, N, N, N, N,
3874 /* 0xD8 - 0xDF */
3875 N, N, N, N, N, N, N, N,
3876 /* 0xE0 - 0xE7 */
3877 N, N, N, N, N, N, N, N,
3878 /* 0xE8 - 0xEF */
3879 N, N, N, N, N, N, N, N,
3880 /* 0xF0 - 0xF7 */
3881 N, N, N, N, N, N, N, N,
3882 /* 0xF8 - 0xFF */
3883 N, N, N, N, N, N, N, N,
3884} };
3885
3886static const struct escape escape_db = { {
3887 N, N, N, N, N, N, N, N,
3888}, {
3889 /* 0xC0 - 0xC7 */
3890 N, N, N, N, N, N, N, N,
3891 /* 0xC8 - 0xCF */
3892 N, N, N, N, N, N, N, N,
3893 /* 0xD0 - 0xC7 */
3894 N, N, N, N, N, N, N, N,
3895 /* 0xD8 - 0xDF */
3896 N, N, N, N, N, N, N, N,
3897 /* 0xE0 - 0xE7 */
3898 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3899 /* 0xE8 - 0xEF */
3900 N, N, N, N, N, N, N, N,
3901 /* 0xF0 - 0xF7 */
3902 N, N, N, N, N, N, N, N,
3903 /* 0xF8 - 0xFF */
3904 N, N, N, N, N, N, N, N,
3905} };
3906
3907static const struct escape escape_dd = { {
3908 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3909}, {
3910 /* 0xC0 - 0xC7 */
3911 N, N, N, N, N, N, N, N,
3912 /* 0xC8 - 0xCF */
3913 N, N, N, N, N, N, N, N,
3914 /* 0xD0 - 0xC7 */
3915 N, N, N, N, N, N, N, N,
3916 /* 0xD8 - 0xDF */
3917 N, N, N, N, N, N, N, N,
3918 /* 0xE0 - 0xE7 */
3919 N, N, N, N, N, N, N, N,
3920 /* 0xE8 - 0xEF */
3921 N, N, N, N, N, N, N, N,
3922 /* 0xF0 - 0xF7 */
3923 N, N, N, N, N, N, N, N,
3924 /* 0xF8 - 0xFF */
3925 N, N, N, N, N, N, N, N,
3926} };
3927
Nadav Amit39f062f2014-11-26 15:47:18 +02003928static const struct instr_dual instr_dual_0f_c3 = {
3929 I(DstMem | SrcReg | ModRM | No16 | Mov, em_mov), N
3930};
3931
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003932static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003933 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003934 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003935 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3936 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003937 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003938 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003939 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3940 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003941 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003942 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003943 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3944 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003945 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003946 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003947 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3948 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003949 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003950 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003951 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003952 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003953 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003954 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003955 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003956 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003957 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02003958 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003959 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003960 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003961 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003962 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003963 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003964 I(ImplicitOps | Stack | No64, em_pusha),
3965 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003966 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3967 N, N, N, N,
3968 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003969 I(SrcImm | Mov | Stack, em_push),
3970 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003971 I(SrcImmByte | Mov | Stack, em_push),
3972 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003973 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003974 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003975 /* 0x70 - 0x7F */
Nadav Amit58b70752014-10-24 11:35:09 +03003976 X16(D(SrcImmByte | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003977 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003978 G(ByteOp | DstMem | SrcImm, group1),
3979 G(DstMem | SrcImm, group1),
3980 G(ByteOp | DstMem | SrcImm | No64, group1),
3981 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003982 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003983 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003984 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003985 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003986 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003987 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003988 D(ModRM | SrcMem | NoAccess | DstReg),
3989 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3990 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003991 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003992 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003993 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003994 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003995 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003996 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01003997 II(ImplicitOps | Stack, em_popf, popf),
3998 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003999 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004000 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004001 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004002 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004003 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004004 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004005 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004006 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
4007 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004008 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004009 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004010 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004011 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02004012 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004013 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004014 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Nadav Amit58b70752014-10-24 11:35:09 +03004015 I(ImplicitOps | NearBranch | SrcImmU16, em_ret_near_imm),
4016 I(ImplicitOps | NearBranch, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03004017 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
4018 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004019 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004020 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03004021 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Bruce Rogers32611072013-09-09 09:40:20 -06004022 I(ImplicitOps | Stack | SrcImmU16, em_ret_far_imm),
4023 I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02004024 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004025 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004026 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004027 G(Src2One | ByteOp, group2), G(Src2One, group2),
4028 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02004029 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02004030 I(DstAcc | SrcImmUByte | No64, em_aad),
4031 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004032 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004033 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02004034 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004035 /* 0xE0 - 0xE7 */
Nadav Amit58b70752014-10-24 11:35:09 +03004036 X3(I(SrcImmByte | NearBranch, em_loop)),
4037 I(SrcImmByte | NearBranch, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004038 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
4039 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004040 /* 0xE8 - 0xEF */
Nadav Amit58b70752014-10-24 11:35:09 +03004041 I(SrcImm | NearBranch, em_call), D(SrcImm | ImplicitOps | NearBranch),
4042 I(SrcImmFAddr | No64, em_jmp_far),
4043 D(SrcImmByte | ImplicitOps | NearBranch),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004044 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
4045 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004046 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004047 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004048 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4049 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004050 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09004051 D(ImplicitOps), D(ImplicitOps),
4052 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004053 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4054};
4055
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004056static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004057 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004058 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02004059 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004060 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004061 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004062 N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004063 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004064 N, N, N, N, N, N, N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004065 D(ImplicitOps | ModRM | SrcMem | NoAccess),
4066 N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004067 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03004068 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
4069 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
4070 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
4071 check_cr_write),
4072 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
4073 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004074 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004075 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
4076 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004077 N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b),
Avi Kivity3e114eb2012-04-09 18:40:01 +03004078 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004079 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004080 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004081 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004082 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004083 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02004084 I(ImplicitOps | EmulateOnUD, em_sysenter),
4085 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004086 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004087 N, N, N, N, N, N, N, N,
4088 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03004089 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004090 /* 0x50 - 0x5F */
4091 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4092 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004093 N, N, N, N,
4094 N, N, N, N,
4095 N, N, N, N,
4096 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004097 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004098 N, N, N, N,
4099 N, N, N, N,
4100 N, N, N, N,
4101 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004102 /* 0x80 - 0x8F */
Nadav Amit58b70752014-10-24 11:35:09 +03004103 X16(D(SrcImm | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004104 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004105 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004106 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004107 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004108 II(ImplicitOps, em_cpuid, cpuid),
4109 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004110 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4111 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004112 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004113 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004114 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004115 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004116 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4117 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Nadav Amit13e457e2014-10-13 13:04:13 +03004118 GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004119 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09004120 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004121 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004122 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004123 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4124 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004125 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004126 /* 0xB8 - 0xBF */
4127 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004128 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004129 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
4130 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004131 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004132 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02004133 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Nadav Amit39f062f2014-11-26 15:47:18 +02004134 N, ID(0, &instr_dual_0f_c3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004135 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004136 /* 0xC8 - 0xCF */
4137 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004138 /* 0xD0 - 0xDF */
4139 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4140 /* 0xE0 - 0xEF */
Alex Williamson0a370272014-07-11 11:56:31 -06004141 N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
4142 N, N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004143 /* 0xF0 - 0xFF */
4144 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4145};
4146
Nadav Amit39f062f2014-11-26 15:47:18 +02004147static const struct instr_dual instr_dual_0f_38_f0 = {
4148 I(DstReg | SrcMem | Mov, em_movbe), N
4149};
4150
4151static const struct instr_dual instr_dual_0f_38_f1 = {
4152 I(DstMem | SrcReg | Mov, em_movbe), N
4153};
4154
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004155static const struct gprefix three_byte_0f_38_f0 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004156 ID(0, &instr_dual_0f_38_f0), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004157};
4158
4159static const struct gprefix three_byte_0f_38_f1 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004160 ID(0, &instr_dual_0f_38_f1), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004161};
4162
4163/*
4164 * Insns below are selected by the prefix which indexed by the third opcode
4165 * byte.
4166 */
4167static const struct opcode opcode_map_0f_38[256] = {
4168 /* 0x00 - 0x7f */
4169 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004170 /* 0x80 - 0xef */
4171 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4172 /* 0xf0 - 0xf1 */
Nadav Amit53bb4f72014-12-07 11:49:42 +02004173 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0),
4174 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004175 /* 0xf2 - 0xff */
4176 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004177};
4178
Avi Kivity73fba5f2010-07-29 15:11:53 +03004179#undef D
4180#undef N
4181#undef G
4182#undef GD
4183#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004184#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004185#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004186
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004187#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004188#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004189#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004190#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004191#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004192
Avi Kivity9dac77f2011-06-01 15:34:25 +03004193static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004194{
4195 unsigned size;
4196
Avi Kivity9dac77f2011-06-01 15:34:25 +03004197 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004198 if (size == 8)
4199 size = 4;
4200 return size;
4201}
4202
4203static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4204 unsigned size, bool sign_extension)
4205{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004206 int rc = X86EMUL_CONTINUE;
4207
4208 op->type = OP_IMM;
4209 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004210 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004211 /* NB. Immediates are sign-extended as necessary. */
4212 switch (op->bytes) {
4213 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004214 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004215 break;
4216 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004217 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004218 break;
4219 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004220 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004221 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004222 case 8:
4223 op->val = insn_fetch(s64, ctxt);
4224 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004225 }
4226 if (!sign_extension) {
4227 switch (op->bytes) {
4228 case 1:
4229 op->val &= 0xff;
4230 break;
4231 case 2:
4232 op->val &= 0xffff;
4233 break;
4234 case 4:
4235 op->val &= 0xffffffff;
4236 break;
4237 }
4238 }
4239done:
4240 return rc;
4241}
4242
Avi Kivitya99455492011-09-13 10:45:41 +03004243static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4244 unsigned d)
4245{
4246 int rc = X86EMUL_CONTINUE;
4247
4248 switch (d) {
4249 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004250 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03004251 break;
4252 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004253 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03004254 break;
4255 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004256 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004257 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03004258 *op = ctxt->memop;
4259 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004260 if (ctxt->d & BitOp)
Avi Kivitya99455492011-09-13 10:45:41 +03004261 fetch_bit_operand(ctxt);
4262 op->orig_val = op->val;
4263 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004264 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004265 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004266 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03004267 case OpAcc:
4268 op->type = OP_REG;
4269 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004270 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03004271 fetch_register_operand(op);
4272 op->orig_val = op->val;
4273 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004274 case OpAccLo:
4275 op->type = OP_REG;
4276 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4277 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4278 fetch_register_operand(op);
4279 op->orig_val = op->val;
4280 break;
4281 case OpAccHi:
4282 if (ctxt->d & ByteOp) {
4283 op->type = OP_NONE;
4284 break;
4285 }
4286 op->type = OP_REG;
4287 op->bytes = ctxt->op_bytes;
4288 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4289 fetch_register_operand(op);
4290 op->orig_val = op->val;
4291 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004292 case OpDI:
4293 op->type = OP_MEM;
4294 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4295 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004296 register_address(ctxt, VCPU_REGS_RDI);
Avi Kivitya99455492011-09-13 10:45:41 +03004297 op->addr.mem.seg = VCPU_SREG_ES;
4298 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004299 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03004300 break;
4301 case OpDX:
4302 op->type = OP_REG;
4303 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004304 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03004305 fetch_register_operand(op);
4306 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004307 case OpCL:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004308 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004309 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004310 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004311 break;
4312 case OpImmByte:
4313 rc = decode_imm(ctxt, op, 1, true);
4314 break;
4315 case OpOne:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004316 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004317 op->bytes = 1;
4318 op->val = 1;
4319 break;
4320 case OpImm:
4321 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4322 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004323 case OpImm64:
4324 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4325 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004326 case OpMem8:
4327 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004328 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004329 ctxt->memop.addr.reg = decode_register(ctxt,
4330 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004331 fetch_register_operand(&ctxt->memop);
4332 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004333 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004334 case OpMem16:
4335 ctxt->memop.bytes = 2;
4336 goto mem_common;
4337 case OpMem32:
4338 ctxt->memop.bytes = 4;
4339 goto mem_common;
4340 case OpImmU16:
4341 rc = decode_imm(ctxt, op, 2, false);
4342 break;
4343 case OpImmU:
4344 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4345 break;
4346 case OpSI:
4347 op->type = OP_MEM;
4348 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4349 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004350 register_address(ctxt, VCPU_REGS_RSI);
Bandan Das573e80f2014-04-16 12:46:13 -04004351 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03004352 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004353 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004354 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004355 case OpXLat:
4356 op->type = OP_MEM;
4357 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4358 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004359 address_mask(ctxt,
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004360 reg_read(ctxt, VCPU_REGS_RBX) +
4361 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04004362 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004363 op->val = 0;
4364 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004365 case OpImmFAddr:
4366 op->type = OP_IMM;
4367 op->addr.mem.ea = ctxt->_eip;
4368 op->bytes = ctxt->op_bytes + 2;
4369 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4370 break;
4371 case OpMemFAddr:
4372 ctxt->memop.bytes = ctxt->op_bytes + 2;
4373 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004374 case OpES:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004375 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004376 op->val = VCPU_SREG_ES;
4377 break;
4378 case OpCS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004379 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004380 op->val = VCPU_SREG_CS;
4381 break;
4382 case OpSS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004383 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004384 op->val = VCPU_SREG_SS;
4385 break;
4386 case OpDS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004387 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004388 op->val = VCPU_SREG_DS;
4389 break;
4390 case OpFS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004391 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004392 op->val = VCPU_SREG_FS;
4393 break;
4394 case OpGS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004395 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004396 op->val = VCPU_SREG_GS;
4397 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004398 case OpImplicit:
4399 /* Special instructions do their own operand decoding. */
4400 default:
4401 op->type = OP_NONE; /* Disable writeback. */
4402 break;
4403 }
4404
4405done:
4406 return rc;
4407}
4408
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004409int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004410{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004411 int rc = X86EMUL_CONTINUE;
4412 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004413 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004414 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04004415 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03004416 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004417
Avi Kivityf09ed832011-09-13 10:45:40 +03004418 ctxt->memop.type = OP_NONE;
4419 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004420 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02004421 ctxt->fetch.ptr = ctxt->fetch.data;
4422 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004423 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01004424 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004425 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004426 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02004427 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004428 if (rc != X86EMUL_CONTINUE)
4429 return rc;
4430 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004431
4432 switch (mode) {
4433 case X86EMUL_MODE_REAL:
4434 case X86EMUL_MODE_VM86:
4435 case X86EMUL_MODE_PROT16:
4436 def_op_bytes = def_ad_bytes = 2;
4437 break;
4438 case X86EMUL_MODE_PROT32:
4439 def_op_bytes = def_ad_bytes = 4;
4440 break;
4441#ifdef CONFIG_X86_64
4442 case X86EMUL_MODE_PROT64:
4443 def_op_bytes = 4;
4444 def_ad_bytes = 8;
4445 break;
4446#endif
4447 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004448 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004449 }
4450
Avi Kivity9dac77f2011-06-01 15:34:25 +03004451 ctxt->op_bytes = def_op_bytes;
4452 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004453
4454 /* Legacy prefixes. */
4455 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004456 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004457 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004458 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004459 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004460 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004461 break;
4462 case 0x67: /* address-size override */
4463 if (mode == X86EMUL_MODE_PROT64)
4464 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004465 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004466 else
4467 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004468 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004469 break;
4470 case 0x26: /* ES override */
4471 case 0x2e: /* CS override */
4472 case 0x36: /* SS override */
4473 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004474 has_seg_override = true;
4475 ctxt->seg_override = (ctxt->b >> 3) & 3;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004476 break;
4477 case 0x64: /* FS override */
4478 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004479 has_seg_override = true;
4480 ctxt->seg_override = ctxt->b & 7;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004481 break;
4482 case 0x40 ... 0x4f: /* REX */
4483 if (mode != X86EMUL_MODE_PROT64)
4484 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004485 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004486 continue;
4487 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004488 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004489 break;
4490 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004491 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004492 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004493 break;
4494 default:
4495 goto done_prefixes;
4496 }
4497
4498 /* Any legacy prefix after a REX prefix nullifies its effect. */
4499
Avi Kivity9dac77f2011-06-01 15:34:25 +03004500 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004501 }
4502
4503done_prefixes:
4504
4505 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004506 if (ctxt->rex_prefix & 8)
4507 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004508
4509 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004510 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004511 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004512 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004513 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004514 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004515 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004516
4517 /* 0F_38 opcode map */
4518 if (ctxt->b == 0x38) {
4519 ctxt->opcode_len = 3;
4520 ctxt->b = insn_fetch(u8, ctxt);
4521 opcode = opcode_map_0f_38[ctxt->b];
4522 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004523 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004524 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004525
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004526 if (ctxt->d & ModRM)
4527 ctxt->modrm = insn_fetch(u8, ctxt);
4528
Nadav Amit7fe864d2014-06-02 18:34:03 +03004529 /* vex-prefix instructions are not implemented */
4530 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
Nadav Amitd14cb5d2014-11-02 11:54:58 +02004531 (mode == X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) == 0xc0)) {
Nadav Amit7fe864d2014-06-02 18:34:03 +03004532 ctxt->d = NotImpl;
4533 }
4534
Avi Kivity9dac77f2011-06-01 15:34:25 +03004535 while (ctxt->d & GroupMask) {
4536 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004537 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004538 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004539 opcode = opcode.u.group[goffset];
4540 break;
4541 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004542 goffset = (ctxt->modrm >> 3) & 7;
4543 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004544 opcode = opcode.u.gdual->mod3[goffset];
4545 else
4546 opcode = opcode.u.gdual->mod012[goffset];
4547 break;
4548 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004549 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004550 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004551 break;
4552 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004553 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004554 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004555 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004556 switch (simd_prefix) {
4557 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4558 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4559 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4560 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4561 }
4562 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004563 case Escape:
4564 if (ctxt->modrm > 0xbf)
4565 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4566 else
4567 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4568 break;
Nadav Amit39f062f2014-11-26 15:47:18 +02004569 case InstrDual:
4570 if ((ctxt->modrm >> 6) == 3)
4571 opcode = opcode.u.idual->mod3;
4572 else
4573 opcode = opcode.u.idual->mod012;
4574 break;
Avi Kivity46561642011-04-24 14:09:59 +03004575 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004576 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004577 }
Avi Kivity46561642011-04-24 14:09:59 +03004578
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004579 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004580 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004581 }
4582
Paolo Bonzinie24186e2014-03-27 12:00:57 +01004583 /* Unrecognised? */
4584 if (ctxt->d == 0)
4585 return EMULATION_FAILED;
4586
Avi Kivity9dac77f2011-06-01 15:34:25 +03004587 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004588
Nadav Amit3a6095a2014-08-13 16:50:13 +03004589 if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD)))
4590 return EMULATION_FAILED;
4591
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004592 if (unlikely(ctxt->d &
Nadav Amited9aad22014-11-02 11:55:00 +02004593 (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch|
4594 No16))) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004595 /*
4596 * These are copied unconditionally here, and checked unconditionally
4597 * in x86_emulate_insn.
4598 */
4599 ctxt->check_perm = opcode.check_perm;
4600 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004601
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004602 if (ctxt->d & NotImpl)
4603 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004604
Nadav Amit58b70752014-10-24 11:35:09 +03004605 if (mode == X86EMUL_MODE_PROT64) {
4606 if (ctxt->op_bytes == 4 && (ctxt->d & Stack))
4607 ctxt->op_bytes = 8;
4608 else if (ctxt->d & NearBranch)
4609 ctxt->op_bytes = 8;
4610 }
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004611
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004612 if (ctxt->d & Op3264) {
4613 if (mode == X86EMUL_MODE_PROT64)
4614 ctxt->op_bytes = 8;
4615 else
4616 ctxt->op_bytes = 4;
4617 }
4618
Nadav Amited9aad22014-11-02 11:55:00 +02004619 if ((ctxt->d & No16) && ctxt->op_bytes == 2)
4620 ctxt->op_bytes = 4;
4621
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004622 if (ctxt->d & Sse)
4623 ctxt->op_bytes = 16;
4624 else if (ctxt->d & Mmx)
4625 ctxt->op_bytes = 8;
4626 }
Avi Kivity1253791d2011-03-29 11:41:27 +02004627
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004628 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004629 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004630 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04004631 if (!has_seg_override) {
4632 has_seg_override = true;
4633 ctxt->seg_override = ctxt->modrm_seg;
4634 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004635 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004636 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004637 if (rc != X86EMUL_CONTINUE)
4638 goto done;
4639
Bandan Das573e80f2014-04-16 12:46:13 -04004640 if (!has_seg_override)
4641 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004642
Bandan Das573e80f2014-04-16 12:46:13 -04004643 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004644
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004645 /*
4646 * Decode and fetch the source operand: register, memory
4647 * or immediate.
4648 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004649 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004650 if (rc != X86EMUL_CONTINUE)
4651 goto done;
4652
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004653 /*
4654 * Decode and fetch the second source operand: register, memory
4655 * or immediate.
4656 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004657 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004658 if (rc != X86EMUL_CONTINUE)
4659 goto done;
4660
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004661 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03004662 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004663
Bandan Das41061cd2014-04-16 12:46:14 -04004664 if (ctxt->rip_relative)
Nadav Amit1c1c35a2014-11-19 17:43:09 +02004665 ctxt->memopp->addr.mem.ea = address_mask(ctxt,
4666 ctxt->memopp->addr.mem.ea + ctxt->_eip);
Avi Kivitycb16c342011-06-19 19:21:11 +03004667
Paolo Bonzinia430c912014-10-23 14:54:14 +02004668done:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004669 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004670}
4671
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004672bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4673{
4674 return ctxt->d & PageTable;
4675}
4676
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004677static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4678{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004679 /* The second termination condition only applies for REPE
4680 * and REPNE. Test if the repeat string operation prefix is
4681 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4682 * corresponding termination condition according to:
4683 * - if REPE/REPZ and ZF = 0 then done
4684 * - if REPNE/REPNZ and ZF = 1 then done
4685 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004686 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4687 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4688 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004689 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004690 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004691 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4692 return true;
4693
4694 return false;
4695}
4696
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004697static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4698{
4699 bool fault = false;
4700
4701 ctxt->ops->get_fpu(ctxt);
4702 asm volatile("1: fwait \n\t"
4703 "2: \n\t"
4704 ".pushsection .fixup,\"ax\" \n\t"
4705 "3: \n\t"
4706 "movb $1, %[fault] \n\t"
4707 "jmp 2b \n\t"
4708 ".popsection \n\t"
4709 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004710 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004711 ctxt->ops->put_fpu(ctxt);
4712
4713 if (unlikely(fault))
4714 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4715
4716 return X86EMUL_CONTINUE;
4717}
4718
4719static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4720 struct operand *op)
4721{
4722 if (op->type == OP_MM)
4723 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4724}
4725
Avi Kivitye28bbd42013-01-04 16:18:48 +02004726static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4727{
4728 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivityb9fa4092013-02-09 11:31:48 +02004729 if (!(ctxt->d & ByteOp))
4730 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Avi Kivitye28bbd42013-01-04 16:18:48 +02004731 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004732 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
4733 [fastop]"+S"(fop)
4734 : "c"(ctxt->src2.val));
Avi Kivitye28bbd42013-01-04 16:18:48 +02004735 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004736 if (!fop) /* exception is returned in fop variable */
4737 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02004738 return X86EMUL_CONTINUE;
4739}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004740
Bandan Das14985072014-04-16 12:46:09 -04004741void init_decode_cache(struct x86_emulate_ctxt *ctxt)
4742{
Bandan Das573e80f2014-04-16 12:46:13 -04004743 memset(&ctxt->rip_relative, 0,
4744 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04004745
Bandan Das14985072014-04-16 12:46:09 -04004746 ctxt->io_read.pos = 0;
4747 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04004748 ctxt->mem_read.end = 0;
4749}
4750
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004751int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004752{
Mathias Krause0225fb52012-08-30 01:30:16 +02004753 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004754 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004755 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004756
Avi Kivity9dac77f2011-06-01 15:34:25 +03004757 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004758
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004759 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004760 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004761 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004762 goto done;
4763 }
4764
Avi Kivity9dac77f2011-06-01 15:34:25 +03004765 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004766 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004767 goto done;
4768 }
4769
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004770 if (unlikely(ctxt->d &
4771 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
4772 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4773 (ctxt->d & Undefined)) {
4774 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004775 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004776 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004777
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004778 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4779 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
4780 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004781 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004782 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004783
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004784 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
4785 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004786 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004787 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004788
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004789 if (ctxt->d & Mmx) {
4790 rc = flush_pending_x87_faults(ctxt);
4791 if (rc != X86EMUL_CONTINUE)
4792 goto done;
4793 /*
4794 * Now that we know the fpu is exception safe, we can fetch
4795 * operands from it.
4796 */
4797 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4798 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4799 if (!(ctxt->d & Mov))
4800 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4801 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004802
Bandan Das685bbf42014-04-16 12:46:10 -04004803 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004804 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4805 X86_ICPT_PRE_EXCEPT);
4806 if (rc != X86EMUL_CONTINUE)
4807 goto done;
4808 }
4809
Nadav Amit64a38292014-12-10 11:19:04 +02004810 /* Instruction can only be executed in protected mode */
4811 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
4812 rc = emulate_ud(ctxt);
4813 goto done;
4814 }
4815
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004816 /* Privileged instruction can be executed only in CPL=0 */
4817 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Nadav Amit68efa762014-06-18 17:19:35 +03004818 if (ctxt->d & PrivUD)
4819 rc = emulate_ud(ctxt);
4820 else
4821 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004822 goto done;
4823 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004824
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004825 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04004826 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004827 rc = ctxt->check_perm(ctxt);
4828 if (rc != X86EMUL_CONTINUE)
4829 goto done;
4830 }
4831
Bandan Das685bbf42014-04-16 12:46:10 -04004832 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004833 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4834 X86_ICPT_POST_EXCEPT);
4835 if (rc != X86EMUL_CONTINUE)
4836 goto done;
4837 }
4838
4839 if (ctxt->rep_prefix && (ctxt->d & String)) {
4840 /* All REP prefixes have the same first termination condition */
4841 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
4842 ctxt->eip = ctxt->_eip;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004843 ctxt->eflags &= ~EFLG_RF;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004844 goto done;
4845 }
4846 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004847 }
4848
Avi Kivity9dac77f2011-06-01 15:34:25 +03004849 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4850 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4851 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004852 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004853 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004854 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004855 }
4856
Avi Kivity9dac77f2011-06-01 15:34:25 +03004857 if (ctxt->src2.type == OP_MEM) {
4858 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4859 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004860 if (rc != X86EMUL_CONTINUE)
4861 goto done;
4862 }
4863
Avi Kivity9dac77f2011-06-01 15:34:25 +03004864 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004865 goto special_insn;
4866
4867
Avi Kivity9dac77f2011-06-01 15:34:25 +03004868 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004869 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004870 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4871 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004872 if (rc != X86EMUL_CONTINUE)
4873 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004874 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004875 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004876
Avi Kivity018a98d2007-11-27 19:30:56 +02004877special_insn:
4878
Bandan Das685bbf42014-04-16 12:46:10 -04004879 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004880 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004881 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004882 if (rc != X86EMUL_CONTINUE)
4883 goto done;
4884 }
4885
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004886 if (ctxt->rep_prefix && (ctxt->d & String))
4887 ctxt->eflags |= EFLG_RF;
4888 else
4889 ctxt->eflags &= ~EFLG_RF;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004890
Avi Kivity9dac77f2011-06-01 15:34:25 +03004891 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004892 if (ctxt->d & Fastop) {
4893 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4894 rc = fastop(ctxt, fop);
4895 if (rc != X86EMUL_CONTINUE)
4896 goto done;
4897 goto writeback;
4898 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004899 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004900 if (rc != X86EMUL_CONTINUE)
4901 goto done;
4902 goto writeback;
4903 }
4904
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004905 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004906 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004907 else if (ctxt->opcode_len == 3)
4908 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004909
Avi Kivity9dac77f2011-06-01 15:34:25 +03004910 switch (ctxt->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004911 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004912 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004913 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004914 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004915 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004916 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004917 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03004918 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004919 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004920 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004921 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004922 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004923 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004924 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03004925 ctxt->dst.type = OP_NONE;
4926 else
4927 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004928 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004929 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004930 switch (ctxt->op_bytes) {
4931 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4932 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4933 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004934 }
4935 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004936 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004937 rc = emulate_int(ctxt, 3);
4938 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004939 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004940 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004941 break;
4942 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004943 if (ctxt->eflags & EFLG_OF)
4944 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004945 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004946 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004947 case 0xeb: /* jmp rel short */
Nadav Amit234f3ce2014-09-18 22:39:38 +03004948 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004949 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004950 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004951 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004952 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004953 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004954 case 0xf5: /* cmc */
4955 /* complement carry flag from eflags reg */
4956 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004957 break;
4958 case 0xf8: /* clc */
4959 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004960 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004961 case 0xf9: /* stc */
4962 ctxt->eflags |= EFLG_CF;
4963 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004964 case 0xfc: /* cld */
4965 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004966 break;
4967 case 0xfd: /* std */
4968 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004969 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004970 default:
4971 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004972 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004973
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004974 if (rc != X86EMUL_CONTINUE)
4975 goto done;
4976
Avi Kivity018a98d2007-11-27 19:30:56 +02004977writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02004978 if (ctxt->d & SrcWrite) {
4979 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
4980 rc = writeback(ctxt, &ctxt->src);
4981 if (rc != X86EMUL_CONTINUE)
4982 goto done;
4983 }
Nadav Amitee212292014-06-15 16:12:58 +03004984 if (!(ctxt->d & NoWrite)) {
4985 rc = writeback(ctxt, &ctxt->dst);
4986 if (rc != X86EMUL_CONTINUE)
4987 goto done;
4988 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004989
Gleb Natapov5cd21912010-03-18 15:20:26 +02004990 /*
4991 * restore dst type in case the decoding will be reused
4992 * (happens for string instruction )
4993 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004994 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004995
Avi Kivity9dac77f2011-06-01 15:34:25 +03004996 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004997 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004998
Avi Kivity9dac77f2011-06-01 15:34:25 +03004999 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005000 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02005001
Avi Kivity9dac77f2011-06-01 15:34:25 +03005002 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005003 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005004 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005005 if ((ctxt->d & SrcMask) == SrcSI)
5006 count = ctxt->src.count;
5007 else
5008 count = ctxt->dst.count;
Paolo Bonzini01485a22014-11-19 18:25:08 +01005009 register_address_increment(ctxt, VCPU_REGS_RCX, -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005010
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005011 if (!string_insn_completed(ctxt)) {
5012 /*
5013 * Re-enter guest when pio read ahead buffer is empty
5014 * or, if it is not used, after each 1024 iteration.
5015 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005016 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005017 (r->end == 0 || r->end != r->pos)) {
5018 /*
5019 * Reset read cache. Usually happens before
5020 * decode, but since instruction is restarted
5021 * we have to do it here.
5022 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005023 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005024 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005025 return EMULATION_RESTART;
5026 }
5027 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03005028 }
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03005029 ctxt->eflags &= ~EFLG_RF;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005030 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005031
Avi Kivity9dac77f2011-06-01 15:34:25 +03005032 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02005033
5034done:
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005035 if (rc == X86EMUL_PROPAGATE_FAULT) {
5036 WARN_ON(ctxt->exception.vector > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +02005037 ctxt->have_exception = true;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005038 }
Joerg Roedel775fde82011-04-04 12:39:24 +02005039 if (rc == X86EMUL_INTERCEPTED)
5040 return EMULATION_INTERCEPTED;
5041
Avi Kivitydd856ef2012-08-27 23:46:17 +03005042 if (rc == X86EMUL_CONTINUE)
5043 writeback_registers(ctxt);
5044
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005045 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005046
5047twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005048 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02005049 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02005050 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005051 break;
5052 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02005053 case 0x0d: /* GrpP (prefetch) */
5054 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02005055 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02005056 break;
5057 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005058 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02005059 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005060 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005061 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005062 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005063 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03005064 if (test_cc(ctxt->b, ctxt->eflags))
5065 ctxt->dst.val = ctxt->src.val;
5066 else if (ctxt->mode != X86EMUL_MODE_PROT64 ||
5067 ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005068 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08005069 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03005070 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03005071 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005072 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005073 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08005074 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005075 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08005076 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005077 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005078 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005079 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03005080 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005081 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005082 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005083 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005084 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03005085 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005086 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005087 default:
5088 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005089 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005090
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005091threebyte_insn:
5092
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005093 if (rc != X86EMUL_CONTINUE)
5094 goto done;
5095
Avi Kivity6aa8b732006-12-10 02:21:36 -08005096 goto writeback;
5097
5098cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02005099 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005100}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005101
5102void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
5103{
5104 invalidate_registers(ctxt);
5105}
5106
5107void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
5108{
5109 writeback_registers(ctxt);
5110}