blob: c3b07574942fa3169ec6e9f5460ccfcc5dd9100a [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)
Nadav Amit16bebef2014-12-25 02:52:18 +020089#define DstMem16 (OpMem16 << DstShift)
Avi Kivitya99455492011-09-13 10:45:41 +030090#define DstImmUByte (OpImmUByte << DstShift)
91#define DstDX (OpDX << DstShift)
Avi Kivity820207c2013-02-09 11:31:45 +020092#define DstAccLo (OpAccLo << DstShift)
Avi Kivitya99455492011-09-13 10:45:41 +030093#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080094/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030095#define SrcShift 6
96#define SrcNone (OpNone << SrcShift)
97#define SrcReg (OpReg << SrcShift)
98#define SrcMem (OpMem << SrcShift)
99#define SrcMem16 (OpMem16 << SrcShift)
100#define SrcMem32 (OpMem32 << SrcShift)
101#define SrcImm (OpImm << SrcShift)
102#define SrcImmByte (OpImmByte << SrcShift)
103#define SrcOne (OpOne << SrcShift)
104#define SrcImmUByte (OpImmUByte << SrcShift)
105#define SrcImmU (OpImmU << SrcShift)
106#define SrcSI (OpSI << SrcShift)
Paolo Bonzini7fa57952013-05-09 11:32:50 +0200107#define SrcXLat (OpXLat << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300108#define SrcImmFAddr (OpImmFAddr << SrcShift)
109#define SrcMemFAddr (OpMemFAddr << SrcShift)
110#define SrcAcc (OpAcc << SrcShift)
111#define SrcImmU16 (OpImmU16 << SrcShift)
Nadav Amit5e2c6882012-12-06 21:55:10 -0200112#define SrcImm64 (OpImm64 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300113#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200114#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity820207c2013-02-09 11:31:45 +0200115#define SrcAccHi (OpAccHi << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300116#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300117#define BitOp (1<<11)
118#define MemAbs (1<<12) /* Memory operand is absolute displacement */
119#define String (1<<13) /* String instruction (rep capable) */
120#define Stack (1<<14) /* Stack instruction (push/pop) */
121#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
122#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
123#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
124#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
125#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
Gleb Natapov045a2822012-12-20 16:57:43 +0200126#define Escape (5<<15) /* Escape to coprocessor instruction */
Nadav Amit39f062f2014-11-26 15:47:18 +0200127#define InstrDual (6<<15) /* Alternate instruction decoding of mod == 3 */
Nadav Amit2276b512015-01-26 09:32:24 +0200128#define ModeDual (7<<15) /* Different instruction for 32/64 bit */
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300129#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300130/* Generic ModRM decode. */
131#define ModRM (1<<19)
132/* Destination is only written; never read. */
133#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300134/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200135#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Borislav Petkovb51e9742013-09-22 16:44:52 +0200136#define EmulateOnUD (1<<22) /* Emulate if unsupported by the host */
Avi Kivity5a506b12010-08-01 15:10:29 +0300137#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300138#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300139#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200140#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200141#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300142#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800143#define PageTable (1 << 29) /* instruction used to write page table */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300144#define NotImpl (1 << 30) /* instruction is not implemented */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100145/* Source 2 operand type */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300146#define Src2Shift (31)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300147#define Src2None (OpNone << Src2Shift)
Avi Kivityab2c5ce2013-02-09 11:31:46 +0200148#define Src2Mem (OpMem << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300149#define Src2CL (OpCL << Src2Shift)
150#define Src2ImmByte (OpImmByte << Src2Shift)
151#define Src2One (OpOne << Src2Shift)
152#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300153#define Src2ES (OpES << Src2Shift)
154#define Src2CS (OpCS << Src2Shift)
155#define Src2SS (OpSS << Src2Shift)
156#define Src2DS (OpDS << Src2Shift)
157#define Src2FS (OpFS << Src2Shift)
158#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300159#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300160#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Avi Kivity1c11b372012-04-09 18:39:59 +0300161#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
162#define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */
163#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
Avi Kivitye28bbd42013-01-04 16:18:48 +0200164#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
Avi Kivityb6744dc2013-01-04 16:18:50 +0200165#define NoWrite ((u64)1 << 45) /* No writeback */
Avi Kivityfb32b1e2013-02-09 11:31:44 +0200166#define SrcWrite ((u64)1 << 46) /* Write back src operand */
Nadav Amit9b88ae92014-05-25 23:05:21 +0300167#define NoMod ((u64)1 << 47) /* Mod field is ignored */
Paolo Bonzinid40a6892014-03-27 11:58:02 +0100168#define Intercept ((u64)1 << 48) /* Has valid intercept field */
169#define CheckPerm ((u64)1 << 49) /* Has valid check_perm field */
Nadav Amit68efa762014-06-18 17:19:35 +0300170#define PrivUD ((u64)1 << 51) /* #UD instead of #GP on CPL > 0 */
Nadav Amit58b70752014-10-24 11:35:09 +0300171#define NearBranch ((u64)1 << 52) /* Near branches */
Nadav Amited9aad22014-11-02 11:55:00 +0200172#define No16 ((u64)1 << 53) /* No 16 bit operand */
Nadav Amitab708092014-12-25 02:52:21 +0200173#define IncSP ((u64)1 << 54) /* SP is incremented before ModRM calc */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800174
Avi Kivity820207c2013-02-09 11:31:45 +0200175#define DstXacc (DstAccLo | SrcAccHi | SrcWrite)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800176
Avi Kivityd0e53322010-07-29 15:11:54 +0300177#define X2(x...) x, x
178#define X3(x...) X2(x), x
179#define X4(x...) X2(x), X2(x)
180#define X5(x...) X4(x), x
181#define X6(x...) X4(x), X2(x)
182#define X7(x...) X4(x), X3(x)
183#define X8(x...) X4(x), X4(x)
184#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300185
Avi Kivitye28bbd42013-01-04 16:18:48 +0200186#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
187#define FASTOP_SIZE 8
188
189/*
190 * fastop functions have a special calling convention:
191 *
Avi Kivity017da7b2013-02-09 11:31:47 +0200192 * dst: rax (in/out)
193 * src: rdx (in/out)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200194 * src2: rcx (in)
195 * flags: rflags (in/out)
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200196 * ex: rsi (in:fastop pointer, out:zero if exception)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200197 *
198 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
199 * different operand sizes can be reached by calculation, rather than a jump
200 * table (which would be bigger than the code).
201 *
202 * fastop functions are declared as taking a never-defined fastop parameter,
203 * so they can't be called from C directly.
204 */
205
206struct fastop;
207
Avi Kivityd65b1de2010-07-29 15:11:35 +0300208struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300209 u64 flags : 56;
210 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300211 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300212 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200213 const struct opcode *group;
214 const struct group_dual *gdual;
215 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200216 const struct escape *esc;
Nadav Amit39f062f2014-11-26 15:47:18 +0200217 const struct instr_dual *idual;
Nadav Amit2276b512015-01-26 09:32:24 +0200218 const struct mode_dual *mdual;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200219 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300220 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200221 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300222};
223
224struct group_dual {
225 struct opcode mod012[8];
226 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300227};
228
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200229struct gprefix {
230 struct opcode pfx_no;
231 struct opcode pfx_66;
232 struct opcode pfx_f2;
233 struct opcode pfx_f3;
234};
235
Gleb Natapov045a2822012-12-20 16:57:43 +0200236struct escape {
237 struct opcode op[8];
238 struct opcode high[64];
239};
240
Nadav Amit39f062f2014-11-26 15:47:18 +0200241struct instr_dual {
242 struct opcode mod012;
243 struct opcode mod3;
244};
245
Nadav Amit2276b512015-01-26 09:32:24 +0200246struct mode_dual {
247 struct opcode mode32;
248 struct opcode mode64;
249};
250
Avi Kivity6aa8b732006-12-10 02:21:36 -0800251/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200252#define EFLG_ID (1<<21)
253#define EFLG_VIP (1<<20)
254#define EFLG_VIF (1<<19)
255#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200256#define EFLG_VM (1<<17)
257#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200258#define EFLG_IOPL (3<<12)
259#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800260#define EFLG_OF (1<<11)
261#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200262#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200263#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800264#define EFLG_SF (1<<7)
265#define EFLG_ZF (1<<6)
266#define EFLG_AF (1<<4)
267#define EFLG_PF (1<<2)
268#define EFLG_CF (1<<0)
269
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300270#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
271#define EFLG_RESERVED_ONE_MASK 2
272
Nadav Amit3dc4bc42014-12-25 02:52:19 +0200273enum x86_transfer_type {
274 X86_TRANSFER_NONE,
275 X86_TRANSFER_CALL_JMP,
276 X86_TRANSFER_RET,
277 X86_TRANSFER_TASK_SWITCH,
278};
279
Avi Kivitydd856ef2012-08-27 23:46:17 +0300280static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
281{
282 if (!(ctxt->regs_valid & (1 << nr))) {
283 ctxt->regs_valid |= 1 << nr;
284 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
285 }
286 return ctxt->_regs[nr];
287}
288
289static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
290{
291 ctxt->regs_valid |= 1 << nr;
292 ctxt->regs_dirty |= 1 << nr;
293 return &ctxt->_regs[nr];
294}
295
296static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
297{
298 reg_read(ctxt, nr);
299 return reg_write(ctxt, nr);
300}
301
302static void writeback_registers(struct x86_emulate_ctxt *ctxt)
303{
304 unsigned reg;
305
306 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
307 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
308}
309
310static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
311{
312 ctxt->regs_dirty = 0;
313 ctxt->regs_valid = 0;
314}
315
Avi Kivity6aa8b732006-12-10 02:21:36 -0800316/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800317 * These EFLAGS bits are restored from saved value during emulation, and
318 * any changes are written back to the saved value after emulation.
319 */
320#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
321
Avi Kivitydda96d82008-11-26 15:14:10 +0200322#ifdef CONFIG_X86_64
323#define ON64(x) x
324#else
325#define ON64(x)
326#endif
327
Avi Kivity4d758342013-01-19 19:51:55 +0200328static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *));
329
Avi Kivityb7d491e2013-01-04 16:18:49 +0200330#define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t"
331#define FOP_RET "ret \n\t"
332
333#define FOP_START(op) \
334 extern void em_##op(struct fastop *fake); \
335 asm(".pushsection .text, \"ax\" \n\t" \
336 ".global em_" #op " \n\t" \
337 FOP_ALIGN \
338 "em_" #op ": \n\t"
339
340#define FOP_END \
341 ".popsection")
342
Avi Kivity0bdea062013-01-19 19:51:50 +0200343#define FOPNOP() FOP_ALIGN FOP_RET
344
Avi Kivityb7d491e2013-01-04 16:18:49 +0200345#define FOP1E(op, dst) \
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200346 FOP_ALIGN "10: " #op " %" #dst " \n\t" FOP_RET
347
348#define FOP1EEX(op, dst) \
349 FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200350
351#define FASTOP1(op) \
352 FOP_START(op) \
353 FOP1E(op##b, al) \
354 FOP1E(op##w, ax) \
355 FOP1E(op##l, eax) \
356 ON64(FOP1E(op##q, rax)) \
357 FOP_END
358
Avi Kivityb9fa4092013-02-09 11:31:48 +0200359/* 1-operand, using src2 (for MUL/DIV r/m) */
360#define FASTOP1SRC2(op, name) \
361 FOP_START(name) \
362 FOP1E(op, cl) \
363 FOP1E(op, cx) \
364 FOP1E(op, ecx) \
365 ON64(FOP1E(op, rcx)) \
366 FOP_END
367
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200368/* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */
369#define FASTOP1SRC2EX(op, name) \
370 FOP_START(name) \
371 FOP1EEX(op, cl) \
372 FOP1EEX(op, cx) \
373 FOP1EEX(op, ecx) \
374 ON64(FOP1EEX(op, rcx)) \
375 FOP_END
376
Avi Kivityf7857f32013-01-04 16:18:53 +0200377#define FOP2E(op, dst, src) \
378 FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET
379
380#define FASTOP2(op) \
381 FOP_START(op) \
Avi Kivity017da7b2013-02-09 11:31:47 +0200382 FOP2E(op##b, al, dl) \
383 FOP2E(op##w, ax, dx) \
384 FOP2E(op##l, eax, edx) \
385 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivityf7857f32013-01-04 16:18:53 +0200386 FOP_END
387
Avi Kivity11c363b2013-01-19 19:51:54 +0200388/* 2 operand, word only */
389#define FASTOP2W(op) \
390 FOP_START(op) \
391 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200392 FOP2E(op##w, ax, dx) \
393 FOP2E(op##l, eax, edx) \
394 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivity11c363b2013-01-19 19:51:54 +0200395 FOP_END
396
Avi Kivity007a3b52013-01-19 19:51:51 +0200397/* 2 operand, src is CL */
398#define FASTOP2CL(op) \
399 FOP_START(op) \
400 FOP2E(op##b, al, cl) \
401 FOP2E(op##w, ax, cl) \
402 FOP2E(op##l, eax, cl) \
403 ON64(FOP2E(op##q, rax, cl)) \
404 FOP_END
405
Nadav Amit5aca3722014-11-02 11:54:50 +0200406/* 2 operand, src and dest are reversed */
407#define FASTOP2R(op, name) \
408 FOP_START(name) \
409 FOP2E(op##b, dl, al) \
410 FOP2E(op##w, dx, ax) \
411 FOP2E(op##l, edx, eax) \
412 ON64(FOP2E(op##q, rdx, rax)) \
413 FOP_END
414
Avi Kivity0bdea062013-01-19 19:51:50 +0200415#define FOP3E(op, dst, src, src2) \
416 FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
417
418/* 3-operand, word-only, src2=cl */
419#define FASTOP3WCL(op) \
420 FOP_START(op) \
421 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200422 FOP3E(op##w, ax, dx, cl) \
423 FOP3E(op##l, eax, edx, cl) \
424 ON64(FOP3E(op##q, rax, rdx, cl)) \
Avi Kivity0bdea062013-01-19 19:51:50 +0200425 FOP_END
426
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200427/* Special case for SETcc - 1 instruction per cc */
428#define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t"
429
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200430asm(".global kvm_fastop_exception \n"
431 "kvm_fastop_exception: xor %esi, %esi; ret");
432
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200433FOP_START(setcc)
434FOP_SETCC(seto)
435FOP_SETCC(setno)
436FOP_SETCC(setc)
437FOP_SETCC(setnc)
438FOP_SETCC(setz)
439FOP_SETCC(setnz)
440FOP_SETCC(setbe)
441FOP_SETCC(setnbe)
442FOP_SETCC(sets)
443FOP_SETCC(setns)
444FOP_SETCC(setp)
445FOP_SETCC(setnp)
446FOP_SETCC(setl)
447FOP_SETCC(setnl)
448FOP_SETCC(setle)
449FOP_SETCC(setnle)
450FOP_END;
451
Paolo Bonzini326f5782013-05-09 11:32:51 +0200452FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET
453FOP_END;
454
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200455static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
456 enum x86_intercept intercept,
457 enum x86_intercept_stage stage)
458{
459 struct x86_instruction_info info = {
460 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300461 .rep_prefix = ctxt->rep_prefix,
462 .modrm_mod = ctxt->modrm_mod,
463 .modrm_reg = ctxt->modrm_reg,
464 .modrm_rm = ctxt->modrm_rm,
465 .src_val = ctxt->src.val64,
Jan Kiszka6cbc5f52014-06-30 12:52:55 +0200466 .dst_val = ctxt->dst.val64,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300467 .src_bytes = ctxt->src.bytes,
468 .dst_bytes = ctxt->dst.bytes,
469 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200470 .next_rip = ctxt->eip,
471 };
472
Avi Kivity29535382011-04-20 13:37:53 +0300473 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200474}
475
Avi Kivityf47cfa32012-06-07 17:49:24 +0300476static void assign_masked(ulong *dest, ulong src, ulong mask)
477{
478 *dest = (*dest & ~mask) | (src & mask);
479}
480
Avi Kivity9dac77f2011-06-01 15:34:25 +0300481static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800482{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300483 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800484}
485
Avi Kivityf47cfa32012-06-07 17:49:24 +0300486static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
487{
488 u16 sel;
489 struct desc_struct ss;
490
491 if (ctxt->mode == X86EMUL_MODE_PROT64)
492 return ~0UL;
493 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
494 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
495}
496
Avi Kivity612e89f2012-06-12 20:03:23 +0300497static int stack_size(struct x86_emulate_ctxt *ctxt)
498{
499 return (__fls(stack_mask(ctxt)) + 1) >> 3;
500}
501
Avi Kivity6aa8b732006-12-10 02:21:36 -0800502/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800503static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300504address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800505{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300506 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800507 return reg;
508 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300509 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800510}
511
512static inline unsigned long
Paolo Bonzini01485a22014-11-19 18:25:08 +0100513register_address(struct x86_emulate_ctxt *ctxt, int reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800514{
Paolo Bonzini01485a22014-11-19 18:25:08 +0100515 return address_mask(ctxt, reg_read(ctxt, reg));
Harvey Harrisone4706772008-02-19 07:40:38 -0800516}
517
Avi Kivity5ad105e2012-08-19 14:34:31 +0300518static void masked_increment(ulong *reg, ulong mask, int inc)
519{
520 assign_masked(reg, *reg + inc, mask);
521}
522
Harvey Harrison7a9572752008-02-19 07:40:41 -0800523static inline void
Paolo Bonzini01485a22014-11-19 18:25:08 +0100524register_address_increment(struct x86_emulate_ctxt *ctxt, int reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800525{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300526 ulong mask;
527
Avi Kivity9dac77f2011-06-01 15:34:25 +0300528 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300529 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800530 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300531 mask = ad_mask(ctxt);
Paolo Bonzini01485a22014-11-19 18:25:08 +0100532 masked_increment(reg_rmw(ctxt, reg), mask, inc);
Avi Kivity5ad105e2012-08-19 14:34:31 +0300533}
534
535static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
536{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300537 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800538}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800539
Avi Kivity56697682011-04-03 14:08:51 +0300540static u32 desc_limit_scaled(struct desc_struct *desc)
541{
542 u32 limit = get_desc_limit(desc);
543
544 return desc->g ? (limit << 12) | 0xfff : limit;
545}
546
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900547static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300548{
549 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
550 return 0;
551
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900552 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300553}
554
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200555static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
556 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300557{
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +0200558 WARN_ON(vec > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +0200559 ctxt->exception.vector = vec;
560 ctxt->exception.error_code = error;
561 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200562 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300563}
564
Joerg Roedel3b88e412011-04-04 12:39:29 +0200565static int emulate_db(struct x86_emulate_ctxt *ctxt)
566{
567 return emulate_exception(ctxt, DB_VECTOR, 0, false);
568}
569
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200570static int emulate_gp(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, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300573}
574
Avi Kivity618ff152011-04-03 12:32:09 +0300575static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
576{
577 return emulate_exception(ctxt, SS_VECTOR, err, true);
578}
579
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200580static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300581{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200582 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300583}
584
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200585static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300586{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200587 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300588}
589
Avi Kivity34d1f492010-08-26 11:59:01 +0300590static int emulate_de(struct x86_emulate_ctxt *ctxt)
591{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200592 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300593}
594
Avi Kivity1253791d2011-03-29 11:41:27 +0200595static int emulate_nm(struct x86_emulate_ctxt *ctxt)
596{
597 return emulate_exception(ctxt, NM_VECTOR, 0, false);
598}
599
Avi Kivity1aa36612011-04-27 13:20:30 +0300600static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
601{
602 u16 selector;
603 struct desc_struct desc;
604
605 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
606 return selector;
607}
608
609static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
610 unsigned seg)
611{
612 u16 dummy;
613 u32 base3;
614 struct desc_struct desc;
615
616 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
617 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
618}
619
Avi Kivity1c11b372012-04-09 18:39:59 +0300620/*
621 * x86 defines three classes of vector instructions: explicitly
622 * aligned, explicitly unaligned, and the rest, which change behaviour
623 * depending on whether they're AVX encoded or not.
624 *
625 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
626 * subject to the same check.
627 */
628static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
629{
630 if (likely(size < 16))
631 return false;
632
633 if (ctxt->d & Aligned)
634 return true;
635 else if (ctxt->d & Unaligned)
636 return false;
637 else if (ctxt->d & Avx)
638 return false;
639 else
640 return true;
641}
642
Paolo Bonzinid09155d2014-10-27 14:54:44 +0100643static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
644 struct segmented_address addr,
645 unsigned *max_size, unsigned size,
646 bool write, bool fetch,
Nadav Amitd50eaa12014-11-19 17:43:11 +0200647 enum x86emul_mode mode, ulong *linear)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300648{
Avi Kivity618ff152011-04-03 12:32:09 +0300649 struct desc_struct desc;
650 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300651 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300652 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300653 u16 sel;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300654
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900655 la = seg_base(ctxt, addr.seg) + addr.ea;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100656 *max_size = 0;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200657 switch (mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300658 case X86EMUL_MODE_PROT64:
Nadav Amit4be4de72014-09-18 22:39:40 +0300659 if (is_noncanonical_address(la))
Nadav Amitabc7d8a2014-11-19 17:43:12 +0200660 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100661
662 *max_size = min_t(u64, ~0u, (1ull << 48) - la);
663 if (size > *max_size)
664 goto bad;
Avi Kivity618ff152011-04-03 12:32:09 +0300665 break;
666 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300667 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
668 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300669 if (!usable)
670 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200671 /* code segment in protected mode or read-only data segment */
672 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
673 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300674 goto bad;
675 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400676 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300677 goto bad;
678 lim = desc_limit_scaled(&desc);
Paolo Bonzini997b0412014-11-19 18:33:38 +0100679 if (!(desc.type & 8) && (desc.type & 4)) {
Guo Chaofc058682012-06-28 15:19:51 +0800680 /* expand-down segment */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100681 if (addr.ea <= lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300682 goto bad;
683 lim = desc.d ? 0xffffffff : 0xffff;
Avi Kivity618ff152011-04-03 12:32:09 +0300684 }
Paolo Bonzini997b0412014-11-19 18:33:38 +0100685 if (addr.ea > lim)
686 goto bad;
Nadav Amitbac155312015-01-26 09:32:26 +0200687 if (lim == 0xffffffff)
688 *max_size = ~0u;
689 else {
690 *max_size = (u64)lim + 1 - addr.ea;
691 if (size > *max_size)
692 goto bad;
693 }
Nadav Amit31ff6482014-11-19 17:43:13 +0200694 la &= (u32)-1;
Avi Kivity618ff152011-04-03 12:32:09 +0300695 break;
696 }
Avi Kivity1c11b372012-04-09 18:39:59 +0300697 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
698 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300699 *linear = la;
700 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300701bad:
702 if (addr.seg == VCPU_SREG_SS)
Paolo Bonzini36061892014-10-27 14:40:49 +0100703 return emulate_ss(ctxt, 0);
Avi Kivity618ff152011-04-03 12:32:09 +0300704 else
Paolo Bonzini36061892014-10-27 14:40:49 +0100705 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300706}
707
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400708static int linearize(struct x86_emulate_ctxt *ctxt,
709 struct segmented_address addr,
710 unsigned size, bool write,
711 ulong *linear)
712{
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100713 unsigned max_size;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200714 return __linearize(ctxt, addr, &max_size, size, write, false,
715 ctxt->mode, linear);
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400716}
717
Nadav Amitd50eaa12014-11-19 17:43:11 +0200718static inline int assign_eip(struct x86_emulate_ctxt *ctxt, ulong dst,
719 enum x86emul_mode mode)
720{
721 ulong linear;
722 int rc;
723 unsigned max_size;
724 struct segmented_address addr = { .seg = VCPU_SREG_CS,
725 .ea = dst };
726
727 if (ctxt->op_bytes != sizeof(unsigned long))
728 addr.ea = dst & ((1UL << (ctxt->op_bytes << 3)) - 1);
729 rc = __linearize(ctxt, addr, &max_size, 1, false, true, mode, &linear);
730 if (rc == X86EMUL_CONTINUE)
731 ctxt->_eip = addr.ea;
732 return rc;
733}
734
735static inline int assign_eip_near(struct x86_emulate_ctxt *ctxt, ulong dst)
736{
737 return assign_eip(ctxt, dst, ctxt->mode);
738}
739
740static int assign_eip_far(struct x86_emulate_ctxt *ctxt, ulong dst,
741 const struct desc_struct *cs_desc)
742{
743 enum x86emul_mode mode = ctxt->mode;
744
745#ifdef CONFIG_X86_64
746 if (ctxt->mode >= X86EMUL_MODE_PROT32 && cs_desc->l) {
747 u64 efer = 0;
748
749 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
750 if (efer & EFER_LMA)
751 mode = X86EMUL_MODE_PROT64;
752 }
753#endif
754 if (mode == X86EMUL_MODE_PROT16 || mode == X86EMUL_MODE_PROT32)
755 mode = cs_desc->d ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
756 return assign_eip(ctxt, dst, mode);
757}
758
759static inline int jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
760{
761 return assign_eip_near(ctxt, ctxt->_eip + rel);
762}
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400763
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200764static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
765 struct segmented_address addr,
766 void *data,
767 unsigned size)
768{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200769 int rc;
770 ulong linear;
771
Avi Kivity83b87952011-04-03 11:31:19 +0300772 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200773 if (rc != X86EMUL_CONTINUE)
774 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300775 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200776}
777
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900778/*
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200779 * Prefetch the remaining bytes of the instruction without crossing page
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900780 * boundary if they are not in fetch_cache yet.
781 */
Paolo Bonzini9506d572014-05-06 13:05:25 +0200782static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
Avi Kivity62266862007-11-20 13:15:52 +0200783{
Avi Kivity62266862007-11-20 13:15:52 +0200784 int rc;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100785 unsigned size, max_size;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200786 unsigned long linear;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200787 int cur_size = ctxt->fetch.end - ctxt->fetch.data;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200788 struct segmented_address addr = { .seg = VCPU_SREG_CS,
Paolo Bonzini17052f12014-05-06 16:33:01 +0200789 .ea = ctxt->eip + cur_size };
790
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100791 /*
792 * We do not know exactly how many bytes will be needed, and
793 * __linearize is expensive, so fetch as much as possible. We
794 * just have to avoid going beyond the 15 byte limit, the end
795 * of the segment, or the end of the page.
796 *
797 * __linearize is called with size 0 so that it does not do any
798 * boundary check itself. Instead, we use max_size to check
799 * against op_size.
800 */
Nadav Amitd50eaa12014-11-19 17:43:11 +0200801 rc = __linearize(ctxt, addr, &max_size, 0, false, true, ctxt->mode,
802 &linear);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200803 if (unlikely(rc != X86EMUL_CONTINUE))
804 return rc;
805
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100806 size = min_t(unsigned, 15UL ^ cur_size, max_size);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200807 size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear));
Paolo Bonzini5cfc7e02014-05-06 13:05:25 +0200808
809 /*
810 * One instruction can only straddle two pages,
811 * and one has been loaded at the beginning of
812 * x86_decode_insn. So, if not enough bytes
813 * still, we must have hit the 15-byte boundary.
814 */
815 if (unlikely(size < op_size))
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100816 return emulate_gp(ctxt, 0);
817
Paolo Bonzini17052f12014-05-06 16:33:01 +0200818 rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end,
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200819 size, &ctxt->exception);
820 if (unlikely(rc != X86EMUL_CONTINUE))
821 return rc;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200822 ctxt->fetch.end += size;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900823 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200824}
825
Paolo Bonzini9506d572014-05-06 13:05:25 +0200826static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt,
827 unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200828{
Nadav Amit08da44a2014-10-03 01:10:04 +0300829 unsigned done_size = ctxt->fetch.end - ctxt->fetch.ptr;
830
831 if (unlikely(done_size < size))
832 return __do_insn_fetch_bytes(ctxt, size - done_size);
Paolo Bonzini9506d572014-05-06 13:05:25 +0200833 else
834 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200835}
836
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900837/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900838#define insn_fetch(_type, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200839({ _type _x; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200840 \
841 rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900842 if (rc != X86EMUL_CONTINUE) \
843 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200844 ctxt->_eip += sizeof(_type); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200845 _x = *(_type __aligned(1) *) ctxt->fetch.ptr; \
846 ctxt->fetch.ptr += sizeof(_type); \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200847 _x; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900848})
849
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900850#define insn_fetch_arr(_arr, _size, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200851({ \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200852 rc = do_insn_fetch_bytes(_ctxt, _size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900853 if (rc != X86EMUL_CONTINUE) \
854 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200855 ctxt->_eip += (_size); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200856 memcpy(_arr, ctxt->fetch.ptr, _size); \
857 ctxt->fetch.ptr += (_size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900858})
859
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000860/*
861 * Given the 'reg' portion of a ModRM byte, and a register block, return a
862 * pointer into the block that addresses the relevant register.
863 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
864 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300865static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200866 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867{
868 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200869 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800870
Avi Kivity6aa8b732006-12-10 02:21:36 -0800871 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300872 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
873 else
874 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875 return p;
876}
877
878static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200879 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800880 u16 *size, unsigned long *address, int op_bytes)
881{
882 int rc;
883
884 if (op_bytes == 2)
885 op_bytes = 3;
886 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200887 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900888 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800889 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200890 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200891 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892 return rc;
893}
894
Avi Kivity34b77652013-01-19 19:51:56 +0200895FASTOP2(add);
896FASTOP2(or);
897FASTOP2(adc);
898FASTOP2(sbb);
899FASTOP2(and);
900FASTOP2(sub);
901FASTOP2(xor);
902FASTOP2(cmp);
903FASTOP2(test);
904
Avi Kivityb9fa4092013-02-09 11:31:48 +0200905FASTOP1SRC2(mul, mul_ex);
906FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200907FASTOP1SRC2EX(div, div_ex);
908FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +0200909
Avi Kivity34b77652013-01-19 19:51:56 +0200910FASTOP3WCL(shld);
911FASTOP3WCL(shrd);
912
913FASTOP2W(imul);
914
915FASTOP1(not);
916FASTOP1(neg);
917FASTOP1(inc);
918FASTOP1(dec);
919
920FASTOP2CL(rol);
921FASTOP2CL(ror);
922FASTOP2CL(rcl);
923FASTOP2CL(rcr);
924FASTOP2CL(shl);
925FASTOP2CL(shr);
926FASTOP2CL(sar);
927
928FASTOP2W(bsf);
929FASTOP2W(bsr);
930FASTOP2W(bt);
931FASTOP2W(bts);
932FASTOP2W(btr);
933FASTOP2W(btc);
934
Avi Kivitye47a5f52013-02-09 11:31:51 +0200935FASTOP2(xadd);
936
Nadav Amit5aca3722014-11-02 11:54:50 +0200937FASTOP2R(cmp, cmp_r);
938
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200939static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300940{
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200941 u8 rc;
942 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300943
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200944 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +0200945 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200946 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
947 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300948}
949
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300950static void fetch_register_operand(struct operand *op)
951{
952 switch (op->bytes) {
953 case 1:
954 op->val = *(u8 *)op->addr.reg;
955 break;
956 case 2:
957 op->val = *(u16 *)op->addr.reg;
958 break;
959 case 4:
960 op->val = *(u32 *)op->addr.reg;
961 break;
962 case 8:
963 op->val = *(u64 *)op->addr.reg;
964 break;
965 }
966}
967
Avi Kivity1253791d2011-03-29 11:41:27 +0200968static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
969{
970 ctxt->ops->get_fpu(ctxt);
971 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200972 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
973 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
974 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
975 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
976 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
977 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
978 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
979 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200980#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200981 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
982 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
983 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
984 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
985 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
986 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
987 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
988 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200989#endif
990 default: BUG();
991 }
992 ctxt->ops->put_fpu(ctxt);
993}
994
995static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
996 int reg)
997{
998 ctxt->ops->get_fpu(ctxt);
999 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001000 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
1001 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
1002 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
1003 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
1004 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
1005 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
1006 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
1007 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001008#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001009 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
1010 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
1011 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
1012 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
1013 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
1014 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
1015 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
1016 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001017#endif
1018 default: BUG();
1019 }
1020 ctxt->ops->put_fpu(ctxt);
1021}
1022
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001023static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1024{
1025 ctxt->ops->get_fpu(ctxt);
1026 switch (reg) {
1027 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1028 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1029 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1030 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1031 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1032 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1033 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1034 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1035 default: BUG();
1036 }
1037 ctxt->ops->put_fpu(ctxt);
1038}
1039
1040static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1041{
1042 ctxt->ops->get_fpu(ctxt);
1043 switch (reg) {
1044 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1045 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1046 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1047 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1048 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1049 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1050 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1051 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1052 default: BUG();
1053 }
1054 ctxt->ops->put_fpu(ctxt);
1055}
1056
Gleb Natapov045a2822012-12-20 16:57:43 +02001057static int em_fninit(struct x86_emulate_ctxt *ctxt)
1058{
1059 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1060 return emulate_nm(ctxt);
1061
1062 ctxt->ops->get_fpu(ctxt);
1063 asm volatile("fninit");
1064 ctxt->ops->put_fpu(ctxt);
1065 return X86EMUL_CONTINUE;
1066}
1067
1068static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1069{
1070 u16 fcw;
1071
1072 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1073 return emulate_nm(ctxt);
1074
1075 ctxt->ops->get_fpu(ctxt);
1076 asm volatile("fnstcw %0": "+m"(fcw));
1077 ctxt->ops->put_fpu(ctxt);
1078
Gleb Natapov045a2822012-12-20 16:57:43 +02001079 ctxt->dst.val = fcw;
1080
1081 return X86EMUL_CONTINUE;
1082}
1083
1084static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1085{
1086 u16 fsw;
1087
1088 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1089 return emulate_nm(ctxt);
1090
1091 ctxt->ops->get_fpu(ctxt);
1092 asm volatile("fnstsw %0": "+m"(fsw));
1093 ctxt->ops->put_fpu(ctxt);
1094
Gleb Natapov045a2822012-12-20 16:57:43 +02001095 ctxt->dst.val = fsw;
1096
1097 return X86EMUL_CONTINUE;
1098}
1099
Avi Kivity1253791d2011-03-29 11:41:27 +02001100static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001101 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001102{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001103 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001104
Avi Kivity9dac77f2011-06-01 15:34:25 +03001105 if (!(ctxt->d & ModRM))
1106 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity1253791d2011-03-29 11:41:27 +02001107
Avi Kivity9dac77f2011-06-01 15:34:25 +03001108 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001109 op->type = OP_XMM;
1110 op->bytes = 16;
1111 op->addr.xmm = reg;
1112 read_sse_reg(ctxt, &op->vec_val, reg);
1113 return;
1114 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001115 if (ctxt->d & Mmx) {
1116 reg &= 7;
1117 op->type = OP_MM;
1118 op->bytes = 8;
1119 op->addr.mm = reg;
1120 return;
1121 }
Avi Kivity1253791d2011-03-29 11:41:27 +02001122
Avi Kivity3c118e22007-10-31 10:27:04 +02001123 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001124 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1125 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1126
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001127 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001128 op->orig_val = op->val;
1129}
1130
Avi Kivitya6e34072012-06-10 17:15:39 +03001131static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1132{
1133 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1134 ctxt->modrm_seg = VCPU_SREG_SS;
1135}
1136
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001137static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001138 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001139{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001140 u8 sib;
Bandan Das02357bd2014-04-16 12:46:11 -04001141 int index_reg, base_reg, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001142 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001143 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001144
Bandan Das02357bd2014-04-16 12:46:11 -04001145 ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */
1146 index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */
1147 base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001148
Bandan Das02357bd2014-04-16 12:46:11 -04001149 ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001150 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
Bandan Das02357bd2014-04-16 12:46:11 -04001151 ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001152 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001153
Nadav Amit9b88ae92014-05-25 23:05:21 +03001154 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001155 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001156 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001157 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001158 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001159 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001160 op->type = OP_XMM;
1161 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001162 op->addr.xmm = ctxt->modrm_rm;
1163 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001164 return rc;
1165 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001166 if (ctxt->d & Mmx) {
1167 op->type = OP_MM;
1168 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001169 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001170 return rc;
1171 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001172 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001173 return rc;
1174 }
1175
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001176 op->type = OP_MEM;
1177
Avi Kivity9dac77f2011-06-01 15:34:25 +03001178 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001179 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1180 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1181 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1182 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001183
1184 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001185 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001186 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001187 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001188 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001189 break;
1190 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001191 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001192 break;
1193 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001194 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001195 break;
1196 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001197 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001198 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001199 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001200 break;
1201 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001202 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001203 break;
1204 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001205 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001206 break;
1207 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001208 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001209 break;
1210 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001211 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001212 break;
1213 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001214 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001215 break;
1216 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001217 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001218 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001219 break;
1220 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001221 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001222 break;
1223 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001224 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1225 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1226 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001227 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001228 } else {
1229 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001230 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001231 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001232 index_reg |= (sib >> 3) & 7;
1233 base_reg |= sib & 7;
1234 scale = sib >> 6;
1235
Avi Kivity9dac77f2011-06-01 15:34:25 +03001236 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001237 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001238 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001239 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001240 adjust_modrm_seg(ctxt, base_reg);
Nadav Amitab708092014-12-25 02:52:21 +02001241 /* Increment ESP on POP [ESP] */
1242 if ((ctxt->d & IncSP) &&
1243 base_reg == VCPU_REGS_RSP)
1244 modrm_ea += ctxt->op_bytes;
Avi Kivitya6e34072012-06-10 17:15:39 +03001245 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001246 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001247 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001248 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Nadav Amit5b38ab82014-11-02 11:54:41 +02001249 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity84411d82008-06-15 21:53:26 -07001250 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001251 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001252 } else {
1253 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001254 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001255 adjust_modrm_seg(ctxt, base_reg);
1256 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001257 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001258 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001259 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001260 break;
1261 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001262 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001263 break;
1264 }
1265 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001266 op->addr.mem.ea = modrm_ea;
Bandan Das41061cd2014-04-16 12:46:14 -04001267 if (ctxt->ad_bytes != 8)
1268 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
1269
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001270done:
1271 return rc;
1272}
1273
1274static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001275 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001276{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001277 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001278
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001279 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001280 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001281 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001282 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001283 break;
1284 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001285 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001286 break;
1287 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001288 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001289 break;
1290 }
1291done:
1292 return rc;
1293}
1294
Avi Kivity9dac77f2011-06-01 15:34:25 +03001295static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001296{
Sheng Yang7129eec2010-09-28 16:33:32 +08001297 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001298
Avi Kivity9dac77f2011-06-01 15:34:25 +03001299 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001300 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001301
Avi Kivity9dac77f2011-06-01 15:34:25 +03001302 if (ctxt->src.bytes == 2)
1303 sv = (s16)ctxt->src.val & (s16)mask;
1304 else if (ctxt->src.bytes == 4)
1305 sv = (s32)ctxt->src.val & (s32)mask;
Nadav Amit7dec5602014-06-15 16:12:57 +03001306 else
1307 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001308
Nadav Amit1c1c35a2014-11-19 17:43:09 +02001309 ctxt->dst.addr.mem.ea = address_mask(ctxt,
1310 ctxt->dst.addr.mem.ea + (sv >> 3));
Wei Yongjun35c843c2010-08-09 11:34:56 +08001311 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001312
1313 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001314 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001315}
1316
Gleb Natapov9de41572010-04-28 19:15:22 +03001317static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001318 unsigned long addr, void *dest, unsigned size)
1319{
1320 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001321 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001322
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001323 if (mc->pos < mc->end)
1324 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001325
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001326 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001327
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001328 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1329 &ctxt->exception);
1330 if (rc != X86EMUL_CONTINUE)
1331 return rc;
1332
1333 mc->end += size;
1334
1335read_cached:
1336 memcpy(dest, mc->data + mc->pos, size);
1337 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001338 return X86EMUL_CONTINUE;
1339}
1340
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001341static int segmented_read(struct x86_emulate_ctxt *ctxt,
1342 struct segmented_address addr,
1343 void *data,
1344 unsigned size)
1345{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001346 int rc;
1347 ulong linear;
1348
Avi Kivity83b87952011-04-03 11:31:19 +03001349 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001350 if (rc != X86EMUL_CONTINUE)
1351 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001352 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001353}
1354
1355static int segmented_write(struct x86_emulate_ctxt *ctxt,
1356 struct segmented_address addr,
1357 const void *data,
1358 unsigned size)
1359{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001360 int rc;
1361 ulong linear;
1362
Avi Kivity83b87952011-04-03 11:31:19 +03001363 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001364 if (rc != X86EMUL_CONTINUE)
1365 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001366 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1367 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001368}
1369
1370static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1371 struct segmented_address addr,
1372 const void *orig_data, const void *data,
1373 unsigned size)
1374{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001375 int rc;
1376 ulong linear;
1377
Avi Kivity83b87952011-04-03 11:31:19 +03001378 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001379 if (rc != X86EMUL_CONTINUE)
1380 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001381 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1382 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001383}
1384
Gleb Natapov7b262e92010-03-18 15:20:27 +02001385static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001386 unsigned int size, unsigned short port,
1387 void *dest)
1388{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001389 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001390
1391 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001392 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001393 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001394 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001395 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001396 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1397 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Mark Rustadb55a8142014-07-25 06:27:05 -07001398 n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001399 if (n == 0)
1400 n = 1;
1401 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001402 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001403 return 0;
1404 rc->end = n * size;
1405 }
1406
Nadav Amite6e39f02014-04-18 03:35:10 +03001407 if (ctxt->rep_prefix && (ctxt->d & String) &&
1408 !(ctxt->eflags & EFLG_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001409 ctxt->dst.data = rc->data + rc->pos;
1410 ctxt->dst.type = OP_MEM_STR;
1411 ctxt->dst.count = (rc->end - rc->pos) / size;
1412 rc->pos = rc->end;
1413 } else {
1414 memcpy(dest, rc->data + rc->pos, size);
1415 rc->pos += size;
1416 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001417 return 1;
1418}
1419
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001420static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1421 u16 index, struct desc_struct *desc)
1422{
1423 struct desc_ptr dt;
1424 ulong addr;
1425
1426 ctxt->ops->get_idt(ctxt, &dt);
1427
1428 if (dt.size < index * 8 + 7)
1429 return emulate_gp(ctxt, index << 3 | 0x2);
1430
1431 addr = dt.address + index * 8;
1432 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1433 &ctxt->exception);
1434}
1435
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001436static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001437 u16 selector, struct desc_ptr *dt)
1438{
Mathias Krause0225fb52012-08-30 01:30:16 +02001439 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001440 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001441
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001442 if (selector & 1 << 2) {
1443 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001444 u16 sel;
1445
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001446 memset (dt, 0, sizeof *dt);
Nadav Amit2eedcac2014-06-02 18:34:05 +03001447 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1448 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001449 return;
1450
1451 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001452 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001453 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001454 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001455}
1456
Nadav Amitedccda72014-12-25 02:52:23 +02001457static int get_descriptor_ptr(struct x86_emulate_ctxt *ctxt,
1458 u16 selector, ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001459{
1460 struct desc_ptr dt;
1461 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001462 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001463
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001464 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001465
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001466 if (dt.size < index * 8 + 7)
1467 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001468
1469 addr = dt.address + index * 8;
Nadav Amitedccda72014-12-25 02:52:23 +02001470
1471#ifdef CONFIG_X86_64
1472 if (addr >> 32 != 0) {
1473 u64 efer = 0;
1474
1475 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1476 if (!(efer & EFER_LMA))
1477 addr &= (u32)-1;
1478 }
1479#endif
1480
1481 *desc_addr_p = addr;
1482 return X86EMUL_CONTINUE;
1483}
1484
1485/* allowed just for 8 bytes segments */
1486static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1487 u16 selector, struct desc_struct *desc,
1488 ulong *desc_addr_p)
1489{
1490 int rc;
1491
1492 rc = get_descriptor_ptr(ctxt, selector, desc_addr_p);
1493 if (rc != X86EMUL_CONTINUE)
1494 return rc;
1495
1496 return ctxt->ops->read_std(ctxt, *desc_addr_p, desc, sizeof(*desc),
1497 &ctxt->exception);
1498}
1499
1500/* allowed just for 8 bytes segments */
1501static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1502 u16 selector, struct desc_struct *desc)
1503{
1504 int rc;
1505 ulong addr;
1506
1507 rc = get_descriptor_ptr(ctxt, selector, &addr);
1508 if (rc != X86EMUL_CONTINUE)
1509 return rc;
1510
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001511 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1512 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001513}
1514
Gleb Natapov5601d052011-03-07 14:55:06 +02001515/* Does not support long mode */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001516static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Nadav Amitd1442d82014-09-18 22:39:39 +03001517 u16 selector, int seg, u8 cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001518 enum x86_transfer_type transfer,
Nadav Amitd1442d82014-09-18 22:39:39 +03001519 struct desc_struct *desc)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001520{
Avi Kivity869be992012-06-13 16:30:53 +03001521 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001522 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001523 unsigned err_vec = GP_VECTOR;
1524 u32 err_code = 0;
1525 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001526 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001527 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001528 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001529 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001530
1531 memset(&seg_desc, 0, sizeof seg_desc);
1532
Kevin Wolff8da94e2013-04-11 14:06:03 +02001533 if (ctxt->mode == X86EMUL_MODE_REAL) {
1534 /* set real mode segment descriptor (keep limit etc. for
1535 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001536 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001537 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001538 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001539 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1540 /* VM86 needs a clean new segment descriptor */
1541 set_desc_base(&seg_desc, selector << 4);
1542 set_desc_limit(&seg_desc, 0xffff);
1543 seg_desc.type = 3;
1544 seg_desc.p = 1;
1545 seg_desc.s = 1;
1546 seg_desc.dpl = 3;
1547 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001548 }
1549
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001550 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001551
1552 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1553 if ((seg == VCPU_SREG_CS
1554 || (seg == VCPU_SREG_SS
1555 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1556 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001557 && null_selector)
1558 goto exception;
1559
1560 /* TR should be in GDT only */
1561 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1562 goto exception;
1563
1564 if (null_selector) /* for NULL selector skip all following checks */
1565 goto load;
1566
Avi Kivitye9194642012-06-13 16:29:39 +03001567 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001568 if (ret != X86EMUL_CONTINUE)
1569 return ret;
1570
1571 err_code = selector & 0xfffc;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001572 err_vec = (transfer == X86_TRANSFER_TASK_SWITCH) ? TS_VECTOR :
1573 GP_VECTOR;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001574
Guo Chaofc058682012-06-28 15:19:51 +08001575 /* can't load system descriptor into segment selector */
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001576 if (seg <= VCPU_SREG_GS && !seg_desc.s) {
1577 if (transfer == X86_TRANSFER_CALL_JMP)
1578 return X86EMUL_UNHANDLEABLE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001579 goto exception;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001580 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001581
1582 if (!seg_desc.p) {
1583 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1584 goto exception;
1585 }
1586
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001587 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001588
1589 switch (seg) {
1590 case VCPU_SREG_SS:
1591 /*
1592 * segment is not a writable data segment or segment
1593 * selector's RPL != CPL or segment selector's RPL != CPL
1594 */
1595 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1596 goto exception;
1597 break;
1598 case VCPU_SREG_CS:
1599 if (!(seg_desc.type & 8))
1600 goto exception;
1601
1602 if (seg_desc.type & 4) {
1603 /* conforming */
1604 if (dpl > cpl)
1605 goto exception;
1606 } else {
1607 /* nonconforming */
1608 if (rpl > cpl || dpl != cpl)
1609 goto exception;
1610 }
Nadav Amit040c8dc2014-09-18 22:39:43 +03001611 /* in long-mode d/b must be clear if l is set */
1612 if (seg_desc.d && seg_desc.l) {
1613 u64 efer = 0;
1614
1615 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1616 if (efer & EFER_LMA)
1617 goto exception;
1618 }
1619
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001620 /* CS(RPL) <- CPL */
1621 selector = (selector & 0xfffc) | cpl;
1622 break;
1623 case VCPU_SREG_TR:
1624 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1625 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001626 old_desc = seg_desc;
1627 seg_desc.type |= 2; /* busy */
1628 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1629 sizeof(seg_desc), &ctxt->exception);
1630 if (ret != X86EMUL_CONTINUE)
1631 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001632 break;
1633 case VCPU_SREG_LDTR:
1634 if (seg_desc.s || seg_desc.type != 2)
1635 goto exception;
1636 break;
1637 default: /* DS, ES, FS, or GS */
1638 /*
1639 * segment is not a data or readable code segment or
1640 * ((segment is a data or nonconforming code segment)
1641 * and (both RPL and CPL > DPL))
1642 */
1643 if ((seg_desc.type & 0xa) == 0x8 ||
1644 (((seg_desc.type & 0xc) != 0xc) &&
1645 (rpl > dpl && cpl > dpl)))
1646 goto exception;
1647 break;
1648 }
1649
1650 if (seg_desc.s) {
1651 /* mark segment as accessed */
Nadav Amite2cefa72014-12-25 02:52:22 +02001652 if (!(seg_desc.type & 1)) {
1653 seg_desc.type |= 1;
1654 ret = write_segment_descriptor(ctxt, selector,
1655 &seg_desc);
1656 if (ret != X86EMUL_CONTINUE)
1657 return ret;
1658 }
Nadav Amite37a75a2014-06-02 18:34:04 +03001659 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
1660 ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3,
1661 sizeof(base3), &ctxt->exception);
1662 if (ret != X86EMUL_CONTINUE)
1663 return ret;
Nadav Amit9a9abf62014-11-02 11:54:56 +02001664 if (is_noncanonical_address(get_desc_base(&seg_desc) |
1665 ((u64)base3 << 32)))
1666 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001667 }
1668load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001669 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Nadav Amitd1442d82014-09-18 22:39:39 +03001670 if (desc)
1671 *desc = seg_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001672 return X86EMUL_CONTINUE;
1673exception:
Paolo Bonzini592f0852014-08-20 10:05:08 +02001674 return emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001675}
1676
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001677static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1678 u16 selector, int seg)
1679{
1680 u8 cpl = ctxt->ops->cpl(ctxt);
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001681 return __load_segment_descriptor(ctxt, selector, seg, cpl,
1682 X86_TRANSFER_NONE, NULL);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001683}
1684
Wei Yongjun31be40b2010-08-17 09:17:30 +08001685static void write_register_operand(struct operand *op)
1686{
1687 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1688 switch (op->bytes) {
1689 case 1:
1690 *(u8 *)op->addr.reg = (u8)op->val;
1691 break;
1692 case 2:
1693 *(u16 *)op->addr.reg = (u16)op->val;
1694 break;
1695 case 4:
1696 *op->addr.reg = (u32)op->val;
1697 break; /* 64b: zero-extend */
1698 case 8:
1699 *op->addr.reg = op->val;
1700 break;
1701 }
1702}
1703
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001704static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001705{
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001706 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001707 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001708 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001709 break;
1710 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001711 if (ctxt->lock_prefix)
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001712 return segmented_cmpxchg(ctxt,
1713 op->addr.mem,
1714 &op->orig_val,
1715 &op->val,
1716 op->bytes);
1717 else
1718 return segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001719 op->addr.mem,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001720 &op->val,
1721 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001722 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001723 case OP_MEM_STR:
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001724 return segmented_write(ctxt,
1725 op->addr.mem,
1726 op->data,
1727 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001728 break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001729 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001730 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001731 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001732 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001733 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001734 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001735 case OP_NONE:
1736 /* no writeback */
1737 break;
1738 default:
1739 break;
1740 }
1741 return X86EMUL_CONTINUE;
1742}
1743
Avi Kivity51ddff52012-06-12 20:19:40 +03001744static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001745{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001746 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001747
Avi Kivity5ad105e2012-08-19 14:34:31 +03001748 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001749 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001750 addr.seg = VCPU_SREG_SS;
1751
Avi Kivity51ddff52012-06-12 20:19:40 +03001752 return segmented_write(ctxt, addr, data, bytes);
1753}
1754
1755static int em_push(struct x86_emulate_ctxt *ctxt)
1756{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001757 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001758 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001759 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001760}
1761
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001762static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001763 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001764{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001765 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001766 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001767
Avi Kivitydd856ef2012-08-27 23:46:17 +03001768 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001769 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001770 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001771 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001772 return rc;
1773
Avi Kivity5ad105e2012-08-19 14:34:31 +03001774 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001775 return rc;
1776}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001777
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001778static int em_pop(struct x86_emulate_ctxt *ctxt)
1779{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001780 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001781}
1782
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001783static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001784 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001785{
1786 int rc;
1787 unsigned long val, change_mask;
1788 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001789 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001790
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001791 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001792 if (rc != X86EMUL_CONTINUE)
1793 return rc;
1794
1795 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
Nadav Amit163b1352014-07-21 14:37:28 +03001796 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_AC | EFLG_ID;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001797
1798 switch(ctxt->mode) {
1799 case X86EMUL_MODE_PROT64:
1800 case X86EMUL_MODE_PROT32:
1801 case X86EMUL_MODE_PROT16:
1802 if (cpl == 0)
1803 change_mask |= EFLG_IOPL;
1804 if (cpl <= iopl)
1805 change_mask |= EFLG_IF;
1806 break;
1807 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001808 if (iopl < 3)
1809 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001810 change_mask |= EFLG_IF;
1811 break;
1812 default: /* real mode */
1813 change_mask |= (EFLG_IOPL | EFLG_IF);
1814 break;
1815 }
1816
1817 *(unsigned long *)dest =
1818 (ctxt->eflags & ~change_mask) | (val & change_mask);
1819
1820 return rc;
1821}
1822
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001823static int em_popf(struct x86_emulate_ctxt *ctxt)
1824{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001825 ctxt->dst.type = OP_REG;
1826 ctxt->dst.addr.reg = &ctxt->eflags;
1827 ctxt->dst.bytes = ctxt->op_bytes;
1828 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001829}
1830
Avi Kivity612e89f2012-06-12 20:03:23 +03001831static int em_enter(struct x86_emulate_ctxt *ctxt)
1832{
1833 int rc;
1834 unsigned frame_size = ctxt->src.val;
1835 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001836 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001837
1838 if (nesting_level)
1839 return X86EMUL_UNHANDLEABLE;
1840
Avi Kivitydd856ef2012-08-27 23:46:17 +03001841 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1842 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001843 if (rc != X86EMUL_CONTINUE)
1844 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001845 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001846 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001847 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1848 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001849 stack_mask(ctxt));
1850 return X86EMUL_CONTINUE;
1851}
1852
Avi Kivityf47cfa32012-06-07 17:49:24 +03001853static int em_leave(struct x86_emulate_ctxt *ctxt)
1854{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001855 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001856 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001857 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001858}
1859
Avi Kivity1cd196e2011-09-13 10:45:51 +03001860static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001861{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001862 int seg = ctxt->src2.val;
1863
Avi Kivity9dac77f2011-06-01 15:34:25 +03001864 ctxt->src.val = get_segment_selector(ctxt, seg);
Nadav Amit0fcc2072014-11-02 11:54:51 +02001865 if (ctxt->op_bytes == 4) {
1866 rsp_increment(ctxt, -2);
1867 ctxt->op_bytes = 2;
1868 }
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001869
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001870 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001871}
1872
Avi Kivity1cd196e2011-09-13 10:45:51 +03001873static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001874{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001875 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001876 unsigned long selector;
1877 int rc;
1878
Nadav Amit3313bc42014-12-25 02:52:17 +02001879 rc = emulate_pop(ctxt, &selector, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001880 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001881 return rc;
1882
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001883 if (ctxt->modrm_reg == VCPU_SREG_SS)
1884 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Nadav Amit3313bc42014-12-25 02:52:17 +02001885 if (ctxt->op_bytes > 2)
1886 rsp_increment(ctxt, ctxt->op_bytes - 2);
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001887
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001888 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001889 return rc;
1890}
1891
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001892static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001893{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001894 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001895 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001896 int reg = VCPU_REGS_RAX;
1897
1898 while (reg <= VCPU_REGS_RDI) {
1899 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001900 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001901
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001902 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001903 if (rc != X86EMUL_CONTINUE)
1904 return rc;
1905
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001906 ++reg;
1907 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001908
Wei Yongjunc37eda12010-06-15 09:03:33 +08001909 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001910}
1911
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001912static int em_pushf(struct x86_emulate_ctxt *ctxt)
1913{
Nadav Amitbc397a62014-12-10 11:19:03 +02001914 ctxt->src.val = (unsigned long)ctxt->eflags & ~EFLG_VM;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001915 return em_push(ctxt);
1916}
1917
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001918static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001919{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001920 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001921 int reg = VCPU_REGS_RDI;
1922
1923 while (reg >= VCPU_REGS_RAX) {
1924 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001925 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001926 --reg;
1927 }
1928
Avi Kivitydd856ef2012-08-27 23:46:17 +03001929 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001930 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001931 break;
1932 --reg;
1933 }
1934 return rc;
1935}
1936
Avi Kivitydd856ef2012-08-27 23:46:17 +03001937static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001938{
Mathias Krause0225fb52012-08-30 01:30:16 +02001939 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001940 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001941 struct desc_ptr dt;
1942 gva_t cs_addr;
1943 gva_t eip_addr;
1944 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001945
1946 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001947 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001948 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001949 if (rc != X86EMUL_CONTINUE)
1950 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001951
1952 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1953
Avi Kivity9dac77f2011-06-01 15:34:25 +03001954 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001955 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001956 if (rc != X86EMUL_CONTINUE)
1957 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001958
Avi Kivity9dac77f2011-06-01 15:34:25 +03001959 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001960 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001961 if (rc != X86EMUL_CONTINUE)
1962 return rc;
1963
Avi Kivity4bff1e862011-04-20 13:37:53 +03001964 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001965
1966 eip_addr = dt.address + (irq << 2);
1967 cs_addr = dt.address + (irq << 2) + 2;
1968
Avi Kivity0f65dd72011-04-20 13:37:53 +03001969 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001970 if (rc != X86EMUL_CONTINUE)
1971 return rc;
1972
Avi Kivity0f65dd72011-04-20 13:37:53 +03001973 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001974 if (rc != X86EMUL_CONTINUE)
1975 return rc;
1976
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001977 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001978 if (rc != X86EMUL_CONTINUE)
1979 return rc;
1980
Avi Kivity9dac77f2011-06-01 15:34:25 +03001981 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001982
1983 return rc;
1984}
1985
Avi Kivitydd856ef2012-08-27 23:46:17 +03001986int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1987{
1988 int rc;
1989
1990 invalidate_registers(ctxt);
1991 rc = __emulate_int_real(ctxt, irq);
1992 if (rc == X86EMUL_CONTINUE)
1993 writeback_registers(ctxt);
1994 return rc;
1995}
1996
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001997static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001998{
1999 switch(ctxt->mode) {
2000 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03002001 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002002 case X86EMUL_MODE_VM86:
2003 case X86EMUL_MODE_PROT16:
2004 case X86EMUL_MODE_PROT32:
2005 case X86EMUL_MODE_PROT64:
2006 default:
2007 /* Protected mode interrupts unimplemented yet */
2008 return X86EMUL_UNHANDLEABLE;
2009 }
2010}
2011
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002012static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002013{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002014 int rc = X86EMUL_CONTINUE;
2015 unsigned long temp_eip = 0;
2016 unsigned long temp_eflags = 0;
2017 unsigned long cs = 0;
2018 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
2019 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
2020 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
2021 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
2022
2023 /* TODO: Add stack limit check */
2024
Avi Kivity9dac77f2011-06-01 15:34:25 +03002025 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002026
2027 if (rc != X86EMUL_CONTINUE)
2028 return rc;
2029
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002030 if (temp_eip & ~0xffff)
2031 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002032
Avi Kivity9dac77f2011-06-01 15:34:25 +03002033 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002034
2035 if (rc != X86EMUL_CONTINUE)
2036 return rc;
2037
Avi Kivity9dac77f2011-06-01 15:34:25 +03002038 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002039
2040 if (rc != X86EMUL_CONTINUE)
2041 return rc;
2042
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002043 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002044
2045 if (rc != X86EMUL_CONTINUE)
2046 return rc;
2047
Avi Kivity9dac77f2011-06-01 15:34:25 +03002048 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002049
2050
Avi Kivity9dac77f2011-06-01 15:34:25 +03002051 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002052 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03002053 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002054 ctxt->eflags &= ~0xffff;
2055 ctxt->eflags |= temp_eflags;
2056 }
2057
2058 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
2059 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
Nadav Amit801806d2015-01-26 09:32:23 +02002060 ctxt->ops->set_nmi_mask(ctxt, false);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002061
2062 return rc;
2063}
2064
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002065static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002066{
2067 switch(ctxt->mode) {
2068 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002069 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002070 case X86EMUL_MODE_VM86:
2071 case X86EMUL_MODE_PROT16:
2072 case X86EMUL_MODE_PROT32:
2073 case X86EMUL_MODE_PROT64:
2074 default:
2075 /* iret from protected mode unimplemented yet */
2076 return X86EMUL_UNHANDLEABLE;
2077 }
2078}
2079
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002080static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2081{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002082 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002083 unsigned short sel, old_sel;
2084 struct desc_struct old_desc, new_desc;
2085 const struct x86_emulate_ops *ops = ctxt->ops;
2086 u8 cpl = ctxt->ops->cpl(ctxt);
2087
2088 /* Assignment of RIP may only fail in 64-bit mode */
2089 if (ctxt->mode == X86EMUL_MODE_PROT64)
2090 ops->get_segment(ctxt, &old_sel, &old_desc, NULL,
2091 VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002092
Avi Kivity9dac77f2011-06-01 15:34:25 +03002093 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002094
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002095 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
2096 X86_TRANSFER_CALL_JMP,
Nadav Amitd1442d82014-09-18 22:39:39 +03002097 &new_desc);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002098 if (rc != X86EMUL_CONTINUE)
2099 return rc;
2100
Nadav Amitd50eaa12014-11-19 17:43:11 +02002101 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03002102 if (rc != X86EMUL_CONTINUE) {
Nadav Amit7e46ddd2014-10-28 00:03:43 +02002103 WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64);
Nadav Amitd1442d82014-09-18 22:39:39 +03002104 /* assigning eip failed; restore the old cs */
2105 ops->set_segment(ctxt, old_sel, &old_desc, 0, VCPU_SREG_CS);
2106 return rc;
2107 }
2108 return rc;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002109}
2110
Nadav Amitf7784042014-09-18 22:39:41 +03002111static int em_jmp_abs(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002112{
Nadav Amitf7784042014-09-18 22:39:41 +03002113 return assign_eip_near(ctxt, ctxt->src.val);
2114}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002115
Nadav Amitf7784042014-09-18 22:39:41 +03002116static int em_call_near_abs(struct x86_emulate_ctxt *ctxt)
2117{
2118 int rc;
2119 long int old_eip;
2120
2121 old_eip = ctxt->_eip;
2122 rc = assign_eip_near(ctxt, ctxt->src.val);
2123 if (rc != X86EMUL_CONTINUE)
2124 return rc;
2125 ctxt->src.val = old_eip;
2126 rc = em_push(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002127 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002128}
2129
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002130static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002131{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002132 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002133
Nadav Amitaaa05f22014-06-02 18:34:10 +03002134 if (ctxt->dst.bytes == 16)
2135 return X86EMUL_UNHANDLEABLE;
2136
Avi Kivitydd856ef2012-08-27 23:46:17 +03002137 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2138 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2139 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2140 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002141 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002142 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002143 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2144 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002145
Laurent Vivier05f086f2007-09-24 11:10:55 +02002146 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002147 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002148 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002149}
2150
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002151static int em_ret(struct x86_emulate_ctxt *ctxt)
2152{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002153 int rc;
2154 unsigned long eip;
2155
2156 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
2157 if (rc != X86EMUL_CONTINUE)
2158 return rc;
2159
2160 return assign_eip_near(ctxt, eip);
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002161}
2162
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002163static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002164{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002165 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002166 unsigned long eip, cs;
2167 u16 old_cs;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002168 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amitd1442d82014-09-18 22:39:39 +03002169 struct desc_struct old_desc, new_desc;
2170 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002171
Nadav Amitd1442d82014-09-18 22:39:39 +03002172 if (ctxt->mode == X86EMUL_MODE_PROT64)
2173 ops->get_segment(ctxt, &old_cs, &old_desc, NULL,
2174 VCPU_SREG_CS);
2175
2176 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002177 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002178 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002179 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002180 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002181 return rc;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002182 /* Outer-privilege level return is not implemented */
2183 if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2184 return X86EMUL_UNHANDLEABLE;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002185 rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, cpl,
2186 X86_TRANSFER_RET,
Nadav Amitd1442d82014-09-18 22:39:39 +03002187 &new_desc);
2188 if (rc != X86EMUL_CONTINUE)
2189 return rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +02002190 rc = assign_eip_far(ctxt, eip, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03002191 if (rc != X86EMUL_CONTINUE) {
Nadav Amit7e46ddd2014-10-28 00:03:43 +02002192 WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64);
Nadav Amitd1442d82014-09-18 22:39:39 +03002193 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
2194 }
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002195 return rc;
2196}
2197
Bruce Rogers32611072013-09-09 09:40:20 -06002198static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2199{
2200 int rc;
2201
2202 rc = em_ret_far(ctxt);
2203 if (rc != X86EMUL_CONTINUE)
2204 return rc;
2205 rsp_increment(ctxt, ctxt->src.val);
2206 return X86EMUL_CONTINUE;
2207}
2208
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002209static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2210{
2211 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002212 ctxt->dst.orig_val = ctxt->dst.val;
2213 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002214 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002215 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002216 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002217
2218 if (ctxt->eflags & EFLG_ZF) {
Nadav Amit2fcf5c82015-01-26 09:32:21 +02002219 /* Success: write back to memory; no update of EAX */
2220 ctxt->src.type = OP_NONE;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002221 ctxt->dst.val = ctxt->src.orig_val;
2222 } else {
2223 /* Failure: write the value we saw to EAX. */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02002224 ctxt->src.type = OP_REG;
2225 ctxt->src.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
2226 ctxt->src.val = ctxt->dst.orig_val;
2227 /* Create write-cycle to dest by writing the same value */
Nadav Amit37c564f2014-06-02 18:34:07 +03002228 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002229 }
2230 return X86EMUL_CONTINUE;
2231}
2232
Avi Kivityd4b43252011-09-13 10:45:50 +03002233static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002234{
Avi Kivityd4b43252011-09-13 10:45:50 +03002235 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002236 unsigned short sel;
2237 int rc;
2238
Avi Kivity9dac77f2011-06-01 15:34:25 +03002239 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002240
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002241 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002242 if (rc != X86EMUL_CONTINUE)
2243 return rc;
2244
Avi Kivity9dac77f2011-06-01 15:34:25 +03002245 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002246 return rc;
2247}
2248
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002249static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002250setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002251 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002252{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002253 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002254 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002255 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002256 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002257 cs->type = 0x0b; /* Read, Execute, Accessed */
2258 cs->s = 1;
2259 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002260 cs->p = 1;
2261 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002262 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002263
Gleb Natapov79168fd2010-04-28 19:15:30 +03002264 set_desc_base(ss, 0); /* flat segment */
2265 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002266 ss->g = 1; /* 4kb granularity */
2267 ss->s = 1;
2268 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002269 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002270 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002271 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002272 ss->l = 0;
2273 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002274}
2275
Avi Kivity1a18a692012-02-01 12:23:21 +02002276static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2277{
2278 u32 eax, ebx, ecx, edx;
2279
2280 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002281 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2282 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002283 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2284 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2285}
2286
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002287static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2288{
Mathias Krause0225fb52012-08-30 01:30:16 +02002289 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002290 u32 eax, ebx, ecx, edx;
2291
2292 /*
2293 * syscall should always be enabled in longmode - so only become
2294 * vendor specific (cpuid) if other modes are active...
2295 */
2296 if (ctxt->mode == X86EMUL_MODE_PROT64)
2297 return true;
2298
2299 eax = 0x00000000;
2300 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002301 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2302 /*
2303 * Intel ("GenuineIntel")
2304 * remark: Intel CPUs only support "syscall" in 64bit
2305 * longmode. Also an 64bit guest with a
2306 * 32bit compat-app running will #UD !! While this
2307 * behaviour can be fixed (by emulating) into AMD
2308 * response - CPUs of AMD can't behave like Intel.
2309 */
2310 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2311 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2312 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2313 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002314
Avi Kivity0017f932012-06-07 14:10:16 +03002315 /* AMD ("AuthenticAMD") */
2316 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2317 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2318 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2319 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002320
Avi Kivity0017f932012-06-07 14:10:16 +03002321 /* AMD ("AMDisbetter!") */
2322 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2323 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2324 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2325 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002326
2327 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2328 return false;
2329}
2330
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002331static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002332{
Mathias Krause0225fb52012-08-30 01:30:16 +02002333 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002334 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002335 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002336 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002337 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002338
2339 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002340 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002341 ctxt->mode == X86EMUL_MODE_VM86)
2342 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002343
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002344 if (!(em_syscall_is_enabled(ctxt)))
2345 return emulate_ud(ctxt);
2346
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002347 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002348 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002349
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002350 if (!(efer & EFER_SCE))
2351 return emulate_ud(ctxt);
2352
Avi Kivity717746e2011-04-20 13:37:53 +03002353 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002354 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002355 cs_sel = (u16)(msr_data & 0xfffc);
2356 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002357
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002358 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002359 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002360 cs.l = 1;
2361 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002362 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2363 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002364
Avi Kivitydd856ef2012-08-27 23:46:17 +03002365 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002366 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002367#ifdef CONFIG_X86_64
Nadav Amit6c6cb692014-07-21 14:37:30 +03002368 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002369
Avi Kivity717746e2011-04-20 13:37:53 +03002370 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002371 ctxt->mode == X86EMUL_MODE_PROT64 ?
2372 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002373 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002374
Avi Kivity717746e2011-04-20 13:37:53 +03002375 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Nadav Amit6c6cb692014-07-21 14:37:30 +03002376 ctxt->eflags &= ~msr_data;
Nadav Amit807c1422014-11-02 11:54:49 +02002377 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002378#endif
2379 } else {
2380 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002381 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002382 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002383
Nadav Amit6c6cb692014-07-21 14:37:30 +03002384 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002385 }
2386
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002387 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002388}
2389
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002390static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002391{
Mathias Krause0225fb52012-08-30 01:30:16 +02002392 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002393 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002394 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002395 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002396 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002397
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002398 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002399 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002400 if (ctxt->mode == X86EMUL_MODE_REAL)
2401 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002402
Avi Kivity1a18a692012-02-01 12:23:21 +02002403 /*
2404 * Not recognized on AMD in compat mode (but is recognized in legacy
2405 * mode).
2406 */
2407 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2408 && !vendor_intel(ctxt))
2409 return emulate_ud(ctxt);
2410
Nadav Amitb2c9d432014-11-02 11:55:01 +02002411 /* sysenter/sysexit have not been tested in 64bit mode. */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002412 if (ctxt->mode == X86EMUL_MODE_PROT64)
Nadav Amitb2c9d432014-11-02 11:55:01 +02002413 return X86EMUL_UNHANDLEABLE;
Andre Przywara8c604352009-06-18 12:56:01 +02002414
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002415 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002416
Avi Kivity717746e2011-04-20 13:37:53 +03002417 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002418 switch (ctxt->mode) {
2419 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002420 if ((msr_data & 0xfffc) == 0x0)
2421 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002422 break;
2423 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002424 if (msr_data == 0x0)
2425 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002426 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002427 default:
2428 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002429 }
2430
Nadav Amit6c6cb692014-07-21 14:37:30 +03002431 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002432 cs_sel = (u16)msr_data;
2433 cs_sel &= ~SELECTOR_RPL_MASK;
2434 ss_sel = cs_sel + 8;
2435 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002436 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002437 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002438 cs.l = 1;
2439 }
2440
Avi Kivity1aa36612011-04-27 13:20:30 +03002441 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2442 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002443
Avi Kivity717746e2011-04-20 13:37:53 +03002444 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002445 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002446
Avi Kivity717746e2011-04-20 13:37:53 +03002447 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002448 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002449
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002450 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002451}
2452
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002453static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002454{
Mathias Krause0225fb52012-08-30 01:30:16 +02002455 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002456 struct desc_struct cs, ss;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002457 u64 msr_data, rcx, rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002458 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002459 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002460
Gleb Natapova0044752010-02-10 14:21:31 +02002461 /* inject #GP if in real mode or Virtual 8086 mode */
2462 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002463 ctxt->mode == X86EMUL_MODE_VM86)
2464 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002465
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002466 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002467
Avi Kivity9dac77f2011-06-01 15:34:25 +03002468 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002469 usermode = X86EMUL_MODE_PROT64;
2470 else
2471 usermode = X86EMUL_MODE_PROT32;
2472
Nadav Amit234f3ce2014-09-18 22:39:38 +03002473 rcx = reg_read(ctxt, VCPU_REGS_RCX);
2474 rdx = reg_read(ctxt, VCPU_REGS_RDX);
2475
Andre Przywara4668f052009-06-18 12:56:02 +02002476 cs.dpl = 3;
2477 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002478 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002479 switch (usermode) {
2480 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002481 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002482 if ((msr_data & 0xfffc) == 0x0)
2483 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002484 ss_sel = (u16)(msr_data + 24);
Nadav Amitbf0b6822014-09-18 22:39:45 +03002485 rcx = (u32)rcx;
2486 rdx = (u32)rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002487 break;
2488 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002489 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002490 if (msr_data == 0x0)
2491 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002492 ss_sel = cs_sel + 8;
2493 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002494 cs.l = 1;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002495 if (is_noncanonical_address(rcx) ||
2496 is_noncanonical_address(rdx))
2497 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002498 break;
2499 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002500 cs_sel |= SELECTOR_RPL_MASK;
2501 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002502
Avi Kivity1aa36612011-04-27 13:20:30 +03002503 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2504 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002505
Nadav Amit234f3ce2014-09-18 22:39:38 +03002506 ctxt->_eip = rdx;
2507 *reg_write(ctxt, VCPU_REGS_RSP) = rcx;
Andre Przywara4668f052009-06-18 12:56:02 +02002508
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002509 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002510}
2511
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002512static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002513{
2514 int iopl;
2515 if (ctxt->mode == X86EMUL_MODE_REAL)
2516 return false;
2517 if (ctxt->mode == X86EMUL_MODE_VM86)
2518 return true;
2519 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002520 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002521}
2522
2523static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002524 u16 port, u16 len)
2525{
Mathias Krause0225fb52012-08-30 01:30:16 +02002526 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002527 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002528 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002529 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002530 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002531 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002532 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002533
Avi Kivity1aa36612011-04-27 13:20:30 +03002534 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002535 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002536 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002537 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002538 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002539 base = get_desc_base(&tr_seg);
2540#ifdef CONFIG_X86_64
2541 base |= ((u64)base3) << 32;
2542#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002543 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002544 if (r != X86EMUL_CONTINUE)
2545 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002546 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002547 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002548 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002549 if (r != X86EMUL_CONTINUE)
2550 return false;
2551 if ((perm >> bit_idx) & mask)
2552 return false;
2553 return true;
2554}
2555
2556static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002557 u16 port, u16 len)
2558{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002559 if (ctxt->perm_ok)
2560 return true;
2561
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002562 if (emulator_bad_iopl(ctxt))
2563 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002564 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002565
2566 ctxt->perm_ok = true;
2567
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002568 return true;
2569}
2570
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002571static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002572 struct tss_segment_16 *tss)
2573{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002574 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002575 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002576 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2577 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2578 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2579 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2580 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2581 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2582 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2583 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002584
Avi Kivity1aa36612011-04-27 13:20:30 +03002585 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2586 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2587 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2588 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2589 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002590}
2591
2592static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002593 struct tss_segment_16 *tss)
2594{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002595 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002596 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002597
Avi Kivity9dac77f2011-06-01 15:34:25 +03002598 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002599 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002600 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2601 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2602 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2603 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2604 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2605 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2606 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2607 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002608
2609 /*
2610 * SDM says that segment selectors are loaded before segment
2611 * descriptors
2612 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002613 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2614 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2615 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2616 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2617 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002618
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002619 cpl = tss->cs & 3;
2620
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002621 /*
Guo Chaofc058682012-06-28 15:19:51 +08002622 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002623 * it is handled in a context of new task
2624 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002625 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002626 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002627 if (ret != X86EMUL_CONTINUE)
2628 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002629 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002630 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002631 if (ret != X86EMUL_CONTINUE)
2632 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002633 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002634 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002635 if (ret != X86EMUL_CONTINUE)
2636 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002637 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002638 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002639 if (ret != X86EMUL_CONTINUE)
2640 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002641 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002642 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002643 if (ret != X86EMUL_CONTINUE)
2644 return ret;
2645
2646 return X86EMUL_CONTINUE;
2647}
2648
2649static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002650 u16 tss_selector, u16 old_tss_sel,
2651 ulong old_tss_base, struct desc_struct *new_desc)
2652{
Mathias Krause0225fb52012-08-30 01:30:16 +02002653 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002654 struct tss_segment_16 tss_seg;
2655 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002656 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002657
Avi Kivity0f65dd72011-04-20 13:37:53 +03002658 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002659 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002660 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002661 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002662
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002663 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002664
Avi Kivity0f65dd72011-04-20 13:37:53 +03002665 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002666 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002667 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002668 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002669
Avi Kivity0f65dd72011-04-20 13:37:53 +03002670 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002671 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002672 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002673 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002674
2675 if (old_tss_sel != 0xffff) {
2676 tss_seg.prev_task_link = old_tss_sel;
2677
Avi Kivity0f65dd72011-04-20 13:37:53 +03002678 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002679 &tss_seg.prev_task_link,
2680 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002681 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002682 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002683 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002684 }
2685
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002686 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002687}
2688
2689static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002690 struct tss_segment_32 *tss)
2691{
Nadav Amit5c7411e2014-04-07 18:37:47 +03002692 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002693 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002694 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002695 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2696 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2697 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2698 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2699 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2700 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2701 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2702 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002703
Avi Kivity1aa36612011-04-27 13:20:30 +03002704 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2705 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2706 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2707 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2708 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2709 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002710}
2711
2712static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002713 struct tss_segment_32 *tss)
2714{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002715 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002716 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002717
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002718 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002719 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002720 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002721 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002722
2723 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002724 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2725 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2726 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2727 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2728 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2729 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2730 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2731 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002732
2733 /*
2734 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002735 * descriptors. This is important because CPL checks will
2736 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002737 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002738 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2739 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2740 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2741 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2742 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2743 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2744 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002745
2746 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002747 * If we're switching between Protected Mode and VM86, we need to make
2748 * sure to update the mode before loading the segment descriptors so
2749 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01002750 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002751 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002752 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002753 cpl = 3;
2754 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002755 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002756 cpl = tss->cs & 3;
2757 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01002758
2759 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002760 * Now load segment descriptors. If fault happenes at this stage
2761 * it is handled in a context of new task
2762 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002763 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002764 cpl, X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002765 if (ret != X86EMUL_CONTINUE)
2766 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002767 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002768 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002769 if (ret != X86EMUL_CONTINUE)
2770 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002771 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002772 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002773 if (ret != X86EMUL_CONTINUE)
2774 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002775 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002776 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002777 if (ret != X86EMUL_CONTINUE)
2778 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002779 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002780 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002781 if (ret != X86EMUL_CONTINUE)
2782 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002783 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002784 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002785 if (ret != X86EMUL_CONTINUE)
2786 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002787 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002788 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002789 if (ret != X86EMUL_CONTINUE)
2790 return ret;
2791
2792 return X86EMUL_CONTINUE;
2793}
2794
2795static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002796 u16 tss_selector, u16 old_tss_sel,
2797 ulong old_tss_base, struct desc_struct *new_desc)
2798{
Mathias Krause0225fb52012-08-30 01:30:16 +02002799 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002800 struct tss_segment_32 tss_seg;
2801 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002802 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03002803 u32 eip_offset = offsetof(struct tss_segment_32, eip);
2804 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002805
Avi Kivity0f65dd72011-04-20 13:37:53 +03002806 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002807 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002808 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002809 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002810
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002811 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002812
Nadav Amit5c7411e2014-04-07 18:37:47 +03002813 /* Only GP registers and segment selectors are saved */
2814 ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
2815 ldt_sel_offset - eip_offset, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002816 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002817 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002818
Avi Kivity0f65dd72011-04-20 13:37:53 +03002819 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002820 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002821 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002822 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002823
2824 if (old_tss_sel != 0xffff) {
2825 tss_seg.prev_task_link = old_tss_sel;
2826
Avi Kivity0f65dd72011-04-20 13:37:53 +03002827 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002828 &tss_seg.prev_task_link,
2829 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002830 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002831 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002832 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002833 }
2834
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002835 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002836}
2837
2838static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002839 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002840 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002841{
Mathias Krause0225fb52012-08-30 01:30:16 +02002842 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002843 struct desc_struct curr_tss_desc, next_tss_desc;
2844 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002845 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002846 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002847 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002848 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002849 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002850
2851 /* FIXME: old_tss_base == ~0 ? */
2852
Avi Kivitye9194642012-06-13 16:29:39 +03002853 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002854 if (ret != X86EMUL_CONTINUE)
2855 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002856 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002857 if (ret != X86EMUL_CONTINUE)
2858 return ret;
2859
2860 /* FIXME: check that next_tss_desc is tss */
2861
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002862 /*
2863 * Check privileges. The three cases are task switch caused by...
2864 *
2865 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2866 * 2. Exception/IRQ/iret: No check is performed
Nadav Amit2c2ca2d2014-11-02 11:54:57 +02002867 * 3. jmp/call to TSS/task-gate: No check is performed since the
2868 * hardware checks it before exiting.
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002869 */
2870 if (reason == TASK_SWITCH_GATE) {
2871 if (idt_index != -1) {
2872 /* Software interrupts */
2873 struct desc_struct task_gate_desc;
2874 int dpl;
2875
2876 ret = read_interrupt_descriptor(ctxt, idt_index,
2877 &task_gate_desc);
2878 if (ret != X86EMUL_CONTINUE)
2879 return ret;
2880
2881 dpl = task_gate_desc.dpl;
2882 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2883 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2884 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002885 }
2886
Gleb Natapovceffb452010-03-18 15:20:19 +02002887 desc_limit = desc_limit_scaled(&next_tss_desc);
2888 if (!next_tss_desc.p ||
2889 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2890 desc_limit < 0x2b)) {
Paolo Bonzini592f0852014-08-20 10:05:08 +02002891 return emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002892 }
2893
2894 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2895 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002896 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002897 }
2898
2899 if (reason == TASK_SWITCH_IRET)
2900 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2901
2902 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002903 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002904 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2905 old_tss_sel = 0xffff;
2906
2907 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002908 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002909 old_tss_base, &next_tss_desc);
2910 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002911 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002912 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002913 if (ret != X86EMUL_CONTINUE)
2914 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002915
2916 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2917 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2918
2919 if (reason != TASK_SWITCH_IRET) {
2920 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002921 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002922 }
2923
Avi Kivity717746e2011-04-20 13:37:53 +03002924 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002925 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002926
Jan Kiszkae269fb22010-04-14 15:51:09 +02002927 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002928 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2929 ctxt->lock_prefix = 0;
2930 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002931 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002932 }
2933
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002934 return ret;
2935}
2936
2937int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002938 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002939 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002940{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002941 int rc;
2942
Avi Kivitydd856ef2012-08-27 23:46:17 +03002943 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002944 ctxt->_eip = ctxt->eip;
2945 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002946
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002947 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002948 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002949
Avi Kivitydd856ef2012-08-27 23:46:17 +03002950 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002951 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002952 writeback_registers(ctxt);
2953 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002954
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002955 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002956}
2957
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002958static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2959 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002960{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002961 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002962
Paolo Bonzini01485a22014-11-19 18:25:08 +01002963 register_address_increment(ctxt, reg, df * op->bytes);
2964 op->addr.mem.ea = register_address(ctxt, reg);
Gleb Natapova682e352010-03-18 15:20:21 +02002965}
2966
Avi Kivity7af04fc2010-08-18 14:16:35 +03002967static int em_das(struct x86_emulate_ctxt *ctxt)
2968{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002969 u8 al, old_al;
2970 bool af, cf, old_cf;
2971
2972 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002973 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002974
2975 old_al = al;
2976 old_cf = cf;
2977 cf = false;
2978 af = ctxt->eflags & X86_EFLAGS_AF;
2979 if ((al & 0x0f) > 9 || af) {
2980 al -= 6;
2981 cf = old_cf | (al >= 250);
2982 af = true;
2983 } else {
2984 af = false;
2985 }
2986 if (old_al > 0x99 || old_cf) {
2987 al -= 0x60;
2988 cf = true;
2989 }
2990
Avi Kivity9dac77f2011-06-01 15:34:25 +03002991 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002992 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002993 ctxt->src.type = OP_IMM;
2994 ctxt->src.val = 0;
2995 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02002996 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03002997 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2998 if (cf)
2999 ctxt->eflags |= X86_EFLAGS_CF;
3000 if (af)
3001 ctxt->eflags |= X86_EFLAGS_AF;
3002 return X86EMUL_CONTINUE;
3003}
3004
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003005static int em_aam(struct x86_emulate_ctxt *ctxt)
3006{
3007 u8 al, ah;
3008
3009 if (ctxt->src.val == 0)
3010 return emulate_de(ctxt);
3011
3012 al = ctxt->dst.val & 0xff;
3013 ah = al / ctxt->src.val;
3014 al %= ctxt->src.val;
3015
3016 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
3017
3018 /* Set PF, ZF, SF */
3019 ctxt->src.type = OP_IMM;
3020 ctxt->src.val = 0;
3021 ctxt->src.bytes = 1;
3022 fastop(ctxt, em_or);
3023
3024 return X86EMUL_CONTINUE;
3025}
3026
Gleb Natapov7f662272012-12-10 11:42:30 +02003027static int em_aad(struct x86_emulate_ctxt *ctxt)
3028{
3029 u8 al = ctxt->dst.val & 0xff;
3030 u8 ah = (ctxt->dst.val >> 8) & 0xff;
3031
3032 al = (al + (ah * ctxt->src.val)) & 0xff;
3033
3034 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
3035
Gleb Natapovf583c292013-02-13 17:50:39 +02003036 /* Set PF, ZF, SF */
3037 ctxt->src.type = OP_IMM;
3038 ctxt->src.val = 0;
3039 ctxt->src.bytes = 1;
3040 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02003041
3042 return X86EMUL_CONTINUE;
3043}
3044
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003045static int em_call(struct x86_emulate_ctxt *ctxt)
3046{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003047 int rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003048 long rel = ctxt->src.val;
3049
3050 ctxt->src.val = (unsigned long)ctxt->_eip;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003051 rc = jmp_rel(ctxt, rel);
3052 if (rc != X86EMUL_CONTINUE)
3053 return rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003054 return em_push(ctxt);
3055}
3056
Avi Kivity0ef753b2010-08-18 14:51:45 +03003057static int em_call_far(struct x86_emulate_ctxt *ctxt)
3058{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003059 u16 sel, old_cs;
3060 ulong old_eip;
3061 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03003062 struct desc_struct old_desc, new_desc;
3063 const struct x86_emulate_ops *ops = ctxt->ops;
3064 int cpl = ctxt->ops->cpl(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003065
Avi Kivity9dac77f2011-06-01 15:34:25 +03003066 old_eip = ctxt->_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003067 ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003068
Avi Kivity9dac77f2011-06-01 15:34:25 +03003069 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003070 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
3071 X86_TRANSFER_CALL_JMP, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003072 if (rc != X86EMUL_CONTINUE)
Nadav Amit80976db2014-12-25 02:52:20 +02003073 return rc;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003074
Nadav Amitd50eaa12014-11-19 17:43:11 +02003075 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003076 if (rc != X86EMUL_CONTINUE)
3077 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003078
Avi Kivity9dac77f2011-06-01 15:34:25 +03003079 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003080 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003081 if (rc != X86EMUL_CONTINUE)
Nadav Amitd1442d82014-09-18 22:39:39 +03003082 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003083
Avi Kivity9dac77f2011-06-01 15:34:25 +03003084 ctxt->src.val = old_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003085 rc = em_push(ctxt);
3086 /* If we failed, we tainted the memory, but the very least we should
3087 restore cs */
3088 if (rc != X86EMUL_CONTINUE)
3089 goto fail;
3090 return rc;
3091fail:
3092 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
3093 return rc;
3094
Avi Kivity0ef753b2010-08-18 14:51:45 +03003095}
3096
Avi Kivity40ece7c2010-08-18 15:12:09 +03003097static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3098{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003099 int rc;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003100 unsigned long eip;
Avi Kivity40ece7c2010-08-18 15:12:09 +03003101
Nadav Amit234f3ce2014-09-18 22:39:38 +03003102 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
3103 if (rc != X86EMUL_CONTINUE)
3104 return rc;
3105 rc = assign_eip_near(ctxt, eip);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003106 if (rc != X86EMUL_CONTINUE)
3107 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003108 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003109 return X86EMUL_CONTINUE;
3110}
3111
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003112static int em_xchg(struct x86_emulate_ctxt *ctxt)
3113{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003114 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003115 ctxt->src.val = ctxt->dst.val;
3116 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003117
3118 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003119 ctxt->dst.val = ctxt->src.orig_val;
3120 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003121 return X86EMUL_CONTINUE;
3122}
3123
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003124static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3125{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003126 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003127 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003128}
3129
Avi Kivity61429142010-08-19 15:13:00 +03003130static int em_cwd(struct x86_emulate_ctxt *ctxt)
3131{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003132 ctxt->dst.type = OP_REG;
3133 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003134 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003135 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003136
3137 return X86EMUL_CONTINUE;
3138}
3139
Avi Kivity48bb5d32010-08-18 18:54:34 +03003140static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3141{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003142 u64 tsc = 0;
3143
Avi Kivity717746e2011-04-20 13:37:53 +03003144 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003145 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3146 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003147 return X86EMUL_CONTINUE;
3148}
3149
Avi Kivity222d21a2011-11-10 14:57:30 +02003150static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3151{
3152 u64 pmc;
3153
Avi Kivitydd856ef2012-08-27 23:46:17 +03003154 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003155 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003156 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3157 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003158 return X86EMUL_CONTINUE;
3159}
3160
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003161static int em_mov(struct x86_emulate_ctxt *ctxt)
3162{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01003163 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003164 return X86EMUL_CONTINUE;
3165}
3166
Borislav Petkov84cffe42013-10-29 12:54:56 +01003167#define FFL(x) bit(X86_FEATURE_##x)
3168
3169static int em_movbe(struct x86_emulate_ctxt *ctxt)
3170{
3171 u32 ebx, ecx, edx, eax = 1;
3172 u16 tmp;
3173
3174 /*
3175 * Check MOVBE is set in the guest-visible CPUID leaf.
3176 */
3177 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
3178 if (!(ecx & FFL(MOVBE)))
3179 return emulate_ud(ctxt);
3180
3181 switch (ctxt->op_bytes) {
3182 case 2:
3183 /*
3184 * From MOVBE definition: "...When the operand size is 16 bits,
3185 * the upper word of the destination register remains unchanged
3186 * ..."
3187 *
3188 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3189 * rules so we have to do the operation almost per hand.
3190 */
3191 tmp = (u16)ctxt->src.val;
3192 ctxt->dst.val &= ~0xffffUL;
3193 ctxt->dst.val |= (unsigned long)swab16(tmp);
3194 break;
3195 case 4:
3196 ctxt->dst.val = swab32((u32)ctxt->src.val);
3197 break;
3198 case 8:
3199 ctxt->dst.val = swab64(ctxt->src.val);
3200 break;
3201 default:
Paolo Bonzini592f0852014-08-20 10:05:08 +02003202 BUG();
Borislav Petkov84cffe42013-10-29 12:54:56 +01003203 }
3204 return X86EMUL_CONTINUE;
3205}
3206
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003207static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3208{
3209 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3210 return emulate_gp(ctxt, 0);
3211
3212 /* Disable writeback. */
3213 ctxt->dst.type = OP_NONE;
3214 return X86EMUL_CONTINUE;
3215}
3216
3217static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3218{
3219 unsigned long val;
3220
3221 if (ctxt->mode == X86EMUL_MODE_PROT64)
3222 val = ctxt->src.val & ~0ULL;
3223 else
3224 val = ctxt->src.val & ~0U;
3225
3226 /* #UD condition is already handled. */
3227 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3228 return emulate_gp(ctxt, 0);
3229
3230 /* Disable writeback. */
3231 ctxt->dst.type = OP_NONE;
3232 return X86EMUL_CONTINUE;
3233}
3234
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003235static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3236{
3237 u64 msr_data;
3238
Avi Kivitydd856ef2012-08-27 23:46:17 +03003239 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3240 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3241 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003242 return emulate_gp(ctxt, 0);
3243
3244 return X86EMUL_CONTINUE;
3245}
3246
3247static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3248{
3249 u64 msr_data;
3250
Avi Kivitydd856ef2012-08-27 23:46:17 +03003251 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003252 return emulate_gp(ctxt, 0);
3253
Avi Kivitydd856ef2012-08-27 23:46:17 +03003254 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3255 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003256 return X86EMUL_CONTINUE;
3257}
3258
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003259static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3260{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003261 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003262 return emulate_ud(ctxt);
3263
Avi Kivity9dac77f2011-06-01 15:34:25 +03003264 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Nadav Amitb5bbf102014-11-02 11:54:46 +02003265 if (ctxt->dst.bytes == 4 && ctxt->dst.type == OP_MEM)
3266 ctxt->dst.bytes = 2;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003267 return X86EMUL_CONTINUE;
3268}
3269
3270static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3271{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003272 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003273
Avi Kivity9dac77f2011-06-01 15:34:25 +03003274 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003275 return emulate_ud(ctxt);
3276
Avi Kivity9dac77f2011-06-01 15:34:25 +03003277 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003278 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3279
3280 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003281 ctxt->dst.type = OP_NONE;
3282 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003283}
3284
Avi Kivitya14e5792012-06-13 12:28:33 +03003285static int em_lldt(struct x86_emulate_ctxt *ctxt)
3286{
3287 u16 sel = ctxt->src.val;
3288
3289 /* Disable writeback. */
3290 ctxt->dst.type = OP_NONE;
3291 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3292}
3293
Avi Kivity80890002012-06-13 16:33:29 +03003294static int em_ltr(struct x86_emulate_ctxt *ctxt)
3295{
3296 u16 sel = ctxt->src.val;
3297
3298 /* Disable writeback. */
3299 ctxt->dst.type = OP_NONE;
3300 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3301}
3302
Avi Kivity38503912011-03-31 18:48:09 +02003303static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3304{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003305 int rc;
3306 ulong linear;
3307
Avi Kivity9dac77f2011-06-01 15:34:25 +03003308 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003309 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003310 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003311 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003312 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003313 return X86EMUL_CONTINUE;
3314}
3315
Avi Kivity2d04a052011-04-20 15:32:49 +03003316static int em_clts(struct x86_emulate_ctxt *ctxt)
3317{
3318 ulong cr0;
3319
3320 cr0 = ctxt->ops->get_cr(ctxt, 0);
3321 cr0 &= ~X86_CR0_TS;
3322 ctxt->ops->set_cr(ctxt, 0, cr0);
3323 return X86EMUL_CONTINUE;
3324}
3325
Avi Kivity26d05cc2011-04-21 12:07:59 +03003326static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3327{
Nadav Amit0f54a322014-08-29 11:26:55 +03003328 int rc = ctxt->ops->fix_hypercall(ctxt);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003329
Avi Kivity26d05cc2011-04-21 12:07:59 +03003330 if (rc != X86EMUL_CONTINUE)
3331 return rc;
3332
3333 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003334 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003335 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003336 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003337 return X86EMUL_CONTINUE;
3338}
3339
Avi Kivity96051572012-06-10 17:21:18 +03003340static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3341 void (*get)(struct x86_emulate_ctxt *ctxt,
3342 struct desc_ptr *ptr))
3343{
3344 struct desc_ptr desc_ptr;
3345
3346 if (ctxt->mode == X86EMUL_MODE_PROT64)
3347 ctxt->op_bytes = 8;
3348 get(ctxt, &desc_ptr);
3349 if (ctxt->op_bytes == 2) {
3350 ctxt->op_bytes = 4;
3351 desc_ptr.address &= 0x00ffffff;
3352 }
3353 /* Disable writeback. */
3354 ctxt->dst.type = OP_NONE;
3355 return segmented_write(ctxt, ctxt->dst.addr.mem,
3356 &desc_ptr, 2 + ctxt->op_bytes);
3357}
3358
3359static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3360{
3361 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3362}
3363
3364static int em_sidt(struct x86_emulate_ctxt *ctxt)
3365{
3366 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3367}
3368
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003369static int em_lgdt_lidt(struct x86_emulate_ctxt *ctxt, bool lgdt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003370{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003371 struct desc_ptr desc_ptr;
3372 int rc;
3373
Avi Kivity510425f2012-06-07 17:04:36 +03003374 if (ctxt->mode == X86EMUL_MODE_PROT64)
3375 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003376 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003377 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003378 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003379 if (rc != X86EMUL_CONTINUE)
3380 return rc;
Nadav Amit9a9abf62014-11-02 11:54:56 +02003381 if (ctxt->mode == X86EMUL_MODE_PROT64 &&
3382 is_noncanonical_address(desc_ptr.address))
3383 return emulate_gp(ctxt, 0);
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003384 if (lgdt)
3385 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3386 else
3387 ctxt->ops->set_idt(ctxt, &desc_ptr);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003388 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003389 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003390 return X86EMUL_CONTINUE;
3391}
3392
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003393static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3394{
3395 return em_lgdt_lidt(ctxt, true);
3396}
3397
Avi Kivity5ef39c72011-04-21 12:21:50 +03003398static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003399{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003400 int rc;
3401
Avi Kivity5ef39c72011-04-21 12:21:50 +03003402 rc = ctxt->ops->fix_hypercall(ctxt);
3403
Avi Kivity26d05cc2011-04-21 12:07:59 +03003404 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003405 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003406 return rc;
3407}
3408
3409static int em_lidt(struct x86_emulate_ctxt *ctxt)
3410{
Nadav Amit5b7f6a1e2014-11-02 11:54:55 +02003411 return em_lgdt_lidt(ctxt, false);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003412}
3413
3414static int em_smsw(struct x86_emulate_ctxt *ctxt)
3415{
Nadav Amit32e94d02014-06-02 18:34:11 +03003416 if (ctxt->dst.type == OP_MEM)
3417 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003418 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003419 return X86EMUL_CONTINUE;
3420}
3421
3422static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3423{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003424 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003425 | (ctxt->src.val & 0x0f));
3426 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003427 return X86EMUL_CONTINUE;
3428}
3429
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003430static int em_loop(struct x86_emulate_ctxt *ctxt)
3431{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003432 int rc = X86EMUL_CONTINUE;
3433
Paolo Bonzini01485a22014-11-19 18:25:08 +01003434 register_address_increment(ctxt, VCPU_REGS_RCX, -1);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003435 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003436 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
Nadav Amit234f3ce2014-09-18 22:39:38 +03003437 rc = jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003438
Nadav Amit234f3ce2014-09-18 22:39:38 +03003439 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003440}
3441
3442static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3443{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003444 int rc = X86EMUL_CONTINUE;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003445
Nadav Amit234f3ce2014-09-18 22:39:38 +03003446 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
3447 rc = jmp_rel(ctxt, ctxt->src.val);
3448
3449 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003450}
3451
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003452static int em_in(struct x86_emulate_ctxt *ctxt)
3453{
3454 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3455 &ctxt->dst.val))
3456 return X86EMUL_IO_NEEDED;
3457
3458 return X86EMUL_CONTINUE;
3459}
3460
3461static int em_out(struct x86_emulate_ctxt *ctxt)
3462{
3463 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3464 &ctxt->src.val, 1);
3465 /* Disable writeback. */
3466 ctxt->dst.type = OP_NONE;
3467 return X86EMUL_CONTINUE;
3468}
3469
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003470static int em_cli(struct x86_emulate_ctxt *ctxt)
3471{
3472 if (emulator_bad_iopl(ctxt))
3473 return emulate_gp(ctxt, 0);
3474
3475 ctxt->eflags &= ~X86_EFLAGS_IF;
3476 return X86EMUL_CONTINUE;
3477}
3478
3479static int em_sti(struct x86_emulate_ctxt *ctxt)
3480{
3481 if (emulator_bad_iopl(ctxt))
3482 return emulate_gp(ctxt, 0);
3483
3484 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3485 ctxt->eflags |= X86_EFLAGS_IF;
3486 return X86EMUL_CONTINUE;
3487}
3488
Avi Kivity6d6eede2012-06-07 14:11:36 +03003489static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3490{
3491 u32 eax, ebx, ecx, edx;
3492
Avi Kivitydd856ef2012-08-27 23:46:17 +03003493 eax = reg_read(ctxt, VCPU_REGS_RAX);
3494 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003495 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003496 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3497 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3498 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3499 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003500 return X86EMUL_CONTINUE;
3501}
3502
Paolo Bonzini98f73632013-10-31 11:19:42 +01003503static int em_sahf(struct x86_emulate_ctxt *ctxt)
3504{
3505 u32 flags;
3506
3507 flags = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF;
3508 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3509
3510 ctxt->eflags &= ~0xffUL;
3511 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3512 return X86EMUL_CONTINUE;
3513}
3514
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003515static int em_lahf(struct x86_emulate_ctxt *ctxt)
3516{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003517 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3518 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003519 return X86EMUL_CONTINUE;
3520}
3521
Avi Kivity92998362012-06-13 12:25:06 +03003522static int em_bswap(struct x86_emulate_ctxt *ctxt)
3523{
3524 switch (ctxt->op_bytes) {
3525#ifdef CONFIG_X86_64
3526 case 8:
3527 asm("bswap %0" : "+r"(ctxt->dst.val));
3528 break;
3529#endif
3530 default:
3531 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3532 break;
3533 }
3534 return X86EMUL_CONTINUE;
3535}
3536
Nadav Amit13e457e2014-10-13 13:04:13 +03003537static int em_clflush(struct x86_emulate_ctxt *ctxt)
3538{
3539 /* emulating clflush regardless of cpuid */
3540 return X86EMUL_CONTINUE;
3541}
3542
Nadav Amit2276b512015-01-26 09:32:24 +02003543static int em_movsxd(struct x86_emulate_ctxt *ctxt)
3544{
3545 ctxt->dst.val = (s32) ctxt->src.val;
3546 return X86EMUL_CONTINUE;
3547}
3548
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003549static bool valid_cr(int nr)
3550{
3551 switch (nr) {
3552 case 0:
3553 case 2 ... 4:
3554 case 8:
3555 return true;
3556 default:
3557 return false;
3558 }
3559}
3560
3561static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3562{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003563 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003564 return emulate_ud(ctxt);
3565
3566 return X86EMUL_CONTINUE;
3567}
3568
3569static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3570{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003571 u64 new_val = ctxt->src.val64;
3572 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003573 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003574
3575 static u64 cr_reserved_bits[] = {
3576 0xffffffff00000000ULL,
3577 0, 0, 0, /* CR3 checked later */
3578 CR4_RESERVED_BITS,
3579 0, 0, 0,
3580 CR8_RESERVED_BITS,
3581 };
3582
3583 if (!valid_cr(cr))
3584 return emulate_ud(ctxt);
3585
3586 if (new_val & cr_reserved_bits[cr])
3587 return emulate_gp(ctxt, 0);
3588
3589 switch (cr) {
3590 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003591 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003592 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3593 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3594 return emulate_gp(ctxt, 0);
3595
Avi Kivity717746e2011-04-20 13:37:53 +03003596 cr4 = ctxt->ops->get_cr(ctxt, 4);
3597 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003598
3599 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3600 !(cr4 & X86_CR4_PAE))
3601 return emulate_gp(ctxt, 0);
3602
3603 break;
3604 }
3605 case 3: {
3606 u64 rsvd = 0;
3607
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003608 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3609 if (efer & EFER_LMA)
Nadav Amit9d88fca2014-11-02 11:54:52 +02003610 rsvd = CR3_L_MODE_RESERVED_BITS & ~CR3_PCID_INVD;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003611
3612 if (new_val & rsvd)
3613 return emulate_gp(ctxt, 0);
3614
3615 break;
3616 }
3617 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003618 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003619
3620 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3621 return emulate_gp(ctxt, 0);
3622
3623 break;
3624 }
3625 }
3626
3627 return X86EMUL_CONTINUE;
3628}
3629
Joerg Roedel3b88e412011-04-04 12:39:29 +02003630static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3631{
3632 unsigned long dr7;
3633
Avi Kivity717746e2011-04-20 13:37:53 +03003634 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003635
3636 /* Check if DR7.Global_Enable is set */
3637 return dr7 & (1 << 13);
3638}
3639
3640static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3641{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003642 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003643 u64 cr4;
3644
3645 if (dr > 7)
3646 return emulate_ud(ctxt);
3647
Avi Kivity717746e2011-04-20 13:37:53 +03003648 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003649 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3650 return emulate_ud(ctxt);
3651
Nadav Amit6d2a0522014-11-02 11:54:43 +02003652 if (check_dr7_gd(ctxt)) {
3653 ulong dr6;
3654
3655 ctxt->ops->get_dr(ctxt, 6, &dr6);
3656 dr6 &= ~15;
3657 dr6 |= DR6_BD | DR6_RTM;
3658 ctxt->ops->set_dr(ctxt, 6, dr6);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003659 return emulate_db(ctxt);
Nadav Amit6d2a0522014-11-02 11:54:43 +02003660 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02003661
3662 return X86EMUL_CONTINUE;
3663}
3664
3665static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3666{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003667 u64 new_val = ctxt->src.val64;
3668 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003669
3670 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3671 return emulate_gp(ctxt, 0);
3672
3673 return check_dr_read(ctxt);
3674}
3675
Joerg Roedel01de8b02011-04-04 12:39:31 +02003676static int check_svme(struct x86_emulate_ctxt *ctxt)
3677{
3678 u64 efer;
3679
Avi Kivity717746e2011-04-20 13:37:53 +03003680 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003681
3682 if (!(efer & EFER_SVME))
3683 return emulate_ud(ctxt);
3684
3685 return X86EMUL_CONTINUE;
3686}
3687
3688static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3689{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003690 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003691
3692 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003693 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003694 return emulate_gp(ctxt, 0);
3695
3696 return check_svme(ctxt);
3697}
3698
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003699static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3700{
Avi Kivity717746e2011-04-20 13:37:53 +03003701 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003702
Avi Kivity717746e2011-04-20 13:37:53 +03003703 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003704 return emulate_ud(ctxt);
3705
3706 return X86EMUL_CONTINUE;
3707}
3708
Joerg Roedel80612522011-04-04 12:39:33 +02003709static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3710{
Avi Kivity717746e2011-04-20 13:37:53 +03003711 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003712 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003713
Avi Kivity717746e2011-04-20 13:37:53 +03003714 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03003715 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02003716 return emulate_gp(ctxt, 0);
3717
3718 return X86EMUL_CONTINUE;
3719}
3720
Joerg Roedelf6511932011-04-04 12:39:35 +02003721static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3722{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003723 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3724 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003725 return emulate_gp(ctxt, 0);
3726
3727 return X86EMUL_CONTINUE;
3728}
3729
3730static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3731{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003732 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3733 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003734 return emulate_gp(ctxt, 0);
3735
3736 return X86EMUL_CONTINUE;
3737}
3738
Avi Kivity73fba5f2010-07-29 15:11:53 +03003739#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003740#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
3741#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
3742 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003743#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003744#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003745#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3746#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Nadav Amit39f062f2014-11-26 15:47:18 +02003747#define ID(_f, _i) { .flags = ((_f) | InstrDual | ModRM), .u.idual = (_i) }
Nadav Amit2276b512015-01-26 09:32:24 +02003748#define MD(_f, _m) { .flags = ((_f) | ModeDual), .u.mdual = (_m) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003749#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003750#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003751#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003752#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003753 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003754#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003755 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
3756 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003757#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003758
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003759#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003760#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003761#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003762#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003763#define I2bvIP(_f, _e, _i, _p) \
3764 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003765
Avi Kivityfb864fb2013-01-04 16:18:54 +02003766#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3767 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3768 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003769
Nadav Amit0f54a322014-08-29 11:26:55 +03003770static const struct opcode group7_rm0[] = {
3771 N,
3772 I(SrcNone | Priv | EmulateOnUD, em_vmcall),
3773 N, N, N, N, N, N,
3774};
3775
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003776static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003777 DI(SrcNone | Priv, monitor),
3778 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003779 N, N, N, N, N, N,
3780};
3781
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003782static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003783 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003784 II(SrcNone | Prot | EmulateOnUD, em_vmmcall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003785 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3786 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3787 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3788 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3789 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3790 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003791};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003792
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003793static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003794 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003795 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003796 N, N, N, N, N, N,
3797};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003798
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003799static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003800 F(Lock, em_add),
3801 F(Lock | PageTable, em_or),
3802 F(Lock, em_adc),
3803 F(Lock, em_sbb),
3804 F(Lock | PageTable, em_and),
3805 F(Lock, em_sub),
3806 F(Lock, em_xor),
3807 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003808};
3809
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003810static const struct opcode group1A[] = {
Nadav Amitab708092014-12-25 02:52:21 +02003811 I(DstMem | SrcNone | Mov | Stack | IncSP, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003812};
3813
Avi Kivity007a3b52013-01-19 19:51:51 +02003814static const struct opcode group2[] = {
3815 F(DstMem | ModRM, em_rol),
3816 F(DstMem | ModRM, em_ror),
3817 F(DstMem | ModRM, em_rcl),
3818 F(DstMem | ModRM, em_rcr),
3819 F(DstMem | ModRM, em_shl),
3820 F(DstMem | ModRM, em_shr),
3821 F(DstMem | ModRM, em_shl),
3822 F(DstMem | ModRM, em_sar),
3823};
3824
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003825static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003826 F(DstMem | SrcImm | NoWrite, em_test),
3827 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003828 F(DstMem | SrcNone | Lock, em_not),
3829 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02003830 F(DstXacc | Src2Mem, em_mul_ex),
3831 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02003832 F(DstXacc | Src2Mem, em_div_ex),
3833 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003834};
3835
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003836static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003837 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3838 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003839 N, N, N, N, N, N,
3840};
3841
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003842static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003843 F(DstMem | SrcNone | Lock, em_inc),
3844 F(DstMem | SrcNone | Lock, em_dec),
Nadav Amit58b70752014-10-24 11:35:09 +03003845 I(SrcMem | NearBranch, em_call_near_abs),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003846 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
Nadav Amit58b70752014-10-24 11:35:09 +03003847 I(SrcMem | NearBranch, em_jmp_abs),
Nadav Amitf7784042014-09-18 22:39:41 +03003848 I(SrcMemFAddr | ImplicitOps, em_jmp_far),
3849 I(SrcMem | Stack, em_push), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003850};
3851
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003852static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003853 DI(Prot, sldt),
3854 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003855 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003856 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003857 N, N, N, N,
3858};
3859
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003860static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03003861 II(Mov | DstMem, em_sgdt, sgdt),
3862 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003863 II(SrcMem | Priv, em_lgdt, lgdt),
3864 II(SrcMem | Priv, em_lidt, lidt),
3865 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3866 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3867 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003868}, {
Nadav Amit0f54a322014-08-29 11:26:55 +03003869 EXT(0, group7_rm0),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003870 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003871 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003872 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3873 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3874 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003875} };
3876
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003877static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003878 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003879 F(DstMem | SrcImmByte | NoWrite, em_bt),
3880 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3881 F(DstMem | SrcImmByte | Lock, em_btr),
3882 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003883};
3884
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003885static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003886 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003887}, {
3888 N, N, N, N, N, N, N, N,
3889} };
3890
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003891static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003892 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003893 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003894};
3895
Nadav Amit13e457e2014-10-13 13:04:13 +03003896static const struct gprefix pfx_0f_ae_7 = {
Nadav Amit3f6f1482014-10-13 13:04:14 +03003897 I(SrcMem | ByteOp, em_clflush), N, N, N,
Nadav Amit13e457e2014-10-13 13:04:13 +03003898};
3899
3900static const struct group_dual group15 = { {
3901 N, N, N, N, N, N, N, GP(0, &pfx_0f_ae_7),
3902}, {
3903 N, N, N, N, N, N, N, N,
3904} };
3905
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003906static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003907 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003908};
3909
Nadav Amit39f062f2014-11-26 15:47:18 +02003910static const struct instr_dual instr_dual_0f_2b = {
3911 I(0, em_mov), N
3912};
3913
Paolo Bonzinid5b77062014-07-14 12:54:48 +02003914static const struct gprefix pfx_0f_2b = {
Nadav Amit39f062f2014-11-26 15:47:18 +02003915 ID(0, &instr_dual_0f_2b), ID(0, &instr_dual_0f_2b), N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003916};
3917
Igor Mammedov27ce8252014-03-15 21:01:59 +01003918static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01003919 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003920};
3921
Alex Williamson0a370272014-07-11 11:56:31 -06003922static const struct gprefix pfx_0f_e7 = {
3923 N, I(Sse, em_mov), N, N,
3924};
3925
Gleb Natapov045a2822012-12-20 16:57:43 +02003926static const struct escape escape_d9 = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02003927 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstcw),
Gleb Natapov045a2822012-12-20 16:57:43 +02003928}, {
3929 /* 0xC0 - 0xC7 */
3930 N, N, N, N, N, N, N, N,
3931 /* 0xC8 - 0xCF */
3932 N, N, N, N, N, N, N, N,
3933 /* 0xD0 - 0xC7 */
3934 N, N, N, N, N, N, N, N,
3935 /* 0xD8 - 0xDF */
3936 N, N, N, N, N, N, N, N,
3937 /* 0xE0 - 0xE7 */
3938 N, N, N, N, N, N, N, N,
3939 /* 0xE8 - 0xEF */
3940 N, N, N, N, N, N, N, N,
3941 /* 0xF0 - 0xF7 */
3942 N, N, N, N, N, N, N, N,
3943 /* 0xF8 - 0xFF */
3944 N, N, N, N, N, N, N, N,
3945} };
3946
3947static const struct escape escape_db = { {
3948 N, N, N, N, N, N, N, N,
3949}, {
3950 /* 0xC0 - 0xC7 */
3951 N, N, N, N, N, N, N, N,
3952 /* 0xC8 - 0xCF */
3953 N, N, N, N, N, N, N, N,
3954 /* 0xD0 - 0xC7 */
3955 N, N, N, N, N, N, N, N,
3956 /* 0xD8 - 0xDF */
3957 N, N, N, N, N, N, N, N,
3958 /* 0xE0 - 0xE7 */
3959 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3960 /* 0xE8 - 0xEF */
3961 N, N, N, N, N, N, N, N,
3962 /* 0xF0 - 0xF7 */
3963 N, N, N, N, N, N, N, N,
3964 /* 0xF8 - 0xFF */
3965 N, N, N, N, N, N, N, N,
3966} };
3967
3968static const struct escape escape_dd = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02003969 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstsw),
Gleb Natapov045a2822012-12-20 16:57:43 +02003970}, {
3971 /* 0xC0 - 0xC7 */
3972 N, N, N, N, N, N, N, N,
3973 /* 0xC8 - 0xCF */
3974 N, N, N, N, N, N, N, N,
3975 /* 0xD0 - 0xC7 */
3976 N, N, N, N, N, N, N, N,
3977 /* 0xD8 - 0xDF */
3978 N, N, N, N, N, N, N, N,
3979 /* 0xE0 - 0xE7 */
3980 N, N, N, N, N, N, N, N,
3981 /* 0xE8 - 0xEF */
3982 N, N, N, N, N, N, N, N,
3983 /* 0xF0 - 0xF7 */
3984 N, N, N, N, N, N, N, N,
3985 /* 0xF8 - 0xFF */
3986 N, N, N, N, N, N, N, N,
3987} };
3988
Nadav Amit39f062f2014-11-26 15:47:18 +02003989static const struct instr_dual instr_dual_0f_c3 = {
3990 I(DstMem | SrcReg | ModRM | No16 | Mov, em_mov), N
3991};
3992
Nadav Amit2276b512015-01-26 09:32:24 +02003993static const struct mode_dual mode_dual_63 = {
3994 N, I(DstReg | SrcMem32 | ModRM | Mov, em_movsxd)
3995};
3996
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003997static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003998 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003999 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004000 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
4001 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004002 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004003 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004004 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
4005 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004006 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004007 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004008 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
4009 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004010 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004011 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004012 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
4013 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004014 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004015 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004016 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004017 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004018 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004019 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004020 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004021 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004022 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02004023 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004024 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03004025 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004026 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09004027 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004028 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09004029 I(ImplicitOps | Stack | No64, em_pusha),
4030 I(ImplicitOps | Stack | No64, em_popa),
Nadav Amit2276b512015-01-26 09:32:24 +02004031 N, MD(ModRM, &mode_dual_63),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004032 N, N, N, N,
4033 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03004034 I(SrcImm | Mov | Stack, em_push),
4035 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03004036 I(SrcImmByte | Mov | Stack, em_push),
4037 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004038 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09004039 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03004040 /* 0x70 - 0x7F */
Nadav Amit58b70752014-10-24 11:35:09 +03004041 X16(D(SrcImmByte | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004042 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004043 G(ByteOp | DstMem | SrcImm, group1),
4044 G(DstMem | SrcImm, group1),
4045 G(ByteOp | DstMem | SrcImm | No64, group1),
4046 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02004047 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004048 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004049 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004050 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004051 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004052 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09004053 D(ModRM | SrcMem | NoAccess | DstReg),
4054 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
4055 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004056 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004057 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004058 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03004059 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08004060 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09004061 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01004062 II(ImplicitOps | Stack, em_popf, popf),
4063 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004064 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004065 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004066 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004067 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004068 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004069 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004070 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004071 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
4072 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004073 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004074 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004075 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004076 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02004077 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004078 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004079 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Nadav Amit58b70752014-10-24 11:35:09 +03004080 I(ImplicitOps | NearBranch | SrcImmU16, em_ret_near_imm),
4081 I(ImplicitOps | NearBranch, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03004082 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
4083 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004084 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004085 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03004086 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Nadav Amit16794aa2015-01-26 09:32:22 +02004087 I(ImplicitOps | SrcImmU16, em_ret_far_imm),
4088 I(ImplicitOps, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02004089 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004090 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004091 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004092 G(Src2One | ByteOp, group2), G(Src2One, group2),
4093 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02004094 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02004095 I(DstAcc | SrcImmUByte | No64, em_aad),
4096 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004097 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004098 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02004099 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004100 /* 0xE0 - 0xE7 */
Nadav Amit58b70752014-10-24 11:35:09 +03004101 X3(I(SrcImmByte | NearBranch, em_loop)),
4102 I(SrcImmByte | NearBranch, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004103 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
4104 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004105 /* 0xE8 - 0xEF */
Nadav Amit58b70752014-10-24 11:35:09 +03004106 I(SrcImm | NearBranch, em_call), D(SrcImm | ImplicitOps | NearBranch),
4107 I(SrcImmFAddr | No64, em_jmp_far),
4108 D(SrcImmByte | ImplicitOps | NearBranch),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004109 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
4110 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004111 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004112 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004113 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4114 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004115 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09004116 D(ImplicitOps), D(ImplicitOps),
4117 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004118 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4119};
4120
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004121static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004122 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004123 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02004124 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004125 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004126 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004127 N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004128 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004129 N, N, N, N, N, N, N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004130 D(ImplicitOps | ModRM | SrcMem | NoAccess),
4131 N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004132 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03004133 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
4134 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
4135 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
4136 check_cr_write),
4137 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
4138 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004139 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004140 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
4141 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004142 N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b),
Avi Kivity3e114eb2012-04-09 18:40:01 +03004143 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004144 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004145 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004146 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004147 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004148 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02004149 I(ImplicitOps | EmulateOnUD, em_sysenter),
4150 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004151 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004152 N, N, N, N, N, N, N, N,
4153 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03004154 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004155 /* 0x50 - 0x5F */
4156 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4157 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004158 N, N, N, N,
4159 N, N, N, N,
4160 N, N, N, N,
4161 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004162 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004163 N, N, N, N,
4164 N, N, N, N,
4165 N, N, N, N,
4166 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004167 /* 0x80 - 0x8F */
Nadav Amit58b70752014-10-24 11:35:09 +03004168 X16(D(SrcImm | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004169 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004170 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004171 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004172 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004173 II(ImplicitOps, em_cpuid, cpuid),
4174 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004175 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4176 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004177 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004178 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004179 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004180 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004181 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4182 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Nadav Amit13e457e2014-10-13 13:04:13 +03004183 GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004184 /* 0xB0 - 0xB7 */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02004185 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable | SrcWrite, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004186 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004187 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004188 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4189 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004190 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004191 /* 0xB8 - 0xBF */
4192 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004193 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004194 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
4195 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004196 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004197 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02004198 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Nadav Amit39f062f2014-11-26 15:47:18 +02004199 N, ID(0, &instr_dual_0f_c3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004200 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004201 /* 0xC8 - 0xCF */
4202 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004203 /* 0xD0 - 0xDF */
4204 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4205 /* 0xE0 - 0xEF */
Alex Williamson0a370272014-07-11 11:56:31 -06004206 N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
4207 N, N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004208 /* 0xF0 - 0xFF */
4209 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4210};
4211
Nadav Amit39f062f2014-11-26 15:47:18 +02004212static const struct instr_dual instr_dual_0f_38_f0 = {
4213 I(DstReg | SrcMem | Mov, em_movbe), N
4214};
4215
4216static const struct instr_dual instr_dual_0f_38_f1 = {
4217 I(DstMem | SrcReg | Mov, em_movbe), N
4218};
4219
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004220static const struct gprefix three_byte_0f_38_f0 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004221 ID(0, &instr_dual_0f_38_f0), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004222};
4223
4224static const struct gprefix three_byte_0f_38_f1 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004225 ID(0, &instr_dual_0f_38_f1), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004226};
4227
4228/*
4229 * Insns below are selected by the prefix which indexed by the third opcode
4230 * byte.
4231 */
4232static const struct opcode opcode_map_0f_38[256] = {
4233 /* 0x00 - 0x7f */
4234 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004235 /* 0x80 - 0xef */
4236 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4237 /* 0xf0 - 0xf1 */
Nadav Amit53bb4f72014-12-07 11:49:42 +02004238 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0),
4239 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004240 /* 0xf2 - 0xff */
4241 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004242};
4243
Avi Kivity73fba5f2010-07-29 15:11:53 +03004244#undef D
4245#undef N
4246#undef G
4247#undef GD
4248#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004249#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004250#undef EXT
Nadav Amit2276b512015-01-26 09:32:24 +02004251#undef MD
Nadav Amit2b42fce2015-01-26 09:32:25 +02004252#undef ID
Avi Kivity73fba5f2010-07-29 15:11:53 +03004253
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004254#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004255#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004256#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004257#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004258#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004259
Avi Kivity9dac77f2011-06-01 15:34:25 +03004260static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004261{
4262 unsigned size;
4263
Avi Kivity9dac77f2011-06-01 15:34:25 +03004264 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004265 if (size == 8)
4266 size = 4;
4267 return size;
4268}
4269
4270static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4271 unsigned size, bool sign_extension)
4272{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004273 int rc = X86EMUL_CONTINUE;
4274
4275 op->type = OP_IMM;
4276 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004277 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004278 /* NB. Immediates are sign-extended as necessary. */
4279 switch (op->bytes) {
4280 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004281 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004282 break;
4283 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004284 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004285 break;
4286 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004287 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004288 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004289 case 8:
4290 op->val = insn_fetch(s64, ctxt);
4291 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004292 }
4293 if (!sign_extension) {
4294 switch (op->bytes) {
4295 case 1:
4296 op->val &= 0xff;
4297 break;
4298 case 2:
4299 op->val &= 0xffff;
4300 break;
4301 case 4:
4302 op->val &= 0xffffffff;
4303 break;
4304 }
4305 }
4306done:
4307 return rc;
4308}
4309
Avi Kivitya99455492011-09-13 10:45:41 +03004310static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4311 unsigned d)
4312{
4313 int rc = X86EMUL_CONTINUE;
4314
4315 switch (d) {
4316 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004317 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03004318 break;
4319 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004320 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03004321 break;
4322 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004323 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004324 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03004325 *op = ctxt->memop;
4326 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004327 if (ctxt->d & BitOp)
Avi Kivitya99455492011-09-13 10:45:41 +03004328 fetch_bit_operand(ctxt);
4329 op->orig_val = op->val;
4330 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004331 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004332 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004333 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03004334 case OpAcc:
4335 op->type = OP_REG;
4336 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004337 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03004338 fetch_register_operand(op);
4339 op->orig_val = op->val;
4340 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004341 case OpAccLo:
4342 op->type = OP_REG;
4343 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4344 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4345 fetch_register_operand(op);
4346 op->orig_val = op->val;
4347 break;
4348 case OpAccHi:
4349 if (ctxt->d & ByteOp) {
4350 op->type = OP_NONE;
4351 break;
4352 }
4353 op->type = OP_REG;
4354 op->bytes = ctxt->op_bytes;
4355 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4356 fetch_register_operand(op);
4357 op->orig_val = op->val;
4358 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004359 case OpDI:
4360 op->type = OP_MEM;
4361 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4362 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004363 register_address(ctxt, VCPU_REGS_RDI);
Avi Kivitya99455492011-09-13 10:45:41 +03004364 op->addr.mem.seg = VCPU_SREG_ES;
4365 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004366 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03004367 break;
4368 case OpDX:
4369 op->type = OP_REG;
4370 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004371 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03004372 fetch_register_operand(op);
4373 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004374 case OpCL:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004375 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004376 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004377 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004378 break;
4379 case OpImmByte:
4380 rc = decode_imm(ctxt, op, 1, true);
4381 break;
4382 case OpOne:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004383 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004384 op->bytes = 1;
4385 op->val = 1;
4386 break;
4387 case OpImm:
4388 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4389 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004390 case OpImm64:
4391 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4392 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004393 case OpMem8:
4394 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004395 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004396 ctxt->memop.addr.reg = decode_register(ctxt,
4397 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004398 fetch_register_operand(&ctxt->memop);
4399 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004400 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004401 case OpMem16:
4402 ctxt->memop.bytes = 2;
4403 goto mem_common;
4404 case OpMem32:
4405 ctxt->memop.bytes = 4;
4406 goto mem_common;
4407 case OpImmU16:
4408 rc = decode_imm(ctxt, op, 2, false);
4409 break;
4410 case OpImmU:
4411 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4412 break;
4413 case OpSI:
4414 op->type = OP_MEM;
4415 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4416 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004417 register_address(ctxt, VCPU_REGS_RSI);
Bandan Das573e80f2014-04-16 12:46:13 -04004418 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03004419 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004420 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004421 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004422 case OpXLat:
4423 op->type = OP_MEM;
4424 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4425 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004426 address_mask(ctxt,
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004427 reg_read(ctxt, VCPU_REGS_RBX) +
4428 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04004429 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004430 op->val = 0;
4431 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004432 case OpImmFAddr:
4433 op->type = OP_IMM;
4434 op->addr.mem.ea = ctxt->_eip;
4435 op->bytes = ctxt->op_bytes + 2;
4436 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4437 break;
4438 case OpMemFAddr:
4439 ctxt->memop.bytes = ctxt->op_bytes + 2;
4440 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004441 case OpES:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004442 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004443 op->val = VCPU_SREG_ES;
4444 break;
4445 case OpCS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004446 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004447 op->val = VCPU_SREG_CS;
4448 break;
4449 case OpSS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004450 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004451 op->val = VCPU_SREG_SS;
4452 break;
4453 case OpDS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004454 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004455 op->val = VCPU_SREG_DS;
4456 break;
4457 case OpFS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004458 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004459 op->val = VCPU_SREG_FS;
4460 break;
4461 case OpGS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004462 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004463 op->val = VCPU_SREG_GS;
4464 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004465 case OpImplicit:
4466 /* Special instructions do their own operand decoding. */
4467 default:
4468 op->type = OP_NONE; /* Disable writeback. */
4469 break;
4470 }
4471
4472done:
4473 return rc;
4474}
4475
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004476int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004477{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004478 int rc = X86EMUL_CONTINUE;
4479 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004480 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004481 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04004482 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03004483 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004484
Avi Kivityf09ed832011-09-13 10:45:40 +03004485 ctxt->memop.type = OP_NONE;
4486 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004487 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02004488 ctxt->fetch.ptr = ctxt->fetch.data;
4489 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004490 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01004491 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004492 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004493 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02004494 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004495 if (rc != X86EMUL_CONTINUE)
4496 return rc;
4497 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004498
4499 switch (mode) {
4500 case X86EMUL_MODE_REAL:
4501 case X86EMUL_MODE_VM86:
4502 case X86EMUL_MODE_PROT16:
4503 def_op_bytes = def_ad_bytes = 2;
4504 break;
4505 case X86EMUL_MODE_PROT32:
4506 def_op_bytes = def_ad_bytes = 4;
4507 break;
4508#ifdef CONFIG_X86_64
4509 case X86EMUL_MODE_PROT64:
4510 def_op_bytes = 4;
4511 def_ad_bytes = 8;
4512 break;
4513#endif
4514 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004515 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004516 }
4517
Avi Kivity9dac77f2011-06-01 15:34:25 +03004518 ctxt->op_bytes = def_op_bytes;
4519 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004520
4521 /* Legacy prefixes. */
4522 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004523 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004524 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004525 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004526 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004527 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004528 break;
4529 case 0x67: /* address-size override */
4530 if (mode == X86EMUL_MODE_PROT64)
4531 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004532 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004533 else
4534 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004535 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004536 break;
4537 case 0x26: /* ES override */
4538 case 0x2e: /* CS override */
4539 case 0x36: /* SS override */
4540 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004541 has_seg_override = true;
4542 ctxt->seg_override = (ctxt->b >> 3) & 3;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004543 break;
4544 case 0x64: /* FS override */
4545 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004546 has_seg_override = true;
4547 ctxt->seg_override = ctxt->b & 7;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004548 break;
4549 case 0x40 ... 0x4f: /* REX */
4550 if (mode != X86EMUL_MODE_PROT64)
4551 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004552 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004553 continue;
4554 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004555 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004556 break;
4557 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004558 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004559 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004560 break;
4561 default:
4562 goto done_prefixes;
4563 }
4564
4565 /* Any legacy prefix after a REX prefix nullifies its effect. */
4566
Avi Kivity9dac77f2011-06-01 15:34:25 +03004567 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004568 }
4569
4570done_prefixes:
4571
4572 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004573 if (ctxt->rex_prefix & 8)
4574 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004575
4576 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004577 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004578 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004579 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004580 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004581 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004582 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004583
4584 /* 0F_38 opcode map */
4585 if (ctxt->b == 0x38) {
4586 ctxt->opcode_len = 3;
4587 ctxt->b = insn_fetch(u8, ctxt);
4588 opcode = opcode_map_0f_38[ctxt->b];
4589 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004590 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004591 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004592
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004593 if (ctxt->d & ModRM)
4594 ctxt->modrm = insn_fetch(u8, ctxt);
4595
Nadav Amit7fe864d2014-06-02 18:34:03 +03004596 /* vex-prefix instructions are not implemented */
4597 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
Nadav Amitd14cb5d2014-11-02 11:54:58 +02004598 (mode == X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) == 0xc0)) {
Nadav Amit7fe864d2014-06-02 18:34:03 +03004599 ctxt->d = NotImpl;
4600 }
4601
Avi Kivity9dac77f2011-06-01 15:34:25 +03004602 while (ctxt->d & GroupMask) {
4603 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004604 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004605 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004606 opcode = opcode.u.group[goffset];
4607 break;
4608 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004609 goffset = (ctxt->modrm >> 3) & 7;
4610 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004611 opcode = opcode.u.gdual->mod3[goffset];
4612 else
4613 opcode = opcode.u.gdual->mod012[goffset];
4614 break;
4615 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004616 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004617 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004618 break;
4619 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004620 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004621 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004622 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004623 switch (simd_prefix) {
4624 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4625 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4626 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4627 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4628 }
4629 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004630 case Escape:
4631 if (ctxt->modrm > 0xbf)
4632 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4633 else
4634 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4635 break;
Nadav Amit39f062f2014-11-26 15:47:18 +02004636 case InstrDual:
4637 if ((ctxt->modrm >> 6) == 3)
4638 opcode = opcode.u.idual->mod3;
4639 else
4640 opcode = opcode.u.idual->mod012;
4641 break;
Nadav Amit2276b512015-01-26 09:32:24 +02004642 case ModeDual:
4643 if (ctxt->mode == X86EMUL_MODE_PROT64)
4644 opcode = opcode.u.mdual->mode64;
4645 else
4646 opcode = opcode.u.mdual->mode32;
4647 break;
Avi Kivity46561642011-04-24 14:09:59 +03004648 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004649 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004650 }
Avi Kivity46561642011-04-24 14:09:59 +03004651
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004652 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004653 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004654 }
4655
Paolo Bonzinie24186e2014-03-27 12:00:57 +01004656 /* Unrecognised? */
4657 if (ctxt->d == 0)
4658 return EMULATION_FAILED;
4659
Avi Kivity9dac77f2011-06-01 15:34:25 +03004660 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004661
Nadav Amit3a6095a2014-08-13 16:50:13 +03004662 if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD)))
4663 return EMULATION_FAILED;
4664
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004665 if (unlikely(ctxt->d &
Nadav Amited9aad22014-11-02 11:55:00 +02004666 (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch|
4667 No16))) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004668 /*
4669 * These are copied unconditionally here, and checked unconditionally
4670 * in x86_emulate_insn.
4671 */
4672 ctxt->check_perm = opcode.check_perm;
4673 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004674
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004675 if (ctxt->d & NotImpl)
4676 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004677
Nadav Amit58b70752014-10-24 11:35:09 +03004678 if (mode == X86EMUL_MODE_PROT64) {
4679 if (ctxt->op_bytes == 4 && (ctxt->d & Stack))
4680 ctxt->op_bytes = 8;
4681 else if (ctxt->d & NearBranch)
4682 ctxt->op_bytes = 8;
4683 }
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004684
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004685 if (ctxt->d & Op3264) {
4686 if (mode == X86EMUL_MODE_PROT64)
4687 ctxt->op_bytes = 8;
4688 else
4689 ctxt->op_bytes = 4;
4690 }
4691
Nadav Amited9aad22014-11-02 11:55:00 +02004692 if ((ctxt->d & No16) && ctxt->op_bytes == 2)
4693 ctxt->op_bytes = 4;
4694
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004695 if (ctxt->d & Sse)
4696 ctxt->op_bytes = 16;
4697 else if (ctxt->d & Mmx)
4698 ctxt->op_bytes = 8;
4699 }
Avi Kivity1253791d2011-03-29 11:41:27 +02004700
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004701 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004702 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004703 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04004704 if (!has_seg_override) {
4705 has_seg_override = true;
4706 ctxt->seg_override = ctxt->modrm_seg;
4707 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004708 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004709 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004710 if (rc != X86EMUL_CONTINUE)
4711 goto done;
4712
Bandan Das573e80f2014-04-16 12:46:13 -04004713 if (!has_seg_override)
4714 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004715
Bandan Das573e80f2014-04-16 12:46:13 -04004716 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004717
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004718 /*
4719 * Decode and fetch the source operand: register, memory
4720 * or immediate.
4721 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004722 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004723 if (rc != X86EMUL_CONTINUE)
4724 goto done;
4725
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004726 /*
4727 * Decode and fetch the second source operand: register, memory
4728 * or immediate.
4729 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004730 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004731 if (rc != X86EMUL_CONTINUE)
4732 goto done;
4733
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004734 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03004735 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004736
Bandan Das41061cd2014-04-16 12:46:14 -04004737 if (ctxt->rip_relative)
Nadav Amit1c1c35a2014-11-19 17:43:09 +02004738 ctxt->memopp->addr.mem.ea = address_mask(ctxt,
4739 ctxt->memopp->addr.mem.ea + ctxt->_eip);
Avi Kivitycb16c342011-06-19 19:21:11 +03004740
Paolo Bonzinia430c912014-10-23 14:54:14 +02004741done:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004742 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004743}
4744
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004745bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4746{
4747 return ctxt->d & PageTable;
4748}
4749
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004750static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4751{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004752 /* The second termination condition only applies for REPE
4753 * and REPNE. Test if the repeat string operation prefix is
4754 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4755 * corresponding termination condition according to:
4756 * - if REPE/REPZ and ZF = 0 then done
4757 * - if REPNE/REPNZ and ZF = 1 then done
4758 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004759 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4760 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4761 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004762 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004763 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004764 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4765 return true;
4766
4767 return false;
4768}
4769
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004770static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4771{
4772 bool fault = false;
4773
4774 ctxt->ops->get_fpu(ctxt);
4775 asm volatile("1: fwait \n\t"
4776 "2: \n\t"
4777 ".pushsection .fixup,\"ax\" \n\t"
4778 "3: \n\t"
4779 "movb $1, %[fault] \n\t"
4780 "jmp 2b \n\t"
4781 ".popsection \n\t"
4782 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004783 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004784 ctxt->ops->put_fpu(ctxt);
4785
4786 if (unlikely(fault))
4787 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4788
4789 return X86EMUL_CONTINUE;
4790}
4791
4792static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4793 struct operand *op)
4794{
4795 if (op->type == OP_MM)
4796 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4797}
4798
Avi Kivitye28bbd42013-01-04 16:18:48 +02004799static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4800{
4801 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivityb9fa4092013-02-09 11:31:48 +02004802 if (!(ctxt->d & ByteOp))
4803 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Avi Kivitye28bbd42013-01-04 16:18:48 +02004804 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004805 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
4806 [fastop]"+S"(fop)
4807 : "c"(ctxt->src2.val));
Avi Kivitye28bbd42013-01-04 16:18:48 +02004808 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004809 if (!fop) /* exception is returned in fop variable */
4810 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02004811 return X86EMUL_CONTINUE;
4812}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004813
Bandan Das14985072014-04-16 12:46:09 -04004814void init_decode_cache(struct x86_emulate_ctxt *ctxt)
4815{
Bandan Das573e80f2014-04-16 12:46:13 -04004816 memset(&ctxt->rip_relative, 0,
4817 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04004818
Bandan Das14985072014-04-16 12:46:09 -04004819 ctxt->io_read.pos = 0;
4820 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04004821 ctxt->mem_read.end = 0;
4822}
4823
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004824int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004825{
Mathias Krause0225fb52012-08-30 01:30:16 +02004826 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004827 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004828 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004829
Avi Kivity9dac77f2011-06-01 15:34:25 +03004830 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004831
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004832 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004833 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004834 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004835 goto done;
4836 }
4837
Avi Kivity9dac77f2011-06-01 15:34:25 +03004838 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004839 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004840 goto done;
4841 }
4842
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004843 if (unlikely(ctxt->d &
4844 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
4845 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4846 (ctxt->d & Undefined)) {
4847 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004848 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004849 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004850
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004851 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4852 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
4853 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004854 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004855 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004856
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004857 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
4858 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004859 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004860 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004861
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004862 if (ctxt->d & Mmx) {
4863 rc = flush_pending_x87_faults(ctxt);
4864 if (rc != X86EMUL_CONTINUE)
4865 goto done;
4866 /*
4867 * Now that we know the fpu is exception safe, we can fetch
4868 * operands from it.
4869 */
4870 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4871 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4872 if (!(ctxt->d & Mov))
4873 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4874 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004875
Bandan Das685bbf42014-04-16 12:46:10 -04004876 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004877 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4878 X86_ICPT_PRE_EXCEPT);
4879 if (rc != X86EMUL_CONTINUE)
4880 goto done;
4881 }
4882
Nadav Amit64a38292014-12-10 11:19:04 +02004883 /* Instruction can only be executed in protected mode */
4884 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
4885 rc = emulate_ud(ctxt);
4886 goto done;
4887 }
4888
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004889 /* Privileged instruction can be executed only in CPL=0 */
4890 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Nadav Amit68efa762014-06-18 17:19:35 +03004891 if (ctxt->d & PrivUD)
4892 rc = emulate_ud(ctxt);
4893 else
4894 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004895 goto done;
4896 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004897
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004898 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04004899 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004900 rc = ctxt->check_perm(ctxt);
4901 if (rc != X86EMUL_CONTINUE)
4902 goto done;
4903 }
4904
Bandan Das685bbf42014-04-16 12:46:10 -04004905 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004906 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4907 X86_ICPT_POST_EXCEPT);
4908 if (rc != X86EMUL_CONTINUE)
4909 goto done;
4910 }
4911
4912 if (ctxt->rep_prefix && (ctxt->d & String)) {
4913 /* All REP prefixes have the same first termination condition */
4914 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
4915 ctxt->eip = ctxt->_eip;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004916 ctxt->eflags &= ~EFLG_RF;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004917 goto done;
4918 }
4919 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004920 }
4921
Avi Kivity9dac77f2011-06-01 15:34:25 +03004922 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4923 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4924 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004925 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004926 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004927 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004928 }
4929
Avi Kivity9dac77f2011-06-01 15:34:25 +03004930 if (ctxt->src2.type == OP_MEM) {
4931 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4932 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004933 if (rc != X86EMUL_CONTINUE)
4934 goto done;
4935 }
4936
Avi Kivity9dac77f2011-06-01 15:34:25 +03004937 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004938 goto special_insn;
4939
4940
Avi Kivity9dac77f2011-06-01 15:34:25 +03004941 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004942 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004943 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4944 &ctxt->dst.val, ctxt->dst.bytes);
Nadav Amitc205fb72014-12-25 02:52:16 +02004945 if (rc != X86EMUL_CONTINUE) {
4946 if (rc == X86EMUL_PROPAGATE_FAULT &&
4947 ctxt->exception.vector == PF_VECTOR)
4948 ctxt->exception.error_code |= PFERR_WRITE_MASK;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004949 goto done;
Nadav Amitc205fb72014-12-25 02:52:16 +02004950 }
Avi Kivity038e51d2007-01-22 20:40:40 -08004951 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004952 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004953
Avi Kivity018a98d2007-11-27 19:30:56 +02004954special_insn:
4955
Bandan Das685bbf42014-04-16 12:46:10 -04004956 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004957 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004958 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004959 if (rc != X86EMUL_CONTINUE)
4960 goto done;
4961 }
4962
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004963 if (ctxt->rep_prefix && (ctxt->d & String))
4964 ctxt->eflags |= EFLG_RF;
4965 else
4966 ctxt->eflags &= ~EFLG_RF;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004967
Avi Kivity9dac77f2011-06-01 15:34:25 +03004968 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004969 if (ctxt->d & Fastop) {
4970 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4971 rc = fastop(ctxt, fop);
4972 if (rc != X86EMUL_CONTINUE)
4973 goto done;
4974 goto writeback;
4975 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004976 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004977 if (rc != X86EMUL_CONTINUE)
4978 goto done;
4979 goto writeback;
4980 }
4981
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004982 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004983 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004984 else if (ctxt->opcode_len == 3)
4985 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004986
Avi Kivity9dac77f2011-06-01 15:34:25 +03004987 switch (ctxt->b) {
Gleb Natapovb2833e32009-04-12 13:36:30 +03004988 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004989 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03004990 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004991 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004992 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004993 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004994 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004995 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004996 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03004997 ctxt->dst.type = OP_NONE;
4998 else
4999 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09005000 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005001 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005002 switch (ctxt->op_bytes) {
5003 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
5004 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
5005 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005006 }
5007 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005008 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005009 rc = emulate_int(ctxt, 3);
5010 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005011 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005012 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005013 break;
5014 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005015 if (ctxt->eflags & EFLG_OF)
5016 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005017 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005018 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09005019 case 0xeb: /* jmp rel short */
Nadav Amit234f3ce2014-09-18 22:39:38 +03005020 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity9dac77f2011-06-01 15:34:25 +03005021 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005022 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005023 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03005024 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03005025 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005026 case 0xf5: /* cmc */
5027 /* complement carry flag from eflags reg */
5028 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005029 break;
5030 case 0xf8: /* clc */
5031 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005032 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03005033 case 0xf9: /* stc */
5034 ctxt->eflags |= EFLG_CF;
5035 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005036 case 0xfc: /* cld */
5037 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005038 break;
5039 case 0xfd: /* std */
5040 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005041 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005042 default:
5043 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005044 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005045
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005046 if (rc != X86EMUL_CONTINUE)
5047 goto done;
5048
Avi Kivity018a98d2007-11-27 19:30:56 +02005049writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02005050 if (ctxt->d & SrcWrite) {
5051 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
5052 rc = writeback(ctxt, &ctxt->src);
5053 if (rc != X86EMUL_CONTINUE)
5054 goto done;
5055 }
Nadav Amitee212292014-06-15 16:12:58 +03005056 if (!(ctxt->d & NoWrite)) {
5057 rc = writeback(ctxt, &ctxt->dst);
5058 if (rc != X86EMUL_CONTINUE)
5059 goto done;
5060 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005061
Gleb Natapov5cd21912010-03-18 15:20:26 +02005062 /*
5063 * restore dst type in case the decoding will be reused
5064 * (happens for string instruction )
5065 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005066 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005067
Avi Kivity9dac77f2011-06-01 15:34:25 +03005068 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005069 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02005070
Avi Kivity9dac77f2011-06-01 15:34:25 +03005071 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005072 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02005073
Avi Kivity9dac77f2011-06-01 15:34:25 +03005074 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005075 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005076 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005077 if ((ctxt->d & SrcMask) == SrcSI)
5078 count = ctxt->src.count;
5079 else
5080 count = ctxt->dst.count;
Paolo Bonzini01485a22014-11-19 18:25:08 +01005081 register_address_increment(ctxt, VCPU_REGS_RCX, -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005082
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005083 if (!string_insn_completed(ctxt)) {
5084 /*
5085 * Re-enter guest when pio read ahead buffer is empty
5086 * or, if it is not used, after each 1024 iteration.
5087 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005088 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005089 (r->end == 0 || r->end != r->pos)) {
5090 /*
5091 * Reset read cache. Usually happens before
5092 * decode, but since instruction is restarted
5093 * we have to do it here.
5094 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005095 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005096 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005097 return EMULATION_RESTART;
5098 }
5099 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03005100 }
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03005101 ctxt->eflags &= ~EFLG_RF;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005102 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005103
Avi Kivity9dac77f2011-06-01 15:34:25 +03005104 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02005105
5106done:
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005107 if (rc == X86EMUL_PROPAGATE_FAULT) {
5108 WARN_ON(ctxt->exception.vector > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +02005109 ctxt->have_exception = true;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005110 }
Joerg Roedel775fde82011-04-04 12:39:24 +02005111 if (rc == X86EMUL_INTERCEPTED)
5112 return EMULATION_INTERCEPTED;
5113
Avi Kivitydd856ef2012-08-27 23:46:17 +03005114 if (rc == X86EMUL_CONTINUE)
5115 writeback_registers(ctxt);
5116
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005117 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005118
5119twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005120 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02005121 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02005122 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005123 break;
5124 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02005125 case 0x0d: /* GrpP (prefetch) */
5126 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02005127 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02005128 break;
5129 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005130 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02005131 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005132 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005133 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005134 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005135 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03005136 if (test_cc(ctxt->b, ctxt->eflags))
5137 ctxt->dst.val = ctxt->src.val;
5138 else if (ctxt->mode != X86EMUL_MODE_PROT64 ||
5139 ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005140 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08005141 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03005142 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03005143 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005144 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005145 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08005146 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005147 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08005148 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005149 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005150 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005151 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03005152 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005153 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005154 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005155 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005156 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03005157 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005158 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005159 default:
5160 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005161 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005162
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005163threebyte_insn:
5164
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005165 if (rc != X86EMUL_CONTINUE)
5166 goto done;
5167
Avi Kivity6aa8b732006-12-10 02:21:36 -08005168 goto writeback;
5169
5170cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02005171 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005172}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005173
5174void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
5175{
5176 invalidate_registers(ctxt);
5177}
5178
5179void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
5180{
5181 writeback_registers(ctxt);
5182}