blob: 997c9ebb70efc5ea601e130b433288061ab0dda1 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivityb7d491e2013-01-04 16:18:49 +020027#include <linux/stringify.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028
Avi Kivity3eeb3282010-01-21 15:31:48 +020029#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020030#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020031
Avi Kivity6aa8b732006-12-10 02:21:36 -080032/*
Avi Kivitya9945542011-09-13 10:45:41 +030033 * Operand types
34 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030035#define OpNone 0ull
36#define OpImplicit 1ull /* No generic decode */
37#define OpReg 2ull /* Register */
38#define OpMem 3ull /* Memory */
39#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
40#define OpDI 5ull /* ES:DI/EDI/RDI */
41#define OpMem64 6ull /* Memory, 64-bit */
42#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
43#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030044#define OpCL 9ull /* CL register (for shifts) */
45#define OpImmByte 10ull /* 8-bit sign extended immediate */
46#define OpOne 11ull /* Implied 1 */
Nadav Amit5e2c6882012-12-06 21:55:10 -020047#define OpImm 12ull /* Sign extended up to 32-bit immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030048#define OpMem16 13ull /* Memory operand (16-bit). */
49#define OpMem32 14ull /* Memory operand (32-bit). */
50#define OpImmU 15ull /* Immediate operand, zero extended */
51#define OpSI 16ull /* SI/ESI/RSI */
52#define OpImmFAddr 17ull /* Immediate far address */
53#define OpMemFAddr 18ull /* Far address in memory */
54#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030055#define OpES 20ull /* ES */
56#define OpCS 21ull /* CS */
57#define OpSS 22ull /* SS */
58#define OpDS 23ull /* DS */
59#define OpFS 24ull /* FS */
60#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020061#define OpMem8 26ull /* 8-bit zero extended memory operand */
Nadav Amit5e2c6882012-12-06 21:55:10 -020062#define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */
Paolo Bonzini7fa57952013-05-09 11:32:50 +020063#define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */
Avi Kivity820207c2013-02-09 11:31:45 +020064#define OpAccLo 29ull /* Low part of extended acc (AX/AX/EAX/RAX) */
65#define OpAccHi 30ull /* High part of extended acc (-/DX/EDX/RDX) */
Avi Kivitya9945542011-09-13 10:45:41 +030066
Avi Kivity0fe59122011-09-13 10:45:47 +030067#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030068#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya9945542011-09-13 10:45:41 +030069
70/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080071 * Opcode effective-address decode tables.
72 * Note that we only emulate instructions that have at least one memory
73 * operand (excluding implicit stack references). We assume that stack
74 * references and instruction fetches will never occur in special memory
75 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
76 * not be handled.
77 */
78
79/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030080#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080081/* Destination operand type. */
Avi Kivitya9945542011-09-13 10:45:41 +030082#define DstShift 1
83#define ImplicitOps (OpImplicit << DstShift)
84#define DstReg (OpReg << DstShift)
85#define DstMem (OpMem << DstShift)
86#define DstAcc (OpAcc << DstShift)
87#define DstDI (OpDI << DstShift)
88#define DstMem64 (OpMem64 << DstShift)
Nadav Amit16bebef2014-12-25 02:52:18 +020089#define DstMem16 (OpMem16 << DstShift)
Avi Kivitya9945542011-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 Kivitya9945542011-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 Kivity12537912011-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;
687 *max_size = min_t(u64, ~0u, (u64)lim + 1 - addr.ea);
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100688 if (size > *max_size)
689 goto bad;
Nadav Amit31ff6482014-11-19 17:43:13 +0200690 la &= (u32)-1;
Avi Kivity618ff152011-04-03 12:32:09 +0300691 break;
692 }
Avi Kivity1c11b372012-04-09 18:39:59 +0300693 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
694 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300695 *linear = la;
696 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300697bad:
698 if (addr.seg == VCPU_SREG_SS)
Paolo Bonzini36061892014-10-27 14:40:49 +0100699 return emulate_ss(ctxt, 0);
Avi Kivity618ff152011-04-03 12:32:09 +0300700 else
Paolo Bonzini36061892014-10-27 14:40:49 +0100701 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300702}
703
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400704static int linearize(struct x86_emulate_ctxt *ctxt,
705 struct segmented_address addr,
706 unsigned size, bool write,
707 ulong *linear)
708{
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100709 unsigned max_size;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200710 return __linearize(ctxt, addr, &max_size, size, write, false,
711 ctxt->mode, linear);
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400712}
713
Nadav Amitd50eaa12014-11-19 17:43:11 +0200714static inline int assign_eip(struct x86_emulate_ctxt *ctxt, ulong dst,
715 enum x86emul_mode mode)
716{
717 ulong linear;
718 int rc;
719 unsigned max_size;
720 struct segmented_address addr = { .seg = VCPU_SREG_CS,
721 .ea = dst };
722
723 if (ctxt->op_bytes != sizeof(unsigned long))
724 addr.ea = dst & ((1UL << (ctxt->op_bytes << 3)) - 1);
725 rc = __linearize(ctxt, addr, &max_size, 1, false, true, mode, &linear);
726 if (rc == X86EMUL_CONTINUE)
727 ctxt->_eip = addr.ea;
728 return rc;
729}
730
731static inline int assign_eip_near(struct x86_emulate_ctxt *ctxt, ulong dst)
732{
733 return assign_eip(ctxt, dst, ctxt->mode);
734}
735
736static int assign_eip_far(struct x86_emulate_ctxt *ctxt, ulong dst,
737 const struct desc_struct *cs_desc)
738{
739 enum x86emul_mode mode = ctxt->mode;
740
741#ifdef CONFIG_X86_64
742 if (ctxt->mode >= X86EMUL_MODE_PROT32 && cs_desc->l) {
743 u64 efer = 0;
744
745 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
746 if (efer & EFER_LMA)
747 mode = X86EMUL_MODE_PROT64;
748 }
749#endif
750 if (mode == X86EMUL_MODE_PROT16 || mode == X86EMUL_MODE_PROT32)
751 mode = cs_desc->d ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
752 return assign_eip(ctxt, dst, mode);
753}
754
755static inline int jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
756{
757 return assign_eip_near(ctxt, ctxt->_eip + rel);
758}
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400759
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200760static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
761 struct segmented_address addr,
762 void *data,
763 unsigned size)
764{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200765 int rc;
766 ulong linear;
767
Avi Kivity83b87952011-04-03 11:31:19 +0300768 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200769 if (rc != X86EMUL_CONTINUE)
770 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300771 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200772}
773
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900774/*
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200775 * Prefetch the remaining bytes of the instruction without crossing page
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900776 * boundary if they are not in fetch_cache yet.
777 */
Paolo Bonzini9506d572014-05-06 13:05:25 +0200778static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
Avi Kivity62266862007-11-20 13:15:52 +0200779{
Avi Kivity62266862007-11-20 13:15:52 +0200780 int rc;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100781 unsigned size, max_size;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200782 unsigned long linear;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200783 int cur_size = ctxt->fetch.end - ctxt->fetch.data;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200784 struct segmented_address addr = { .seg = VCPU_SREG_CS,
Paolo Bonzini17052f12014-05-06 16:33:01 +0200785 .ea = ctxt->eip + cur_size };
786
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100787 /*
788 * We do not know exactly how many bytes will be needed, and
789 * __linearize is expensive, so fetch as much as possible. We
790 * just have to avoid going beyond the 15 byte limit, the end
791 * of the segment, or the end of the page.
792 *
793 * __linearize is called with size 0 so that it does not do any
794 * boundary check itself. Instead, we use max_size to check
795 * against op_size.
796 */
Nadav Amitd50eaa12014-11-19 17:43:11 +0200797 rc = __linearize(ctxt, addr, &max_size, 0, false, true, ctxt->mode,
798 &linear);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200799 if (unlikely(rc != X86EMUL_CONTINUE))
800 return rc;
801
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100802 size = min_t(unsigned, 15UL ^ cur_size, max_size);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200803 size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear));
Paolo Bonzini5cfc7e02014-05-06 13:05:25 +0200804
805 /*
806 * One instruction can only straddle two pages,
807 * and one has been loaded at the beginning of
808 * x86_decode_insn. So, if not enough bytes
809 * still, we must have hit the 15-byte boundary.
810 */
811 if (unlikely(size < op_size))
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100812 return emulate_gp(ctxt, 0);
813
Paolo Bonzini17052f12014-05-06 16:33:01 +0200814 rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end,
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200815 size, &ctxt->exception);
816 if (unlikely(rc != X86EMUL_CONTINUE))
817 return rc;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200818 ctxt->fetch.end += size;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900819 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200820}
821
Paolo Bonzini9506d572014-05-06 13:05:25 +0200822static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt,
823 unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200824{
Nadav Amit08da44a2014-10-03 01:10:04 +0300825 unsigned done_size = ctxt->fetch.end - ctxt->fetch.ptr;
826
827 if (unlikely(done_size < size))
828 return __do_insn_fetch_bytes(ctxt, size - done_size);
Paolo Bonzini9506d572014-05-06 13:05:25 +0200829 else
830 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200831}
832
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900833/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900834#define insn_fetch(_type, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200835({ _type _x; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200836 \
837 rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900838 if (rc != X86EMUL_CONTINUE) \
839 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200840 ctxt->_eip += sizeof(_type); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200841 _x = *(_type __aligned(1) *) ctxt->fetch.ptr; \
842 ctxt->fetch.ptr += sizeof(_type); \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200843 _x; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900844})
845
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900846#define insn_fetch_arr(_arr, _size, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200847({ \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200848 rc = do_insn_fetch_bytes(_ctxt, _size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900849 if (rc != X86EMUL_CONTINUE) \
850 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200851 ctxt->_eip += (_size); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200852 memcpy(_arr, ctxt->fetch.ptr, _size); \
853 ctxt->fetch.ptr += (_size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900854})
855
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000856/*
857 * Given the 'reg' portion of a ModRM byte, and a register block, return a
858 * pointer into the block that addresses the relevant register.
859 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
860 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300861static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200862 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800863{
864 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200865 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800866
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300868 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
869 else
870 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800871 return p;
872}
873
874static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200875 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800876 u16 *size, unsigned long *address, int op_bytes)
877{
878 int rc;
879
880 if (op_bytes == 2)
881 op_bytes = 3;
882 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200883 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900884 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800885 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200886 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200887 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800888 return rc;
889}
890
Avi Kivity34b77652013-01-19 19:51:56 +0200891FASTOP2(add);
892FASTOP2(or);
893FASTOP2(adc);
894FASTOP2(sbb);
895FASTOP2(and);
896FASTOP2(sub);
897FASTOP2(xor);
898FASTOP2(cmp);
899FASTOP2(test);
900
Avi Kivityb9fa4092013-02-09 11:31:48 +0200901FASTOP1SRC2(mul, mul_ex);
902FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200903FASTOP1SRC2EX(div, div_ex);
904FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +0200905
Avi Kivity34b77652013-01-19 19:51:56 +0200906FASTOP3WCL(shld);
907FASTOP3WCL(shrd);
908
909FASTOP2W(imul);
910
911FASTOP1(not);
912FASTOP1(neg);
913FASTOP1(inc);
914FASTOP1(dec);
915
916FASTOP2CL(rol);
917FASTOP2CL(ror);
918FASTOP2CL(rcl);
919FASTOP2CL(rcr);
920FASTOP2CL(shl);
921FASTOP2CL(shr);
922FASTOP2CL(sar);
923
924FASTOP2W(bsf);
925FASTOP2W(bsr);
926FASTOP2W(bt);
927FASTOP2W(bts);
928FASTOP2W(btr);
929FASTOP2W(btc);
930
Avi Kivitye47a5f52013-02-09 11:31:51 +0200931FASTOP2(xadd);
932
Nadav Amit5aca3722014-11-02 11:54:50 +0200933FASTOP2R(cmp, cmp_r);
934
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200935static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300936{
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200937 u8 rc;
938 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300939
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200940 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +0200941 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200942 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
943 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300944}
945
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300946static void fetch_register_operand(struct operand *op)
947{
948 switch (op->bytes) {
949 case 1:
950 op->val = *(u8 *)op->addr.reg;
951 break;
952 case 2:
953 op->val = *(u16 *)op->addr.reg;
954 break;
955 case 4:
956 op->val = *(u32 *)op->addr.reg;
957 break;
958 case 8:
959 op->val = *(u64 *)op->addr.reg;
960 break;
961 }
962}
963
Avi Kivity12537912011-03-29 11:41:27 +0200964static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
965{
966 ctxt->ops->get_fpu(ctxt);
967 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200968 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
969 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
970 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
971 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
972 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
973 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
974 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
975 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200976#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200977 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
978 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
979 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
980 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
981 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
982 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
983 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
984 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200985#endif
986 default: BUG();
987 }
988 ctxt->ops->put_fpu(ctxt);
989}
990
991static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
992 int reg)
993{
994 ctxt->ops->get_fpu(ctxt);
995 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200996 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
997 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
998 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
999 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
1000 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
1001 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
1002 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
1003 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001004#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001005 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
1006 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
1007 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
1008 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
1009 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
1010 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
1011 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
1012 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001013#endif
1014 default: BUG();
1015 }
1016 ctxt->ops->put_fpu(ctxt);
1017}
1018
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001019static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1020{
1021 ctxt->ops->get_fpu(ctxt);
1022 switch (reg) {
1023 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1024 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1025 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1026 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1027 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1028 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1029 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1030 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1031 default: BUG();
1032 }
1033 ctxt->ops->put_fpu(ctxt);
1034}
1035
1036static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1037{
1038 ctxt->ops->get_fpu(ctxt);
1039 switch (reg) {
1040 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1041 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1042 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1043 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1044 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1045 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1046 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1047 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1048 default: BUG();
1049 }
1050 ctxt->ops->put_fpu(ctxt);
1051}
1052
Gleb Natapov045a2822012-12-20 16:57:43 +02001053static int em_fninit(struct x86_emulate_ctxt *ctxt)
1054{
1055 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1056 return emulate_nm(ctxt);
1057
1058 ctxt->ops->get_fpu(ctxt);
1059 asm volatile("fninit");
1060 ctxt->ops->put_fpu(ctxt);
1061 return X86EMUL_CONTINUE;
1062}
1063
1064static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1065{
1066 u16 fcw;
1067
1068 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1069 return emulate_nm(ctxt);
1070
1071 ctxt->ops->get_fpu(ctxt);
1072 asm volatile("fnstcw %0": "+m"(fcw));
1073 ctxt->ops->put_fpu(ctxt);
1074
Gleb Natapov045a2822012-12-20 16:57:43 +02001075 ctxt->dst.val = fcw;
1076
1077 return X86EMUL_CONTINUE;
1078}
1079
1080static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1081{
1082 u16 fsw;
1083
1084 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1085 return emulate_nm(ctxt);
1086
1087 ctxt->ops->get_fpu(ctxt);
1088 asm volatile("fnstsw %0": "+m"(fsw));
1089 ctxt->ops->put_fpu(ctxt);
1090
Gleb Natapov045a2822012-12-20 16:57:43 +02001091 ctxt->dst.val = fsw;
1092
1093 return X86EMUL_CONTINUE;
1094}
1095
Avi Kivity12537912011-03-29 11:41:27 +02001096static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001097 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001098{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001099 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001100
Avi Kivity9dac77f2011-06-01 15:34:25 +03001101 if (!(ctxt->d & ModRM))
1102 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +02001103
Avi Kivity9dac77f2011-06-01 15:34:25 +03001104 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001105 op->type = OP_XMM;
1106 op->bytes = 16;
1107 op->addr.xmm = reg;
1108 read_sse_reg(ctxt, &op->vec_val, reg);
1109 return;
1110 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001111 if (ctxt->d & Mmx) {
1112 reg &= 7;
1113 op->type = OP_MM;
1114 op->bytes = 8;
1115 op->addr.mm = reg;
1116 return;
1117 }
Avi Kivity12537912011-03-29 11:41:27 +02001118
Avi Kivity3c118e22007-10-31 10:27:04 +02001119 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001120 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1121 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1122
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001123 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001124 op->orig_val = op->val;
1125}
1126
Avi Kivitya6e34072012-06-10 17:15:39 +03001127static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1128{
1129 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1130 ctxt->modrm_seg = VCPU_SREG_SS;
1131}
1132
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001133static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001134 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001135{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001136 u8 sib;
Bandan Das02357bd2014-04-16 12:46:11 -04001137 int index_reg, base_reg, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001138 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001139 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001140
Bandan Das02357bd2014-04-16 12:46:11 -04001141 ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */
1142 index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */
1143 base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001144
Bandan Das02357bd2014-04-16 12:46:11 -04001145 ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001146 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
Bandan Das02357bd2014-04-16 12:46:11 -04001147 ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001148 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001149
Nadav Amit9b88ae92014-05-25 23:05:21 +03001150 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001151 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001152 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001153 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001154 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001155 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001156 op->type = OP_XMM;
1157 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001158 op->addr.xmm = ctxt->modrm_rm;
1159 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001160 return rc;
1161 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001162 if (ctxt->d & Mmx) {
1163 op->type = OP_MM;
1164 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001165 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001166 return rc;
1167 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001168 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001169 return rc;
1170 }
1171
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001172 op->type = OP_MEM;
1173
Avi Kivity9dac77f2011-06-01 15:34:25 +03001174 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001175 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1176 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1177 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1178 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001179
1180 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001181 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001182 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001183 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001184 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001185 break;
1186 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001187 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001188 break;
1189 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001190 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001191 break;
1192 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001193 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001194 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001195 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001196 break;
1197 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001198 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001199 break;
1200 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001201 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001202 break;
1203 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001204 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001205 break;
1206 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001207 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001208 break;
1209 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001210 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001211 break;
1212 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001213 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001214 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001215 break;
1216 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001217 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001218 break;
1219 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001220 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1221 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1222 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001223 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001224 } else {
1225 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001226 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001227 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001228 index_reg |= (sib >> 3) & 7;
1229 base_reg |= sib & 7;
1230 scale = sib >> 6;
1231
Avi Kivity9dac77f2011-06-01 15:34:25 +03001232 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001233 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001234 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001235 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001236 adjust_modrm_seg(ctxt, base_reg);
Nadav Amitab708092014-12-25 02:52:21 +02001237 /* Increment ESP on POP [ESP] */
1238 if ((ctxt->d & IncSP) &&
1239 base_reg == VCPU_REGS_RSP)
1240 modrm_ea += ctxt->op_bytes;
Avi Kivitya6e34072012-06-10 17:15:39 +03001241 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001242 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001243 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001244 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Nadav Amit5b38ab82014-11-02 11:54:41 +02001245 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity84411d82008-06-15 21:53:26 -07001246 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001247 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001248 } else {
1249 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001250 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001251 adjust_modrm_seg(ctxt, base_reg);
1252 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001253 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001254 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001255 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001256 break;
1257 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001258 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001259 break;
1260 }
1261 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001262 op->addr.mem.ea = modrm_ea;
Bandan Das41061cd2014-04-16 12:46:14 -04001263 if (ctxt->ad_bytes != 8)
1264 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
1265
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001266done:
1267 return rc;
1268}
1269
1270static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001271 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001272{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001273 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001274
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001275 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001276 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001277 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001278 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001279 break;
1280 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001281 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001282 break;
1283 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001284 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001285 break;
1286 }
1287done:
1288 return rc;
1289}
1290
Avi Kivity9dac77f2011-06-01 15:34:25 +03001291static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001292{
Sheng Yang7129eec2010-09-28 16:33:32 +08001293 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001294
Avi Kivity9dac77f2011-06-01 15:34:25 +03001295 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001296 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001297
Avi Kivity9dac77f2011-06-01 15:34:25 +03001298 if (ctxt->src.bytes == 2)
1299 sv = (s16)ctxt->src.val & (s16)mask;
1300 else if (ctxt->src.bytes == 4)
1301 sv = (s32)ctxt->src.val & (s32)mask;
Nadav Amit7dec5602014-06-15 16:12:57 +03001302 else
1303 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001304
Nadav Amit1c1c35a2014-11-19 17:43:09 +02001305 ctxt->dst.addr.mem.ea = address_mask(ctxt,
1306 ctxt->dst.addr.mem.ea + (sv >> 3));
Wei Yongjun35c843c2010-08-09 11:34:56 +08001307 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001308
1309 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001310 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001311}
1312
Gleb Natapov9de41572010-04-28 19:15:22 +03001313static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001314 unsigned long addr, void *dest, unsigned size)
1315{
1316 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001317 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001318
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001319 if (mc->pos < mc->end)
1320 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001321
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001322 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001323
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001324 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1325 &ctxt->exception);
1326 if (rc != X86EMUL_CONTINUE)
1327 return rc;
1328
1329 mc->end += size;
1330
1331read_cached:
1332 memcpy(dest, mc->data + mc->pos, size);
1333 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001334 return X86EMUL_CONTINUE;
1335}
1336
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001337static int segmented_read(struct x86_emulate_ctxt *ctxt,
1338 struct segmented_address addr,
1339 void *data,
1340 unsigned size)
1341{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001342 int rc;
1343 ulong linear;
1344
Avi Kivity83b87952011-04-03 11:31:19 +03001345 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001346 if (rc != X86EMUL_CONTINUE)
1347 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001348 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001349}
1350
1351static int segmented_write(struct x86_emulate_ctxt *ctxt,
1352 struct segmented_address addr,
1353 const void *data,
1354 unsigned size)
1355{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001356 int rc;
1357 ulong linear;
1358
Avi Kivity83b87952011-04-03 11:31:19 +03001359 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001360 if (rc != X86EMUL_CONTINUE)
1361 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001362 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1363 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001364}
1365
1366static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1367 struct segmented_address addr,
1368 const void *orig_data, const void *data,
1369 unsigned size)
1370{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001371 int rc;
1372 ulong linear;
1373
Avi Kivity83b87952011-04-03 11:31:19 +03001374 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001375 if (rc != X86EMUL_CONTINUE)
1376 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001377 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1378 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001379}
1380
Gleb Natapov7b262e92010-03-18 15:20:27 +02001381static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001382 unsigned int size, unsigned short port,
1383 void *dest)
1384{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001385 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001386
1387 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001388 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001389 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001390 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001391 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001392 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1393 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Mark Rustadb55a8142014-07-25 06:27:05 -07001394 n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001395 if (n == 0)
1396 n = 1;
1397 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001398 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001399 return 0;
1400 rc->end = n * size;
1401 }
1402
Nadav Amite6e39f02014-04-18 03:35:10 +03001403 if (ctxt->rep_prefix && (ctxt->d & String) &&
1404 !(ctxt->eflags & EFLG_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001405 ctxt->dst.data = rc->data + rc->pos;
1406 ctxt->dst.type = OP_MEM_STR;
1407 ctxt->dst.count = (rc->end - rc->pos) / size;
1408 rc->pos = rc->end;
1409 } else {
1410 memcpy(dest, rc->data + rc->pos, size);
1411 rc->pos += size;
1412 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001413 return 1;
1414}
1415
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001416static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1417 u16 index, struct desc_struct *desc)
1418{
1419 struct desc_ptr dt;
1420 ulong addr;
1421
1422 ctxt->ops->get_idt(ctxt, &dt);
1423
1424 if (dt.size < index * 8 + 7)
1425 return emulate_gp(ctxt, index << 3 | 0x2);
1426
1427 addr = dt.address + index * 8;
1428 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1429 &ctxt->exception);
1430}
1431
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001432static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001433 u16 selector, struct desc_ptr *dt)
1434{
Mathias Krause0225fb52012-08-30 01:30:16 +02001435 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001436 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001437
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001438 if (selector & 1 << 2) {
1439 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001440 u16 sel;
1441
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001442 memset (dt, 0, sizeof *dt);
Nadav Amit2eedcac2014-06-02 18:34:05 +03001443 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1444 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001445 return;
1446
1447 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001448 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001449 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001450 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001451}
1452
Nadav Amitedccda72014-12-25 02:52:23 +02001453static int get_descriptor_ptr(struct x86_emulate_ctxt *ctxt,
1454 u16 selector, ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001455{
1456 struct desc_ptr dt;
1457 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001458 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001459
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001460 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001461
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001462 if (dt.size < index * 8 + 7)
1463 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001464
1465 addr = dt.address + index * 8;
Nadav Amitedccda72014-12-25 02:52:23 +02001466
1467#ifdef CONFIG_X86_64
1468 if (addr >> 32 != 0) {
1469 u64 efer = 0;
1470
1471 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1472 if (!(efer & EFER_LMA))
1473 addr &= (u32)-1;
1474 }
1475#endif
1476
1477 *desc_addr_p = addr;
1478 return X86EMUL_CONTINUE;
1479}
1480
1481/* allowed just for 8 bytes segments */
1482static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1483 u16 selector, struct desc_struct *desc,
1484 ulong *desc_addr_p)
1485{
1486 int rc;
1487
1488 rc = get_descriptor_ptr(ctxt, selector, desc_addr_p);
1489 if (rc != X86EMUL_CONTINUE)
1490 return rc;
1491
1492 return ctxt->ops->read_std(ctxt, *desc_addr_p, desc, sizeof(*desc),
1493 &ctxt->exception);
1494}
1495
1496/* allowed just for 8 bytes segments */
1497static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1498 u16 selector, struct desc_struct *desc)
1499{
1500 int rc;
1501 ulong addr;
1502
1503 rc = get_descriptor_ptr(ctxt, selector, &addr);
1504 if (rc != X86EMUL_CONTINUE)
1505 return rc;
1506
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001507 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1508 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001509}
1510
Gleb Natapov5601d052011-03-07 14:55:06 +02001511/* Does not support long mode */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001512static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Nadav Amitd1442d82014-09-18 22:39:39 +03001513 u16 selector, int seg, u8 cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001514 enum x86_transfer_type transfer,
Nadav Amitd1442d82014-09-18 22:39:39 +03001515 struct desc_struct *desc)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001516{
Avi Kivity869be992012-06-13 16:30:53 +03001517 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001518 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001519 unsigned err_vec = GP_VECTOR;
1520 u32 err_code = 0;
1521 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001522 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001523 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001524 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001525 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001526
1527 memset(&seg_desc, 0, sizeof seg_desc);
1528
Kevin Wolff8da94e2013-04-11 14:06:03 +02001529 if (ctxt->mode == X86EMUL_MODE_REAL) {
1530 /* set real mode segment descriptor (keep limit etc. for
1531 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001532 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001533 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001534 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001535 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1536 /* VM86 needs a clean new segment descriptor */
1537 set_desc_base(&seg_desc, selector << 4);
1538 set_desc_limit(&seg_desc, 0xffff);
1539 seg_desc.type = 3;
1540 seg_desc.p = 1;
1541 seg_desc.s = 1;
1542 seg_desc.dpl = 3;
1543 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001544 }
1545
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001546 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001547
1548 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1549 if ((seg == VCPU_SREG_CS
1550 || (seg == VCPU_SREG_SS
1551 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1552 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001553 && null_selector)
1554 goto exception;
1555
1556 /* TR should be in GDT only */
1557 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1558 goto exception;
1559
1560 if (null_selector) /* for NULL selector skip all following checks */
1561 goto load;
1562
Avi Kivitye9194642012-06-13 16:29:39 +03001563 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001564 if (ret != X86EMUL_CONTINUE)
1565 return ret;
1566
1567 err_code = selector & 0xfffc;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001568 err_vec = (transfer == X86_TRANSFER_TASK_SWITCH) ? TS_VECTOR :
1569 GP_VECTOR;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001570
Guo Chaofc058682012-06-28 15:19:51 +08001571 /* can't load system descriptor into segment selector */
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001572 if (seg <= VCPU_SREG_GS && !seg_desc.s) {
1573 if (transfer == X86_TRANSFER_CALL_JMP)
1574 return X86EMUL_UNHANDLEABLE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001575 goto exception;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001576 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001577
1578 if (!seg_desc.p) {
1579 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1580 goto exception;
1581 }
1582
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001583 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001584
1585 switch (seg) {
1586 case VCPU_SREG_SS:
1587 /*
1588 * segment is not a writable data segment or segment
1589 * selector's RPL != CPL or segment selector's RPL != CPL
1590 */
1591 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1592 goto exception;
1593 break;
1594 case VCPU_SREG_CS:
1595 if (!(seg_desc.type & 8))
1596 goto exception;
1597
1598 if (seg_desc.type & 4) {
1599 /* conforming */
1600 if (dpl > cpl)
1601 goto exception;
1602 } else {
1603 /* nonconforming */
1604 if (rpl > cpl || dpl != cpl)
1605 goto exception;
1606 }
Nadav Amit040c8dc2014-09-18 22:39:43 +03001607 /* in long-mode d/b must be clear if l is set */
1608 if (seg_desc.d && seg_desc.l) {
1609 u64 efer = 0;
1610
1611 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1612 if (efer & EFER_LMA)
1613 goto exception;
1614 }
1615
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001616 /* CS(RPL) <- CPL */
1617 selector = (selector & 0xfffc) | cpl;
1618 break;
1619 case VCPU_SREG_TR:
1620 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1621 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001622 old_desc = seg_desc;
1623 seg_desc.type |= 2; /* busy */
1624 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1625 sizeof(seg_desc), &ctxt->exception);
1626 if (ret != X86EMUL_CONTINUE)
1627 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001628 break;
1629 case VCPU_SREG_LDTR:
1630 if (seg_desc.s || seg_desc.type != 2)
1631 goto exception;
1632 break;
1633 default: /* DS, ES, FS, or GS */
1634 /*
1635 * segment is not a data or readable code segment or
1636 * ((segment is a data or nonconforming code segment)
1637 * and (both RPL and CPL > DPL))
1638 */
1639 if ((seg_desc.type & 0xa) == 0x8 ||
1640 (((seg_desc.type & 0xc) != 0xc) &&
1641 (rpl > dpl && cpl > dpl)))
1642 goto exception;
1643 break;
1644 }
1645
1646 if (seg_desc.s) {
1647 /* mark segment as accessed */
Nadav Amite2cefa72014-12-25 02:52:22 +02001648 if (!(seg_desc.type & 1)) {
1649 seg_desc.type |= 1;
1650 ret = write_segment_descriptor(ctxt, selector,
1651 &seg_desc);
1652 if (ret != X86EMUL_CONTINUE)
1653 return ret;
1654 }
Nadav Amite37a75a2014-06-02 18:34:04 +03001655 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
1656 ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3,
1657 sizeof(base3), &ctxt->exception);
1658 if (ret != X86EMUL_CONTINUE)
1659 return ret;
Nadav Amit9a9abf62014-11-02 11:54:56 +02001660 if (is_noncanonical_address(get_desc_base(&seg_desc) |
1661 ((u64)base3 << 32)))
1662 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001663 }
1664load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001665 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Nadav Amitd1442d82014-09-18 22:39:39 +03001666 if (desc)
1667 *desc = seg_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001668 return X86EMUL_CONTINUE;
1669exception:
Paolo Bonzini592f0852014-08-20 10:05:08 +02001670 return emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001671}
1672
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001673static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1674 u16 selector, int seg)
1675{
1676 u8 cpl = ctxt->ops->cpl(ctxt);
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001677 return __load_segment_descriptor(ctxt, selector, seg, cpl,
1678 X86_TRANSFER_NONE, NULL);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001679}
1680
Wei Yongjun31be40b2010-08-17 09:17:30 +08001681static void write_register_operand(struct operand *op)
1682{
1683 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1684 switch (op->bytes) {
1685 case 1:
1686 *(u8 *)op->addr.reg = (u8)op->val;
1687 break;
1688 case 2:
1689 *(u16 *)op->addr.reg = (u16)op->val;
1690 break;
1691 case 4:
1692 *op->addr.reg = (u32)op->val;
1693 break; /* 64b: zero-extend */
1694 case 8:
1695 *op->addr.reg = op->val;
1696 break;
1697 }
1698}
1699
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001700static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001701{
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001702 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001703 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001704 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001705 break;
1706 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001707 if (ctxt->lock_prefix)
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001708 return segmented_cmpxchg(ctxt,
1709 op->addr.mem,
1710 &op->orig_val,
1711 &op->val,
1712 op->bytes);
1713 else
1714 return segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001715 op->addr.mem,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001716 &op->val,
1717 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001718 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001719 case OP_MEM_STR:
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001720 return segmented_write(ctxt,
1721 op->addr.mem,
1722 op->data,
1723 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001724 break;
Avi Kivity12537912011-03-29 11:41:27 +02001725 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001726 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001727 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001728 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001729 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001730 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001731 case OP_NONE:
1732 /* no writeback */
1733 break;
1734 default:
1735 break;
1736 }
1737 return X86EMUL_CONTINUE;
1738}
1739
Avi Kivity51ddff52012-06-12 20:19:40 +03001740static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001741{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001742 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001743
Avi Kivity5ad105e2012-08-19 14:34:31 +03001744 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001745 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001746 addr.seg = VCPU_SREG_SS;
1747
Avi Kivity51ddff52012-06-12 20:19:40 +03001748 return segmented_write(ctxt, addr, data, bytes);
1749}
1750
1751static int em_push(struct x86_emulate_ctxt *ctxt)
1752{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001753 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001754 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001755 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001756}
1757
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001758static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001759 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001760{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001761 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001762 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001763
Avi Kivitydd856ef2012-08-27 23:46:17 +03001764 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001765 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001766 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001767 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001768 return rc;
1769
Avi Kivity5ad105e2012-08-19 14:34:31 +03001770 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001771 return rc;
1772}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001773
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001774static int em_pop(struct x86_emulate_ctxt *ctxt)
1775{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001776 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001777}
1778
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001779static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001780 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001781{
1782 int rc;
1783 unsigned long val, change_mask;
1784 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001785 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001786
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001787 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001788 if (rc != X86EMUL_CONTINUE)
1789 return rc;
1790
1791 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
Nadav Amit163b1352014-07-21 14:37:28 +03001792 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_AC | EFLG_ID;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001793
1794 switch(ctxt->mode) {
1795 case X86EMUL_MODE_PROT64:
1796 case X86EMUL_MODE_PROT32:
1797 case X86EMUL_MODE_PROT16:
1798 if (cpl == 0)
1799 change_mask |= EFLG_IOPL;
1800 if (cpl <= iopl)
1801 change_mask |= EFLG_IF;
1802 break;
1803 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001804 if (iopl < 3)
1805 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001806 change_mask |= EFLG_IF;
1807 break;
1808 default: /* real mode */
1809 change_mask |= (EFLG_IOPL | EFLG_IF);
1810 break;
1811 }
1812
1813 *(unsigned long *)dest =
1814 (ctxt->eflags & ~change_mask) | (val & change_mask);
1815
1816 return rc;
1817}
1818
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001819static int em_popf(struct x86_emulate_ctxt *ctxt)
1820{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001821 ctxt->dst.type = OP_REG;
1822 ctxt->dst.addr.reg = &ctxt->eflags;
1823 ctxt->dst.bytes = ctxt->op_bytes;
1824 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001825}
1826
Avi Kivity612e89f2012-06-12 20:03:23 +03001827static int em_enter(struct x86_emulate_ctxt *ctxt)
1828{
1829 int rc;
1830 unsigned frame_size = ctxt->src.val;
1831 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001832 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001833
1834 if (nesting_level)
1835 return X86EMUL_UNHANDLEABLE;
1836
Avi Kivitydd856ef2012-08-27 23:46:17 +03001837 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1838 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001839 if (rc != X86EMUL_CONTINUE)
1840 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001841 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001842 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001843 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1844 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001845 stack_mask(ctxt));
1846 return X86EMUL_CONTINUE;
1847}
1848
Avi Kivityf47cfa32012-06-07 17:49:24 +03001849static int em_leave(struct x86_emulate_ctxt *ctxt)
1850{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001851 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001852 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001853 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001854}
1855
Avi Kivity1cd196e2011-09-13 10:45:51 +03001856static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001857{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001858 int seg = ctxt->src2.val;
1859
Avi Kivity9dac77f2011-06-01 15:34:25 +03001860 ctxt->src.val = get_segment_selector(ctxt, seg);
Nadav Amit0fcc2072014-11-02 11:54:51 +02001861 if (ctxt->op_bytes == 4) {
1862 rsp_increment(ctxt, -2);
1863 ctxt->op_bytes = 2;
1864 }
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001865
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001866 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001867}
1868
Avi Kivity1cd196e2011-09-13 10:45:51 +03001869static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001870{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001871 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001872 unsigned long selector;
1873 int rc;
1874
Nadav Amit3313bc42014-12-25 02:52:17 +02001875 rc = emulate_pop(ctxt, &selector, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001876 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001877 return rc;
1878
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001879 if (ctxt->modrm_reg == VCPU_SREG_SS)
1880 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Nadav Amit3313bc42014-12-25 02:52:17 +02001881 if (ctxt->op_bytes > 2)
1882 rsp_increment(ctxt, ctxt->op_bytes - 2);
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001883
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001884 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001885 return rc;
1886}
1887
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001888static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001889{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001890 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001891 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001892 int reg = VCPU_REGS_RAX;
1893
1894 while (reg <= VCPU_REGS_RDI) {
1895 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001896 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001897
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001898 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001899 if (rc != X86EMUL_CONTINUE)
1900 return rc;
1901
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001902 ++reg;
1903 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001904
Wei Yongjunc37eda12010-06-15 09:03:33 +08001905 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001906}
1907
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001908static int em_pushf(struct x86_emulate_ctxt *ctxt)
1909{
Nadav Amitbc397a62014-12-10 11:19:03 +02001910 ctxt->src.val = (unsigned long)ctxt->eflags & ~EFLG_VM;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001911 return em_push(ctxt);
1912}
1913
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001914static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001915{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001916 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001917 int reg = VCPU_REGS_RDI;
1918
1919 while (reg >= VCPU_REGS_RAX) {
1920 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001921 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001922 --reg;
1923 }
1924
Avi Kivitydd856ef2012-08-27 23:46:17 +03001925 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001926 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001927 break;
1928 --reg;
1929 }
1930 return rc;
1931}
1932
Avi Kivitydd856ef2012-08-27 23:46:17 +03001933static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001934{
Mathias Krause0225fb52012-08-30 01:30:16 +02001935 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001936 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001937 struct desc_ptr dt;
1938 gva_t cs_addr;
1939 gva_t eip_addr;
1940 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001941
1942 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001943 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001944 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001945 if (rc != X86EMUL_CONTINUE)
1946 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001947
1948 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1949
Avi Kivity9dac77f2011-06-01 15:34:25 +03001950 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001951 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001952 if (rc != X86EMUL_CONTINUE)
1953 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001954
Avi Kivity9dac77f2011-06-01 15:34:25 +03001955 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001956 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001957 if (rc != X86EMUL_CONTINUE)
1958 return rc;
1959
Avi Kivity4bff1e862011-04-20 13:37:53 +03001960 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001961
1962 eip_addr = dt.address + (irq << 2);
1963 cs_addr = dt.address + (irq << 2) + 2;
1964
Avi Kivity0f65dd72011-04-20 13:37:53 +03001965 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001966 if (rc != X86EMUL_CONTINUE)
1967 return rc;
1968
Avi Kivity0f65dd72011-04-20 13:37:53 +03001969 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001970 if (rc != X86EMUL_CONTINUE)
1971 return rc;
1972
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001973 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001974 if (rc != X86EMUL_CONTINUE)
1975 return rc;
1976
Avi Kivity9dac77f2011-06-01 15:34:25 +03001977 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001978
1979 return rc;
1980}
1981
Avi Kivitydd856ef2012-08-27 23:46:17 +03001982int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1983{
1984 int rc;
1985
1986 invalidate_registers(ctxt);
1987 rc = __emulate_int_real(ctxt, irq);
1988 if (rc == X86EMUL_CONTINUE)
1989 writeback_registers(ctxt);
1990 return rc;
1991}
1992
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001993static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001994{
1995 switch(ctxt->mode) {
1996 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001997 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001998 case X86EMUL_MODE_VM86:
1999 case X86EMUL_MODE_PROT16:
2000 case X86EMUL_MODE_PROT32:
2001 case X86EMUL_MODE_PROT64:
2002 default:
2003 /* Protected mode interrupts unimplemented yet */
2004 return X86EMUL_UNHANDLEABLE;
2005 }
2006}
2007
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002008static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002009{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002010 int rc = X86EMUL_CONTINUE;
2011 unsigned long temp_eip = 0;
2012 unsigned long temp_eflags = 0;
2013 unsigned long cs = 0;
2014 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
2015 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
2016 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
2017 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
2018
2019 /* TODO: Add stack limit check */
2020
Avi Kivity9dac77f2011-06-01 15:34:25 +03002021 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002022
2023 if (rc != X86EMUL_CONTINUE)
2024 return rc;
2025
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002026 if (temp_eip & ~0xffff)
2027 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002028
Avi Kivity9dac77f2011-06-01 15:34:25 +03002029 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002030
2031 if (rc != X86EMUL_CONTINUE)
2032 return rc;
2033
Avi Kivity9dac77f2011-06-01 15:34:25 +03002034 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002035
2036 if (rc != X86EMUL_CONTINUE)
2037 return rc;
2038
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002039 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002040
2041 if (rc != X86EMUL_CONTINUE)
2042 return rc;
2043
Avi Kivity9dac77f2011-06-01 15:34:25 +03002044 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002045
2046
Avi Kivity9dac77f2011-06-01 15:34:25 +03002047 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002048 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03002049 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002050 ctxt->eflags &= ~0xffff;
2051 ctxt->eflags |= temp_eflags;
2052 }
2053
2054 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
2055 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
Nadav Amit801806d2015-01-26 09:32:23 +02002056 ctxt->ops->set_nmi_mask(ctxt, false);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002057
2058 return rc;
2059}
2060
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002061static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002062{
2063 switch(ctxt->mode) {
2064 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002065 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002066 case X86EMUL_MODE_VM86:
2067 case X86EMUL_MODE_PROT16:
2068 case X86EMUL_MODE_PROT32:
2069 case X86EMUL_MODE_PROT64:
2070 default:
2071 /* iret from protected mode unimplemented yet */
2072 return X86EMUL_UNHANDLEABLE;
2073 }
2074}
2075
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002076static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2077{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002078 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002079 unsigned short sel, old_sel;
2080 struct desc_struct old_desc, new_desc;
2081 const struct x86_emulate_ops *ops = ctxt->ops;
2082 u8 cpl = ctxt->ops->cpl(ctxt);
2083
2084 /* Assignment of RIP may only fail in 64-bit mode */
2085 if (ctxt->mode == X86EMUL_MODE_PROT64)
2086 ops->get_segment(ctxt, &old_sel, &old_desc, NULL,
2087 VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002088
Avi Kivity9dac77f2011-06-01 15:34:25 +03002089 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002090
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002091 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
2092 X86_TRANSFER_CALL_JMP,
Nadav Amitd1442d82014-09-18 22:39:39 +03002093 &new_desc);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002094 if (rc != X86EMUL_CONTINUE)
2095 return rc;
2096
Nadav Amitd50eaa12014-11-19 17:43:11 +02002097 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03002098 if (rc != X86EMUL_CONTINUE) {
Nadav Amit7e46ddd2014-10-28 00:03:43 +02002099 WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64);
Nadav Amitd1442d82014-09-18 22:39:39 +03002100 /* assigning eip failed; restore the old cs */
2101 ops->set_segment(ctxt, old_sel, &old_desc, 0, VCPU_SREG_CS);
2102 return rc;
2103 }
2104 return rc;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002105}
2106
Nadav Amitf7784042014-09-18 22:39:41 +03002107static int em_jmp_abs(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002108{
Nadav Amitf7784042014-09-18 22:39:41 +03002109 return assign_eip_near(ctxt, ctxt->src.val);
2110}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002111
Nadav Amitf7784042014-09-18 22:39:41 +03002112static int em_call_near_abs(struct x86_emulate_ctxt *ctxt)
2113{
2114 int rc;
2115 long int old_eip;
2116
2117 old_eip = ctxt->_eip;
2118 rc = assign_eip_near(ctxt, ctxt->src.val);
2119 if (rc != X86EMUL_CONTINUE)
2120 return rc;
2121 ctxt->src.val = old_eip;
2122 rc = em_push(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002123 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002124}
2125
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002126static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002127{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002128 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002129
Nadav Amitaaa05f22014-06-02 18:34:10 +03002130 if (ctxt->dst.bytes == 16)
2131 return X86EMUL_UNHANDLEABLE;
2132
Avi Kivitydd856ef2012-08-27 23:46:17 +03002133 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2134 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2135 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2136 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002137 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002138 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002139 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2140 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002141
Laurent Vivier05f086f2007-09-24 11:10:55 +02002142 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002143 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002144 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002145}
2146
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002147static int em_ret(struct x86_emulate_ctxt *ctxt)
2148{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002149 int rc;
2150 unsigned long eip;
2151
2152 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
2153 if (rc != X86EMUL_CONTINUE)
2154 return rc;
2155
2156 return assign_eip_near(ctxt, eip);
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002157}
2158
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002159static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002160{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002161 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002162 unsigned long eip, cs;
2163 u16 old_cs;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002164 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amitd1442d82014-09-18 22:39:39 +03002165 struct desc_struct old_desc, new_desc;
2166 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002167
Nadav Amitd1442d82014-09-18 22:39:39 +03002168 if (ctxt->mode == X86EMUL_MODE_PROT64)
2169 ops->get_segment(ctxt, &old_cs, &old_desc, NULL,
2170 VCPU_SREG_CS);
2171
2172 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002173 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002174 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002175 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002176 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002177 return rc;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002178 /* Outer-privilege level return is not implemented */
2179 if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2180 return X86EMUL_UNHANDLEABLE;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002181 rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, cpl,
2182 X86_TRANSFER_RET,
Nadav Amitd1442d82014-09-18 22:39:39 +03002183 &new_desc);
2184 if (rc != X86EMUL_CONTINUE)
2185 return rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +02002186 rc = assign_eip_far(ctxt, eip, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03002187 if (rc != X86EMUL_CONTINUE) {
Nadav Amit7e46ddd2014-10-28 00:03:43 +02002188 WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64);
Nadav Amitd1442d82014-09-18 22:39:39 +03002189 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
2190 }
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002191 return rc;
2192}
2193
Bruce Rogers32611072013-09-09 09:40:20 -06002194static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2195{
2196 int rc;
2197
2198 rc = em_ret_far(ctxt);
2199 if (rc != X86EMUL_CONTINUE)
2200 return rc;
2201 rsp_increment(ctxt, ctxt->src.val);
2202 return X86EMUL_CONTINUE;
2203}
2204
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002205static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2206{
2207 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002208 ctxt->dst.orig_val = ctxt->dst.val;
2209 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002210 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002211 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002212 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002213
2214 if (ctxt->eflags & EFLG_ZF) {
Nadav Amit2fcf5c82015-01-26 09:32:21 +02002215 /* Success: write back to memory; no update of EAX */
2216 ctxt->src.type = OP_NONE;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002217 ctxt->dst.val = ctxt->src.orig_val;
2218 } else {
2219 /* Failure: write the value we saw to EAX. */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02002220 ctxt->src.type = OP_REG;
2221 ctxt->src.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
2222 ctxt->src.val = ctxt->dst.orig_val;
2223 /* Create write-cycle to dest by writing the same value */
Nadav Amit37c564f2014-06-02 18:34:07 +03002224 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002225 }
2226 return X86EMUL_CONTINUE;
2227}
2228
Avi Kivityd4b43252011-09-13 10:45:50 +03002229static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002230{
Avi Kivityd4b43252011-09-13 10:45:50 +03002231 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002232 unsigned short sel;
2233 int rc;
2234
Avi Kivity9dac77f2011-06-01 15:34:25 +03002235 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002236
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002237 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002238 if (rc != X86EMUL_CONTINUE)
2239 return rc;
2240
Avi Kivity9dac77f2011-06-01 15:34:25 +03002241 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002242 return rc;
2243}
2244
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002245static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002246setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002247 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002248{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002249 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002250 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002251 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002252 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002253 cs->type = 0x0b; /* Read, Execute, Accessed */
2254 cs->s = 1;
2255 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002256 cs->p = 1;
2257 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002258 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002259
Gleb Natapov79168fd2010-04-28 19:15:30 +03002260 set_desc_base(ss, 0); /* flat segment */
2261 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002262 ss->g = 1; /* 4kb granularity */
2263 ss->s = 1;
2264 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002265 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002266 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002267 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002268 ss->l = 0;
2269 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002270}
2271
Avi Kivity1a18a692012-02-01 12:23:21 +02002272static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2273{
2274 u32 eax, ebx, ecx, edx;
2275
2276 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002277 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2278 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002279 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2280 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2281}
2282
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002283static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2284{
Mathias Krause0225fb52012-08-30 01:30:16 +02002285 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002286 u32 eax, ebx, ecx, edx;
2287
2288 /*
2289 * syscall should always be enabled in longmode - so only become
2290 * vendor specific (cpuid) if other modes are active...
2291 */
2292 if (ctxt->mode == X86EMUL_MODE_PROT64)
2293 return true;
2294
2295 eax = 0x00000000;
2296 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002297 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2298 /*
2299 * Intel ("GenuineIntel")
2300 * remark: Intel CPUs only support "syscall" in 64bit
2301 * longmode. Also an 64bit guest with a
2302 * 32bit compat-app running will #UD !! While this
2303 * behaviour can be fixed (by emulating) into AMD
2304 * response - CPUs of AMD can't behave like Intel.
2305 */
2306 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2307 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2308 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2309 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002310
Avi Kivity0017f932012-06-07 14:10:16 +03002311 /* AMD ("AuthenticAMD") */
2312 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2313 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2314 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2315 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002316
Avi Kivity0017f932012-06-07 14:10:16 +03002317 /* AMD ("AMDisbetter!") */
2318 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2319 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2320 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2321 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002322
2323 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2324 return false;
2325}
2326
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002327static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002328{
Mathias Krause0225fb52012-08-30 01:30:16 +02002329 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002330 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002331 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002332 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002333 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002334
2335 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002336 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002337 ctxt->mode == X86EMUL_MODE_VM86)
2338 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002339
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002340 if (!(em_syscall_is_enabled(ctxt)))
2341 return emulate_ud(ctxt);
2342
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002343 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002344 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002345
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002346 if (!(efer & EFER_SCE))
2347 return emulate_ud(ctxt);
2348
Avi Kivity717746e2011-04-20 13:37:53 +03002349 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002350 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002351 cs_sel = (u16)(msr_data & 0xfffc);
2352 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002353
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002354 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002355 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002356 cs.l = 1;
2357 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002358 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2359 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002360
Avi Kivitydd856ef2012-08-27 23:46:17 +03002361 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002362 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002363#ifdef CONFIG_X86_64
Nadav Amit6c6cb692014-07-21 14:37:30 +03002364 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002365
Avi Kivity717746e2011-04-20 13:37:53 +03002366 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002367 ctxt->mode == X86EMUL_MODE_PROT64 ?
2368 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002369 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002370
Avi Kivity717746e2011-04-20 13:37:53 +03002371 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Nadav Amit6c6cb692014-07-21 14:37:30 +03002372 ctxt->eflags &= ~msr_data;
Nadav Amit807c1422014-11-02 11:54:49 +02002373 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002374#endif
2375 } else {
2376 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002377 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002378 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002379
Nadav Amit6c6cb692014-07-21 14:37:30 +03002380 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002381 }
2382
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002383 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002384}
2385
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002386static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002387{
Mathias Krause0225fb52012-08-30 01:30:16 +02002388 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002389 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002390 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002391 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002392 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002393
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002394 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002395 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002396 if (ctxt->mode == X86EMUL_MODE_REAL)
2397 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002398
Avi Kivity1a18a692012-02-01 12:23:21 +02002399 /*
2400 * Not recognized on AMD in compat mode (but is recognized in legacy
2401 * mode).
2402 */
2403 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2404 && !vendor_intel(ctxt))
2405 return emulate_ud(ctxt);
2406
Nadav Amitb2c9d432014-11-02 11:55:01 +02002407 /* sysenter/sysexit have not been tested in 64bit mode. */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002408 if (ctxt->mode == X86EMUL_MODE_PROT64)
Nadav Amitb2c9d432014-11-02 11:55:01 +02002409 return X86EMUL_UNHANDLEABLE;
Andre Przywara8c604352009-06-18 12:56:01 +02002410
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002411 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002412
Avi Kivity717746e2011-04-20 13:37:53 +03002413 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002414 switch (ctxt->mode) {
2415 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002416 if ((msr_data & 0xfffc) == 0x0)
2417 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002418 break;
2419 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002420 if (msr_data == 0x0)
2421 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002422 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002423 default:
2424 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002425 }
2426
Nadav Amit6c6cb692014-07-21 14:37:30 +03002427 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002428 cs_sel = (u16)msr_data;
2429 cs_sel &= ~SELECTOR_RPL_MASK;
2430 ss_sel = cs_sel + 8;
2431 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002432 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002433 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002434 cs.l = 1;
2435 }
2436
Avi Kivity1aa36612011-04-27 13:20:30 +03002437 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2438 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002439
Avi Kivity717746e2011-04-20 13:37:53 +03002440 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002441 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002442
Avi Kivity717746e2011-04-20 13:37:53 +03002443 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002444 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002445
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002446 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002447}
2448
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002449static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002450{
Mathias Krause0225fb52012-08-30 01:30:16 +02002451 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002452 struct desc_struct cs, ss;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002453 u64 msr_data, rcx, rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002454 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002455 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002456
Gleb Natapova0044752010-02-10 14:21:31 +02002457 /* inject #GP if in real mode or Virtual 8086 mode */
2458 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002459 ctxt->mode == X86EMUL_MODE_VM86)
2460 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002461
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002462 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002463
Avi Kivity9dac77f2011-06-01 15:34:25 +03002464 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002465 usermode = X86EMUL_MODE_PROT64;
2466 else
2467 usermode = X86EMUL_MODE_PROT32;
2468
Nadav Amit234f3ce2014-09-18 22:39:38 +03002469 rcx = reg_read(ctxt, VCPU_REGS_RCX);
2470 rdx = reg_read(ctxt, VCPU_REGS_RDX);
2471
Andre Przywara4668f052009-06-18 12:56:02 +02002472 cs.dpl = 3;
2473 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002474 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002475 switch (usermode) {
2476 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002477 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002478 if ((msr_data & 0xfffc) == 0x0)
2479 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002480 ss_sel = (u16)(msr_data + 24);
Nadav Amitbf0b6822014-09-18 22:39:45 +03002481 rcx = (u32)rcx;
2482 rdx = (u32)rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002483 break;
2484 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002485 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002486 if (msr_data == 0x0)
2487 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002488 ss_sel = cs_sel + 8;
2489 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002490 cs.l = 1;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002491 if (is_noncanonical_address(rcx) ||
2492 is_noncanonical_address(rdx))
2493 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002494 break;
2495 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002496 cs_sel |= SELECTOR_RPL_MASK;
2497 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002498
Avi Kivity1aa36612011-04-27 13:20:30 +03002499 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2500 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002501
Nadav Amit234f3ce2014-09-18 22:39:38 +03002502 ctxt->_eip = rdx;
2503 *reg_write(ctxt, VCPU_REGS_RSP) = rcx;
Andre Przywara4668f052009-06-18 12:56:02 +02002504
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002505 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002506}
2507
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002508static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002509{
2510 int iopl;
2511 if (ctxt->mode == X86EMUL_MODE_REAL)
2512 return false;
2513 if (ctxt->mode == X86EMUL_MODE_VM86)
2514 return true;
2515 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002516 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002517}
2518
2519static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002520 u16 port, u16 len)
2521{
Mathias Krause0225fb52012-08-30 01:30:16 +02002522 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002523 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002524 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002525 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002526 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002527 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002528 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002529
Avi Kivity1aa36612011-04-27 13:20:30 +03002530 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002531 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002532 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002533 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002534 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002535 base = get_desc_base(&tr_seg);
2536#ifdef CONFIG_X86_64
2537 base |= ((u64)base3) << 32;
2538#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002539 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002540 if (r != X86EMUL_CONTINUE)
2541 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002542 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002543 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002544 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002545 if (r != X86EMUL_CONTINUE)
2546 return false;
2547 if ((perm >> bit_idx) & mask)
2548 return false;
2549 return true;
2550}
2551
2552static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002553 u16 port, u16 len)
2554{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002555 if (ctxt->perm_ok)
2556 return true;
2557
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002558 if (emulator_bad_iopl(ctxt))
2559 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002560 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002561
2562 ctxt->perm_ok = true;
2563
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002564 return true;
2565}
2566
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002567static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002568 struct tss_segment_16 *tss)
2569{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002570 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002571 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002572 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2573 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2574 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2575 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2576 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2577 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2578 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2579 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002580
Avi Kivity1aa36612011-04-27 13:20:30 +03002581 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2582 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2583 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2584 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2585 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002586}
2587
2588static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002589 struct tss_segment_16 *tss)
2590{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002591 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002592 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002593
Avi Kivity9dac77f2011-06-01 15:34:25 +03002594 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002595 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002596 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2597 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2598 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2599 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2600 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2601 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2602 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2603 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002604
2605 /*
2606 * SDM says that segment selectors are loaded before segment
2607 * descriptors
2608 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002609 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2610 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2611 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2612 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2613 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002614
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002615 cpl = tss->cs & 3;
2616
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002617 /*
Guo Chaofc058682012-06-28 15:19:51 +08002618 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002619 * it is handled in a context of new task
2620 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002621 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002622 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002623 if (ret != X86EMUL_CONTINUE)
2624 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002625 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, 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->cs, VCPU_SREG_CS, 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->ss, VCPU_SREG_SS, 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->ds, VCPU_SREG_DS, 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;
2641
2642 return X86EMUL_CONTINUE;
2643}
2644
2645static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002646 u16 tss_selector, u16 old_tss_sel,
2647 ulong old_tss_base, struct desc_struct *new_desc)
2648{
Mathias Krause0225fb52012-08-30 01:30:16 +02002649 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002650 struct tss_segment_16 tss_seg;
2651 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002652 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002653
Avi Kivity0f65dd72011-04-20 13:37:53 +03002654 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002655 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002656 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002657 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002658
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002659 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002660
Avi Kivity0f65dd72011-04-20 13:37:53 +03002661 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002662 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002663 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002664 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002665
Avi Kivity0f65dd72011-04-20 13:37:53 +03002666 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002667 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002668 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002669 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002670
2671 if (old_tss_sel != 0xffff) {
2672 tss_seg.prev_task_link = old_tss_sel;
2673
Avi Kivity0f65dd72011-04-20 13:37:53 +03002674 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002675 &tss_seg.prev_task_link,
2676 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002677 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002678 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002679 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002680 }
2681
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002682 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002683}
2684
2685static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002686 struct tss_segment_32 *tss)
2687{
Nadav Amit5c7411e2014-04-07 18:37:47 +03002688 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002689 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002690 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002691 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2692 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2693 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2694 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2695 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2696 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2697 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2698 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002699
Avi Kivity1aa36612011-04-27 13:20:30 +03002700 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2701 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2702 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2703 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2704 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2705 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002706}
2707
2708static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002709 struct tss_segment_32 *tss)
2710{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002711 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002712 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002713
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002714 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002715 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002716 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002717 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002718
2719 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002720 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2721 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2722 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2723 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2724 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2725 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2726 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2727 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002728
2729 /*
2730 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002731 * descriptors. This is important because CPL checks will
2732 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002733 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002734 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2735 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2736 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2737 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2738 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2739 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2740 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002741
2742 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002743 * If we're switching between Protected Mode and VM86, we need to make
2744 * sure to update the mode before loading the segment descriptors so
2745 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01002746 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002747 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002748 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002749 cpl = 3;
2750 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002751 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002752 cpl = tss->cs & 3;
2753 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01002754
2755 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002756 * Now load segment descriptors. If fault happenes at this stage
2757 * it is handled in a context of new task
2758 */
Nadav Amitd1442d82014-09-18 22:39:39 +03002759 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002760 cpl, X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002761 if (ret != X86EMUL_CONTINUE)
2762 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03002763 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002764 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->cs, VCPU_SREG_CS, 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->ss, VCPU_SREG_SS, 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->ds, VCPU_SREG_DS, 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->fs, VCPU_SREG_FS, 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->gs, VCPU_SREG_GS, 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;
2787
2788 return X86EMUL_CONTINUE;
2789}
2790
2791static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002792 u16 tss_selector, u16 old_tss_sel,
2793 ulong old_tss_base, struct desc_struct *new_desc)
2794{
Mathias Krause0225fb52012-08-30 01:30:16 +02002795 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002796 struct tss_segment_32 tss_seg;
2797 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002798 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03002799 u32 eip_offset = offsetof(struct tss_segment_32, eip);
2800 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002801
Avi Kivity0f65dd72011-04-20 13:37:53 +03002802 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002803 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002804 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002805 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002806
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002807 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002808
Nadav Amit5c7411e2014-04-07 18:37:47 +03002809 /* Only GP registers and segment selectors are saved */
2810 ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
2811 ldt_sel_offset - eip_offset, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002812 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002813 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002814
Avi Kivity0f65dd72011-04-20 13:37:53 +03002815 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002816 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002817 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002818 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002819
2820 if (old_tss_sel != 0xffff) {
2821 tss_seg.prev_task_link = old_tss_sel;
2822
Avi Kivity0f65dd72011-04-20 13:37:53 +03002823 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002824 &tss_seg.prev_task_link,
2825 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002826 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002827 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002828 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002829 }
2830
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002831 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002832}
2833
2834static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002835 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002836 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002837{
Mathias Krause0225fb52012-08-30 01:30:16 +02002838 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002839 struct desc_struct curr_tss_desc, next_tss_desc;
2840 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002841 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002842 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002843 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002844 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002845 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002846
2847 /* FIXME: old_tss_base == ~0 ? */
2848
Avi Kivitye9194642012-06-13 16:29:39 +03002849 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002850 if (ret != X86EMUL_CONTINUE)
2851 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002852 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002853 if (ret != X86EMUL_CONTINUE)
2854 return ret;
2855
2856 /* FIXME: check that next_tss_desc is tss */
2857
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002858 /*
2859 * Check privileges. The three cases are task switch caused by...
2860 *
2861 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2862 * 2. Exception/IRQ/iret: No check is performed
Nadav Amit2c2ca2d2014-11-02 11:54:57 +02002863 * 3. jmp/call to TSS/task-gate: No check is performed since the
2864 * hardware checks it before exiting.
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002865 */
2866 if (reason == TASK_SWITCH_GATE) {
2867 if (idt_index != -1) {
2868 /* Software interrupts */
2869 struct desc_struct task_gate_desc;
2870 int dpl;
2871
2872 ret = read_interrupt_descriptor(ctxt, idt_index,
2873 &task_gate_desc);
2874 if (ret != X86EMUL_CONTINUE)
2875 return ret;
2876
2877 dpl = task_gate_desc.dpl;
2878 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2879 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2880 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002881 }
2882
Gleb Natapovceffb452010-03-18 15:20:19 +02002883 desc_limit = desc_limit_scaled(&next_tss_desc);
2884 if (!next_tss_desc.p ||
2885 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2886 desc_limit < 0x2b)) {
Paolo Bonzini592f0852014-08-20 10:05:08 +02002887 return emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002888 }
2889
2890 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2891 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002892 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002893 }
2894
2895 if (reason == TASK_SWITCH_IRET)
2896 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2897
2898 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002899 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002900 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2901 old_tss_sel = 0xffff;
2902
2903 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002904 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002905 old_tss_base, &next_tss_desc);
2906 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002907 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002908 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002909 if (ret != X86EMUL_CONTINUE)
2910 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002911
2912 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2913 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2914
2915 if (reason != TASK_SWITCH_IRET) {
2916 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002917 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002918 }
2919
Avi Kivity717746e2011-04-20 13:37:53 +03002920 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002921 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002922
Jan Kiszkae269fb22010-04-14 15:51:09 +02002923 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002924 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2925 ctxt->lock_prefix = 0;
2926 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002927 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002928 }
2929
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002930 return ret;
2931}
2932
2933int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002934 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002935 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002936{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002937 int rc;
2938
Avi Kivitydd856ef2012-08-27 23:46:17 +03002939 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002940 ctxt->_eip = ctxt->eip;
2941 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002942
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002943 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002944 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002945
Avi Kivitydd856ef2012-08-27 23:46:17 +03002946 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002947 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002948 writeback_registers(ctxt);
2949 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002950
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002951 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002952}
2953
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002954static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2955 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002956{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002957 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002958
Paolo Bonzini01485a22014-11-19 18:25:08 +01002959 register_address_increment(ctxt, reg, df * op->bytes);
2960 op->addr.mem.ea = register_address(ctxt, reg);
Gleb Natapova682e352010-03-18 15:20:21 +02002961}
2962
Avi Kivity7af04fc2010-08-18 14:16:35 +03002963static int em_das(struct x86_emulate_ctxt *ctxt)
2964{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002965 u8 al, old_al;
2966 bool af, cf, old_cf;
2967
2968 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002969 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002970
2971 old_al = al;
2972 old_cf = cf;
2973 cf = false;
2974 af = ctxt->eflags & X86_EFLAGS_AF;
2975 if ((al & 0x0f) > 9 || af) {
2976 al -= 6;
2977 cf = old_cf | (al >= 250);
2978 af = true;
2979 } else {
2980 af = false;
2981 }
2982 if (old_al > 0x99 || old_cf) {
2983 al -= 0x60;
2984 cf = true;
2985 }
2986
Avi Kivity9dac77f2011-06-01 15:34:25 +03002987 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002988 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002989 ctxt->src.type = OP_IMM;
2990 ctxt->src.val = 0;
2991 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02002992 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03002993 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2994 if (cf)
2995 ctxt->eflags |= X86_EFLAGS_CF;
2996 if (af)
2997 ctxt->eflags |= X86_EFLAGS_AF;
2998 return X86EMUL_CONTINUE;
2999}
3000
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003001static int em_aam(struct x86_emulate_ctxt *ctxt)
3002{
3003 u8 al, ah;
3004
3005 if (ctxt->src.val == 0)
3006 return emulate_de(ctxt);
3007
3008 al = ctxt->dst.val & 0xff;
3009 ah = al / ctxt->src.val;
3010 al %= ctxt->src.val;
3011
3012 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
3013
3014 /* Set PF, ZF, SF */
3015 ctxt->src.type = OP_IMM;
3016 ctxt->src.val = 0;
3017 ctxt->src.bytes = 1;
3018 fastop(ctxt, em_or);
3019
3020 return X86EMUL_CONTINUE;
3021}
3022
Gleb Natapov7f662272012-12-10 11:42:30 +02003023static int em_aad(struct x86_emulate_ctxt *ctxt)
3024{
3025 u8 al = ctxt->dst.val & 0xff;
3026 u8 ah = (ctxt->dst.val >> 8) & 0xff;
3027
3028 al = (al + (ah * ctxt->src.val)) & 0xff;
3029
3030 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
3031
Gleb Natapovf583c292013-02-13 17:50:39 +02003032 /* Set PF, ZF, SF */
3033 ctxt->src.type = OP_IMM;
3034 ctxt->src.val = 0;
3035 ctxt->src.bytes = 1;
3036 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02003037
3038 return X86EMUL_CONTINUE;
3039}
3040
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003041static int em_call(struct x86_emulate_ctxt *ctxt)
3042{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003043 int rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003044 long rel = ctxt->src.val;
3045
3046 ctxt->src.val = (unsigned long)ctxt->_eip;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003047 rc = jmp_rel(ctxt, rel);
3048 if (rc != X86EMUL_CONTINUE)
3049 return rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003050 return em_push(ctxt);
3051}
3052
Avi Kivity0ef753b2010-08-18 14:51:45 +03003053static int em_call_far(struct x86_emulate_ctxt *ctxt)
3054{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003055 u16 sel, old_cs;
3056 ulong old_eip;
3057 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03003058 struct desc_struct old_desc, new_desc;
3059 const struct x86_emulate_ops *ops = ctxt->ops;
3060 int cpl = ctxt->ops->cpl(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003061
Avi Kivity9dac77f2011-06-01 15:34:25 +03003062 old_eip = ctxt->_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003063 ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003064
Avi Kivity9dac77f2011-06-01 15:34:25 +03003065 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003066 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
3067 X86_TRANSFER_CALL_JMP, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003068 if (rc != X86EMUL_CONTINUE)
Nadav Amit80976db2014-12-25 02:52:20 +02003069 return rc;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003070
Nadav Amitd50eaa12014-11-19 17:43:11 +02003071 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003072 if (rc != X86EMUL_CONTINUE)
3073 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003074
Avi Kivity9dac77f2011-06-01 15:34:25 +03003075 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003076 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003077 if (rc != X86EMUL_CONTINUE)
Nadav Amitd1442d82014-09-18 22:39:39 +03003078 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003079
Avi Kivity9dac77f2011-06-01 15:34:25 +03003080 ctxt->src.val = old_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003081 rc = em_push(ctxt);
3082 /* If we failed, we tainted the memory, but the very least we should
3083 restore cs */
3084 if (rc != X86EMUL_CONTINUE)
3085 goto fail;
3086 return rc;
3087fail:
3088 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
3089 return rc;
3090
Avi Kivity0ef753b2010-08-18 14:51:45 +03003091}
3092
Avi Kivity40ece7c2010-08-18 15:12:09 +03003093static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3094{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003095 int rc;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003096 unsigned long eip;
Avi Kivity40ece7c2010-08-18 15:12:09 +03003097
Nadav Amit234f3ce2014-09-18 22:39:38 +03003098 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
3099 if (rc != X86EMUL_CONTINUE)
3100 return rc;
3101 rc = assign_eip_near(ctxt, eip);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003102 if (rc != X86EMUL_CONTINUE)
3103 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003104 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003105 return X86EMUL_CONTINUE;
3106}
3107
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003108static int em_xchg(struct x86_emulate_ctxt *ctxt)
3109{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003110 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003111 ctxt->src.val = ctxt->dst.val;
3112 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003113
3114 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003115 ctxt->dst.val = ctxt->src.orig_val;
3116 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003117 return X86EMUL_CONTINUE;
3118}
3119
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003120static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3121{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003122 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003123 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003124}
3125
Avi Kivity61429142010-08-19 15:13:00 +03003126static int em_cwd(struct x86_emulate_ctxt *ctxt)
3127{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003128 ctxt->dst.type = OP_REG;
3129 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003130 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003131 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003132
3133 return X86EMUL_CONTINUE;
3134}
3135
Avi Kivity48bb5d32010-08-18 18:54:34 +03003136static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3137{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003138 u64 tsc = 0;
3139
Avi Kivity717746e2011-04-20 13:37:53 +03003140 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003141 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3142 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003143 return X86EMUL_CONTINUE;
3144}
3145
Avi Kivity222d21a2011-11-10 14:57:30 +02003146static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3147{
3148 u64 pmc;
3149
Avi Kivitydd856ef2012-08-27 23:46:17 +03003150 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003151 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003152 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3153 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003154 return X86EMUL_CONTINUE;
3155}
3156
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003157static int em_mov(struct x86_emulate_ctxt *ctxt)
3158{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01003159 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003160 return X86EMUL_CONTINUE;
3161}
3162
Borislav Petkov84cffe42013-10-29 12:54:56 +01003163#define FFL(x) bit(X86_FEATURE_##x)
3164
3165static int em_movbe(struct x86_emulate_ctxt *ctxt)
3166{
3167 u32 ebx, ecx, edx, eax = 1;
3168 u16 tmp;
3169
3170 /*
3171 * Check MOVBE is set in the guest-visible CPUID leaf.
3172 */
3173 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
3174 if (!(ecx & FFL(MOVBE)))
3175 return emulate_ud(ctxt);
3176
3177 switch (ctxt->op_bytes) {
3178 case 2:
3179 /*
3180 * From MOVBE definition: "...When the operand size is 16 bits,
3181 * the upper word of the destination register remains unchanged
3182 * ..."
3183 *
3184 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3185 * rules so we have to do the operation almost per hand.
3186 */
3187 tmp = (u16)ctxt->src.val;
3188 ctxt->dst.val &= ~0xffffUL;
3189 ctxt->dst.val |= (unsigned long)swab16(tmp);
3190 break;
3191 case 4:
3192 ctxt->dst.val = swab32((u32)ctxt->src.val);
3193 break;
3194 case 8:
3195 ctxt->dst.val = swab64(ctxt->src.val);
3196 break;
3197 default:
Paolo Bonzini592f0852014-08-20 10:05:08 +02003198 BUG();
Borislav Petkov84cffe42013-10-29 12:54:56 +01003199 }
3200 return X86EMUL_CONTINUE;
3201}
3202
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003203static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3204{
3205 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3206 return emulate_gp(ctxt, 0);
3207
3208 /* Disable writeback. */
3209 ctxt->dst.type = OP_NONE;
3210 return X86EMUL_CONTINUE;
3211}
3212
3213static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3214{
3215 unsigned long val;
3216
3217 if (ctxt->mode == X86EMUL_MODE_PROT64)
3218 val = ctxt->src.val & ~0ULL;
3219 else
3220 val = ctxt->src.val & ~0U;
3221
3222 /* #UD condition is already handled. */
3223 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3224 return emulate_gp(ctxt, 0);
3225
3226 /* Disable writeback. */
3227 ctxt->dst.type = OP_NONE;
3228 return X86EMUL_CONTINUE;
3229}
3230
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003231static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3232{
3233 u64 msr_data;
3234
Avi Kivitydd856ef2012-08-27 23:46:17 +03003235 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3236 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3237 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003238 return emulate_gp(ctxt, 0);
3239
3240 return X86EMUL_CONTINUE;
3241}
3242
3243static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3244{
3245 u64 msr_data;
3246
Avi Kivitydd856ef2012-08-27 23:46:17 +03003247 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003248 return emulate_gp(ctxt, 0);
3249
Avi Kivitydd856ef2012-08-27 23:46:17 +03003250 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3251 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003252 return X86EMUL_CONTINUE;
3253}
3254
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003255static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3256{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003257 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003258 return emulate_ud(ctxt);
3259
Avi Kivity9dac77f2011-06-01 15:34:25 +03003260 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Nadav Amitb5bbf102014-11-02 11:54:46 +02003261 if (ctxt->dst.bytes == 4 && ctxt->dst.type == OP_MEM)
3262 ctxt->dst.bytes = 2;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003263 return X86EMUL_CONTINUE;
3264}
3265
3266static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3267{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003268 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003269
Avi Kivity9dac77f2011-06-01 15:34:25 +03003270 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003271 return emulate_ud(ctxt);
3272
Avi Kivity9dac77f2011-06-01 15:34:25 +03003273 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003274 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3275
3276 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003277 ctxt->dst.type = OP_NONE;
3278 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003279}
3280
Avi Kivitya14e5792012-06-13 12:28:33 +03003281static int em_lldt(struct x86_emulate_ctxt *ctxt)
3282{
3283 u16 sel = ctxt->src.val;
3284
3285 /* Disable writeback. */
3286 ctxt->dst.type = OP_NONE;
3287 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3288}
3289
Avi Kivity80890002012-06-13 16:33:29 +03003290static int em_ltr(struct x86_emulate_ctxt *ctxt)
3291{
3292 u16 sel = ctxt->src.val;
3293
3294 /* Disable writeback. */
3295 ctxt->dst.type = OP_NONE;
3296 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3297}
3298
Avi Kivity38503912011-03-31 18:48:09 +02003299static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3300{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003301 int rc;
3302 ulong linear;
3303
Avi Kivity9dac77f2011-06-01 15:34:25 +03003304 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003305 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003306 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003307 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003308 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003309 return X86EMUL_CONTINUE;
3310}
3311
Avi Kivity2d04a052011-04-20 15:32:49 +03003312static int em_clts(struct x86_emulate_ctxt *ctxt)
3313{
3314 ulong cr0;
3315
3316 cr0 = ctxt->ops->get_cr(ctxt, 0);
3317 cr0 &= ~X86_CR0_TS;
3318 ctxt->ops->set_cr(ctxt, 0, cr0);
3319 return X86EMUL_CONTINUE;
3320}
3321
Avi Kivity26d05cc2011-04-21 12:07:59 +03003322static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3323{
Nadav Amit0f54a322014-08-29 11:26:55 +03003324 int rc = ctxt->ops->fix_hypercall(ctxt);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003325
Avi Kivity26d05cc2011-04-21 12:07:59 +03003326 if (rc != X86EMUL_CONTINUE)
3327 return rc;
3328
3329 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003330 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003331 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003332 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003333 return X86EMUL_CONTINUE;
3334}
3335
Avi Kivity96051572012-06-10 17:21:18 +03003336static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3337 void (*get)(struct x86_emulate_ctxt *ctxt,
3338 struct desc_ptr *ptr))
3339{
3340 struct desc_ptr desc_ptr;
3341
3342 if (ctxt->mode == X86EMUL_MODE_PROT64)
3343 ctxt->op_bytes = 8;
3344 get(ctxt, &desc_ptr);
3345 if (ctxt->op_bytes == 2) {
3346 ctxt->op_bytes = 4;
3347 desc_ptr.address &= 0x00ffffff;
3348 }
3349 /* Disable writeback. */
3350 ctxt->dst.type = OP_NONE;
3351 return segmented_write(ctxt, ctxt->dst.addr.mem,
3352 &desc_ptr, 2 + ctxt->op_bytes);
3353}
3354
3355static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3356{
3357 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3358}
3359
3360static int em_sidt(struct x86_emulate_ctxt *ctxt)
3361{
3362 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3363}
3364
Nadav Amit5b7f6a12014-11-02 11:54:55 +02003365static int em_lgdt_lidt(struct x86_emulate_ctxt *ctxt, bool lgdt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003366{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003367 struct desc_ptr desc_ptr;
3368 int rc;
3369
Avi Kivity510425f2012-06-07 17:04:36 +03003370 if (ctxt->mode == X86EMUL_MODE_PROT64)
3371 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003372 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003373 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003374 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003375 if (rc != X86EMUL_CONTINUE)
3376 return rc;
Nadav Amit9a9abf62014-11-02 11:54:56 +02003377 if (ctxt->mode == X86EMUL_MODE_PROT64 &&
3378 is_noncanonical_address(desc_ptr.address))
3379 return emulate_gp(ctxt, 0);
Nadav Amit5b7f6a12014-11-02 11:54:55 +02003380 if (lgdt)
3381 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3382 else
3383 ctxt->ops->set_idt(ctxt, &desc_ptr);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003384 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003385 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003386 return X86EMUL_CONTINUE;
3387}
3388
Nadav Amit5b7f6a12014-11-02 11:54:55 +02003389static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3390{
3391 return em_lgdt_lidt(ctxt, true);
3392}
3393
Avi Kivity5ef39c72011-04-21 12:21:50 +03003394static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003395{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003396 int rc;
3397
Avi Kivity5ef39c72011-04-21 12:21:50 +03003398 rc = ctxt->ops->fix_hypercall(ctxt);
3399
Avi Kivity26d05cc2011-04-21 12:07:59 +03003400 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003401 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003402 return rc;
3403}
3404
3405static int em_lidt(struct x86_emulate_ctxt *ctxt)
3406{
Nadav Amit5b7f6a12014-11-02 11:54:55 +02003407 return em_lgdt_lidt(ctxt, false);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003408}
3409
3410static int em_smsw(struct x86_emulate_ctxt *ctxt)
3411{
Nadav Amit32e94d02014-06-02 18:34:11 +03003412 if (ctxt->dst.type == OP_MEM)
3413 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003414 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003415 return X86EMUL_CONTINUE;
3416}
3417
3418static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3419{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003420 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003421 | (ctxt->src.val & 0x0f));
3422 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003423 return X86EMUL_CONTINUE;
3424}
3425
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003426static int em_loop(struct x86_emulate_ctxt *ctxt)
3427{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003428 int rc = X86EMUL_CONTINUE;
3429
Paolo Bonzini01485a22014-11-19 18:25:08 +01003430 register_address_increment(ctxt, VCPU_REGS_RCX, -1);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003431 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003432 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
Nadav Amit234f3ce2014-09-18 22:39:38 +03003433 rc = jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003434
Nadav Amit234f3ce2014-09-18 22:39:38 +03003435 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003436}
3437
3438static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3439{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003440 int rc = X86EMUL_CONTINUE;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003441
Nadav Amit234f3ce2014-09-18 22:39:38 +03003442 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
3443 rc = jmp_rel(ctxt, ctxt->src.val);
3444
3445 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003446}
3447
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003448static int em_in(struct x86_emulate_ctxt *ctxt)
3449{
3450 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3451 &ctxt->dst.val))
3452 return X86EMUL_IO_NEEDED;
3453
3454 return X86EMUL_CONTINUE;
3455}
3456
3457static int em_out(struct x86_emulate_ctxt *ctxt)
3458{
3459 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3460 &ctxt->src.val, 1);
3461 /* Disable writeback. */
3462 ctxt->dst.type = OP_NONE;
3463 return X86EMUL_CONTINUE;
3464}
3465
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003466static int em_cli(struct x86_emulate_ctxt *ctxt)
3467{
3468 if (emulator_bad_iopl(ctxt))
3469 return emulate_gp(ctxt, 0);
3470
3471 ctxt->eflags &= ~X86_EFLAGS_IF;
3472 return X86EMUL_CONTINUE;
3473}
3474
3475static int em_sti(struct x86_emulate_ctxt *ctxt)
3476{
3477 if (emulator_bad_iopl(ctxt))
3478 return emulate_gp(ctxt, 0);
3479
3480 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3481 ctxt->eflags |= X86_EFLAGS_IF;
3482 return X86EMUL_CONTINUE;
3483}
3484
Avi Kivity6d6eede2012-06-07 14:11:36 +03003485static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3486{
3487 u32 eax, ebx, ecx, edx;
3488
Avi Kivitydd856ef2012-08-27 23:46:17 +03003489 eax = reg_read(ctxt, VCPU_REGS_RAX);
3490 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003491 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003492 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3493 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3494 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3495 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003496 return X86EMUL_CONTINUE;
3497}
3498
Paolo Bonzini98f73632013-10-31 11:19:42 +01003499static int em_sahf(struct x86_emulate_ctxt *ctxt)
3500{
3501 u32 flags;
3502
3503 flags = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF;
3504 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3505
3506 ctxt->eflags &= ~0xffUL;
3507 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3508 return X86EMUL_CONTINUE;
3509}
3510
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003511static int em_lahf(struct x86_emulate_ctxt *ctxt)
3512{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003513 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3514 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003515 return X86EMUL_CONTINUE;
3516}
3517
Avi Kivity92998362012-06-13 12:25:06 +03003518static int em_bswap(struct x86_emulate_ctxt *ctxt)
3519{
3520 switch (ctxt->op_bytes) {
3521#ifdef CONFIG_X86_64
3522 case 8:
3523 asm("bswap %0" : "+r"(ctxt->dst.val));
3524 break;
3525#endif
3526 default:
3527 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3528 break;
3529 }
3530 return X86EMUL_CONTINUE;
3531}
3532
Nadav Amit13e457e2014-10-13 13:04:13 +03003533static int em_clflush(struct x86_emulate_ctxt *ctxt)
3534{
3535 /* emulating clflush regardless of cpuid */
3536 return X86EMUL_CONTINUE;
3537}
3538
Nadav Amit2276b512015-01-26 09:32:24 +02003539static int em_movsxd(struct x86_emulate_ctxt *ctxt)
3540{
3541 ctxt->dst.val = (s32) ctxt->src.val;
3542 return X86EMUL_CONTINUE;
3543}
3544
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003545static bool valid_cr(int nr)
3546{
3547 switch (nr) {
3548 case 0:
3549 case 2 ... 4:
3550 case 8:
3551 return true;
3552 default:
3553 return false;
3554 }
3555}
3556
3557static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3558{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003559 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003560 return emulate_ud(ctxt);
3561
3562 return X86EMUL_CONTINUE;
3563}
3564
3565static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3566{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003567 u64 new_val = ctxt->src.val64;
3568 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003569 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003570
3571 static u64 cr_reserved_bits[] = {
3572 0xffffffff00000000ULL,
3573 0, 0, 0, /* CR3 checked later */
3574 CR4_RESERVED_BITS,
3575 0, 0, 0,
3576 CR8_RESERVED_BITS,
3577 };
3578
3579 if (!valid_cr(cr))
3580 return emulate_ud(ctxt);
3581
3582 if (new_val & cr_reserved_bits[cr])
3583 return emulate_gp(ctxt, 0);
3584
3585 switch (cr) {
3586 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003587 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003588 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3589 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3590 return emulate_gp(ctxt, 0);
3591
Avi Kivity717746e2011-04-20 13:37:53 +03003592 cr4 = ctxt->ops->get_cr(ctxt, 4);
3593 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003594
3595 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3596 !(cr4 & X86_CR4_PAE))
3597 return emulate_gp(ctxt, 0);
3598
3599 break;
3600 }
3601 case 3: {
3602 u64 rsvd = 0;
3603
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003604 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3605 if (efer & EFER_LMA)
Nadav Amit9d88fca2014-11-02 11:54:52 +02003606 rsvd = CR3_L_MODE_RESERVED_BITS & ~CR3_PCID_INVD;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003607
3608 if (new_val & rsvd)
3609 return emulate_gp(ctxt, 0);
3610
3611 break;
3612 }
3613 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003614 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003615
3616 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3617 return emulate_gp(ctxt, 0);
3618
3619 break;
3620 }
3621 }
3622
3623 return X86EMUL_CONTINUE;
3624}
3625
Joerg Roedel3b88e412011-04-04 12:39:29 +02003626static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3627{
3628 unsigned long dr7;
3629
Avi Kivity717746e2011-04-20 13:37:53 +03003630 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003631
3632 /* Check if DR7.Global_Enable is set */
3633 return dr7 & (1 << 13);
3634}
3635
3636static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3637{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003638 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003639 u64 cr4;
3640
3641 if (dr > 7)
3642 return emulate_ud(ctxt);
3643
Avi Kivity717746e2011-04-20 13:37:53 +03003644 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003645 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3646 return emulate_ud(ctxt);
3647
Nadav Amit6d2a0522014-11-02 11:54:43 +02003648 if (check_dr7_gd(ctxt)) {
3649 ulong dr6;
3650
3651 ctxt->ops->get_dr(ctxt, 6, &dr6);
3652 dr6 &= ~15;
3653 dr6 |= DR6_BD | DR6_RTM;
3654 ctxt->ops->set_dr(ctxt, 6, dr6);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003655 return emulate_db(ctxt);
Nadav Amit6d2a0522014-11-02 11:54:43 +02003656 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02003657
3658 return X86EMUL_CONTINUE;
3659}
3660
3661static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3662{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003663 u64 new_val = ctxt->src.val64;
3664 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003665
3666 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3667 return emulate_gp(ctxt, 0);
3668
3669 return check_dr_read(ctxt);
3670}
3671
Joerg Roedel01de8b02011-04-04 12:39:31 +02003672static int check_svme(struct x86_emulate_ctxt *ctxt)
3673{
3674 u64 efer;
3675
Avi Kivity717746e2011-04-20 13:37:53 +03003676 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003677
3678 if (!(efer & EFER_SVME))
3679 return emulate_ud(ctxt);
3680
3681 return X86EMUL_CONTINUE;
3682}
3683
3684static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3685{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003686 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003687
3688 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003689 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003690 return emulate_gp(ctxt, 0);
3691
3692 return check_svme(ctxt);
3693}
3694
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003695static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3696{
Avi Kivity717746e2011-04-20 13:37:53 +03003697 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003698
Avi Kivity717746e2011-04-20 13:37:53 +03003699 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003700 return emulate_ud(ctxt);
3701
3702 return X86EMUL_CONTINUE;
3703}
3704
Joerg Roedel80612522011-04-04 12:39:33 +02003705static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3706{
Avi Kivity717746e2011-04-20 13:37:53 +03003707 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003708 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003709
Avi Kivity717746e2011-04-20 13:37:53 +03003710 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03003711 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02003712 return emulate_gp(ctxt, 0);
3713
3714 return X86EMUL_CONTINUE;
3715}
3716
Joerg Roedelf6511932011-04-04 12:39:35 +02003717static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3718{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003719 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3720 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003721 return emulate_gp(ctxt, 0);
3722
3723 return X86EMUL_CONTINUE;
3724}
3725
3726static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3727{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003728 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3729 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003730 return emulate_gp(ctxt, 0);
3731
3732 return X86EMUL_CONTINUE;
3733}
3734
Avi Kivity73fba5f2010-07-29 15:11:53 +03003735#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003736#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
3737#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
3738 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003739#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003740#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003741#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3742#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Nadav Amit39f062f2014-11-26 15:47:18 +02003743#define ID(_f, _i) { .flags = ((_f) | InstrDual | ModRM), .u.idual = (_i) }
Nadav Amit2276b512015-01-26 09:32:24 +02003744#define MD(_f, _m) { .flags = ((_f) | ModeDual), .u.mdual = (_m) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003745#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003746#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003747#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003748#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003749 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003750#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003751 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
3752 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003753#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003754
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003755#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003756#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003757#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003758#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003759#define I2bvIP(_f, _e, _i, _p) \
3760 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003761
Avi Kivityfb864fb2013-01-04 16:18:54 +02003762#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3763 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3764 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003765
Nadav Amit0f54a322014-08-29 11:26:55 +03003766static const struct opcode group7_rm0[] = {
3767 N,
3768 I(SrcNone | Priv | EmulateOnUD, em_vmcall),
3769 N, N, N, N, N, N,
3770};
3771
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003772static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003773 DI(SrcNone | Priv, monitor),
3774 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003775 N, N, N, N, N, N,
3776};
3777
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003778static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003779 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003780 II(SrcNone | Prot | EmulateOnUD, em_vmmcall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003781 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3782 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3783 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3784 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3785 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3786 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003787};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003788
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003789static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003790 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003791 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003792 N, N, N, N, N, N,
3793};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003794
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003795static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003796 F(Lock, em_add),
3797 F(Lock | PageTable, em_or),
3798 F(Lock, em_adc),
3799 F(Lock, em_sbb),
3800 F(Lock | PageTable, em_and),
3801 F(Lock, em_sub),
3802 F(Lock, em_xor),
3803 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003804};
3805
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003806static const struct opcode group1A[] = {
Nadav Amitab708092014-12-25 02:52:21 +02003807 I(DstMem | SrcNone | Mov | Stack | IncSP, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003808};
3809
Avi Kivity007a3b52013-01-19 19:51:51 +02003810static const struct opcode group2[] = {
3811 F(DstMem | ModRM, em_rol),
3812 F(DstMem | ModRM, em_ror),
3813 F(DstMem | ModRM, em_rcl),
3814 F(DstMem | ModRM, em_rcr),
3815 F(DstMem | ModRM, em_shl),
3816 F(DstMem | ModRM, em_shr),
3817 F(DstMem | ModRM, em_shl),
3818 F(DstMem | ModRM, em_sar),
3819};
3820
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003821static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003822 F(DstMem | SrcImm | NoWrite, em_test),
3823 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003824 F(DstMem | SrcNone | Lock, em_not),
3825 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02003826 F(DstXacc | Src2Mem, em_mul_ex),
3827 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02003828 F(DstXacc | Src2Mem, em_div_ex),
3829 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003830};
3831
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003832static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003833 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3834 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003835 N, N, N, N, N, N,
3836};
3837
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003838static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003839 F(DstMem | SrcNone | Lock, em_inc),
3840 F(DstMem | SrcNone | Lock, em_dec),
Nadav Amit58b70752014-10-24 11:35:09 +03003841 I(SrcMem | NearBranch, em_call_near_abs),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003842 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
Nadav Amit58b70752014-10-24 11:35:09 +03003843 I(SrcMem | NearBranch, em_jmp_abs),
Nadav Amitf7784042014-09-18 22:39:41 +03003844 I(SrcMemFAddr | ImplicitOps, em_jmp_far),
3845 I(SrcMem | Stack, em_push), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003846};
3847
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003848static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003849 DI(Prot, sldt),
3850 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003851 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003852 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003853 N, N, N, N,
3854};
3855
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003856static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03003857 II(Mov | DstMem, em_sgdt, sgdt),
3858 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003859 II(SrcMem | Priv, em_lgdt, lgdt),
3860 II(SrcMem | Priv, em_lidt, lidt),
3861 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3862 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3863 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003864}, {
Nadav Amit0f54a322014-08-29 11:26:55 +03003865 EXT(0, group7_rm0),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003866 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003867 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003868 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3869 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3870 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003871} };
3872
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003873static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003874 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003875 F(DstMem | SrcImmByte | NoWrite, em_bt),
3876 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3877 F(DstMem | SrcImmByte | Lock, em_btr),
3878 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003879};
3880
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003881static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003882 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003883}, {
3884 N, N, N, N, N, N, N, N,
3885} };
3886
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003887static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003888 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003889 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003890};
3891
Nadav Amit13e457e2014-10-13 13:04:13 +03003892static const struct gprefix pfx_0f_ae_7 = {
Nadav Amit3f6f1482014-10-13 13:04:14 +03003893 I(SrcMem | ByteOp, em_clflush), N, N, N,
Nadav Amit13e457e2014-10-13 13:04:13 +03003894};
3895
3896static const struct group_dual group15 = { {
3897 N, N, N, N, N, N, N, GP(0, &pfx_0f_ae_7),
3898}, {
3899 N, N, N, N, N, N, N, N,
3900} };
3901
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003902static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003903 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003904};
3905
Nadav Amit39f062f2014-11-26 15:47:18 +02003906static const struct instr_dual instr_dual_0f_2b = {
3907 I(0, em_mov), N
3908};
3909
Paolo Bonzinid5b77062014-07-14 12:54:48 +02003910static const struct gprefix pfx_0f_2b = {
Nadav Amit39f062f2014-11-26 15:47:18 +02003911 ID(0, &instr_dual_0f_2b), ID(0, &instr_dual_0f_2b), N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003912};
3913
Igor Mammedov27ce8252014-03-15 21:01:59 +01003914static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01003915 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003916};
3917
Alex Williamson0a370272014-07-11 11:56:31 -06003918static const struct gprefix pfx_0f_e7 = {
3919 N, I(Sse, em_mov), N, N,
3920};
3921
Gleb Natapov045a2822012-12-20 16:57:43 +02003922static const struct escape escape_d9 = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02003923 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstcw),
Gleb Natapov045a2822012-12-20 16:57:43 +02003924}, {
3925 /* 0xC0 - 0xC7 */
3926 N, N, N, N, N, N, N, N,
3927 /* 0xC8 - 0xCF */
3928 N, N, N, N, N, N, N, N,
3929 /* 0xD0 - 0xC7 */
3930 N, N, N, N, N, N, N, N,
3931 /* 0xD8 - 0xDF */
3932 N, N, N, N, N, N, N, N,
3933 /* 0xE0 - 0xE7 */
3934 N, N, N, N, N, N, N, N,
3935 /* 0xE8 - 0xEF */
3936 N, N, N, N, N, N, N, N,
3937 /* 0xF0 - 0xF7 */
3938 N, N, N, N, N, N, N, N,
3939 /* 0xF8 - 0xFF */
3940 N, N, N, N, N, N, N, N,
3941} };
3942
3943static const struct escape escape_db = { {
3944 N, N, N, N, N, N, N, N,
3945}, {
3946 /* 0xC0 - 0xC7 */
3947 N, N, N, N, N, N, N, N,
3948 /* 0xC8 - 0xCF */
3949 N, N, N, N, N, N, N, N,
3950 /* 0xD0 - 0xC7 */
3951 N, N, N, N, N, N, N, N,
3952 /* 0xD8 - 0xDF */
3953 N, N, N, N, N, N, N, N,
3954 /* 0xE0 - 0xE7 */
3955 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3956 /* 0xE8 - 0xEF */
3957 N, N, N, N, N, N, N, N,
3958 /* 0xF0 - 0xF7 */
3959 N, N, N, N, N, N, N, N,
3960 /* 0xF8 - 0xFF */
3961 N, N, N, N, N, N, N, N,
3962} };
3963
3964static const struct escape escape_dd = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02003965 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstsw),
Gleb Natapov045a2822012-12-20 16:57:43 +02003966}, {
3967 /* 0xC0 - 0xC7 */
3968 N, N, N, N, N, N, N, N,
3969 /* 0xC8 - 0xCF */
3970 N, N, N, N, N, N, N, N,
3971 /* 0xD0 - 0xC7 */
3972 N, N, N, N, N, N, N, N,
3973 /* 0xD8 - 0xDF */
3974 N, N, N, N, N, N, N, N,
3975 /* 0xE0 - 0xE7 */
3976 N, N, N, N, N, N, N, N,
3977 /* 0xE8 - 0xEF */
3978 N, N, N, N, N, N, N, N,
3979 /* 0xF0 - 0xF7 */
3980 N, N, N, N, N, N, N, N,
3981 /* 0xF8 - 0xFF */
3982 N, N, N, N, N, N, N, N,
3983} };
3984
Nadav Amit39f062f2014-11-26 15:47:18 +02003985static const struct instr_dual instr_dual_0f_c3 = {
3986 I(DstMem | SrcReg | ModRM | No16 | Mov, em_mov), N
3987};
3988
Nadav Amit2276b512015-01-26 09:32:24 +02003989static const struct mode_dual mode_dual_63 = {
3990 N, I(DstReg | SrcMem32 | ModRM | Mov, em_movsxd)
3991};
3992
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003993static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003994 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003995 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003996 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3997 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003998 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003999 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004000 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
4001 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004002 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004003 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004004 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
4005 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004006 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004007 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004008 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
4009 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004010 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004011 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004012 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004013 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004014 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004015 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004016 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004017 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004018 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02004019 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004020 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03004021 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004022 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09004023 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004024 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09004025 I(ImplicitOps | Stack | No64, em_pusha),
4026 I(ImplicitOps | Stack | No64, em_popa),
Nadav Amit2276b512015-01-26 09:32:24 +02004027 N, MD(ModRM, &mode_dual_63),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004028 N, N, N, N,
4029 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03004030 I(SrcImm | Mov | Stack, em_push),
4031 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03004032 I(SrcImmByte | Mov | Stack, em_push),
4033 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004034 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09004035 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03004036 /* 0x70 - 0x7F */
Nadav Amit58b70752014-10-24 11:35:09 +03004037 X16(D(SrcImmByte | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004038 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004039 G(ByteOp | DstMem | SrcImm, group1),
4040 G(DstMem | SrcImm, group1),
4041 G(ByteOp | DstMem | SrcImm | No64, group1),
4042 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02004043 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004044 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004045 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004046 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004047 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004048 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09004049 D(ModRM | SrcMem | NoAccess | DstReg),
4050 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
4051 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004052 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004053 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004054 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03004055 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08004056 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09004057 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01004058 II(ImplicitOps | Stack, em_popf, popf),
4059 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004060 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004061 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004062 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004063 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004064 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004065 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004066 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004067 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
4068 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004069 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004070 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004071 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004072 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02004073 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004074 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004075 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Nadav Amit58b70752014-10-24 11:35:09 +03004076 I(ImplicitOps | NearBranch | SrcImmU16, em_ret_near_imm),
4077 I(ImplicitOps | NearBranch, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03004078 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
4079 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004080 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004081 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03004082 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Nadav Amit16794aa2015-01-26 09:32:22 +02004083 I(ImplicitOps | SrcImmU16, em_ret_far_imm),
4084 I(ImplicitOps, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02004085 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004086 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004087 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004088 G(Src2One | ByteOp, group2), G(Src2One, group2),
4089 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02004090 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02004091 I(DstAcc | SrcImmUByte | No64, em_aad),
4092 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004093 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004094 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02004095 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004096 /* 0xE0 - 0xE7 */
Nadav Amit58b70752014-10-24 11:35:09 +03004097 X3(I(SrcImmByte | NearBranch, em_loop)),
4098 I(SrcImmByte | NearBranch, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004099 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
4100 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004101 /* 0xE8 - 0xEF */
Nadav Amit58b70752014-10-24 11:35:09 +03004102 I(SrcImm | NearBranch, em_call), D(SrcImm | ImplicitOps | NearBranch),
4103 I(SrcImmFAddr | No64, em_jmp_far),
4104 D(SrcImmByte | ImplicitOps | NearBranch),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004105 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
4106 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004107 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004108 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004109 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4110 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004111 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09004112 D(ImplicitOps), D(ImplicitOps),
4113 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004114 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4115};
4116
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004117static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004118 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004119 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02004120 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004121 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004122 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004123 N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004124 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004125 N, N, N, N, N, N, N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004126 D(ImplicitOps | ModRM | SrcMem | NoAccess),
4127 N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004128 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03004129 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
4130 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
4131 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
4132 check_cr_write),
4133 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
4134 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004135 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004136 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
4137 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004138 N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b),
Avi Kivity3e114eb2012-04-09 18:40:01 +03004139 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004140 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004141 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004142 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004143 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004144 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02004145 I(ImplicitOps | EmulateOnUD, em_sysenter),
4146 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004147 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004148 N, N, N, N, N, N, N, N,
4149 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03004150 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004151 /* 0x50 - 0x5F */
4152 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4153 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004154 N, N, N, N,
4155 N, N, N, N,
4156 N, N, N, N,
4157 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004158 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004159 N, N, N, N,
4160 N, N, N, N,
4161 N, N, N, N,
4162 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004163 /* 0x80 - 0x8F */
Nadav Amit58b70752014-10-24 11:35:09 +03004164 X16(D(SrcImm | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004165 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004166 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004167 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004168 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004169 II(ImplicitOps, em_cpuid, cpuid),
4170 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004171 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4172 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004173 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004174 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004175 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004176 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004177 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4178 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Nadav Amit13e457e2014-10-13 13:04:13 +03004179 GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004180 /* 0xB0 - 0xB7 */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02004181 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable | SrcWrite, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004182 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004183 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004184 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4185 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004186 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004187 /* 0xB8 - 0xBF */
4188 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004189 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004190 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
4191 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004192 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004193 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02004194 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Nadav Amit39f062f2014-11-26 15:47:18 +02004195 N, ID(0, &instr_dual_0f_c3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004196 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004197 /* 0xC8 - 0xCF */
4198 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004199 /* 0xD0 - 0xDF */
4200 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4201 /* 0xE0 - 0xEF */
Alex Williamson0a370272014-07-11 11:56:31 -06004202 N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
4203 N, N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004204 /* 0xF0 - 0xFF */
4205 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4206};
4207
Nadav Amit39f062f2014-11-26 15:47:18 +02004208static const struct instr_dual instr_dual_0f_38_f0 = {
4209 I(DstReg | SrcMem | Mov, em_movbe), N
4210};
4211
4212static const struct instr_dual instr_dual_0f_38_f1 = {
4213 I(DstMem | SrcReg | Mov, em_movbe), N
4214};
4215
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004216static const struct gprefix three_byte_0f_38_f0 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004217 ID(0, &instr_dual_0f_38_f0), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004218};
4219
4220static const struct gprefix three_byte_0f_38_f1 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004221 ID(0, &instr_dual_0f_38_f1), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004222};
4223
4224/*
4225 * Insns below are selected by the prefix which indexed by the third opcode
4226 * byte.
4227 */
4228static const struct opcode opcode_map_0f_38[256] = {
4229 /* 0x00 - 0x7f */
4230 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004231 /* 0x80 - 0xef */
4232 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4233 /* 0xf0 - 0xf1 */
Nadav Amit53bb4f72014-12-07 11:49:42 +02004234 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0),
4235 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004236 /* 0xf2 - 0xff */
4237 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004238};
4239
Avi Kivity73fba5f2010-07-29 15:11:53 +03004240#undef D
4241#undef N
4242#undef G
4243#undef GD
4244#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004245#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004246#undef EXT
Nadav Amit2276b512015-01-26 09:32:24 +02004247#undef MD
Nadav Amit2b42fce2015-01-26 09:32:25 +02004248#undef ID
Avi Kivity73fba5f2010-07-29 15:11:53 +03004249
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004250#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004251#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004252#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004253#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004254#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004255
Avi Kivity9dac77f2011-06-01 15:34:25 +03004256static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004257{
4258 unsigned size;
4259
Avi Kivity9dac77f2011-06-01 15:34:25 +03004260 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004261 if (size == 8)
4262 size = 4;
4263 return size;
4264}
4265
4266static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4267 unsigned size, bool sign_extension)
4268{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004269 int rc = X86EMUL_CONTINUE;
4270
4271 op->type = OP_IMM;
4272 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004273 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004274 /* NB. Immediates are sign-extended as necessary. */
4275 switch (op->bytes) {
4276 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004277 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004278 break;
4279 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004280 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004281 break;
4282 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004283 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004284 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004285 case 8:
4286 op->val = insn_fetch(s64, ctxt);
4287 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004288 }
4289 if (!sign_extension) {
4290 switch (op->bytes) {
4291 case 1:
4292 op->val &= 0xff;
4293 break;
4294 case 2:
4295 op->val &= 0xffff;
4296 break;
4297 case 4:
4298 op->val &= 0xffffffff;
4299 break;
4300 }
4301 }
4302done:
4303 return rc;
4304}
4305
Avi Kivitya9945542011-09-13 10:45:41 +03004306static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4307 unsigned d)
4308{
4309 int rc = X86EMUL_CONTINUE;
4310
4311 switch (d) {
4312 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004313 decode_register_operand(ctxt, op);
Avi Kivitya9945542011-09-13 10:45:41 +03004314 break;
4315 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004316 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03004317 break;
4318 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004319 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004320 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03004321 *op = ctxt->memop;
4322 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004323 if (ctxt->d & BitOp)
Avi Kivitya9945542011-09-13 10:45:41 +03004324 fetch_bit_operand(ctxt);
4325 op->orig_val = op->val;
4326 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004327 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004328 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004329 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03004330 case OpAcc:
4331 op->type = OP_REG;
4332 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004333 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya9945542011-09-13 10:45:41 +03004334 fetch_register_operand(op);
4335 op->orig_val = op->val;
4336 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004337 case OpAccLo:
4338 op->type = OP_REG;
4339 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4340 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4341 fetch_register_operand(op);
4342 op->orig_val = op->val;
4343 break;
4344 case OpAccHi:
4345 if (ctxt->d & ByteOp) {
4346 op->type = OP_NONE;
4347 break;
4348 }
4349 op->type = OP_REG;
4350 op->bytes = ctxt->op_bytes;
4351 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4352 fetch_register_operand(op);
4353 op->orig_val = op->val;
4354 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004355 case OpDI:
4356 op->type = OP_MEM;
4357 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4358 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004359 register_address(ctxt, VCPU_REGS_RDI);
Avi Kivitya9945542011-09-13 10:45:41 +03004360 op->addr.mem.seg = VCPU_SREG_ES;
4361 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004362 op->count = 1;
Avi Kivitya9945542011-09-13 10:45:41 +03004363 break;
4364 case OpDX:
4365 op->type = OP_REG;
4366 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004367 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya9945542011-09-13 10:45:41 +03004368 fetch_register_operand(op);
4369 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004370 case OpCL:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004371 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004372 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004373 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004374 break;
4375 case OpImmByte:
4376 rc = decode_imm(ctxt, op, 1, true);
4377 break;
4378 case OpOne:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004379 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004380 op->bytes = 1;
4381 op->val = 1;
4382 break;
4383 case OpImm:
4384 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4385 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004386 case OpImm64:
4387 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4388 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004389 case OpMem8:
4390 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004391 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004392 ctxt->memop.addr.reg = decode_register(ctxt,
4393 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004394 fetch_register_operand(&ctxt->memop);
4395 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004396 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004397 case OpMem16:
4398 ctxt->memop.bytes = 2;
4399 goto mem_common;
4400 case OpMem32:
4401 ctxt->memop.bytes = 4;
4402 goto mem_common;
4403 case OpImmU16:
4404 rc = decode_imm(ctxt, op, 2, false);
4405 break;
4406 case OpImmU:
4407 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4408 break;
4409 case OpSI:
4410 op->type = OP_MEM;
4411 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4412 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004413 register_address(ctxt, VCPU_REGS_RSI);
Bandan Das573e80f2014-04-16 12:46:13 -04004414 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03004415 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004416 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004417 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004418 case OpXLat:
4419 op->type = OP_MEM;
4420 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4421 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004422 address_mask(ctxt,
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004423 reg_read(ctxt, VCPU_REGS_RBX) +
4424 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04004425 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004426 op->val = 0;
4427 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004428 case OpImmFAddr:
4429 op->type = OP_IMM;
4430 op->addr.mem.ea = ctxt->_eip;
4431 op->bytes = ctxt->op_bytes + 2;
4432 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4433 break;
4434 case OpMemFAddr:
4435 ctxt->memop.bytes = ctxt->op_bytes + 2;
4436 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004437 case OpES:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004438 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004439 op->val = VCPU_SREG_ES;
4440 break;
4441 case OpCS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004442 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004443 op->val = VCPU_SREG_CS;
4444 break;
4445 case OpSS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004446 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004447 op->val = VCPU_SREG_SS;
4448 break;
4449 case OpDS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004450 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004451 op->val = VCPU_SREG_DS;
4452 break;
4453 case OpFS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004454 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004455 op->val = VCPU_SREG_FS;
4456 break;
4457 case OpGS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004458 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004459 op->val = VCPU_SREG_GS;
4460 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004461 case OpImplicit:
4462 /* Special instructions do their own operand decoding. */
4463 default:
4464 op->type = OP_NONE; /* Disable writeback. */
4465 break;
4466 }
4467
4468done:
4469 return rc;
4470}
4471
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004472int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004473{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004474 int rc = X86EMUL_CONTINUE;
4475 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004476 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004477 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04004478 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03004479 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004480
Avi Kivityf09ed832011-09-13 10:45:40 +03004481 ctxt->memop.type = OP_NONE;
4482 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004483 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02004484 ctxt->fetch.ptr = ctxt->fetch.data;
4485 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004486 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01004487 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004488 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004489 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02004490 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004491 if (rc != X86EMUL_CONTINUE)
4492 return rc;
4493 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004494
4495 switch (mode) {
4496 case X86EMUL_MODE_REAL:
4497 case X86EMUL_MODE_VM86:
4498 case X86EMUL_MODE_PROT16:
4499 def_op_bytes = def_ad_bytes = 2;
4500 break;
4501 case X86EMUL_MODE_PROT32:
4502 def_op_bytes = def_ad_bytes = 4;
4503 break;
4504#ifdef CONFIG_X86_64
4505 case X86EMUL_MODE_PROT64:
4506 def_op_bytes = 4;
4507 def_ad_bytes = 8;
4508 break;
4509#endif
4510 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004511 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004512 }
4513
Avi Kivity9dac77f2011-06-01 15:34:25 +03004514 ctxt->op_bytes = def_op_bytes;
4515 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004516
4517 /* Legacy prefixes. */
4518 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004519 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004520 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004521 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004522 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004523 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004524 break;
4525 case 0x67: /* address-size override */
4526 if (mode == X86EMUL_MODE_PROT64)
4527 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004528 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004529 else
4530 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004531 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004532 break;
4533 case 0x26: /* ES override */
4534 case 0x2e: /* CS override */
4535 case 0x36: /* SS override */
4536 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004537 has_seg_override = true;
4538 ctxt->seg_override = (ctxt->b >> 3) & 3;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004539 break;
4540 case 0x64: /* FS override */
4541 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004542 has_seg_override = true;
4543 ctxt->seg_override = ctxt->b & 7;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004544 break;
4545 case 0x40 ... 0x4f: /* REX */
4546 if (mode != X86EMUL_MODE_PROT64)
4547 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004548 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004549 continue;
4550 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004551 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004552 break;
4553 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004554 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004555 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004556 break;
4557 default:
4558 goto done_prefixes;
4559 }
4560
4561 /* Any legacy prefix after a REX prefix nullifies its effect. */
4562
Avi Kivity9dac77f2011-06-01 15:34:25 +03004563 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004564 }
4565
4566done_prefixes:
4567
4568 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004569 if (ctxt->rex_prefix & 8)
4570 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004571
4572 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004573 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004574 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004575 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004576 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004577 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004578 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004579
4580 /* 0F_38 opcode map */
4581 if (ctxt->b == 0x38) {
4582 ctxt->opcode_len = 3;
4583 ctxt->b = insn_fetch(u8, ctxt);
4584 opcode = opcode_map_0f_38[ctxt->b];
4585 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004586 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004587 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004588
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004589 if (ctxt->d & ModRM)
4590 ctxt->modrm = insn_fetch(u8, ctxt);
4591
Nadav Amit7fe864d2014-06-02 18:34:03 +03004592 /* vex-prefix instructions are not implemented */
4593 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
Nadav Amitd14cb5d2014-11-02 11:54:58 +02004594 (mode == X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) == 0xc0)) {
Nadav Amit7fe864d2014-06-02 18:34:03 +03004595 ctxt->d = NotImpl;
4596 }
4597
Avi Kivity9dac77f2011-06-01 15:34:25 +03004598 while (ctxt->d & GroupMask) {
4599 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004600 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004601 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004602 opcode = opcode.u.group[goffset];
4603 break;
4604 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004605 goffset = (ctxt->modrm >> 3) & 7;
4606 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004607 opcode = opcode.u.gdual->mod3[goffset];
4608 else
4609 opcode = opcode.u.gdual->mod012[goffset];
4610 break;
4611 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004612 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004613 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004614 break;
4615 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004616 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004617 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004618 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004619 switch (simd_prefix) {
4620 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4621 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4622 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4623 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4624 }
4625 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004626 case Escape:
4627 if (ctxt->modrm > 0xbf)
4628 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4629 else
4630 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4631 break;
Nadav Amit39f062f2014-11-26 15:47:18 +02004632 case InstrDual:
4633 if ((ctxt->modrm >> 6) == 3)
4634 opcode = opcode.u.idual->mod3;
4635 else
4636 opcode = opcode.u.idual->mod012;
4637 break;
Nadav Amit2276b512015-01-26 09:32:24 +02004638 case ModeDual:
4639 if (ctxt->mode == X86EMUL_MODE_PROT64)
4640 opcode = opcode.u.mdual->mode64;
4641 else
4642 opcode = opcode.u.mdual->mode32;
4643 break;
Avi Kivity46561642011-04-24 14:09:59 +03004644 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004645 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004646 }
Avi Kivity46561642011-04-24 14:09:59 +03004647
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004648 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004649 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004650 }
4651
Paolo Bonzinie24186e2014-03-27 12:00:57 +01004652 /* Unrecognised? */
4653 if (ctxt->d == 0)
4654 return EMULATION_FAILED;
4655
Avi Kivity9dac77f2011-06-01 15:34:25 +03004656 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004657
Nadav Amit3a6095a2014-08-13 16:50:13 +03004658 if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD)))
4659 return EMULATION_FAILED;
4660
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004661 if (unlikely(ctxt->d &
Nadav Amited9aad22014-11-02 11:55:00 +02004662 (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch|
4663 No16))) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004664 /*
4665 * These are copied unconditionally here, and checked unconditionally
4666 * in x86_emulate_insn.
4667 */
4668 ctxt->check_perm = opcode.check_perm;
4669 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004670
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004671 if (ctxt->d & NotImpl)
4672 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004673
Nadav Amit58b70752014-10-24 11:35:09 +03004674 if (mode == X86EMUL_MODE_PROT64) {
4675 if (ctxt->op_bytes == 4 && (ctxt->d & Stack))
4676 ctxt->op_bytes = 8;
4677 else if (ctxt->d & NearBranch)
4678 ctxt->op_bytes = 8;
4679 }
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004680
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004681 if (ctxt->d & Op3264) {
4682 if (mode == X86EMUL_MODE_PROT64)
4683 ctxt->op_bytes = 8;
4684 else
4685 ctxt->op_bytes = 4;
4686 }
4687
Nadav Amited9aad22014-11-02 11:55:00 +02004688 if ((ctxt->d & No16) && ctxt->op_bytes == 2)
4689 ctxt->op_bytes = 4;
4690
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004691 if (ctxt->d & Sse)
4692 ctxt->op_bytes = 16;
4693 else if (ctxt->d & Mmx)
4694 ctxt->op_bytes = 8;
4695 }
Avi Kivity12537912011-03-29 11:41:27 +02004696
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004697 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004698 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004699 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04004700 if (!has_seg_override) {
4701 has_seg_override = true;
4702 ctxt->seg_override = ctxt->modrm_seg;
4703 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004704 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004705 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004706 if (rc != X86EMUL_CONTINUE)
4707 goto done;
4708
Bandan Das573e80f2014-04-16 12:46:13 -04004709 if (!has_seg_override)
4710 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004711
Bandan Das573e80f2014-04-16 12:46:13 -04004712 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004713
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004714 /*
4715 * Decode and fetch the source operand: register, memory
4716 * or immediate.
4717 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004718 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004719 if (rc != X86EMUL_CONTINUE)
4720 goto done;
4721
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004722 /*
4723 * Decode and fetch the second source operand: register, memory
4724 * or immediate.
4725 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004726 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004727 if (rc != X86EMUL_CONTINUE)
4728 goto done;
4729
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004730 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03004731 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004732
Bandan Das41061cd2014-04-16 12:46:14 -04004733 if (ctxt->rip_relative)
Nadav Amit1c1c35a2014-11-19 17:43:09 +02004734 ctxt->memopp->addr.mem.ea = address_mask(ctxt,
4735 ctxt->memopp->addr.mem.ea + ctxt->_eip);
Avi Kivitycb16c342011-06-19 19:21:11 +03004736
Paolo Bonzinia430c912014-10-23 14:54:14 +02004737done:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004738 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004739}
4740
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004741bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4742{
4743 return ctxt->d & PageTable;
4744}
4745
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004746static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4747{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004748 /* The second termination condition only applies for REPE
4749 * and REPNE. Test if the repeat string operation prefix is
4750 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4751 * corresponding termination condition according to:
4752 * - if REPE/REPZ and ZF = 0 then done
4753 * - if REPNE/REPNZ and ZF = 1 then done
4754 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004755 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4756 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4757 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004758 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004759 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004760 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4761 return true;
4762
4763 return false;
4764}
4765
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004766static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4767{
4768 bool fault = false;
4769
4770 ctxt->ops->get_fpu(ctxt);
4771 asm volatile("1: fwait \n\t"
4772 "2: \n\t"
4773 ".pushsection .fixup,\"ax\" \n\t"
4774 "3: \n\t"
4775 "movb $1, %[fault] \n\t"
4776 "jmp 2b \n\t"
4777 ".popsection \n\t"
4778 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004779 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004780 ctxt->ops->put_fpu(ctxt);
4781
4782 if (unlikely(fault))
4783 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4784
4785 return X86EMUL_CONTINUE;
4786}
4787
4788static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4789 struct operand *op)
4790{
4791 if (op->type == OP_MM)
4792 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4793}
4794
Avi Kivitye28bbd42013-01-04 16:18:48 +02004795static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4796{
4797 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivityb9fa4092013-02-09 11:31:48 +02004798 if (!(ctxt->d & ByteOp))
4799 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Avi Kivitye28bbd42013-01-04 16:18:48 +02004800 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004801 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
4802 [fastop]"+S"(fop)
4803 : "c"(ctxt->src2.val));
Avi Kivitye28bbd42013-01-04 16:18:48 +02004804 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004805 if (!fop) /* exception is returned in fop variable */
4806 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02004807 return X86EMUL_CONTINUE;
4808}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004809
Bandan Das14985072014-04-16 12:46:09 -04004810void init_decode_cache(struct x86_emulate_ctxt *ctxt)
4811{
Bandan Das573e80f2014-04-16 12:46:13 -04004812 memset(&ctxt->rip_relative, 0,
4813 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04004814
Bandan Das14985072014-04-16 12:46:09 -04004815 ctxt->io_read.pos = 0;
4816 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04004817 ctxt->mem_read.end = 0;
4818}
4819
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004820int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004821{
Mathias Krause0225fb52012-08-30 01:30:16 +02004822 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004823 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004824 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004825
Avi Kivity9dac77f2011-06-01 15:34:25 +03004826 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004827
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004828 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004829 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004830 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004831 goto done;
4832 }
4833
Avi Kivity9dac77f2011-06-01 15:34:25 +03004834 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004835 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004836 goto done;
4837 }
4838
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004839 if (unlikely(ctxt->d &
4840 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
4841 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4842 (ctxt->d & Undefined)) {
4843 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004844 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004845 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004846
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004847 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4848 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
4849 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004850 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004851 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004852
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004853 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
4854 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004855 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004856 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004857
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004858 if (ctxt->d & Mmx) {
4859 rc = flush_pending_x87_faults(ctxt);
4860 if (rc != X86EMUL_CONTINUE)
4861 goto done;
4862 /*
4863 * Now that we know the fpu is exception safe, we can fetch
4864 * operands from it.
4865 */
4866 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4867 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4868 if (!(ctxt->d & Mov))
4869 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4870 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004871
Bandan Das685bbf42014-04-16 12:46:10 -04004872 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004873 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4874 X86_ICPT_PRE_EXCEPT);
4875 if (rc != X86EMUL_CONTINUE)
4876 goto done;
4877 }
4878
Nadav Amit64a38292014-12-10 11:19:04 +02004879 /* Instruction can only be executed in protected mode */
4880 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
4881 rc = emulate_ud(ctxt);
4882 goto done;
4883 }
4884
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004885 /* Privileged instruction can be executed only in CPL=0 */
4886 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Nadav Amit68efa762014-06-18 17:19:35 +03004887 if (ctxt->d & PrivUD)
4888 rc = emulate_ud(ctxt);
4889 else
4890 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004891 goto done;
4892 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004893
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004894 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04004895 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004896 rc = ctxt->check_perm(ctxt);
4897 if (rc != X86EMUL_CONTINUE)
4898 goto done;
4899 }
4900
Bandan Das685bbf42014-04-16 12:46:10 -04004901 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004902 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4903 X86_ICPT_POST_EXCEPT);
4904 if (rc != X86EMUL_CONTINUE)
4905 goto done;
4906 }
4907
4908 if (ctxt->rep_prefix && (ctxt->d & String)) {
4909 /* All REP prefixes have the same first termination condition */
4910 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
4911 ctxt->eip = ctxt->_eip;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004912 ctxt->eflags &= ~EFLG_RF;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004913 goto done;
4914 }
4915 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004916 }
4917
Avi Kivity9dac77f2011-06-01 15:34:25 +03004918 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4919 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4920 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004921 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004922 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004923 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004924 }
4925
Avi Kivity9dac77f2011-06-01 15:34:25 +03004926 if (ctxt->src2.type == OP_MEM) {
4927 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4928 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004929 if (rc != X86EMUL_CONTINUE)
4930 goto done;
4931 }
4932
Avi Kivity9dac77f2011-06-01 15:34:25 +03004933 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004934 goto special_insn;
4935
4936
Avi Kivity9dac77f2011-06-01 15:34:25 +03004937 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004938 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004939 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4940 &ctxt->dst.val, ctxt->dst.bytes);
Nadav Amitc205fb72014-12-25 02:52:16 +02004941 if (rc != X86EMUL_CONTINUE) {
4942 if (rc == X86EMUL_PROPAGATE_FAULT &&
4943 ctxt->exception.vector == PF_VECTOR)
4944 ctxt->exception.error_code |= PFERR_WRITE_MASK;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004945 goto done;
Nadav Amitc205fb72014-12-25 02:52:16 +02004946 }
Avi Kivity038e51d2007-01-22 20:40:40 -08004947 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004948 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004949
Avi Kivity018a98d2007-11-27 19:30:56 +02004950special_insn:
4951
Bandan Das685bbf42014-04-16 12:46:10 -04004952 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004953 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004954 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004955 if (rc != X86EMUL_CONTINUE)
4956 goto done;
4957 }
4958
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004959 if (ctxt->rep_prefix && (ctxt->d & String))
4960 ctxt->eflags |= EFLG_RF;
4961 else
4962 ctxt->eflags &= ~EFLG_RF;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004963
Avi Kivity9dac77f2011-06-01 15:34:25 +03004964 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004965 if (ctxt->d & Fastop) {
4966 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4967 rc = fastop(ctxt, fop);
4968 if (rc != X86EMUL_CONTINUE)
4969 goto done;
4970 goto writeback;
4971 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004972 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004973 if (rc != X86EMUL_CONTINUE)
4974 goto done;
4975 goto writeback;
4976 }
4977
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004978 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004979 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004980 else if (ctxt->opcode_len == 3)
4981 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004982
Avi Kivity9dac77f2011-06-01 15:34:25 +03004983 switch (ctxt->b) {
Gleb Natapovb2833e32009-04-12 13:36:30 +03004984 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004985 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03004986 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004987 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004988 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004989 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004990 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004991 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004992 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03004993 ctxt->dst.type = OP_NONE;
4994 else
4995 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004996 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004997 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004998 switch (ctxt->op_bytes) {
4999 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
5000 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
5001 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005002 }
5003 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005004 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005005 rc = emulate_int(ctxt, 3);
5006 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005007 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005008 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005009 break;
5010 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005011 if (ctxt->eflags & EFLG_OF)
5012 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005013 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005014 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09005015 case 0xeb: /* jmp rel short */
Nadav Amit234f3ce2014-09-18 22:39:38 +03005016 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity9dac77f2011-06-01 15:34:25 +03005017 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005018 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005019 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03005020 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03005021 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005022 case 0xf5: /* cmc */
5023 /* complement carry flag from eflags reg */
5024 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005025 break;
5026 case 0xf8: /* clc */
5027 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005028 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03005029 case 0xf9: /* stc */
5030 ctxt->eflags |= EFLG_CF;
5031 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005032 case 0xfc: /* cld */
5033 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005034 break;
5035 case 0xfd: /* std */
5036 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005037 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005038 default:
5039 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005040 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005041
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005042 if (rc != X86EMUL_CONTINUE)
5043 goto done;
5044
Avi Kivity018a98d2007-11-27 19:30:56 +02005045writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02005046 if (ctxt->d & SrcWrite) {
5047 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
5048 rc = writeback(ctxt, &ctxt->src);
5049 if (rc != X86EMUL_CONTINUE)
5050 goto done;
5051 }
Nadav Amitee212292014-06-15 16:12:58 +03005052 if (!(ctxt->d & NoWrite)) {
5053 rc = writeback(ctxt, &ctxt->dst);
5054 if (rc != X86EMUL_CONTINUE)
5055 goto done;
5056 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005057
Gleb Natapov5cd21912010-03-18 15:20:26 +02005058 /*
5059 * restore dst type in case the decoding will be reused
5060 * (happens for string instruction )
5061 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005062 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005063
Avi Kivity9dac77f2011-06-01 15:34:25 +03005064 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005065 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02005066
Avi Kivity9dac77f2011-06-01 15:34:25 +03005067 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005068 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02005069
Avi Kivity9dac77f2011-06-01 15:34:25 +03005070 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005071 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005072 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005073 if ((ctxt->d & SrcMask) == SrcSI)
5074 count = ctxt->src.count;
5075 else
5076 count = ctxt->dst.count;
Paolo Bonzini01485a22014-11-19 18:25:08 +01005077 register_address_increment(ctxt, VCPU_REGS_RCX, -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005078
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005079 if (!string_insn_completed(ctxt)) {
5080 /*
5081 * Re-enter guest when pio read ahead buffer is empty
5082 * or, if it is not used, after each 1024 iteration.
5083 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005084 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005085 (r->end == 0 || r->end != r->pos)) {
5086 /*
5087 * Reset read cache. Usually happens before
5088 * decode, but since instruction is restarted
5089 * we have to do it here.
5090 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005091 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005092 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005093 return EMULATION_RESTART;
5094 }
5095 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03005096 }
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03005097 ctxt->eflags &= ~EFLG_RF;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005098 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005099
Avi Kivity9dac77f2011-06-01 15:34:25 +03005100 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02005101
5102done:
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005103 if (rc == X86EMUL_PROPAGATE_FAULT) {
5104 WARN_ON(ctxt->exception.vector > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +02005105 ctxt->have_exception = true;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005106 }
Joerg Roedel775fde82011-04-04 12:39:24 +02005107 if (rc == X86EMUL_INTERCEPTED)
5108 return EMULATION_INTERCEPTED;
5109
Avi Kivitydd856ef2012-08-27 23:46:17 +03005110 if (rc == X86EMUL_CONTINUE)
5111 writeback_registers(ctxt);
5112
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005113 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005114
5115twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005116 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02005117 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02005118 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005119 break;
5120 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02005121 case 0x0d: /* GrpP (prefetch) */
5122 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02005123 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02005124 break;
5125 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005126 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02005127 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005128 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005129 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005130 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005131 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03005132 if (test_cc(ctxt->b, ctxt->eflags))
5133 ctxt->dst.val = ctxt->src.val;
5134 else if (ctxt->mode != X86EMUL_MODE_PROT64 ||
5135 ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005136 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08005137 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03005138 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03005139 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005140 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005141 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08005142 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005143 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08005144 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005145 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005146 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005147 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03005148 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005149 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005150 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005151 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005152 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03005153 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005154 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005155 default:
5156 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005157 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005158
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005159threebyte_insn:
5160
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005161 if (rc != X86EMUL_CONTINUE)
5162 goto done;
5163
Avi Kivity6aa8b732006-12-10 02:21:36 -08005164 goto writeback;
5165
5166cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02005167 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005168}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005169
5170void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
5171{
5172 invalidate_registers(ctxt);
5173}
5174
5175void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
5176{
5177 writeback_registers(ctxt);
5178}