blob: 0892622f9258e6265c575b4f9157fc5decc4ee35 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivityb7d491e2013-01-04 16:18:49 +020027#include <linux/stringify.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028
Avi Kivity3eeb3282010-01-21 15:31:48 +020029#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020030#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020031
Avi Kivity6aa8b732006-12-10 02:21:36 -080032/*
Avi Kivitya99455492011-09-13 10:45:41 +030033 * Operand types
34 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030035#define OpNone 0ull
36#define OpImplicit 1ull /* No generic decode */
37#define OpReg 2ull /* Register */
38#define OpMem 3ull /* Memory */
39#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
40#define OpDI 5ull /* ES:DI/EDI/RDI */
41#define OpMem64 6ull /* Memory, 64-bit */
42#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
43#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030044#define OpCL 9ull /* CL register (for shifts) */
45#define OpImmByte 10ull /* 8-bit sign extended immediate */
46#define OpOne 11ull /* Implied 1 */
Nadav Amit5e2c6882012-12-06 21:55:10 -020047#define OpImm 12ull /* Sign extended up to 32-bit immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030048#define OpMem16 13ull /* Memory operand (16-bit). */
49#define OpMem32 14ull /* Memory operand (32-bit). */
50#define OpImmU 15ull /* Immediate operand, zero extended */
51#define OpSI 16ull /* SI/ESI/RSI */
52#define OpImmFAddr 17ull /* Immediate far address */
53#define OpMemFAddr 18ull /* Far address in memory */
54#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030055#define OpES 20ull /* ES */
56#define OpCS 21ull /* CS */
57#define OpSS 22ull /* SS */
58#define OpDS 23ull /* DS */
59#define OpFS 24ull /* FS */
60#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020061#define OpMem8 26ull /* 8-bit zero extended memory operand */
Nadav Amit5e2c6882012-12-06 21:55:10 -020062#define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */
Paolo Bonzini7fa57952013-05-09 11:32:50 +020063#define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */
Avi Kivity820207c2013-02-09 11:31:45 +020064#define OpAccLo 29ull /* Low part of extended acc (AX/AX/EAX/RAX) */
65#define OpAccHi 30ull /* High part of extended acc (-/DX/EDX/RDX) */
Avi Kivitya99455492011-09-13 10:45:41 +030066
Avi Kivity0fe59122011-09-13 10:45:47 +030067#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030068#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya99455492011-09-13 10:45:41 +030069
70/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080071 * Opcode effective-address decode tables.
72 * Note that we only emulate instructions that have at least one memory
73 * operand (excluding implicit stack references). We assume that stack
74 * references and instruction fetches will never occur in special memory
75 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
76 * not be handled.
77 */
78
79/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030080#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080081/* Destination operand type. */
Avi Kivitya99455492011-09-13 10:45:41 +030082#define DstShift 1
83#define ImplicitOps (OpImplicit << DstShift)
84#define DstReg (OpReg << DstShift)
85#define DstMem (OpMem << DstShift)
86#define DstAcc (OpAcc << DstShift)
87#define DstDI (OpDI << DstShift)
88#define DstMem64 (OpMem64 << DstShift)
89#define DstImmUByte (OpImmUByte << DstShift)
90#define DstDX (OpDX << DstShift)
Avi Kivity820207c2013-02-09 11:31:45 +020091#define DstAccLo (OpAccLo << DstShift)
Avi Kivitya99455492011-09-13 10:45:41 +030092#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080093/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030094#define SrcShift 6
95#define SrcNone (OpNone << SrcShift)
96#define SrcReg (OpReg << SrcShift)
97#define SrcMem (OpMem << SrcShift)
98#define SrcMem16 (OpMem16 << SrcShift)
99#define SrcMem32 (OpMem32 << SrcShift)
100#define SrcImm (OpImm << SrcShift)
101#define SrcImmByte (OpImmByte << SrcShift)
102#define SrcOne (OpOne << SrcShift)
103#define SrcImmUByte (OpImmUByte << SrcShift)
104#define SrcImmU (OpImmU << SrcShift)
105#define SrcSI (OpSI << SrcShift)
Paolo Bonzini7fa57952013-05-09 11:32:50 +0200106#define SrcXLat (OpXLat << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300107#define SrcImmFAddr (OpImmFAddr << SrcShift)
108#define SrcMemFAddr (OpMemFAddr << SrcShift)
109#define SrcAcc (OpAcc << SrcShift)
110#define SrcImmU16 (OpImmU16 << SrcShift)
Nadav Amit5e2c6882012-12-06 21:55:10 -0200111#define SrcImm64 (OpImm64 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300112#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200113#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity820207c2013-02-09 11:31:45 +0200114#define SrcAccHi (OpAccHi << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300115#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300116#define BitOp (1<<11)
117#define MemAbs (1<<12) /* Memory operand is absolute displacement */
118#define String (1<<13) /* String instruction (rep capable) */
119#define Stack (1<<14) /* Stack instruction (push/pop) */
120#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
121#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
122#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
123#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
124#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
Gleb Natapov045a2822012-12-20 16:57:43 +0200125#define Escape (5<<15) /* Escape to coprocessor instruction */
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300126#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300127/* Generic ModRM decode. */
128#define ModRM (1<<19)
129/* Destination is only written; never read. */
130#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300131/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200132#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Borislav Petkovb51e9742013-09-22 16:44:52 +0200133#define EmulateOnUD (1<<22) /* Emulate if unsupported by the host */
Avi Kivity5a506b12010-08-01 15:10:29 +0300134#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300135#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300136#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200137#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200138#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300139#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800140#define PageTable (1 << 29) /* instruction used to write page table */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300141#define NotImpl (1 << 30) /* instruction is not implemented */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100142/* Source 2 operand type */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300143#define Src2Shift (31)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300144#define Src2None (OpNone << Src2Shift)
Avi Kivityab2c5ce2013-02-09 11:31:46 +0200145#define Src2Mem (OpMem << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300146#define Src2CL (OpCL << Src2Shift)
147#define Src2ImmByte (OpImmByte << Src2Shift)
148#define Src2One (OpOne << Src2Shift)
149#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300150#define Src2ES (OpES << Src2Shift)
151#define Src2CS (OpCS << Src2Shift)
152#define Src2SS (OpSS << Src2Shift)
153#define Src2DS (OpDS << Src2Shift)
154#define Src2FS (OpFS << Src2Shift)
155#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300156#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300157#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Avi Kivity1c11b372012-04-09 18:39:59 +0300158#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
159#define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */
160#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
Avi Kivitye28bbd42013-01-04 16:18:48 +0200161#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
Avi Kivityb6744dc2013-01-04 16:18:50 +0200162#define NoWrite ((u64)1 << 45) /* No writeback */
Avi Kivityfb32b1e2013-02-09 11:31:44 +0200163#define SrcWrite ((u64)1 << 46) /* Write back src operand */
Nadav Amit9b88ae92014-05-25 23:05:21 +0300164#define NoMod ((u64)1 << 47) /* Mod field is ignored */
Paolo Bonzinid40a6892014-03-27 11:58:02 +0100165#define Intercept ((u64)1 << 48) /* Has valid intercept field */
166#define CheckPerm ((u64)1 << 49) /* Has valid check_perm field */
Nadav Amit10e38fc2014-06-18 17:19:34 +0300167#define NoBigReal ((u64)1 << 50) /* No big real mode */
Nadav Amit68efa762014-06-18 17:19:35 +0300168#define PrivUD ((u64)1 << 51) /* #UD instead of #GP on CPL > 0 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800169
Avi Kivity820207c2013-02-09 11:31:45 +0200170#define DstXacc (DstAccLo | SrcAccHi | SrcWrite)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800171
Avi Kivityd0e53322010-07-29 15:11:54 +0300172#define X2(x...) x, x
173#define X3(x...) X2(x), x
174#define X4(x...) X2(x), X2(x)
175#define X5(x...) X4(x), x
176#define X6(x...) X4(x), X2(x)
177#define X7(x...) X4(x), X3(x)
178#define X8(x...) X4(x), X4(x)
179#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300180
Avi Kivitye28bbd42013-01-04 16:18:48 +0200181#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
182#define FASTOP_SIZE 8
183
184/*
185 * fastop functions have a special calling convention:
186 *
Avi Kivity017da7b2013-02-09 11:31:47 +0200187 * dst: rax (in/out)
188 * src: rdx (in/out)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200189 * src2: rcx (in)
190 * flags: rflags (in/out)
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200191 * ex: rsi (in:fastop pointer, out:zero if exception)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200192 *
193 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
194 * different operand sizes can be reached by calculation, rather than a jump
195 * table (which would be bigger than the code).
196 *
197 * fastop functions are declared as taking a never-defined fastop parameter,
198 * so they can't be called from C directly.
199 */
200
201struct fastop;
202
Avi Kivityd65b1de2010-07-29 15:11:35 +0300203struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300204 u64 flags : 56;
205 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300206 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300207 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200208 const struct opcode *group;
209 const struct group_dual *gdual;
210 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200211 const struct escape *esc;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200212 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300213 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200214 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300215};
216
217struct group_dual {
218 struct opcode mod012[8];
219 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300220};
221
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200222struct gprefix {
223 struct opcode pfx_no;
224 struct opcode pfx_66;
225 struct opcode pfx_f2;
226 struct opcode pfx_f3;
227};
228
Gleb Natapov045a2822012-12-20 16:57:43 +0200229struct escape {
230 struct opcode op[8];
231 struct opcode high[64];
232};
233
Avi Kivity6aa8b732006-12-10 02:21:36 -0800234/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200235#define EFLG_ID (1<<21)
236#define EFLG_VIP (1<<20)
237#define EFLG_VIF (1<<19)
238#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200239#define EFLG_VM (1<<17)
240#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200241#define EFLG_IOPL (3<<12)
242#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800243#define EFLG_OF (1<<11)
244#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200245#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200246#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800247#define EFLG_SF (1<<7)
248#define EFLG_ZF (1<<6)
249#define EFLG_AF (1<<4)
250#define EFLG_PF (1<<2)
251#define EFLG_CF (1<<0)
252
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300253#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
254#define EFLG_RESERVED_ONE_MASK 2
255
Avi Kivitydd856ef2012-08-27 23:46:17 +0300256static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
257{
258 if (!(ctxt->regs_valid & (1 << nr))) {
259 ctxt->regs_valid |= 1 << nr;
260 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
261 }
262 return ctxt->_regs[nr];
263}
264
265static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
266{
267 ctxt->regs_valid |= 1 << nr;
268 ctxt->regs_dirty |= 1 << nr;
269 return &ctxt->_regs[nr];
270}
271
272static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
273{
274 reg_read(ctxt, nr);
275 return reg_write(ctxt, nr);
276}
277
278static void writeback_registers(struct x86_emulate_ctxt *ctxt)
279{
280 unsigned reg;
281
282 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
283 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
284}
285
286static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
287{
288 ctxt->regs_dirty = 0;
289 ctxt->regs_valid = 0;
290}
291
Avi Kivity6aa8b732006-12-10 02:21:36 -0800292/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800293 * These EFLAGS bits are restored from saved value during emulation, and
294 * any changes are written back to the saved value after emulation.
295 */
296#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
297
Avi Kivitydda96d82008-11-26 15:14:10 +0200298#ifdef CONFIG_X86_64
299#define ON64(x) x
300#else
301#define ON64(x)
302#endif
303
Avi Kivity4d758342013-01-19 19:51:55 +0200304static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *));
305
Avi Kivityb7d491e2013-01-04 16:18:49 +0200306#define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t"
307#define FOP_RET "ret \n\t"
308
309#define FOP_START(op) \
310 extern void em_##op(struct fastop *fake); \
311 asm(".pushsection .text, \"ax\" \n\t" \
312 ".global em_" #op " \n\t" \
313 FOP_ALIGN \
314 "em_" #op ": \n\t"
315
316#define FOP_END \
317 ".popsection")
318
Avi Kivity0bdea062013-01-19 19:51:50 +0200319#define FOPNOP() FOP_ALIGN FOP_RET
320
Avi Kivityb7d491e2013-01-04 16:18:49 +0200321#define FOP1E(op, dst) \
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200322 FOP_ALIGN "10: " #op " %" #dst " \n\t" FOP_RET
323
324#define FOP1EEX(op, dst) \
325 FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200326
327#define FASTOP1(op) \
328 FOP_START(op) \
329 FOP1E(op##b, al) \
330 FOP1E(op##w, ax) \
331 FOP1E(op##l, eax) \
332 ON64(FOP1E(op##q, rax)) \
333 FOP_END
334
Avi Kivityb9fa4092013-02-09 11:31:48 +0200335/* 1-operand, using src2 (for MUL/DIV r/m) */
336#define FASTOP1SRC2(op, name) \
337 FOP_START(name) \
338 FOP1E(op, cl) \
339 FOP1E(op, cx) \
340 FOP1E(op, ecx) \
341 ON64(FOP1E(op, rcx)) \
342 FOP_END
343
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200344/* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */
345#define FASTOP1SRC2EX(op, name) \
346 FOP_START(name) \
347 FOP1EEX(op, cl) \
348 FOP1EEX(op, cx) \
349 FOP1EEX(op, ecx) \
350 ON64(FOP1EEX(op, rcx)) \
351 FOP_END
352
Avi Kivityf7857f32013-01-04 16:18:53 +0200353#define FOP2E(op, dst, src) \
354 FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET
355
356#define FASTOP2(op) \
357 FOP_START(op) \
Avi Kivity017da7b2013-02-09 11:31:47 +0200358 FOP2E(op##b, al, dl) \
359 FOP2E(op##w, ax, dx) \
360 FOP2E(op##l, eax, edx) \
361 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivityf7857f32013-01-04 16:18:53 +0200362 FOP_END
363
Avi Kivity11c363b2013-01-19 19:51:54 +0200364/* 2 operand, word only */
365#define FASTOP2W(op) \
366 FOP_START(op) \
367 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200368 FOP2E(op##w, ax, dx) \
369 FOP2E(op##l, eax, edx) \
370 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivity11c363b2013-01-19 19:51:54 +0200371 FOP_END
372
Avi Kivity007a3b52013-01-19 19:51:51 +0200373/* 2 operand, src is CL */
374#define FASTOP2CL(op) \
375 FOP_START(op) \
376 FOP2E(op##b, al, cl) \
377 FOP2E(op##w, ax, cl) \
378 FOP2E(op##l, eax, cl) \
379 ON64(FOP2E(op##q, rax, cl)) \
380 FOP_END
381
Avi Kivity0bdea062013-01-19 19:51:50 +0200382#define FOP3E(op, dst, src, src2) \
383 FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
384
385/* 3-operand, word-only, src2=cl */
386#define FASTOP3WCL(op) \
387 FOP_START(op) \
388 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200389 FOP3E(op##w, ax, dx, cl) \
390 FOP3E(op##l, eax, edx, cl) \
391 ON64(FOP3E(op##q, rax, rdx, cl)) \
Avi Kivity0bdea062013-01-19 19:51:50 +0200392 FOP_END
393
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200394/* Special case for SETcc - 1 instruction per cc */
395#define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t"
396
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200397asm(".global kvm_fastop_exception \n"
398 "kvm_fastop_exception: xor %esi, %esi; ret");
399
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200400FOP_START(setcc)
401FOP_SETCC(seto)
402FOP_SETCC(setno)
403FOP_SETCC(setc)
404FOP_SETCC(setnc)
405FOP_SETCC(setz)
406FOP_SETCC(setnz)
407FOP_SETCC(setbe)
408FOP_SETCC(setnbe)
409FOP_SETCC(sets)
410FOP_SETCC(setns)
411FOP_SETCC(setp)
412FOP_SETCC(setnp)
413FOP_SETCC(setl)
414FOP_SETCC(setnl)
415FOP_SETCC(setle)
416FOP_SETCC(setnle)
417FOP_END;
418
Paolo Bonzini326f5782013-05-09 11:32:51 +0200419FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET
420FOP_END;
421
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200422static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
423 enum x86_intercept intercept,
424 enum x86_intercept_stage stage)
425{
426 struct x86_instruction_info info = {
427 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300428 .rep_prefix = ctxt->rep_prefix,
429 .modrm_mod = ctxt->modrm_mod,
430 .modrm_reg = ctxt->modrm_reg,
431 .modrm_rm = ctxt->modrm_rm,
432 .src_val = ctxt->src.val64,
Jan Kiszka6cbc5f52014-06-30 12:52:55 +0200433 .dst_val = ctxt->dst.val64,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300434 .src_bytes = ctxt->src.bytes,
435 .dst_bytes = ctxt->dst.bytes,
436 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200437 .next_rip = ctxt->eip,
438 };
439
Avi Kivity29535382011-04-20 13:37:53 +0300440 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200441}
442
Avi Kivityf47cfa32012-06-07 17:49:24 +0300443static void assign_masked(ulong *dest, ulong src, ulong mask)
444{
445 *dest = (*dest & ~mask) | (src & mask);
446}
447
Avi Kivity9dac77f2011-06-01 15:34:25 +0300448static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800449{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300450 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800451}
452
Avi Kivityf47cfa32012-06-07 17:49:24 +0300453static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
454{
455 u16 sel;
456 struct desc_struct ss;
457
458 if (ctxt->mode == X86EMUL_MODE_PROT64)
459 return ~0UL;
460 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
461 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
462}
463
Avi Kivity612e89f2012-06-12 20:03:23 +0300464static int stack_size(struct x86_emulate_ctxt *ctxt)
465{
466 return (__fls(stack_mask(ctxt)) + 1) >> 3;
467}
468
Avi Kivity6aa8b732006-12-10 02:21:36 -0800469/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800470static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300471address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800472{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300473 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800474 return reg;
475 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300476 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800477}
478
479static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300480register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800481{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300482 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800483}
484
Avi Kivity5ad105e2012-08-19 14:34:31 +0300485static void masked_increment(ulong *reg, ulong mask, int inc)
486{
487 assign_masked(reg, *reg + inc, mask);
488}
489
Harvey Harrison7a9572752008-02-19 07:40:41 -0800490static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300491register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800492{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300493 ulong mask;
494
Avi Kivity9dac77f2011-06-01 15:34:25 +0300495 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300496 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800497 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300498 mask = ad_mask(ctxt);
499 masked_increment(reg, mask, inc);
500}
501
502static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
503{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300504 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800505}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800506
Avi Kivity9dac77f2011-06-01 15:34:25 +0300507static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800508{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300509 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800510}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300511
Avi Kivity56697682011-04-03 14:08:51 +0300512static u32 desc_limit_scaled(struct desc_struct *desc)
513{
514 u32 limit = get_desc_limit(desc);
515
516 return desc->g ? (limit << 12) | 0xfff : limit;
517}
518
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900519static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300520{
521 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
522 return 0;
523
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900524 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300525}
526
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200527static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
528 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300529{
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +0200530 WARN_ON(vec > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +0200531 ctxt->exception.vector = vec;
532 ctxt->exception.error_code = error;
533 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200534 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300535}
536
Joerg Roedel3b88e412011-04-04 12:39:29 +0200537static int emulate_db(struct x86_emulate_ctxt *ctxt)
538{
539 return emulate_exception(ctxt, DB_VECTOR, 0, false);
540}
541
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200542static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300543{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200544 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300545}
546
Avi Kivity618ff152011-04-03 12:32:09 +0300547static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
548{
549 return emulate_exception(ctxt, SS_VECTOR, err, true);
550}
551
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200552static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300553{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200554 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300555}
556
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200557static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300558{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200559 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300560}
561
Avi Kivity34d1f492010-08-26 11:59:01 +0300562static int emulate_de(struct x86_emulate_ctxt *ctxt)
563{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200564 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300565}
566
Avi Kivity1253791d2011-03-29 11:41:27 +0200567static int emulate_nm(struct x86_emulate_ctxt *ctxt)
568{
569 return emulate_exception(ctxt, NM_VECTOR, 0, false);
570}
571
Avi Kivity1aa36612011-04-27 13:20:30 +0300572static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
573{
574 u16 selector;
575 struct desc_struct desc;
576
577 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
578 return selector;
579}
580
581static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
582 unsigned seg)
583{
584 u16 dummy;
585 u32 base3;
586 struct desc_struct desc;
587
588 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
589 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
590}
591
Avi Kivity1c11b372012-04-09 18:39:59 +0300592/*
593 * x86 defines three classes of vector instructions: explicitly
594 * aligned, explicitly unaligned, and the rest, which change behaviour
595 * depending on whether they're AVX encoded or not.
596 *
597 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
598 * subject to the same check.
599 */
600static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
601{
602 if (likely(size < 16))
603 return false;
604
605 if (ctxt->d & Aligned)
606 return true;
607 else if (ctxt->d & Unaligned)
608 return false;
609 else if (ctxt->d & Avx)
610 return false;
611 else
612 return true;
613}
614
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400615static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300616 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400617 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300618 ulong *linear)
619{
Avi Kivity618ff152011-04-03 12:32:09 +0300620 struct desc_struct desc;
621 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300622 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300623 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300624 u16 sel;
Gleb Natapov3a78a4f2012-12-20 16:57:42 +0200625 unsigned cpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300626
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900627 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300628 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300629 case X86EMUL_MODE_PROT64:
630 if (((signed long)la << 16) >> 16 != la)
631 return emulate_gp(ctxt, 0);
632 break;
633 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300634 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
635 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300636 if (!usable)
637 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200638 /* code segment in protected mode or read-only data segment */
639 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
640 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300641 goto bad;
642 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400643 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300644 goto bad;
645 lim = desc_limit_scaled(&desc);
Nadav Amit10e38fc2014-06-18 17:19:34 +0300646 if ((ctxt->mode == X86EMUL_MODE_REAL) && !fetch &&
647 (ctxt->d & NoBigReal)) {
648 /* la is between zero and 0xffff */
649 if (la > 0xffff || (u32)(la + size - 1) > 0xffff)
650 goto bad;
651 } else if ((desc.type & 8) || !(desc.type & 4)) {
Avi Kivity618ff152011-04-03 12:32:09 +0300652 /* expand-up segment */
653 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
654 goto bad;
655 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800656 /* expand-down segment */
Avi Kivity618ff152011-04-03 12:32:09 +0300657 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
658 goto bad;
659 lim = desc.d ? 0xffffffff : 0xffff;
660 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
661 goto bad;
662 }
Avi Kivity717746e2011-04-20 13:37:53 +0300663 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity618ff152011-04-03 12:32:09 +0300664 if (!(desc.type & 8)) {
665 /* data segment */
666 if (cpl > desc.dpl)
667 goto bad;
668 } else if ((desc.type & 8) && !(desc.type & 4)) {
669 /* nonconforming code segment */
670 if (cpl != desc.dpl)
671 goto bad;
672 } else if ((desc.type & 8) && (desc.type & 4)) {
673 /* conforming code segment */
674 if (cpl < desc.dpl)
675 goto bad;
676 }
677 break;
678 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300679 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300680 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300681 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
682 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300683 *linear = la;
684 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300685bad:
686 if (addr.seg == VCPU_SREG_SS)
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300687 return emulate_ss(ctxt, sel);
Avi Kivity618ff152011-04-03 12:32:09 +0300688 else
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300689 return emulate_gp(ctxt, sel);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300690}
691
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400692static int linearize(struct x86_emulate_ctxt *ctxt,
693 struct segmented_address addr,
694 unsigned size, bool write,
695 ulong *linear)
696{
697 return __linearize(ctxt, addr, size, write, false, linear);
698}
699
700
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200701static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
702 struct segmented_address addr,
703 void *data,
704 unsigned size)
705{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200706 int rc;
707 ulong linear;
708
Avi Kivity83b87952011-04-03 11:31:19 +0300709 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200710 if (rc != X86EMUL_CONTINUE)
711 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300712 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200713}
714
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900715/*
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200716 * Prefetch the remaining bytes of the instruction without crossing page
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900717 * boundary if they are not in fetch_cache yet.
718 */
Paolo Bonzini9506d572014-05-06 13:05:25 +0200719static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
Avi Kivity62266862007-11-20 13:15:52 +0200720{
Avi Kivity62266862007-11-20 13:15:52 +0200721 int rc;
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200722 unsigned size;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200723 unsigned long linear;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200724 int cur_size = ctxt->fetch.end - ctxt->fetch.data;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200725 struct segmented_address addr = { .seg = VCPU_SREG_CS,
Paolo Bonzini17052f12014-05-06 16:33:01 +0200726 .ea = ctxt->eip + cur_size };
727
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200728 size = 15UL ^ cur_size;
729 rc = __linearize(ctxt, addr, size, false, true, &linear);
730 if (unlikely(rc != X86EMUL_CONTINUE))
731 return rc;
732
733 size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear));
Paolo Bonzini5cfc7e02014-05-06 13:05:25 +0200734
735 /*
736 * One instruction can only straddle two pages,
737 * and one has been loaded at the beginning of
738 * x86_decode_insn. So, if not enough bytes
739 * still, we must have hit the 15-byte boundary.
740 */
741 if (unlikely(size < op_size))
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200742 return X86EMUL_UNHANDLEABLE;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200743 rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end,
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200744 size, &ctxt->exception);
745 if (unlikely(rc != X86EMUL_CONTINUE))
746 return rc;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200747 ctxt->fetch.end += size;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900748 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200749}
750
Paolo Bonzini9506d572014-05-06 13:05:25 +0200751static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt,
752 unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200753{
Paolo Bonzini17052f12014-05-06 16:33:01 +0200754 if (unlikely(ctxt->fetch.end - ctxt->fetch.ptr < size))
Paolo Bonzini9506d572014-05-06 13:05:25 +0200755 return __do_insn_fetch_bytes(ctxt, size);
756 else
757 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200758}
759
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900760/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900761#define insn_fetch(_type, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200762({ _type _x; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200763 \
764 rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900765 if (rc != X86EMUL_CONTINUE) \
766 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200767 ctxt->_eip += sizeof(_type); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200768 _x = *(_type __aligned(1) *) ctxt->fetch.ptr; \
769 ctxt->fetch.ptr += sizeof(_type); \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200770 _x; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900771})
772
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900773#define insn_fetch_arr(_arr, _size, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200774({ \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200775 rc = do_insn_fetch_bytes(_ctxt, _size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900776 if (rc != X86EMUL_CONTINUE) \
777 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200778 ctxt->_eip += (_size); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200779 memcpy(_arr, ctxt->fetch.ptr, _size); \
780 ctxt->fetch.ptr += (_size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900781})
782
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000783/*
784 * Given the 'reg' portion of a ModRM byte, and a register block, return a
785 * pointer into the block that addresses the relevant register.
786 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
787 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300788static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200789 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790{
791 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200792 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800793
Avi Kivity6aa8b732006-12-10 02:21:36 -0800794 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300795 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
796 else
797 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800798 return p;
799}
800
801static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200802 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800803 u16 *size, unsigned long *address, int op_bytes)
804{
805 int rc;
806
807 if (op_bytes == 2)
808 op_bytes = 3;
809 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200810 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900811 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800812 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200813 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200814 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800815 return rc;
816}
817
Avi Kivity34b77652013-01-19 19:51:56 +0200818FASTOP2(add);
819FASTOP2(or);
820FASTOP2(adc);
821FASTOP2(sbb);
822FASTOP2(and);
823FASTOP2(sub);
824FASTOP2(xor);
825FASTOP2(cmp);
826FASTOP2(test);
827
Avi Kivityb9fa4092013-02-09 11:31:48 +0200828FASTOP1SRC2(mul, mul_ex);
829FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200830FASTOP1SRC2EX(div, div_ex);
831FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +0200832
Avi Kivity34b77652013-01-19 19:51:56 +0200833FASTOP3WCL(shld);
834FASTOP3WCL(shrd);
835
836FASTOP2W(imul);
837
838FASTOP1(not);
839FASTOP1(neg);
840FASTOP1(inc);
841FASTOP1(dec);
842
843FASTOP2CL(rol);
844FASTOP2CL(ror);
845FASTOP2CL(rcl);
846FASTOP2CL(rcr);
847FASTOP2CL(shl);
848FASTOP2CL(shr);
849FASTOP2CL(sar);
850
851FASTOP2W(bsf);
852FASTOP2W(bsr);
853FASTOP2W(bt);
854FASTOP2W(bts);
855FASTOP2W(btr);
856FASTOP2W(btc);
857
Avi Kivitye47a5f52013-02-09 11:31:51 +0200858FASTOP2(xadd);
859
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200860static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300861{
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200862 u8 rc;
863 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300864
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200865 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +0200866 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200867 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
868 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300869}
870
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300871static void fetch_register_operand(struct operand *op)
872{
873 switch (op->bytes) {
874 case 1:
875 op->val = *(u8 *)op->addr.reg;
876 break;
877 case 2:
878 op->val = *(u16 *)op->addr.reg;
879 break;
880 case 4:
881 op->val = *(u32 *)op->addr.reg;
882 break;
883 case 8:
884 op->val = *(u64 *)op->addr.reg;
885 break;
886 }
887}
888
Avi Kivity1253791d2011-03-29 11:41:27 +0200889static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
890{
891 ctxt->ops->get_fpu(ctxt);
892 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200893 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
894 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
895 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
896 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
897 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
898 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
899 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
900 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200901#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200902 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
903 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
904 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
905 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
906 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
907 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
908 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
909 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200910#endif
911 default: BUG();
912 }
913 ctxt->ops->put_fpu(ctxt);
914}
915
916static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
917 int reg)
918{
919 ctxt->ops->get_fpu(ctxt);
920 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200921 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
922 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
923 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
924 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
925 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
926 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
927 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
928 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200929#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200930 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
931 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
932 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
933 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
934 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
935 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
936 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
937 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200938#endif
939 default: BUG();
940 }
941 ctxt->ops->put_fpu(ctxt);
942}
943
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300944static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
945{
946 ctxt->ops->get_fpu(ctxt);
947 switch (reg) {
948 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
949 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
950 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
951 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
952 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
953 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
954 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
955 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
956 default: BUG();
957 }
958 ctxt->ops->put_fpu(ctxt);
959}
960
961static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
962{
963 ctxt->ops->get_fpu(ctxt);
964 switch (reg) {
965 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
966 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
967 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
968 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
969 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
970 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
971 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
972 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
973 default: BUG();
974 }
975 ctxt->ops->put_fpu(ctxt);
976}
977
Gleb Natapov045a2822012-12-20 16:57:43 +0200978static int em_fninit(struct x86_emulate_ctxt *ctxt)
979{
980 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
981 return emulate_nm(ctxt);
982
983 ctxt->ops->get_fpu(ctxt);
984 asm volatile("fninit");
985 ctxt->ops->put_fpu(ctxt);
986 return X86EMUL_CONTINUE;
987}
988
989static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
990{
991 u16 fcw;
992
993 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
994 return emulate_nm(ctxt);
995
996 ctxt->ops->get_fpu(ctxt);
997 asm volatile("fnstcw %0": "+m"(fcw));
998 ctxt->ops->put_fpu(ctxt);
999
1000 /* force 2 byte destination */
1001 ctxt->dst.bytes = 2;
1002 ctxt->dst.val = fcw;
1003
1004 return X86EMUL_CONTINUE;
1005}
1006
1007static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1008{
1009 u16 fsw;
1010
1011 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1012 return emulate_nm(ctxt);
1013
1014 ctxt->ops->get_fpu(ctxt);
1015 asm volatile("fnstsw %0": "+m"(fsw));
1016 ctxt->ops->put_fpu(ctxt);
1017
1018 /* force 2 byte destination */
1019 ctxt->dst.bytes = 2;
1020 ctxt->dst.val = fsw;
1021
1022 return X86EMUL_CONTINUE;
1023}
1024
Avi Kivity1253791d2011-03-29 11:41:27 +02001025static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001026 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001027{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001028 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001029
Avi Kivity9dac77f2011-06-01 15:34:25 +03001030 if (!(ctxt->d & ModRM))
1031 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity1253791d2011-03-29 11:41:27 +02001032
Avi Kivity9dac77f2011-06-01 15:34:25 +03001033 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001034 op->type = OP_XMM;
1035 op->bytes = 16;
1036 op->addr.xmm = reg;
1037 read_sse_reg(ctxt, &op->vec_val, reg);
1038 return;
1039 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001040 if (ctxt->d & Mmx) {
1041 reg &= 7;
1042 op->type = OP_MM;
1043 op->bytes = 8;
1044 op->addr.mm = reg;
1045 return;
1046 }
Avi Kivity1253791d2011-03-29 11:41:27 +02001047
Avi Kivity3c118e22007-10-31 10:27:04 +02001048 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001049 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1050 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1051
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001052 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001053 op->orig_val = op->val;
1054}
1055
Avi Kivitya6e34072012-06-10 17:15:39 +03001056static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1057{
1058 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1059 ctxt->modrm_seg = VCPU_SREG_SS;
1060}
1061
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001062static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001063 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001064{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001065 u8 sib;
Bandan Das02357bd2014-04-16 12:46:11 -04001066 int index_reg, base_reg, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001067 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001068 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001069
Bandan Das02357bd2014-04-16 12:46:11 -04001070 ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */
1071 index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */
1072 base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001073
Bandan Das02357bd2014-04-16 12:46:11 -04001074 ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001075 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
Bandan Das02357bd2014-04-16 12:46:11 -04001076 ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001077 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001078
Nadav Amit9b88ae92014-05-25 23:05:21 +03001079 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001080 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001081 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001082 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001083 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001084 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001085 op->type = OP_XMM;
1086 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001087 op->addr.xmm = ctxt->modrm_rm;
1088 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001089 return rc;
1090 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001091 if (ctxt->d & Mmx) {
1092 op->type = OP_MM;
1093 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001094 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001095 return rc;
1096 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001097 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001098 return rc;
1099 }
1100
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001101 op->type = OP_MEM;
1102
Avi Kivity9dac77f2011-06-01 15:34:25 +03001103 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001104 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1105 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1106 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1107 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001108
1109 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001110 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001111 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001112 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001113 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001114 break;
1115 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001116 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001117 break;
1118 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001119 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001120 break;
1121 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001122 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001123 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001124 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001125 break;
1126 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001127 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001128 break;
1129 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001130 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001131 break;
1132 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001133 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001134 break;
1135 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001136 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001137 break;
1138 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001139 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001140 break;
1141 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001142 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001143 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001144 break;
1145 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001146 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001147 break;
1148 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001149 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1150 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1151 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001152 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001153 } else {
1154 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001155 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001156 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001157 index_reg |= (sib >> 3) & 7;
1158 base_reg |= sib & 7;
1159 scale = sib >> 6;
1160
Avi Kivity9dac77f2011-06-01 15:34:25 +03001161 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001162 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001163 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001164 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001165 adjust_modrm_seg(ctxt, base_reg);
1166 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001167 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001168 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001169 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001170 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001171 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001172 } else {
1173 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001174 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001175 adjust_modrm_seg(ctxt, base_reg);
1176 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001177 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001178 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001179 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001180 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001181 break;
1182 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001183 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001184 break;
1185 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001186 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001187 break;
1188 }
1189 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001190 op->addr.mem.ea = modrm_ea;
Bandan Das41061cd2014-04-16 12:46:14 -04001191 if (ctxt->ad_bytes != 8)
1192 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
1193
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001194done:
1195 return rc;
1196}
1197
1198static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001199 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001200{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001201 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001202
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001203 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001204 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001205 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001206 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001207 break;
1208 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001209 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001210 break;
1211 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001212 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001213 break;
1214 }
1215done:
1216 return rc;
1217}
1218
Avi Kivity9dac77f2011-06-01 15:34:25 +03001219static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001220{
Sheng Yang7129eec2010-09-28 16:33:32 +08001221 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001222
Avi Kivity9dac77f2011-06-01 15:34:25 +03001223 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001224 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001225
Avi Kivity9dac77f2011-06-01 15:34:25 +03001226 if (ctxt->src.bytes == 2)
1227 sv = (s16)ctxt->src.val & (s16)mask;
1228 else if (ctxt->src.bytes == 4)
1229 sv = (s32)ctxt->src.val & (s32)mask;
Nadav Amit7dec5602014-06-15 16:12:57 +03001230 else
1231 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001232
Avi Kivity9dac77f2011-06-01 15:34:25 +03001233 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001234 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001235
1236 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001237 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001238}
1239
Gleb Natapov9de41572010-04-28 19:15:22 +03001240static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001241 unsigned long addr, void *dest, unsigned size)
1242{
1243 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001244 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001245
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001246 if (mc->pos < mc->end)
1247 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001248
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001249 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001250
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001251 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1252 &ctxt->exception);
1253 if (rc != X86EMUL_CONTINUE)
1254 return rc;
1255
1256 mc->end += size;
1257
1258read_cached:
1259 memcpy(dest, mc->data + mc->pos, size);
1260 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001261 return X86EMUL_CONTINUE;
1262}
1263
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001264static int segmented_read(struct x86_emulate_ctxt *ctxt,
1265 struct segmented_address addr,
1266 void *data,
1267 unsigned size)
1268{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001269 int rc;
1270 ulong linear;
1271
Avi Kivity83b87952011-04-03 11:31:19 +03001272 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001273 if (rc != X86EMUL_CONTINUE)
1274 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001275 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001276}
1277
1278static int segmented_write(struct x86_emulate_ctxt *ctxt,
1279 struct segmented_address addr,
1280 const void *data,
1281 unsigned size)
1282{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001283 int rc;
1284 ulong linear;
1285
Avi Kivity83b87952011-04-03 11:31:19 +03001286 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001287 if (rc != X86EMUL_CONTINUE)
1288 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001289 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1290 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001291}
1292
1293static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1294 struct segmented_address addr,
1295 const void *orig_data, const void *data,
1296 unsigned size)
1297{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001298 int rc;
1299 ulong linear;
1300
Avi Kivity83b87952011-04-03 11:31:19 +03001301 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001302 if (rc != X86EMUL_CONTINUE)
1303 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001304 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1305 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001306}
1307
Gleb Natapov7b262e92010-03-18 15:20:27 +02001308static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001309 unsigned int size, unsigned short port,
1310 void *dest)
1311{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001312 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001313
1314 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001315 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001316 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001317 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001318 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001319 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1320 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Mark Rustadb55a8142014-07-25 06:27:05 -07001321 n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001322 if (n == 0)
1323 n = 1;
1324 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001325 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001326 return 0;
1327 rc->end = n * size;
1328 }
1329
Nadav Amite6e39f02014-04-18 03:35:10 +03001330 if (ctxt->rep_prefix && (ctxt->d & String) &&
1331 !(ctxt->eflags & EFLG_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001332 ctxt->dst.data = rc->data + rc->pos;
1333 ctxt->dst.type = OP_MEM_STR;
1334 ctxt->dst.count = (rc->end - rc->pos) / size;
1335 rc->pos = rc->end;
1336 } else {
1337 memcpy(dest, rc->data + rc->pos, size);
1338 rc->pos += size;
1339 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001340 return 1;
1341}
1342
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001343static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1344 u16 index, struct desc_struct *desc)
1345{
1346 struct desc_ptr dt;
1347 ulong addr;
1348
1349 ctxt->ops->get_idt(ctxt, &dt);
1350
1351 if (dt.size < index * 8 + 7)
1352 return emulate_gp(ctxt, index << 3 | 0x2);
1353
1354 addr = dt.address + index * 8;
1355 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1356 &ctxt->exception);
1357}
1358
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001359static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001360 u16 selector, struct desc_ptr *dt)
1361{
Mathias Krause0225fb52012-08-30 01:30:16 +02001362 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001363 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001364
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001365 if (selector & 1 << 2) {
1366 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001367 u16 sel;
1368
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001369 memset (dt, 0, sizeof *dt);
Nadav Amit2eedcac2014-06-02 18:34:05 +03001370 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1371 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001372 return;
1373
1374 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001375 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001376 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001377 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001378}
1379
1380/* allowed just for 8 bytes segments */
1381static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001382 u16 selector, struct desc_struct *desc,
1383 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001384{
1385 struct desc_ptr dt;
1386 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001387 ulong addr;
1388
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001389 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001390
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001391 if (dt.size < index * 8 + 7)
1392 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001393
Avi Kivitye9194642012-06-13 16:29:39 +03001394 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001395 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1396 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001397}
1398
1399/* allowed just for 8 bytes segments */
1400static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001401 u16 selector, struct desc_struct *desc)
1402{
1403 struct desc_ptr dt;
1404 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001405 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001406
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001407 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001408
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001409 if (dt.size < index * 8 + 7)
1410 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001411
1412 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001413 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1414 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001415}
1416
Gleb Natapov5601d052011-03-07 14:55:06 +02001417/* Does not support long mode */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001418static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Paolo Bonzini5045b462014-05-15 18:09:29 +02001419 u16 selector, int seg, u8 cpl, bool in_task_switch)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001420{
Avi Kivity869be992012-06-13 16:30:53 +03001421 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001422 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001423 unsigned err_vec = GP_VECTOR;
1424 u32 err_code = 0;
1425 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001426 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001427 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001428 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001429 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001430
1431 memset(&seg_desc, 0, sizeof seg_desc);
1432
Kevin Wolff8da94e2013-04-11 14:06:03 +02001433 if (ctxt->mode == X86EMUL_MODE_REAL) {
1434 /* set real mode segment descriptor (keep limit etc. for
1435 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001436 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001437 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001438 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001439 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1440 /* VM86 needs a clean new segment descriptor */
1441 set_desc_base(&seg_desc, selector << 4);
1442 set_desc_limit(&seg_desc, 0xffff);
1443 seg_desc.type = 3;
1444 seg_desc.p = 1;
1445 seg_desc.s = 1;
1446 seg_desc.dpl = 3;
1447 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001448 }
1449
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001450 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001451
1452 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1453 if ((seg == VCPU_SREG_CS
1454 || (seg == VCPU_SREG_SS
1455 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1456 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001457 && null_selector)
1458 goto exception;
1459
1460 /* TR should be in GDT only */
1461 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1462 goto exception;
1463
1464 if (null_selector) /* for NULL selector skip all following checks */
1465 goto load;
1466
Avi Kivitye9194642012-06-13 16:29:39 +03001467 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001468 if (ret != X86EMUL_CONTINUE)
1469 return ret;
1470
1471 err_code = selector & 0xfffc;
Paolo Bonzini15fc0752014-08-18 13:17:00 +02001472 err_vec = in_task_switch ? TS_VECTOR : GP_VECTOR;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001473
Guo Chaofc058682012-06-28 15:19:51 +08001474 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001475 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1476 goto exception;
1477
1478 if (!seg_desc.p) {
1479 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1480 goto exception;
1481 }
1482
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001483 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001484
1485 switch (seg) {
1486 case VCPU_SREG_SS:
1487 /*
1488 * segment is not a writable data segment or segment
1489 * selector's RPL != CPL or segment selector's RPL != CPL
1490 */
1491 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1492 goto exception;
1493 break;
1494 case VCPU_SREG_CS:
1495 if (!(seg_desc.type & 8))
1496 goto exception;
1497
1498 if (seg_desc.type & 4) {
1499 /* conforming */
1500 if (dpl > cpl)
1501 goto exception;
1502 } else {
1503 /* nonconforming */
1504 if (rpl > cpl || dpl != cpl)
1505 goto exception;
1506 }
1507 /* CS(RPL) <- CPL */
1508 selector = (selector & 0xfffc) | cpl;
1509 break;
1510 case VCPU_SREG_TR:
1511 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1512 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001513 old_desc = seg_desc;
1514 seg_desc.type |= 2; /* busy */
1515 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1516 sizeof(seg_desc), &ctxt->exception);
1517 if (ret != X86EMUL_CONTINUE)
1518 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001519 break;
1520 case VCPU_SREG_LDTR:
1521 if (seg_desc.s || seg_desc.type != 2)
1522 goto exception;
1523 break;
1524 default: /* DS, ES, FS, or GS */
1525 /*
1526 * segment is not a data or readable code segment or
1527 * ((segment is a data or nonconforming code segment)
1528 * and (both RPL and CPL > DPL))
1529 */
1530 if ((seg_desc.type & 0xa) == 0x8 ||
1531 (((seg_desc.type & 0xc) != 0xc) &&
1532 (rpl > dpl && cpl > dpl)))
1533 goto exception;
1534 break;
1535 }
1536
1537 if (seg_desc.s) {
1538 /* mark segment as accessed */
1539 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001540 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001541 if (ret != X86EMUL_CONTINUE)
1542 return ret;
Nadav Amite37a75a2014-06-02 18:34:04 +03001543 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
1544 ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3,
1545 sizeof(base3), &ctxt->exception);
1546 if (ret != X86EMUL_CONTINUE)
1547 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001548 }
1549load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001550 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001551 return X86EMUL_CONTINUE;
1552exception:
Paolo Bonzini592f0852014-08-20 10:05:08 +02001553 return emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001554}
1555
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001556static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1557 u16 selector, int seg)
1558{
1559 u8 cpl = ctxt->ops->cpl(ctxt);
Paolo Bonzini5045b462014-05-15 18:09:29 +02001560 return __load_segment_descriptor(ctxt, selector, seg, cpl, false);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001561}
1562
Wei Yongjun31be40b2010-08-17 09:17:30 +08001563static void write_register_operand(struct operand *op)
1564{
1565 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1566 switch (op->bytes) {
1567 case 1:
1568 *(u8 *)op->addr.reg = (u8)op->val;
1569 break;
1570 case 2:
1571 *(u16 *)op->addr.reg = (u16)op->val;
1572 break;
1573 case 4:
1574 *op->addr.reg = (u32)op->val;
1575 break; /* 64b: zero-extend */
1576 case 8:
1577 *op->addr.reg = op->val;
1578 break;
1579 }
1580}
1581
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001582static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001583{
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001584 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001585 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001586 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001587 break;
1588 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001589 if (ctxt->lock_prefix)
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001590 return segmented_cmpxchg(ctxt,
1591 op->addr.mem,
1592 &op->orig_val,
1593 &op->val,
1594 op->bytes);
1595 else
1596 return segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001597 op->addr.mem,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001598 &op->val,
1599 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001600 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001601 case OP_MEM_STR:
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001602 return segmented_write(ctxt,
1603 op->addr.mem,
1604 op->data,
1605 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001606 break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001607 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001608 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001609 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001610 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001611 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001612 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001613 case OP_NONE:
1614 /* no writeback */
1615 break;
1616 default:
1617 break;
1618 }
1619 return X86EMUL_CONTINUE;
1620}
1621
Avi Kivity51ddff52012-06-12 20:19:40 +03001622static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001623{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001624 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001625
Avi Kivity5ad105e2012-08-19 14:34:31 +03001626 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001627 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001628 addr.seg = VCPU_SREG_SS;
1629
Avi Kivity51ddff52012-06-12 20:19:40 +03001630 return segmented_write(ctxt, addr, data, bytes);
1631}
1632
1633static int em_push(struct x86_emulate_ctxt *ctxt)
1634{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001635 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001636 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001637 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001638}
1639
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001640static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001641 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001642{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001643 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001644 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001645
Avi Kivitydd856ef2012-08-27 23:46:17 +03001646 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001647 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001648 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001649 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001650 return rc;
1651
Avi Kivity5ad105e2012-08-19 14:34:31 +03001652 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001653 return rc;
1654}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001655
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001656static int em_pop(struct x86_emulate_ctxt *ctxt)
1657{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001658 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001659}
1660
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001661static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001662 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001663{
1664 int rc;
1665 unsigned long val, change_mask;
1666 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001667 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001668
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001669 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001670 if (rc != X86EMUL_CONTINUE)
1671 return rc;
1672
1673 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
Nadav Amit163b1352014-07-21 14:37:28 +03001674 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_AC | EFLG_ID;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001675
1676 switch(ctxt->mode) {
1677 case X86EMUL_MODE_PROT64:
1678 case X86EMUL_MODE_PROT32:
1679 case X86EMUL_MODE_PROT16:
1680 if (cpl == 0)
1681 change_mask |= EFLG_IOPL;
1682 if (cpl <= iopl)
1683 change_mask |= EFLG_IF;
1684 break;
1685 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001686 if (iopl < 3)
1687 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001688 change_mask |= EFLG_IF;
1689 break;
1690 default: /* real mode */
1691 change_mask |= (EFLG_IOPL | EFLG_IF);
1692 break;
1693 }
1694
1695 *(unsigned long *)dest =
1696 (ctxt->eflags & ~change_mask) | (val & change_mask);
1697
1698 return rc;
1699}
1700
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001701static int em_popf(struct x86_emulate_ctxt *ctxt)
1702{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001703 ctxt->dst.type = OP_REG;
1704 ctxt->dst.addr.reg = &ctxt->eflags;
1705 ctxt->dst.bytes = ctxt->op_bytes;
1706 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001707}
1708
Avi Kivity612e89f2012-06-12 20:03:23 +03001709static int em_enter(struct x86_emulate_ctxt *ctxt)
1710{
1711 int rc;
1712 unsigned frame_size = ctxt->src.val;
1713 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001714 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001715
1716 if (nesting_level)
1717 return X86EMUL_UNHANDLEABLE;
1718
Avi Kivitydd856ef2012-08-27 23:46:17 +03001719 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1720 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001721 if (rc != X86EMUL_CONTINUE)
1722 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001723 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001724 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001725 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1726 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001727 stack_mask(ctxt));
1728 return X86EMUL_CONTINUE;
1729}
1730
Avi Kivityf47cfa32012-06-07 17:49:24 +03001731static int em_leave(struct x86_emulate_ctxt *ctxt)
1732{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001733 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001734 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001735 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001736}
1737
Avi Kivity1cd196e2011-09-13 10:45:51 +03001738static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001739{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001740 int seg = ctxt->src2.val;
1741
Avi Kivity9dac77f2011-06-01 15:34:25 +03001742 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001743
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001744 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001745}
1746
Avi Kivity1cd196e2011-09-13 10:45:51 +03001747static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001748{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001749 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001750 unsigned long selector;
1751 int rc;
1752
Avi Kivity9dac77f2011-06-01 15:34:25 +03001753 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001754 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001755 return rc;
1756
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001757 if (ctxt->modrm_reg == VCPU_SREG_SS)
1758 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
1759
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001760 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001761 return rc;
1762}
1763
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001764static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001765{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001766 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001767 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001768 int reg = VCPU_REGS_RAX;
1769
1770 while (reg <= VCPU_REGS_RDI) {
1771 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001772 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001773
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001774 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001775 if (rc != X86EMUL_CONTINUE)
1776 return rc;
1777
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001778 ++reg;
1779 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001780
Wei Yongjunc37eda12010-06-15 09:03:33 +08001781 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001782}
1783
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001784static int em_pushf(struct x86_emulate_ctxt *ctxt)
1785{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001786 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001787 return em_push(ctxt);
1788}
1789
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001790static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001791{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001792 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001793 int reg = VCPU_REGS_RDI;
1794
1795 while (reg >= VCPU_REGS_RAX) {
1796 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001797 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001798 --reg;
1799 }
1800
Avi Kivitydd856ef2012-08-27 23:46:17 +03001801 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001802 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001803 break;
1804 --reg;
1805 }
1806 return rc;
1807}
1808
Avi Kivitydd856ef2012-08-27 23:46:17 +03001809static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001810{
Mathias Krause0225fb52012-08-30 01:30:16 +02001811 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001812 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001813 struct desc_ptr dt;
1814 gva_t cs_addr;
1815 gva_t eip_addr;
1816 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001817
1818 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001819 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001820 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001821 if (rc != X86EMUL_CONTINUE)
1822 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001823
1824 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1825
Avi Kivity9dac77f2011-06-01 15:34:25 +03001826 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001827 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001828 if (rc != X86EMUL_CONTINUE)
1829 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001830
Avi Kivity9dac77f2011-06-01 15:34:25 +03001831 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001832 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001833 if (rc != X86EMUL_CONTINUE)
1834 return rc;
1835
Avi Kivity4bff1e862011-04-20 13:37:53 +03001836 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001837
1838 eip_addr = dt.address + (irq << 2);
1839 cs_addr = dt.address + (irq << 2) + 2;
1840
Avi Kivity0f65dd72011-04-20 13:37:53 +03001841 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001842 if (rc != X86EMUL_CONTINUE)
1843 return rc;
1844
Avi Kivity0f65dd72011-04-20 13:37:53 +03001845 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001846 if (rc != X86EMUL_CONTINUE)
1847 return rc;
1848
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001849 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001850 if (rc != X86EMUL_CONTINUE)
1851 return rc;
1852
Avi Kivity9dac77f2011-06-01 15:34:25 +03001853 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001854
1855 return rc;
1856}
1857
Avi Kivitydd856ef2012-08-27 23:46:17 +03001858int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1859{
1860 int rc;
1861
1862 invalidate_registers(ctxt);
1863 rc = __emulate_int_real(ctxt, irq);
1864 if (rc == X86EMUL_CONTINUE)
1865 writeback_registers(ctxt);
1866 return rc;
1867}
1868
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001869static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001870{
1871 switch(ctxt->mode) {
1872 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001873 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001874 case X86EMUL_MODE_VM86:
1875 case X86EMUL_MODE_PROT16:
1876 case X86EMUL_MODE_PROT32:
1877 case X86EMUL_MODE_PROT64:
1878 default:
1879 /* Protected mode interrupts unimplemented yet */
1880 return X86EMUL_UNHANDLEABLE;
1881 }
1882}
1883
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001884static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001885{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001886 int rc = X86EMUL_CONTINUE;
1887 unsigned long temp_eip = 0;
1888 unsigned long temp_eflags = 0;
1889 unsigned long cs = 0;
1890 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1891 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1892 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1893 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1894
1895 /* TODO: Add stack limit check */
1896
Avi Kivity9dac77f2011-06-01 15:34:25 +03001897 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001898
1899 if (rc != X86EMUL_CONTINUE)
1900 return rc;
1901
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001902 if (temp_eip & ~0xffff)
1903 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001904
Avi Kivity9dac77f2011-06-01 15:34:25 +03001905 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001906
1907 if (rc != X86EMUL_CONTINUE)
1908 return rc;
1909
Avi Kivity9dac77f2011-06-01 15:34:25 +03001910 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001911
1912 if (rc != X86EMUL_CONTINUE)
1913 return rc;
1914
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001915 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001916
1917 if (rc != X86EMUL_CONTINUE)
1918 return rc;
1919
Avi Kivity9dac77f2011-06-01 15:34:25 +03001920 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001921
1922
Avi Kivity9dac77f2011-06-01 15:34:25 +03001923 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001924 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001925 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001926 ctxt->eflags &= ~0xffff;
1927 ctxt->eflags |= temp_eflags;
1928 }
1929
1930 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1931 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1932
1933 return rc;
1934}
1935
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001936static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001937{
1938 switch(ctxt->mode) {
1939 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001940 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001941 case X86EMUL_MODE_VM86:
1942 case X86EMUL_MODE_PROT16:
1943 case X86EMUL_MODE_PROT32:
1944 case X86EMUL_MODE_PROT64:
1945 default:
1946 /* iret from protected mode unimplemented yet */
1947 return X86EMUL_UNHANDLEABLE;
1948 }
1949}
1950
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001951static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1952{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001953 int rc;
1954 unsigned short sel;
1955
Avi Kivity9dac77f2011-06-01 15:34:25 +03001956 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001957
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001958 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001959 if (rc != X86EMUL_CONTINUE)
1960 return rc;
1961
Avi Kivity9dac77f2011-06-01 15:34:25 +03001962 ctxt->_eip = 0;
1963 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001964 return X86EMUL_CONTINUE;
1965}
1966
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001967static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001968{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001969 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001970
Avi Kivity9dac77f2011-06-01 15:34:25 +03001971 switch (ctxt->modrm_reg) {
Mohammed Gamald19292e2008-09-08 21:47:19 +03001972 case 2: /* call near abs */ {
1973 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001974 old_eip = ctxt->_eip;
1975 ctxt->_eip = ctxt->src.val;
1976 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001977 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001978 break;
1979 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001980 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001981 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001982 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001983 case 5: /* jmp far */
1984 rc = em_jmp_far(ctxt);
1985 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001986 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001987 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001988 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001989 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001990 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001991}
1992
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09001993static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001994{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001995 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001996
Nadav Amitaaa05f22014-06-02 18:34:10 +03001997 if (ctxt->dst.bytes == 16)
1998 return X86EMUL_UNHANDLEABLE;
1999
Avi Kivitydd856ef2012-08-27 23:46:17 +03002000 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2001 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2002 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2003 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002004 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002005 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002006 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2007 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002008
Laurent Vivier05f086f2007-09-24 11:10:55 +02002009 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002010 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002011 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002012}
2013
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002014static int em_ret(struct x86_emulate_ctxt *ctxt)
2015{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002016 ctxt->dst.type = OP_REG;
2017 ctxt->dst.addr.reg = &ctxt->_eip;
2018 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002019 return em_pop(ctxt);
2020}
2021
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002022static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002023{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002024 int rc;
2025 unsigned long cs;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002026 int cpl = ctxt->ops->cpl(ctxt);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002027
Avi Kivity9dac77f2011-06-01 15:34:25 +03002028 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002029 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002030 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002031 if (ctxt->op_bytes == 4)
2032 ctxt->_eip = (u32)ctxt->_eip;
2033 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002034 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002035 return rc;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002036 /* Outer-privilege level return is not implemented */
2037 if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2038 return X86EMUL_UNHANDLEABLE;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002039 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002040 return rc;
2041}
2042
Bruce Rogers32611072013-09-09 09:40:20 -06002043static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2044{
2045 int rc;
2046
2047 rc = em_ret_far(ctxt);
2048 if (rc != X86EMUL_CONTINUE)
2049 return rc;
2050 rsp_increment(ctxt, ctxt->src.val);
2051 return X86EMUL_CONTINUE;
2052}
2053
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002054static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2055{
2056 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002057 ctxt->dst.orig_val = ctxt->dst.val;
2058 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002059 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002060 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002061 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002062
2063 if (ctxt->eflags & EFLG_ZF) {
2064 /* Success: write back to memory. */
2065 ctxt->dst.val = ctxt->src.orig_val;
2066 } else {
2067 /* Failure: write the value we saw to EAX. */
2068 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002069 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Nadav Amit37c564f2014-06-02 18:34:07 +03002070 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002071 }
2072 return X86EMUL_CONTINUE;
2073}
2074
Avi Kivityd4b43252011-09-13 10:45:50 +03002075static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002076{
Avi Kivityd4b43252011-09-13 10:45:50 +03002077 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002078 unsigned short sel;
2079 int rc;
2080
Avi Kivity9dac77f2011-06-01 15:34:25 +03002081 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002082
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002083 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002084 if (rc != X86EMUL_CONTINUE)
2085 return rc;
2086
Avi Kivity9dac77f2011-06-01 15:34:25 +03002087 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002088 return rc;
2089}
2090
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002091static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002092setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002093 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002094{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002095 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002096 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002097 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002098 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002099 cs->type = 0x0b; /* Read, Execute, Accessed */
2100 cs->s = 1;
2101 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002102 cs->p = 1;
2103 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002104 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002105
Gleb Natapov79168fd2010-04-28 19:15:30 +03002106 set_desc_base(ss, 0); /* flat segment */
2107 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002108 ss->g = 1; /* 4kb granularity */
2109 ss->s = 1;
2110 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002111 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002112 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002113 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002114 ss->l = 0;
2115 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002116}
2117
Avi Kivity1a18a692012-02-01 12:23:21 +02002118static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2119{
2120 u32 eax, ebx, ecx, edx;
2121
2122 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002123 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2124 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002125 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2126 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2127}
2128
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002129static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2130{
Mathias Krause0225fb52012-08-30 01:30:16 +02002131 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002132 u32 eax, ebx, ecx, edx;
2133
2134 /*
2135 * syscall should always be enabled in longmode - so only become
2136 * vendor specific (cpuid) if other modes are active...
2137 */
2138 if (ctxt->mode == X86EMUL_MODE_PROT64)
2139 return true;
2140
2141 eax = 0x00000000;
2142 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002143 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2144 /*
2145 * Intel ("GenuineIntel")
2146 * remark: Intel CPUs only support "syscall" in 64bit
2147 * longmode. Also an 64bit guest with a
2148 * 32bit compat-app running will #UD !! While this
2149 * behaviour can be fixed (by emulating) into AMD
2150 * response - CPUs of AMD can't behave like Intel.
2151 */
2152 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2153 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2154 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2155 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002156
Avi Kivity0017f932012-06-07 14:10:16 +03002157 /* AMD ("AuthenticAMD") */
2158 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2159 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2160 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2161 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002162
Avi Kivity0017f932012-06-07 14:10:16 +03002163 /* AMD ("AMDisbetter!") */
2164 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2165 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2166 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2167 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002168
2169 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2170 return false;
2171}
2172
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002173static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002174{
Mathias Krause0225fb52012-08-30 01:30:16 +02002175 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002176 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002177 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002178 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002179 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002180
2181 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002182 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002183 ctxt->mode == X86EMUL_MODE_VM86)
2184 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002185
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002186 if (!(em_syscall_is_enabled(ctxt)))
2187 return emulate_ud(ctxt);
2188
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002189 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002190 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002191
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002192 if (!(efer & EFER_SCE))
2193 return emulate_ud(ctxt);
2194
Avi Kivity717746e2011-04-20 13:37:53 +03002195 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002196 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002197 cs_sel = (u16)(msr_data & 0xfffc);
2198 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002199
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002200 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002201 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002202 cs.l = 1;
2203 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002204 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2205 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002206
Avi Kivitydd856ef2012-08-27 23:46:17 +03002207 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002208 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002209#ifdef CONFIG_X86_64
Nadav Amit6c6cb692014-07-21 14:37:30 +03002210 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002211
Avi Kivity717746e2011-04-20 13:37:53 +03002212 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002213 ctxt->mode == X86EMUL_MODE_PROT64 ?
2214 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002215 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002216
Avi Kivity717746e2011-04-20 13:37:53 +03002217 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Nadav Amit6c6cb692014-07-21 14:37:30 +03002218 ctxt->eflags &= ~msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002219#endif
2220 } else {
2221 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002222 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002223 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002224
Nadav Amit6c6cb692014-07-21 14:37:30 +03002225 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002226 }
2227
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002228 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002229}
2230
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002231static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002232{
Mathias Krause0225fb52012-08-30 01:30:16 +02002233 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002234 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002235 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002236 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002237 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002238
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002239 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002240 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002241 if (ctxt->mode == X86EMUL_MODE_REAL)
2242 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002243
Avi Kivity1a18a692012-02-01 12:23:21 +02002244 /*
2245 * Not recognized on AMD in compat mode (but is recognized in legacy
2246 * mode).
2247 */
2248 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2249 && !vendor_intel(ctxt))
2250 return emulate_ud(ctxt);
2251
Andre Przywara8c604352009-06-18 12:56:01 +02002252 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2253 * Therefore, we inject an #UD.
2254 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002255 if (ctxt->mode == X86EMUL_MODE_PROT64)
2256 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002257
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002258 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002259
Avi Kivity717746e2011-04-20 13:37:53 +03002260 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002261 switch (ctxt->mode) {
2262 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002263 if ((msr_data & 0xfffc) == 0x0)
2264 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002265 break;
2266 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002267 if (msr_data == 0x0)
2268 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002269 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002270 default:
2271 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002272 }
2273
Nadav Amit6c6cb692014-07-21 14:37:30 +03002274 ctxt->eflags &= ~(EFLG_VM | EFLG_IF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002275 cs_sel = (u16)msr_data;
2276 cs_sel &= ~SELECTOR_RPL_MASK;
2277 ss_sel = cs_sel + 8;
2278 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002279 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002280 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002281 cs.l = 1;
2282 }
2283
Avi Kivity1aa36612011-04-27 13:20:30 +03002284 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2285 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002286
Avi Kivity717746e2011-04-20 13:37:53 +03002287 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002288 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002289
Avi Kivity717746e2011-04-20 13:37:53 +03002290 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002291 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002292
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002293 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002294}
2295
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002296static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002297{
Mathias Krause0225fb52012-08-30 01:30:16 +02002298 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002299 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002300 u64 msr_data;
2301 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002302 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002303
Gleb Natapova0044752010-02-10 14:21:31 +02002304 /* inject #GP if in real mode or Virtual 8086 mode */
2305 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002306 ctxt->mode == X86EMUL_MODE_VM86)
2307 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002308
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002309 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002310
Avi Kivity9dac77f2011-06-01 15:34:25 +03002311 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002312 usermode = X86EMUL_MODE_PROT64;
2313 else
2314 usermode = X86EMUL_MODE_PROT32;
2315
2316 cs.dpl = 3;
2317 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002318 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002319 switch (usermode) {
2320 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002321 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002322 if ((msr_data & 0xfffc) == 0x0)
2323 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002324 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002325 break;
2326 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002327 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002328 if (msr_data == 0x0)
2329 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002330 ss_sel = cs_sel + 8;
2331 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002332 cs.l = 1;
2333 break;
2334 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002335 cs_sel |= SELECTOR_RPL_MASK;
2336 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002337
Avi Kivity1aa36612011-04-27 13:20:30 +03002338 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2339 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002340
Avi Kivitydd856ef2012-08-27 23:46:17 +03002341 ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX);
2342 *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX);
Andre Przywara4668f052009-06-18 12:56:02 +02002343
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002344 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002345}
2346
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002347static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002348{
2349 int iopl;
2350 if (ctxt->mode == X86EMUL_MODE_REAL)
2351 return false;
2352 if (ctxt->mode == X86EMUL_MODE_VM86)
2353 return true;
2354 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002355 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002356}
2357
2358static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002359 u16 port, u16 len)
2360{
Mathias Krause0225fb52012-08-30 01:30:16 +02002361 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002362 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002363 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002364 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002365 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002366 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002367 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002368
Avi Kivity1aa36612011-04-27 13:20:30 +03002369 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002370 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002371 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002372 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002373 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002374 base = get_desc_base(&tr_seg);
2375#ifdef CONFIG_X86_64
2376 base |= ((u64)base3) << 32;
2377#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002378 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002379 if (r != X86EMUL_CONTINUE)
2380 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002381 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002382 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002383 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002384 if (r != X86EMUL_CONTINUE)
2385 return false;
2386 if ((perm >> bit_idx) & mask)
2387 return false;
2388 return true;
2389}
2390
2391static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002392 u16 port, u16 len)
2393{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002394 if (ctxt->perm_ok)
2395 return true;
2396
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002397 if (emulator_bad_iopl(ctxt))
2398 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002399 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002400
2401 ctxt->perm_ok = true;
2402
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002403 return true;
2404}
2405
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002406static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002407 struct tss_segment_16 *tss)
2408{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002409 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002410 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002411 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2412 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2413 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2414 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2415 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2416 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2417 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2418 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002419
Avi Kivity1aa36612011-04-27 13:20:30 +03002420 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2421 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2422 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2423 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2424 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002425}
2426
2427static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002428 struct tss_segment_16 *tss)
2429{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002430 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002431 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002432
Avi Kivity9dac77f2011-06-01 15:34:25 +03002433 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002434 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002435 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2436 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2437 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2438 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2439 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2440 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2441 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2442 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002443
2444 /*
2445 * SDM says that segment selectors are loaded before segment
2446 * descriptors
2447 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002448 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2449 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2450 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2451 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2452 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002453
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002454 cpl = tss->cs & 3;
2455
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002456 /*
Guo Chaofc058682012-06-28 15:19:51 +08002457 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002458 * it is handled in a context of new task
2459 */
Paolo Bonzini5045b462014-05-15 18:09:29 +02002460 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002461 if (ret != X86EMUL_CONTINUE)
2462 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002463 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002464 if (ret != X86EMUL_CONTINUE)
2465 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002466 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002467 if (ret != X86EMUL_CONTINUE)
2468 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002469 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002470 if (ret != X86EMUL_CONTINUE)
2471 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002472 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002473 if (ret != X86EMUL_CONTINUE)
2474 return ret;
2475
2476 return X86EMUL_CONTINUE;
2477}
2478
2479static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002480 u16 tss_selector, u16 old_tss_sel,
2481 ulong old_tss_base, struct desc_struct *new_desc)
2482{
Mathias Krause0225fb52012-08-30 01:30:16 +02002483 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002484 struct tss_segment_16 tss_seg;
2485 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002486 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002487
Avi Kivity0f65dd72011-04-20 13:37:53 +03002488 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002489 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002490 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002491 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002492 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002493
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002494 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002495
Avi Kivity0f65dd72011-04-20 13:37:53 +03002496 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002497 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002498 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002499 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002500 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002501
Avi Kivity0f65dd72011-04-20 13:37:53 +03002502 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002503 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002504 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002505 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002506 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002507
2508 if (old_tss_sel != 0xffff) {
2509 tss_seg.prev_task_link = old_tss_sel;
2510
Avi Kivity0f65dd72011-04-20 13:37:53 +03002511 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002512 &tss_seg.prev_task_link,
2513 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002514 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002515 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002516 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002517 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002518 }
2519
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002520 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002521}
2522
2523static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002524 struct tss_segment_32 *tss)
2525{
Nadav Amit5c7411e2014-04-07 18:37:47 +03002526 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002527 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002528 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002529 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2530 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2531 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2532 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2533 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2534 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2535 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2536 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002537
Avi Kivity1aa36612011-04-27 13:20:30 +03002538 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2539 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2540 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2541 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2542 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2543 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002544}
2545
2546static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002547 struct tss_segment_32 *tss)
2548{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002549 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002550 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002551
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002552 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002553 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002554 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002555 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002556
2557 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002558 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2559 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2560 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2561 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2562 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2563 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2564 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2565 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002566
2567 /*
2568 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002569 * descriptors. This is important because CPL checks will
2570 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002571 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002572 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2573 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2574 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2575 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2576 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2577 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2578 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002579
2580 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002581 * If we're switching between Protected Mode and VM86, we need to make
2582 * sure to update the mode before loading the segment descriptors so
2583 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01002584 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002585 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002586 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002587 cpl = 3;
2588 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01002589 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002590 cpl = tss->cs & 3;
2591 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01002592
2593 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002594 * Now load segment descriptors. If fault happenes at this stage
2595 * it is handled in a context of new task
2596 */
Paolo Bonzini5045b462014-05-15 18:09:29 +02002597 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002598 if (ret != X86EMUL_CONTINUE)
2599 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002600 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002601 if (ret != X86EMUL_CONTINUE)
2602 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002603 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002604 if (ret != X86EMUL_CONTINUE)
2605 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002606 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002607 if (ret != X86EMUL_CONTINUE)
2608 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002609 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002610 if (ret != X86EMUL_CONTINUE)
2611 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002612 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002613 if (ret != X86EMUL_CONTINUE)
2614 return ret;
Paolo Bonzini5045b462014-05-15 18:09:29 +02002615 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002616 if (ret != X86EMUL_CONTINUE)
2617 return ret;
2618
2619 return X86EMUL_CONTINUE;
2620}
2621
2622static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002623 u16 tss_selector, u16 old_tss_sel,
2624 ulong old_tss_base, struct desc_struct *new_desc)
2625{
Mathias Krause0225fb52012-08-30 01:30:16 +02002626 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002627 struct tss_segment_32 tss_seg;
2628 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002629 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03002630 u32 eip_offset = offsetof(struct tss_segment_32, eip);
2631 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002632
Avi Kivity0f65dd72011-04-20 13:37:53 +03002633 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002634 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002635 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002636 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002637 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002638
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002639 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002640
Nadav Amit5c7411e2014-04-07 18:37:47 +03002641 /* Only GP registers and segment selectors are saved */
2642 ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
2643 ldt_sel_offset - eip_offset, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002644 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002645 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002646 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002647
Avi Kivity0f65dd72011-04-20 13:37:53 +03002648 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002649 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002650 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002651 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002652 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002653
2654 if (old_tss_sel != 0xffff) {
2655 tss_seg.prev_task_link = old_tss_sel;
2656
Avi Kivity0f65dd72011-04-20 13:37:53 +03002657 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002658 &tss_seg.prev_task_link,
2659 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002660 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002661 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002662 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002663 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002664 }
2665
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002666 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002667}
2668
2669static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002670 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002671 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002672{
Mathias Krause0225fb52012-08-30 01:30:16 +02002673 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002674 struct desc_struct curr_tss_desc, next_tss_desc;
2675 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002676 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002677 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002678 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002679 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002680 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002681
2682 /* FIXME: old_tss_base == ~0 ? */
2683
Avi Kivitye9194642012-06-13 16:29:39 +03002684 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002685 if (ret != X86EMUL_CONTINUE)
2686 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002687 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002688 if (ret != X86EMUL_CONTINUE)
2689 return ret;
2690
2691 /* FIXME: check that next_tss_desc is tss */
2692
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002693 /*
2694 * Check privileges. The three cases are task switch caused by...
2695 *
2696 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2697 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002698 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002699 */
2700 if (reason == TASK_SWITCH_GATE) {
2701 if (idt_index != -1) {
2702 /* Software interrupts */
2703 struct desc_struct task_gate_desc;
2704 int dpl;
2705
2706 ret = read_interrupt_descriptor(ctxt, idt_index,
2707 &task_gate_desc);
2708 if (ret != X86EMUL_CONTINUE)
2709 return ret;
2710
2711 dpl = task_gate_desc.dpl;
2712 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2713 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2714 }
2715 } else if (reason != TASK_SWITCH_IRET) {
2716 int dpl = next_tss_desc.dpl;
2717 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2718 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002719 }
2720
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002721
Gleb Natapovceffb452010-03-18 15:20:19 +02002722 desc_limit = desc_limit_scaled(&next_tss_desc);
2723 if (!next_tss_desc.p ||
2724 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2725 desc_limit < 0x2b)) {
Paolo Bonzini592f0852014-08-20 10:05:08 +02002726 return emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002727 }
2728
2729 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2730 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002731 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002732 }
2733
2734 if (reason == TASK_SWITCH_IRET)
2735 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2736
2737 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002738 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002739 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2740 old_tss_sel = 0xffff;
2741
2742 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002743 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002744 old_tss_base, &next_tss_desc);
2745 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002746 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002747 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002748 if (ret != X86EMUL_CONTINUE)
2749 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002750
2751 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2752 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2753
2754 if (reason != TASK_SWITCH_IRET) {
2755 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002756 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002757 }
2758
Avi Kivity717746e2011-04-20 13:37:53 +03002759 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002760 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002761
Jan Kiszkae269fb22010-04-14 15:51:09 +02002762 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002763 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2764 ctxt->lock_prefix = 0;
2765 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002766 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002767 }
2768
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002769 return ret;
2770}
2771
2772int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002773 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002774 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002775{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002776 int rc;
2777
Avi Kivitydd856ef2012-08-27 23:46:17 +03002778 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002779 ctxt->_eip = ctxt->eip;
2780 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002781
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002782 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002783 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002784
Avi Kivitydd856ef2012-08-27 23:46:17 +03002785 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002786 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002787 writeback_registers(ctxt);
2788 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002789
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002790 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002791}
2792
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002793static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2794 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002795{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002796 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002797
Avi Kivitydd856ef2012-08-27 23:46:17 +03002798 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2799 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002800}
2801
Avi Kivity7af04fc2010-08-18 14:16:35 +03002802static int em_das(struct x86_emulate_ctxt *ctxt)
2803{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002804 u8 al, old_al;
2805 bool af, cf, old_cf;
2806
2807 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002808 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002809
2810 old_al = al;
2811 old_cf = cf;
2812 cf = false;
2813 af = ctxt->eflags & X86_EFLAGS_AF;
2814 if ((al & 0x0f) > 9 || af) {
2815 al -= 6;
2816 cf = old_cf | (al >= 250);
2817 af = true;
2818 } else {
2819 af = false;
2820 }
2821 if (old_al > 0x99 || old_cf) {
2822 al -= 0x60;
2823 cf = true;
2824 }
2825
Avi Kivity9dac77f2011-06-01 15:34:25 +03002826 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002827 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002828 ctxt->src.type = OP_IMM;
2829 ctxt->src.val = 0;
2830 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02002831 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03002832 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2833 if (cf)
2834 ctxt->eflags |= X86_EFLAGS_CF;
2835 if (af)
2836 ctxt->eflags |= X86_EFLAGS_AF;
2837 return X86EMUL_CONTINUE;
2838}
2839
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02002840static int em_aam(struct x86_emulate_ctxt *ctxt)
2841{
2842 u8 al, ah;
2843
2844 if (ctxt->src.val == 0)
2845 return emulate_de(ctxt);
2846
2847 al = ctxt->dst.val & 0xff;
2848 ah = al / ctxt->src.val;
2849 al %= ctxt->src.val;
2850
2851 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
2852
2853 /* Set PF, ZF, SF */
2854 ctxt->src.type = OP_IMM;
2855 ctxt->src.val = 0;
2856 ctxt->src.bytes = 1;
2857 fastop(ctxt, em_or);
2858
2859 return X86EMUL_CONTINUE;
2860}
2861
Gleb Natapov7f662272012-12-10 11:42:30 +02002862static int em_aad(struct x86_emulate_ctxt *ctxt)
2863{
2864 u8 al = ctxt->dst.val & 0xff;
2865 u8 ah = (ctxt->dst.val >> 8) & 0xff;
2866
2867 al = (al + (ah * ctxt->src.val)) & 0xff;
2868
2869 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
2870
Gleb Natapovf583c292013-02-13 17:50:39 +02002871 /* Set PF, ZF, SF */
2872 ctxt->src.type = OP_IMM;
2873 ctxt->src.val = 0;
2874 ctxt->src.bytes = 1;
2875 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02002876
2877 return X86EMUL_CONTINUE;
2878}
2879
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002880static int em_call(struct x86_emulate_ctxt *ctxt)
2881{
2882 long rel = ctxt->src.val;
2883
2884 ctxt->src.val = (unsigned long)ctxt->_eip;
2885 jmp_rel(ctxt, rel);
2886 return em_push(ctxt);
2887}
2888
Avi Kivity0ef753b2010-08-18 14:51:45 +03002889static int em_call_far(struct x86_emulate_ctxt *ctxt)
2890{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002891 u16 sel, old_cs;
2892 ulong old_eip;
2893 int rc;
2894
Avi Kivity1aa36612011-04-27 13:20:30 +03002895 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002896 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002897
Avi Kivity9dac77f2011-06-01 15:34:25 +03002898 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002899 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002900 return X86EMUL_CONTINUE;
2901
Avi Kivity9dac77f2011-06-01 15:34:25 +03002902 ctxt->_eip = 0;
2903 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002904
Avi Kivity9dac77f2011-06-01 15:34:25 +03002905 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002906 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002907 if (rc != X86EMUL_CONTINUE)
2908 return rc;
2909
Avi Kivity9dac77f2011-06-01 15:34:25 +03002910 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002911 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002912}
2913
Avi Kivity40ece7c2010-08-18 15:12:09 +03002914static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2915{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002916 int rc;
2917
Avi Kivity9dac77f2011-06-01 15:34:25 +03002918 ctxt->dst.type = OP_REG;
2919 ctxt->dst.addr.reg = &ctxt->_eip;
2920 ctxt->dst.bytes = ctxt->op_bytes;
2921 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002922 if (rc != X86EMUL_CONTINUE)
2923 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03002924 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002925 return X86EMUL_CONTINUE;
2926}
2927
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002928static int em_xchg(struct x86_emulate_ctxt *ctxt)
2929{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002930 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002931 ctxt->src.val = ctxt->dst.val;
2932 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002933
2934 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002935 ctxt->dst.val = ctxt->src.orig_val;
2936 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002937 return X86EMUL_CONTINUE;
2938}
2939
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002940static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2941{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002942 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02002943 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002944}
2945
Avi Kivity61429142010-08-19 15:13:00 +03002946static int em_cwd(struct x86_emulate_ctxt *ctxt)
2947{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002948 ctxt->dst.type = OP_REG;
2949 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002950 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002951 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002952
2953 return X86EMUL_CONTINUE;
2954}
2955
Avi Kivity48bb5d32010-08-18 18:54:34 +03002956static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2957{
Avi Kivity48bb5d32010-08-18 18:54:34 +03002958 u64 tsc = 0;
2959
Avi Kivity717746e2011-04-20 13:37:53 +03002960 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002961 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
2962 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03002963 return X86EMUL_CONTINUE;
2964}
2965
Avi Kivity222d21a2011-11-10 14:57:30 +02002966static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
2967{
2968 u64 pmc;
2969
Avi Kivitydd856ef2012-08-27 23:46:17 +03002970 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02002971 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002972 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
2973 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02002974 return X86EMUL_CONTINUE;
2975}
2976
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002977static int em_mov(struct x86_emulate_ctxt *ctxt)
2978{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01002979 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002980 return X86EMUL_CONTINUE;
2981}
2982
Borislav Petkov84cffe42013-10-29 12:54:56 +01002983#define FFL(x) bit(X86_FEATURE_##x)
2984
2985static int em_movbe(struct x86_emulate_ctxt *ctxt)
2986{
2987 u32 ebx, ecx, edx, eax = 1;
2988 u16 tmp;
2989
2990 /*
2991 * Check MOVBE is set in the guest-visible CPUID leaf.
2992 */
2993 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2994 if (!(ecx & FFL(MOVBE)))
2995 return emulate_ud(ctxt);
2996
2997 switch (ctxt->op_bytes) {
2998 case 2:
2999 /*
3000 * From MOVBE definition: "...When the operand size is 16 bits,
3001 * the upper word of the destination register remains unchanged
3002 * ..."
3003 *
3004 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3005 * rules so we have to do the operation almost per hand.
3006 */
3007 tmp = (u16)ctxt->src.val;
3008 ctxt->dst.val &= ~0xffffUL;
3009 ctxt->dst.val |= (unsigned long)swab16(tmp);
3010 break;
3011 case 4:
3012 ctxt->dst.val = swab32((u32)ctxt->src.val);
3013 break;
3014 case 8:
3015 ctxt->dst.val = swab64(ctxt->src.val);
3016 break;
3017 default:
Paolo Bonzini592f0852014-08-20 10:05:08 +02003018 BUG();
Borislav Petkov84cffe42013-10-29 12:54:56 +01003019 }
3020 return X86EMUL_CONTINUE;
3021}
3022
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003023static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3024{
3025 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3026 return emulate_gp(ctxt, 0);
3027
3028 /* Disable writeback. */
3029 ctxt->dst.type = OP_NONE;
3030 return X86EMUL_CONTINUE;
3031}
3032
3033static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3034{
3035 unsigned long val;
3036
3037 if (ctxt->mode == X86EMUL_MODE_PROT64)
3038 val = ctxt->src.val & ~0ULL;
3039 else
3040 val = ctxt->src.val & ~0U;
3041
3042 /* #UD condition is already handled. */
3043 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3044 return emulate_gp(ctxt, 0);
3045
3046 /* Disable writeback. */
3047 ctxt->dst.type = OP_NONE;
3048 return X86EMUL_CONTINUE;
3049}
3050
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003051static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3052{
3053 u64 msr_data;
3054
Avi Kivitydd856ef2012-08-27 23:46:17 +03003055 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3056 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3057 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003058 return emulate_gp(ctxt, 0);
3059
3060 return X86EMUL_CONTINUE;
3061}
3062
3063static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3064{
3065 u64 msr_data;
3066
Avi Kivitydd856ef2012-08-27 23:46:17 +03003067 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003068 return emulate_gp(ctxt, 0);
3069
Avi Kivitydd856ef2012-08-27 23:46:17 +03003070 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3071 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003072 return X86EMUL_CONTINUE;
3073}
3074
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003075static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3076{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003077 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003078 return emulate_ud(ctxt);
3079
Avi Kivity9dac77f2011-06-01 15:34:25 +03003080 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003081 return X86EMUL_CONTINUE;
3082}
3083
3084static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3085{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003086 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003087
Avi Kivity9dac77f2011-06-01 15:34:25 +03003088 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003089 return emulate_ud(ctxt);
3090
Avi Kivity9dac77f2011-06-01 15:34:25 +03003091 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003092 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3093
3094 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003095 ctxt->dst.type = OP_NONE;
3096 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003097}
3098
Avi Kivitya14e5792012-06-13 12:28:33 +03003099static int em_lldt(struct x86_emulate_ctxt *ctxt)
3100{
3101 u16 sel = ctxt->src.val;
3102
3103 /* Disable writeback. */
3104 ctxt->dst.type = OP_NONE;
3105 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3106}
3107
Avi Kivity80890002012-06-13 16:33:29 +03003108static int em_ltr(struct x86_emulate_ctxt *ctxt)
3109{
3110 u16 sel = ctxt->src.val;
3111
3112 /* Disable writeback. */
3113 ctxt->dst.type = OP_NONE;
3114 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3115}
3116
Avi Kivity38503912011-03-31 18:48:09 +02003117static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3118{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003119 int rc;
3120 ulong linear;
3121
Avi Kivity9dac77f2011-06-01 15:34:25 +03003122 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003123 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003124 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003125 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003126 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003127 return X86EMUL_CONTINUE;
3128}
3129
Avi Kivity2d04a052011-04-20 15:32:49 +03003130static int em_clts(struct x86_emulate_ctxt *ctxt)
3131{
3132 ulong cr0;
3133
3134 cr0 = ctxt->ops->get_cr(ctxt, 0);
3135 cr0 &= ~X86_CR0_TS;
3136 ctxt->ops->set_cr(ctxt, 0, cr0);
3137 return X86EMUL_CONTINUE;
3138}
3139
Avi Kivity26d05cc2011-04-21 12:07:59 +03003140static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3141{
Nadav Amit0f54a322014-08-29 11:26:55 +03003142 int rc = ctxt->ops->fix_hypercall(ctxt);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003143
Avi Kivity26d05cc2011-04-21 12:07:59 +03003144 if (rc != X86EMUL_CONTINUE)
3145 return rc;
3146
3147 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003148 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003149 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003150 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003151 return X86EMUL_CONTINUE;
3152}
3153
Avi Kivity96051572012-06-10 17:21:18 +03003154static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3155 void (*get)(struct x86_emulate_ctxt *ctxt,
3156 struct desc_ptr *ptr))
3157{
3158 struct desc_ptr desc_ptr;
3159
3160 if (ctxt->mode == X86EMUL_MODE_PROT64)
3161 ctxt->op_bytes = 8;
3162 get(ctxt, &desc_ptr);
3163 if (ctxt->op_bytes == 2) {
3164 ctxt->op_bytes = 4;
3165 desc_ptr.address &= 0x00ffffff;
3166 }
3167 /* Disable writeback. */
3168 ctxt->dst.type = OP_NONE;
3169 return segmented_write(ctxt, ctxt->dst.addr.mem,
3170 &desc_ptr, 2 + ctxt->op_bytes);
3171}
3172
3173static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3174{
3175 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3176}
3177
3178static int em_sidt(struct x86_emulate_ctxt *ctxt)
3179{
3180 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3181}
3182
Avi Kivity26d05cc2011-04-21 12:07:59 +03003183static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3184{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003185 struct desc_ptr desc_ptr;
3186 int rc;
3187
Avi Kivity510425f2012-06-07 17:04:36 +03003188 if (ctxt->mode == X86EMUL_MODE_PROT64)
3189 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003190 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003191 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003192 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003193 if (rc != X86EMUL_CONTINUE)
3194 return rc;
3195 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3196 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003197 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003198 return X86EMUL_CONTINUE;
3199}
3200
Avi Kivity5ef39c72011-04-21 12:21:50 +03003201static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003202{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003203 int rc;
3204
Avi Kivity5ef39c72011-04-21 12:21:50 +03003205 rc = ctxt->ops->fix_hypercall(ctxt);
3206
Avi Kivity26d05cc2011-04-21 12:07:59 +03003207 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003208 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003209 return rc;
3210}
3211
3212static int em_lidt(struct x86_emulate_ctxt *ctxt)
3213{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003214 struct desc_ptr desc_ptr;
3215 int rc;
3216
Avi Kivity510425f2012-06-07 17:04:36 +03003217 if (ctxt->mode == X86EMUL_MODE_PROT64)
3218 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003219 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003220 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003221 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003222 if (rc != X86EMUL_CONTINUE)
3223 return rc;
3224 ctxt->ops->set_idt(ctxt, &desc_ptr);
3225 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003226 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003227 return X86EMUL_CONTINUE;
3228}
3229
3230static int em_smsw(struct x86_emulate_ctxt *ctxt)
3231{
Nadav Amit32e94d02014-06-02 18:34:11 +03003232 if (ctxt->dst.type == OP_MEM)
3233 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003234 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003235 return X86EMUL_CONTINUE;
3236}
3237
3238static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3239{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003240 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003241 | (ctxt->src.val & 0x0f));
3242 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003243 return X86EMUL_CONTINUE;
3244}
3245
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003246static int em_loop(struct x86_emulate_ctxt *ctxt)
3247{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003248 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3249 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003250 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3251 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003252
3253 return X86EMUL_CONTINUE;
3254}
3255
3256static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3257{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003258 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003259 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003260
3261 return X86EMUL_CONTINUE;
3262}
3263
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003264static int em_in(struct x86_emulate_ctxt *ctxt)
3265{
3266 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3267 &ctxt->dst.val))
3268 return X86EMUL_IO_NEEDED;
3269
3270 return X86EMUL_CONTINUE;
3271}
3272
3273static int em_out(struct x86_emulate_ctxt *ctxt)
3274{
3275 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3276 &ctxt->src.val, 1);
3277 /* Disable writeback. */
3278 ctxt->dst.type = OP_NONE;
3279 return X86EMUL_CONTINUE;
3280}
3281
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003282static int em_cli(struct x86_emulate_ctxt *ctxt)
3283{
3284 if (emulator_bad_iopl(ctxt))
3285 return emulate_gp(ctxt, 0);
3286
3287 ctxt->eflags &= ~X86_EFLAGS_IF;
3288 return X86EMUL_CONTINUE;
3289}
3290
3291static int em_sti(struct x86_emulate_ctxt *ctxt)
3292{
3293 if (emulator_bad_iopl(ctxt))
3294 return emulate_gp(ctxt, 0);
3295
3296 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3297 ctxt->eflags |= X86_EFLAGS_IF;
3298 return X86EMUL_CONTINUE;
3299}
3300
Avi Kivity6d6eede2012-06-07 14:11:36 +03003301static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3302{
3303 u32 eax, ebx, ecx, edx;
3304
Avi Kivitydd856ef2012-08-27 23:46:17 +03003305 eax = reg_read(ctxt, VCPU_REGS_RAX);
3306 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003307 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003308 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3309 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3310 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3311 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003312 return X86EMUL_CONTINUE;
3313}
3314
Paolo Bonzini98f73632013-10-31 11:19:42 +01003315static int em_sahf(struct x86_emulate_ctxt *ctxt)
3316{
3317 u32 flags;
3318
3319 flags = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF;
3320 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3321
3322 ctxt->eflags &= ~0xffUL;
3323 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3324 return X86EMUL_CONTINUE;
3325}
3326
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003327static int em_lahf(struct x86_emulate_ctxt *ctxt)
3328{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003329 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3330 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003331 return X86EMUL_CONTINUE;
3332}
3333
Avi Kivity92998362012-06-13 12:25:06 +03003334static int em_bswap(struct x86_emulate_ctxt *ctxt)
3335{
3336 switch (ctxt->op_bytes) {
3337#ifdef CONFIG_X86_64
3338 case 8:
3339 asm("bswap %0" : "+r"(ctxt->dst.val));
3340 break;
3341#endif
3342 default:
3343 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3344 break;
3345 }
3346 return X86EMUL_CONTINUE;
3347}
3348
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003349static bool valid_cr(int nr)
3350{
3351 switch (nr) {
3352 case 0:
3353 case 2 ... 4:
3354 case 8:
3355 return true;
3356 default:
3357 return false;
3358 }
3359}
3360
3361static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3362{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003363 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003364 return emulate_ud(ctxt);
3365
3366 return X86EMUL_CONTINUE;
3367}
3368
3369static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3370{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003371 u64 new_val = ctxt->src.val64;
3372 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003373 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003374
3375 static u64 cr_reserved_bits[] = {
3376 0xffffffff00000000ULL,
3377 0, 0, 0, /* CR3 checked later */
3378 CR4_RESERVED_BITS,
3379 0, 0, 0,
3380 CR8_RESERVED_BITS,
3381 };
3382
3383 if (!valid_cr(cr))
3384 return emulate_ud(ctxt);
3385
3386 if (new_val & cr_reserved_bits[cr])
3387 return emulate_gp(ctxt, 0);
3388
3389 switch (cr) {
3390 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003391 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003392 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3393 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3394 return emulate_gp(ctxt, 0);
3395
Avi Kivity717746e2011-04-20 13:37:53 +03003396 cr4 = ctxt->ops->get_cr(ctxt, 4);
3397 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003398
3399 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3400 !(cr4 & X86_CR4_PAE))
3401 return emulate_gp(ctxt, 0);
3402
3403 break;
3404 }
3405 case 3: {
3406 u64 rsvd = 0;
3407
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003408 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3409 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003410 rsvd = CR3_L_MODE_RESERVED_BITS;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003411
3412 if (new_val & rsvd)
3413 return emulate_gp(ctxt, 0);
3414
3415 break;
3416 }
3417 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003418 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003419
3420 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3421 return emulate_gp(ctxt, 0);
3422
3423 break;
3424 }
3425 }
3426
3427 return X86EMUL_CONTINUE;
3428}
3429
Joerg Roedel3b88e412011-04-04 12:39:29 +02003430static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3431{
3432 unsigned long dr7;
3433
Avi Kivity717746e2011-04-20 13:37:53 +03003434 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003435
3436 /* Check if DR7.Global_Enable is set */
3437 return dr7 & (1 << 13);
3438}
3439
3440static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3441{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003442 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003443 u64 cr4;
3444
3445 if (dr > 7)
3446 return emulate_ud(ctxt);
3447
Avi Kivity717746e2011-04-20 13:37:53 +03003448 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003449 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3450 return emulate_ud(ctxt);
3451
3452 if (check_dr7_gd(ctxt))
3453 return emulate_db(ctxt);
3454
3455 return X86EMUL_CONTINUE;
3456}
3457
3458static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3459{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003460 u64 new_val = ctxt->src.val64;
3461 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003462
3463 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3464 return emulate_gp(ctxt, 0);
3465
3466 return check_dr_read(ctxt);
3467}
3468
Joerg Roedel01de8b02011-04-04 12:39:31 +02003469static int check_svme(struct x86_emulate_ctxt *ctxt)
3470{
3471 u64 efer;
3472
Avi Kivity717746e2011-04-20 13:37:53 +03003473 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003474
3475 if (!(efer & EFER_SVME))
3476 return emulate_ud(ctxt);
3477
3478 return X86EMUL_CONTINUE;
3479}
3480
3481static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3482{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003483 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003484
3485 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003486 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003487 return emulate_gp(ctxt, 0);
3488
3489 return check_svme(ctxt);
3490}
3491
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003492static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3493{
Avi Kivity717746e2011-04-20 13:37:53 +03003494 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003495
Avi Kivity717746e2011-04-20 13:37:53 +03003496 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003497 return emulate_ud(ctxt);
3498
3499 return X86EMUL_CONTINUE;
3500}
3501
Joerg Roedel80612522011-04-04 12:39:33 +02003502static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3503{
Avi Kivity717746e2011-04-20 13:37:53 +03003504 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003505 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003506
Avi Kivity717746e2011-04-20 13:37:53 +03003507 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03003508 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02003509 return emulate_gp(ctxt, 0);
3510
3511 return X86EMUL_CONTINUE;
3512}
3513
Joerg Roedelf6511932011-04-04 12:39:35 +02003514static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3515{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003516 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3517 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003518 return emulate_gp(ctxt, 0);
3519
3520 return X86EMUL_CONTINUE;
3521}
3522
3523static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3524{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003525 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3526 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003527 return emulate_gp(ctxt, 0);
3528
3529 return X86EMUL_CONTINUE;
3530}
3531
Avi Kivity73fba5f2010-07-29 15:11:53 +03003532#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003533#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
3534#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
3535 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003536#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003537#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003538#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3539#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003540#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003541#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003542#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003543#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003544 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003545#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01003546 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
3547 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003548#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003549
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003550#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003551#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003552#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003553#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003554#define I2bvIP(_f, _e, _i, _p) \
3555 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003556
Avi Kivityfb864fb2013-01-04 16:18:54 +02003557#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3558 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3559 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003560
Nadav Amit0f54a322014-08-29 11:26:55 +03003561static const struct opcode group7_rm0[] = {
3562 N,
3563 I(SrcNone | Priv | EmulateOnUD, em_vmcall),
3564 N, N, N, N, N, N,
3565};
3566
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003567static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003568 DI(SrcNone | Priv, monitor),
3569 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003570 N, N, N, N, N, N,
3571};
3572
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003573static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003574 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003575 II(SrcNone | Prot | EmulateOnUD, em_vmmcall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003576 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3577 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3578 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3579 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3580 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3581 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003582};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003583
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003584static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003585 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003586 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003587 N, N, N, N, N, N,
3588};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003589
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003590static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003591 F(Lock, em_add),
3592 F(Lock | PageTable, em_or),
3593 F(Lock, em_adc),
3594 F(Lock, em_sbb),
3595 F(Lock | PageTable, em_and),
3596 F(Lock, em_sub),
3597 F(Lock, em_xor),
3598 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003599};
3600
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003601static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003602 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003603};
3604
Avi Kivity007a3b52013-01-19 19:51:51 +02003605static const struct opcode group2[] = {
3606 F(DstMem | ModRM, em_rol),
3607 F(DstMem | ModRM, em_ror),
3608 F(DstMem | ModRM, em_rcl),
3609 F(DstMem | ModRM, em_rcr),
3610 F(DstMem | ModRM, em_shl),
3611 F(DstMem | ModRM, em_shr),
3612 F(DstMem | ModRM, em_shl),
3613 F(DstMem | ModRM, em_sar),
3614};
3615
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003616static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003617 F(DstMem | SrcImm | NoWrite, em_test),
3618 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003619 F(DstMem | SrcNone | Lock, em_not),
3620 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02003621 F(DstXacc | Src2Mem, em_mul_ex),
3622 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02003623 F(DstXacc | Src2Mem, em_div_ex),
3624 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003625};
3626
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003627static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003628 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3629 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003630 N, N, N, N, N, N,
3631};
3632
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003633static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003634 F(DstMem | SrcNone | Lock, em_inc),
3635 F(DstMem | SrcNone | Lock, em_dec),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003636 I(SrcMem | Stack, em_grp45),
3637 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3638 I(SrcMem | Stack, em_grp45),
3639 I(SrcMemFAddr | ImplicitOps, em_grp45),
Gleb Natapov188424b2013-04-11 12:32:14 +03003640 I(SrcMem | Stack, em_grp45), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003641};
3642
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003643static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003644 DI(Prot, sldt),
3645 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003646 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003647 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003648 N, N, N, N,
3649};
3650
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003651static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03003652 II(Mov | DstMem, em_sgdt, sgdt),
3653 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003654 II(SrcMem | Priv, em_lgdt, lgdt),
3655 II(SrcMem | Priv, em_lidt, lidt),
3656 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3657 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3658 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003659}, {
Nadav Amit0f54a322014-08-29 11:26:55 +03003660 EXT(0, group7_rm0),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003661 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003662 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003663 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3664 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3665 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003666} };
3667
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003668static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003669 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003670 F(DstMem | SrcImmByte | NoWrite, em_bt),
3671 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3672 F(DstMem | SrcImmByte | Lock, em_btr),
3673 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003674};
3675
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003676static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003677 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003678}, {
3679 N, N, N, N, N, N, N, N,
3680} };
3681
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003682static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003683 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003684 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003685};
3686
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003687static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003688 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003689};
3690
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003691static const struct gprefix pfx_vmovntpx = {
Avi Kivity3e114eb2012-04-09 18:40:01 +03003692 I(0, em_mov), N, N, N,
3693};
3694
Igor Mammedov27ce8252014-03-15 21:01:59 +01003695static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01003696 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003697};
3698
Alex Williamson0a370272014-07-11 11:56:31 -06003699static const struct gprefix pfx_0f_e7 = {
3700 N, I(Sse, em_mov), N, N,
3701};
3702
Gleb Natapov045a2822012-12-20 16:57:43 +02003703static const struct escape escape_d9 = { {
3704 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3705}, {
3706 /* 0xC0 - 0xC7 */
3707 N, N, N, N, N, N, N, N,
3708 /* 0xC8 - 0xCF */
3709 N, N, N, N, N, N, N, N,
3710 /* 0xD0 - 0xC7 */
3711 N, N, N, N, N, N, N, N,
3712 /* 0xD8 - 0xDF */
3713 N, N, N, N, N, N, N, N,
3714 /* 0xE0 - 0xE7 */
3715 N, N, N, N, N, N, N, N,
3716 /* 0xE8 - 0xEF */
3717 N, N, N, N, N, N, N, N,
3718 /* 0xF0 - 0xF7 */
3719 N, N, N, N, N, N, N, N,
3720 /* 0xF8 - 0xFF */
3721 N, N, N, N, N, N, N, N,
3722} };
3723
3724static const struct escape escape_db = { {
3725 N, N, N, N, N, N, N, N,
3726}, {
3727 /* 0xC0 - 0xC7 */
3728 N, N, N, N, N, N, N, N,
3729 /* 0xC8 - 0xCF */
3730 N, N, N, N, N, N, N, N,
3731 /* 0xD0 - 0xC7 */
3732 N, N, N, N, N, N, N, N,
3733 /* 0xD8 - 0xDF */
3734 N, N, N, N, N, N, N, N,
3735 /* 0xE0 - 0xE7 */
3736 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3737 /* 0xE8 - 0xEF */
3738 N, N, N, N, N, N, N, N,
3739 /* 0xF0 - 0xF7 */
3740 N, N, N, N, N, N, N, N,
3741 /* 0xF8 - 0xFF */
3742 N, N, N, N, N, N, N, N,
3743} };
3744
3745static const struct escape escape_dd = { {
3746 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3747}, {
3748 /* 0xC0 - 0xC7 */
3749 N, N, N, N, N, N, N, N,
3750 /* 0xC8 - 0xCF */
3751 N, N, N, N, N, N, N, N,
3752 /* 0xD0 - 0xC7 */
3753 N, N, N, N, N, N, N, N,
3754 /* 0xD8 - 0xDF */
3755 N, N, N, N, N, N, N, N,
3756 /* 0xE0 - 0xE7 */
3757 N, N, N, N, N, N, N, N,
3758 /* 0xE8 - 0xEF */
3759 N, N, N, N, N, N, N, N,
3760 /* 0xF0 - 0xF7 */
3761 N, N, N, N, N, N, N, N,
3762 /* 0xF8 - 0xFF */
3763 N, N, N, N, N, N, N, N,
3764} };
3765
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003766static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003767 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003768 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003769 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3770 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003771 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003772 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003773 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3774 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003775 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003776 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003777 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3778 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003779 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003780 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003781 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3782 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003783 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003784 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003785 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003786 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003787 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003788 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003789 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003790 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003791 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02003792 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003793 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003794 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003795 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003796 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003797 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003798 I(ImplicitOps | Stack | No64, em_pusha),
3799 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003800 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3801 N, N, N, N,
3802 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003803 I(SrcImm | Mov | Stack, em_push),
3804 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003805 I(SrcImmByte | Mov | Stack, em_push),
3806 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003807 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003808 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003809 /* 0x70 - 0x7F */
3810 X16(D(SrcImmByte)),
3811 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003812 G(ByteOp | DstMem | SrcImm, group1),
3813 G(DstMem | SrcImm, group1),
3814 G(ByteOp | DstMem | SrcImm | No64, group1),
3815 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003816 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003817 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003818 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003819 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003820 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003821 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003822 D(ModRM | SrcMem | NoAccess | DstReg),
3823 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3824 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003825 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003826 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003827 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003828 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003829 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003830 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01003831 II(ImplicitOps | Stack, em_popf, popf),
3832 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003833 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003834 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003835 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003836 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003837 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003838 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003839 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003840 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3841 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003842 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003843 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003844 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003845 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02003846 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003847 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003848 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003849 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003850 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003851 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3852 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003853 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003854 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003855 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Bruce Rogers32611072013-09-09 09:40:20 -06003856 I(ImplicitOps | Stack | SrcImmU16, em_ret_far_imm),
3857 I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003858 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003859 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003860 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003861 G(Src2One | ByteOp, group2), G(Src2One, group2),
3862 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003863 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02003864 I(DstAcc | SrcImmUByte | No64, em_aad),
3865 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02003866 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003867 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02003868 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003869 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003870 X3(I(SrcImmByte, em_loop)),
3871 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003872 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3873 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003874 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003875 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003876 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003877 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3878 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003879 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003880 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003881 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3882 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003883 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003884 D(ImplicitOps), D(ImplicitOps),
3885 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003886 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3887};
3888
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003889static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003890 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003891 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02003892 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003893 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003894 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003895 N, D(ImplicitOps | ModRM), N, N,
3896 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02003897 N, N, N, N, N, N, N, N,
3898 D(ImplicitOps | ModRM), N, N, N, N, N, N, D(ImplicitOps | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003899 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03003900 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
3901 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
3902 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
3903 check_cr_write),
3904 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
3905 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003906 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01003907 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
3908 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
3909 N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
Avi Kivity3e114eb2012-04-09 18:40:01 +03003910 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003911 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003912 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02003913 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003914 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02003915 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02003916 I(ImplicitOps | EmulateOnUD, em_sysenter),
3917 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003918 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003919 N, N, N, N, N, N, N, N,
3920 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03003921 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003922 /* 0x50 - 0x5F */
3923 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3924 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003925 N, N, N, N,
3926 N, N, N, N,
3927 N, N, N, N,
3928 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003929 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003930 N, N, N, N,
3931 N, N, N, N,
3932 N, N, N, N,
3933 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003934 /* 0x80 - 0x8F */
3935 X16(D(SrcImm)),
3936 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003937 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003938 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003939 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02003940 II(ImplicitOps, em_cpuid, cpuid),
3941 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02003942 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
3943 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003944 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003945 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003946 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02003947 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02003948 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
3949 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Avi Kivity4d758342013-01-19 19:51:55 +02003950 D(ModRM), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003951 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09003952 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03003953 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02003954 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03003955 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
3956 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003957 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003958 /* 0xB8 - 0xBF */
3959 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003960 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02003961 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
3962 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003963 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03003964 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02003965 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Wei Yongjun92f738a52010-08-17 09:19:34 +08003966 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003967 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03003968 /* 0xC8 - 0xCF */
3969 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003970 /* 0xD0 - 0xDF */
3971 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3972 /* 0xE0 - 0xEF */
Alex Williamson0a370272014-07-11 11:56:31 -06003973 N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
3974 N, N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003975 /* 0xF0 - 0xFF */
3976 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3977};
3978
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003979static const struct gprefix three_byte_0f_38_f0 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01003980 I(DstReg | SrcMem | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003981};
3982
3983static const struct gprefix three_byte_0f_38_f1 = {
Borislav Petkov84cffe42013-10-29 12:54:56 +01003984 I(DstMem | SrcReg | Mov, em_movbe), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01003985};
3986
3987/*
3988 * Insns below are selected by the prefix which indexed by the third opcode
3989 * byte.
3990 */
3991static const struct opcode opcode_map_0f_38[256] = {
3992 /* 0x00 - 0x7f */
3993 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01003994 /* 0x80 - 0xef */
3995 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
3996 /* 0xf0 - 0xf1 */
3997 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f0),
3998 GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f1),
3999 /* 0xf2 - 0xff */
4000 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004001};
4002
Avi Kivity73fba5f2010-07-29 15:11:53 +03004003#undef D
4004#undef N
4005#undef G
4006#undef GD
4007#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004008#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004009#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004010
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004011#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004012#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004013#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004014#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004015#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004016
Avi Kivity9dac77f2011-06-01 15:34:25 +03004017static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004018{
4019 unsigned size;
4020
Avi Kivity9dac77f2011-06-01 15:34:25 +03004021 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004022 if (size == 8)
4023 size = 4;
4024 return size;
4025}
4026
4027static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4028 unsigned size, bool sign_extension)
4029{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004030 int rc = X86EMUL_CONTINUE;
4031
4032 op->type = OP_IMM;
4033 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004034 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004035 /* NB. Immediates are sign-extended as necessary. */
4036 switch (op->bytes) {
4037 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004038 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004039 break;
4040 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004041 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004042 break;
4043 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004044 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004045 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004046 case 8:
4047 op->val = insn_fetch(s64, ctxt);
4048 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004049 }
4050 if (!sign_extension) {
4051 switch (op->bytes) {
4052 case 1:
4053 op->val &= 0xff;
4054 break;
4055 case 2:
4056 op->val &= 0xffff;
4057 break;
4058 case 4:
4059 op->val &= 0xffffffff;
4060 break;
4061 }
4062 }
4063done:
4064 return rc;
4065}
4066
Avi Kivitya99455492011-09-13 10:45:41 +03004067static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4068 unsigned d)
4069{
4070 int rc = X86EMUL_CONTINUE;
4071
4072 switch (d) {
4073 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004074 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03004075 break;
4076 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004077 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03004078 break;
4079 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004080 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004081 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03004082 *op = ctxt->memop;
4083 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004084 if (ctxt->d & BitOp)
Avi Kivitya99455492011-09-13 10:45:41 +03004085 fetch_bit_operand(ctxt);
4086 op->orig_val = op->val;
4087 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004088 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004089 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004090 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03004091 case OpAcc:
4092 op->type = OP_REG;
4093 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004094 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03004095 fetch_register_operand(op);
4096 op->orig_val = op->val;
4097 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004098 case OpAccLo:
4099 op->type = OP_REG;
4100 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4101 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4102 fetch_register_operand(op);
4103 op->orig_val = op->val;
4104 break;
4105 case OpAccHi:
4106 if (ctxt->d & ByteOp) {
4107 op->type = OP_NONE;
4108 break;
4109 }
4110 op->type = OP_REG;
4111 op->bytes = ctxt->op_bytes;
4112 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4113 fetch_register_operand(op);
4114 op->orig_val = op->val;
4115 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004116 case OpDI:
4117 op->type = OP_MEM;
4118 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4119 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004120 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya99455492011-09-13 10:45:41 +03004121 op->addr.mem.seg = VCPU_SREG_ES;
4122 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004123 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03004124 break;
4125 case OpDX:
4126 op->type = OP_REG;
4127 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004128 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03004129 fetch_register_operand(op);
4130 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004131 case OpCL:
4132 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004133 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004134 break;
4135 case OpImmByte:
4136 rc = decode_imm(ctxt, op, 1, true);
4137 break;
4138 case OpOne:
4139 op->bytes = 1;
4140 op->val = 1;
4141 break;
4142 case OpImm:
4143 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4144 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004145 case OpImm64:
4146 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4147 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004148 case OpMem8:
4149 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004150 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004151 ctxt->memop.addr.reg = decode_register(ctxt,
4152 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004153 fetch_register_operand(&ctxt->memop);
4154 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004155 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004156 case OpMem16:
4157 ctxt->memop.bytes = 2;
4158 goto mem_common;
4159 case OpMem32:
4160 ctxt->memop.bytes = 4;
4161 goto mem_common;
4162 case OpImmU16:
4163 rc = decode_imm(ctxt, op, 2, false);
4164 break;
4165 case OpImmU:
4166 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4167 break;
4168 case OpSI:
4169 op->type = OP_MEM;
4170 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4171 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004172 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Bandan Das573e80f2014-04-16 12:46:13 -04004173 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03004174 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004175 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004176 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004177 case OpXLat:
4178 op->type = OP_MEM;
4179 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4180 op->addr.mem.ea =
4181 register_address(ctxt,
4182 reg_read(ctxt, VCPU_REGS_RBX) +
4183 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04004184 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004185 op->val = 0;
4186 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004187 case OpImmFAddr:
4188 op->type = OP_IMM;
4189 op->addr.mem.ea = ctxt->_eip;
4190 op->bytes = ctxt->op_bytes + 2;
4191 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4192 break;
4193 case OpMemFAddr:
4194 ctxt->memop.bytes = ctxt->op_bytes + 2;
4195 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004196 case OpES:
4197 op->val = VCPU_SREG_ES;
4198 break;
4199 case OpCS:
4200 op->val = VCPU_SREG_CS;
4201 break;
4202 case OpSS:
4203 op->val = VCPU_SREG_SS;
4204 break;
4205 case OpDS:
4206 op->val = VCPU_SREG_DS;
4207 break;
4208 case OpFS:
4209 op->val = VCPU_SREG_FS;
4210 break;
4211 case OpGS:
4212 op->val = VCPU_SREG_GS;
4213 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004214 case OpImplicit:
4215 /* Special instructions do their own operand decoding. */
4216 default:
4217 op->type = OP_NONE; /* Disable writeback. */
4218 break;
4219 }
4220
4221done:
4222 return rc;
4223}
4224
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004225int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004226{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004227 int rc = X86EMUL_CONTINUE;
4228 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004229 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004230 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04004231 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03004232 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004233
Avi Kivityf09ed832011-09-13 10:45:40 +03004234 ctxt->memop.type = OP_NONE;
4235 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004236 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02004237 ctxt->fetch.ptr = ctxt->fetch.data;
4238 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004239 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01004240 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004241 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004242 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02004243 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02004244 if (rc != X86EMUL_CONTINUE)
4245 return rc;
4246 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004247
4248 switch (mode) {
4249 case X86EMUL_MODE_REAL:
4250 case X86EMUL_MODE_VM86:
4251 case X86EMUL_MODE_PROT16:
4252 def_op_bytes = def_ad_bytes = 2;
4253 break;
4254 case X86EMUL_MODE_PROT32:
4255 def_op_bytes = def_ad_bytes = 4;
4256 break;
4257#ifdef CONFIG_X86_64
4258 case X86EMUL_MODE_PROT64:
4259 def_op_bytes = 4;
4260 def_ad_bytes = 8;
4261 break;
4262#endif
4263 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004264 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004265 }
4266
Avi Kivity9dac77f2011-06-01 15:34:25 +03004267 ctxt->op_bytes = def_op_bytes;
4268 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004269
4270 /* Legacy prefixes. */
4271 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004272 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004273 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004274 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004275 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004276 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004277 break;
4278 case 0x67: /* address-size override */
4279 if (mode == X86EMUL_MODE_PROT64)
4280 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004281 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004282 else
4283 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004284 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004285 break;
4286 case 0x26: /* ES override */
4287 case 0x2e: /* CS override */
4288 case 0x36: /* SS override */
4289 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004290 has_seg_override = true;
4291 ctxt->seg_override = (ctxt->b >> 3) & 3;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004292 break;
4293 case 0x64: /* FS override */
4294 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04004295 has_seg_override = true;
4296 ctxt->seg_override = ctxt->b & 7;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004297 break;
4298 case 0x40 ... 0x4f: /* REX */
4299 if (mode != X86EMUL_MODE_PROT64)
4300 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004301 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004302 continue;
4303 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004304 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004305 break;
4306 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004307 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004308 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004309 break;
4310 default:
4311 goto done_prefixes;
4312 }
4313
4314 /* Any legacy prefix after a REX prefix nullifies its effect. */
4315
Avi Kivity9dac77f2011-06-01 15:34:25 +03004316 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004317 }
4318
4319done_prefixes:
4320
4321 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004322 if (ctxt->rex_prefix & 8)
4323 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004324
4325 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004326 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004327 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004328 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004329 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004330 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004331 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004332
4333 /* 0F_38 opcode map */
4334 if (ctxt->b == 0x38) {
4335 ctxt->opcode_len = 3;
4336 ctxt->b = insn_fetch(u8, ctxt);
4337 opcode = opcode_map_0f_38[ctxt->b];
4338 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004339 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004340 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004341
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004342 if (ctxt->d & ModRM)
4343 ctxt->modrm = insn_fetch(u8, ctxt);
4344
Nadav Amit7fe864d2014-06-02 18:34:03 +03004345 /* vex-prefix instructions are not implemented */
4346 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
4347 (mode == X86EMUL_MODE_PROT64 ||
4348 (mode >= X86EMUL_MODE_PROT16 && (ctxt->modrm & 0x80)))) {
4349 ctxt->d = NotImpl;
4350 }
4351
Avi Kivity9dac77f2011-06-01 15:34:25 +03004352 while (ctxt->d & GroupMask) {
4353 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004354 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004355 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004356 opcode = opcode.u.group[goffset];
4357 break;
4358 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004359 goffset = (ctxt->modrm >> 3) & 7;
4360 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004361 opcode = opcode.u.gdual->mod3[goffset];
4362 else
4363 opcode = opcode.u.gdual->mod012[goffset];
4364 break;
4365 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004366 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004367 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004368 break;
4369 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004370 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004371 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004372 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004373 switch (simd_prefix) {
4374 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4375 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4376 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4377 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4378 }
4379 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004380 case Escape:
4381 if (ctxt->modrm > 0xbf)
4382 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4383 else
4384 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4385 break;
Avi Kivity46561642011-04-24 14:09:59 +03004386 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004387 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004388 }
Avi Kivity46561642011-04-24 14:09:59 +03004389
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004390 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004391 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004392 }
4393
Paolo Bonzinie24186e2014-03-27 12:00:57 +01004394 /* Unrecognised? */
4395 if (ctxt->d == 0)
4396 return EMULATION_FAILED;
4397
Avi Kivity9dac77f2011-06-01 15:34:25 +03004398 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004399
Nadav Amit3a6095a2014-08-13 16:50:13 +03004400 if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD)))
4401 return EMULATION_FAILED;
4402
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004403 if (unlikely(ctxt->d &
Nadav Amit3a6095a2014-08-13 16:50:13 +03004404 (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm))) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004405 /*
4406 * These are copied unconditionally here, and checked unconditionally
4407 * in x86_emulate_insn.
4408 */
4409 ctxt->check_perm = opcode.check_perm;
4410 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004411
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004412 if (ctxt->d & NotImpl)
4413 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004414
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004415 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004416 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004417
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004418 if (ctxt->d & Op3264) {
4419 if (mode == X86EMUL_MODE_PROT64)
4420 ctxt->op_bytes = 8;
4421 else
4422 ctxt->op_bytes = 4;
4423 }
4424
4425 if (ctxt->d & Sse)
4426 ctxt->op_bytes = 16;
4427 else if (ctxt->d & Mmx)
4428 ctxt->op_bytes = 8;
4429 }
Avi Kivity1253791d2011-03-29 11:41:27 +02004430
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004431 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004432 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004433 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04004434 if (!has_seg_override) {
4435 has_seg_override = true;
4436 ctxt->seg_override = ctxt->modrm_seg;
4437 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004438 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004439 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004440 if (rc != X86EMUL_CONTINUE)
4441 goto done;
4442
Bandan Das573e80f2014-04-16 12:46:13 -04004443 if (!has_seg_override)
4444 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004445
Bandan Das573e80f2014-04-16 12:46:13 -04004446 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004447
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004448 /*
4449 * Decode and fetch the source operand: register, memory
4450 * or immediate.
4451 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004452 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004453 if (rc != X86EMUL_CONTINUE)
4454 goto done;
4455
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004456 /*
4457 * Decode and fetch the second source operand: register, memory
4458 * or immediate.
4459 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004460 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004461 if (rc != X86EMUL_CONTINUE)
4462 goto done;
4463
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004464 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03004465 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004466
4467done:
Bandan Das41061cd2014-04-16 12:46:14 -04004468 if (ctxt->rip_relative)
Avi Kivityf09ed832011-09-13 10:45:40 +03004469 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004470
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004471 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004472}
4473
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004474bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4475{
4476 return ctxt->d & PageTable;
4477}
4478
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004479static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4480{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004481 /* The second termination condition only applies for REPE
4482 * and REPNE. Test if the repeat string operation prefix is
4483 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4484 * corresponding termination condition according to:
4485 * - if REPE/REPZ and ZF = 0 then done
4486 * - if REPNE/REPNZ and ZF = 1 then done
4487 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004488 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4489 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4490 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004491 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004492 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004493 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4494 return true;
4495
4496 return false;
4497}
4498
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004499static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4500{
4501 bool fault = false;
4502
4503 ctxt->ops->get_fpu(ctxt);
4504 asm volatile("1: fwait \n\t"
4505 "2: \n\t"
4506 ".pushsection .fixup,\"ax\" \n\t"
4507 "3: \n\t"
4508 "movb $1, %[fault] \n\t"
4509 "jmp 2b \n\t"
4510 ".popsection \n\t"
4511 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004512 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004513 ctxt->ops->put_fpu(ctxt);
4514
4515 if (unlikely(fault))
4516 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4517
4518 return X86EMUL_CONTINUE;
4519}
4520
4521static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4522 struct operand *op)
4523{
4524 if (op->type == OP_MM)
4525 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4526}
4527
Avi Kivitye28bbd42013-01-04 16:18:48 +02004528static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4529{
4530 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivityb9fa4092013-02-09 11:31:48 +02004531 if (!(ctxt->d & ByteOp))
4532 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Avi Kivitye28bbd42013-01-04 16:18:48 +02004533 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004534 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
4535 [fastop]"+S"(fop)
4536 : "c"(ctxt->src2.val));
Avi Kivitye28bbd42013-01-04 16:18:48 +02004537 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004538 if (!fop) /* exception is returned in fop variable */
4539 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02004540 return X86EMUL_CONTINUE;
4541}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004542
Bandan Das14985072014-04-16 12:46:09 -04004543void init_decode_cache(struct x86_emulate_ctxt *ctxt)
4544{
Bandan Das573e80f2014-04-16 12:46:13 -04004545 memset(&ctxt->rip_relative, 0,
4546 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04004547
Bandan Das14985072014-04-16 12:46:09 -04004548 ctxt->io_read.pos = 0;
4549 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04004550 ctxt->mem_read.end = 0;
4551}
4552
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004553int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004554{
Mathias Krause0225fb52012-08-30 01:30:16 +02004555 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004556 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004557 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004558
Avi Kivity9dac77f2011-06-01 15:34:25 +03004559 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004560
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004561 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004562 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004563 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004564 goto done;
4565 }
4566
Avi Kivity9dac77f2011-06-01 15:34:25 +03004567 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004568 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004569 goto done;
4570 }
4571
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004572 if (unlikely(ctxt->d &
4573 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
4574 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4575 (ctxt->d & Undefined)) {
4576 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004577 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004578 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004579
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004580 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4581 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
4582 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004583 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004584 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004585
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004586 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
4587 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004588 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004589 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004590
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004591 if (ctxt->d & Mmx) {
4592 rc = flush_pending_x87_faults(ctxt);
4593 if (rc != X86EMUL_CONTINUE)
4594 goto done;
4595 /*
4596 * Now that we know the fpu is exception safe, we can fetch
4597 * operands from it.
4598 */
4599 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4600 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4601 if (!(ctxt->d & Mov))
4602 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4603 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004604
Bandan Das685bbf42014-04-16 12:46:10 -04004605 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004606 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4607 X86_ICPT_PRE_EXCEPT);
4608 if (rc != X86EMUL_CONTINUE)
4609 goto done;
4610 }
4611
4612 /* Privileged instruction can be executed only in CPL=0 */
4613 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Nadav Amit68efa762014-06-18 17:19:35 +03004614 if (ctxt->d & PrivUD)
4615 rc = emulate_ud(ctxt);
4616 else
4617 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004618 goto done;
4619 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004620
4621 /* Instruction can only be executed in protected mode */
4622 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
4623 rc = emulate_ud(ctxt);
4624 goto done;
4625 }
4626
4627 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04004628 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004629 rc = ctxt->check_perm(ctxt);
4630 if (rc != X86EMUL_CONTINUE)
4631 goto done;
4632 }
4633
Bandan Das685bbf42014-04-16 12:46:10 -04004634 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004635 rc = emulator_check_intercept(ctxt, ctxt->intercept,
4636 X86_ICPT_POST_EXCEPT);
4637 if (rc != X86EMUL_CONTINUE)
4638 goto done;
4639 }
4640
4641 if (ctxt->rep_prefix && (ctxt->d & String)) {
4642 /* All REP prefixes have the same first termination condition */
4643 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
4644 ctxt->eip = ctxt->_eip;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004645 ctxt->eflags &= ~EFLG_RF;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004646 goto done;
4647 }
4648 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004649 }
4650
Avi Kivity9dac77f2011-06-01 15:34:25 +03004651 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4652 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4653 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004654 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004655 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004656 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004657 }
4658
Avi Kivity9dac77f2011-06-01 15:34:25 +03004659 if (ctxt->src2.type == OP_MEM) {
4660 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4661 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004662 if (rc != X86EMUL_CONTINUE)
4663 goto done;
4664 }
4665
Avi Kivity9dac77f2011-06-01 15:34:25 +03004666 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004667 goto special_insn;
4668
4669
Avi Kivity9dac77f2011-06-01 15:34:25 +03004670 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004671 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004672 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4673 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004674 if (rc != X86EMUL_CONTINUE)
4675 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004676 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004677 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004678
Avi Kivity018a98d2007-11-27 19:30:56 +02004679special_insn:
4680
Bandan Das685bbf42014-04-16 12:46:10 -04004681 if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004682 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004683 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004684 if (rc != X86EMUL_CONTINUE)
4685 goto done;
4686 }
4687
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004688 if (ctxt->rep_prefix && (ctxt->d & String))
4689 ctxt->eflags |= EFLG_RF;
4690 else
4691 ctxt->eflags &= ~EFLG_RF;
Nadav Amit4467c3f2014-07-21 14:37:29 +03004692
Avi Kivity9dac77f2011-06-01 15:34:25 +03004693 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004694 if (ctxt->d & Fastop) {
4695 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4696 rc = fastop(ctxt, fop);
4697 if (rc != X86EMUL_CONTINUE)
4698 goto done;
4699 goto writeback;
4700 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004701 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004702 if (rc != X86EMUL_CONTINUE)
4703 goto done;
4704 goto writeback;
4705 }
4706
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02004707 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004708 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004709 else if (ctxt->opcode_len == 3)
4710 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004711
Avi Kivity9dac77f2011-06-01 15:34:25 +03004712 switch (ctxt->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004713 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004714 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004715 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004716 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004717 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004718 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004719 if (test_cc(ctxt->b, ctxt->eflags))
4720 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004721 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004722 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004723 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004724 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004725 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004726 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03004727 ctxt->dst.type = OP_NONE;
4728 else
4729 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004730 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004731 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004732 switch (ctxt->op_bytes) {
4733 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4734 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4735 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004736 }
4737 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004738 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004739 rc = emulate_int(ctxt, 3);
4740 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004741 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004742 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004743 break;
4744 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004745 if (ctxt->eflags & EFLG_OF)
4746 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004747 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004748 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004749 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004750 jmp_rel(ctxt, ctxt->src.val);
4751 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004752 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004753 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004754 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004755 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004756 case 0xf5: /* cmc */
4757 /* complement carry flag from eflags reg */
4758 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004759 break;
4760 case 0xf8: /* clc */
4761 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004762 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004763 case 0xf9: /* stc */
4764 ctxt->eflags |= EFLG_CF;
4765 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004766 case 0xfc: /* cld */
4767 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004768 break;
4769 case 0xfd: /* std */
4770 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004771 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004772 default:
4773 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004774 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004775
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004776 if (rc != X86EMUL_CONTINUE)
4777 goto done;
4778
Avi Kivity018a98d2007-11-27 19:30:56 +02004779writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02004780 if (ctxt->d & SrcWrite) {
4781 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
4782 rc = writeback(ctxt, &ctxt->src);
4783 if (rc != X86EMUL_CONTINUE)
4784 goto done;
4785 }
Nadav Amitee212292014-06-15 16:12:58 +03004786 if (!(ctxt->d & NoWrite)) {
4787 rc = writeback(ctxt, &ctxt->dst);
4788 if (rc != X86EMUL_CONTINUE)
4789 goto done;
4790 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004791
Gleb Natapov5cd21912010-03-18 15:20:26 +02004792 /*
4793 * restore dst type in case the decoding will be reused
4794 * (happens for string instruction )
4795 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004796 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004797
Avi Kivity9dac77f2011-06-01 15:34:25 +03004798 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004799 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004800
Avi Kivity9dac77f2011-06-01 15:34:25 +03004801 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004802 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004803
Avi Kivity9dac77f2011-06-01 15:34:25 +03004804 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004805 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004806 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004807 if ((ctxt->d & SrcMask) == SrcSI)
4808 count = ctxt->src.count;
4809 else
4810 count = ctxt->dst.count;
4811 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4812 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004813
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004814 if (!string_insn_completed(ctxt)) {
4815 /*
4816 * Re-enter guest when pio read ahead buffer is empty
4817 * or, if it is not used, after each 1024 iteration.
4818 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004819 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004820 (r->end == 0 || r->end != r->pos)) {
4821 /*
4822 * Reset read cache. Usually happens before
4823 * decode, but since instruction is restarted
4824 * we have to do it here.
4825 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004826 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004827 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004828 return EMULATION_RESTART;
4829 }
4830 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004831 }
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03004832 ctxt->eflags &= ~EFLG_RF;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004833 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004834
Avi Kivity9dac77f2011-06-01 15:34:25 +03004835 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004836
4837done:
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02004838 if (rc == X86EMUL_PROPAGATE_FAULT) {
4839 WARN_ON(ctxt->exception.vector > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +02004840 ctxt->have_exception = true;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02004841 }
Joerg Roedel775fde82011-04-04 12:39:24 +02004842 if (rc == X86EMUL_INTERCEPTED)
4843 return EMULATION_INTERCEPTED;
4844
Avi Kivitydd856ef2012-08-27 23:46:17 +03004845 if (rc == X86EMUL_CONTINUE)
4846 writeback_registers(ctxt);
4847
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004848 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004849
4850twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004851 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004852 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004853 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004854 break;
4855 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004856 case 0x0d: /* GrpP (prefetch) */
4857 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004858 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02004859 break;
4860 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004861 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004862 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004863 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004864 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004865 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004866 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03004867 if (test_cc(ctxt->b, ctxt->eflags))
4868 ctxt->dst.val = ctxt->src.val;
4869 else if (ctxt->mode != X86EMUL_MODE_PROT64 ||
4870 ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004871 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004872 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004873 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004874 if (test_cc(ctxt->b, ctxt->eflags))
4875 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004876 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004877 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004878 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004879 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004880 case 0xae: /* clflush */
4881 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004882 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004883 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004884 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004885 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004886 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004887 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004888 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004889 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004890 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004891 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004892 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004893 ctxt->dst.bytes = ctxt->op_bytes;
Nadav Amit3b320042014-06-02 18:34:08 +03004894 ctxt->dst.val = (ctxt->op_bytes == 8) ? (u64) ctxt->src.val :
4895 (u32) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004896 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004897 default:
4898 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004899 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004900
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004901threebyte_insn:
4902
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004903 if (rc != X86EMUL_CONTINUE)
4904 goto done;
4905
Avi Kivity6aa8b732006-12-10 02:21:36 -08004906 goto writeback;
4907
4908cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004909 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004910}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004911
4912void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
4913{
4914 invalidate_registers(ctxt);
4915}
4916
4917void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
4918{
4919 writeback_registers(ctxt);
4920}